fix(opencode): preserve anthropic signed thinking order

This commit is contained in:
Aiden Cline
2026-05-31 23:47:15 -05:00
parent 917a36abc3
commit cff35de9fd
2 changed files with 12 additions and 39 deletions
+2 -25
View File
@@ -212,31 +212,8 @@ function normalizeMessages(
return msg
})
}
if (["@ai-sdk/anthropic", "@ai-sdk/google-vertex/anthropic"].includes(model.api.npm)) {
// Anthropic rejects assistant turns where tool_use blocks are followed by non-tool
// content, e.g. [tool_use, tool_use, text], with:
// `tool_use` ids were found without `tool_result` blocks immediately after...
//
// Reorder that invalid shape into [text] + [tool_use, tool_use]. Consecutive
// assistant messages are later merged by the provider/SDK, so preserving the
// original [tool_use...] then [text] order still produces the invalid payload.
//
// The root cause appears to be somewhere upstream where the stream is originally
// processed. We were unable to locate an exact narrower reproduction elsewhere,
// so we keep this transform in place for the time being.
msgs = msgs.flatMap((msg) => {
if (msg.role !== "assistant" || !Array.isArray(msg.content)) return [msg]
const parts = msg.content
const first = parts.findIndex((part) => part.type === "tool-call")
if (first === -1) return [msg]
if (!parts.slice(first).some((part) => part.type !== "tool-call")) return [msg]
return [
{ ...msg, content: parts.filter((part) => part.type !== "tool-call") },
{ ...msg, content: parts.filter((part) => part.type === "tool-call") },
]
})
}
// Do not reorder Anthropic assistant parts here. Reasoning blocks carry signatures,
// and relocating them causes Anthropic to reject replayed assistant messages.
if (
model.providerID === "mistral" ||
model.api.id.toLowerCase().includes("mistral") ||
@@ -1614,7 +1614,7 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
expect(result[1].content).toHaveLength(1)
})
test("splits anthropic assistant messages when text trails tool calls", () => {
test("preserves anthropic signed reasoning after tool calls", () => {
const msgs = [
{
role: "user",
@@ -1623,9 +1623,10 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
{
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: "text", text: "I checked your home directory and looked for PDF files." },
{ type: "reasoning", text: "Second thought", providerOptions: { anthropic: { signature: "sig-2" } } },
],
},
{
@@ -1644,16 +1645,14 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
expect(result).toHaveLength(4)
expect(result).toHaveLength(3)
expect(result[1]).toMatchObject({
role: "assistant",
content: [{ type: "text", text: "I checked your home directory and looked for PDF files." }],
})
expect(result[2]).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" } } },
],
})
})
@@ -1680,7 +1679,7 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
])
})
test("splits vertex anthropic assistant messages when text trails tool calls", () => {
test("preserves vertex anthropic signed reasoning after tool calls", () => {
const model = {
...anthropicModel,
providerID: "google-vertex-anthropic",
@@ -1697,23 +1696,20 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
content: [
{ type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
{ type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
{ type: "text", text: "I checked your home directory and looked for PDF files." },
{ type: "reasoning", text: "Second thought", providerOptions: { anthropic: { signature: "sig-2" } } },
],
},
] as any[]
const result = ProviderTransform.message(msgs, model, {}) as any[]
expect(result).toHaveLength(2)
expect(result).toHaveLength(1)
expect(result[0]).toMatchObject({
role: "assistant",
content: [{ type: "text", text: "I checked your home directory and looked for PDF files." }],
})
expect(result[1]).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" } } },
],
})
})