fix(stream): report aborts after HTTP 200 in-band instead of closing silently
A stream that stalled or lost its upstream was closed with no terminal frame
at all, so clients saw "200 OK, a few chunks, then nothing" and could not tell
a truncated reply from a finished one. The Responses passthrough path already
synthesized response.failed; every other client format got nothing.
The watchdog now hands its reason ("stream stall timeout" or "upstream
connection lost") to onAbortTerminal, and buildStreamErrorBytes frames it per
client format: OpenAI-compatible clients get data: {"error":{...}} followed by
data: [DONE], Anthropic clients get `event: error`. The error frame always
precedes [DONE] (openai-python raises APIError on any data payload carrying an
error key), and no synthetic finish_reason is ever emitted — a truncated
stream must not look like a clean stop.
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import { FORMATS } from "../translator/formats.js";
|
||||
import { buildErrorBody } from "./error.js";
|
||||
import { SSE_DONE } from "./sseConstants.js";
|
||||
|
||||
const sharedEncoder = new TextEncoder();
|
||||
|
||||
// Parse SSE data line
|
||||
export function parseSSELine(line, format = null) {
|
||||
@@ -120,3 +124,24 @@ export function formatSSE(data, sourceFormat) {
|
||||
|
||||
return `data: ${JSON.stringify(data)}\n\n`;
|
||||
}
|
||||
|
||||
// Terminal frames for a stream that aborted after HTTP 200 was already sent, so
|
||||
// the status code can no longer change. OpenAI-compatible clients (openai-python
|
||||
// raises APIError on any `data:` payload carrying an `error` key, checked before
|
||||
// [DONE]) need the error frame first, then [DONE]; Anthropic clients need
|
||||
// `event: error`. Never fabricate a successful finish_reason instead.
|
||||
//
|
||||
// Returns encoded bytes: onAbortTerminal callbacks are enqueued verbatim, same
|
||||
// as buildAbortedResponsesTerminalBytes.
|
||||
//
|
||||
// NOTE: non-SSE client formats (Ollama NDJSON) get an SSE frame here — dead in
|
||||
// practice because detectFormatByEndpoint never resolves to OLLAMA.
|
||||
export function buildStreamErrorBytes(statusCode, message, clientFormat) {
|
||||
const { error } = buildErrorBody(statusCode, message);
|
||||
|
||||
const sse = clientFormat === FORMATS.CLAUDE
|
||||
? formatSSE({ type: "error", error }, FORMATS.CLAUDE)
|
||||
: formatSSE({ error }, clientFormat) + SSE_DONE;
|
||||
|
||||
return sharedEncoder.encode(sse);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user