fix(open-sse): treat CommandCode in-stream error events as request failures

Upstream emits AI SDK v5 {"type":"error"} events inside an HTTP 200 stream.
The translator turned them into fake success content ([CommandCode error: ...]
+ finish_reason stop), so account/model fallback never fired and logs showed
Status: success.

- translator: error events now emit an OpenAI-shaped error chunk (chunk.error)
  instead of content; parseSSEToOpenAIResponse already detects chunk?.error
- executor: peek the first events before committing the response; an early
  error event returns 502 so fallback runs before any byte reaches the client
This commit is contained in:
2026-08-04 23:26:37 +07:00
parent c5ce1ef140
commit 9b27ee2611
4 changed files with 655 additions and 292 deletions

View File

@@ -0,0 +1,101 @@
/**
* Unit tests for the CommandCode executor early-error peek.
*
* The upstream emits AI SDK v5 NDJSON over an HTTP 200 stream, so a terminal
* `{"type":"error"}` event is invisible to the normal `response.ok` success
* check. `peekForUpstreamError` reads the first events before committing the
* response: an error event → non-ok Response (fallback can kick in); otherwise
* the buffered bytes are re-emitted and streaming proceeds as before.
*/
import { describe, it, expect } from "vitest";
import { peekForUpstreamError } from "../../open-sse/executors/commandcode.js";
const encoder = new TextEncoder();
function ndjsonResponse(lines) {
const body = new ReadableStream({
start(controller) {
for (const line of lines) controller.enqueue(encoder.encode(line + "\n"));
controller.close();
},
});
return new Response(body, {
status: 200,
headers: { "content-type": "text/event-stream" },
});
}
describe("commandcode executor — early-error peek", () => {
it("returns 502 when the first meaningful event is an error", async () => {
const res = await peekForUpstreamError(
ndjsonResponse([
'{"type":"error","error":{"type":"server_error","message":"Network connection lost."}}',
]),
"model",
);
expect(res.status).toBe(502);
const body = await res.json();
expect(body.error.message).toBe("Network connection lost.");
expect(body.error.type).toBe("server_error");
});
it("detects an error event even when metadata events arrive first", async () => {
const res = await peekForUpstreamError(
ndjsonResponse([
'{"type":"start"}',
'{"type":"start-step"}',
'{"type":"error","error":{"type":"server_error","message":"Network connection lost."}}',
]),
"model",
);
expect(res.status).toBe(502);
const body = await res.json();
expect(body.error.message).toContain("Network connection lost");
});
it("commits and streams normally when the first meaningful event is content", async () => {
const res = await peekForUpstreamError(
ndjsonResponse([
'{"type":"start"}',
'{"type":"text-delta","text":"hi there"}',
'{"type":"finish"}',
]),
"model",
);
expect(res.status).toBe(200);
const text = await res.text();
expect(text).toContain('"content":"hi there"');
expect(text).not.toContain("[CommandCode error:");
});
it("commits when the stream ends without any event", async () => {
const res = await peekForUpstreamError(ndjsonResponse([]), "model");
expect(res.status).toBe(200);
await res.body.cancel();
});
it("commits (does not hang) when no event arrives before the peek timeout", async () => {
const stalled = new Response(new ReadableStream({ start() {} }), {
status: 200,
headers: { "content-type": "text/event-stream" },
});
const res = await peekForUpstreamError(stalled, "model", { timeoutMs: 50 });
expect(res.status).toBe(200);
await res.body.cancel();
});
it("does not hang when the request signal aborts during the peek", async () => {
const controller = new AbortController();
const stalled = new Response(new ReadableStream({ start() {} }), {
status: 200,
headers: { "content-type": "text/event-stream" },
});
setTimeout(() => controller.abort(new Error("client gone")), 10);
const res = await peekForUpstreamError(stalled, "model", {
signal: controller.signal,
timeoutMs: 2000,
});
expect(res.status).toBe(200);
await res.body.cancel();
});
});

View File

@@ -12,116 +12,152 @@ import { describe, it, expect } from "vitest";
import { commandCodeToOpenAIResponse } from "../../open-sse/translator/response/commandcode-to-openai.js";
function feed(events) {
const state = {};
const all = [];
for (const e of events) {
const out = commandCodeToOpenAIResponse(JSON.stringify(e), state);
if (out) for (const c of out) all.push(c);
}
return { state, chunks: all };
const state = {};
const all = [];
for (const e of events) {
const out = commandCodeToOpenAIResponse(JSON.stringify(e), state);
if (out) for (const c of out) all.push(c);
}
return { state, chunks: all };
}
describe("commandcode-to-openai — text-delta", () => {
it("emits assistant role on first delta then content-only", () => {
const { chunks } = feed([
{ type: "text-delta", text: "Hello" },
{ type: "text-delta", text: " world" },
]);
expect(chunks[0].choices[0].delta.role).toBe("assistant");
expect(chunks[0].choices[0].delta.content).toBe("Hello");
expect(chunks[1].choices[0].delta.role).toBeUndefined();
expect(chunks[1].choices[0].delta.content).toBe(" world");
});
it("emits assistant role on first delta then content-only", () => {
const { chunks } = feed([
{ type: "text-delta", text: "Hello" },
{ type: "text-delta", text: " world" },
]);
expect(chunks[0].choices[0].delta.role).toBe("assistant");
expect(chunks[0].choices[0].delta.content).toBe("Hello");
expect(chunks[1].choices[0].delta.role).toBeUndefined();
expect(chunks[1].choices[0].delta.content).toBe(" world");
});
});
describe("commandcode-to-openai — reasoning-delta", () => {
it("maps reasoning-delta to reasoning_content delta", () => {
const { chunks } = feed([
{ type: "reasoning-delta", text: "thinking..." },
]);
expect(chunks[0].choices[0].delta.reasoning_content).toBe("thinking...");
});
it("maps reasoning-delta to reasoning_content delta", () => {
const { chunks } = feed([{ type: "reasoning-delta", text: "thinking..." }]);
expect(chunks[0].choices[0].delta.reasoning_content).toBe("thinking...");
});
});
describe("commandcode-to-openai — tool-input-* with id field (live schema)", () => {
it("registers tool index using event.id (NOT toolCallId)", () => {
const { chunks } = feed([
{ type: "tool-input-start", id: "call_X", toolName: "Bash" },
{ type: "tool-input-delta", id: "call_X", delta: "{\"cmd" },
{ type: "tool-input-delta", id: "call_X", delta: "\":\"ls\"}" },
]);
it("registers tool index using event.id (NOT toolCallId)", () => {
const { chunks } = feed([
{ type: "tool-input-start", id: "call_X", toolName: "Bash" },
{ type: "tool-input-delta", id: "call_X", delta: '{"cmd' },
{ type: "tool-input-delta", id: "call_X", delta: '":"ls"}' },
]);
// First chunk emits tool_calls with id
const startChunk = chunks[0].choices[0].delta.tool_calls[0];
expect(startChunk.id).toBe("call_X");
expect(startChunk.function.name).toBe("Bash");
// First chunk emits tool_calls with id
const startChunk = chunks[0].choices[0].delta.tool_calls[0];
expect(startChunk.id).toBe("call_X");
expect(startChunk.function.name).toBe("Bash");
// Subsequent deltas accumulate arguments
expect(chunks[1].choices[0].delta.tool_calls[0].function.arguments).toBe("{\"cmd");
expect(chunks[2].choices[0].delta.tool_calls[0].function.arguments).toBe("\":\"ls\"}");
});
// Subsequent deltas accumulate arguments
expect(chunks[1].choices[0].delta.tool_calls[0].function.arguments).toBe(
'{"cmd',
);
expect(chunks[2].choices[0].delta.tool_calls[0].function.arguments).toBe(
'":"ls"}',
);
});
it("ignores tool-input-delta when id is unknown (no prior start)", () => {
const { chunks } = feed([
{ type: "tool-input-delta", id: "unknown", delta: "x" },
]);
expect(chunks.length).toBe(0);
});
it("ignores tool-input-delta when id is unknown (no prior start)", () => {
const { chunks } = feed([
{ type: "tool-input-delta", id: "unknown", delta: "x" },
]);
expect(chunks.length).toBe(0);
});
});
describe("commandcode-to-openai — final tool-call event", () => {
it("does NOT re-emit tool_calls when tool-input-* deltas already fired", () => {
const { chunks } = feed([
{ type: "tool-input-start", id: "call_Y", toolName: "Write" },
{ type: "tool-input-delta", id: "call_Y", delta: "{\"file\":\"a\"}" },
{ type: "tool-call", toolCallId: "call_Y", toolName: "Write", input: { file: "a" } },
]);
// Should be exactly 2 chunks (start + delta), no duplicate from final tool-call
expect(chunks.length).toBe(2);
});
it("does NOT re-emit tool_calls when tool-input-* deltas already fired", () => {
const { chunks } = feed([
{ type: "tool-input-start", id: "call_Y", toolName: "Write" },
{ type: "tool-input-delta", id: "call_Y", delta: '{"file":"a"}' },
{
type: "tool-call",
toolCallId: "call_Y",
toolName: "Write",
input: { file: "a" },
},
]);
// Should be exactly 2 chunks (start + delta), no duplicate from final tool-call
expect(chunks.length).toBe(2);
});
it("emits a consolidated tool_calls when only the final tool-call event arrives", () => {
const { chunks } = feed([
{ type: "tool-call", toolCallId: "call_Z", toolName: "Read", input: { path: "/x" } },
]);
expect(chunks.length).toBe(1);
const tc = chunks[0].choices[0].delta.tool_calls[0];
expect(tc.id).toBe("call_Z");
expect(tc.function.name).toBe("Read");
expect(tc.function.arguments).toBe(JSON.stringify({ path: "/x" }));
});
it("emits a consolidated tool_calls when only the final tool-call event arrives", () => {
const { chunks } = feed([
{
type: "tool-call",
toolCallId: "call_Z",
toolName: "Read",
input: { path: "/x" },
},
]);
expect(chunks.length).toBe(1);
const tc = chunks[0].choices[0].delta.tool_calls[0];
expect(tc.id).toBe("call_Z");
expect(tc.function.name).toBe("Read");
expect(tc.function.arguments).toBe(JSON.stringify({ path: "/x" }));
});
});
describe("commandcode-to-openai — finish", () => {
it("emits a final chunk with finish_reason=tool_calls when finishReason is tool-calls", () => {
const { chunks } = feed([
{ type: "tool-input-start", id: "call_F", toolName: "Bash" },
{ type: "tool-input-delta", id: "call_F", delta: "{}" },
{ type: "finish-step", finishReason: "tool-calls" },
{ type: "finish" },
]);
const last = chunks[chunks.length - 1];
expect(last.choices[0].finish_reason).toBe("tool_calls");
});
it("emits a final chunk with finish_reason=tool_calls when finishReason is tool-calls", () => {
const { chunks } = feed([
{ type: "tool-input-start", id: "call_F", toolName: "Bash" },
{ type: "tool-input-delta", id: "call_F", delta: "{}" },
{ type: "finish-step", finishReason: "tool-calls" },
{ type: "finish" },
]);
const last = chunks[chunks.length - 1];
expect(last.choices[0].finish_reason).toBe("tool_calls");
});
it("includes usage on the final chunk when totalUsage provided", () => {
const { chunks } = feed([
{ type: "text-delta", text: "hi" },
{ type: "finish-step", finishReason: "stop", usage: { inputTokens: 10, outputTokens: 5, totalTokens: 15 } },
{ type: "finish", totalUsage: { inputTokens: 10, outputTokens: 5, totalTokens: 15 } },
]);
const last = chunks[chunks.length - 1];
expect(last.usage).toEqual({ prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 });
});
it("includes usage on the final chunk when totalUsage provided", () => {
const { chunks } = feed([
{ type: "text-delta", text: "hi" },
{
type: "finish-step",
finishReason: "stop",
usage: { inputTokens: 10, outputTokens: 5, totalTokens: 15 },
},
{
type: "finish",
totalUsage: { inputTokens: 10, outputTokens: 5, totalTokens: 15 },
},
]);
const last = chunks[chunks.length - 1];
expect(last.usage).toEqual({
prompt_tokens: 10,
completion_tokens: 5,
total_tokens: 15,
});
});
});
describe("commandcode-to-openai — error event", () => {
it("stringifies object errors so client sees readable message", () => {
const { chunks } = feed([
{ type: "error", error: { type: "server_error", message: "Boom" } },
]);
const text = chunks[0].choices[0].delta.content;
expect(text).toContain("Boom");
expect(text).not.toContain("[object Object]");
});
it("emits an OpenAI-shaped error chunk instead of fake success content", () => {
const { chunks } = feed([
{ type: "error", error: { type: "server_error", message: "Boom" } },
]);
expect(chunks[0].error).toEqual({ message: "Boom", type: "server_error" });
expect(chunks[0].choices[0].delta.content).toBeUndefined();
expect(chunks[1].choices[0].finish_reason).toBe("stop");
expect(JSON.stringify(chunks)).not.toContain("[CommandCode error:");
});
it("keeps the stream terminal so clients do not hang waiting for more", () => {
const { chunks } = feed([
{ type: "start" },
{
type: "error",
error: { type: "server_error", message: "Network connection lost." },
},
]);
expect(chunks[0].error.message).toBe("Network connection lost.");
expect(chunks[1].choices[0].finish_reason).toBe("stop");
});
});