diff --git a/packages/opencode/src/provider/transform.ts b/packages/opencode/src/provider/transform.ts index 0e163029b3..9952762d85 100644 --- a/packages/opencode/src/provider/transform.ts +++ b/packages/opencode/src/provider/transform.ts @@ -212,8 +212,68 @@ function normalizeMessages( return msg }) } - // Do not reorder Anthropic assistant parts here. Reasoning blocks carry signatures, - // and relocating them causes Anthropic to reject replayed assistant messages. + if (["@ai-sdk/anthropic", "@ai-sdk/google-vertex/anthropic"].includes(model.api.npm)) { + // A tool result must immediately follow its tool call, but signed thinking must + // not move. Split merged steps around their matching tool results instead. + const result: ModelMessage[] = [] + for (let i = 0; i < msgs.length; i++) { + const msg = msgs[i] + const next = msgs[i + 1] + if (msg.role !== "assistant" || !Array.isArray(msg.content) || next?.role !== "tool") { + result.push(msg) + continue + } + + const clientToolIDs = new Set( + msg.content.flatMap((part) => + part.type === "tool-call" && part.providerExecuted !== true ? [part.toolCallId] : [], + ), + ) + if (clientToolIDs.size === 0) { + result.push(msg) + continue + } + + const chunks = msg.content.reduce>( + (chunks, part) => { + const current = chunks.at(-1)! + const hasToolCall = current.some( + (item) => item.type === "tool-call" && clientToolIDs.has(item.toolCallId), + ) + const isClientToolPart = + (part.type === "tool-call" || part.type === "tool-result") && clientToolIDs.has(part.toolCallId) + if (hasToolCall && !isClientToolPart) chunks.push([]) + chunks.at(-1)!.push(part) + return chunks + }, + [[]], + ) + if (chunks.length === 1) { + result.push(msg) + continue + } + + const used = new Set() + for (const chunk of chunks) { + result.push({ ...msg, content: chunk }) + const calls = new Set( + chunk.flatMap((part) => + part.type === "tool-call" && clientToolIDs.has(part.toolCallId) ? [part.toolCallId] : [], + ), + ) + if (calls.size === 0) continue + const output = next.content.filter((part) => part.type === "tool-result" && calls.has(part.toolCallId)) + if (output.length === 0) continue + output.forEach((part) => used.add(part.toolCallId)) + result.push({ ...next, content: output }) + } + + const remaining = next.content.filter((part) => part.type !== "tool-result" || !used.has(part.toolCallId)) + if (remaining.length > 0) result.push({ ...next, content: remaining }) + i++ + } + msgs = result + } if ( model.providerID === "mistral" || model.api.id.toLowerCase().includes("mistral") || diff --git a/packages/opencode/test/provider/transform.test.ts b/packages/opencode/test/provider/transform.test.ts index d353c7d27a..b4ad41fe3e 100644 --- a/packages/opencode/test/provider/transform.test.ts +++ b/packages/opencode/test/provider/transform.test.ts @@ -1614,7 +1614,34 @@ describe("ProviderTransform.message - anthropic empty content filtering", () => expect(result[1].content).toHaveLength(1) }) - test("preserves anthropic signed reasoning after tool calls", () => { + test("places anthropic tool results before trailing text", () => { + const msgs = [ + { + role: "assistant", + content: [ + { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } }, + { type: "text", text: "I checked your home directory." }, + ], + }, + { + role: "tool", + content: [ + { type: "tool-result", toolCallId: "toolu_1", toolName: "read", output: { type: "text", value: "ok" } }, + ], + }, + ] as any[] + + const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[] + + expect(result).toHaveLength(3) + expect(result).toMatchObject([ + { role: "assistant", content: [{ type: "tool-call", toolCallId: "toolu_1" }] }, + { role: "tool", content: [{ type: "tool-result", toolCallId: "toolu_1" }] }, + { role: "assistant", content: [{ type: "text", text: "I checked your home directory." }] }, + ]) + }) + + test("keeps anthropic signed reasoning in order around tool results", () => { const msgs = [ { role: "user", @@ -1627,6 +1654,7 @@ describe("ProviderTransform.message - anthropic empty content filtering", () => { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } }, { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } }, { type: "reasoning", text: "Second thought", providerOptions: { anthropic: { signature: "sig-2" } } }, + { type: "tool-call", toolCallId: "toolu_3", toolName: "bash", input: { command: "pwd" } }, ], }, { @@ -1639,22 +1667,31 @@ describe("ProviderTransform.message - anthropic empty content filtering", () => toolName: "glob", output: { type: "text", value: "No files found" }, }, + { type: "tool-result", toolCallId: "toolu_3", toolName: "bash", output: { type: "text", value: "/root" } }, ], }, ] as any[] const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[] - expect(result).toHaveLength(3) + expect(result).toHaveLength(5) expect(result[1]).toMatchObject({ role: "assistant", content: [ { type: "reasoning", text: "First thought", providerOptions: { anthropic: { signature: "sig-1" } } }, { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } }, { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } }, - { type: "reasoning", text: "Second thought", providerOptions: { anthropic: { signature: "sig-2" } } }, ], }) + expect(result[2]).toMatchObject({ role: "tool" }) + expect(result[3]).toMatchObject({ + role: "assistant", + content: [ + { type: "reasoning", text: "Second thought", providerOptions: { anthropic: { signature: "sig-2" } } }, + { type: "tool-call", toolCallId: "toolu_3", toolName: "bash", input: { command: "pwd" } }, + ], + }) + expect(result[4]).toMatchObject({ role: "tool", content: [{ type: "tool-result", toolCallId: "toolu_3" }] }) }) test("leaves valid anthropic assistant tool ordering unchanged", () => { @@ -1679,7 +1716,7 @@ describe("ProviderTransform.message - anthropic empty content filtering", () => ]) }) - test("preserves vertex anthropic signed reasoning after tool calls", () => { + test("keeps vertex anthropic signed reasoning in order around tool results", () => { const model = { ...anthropicModel, providerID: "google-vertex-anthropic", @@ -1699,19 +1736,30 @@ describe("ProviderTransform.message - anthropic empty content filtering", () => { type: "reasoning", text: "Second thought", providerOptions: { anthropic: { signature: "sig-2" } } }, ], }, + { + role: "tool", + content: [ + { type: "tool-result", toolCallId: "toolu_1", toolName: "read", output: { type: "text", value: "ok" } }, + { type: "tool-result", toolCallId: "toolu_2", toolName: "glob", output: { type: "text", value: "ok" } }, + ], + }, ] as any[] const result = ProviderTransform.message(msgs, model, {}) as any[] - expect(result).toHaveLength(1) + expect(result).toHaveLength(3) expect(result[0]).toMatchObject({ role: "assistant", content: [ { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } }, { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } }, - { type: "reasoning", text: "Second thought", providerOptions: { anthropic: { signature: "sig-2" } } }, ], }) + expect(result[1]).toMatchObject({ role: "tool" }) + expect(result[2]).toMatchObject({ + role: "assistant", + content: [{ type: "reasoning", text: "Second thought", providerOptions: { anthropic: { signature: "sig-2" } } }], + }) }) })