@bojackduy/opencode-voice 0.11.3 → 0.11.5

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.
@@ -337,40 +393,85 @@ export async function getTurnAssistantText(client, api) {
337
393
  if (route.name !== "session") return null;
338
394
 
339
395
  const sessionID = route.params.sessionID;
340
- const stateMessages = api.state.session.messages(sessionID);
341
- if (!stateMessages || stateMessages.length === 0) return null;
342
-
343
- const assistantIDs = [];
344
- for (let i = stateMessages.length - 1; i >= 0; i--) {
345
- if (stateMessages[i].role === "user") break;
346
- if (stateMessages[i].role === "assistant") {
347
- assistantIDs.unshift(stateMessages[i].id);
396
+
397
+ // The message cache can trail the execution-completed signal by a beat, so a
398
+ // single read here can still show the previous turn and report "no reply".
399
+ // Retry briefly instead of dropping the answer.
400
+ let stateMessages = [];
401
+ let assistantIDs = [];
402
+ for (let attempt = 0; attempt < 4; attempt += 1) {
403
+ stateMessages = api.state.session.messages(sessionID) || [];
404
+ assistantIDs = [];
405
+ for (let i = stateMessages.length - 1; i >= 0; i -= 1) {
406
+ if (stateMessages[i].role === "user") break;
407
+ if (stateMessages[i].role === "assistant") {
408
+ assistantIDs.unshift(stateMessages[i].id);
409
+ }
348
410
  }
411
+ if (assistantIDs.length > 0) break;
412
+ if (attempt < 3) await new Promise((resolve) => setTimeout(resolve, 150));
349
413
  }
414
+ sttLog(
415
+ "tts.conv",
416
+ `getTurnAssistantText session=${sessionID} n=${stateMessages.length} assistants=${assistantIDs.length}`,
417
+ );
350
418
  if (assistantIDs.length === 0) return null;
351
419
 
352
- const allText = [];
353
- for (const msgID of assistantIDs) {
354
- try {
355
- const fullMsg = await client.session
356
- .message({ sessionID, messageID: msgID }, { throwOnError: true })
357
- .then((r) => r.data);
420
+ // The answer is the last assistant message that finished WITHOUT asking for
421
+ // more tool calls. Earlier ones are mid-turn commentary ("let me check...")
422
+ // and reading them aloud reads half a thought. Fall back to the whole turn
423
+ // when no message carries a terminal finish, or when the terminal one holds
424
+ // no prose (a tool-only finish).
425
+ const terminalIDs = assistantIDs.filter((id) => {
426
+ const finish = stateMessages.find((m) => m?.id === id)?.finish;
427
+ return !!finish && finish !== "tool-calls" && finish !== "tool_calls";
428
+ });
429
+ const answerID = terminalIDs.length > 0 ? terminalIDs[terminalIDs.length - 1] : null;
358
430
 
431
+ const textOf = async (msgID) => {
432
+ let fullMsg = stateMessages.find((m) => m?.id === msgID);
433
+ if (!fullMsg || !(fullMsg.parts && fullMsg.parts.length)) {
434
+ try {
435
+ fullMsg = await client.session
436
+ .message({ sessionID, messageID: msgID }, { throwOnError: true })
437
+ .then((r) => r.data);
438
+ } catch {
439
+ fullMsg = null;
440
+ }
441
+ }
442
+ try {
359
443
  const textParts = (fullMsg?.parts || []).filter((p) => p.type === "text");
360
- const text = textParts
444
+ sttLog(
445
+ "tts.conv",
446
+ `msg=${msgID} parts=${fullMsg?.parts?.length ?? 0} textParts=${textParts.length}`,
447
+ );
448
+ return textParts
361
449
  .map((p) => p.text || "")
362
450
  .join("\n\n")
363
451
  .trim();
364
- if (text) allText.push(text);
365
452
  } catch {
366
- // Skip messages that fail to fetch
453
+ return "";
454
+ }
455
+ };
456
+
457
+ let spokenIDs = answerID ? [answerID] : assistantIDs;
458
+ let allText = [];
459
+ for (const msgID of spokenIDs) {
460
+ const text = await textOf(msgID);
461
+ if (text) allText.push(text);
462
+ }
463
+ if (allText.length === 0 && answerID) {
464
+ spokenIDs = assistantIDs;
465
+ for (const msgID of spokenIDs) {
466
+ const text = await textOf(msgID);
467
+ if (text) allText.push(text);
367
468
  }
368
469
  }
369
470
 
370
471
  if (allText.length === 0) return null;
371
472
 
372
473
  return {
373
- lastMessageID: assistantIDs[assistantIDs.length - 1],
474
+ lastMessageID: spokenIDs[spokenIDs.length - 1],
374
475
  text: allText.join("\n\n"),
375
476
  };
376
477
  }
@@ -378,6 +479,17 @@ export async function getTurnAssistantText(client, api) {
378
479
  // ---- Public API for TUI plugin ----
379
480
 
380
481
  export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {}) {
482
+ // Seed engine from opts if the host delivered it (same pattern as sttMode).
483
+ try {
484
+ if (opts?.ttsEngine && kv.get("tts.engine") == null) {
485
+ kv.set("tts.engine", opts.ttsEngine);
486
+ }
487
+ } catch {}
488
+ logger?.log?.(
489
+ "TTS",
490
+ `Engine=${kv.get("tts.engine", "piper")} opts.engine=${opts?.ttsEngine ?? "none"}`,
491
+ "debug",
492
+ );
381
493
  if (deps.isConversationActive || deps.onConversationStopKey) {
382
494
  __setConversationTtsHooks({
383
495
  isActive: deps.isConversationActive,
@@ -732,9 +844,13 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
732
844
 
733
845
  function getChatterboxClient() {
734
846
  if (!chatterboxClient) {
847
+ const defaultChatterboxPython =
848
+ "/Users/duytrinh/.local/share/opencode-voice/chatterbox-venv/bin/python";
735
849
  chatterboxClient = createChatterbox({
736
850
  variant: opts?.ttsChatterboxVariant,
737
- python: opts?.ttsChatterboxPython,
851
+ python:
852
+ opts?.ttsChatterboxPython ||
853
+ (fs.existsSync(defaultChatterboxPython) ? defaultChatterboxPython : undefined),
738
854
  voiceRef: opts?.ttsChatterboxVoiceRef,
739
855
  logger,
740
856
  });
@@ -772,6 +888,73 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
772
888
  return chatterboxStarting;
773
889
  }
774
890
 
891
+ // ---- Optional VieNeu-TTS engine (lib/vieneu-server.js) ----
892
+ // Bilingual Vietnamese-English with code-switching. Unlike Chatterbox
893
+ // (which cannot speak vi) and Piper (which needs separate voices), VieNeu
894
+ // handles mixed-language text in a single pass. Runs as a Python sidecar
895
+ // serving an OpenAI-compatible API at /v1/audio/speech.
896
+ const createVieneu = deps.createVieneuClient ?? createVieneuClient;
897
+ let vieneuClient = null;
898
+ let vieneuStarting = null;
899
+ let vieneuToastShown = false;
900
+
901
+ function vieneuWarn(message) {
902
+ logger?.log?.("TTS", message, "warn");
903
+ }
904
+
905
+ function noteVieneuFailure(message) {
906
+ logger?.log?.("TTS", message, "warn");
907
+ if (vieneuToastShown) return;
908
+ vieneuToastShown = true;
909
+ toast(`${message} - using Piper`);
910
+ }
911
+
912
+ function noteVieneuOk() {
913
+ vieneuToastShown = false;
914
+ }
915
+
916
+ function getVieneuClient() {
917
+ if (!vieneuClient) {
918
+ const defaultVieneuPython =
919
+ "/Users/duytrinh/.local/share/opencode-voice/vieneu-venv/bin/python";
920
+ vieneuClient = createVieneu({
921
+ python:
922
+ opts?.ttsVieneuPython ||
923
+ (fs.existsSync(defaultVieneuPython) ? defaultVieneuPython : undefined),
924
+ voice: kv.get("tts.vieneuVoice", opts?.ttsVieneuVoice || "Mai Anh"),
925
+ logger,
926
+ });
927
+ }
928
+ return vieneuClient;
929
+ }
930
+
931
+ async function ensureVieneuReady() {
932
+ if (vieneuClient?.isRunning()) return true;
933
+ if (!vieneuStarting) {
934
+ vieneuStarting = getVieneuClient()
935
+ .start()
936
+ .then((ok) => {
937
+ if (ok) {
938
+ noteVieneuOk();
939
+ return true;
940
+ }
941
+ const err = vieneuClient.getLastError?.();
942
+ noteVieneuFailure(
943
+ `VieNeu-TTS unavailable (${err?.code || "START_FAILED"}: ${err?.message || "unknown"})`,
944
+ );
945
+ return false;
946
+ })
947
+ .catch((err) => {
948
+ noteVieneuFailure(`VieNeu-TTS unavailable (${err.message})`);
949
+ return false;
950
+ })
951
+ .finally(() => {
952
+ vieneuStarting = null;
953
+ });
954
+ }
955
+ return vieneuStarting;
956
+ }
957
+
775
958
  function speak(text) {
776
959
  if (!text) return Promise.resolve();
777
960
  const line = text.replace(/\n/g, " ").trim();
@@ -790,7 +973,10 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
790
973
  utterReset();
791
974
  if (takingOver) statusClear();
792
975
 
793
- const groups = groupSentencesByLang(line);
976
+ // VieNeu-TTS handles bilingual code-switching in one pass, so the
977
+ // whole utterance goes as a single group (no per-sentence routing).
978
+ const engine = kv.get("tts.engine", opts?.ttsEngine);
979
+ const groups = engine === "vieneu" ? [{ lang: "vi", text: line }] : groupSentencesByLang(line);
794
980
  if (groups.length === 0) return Promise.resolve();
795
981
  sttLog(
796
982
  "tts.speak",
@@ -870,7 +1056,8 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
870
1056
  // null when stopped, when no voice exists, or when nothing is speakable -
871
1057
  // callers skip null groups instead of going silent.
872
1058
  async function synthGroupAsset(group, resolveGroupVoice, myGen) {
873
- const route = resolveTtsEngine(opts, group.lang, chatterboxWarn);
1059
+ const effectiveOpts = { ...opts, ttsEngine: kv.get("tts.engine", opts?.ttsEngine) };
1060
+ const route = resolveTtsEngine(effectiveOpts, group.lang, chatterboxWarn);
874
1061
  sttLog("tts.synth", `lang=${group.lang} engine=${route.engine} chars=${group.text.length}`);
875
1062
  if (route.engine === "chatterbox") {
876
1063
  const ready = await ensureChatterboxReady();
@@ -880,8 +1067,6 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
880
1067
  try {
881
1068
  result = await getChatterboxClient().synthesize(group.text, group.lang);
882
1069
  } 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
1070
  result = { ok: false, code: "REQUEST_FAILED", message: err.message };
886
1071
  }
887
1072
  if (myGen !== speechGen) return null;
@@ -892,7 +1077,7 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
892
1077
  noteChatterboxOk();
893
1078
  logger?.log?.(
894
1079
  "TTS",
895
- `Speak via Chatterbox chars=${group.text.length} lang=${group.lang} variant=${route.variant} wavBytes=${result.wav.length}`,
1080
+ `Speak via Chatterbox chars=${group.text.length} lang=${group.lang} wavBytes=${result.wav.length}`,
896
1081
  "debug",
897
1082
  );
898
1083
  return {
@@ -909,6 +1094,34 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
909
1094
  // Fall through to Piper for this group.
910
1095
  }
911
1096
  }
1097
+ if (route.engine === "vieneu") {
1098
+ const ready = await ensureVieneuReady();
1099
+ if (myGen !== speechGen) return null;
1100
+ if (ready) {
1101
+ let result;
1102
+ try {
1103
+ result = await getVieneuClient().synthesize(group.text);
1104
+ } catch (err) {
1105
+ result = { ok: false, code: "REQUEST_FAILED", message: err.message };
1106
+ }
1107
+ if (myGen !== speechGen) return null;
1108
+ if (result?.ok) {
1109
+ const totalSec = measureWavDurationSec(result.wav);
1110
+ sttLog("tts.synth", `vieneu ok=${!!result?.ok} wavBytes=${result?.wav?.length ?? 0}`);
1111
+ if (totalSec !== null && totalSec > 0) {
1112
+ noteVieneuOk();
1113
+ return {
1114
+ totalSec,
1115
+ loadArg: { wav: result.wav, rate: currentRate() },
1116
+ };
1117
+ }
1118
+ noteVieneuFailure(`VieNeu-TTS synthesis failed (BAD_WAV: unreadable audio)`);
1119
+ } else {
1120
+ noteVieneuFailure(`VieNeu-TTS synthesis failed (${result?.code}: ${result?.message})`);
1121
+ }
1122
+ // Fall through to Piper for this group.
1123
+ }
1124
+ }
912
1125
  // Piper path (default, vi, and Chatterbox fallback): buffer the whole
913
1126
  // group, measure the exact total from real PCM frames, then play.
914
1127
  // Test seam: resolveVoice replaces the PATH/model-filesystem probe so
@@ -1118,8 +1331,10 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
1118
1331
  }
1119
1332
 
1120
1333
  async function speakAssistantTurn() {
1334
+ sttLog("tts.conv", "speakAssistantTurn start");
1121
1335
  const tFetch = Date.now();
1122
1336
  const result = await getTurnAssistantText(client, api);
1337
+ sttLog("tts.conv", `speakAssistantTurn result=${!!result} text=${result?.text?.length ?? 0}`);
1123
1338
  if (!result || !result.text) {
1124
1339
  logger?.log?.("TTS", "Conversation: no assistant text to speak", "warn");
1125
1340
  return { spoken: false };
@@ -1138,6 +1353,7 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
1138
1353
  return { spoken: false };
1139
1354
  }
1140
1355
  if (!announceNormalize("Conversation", llmResult)) {
1356
+ sttLog("tts.conv", "announceNormalize failed");
1141
1357
  clearProcessingToast();
1142
1358
  return { spoken: false };
1143
1359
  }
@@ -1262,10 +1478,40 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
1262
1478
  } catch {}
1263
1479
  chatterboxClient?.stop();
1264
1480
  chatterboxClient = null;
1481
+ vieneuClient?.stop();
1482
+ vieneuClient = null;
1265
1483
  });
1266
1484
 
1267
1485
  // ---- Commands ----
1268
1486
 
1487
+ const VIENEU_VOICES = [
1488
+ "Adam bựa",
1489
+ "Trúc Ly",
1490
+ "Thiện Minh",
1491
+ "Mai Anh",
1492
+ "Hải Đăng",
1493
+ "Thùy Dung",
1494
+ "Thiền Tâm Đức",
1495
+ "Ngọc Huyền",
1496
+ "Quang Sơn",
1497
+ "Ngọc Trân",
1498
+ "Minh Đức",
1499
+ "Phạm Tuyên",
1500
+ "Thái Sơn",
1501
+ "Xuân Vĩnh",
1502
+ "Thanh Bình",
1503
+ "Ngọc Linh",
1504
+ "Đoan Trang",
1505
+ "Thục Đoan",
1506
+ "Minh Triết",
1507
+ "Mỹ Duyên",
1508
+ "Quỳnh Anh",
1509
+ "Đức Trí",
1510
+ "Kim Thanh",
1511
+ "Adam",
1512
+ "Quốc Tuấn",
1513
+ ];
1514
+
1269
1515
  const commands = [
1270
1516
  {
1271
1517
  title: "TTS: speak last response",
@@ -1422,6 +1668,63 @@ export function registerTTS(api, kv, complete, prompts, opts, logger, deps = {})
1422
1668
  );
1423
1669
  },
1424
1670
  },
1671
+ {
1672
+ title: "TTS: select engine",
1673
+ value: "tts.engine",
1674
+ category: "opencode-voice",
1675
+ description: "Choose TTS engine (piper, chatterbox, vieneu) - persisted",
1676
+ slash: { name: "tts-engine" },
1677
+ onSelect() {
1678
+ const current = kv.get("tts.engine", opts?.ttsEngine || "piper");
1679
+ api.ui.dialog.replace(() =>
1680
+ api.ui.DialogSelect({
1681
+ title: "Select TTS engine",
1682
+ current,
1683
+ options: [
1684
+ { title: "Piper (default, local, vi+en separate voices)", value: "piper" },
1685
+ { title: "Chatterbox (English, voice cloning)", value: "chatterbox" },
1686
+ { title: "VieNeu-TTS (bilingual vi+en code-switching)", value: "vieneu" },
1687
+ ].map((o) => ({
1688
+ title: o.title,
1689
+ value: o.value,
1690
+ onSelect() {
1691
+ kv.set("tts.engine", o.value);
1692
+ sttLog("tts.engine.set", o.value);
1693
+ toast(`TTS engine: ${o.value}`);
1694
+ api.ui.dialog.clear();
1695
+ },
1696
+ })),
1697
+ }),
1698
+ );
1699
+ },
1700
+ },
1701
+ {
1702
+ title: "TTS: select VieNeu voice",
1703
+ value: "tts.vieneu-voice",
1704
+ category: "opencode-voice",
1705
+ description: "Choose VieNeu-TTS voice (bilingual vi+en) - persisted",
1706
+ slash: { name: "tts-vieneu-voice" },
1707
+ onSelect() {
1708
+ const current = kv.get("tts.vieneuVoice", opts?.ttsVieneuVoice || "Mai Anh");
1709
+ api.ui.dialog.replace(() =>
1710
+ api.ui.DialogSelect({
1711
+ title: "Select VieNeu voice",
1712
+ current,
1713
+ options: VIENEU_VOICES.map((name) => ({
1714
+ title: name,
1715
+ value: name,
1716
+ onSelect() {
1717
+ kv.set("tts.vieneuVoice", name);
1718
+ // Reset client so next speak uses new voice.
1719
+ vieneuClient = null;
1720
+ toast(`VieNeu voice: ${name}`);
1721
+ api.ui.dialog.clear();
1722
+ },
1723
+ })),
1724
+ }),
1725
+ );
1726
+ },
1727
+ },
1425
1728
  ];
1426
1729
 
1427
1730
  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) => {