mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +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);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { FORMATS } from "../translator/formats.js";
|
||||
import { trackPendingRequest, appendRequestLog } from "@/lib/usageDb.js";
|
||||
import { extractUsage, hasValidUsage, estimateUsage, logUsage, addBufferToUsage, filterUsageForFormat, COLORS } from "./usageTracking.js";
|
||||
import { parseSSELine, hasValuableContent, fixInvalidId, formatSSE } from "./streamHelpers.js";
|
||||
import { getOpenAIResponsesEventName, isOpenAIResponsesTerminalEvent, formatIncompleteOpenAIResponsesStreamFailure } from "./responsesStreamHelpers.js";
|
||||
import { dbg, isDebugEnabled } from "./debugLog.js";
|
||||
|
||||
export { COLORS, formatSSE };
|
||||
@@ -63,6 +64,11 @@ export function createSSEStream(options = {}) {
|
||||
let sseEmittedCount = 0;
|
||||
const eventTypeCounts = {};
|
||||
|
||||
// Track Responses API event framing for same-format passthrough (codex)
|
||||
let currentOpenAIResponsesEvent = null;
|
||||
let openAIResponsesTerminalSeen = false;
|
||||
let openAIResponsesDoneSent = false;
|
||||
|
||||
return new TransformStream({
|
||||
transform(chunk, controller) {
|
||||
if (!ttftAt) ttftAt = Date.now();
|
||||
@@ -83,6 +89,11 @@ export function createSSEStream(options = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
// Capture Responses API event name to preserve framing in same-format passthrough
|
||||
if (mode === STREAM_MODE.TRANSLATE && targetFormat === FORMATS.OPENAI_RESPONSES && trimmed.startsWith("event:")) {
|
||||
currentOpenAIResponsesEvent = trimmed.slice(6).trim();
|
||||
}
|
||||
|
||||
// Passthrough mode: normalize and forward
|
||||
if (mode === STREAM_MODE.PASSTHROUGH) {
|
||||
let output;
|
||||
@@ -174,12 +185,33 @@ export function createSSEStream(options = {}) {
|
||||
const parsed = parseSSELine(trimmed, targetFormat);
|
||||
if (!parsed) continue;
|
||||
|
||||
// Responses API same-format passthrough: preserve event framing + track terminal state
|
||||
const isOpenAIResponsesStream = targetFormat === FORMATS.OPENAI_RESPONSES;
|
||||
const keepsOpenAIResponsesFormat = isOpenAIResponsesStream && sourceFormat === FORMATS.OPENAI_RESPONSES;
|
||||
const openAIResponsesEventName = isOpenAIResponsesStream
|
||||
? getOpenAIResponsesEventName(currentOpenAIResponsesEvent, parsed)
|
||||
: null;
|
||||
|
||||
if (isOpenAIResponsesStream && isOpenAIResponsesTerminalEvent(openAIResponsesEventName, parsed)) {
|
||||
openAIResponsesTerminalSeen = true;
|
||||
}
|
||||
|
||||
// For Ollama: done=true is the final chunk with finish_reason/usage, must translate
|
||||
// For other formats: done=true is the [DONE] sentinel, skip
|
||||
if (parsed && parsed.done && targetFormat !== FORMATS.OLLAMA) {
|
||||
// Synthesize response.failed if the Responses stream never sent a terminal event
|
||||
if (keepsOpenAIResponsesFormat && !openAIResponsesTerminalSeen) {
|
||||
const failedOutput = formatIncompleteOpenAIResponsesStreamFailure();
|
||||
reqLogger?.appendConvertedChunk?.(failedOutput);
|
||||
controller.enqueue(sharedEncoder.encode(failedOutput));
|
||||
openAIResponsesTerminalSeen = true;
|
||||
sseEmittedCount++;
|
||||
}
|
||||
|
||||
const output = "data: [DONE]\n\n";
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
if (keepsOpenAIResponsesFormat) openAIResponsesDoneSent = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -224,6 +256,18 @@ export function createSSEStream(options = {}) {
|
||||
const extracted = extractUsage(parsed);
|
||||
if (extracted) state.usage = extracted; // Keep original usage for logging
|
||||
|
||||
// Responses same-format passthrough: re-emit with original event framing
|
||||
if (keepsOpenAIResponsesFormat && openAIResponsesEventName) {
|
||||
const output = formatSSE({ event: openAIResponsesEventName, data: parsed }, sourceFormat);
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
currentOpenAIResponsesEvent = null;
|
||||
sseEmittedCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
currentOpenAIResponsesEvent = null;
|
||||
|
||||
// Translate: targetFormat -> openai -> sourceFormat
|
||||
const translated = translateResponse(targetFormat, sourceFormat, parsed, state);
|
||||
|
||||
@@ -237,6 +281,7 @@ export function createSSEStream(options = {}) {
|
||||
|
||||
if (translated?.length > 0) {
|
||||
for (const item of translated) {
|
||||
if (item === null || item === undefined) continue;
|
||||
// Filter empty chunks
|
||||
if (!hasValuableContent(item, sourceFormat)) {
|
||||
continue; // Skip this empty chunk
|
||||
@@ -322,6 +367,7 @@ export function createSSEStream(options = {}) {
|
||||
|
||||
if (translated?.length > 0) {
|
||||
for (const item of translated) {
|
||||
if (item === null || item === undefined) continue;
|
||||
const output = formatSSE(item, sourceFormat);
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
@@ -341,15 +387,27 @@ export function createSSEStream(options = {}) {
|
||||
|
||||
if (flushed?.length > 0) {
|
||||
for (const item of flushed) {
|
||||
if (item === null || item === undefined) continue;
|
||||
const output = formatSSE(item, sourceFormat);
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
}
|
||||
}
|
||||
|
||||
const doneOutput = "data: [DONE]\n\n";
|
||||
reqLogger?.appendConvertedChunk?.(doneOutput);
|
||||
controller.enqueue(sharedEncoder.encode(doneOutput));
|
||||
// Synthesize response.failed if a Responses passthrough stream never reached a terminal event
|
||||
const keepsOpenAIResponsesFormat = targetFormat === FORMATS.OPENAI_RESPONSES && sourceFormat === FORMATS.OPENAI_RESPONSES;
|
||||
if (keepsOpenAIResponsesFormat && !openAIResponsesTerminalSeen) {
|
||||
const failedOutput = formatIncompleteOpenAIResponsesStreamFailure();
|
||||
reqLogger?.appendConvertedChunk?.(failedOutput);
|
||||
controller.enqueue(sharedEncoder.encode(failedOutput));
|
||||
openAIResponsesTerminalSeen = true;
|
||||
}
|
||||
|
||||
if (!keepsOpenAIResponsesFormat || !openAIResponsesDoneSent) {
|
||||
const doneOutput = "data: [DONE]\n\n";
|
||||
reqLogger?.appendConvertedChunk?.(doneOutput);
|
||||
controller.enqueue(sharedEncoder.encode(doneOutput));
|
||||
}
|
||||
|
||||
if (!hasValidUsage(state?.usage) && totalContentLength > 0) {
|
||||
state.usage = estimateUsage(body, totalContentLength, sourceFormat);
|
||||
|
||||
@@ -94,13 +94,25 @@ export function createStreamController({ onDisconnect, onError, log, provider, m
|
||||
* for long periods while raw bytes still flow (e.g. Kiro EventStream
|
||||
* binary frames buffering, Claude reasoning streams).
|
||||
*/
|
||||
export function createDisconnectAwareStream(transformStream, streamController) {
|
||||
export function createDisconnectAwareStream(transformStream, streamController, onAbortTerminal = null) {
|
||||
const reader = transformStream.readable.getReader();
|
||||
const writer = transformStream.writable.getWriter();
|
||||
let terminalEmitted = false;
|
||||
|
||||
// Emit a synthesized terminal payload (e.g. Responses response.failed + [DONE]) once
|
||||
const emitTerminal = (controller) => {
|
||||
if (terminalEmitted || !onAbortTerminal) return;
|
||||
terminalEmitted = true;
|
||||
try {
|
||||
const bytes = onAbortTerminal();
|
||||
if (bytes) controller.enqueue(bytes);
|
||||
} catch { /* best-effort terminal */ }
|
||||
};
|
||||
|
||||
return new ReadableStream({
|
||||
async pull(controller) {
|
||||
if (!streamController.isConnected()) {
|
||||
emitTerminal(controller);
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
@@ -135,17 +147,16 @@ export function createDisconnectAwareStream(transformStream, streamController) {
|
||||
code === "EPIPE" ||
|
||||
code === "UND_ERR_SOCKET";
|
||||
|
||||
if (!wasConnected || isNetworkClose) {
|
||||
try {
|
||||
// Graceful close on network/abort, or when a structured terminal is available
|
||||
// (Responses passthrough prefers response.failed + [DONE] over a raw transport error)
|
||||
try {
|
||||
if (!wasConnected || isNetworkClose || onAbortTerminal) {
|
||||
emitTerminal(controller);
|
||||
controller.close();
|
||||
} catch (e) {
|
||||
// Stream might already be closed or cancelled
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
} else {
|
||||
controller.error(error);
|
||||
} catch (e) { /* already closed */ }
|
||||
}
|
||||
}
|
||||
} catch (e) { /* already closed or cancelled */ }
|
||||
}
|
||||
},
|
||||
|
||||
@@ -173,7 +184,7 @@ export function createDisconnectAwareStream(transformStream, streamController) {
|
||||
* @param {TransformStream} transformStream - Transform stream for SSE
|
||||
* @param {object} streamController - Stream controller from createStreamController
|
||||
*/
|
||||
export function pipeWithDisconnect(providerResponse, transformStream, streamController) {
|
||||
export function pipeWithDisconnect(providerResponse, transformStream, streamController, onAbortTerminal = null) {
|
||||
let stallTimer = null;
|
||||
let chunkCount = 0;
|
||||
let totalBytes = 0;
|
||||
@@ -232,7 +243,8 @@ export function pipeWithDisconnect(providerResponse, transformStream, streamCont
|
||||
|
||||
return createDisconnectAwareStream(
|
||||
{ readable: transformedBody, writable: { getWriter: () => ({ abort: () => Promise.resolve() }) } },
|
||||
wrappedController
|
||||
wrappedController,
|
||||
onAbortTerminal
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user