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:
@@ -1,4 +1,4 @@
|
|||||||
import { eq, desc, asc } from "drizzle-orm";
|
import { eq, desc } from "drizzle-orm";
|
||||||
import type { NextRequest } from "next/server";
|
import type { NextRequest } from "next/server";
|
||||||
import { getSession } from "@/lib/session";
|
import { getSession } from "@/lib/session";
|
||||||
import { appDb, schema } from "@/lib/appdb";
|
import { appDb, schema } from "@/lib/appdb";
|
||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
} from "@/lib/credits";
|
} from "@/lib/credits";
|
||||||
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
|
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
|
||||||
import { anahtarVar, SORUMLULUK_REDDI } from "@/lib/ai/client";
|
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 type { AracBaglami } from "@/lib/ai/araclar";
|
||||||
import { danismanAkisi } from "@/lib/ai/sohbet-akisi";
|
import { danismanAkisi } from "@/lib/ai/sohbet-akisi";
|
||||||
import { sistemPromptu } from "@/lib/ai/danisman-prompt";
|
import { sistemPromptu } from "@/lib/ai/danisman-prompt";
|
||||||
@@ -52,16 +52,7 @@ export async function GET() {
|
|||||||
return Response.json({ error: "Oturum gerekli." }, { status: 401 });
|
return Response.json({ error: "Oturum gerekli." }, { status: 401 });
|
||||||
}
|
}
|
||||||
const [mesajlar, kullanici] = await Promise.all([
|
const [mesajlar, kullanici] = await Promise.all([
|
||||||
appDb
|
sohbetGecmisiGetir(session.user.id),
|
||||||
.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),
|
|
||||||
appDb.query.user.findFirst({
|
appDb.query.user.findFirst({
|
||||||
where: eq(schema.user.id, session.user.id),
|
where: eq(schema.user.id, session.user.id),
|
||||||
columns: { creditBalance: true },
|
columns: { creditBalance: true },
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { asc, eq } from "drizzle-orm";
|
import { desc, eq, sql } from "drizzle-orm";
|
||||||
import { appDb, schema } from "@/lib/appdb";
|
import { appDb, schema } from "@/lib/appdb";
|
||||||
import { anahtarVar } from "@/lib/ai/client";
|
import { anahtarVar } from "@/lib/ai/client";
|
||||||
import type { RaporSonuc } from "./types/rapor";
|
import type { RaporSonuc } from "./types/rapor";
|
||||||
@@ -40,9 +40,13 @@ export async function kullanicininRaporu(userId: string) {
|
|||||||
return satir ?? null;
|
return satir ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Kullanıcının danışman sohbet geçmişi, eskiden yeniye (en fazla 100 mesaj). */
|
/**
|
||||||
export function sohbetGecmisiGetir(userId: string) {
|
* Kullanıcının danışman sohbet geçmişi: en YENİ 100 mesaj, eskiden yeniye.
|
||||||
return appDb
|
* 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({
|
.select({
|
||||||
id: schema.chatMessages.id,
|
id: schema.chatMessages.id,
|
||||||
role: schema.chatMessages.role,
|
role: schema.chatMessages.role,
|
||||||
@@ -50,6 +54,7 @@ export function sohbetGecmisiGetir(userId: string) {
|
|||||||
})
|
})
|
||||||
.from(schema.chatMessages)
|
.from(schema.chatMessages)
|
||||||
.where(eq(schema.chatMessages.userId, userId))
|
.where(eq(schema.chatMessages.userId, userId))
|
||||||
.orderBy(asc(schema.chatMessages.createdAt))
|
.orderBy(desc(schema.chatMessages.createdAt), sql`rowid DESC`)
|
||||||
.limit(100);
|
.limit(100);
|
||||||
|
return satirlar.reverse();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user