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

@@ -291,15 +291,35 @@ function getContentBlocksFromMessage(msg, toolNameMap = new Map()) {
return blocks;
}
// Convert OpenAI tool choice to Claude format
// Convert OpenAI tool choice to Claude format.
// Claude only accepts tool_choice.type of "auto" | "any" | "tool" | "none";
// anything else (e.g. OpenAI's "function") triggers a 400, so we never pass an
// unrecognized type through.
const CLAUDE_TOOL_CHOICE_TYPES = new Set(["auto", "any", "tool", "none"]);
function convertOpenAIToolChoice(choice) {
if (!choice) return { type: "auto" };
if (typeof choice === "object" && choice.type) return choice;
if (choice === "auto" || choice === "none") return { type: "auto" };
if (choice === "required") return { type: "any" };
if (typeof choice === "object" && choice.function) {
return { type: "tool", name: choice.function.name };
// OpenAI string forms: "auto" | "none" | "required"
if (typeof choice === "string") {
if (choice === "required") return { type: "any" };
return { type: "auto" }; // "auto", "none", or anything unexpected
}
if (typeof choice === "object") {
// OpenAI forced tool: { type: "function", function: { name } }.
// Checked before the native pass-through below, because the OpenAI shape
// also carries a `.type` ("function") that Claude rejects.
if (choice.function?.name) {
return { type: "tool", name: choice.function.name };
}
// Already Claude-native — only pass through types Claude actually accepts,
// so a malformed or unknown type can never leak into the upstream request.
if (CLAUDE_TOOL_CHOICE_TYPES.has(choice.type)) {
return choice;
}
}
return { type: "auto" };
}

View File

@@ -35,13 +35,16 @@ export function cloakClaudeTools(body) {
const tools = body.tools;
if (!tools || tools.length === 0) return { body, toolNameMap: null };
const suffix = (name) => `${name}${CLAUDE_TOOL_SUFFIX}`;
const toolNameMap = new Map();
const clientToolNames = new Set();
const clientDeclarations = [];
// All client tools get renamed with suffix
for (const tool of tools) {
const suffixed = `${tool.name}${CLAUDE_TOOL_SUFFIX}`;
const suffixed = suffix(tool.name);
toolNameMap.set(suffixed, tool.name);
clientToolNames.add(tool.name);
clientDeclarations.push({ ...tool, name: suffixed });
}
@@ -51,17 +54,27 @@ export function cloakClaudeTools(body) {
// Rename tool_use in message history (all client tools get suffix)
const renamedMessages = body.messages?.map(msg => {
if (!Array.isArray(msg.content)) return msg;
const renamedContent = msg.content.map(block => {
if (block.type === "tool_use") {
return { ...block, name: `${block.name}${CLAUDE_TOOL_SUFFIX}` };
}
return block;
});
const renamedContent = msg.content.map(block =>
block.type === "tool_use" ? { ...block, name: suffix(block.name) } : block
);
return { ...msg, content: renamedContent };
});
const cloakedBody = { ...body, tools: allTools, messages: renamedMessages || body.messages };
// A forced tool_choice ({ type: "tool", name }) must point at the suffixed
// tool name, otherwise Claude rejects it: "Tool '<name>' not found in provided tools".
// Only rewrite when the choice targets one of the client tools we actually
// renamed — never a decoy/built-in name (those are sent unsuffixed).
if (
body.tool_choice?.type === "tool" &&
clientToolNames.has(body.tool_choice.name)
) {
cloakedBody.tool_choice = { ...body.tool_choice, name: suffix(body.tool_choice.name) };
}
return {
body: { ...body, tools: allTools, messages: renamedMessages || body.messages },
body: cloakedBody,
toolNameMap: toolNameMap.size > 0 ? toolNameMap : null
};
}