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

@@ -1,6 +1,7 @@
import { randomUUID } from "crypto"; import { randomUUID } from "crypto";
import { BaseExecutor } from "./base.js"; import { BaseExecutor } from "./base.js";
import { PROVIDERS } from "../config/providers.js"; import { PROVIDERS } from "../config/providers.js";
import { HTTP_STATUS } from "../config/runtimeConfig.js";
import { commandCodeToOpenAIResponse } from "../translator/response/commandcode-to-openai.js"; import { commandCodeToOpenAIResponse } from "../translator/response/commandcode-to-openai.js";
import { SSE_DONE } from "../utils/sseConstants.js"; import { SSE_DONE } from "../utils/sseConstants.js";
@@ -14,6 +15,12 @@ import { SSE_DONE } from "../utils/sseConstants.js";
* We translate each event to an OpenAI chat.completion.chunk and emit it as SSE so * We translate each event to an OpenAI chat.completion.chunk and emit it as SSE so
* both the streaming and non-streaming (forced SSE → JSON) downstream handlers in * both the streaming and non-streaming (forced SSE → JSON) downstream handlers in
* 9router can consume it without further format translation. * 9router can consume it without further format translation.
*
* Terminal upstream failures arrive as `{"type":"error"}` events inside the HTTP
* 200 stream, so a plain `response.ok` check cannot see them. We peek the first
* events before committing the response (see peekForUpstreamError) so a stream
* that starts with an error fails fast — the normal `!response.ok` path then
* triggers account/model fallback instead of streaming fake success content.
*/ */
export class CommandCodeExecutor extends BaseExecutor { export class CommandCodeExecutor extends BaseExecutor {
constructor() { constructor() {
@@ -42,11 +49,192 @@ export class CommandCodeExecutor extends BaseExecutor {
async execute(opts) { async execute(opts) {
const result = await super.execute(opts); const result = await super.execute(opts);
if (!result?.response?.ok || !result.response.body) return result; if (!result?.response?.ok || !result.response.body) return result;
result.response = wrapNdjsonAsOpenAISse(result.response, opts.model); result.response = await peekForUpstreamError(result.response, opts.model, {
signal: opts.signal,
});
return result; return result;
} }
} }
// How long to hold the response open while peeking the first upstream events.
// An upstream error event ("Network connection lost") is emitted at stream
// start, so the peek is fast; the bound just prevents a slow-started stream
// from being held hostage. Env: COMMANDCODE_PEEK_TIMEOUT_MS.
const PEEK_TIMEOUT_MS = (() => {
const raw = process.env.COMMANDCODE_PEEK_TIMEOUT_MS;
const n = raw ? parseInt(raw, 10) : NaN;
return Number.isFinite(n) && n > 0 ? n : 10 * 1000;
})();
// Event types that count as "the stream has started producing". Everything
// else (start, start-step, reasoning-start, text-start, ...) is metadata and
// does not end the peek.
const MEANINGFUL_EVENT_TYPES = new Set([
"text-delta",
"reasoning-delta",
"tool-input-start",
"tool-input-delta",
"tool-input-end",
"tool-call",
"finish-step",
"finish",
]);
function makeAbortError(reason) {
const error = new Error(reason?.message || reason || "Request aborted");
error.name = "AbortError";
return error;
}
function tryParseEvent(line) {
const trimmed = line.trim();
if (!trimmed) return null;
const json = trimmed.startsWith("data:") ? trimmed.slice(5).trim() : trimmed;
if (!json || json === "[DONE]") return null;
try {
return JSON.parse(json);
} catch {
return null;
}
}
function formatErrorValue(errVal) {
const errStr =
typeof errVal === "string"
? errVal
: typeof errVal?.message === "string"
? errVal.message
: JSON.stringify(errVal);
const errType =
typeof errVal === "string"
? "upstream_error"
: errVal?.type || "upstream_error";
return { message: errStr, type: errType };
}
/**
* Read the first upstream events before committing the response.
*
* - `{"type":"error"}` as the first meaningful event → return a 502 Response so
* chatCore's `!response.ok` path parses the error and triggers fallback.
* - Otherwise → re-emit the buffered bytes + the rest of the stream through the
* normal NDJSON → OpenAI SSE wrapper and return it untouched in spirit.
*
* Bounded by `timeoutMs` (default PEEK_TIMEOUT_MS): if no meaningful event
* arrives in time, or the request signal aborts, we commit whatever we have and
* let the regular stream pipeline (stall detection, abort handling) take over.
*/
export async function peekForUpstreamError(
originalResponse,
model,
{ signal = null, timeoutMs = PEEK_TIMEOUT_MS } = {},
) {
const reader = originalResponse.body.getReader();
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const abortController = new AbortController();
const forwardAbort = () => abortController.abort(signal?.reason);
if (signal?.aborted) abortController.abort(signal?.reason);
else if (signal)
signal.addEventListener("abort", forwardAbort, { once: true });
let peeked = "";
let errorEvent = null;
let committed = false;
const readWithTimeout = (ms) => {
if (abortController.signal.aborted) {
return Promise.reject(makeAbortError(abortController.signal.reason));
}
const timeoutPromise = new Promise((_, reject) => {
const t = setTimeout(() => reject(new Error("peek timeout")), ms);
t.unref?.();
});
const abortPromise = new Promise((_, reject) => {
abortController.signal.addEventListener(
"abort",
() => reject(makeAbortError(abortController.signal.reason)),
{ once: true },
);
});
return Promise.race([reader.read(), timeoutPromise, abortPromise]);
};
try {
const deadline = Date.now() + timeoutMs;
while (!errorEvent && !committed && Date.now() < deadline) {
const { done, value } = await readWithTimeout(
Math.max(deadline - Date.now(), 1),
);
if (done) break;
peeked += decoder.decode(value, { stream: true });
const lines = peeked.split("\n");
// The last segment may be a partial line — only parse complete ones.
for (const line of lines.slice(0, -1)) {
const event = tryParseEvent(line);
if (!event?.type) continue;
if (event.type === "error") {
errorEvent = event;
break;
}
if (MEANINGFUL_EVENT_TYPES.has(event.type)) {
committed = true;
break;
}
}
}
} catch {
// timeout / abort / read failure during the peek → commit whatever we have;
// the downstream stream pipeline (stall detection, abort handling) takes over.
}
// Flush any partial multi-byte UTF-8 sequence held by the decoder so the
// re-encoded peeked bytes round-trip losslessly.
peeked += decoder.decode();
if (signal) signal.removeEventListener("abort", forwardAbort);
if (errorEvent) {
await reader.cancel("commandcode early error detected").catch(() => {});
const { message, type } = formatErrorValue(
errorEvent.error ?? errorEvent.message ?? "unknown",
);
return new Response(JSON.stringify({ error: { message, type } }), {
status: HTTP_STATUS.BAD_GATEWAY,
statusText: message.slice(0, 200),
headers: { "Content-Type": "application/json" },
});
}
const remaining = new ReadableStream({
start(controller) {
(async () => {
try {
if (peeked) controller.enqueue(encoder.encode(peeked));
while (true) {
const { done, value } = await reader.read();
if (done) break;
controller.enqueue(value);
}
controller.close();
} catch (err) {
controller.error(err);
}
})();
},
cancel() {
reader.cancel("commandcode stream cancelled").catch(() => {});
},
});
const combined = new Response(remaining, {
status: originalResponse.status,
statusText: originalResponse.statusText,
headers: originalResponse.headers,
});
return wrapNdjsonAsOpenAISse(combined, model);
}
function wrapNdjsonAsOpenAISse(originalResponse, model) { function wrapNdjsonAsOpenAISse(originalResponse, model) {
const decoder = new TextDecoder(); const decoder = new TextDecoder();
const encoder = new TextEncoder(); const encoder = new TextEncoder();

View File

@@ -43,7 +43,7 @@ function makeChunk(state, delta, finishReason = null) {
return buildChunk( return buildChunk(
{ id: state.responseId, created: state.created, model: state.model }, { id: state.responseId, created: state.created, model: state.model },
delta, delta,
finishReason finishReason,
); );
} }
@@ -53,7 +53,11 @@ export function commandCodeToOpenAIResponse(chunk, state) {
if (!chunk) return null; if (!chunk) return null;
// Already-OpenAI chunk: pass through // Already-OpenAI chunk: pass through
if (chunk && typeof chunk === "object" && chunk.object === "chat.completion.chunk") { if (
chunk &&
typeof chunk === "object" &&
chunk.object === "chat.completion.chunk"
) {
return chunk; return chunk;
} }
@@ -81,7 +85,10 @@ export function commandCodeToOpenAIResponse(chunk, state) {
case "text-delta": { case "text-delta": {
const text = event.text || event.delta || ""; const text = event.text || event.delta || "";
if (!text) break; if (!text) break;
const delta = state.chunkIndex === 0 ? { role: ROLE.ASSISTANT, content: text } : { content: text }; const delta =
state.chunkIndex === 0
? { role: ROLE.ASSISTANT, content: text }
: { content: text };
state.chunkIndex++; state.chunkIndex++;
state.openText = true; state.openText = true;
out.push(makeChunk(state, delta)); out.push(makeChunk(state, delta));
@@ -97,7 +104,8 @@ export function commandCodeToOpenAIResponse(chunk, state) {
break; break;
} }
case "tool-input-start": { case "tool-input-start": {
const id = event.id || event.toolCallId || fallbackToolCallId(state.toolIndex); const id =
event.id || event.toolCallId || fallbackToolCallId(state.toolIndex);
let idx = state.toolIndexById.get(id); let idx = state.toolIndexById.get(id);
if (idx == null) { if (idx == null) {
idx = state.toolIndex++; idx = state.toolIndex++;
@@ -106,12 +114,14 @@ export function commandCodeToOpenAIResponse(chunk, state) {
state.openTools.add(id); state.openTools.add(id);
const delta = { const delta = {
...(state.chunkIndex === 0 ? { role: ROLE.ASSISTANT } : {}), ...(state.chunkIndex === 0 ? { role: ROLE.ASSISTANT } : {}),
tool_calls: [{ tool_calls: [
{
index: idx, index: idx,
id, id,
type: OPENAI_BLOCK.FUNCTION, type: OPENAI_BLOCK.FUNCTION,
function: { name: event.toolName || "", arguments: "" }, function: { name: event.toolName || "", arguments: "" },
}], },
],
}; };
state.chunkIndex++; state.chunkIndex++;
out.push(makeChunk(state, delta)); out.push(makeChunk(state, delta));
@@ -122,10 +132,12 @@ export function commandCodeToOpenAIResponse(chunk, state) {
const idx = state.toolIndexById.get(id); const idx = state.toolIndexById.get(id);
if (idx == null) break; if (idx == null) break;
const delta = { const delta = {
tool_calls: [{ tool_calls: [
{
index: idx, index: idx,
function: { arguments: event.delta || event.inputTextDelta || "" }, function: { arguments: event.delta || event.inputTextDelta || "" },
}], },
],
}; };
out.push(makeChunk(state, delta)); out.push(makeChunk(state, delta));
break; break;
@@ -136,15 +148,20 @@ export function commandCodeToOpenAIResponse(chunk, state) {
if (state.toolIndexById.has(id)) break; if (state.toolIndexById.has(id)) break;
const idx = state.toolIndex++; const idx = state.toolIndex++;
state.toolIndexById.set(id, idx); state.toolIndexById.set(id, idx);
const argsStr = typeof event.input === "string" ? event.input : JSON.stringify(event.input ?? {}); const argsStr =
typeof event.input === "string"
? event.input
: JSON.stringify(event.input ?? {});
const delta = { const delta = {
...(state.chunkIndex === 0 ? { role: ROLE.ASSISTANT } : {}), ...(state.chunkIndex === 0 ? { role: ROLE.ASSISTANT } : {}),
tool_calls: [{ tool_calls: [
{
index: idx, index: idx,
id, id,
type: OPENAI_BLOCK.FUNCTION, type: OPENAI_BLOCK.FUNCTION,
function: { name: event.toolName || "", arguments: argsStr }, function: { name: event.toolName || "", arguments: argsStr },
}], },
],
}; };
state.chunkIndex++; state.chunkIndex++;
out.push(makeChunk(state, delta)); out.push(makeChunk(state, delta));
@@ -156,7 +173,8 @@ export function commandCodeToOpenAIResponse(chunk, state) {
break; break;
} }
case "finish": { case "finish": {
const finishReason = state.finishReason || mapFinishReason(event.finishReason || "stop"); const finishReason =
state.finishReason || mapFinishReason(event.finishReason || "stop");
const finalChunk = makeChunk(state, {}, finishReason); const finalChunk = makeChunk(state, {}, finishReason);
const totalUsage = event.totalUsage || state.usage; const totalUsage = event.totalUsage || state.usage;
const usage = toOpenAIUsage(totalUsage, "commandcode"); const usage = toOpenAIUsage(totalUsage, "commandcode");
@@ -165,10 +183,25 @@ export function commandCodeToOpenAIResponse(chunk, state) {
break; break;
} }
case "error": { case "error": {
// Terminal upstream failure (AI SDK v5 error event) — NOT content. Emit an
// OpenAI-shaped error chunk (chunk.error) so downstream — parseSSEToOpenAIResponse
// for non-streaming, OpenAI SDK clients for streaming — treats the request as
// failed instead of surfacing fake success content like "[CommandCode error: ...]".
state.finishReason = OPENAI_FINISH.STOP; state.finishReason = OPENAI_FINISH.STOP;
const errVal = event.error ?? event.message ?? "unknown"; const errVal = event.error ?? event.message ?? "unknown";
const errStr = typeof errVal === "string" ? errVal : JSON.stringify(errVal); const errStr =
out.push(makeChunk(state, { content: `\n\n[CommandCode error: ${errStr}]` })); typeof errVal === "string"
? errVal
: typeof errVal?.message === "string"
? errVal.message
: JSON.stringify(errVal);
const errType =
typeof errVal === "string"
? "upstream_error"
: errVal?.type || "upstream_error";
const errChunk = makeChunk(state, {});
errChunk.error = { message: errStr, type: errType };
out.push(errChunk);
out.push(makeChunk(state, {}, OPENAI_FINISH.STOP)); out.push(makeChunk(state, {}, OPENAI_FINISH.STOP));
break; break;
} }
@@ -181,4 +214,9 @@ export function commandCodeToOpenAIResponse(chunk, state) {
return out.length ? out : null; return out.length ? out : null;
} }
register(FORMATS.COMMANDCODE, FORMATS.OPENAI, null, commandCodeToOpenAIResponse); register(
FORMATS.COMMANDCODE,
FORMATS.OPENAI,
null,
commandCodeToOpenAIResponse,
);

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

@@ -36,9 +36,7 @@ describe("commandcode-to-openai — text-delta", () => {
describe("commandcode-to-openai — reasoning-delta", () => { describe("commandcode-to-openai — reasoning-delta", () => {
it("maps reasoning-delta to reasoning_content delta", () => { it("maps reasoning-delta to reasoning_content delta", () => {
const { chunks } = feed([ const { chunks } = feed([{ type: "reasoning-delta", text: "thinking..." }]);
{ type: "reasoning-delta", text: "thinking..." },
]);
expect(chunks[0].choices[0].delta.reasoning_content).toBe("thinking..."); expect(chunks[0].choices[0].delta.reasoning_content).toBe("thinking...");
}); });
}); });
@@ -47,8 +45,8 @@ describe("commandcode-to-openai — tool-input-* with id field (live schema)", (
it("registers tool index using event.id (NOT toolCallId)", () => { it("registers tool index using event.id (NOT toolCallId)", () => {
const { chunks } = feed([ const { chunks } = feed([
{ type: "tool-input-start", id: "call_X", toolName: "Bash" }, { 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: '{"cmd' },
{ type: "tool-input-delta", id: "call_X", delta: "\":\"ls\"}" }, { type: "tool-input-delta", id: "call_X", delta: '":"ls"}' },
]); ]);
// First chunk emits tool_calls with id // First chunk emits tool_calls with id
@@ -57,8 +55,12 @@ describe("commandcode-to-openai — tool-input-* with id field (live schema)", (
expect(startChunk.function.name).toBe("Bash"); expect(startChunk.function.name).toBe("Bash");
// Subsequent deltas accumulate arguments // Subsequent deltas accumulate arguments
expect(chunks[1].choices[0].delta.tool_calls[0].function.arguments).toBe("{\"cmd"); expect(chunks[1].choices[0].delta.tool_calls[0].function.arguments).toBe(
expect(chunks[2].choices[0].delta.tool_calls[0].function.arguments).toBe("\":\"ls\"}"); '{"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)", () => { it("ignores tool-input-delta when id is unknown (no prior start)", () => {
@@ -73,8 +75,13 @@ describe("commandcode-to-openai — final tool-call event", () => {
it("does NOT re-emit tool_calls when tool-input-* deltas already fired", () => { it("does NOT re-emit tool_calls when tool-input-* deltas already fired", () => {
const { chunks } = feed([ const { chunks } = feed([
{ type: "tool-input-start", id: "call_Y", toolName: "Write" }, { type: "tool-input-start", id: "call_Y", toolName: "Write" },
{ type: "tool-input-delta", id: "call_Y", delta: "{\"file\":\"a\"}" }, { type: "tool-input-delta", id: "call_Y", delta: '{"file":"a"}' },
{ type: "tool-call", toolCallId: "call_Y", toolName: "Write", input: { 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 // Should be exactly 2 chunks (start + delta), no duplicate from final tool-call
expect(chunks.length).toBe(2); expect(chunks.length).toBe(2);
@@ -82,7 +89,12 @@ describe("commandcode-to-openai — final tool-call event", () => {
it("emits a consolidated tool_calls when only the final tool-call event arrives", () => { it("emits a consolidated tool_calls when only the final tool-call event arrives", () => {
const { chunks } = feed([ const { chunks } = feed([
{ type: "tool-call", toolCallId: "call_Z", toolName: "Read", input: { path: "/x" } }, {
type: "tool-call",
toolCallId: "call_Z",
toolName: "Read",
input: { path: "/x" },
},
]); ]);
expect(chunks.length).toBe(1); expect(chunks.length).toBe(1);
const tc = chunks[0].choices[0].delta.tool_calls[0]; const tc = chunks[0].choices[0].delta.tool_calls[0];
@@ -107,21 +119,45 @@ describe("commandcode-to-openai — finish", () => {
it("includes usage on the final chunk when totalUsage provided", () => { it("includes usage on the final chunk when totalUsage provided", () => {
const { chunks } = feed([ const { chunks } = feed([
{ type: "text-delta", text: "hi" }, { 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 } }, 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]; const last = chunks[chunks.length - 1];
expect(last.usage).toEqual({ prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 }); expect(last.usage).toEqual({
prompt_tokens: 10,
completion_tokens: 5,
total_tokens: 15,
});
}); });
}); });
describe("commandcode-to-openai — error event", () => { describe("commandcode-to-openai — error event", () => {
it("stringifies object errors so client sees readable message", () => { it("emits an OpenAI-shaped error chunk instead of fake success content", () => {
const { chunks } = feed([ const { chunks } = feed([
{ type: "error", error: { type: "server_error", message: "Boom" } }, { type: "error", error: { type: "server_error", message: "Boom" } },
]); ]);
const text = chunks[0].choices[0].delta.content; expect(chunks[0].error).toEqual({ message: "Boom", type: "server_error" });
expect(text).toContain("Boom"); expect(chunks[0].choices[0].delta.content).toBeUndefined();
expect(text).not.toContain("[object Object]"); 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");
}); });
}); });