diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts index 1efbbfad09d81..2019b1148a27f 100644 --- a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts @@ -744,6 +744,96 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { event.turnItem.text === "DONE", ), ); + assert.isFalse( + seen.some((event) => event.type === "plan.updated" && event.plan.kind === "proposed_plan"), + ); + }).pipe(Effect.scoped), + ); + + it.effect("turns a Plan mode turn's final answer into its proposed plan", () => + Effect.gen(function* () { + const text = (message: string, ordinal: number, value: string) => + event("session.text.ended", { + sessionID: SESSION, + assistantMessageID: message, + ordinal, + text: value, + }); + const { runtime, thread } = yield* resumed([ + out("session.switchAgent", { sessionID: SESSION, agent: "plan" }), + reply("session.switchAgent", null), + out("agent.list", ""), + reply("agent.list", agentList), + out("session.update", { sessionID: SESSION, permissions: "" }), + reply("session.update", null), + out("session.prompt", { sessionID: SESSION, text: "" }), + promptAccepted, + text("msg_plan", 0, "Reading the project first."), + text("msg_plan", 1, "## Plan\n\n1. Add the store\n2. Add the list"), + event("session.execution.succeeded", { sessionID: SESSION }), + out("session.prompt", { sessionID: SESSION, text: "" }), + replyData("session.prompt", { + id: "msg_refine", + sessionID: SESSION, + time: { created: 1790656601411 }, + type: "user", + payload: { text: "refine" }, + delivery: "steer", + }), + text("msg_refined", 0, "## Plan\n\n1. Add the list"), + event("session.execution.succeeded", { sessionID: SESSION }), + ]); + const firstEnded = yield* Deferred.make(); + let terminals = 0; + const collected = yield* runtime.events.pipe( + Stream.tap((event) => + event.type === "turn.terminal" ? Deferred.succeed(firstEnded, undefined) : Effect.void, + ), + Stream.takeUntil((event) => event.type === "turn.terminal" && ++terminals === 2), + Stream.runCollect, + Effect.forkScoped, + ); + const input = turnInput(thread); + const plan = { ...input.runtimePolicy, interactionMode: "plan" as const }; + yield* runtime.startTurn({ ...input, runtimePolicy: plan }); + yield* Deferred.await(firstEnded); + yield* runtime.startTurn({ + ...input, + runId: RunId.make("run:opencode2-adapter:2"), + runOrdinal: 2, + providerTurnOrdinal: 2, + attemptId: RunAttemptId.make("attempt:opencode2-adapter:2"), + runtimePolicy: plan, + }); + const seen = yield* Fiber.join(collected); + const plans = seen.flatMap((event) => + event.type === "plan.updated" && event.plan.kind === "proposed_plan" ? [event.plan] : [], + ); + const [first, superseded, refined] = plans; + assert.deepEqual( + plans.map((each) => [each.status, each.kind === "proposed_plan" && each.markdown]), + [ + ["active", "## Plan\n\n1. Add the store\n2. Add the list"], + ["superseded", "## Plan\n\n1. Add the store\n2. Add the list"], + ["active", "## Plan\n\n1. Add the list"], + ], + ); + assert.equal(superseded?.id, first?.id); + assert.notEqual(refined?.id, first?.id); + const items = seen.flatMap((event) => + event.type === "turn_item.updated" ? [event.turnItem] : [], + ); + const answer = items.find( + (item) => item.type === "assistant_message" && item.text.includes("Add the store"), + ); + const card = items.find((item) => item.type === "proposed_plan" && item.planId === first?.id); + // The answer becomes the card in place, so the plan is not shown twice. + assert.equal(card?.id, answer?.id); + assert.isTrue( + items.some( + (item) => item.type === "assistant_message" && item.text === "Reading the project first.", + ), + ); }).pipe(Effect.scoped), ); diff --git a/docs/user/providers-opencode.md b/docs/user/providers-opencode.md index bcdc33fb20246..dbb733eeefd79 100644 --- a/docs/user/providers-opencode.md +++ b/docs/user/providers-opencode.md @@ -20,7 +20,8 @@ OpenCode 2 converts the shared OpenCode database to its own format the first tim runs. Don't run OpenCode 1.x and 2.x side by side on the same machine. Threads you started on 1.x continue on 2.x. -Plan mode uses OpenCode's `plan` agent. +Plan mode uses OpenCode's `plan` agent. Its final answer becomes the plan card, so you can +refine it or implement it, in this thread or a new one. OpenCode 1.x has no Plan mode. ## Compaction diff --git a/packages/provider-opencode/src/server/v2/adapter.ts b/packages/provider-opencode/src/server/v2/adapter.ts index b13f6c361bacf..68286c66acd2b 100644 --- a/packages/provider-opencode/src/server/v2/adapter.ts +++ b/packages/provider-opencode/src/server/v2/adapter.ts @@ -31,6 +31,7 @@ import { type OrchestrationV2ProviderSession, type OrchestrationV2ProviderThread, type OrchestrationV2ProviderTurn, + type OrchestrationV2PlanArtifact, type OrchestrationV2PlanStep, type OrchestrationV2RuntimeRequest, type OrchestrationV2Subagent, @@ -171,7 +172,7 @@ const OpenCode2ProviderCapabilities = { planning: { emitsPlanUpdated: true, emitsTodoList: true, - emitsProposedPlan: false, + emitsProposedPlan: true, supportsStructuredQuestions: true, planDeltasHaveItemIds: false, }, @@ -219,7 +220,10 @@ interface TurnOwner { readonly runOrdinal: number; readonly rootNodeId: ProviderAdapter.ProviderAdapterV2TurnInput["rootNodeId"]; readonly modelSelection: ModelSelection; - readonly runtimePolicy: Pick; + readonly runtimePolicy: Pick< + ProviderAdapter.ProviderAdapterV2RuntimePolicy, + "cwd" | "interactionMode" + >; } interface ActiveTurn { @@ -248,6 +252,10 @@ interface ActiveTurn { compaction: { readonly nativeId: string; readonly startedAt: DateTime.Utc } | undefined; compactions: number; todoPlanId: PlanId | undefined; + // The plan agent answers in text; in Plan mode the final answer becomes the proposed plan. + answer: + | { readonly nativeId: string; readonly text: string; readonly startedAt: DateTime.Utc } + | undefined; retry: | { readonly retry: OrchestrationV2ProviderRetry; @@ -361,6 +369,9 @@ interface ThreadState { >; readonly stoppedChildren: Set; readonly strandedSteers: Set; + proposedPlan: + | Extract + | undefined; /** Prompts other clients queued while no T3 turn ran, shown once delivered; `leads` the next wake if no report preceded it. */ readonly externalPrompts: Map; mcp: @@ -843,6 +854,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr reports: new Map(), stoppedChildren: new Set(), strandedSteers: new Set(), + proposedPlan: undefined, externalPrompts: new Map(), mcp: undefined, instructions: undefined, @@ -1081,6 +1093,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr if (completed) turn.texts.delete(nativeId); else turn.texts.set(nativeId, entry); if (entry.text.length === 0) return; + if (completed && kind === "text") { + turn.answer = { nativeId, text: entry.text, startedAt: entry.startedAt }; + } const status = completed ? "completed" : "running"; const completedAt = completed ? updatedAt : null; const nodeKind = kind === "text" ? "assistant_message" : "reasoning"; @@ -1236,6 +1251,49 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr }); }); + // Re-emits the answer's own turn item as the plan card, so the plan is not shown twice. + const emitProposedPlan = Effect.fnUntraced(function* (state: ThreadState, turn: ActiveTurn) { + const answer = turn.answer; + // Tools or other items after the last text mean that text was narration, not the plan. + if (answer === undefined || turn.ordinals.get(answer.nativeId) !== turn.nextOrdinal - 1) { + return; + } + const markdown = answer.text.trim(); + if (markdown.length === 0) return; + const now = yield* DateTime.now; + const base = itemBase(state, turn, answer.nativeId, "completed", answer.startedAt, now, now); + const plan = { + id: yield* idAllocator.allocate + .plan({ + threadId: turn.input.threadId, + ...(turn.input.runId === null ? {} : { runId: turn.input.runId }), + driver, + }) + .pipe(Effect.orDie), + threadId: turn.input.threadId, + runId: turn.input.runId, + nodeId: base.nodeId, + kind: "proposed_plan" as const, + status: "active" as const, + markdown, + }; + if (state.proposedPlan?.status === "active") { + yield* emit({ + type: "plan.updated", + driver, + plan: { ...state.proposedPlan, status: "superseded" }, + }); + } + state.proposedPlan = plan; + yield* emitNode(state, turn, answer.nativeId, "plan", "completed", answer.startedAt, now); + yield* emit({ type: "plan.updated", driver, plan }); + yield* emit({ + type: "turn_item.updated", + driver, + turnItem: { ...base, type: "proposed_plan", planId: plan.id, markdown, streaming: false }, + }); + }); + const emitTool = Effect.fnUntraced(function* ( state: ThreadState, turn: ActiveTurn, @@ -1451,6 +1509,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr compaction: undefined, compactions: 0, todoPlanId: undefined, + answer: undefined, retry: undefined, interrupted: false, unsent: false, @@ -2128,6 +2187,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr for (const open of turn.texts.values()) { yield* emitText(state, turn, open.block, open.kind, (text) => text, true); } + if ( + terminal.status === "completed" && + state.subagent === undefined && + turn.input.runtimePolicy.interactionMode === "plan" + ) { + yield* emitProposedPlan(state, turn); + } for (const id of turn.tools.keys()) { if ([...state.imageJobs.values()].some((job) => job.turn === turn && job.toolId === id)) continue;