diff --git a/open-sse/services/claudeCodeToolRemapper.ts b/open-sse/services/claudeCodeToolRemapper.ts index bc6c4724a0..034cba4f7e 100644 --- a/open-sse/services/claudeCodeToolRemapper.ts +++ b/open-sse/services/claudeCodeToolRemapper.ts @@ -38,7 +38,10 @@ for (const [k, v] of Object.entries(TOOL_RENAME_MAP)) { REVERSE_MAP[v] = k; } -export function remapToolNamesInRequest(body: Record): void { +export function remapToolNamesInRequest(body: Record): boolean { + let hasLowercase = false; + let hasTitleCase = false; + // Remap tool definitions const tools = body.tools as Array> | undefined; if (Array.isArray(tools)) { @@ -46,6 +49,9 @@ export function remapToolNamesInRequest(body: Record): void { const name = String(tool.name || ""); if (TOOL_RENAME_MAP[name]) { tool.name = TOOL_RENAME_MAP[name]; + hasLowercase = true; + } else if (REVERSE_MAP[name]) { + hasTitleCase = true; } } } @@ -59,7 +65,12 @@ export function remapToolNamesInRequest(body: Record): void { for (const block of content) { if (block.type === "tool_use" && typeof block.name === "string") { const mapped = TOOL_RENAME_MAP[block.name]; - if (mapped) block.name = mapped; + if (mapped) { + block.name = mapped; + hasLowercase = true; + } else if (REVERSE_MAP[block.name]) { + hasTitleCase = true; + } } } } @@ -69,11 +80,24 @@ export function remapToolNamesInRequest(body: Record): void { const toolChoice = body.tool_choice as Record | undefined; if (toolChoice?.type === "tool" && typeof toolChoice.name === "string") { const mapped = TOOL_RENAME_MAP[toolChoice.name]; - if (mapped) toolChoice.name = mapped; + if (mapped) { + toolChoice.name = mapped; + hasLowercase = true; + } else if (REVERSE_MAP[toolChoice.name]) { + hasTitleCase = true; + } } + + if (hasLowercase && !hasTitleCase) { + body._claudeCodeRequiresLowercaseToolNames = true; + } + + return hasLowercase && !hasTitleCase; } -export function remapToolNamesInResponse(text: string): string { +export function remapToolNamesInResponse(text: string, forceLowercase = true): string { + if (!forceLowercase) return text; + // Replace TitleCase tool names back to lowercase in SSE chunks for (const [titleCase, lower] of Object.entries(REVERSE_MAP)) { // Match in "name":"ToolName" patterns