Keep chat stream alive during long runs

This commit is contained in:
武阳
2026-05-07 15:33:03 +08:00
parent cd4e73c159
commit ce6445bf77
+14
View File
@@ -13,6 +13,7 @@ import {
} from "@/lib/claw-auth";
export const runtime = "nodejs";
export const maxDuration = 300;
type ClawChatResponse = {
final_output?: string;
@@ -92,9 +93,19 @@ export async function POST(req: Request) {
const stream = createUIMessageStream({
execute: async ({ writer }) => {
const streamState = { textStarted: false, textStreamed: false };
let heartbeatCount = 0;
const heartbeat = setInterval(() => {
heartbeatCount += 1;
writer.write({
type: "reasoning-delta",
id: "reasoning-1",
delta: `\n仍在处理请求,已等待 ${formatDuration(heartbeatCount * 15000)}`,
});
}, 15000);
writer.write({ type: "start" });
writer.write({ type: "start-step" });
try {
const startedAt = Date.now();
writer.write({ type: "reasoning-start", id: "reasoning-1" });
writer.write({
@@ -141,6 +152,9 @@ export async function POST(req: Request) {
usage: payload.usage,
},
});
} finally {
clearInterval(heartbeat);
}
},
});