@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/README.md +71 -15
- 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 +81 -0
- package/lib/streaming-transcript.js +59 -6
- package/lib/stt.js +253 -8
- package/lib/tts.js +205 -13
- package/lib/v2-compat.js +22 -0
- package/lib/v2-preview.js +123 -23
- package/lib/vieneu-server.js +287 -0
- package/package.json +12 -1
|
@@ -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",
|