@bojackduy/opencode-voice 0.11.3 → 0.11.4

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/lib/tts.js CHANGED
@@ -7,6 +7,7 @@ import { spawn } from "node:child_process";
7
7
  import { getSessionTitle } from "./session.js";
8
8
  import { clearProcessingToast, showProcessingToast, updateProcessingToast } from "./stt.js";
9
9
  import { createChatterboxClient, resolveTtsEngine } from "./chatterbox-server.js";
10
+ import { createVieneuClient } from "./vieneu-server.js";
10
11
  import { createTtsPlayer, measurePcmDurationSec, measureWavDurationSec } from "./tts-player.js";
11
12
  import { nextRate as nextTtsRate } from "./tts-status.js";
12
13
  import { sttLog } from "./stt-log.js";
@@ -32,6 +33,13 @@ const PIPER_CHANNELS = 1;
32
33
  // local cleanup and the LLM narrator summarize instead: what the table is
33
34
  // about, then the few rows that matter. Pure functions, tested in tts.test.js.
34
35
 
36
+ // Structural-only guard: diagrams, tables, fences. Safe on clean text.
37
+ const MERMAID_FENCE_RE = /```mermaid[^\S\n]*\r?\n?([\s\S]*?)```/gi;
38
+ const MERMAID_DECL_RE =
39
+ /^(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|gantt|pie|mindmap|journey|gitGraph)\b/i;
40
+ const MERMAID_LINE_RE = /-->|---|==>|-\.->|==>|\[|\]|\(|\)/;
41
+ const MERMAID_DECL_MAX_LEN = 32;
42
+
35
43
  const TABLE_ROW_CAP = 3;
36
44
  const TABLE_TEXT_CAP = 320;
37
45
  const MERMAID_LABEL_CAP = 6;
@@ -104,15 +112,56 @@ function splitIdentifier(token) {
104
112
  .replace(/[_-]+/g, " ");
105
113
  }
106
114
 
107
- export function localSpeechCleanup(text) {
115
+ function summarizeUnfencedMermaid(lines, start) {
116
+ if (lines[start].trim().length > MERMAID_DECL_MAX_LEN) return null;
117
+ const body = [];
118
+ let i = start + 1;
119
+ while (i < lines.length && lines[i].trim() === "") i++;
120
+ for (; i < lines.length; i++) {
121
+ const line = lines[i];
122
+ if (line.trim() === "") break;
123
+ if (!MERMAID_LINE_RE.test(line)) return null;
124
+ body.push(line);
125
+ }
126
+ if (body.length === 0) return null;
127
+ return {
128
+ text: summarizeMermaidForSpeech([lines[start], ...body].join("\n")),
129
+ end: i,
130
+ };
131
+ }
132
+
133
+ export function sanitizeSpokenText(text) {
108
134
  if (!text) return "";
109
- let out = text;
110
- // Diagrams first: a mermaid fence would otherwise collapse into the
111
- // generic "code snippet" below and lose the flow.
135
+ let out = String(text);
136
+ out = out.replace(MERMAID_FENCE_RE, (_, code) => ` ${summarizeMermaidForSpeech(code)} `);
137
+ {
138
+ const lines = out.split("\n");
139
+ const kept = [];
140
+ for (let i = 0; i < lines.length; i++) {
141
+ if (MERMAID_DECL_RE.test(lines[i].trim())) {
142
+ const found = summarizeUnfencedMermaid(lines, i);
143
+ if (found) {
144
+ kept.push(found.text);
145
+ i = found.end - 1;
146
+ continue;
147
+ }
148
+ }
149
+ kept.push(lines[i]);
150
+ }
151
+ out = kept.join("\n");
152
+ }
112
153
  out = out.replace(
113
- /```mermaid\n([\s\S]*?)```/gi,
114
- (_, code) => ` ${summarizeMermaidForSpeech(code)} `,
154
+ /((?:^[ \t]*\|.*\|\s*$\n?){2,})/gm,
155
+ (block) => ` ${summarizeMarkdownTable(block)} `,
115
156
  );
157
+ out = out.replace(/```[\s\S]*?```/g, " code snippet ");
158
+ return out;
159
+ }
160
+
161
+ export function localSpeechCleanup(text) {
162
+ if (!text) return "";
163
+ let out = sanitizeSpokenText(text);
164
+ // (Diagrams, tables and generic fences were already handled by sanitizeSpokenText above.)
116
165
  // Tables next: runs of pipe lines become a short spoken summary.
117
166
  out = out.replace(
118
167
  /((?:^[ \t]*\|.*\|\s*$\n?){2,})/gm,
@@ -156,7 +205,7 @@ export function localSpeechCleanup(text) {
156
205
  // Pure so it is testable without a TUI, a model, or piper.
157
206
 
158
207
  export function resolveSpeechText(rawText, llmResult) {
159
- if (llmResult?.text) return { text: llmResult.text };
208
+ if (llmResult?.text) return { text: sanitizeSpokenText(llmResult.text) };
160
209
  return { text: localSpeechCleanup(rawText), error: llmResult?.error, fellBack: true };
161
210
  }
162
211
 
@@ -190,7 +239,11 @@ async function normalizeWithinBound(promise) {
190
239
 
191
240
  export function splitSpokenSentences(text) {
192
241
  const flat = (text || "").replace(/\s+/g, " ");
193
- const parts = flat.split(/(?<=[.!?…])\s+/);
242
+ // Only split where a new sentence plausibly starts (uppercase letter,
243
+ // quote, or bracket). This keeps version numbers ("v1.2.3"), decimals,
244
+ // and abbreviations ("e.g. this") inside one span so a fragment can't
245
+ // be voice-routed on its own.
246
+ const parts = flat.split(/(?<=[.!?…])\s+(?=[\p{Lu}"“'‘(\[])/u);
194
247
  if (parts.length === 0) return { sentences: [], rest: "" };
195
248
  const lastComplete = /[.!?…]\s*$/.test(flat);
196
249
  if (lastComplete) return { sentences: parts.filter(Boolean), rest: "" };
@@ -301,6 +354,9 @@ STRUCTURED CONTENT RULES (apply in every mode):
301
354
  CODE-SWITCHING (Vietnamese matrix language with English technical terms):
302
355
  - Keep technical terms, commands, identifiers, and proper nouns in English.
303
356
  Never translate them into Vietnamese.
357
+ - When a sentence mixes Vietnamese and English, prefer keeping the English
358
+ term at the end or beginning of its own clause so it can be spoken with
359
+ the right voice. Do not embed English terms mid-Vietnamese-phrase.
304
360
  - Expand an acronym on first use only when it aids understanding, then use
305
361
  the term as-is. Never spell acronyms letter by letter unprompted.
306
362
  - Do not narrate markdown table pipes or diagram arrows - see above.
@@ -378,6 +434,17 @@ export async function getTurnAssistantText(client, api) {
378
434
  // ---- Public API for TUI plugin ----
379
435
 
380
436
  export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {}) {
437
+ // Seed engine from opts if the host delivered it (same pattern as sttMode).
438
+ try {
439
+ if (opts?.ttsEngine && kv.get("tts.engine") == null) {
440
+ kv.set("tts.engine", opts.ttsEngine);
441
+ }
442
+ } catch {}
443
+ logger?.log?.(
444
+ "TTS",
445
+ `Engine=${kv.get("tts.engine", "piper")} opts.engine=${opts?.ttsEngine ?? "none"}`,
446
+ "debug",
447
+ );
381
448
  if (deps.isConversationActive || deps.onConversationStopKey) {
382
449
  __setConversationTtsHooks({
383
450
  isActive: deps.isConversationActive,
@@ -772,6 +839,69 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
772
839
  return chatterboxStarting;
773
840
  }
774
841
 
842
+ // ---- Optional VieNeu-TTS engine (lib/vieneu-server.js) ----
843
+ // Bilingual Vietnamese-English with code-switching. Unlike Chatterbox
844
+ // (which cannot speak vi) and Piper (which needs separate voices), VieNeu
845
+ // handles mixed-language text in a single pass. Runs as a Python sidecar
846
+ // serving an OpenAI-compatible API at /v1/audio/speech.
847
+ const createVieneu = deps.createVieneuClient ?? createVieneuClient;
848
+ let vieneuClient = null;
849
+ let vieneuStarting = null;
850
+ let vieneuToastShown = false;
851
+
852
+ function vieneuWarn(message) {
853
+ logger?.log?.("TTS", message, "warn");
854
+ }
855
+
856
+ function noteVieneuFailure(message) {
857
+ logger?.log?.("TTS", message, "warn");
858
+ if (vieneuToastShown) return;
859
+ vieneuToastShown = true;
860
+ toast(`${message} - using Piper`);
861
+ }
862
+
863
+ function noteVieneuOk() {
864
+ vieneuToastShown = false;
865
+ }
866
+
867
+ function getVieneuClient() {
868
+ if (!vieneuClient) {
869
+ vieneuClient = createVieneu({
870
+ python: opts?.ttsVieneuPython,
871
+ voice: opts?.ttsVieneuVoice,
872
+ logger,
873
+ });
874
+ }
875
+ return vieneuClient;
876
+ }
877
+
878
+ async function ensureVieneuReady() {
879
+ if (vieneuClient?.isRunning()) return true;
880
+ if (!vieneuStarting) {
881
+ vieneuStarting = getVieneuClient()
882
+ .start()
883
+ .then((ok) => {
884
+ if (ok) {
885
+ noteVieneuOk();
886
+ return true;
887
+ }
888
+ const err = vieneuClient.getLastError?.();
889
+ noteVieneuFailure(
890
+ `VieNeu-TTS unavailable (${err?.code || "START_FAILED"}: ${err?.message || "unknown"})`,
891
+ );
892
+ return false;
893
+ })
894
+ .catch((err) => {
895
+ noteVieneuFailure(`VieNeu-TTS unavailable (${err.message})`);
896
+ return false;
897
+ })
898
+ .finally(() => {
899
+ vieneuStarting = null;
900
+ });
901
+ }
902
+ return vieneuStarting;
903
+ }
904
+
775
905
  function speak(text) {
776
906
  if (!text) return Promise.resolve();
777
907
  const line = text.replace(/\n/g, " ").trim();
@@ -790,7 +920,10 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
790
920
  utterReset();
791
921
  if (takingOver) statusClear();
792
922
 
793
- const groups = groupSentencesByLang(line);
923
+ // VieNeu-TTS handles bilingual code-switching in one pass, so the
924
+ // whole utterance goes as a single group (no per-sentence routing).
925
+ const engine = kv.get("tts.engine", opts?.ttsEngine);
926
+ const groups = engine === "vieneu" ? [{ lang: "vi", text: line }] : groupSentencesByLang(line);
794
927
  if (groups.length === 0) return Promise.resolve();
795
928
  sttLog(
796
929
  "tts.speak",
@@ -870,7 +1003,8 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
870
1003
  // null when stopped, when no voice exists, or when nothing is speakable -
871
1004
  // callers skip null groups instead of going silent.
872
1005
  async function synthGroupAsset(group, resolveGroupVoice, myGen) {
873
- const route = resolveTtsEngine(opts, group.lang, chatterboxWarn);
1006
+ const effectiveOpts = { ...opts, ttsEngine: kv.get("tts.engine", opts?.ttsEngine) };
1007
+ const route = resolveTtsEngine(effectiveOpts, group.lang, chatterboxWarn);
874
1008
  sttLog("tts.synth", `lang=${group.lang} engine=${route.engine} chars=${group.text.length}`);
875
1009
  if (route.engine === "chatterbox") {
876
1010
  const ready = await ensureChatterboxReady();
@@ -880,8 +1014,6 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
880
1014
  try {
881
1015
  result = await getChatterboxClient().synthesize(group.text, group.lang);
882
1016
  } catch (err) {
883
- // The client already returns typed errors; this only guards a
884
- // thrown client bug so speech still falls back instead of rejecting.
885
1017
  result = { ok: false, code: "REQUEST_FAILED", message: err.message };
886
1018
  }
887
1019
  if (myGen !== speechGen) return null;
@@ -892,7 +1024,7 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
892
1024
  noteChatterboxOk();
893
1025
  logger?.log?.(
894
1026
  "TTS",
895
- `Speak via Chatterbox chars=${group.text.length} lang=${group.lang} variant=${route.variant} wavBytes=${result.wav.length}`,
1027
+ `Speak via Chatterbox chars=${group.text.length} lang=${group.lang} wavBytes=${result.wav.length}`,
896
1028
  "debug",
897
1029
  );
898
1030
  return {
@@ -909,6 +1041,34 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
909
1041
  // Fall through to Piper for this group.
910
1042
  }
911
1043
  }
1044
+ if (route.engine === "vieneu") {
1045
+ const ready = await ensureVieneuReady();
1046
+ if (myGen !== speechGen) return null;
1047
+ if (ready) {
1048
+ let result;
1049
+ try {
1050
+ result = await getVieneuClient().synthesize(group.text);
1051
+ } catch (err) {
1052
+ result = { ok: false, code: "REQUEST_FAILED", message: err.message };
1053
+ }
1054
+ if (myGen !== speechGen) return null;
1055
+ if (result?.ok) {
1056
+ const totalSec = measureWavDurationSec(result.wav);
1057
+ sttLog("tts.synth", `vieneu ok=${!!result?.ok} wavBytes=${result?.wav?.length ?? 0}`);
1058
+ if (totalSec !== null && totalSec > 0) {
1059
+ noteVieneuOk();
1060
+ return {
1061
+ totalSec,
1062
+ loadArg: { wav: result.wav, rate: currentRate() },
1063
+ };
1064
+ }
1065
+ noteVieneuFailure(`VieNeu-TTS synthesis failed (BAD_WAV: unreadable audio)`);
1066
+ } else {
1067
+ noteVieneuFailure(`VieNeu-TTS synthesis failed (${result?.code}: ${result?.message})`);
1068
+ }
1069
+ // Fall through to Piper for this group.
1070
+ }
1071
+ }
912
1072
  // Piper path (default, vi, and Chatterbox fallback): buffer the whole
913
1073
  // group, measure the exact total from real PCM frames, then play.
914
1074
  // Test seam: resolveVoice replaces the PATH/model-filesystem probe so
@@ -1262,6 +1422,8 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
1262
1422
  } catch {}
1263
1423
  chatterboxClient?.stop();
1264
1424
  chatterboxClient = null;
1425
+ vieneuClient?.stop();
1426
+ vieneuClient = null;
1265
1427
  });
1266
1428
 
1267
1429
  // ---- Commands ----
@@ -1422,6 +1584,36 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
1422
1584
  );
1423
1585
  },
1424
1586
  },
1587
+ {
1588
+ title: "TTS: select engine",
1589
+ value: "tts.engine",
1590
+ category: "opencode-voice",
1591
+ description: "Choose TTS engine (piper, chatterbox, vieneu) - persisted",
1592
+ slash: { name: "tts-engine" },
1593
+ onSelect() {
1594
+ const current = kv.get("tts.engine", opts?.ttsEngine || "piper");
1595
+ api.ui.dialog.replace(() =>
1596
+ api.ui.DialogSelect({
1597
+ title: "Select TTS engine",
1598
+ current,
1599
+ options: [
1600
+ { title: "Piper (default, local, vi+en separate voices)", value: "piper" },
1601
+ { title: "Chatterbox (English, voice cloning)", value: "chatterbox" },
1602
+ { title: "VieNeu-TTS (bilingual vi+en code-switching)", value: "vieneu" },
1603
+ ].map((o) => ({
1604
+ title: o.title,
1605
+ value: o.value,
1606
+ onSelect() {
1607
+ kv.set("tts.engine", o.value);
1608
+ sttLog("tts.engine.set", o.value);
1609
+ toast(`TTS engine: ${o.value}`);
1610
+ api.ui.dialog.clear();
1611
+ },
1612
+ })),
1613
+ }),
1614
+ );
1615
+ },
1616
+ },
1425
1617
  ];
1426
1618
 
1427
1619
  return { commands, controller };
package/lib/v2-compat.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createV2Preview } from "./v2-preview.js";
2
2
  import { createTtsStatus } from "./tts-status.js";
3
+ import { createConversationStatus } from "./conversation-status.js";
3
4
 
4
5
  // OpenCode v2 compatibility shim.
5
6
  //
@@ -222,6 +223,24 @@ export function adaptV2Context(ctx) {
222
223
  if (next && typeof next === "object") Object.assign(ttsStatusActions, next);
223
224
  };
224
225
  disposers.push(() => ttsStatus.clear());
226
+
227
+ // Conversation status: phase-aware slot line with phase-appropriate
228
+ // clickable buttons. Driven by api.conversationPhase / api.setConversationPhase.
229
+ const convStatusReader = { current: () => "idle" };
230
+ const convStatusActions = {};
231
+ const convStatus = createConversationStatus(
232
+ ctx,
233
+ () => convStatusReader.current(),
234
+ convStatusActions,
235
+ );
236
+ const setConvPhase = (phase) => {
237
+ convStatusReader.current = typeof phase === "string" ? () => phase : convStatusReader.current;
238
+ };
239
+ const setConvActions = (next) => {
240
+ for (const key of Object.keys(convStatusActions)) delete convStatusActions[key];
241
+ if (next && typeof next === "object") Object.assign(convStatusActions, next);
242
+ };
243
+ disposers.push(() => convStatus.clear());
225
244
  const warned = new Set();
226
245
  const warnMessages = [];
227
246
  let disposed = false;
@@ -390,6 +409,9 @@ export function adaptV2Context(ctx) {
390
409
  ttsStatus,
391
410
  setTtsStatusReader,
392
411
  setTtsStatusActions,
412
+ conversationStatus: convStatus,
413
+ setConvPhase,
414
+ setConvActions,
393
415
  ui: { toast, dialog, DialogSelect },
394
416
  event: {
395
417
  on: (type, fn) => {
package/lib/v2-preview.js CHANGED
@@ -5,9 +5,16 @@
5
5
  // 16-column bar style used by the batch recording toast, sourced from the
6
6
  // streaming capture's newest PCM tail. The waveform timer lives here so every
7
7
  // stop path (`clear()`) cancels it in one place.
8
+ //
9
+ // Layout (single row, fixed width): [toggle] [wave] [body...] [auto-submit] [end-of-speech]
10
+ // - toggle/wave/auto-submit/end-of-speech are fixed-width segments that never
11
+ // scroll, so they stay visible no matter how long the transcript grows.
12
+ // - body is the only scrolling segment: it shows the LAST N chars of the
13
+ // transcript (auto-scroll) so the user always sees what was just said.
8
14
  import { sttLog } from "./stt-log.js";
9
15
 
10
16
  const WAVE_CHARS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
17
+ const BODY_WIDTH = 60;
11
18
 
12
19
  export function createV2Preview(
13
20
  ctx,
@@ -18,31 +25,65 @@ export function createV2Preview(
18
25
  ) {
19
26
  let generation = 0;
20
27
  let active = false;
28
+ let paused = false;
29
+ const onToggleRef = { current: null };
30
+ const onAutoSubmitRef = { current: null };
31
+ const onAutoEndRef = { current: null };
32
+ let autoSubmit = false;
33
+ let autoEndOfSpeech = false;
21
34
  let partial = { stableText: "", tentativeText: "" };
22
35
  let levels = null;
23
36
  let applyLogCount = 0;
24
37
  let waveProvider = null;
25
38
  let waveTimer = null;
26
39
  const nodes = new Set();
40
+ const lastTexts = { toggle: null, wave: null, body: null, autoSubmit: null, autoEnd: null };
27
41
  let setContent = null;
28
42
 
29
- function display() {
43
+ function toggleText() {
44
+ return paused ? "[▶]" : "[⏸]";
45
+ }
46
+
47
+ function waveText() {
48
+ return levels
49
+ ? `${levels.map((level) => WAVE_CHARS[level] ?? WAVE_CHARS[0]).join("")} `
50
+ : "▁".repeat(16) + " ";
51
+ }
52
+
53
+ function bodyText() {
30
54
  const stable = partial.stableText || "";
31
55
  const tentative = partial.tentativeText || "";
32
- const body =
33
- `Voice live · ${stable ? `${stable} ` : ""}${tentative ? `[${tentative}]` : stable ? "" : "Listening…"}`.slice(
34
- -1580,
35
- );
36
- const wave = levels
37
- ? `${levels.map((level) => WAVE_CHARS[level] ?? WAVE_CHARS[0]).join("")} `
38
- : "";
39
- return `${wave}${body}`;
56
+ const full = `Voice live · ${stable ? `${stable} ` : ""}${tentative ? `[${tentative}]` : stable ? "" : "Listening…"}`;
57
+ // Auto-scroll: show the tail so the newest speech is always visible.
58
+ if (full.length <= BODY_WIDTH) return full;
59
+ return "…" + full.slice(-(BODY_WIDTH - 1));
60
+ }
61
+
62
+ function autoSubmitText() {
63
+ return autoSubmit ? "[auto-submit:ON]" : "[auto-submit:off]";
64
+ }
65
+
66
+ function autoEndText() {
67
+ return autoEndOfSpeech ? "[auto-end:ON]" : "[auto-end:off]";
40
68
  }
41
69
 
42
70
  function redraw() {
43
- for (const node of nodes) {
44
- if (node.isDestroyed) nodes.delete(node);
45
- else setContent?.(node, display());
71
+ const texts = {
72
+ toggle: toggleText(),
73
+ wave: waveText(),
74
+ body: bodyText(),
75
+ autoSubmit: autoSubmitText(),
76
+ autoEnd: autoEndText(),
77
+ };
78
+ for (const entry of nodes) {
79
+ if (entry.node.isDestroyed) {
80
+ nodes.delete(entry);
81
+ continue;
82
+ }
83
+ const text = texts[entry.key];
84
+ if (text === undefined || lastTexts[entry.key] === text) continue;
85
+ lastTexts[entry.key] = text;
86
+ setContent?.(entry.node, text);
46
87
  }
47
88
  }
48
89
 
@@ -91,13 +132,20 @@ export function createV2Preview(
91
132
  function clear() {
92
133
  generation += 1;
93
134
  active = false;
135
+ paused = false;
94
136
  partial = { stableText: "", tentativeText: "" };
95
137
  levels = null;
96
138
  applyLogCount = 0;
97
139
  stopWave();
98
140
  nodes.clear();
141
+ for (const key of Object.keys(lastTexts)) lastTexts[key] = null;
99
142
  setContent = null;
100
- renderCalls = 0;
143
+ // Action handlers persist across begin/clear cycles: they are wired by
144
+ // stt.js after begin() resolves, but a re-begin (new generation) must
145
+ // not wipe handlers set before the previous clear. Only the toggle
146
+ // glyph state resets.
147
+ autoSubmit = false;
148
+ autoEndOfSpeech = false;
101
149
  for (const unclaim of unclaims.splice(0)) unclaim();
102
150
  }
103
151
  let renderErrorShown = false;
@@ -145,18 +193,47 @@ export function createV2Preview(
145
193
  if (renderCalls === 1) sttLog("preview.render-first");
146
194
  try {
147
195
  const box = createElement("box");
148
- setProp(box, "height", 3);
196
+ setProp(box, "height", 2);
149
197
  setProp(box, "flexShrink", 0);
198
+ setProp(box, "flexDirection", "row");
150
199
  setProp(box, "width", "100%");
151
- const node = createElement("text");
152
- setProp(node, "maxHeight", 5);
153
- setProp(node, "minHeight", 1);
154
- setProp(node, "width", "100%");
155
- setProp(node, "fg", fg);
156
- setProp(node, "content", display());
157
- nodes.add(node);
158
- insertNode(box, node);
159
- onCleanup?.(() => nodes.delete(node));
200
+ setProp(box, "gap", 1);
201
+ const segs = [
202
+ { key: "toggle", text: toggleText(), action: "toggle" },
203
+ { key: "wave", text: waveText(), action: null },
204
+ { key: "body", text: bodyText(), action: null },
205
+ { key: "autoSubmit", text: autoSubmitText(), action: "autoSubmit" },
206
+ { key: "autoEnd", text: autoEndText(), action: "autoEnd" },
207
+ ];
208
+ const mountEntries = [];
209
+ for (const seg of segs) {
210
+ const node = createElement("text");
211
+ setProp(node, "maxHeight", 2);
212
+ setProp(node, "minHeight", 1);
213
+ setProp(node, "fg", fg);
214
+ setProp(node, "content", seg.text);
215
+ lastTexts[seg.key] = seg.text;
216
+ if (seg.action) {
217
+ const action = seg.action;
218
+ try {
219
+ setProp(node, "onMouseUp", () => {
220
+ try {
221
+ if (action === "toggle") onToggleRef.current?.();
222
+ else if (action === "autoSubmit") onAutoSubmitRef.current?.();
223
+ else if (action === "autoEnd") onAutoEndRef.current?.();
224
+ } catch {}
225
+ });
226
+ } catch {}
227
+ }
228
+ const entry = { key: seg.key, node };
229
+ nodes.add(entry);
230
+ mountEntries.push(entry);
231
+ insertNode(box, node);
232
+ }
233
+ onCleanup?.(() => {
234
+ for (const entry of mountEntries) nodes.delete(entry);
235
+ });
236
+ redraw();
160
237
  return box;
161
238
  } catch (renderErr) {
162
239
  loudRenderError(renderErr);
@@ -183,6 +260,29 @@ export function createV2Preview(
183
260
  applyLevels,
184
261
  startWave,
185
262
  stopWave,
263
+ setPaused(p) {
264
+ paused = !!p;
265
+ if (active) redraw();
266
+ },
267
+ setToggleAction(fn) {
268
+ onToggleRef.current = typeof fn === "function" ? fn : null;
269
+ },
270
+ setAutoSubmit(v) {
271
+ autoSubmit = !!v;
272
+ if (active) redraw();
273
+ },
274
+ setAutoEndOfSpeech(v) {
275
+ autoEndOfSpeech = !!v;
276
+ if (active) redraw();
277
+ },
278
+ setAutoSubmitToggleAction(fn) {
279
+ onAutoSubmitRef.current = typeof fn === "function" ? fn : null;
280
+ },
281
+ setAutoEndToggleAction(fn) {
282
+ onAutoEndRef.current = typeof fn === "function" ? fn : null;
283
+ },
284
+ getAutoSubmit: () => autoSubmit,
285
+ getAutoEndOfSpeech: () => autoEndOfSpeech,
186
286
  getTranscript: () => [partial.stableText, partial.tentativeText].filter(Boolean).join(" "),
187
287
  finalize(text) {
188
288
  clear();