Repository navigation
fix(translator): provider thinking compatibility (DeepSeek/Gemini) #4946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 1 commit into
release/v3.8.36
from
fix/port-pr-2043-thinking-compat
Jun 24, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
tests/unit/translator-thinking-provider-compat-2043.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /** | ||
| * Tests for provider thinking compatibility fixes ported from decolua/9router#2043. | ||
| * | ||
| * Covers: | ||
| * (a) DeepSeek unsigned thinking placeholders — no `signature` field in injected blocks. | ||
| * (b) DeepSeek keeps existing thinking blocks as-is (not overwritten by cache/placeholder). | ||
| * (c) Gemini `reasoning_effort: "auto"` maps to high budget (not silent fallback to default). | ||
| * (d) Gemini `reasoning_effort: "max"` / `"xhigh"` clamp to high budget. | ||
| */ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| const { prepareClaudeRequest } = await import("../../open-sse/translator/helpers/claudeHelper.ts"); | ||
| const reasoningCache = await import("../../open-sse/services/reasoningCache.ts"); | ||
| const { openaiToGeminiRequest } = | ||
| await import("../../open-sse/translator/request/openai-to-gemini.ts"); | ||
| const { capThinkingBudget } = await import("../../src/lib/modelCapabilities.ts"); | ||
|
|
||
| // ──────────────── Fix (a): DeepSeek placeholder has NO signature field ──────────────── | ||
|
|
||
| test("deepseek: injected thinking placeholder has no signature field", () => { | ||
| reasoningCache.clearReasoningCacheAll(); | ||
| const body = { | ||
| model: "deepseek-v4-pro", | ||
| thinking: { type: "enabled", budget_tokens: 4096 }, | ||
| messages: [ | ||
| { role: "user", content: [{ type: "text", text: "q" }] }, | ||
| { | ||
| role: "assistant", | ||
| content: [{ type: "tool_use", id: "toolu_1", name: "Read", input: { file_path: "x" } }], | ||
| }, | ||
| { | ||
| role: "user", | ||
| content: [{ type: "tool_result", tool_use_id: "toolu_1", content: "ok" }], | ||
| }, | ||
| ], | ||
| }; | ||
| const out = prepareClaudeRequest(body as any, "deepseek"); | ||
| const assistant = (out as any).messages.find((m: any) => m.role === "assistant"); | ||
| assert.ok(assistant, "assistant message should exist"); | ||
| assert.equal(assistant.content[0].type, "thinking", "injected block should be type=thinking"); | ||
| assert.equal( | ||
| assistant.content[0].signature, | ||
| undefined, | ||
| "DeepSeek placeholder must NOT have a signature field" | ||
| ); | ||
| assert.equal(assistant.content[1].type, "tool_use", "tool_use should follow the thinking block"); | ||
| }); | ||
|
|
||
| // ──────────────── Fix (b): DeepSeek keeps existing thinking blocks as-is ──────────────── | ||
|
|
||
| test("deepseek: existing thinking blocks are preserved as-is (text and type unchanged)", () => { | ||
| reasoningCache.clearReasoningCacheAll(); | ||
| const body = { | ||
| model: "deepseek-v4-pro", | ||
| thinking: { type: "enabled", budget_tokens: 4096 }, | ||
| messages: [ | ||
| { role: "user", content: [{ type: "text", text: "q" }] }, | ||
| { | ||
| role: "assistant", | ||
| content: [ | ||
| { type: "thinking", thinking: "my actual reasoning", signature: "ds-sig" }, | ||
| { type: "tool_use", id: "toolu_2", name: "Read", input: {} }, | ||
| ], | ||
| }, | ||
| { | ||
| role: "user", | ||
| content: [{ type: "tool_result", tool_use_id: "toolu_2", content: "ok" }], | ||
| }, | ||
| ], | ||
| }; | ||
| const out = prepareClaudeRequest(body as any, "deepseek"); | ||
| const assistant = (out as any).messages.find((m: any) => m.role === "assistant"); | ||
| assert.equal(assistant.content[0].type, "thinking"); | ||
| assert.equal( | ||
| assistant.content[0].thinking, | ||
| "my actual reasoning", | ||
| "DeepSeek must keep existing thinking text verbatim" | ||
| ); | ||
| // Signature stripping is acceptable (DeepSeek doesn't need it), but text must not | ||
| // be overwritten with placeholder or cache content. | ||
| assert.ok( | ||
| assistant.content.length >= 2, | ||
| "should not drop blocks: thinking + tool_use both present" | ||
| ); | ||
| assert.equal(assistant.content[1].type, "tool_use"); | ||
| }); | ||
|
|
||
| test("deepseek: injected placeholder thinking text is non-empty dot sentinel", () => { | ||
| // The upstream uses "." as placeholder text for DeepSeek (not the long NON_ANTHROPIC placeholder) | ||
| reasoningCache.clearReasoningCacheAll(); | ||
| const body = { | ||
| model: "deepseek-v4-pro", | ||
| thinking: { type: "enabled", budget_tokens: 4096 }, | ||
| messages: [ | ||
| { role: "user", content: [{ type: "text", text: "q" }] }, | ||
| { | ||
| role: "assistant", | ||
| content: [{ type: "tool_use", id: "toolu_3", name: "Write", input: {} }], | ||
| }, | ||
| { | ||
| role: "user", | ||
| content: [{ type: "tool_result", tool_use_id: "toolu_3", content: "done" }], | ||
| }, | ||
| ], | ||
| }; | ||
| const out = prepareClaudeRequest(body as any, "deepseek"); | ||
| const assistant = (out as any).messages.find((m: any) => m.role === "assistant"); | ||
| assert.equal(assistant.content[0].type, "thinking"); | ||
| // Placeholder must be non-empty (DeepSeek rejects empty thinking text). | ||
| // The value is either "." (upstream canonical) or another non-empty fallback. | ||
| assert.ok( | ||
| typeof assistant.content[0].thinking === "string" && assistant.content[0].thinking.length > 0, | ||
| `thinking placeholder must be non-empty, got: ${JSON.stringify(assistant.content[0].thinking)}` | ||
| ); | ||
| assert.equal( | ||
| assistant.content[0].signature, | ||
| undefined, | ||
| "no signature on injected DeepSeek block" | ||
| ); | ||
| }); | ||
|
|
||
| // ──────────────── Fix (c): Gemini reasoning_effort "auto" → high budget ──────────────── | ||
|
|
||
| test("Gemini: reasoning_effort 'auto' maps to a defined (non-zero) thinking budget", () => { | ||
| const out = openaiToGeminiRequest( | ||
| "gemini-3-pro", | ||
| { | ||
| messages: [{ role: "user", content: "hello" }], | ||
| reasoning_effort: "auto", | ||
| }, | ||
| false | ||
| ) as any; | ||
|
|
||
| const thinkingBudget = out.generationConfig?.thinkingConfig?.thinkingBudget; | ||
| assert.ok( | ||
| typeof thinkingBudget === "number" && thinkingBudget > 0, | ||
| `reasoning_effort 'auto' should produce a positive thinkingBudget, got: ${thinkingBudget}` | ||
| ); | ||
| // Specifically should be at least the high-tier budget level (clamped to model max) | ||
| const highBudget = capThinkingBudget("gemini-3-pro", 32768); | ||
| assert.equal( | ||
| thinkingBudget, | ||
| highBudget, | ||
| `reasoning_effort 'auto' should map to high budget (${highBudget})` | ||
| ); | ||
| }); | ||
|
|
||
| // ──────────────── Fix (d): Gemini reasoning_effort "max"/"xhigh" → high budget ──────────────── | ||
|
|
||
| test("Gemini: reasoning_effort 'max' clamps to high budget (not default fallback)", () => { | ||
| const out = openaiToGeminiRequest( | ||
| "gemini-3-pro", | ||
| { | ||
| messages: [{ role: "user", content: "hello" }], | ||
| reasoning_effort: "max", | ||
| }, | ||
| false | ||
| ) as any; | ||
|
|
||
| const thinkingBudget = out.generationConfig?.thinkingConfig?.thinkingBudget; | ||
| const highBudget = capThinkingBudget("gemini-3-pro", 32768); | ||
| assert.equal( | ||
| thinkingBudget, | ||
| highBudget, | ||
| `reasoning_effort 'max' should clamp to high budget (${highBudget})` | ||
| ); | ||
| }); | ||
|
|
||
| test("Gemini: reasoning_effort 'xhigh' clamps to high budget (not default fallback)", () => { | ||
| const out = openaiToGeminiRequest( | ||
| "gemini-3-pro", | ||
| { | ||
| messages: [{ role: "user", content: "hello" }], | ||
| reasoning_effort: "xhigh", | ||
| }, | ||
| false | ||
| ) as any; | ||
|
|
||
| const thinkingBudget = out.generationConfig?.thinkingConfig?.thinkingBudget; | ||
| const highBudget = capThinkingBudget("gemini-3-pro", 32768); | ||
| assert.equal( | ||
| thinkingBudget, | ||
| highBudget, | ||
| `reasoning_effort 'xhigh' should clamp to high budget (${highBudget})` | ||
| ); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the nullish coalescing operator (??) for getDefaultThinkingBudget(model) can result in a budget of 0 if the model's default thinking budget is not configured (which defaults to 0). Since a thinking budget of 0 is invalid for Gemini when thinking is enabled, this will cause API errors. We should use the logical OR operator (||) instead, ensuring that a falsy 0 correctly falls back to 8192.