fix(web): keep rectification sessions listed and persist chats on first send
Empty-consultation filtering is now session_type scoped so birth-time rows stay in the sidebar. Subtitles use updatedAt with sort/group/cursor. New chats stay local until the first send.
This commit is contained in:
@@ -16,22 +16,9 @@ import {
|
||||
parseSessionCursor,
|
||||
sessionCursorFilter,
|
||||
} from "@/lib/session-cursor";
|
||||
const SESSION_LIST_COLUMNS = "id,title,theme,model_id,session_type,rectification_case_id,chart_profile_id,chart_profile_name,chart_profile_role,created_at,updated_at,pinned,archived_at";
|
||||
import { applyArchiveFilter, excludeEmptyConsultations } from "@/lib/session-list-filter";
|
||||
|
||||
function excludeEmptyConsultations<Query extends {
|
||||
not: (column: string, operator: string, value: unknown) => Query;
|
||||
}>(query: Query): Query {
|
||||
// Empty consultations are `messages = []`. Rectification rows keep an opening
|
||||
// turn, so excluding empty arrays leaves them in the list (BUG-928).
|
||||
return query.not("messages", "eq", []);
|
||||
}
|
||||
|
||||
function applyArchiveFilter<Query extends {
|
||||
is: (column: string, value: null) => Query;
|
||||
not: (column: string, operator: string, value: null) => Query;
|
||||
}>(query: Query, archived: boolean): Query {
|
||||
return archived ? query.not("archived_at", "is", null) : query.is("archived_at", null);
|
||||
}
|
||||
const SESSION_LIST_COLUMNS = "id,title,theme,model_id,session_type,rectification_case_id,chart_profile_id,chart_profile_name,chart_profile_role,updated_at,pinned,archived_at";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
@@ -67,7 +54,6 @@ export async function GET(request: Request) {
|
||||
if (pageError) return NextResponse.json({ error: "聊天记录暂时无法读取" }, { status: 500 });
|
||||
|
||||
let pinnedRows: typeof pageRows = [];
|
||||
let draft: NonNullable<typeof pageRows>[number] | null = null;
|
||||
if (!cursor) {
|
||||
const { data: pinnedData, error: pinnedError } = await excludeEmptyConsultations(
|
||||
applyArchiveFilter(
|
||||
@@ -83,26 +69,12 @@ export async function GET(request: Request) {
|
||||
.order("id", { ascending: false });
|
||||
if (pinnedError) return NextResponse.json({ error: "聊天记录暂时无法读取" }, { status: 500 });
|
||||
pinnedRows = pinnedData ?? [];
|
||||
if (!archived) {
|
||||
const { data: draftData, error: draftError } = await supabase
|
||||
.from("chat_sessions")
|
||||
.select(SESSION_LIST_COLUMNS)
|
||||
.eq("user_id", user.id)
|
||||
.eq("session_type", "consultation")
|
||||
.eq("messages", [])
|
||||
.is("archived_at", null)
|
||||
.order("updated_at", { ascending: false })
|
||||
.order("id", { ascending: false })
|
||||
.limit(1);
|
||||
if (draftError) return NextResponse.json({ error: "聊天记录暂时无法读取" }, { status: 500 });
|
||||
draft = draftData?.[0] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
const nextCursor = nextSessionCursor(pageRows ?? [], limit);
|
||||
const page = (pageRows ?? []).slice(0, limit);
|
||||
const sessions = cursor ? page : [...(pinnedRows ?? []), ...page];
|
||||
return NextResponse.json({ sessions, nextCursor, draft });
|
||||
return NextResponse.json({ sessions, nextCursor });
|
||||
} catch (error) {
|
||||
if (isSupabaseConfigurationError(error)) {
|
||||
return NextResponse.json({ error: "数据库尚未配置", code: "DATABASE_NOT_CONFIGURED" }, { status: 503 });
|
||||
|
||||
Reference in New Issue
Block a user