This commit is contained in:
Ayush Ojha 2026-01-30 13:24:32 +00:00 committed by GitHub
commit 928fcffc36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -252,6 +252,26 @@ export async function handleOpenAiHttpRequest(
let wroteRole = false; let wroteRole = false;
let sawAssistantDelta = false; let sawAssistantDelta = false;
let closed = false; let closed = false;
let sentTerminalChunk = false;
/** Send a terminal chunk with finish_reason before [DONE]. */
const endStream = () => {
if (closed) return;
closed = true;
if (!sentTerminalChunk) {
sentTerminalChunk = true;
writeSse(res, {
id: runId,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model,
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
});
}
unsubscribe();
writeDone(res);
res.end();
};
const unsubscribe = onAgentEvent((evt) => { const unsubscribe = onAgentEvent((evt) => {
if (evt.runId !== runId) return; if (evt.runId !== runId) return;
@ -294,17 +314,16 @@ export async function handleOpenAiHttpRequest(
if (evt.stream === "lifecycle") { if (evt.stream === "lifecycle") {
const phase = evt.data?.phase; const phase = evt.data?.phase;
if (phase === "end" || phase === "error") { if (phase === "end" || phase === "error") {
closed = true; endStream();
unsubscribe();
writeDone(res);
res.end();
} }
} }
}); });
req.on("close", () => { req.on("close", () => {
closed = true; if (!closed) {
unsubscribe(); closed = true;
unsubscribe();
}
}); });
void (async () => { void (async () => {
@ -363,6 +382,7 @@ export async function handleOpenAiHttpRequest(
} }
} catch (err) { } catch (err) {
if (closed) return; if (closed) return;
sentTerminalChunk = true;
writeSse(res, { writeSse(res, {
id: runId, id: runId,
object: "chat.completion.chunk", object: "chat.completion.chunk",
@ -382,12 +402,7 @@ export async function handleOpenAiHttpRequest(
data: { phase: "error" }, data: { phase: "error" },
}); });
} finally { } finally {
if (!closed) { endStream();
closed = true;
unsubscribe();
writeDone(res);
res.end();
}
} }
})(); })();