Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
fix(skills): avoid Claude assistant tool_result blocks (#2956)
terence71-glitch52503064a8e6
diff --git a/src/lib/skills/interception.ts b/src/lib/skills/interception.tsindex f26b954109..f026e3a6ab 100644--- a/src/lib/skills/interception.ts+++ b/src/lib/skills/interception.ts@@ -290,18 +290,52 @@ export async function handleToolCallExecution(};}- case "anthropic":+ case "anthropic": {+ // Anthropic only permits tool_result blocks in user messages. This helper+ // returns a single assistant response, so there is no valid place to put a+ // server-side skill result as tool_result here. Keep client-native tool_use+ // blocks untouched, remove the OmniRoute-handled tool_use blocks, and expose+ // their results as plain assistant text instead of corrupting history with+ // assistant-side tool_result blocks. See #2815.+ //+ // When no client-native tool_use blocks remain (all were handled here), the+ // upstream stop_reason "tool_use" is stale and would make clients wait for+ // tool_use blocks that no longer exist — so it is normalized to "end_turn"+ // (with stop_sequence cleared). The mixed-tool branch keeps the original+ // stop_reason because real native tool_use blocks still need the client.+ const handledToolCallIds = new Set(results.map((r) => r.id));+ const toolNamesById = new Map(toolCalls.map((call) => [call.id, call.name]));+ const remainingContent = (Array.isArray(response.content) ? response.content : []).filter(+ (block: any) => !(block?.type === "tool_use" && handledToolCallIds.has(block.id))+ );+ const resultTextBlocks = results.map((r) => ({+ type: "text",+ text: `[Skill result: ${toolNamesById.get(r.id) || r.id}]\n${JSON.stringify(+ r.result+ )}`,+ }));+ const firstRemainingToolUseIndex = remainingContent.findIndex(+ (block: any) => block?.type === "tool_use"+ );++ if (firstRemainingToolUseIndex === -1) {+ return {+ ...response,+ content: [...remainingContent, ...resultTextBlocks],+ stop_reason: "end_turn",+ stop_sequence: null,+ };+ }+return {...response,content: [- ...response.content,- ...results.map((r) => ({- type: "tool_result",- tool_use_id: r.id,- content: JSON.stringify(r.result),- })),+ ...remainingContent.slice(0, firstRemainingToolUseIndex),+ ...resultTextBlocks,+ ...remainingContent.slice(firstRemainingToolUseIndex),],};+ }default:return response;diff --git a/tests/unit/skills-interception.test.ts b/tests/unit/skills-interception.test.tsindex 3cc7b4491b..075b24452a 100644--- a/tests/unit/skills-interception.test.ts+++ b/tests/unit/skills-interception.test.ts@@ -215,9 +215,10 @@ test("handleToolCallExecution appends OpenAI tool results and leaves empty respoassert.equal(await handleToolCallExecution(untouched, "gpt-4.1", executionContext), untouched);});-test("handleToolCallExecution appends Anthropic tool_result blocks", async () => {+test("handleToolCallExecution returns Anthropic skill results as text", async () => {const anthropicResponse = await handleToolCallExecution({+ stop_reason: "tool_use",content: [{ type: "tool_use", id: "tool-1", name: "[email protected]", input: { id: "77" } }],},"claude-3-7-sonnet",@@ -225,13 +226,17 @@ test("handleToolCallExecution appends Anthropic tool_result blocks", async () =>);assert.deepEqual(anthropicResponse.content, [- { type: "tool_use", id: "tool-1", name: "[email protected]", input: { id: "77" } },{- type: "tool_result",- tool_use_id: "tool-1",- content: '{"record":"resolved:77"}',+ type: "text",+ text: '[Skill result: [email protected]]\n{"record":"resolved:77"}',},]);+ assert.equal(+ anthropicResponse.content.some((b: { type: string }) => b.type === "tool_result"),+ false+ );+ assert.equal(anthropicResponse.stop_reason, "end_turn");+ assert.equal(anthropicResponse.stop_sequence, null);});test("handleToolCallExecution appends Responses API function_call_output items", async () => {@@ -285,6 +290,7 @@ test("handleToolCallExecution forwards unregistered client-native tool_use untoutest("handleToolCallExecution intercepts a registered skill alongside an unregistered tool (#2815)", async () => {const mixed = await handleToolCallExecution({+ stop_reason: "tool_use",content: [{ type: "tool_use", id: "tool-native", name: "Bash", input: { command: "ls" } },{ type: "tool_use", id: "tool-skill", name: "[email protected]", input: { id: "9" } },@@ -295,14 +301,14 @@ test("handleToolCallExecution intercepts a registered skill alongside an unregis);assert.deepEqual(mixed.content, [- { type: "tool_use", id: "tool-native", name: "Bash", input: { command: "ls" } },- { type: "tool_use", id: "tool-skill", name: "[email protected]", input: { id: "9" } },{- type: "tool_result",- tool_use_id: "tool-skill",- content: '{"record":"resolved:9"}',+ type: "text",+ text: '[Skill result: [email protected]]\n{"record":"resolved:9"}',},+ { type: "tool_use", id: "tool-native", name: "Bash", input: { command: "ls" } },]);+ assert.equal(mixed.content.some((b: { type: string }) => b.type === "tool_result"), false);+ assert.equal(mixed.stop_reason, "tool_use");});test("handleToolCallExecution loads registry from DB on cold cache (covers loadFromDatabase fix)", async () => {@@ -316,6 +322,7 @@ test("handleToolCallExecution loads registry from DB on cold cache (covers loadFconst result = await handleToolCallExecution({+ stop_reason: "tool_use",content: [{ type: "tool_use", id: "tool-skill", name: "[email protected]", input: { id: "cold" } },],@@ -325,11 +332,12 @@ test("handleToolCallExecution loads registry from DB on cold cache (covers loadF);assert.deepEqual(result.content, [- { type: "tool_use", id: "tool-skill", name: "[email protected]", input: { id: "cold" } },{- type: "tool_result",- tool_use_id: "tool-skill",- content: '{"record":"resolved:cold"}',+ type: "text",+ text: '[Skill result: [email protected]]\n{"record":"resolved:cold"}',},]);+ assert.equal(result.content.some((b: { type: string }) => b.type === "tool_result"), false);+ assert.equal(result.stop_reason, "end_turn");+ assert.equal(result.stop_sequence, null);});