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
3 changes: 3 additions & 0 deletions packages/opencode/src/session/message-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ export const toModelMessagesEffect = Effect.fnUntraced(function* (
})
}
if (part.type === "reasoning") {
// A provider can open and close a reasoning stream without emitting a delta.
// Replaying that empty step creates an assistant message with no usable content.
if (part.text === "" && !part.metadata) continue
if (differentModel) {
if (part.text.trim().length > 0)
assistantMessage.parts.push({
Expand Down
47 changes: 47 additions & 0 deletions packages/opencode/test/session/message-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,53 @@ describe("session.message-v2.toModelMessage", () => {
])
})

test("does not replay an empty assistant step between duplicate step-start boundaries", async () => {
const assistantID = "m-assistant"
const kimiModel: Provider.Model = {
...model,
id: ModelV2.ID.make("kimi-k3"),
providerID: ProviderV2.ID.make("opencode-go"),
api: {
id: "kimi-k3",
url: "https://opencode.ai/zen/go/v1",
npm: "@ai-sdk/openai-compatible",
},
capabilities: {
...model.capabilities,
reasoning: true,
interleaved: { field: "reasoning_content" },
},
}

const input: SessionV1.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent", undefined, {
providerID: kimiModel.providerID,
modelID: kimiModel.api.id,
}),
parts: [
{ ...basePart(assistantID, "p1"), type: "step-start" },
{ ...basePart(assistantID, "p2"), type: "reasoning", text: "" },
{ ...basePart(assistantID, "p3"), type: "step-start" },
{ ...basePart(assistantID, "p4"), type: "reasoning", text: "thinking" },
{ ...basePart(assistantID, "p5"), type: "text", text: "answer" },
] satisfies SessionV1.Part[],
},
]

expect(ProviderTransform.message(await MessageV2.toModelMessages(input, kimiModel), kimiModel, {})).toStrictEqual([
{
role: "assistant",
content: [{ type: "text", text: "answer" }],
providerOptions: {
openaiCompatible: {
reasoning_content: "thinking",
},
},
},
])
})

test("drops messages that only contain step-start parts", async () => {
const assistantID = "m-assistant"

Expand Down
Loading