Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/opencode/src/session/message-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,13 +854,27 @@ export const toModelMessagesEffect = Effect.fnUntraced(function* (
role: "assistant",
parts: [],
}
// Substitute a space for empty text between signed reasoning
// blocks to keep thinking block positions (and signatures) valid without
// triggering Anthropic's empty-content rejection or the AI SDK's filter.
// Signatures live under the provider-namespaced metadata key: anthropic
// (direct API), bedrock (AWS Bedrock), or vertex (GCP Vertex AI).
const hasSignedReasoning = msg.parts.some(
(p) =>
p.type === "reasoning" &&
((p.metadata as any)?.anthropic?.signature != null ||
(p.metadata as any)?.bedrock?.signature != null ||
(p.metadata as any)?.vertex?.signature != null),
)
for (const part of msg.parts) {
if (part.type === "text")
if (part.type === "text") {
const text = part.text === "" && hasSignedReasoning ? " " : part.text
assistantMessage.parts.push({
type: "text",
text: part.text,
text,
...(differentModel ? {} : { providerMetadata: part.metadata }),
})
}
if (part.type === "step-start")
assistantMessage.parts.push({
type: "step-start",
Expand Down
128 changes: 128 additions & 0 deletions packages/opencode/test/session/message-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,134 @@ describe("session.message-v2.toModelMessage", () => {
},
])
})

test("substitutes space for empty text between signed reasoning blocks", async () => {
// Reproduces the bug pattern: [reasoning(sig), text(""), reasoning(sig), text(full)]
const assistantID = "m-assistant"
const input: MessageV2.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{ ...basePart(assistantID, "p1"), type: "step-start" },
{
...basePart(assistantID, "p2"),
type: "reasoning",
text: "thinking-one",
metadata: { anthropic: { signature: "sig1" } },
},
{ ...basePart(assistantID, "p3"), type: "text", text: "" },
{ ...basePart(assistantID, "p4"), type: "step-start" },
{
...basePart(assistantID, "p5"),
type: "reasoning",
text: "thinking-two",
metadata: { anthropic: { signature: "sig2" } },
},
{ ...basePart(assistantID, "p6"), type: "text", text: "the answer" },
] as MessageV2.Part[],
},
]

const result = await MessageV2.toModelMessages(input, model)

// step-start splits into two assistant messages; SDK's groupIntoBlocks merges them later
expect(result).toHaveLength(2)
expect((result[0].content as any[]).find((p) => p.type === "text").text).toBe(" ")
expect((result[1].content as any[]).find((p) => p.type === "text").text).toBe("the answer")
})

test("substitutes space for empty text when reasoning signature is under 'bedrock' namespace", async () => {
// AWS Bedrock hosts Anthropic Claude but stores signatures under metadata.bedrock
const assistantID = "m-assistant-bedrock"
const input: MessageV2.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{
...basePart(assistantID, "p1"),
type: "reasoning",
text: "thinking-bedrock",
metadata: { bedrock: { signature: "bedrock-sig" } },
},
{ ...basePart(assistantID, "p2"), type: "text", text: "" },
{ ...basePart(assistantID, "p3"), type: "text", text: "answer" },
] as MessageV2.Part[],
},
]

const result = await MessageV2.toModelMessages(input, model)

expect(result).toHaveLength(1)
const texts = (result[0].content as any[]).filter((p) => p.type === "text")
expect(texts.map((t) => t.text)).toStrictEqual([" ", "answer"])
})

test("substitutes space for empty text when reasoning signature is under 'vertex' namespace", async () => {
// GCP Vertex AI hosts Anthropic Claude but stores signatures under metadata.vertex
const assistantID = "m-assistant-vertex"
const input: MessageV2.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{
...basePart(assistantID, "p1"),
type: "reasoning",
text: "thinking-vertex",
metadata: { vertex: { signature: "vertex-sig" } },
},
{ ...basePart(assistantID, "p2"), type: "text", text: "" },
{ ...basePart(assistantID, "p3"), type: "text", text: "answer" },
] as MessageV2.Part[],
},
]

const result = await MessageV2.toModelMessages(input, model)

expect(result).toHaveLength(1)
const texts = (result[0].content as any[]).filter((p) => p.type === "text")
expect(texts.map((t) => t.text)).toStrictEqual([" ", "answer"])
})

test("leaves empty text alone when reasoning has no Anthropic signature", async () => {
// Non-Anthropic providers' reasoning doesn't position-validate, so empty text
// should be filtered normally rather than substituted.
const assistantID = "m-assistant-unsigned"
const input: MessageV2.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{ ...basePart(assistantID, "p1"), type: "reasoning", text: "thinking" },
{ ...basePart(assistantID, "p2"), type: "text", text: "" },
{ ...basePart(assistantID, "p3"), type: "text", text: "answer" },
] as MessageV2.Part[],
},
]

const result = await MessageV2.toModelMessages(input, model)

expect(result).toHaveLength(1)
const texts = (result[0].content as any[]).filter((p) => p.type === "text")
expect(texts.map((t) => t.text)).toStrictEqual(["", "answer"])
})

test("leaves empty text alone in assistant messages without reasoning", async () => {
const assistantID = "m-assistant-no-reasoning"
const input: MessageV2.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{ ...basePart(assistantID, "p1"), type: "text", text: "" },
{ ...basePart(assistantID, "p2"), type: "text", text: "hello" },
] as MessageV2.Part[],
},
]

const result = await MessageV2.toModelMessages(input, model)

expect(result).toHaveLength(1)
const texts = (result[0].content as any[]).filter((p) => p.type === "text")
expect(texts.map((t) => t.text)).toStrictEqual(["", "hello"])
})
})

describe("session.message-v2.fromError", () => {
Expand Down
Loading