mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-19 05:32:19 +03:00
* fix(translator): Normalize tool call names from lowercase to PascalCase (#1) * Fix: Map lowercase tool names from Antigravity (Gemini format) to Claude Code expected PascalCase * Fix: toolNameMap in fun restoreClaudePassthroughToolUseName * fix(translator): Normalize tool call names from lowercase to PascalCase when translating upstream responses (OpenAI, Gemini, Antigravity) to Claude Messages API format This resolves `Error: No such tool available: read`/`bash`/`write` errors when using Claude Code CLI with third-party providers that emit lowercase tool names. The fix adds case-insensitive tool name lookups in `openai-to-claude.ts`, `gemini-to-claude.ts`, and related translators, ensuring tool names like `read`/`bash` are mapped to `Read`/`Bash` before being sent to Claude Code. Includes unit tests and comprehensive changelog notes ([#10250](https://github.com/diegosouzapw/OmniRoute/pull/10250)) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com> * fix(translator): Parse <tool_call> JSON and TOOL_CALL text formats fr… (#2) * fix(translator): Parse <tool_call> JSON and TOOL_CALL text formats from model output Some models (DeepSeek, Qwen) emit tool calls as text instead of proper tool_calls JSON: either <tool_call>{...}</tool_call> or TOOL_CALL Name: {...}. Extend extractXmlInvokeBlocks to handle all 3 formats in a single scan pass, picking whichever pattern appears first. Includes unit tests for all formats. * fix(translator): Parse text-format tool calls in gemini-to-claude translator Extend the Gemini->Claude translator to detect <invoke>, <tool_call> JSON, and TOOL_CALL text formats emitted inline in text parts (Antigravity/Gemini models), converting them to proper tool_use content blocks instead of leaking raw text to Claude Code. * docs(changelog): Add changelog entry for text tool call parsing fix * fix(translator): consolidate tool name casing normalization and restore thought-signature persistence (#3) * fix(translator): sanitize tool_use.id and tool_result.tool_use_id to match Anthropic schema (#4) Ensure tool IDs from OpenAI-compatible upstreams (which may contain dots, colons, or special characters) are sanitized to ^[a-zA-Z0-9_-]+$ in response translators and passthrough requests before reaching Claude endpoints. * fix(responses): preserve native tools for openai-compatible Responses targets (#5) A Responses-shaped request to a custom openai-compatible connection whose outbound protocol is Responses took a Responses -> Chat -> Responses round trip, so Codex custom tools lost their grammar (`exec`), namespace groups were flattened (`collaboration`), and tool invocations failed upstream. Gate a native Responses passthrough on the connection's configured protocol (`apiType: "responses"` / `_omnirouteForceResponsesUpstream`) so the original tool definitions reach a Responses-capable upstream unchanged. Chat-only connections keep the existing downgrade. Closes #10374 * fix(translator): add support for 'applypatch' tool name in tool call checks * test(translator): add unit test for apply_patch and applypatch tool name remapping * fix(translator): remove no-explicit-any lint errors in tool-use-id-sanitization test Type the openaiToClaudeResponse/translateNonStreamingResponse return values with narrow local shapes instead of `any`, satisfying the repo's no-explicit-any = error rule for tests/. No behavior change — the same 3 assertions still pass. Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> * test: update 9568 casing regression to match #10392's consolidated fix restoreClaudeToolName's static casing map now normalizes known lowercase tool names to canonical PascalCase unconditionally on the gemini-to-claude and openai-to-claude Claude Messages API paths (not gated behind toolNameMap), superseding the earlier per-map-only fix that the original #9568 regression test locked in as "expected" (it was previously labeled a known bug case). The gemini-to-openai passthrough path is unaffected by #10392 and keeps its original pass-through assertion. Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
230 lines
8.8 KiB
TypeScript
230 lines
8.8 KiB
TypeScript
import { describe, it } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { geminiToClaudeResponse } from "../../open-sse/translator/response/gemini-to-claude.ts";
|
|
import { openaiToClaudeResponse } from "../../open-sse/translator/response/openai-to-claude.ts";
|
|
import { restoreClaudeToolName } from "../../open-sse/services/claudeCodeToolRemapper.ts";
|
|
import { restoreClaudePassthroughToolUseName } from "../../open-sse/utils/stream.ts";
|
|
import {
|
|
buildGeminiThoughtSignatureKey,
|
|
getGeminiThoughtSignature,
|
|
} from "../../open-sse/services/geminiThoughtSignatureStore.ts";
|
|
|
|
interface ClaudeEvent {
|
|
type: string;
|
|
index?: number;
|
|
content_block?: { type: string; id?: string; name?: string; input?: unknown };
|
|
}
|
|
|
|
type TranslatorState = Record<string, unknown>;
|
|
|
|
function firstToolUse(events: ClaudeEvent[] | null): ClaudeEvent["content_block"] {
|
|
return events?.find(
|
|
(e) => e.type === "content_block_start" && e.content_block?.type === "tool_use"
|
|
)?.content_block;
|
|
}
|
|
|
|
describe("Claude Code Tool Name Casing Fixes", () => {
|
|
it("restoreClaudeToolName maps lowercase tool names to PascalCase", () => {
|
|
assert.equal(restoreClaudeToolName("bash"), "Bash");
|
|
assert.equal(restoreClaudeToolName("read"), "Read");
|
|
assert.equal(restoreClaudeToolName("write"), "Write");
|
|
assert.equal(restoreClaudeToolName("websearch"), "WebSearch");
|
|
assert.equal(restoreClaudeToolName("webfetch"), "WebFetch");
|
|
assert.equal(restoreClaudeToolName("agent"), "Agent");
|
|
assert.equal(restoreClaudeToolName("unknown_third_party", null), "unknown_third_party");
|
|
});
|
|
|
|
it("restoreClaudeToolName covers apply_patch and applypatch variants", () => {
|
|
assert.equal(restoreClaudeToolName("apply_patch"), "ApplyPatch");
|
|
assert.equal(restoreClaudeToolName("applypatch"), "ApplyPatch");
|
|
});
|
|
|
|
it("restoreClaudeToolName covers the tools the 7-entry map missed", () => {
|
|
assert.equal(restoreClaudeToolName("todowrite"), "TodoWrite");
|
|
assert.equal(restoreClaudeToolName("glob"), "Glob");
|
|
assert.equal(restoreClaudeToolName("grep"), "Grep");
|
|
assert.equal(restoreClaudeToolName("task"), "Task");
|
|
assert.equal(restoreClaudeToolName("skill"), "Skill");
|
|
assert.equal(restoreClaudeToolName("multiedit"), "MultiEdit");
|
|
assert.equal(restoreClaudeToolName("askuserquestion"), "AskUserQuestion");
|
|
assert.equal(restoreClaudeToolName("exitplanmode"), "ExitPlanMode");
|
|
});
|
|
|
|
it("restoreClaudeToolName keeps the #7926 TitleCase→lowercase fallback with no map", () => {
|
|
// Clients with no request-side map (XML / OpenCode-style) expect lowercase.
|
|
assert.equal(restoreClaudeToolName("TodoWrite"), "todowrite");
|
|
assert.equal(restoreClaudeToolName("Read"), "read");
|
|
});
|
|
|
|
it("restoreClaudeToolName prefers toolNameMap over the static map", () => {
|
|
const toolNameMap = new Map([
|
|
["custom_read", "CustomRead"],
|
|
["read", "mcp__fs__read"],
|
|
]);
|
|
assert.equal(restoreClaudeToolName("custom_read", toolNameMap), "CustomRead");
|
|
// A request-side alias wins over the built-in casing table.
|
|
assert.equal(restoreClaudeToolName("read", toolNameMap), "mcp__fs__read");
|
|
// Names absent from the map still fall back to the static table.
|
|
assert.equal(restoreClaudeToolName("bash", toolNameMap), "Bash");
|
|
});
|
|
|
|
it("geminiToClaudeResponse normalizes lowercase tool names to PascalCase", () => {
|
|
const chunk = {
|
|
candidates: [
|
|
{
|
|
content: {
|
|
parts: [{ functionCall: { name: "todowrite", args: { todos: [] } } }],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
const state: TranslatorState = {};
|
|
const block = firstToolUse(geminiToClaudeResponse(chunk, state) as ClaudeEvent[]);
|
|
assert.equal(block?.name, "TodoWrite");
|
|
});
|
|
|
|
it("geminiToClaudeResponse honors state.toolNameMap ahead of the casing table", () => {
|
|
const chunk = {
|
|
candidates: [
|
|
{
|
|
content: { parts: [{ functionCall: { name: "read", args: {} } }] },
|
|
},
|
|
],
|
|
};
|
|
const state: TranslatorState = { toolNameMap: new Map([["read", "mcp__fs__read"]]) };
|
|
const block = firstToolUse(geminiToClaudeResponse(chunk, state) as ClaudeEvent[]);
|
|
assert.equal(block?.name, "mcp__fs__read");
|
|
});
|
|
|
|
it("geminiToClaudeResponse still persists thoughtSignature for follow-up turns (#8979)", () => {
|
|
const chunk = {
|
|
candidates: [
|
|
{
|
|
content: {
|
|
parts: [
|
|
{ thoughtSignature: "sig-abc123" },
|
|
{ functionCall: { id: "call_sig_1", name: "read", args: {} } },
|
|
],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
const state: TranslatorState = { signatureNamespace: "ns-test" };
|
|
const block = firstToolUse(geminiToClaudeResponse(chunk, state) as ClaudeEvent[]);
|
|
assert.equal(block?.name, "Read");
|
|
assert.equal(
|
|
getGeminiThoughtSignature(buildGeminiThoughtSignatureKey("ns-test", "call_sig_1")),
|
|
"sig-abc123"
|
|
);
|
|
assert.equal(state.pendingThoughtSignature, null);
|
|
});
|
|
|
|
it("geminiToClaudeResponse persists thoughtSignature for text-extracted tool calls", () => {
|
|
const chunk = {
|
|
candidates: [
|
|
{
|
|
content: {
|
|
parts: [
|
|
{
|
|
thoughtSignature: "sig-text-1",
|
|
text: '<tool_call>{"name":"bash","arguments":{"command":"ls"}}</tool_call>',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
const state: TranslatorState = { signatureNamespace: "ns-text" };
|
|
const events = geminiToClaudeResponse(chunk, state) as ClaudeEvent[];
|
|
const block = firstToolUse(events);
|
|
assert.equal(block?.name, "Bash");
|
|
assert.ok(block?.id);
|
|
assert.equal(
|
|
getGeminiThoughtSignature(buildGeminiThoughtSignatureKey("ns-text", block!.id!)),
|
|
"sig-text-1"
|
|
);
|
|
});
|
|
|
|
it("openaiToClaudeResponse normalizes lowercase tool names to PascalCase", () => {
|
|
const chunk = {
|
|
choices: [
|
|
{
|
|
delta: {
|
|
tool_calls: [
|
|
{ index: 0, id: "call_123", function: { name: "bash", arguments: "" } },
|
|
],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
const state: TranslatorState = { toolCalls: new Map(), nextBlockIndex: 0 };
|
|
const block = firstToolUse(openaiToClaudeResponse(chunk, state) as ClaudeEvent[]);
|
|
assert.equal(block?.name, "Bash");
|
|
});
|
|
|
|
it("openaiToClaudeResponse maps lowercase todowrite to TodoWrite", () => {
|
|
const chunk = {
|
|
choices: [
|
|
{
|
|
delta: {
|
|
tool_calls: [
|
|
{ index: 0, id: "call_todo", function: { name: "todowrite", arguments: "" } },
|
|
],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
const state: TranslatorState = { toolCalls: new Map(), nextBlockIndex: 0 };
|
|
const block = firstToolUse(openaiToClaudeResponse(chunk, state) as ClaudeEvent[]);
|
|
assert.equal(block?.name, "TodoWrite");
|
|
});
|
|
|
|
it("openaiToClaudeResponse restores request-side aliases via state.toolNameMap", () => {
|
|
const chunk = {
|
|
choices: [
|
|
{
|
|
delta: {
|
|
tool_calls: [
|
|
{ index: 0, id: "call_alias", function: { name: "SubDispatch", arguments: "" } },
|
|
],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
const state: TranslatorState = {
|
|
toolCalls: new Map(),
|
|
nextBlockIndex: 0,
|
|
toolNameMap: new Map([["SubDispatch", "subagents"]]),
|
|
};
|
|
const block = firstToolUse(openaiToClaudeResponse(chunk, state) as ClaudeEvent[]);
|
|
assert.equal(block?.name, "subagents");
|
|
});
|
|
|
|
it("restoreClaudePassthroughToolUseName maps lowercase names with no toolNameMap", () => {
|
|
const parsed = { content_block: { type: "tool_use", id: "tool_123", name: "read" } };
|
|
assert.equal(restoreClaudePassthroughToolUseName(parsed, null), true);
|
|
assert.equal(parsed.content_block.name, "Read");
|
|
});
|
|
|
|
it("restoreClaudePassthroughToolUseName maps lowercase todowrite to TodoWrite", () => {
|
|
const parsed = { content_block: { type: "tool_use", id: "tool_todo", name: "todowrite" } };
|
|
assert.equal(restoreClaudePassthroughToolUseName(parsed, null), true);
|
|
assert.equal(parsed.content_block.name, "TodoWrite");
|
|
});
|
|
|
|
it("restoreClaudePassthroughToolUseName respects toolNameMap when provided", () => {
|
|
const parsed = { content_block: { type: "tool_use", id: "tool_123", name: "custom_tool" } };
|
|
const toolNameMap = new Map([["custom_tool", "CustomTool"]]);
|
|
assert.equal(restoreClaudePassthroughToolUseName(parsed, toolNameMap), true);
|
|
assert.equal(parsed.content_block.name, "CustomTool");
|
|
});
|
|
|
|
it("restoreClaudePassthroughToolUseName preserves request-declared casing via toolNameMap", () => {
|
|
// With the request map present, PascalCase survives (no #7926 lowercasing).
|
|
const parsed = { content_block: { type: "tool_use", id: "t", name: "TodoWrite" } };
|
|
const toolNameMap = new Map([["TodoWrite", "TodoWrite"]]);
|
|
assert.equal(restoreClaudePassthroughToolUseName(parsed, toolNameMap), false);
|
|
assert.equal(parsed.content_block.name, "TodoWrite");
|
|
});
|
|
});
|