TypeScript
fix(opencode): keep streamed reply text after an event stream reconnect (#23591)
Victor Vogel200a373fbde4
diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.tsindex 37350b4925c..bb26a5dd53a 100644--- a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts@@ -2265,6 +2265,49 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {}).pipe(Effect.scoped),);+ it.effect("continues a reply still streaming at reconnect from the text already shown", () =>+ Effect.gen(function* () {+ const delta = (text: string) =>+ event("session.text.delta", {+ sessionID: SESSION,+ assistantMessageID: "msg_assistant_gap",+ ordinal: 0,+ delta: text,+ });+ const { runtime, thread } = yield* resumed([+ out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),+ promptAccepted,+ delta("Sent before the stream dropped"),+ // OpenCode saves a block's text only when the block ends.+ ...reconnected({ [SESSION]: { type: "running" } }, undefined, {+ content: [{ type: "text", text: "" }],+ finish: undefined,+ }),+ ...openRequests([]),+ delta(" and after it came back."),+ event("session.execution.succeeded", { sessionID: SESSION }),+ ]);+ const events = yield* runtime.events.pipe(+ Stream.takeUntil((event) => event.type === "turn.terminal"),+ Stream.runCollect,+ Effect.forkScoped,+ );+ yield* runtime.startTurn(turnInput(thread));+ const collected = yield* Fiber.join(events);+ const texts = collected.flatMap((event) =>+ event.type === "turn_item.updated" && event.turnItem.type === "assistant_message"+ ? [event.turnItem.text]+ : [],+ );+ assert.deepEqual(texts, [+ "Sent before the stream dropped",+ "Sent before the stream dropped",+ "Sent before the stream dropped and after it came back.",+ "Sent before the stream dropped and after it came back.",+ ]);+ }).pipe(Effect.scoped),+ );+it.effect("shows a permission asked while the stream was down, and answers it", () =>Effect.gen(function* () {const { runtime, thread } = yield* resumed(diff --git a/packages/provider-opencode/src/server/v2/adapter.ts b/packages/provider-opencode/src/server/v2/adapter.tsindex 1f2dd94ca21..91b3d62fffd 100644--- a/packages/provider-opencode/src/server/v2/adapter.ts+++ b/packages/provider-opencode/src/server/v2/adapter.ts@@ -2493,10 +2493,21 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Prfor (const message of recent.toReversed()) {if (message.type !== "assistant" || state.active !== turn) continue;const ordinals = { text: 0, reasoning: 0 };- for (const part of message.content) {+ for (const [index, part] of message.content.entries()) {if (part.type === "text" || part.type === "reasoning") {const block = { assistantMessageID: message.id, ordinal: ordinals[part.type]++ };- yield* emitText(state, turn, block, part.type, () => part.text, true);+ // OpenCode saves a block's text only when the block ends, so the last block of a+ // response still streaming stays open and keeps the text that already arrived.+ const open =+ message.time.streamed === undefined && index === message.content.length - 1;+ yield* emitText(+ state,+ turn,+ block,+ part.type,+ (text) => (open && !part.text.startsWith(text) ? text : part.text),+ !open,+ );continue;}if (part.type !== "tool") continue;