web: add SSE keepalive and waiting-for-subagents header in stream route

This commit is contained in:
kumarabhirup 2026-02-21 11:04:32 -08:00
parent 9d8c384517
commit 149dfb7d50
No known key found for this signature in database
GPG Key ID: DB7CA2289CAB0167

View File

@ -32,9 +32,20 @@ export async function GET(req: Request) {
const encoder = new TextEncoder(); const encoder = new TextEncoder();
let closed = false; let closed = false;
let unsubscribe: (() => void) | null = null; let unsubscribe: (() => void) | null = null;
let keepalive: ReturnType<typeof setInterval> | null = null;
const stream = new ReadableStream({ const stream = new ReadableStream({
start(controller) { start(controller) {
// Keep idle SSE connections alive while waiting for subagent announcements.
keepalive = setInterval(() => {
if (closed) {return;}
try {
controller.enqueue(encoder.encode(": keepalive\n\n"));
} catch {
/* ignore enqueue errors on closed stream */
}
}, 15_000);
// subscribeToRun with replay=true replays the full event buffer // subscribeToRun with replay=true replays the full event buffer
// synchronously, then subscribes for live events. // synchronously, then subscribes for live events.
unsubscribe = subscribeToRun( unsubscribe = subscribeToRun(
@ -44,6 +55,10 @@ export async function GET(req: Request) {
if (event === null) { if (event === null) {
// Run completed — close the SSE stream. // Run completed — close the SSE stream.
closed = true; closed = true;
if (keepalive) {
clearInterval(keepalive);
keepalive = null;
}
try { try {
controller.close(); controller.close();
} catch { } catch {
@ -66,12 +81,20 @@ export async function GET(req: Request) {
if (!unsubscribe) { if (!unsubscribe) {
// Run was cleaned up between getActiveRun and subscribe. // Run was cleaned up between getActiveRun and subscribe.
closed = true; closed = true;
if (keepalive) {
clearInterval(keepalive);
keepalive = null;
}
controller.close(); controller.close();
} }
}, },
cancel() { cancel() {
// Client disconnected — unsubscribe only (don't kill the run). // Client disconnected — unsubscribe only (don't kill the run).
closed = true; closed = true;
if (keepalive) {
clearInterval(keepalive);
keepalive = null;
}
unsubscribe?.(); unsubscribe?.();
}, },
}); });
@ -81,7 +104,7 @@ export async function GET(req: Request) {
"Content-Type": "text/event-stream", "Content-Type": "text/event-stream",
"Cache-Control": "no-cache, no-transform", "Cache-Control": "no-cache, no-transform",
Connection: "keep-alive", Connection: "keep-alive",
"X-Run-Active": run.status === "running" ? "true" : "false", "X-Run-Active": run.status === "running" || run.status === "waiting-for-subagents" ? "true" : "false",
}, },
}); });
} }