Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix: eliminate injectedUsage reuse bug and add reasoning alias tests

Mikhail Salnikov4083447c3f41
diff --git a/open-sse/utils/stream.ts b/open-sse/utils/stream.ts
index 6c274663d7..bcf8eedb3b 100644
--- a/open-sse/utils/stream.ts
+++ b/open-sse/utils/stream.ts
@@ -304,6 +304,14 @@ export function createSSEStream(options: StreamOptions = {}) {
}
} else {
// Chat Completions: full sanitization pipeline
+
+ // Detect reasoning alias before sanitization strips it
+ const hadReasoningAlias = !!(
+ parsed.choices?.[0]?.delta?.reasoning &&
+ typeof parsed.choices[0].delta.reasoning === "string" &&
+ !parsed.choices[0].delta.reasoning_content
+ );
+
parsed = sanitizeStreamingChunk(parsed);
const idFixed = fixInvalidId(parsed);
@@ -314,12 +322,6 @@ export function createSSEStream(options: StreamOptions = {}) {
const delta = parsed.choices?.[0]?.delta;
- // Normalize `reasoning` alias → `reasoning_content` (NVIDIA kimi-k2.5 etc.)
- if (delta?.reasoning && typeof delta.reasoning === "string" && !delta.reasoning_content) {
- delta.reasoning_content = delta.reasoning;
- delete delta.reasoning;
- }
-
// Extract <think> tags from streaming content
if (delta?.content && typeof delta.content === "string") {
const { content, thinking } = extractThinkingFromContent(delta.content);
@@ -329,13 +331,10 @@ export function createSSEStream(options: StreamOptions = {}) {
}
}
- // If reasoning was normalized (reasoning → reasoning_content) or <think>
- // tags were extracted, force re-serialization so the client sees
- // the standard `reasoning_content` field instead of the raw provider line.
- if (delta?.reasoning_content && !injectedUsage) {
- output = `data: ${JSON.stringify(parsed)}\n`;
- injectedUsage = true;
- }
+ // Track whether we need to re-serialize (separate from injectedUsage
+ // to avoid blocking subsequent finish_reason / usage mutations)
+ const needsReserialization =
+ hadReasoningAlias || (delta?.content === "" && delta?.reasoning_content);
// T18: Track if we saw tool calls & accumulate for call log
if (delta?.tool_calls && delta.tool_calls.length > 0) {
@@ -412,7 +411,7 @@ export function createSSEStream(options: StreamOptions = {}) {
parsed.usage = filterUsageForFormat(buffered, FORMATS.OPENAI);
output = `data: ${JSON.stringify(parsed)}\n`;
injectedUsage = true;
- } else if (idFixed) {
+ } else if (idFixed || needsReserialization) {
output = `data: ${JSON.stringify(parsed)}\n`;
injectedUsage = true;
}
@@ -498,7 +497,10 @@ export function createSSEStream(options: StreamOptions = {}) {
}
}
// Normalize `reasoning` alias → `reasoning_content` (NVIDIA kimi-k2.5 etc.)
- if (parsed.choices?.[0]?.delta?.reasoning && !parsed.choices?.[0]?.delta?.reasoning_content) {
+ if (
+ parsed.choices?.[0]?.delta?.reasoning &&
+ !parsed.choices?.[0]?.delta?.reasoning_content
+ ) {
const r = parsed.choices[0].delta.reasoning;
if (typeof r === "string") {
parsed.choices[0].delta.reasoning_content = r;
diff --git a/tests/unit/plan3-p0.test.mjs b/tests/unit/plan3-p0.test.mjs
index 6fa213b929..b59cd4824c 100644
--- a/tests/unit/plan3-p0.test.mjs
+++ b/tests/unit/plan3-p0.test.mjs
@@ -503,3 +503,29 @@ test("parseSSEToOpenAIResponse merges split tool call chunks by id without dupli
assert.equal(parsed.choices[0].message.tool_calls[0].function.name, "sum");
assert.equal(parsed.choices[0].message.tool_calls[0].function.arguments, '{"a":1}');
});
+
+test("parseSSEToOpenAIResponse normalizes delta.reasoning alias to reasoning_content", () => {
+ const rawSSE = [
+ `data: ${JSON.stringify({
+ id: "chatcmpl_2",
+ object: "chat.completion.chunk",
+ choices: [{ index: 0, delta: { reasoning: "Let me think..." } }],
+ })}`,
+ `data: ${JSON.stringify({
+ id: "chatcmpl_2",
+ object: "chat.completion.chunk",
+ choices: [{ index: 0, delta: { reasoning: " The answer is 4." } }],
+ })}`,
+ `data: ${JSON.stringify({
+ id: "chatcmpl_2",
+ object: "chat.completion.chunk",
+ choices: [{ index: 0, delta: { content: "2+2=4" }, finish_reason: "stop" }],
+ })}`,
+ "data: [DONE]",
+ ].join("\n");
+
+ const parsed = parseSSEToOpenAIResponse(rawSSE, "moonshotai/kimi-k2.5");
+ assert.ok(parsed);
+ assert.equal(parsed.choices[0].message.reasoning_content, "Let me think... The answer is 4.");
+ assert.equal(parsed.choices[0].message.content, "2+2=4");
+});
diff --git a/tests/unit/request-log-payloads.test.mjs b/tests/unit/request-log-payloads.test.mjs
index dc6ae405f7..6a4ece1e07 100644
--- a/tests/unit/request-log-payloads.test.mjs
+++ b/tests/unit/request-log-payloads.test.mjs
@@ -155,3 +155,51 @@ test("builds compact Claude stream summary for detailed logs", () => {
assert.equal(compact.usage.output_tokens, 7);
assert.equal(compact._omniroute_stream.eventCount, 4);
});
+
+test("builds compact OpenAI summary with reasoning alias (delta.reasoning)", () => {
+ const collector = createStructuredSSECollector({ stage: "provider_response" });
+
+ collector.push({
+ id: "chatcmpl_r1",
+ object: "chat.completion.chunk",
+ created: 100,
+ model: "moonshotai/kimi-k2.5",
+ choices: [{ index: 0, delta: { role: "assistant" } }],
+ });
+ collector.push({
+ id: "chatcmpl_r1",
+ object: "chat.completion.chunk",
+ created: 100,
+ model: "moonshotai/kimi-k2.5",
+ choices: [{ index: 0, delta: { reasoning: "Let me think..." } }],
+ });
+ collector.push({
+ id: "chatcmpl_r1",
+ object: "chat.completion.chunk",
+ created: 100,
+ model: "moonshotai/kimi-k2.5",
+ choices: [{ index: 0, delta: { content: "The answer is 4." } }],
+ });
+ collector.push({
+ id: "chatcmpl_r1",
+ object: "chat.completion.chunk",
+ created: 100,
+ model: "moonshotai/kimi-k2.5",
+ choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
+ usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 },
+ });
+
+ const summary = buildStreamSummaryFromEvents(
+ collector.getEvents(),
+ FORMATS.OPENAI,
+ "moonshotai/kimi-k2.5"
+ );
+ const compact = compactStructuredStreamPayload(
+ collector.build(summary, { includeEvents: false })
+ );
+
+ assert.equal(compact.object, "chat.completion");
+ assert.equal(compact.choices[0].message.content, "The answer is 4.");
+ assert.equal(compact.choices[0].message.reasoning_content, "Let me think...");
+ assert.equal(compact.choices[0].finish_reason, "stop");
+});