@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/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();
@@ -0,0 +1,305 @@
1
+ // Managed VieNeu-TTS sidecar (see https://github.com/pnnbao97/VieNeu-TTS).
2
+ //
3
+ // VieNeu-TTS is an open-source Vietnamese TTS with built-in English-Vietnamese
4
+ // bilingual code-switching. Unlike Chatterbox (which cannot speak Vietnamese)
5
+ // and Piper (which needs separate vi/en voices and can't mix within an
6
+ // utterance), VieNeu handles mixed-language text in a single pass.
7
+ //
8
+ // Like Chatterbox, it ships as a Python sidecar serving an OpenAI-compatible
9
+ // API at POST /v1/audio/speech (returns WAV/PCM). The plugin spawns it once
10
+ // and keeps it loaded for the session.
11
+ //
12
+ // Install: `uv pip install vieneu` (or pip). First run downloads model weights.
13
+ // CPU-only (ONNX Runtime) works out of the box; GPU needs CUDA torch.
14
+
15
+ import { spawn } from "node:child_process";
16
+ import fs from "node:fs";
17
+
18
+ const DEFAULT_HOST = "127.0.0.1";
19
+ const DEFAULT_PORT = 8000;
20
+ const DEFAULT_PORT_SCAN_MAX = 4;
21
+ const READY_TIMEOUT_MS = 300000;
22
+ const READY_POLL_MS = 1000;
23
+ const SYNTHESIZE_TIMEOUT_MS = 120000;
24
+
25
+ // VieNeu-TTS supports Vietnamese + English code-switching natively.
26
+ export const VIENEU_LANGUAGES = new Set(["vi", "en"]);
27
+
28
+ export function vieneuSupportsLang(lang) {
29
+ return VIENEU_LANGUAGES.has(lang);
30
+ }
31
+
32
+ // Routing: VieNeu handles both vi and en (code-switching), so it is always
33
+ // eligible when configured. Returns the engine + reason like resolveTtsEngine.
34
+ export function resolveVieneuEngine(opts, lang, warn) {
35
+ const configured = opts?.ttsEngine;
36
+ if (configured !== "vieneu") return { engine: "piper", reason: "not-configured" };
37
+ if (vieneuSupportsLang(lang)) return { engine: "vieneu", reason: "ok" };
38
+ warn?.(`VieNeu-TTS cannot speak "${lang}" - using Piper for this utterance`);
39
+ return { engine: "piper", reason: "unsupported-language" };
40
+ }
41
+
42
+ export function buildVieneuArgs({ host, port }) {
43
+ return [
44
+ "-m",
45
+ "apps.openai_speech",
46
+ "--host",
47
+ host || DEFAULT_HOST,
48
+ "--port",
49
+ String(port || DEFAULT_PORT),
50
+ ];
51
+ }
52
+
53
+ export function createVieneuClient(options = {}) {
54
+ const {
55
+ host = DEFAULT_HOST,
56
+ python,
57
+ voice = "Mai Anh",
58
+ logger,
59
+ readyTimeoutMs = READY_TIMEOUT_MS,
60
+ readyPollMs = READY_POLL_MS,
61
+ synthesizeTimeoutMs = SYNTHESIZE_TIMEOUT_MS,
62
+ portScanMax = DEFAULT_PORT_SCAN_MAX,
63
+ deps = {},
64
+ } = options;
65
+
66
+ // Default to the venv python if it exists (host truncates plugin options,
67
+ // so ttsVieneuPython may not be delivered).
68
+
69
+ const spawnFn = deps.spawn ?? spawn;
70
+ const fetchFn = deps.fetch ?? fetch;
71
+
72
+ let proc = null;
73
+ let ready = false;
74
+ // True when `ready` points at a sidecar this client did not spawn (it was
75
+ // already serving the port). Such a server is never killed by stop().
76
+ let adopted = false;
77
+ let ownerPid = null;
78
+ let boundPort = options.port ?? DEFAULT_PORT;
79
+ let lastError = null;
80
+
81
+ const baseUrlFor = (p) => `http://${host}:${p}`;
82
+
83
+ async function portResponds(p) {
84
+ try {
85
+ const resp = await fetchFn(`${baseUrlFor(p)}/health`, {
86
+ signal: AbortSignal.timeout(1500),
87
+ });
88
+ return !!resp;
89
+ } catch {
90
+ return false;
91
+ }
92
+ }
93
+
94
+ async function probeOnce() {
95
+ try {
96
+ const resp = await fetchFn(`${baseUrlFor(boundPort)}/health`, {
97
+ signal: AbortSignal.timeout(2000),
98
+ });
99
+ if (!resp) return "down";
100
+ if (resp.ok) return "ready";
101
+ return "down";
102
+ } catch {
103
+ return "down";
104
+ }
105
+ }
106
+
107
+ async function pollReady(deadline) {
108
+ while (Date.now() < deadline) {
109
+ if (!proc) return { failed: "vieneu sidecar exited before becoming ready" };
110
+ const state = await probeOnce();
111
+ if (state === "ready") return state;
112
+ await new Promise((r) => setTimeout(r, readyPollMs));
113
+ }
114
+ return "timeout";
115
+ }
116
+
117
+ async function startOnPort(port) {
118
+ boundPort = port;
119
+ if (await portResponds(port)) {
120
+ // A healthy sidecar already serves this port (earlier plugin session,
121
+ // manual run, another opencode instance). Adopt it. The app binds its
122
+ // own default port regardless of flags, so a second child cannot start
123
+ // beside it: it exits and a working sidecar becomes a Piper fallback.
124
+ if ((await probeOnce()) === "ready") {
125
+ adopted = true;
126
+ ready = true;
127
+ ownerPid = null;
128
+ return { ok: true, adopted: true };
129
+ }
130
+ return {
131
+ ok: false,
132
+ code: "PORT_IN_USE",
133
+ advancable: true,
134
+ message: `Port ${port} already answers`,
135
+ };
136
+ }
137
+ adopted = false;
138
+ const bin = python || "python3";
139
+ try {
140
+ proc = spawnFn(bin, ["-m", "apps.openai_speech"], {
141
+ stdio: ["ignore", "ignore", "pipe"],
142
+ });
143
+ } catch (err) {
144
+ proc = null;
145
+ return {
146
+ ok: false,
147
+ code: "SPAWN_FAILED",
148
+ advancable: false,
149
+ message: `Failed to spawn ${bin}: ${err.message}`,
150
+ };
151
+ }
152
+
153
+ let stderr = "";
154
+ const owned = proc;
155
+ proc.stderr?.on("data", (chunk) => {
156
+ stderr += chunk.toString();
157
+ // Log stderr in real-time for debugging
158
+ logger?.log?.("VOICE", `vieneu stderr: ${chunk.toString().trim()}`, "debug");
159
+ });
160
+ proc.on("exit", (code) => {
161
+ if (proc !== owned) return;
162
+ lastError = {
163
+ code: "EXITED",
164
+ message: `vieneu sidecar exited (code=${code}) stderr=${stderr.trim().slice(-200)}`,
165
+ };
166
+ proc = null;
167
+ ready = false;
168
+ ownerPid = null;
169
+ logger?.log?.(
170
+ "VOICE",
171
+ `vieneu sidecar exited (code=${code}) stderr=${stderr.trim().slice(-200)}`,
172
+ "warn",
173
+ );
174
+ });
175
+ proc.on("error", (err) => {
176
+ if (proc !== owned) return;
177
+ logger?.log?.("VOICE", `vieneu sidecar error: ${err.message}`, "warn");
178
+ proc = null;
179
+ ready = false;
180
+ ownerPid = null;
181
+ });
182
+
183
+ const outcome = await pollReady(Date.now() + readyTimeoutMs);
184
+ if (outcome !== "ready") {
185
+ const tail = stderr.trim().slice(-300);
186
+ const message =
187
+ outcome === "timeout"
188
+ ? `vieneu sidecar did not become ready in time (${readyTimeoutMs}ms) stderr=${tail}`
189
+ : `vieneu sidecar unavailable: ${outcome.failed ?? "unknown"}`;
190
+ const code = outcome === "timeout" ? "START_TIMEOUT" : "MODEL_LOAD_FAILED";
191
+ stop();
192
+ return { ok: false, code, advancable: false, message };
193
+ }
194
+ ownerPid = proc?.pid ?? null;
195
+ return { ok: true };
196
+ }
197
+
198
+ async function start() {
199
+ if (ready) return true;
200
+ const scanMax = Math.max(1, Math.floor(portScanMax) || 1);
201
+ for (let i = 0; i < scanMax; i++) {
202
+ const port = boundPort + i;
203
+ const r = await startOnPort(port);
204
+ if (r.ok) {
205
+ ready = true;
206
+ lastError = null;
207
+ logger?.log?.(
208
+ "VOICE",
209
+ `vieneu sidecar ready at ${baseUrlFor(boundPort)} voice=${voice} pid=${ownerPid}`,
210
+ "debug",
211
+ );
212
+ return true;
213
+ }
214
+ if (!r.advancable) {
215
+ lastError = { code: r.code, message: r.message };
216
+ logger?.log?.("VOICE", r.message, "warn");
217
+ return false;
218
+ }
219
+ logger?.log?.("VOICE", r.message, "warn");
220
+ }
221
+ lastError = {
222
+ code: "PORT_RANGE_EXHAUSTED",
223
+ message: `No free port in ${boundPort}..${boundPort + scanMax - 1}`,
224
+ };
225
+ logger?.log?.("VOICE", lastError.message, "warn");
226
+ return false;
227
+ }
228
+
229
+ async function synthesize(text, opts = {}) {
230
+ if (!ready) {
231
+ return {
232
+ ok: false,
233
+ code: lastError?.code || "NOT_READY",
234
+ message: lastError?.message || "vieneu sidecar not ready",
235
+ };
236
+ }
237
+ try {
238
+ const resp = await fetchFn(`${baseUrlFor(boundPort)}/v1/audio/speech`, {
239
+ method: "POST",
240
+ headers: { "Content-Type": "application/json" },
241
+ body: JSON.stringify({
242
+ model: "vieneu-v3-turbo",
243
+ input: text,
244
+ voice: opts.voice || voice,
245
+ response_format: "wav",
246
+ }),
247
+ signal: AbortSignal.timeout(synthesizeTimeoutMs),
248
+ });
249
+ if (!resp?.ok) {
250
+ let message = `vieneu sidecar responded ${resp?.status}`;
251
+ try {
252
+ const data = await resp.json();
253
+ if (data?.error) message = data.error;
254
+ } catch {}
255
+ return { ok: false, code: "BAD_STATUS", message };
256
+ }
257
+ const audio = Buffer.from(await resp.arrayBuffer());
258
+ if (audio.length === 0) {
259
+ return { ok: false, code: "EMPTY_AUDIO", message: "vieneu returned no audio" };
260
+ }
261
+ return { ok: true, wav: audio };
262
+ } catch (err) {
263
+ const timedOut = err?.name === "TimeoutError" || err?.name === "AbortError";
264
+ if (adopted && !timedOut) {
265
+ // The sidecar we adopted is gone; let the next turn start one.
266
+ ready = false;
267
+ adopted = false;
268
+ }
269
+ return {
270
+ ok: false,
271
+ code: timedOut ? "SYNTH_TIMEOUT" : "REQUEST_FAILED",
272
+ message: `vieneu synthesis failed: ${err.message}`,
273
+ };
274
+ }
275
+ }
276
+
277
+ function stop() {
278
+ ready = false;
279
+ adopted = false;
280
+ ownerPid = null;
281
+ if (proc) {
282
+ const owned = proc;
283
+ proc = null;
284
+ try {
285
+ owned.kill("SIGTERM");
286
+ } catch {}
287
+ }
288
+ }
289
+
290
+ function isRunning() {
291
+ return ready && (proc !== null || adopted);
292
+ }
293
+
294
+ function getLastError() {
295
+ return lastError;
296
+ }
297
+
298
+ function getPort() {
299
+ return boundPort;
300
+ }
301
+
302
+ return { start, stop, synthesize, isRunning, getLastError, getPort };
303
+ }
304
+
305
+ export { DEFAULT_HOST, DEFAULT_PORT, DEFAULT_PORT_SCAN_MAX };
package/package.json CHANGED
@@ -1,16 +1,27 @@
1
1
  {
2
2
  "name": "@bojackduy/opencode-voice",
3
- "version": "0.11.3",
3
+ "version": "0.11.5",
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
+ "accessibility",
7
+ "bilingual",
8
+ "cli",
9
+ "conversation",
10
+ "dictation",
11
+ "local-first",
6
12
  "opencode",
13
+ "opencode-plugin",
7
14
  "piper",
8
15
  "plugin",
9
16
  "speech-to-text",
10
17
  "stt",
18
+ "terminal",
11
19
  "text-to-speech",
12
20
  "tts",
21
+ "vietnamese",
13
22
  "voice",
23
+ "voice-input",
24
+ "voice-output",
14
25
  "whisper"
15
26
  ],
16
27
  "license": "AGPL-3.0-or-later",
@@ -46,10 +57,13 @@
46
57
  "prepack": "npm run check && npm test",
47
58
  "release:patch": "npm run check && npm test && npm version patch -m \"chore: release %s\" && git push fork master && git push fork $(git describe --tags --abbrev=0)",
48
59
  "release:minor": "npm run check && npm test && npm version minor -m \"chore: release %s\" && git push fork master && git push fork $(git describe --tags --abbrev=0)",
49
- "release:major": "npm run check && npm test && npm version major -m \"chore: release %s\" && git push fork master && git push fork $(git describe --tags --abbrev=0)"
60
+ "release:major": "npm run check && npm test && npm version major -m \"chore: release %s\" && git push fork master && git push fork $(git describe --tags --abbrev=0)",
61
+ "deploy": "wrangler deploy",
62
+ "preview": "wrangler dev"
50
63
  },
51
64
  "devDependencies": {
52
65
  "oxfmt": "0.50.0",
53
- "oxlint": "1.65.0"
66
+ "oxlint": "1.65.0",
67
+ "wrangler": "^4.156.0"
54
68
  }
55
69
  }