@bojackduy/opencode-voice 0.11.0 → 0.11.1

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
@@ -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,21 @@ 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):** submits immediately, no edit step (see above). Recording
88
+ from home creates a session only when final text is ready, opens it, and
89
+ submits once. The selected model is carried into the new session; existing
90
+ sessions retain their agent/model. On home, v2's public plugin API exposes
91
+ the selected model but no selected-agent getter, so session creation uses
92
+ the host's default agent. Create/submit failures show the full transcript in
93
+ a recovery dialog. Cancelling before submission drops the captured text.
94
+ - **STT (streaming dictation):** enable with `"sttMode": "streaming"` in plugin
95
+ options. Local whisper streaming displays a bounded live transcript above
96
+ the prompt footer on home and session screens. Stable words are plain;
97
+ the tentative tail is bracketed and may change. Press the record key again
98
+ to finalize and submit once, or cancel to clear the preview without sending.
99
+ No composer edit step is promised on v2. Batch remains the default and
100
+ produces text after recording stops; the hosted `sttEndpoint` option is for
101
+ batch transcription, not this local streaming preview.
95
102
  - **TTS / conversation / live notes / cancel:** same semantics as v1 (auto
96
103
  TTS on idle/permission/question gates, conversation barge-in, live-notes
97
104
  background saves, one-key cancel).
@@ -740,7 +747,8 @@ Idle invocations toast `Nothing to cancel`. Keybind override:
740
747
 
741
748
  Live local dictation for short prompts: the mic stays on, partial
742
749
  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
750
+ finalizes the text into the prompt box on v1, or submits it once on v2.
751
+ Set `"sttMode": "streaming"` in the
744
752
  plugin options (default `"batch"` keeps the record-then-transcribe flow
745
753
  exactly as before).
746
754
 
@@ -754,7 +762,11 @@ What you see: a sticky status toast stays up for the whole session so
754
762
  there is never a silent gap — `Loading speech model…` during cold start,
755
763
  `● Streaming dictation — listening` with an elapsed timer while live
756
764
  (plus a `(listening…)` note when inference runs slow), and `Finalizing…`
757
- between the second keypress and the insert. The OpenCode renderer only
765
+ between the second keypress and finalization. On v2, a bounded transcript
766
+ above the prompt footer updates during recording: stable words are plain,
767
+ tentative words are bracketed. Finalization submits directly to the active
768
+ session (or creates one from home); cancel clears the preview without sending.
769
+ On v1, the OpenCode renderer only
758
770
  exposes `insertText`/`submit`, with no live range-replacement API, so
759
771
  partials render as preview toasts (`🎙 ...`) alongside (not instead of)
760
772
  the sticky status, and the full text is inserted EXACTLY ONCE at
@@ -784,8 +796,9 @@ Options:
784
796
  - `sttMode` _(optional, default `"batch"`)_ - `"streaming"` selects this mode
785
797
  - `sttStreamWindowMs` _(optional, default `10000`)_ - rolling audio window
786
798
  - `sttStreamStepMs` _(optional, default `1000`)_ - live transcription cadence
787
- - `sttAutoSubmit` applies after streaming finalize (submit via the dictated
788
- field only, as above)
799
+ - `sttAutoSubmit` applies after streaming finalize on v1 (submit via the
800
+ dictated field only, as above). v2 always submits final text because there
801
+ is no composer append/edit API.
789
802
 
790
803
  Measured latencies (Apple M3 Pro, 36 GB, whisper.cpp 1.9.4 with Metal,
791
804
  `ggml-large-v3-turbo-q5_0.bin`, scripted speech through the real
@@ -806,11 +819,14 @@ at roughly that rate, not every cadence tick — the scheduler coalesces
806
819
  while inference is busy, so partials are flicker-free but not instant.
807
820
  Only one smaller-model data point exists (none: no smaller model file is
808
821
  installed locally, and nothing was downloaded for measurement). E2E
809
- coverage is mic-less/TUI-less by necessity: session flows are proven
822
+ coverage is mic-less: v1 session flows are proven
810
823
  through the real command paths with faked capture/renderer
811
824
  (`test/streaming-e2e.test.js`, `test/streaming-gaps.test.js`), while the
812
825
  numbers above come from the real server path with scripted audio — no
813
- live-mic session was measured.
826
+ live-mic session was measured. v2 preview rendering was additionally verified
827
+ in the real v2.0.26 TUI using injected stable/tentative text on home and
828
+ session screens; final-once/cancellation flows use scripted capture and the
829
+ real streaming controller in `test/v2-preview.test.js`.
814
830
 
815
831
  ## How it works
816
832
 
package/index.js CHANGED
@@ -61,7 +61,8 @@ export default {
61
61
  // append-to-composer API, so every STT turn submits immediately.
62
62
  const notice =
63
63
  "Voice on OpenCode v2: transcripts submit immediately as-is " +
64
- "(submit-only mode — append-for-edit is unavailable on v2)";
64
+ "(submit-only mode — append-for-edit is unavailable on v2). " +
65
+ "For live preview, set sttMode to streaming.";
65
66
  adapted.api.ui.toast({ message: notice, variant: "warning", duration: 8000 });
66
67
  logger?.log?.("plugin", `v2 submit-only mode: ${notice}`, "warn");
67
68
  // v2 has no client.app.log, so the logger above is a no-op: compat
@@ -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?.submitOnly) {
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/stt.js CHANGED
@@ -1371,12 +1371,15 @@ 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 submit-only adapter consumes this fence; v1 request bodies
1380
+ // remain unchanged. Re-check after an asynchronous home session create.
1381
+ const fence = client.tui.submitOnly ? { isCurrent } : {};
1382
+ let appendResult = await client.tui.appendPrompt({ body: { text }, ...fence });
1380
1383
 
1381
1384
  if (appendResult?.error?.data?.message === "Expected object, got undefined") {
1382
1385
  appendResult = await client.tui.appendPrompt({ text });
@@ -1482,8 +1485,9 @@ async function transcribeTurn(kv, complete, client, api, toast, systemPrompt, lo
1482
1485
  }
1483
1486
 
1484
1487
  async function submitTurnText(client, renderer, toast, text, logger) {
1488
+ const myGen = pipelineGeneration;
1485
1489
  try {
1486
- await appendTranscription(client, renderer, text, true);
1490
+ await appendTranscription(client, renderer, text, true, () => myGen === pipelineGeneration);
1487
1491
  clearProcessingToast();
1488
1492
  logger?.log("STT", `Turn submitted chars=${text.length}`, "debug");
1489
1493
  toast("Transcription submitted", "success");
@@ -1500,8 +1504,15 @@ async function submitTurnText(client, renderer, toast, text, logger) {
1500
1504
  }
1501
1505
 
1502
1506
  async function appendTurnText(client, renderer, toast, text, logger) {
1507
+ const myGen = pipelineGeneration;
1503
1508
  try {
1504
- const appendResult = await appendTranscription(client, renderer, text, false);
1509
+ const appendResult = await appendTranscription(
1510
+ client,
1511
+ renderer,
1512
+ text,
1513
+ false,
1514
+ () => myGen === pipelineGeneration,
1515
+ );
1505
1516
  clearProcessingToast();
1506
1517
  logger?.log("STT", `Turn appended chars=${text.length}`, "debug");
1507
1518
  // Submit-only hosts (OpenCode v2: no append-to-composer API) submit via
@@ -1589,6 +1600,10 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1589
1600
  }
1590
1601
  const client = api.client;
1591
1602
  const renderer = opts?.focusMode === "primary" ? null : api.renderer;
1603
+ const preview = api.voicePreview;
1604
+ let streamGeneration = 0;
1605
+ let streamDisposed = false;
1606
+ let finalizingController = null;
1592
1607
  const systemPrompt = selectSttSystemPrompt(sttNormalizeMode, prompts?.stt);
1593
1608
  const backend = detectAudioBackend();
1594
1609
  logger?.log("STT", `Audio backend=${backend}`, "debug");
@@ -1733,11 +1748,12 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1733
1748
  }
1734
1749
 
1735
1750
  function isStreaming() {
1736
- return streamingDictationActive;
1751
+ return streamingDictationActive || (preview && (streamStarting || !!finalizingController));
1737
1752
  }
1738
1753
 
1739
1754
  async function startStreamingDictation() {
1740
- if (streamingDictationActive || streamStarting) return false;
1755
+ if (streamingDictationActive || streamStarting || finalizingController || streamDisposed)
1756
+ return false;
1741
1757
  if (recording || processing) {
1742
1758
  toast("STT busy, please wait...");
1743
1759
  return false;
@@ -1751,6 +1767,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1751
1767
  return false;
1752
1768
  }
1753
1769
  streamStarting = true;
1770
+ const myGen = ++streamGeneration;
1754
1771
  // Model/language change tears down the warm lease first so the next
1755
1772
  // start loads the new model instead of reusing the stale server. The
1756
1773
  // key carries the bound server port, so a lease on a different port is
@@ -1768,13 +1785,21 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1768
1785
  const { createStreamingEditorAdapter } = await import("./streaming-editor.js");
1769
1786
  const focused = renderer?.currentFocusedRenderable || null;
1770
1787
  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);
1788
+ const editor =
1789
+ preview ||
1790
+ createStreamingEditorAdapter({
1791
+ toast: (message, variant) => api.ui.toast({ message, variant, duration: 2500 }),
1792
+ getFocused: () => renderer?.currentFocusedRenderable || null,
1793
+ });
1794
+ if (preview) await preview.begin();
1795
+ else editor.begin(target);
1796
+ if (streamDisposed || myGen !== streamGeneration) {
1797
+ preview?.clear();
1798
+ return false;
1799
+ }
1776
1800
  const sessionCallbacks = (ed) => ({
1777
1801
  onPartial: (p) => {
1802
+ if (streamDisposed || myGen !== streamGeneration) return;
1778
1803
  try {
1779
1804
  noteStreamingPartial();
1780
1805
  } catch {}
@@ -1845,6 +1870,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1845
1870
  logger?.log("STT", "Streaming dictation started", "debug");
1846
1871
  return true;
1847
1872
  } catch (err) {
1873
+ preview?.clear();
1848
1874
  logger?.log("STT", `Streaming start failed: ${err?.message || err}`, "error");
1849
1875
  try {
1850
1876
  clearStreamingToast();
@@ -1884,6 +1910,9 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1884
1910
  const controller = streamController;
1885
1911
  const editor = streamEditor;
1886
1912
  const target = streamTarget;
1913
+ const myGen = streamGeneration;
1914
+ finalizingController = controller;
1915
+ if (preview) processing = true;
1887
1916
  streamController = null;
1888
1917
  streamEditor = null;
1889
1918
  streamTarget = null;
@@ -1897,6 +1926,8 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1897
1926
  try {
1898
1927
  done = await controller.stop();
1899
1928
  } catch (err) {
1929
+ finalizingController = null;
1930
+ if (preview) processing = false;
1900
1931
  logger?.log("STT", `Streaming stop failed: ${err?.message || err}`, "error");
1901
1932
  try {
1902
1933
  clearStreamingToast();
@@ -1907,12 +1938,20 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1907
1938
  retainWarmStreamController(controller);
1908
1939
  return null;
1909
1940
  }
1941
+ finalizingController = null;
1942
+ if (streamDisposed || myGen !== streamGeneration) {
1943
+ preview?.clear();
1944
+ if (preview) processing = false;
1945
+ await controller.dispose?.();
1946
+ return null;
1947
+ }
1910
1948
  // The stop-tail resolved: the sticky status served its purpose (no
1911
1949
  // silent gap during the wait). Clear before the terminal toasts below.
1912
1950
  try {
1913
1951
  clearStreamingToast();
1914
1952
  } catch {}
1915
1953
  if (done?.error && isStreamingServerFault(done.error)) {
1954
+ if (preview) processing = false;
1916
1955
  // Server at fault (not ready / transcribe failure): release the lease
1917
1956
  // so the next start cold-loads from a clean handle instead of reusing
1918
1957
  // a dead server.
@@ -1940,10 +1979,20 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1940
1979
  // Every path below retains the warm lease: the mic stopped, the server
1941
1980
  // stays loaded for the next dictation.
1942
1981
  if (!text) {
1982
+ if (preview) processing = false;
1943
1983
  toast("No speech detected", "warning");
1944
1984
  retainWarmStreamController(controller);
1945
1985
  return { text: "" };
1946
1986
  }
1987
+ if (preview) {
1988
+ // v2 has a visible preview but no composer append. Finalize submits
1989
+ // once through the same home/create/cancellation fence as batch STT.
1990
+ processing = true;
1991
+ const result = await submitTurnText(client, null, toast, text, logger);
1992
+ if (!streamDisposed && myGen === streamGeneration) retainWarmStreamController(controller);
1993
+ else await controller.dispose?.();
1994
+ return { ...result, submitted: !result.error };
1995
+ }
1947
1996
  if (fin?.inserted) {
1948
1997
  logger?.log("STT", `Streaming finalized via owned range chars=${text.length}`, "debug");
1949
1998
  if (submit || autoSubmit) {
@@ -2007,6 +2056,18 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2007
2056
  // is fully stopped by cancel(); the server lease is retained warm for the
2008
2057
  // next dictation (user cancel is per-dictation, never a server fault).
2009
2058
  async function cancelStreamingDictation() {
2059
+ streamGeneration += 1;
2060
+ preview?.clear();
2061
+ if (preview && processing) cancelRecording(logger);
2062
+ if (finalizingController) {
2063
+ const finishing = finalizingController;
2064
+ finalizingController = null;
2065
+ cancelRecording(logger);
2066
+ await finishing.cancel();
2067
+ processing = false;
2068
+ clearStreamingToast();
2069
+ return true;
2070
+ }
2010
2071
  if (!streamingDictationActive || !streamController) return false;
2011
2072
  const controller = streamController;
2012
2073
  const editor = streamEditor;
@@ -2031,6 +2092,14 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2031
2092
  }
2032
2093
 
2033
2094
  api.lifecycle?.onDispose?.(() => {
2095
+ streamDisposed = true;
2096
+ streamGeneration += 1;
2097
+ preview?.clear();
2098
+ if (finalizingController) {
2099
+ const finishing = finalizingController;
2100
+ finalizingController = null;
2101
+ void finishing.dispose?.();
2102
+ }
2034
2103
  // Unload mid-recording must not orphan the sox child: cancel kills it,
2035
2104
  // bumps the pipeline generation (late transcriptions are dropped), and
2036
2105
  // clears the sticky toast. processing is not owned by cancel, reset it
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,3 +1,5 @@
1
+ import { createV2Preview } from "./v2-preview.js";
2
+
1
3
  // OpenCode v2 compatibility shim (submit-only mode).
2
4
  //
3
5
  // WHY this file exists: v2's TUI plugin `Context` renamed or replaced every
@@ -35,6 +37,36 @@ export function isV2Context(ctx) {
35
37
  );
36
38
  }
37
39
 
40
+ // Normalize a v2 `SessionMessageInfo` union member to the v1 message shape
41
+ // our lib modules read (`{id, role, time, summary?}` + v1-like `parts`).
42
+ // v2 uses `type: "user" | "assistant" | …` (no `role` field); assistant text
43
+ // lives in `content[]` (`{type: "text", text}`), user text in `text`.
44
+ // Without this, every v1 `msg.role === "assistant"` filter is empty on v2 —
45
+ // the root cause of "no assistant response to speak" and the dead
46
+ // conversation reply stream. v1 hosts never pass through here.
47
+ function normalizeV2Message(msg) {
48
+ if (!msg || typeof msg !== "object") return msg;
49
+ const type = msg?.type;
50
+ const role = type === "assistant" ? "assistant" : type === "user" ? "user" : type;
51
+ let parts = [];
52
+ try {
53
+ if (Array.isArray(msg.content)) {
54
+ parts = msg.content
55
+ .map((c, i) =>
56
+ c && typeof c.text === "string"
57
+ ? { id: `${msg.id}:${i}`, type: c.type === "text" ? "text" : c.type, text: c.text }
58
+ : null,
59
+ )
60
+ .filter(Boolean);
61
+ } else if (typeof msg.text === "string" && msg.text) {
62
+ parts = [{ id: `${msg.id}:0`, type: "text", text: msg.text }];
63
+ }
64
+ } catch {
65
+ parts = [];
66
+ }
67
+ return { ...msg, role, parts };
68
+ }
69
+
38
70
  // Wrap a v2 event envelope `{type, data}` so v1-style handlers reading
39
71
  // `event.properties` keep working. v1 handlers also read renamed fields,
40
72
  // normalized per remapped type below.
@@ -43,11 +75,20 @@ function wrapHandler(type, fn) {
43
75
  const data = event?.data ?? event?.properties ?? {};
44
76
  let properties = { ...data };
45
77
  if (type === "message.part.delta") {
46
- // v2 `session.text.delta` data: {sessionID, assistantMessageID, delta}.
47
- // v1 handler expects {sessionID, messageID, delta, field}.
78
+ // v2 `session.text.delta` data: {sessionID, assistantMessageID,
79
+ // ordinal, delta}. v1 handler expects {sessionID, messageID, partID?,
80
+ // delta, field}. `ordinal` is the content-block index, so synthesize
81
+ // the part id the normalized parts carry (`${messageID}:${ordinal}`)
82
+ // for the part-type guard; a missed lookup stays fail-open downstream.
83
+ const messageID = data.assistantMessageID ?? data.messageID;
48
84
  properties = {
49
85
  ...properties,
50
- messageID: data.assistantMessageID ?? data.messageID,
86
+ messageID,
87
+ partID:
88
+ data.partID ??
89
+ (messageID !== undefined && data.ordinal !== undefined
90
+ ? `${messageID}:${data.ordinal}`
91
+ : data.partID),
51
92
  field: "text",
52
93
  };
53
94
  }
@@ -160,8 +201,13 @@ function DialogSelect(props) {
160
201
 
161
202
  export function adaptV2Context(ctx) {
162
203
  const disposers = [];
204
+ const preview = createV2Preview(ctx);
205
+ disposers.push(() => preview.clear());
163
206
  const warned = new Set();
164
207
  const warnMessages = [];
208
+ let disposed = false;
209
+ let submission = null;
210
+ let submissionText = null;
165
211
  // Loud degradation: warn logs are collected for evidence and also sent
166
212
  // through the real logger once boot creates it (wired via onWarn below).
167
213
  let warnSink = null;
@@ -196,37 +242,94 @@ export function adaptV2Context(ctx) {
196
242
  }
197
243
  }
198
244
 
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");
245
+ function submitTextAsPrompt(text, isCurrent = () => true) {
246
+ const captured = String(text || "");
247
+ if (submission) {
248
+ return captured === submissionText
249
+ ? submission
250
+ : Promise.reject(new Error("Voice submission already in progress"));
210
251
  }
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.
252
+ const alive = () => !disposed && isCurrent();
253
+ const check = () => {
254
+ if (!alive()) throw new Error("Voice submission cancelled");
255
+ };
256
+ const task = (async () => {
257
+ check();
258
+ if (!captured.trim()) throw new Error("No transcript to submit");
259
+ const selected = ctx.ui.model?.current();
260
+ const model = selected
261
+ ? {
262
+ id: selected.modelID,
263
+ providerID: selected.providerID,
264
+ ...(selected.variant ? { variant: selected.variant } : {}),
265
+ }
266
+ : undefined;
267
+ let sessionID = currentSessionID();
268
+ try {
269
+ if (!sessionID) {
270
+ const created = await ctx.client.session.create({
271
+ ...(ctx.location ? { location: ctx.location } : {}),
272
+ ...(model ? { model } : {}),
273
+ });
274
+ check();
275
+ sessionID = created.id;
276
+ ctx.ui.router.navigate({ type: "session", sessionID });
277
+ }
278
+ check();
279
+ // Model/agent belong to session metadata in v2, not prompt input.
280
+ await ctx.client.session.prompt({ sessionID, text: captured });
281
+ check();
282
+ } catch (err) {
283
+ if (alive()) {
284
+ // A dialog retains the full transcript for recovery; a short toast
285
+ // alone would lose long recordings on create/prompt failure.
286
+ void ctx.ui.dialog.alert({
287
+ title: "Voice transcript — not submitted",
288
+ message: captured,
289
+ });
290
+ }
291
+ throw err;
292
+ }
293
+ })();
294
+ submission = task;
295
+ submissionText = captured;
296
+ void task
297
+ .finally(() => {
298
+ if (submission === task) submission = null;
299
+ })
300
+ .catch(() => {});
301
+ return task;
215
302
  }
216
303
 
217
304
  // `Object.create` (not spread) so client methods keep their `this`.
305
+ // v2 `session.message` is a `{get}` namespace, not the v1 callable, so
306
+ // the v1 call shape `client.session.message({sessionID, messageID})` is
307
+ // shimmed onto `message.get` with the result normalized (see above).
308
+ const session = Object.create(ctx.client.session, {
309
+ message: {
310
+ value: async (input) => {
311
+ const full = await ctx.client.session.message.get({
312
+ sessionID: input?.sessionID,
313
+ messageID: input?.messageID,
314
+ });
315
+ return { data: normalizeV2Message(full) };
316
+ },
317
+ },
318
+ });
218
319
  const client = Object.create(ctx.client, {
320
+ session: { value: session },
219
321
  // `client.app.log` has no v2 path — silent no-op (pre-existing `?.`
220
322
  // tolerance in createLogger already expects absence).
221
323
  app: { value: { log: async () => {} } },
222
324
  tui: {
223
325
  value: {
326
+ submitOnly: true,
224
327
  // Submit-only: append-for-edit is unimplementable on v2, so append
225
328
  // becomes submit. Accepts both v1 call shapes ({body:{text}} and
226
329
  // {text}) used by stt.js. The `submitted` marker lets the caller's
227
330
  // toast report a submit instead of an edit-step append.
228
331
  appendPrompt: async (input) => {
229
- await submitTextAsPrompt(input?.body?.text ?? input?.text ?? "");
332
+ await submitTextAsPrompt(input?.body?.text ?? input?.text ?? "", input?.isCurrent);
230
333
  return { data: null, submitted: true };
231
334
  },
232
335
  submitPrompt: async () => {},
@@ -240,9 +343,10 @@ export function adaptV2Context(ctx) {
240
343
  client,
241
344
  // Submit-only enforcement: with no renderer, `appendTranscription`
242
345
  // 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.
346
+ // which submits via `session.prompt`. Streaming display is separate:
347
+ // voicePreview uses additive slots without composer editing.
245
348
  renderer: null,
349
+ voicePreview: preview,
246
350
  ui: { toast, dialog, DialogSelect },
247
351
  event: {
248
352
  on: (type, fn) => {
@@ -279,15 +383,39 @@ export function adaptV2Context(ctx) {
279
383
  session: {
280
384
  messages: (sessionID) => {
281
385
  try {
282
- return ctx.data.session.message.list(sessionID) || [];
386
+ return (ctx.data.session.message.list(sessionID) || []).map(normalizeV2Message);
283
387
  } catch {
284
388
  return [];
285
389
  }
286
390
  },
287
391
  },
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: () => [],
392
+ // v1 `state.part(messageID)` over live v2 embedded content. Resolve
393
+ // through the current session first, then known sessions (message ids
394
+ // are unique). Never cache text: content changes while replies stream.
395
+ part: (messageID) => {
396
+ try {
397
+ const sessionIDs = [];
398
+ const current = currentSessionID();
399
+ if (current) sessionIDs.push(current);
400
+ for (const s of ctx.data.session.list() || []) {
401
+ if (s?.id && !sessionIDs.includes(s.id)) sessionIDs.push(s.id);
402
+ }
403
+ for (const sid of sessionIDs) {
404
+ let msg;
405
+ try {
406
+ msg = ctx.data.session.message.get(sid, messageID);
407
+ } catch {
408
+ msg = undefined;
409
+ }
410
+ if (msg) {
411
+ return normalizeV2Message(msg)?.parts || [];
412
+ }
413
+ }
414
+ } catch {
415
+ // Fall through to [] below.
416
+ }
417
+ return [];
418
+ },
291
419
  path: {
292
420
  // v2 `location` is a LocationRef, not a directory string.
293
421
  get directory() {
@@ -313,15 +441,16 @@ export function adaptV2Context(ctx) {
313
441
  // + layer `bindings`. Every command joins palette + slash so all
314
442
  // stay reachable; keybinds are translated below (invalid binds are
315
443
  // 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).
444
+ // v1 bind strings use a `<leader>X` token for two-key sequences; the
445
+ // v2 keymap resolves the `<leader>` token itself (leader defaults to
446
+ // ctrl+x, configurable as `keybinds.leader`), so v1 binds pass
447
+ // through UNTOUCHED — including v2-native "ctrl+r" style customs.
448
+ // (An earlier revision translated to `ctrl+x X`; that form never
449
+ // dispatches on the host and was the actual shortcut bug.)
450
+ // "none"/falsy disables: reported as bind:false so no automatic
451
+ // binding is configured, and the layer builder omits it.
321
452
  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]}`;
453
+ if (typeof bind !== "string" || bind.length === 0 || bind === "none") return false;
325
454
  return bind;
326
455
  };
327
456
  const mapped = commands.map((c) => ({
@@ -413,6 +542,7 @@ export function adaptV2Context(ctx) {
413
542
  });
414
543
 
415
544
  function cleanup() {
545
+ disposed = true;
416
546
  for (const fn of disposers.splice(0)) {
417
547
  try {
418
548
  const r = fn();
@@ -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.1",
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",