fix(agents): use alphanumeric-only tool call IDs for OpenRouter compatibility

Some providers like Mistral via OpenRouter require strictly alphanumeric
tool call IDs. The error message indicates: "Tool call id was
whatsapp_login_1768799841527_1 but must be a-z, A-Z, 0-9, with a length
of 9."

Changes:
- Update sanitizeToolCallId to strip all non-alphanumeric characters
  (previously allowed underscores and hyphens)
- Update makeUniqueToolId to use alphanumeric suffixes (x2, x3, etc.)
  instead of underscores
- Update isValidCloudCodeAssistToolId to validate alphanumeric-only IDs
- Update tests to reflect stricter sanitization

Fixes #1359

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
zerone0x
2026-01-21 17:49:10 +08:00
committed by Peter Steinberger
parent 39b375e32b
commit d0f9e22a4b
5 changed files with 64 additions and 30 deletions

View File

@@ -7,15 +7,15 @@ import {
} from "./tool-call-id.js";
describe("sanitizeToolCallIdsForCloudCodeAssist", () => {
it("is a no-op for already-valid non-colliding IDs", () => {
it("is a no-op for already-valid non-colliding alphanumeric IDs", () => {
const input = [
{
role: "assistant",
content: [{ type: "toolCall", id: "call_1", name: "read", arguments: {} }],
content: [{ type: "toolCall", id: "call1", name: "read", arguments: {} }],
},
{
role: "toolResult",
toolCallId: "call_1",
toolCallId: "call1",
toolName: "read",
content: [{ type: "text", text: "ok" }],
},
@@ -25,6 +25,35 @@ describe("sanitizeToolCallIdsForCloudCodeAssist", () => {
expect(out).toBe(input);
});
it("strips underscores from tool call IDs (Mistral/OpenRouter compatibility)", () => {
const input = [
{
role: "assistant",
content: [
{ type: "toolCall", id: "whatsapp_login_1768799841527_1", name: "login", arguments: {} },
],
},
{
role: "toolResult",
toolCallId: "whatsapp_login_1768799841527_1",
toolName: "login",
content: [{ type: "text", text: "ok" }],
},
] satisfies AgentMessage[];
const out = sanitizeToolCallIdsForCloudCodeAssist(input);
expect(out).not.toBe(input);
const assistant = out[0] as Extract<AgentMessage, { role: "assistant" }>;
const toolCall = assistant.content?.[0] as { id?: string };
// ID should be alphanumeric only, no underscores
expect(toolCall.id).toBe("whatsapplogin17687998415271");
expect(isValidCloudCodeAssistToolId(toolCall.id as string)).toBe(true);
const result = out[1] as Extract<AgentMessage, { role: "toolResult" }>;
expect(result.toolCallId).toBe(toolCall.id);
});
it("avoids collisions when sanitization would produce duplicate IDs", () => {
const input = [
{