Fix UI stop button delay by using Next.js after() for background chat summaries

This commit is contained in:
2026-06-15 16:48:40 -07:00
parent 7aa3056f59
commit 74f81f23d0

View File

@@ -14,7 +14,7 @@
* data: {"type":"done"}
* data: {"type":"error","error":"..."}
*/
import { NextResponse } from "next/server";
import { NextResponse, after } from "next/server";
import { requireWorkspacePrincipal } from "@/lib/auth/workspace-auth";
import { query, queryOne } from "@/lib/db-postgres";
import { callVibnChat, streamVibnChat } from "@/lib/ai/vibn-chat-model";
@@ -628,22 +628,21 @@ function extractPreviewUrl(messages: ChatMessage[]): string | undefined {
return undefined;
}
function summarizeForUI(raw: string): string {
try {
const p = JSON.parse(raw);
if (p && typeof p === "object") {
const clone = { ...p };
// Strip massive payload fields so the UI gets intact JSON
if (clone.result && typeof clone.result === 'object') {
if (clone.result && typeof clone.result === "object") {
if (clone.result.log) clone.result.log = "...";
if (clone.result.content) clone.result.content = "...";
if (clone.result.listing) clone.result.listing = "...";
}
if (typeof clone.stdout === 'string' && clone.stdout.length > 200) {
if (typeof clone.stdout === "string" && clone.stdout.length > 200) {
clone.stdout = clone.stdout.slice(0, 200) + "...";
}
if (typeof clone.stderr === 'string' && clone.stderr.length > 200) {
if (typeof clone.stderr === "string" && clone.stderr.length > 200) {
clone.stderr = clone.stderr.slice(0, 200) + "...";
}
return JSON.stringify(clone);
@@ -1898,7 +1897,7 @@ export async function POST(request: Request) {
// Wrapped in try/catch + .catch — the response stream is already
// closed and we don't want a summary failure to surface as an
// error to the user.
(async () => {
after(async () => {
try {
const allMessages = [...history, finalMsg];
// Only summarize if there's something worth summarizing.
@@ -1951,7 +1950,7 @@ export async function POST(request: Request) {
} catch {
// best-effort; silent failure
}
})().catch(() => {});
});
// Plan extraction is handled inline during tool calls or proactively.
emit({ type: "done" });