opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[FEATURE]: Expose the V2 TUI composer to plugins

Open luxass opened this issue 2 months ago • 15 comments

Feature hasn't been suggested before.

  • [x] I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

V1 plugins could append text to the TUI composer with client.tui.appendPrompt({ body: { text } }). V2 still handles the internal tui.prompt.append event, but I can't find an equivalent API for V2 TUI plugins.

Could V2 plugins get a ctx.ui.prompt API to read the current draft, append text at the end, and focus the composer? This would let plugins prepare text for users to review without submitting it.

I have a working prototype on the v2 branch. Is this feature in scope for the V2 TUI plugin API?

luxass avatar Sep 24 '26 19:09 luxass

+1 from a real-world blocker: I'm trying to bring voice dictation to V2 (porting the community plugin renjfk/opencode-voice, which on V1 used client.tui.appendPrompt for its core loop: record → local whisper STT → LLM cleanup → append the text to the composer so the user can review/edit before submitting).

Everything else in that flow is possible with the current V2 TUI plugin API — keymap commands, toasts, dialogs, the OpenCode client. Only the final "put the text into the composer" step is missing.

Two notes from digging through 2.0.16:

  • The TUI still handles the internal tui.prompt.append event (it calls insertText on the composer), but nothing publishes it anymore — no HTTP endpoint, no RPC surface. Could publishing that event be exposed alongside ctx.ui.prompt?
  • ctx.ui.prompt.append() as in @luxass's prototype (feat/composer-plugin-api) would be exactly the right shape for this.

jimirocks avatar Sep 25 '26 18:09 jimirocks

Related surface that would round this out: composer visibility and placement.

The composer always renders at the bottom of the session view. session.sidebar supports auto / hide, but the composer has no equivalent — prompt.* and session.markdown / session.tps only shrink it, and slot replace swaps slot contents while the composer surface itself still draws. No ui.composer.hide() exists.

Two additions alongside ctx.ui.prompt:

  • hide / show — a session.composer: "hide" setting plus a keybind to summon it, pairing with focus()
  • anchor — a placement value (bottom / top / floating)

focus() covers summoning; hiding and moving aren't expressible through the slot system.

SanD94 avatar Sep 25 '26 20:09 SanD94

While looking for something for a very similar plugin as @jimirocks I found that adding a publisher for tui.prompt.append should work and seems to be the only thing missing.

The consumer side is all still there. Schema at packages/schema/src/tui-event.ts:11. Listener at packages/tui/src/component/prompt/index.tsx:338, which just calls input.insertText(evt.data.text). The event's in EventManifest.ServerDefinitions and in the generated client, and the SSE filter at groups/event.ts:67 passes it through. Grepping packages/{tui,schema,client} for tui.prompt.append gets you those three files and nothing else — no publisher anywhere.

Also worth knowing: the v2 protocol never had a tui group. packages/protocol/src/groups/tui.ts returns an empty commit list on both v2 and dev. The v1 routes lived on v2 until 44b6938b ("removed packages/opencode", 2026-07-21, ~329k deletions) took the whole package out. So there's no v2 deprecation to point at — git just doesn't show a decision either way.

Same for the other three events in TuiEvent.Definitions: schemas and listeners, no publishers. #50984 hits that wall from another angle.

NotPellew avatar Oct 09 '26 23:10 NotPellew

Adding a distinct requirement to the same API: not just append, but read and set.

I maintain a V1 TUI plugin that does history-based autocomplete in the prompt. Its core loop is:

  1. read the focused composer draft
  2. match it against history
  3. on Tab, write the completed text back

Steps 1 and 3 both need the composer. An append()-only API covers neither: matching needs the current draft, and accepting a suggestion needs replacement (or a cursor-aware set), not append.

This is a different V1 surface than appendPrompt

Worth separating from the tui.prompt.append thread, because V1 prompt-replacing plugins did not use client.tui.appendPrompt. They claimed the prompt slot: the V1 TuiPlugin slots home_prompt and session_prompt (registered by the host with mode: "replace"), re-rendering the prompt via api.ui.Prompt with a TuiPromptRef. So even a ctx.ui.prompt that mirrors appendPrompt would not restore this class of plugin.

Evidence from the shipped package

@opencode/[email protected] (dist-tag latest), dist/tui/context.d.ts:

export interface SlotMap {
    readonly app: Readonly<Record<string, never>>;
    readonly "home.footer": Readonly<Record<string, never>>;
    readonly "home.footer.status": Readonly<Record<string, never>>;
    readonly "prompt.footer": PromptFooterInput;
    readonly "prompt.footer.status": PromptFooterInput;
    readonly "prompt.footer.file": PromptFooterInput;
    readonly "session.composer.top": { readonly sessionID: string };
    readonly "session.panel": PanelInput;
    readonly "sidebar.content": { readonly sessionID: string };
    readonly "sidebar.footer": { readonly sessionID: string };
}

The slot tree is documented as closed ("Paths and their inputs are documented API: coarse, few, and kept stable across host refactors"), and no path carries composer content. The closest one, session.composer.top, publishes { sessionID } only. dist/tui/index.d.ts exports exactly Plugin, PluginContextProvider, and usePlugin. PromptFooterInput is { sessionID?, mode, showDetails } — state, not text.

Ask

If ctx.ui.prompt is in scope, the autocomplete case needs read() and set() (or a cursor-aware replace) in addition to append() and focus().

Separately: is a slot-level escape hatch — an equivalent of the V1 prompt-slot replace — under consideration, or is ctx.ui.prompt meant to be the only supported path? That decides whether prompt-replacing V1 plugins are portable at all, or effectively deprecated.

Same gap class as #51916 (V1 home_logo) and #53547 (slot to style user prompts): V1 slot surfaces that have no V2 equivalent.

Env: opencode 1.19.0 (V1), Windows 11 build 26200, opencode2 not installed. This is a request against the API surface, verified by inspecting @opencode/[email protected] — not a runtime repro.

tleclaire avatar Oct 13 '26 04:10 tleclaire

Another use case that needs read and replace, not only append. My TUI plugin command rewrites the user's current draft into a better-formed query and leaves it for them to review before sending. It needs to read the draft, replace it with plain text, and focus the editor afterwards.

On 2.0.30 the host already has these controls internally: PromptRef has current, set and focus (tui/src/component/prompt/index.tsx#L88). The plugin UI interface (plugin/src/tui/context.ts#L490) doesn't expose them.

Something like ctx.ui.composer.get(), set(text) and focus() would cover me: get returns undefined when no composer is mounted, and set replaces the whole draft and clears attachments and mentions. Submitting can stay out of scope. I'm fine with a ctx.ui.prompt name if that's the direction.

anandgupta42 avatar Oct 16 '26 06:10 anandgupta42

need it

jensenojs avatar Oct 16 '26 07:10 jensenojs

+1 from another real-world case. I just shipped a voice dictation plugin for V2 and hit exactly this wall.

Confirming the finding from the other side, with what I checked:

  • The TUI does handle tui.prompt.append (it calls insertText on the composer), but nothing publishes it. ctx.event only exposes subscribe, and events a plugin emits are namespaced rpc.*, so they never match the literal tui.* the TUI listens for.
  • There is no HTTP surface for it: GET /openapi.json has no /tui/* route at all — only /api/session/{sessionID}/prompt.
  • The plugin UI API has nothing for the composer, in either 2.0.29 or 2.0.32 (dialog, toast, format, router, panel, tabs, model, slot).

So V1's client.tui.appendPrompt({ body: { text } }) has no equivalent, which makes the natural dictation flow (speak → see the text in the composer → fix a word → send) impossible to build.

What I had to do instead: pre-fill a dialog.prompt with the transcript and send it with session.prompt after confirmation. It works, but it is a modal interruption in the middle of the composer, and it cannot reuse whatever you were already typing.

Exposing a way to publish that event (or a ctx.ui.prompt.append(...)) would be enough — the TUI side already does the right thing.

Plugin, for reference: https://github.com/kabrau/opencode-voice-plugin

kabrau avatar Oct 18 '26 20:10 kabrau

Another use case: follow-up prompt suggestions that fill the composer without sending. #57137 adds a generic Composer.insert/text/focus and a session.composer.above slot to the desktop/web extension SDK. Happy to align it with whatever API you choose for the TUI here.

mv2a avatar Oct 19 '26 16:10 mv2a

+1 to a small composer surface instead of only publishing tui.prompt.append.

Looking at the cases in this thread, they cover insert, read and replace:

ctx.ui.composer
  .insert(text)              // insert at the cursor; never sends
  .get(): string | undefined // current draft; undefined when no composer is mounted
  .set(text)                 // replace the draft (and clear attachments/mentions)
  .focus()

For voice dictation, insert + focus is enough: speak → see the text in the composer → fix a word → send. get/set cover the autocomplete and draft-rewrite cases above.

Why not only the event: publishing tui.prompt.append would be the smallest possible change (the listener already calls insertText), but it is insert-only, so the read/replace cases stay blocked. One composer API is one thing to document and keep stable.

On naming, Composer.insert/text/focus from #57137 reads well and would keep the TUI and the desktop/web SDK using the same words. (That PR looks closed and unmerged, so I'm treating the names as a proposal rather than as something already available.)

kabrau avatar Oct 19 '26 18:10 kabrau

Thanks for the request, @luxass! You're asking for V2 TUI plugins to get a ctx.ui.prompt API that can read the current composer draft, append text at the end and focus the composer, so a plugin can stage text for you to review without sending it, as client.tui.appendPrompt allowed in v1.

You're right that the V2 TUI still handles the internal tui.prompt.append event, but the TUI plugin API (ctx.ui) has nothing that reaches the composer right now, so this can't be built as a plugin yet. Maintainers will consider it.

Related:

  • #51490 and #53152: open PRs that I understand add ui.prompt.append() to the TUI plugin API
  • #54681: asks for the same ability for external HTTP clients (such as editor integrations) after v2 removed /tui/append-prompt

opencodejr[bot] avatar Oct 28 '26 05:10 opencodejr[bot]

Another use case: quoting part of the transcript into the prompt. opencode-quote-reply takes the selected transcript text and inserts it as a Markdown quote, so you can reply to a specific part of the response (the same idea as #21025).

This needs to insert at the cursor rather than append at the end, since the quote can go anywhere in a draft. That makes the insert(text) + focus() part of the composer API proposed above enough for this case.

With no composer API, the plugin searches OpenTUI's renderable tree for the focused (or only) object that has insertText() and calls that directly. It works on 2.2.3, but it relies on host internals that can change in any release.

davdroman avatar Oct 30 '26 19:10 davdroman

Another use case for this API: restoring a draft after an idle-window plugin releases the TUI process, while work continues in the server.

With opencode-nap on 2.2.4, visible text can return after wake, but unsent images and attachments are lost. A text-only get/set API would not fix this: the structured draft also contains attachment payloads and metadata, agent/skill mentions, expanded pasted text and their ranges.

Could the proposed composer API support a versioned, detached structured snapshot and restore? For an initial slice, I would like to preserve the active session or Home draft, mode and cursor. Selected agent, model and effort also need a supported restoration path, either here or through selection APIs. Inactive tabs and transcript scroll recovery could follow separately.

Restore should report readiness, validate before mutation, avoid overwriting newer user input, and never submit, execute shell input or fetch attachment URLs. Recovery data would stay in an owner-only local checkpoint, not be uploaded to the server. Nap should still sleep using its existing lossy fallback if recovery is unavailable; this must not become a keep-awake requirement.

Would this fit the intended composer API, possibly as an experimental snapshot format? I would like maintainer design feedback before working on an implementation PR, rather than introduce another overlapping composer API.

bukatea avatar Nov 12 '26 00:11 bukatea

+1. My use case is Claude Code-style next-prompt suggestions. After a turn finishes, a server plugin generates a likely next prompt, and the TUI shows it as ghost text in the empty composer. Tab inserts it without sending and Escape dismisses it.

The server half already works as a V2 plugin on 2.3.0. On session.execution.succeeded, ctx.session.generate() returns a short suggestion and adds nothing to the session history. The composer is the missing piece. dist/tui/context.d.ts in @opencode/plugin 2.3.0 offers slots around the prompt but no way to show text in it or handle Tab there.

#54197 implemented this as context.ui.prompt.suggest({ sessionID, text }). The automated cleanup closed it on Nov 9 because it had fewer than 2 reactions, not after a review. Could a maintainer reopen it, or say whether a composer suggestion API like it is in scope? The Desktop plugin Composer has the same gap (see #56692), so one API in both plugin SDKs would let a single plugin cover both apps.

mboisvertdupras avatar Nov 16 '26 15:11 mboisvertdupras

@mboisvertdupras you can have this today without waiting for the API: opencode-autosuggestions does next-prompt suggestions as ghost text after each turn, plus completions while you type. Tab accepts, Esc dismisses. Works on 2.3, just add "opencode-autosuggestions" to plugins in cli.json.

demo

fedeya avatar Nov 18 '26 05:11 fedeya

Another use case, and one requirement the insert/get/set shape above doesn't quite cover: streaming dictation.

I built hold-space push-to-talk as a V2 TUI plugin (local whisper.cpp, partial transcripts every ~350 ms). Each partial has to replace the previous one in place, at the point where dictation started. insert alone can only add text. set would replace the whole draft, which clobbers anything the user typed around the cursor.

What I'd suggest is that insert returns a handle for the span it wrote:

const span = ctx.ui.prompt.insert("🎤")  // at the cursor, never submits; undefined if no composer
span?.update(" open a")                  // replaces exactly what this span wrote
span?.update(" open a pull request.")
span?.update("")                         // cancel removes it

The host keeps the offsets, which matters because the textarea's cursor is in display-width units while deletion is per grapheme. Plugins doing their own offset arithmetic will get emoji/CJK wrong. The same handle would also cover ghost-text accept and quote-insert.

I have this implemented against dev with tests (insert/update at the end, mid-draft, and a wide-character regression). It's a small change across packages/plugin and packages/tui, and I've been using it daily. Happy to open a PR if a maintainer is OK with this shape.

DavidJBarnes avatar Nov 19 '26 22:11 DavidJBarnes