fix(responses): report usage on response.completed so clients can auto-compact

Map upstream Chat Completions usage to the Responses API shape and attach it to response.completed. Capture chunk.usage before the empty-choices guard so the usage-only trailer chunk survives, and defer completion to flushEvents() when usage is not yet known — only on the direct openai:openai-responses route, since a pivoted stream never reaches flushEvents. Fixes #3432.
This commit is contained in:
82030615
2026-09-21 21:24:11 +07:00
committed by decolua
parent 402745dc1f
commit da0046550a
3 changed files with 220 additions and 5 deletions

View File

@@ -14,13 +14,47 @@ import { ROLE, OPENAI_BLOCK, RESPONSES_ITEM, OPENAI_FINISH, MODEL_FALLBACK } fro
* Translate OpenAI chunk to Responses API events
* @returns {Array} Array of events with { event, data } structure
*/
// Upstream Chat Completions usage -> Responses API usage shape.
// Without this, /v1/responses never reports usage: Responses clients (Codex CLI)
// keep their "context used" gauge pinned at 0 and never auto-compact, so a long
// session grows until the upstream context limit rejects it (9router issue #3432).
//
// Note this is stored under state.responsesUsage, NOT state.usage: state.usage is
// owned by the stream layer, which fills it with normalizeUsage()-shaped counts
// (prompt_tokens/prompt_tokens_details) and hands it to finalizeStream() for
// logging and cost accounting. Overwriting it with this shape silently drops
// cached/reasoning tokens from those stats.
function toResponsesUsage(usage) {
if (!usage || typeof usage !== "object") return null;
const inputTokens = [usage.input_tokens, usage.prompt_tokens].find(Number.isFinite) ?? 0;
const outputTokens = [usage.output_tokens, usage.completion_tokens].find(Number.isFinite) ?? 0;
const responseUsage = {
input_tokens: inputTokens,
output_tokens: outputTokens,
total_tokens: Number.isFinite(usage.total_tokens) ? usage.total_tokens : inputTokens + outputTokens
};
const cachedTokens = [usage.input_tokens_details?.cached_tokens, usage.prompt_tokens_details?.cached_tokens].find(Number.isFinite);
const reasoningTokens = [usage.output_tokens_details?.reasoning_tokens, usage.completion_tokens_details?.reasoning_tokens].find(Number.isFinite);
if (Number.isFinite(cachedTokens)) responseUsage.input_tokens_details = { cached_tokens: cachedTokens };
if (Number.isFinite(reasoningTokens)) responseUsage.output_tokens_details = { reasoning_tokens: reasoningTokens };
return responseUsage;
}
export function openaiToOpenAIResponsesResponse(chunk, state) {
if (!chunk) {
return flushEvents(state);
}
// Capture upstream usage BEFORE the choices guard below: the last OpenAI chunk
// may carry usage together with an empty choices array, and it must not be dropped.
if (chunk.usage) {
state.responsesUsage = toResponsesUsage(chunk.usage);
}
if (!chunk.choices?.length) return [];
const events = [];
const nextSeq = () => ++state.seq;
@@ -112,7 +146,19 @@ export function openaiToOpenAIResponsesResponse(chunk, state) {
for (const i in state.msgItemAdded) closeMessage(state, emit, i);
closeReasoning(state, emit);
for (const i in state.funcCallIds) closeToolCall(state, emit, i);
sendCompleted(state, emit);
// Upstreams report usage either on the finish chunk itself or on a trailing chunk
// whose `choices` array is empty (OpenAI does the latter). Emitting
// response.completed here would freeze the payload before that trailing chunk is
// parsed, so when usage is not known yet we leave completion to flushEvents(),
// which runs once the upstream stream ends and by then has seen every chunk.
//
// That only holds on the direct openai:openai-responses route. When this converter
// runs as the second hop of a pivot (Claude/Gemini/Kiro upstream), translateResponse()
// drops the terminal null chunk before reaching us — the first hop returns null for
// it, leaving nothing to iterate — so flushEvents() is never called and deferring
// would swallow the terminal event entirely. Keep the old behaviour there.
const flushReachesUs = state.targetFormat === FORMATS.OPENAI;
if (state.responsesUsage || !flushReachesUs) sendCompleted(state, emit);
}
return events;
@@ -376,7 +422,8 @@ function sendCompleted(state, emit) {
created_at: state.created,
status: "completed",
background: false,
error: null
error: null,
...(state.responsesUsage ? { usage: state.responsesUsage } : {})
}
});
}

View File

@@ -60,7 +60,13 @@ export function createSSEStream(options = {}) {
const decoder = new TextDecoder("utf-8", { fatal: false });
const state = mode === STREAM_MODE.TRANSLATE
? { ...initState(sourceFormat), provider, toolNameMap, customToolNames: new Set(customToolNames || []), model, sessionId: credentials?._clientSessionId || null }
? { ...initState(sourceFormat), provider, toolNameMap, customToolNames: new Set(customToolNames || []), model, sessionId: credentials?._clientSessionId || null,
// Which upstream format this stream came from. A response translator can be
// reached either directly (target === its registered source) or as the second
// hop of a pivot, and on the terminal null chunk the pivot drops it — so a
// translator that defers closing events until flush needs to know which case
// it is in. Absent/undefined means "unknown", i.e. do not defer.
targetFormat }
: null;
let totalContentLength = 0;