mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +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,49 @@
|
||||
// Helpers for OpenAI Responses API streaming termination + event framing
|
||||
import { FORMATS } from "../translator/formats.js";
|
||||
import { formatSSE } from "./streamHelpers.js";
|
||||
|
||||
// Responses API events that signal the stream has reached a terminal state
|
||||
const OPENAI_RESPONSES_TERMINAL_EVENTS = new Set([
|
||||
"response.completed",
|
||||
"response.failed",
|
||||
"error"
|
||||
]);
|
||||
|
||||
export function getOpenAIResponsesEventName(eventName, chunk) {
|
||||
if (eventName) return eventName;
|
||||
if (chunk && typeof chunk.type === "string") return chunk.type;
|
||||
return null;
|
||||
}
|
||||
|
||||
export function isOpenAIResponsesTerminalEvent(eventName, chunk) {
|
||||
const type = getOpenAIResponsesEventName(eventName, chunk);
|
||||
if (OPENAI_RESPONSES_TERMINAL_EVENTS.has(type)) return true;
|
||||
const status = chunk?.response?.status;
|
||||
return status === "completed" || status === "failed";
|
||||
}
|
||||
|
||||
const sharedEncoder = new TextEncoder();
|
||||
|
||||
// Encoded response.failed + [DONE] payload for aborted/stalled Responses passthrough streams
|
||||
export function buildAbortedResponsesTerminalBytes() {
|
||||
return sharedEncoder.encode(`${formatIncompleteOpenAIResponsesStreamFailure()}data: [DONE]\n\n`);
|
||||
}
|
||||
|
||||
// Synthesize a response.failed event for streams that close without a terminal event
|
||||
export function formatIncompleteOpenAIResponsesStreamFailure() {
|
||||
return formatSSE({
|
||||
event: "response.failed",
|
||||
data: {
|
||||
type: "response.failed",
|
||||
response: {
|
||||
id: `resp_${Date.now()}`,
|
||||
status: "failed",
|
||||
error: {
|
||||
type: "stream_error",
|
||||
code: "stream_disconnected",
|
||||
message: "stream closed before response.completed"
|
||||
}
|
||||
}
|
||||
}
|
||||
}, FORMATS.OPENAI_RESPONSES);
|
||||
}
|
||||
Reference in New Issue
Block a user