fix(claude): forced tool_choice 400 on cc/ OAuth route

convertOpenAIToolChoice mapped {type:"function"} verbatim and cloakClaudeTools
left tool_choice.name unsuffixed, both rejected by Claude on the cc/ path.
Map forced-function to {type:"tool",name}, allowlist Claude-valid types, and
suffix tool_choice.name when it targets a renamed client tool.

Fixes #1592

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Claude Code
2026-06-06 12:54:01 +07:00
committed by decolua
parent 64f58420db
commit c785051360
4 changed files with 167 additions and 14 deletions

View File

@@ -122,6 +122,50 @@ describe("openaiToClaudeRequest", () => {
expect(systemText).toContain("You must respond with valid JSON");
});
});
describe("tool_choice handling", () => {
const baseBody = {
messages: [{ role: "user", content: "add a todo" }],
tools: [{
type: "function",
function: { name: "todo_write", description: "write todos", parameters: { type: "object", properties: {} } }
}]
};
const choiceOf = (tc) =>
openaiToClaudeRequest("claude-sonnet-4.5", { ...baseBody, tool_choice: tc }, false).tool_choice;
it("converts OpenAI forced tool ({type:'function'}) to Claude {type:'tool'}", () => {
// Must NOT leak the OpenAI "function" type — Claude only accepts auto|any|tool|none.
expect(choiceOf({ type: "function", function: { name: "todo_write" } }))
.toEqual({ type: "tool", name: "todo_write" });
});
it("maps string tool_choice values", () => {
expect(choiceOf("auto")).toEqual({ type: "auto" });
expect(choiceOf("none")).toEqual({ type: "auto" });
expect(choiceOf("required")).toEqual({ type: "any" });
});
it("passes through Claude-native tool_choice objects unchanged", () => {
expect(choiceOf({ type: "tool", name: "todo_write" })).toEqual({ type: "tool", name: "todo_write" });
expect(choiceOf({ type: "any" })).toEqual({ type: "any" });
expect(choiceOf({ type: "none" })).toEqual({ type: "none" });
});
it("never leaks an invalid type (falls back to auto)", () => {
// Malformed forced choice with no tool name, and unknown types, must not
// pass an invalid `type` through to Claude.
expect(choiceOf({ type: "function", function: {} })).toEqual({ type: "auto" });
expect(choiceOf({ type: "function" })).toEqual({ type: "auto" });
expect(choiceOf({ type: "bogus" })).toEqual({ type: "auto" });
});
it("omits tool_choice entirely when the request has none", () => {
const result = openaiToClaudeRequest("claude-sonnet-4.5", baseBody, false);
expect(result.tool_choice).toBeUndefined();
});
});
});
describe("openaiToClaudeResponse", () => {