fix(tool-remapper): selectively remap tool names based on request context (#1852)

This commit is contained in:
Antigravity Assistant
2026-05-01 12:56:29 -03:00
parent 8f14452614
commit a906bda7fc

View File

@@ -38,7 +38,10 @@ for (const [k, v] of Object.entries(TOOL_RENAME_MAP)) {
REVERSE_MAP[v] = k;
}
export function remapToolNamesInRequest(body: Record<string, unknown>): void {
export function remapToolNamesInRequest(body: Record<string, unknown>): boolean {
let hasLowercase = false;
let hasTitleCase = false;
// Remap tool definitions
const tools = body.tools as Array<Record<string, unknown>> | undefined;
if (Array.isArray(tools)) {
@@ -46,6 +49,9 @@ export function remapToolNamesInRequest(body: Record<string, unknown>): 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<string, unknown>): 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<string, unknown>): void {
const toolChoice = body.tool_choice as Record<string, unknown> | 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