From bd1ef8abf5efbeb61e774a0f9d32b71edc42404a Mon Sep 17 00:00:00 2001 From: m4 Date: Fri, 31 Jul 2026 08:23:39 +0800 Subject: [PATCH] perf(webui): slim the conversation list payload to derived summaries The list route forwarded each thread's full values.messages to the browser (~47MB for 20 threads), blocking the main thread on JSON.parse and making the list appear suddenly after a long wait. The BFF now derives title/description/pinned/needsUserInput server-side via the shared conversationSummary module and never sends values or interrupts; useThreads consumes the slim shape directly. Co-Authored-By: Claude Opus 4.7 --- src/app/api/conversations/route.test.ts | 24 +++++- src/app/api/conversations/route.ts | 13 +++- src/app/hooks/useThreads.ts | 87 +++------------------- src/lib/conversationSummary.test.ts | 97 ++++++++++++++++++++++++ src/lib/conversationSummary.ts | 99 +++++++++++++++++++++++++ 5 files changed, 240 insertions(+), 80 deletions(-) create mode 100644 src/lib/conversationSummary.test.ts create mode 100644 src/lib/conversationSummary.ts diff --git a/src/app/api/conversations/route.test.ts b/src/app/api/conversations/route.test.ts index 2e7dc8f..bf225a7 100644 --- a/src/app/api/conversations/route.test.ts +++ b/src/app/api/conversations/route.test.ts @@ -40,6 +40,12 @@ const SCOPED_THREAD = { workspace_deployment_id: "deployment-a", workspace_status: "active", }, + values: { + messages: [ + { type: "human", content: "scoped hello" }, + { type: "ai", content: "scoped answer" }, + ], + }, }; const LEGACY_THREAD = { @@ -85,8 +91,22 @@ describe("conversations list route", () => { "thread-scoped" ); - const body = (await response.json()) as { threads: unknown[] }; - expect(body.threads).toEqual([SCOPED_THREAD]); + const body = (await response.json()) as { + threads: Array>; + }; + expect(body.threads).toHaveLength(1); + const thread = body.threads[0]; + // The list payload must stay slim: full message state never crosses to + // the browser (47MB for 20 threads), only the derived summary fields. + expect(thread).not.toHaveProperty("values"); + expect(thread).not.toHaveProperty("interrupts"); + expect(thread).toMatchObject({ + thread_id: "thread-scoped", + title: "scoped hello", + description: "scoped answer", + pinned: false, + needsUserInput: false, + }); }); it("skips probing entirely when every visible thread is unscoped", async () => { diff --git a/src/app/api/conversations/route.ts b/src/app/api/conversations/route.ts index 202d333..4a18286 100644 --- a/src/app/api/conversations/route.ts +++ b/src/app/api/conversations/route.ts @@ -8,6 +8,7 @@ import { assertThreadMatchesScope, sanitizeConversationThread, } from "@/lib/server/conversationResponse"; +import { summarizeConversationThread } from "@/lib/conversationSummary"; export const runtime = "nodejs"; @@ -52,7 +53,9 @@ export async function GET(request: NextRequest) { throw new Error("Workspace scope service is unavailable."); } return NextResponse.json({ - threads: visible.map(sanitizeConversationThread), + threads: visible.map(sanitizeConversationThread).map( + summarizeConversationThread + ), }); } const verified = await Promise.all( @@ -74,7 +77,13 @@ export async function GET(request: NextRequest) { } }) ); - return NextResponse.json({ threads: verified.filter(Boolean) }); + return NextResponse.json({ + threads: verified + .filter((thread): thread is NonNullable => + Boolean(thread) + ) + .map(summarizeConversationThread), + }); } catch (error) { return errorResponse(error); } diff --git a/src/app/hooks/useThreads.ts b/src/app/hooks/useThreads.ts index 958365e..87aad73 100644 --- a/src/app/hooks/useThreads.ts +++ b/src/app/hooks/useThreads.ts @@ -1,5 +1,6 @@ import useSWRInfinite from "swr/infinite"; import { patchConversation } from "@/lib/conversationApi"; +import type { ConversationThreadSummary } from "@/lib/conversationSummary"; import type { ThreadModelSelection } from "@/lib/modelRegistry"; export interface ThreadItem { @@ -13,84 +14,18 @@ export interface ThreadItem { needsUserInput: boolean; } -interface ConversationThread { - thread_id: string; - updated_at: string; - status: string; - metadata?: Record; - values?: { messages?: unknown[] }; - interrupts?: Record>; -} - const DEFAULT_PAGE_SIZE = 20; -function contentText(content: unknown): string { - if (typeof content === "string") return content; - if (!Array.isArray(content)) return ""; - return content - .map((part) => - part && typeof part === "object" && typeof (part as { text?: unknown }).text === "string" - ? String((part as { text: string }).text) - : "" - ) - .join(""); -} - -function toThreadItem(thread: ConversationThread): ThreadItem { - let title = "Untitled Thread"; - let description = ""; - const messages = Array.isArray(thread.values?.messages) - ? thread.values?.messages - : []; - for (const message of messages) { - if (!message || typeof message !== "object") continue; - const item = message as { type?: unknown; content?: unknown }; - if (item.type === "human") { - const text = contentText(item.content).trim(); - if (text) { - title = text.slice(0, 50) + (text.length > 50 ? "..." : ""); - break; - } - } - } - for (const message of messages) { - if (!message || typeof message !== "object") continue; - const item = message as { type?: unknown; content?: unknown }; - if (item.type !== "ai") continue; - const text = contentText(item.content).trim(); - if (text) { - description = text.slice(0, 100); - break; - } - } - if (title === "Untitled Thread" && description) { - title = description.slice(0, 50) + (description.length > 50 ? "..." : ""); - } - const metadata = thread.metadata ?? {}; - if (typeof metadata.title === "string" && metadata.title.trim()) { - title = metadata.title.trim(); - } - let needsUserInput = false; - for (const interrupts of Object.values(thread.interrupts ?? {})) { - for (const interrupt of interrupts) { - const value = interrupt?.value as { type?: unknown } | undefined; - if (value?.type === "ask_user") { - needsUserInput = true; - break; - } - } - if (needsUserInput) break; - } +function toThreadItem(summary: ConversationThreadSummary): ThreadItem { return { - id: thread.thread_id, - updatedAt: new Date(thread.updated_at), - status: thread.status, - title, - description, - assistantId: - typeof metadata.assistant_id === "string" ? metadata.assistant_id : undefined, - pinned: metadata.pinned === true, - needsUserInput, + id: summary.thread_id, + updatedAt: new Date(summary.updated_at), + status: summary.status, + title: summary.title, + description: summary.description, + assistantId: summary.assistant_id, + pinned: summary.pinned, + needsUserInput: summary.needsUserInput, }; } @@ -119,7 +54,7 @@ export function useThreads(props: { status?: string; limit?: number }) { cache: "no-store", }); const payload = (await response.json().catch(() => null)) as { - threads?: ConversationThread[]; + threads?: ConversationThreadSummary[]; error?: string; } | null; if (!response.ok) { diff --git a/src/lib/conversationSummary.test.ts b/src/lib/conversationSummary.test.ts new file mode 100644 index 0000000..57e94c6 --- /dev/null +++ b/src/lib/conversationSummary.test.ts @@ -0,0 +1,97 @@ +import { describe, expect, it } from "vitest"; + +import { summarizeConversationThread } from "./conversationSummary"; + +const BASE = { + thread_id: "thread-a", + updated_at: "2026-07-31T01:00:00.000Z", + status: "idle", +}; + +describe("summarizeConversationThread", () => { + it("derives the title from the first human message and truncates at 50 chars", () => { + const summary = summarizeConversationThread({ + ...BASE, + values: { + messages: [ + { type: "human", content: "x".repeat(60) }, + { type: "ai", content: "answer" }, + ], + }, + }); + expect(summary.title).toBe(`${"x".repeat(50)}...`); + expect(summary.description).toBe("answer"); + expect(summary).not.toHaveProperty("values"); + expect(summary).not.toHaveProperty("interrupts"); + }); + + it("reads text parts from structured message content", () => { + const summary = summarizeConversationThread({ + ...BASE, + values: { + messages: [ + { + type: "human", + content: [ + { type: "text", text: "hello " }, + { type: "text", text: "world" }, + ], + }, + ], + }, + }); + expect(summary.title).toBe("hello world"); + }); + + it("falls back to the ai description when no human message has text", () => { + const summary = summarizeConversationThread({ + ...BASE, + values: { + messages: [{ type: "ai", content: "ai opens the thread" }], + }, + }); + expect(summary.title).toBe("ai opens the thread"); + expect(summary.description).toBe("ai opens the thread"); + }); + + it("prefers an explicit metadata title over derived text", () => { + const summary = summarizeConversationThread({ + ...BASE, + metadata: { title: " Renamed ", pinned: true }, + values: { messages: [{ type: "human", content: "ignored" }] }, + }); + expect(summary.title).toBe("Renamed"); + expect(summary.pinned).toBe(true); + }); + + it("returns the untitled placeholder when there are no messages", () => { + const summary = summarizeConversationThread({ ...BASE }); + expect(summary.title).toBe("Untitled Thread"); + expect(summary.description).toBe(""); + expect(summary.pinned).toBe(false); + expect(summary.needsUserInput).toBe(false); + }); + + it("flags threads interrupted by an ask_user question", () => { + const summary = summarizeConversationThread({ + ...BASE, + interrupts: { "task-1": [{ value: { type: "ask_user" } }] }, + }); + expect(summary.needsUserInput).toBe(true); + }); + + it("passes through identity fields and metadata", () => { + const summary = summarizeConversationThread({ + ...BASE, + metadata: { assistant_id: "EvoScientist", scope: "kept" }, + }); + expect(summary.thread_id).toBe("thread-a"); + expect(summary.updated_at).toBe(BASE.updated_at); + expect(summary.status).toBe("idle"); + expect(summary.assistant_id).toBe("EvoScientist"); + expect(summary.metadata).toEqual({ + assistant_id: "EvoScientist", + scope: "kept", + }); + }); +}); diff --git a/src/lib/conversationSummary.ts b/src/lib/conversationSummary.ts new file mode 100644 index 0000000..2debe41 --- /dev/null +++ b/src/lib/conversationSummary.ts @@ -0,0 +1,99 @@ +export interface ConversationThreadSummary { + thread_id: string; + updated_at: string; + status: string; + metadata?: Record; + title: string; + description: string; + pinned: boolean; + needsUserInput: boolean; + assistant_id?: string; +} + +export interface ConversationThreadState { + thread_id: string; + updated_at: string; + status: string; + metadata?: Record | null; + values?: unknown; + interrupts?: unknown; +} + +function contentText(content: unknown): string { + if (typeof content === "string") return content; + if (!Array.isArray(content)) return ""; + return content + .map((part) => + part && + typeof part === "object" && + typeof (part as { text?: unknown }).text === "string" + ? String((part as { text: string }).text) + : "" + ) + .join(""); +} + +export function summarizeConversationThread( + thread: ConversationThreadState +): ConversationThreadSummary { + let title = "Untitled Thread"; + let description = ""; + const values = thread.values as { messages?: unknown[] } | null | undefined; + const messages = Array.isArray(values?.messages) ? values.messages : []; + for (const message of messages) { + if (!message || typeof message !== "object") continue; + const item = message as { type?: unknown; content?: unknown }; + if (item.type === "human") { + const text = contentText(item.content).trim(); + if (text) { + title = text.slice(0, 50) + (text.length > 50 ? "..." : ""); + break; + } + } + } + for (const message of messages) { + if (!message || typeof message !== "object") continue; + const item = message as { type?: unknown; content?: unknown }; + if (item.type !== "ai") continue; + const text = contentText(item.content).trim(); + if (text) { + description = text.slice(0, 100); + break; + } + } + if (title === "Untitled Thread" && description) { + title = description.slice(0, 50) + (description.length > 50 ? "..." : ""); + } + const metadata = thread.metadata ?? {}; + if (typeof metadata.title === "string" && metadata.title.trim()) { + title = metadata.title.trim(); + } + let needsUserInput = false; + const interrupts = (thread.interrupts ?? {}) as Record< + string, + Array<{ value?: unknown }> + >; + for (const entries of Object.values(interrupts)) { + for (const interrupt of entries) { + const value = interrupt?.value as { type?: unknown } | undefined; + if (value?.type === "ask_user") { + needsUserInput = true; + break; + } + } + if (needsUserInput) break; + } + return { + thread_id: thread.thread_id, + updated_at: thread.updated_at, + status: thread.status, + ...(thread.metadata ? { metadata: thread.metadata } : {}), + title, + description, + pinned: metadata.pinned === true, + needsUserInput, + ...(typeof metadata.assistant_id === "string" + ? { assistant_id: metadata.assistant_id } + : {}), + }; +}