mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
Treat response.done as a terminal OpenAI Responses stream event so passthrough streams ending with response.done are not flagged incomplete and no synthetic response.failed is emitted. Restore the data: [DONE] sentinel for same-format Responses passthrough streams. Co-authored-by: Cursor <cursoragent@cursor.com>
97 lines
3.2 KiB
JavaScript
97 lines
3.2 KiB
JavaScript
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("does not add response.failed when a Responses stream sends response.done", async () => {
|
|
const output = await runTransform([
|
|
`event: response.done`,
|
|
`data: ${JSON.stringify({ type: "response.done", response: { id: "resp_test" } })}`,
|
|
"",
|
|
].join("\n"));
|
|
|
|
expect(output).toContain("event: response.done");
|
|
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");
|
|
});
|
|
});
|