@bojackduy/opencode-voice 0.11.0 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,13 +18,13 @@ One package runs on both OpenCode 1.x and 2.x. The plugin detects the host
18
18
  by capability (never by version string) and adapts; the v1 path is unchanged.
19
19
 
20
20
  > [!IMPORTANT]
21
- > **Submit-only mode on v2.** OpenCode v2.0.26 exposes no plugin API for
22
- > appending text to the composer for editing, so the v1 STT loop of
23
- > transcribe → append → edit → submit is unimplementable there. On v2 every
24
- > STT turn **transcribes and submits immediately** via `session.prompt` —
25
- > both `/stt-record` and `/stt-submit` submit — and the turn toast says
26
- > submitted, never "added to prompt". A warning toast on first load says the
27
- > same. There is deliberately no edit step; nothing is faked.
21
+ > **Dictation inserts; it does not send.** On both v1 and v2 the transcript
22
+ > is typed at the cursor of whichever editable field currently has focus —
23
+ > the session composer, a dialog/search input, or another plugin's text
24
+ > field — leaving the text editable. Nothing is submitted unless you asked
25
+ > for it (`/stt-submit`, `sttAutoSubmit`, or conversation mode). If no
26
+ > editable field is focused, v2 keeps the transcript in a recovery dialog
27
+ > instead of sending it as a prompt.
28
28
 
29
29
  ### v1 (OpenCode 1.x)
30
30
 
@@ -64,14 +64,13 @@ under the `opencode-voice` group with their slash names (`/stt-record`,
64
64
  `/tts-speak`, `/voice-cancel`, …), verified live on 2.0.26.
65
65
 
66
66
  > [!NOTE]
67
- > **Default keybinds on v2.** The defaults (`<leader>[`, `<leader>.`, …)
68
- > display correctly in the palette (translated to `ctrl+x [` style, since v2
69
- > bind strings have no `<leader>` token) but multi-key sequences do not
70
- > dispatch on 2.0.26 — only single-combo binds fire (verified: an overridden
71
- > `"keybinds": { "voice.cancel": "ctrl+r" }` runs the command, while
72
- > `ctrl+x .` stays silent). Until the host fixes sequence dispatch, override
73
- > any keybind you need with a single combo via the `keybinds` option; every
74
- > command stays reachable via palette and `/slash` regardless.
67
+ > **Default keybinds on v2.** The same leader shortcuts work on both hosts:
68
+ > press `Ctrl+X`, then `[` to record/stop, `]` to read the reply, `;` to stop
69
+ > playback, `v` for conversation, or `.` to cancel. The v2 keymap resolves
70
+ > `<leader>` itself, including a customized host leader. Record/stop and cancel
71
+ > dispatch were verified on v2.0.26 on both home and session screens. Plugin
72
+ > `keybinds` overrides and `"none"` still apply; palette and `/slash` remain
73
+ > available.
75
74
 
76
75
  `engines` status: the package declares no `engines.opencode` field, so both
77
76
  v1 and v2 load it unconstrained. A version pin was considered and skipped on
@@ -85,13 +84,25 @@ capability detection already picks the right path per host.
85
84
  every one reliably; the `Ctrl+P` palette shows them for matching queries
86
85
  but its search occasionally reports no results for queries that should
87
86
  match — when in doubt, use `/slash`.
88
- - **STT (batch):** submits immediately, no edit step (see above). With no
89
- active session the transcript is kept via warning toast and nothing is
90
- submitted.
91
- - **STT (streaming dictation):** live composer previews and finalize-insert
92
- have no v2 primitive, so finalizing keeps the dictated text via toast
93
- instead of inserting or submitting. Untested on v2 beyond that — prefer
94
- batch mode there.
87
+ - **STT (batch):** the transcript is typed at the cursor of the focused
88
+ editable field and nothing is sent unless submission is requested.
89
+ On home the composer is focused, so dictation works before a session
90
+ exists; an explicit submit creates the session, carries the selected
91
+ model into it, and sends once. Existing sessions retain their
92
+ agent/model. On home, v2's public plugin API exposes the selected model
93
+ but no selected-agent getter, so session creation uses the host's
94
+ default agent. Create/submit failures show the full transcript in a
95
+ recovery dialog. With no editable field focused, the transcript is kept
96
+ in that dialog instead of being sent as a prompt.
97
+ - **STT (streaming dictation):** enable with `"sttMode": "streaming"` in plugin
98
+ options. Local whisper streaming displays a bounded live transcript above
99
+ the prompt footer on home and session screens. Stable words are plain;
100
+ the tentative tail is bracketed and may change. Press the record key again
101
+ to finalize: the full text is inserted once at the focused cursor (and
102
+ submitted when `sttAutoSubmit` is set), or cancel to clear the preview
103
+ without sending. Batch remains the default and produces text after
104
+ recording stops; the hosted `sttEndpoint` option is for batch
105
+ transcription, not this local streaming preview.
95
106
  - **TTS / conversation / live notes / cancel:** same semantics as v1 (auto
96
107
  TTS on idle/permission/question gates, conversation barge-in, live-notes
97
108
  background saves, one-key cancel).
@@ -740,7 +751,8 @@ Idle invocations toast `Nothing to cancel`. Keybind override:
740
751
 
741
752
  Live local dictation for short prompts: the mic stays on, partial
742
753
  transcriptions stream in as you speak, and one press of `/stt-record`
743
- finalizes the text into the prompt box. Set `"sttMode": "streaming"` in the
754
+ finalizes the text at the focused cursor on both hosts.
755
+ Set `"sttMode": "streaming"` in the
744
756
  plugin options (default `"batch"` keeps the record-then-transcribe flow
745
757
  exactly as before).
746
758
 
@@ -754,16 +766,20 @@ What you see: a sticky status toast stays up for the whole session so
754
766
  there is never a silent gap — `Loading speech model…` during cold start,
755
767
  `● Streaming dictation — listening` with an elapsed timer while live
756
768
  (plus a `(listening…)` note when inference runs slow), and `Finalizing…`
757
- between the second keypress and the insert. The OpenCode renderer only
769
+ between the second keypress and finalization. On v2, a bounded transcript
770
+ above the prompt footer updates during recording: stable words are plain,
771
+ tentative words are bracketed. Finalization inserts the full text once at
772
+ the focused cursor; cancel clears the preview without sending.
773
+ The OpenCode renderer only
758
774
  exposes `insertText`/`submit`, with no live range-replacement API, so
759
775
  partials render as preview toasts (`🎙 ...`) alongside (not instead of)
760
776
  the sticky status, and the full text is inserted EXACTLY ONCE at
761
- finalize. If no editable field is focused, the transcript is kept via
762
- toast instead of being redirected into the chat. `/stt-submit` submits
763
- only through the dictated field's own `submit()` — a field that cannot
764
- submit keeps its text un-submitted rather than falling through to the
765
- primary chat. `/stt-stop` cancels and removes only plugin-dictated
766
- text, never your typed prefix/suffix.
777
+ finalize. If no editable field is focused, the transcript is kept visibly
778
+ (v2 recovery dialog, v1 toast) instead of being redirected into the chat.
779
+ `/stt-submit` submits only through the dictated field's own `submit()` — a
780
+ field that cannot submit keeps its text un-submitted rather than falling
781
+ through to the primary chat. `/stt-stop` cancels and removes only
782
+ plugin-dictated text, never your typed prefix/suffix.
767
783
 
768
784
  Warm server: the whisper model stays loaded across dictations (finalize
769
785
  and cancel keep the lease); it reloads only on model/language change,
@@ -784,8 +800,8 @@ Options:
784
800
  - `sttMode` _(optional, default `"batch"`)_ - `"streaming"` selects this mode
785
801
  - `sttStreamWindowMs` _(optional, default `10000`)_ - rolling audio window
786
802
  - `sttStreamStepMs` _(optional, default `1000`)_ - live transcription cadence
787
- - `sttAutoSubmit` applies after streaming finalize (submit via the dictated
788
- field only, as above)
803
+ - `sttAutoSubmit` applies after streaming finalize on both hosts (submit via
804
+ the dictated field only, as above).
789
805
 
790
806
  Measured latencies (Apple M3 Pro, 36 GB, whisper.cpp 1.9.4 with Metal,
791
807
  `ggml-large-v3-turbo-q5_0.bin`, scripted speech through the real
@@ -806,11 +822,14 @@ at roughly that rate, not every cadence tick — the scheduler coalesces
806
822
  while inference is busy, so partials are flicker-free but not instant.
807
823
  Only one smaller-model data point exists (none: no smaller model file is
808
824
  installed locally, and nothing was downloaded for measurement). E2E
809
- coverage is mic-less/TUI-less by necessity: session flows are proven
825
+ coverage is mic-less: v1 session flows are proven
810
826
  through the real command paths with faked capture/renderer
811
827
  (`test/streaming-e2e.test.js`, `test/streaming-gaps.test.js`), while the
812
828
  numbers above come from the real server path with scripted audio — no
813
- live-mic session was measured.
829
+ live-mic session was measured. v2 preview rendering was additionally verified
830
+ in the real v2.0.26 TUI using injected stable/tentative text on home and
831
+ session screens; final-once/cancellation flows use scripted capture and the
832
+ real streaming controller in `test/v2-preview.test.js`.
814
833
 
815
834
  ## How it works
816
835
 
@@ -823,10 +842,11 @@ live-mic session was measured.
823
842
  3. LLM normalizes the transcription: fixes punctuation, removes filler words,
824
843
  corrects software engineering homophones ("Jason" to "JSON", "bullion" to
825
844
  "boolean", etc.)
826
- 4. Cleaned text is appended to the OpenCode prompt, or submitted immediately
827
- when `/stt-submit` is used. If normalization fails (e.g. LLM endpoint
828
- unreachable), the raw transcription is used as a fallback so you never lose
829
- your input
845
+ 4. Cleaned text is inserted at the cursor of the focused editable field
846
+ (composer, search, or plugin input), or submitted immediately when
847
+ `/stt-submit` (or `sttAutoSubmit`) is used. If normalization fails (e.g.
848
+ LLM endpoint unreachable), the raw transcription is used as a fallback
849
+ so you never lose your input
830
850
 
831
851
  ### TTS pipeline
832
852
 
package/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  // opencode-voice: Speech-to-text and text-to-speech for OpenCode.
2
2
  //
3
3
  // STT: Record voice via sox, transcribe with whisper-cpp, normalize with
4
- // an OpenAI-compatible LLM, append to the TUI prompt (v1) or submit
5
- // immediately via session.prompt (v2 submit-only mode, see README
6
- // "OpenCode v1 and v2").
4
+ // an OpenAI-compatible LLM, and insert the text at the cursor of the
5
+ // focused input (composer, dialog search, or plugin field) on both
6
+ // hosts. Explicit submits only: /stt-submit, sttAutoSubmit, conversation.
7
7
  //
8
8
  // TTS: Auto-speak assistant responses (or read on demand) via Piper,
9
9
  // with LLM normalization for natural speech.
@@ -46,7 +46,8 @@ import { adaptV2Context } from "./lib/v2-compat.js";
46
46
  // Dual-compat export: v1's loader reads `tui`, v2's reads `setup`
47
47
  // (`Plugin.define` is an identity wrapper, so a plain object carries both).
48
48
  // Each host ignores the other's key. The core (`bootVoice`) is shared; v2
49
- // runs it over the v2-compat adapted api in submit-only mode.
49
+ // runs it over the v2-compat adapted api, which forwards the live renderer
50
+ // so dictation lands in whatever input currently has focus.
50
51
  export default {
51
52
  id: "opencode-voice",
52
53
  tui: async (api, options) => {
@@ -55,15 +56,16 @@ export default {
55
56
  setup: async (ctx) => {
56
57
  const adapted = adaptV2Context(ctx);
57
58
  await bootVoice(adapted.api, ctx.options, {
58
- submitOnly: true,
59
+ v2: true,
59
60
  onReady: async ({ logger }) => {
60
- // First-run submit-only notice (required UX disclosure): v2 has no
61
- // append-to-composer API, so every STT turn submits immediately.
61
+ // First-run v2 notice (required UX disclosure): text is typed into
62
+ // the focused field and is never sent automatically.
62
63
  const notice =
63
- "Voice on OpenCode v2: transcripts submit immediately as-is " +
64
- "(submit-only mode — append-for-edit is unavailable on v2)";
64
+ "Voice on OpenCode v2: transcripts are inserted at the cursor of the focused text field " +
65
+ "(prompt, search, or dialog input) and are not sent automatically. " +
66
+ "Set sttAutoSubmit to submit after recording.";
65
67
  adapted.api.ui.toast({ message: notice, variant: "warning", duration: 8000 });
66
- logger?.log?.("plugin", `v2 submit-only mode: ${notice}`, "warn");
68
+ logger?.log?.("plugin", `v2 focused-input mode: ${notice}`, "warn");
67
69
  // v2 has no client.app.log, so the logger above is a no-op: compat
68
70
  // warnings must surface as a toast or they are lost entirely.
69
71
  if (adapted.warnMessages.length > 0) {
package/lib/boot.js CHANGED
@@ -4,9 +4,9 @@
4
4
  // an `api` object (raw v1 `TuiPluginApi` vs the v2-compat adapted `Context`).
5
5
  // Everything below runs identically on both, so there is exactly one core to
6
6
  // maintain. The v1 `tui` path calls `bootVoice(api, options)` with no extras
7
- // and behaves exactly as before; the v2 `setup` path passes
8
- // `{ submitOnly: true }` for the first-run notice. No STT/TTS/routing/
9
- // keybind behavior branches live here.
7
+ // and behaves exactly as before; the v2 `setup` path passes `{ v2: true }`
8
+ // for the first-run notice. No STT/TTS/routing/keybind behavior branches
9
+ // live here.
10
10
 
11
11
  import fs from "node:fs";
12
12
  import os from "node:os";
@@ -98,7 +98,7 @@ export async function bootVoice(api, options, extras = {}) {
98
98
  ...cancel.commands,
99
99
  ]);
100
100
 
101
- if (extras?.submitOnly && typeof extras?.onReady === "function") {
101
+ if (extras?.v2 && typeof extras?.onReady === "function") {
102
102
  await extras.onReady({ logger, stt, tts, conversation, liveNotes, voiceModel, cancel });
103
103
  }
104
104
 
@@ -235,10 +235,12 @@ export function registerConversation(api, opts, logger, { stt, tts, isLiveNotesA
235
235
  try {
236
236
  if (!active || phase !== "waiting") return;
237
237
  const props = event.properties || {};
238
- if (sessionID && props.sessionID && props.sessionID !== sessionID) return;
238
+ const expectedID = typeof sessionID === "function" ? sessionID() : sessionID;
239
+ if (typeof sessionID === "function" && !expectedID) return;
240
+ if (expectedID && props.sessionID && props.sessionID !== expectedID) return;
239
241
  if (typeof props.delta !== "string" || !props.delta) return;
240
242
  if (props.field && props.field !== "text") return;
241
- const lookupID = props.sessionID || sessionID;
243
+ const lookupID = props.sessionID || expectedID;
242
244
  const messages = api?.state?.session?.messages?.(lookupID) || [];
243
245
  const msg = messages.find((m) => m?.id === props.messageID);
244
246
  if (!msg || msg.role !== "assistant") return;
@@ -302,8 +304,11 @@ export function registerConversation(api, opts, logger, { stt, tts, isLiveNotesA
302
304
  cancelFn = (outcome) => finish(outcome || "stopped");
303
305
  waitCancel = cancelFn;
304
306
 
305
- const forSession = (props) =>
306
- !sessionID || !props?.sessionID || props.sessionID === sessionID;
307
+ const forSession = (props) => {
308
+ const expectedID = typeof sessionID === "function" ? sessionID() : sessionID;
309
+ if (typeof sessionID === "function" && !expectedID) return false;
310
+ return !expectedID || !props?.sessionID || props.sessionID === expectedID;
311
+ };
307
312
 
308
313
  unsubs = [
309
314
  api.event.on("session.idle", (event) => {
@@ -364,6 +369,15 @@ export function registerConversation(api, opts, logger, { stt, tts, isLiveNotesA
364
369
  return;
365
370
  }
366
371
 
372
+ const turnStart = Date.now();
373
+ // v2 prompt dispatch may emit the entire reply before its promise returns.
374
+ // Home creation navigates before prompting, so resolve the ID at event time.
375
+ let earlyWait = null;
376
+ if (api.client?.tui?.v2) {
377
+ phase = "waiting";
378
+ startReplyStream(currentSessionID, turnStart);
379
+ earlyWait = waitForReply(currentSessionID);
380
+ }
367
381
  const submitted = await stt.submitTurnText(res.text);
368
382
  if (!active || myGen !== generation) return;
369
383
  if (submitted.error) {
@@ -373,11 +387,10 @@ export function registerConversation(api, opts, logger, { stt, tts, isLiveNotesA
373
387
 
374
388
  phase = "waiting";
375
389
  const sessionID = currentSessionID();
376
- const turnStart = Date.now();
377
390
  showProcessingToast(`Working - press ${keyLabel()} to talk`);
378
- startReplyStream(sessionID, turnStart);
391
+ if (!earlyWait) startReplyStream(sessionID, turnStart);
379
392
  const tWait = Date.now();
380
- const outcome = await waitForReply(sessionID);
393
+ const outcome = await (earlyWait || waitForReply(sessionID));
381
394
  const waitMs = Date.now() - tWait;
382
395
  endReplyStream();
383
396
  logger?.log(
package/lib/llm-client.js CHANGED
@@ -102,7 +102,9 @@ export function resolveProviderConfig(provider, model) {
102
102
  const exported = envVars.find((name) => name && process.env[name]);
103
103
  return {
104
104
  endpoint,
105
- model: model.id,
105
+ // v2 keeps the upstream provider's model id in `modelID` (`id` is the
106
+ // selection id and can differ for configured aliases), so prefer it.
107
+ model: model.modelID ?? model.id,
106
108
  apiKeyEnv: exported || envVars[0] || null,
107
109
  };
108
110
  }
package/lib/stt.js CHANGED
@@ -1371,12 +1371,17 @@ export function insertIntoFocusedInput(renderer, text, submit = false) {
1371
1371
  }
1372
1372
  }
1373
1373
 
1374
- async function appendTranscription(client, renderer, text, submit) {
1374
+ async function appendTranscription(client, renderer, text, submit, isCurrent) {
1375
1375
  if (insertIntoFocusedInput(renderer, text, submit)) {
1376
1376
  return true;
1377
1377
  }
1378
1378
 
1379
- let appendResult = await client.tui.appendPrompt({ body: { text } });
1379
+ // Only the v2 adapter consumes this fence; v1 request bodies remain
1380
+ // unchanged. Re-check after an asynchronous home session create. The
1381
+ // `submit` flag lets the adapter distinguish an explicit submit from a
1382
+ // plain append with no editable field focused (append must not send).
1383
+ const fence = client.tui?.v2 ? { isCurrent, submit } : {};
1384
+ let appendResult = await client.tui.appendPrompt({ body: { text }, ...fence });
1380
1385
 
1381
1386
  if (appendResult?.error?.data?.message === "Expected object, got undefined") {
1382
1387
  appendResult = await client.tui.appendPrompt({ text });
@@ -1388,7 +1393,9 @@ async function appendTranscription(client, renderer, text, submit) {
1388
1393
  );
1389
1394
  }
1390
1395
 
1391
- if (submit) {
1396
+ if (appendResult?.retained) return appendResult;
1397
+
1398
+ if (submit && !appendResult?.submitted) {
1392
1399
  await client.tui.submitPrompt();
1393
1400
  }
1394
1401
  return appendResult;
@@ -1482,8 +1489,9 @@ async function transcribeTurn(kv, complete, client, api, toast, systemPrompt, lo
1482
1489
  }
1483
1490
 
1484
1491
  async function submitTurnText(client, renderer, toast, text, logger) {
1492
+ const myGen = pipelineGeneration;
1485
1493
  try {
1486
- await appendTranscription(client, renderer, text, true);
1494
+ await appendTranscription(client, renderer, text, true, () => myGen === pipelineGeneration);
1487
1495
  clearProcessingToast();
1488
1496
  logger?.log("STT", `Turn submitted chars=${text.length}`, "debug");
1489
1497
  toast("Transcription submitted", "success");
@@ -1500,19 +1508,26 @@ async function submitTurnText(client, renderer, toast, text, logger) {
1500
1508
  }
1501
1509
 
1502
1510
  async function appendTurnText(client, renderer, toast, text, logger) {
1511
+ const myGen = pipelineGeneration;
1503
1512
  try {
1504
- const appendResult = await appendTranscription(client, renderer, text, false);
1513
+ const appendResult = await appendTranscription(
1514
+ client,
1515
+ renderer,
1516
+ text,
1517
+ false,
1518
+ () => myGen === pipelineGeneration,
1519
+ );
1505
1520
  clearProcessingToast();
1506
1521
  logger?.log("STT", `Turn appended chars=${text.length}`, "debug");
1507
- // Submit-only hosts (OpenCode v2: no append-to-composer API) submit via
1508
- // the client shim, which marks the result — so the toast must not claim
1509
- // the text was "added to prompt" for editing there. v1 results carry no
1522
+ // The v2 adapter retains the transcript when no editable field has
1523
+ // focus — it never sends an unrequested prompt. v1 results carry no
1510
1524
  // marker and keep the original toast.
1525
+ const retained = appendResult?.retained === true;
1511
1526
  toast(
1512
- appendResult?.submitted === true
1513
- ? "Transcription submitted as-is (submit-only mode)"
1527
+ retained
1528
+ ? "No text field focused - transcription kept (not sent)"
1514
1529
  : "Transcription added to prompt",
1515
- "success",
1530
+ retained ? "warning" : "success",
1516
1531
  );
1517
1532
  return { text };
1518
1533
  } catch (err) {
@@ -1589,6 +1604,10 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1589
1604
  }
1590
1605
  const client = api.client;
1591
1606
  const renderer = opts?.focusMode === "primary" ? null : api.renderer;
1607
+ const preview = api.voicePreview;
1608
+ let streamGeneration = 0;
1609
+ let streamDisposed = false;
1610
+ let finalizingController = null;
1592
1611
  const systemPrompt = selectSttSystemPrompt(sttNormalizeMode, prompts?.stt);
1593
1612
  const backend = detectAudioBackend();
1594
1613
  logger?.log("STT", `Audio backend=${backend}`, "debug");
@@ -1733,11 +1752,12 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1733
1752
  }
1734
1753
 
1735
1754
  function isStreaming() {
1736
- return streamingDictationActive;
1755
+ return streamingDictationActive || (preview && (streamStarting || !!finalizingController));
1737
1756
  }
1738
1757
 
1739
1758
  async function startStreamingDictation() {
1740
- if (streamingDictationActive || streamStarting) return false;
1759
+ if (streamingDictationActive || streamStarting || finalizingController || streamDisposed)
1760
+ return false;
1741
1761
  if (recording || processing) {
1742
1762
  toast("STT busy, please wait...");
1743
1763
  return false;
@@ -1751,6 +1771,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1751
1771
  return false;
1752
1772
  }
1753
1773
  streamStarting = true;
1774
+ const myGen = ++streamGeneration;
1754
1775
  // Model/language change tears down the warm lease first so the next
1755
1776
  // start loads the new model instead of reusing the stale server. The
1756
1777
  // key carries the bound server port, so a lease on a different port is
@@ -1768,13 +1789,21 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1768
1789
  const { createStreamingEditorAdapter } = await import("./streaming-editor.js");
1769
1790
  const focused = renderer?.currentFocusedRenderable || null;
1770
1791
  const target = focused && typeof focused.insertText === "function" ? focused : null;
1771
- const editor = createStreamingEditorAdapter({
1772
- toast: (message, variant) => api.ui.toast({ message, variant, duration: 2500 }),
1773
- getFocused: () => renderer?.currentFocusedRenderable || null,
1774
- });
1775
- editor.begin(target);
1792
+ const editor =
1793
+ preview ||
1794
+ createStreamingEditorAdapter({
1795
+ toast: (message, variant) => api.ui.toast({ message, variant, duration: 2500 }),
1796
+ getFocused: () => renderer?.currentFocusedRenderable || null,
1797
+ });
1798
+ if (preview) await preview.begin();
1799
+ else editor.begin(target);
1800
+ if (streamDisposed || myGen !== streamGeneration) {
1801
+ preview?.clear();
1802
+ return false;
1803
+ }
1776
1804
  const sessionCallbacks = (ed) => ({
1777
1805
  onPartial: (p) => {
1806
+ if (streamDisposed || myGen !== streamGeneration) return;
1778
1807
  try {
1779
1808
  noteStreamingPartial();
1780
1809
  } catch {}
@@ -1845,6 +1874,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1845
1874
  logger?.log("STT", "Streaming dictation started", "debug");
1846
1875
  return true;
1847
1876
  } catch (err) {
1877
+ preview?.clear();
1848
1878
  logger?.log("STT", `Streaming start failed: ${err?.message || err}`, "error");
1849
1879
  try {
1850
1880
  clearStreamingToast();
@@ -1884,6 +1914,9 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1884
1914
  const controller = streamController;
1885
1915
  const editor = streamEditor;
1886
1916
  const target = streamTarget;
1917
+ const myGen = streamGeneration;
1918
+ finalizingController = controller;
1919
+ if (preview) processing = true;
1887
1920
  streamController = null;
1888
1921
  streamEditor = null;
1889
1922
  streamTarget = null;
@@ -1897,6 +1930,8 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1897
1930
  try {
1898
1931
  done = await controller.stop();
1899
1932
  } catch (err) {
1933
+ finalizingController = null;
1934
+ if (preview) processing = false;
1900
1935
  logger?.log("STT", `Streaming stop failed: ${err?.message || err}`, "error");
1901
1936
  try {
1902
1937
  clearStreamingToast();
@@ -1907,12 +1942,20 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1907
1942
  retainWarmStreamController(controller);
1908
1943
  return null;
1909
1944
  }
1945
+ finalizingController = null;
1946
+ if (streamDisposed || myGen !== streamGeneration) {
1947
+ preview?.clear();
1948
+ if (preview) processing = false;
1949
+ await controller.dispose?.();
1950
+ return null;
1951
+ }
1910
1952
  // The stop-tail resolved: the sticky status served its purpose (no
1911
1953
  // silent gap during the wait). Clear before the terminal toasts below.
1912
1954
  try {
1913
1955
  clearStreamingToast();
1914
1956
  } catch {}
1915
1957
  if (done?.error && isStreamingServerFault(done.error)) {
1958
+ if (preview) processing = false;
1916
1959
  // Server at fault (not ready / transcribe failure): release the lease
1917
1960
  // so the next start cold-loads from a clean handle instead of reusing
1918
1961
  // a dead server.
@@ -1940,10 +1983,20 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1940
1983
  // Every path below retains the warm lease: the mic stopped, the server
1941
1984
  // stays loaded for the next dictation.
1942
1985
  if (!text) {
1986
+ if (preview) processing = false;
1943
1987
  toast("No speech detected", "warning");
1944
1988
  retainWarmStreamController(controller);
1945
1989
  return { text: "" };
1946
1990
  }
1991
+ if (preview) {
1992
+ // v2 has a visible preview but no composer append. Finalize submits
1993
+ // once through the same home/create/cancellation fence as batch STT.
1994
+ processing = true;
1995
+ const result = await submitTurnText(client, null, toast, text, logger);
1996
+ if (!streamDisposed && myGen === streamGeneration) retainWarmStreamController(controller);
1997
+ else await controller.dispose?.();
1998
+ return { ...result, submitted: !result.error };
1999
+ }
1947
2000
  if (fin?.inserted) {
1948
2001
  logger?.log("STT", `Streaming finalized via owned range chars=${text.length}`, "debug");
1949
2002
  if (submit || autoSubmit) {
@@ -2007,6 +2060,18 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2007
2060
  // is fully stopped by cancel(); the server lease is retained warm for the
2008
2061
  // next dictation (user cancel is per-dictation, never a server fault).
2009
2062
  async function cancelStreamingDictation() {
2063
+ streamGeneration += 1;
2064
+ preview?.clear();
2065
+ if (preview && processing) cancelRecording(logger);
2066
+ if (finalizingController) {
2067
+ const finishing = finalizingController;
2068
+ finalizingController = null;
2069
+ cancelRecording(logger);
2070
+ await finishing.cancel();
2071
+ processing = false;
2072
+ clearStreamingToast();
2073
+ return true;
2074
+ }
2010
2075
  if (!streamingDictationActive || !streamController) return false;
2011
2076
  const controller = streamController;
2012
2077
  const editor = streamEditor;
@@ -2031,6 +2096,14 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2031
2096
  }
2032
2097
 
2033
2098
  api.lifecycle?.onDispose?.(() => {
2099
+ streamDisposed = true;
2100
+ streamGeneration += 1;
2101
+ preview?.clear();
2102
+ if (finalizingController) {
2103
+ const finishing = finalizingController;
2104
+ finalizingController = null;
2105
+ void finishing.dispose?.();
2106
+ }
2034
2107
  // Unload mid-recording must not orphan the sox child: cancel kills it,
2035
2108
  // bumps the pipeline generation (late transcriptions are dropped), and
2036
2109
  // clears the sticky toast. processing is not owned by cancel, reset it
@@ -2067,7 +2140,12 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2067
2140
  cancel: () => cancelRecording(logger),
2068
2141
  discard: () => discardTurn(),
2069
2142
  transcribeTurn: () => transcribeTurn(kv, complete, client, api, toast, systemPrompt, logger),
2070
- submitTurnText: (text) => submitTurnText(client, renderer, toast, text, logger),
2143
+ // Conversation turns are hands-free submissions, not dictation: on v2
2144
+ // they must not press Enter in whatever field happens to have focus (a
2145
+ // permission dialog could own it), so they always go through the
2146
+ // session.prompt submit path. v1 keeps its focused-insert + submit.
2147
+ submitTurnText: (text) =>
2148
+ submitTurnText(client, client.tui?.v2 ? null : renderer, toast, text, logger),
2071
2149
  startStreaming: startStreamingDictation,
2072
2150
  finalizeStreaming: finalizeStreamingDictation,
2073
2151
  cancelStreaming: cancelStreamingDictation,
package/lib/tts.js CHANGED
@@ -209,7 +209,7 @@ Rules:
209
209
 
210
210
  // ---- Session helpers ----
211
211
 
212
- async function getTurnAssistantText(client, api) {
212
+ export async function getTurnAssistantText(client, api) {
213
213
  const route = api.route.current;
214
214
  if (route.name !== "session") return null;
215
215
 
package/lib/v2-compat.js CHANGED
@@ -1,4 +1,6 @@
1
- // OpenCode v2 compatibility shim (submit-only mode).
1
+ import { createV2Preview } from "./v2-preview.js";
2
+
3
+ // OpenCode v2 compatibility shim.
2
4
  //
3
5
  // WHY this file exists: v2's TUI plugin `Context` renamed or replaced every
4
6
  // v1 `TuiPluginApi` surface this plugin uses (see the drift table in the
@@ -8,12 +10,15 @@
8
10
  // presence, never version strings), and every surface with no v2 path
9
11
  // degrades loudly (warn log + honest toast), never silently.
10
12
  //
11
- // Non-negotiable product decision (user-accepted): v2 2.0.26 has no
12
- // plugin-reachable append-to-composer API (`tui.prompt.append` exists only
13
- // as a server→TUI event type, not a client method). So on v2, STT
14
- // transcribes and SUBMITS immediately via `session.prompt`. The adapted api
15
- // passes `renderer: null` so `appendTranscription` skips the focused-input
16
- // insert path and lands on the client shim below, which submits.
13
+ // Composer editing on v2: there is no plugin-reachable append-to-composer
14
+ // call (`tui.prompt.append` exists only as a server→TUI event type, not a
15
+ // client method), but `Context.renderer` IS the live `CliRenderer`. So the
16
+ // adapted api forwards it, and the shared `insertIntoFocusedInput` helper
17
+ // types at the cursor of whatever editable field currently has focus — the
18
+ // session composer, a dialog search box, or another plugin's input. Only an
19
+ // explicit submit (`/stt-submit`, `sttAutoSubmit`, conversation) presses
20
+ // Enter. With no editable field focused, text is retained in a recovery
21
+ // dialog instead of being sent as a prompt.
17
22
 
18
23
  // Event renames with no same-named v2 equivalent. Values are the v2 event
19
24
  // type to subscribe to instead (both verified present in the v2
@@ -35,6 +40,36 @@ export function isV2Context(ctx) {
35
40
  );
36
41
  }
37
42
 
43
+ // Normalize a v2 `SessionMessageInfo` union member to the v1 message shape
44
+ // our lib modules read (`{id, role, time, summary?}` + v1-like `parts`).
45
+ // v2 uses `type: "user" | "assistant" | …` (no `role` field); assistant text
46
+ // lives in `content[]` (`{type: "text", text}`), user text in `text`.
47
+ // Without this, every v1 `msg.role === "assistant"` filter is empty on v2 —
48
+ // the root cause of "no assistant response to speak" and the dead
49
+ // conversation reply stream. v1 hosts never pass through here.
50
+ function normalizeV2Message(msg) {
51
+ if (!msg || typeof msg !== "object") return msg;
52
+ const type = msg?.type;
53
+ const role = type === "assistant" ? "assistant" : type === "user" ? "user" : type;
54
+ let parts = [];
55
+ try {
56
+ if (Array.isArray(msg.content)) {
57
+ parts = msg.content
58
+ .map((c, i) =>
59
+ c && typeof c.text === "string"
60
+ ? { id: `${msg.id}:${i}`, type: c.type === "text" ? "text" : c.type, text: c.text }
61
+ : null,
62
+ )
63
+ .filter(Boolean);
64
+ } else if (typeof msg.text === "string" && msg.text) {
65
+ parts = [{ id: `${msg.id}:0`, type: "text", text: msg.text }];
66
+ }
67
+ } catch {
68
+ parts = [];
69
+ }
70
+ return { ...msg, role, parts };
71
+ }
72
+
38
73
  // Wrap a v2 event envelope `{type, data}` so v1-style handlers reading
39
74
  // `event.properties` keep working. v1 handlers also read renamed fields,
40
75
  // normalized per remapped type below.
@@ -43,11 +78,20 @@ function wrapHandler(type, fn) {
43
78
  const data = event?.data ?? event?.properties ?? {};
44
79
  let properties = { ...data };
45
80
  if (type === "message.part.delta") {
46
- // v2 `session.text.delta` data: {sessionID, assistantMessageID, delta}.
47
- // v1 handler expects {sessionID, messageID, delta, field}.
81
+ // v2 `session.text.delta` data: {sessionID, assistantMessageID,
82
+ // ordinal, delta}. v1 handler expects {sessionID, messageID, partID?,
83
+ // delta, field}. `ordinal` is the content-block index, so synthesize
84
+ // the part id the normalized parts carry (`${messageID}:${ordinal}`)
85
+ // for the part-type guard; a missed lookup stays fail-open downstream.
86
+ const messageID = data.assistantMessageID ?? data.messageID;
48
87
  properties = {
49
88
  ...properties,
50
- messageID: data.assistantMessageID ?? data.messageID,
89
+ messageID,
90
+ partID:
91
+ data.partID ??
92
+ (messageID !== undefined && data.ordinal !== undefined
93
+ ? `${messageID}:${data.ordinal}`
94
+ : data.partID),
51
95
  field: "text",
52
96
  };
53
97
  }
@@ -160,8 +204,13 @@ function DialogSelect(props) {
160
204
 
161
205
  export function adaptV2Context(ctx) {
162
206
  const disposers = [];
207
+ const preview = createV2Preview(ctx);
208
+ disposers.push(() => preview.clear());
163
209
  const warned = new Set();
164
210
  const warnMessages = [];
211
+ let disposed = false;
212
+ let submission = null;
213
+ let submissionText = null;
165
214
  // Loud degradation: warn logs are collected for evidence and also sent
166
215
  // through the real logger once boot creates it (wired via onWarn below).
167
216
  let warnSink = null;
@@ -196,38 +245,116 @@ export function adaptV2Context(ctx) {
196
245
  }
197
246
  }
198
247
 
199
- async function submitTextAsPrompt(text) {
200
- const sessionID = currentSessionID();
201
- if (!sessionID) {
202
- const kept = String(text || "").slice(0, 120);
203
- toast({
204
- message: `Voice: no active session — transcript kept on screen, nothing submitted (${kept})`,
205
- variant: "warning",
206
- duration: 5000,
207
- });
208
- warn("submit attempted with no active session; transcript not submitted");
209
- throw new Error("No active session to submit to");
248
+ function submitTextAsPrompt(text, isCurrent = () => true) {
249
+ const captured = String(text || "");
250
+ if (submission) {
251
+ return captured === submissionText
252
+ ? submission
253
+ : Promise.reject(new Error("Voice submission already in progress"));
210
254
  }
211
- await ctx.client.session.prompt({ sessionID, text: String(text || "") });
212
- // No toast here: the caller's turn toast (appendTurnText/submitTurnText)
213
- // reports the submit, keyed off the `submitted` marker below. One toast
214
- // per turn, never a faked edit step.
255
+ const alive = () => !disposed && isCurrent();
256
+ const check = () => {
257
+ if (!alive()) throw new Error("Voice submission cancelled");
258
+ };
259
+ const task = (async () => {
260
+ check();
261
+ if (!captured.trim()) throw new Error("No transcript to submit");
262
+ const selected = ctx.ui.model?.current();
263
+ const model = selected
264
+ ? {
265
+ id: selected.modelID,
266
+ providerID: selected.providerID,
267
+ ...(selected.variant ? { variant: selected.variant } : {}),
268
+ }
269
+ : undefined;
270
+ let sessionID = currentSessionID();
271
+ try {
272
+ if (!sessionID) {
273
+ const created = await ctx.client.session.create({
274
+ ...(ctx.location ? { location: ctx.location } : {}),
275
+ ...(model ? { model } : {}),
276
+ });
277
+ check();
278
+ sessionID = created.id;
279
+ ctx.ui.router.navigate({ type: "session", sessionID });
280
+ }
281
+ check();
282
+ // Model/agent belong to session metadata in v2, not prompt input.
283
+ await ctx.client.session.prompt({ sessionID, text: captured });
284
+ check();
285
+ } catch (err) {
286
+ if (alive()) {
287
+ // A dialog retains the full transcript for recovery; a short toast
288
+ // alone would lose long recordings on create/prompt failure.
289
+ void ctx.ui.dialog.alert({
290
+ title: "Voice transcript — not submitted",
291
+ message: captured,
292
+ });
293
+ }
294
+ throw err;
295
+ }
296
+ })();
297
+ submission = task;
298
+ submissionText = captured;
299
+ void task
300
+ .finally(() => {
301
+ if (submission === task) submission = null;
302
+ })
303
+ .catch(() => {});
304
+ return task;
305
+ }
306
+
307
+ // Reached only when no focused renderable accepts text. Never redirect the
308
+ // transcript into a chat prompt the user did not ask for: keep it visible
309
+ // in a recovery dialog and let the caller report `retained`.
310
+ function retainTranscript(text) {
311
+ const captured = String(text || "");
312
+ try {
313
+ void ctx.ui.dialog.alert({
314
+ title: "Voice transcript — no text field focused",
315
+ message: captured,
316
+ });
317
+ } catch {}
318
+ return { data: null, retained: true };
215
319
  }
216
320
 
217
321
  // `Object.create` (not spread) so client methods keep their `this`.
322
+ // v2 `session.message` is a `{get}` namespace, not the v1 callable, so
323
+ // the v1 call shape `client.session.message({sessionID, messageID})` is
324
+ // shimmed onto `message.get` with the result normalized (see above).
325
+ const session = Object.create(ctx.client.session, {
326
+ message: {
327
+ value: async (input) => {
328
+ const full = await ctx.client.session.message.get({
329
+ sessionID: input?.sessionID,
330
+ messageID: input?.messageID,
331
+ });
332
+ return { data: normalizeV2Message(full) };
333
+ },
334
+ },
335
+ });
218
336
  const client = Object.create(ctx.client, {
337
+ session: { value: session },
219
338
  // `client.app.log` has no v2 path — silent no-op (pre-existing `?.`
220
339
  // tolerance in createLogger already expects absence).
221
340
  app: { value: { log: async () => {} } },
222
341
  tui: {
223
342
  value: {
224
- // Submit-only: append-for-edit is unimplementable on v2, so append
225
- // becomes submit. Accepts both v1 call shapes ({body:{text}} and
226
- // {text}) used by stt.js. The `submitted` marker lets the caller's
227
- // toast report a submit instead of an edit-step append.
343
+ // Marker for v1-shaped callers: this host has no composer-append API
344
+ // and dispatches prompts through `session.prompt` below (an explicit
345
+ // submit may resolve after the reply stream has started).
346
+ v2: true,
347
+ // Called only after `insertIntoFocusedInput` found no editable field.
348
+ // Accepts both v1 call shapes ({body:{text}} and {text}) used by
349
+ // stt.js. Explicit submits still go out via `session.prompt`; plain
350
+ // appends are retained visibly instead of being sent.
228
351
  appendPrompt: async (input) => {
229
- await submitTextAsPrompt(input?.body?.text ?? input?.text ?? "");
230
- return { data: null, submitted: true };
352
+ const text = input?.body?.text ?? input?.text ?? "";
353
+ if (input?.submit === true) {
354
+ await submitTextAsPrompt(text, input?.isCurrent);
355
+ return { data: null, submitted: true };
356
+ }
357
+ return retainTranscript(text);
231
358
  },
232
359
  submitPrompt: async () => {},
233
360
  },
@@ -238,11 +365,12 @@ export function adaptV2Context(ctx) {
238
365
 
239
366
  const api = {
240
367
  client,
241
- // Submit-only enforcement: with no renderer, `appendTranscription`
242
- // skips `insertIntoFocusedInput` and lands on the client shim above,
243
- // which submits via `session.prompt`. v2 renderer streaming previews
244
- // are therefore disabled by construction, not by flag.
245
- renderer: null,
368
+ // The live CliRenderer: `currentFocusedRenderable` lets the shared
369
+ // `insertIntoFocusedInput` helper type into the focused composer, dialog
370
+ // search box, or plugin input. Streaming display is separate:
371
+ // voicePreview uses additive slots without composer editing.
372
+ renderer: ctx.renderer ?? null,
373
+ voicePreview: preview,
246
374
  ui: { toast, dialog, DialogSelect },
247
375
  event: {
248
376
  on: (type, fn) => {
@@ -279,15 +407,39 @@ export function adaptV2Context(ctx) {
279
407
  session: {
280
408
  messages: (sessionID) => {
281
409
  try {
282
- return ctx.data.session.message.list(sessionID) || [];
410
+ return (ctx.data.session.message.list(sessionID) || []).map(normalizeV2Message);
283
411
  } catch {
284
412
  return [];
285
413
  }
286
414
  },
287
415
  },
288
- // v1 `state.part(messageID)` (message parts) has no v2 data path;
289
- // callers treat a missing part as "not non-text" and proceed.
290
- part: () => [],
416
+ // v1 `state.part(messageID)` over live v2 embedded content. Resolve
417
+ // through the current session first, then known sessions (message ids
418
+ // are unique). Never cache text: content changes while replies stream.
419
+ part: (messageID) => {
420
+ try {
421
+ const sessionIDs = [];
422
+ const current = currentSessionID();
423
+ if (current) sessionIDs.push(current);
424
+ for (const s of ctx.data.session.list() || []) {
425
+ if (s?.id && !sessionIDs.includes(s.id)) sessionIDs.push(s.id);
426
+ }
427
+ for (const sid of sessionIDs) {
428
+ let msg;
429
+ try {
430
+ msg = ctx.data.session.message.get(sid, messageID);
431
+ } catch {
432
+ msg = undefined;
433
+ }
434
+ if (msg) {
435
+ return normalizeV2Message(msg)?.parts || [];
436
+ }
437
+ }
438
+ } catch {
439
+ // Fall through to [] below.
440
+ }
441
+ return [];
442
+ },
291
443
  path: {
292
444
  // v2 `location` is a LocationRef, not a directory string.
293
445
  get directory() {
@@ -313,15 +465,16 @@ export function adaptV2Context(ctx) {
313
465
  // + layer `bindings`. Every command joins palette + slash so all
314
466
  // stay reachable; keybinds are translated below (invalid binds are
315
467
  // reported by the host parser, not here).
316
- // v1 bind strings use a `<leader>X` token for two-key sequences; v2
317
- // bind strings have no leader token — sequences are space-separated
318
- // keys with +-joined modifiers, and the leader is literally ctrl+x
319
- // (cf the builtin "Switch model ctrl+x m"). Anything else passes
320
- // through untouched (v2-native "ctrl+r" style binds need no mapping).
468
+ // v1 bind strings use a `<leader>X` token for two-key sequences; the
469
+ // v2 keymap resolves the `<leader>` token itself (leader defaults to
470
+ // ctrl+x, configurable as `keybinds.leader`), so v1 binds pass
471
+ // through UNTOUCHED — including v2-native "ctrl+r" style customs.
472
+ // (An earlier revision translated to `ctrl+x X`; that form never
473
+ // dispatches on the host and was the actual shortcut bug.)
474
+ // "none"/falsy disables: reported as bind:false so no automatic
475
+ // binding is configured, and the layer builder omits it.
321
476
  const toV2Bind = (bind) => {
322
- if (typeof bind !== "string" || bind.length === 0) return false;
323
- const leader = bind.match(/^<leader>(.+)$/);
324
- if (leader) return `ctrl+x ${leader[1]}`;
477
+ if (typeof bind !== "string" || bind.length === 0 || bind === "none") return false;
325
478
  return bind;
326
479
  };
327
480
  const mapped = commands.map((c) => ({
@@ -398,13 +551,44 @@ export function adaptV2Context(ctx) {
398
551
  },
399
552
  };
400
553
 
401
- // v1 reads `api.state.provider` as an array (`resolveVoiceProviderModel`
402
- // does `api?.state?.provider || []` then `.find`); v2 lists per location.
403
- // A getter keeps the array shape without touching callers.
554
+ // v1 reads `api.state.provider` as an array of `{models, env, options}`
555
+ // (`voice-model.js` / `llm-client.js`). v2 splits those across collections:
556
+ // providers carry NO models (they live in `location.model`), auth env names
557
+ // live on the provider's integration method `{type:"env", names}`, and the
558
+ // endpoint is `settings.baseURL` (`options` in v1). Recombining them here is
559
+ // what keeps /voice-model from seeing every provider as model-less.
404
560
  Object.defineProperty(api.state, "provider", {
405
561
  get() {
406
562
  try {
407
- return ctx.data.location.provider.list() || [];
563
+ const providers = ctx.data.location.provider.list() || [];
564
+ const models = ctx.data.location.model?.list?.() || [];
565
+ const integrations = ctx.data.location.integration?.list?.() || [];
566
+ const envByIntegration = new Map();
567
+ for (const integration of integrations) {
568
+ const names = (integration?.methods || [])
569
+ .filter((method) => method?.type === "env")
570
+ .flatMap((method) => method?.names || []);
571
+ if (names.length > 0) envByIntegration.set(integration.id, names);
572
+ }
573
+ const modelsByProvider = new Map();
574
+ for (const model of models) {
575
+ const providerID = model?.providerID;
576
+ if (!providerID) continue;
577
+ const list = modelsByProvider.get(providerID);
578
+ if (list) list.push(model);
579
+ else modelsByProvider.set(providerID, [model]);
580
+ }
581
+ return providers.map((provider) => {
582
+ const own = modelsByProvider.get(provider.id) || [];
583
+ return {
584
+ ...provider,
585
+ models: Object.fromEntries(own.map((model) => [model.id, model])),
586
+ env:
587
+ envByIntegration.get(provider.integrationID ?? provider.id) ??
588
+ (Array.isArray(provider.env) ? provider.env : []),
589
+ options: provider.settings || {},
590
+ };
591
+ });
408
592
  } catch {
409
593
  return [];
410
594
  }
@@ -413,6 +597,7 @@ export function adaptV2Context(ctx) {
413
597
  });
414
598
 
415
599
  function cleanup() {
600
+ disposed = true;
416
601
  for (const fn of disposers.splice(0)) {
417
602
  try {
418
603
  const r = fn();
@@ -424,7 +609,6 @@ export function adaptV2Context(ctx) {
424
609
  return {
425
610
  api,
426
611
  cleanup,
427
- submitOnly: true,
428
612
  warnMessages,
429
613
  onWarn: (sink) => void (warnSink = sink),
430
614
  };
@@ -0,0 +1,74 @@
1
+ // Host-owned Solid/OpenTUI modules are loaded only when a v2 recording starts.
2
+ // No composer mutation: this bounded display is an additive, documented slot.
3
+ export function createV2Preview(
4
+ ctx,
5
+ load = async () => {
6
+ const [solid, tui] = await Promise.all([import("solid-js"), import("@opentui/solid")]);
7
+ return { ...solid, ...tui };
8
+ },
9
+ ) {
10
+ let generation = 0;
11
+ let active = false;
12
+ let partial = { stableText: "", tentativeText: "" };
13
+ const nodes = new Set();
14
+ let setContent = null;
15
+ function display() {
16
+ const stable = partial.stableText || "";
17
+ const tentative = partial.tentativeText || "";
18
+ return `Voice live · ${stable ? `${stable} ` : ""}${tentative ? `[${tentative}]` : stable ? "" : "Listening…"}`.slice(
19
+ -1600,
20
+ );
21
+ }
22
+ let unclaims = [];
23
+ function clear() {
24
+ generation += 1;
25
+ active = false;
26
+ partial = { stableText: "", tentativeText: "" };
27
+ nodes.clear();
28
+ setContent = null;
29
+ for (const unclaim of unclaims.splice(0)) unclaim();
30
+ }
31
+ return {
32
+ async begin() {
33
+ clear();
34
+ active = true;
35
+ const mine = generation;
36
+ const { createElement, setProp, insertNode, onCleanup } = await load();
37
+ if (!active || mine !== generation) return false;
38
+ setContent = (node, value) => setProp(node, "content", value);
39
+ const render = () => {
40
+ if (!active || mine !== generation) return null;
41
+ const box = createElement("box");
42
+ setProp(box, "height", 3);
43
+ setProp(box, "flexShrink", 0);
44
+ const node = createElement("text");
45
+ setProp(node, "maxHeight", 5);
46
+ setProp(node, "minHeight", 1);
47
+ setProp(node, "width", "100%");
48
+ setProp(node, "fg", ctx.theme?.text || "#eeeeee");
49
+ setProp(node, "content", display());
50
+ nodes.add(node);
51
+ insertNode(box, node);
52
+ onCleanup?.(() => nodes.delete(node));
53
+ return box;
54
+ };
55
+ unclaims = [ctx.ui.slot({ before: "prompt.footer", render })];
56
+ return true;
57
+ },
58
+ applyPartial(p) {
59
+ if (!active) return;
60
+ partial = { stableText: p?.stableText || "", tentativeText: p?.tentativeText || "" };
61
+ for (const node of nodes) {
62
+ if (node.isDestroyed) nodes.delete(node);
63
+ else setContent?.(node, display());
64
+ }
65
+ },
66
+ getTranscript: () => [partial.stableText, partial.tentativeText].filter(Boolean).join(" "),
67
+ finalize(text) {
68
+ clear();
69
+ return { text, inserted: false };
70
+ },
71
+ cancel: clear,
72
+ clear,
73
+ };
74
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bojackduy/opencode-voice",
3
- "version": "0.11.0",
3
+ "version": "0.11.2",
4
4
  "description": "Speech-to-text and text-to-speech for OpenCode. Record voice prompts with whisper-cpp, hear responses via Piper TTS, with LLM normalization through any OpenAI-compatible endpoint.",
5
5
  "keywords": [
6
6
  "opencode",