@bojackduy/opencode-voice 0.11.2 → 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/README.md +77 -19
- package/index.js +8 -1
- package/lib/chatterbox-server.js +9 -1
- package/lib/conversation-status.js +269 -0
- package/lib/conversation.js +139 -1
- package/lib/streaming-stt.js +101 -0
- package/lib/streaming-transcript.js +59 -6
- package/lib/stt-log.js +39 -0
- package/lib/stt.js +474 -27
- package/lib/tts-player.js +356 -0
- package/lib/tts-status.js +266 -0
- package/lib/tts.js +856 -173
- package/lib/v2-compat.js +41 -0
- package/lib/v2-preview.js +242 -22
- package/lib/vieneu-server.js +287 -0
- package/package.json +12 -1
package/lib/v2-compat.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { createV2Preview } from "./v2-preview.js";
|
|
2
|
+
import { createTtsStatus } from "./tts-status.js";
|
|
3
|
+
import { createConversationStatus } from "./conversation-status.js";
|
|
2
4
|
|
|
3
5
|
// OpenCode v2 compatibility shim.
|
|
4
6
|
//
|
|
@@ -206,6 +208,39 @@ export function adaptV2Context(ctx) {
|
|
|
206
208
|
const disposers = [];
|
|
207
209
|
const preview = createV2Preview(ctx);
|
|
208
210
|
disposers.push(() => preview.clear());
|
|
211
|
+
// v1 has no additive media surface: toasts stay the progress carrier
|
|
212
|
+
// there. v2 gets the persistent now-playing line (same slot pattern as
|
|
213
|
+
// the voice preview); tts.js drives it through api.ttsStatus and degrades
|
|
214
|
+
// to toasts when the surface is absent.
|
|
215
|
+
const ttsStatusReader = { current: () => null };
|
|
216
|
+
const ttsStatusActions = {};
|
|
217
|
+
const ttsStatus = createTtsStatus(ctx, () => ttsStatusReader.current(), ttsStatusActions);
|
|
218
|
+
const setTtsStatusReader = (fn) => {
|
|
219
|
+
ttsStatusReader.current = typeof fn === "function" ? fn : () => null;
|
|
220
|
+
};
|
|
221
|
+
const setTtsStatusActions = (next) => {
|
|
222
|
+
for (const key of Object.keys(ttsStatusActions)) delete ttsStatusActions[key];
|
|
223
|
+
if (next && typeof next === "object") Object.assign(ttsStatusActions, next);
|
|
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());
|
|
209
244
|
const warned = new Set();
|
|
210
245
|
const warnMessages = [];
|
|
211
246
|
let disposed = false;
|
|
@@ -371,6 +406,12 @@ export function adaptV2Context(ctx) {
|
|
|
371
406
|
// voicePreview uses additive slots without composer editing.
|
|
372
407
|
renderer: ctx.renderer ?? null,
|
|
373
408
|
voicePreview: preview,
|
|
409
|
+
ttsStatus,
|
|
410
|
+
setTtsStatusReader,
|
|
411
|
+
setTtsStatusActions,
|
|
412
|
+
conversationStatus: convStatus,
|
|
413
|
+
setConvPhase,
|
|
414
|
+
setConvActions,
|
|
374
415
|
ui: { toast, dialog, DialogSelect },
|
|
375
416
|
event: {
|
|
376
417
|
on: (type, fn) => {
|
package/lib/v2-preview.js
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
// Host-owned Solid/OpenTUI modules are loaded only when a v2 recording starts.
|
|
2
2
|
// No composer mutation: this bounded display is an additive, documented slot.
|
|
3
|
+
//
|
|
4
|
+
// Renders the live transcript and the live input waveform together: the same
|
|
5
|
+
// 16-column bar style used by the batch recording toast, sourced from the
|
|
6
|
+
// streaming capture's newest PCM tail. The waveform timer lives here so every
|
|
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.
|
|
14
|
+
import { sttLog } from "./stt-log.js";
|
|
15
|
+
|
|
16
|
+
const WAVE_CHARS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
|
|
17
|
+
const BODY_WIDTH = 60;
|
|
18
|
+
|
|
3
19
|
export function createV2Preview(
|
|
4
20
|
ctx,
|
|
5
21
|
load = async () => {
|
|
@@ -9,60 +25,264 @@ export function createV2Preview(
|
|
|
9
25
|
) {
|
|
10
26
|
let generation = 0;
|
|
11
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;
|
|
12
34
|
let partial = { stableText: "", tentativeText: "" };
|
|
35
|
+
let levels = null;
|
|
36
|
+
let applyLogCount = 0;
|
|
37
|
+
let waveProvider = null;
|
|
38
|
+
let waveTimer = null;
|
|
13
39
|
const nodes = new Set();
|
|
40
|
+
const lastTexts = { toggle: null, wave: null, body: null, autoSubmit: null, autoEnd: null };
|
|
14
41
|
let setContent = null;
|
|
15
|
-
|
|
42
|
+
|
|
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() {
|
|
16
54
|
const stable = partial.stableText || "";
|
|
17
55
|
const tentative = partial.tentativeText || "";
|
|
18
|
-
|
|
19
|
-
|
|
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]";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function redraw() {
|
|
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);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function applyLevels(next) {
|
|
91
|
+
if (!active || !Array.isArray(next) || next.length === 0) return;
|
|
92
|
+
if (applyLogCount < 3) {
|
|
93
|
+
applyLogCount += 1;
|
|
94
|
+
sttLog("preview.levels", `max=${Math.max(...next)} n=${next.length}`);
|
|
95
|
+
}
|
|
96
|
+
levels = next.map((value) => {
|
|
97
|
+
const level = Math.trunc(Number(value));
|
|
98
|
+
if (!Number.isFinite(level)) return 0;
|
|
99
|
+
return Math.max(0, Math.min(WAVE_CHARS.length - 1, level));
|
|
100
|
+
});
|
|
101
|
+
redraw();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function stopWave() {
|
|
105
|
+
if (waveTimer) {
|
|
106
|
+
clearInterval(waveTimer);
|
|
107
|
+
waveTimer = null;
|
|
108
|
+
}
|
|
109
|
+
waveProvider = null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function startWave(provider, intervalMs = 80) {
|
|
113
|
+
stopWave();
|
|
114
|
+
sttLog("preview.wave", active && typeof provider === "function" ? "start" : "rejected");
|
|
115
|
+
if (!active || typeof provider !== "function") return;
|
|
116
|
+
waveProvider = provider;
|
|
117
|
+
const timer = setInterval(
|
|
118
|
+
() => {
|
|
119
|
+
if (!active || !waveProvider) return;
|
|
120
|
+
try {
|
|
121
|
+
const next = waveProvider();
|
|
122
|
+
if (next) applyLevels(next);
|
|
123
|
+
} catch {}
|
|
124
|
+
},
|
|
125
|
+
Math.max(20, Math.floor(Number(intervalMs) || 80)),
|
|
20
126
|
);
|
|
127
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
128
|
+
waveTimer = timer;
|
|
21
129
|
}
|
|
130
|
+
|
|
22
131
|
let unclaims = [];
|
|
23
132
|
function clear() {
|
|
24
133
|
generation += 1;
|
|
25
134
|
active = false;
|
|
135
|
+
paused = false;
|
|
26
136
|
partial = { stableText: "", tentativeText: "" };
|
|
137
|
+
levels = null;
|
|
138
|
+
applyLogCount = 0;
|
|
139
|
+
stopWave();
|
|
27
140
|
nodes.clear();
|
|
141
|
+
for (const key of Object.keys(lastTexts)) lastTexts[key] = null;
|
|
28
142
|
setContent = null;
|
|
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;
|
|
29
149
|
for (const unclaim of unclaims.splice(0)) unclaim();
|
|
30
150
|
}
|
|
151
|
+
let renderErrorShown = false;
|
|
152
|
+
let renderCalls = 0;
|
|
153
|
+
function loudRenderError(err) {
|
|
154
|
+
sttLog("preview.render-error", err?.message || String(err));
|
|
155
|
+
if (renderErrorShown) return;
|
|
156
|
+
renderErrorShown = true;
|
|
157
|
+
try {
|
|
158
|
+
ctx.ui?.toast?.show?.({
|
|
159
|
+
message: `Voice live preview failed: ${err?.message || err}`,
|
|
160
|
+
variant: "error",
|
|
161
|
+
duration: 5000,
|
|
162
|
+
});
|
|
163
|
+
} catch {}
|
|
164
|
+
}
|
|
31
165
|
return {
|
|
32
166
|
async begin() {
|
|
33
167
|
clear();
|
|
34
168
|
active = true;
|
|
169
|
+
renderErrorShown = false;
|
|
35
170
|
const mine = generation;
|
|
36
|
-
|
|
171
|
+
let createElement;
|
|
172
|
+
let setProp;
|
|
173
|
+
let insertNode;
|
|
174
|
+
let onCleanup;
|
|
175
|
+
try {
|
|
176
|
+
({ createElement, setProp, insertNode, onCleanup } = await load());
|
|
177
|
+
} catch (loadErr) {
|
|
178
|
+
sttLog("preview.begin-failed", loadErr?.message || String(loadErr));
|
|
179
|
+
loudRenderError(loadErr);
|
|
180
|
+
throw loadErr;
|
|
181
|
+
}
|
|
37
182
|
if (!active || mine !== generation) return false;
|
|
183
|
+
sttLog("preview.begin-ok");
|
|
38
184
|
setContent = (node, value) => setProp(node, "content", value);
|
|
185
|
+
const themeText = ctx.theme?.text;
|
|
186
|
+
const fg =
|
|
187
|
+
typeof themeText === "string"
|
|
188
|
+
? themeText
|
|
189
|
+
: themeText?.base || themeText?.muted || "#eeeeee";
|
|
39
190
|
const render = () => {
|
|
40
191
|
if (!active || mine !== generation) return null;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
192
|
+
renderCalls += 1;
|
|
193
|
+
if (renderCalls === 1) sttLog("preview.render-first");
|
|
194
|
+
try {
|
|
195
|
+
const box = createElement("box");
|
|
196
|
+
setProp(box, "height", 2);
|
|
197
|
+
setProp(box, "flexShrink", 0);
|
|
198
|
+
setProp(box, "flexDirection", "row");
|
|
199
|
+
setProp(box, "width", "100%");
|
|
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();
|
|
237
|
+
return box;
|
|
238
|
+
} catch (renderErr) {
|
|
239
|
+
loudRenderError(renderErr);
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
54
242
|
};
|
|
55
|
-
|
|
243
|
+
// Vertical slots only: prompt.footer is a horizontal row (zero-width
|
|
244
|
+
// text) and is absent on the home screen.
|
|
245
|
+
unclaims = [
|
|
246
|
+
ctx.ui.slot({ before: "session.composer.top", render }),
|
|
247
|
+
ctx.ui.slot({ before: "home.footer", render }),
|
|
248
|
+
];
|
|
56
249
|
return true;
|
|
57
250
|
},
|
|
58
251
|
applyPartial(p) {
|
|
59
252
|
if (!active) return;
|
|
60
253
|
partial = { stableText: p?.stableText || "", tentativeText: p?.tentativeText || "" };
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
254
|
+
sttLog(
|
|
255
|
+
"preview.partial",
|
|
256
|
+
`stable=${partial.stableText.length} tentative=${partial.tentativeText.length} nodes=${nodes.size}`,
|
|
257
|
+
);
|
|
258
|
+
redraw();
|
|
259
|
+
},
|
|
260
|
+
applyLevels,
|
|
261
|
+
startWave,
|
|
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;
|
|
65
283
|
},
|
|
284
|
+
getAutoSubmit: () => autoSubmit,
|
|
285
|
+
getAutoEndOfSpeech: () => autoEndOfSpeech,
|
|
66
286
|
getTranscript: () => [partial.stableText, partial.tentativeText].filter(Boolean).join(" "),
|
|
67
287
|
finalize(text) {
|
|
68
288
|
clear();
|
|
@@ -0,0 +1,287 @@
|
|
|
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 = 30000;
|
|
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
|
+
const defaultPython = "/Users/duytrinh/.local/share/opencode-voice/vieneu-venv/bin/python";
|
|
69
|
+
const resolvedPython = python || (fs.existsSync(defaultPython) ? defaultPython : "python3");
|
|
70
|
+
|
|
71
|
+
const spawnFn = deps.spawn ?? spawn;
|
|
72
|
+
const fetchFn = deps.fetch ?? fetch;
|
|
73
|
+
|
|
74
|
+
let proc = null;
|
|
75
|
+
let ready = false;
|
|
76
|
+
let ownerPid = null;
|
|
77
|
+
let boundPort = options.port ?? DEFAULT_PORT;
|
|
78
|
+
let lastError = null;
|
|
79
|
+
|
|
80
|
+
const baseUrlFor = (p) => `http://${host}:${p}`;
|
|
81
|
+
|
|
82
|
+
async function portResponds(p) {
|
|
83
|
+
try {
|
|
84
|
+
const resp = await fetchFn(`${baseUrlFor(p)}/health`, {
|
|
85
|
+
signal: AbortSignal.timeout(1500),
|
|
86
|
+
});
|
|
87
|
+
return !!resp;
|
|
88
|
+
} catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function probeOnce() {
|
|
94
|
+
try {
|
|
95
|
+
const resp = await fetchFn(`${baseUrlFor(boundPort)}/health`, {
|
|
96
|
+
signal: AbortSignal.timeout(2000),
|
|
97
|
+
});
|
|
98
|
+
if (!resp) return "down";
|
|
99
|
+
if (resp.ok) return "ready";
|
|
100
|
+
return "down";
|
|
101
|
+
} catch {
|
|
102
|
+
return "down";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function pollReady(deadline) {
|
|
107
|
+
while (Date.now() < deadline) {
|
|
108
|
+
if (!proc) return { failed: "vieneu sidecar exited before becoming ready" };
|
|
109
|
+
const state = await probeOnce();
|
|
110
|
+
if (state === "ready") return state;
|
|
111
|
+
await new Promise((r) => setTimeout(r, readyPollMs));
|
|
112
|
+
}
|
|
113
|
+
return "timeout";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function startOnPort(port) {
|
|
117
|
+
boundPort = port;
|
|
118
|
+
if (await portResponds(port)) {
|
|
119
|
+
return {
|
|
120
|
+
ok: false,
|
|
121
|
+
code: "PORT_IN_USE",
|
|
122
|
+
advancable: true,
|
|
123
|
+
message: `Port ${port} already answers`,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const bin = resolvedPython;
|
|
127
|
+
try {
|
|
128
|
+
proc = spawnFn(bin, ["-m", "apps.openai_speech"], {
|
|
129
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
130
|
+
});
|
|
131
|
+
} catch (err) {
|
|
132
|
+
proc = null;
|
|
133
|
+
return {
|
|
134
|
+
ok: false,
|
|
135
|
+
code: "SPAWN_FAILED",
|
|
136
|
+
advancable: false,
|
|
137
|
+
message: `Failed to spawn ${bin}: ${err.message}`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let stderr = "";
|
|
142
|
+
const owned = proc;
|
|
143
|
+
proc.stderr?.on("data", (chunk) => {
|
|
144
|
+
stderr += chunk.toString();
|
|
145
|
+
// Log stderr in real-time for debugging
|
|
146
|
+
logger?.log?.("VOICE", `vieneu stderr: ${chunk.toString().trim()}`, "debug");
|
|
147
|
+
});
|
|
148
|
+
proc.on("exit", (code) => {
|
|
149
|
+
if (proc !== owned) return;
|
|
150
|
+
lastError = {
|
|
151
|
+
code: "EXITED",
|
|
152
|
+
message: `vieneu sidecar exited (code=${code}) stderr=${stderr.trim().slice(-200)}`,
|
|
153
|
+
};
|
|
154
|
+
proc = null;
|
|
155
|
+
ready = false;
|
|
156
|
+
ownerPid = null;
|
|
157
|
+
logger?.log?.(
|
|
158
|
+
"VOICE",
|
|
159
|
+
`vieneu sidecar exited (code=${code}) stderr=${stderr.trim().slice(-200)}`,
|
|
160
|
+
"warn",
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
proc.on("error", (err) => {
|
|
164
|
+
if (proc !== owned) return;
|
|
165
|
+
logger?.log?.("VOICE", `vieneu sidecar error: ${err.message}`, "warn");
|
|
166
|
+
proc = null;
|
|
167
|
+
ready = false;
|
|
168
|
+
ownerPid = null;
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const outcome = await pollReady(Date.now() + readyTimeoutMs);
|
|
172
|
+
if (outcome !== "ready") {
|
|
173
|
+
const tail = stderr.trim().slice(-300);
|
|
174
|
+
const message =
|
|
175
|
+
outcome === "timeout"
|
|
176
|
+
? `vieneu sidecar did not become ready in time (${readyTimeoutMs}ms) stderr=${tail}`
|
|
177
|
+
: `vieneu sidecar unavailable: ${outcome.failed ?? "unknown"}`;
|
|
178
|
+
const code = outcome === "timeout" ? "START_TIMEOUT" : "MODEL_LOAD_FAILED";
|
|
179
|
+
stop();
|
|
180
|
+
return { ok: false, code, advancable: false, message };
|
|
181
|
+
}
|
|
182
|
+
ownerPid = proc?.pid ?? null;
|
|
183
|
+
return { ok: true };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function start() {
|
|
187
|
+
if (proc && ready) return true;
|
|
188
|
+
const scanMax = Math.max(1, Math.floor(portScanMax) || 1);
|
|
189
|
+
for (let i = 0; i < scanMax; i++) {
|
|
190
|
+
const port = boundPort + i;
|
|
191
|
+
const r = await startOnPort(port);
|
|
192
|
+
if (r.ok) {
|
|
193
|
+
ready = true;
|
|
194
|
+
lastError = null;
|
|
195
|
+
logger?.log?.(
|
|
196
|
+
"VOICE",
|
|
197
|
+
`vieneu sidecar ready at ${baseUrlFor(boundPort)} voice=${voice} pid=${ownerPid}`,
|
|
198
|
+
"debug",
|
|
199
|
+
);
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
if (!r.advancable) {
|
|
203
|
+
lastError = { code: r.code, message: r.message };
|
|
204
|
+
logger?.log?.("VOICE", r.message, "warn");
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
logger?.log?.("VOICE", r.message, "warn");
|
|
208
|
+
}
|
|
209
|
+
lastError = {
|
|
210
|
+
code: "PORT_RANGE_EXHAUSTED",
|
|
211
|
+
message: `No free port in ${boundPort}..${boundPort + scanMax - 1}`,
|
|
212
|
+
};
|
|
213
|
+
logger?.log?.("VOICE", lastError.message, "warn");
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function synthesize(text, opts = {}) {
|
|
218
|
+
if (!ready) {
|
|
219
|
+
return {
|
|
220
|
+
ok: false,
|
|
221
|
+
code: lastError?.code || "NOT_READY",
|
|
222
|
+
message: lastError?.message || "vieneu sidecar not ready",
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
try {
|
|
226
|
+
const resp = await fetchFn(`${baseUrlFor(boundPort)}/v1/audio/speech`, {
|
|
227
|
+
method: "POST",
|
|
228
|
+
headers: { "Content-Type": "application/json" },
|
|
229
|
+
body: JSON.stringify({
|
|
230
|
+
model: "vieneu-v3-turbo",
|
|
231
|
+
input: text,
|
|
232
|
+
voice: opts.voice || voice,
|
|
233
|
+
response_format: "wav",
|
|
234
|
+
}),
|
|
235
|
+
signal: AbortSignal.timeout(synthesizeTimeoutMs),
|
|
236
|
+
});
|
|
237
|
+
if (!resp?.ok) {
|
|
238
|
+
let message = `vieneu sidecar responded ${resp?.status}`;
|
|
239
|
+
try {
|
|
240
|
+
const data = await resp.json();
|
|
241
|
+
if (data?.error) message = data.error;
|
|
242
|
+
} catch {}
|
|
243
|
+
return { ok: false, code: "BAD_STATUS", message };
|
|
244
|
+
}
|
|
245
|
+
const audio = Buffer.from(await resp.arrayBuffer());
|
|
246
|
+
if (audio.length === 0) {
|
|
247
|
+
return { ok: false, code: "EMPTY_AUDIO", message: "vieneu returned no audio" };
|
|
248
|
+
}
|
|
249
|
+
return { ok: true, wav: audio };
|
|
250
|
+
} catch (err) {
|
|
251
|
+
const timedOut = err?.name === "TimeoutError" || err?.name === "AbortError";
|
|
252
|
+
return {
|
|
253
|
+
ok: false,
|
|
254
|
+
code: timedOut ? "SYNTH_TIMEOUT" : "REQUEST_FAILED",
|
|
255
|
+
message: `vieneu synthesis failed: ${err.message}`,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function stop() {
|
|
261
|
+
ready = false;
|
|
262
|
+
ownerPid = null;
|
|
263
|
+
if (proc) {
|
|
264
|
+
const owned = proc;
|
|
265
|
+
proc = null;
|
|
266
|
+
try {
|
|
267
|
+
owned.kill("SIGTERM");
|
|
268
|
+
} catch {}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function isRunning() {
|
|
273
|
+
return ready && proc !== null;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function getLastError() {
|
|
277
|
+
return lastError;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function getPort() {
|
|
281
|
+
return boundPort;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return { start, stop, synthesize, isRunning, getLastError, getPort };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
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
|
+
"version": "0.11.4",
|
|
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",
|