mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
fix(codex): harden streaming timeouts + Responses terminal events
Raise stall/connect timeouts to 60s (configurable per-provider), accept codex response.done, and always emit a terminal response.failed + [DONE] for Responses passthrough when a stream closes, stalls, or aborts before a terminal event — preventing codex clients from hanging. Co-authored-by: jonathanli12 <jonathanli12@users.noreply.github.com> Co-authored-by: rifuki <rifuki@users.noreply.github.com> Co-authored-by: nguyenha935 <nguyenha935@users.noreply.github.com> Co-authored-by: trananhtung <trananhtung@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
jonathanli12
rifuki
nguyenha935
trananhtung
Cursor
parent
f161b295a5
commit
9caea88528
@@ -0,0 +1,83 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { FORMATS } from "../../open-sse/translator/formats.js";
|
||||
import { createSSETransformStreamWithLogger } from "../../open-sse/utils/stream.js";
|
||||
|
||||
async function runTransform(input) {
|
||||
const encoder = new TextEncoder();
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(input));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
|
||||
const output = stream.pipeThrough(
|
||||
createSSETransformStreamWithLogger(
|
||||
FORMATS.OPENAI_RESPONSES,
|
||||
FORMATS.OPENAI_RESPONSES,
|
||||
"codex",
|
||||
null,
|
||||
null,
|
||||
"gpt-5.5",
|
||||
),
|
||||
);
|
||||
|
||||
const reader = output.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let text = "";
|
||||
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
text += decoder.decode(value, { stream: true });
|
||||
}
|
||||
|
||||
text += decoder.decode();
|
||||
return text;
|
||||
}
|
||||
|
||||
describe("OpenAI Responses streaming termination", () => {
|
||||
it("emits a response.failed event when a Responses stream closes before a terminal event", async () => {
|
||||
const output = await runTransform([
|
||||
`event: response.created`,
|
||||
`data: ${JSON.stringify({ type: "response.created", response: { id: "resp_test", status: "in_progress" } })}`,
|
||||
"",
|
||||
`event: response.output_text.delta`,
|
||||
`data: ${JSON.stringify({ type: "response.output_text.delta", delta: "partial" })}`,
|
||||
"",
|
||||
].join("\n"));
|
||||
|
||||
expect(output).toContain("event: response.failed");
|
||||
expect(output).toContain('"type":"response.failed"');
|
||||
expect(output).not.toContain("data: null");
|
||||
expect(output).toContain("data: [DONE]");
|
||||
});
|
||||
|
||||
it("does not add response.failed when a Responses stream already completed", async () => {
|
||||
const output = await runTransform([
|
||||
`event: response.completed`,
|
||||
`data: ${JSON.stringify({ type: "response.completed", response: { id: "resp_test", status: "completed" } })}`,
|
||||
"",
|
||||
].join("\n"));
|
||||
|
||||
expect(output).toContain("event: response.completed");
|
||||
expect(output).not.toContain("event: response.failed");
|
||||
expect(output).not.toContain("data: null");
|
||||
expect(output).toContain("data: [DONE]");
|
||||
});
|
||||
|
||||
it("emits response.failed before DONE when a Responses stream sends DONE without a terminal event", async () => {
|
||||
const output = await runTransform([
|
||||
`event: response.created`,
|
||||
`data: ${JSON.stringify({ type: "response.created", response: { id: "resp_test", status: "in_progress" } })}`,
|
||||
"",
|
||||
"data: [DONE]",
|
||||
"",
|
||||
].join("\n"));
|
||||
|
||||
expect(output.indexOf("event: response.failed")).toBeLessThan(output.indexOf("data: [DONE]"));
|
||||
expect(output.match(/data: \[DONE\]/g)).toHaveLength(1);
|
||||
expect(output).not.toContain("data: null");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user