fix(danisman): M2 (6) — 100 mesajı aşan geçmişte en yeni 100 dönsün

sohbetGecmisiGetir yeniden eskiye sıralayıp 100 alır, sonra çevirir; aynı
saniyedeki soru-cevap sırası rowid ile korunur. GET /api/soru da aynı
sorguyu kullanır (iki kopya vardı).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
bilalgursen
2026-10-05 01:04:42 +03:00
parent 4e2470e713
commit 1191f16916
2 changed files with 13 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { eq, desc, asc } from "drizzle-orm";
import { eq, desc } from "drizzle-orm";
import type { NextRequest } from "next/server";
import { getSession } from "@/lib/session";
import { appDb, schema } from "@/lib/appdb";
@@ -10,7 +10,7 @@ import {
} from "@/lib/credits";
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
import { anahtarVar, SORUMLULUK_REDDI } from "@/lib/ai/client";
import { kullanicininRaporu } from "@/features/rapor/rapor-queries";
import { kullanicininRaporu, sohbetGecmisiGetir } from "@/features/rapor/rapor-queries";
import type { AracBaglami } from "@/lib/ai/araclar";
import { danismanAkisi } from "@/lib/ai/sohbet-akisi";
import { sistemPromptu } from "@/lib/ai/danisman-prompt";
@@ -52,16 +52,7 @@ export async function GET() {
return Response.json({ error: "Oturum gerekli." }, { status: 401 });
}
const [mesajlar, kullanici] = await Promise.all([
appDb
.select({
id: schema.chatMessages.id,
role: schema.chatMessages.role,
content: schema.chatMessages.content,
})
.from(schema.chatMessages)
.where(eq(schema.chatMessages.userId, session.user.id))
.orderBy(asc(schema.chatMessages.createdAt))
.limit(100),
sohbetGecmisiGetir(session.user.id),
appDb.query.user.findFirst({
where: eq(schema.user.id, session.user.id),
columns: { creditBalance: true },

View File

@@ -1,5 +1,5 @@
import "server-only";
import { asc, eq } from "drizzle-orm";
import { desc, eq, sql } from "drizzle-orm";
import { appDb, schema } from "@/lib/appdb";
import { anahtarVar } from "@/lib/ai/client";
import type { RaporSonuc } from "./types/rapor";
@@ -40,9 +40,13 @@ export async function kullanicininRaporu(userId: string) {
return satir ?? null;
}
/** Kullanıcının danışman sohbet geçmişi, eskiden yeniye (en fazla 100 mesaj). */
export function sohbetGecmisiGetir(userId: string) {
return appDb
/**
* Kullanıcının danışman sohbet geçmişi: en YENİ 100 mesaj, eskiden yeniye.
* created_at saniye çözünürlüklü; aynı saniyedeki soru-cevap sırası rowid ile
* korunur.
*/
export async function sohbetGecmisiGetir(userId: string) {
const satirlar = await appDb
.select({
id: schema.chatMessages.id,
role: schema.chatMessages.role,
@@ -50,6 +54,7 @@ export function sohbetGecmisiGetir(userId: string) {
})
.from(schema.chatMessages)
.where(eq(schema.chatMessages.userId, userId))
.orderBy(asc(schema.chatMessages.createdAt))
.orderBy(desc(schema.chatMessages.createdAt), sql`rowid DESC`)
.limit(100);
return satirlar.reverse();
}