Keep chat stream alive during long runs
This commit is contained in:
@@ -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);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user