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
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
// Regression test for issue #5123 — </think> leaks as delta.content before tool_calls
// in Claude→OpenAI streaming translation, corrupting OpenAI-compatible clients (Kimi Coding).
//
// Root cause: content_block_stop for a thinking block unconditionally emitted
// createChunk(state, { content: "</think>" }) even when the next block was tool_use.
//
// Fix: defer </think> emission. Only flush when the next event is a text_delta
// (preserving the #4633 behavior for Claude Code / Cursor) or at message finish
// when there are no tool_calls.
import test from "node:test";
import assert from "node:assert/strict";
const { claudeToOpenAIResponse } = await import(
"../../open-sse/translator/response/claude-to-openai.ts"
);
function newState() {
return {
toolCalls: new Map(),
toolNameMap: new Map(),
messageId: "msg_test",
model: "claude-3-7-sonnet",
toolCallIndex: 0,
};
}
/** Minimal shape these assertions read off a translated SSE chunk. */
type StreamChunk = { choices?: Array<{ delta?: { content?: unknown } }> };
function collectChunks(results: ReturnType<typeof claudeToOpenAIResponse>[]): StreamChunk[] {
// The translator returns a chunk, an array of chunks, or nothing; narrow once here so
// the call sites below can read `choices[0].delta.content` without per-callback casts.
return results.flatMap((r) => (Array.isArray(r) ? r : r ? [r] : [])) as StreamChunk[];
}
// ─── Case (a): thinking block followed by tool_use ───────────────────────────
// This is the regression case. Before the fix, </think> appears as a spurious
// assistant text chunk right before the tool_calls delta, corrupting clients.
test("thinking block followed by tool_use: </think> must NOT appear in any content chunk", () => {
const state = newState();
const allResults: ReturnType<typeof claudeToOpenAIResponse>[] = [];
// message_start
allResults.push(
claudeToOpenAIResponse({ type: "message_start", message: { id: "msg_1", model: "claude-3-7-sonnet" } }, state)
);
// thinking block open
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } },
state
)
);
// thinking delta
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: "Let me think..." } },
state
)
);
// thinking block stop — currently emits </think> unconditionally (the bug)
allResults.push(claudeToOpenAIResponse({ type: "content_block_stop", index: 0 }, state));
// tool_use block open
allResults.push(
claudeToOpenAIResponse(
{
type: "content_block_start",
index: 1,
content_block: { type: "tool_use", id: "toolu_01", name: "get_weather" },
},
state
)
);
// tool arguments delta
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_delta", index: 1, delta: { type: "input_json_delta", partial_json: '{"city":"Paris"}' } },
state
)
);
// tool_use block stop
allResults.push(claudeToOpenAIResponse({ type: "content_block_stop", index: 1 }, state));
// message_delta with stop_reason=tool_use
allResults.push(
claudeToOpenAIResponse(
{ type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 42 } },
state
)
);
const chunks = collectChunks(allResults);
const spuriousThinkClose = chunks.filter(
(chunk) => chunk?.choices?.[0]?.delta?.content === "</think>"
);
assert.equal(
spuriousThinkClose.length,
0,
`Expected NO chunk with delta.content === "</think>" before tool_calls, but found ${spuriousThinkClose.length}:\n${JSON.stringify(spuriousThinkClose, null, 2)}`
);
});
// ─── Case (b): thinking block followed by text (pure-text response) ──────────
// #4633 opt-in path: when suppressThinkClose is false (header `on`), </think>
// is still emitted for legacy content-scanning clients. Production default
// (#8245) sets suppressThinkClose=true via resolveSuppressThinkClose.
test("thinking block followed by text: </think> emitted when suppressThinkClose=false (#4633 opt-in)", () => {
const state = { ...newState(), suppressThinkClose: false };
const allResults: ReturnType<typeof claudeToOpenAIResponse>[] = [];
// message_start
allResults.push(
claudeToOpenAIResponse({ type: "message_start", message: { id: "msg_2", model: "claude-3-7-sonnet" } }, state)
);
// thinking block open
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } },
state
)
);
// thinking delta
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: "Plan..." } },
state
)
);
// thinking block stop
allResults.push(claudeToOpenAIResponse({ type: "content_block_stop", index: 0 }, state));
// text block open
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_start", index: 1, content_block: { type: "text", text: "" } },
state
)
);
// text delta (this should trigger the deferred </think> flush)
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_delta", index: 1, delta: { type: "text_delta", text: "Hello!" } },
state
)
);
// text block stop
allResults.push(claudeToOpenAIResponse({ type: "content_block_stop", index: 1 }, state));
// message_delta with stop_reason=end_turn
allResults.push(
claudeToOpenAIResponse(
{ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 10 } },
state
)
);
const chunks = collectChunks(allResults);
const hasThinkClose = chunks.some(
(chunk) => chunk?.choices?.[0]?.delta?.content === "</think>"
);
assert.ok(
hasThinkClose,
`Expected a chunk with delta.content === "</think>" in a pure-text thinking response, but none found.\nAll chunks: ${JSON.stringify(chunks, null, 2)}`
);
});
// #8245 production default: suppressThinkClose=true → no literal marker in content.
test("thinking block followed by text: </think> suppressed when suppressThinkClose=true (#8245)", () => {
const state = { ...newState(), suppressThinkClose: true };
const allResults: ReturnType<typeof claudeToOpenAIResponse>[] = [];
allResults.push(
claudeToOpenAIResponse({ type: "message_start", message: { id: "msg_3", model: "claude-3-7-sonnet" } }, state)
);
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } },
state
)
);
allResults.push(
claudeToOpenAIResponse(
{
type: "content_block_delta",
index: 0,
delta: { type: "thinking_delta", thinking: "Plan..." },
},
state
)
);
allResults.push(claudeToOpenAIResponse({ type: "content_block_stop", index: 0 }, state));
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_start", index: 1, content_block: { type: "text", text: "" } },
state
)
);
allResults.push(
claudeToOpenAIResponse(
{ type: "content_block_delta", index: 1, delta: { type: "text_delta", text: "Hello!" } },
state
)
);
const chunks = collectChunks(allResults);
const hasThinkClose = chunks.some(
(chunk) => chunk?.choices?.[0]?.delta?.content === "</think>"
);
assert.equal(hasThinkClose, false, "marker must not leak into content under #8245 default");
const hasText = chunks.some((chunk) => chunk?.choices?.[0]?.delta?.content === "Hello!");
assert.ok(hasText, "assistant text must still be emitted");
});