@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/README.md +71 -15
- package/index.js +8 -1
- package/lib/chatterbox-server.js +9 -1
- package/lib/conversation-status.js +274 -0
- package/lib/conversation.js +392 -116
- package/lib/streaming-stt.js +81 -0
- package/lib/streaming-transcript.js +62 -6
- package/lib/stt.js +356 -34
- package/lib/tts-player.js +3 -1
- package/lib/tts.js +335 -32
- package/lib/v2-compat.js +22 -0
- package/lib/v2-preview.js +123 -23
- package/lib/vieneu-server.js +305 -0
- package/package.json +17 -3
package/lib/streaming-stt.js
CHANGED
|
@@ -288,6 +288,30 @@ export function createSoxRollingCapture({
|
|
|
288
288
|
return spoolChain;
|
|
289
289
|
}
|
|
290
290
|
|
|
291
|
+
// Pause: SIGSTOP freezes the mic so paused spans never enter the ring
|
|
292
|
+
// (and the first post-resume snapshot re-reads pre-pause audio the
|
|
293
|
+
// overlap tracker already absorbed). Best-effort: missing/dead proc
|
|
294
|
+
// reports false, never throws.
|
|
295
|
+
function pauseCapture() {
|
|
296
|
+
if (!proc) return false;
|
|
297
|
+
try {
|
|
298
|
+
proc.kill("SIGSTOP");
|
|
299
|
+
return true;
|
|
300
|
+
} catch {
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function resumeCapture() {
|
|
306
|
+
if (!proc) return false;
|
|
307
|
+
try {
|
|
308
|
+
proc.kill("SIGCONT");
|
|
309
|
+
return true;
|
|
310
|
+
} catch {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
291
315
|
// Honest coverage of retained audio. The spool holds a contiguous prefix
|
|
292
316
|
// from t=0; the RAM ring holds the trailing window. With no overflow the
|
|
293
317
|
// session is fully retained; with overflow the middle is explicitly listed
|
|
@@ -487,6 +511,9 @@ export function createSoxRollingCapture({
|
|
|
487
511
|
// coverage descriptor that says exactly what is retained. Bounded: SIGINT,
|
|
488
512
|
// then SIGKILL after soxExitTimeoutMs. Consumes the spool file (cleanup).
|
|
489
513
|
async function stopFinal({ soxExitTimeoutMs = STREAMING_DEFAULTS.soxExitTimeoutMs } = {}) {
|
|
514
|
+
// A SIGSTOPped sox ignores SIGINT: always continue first so teardown
|
|
515
|
+
// can never hang on a paused mic.
|
|
516
|
+
resumeCapture();
|
|
490
517
|
if (proc) {
|
|
491
518
|
try {
|
|
492
519
|
proc.kill("SIGINT");
|
|
@@ -529,6 +556,9 @@ export function createSoxRollingCapture({
|
|
|
529
556
|
}
|
|
530
557
|
|
|
531
558
|
async function cancel() {
|
|
559
|
+
// SIGKILL terminates even a stopped process, but continuing first lets
|
|
560
|
+
// stdio flush and the close handler release the handle cleanly.
|
|
561
|
+
resumeCapture();
|
|
532
562
|
if (proc) {
|
|
533
563
|
const owned = proc;
|
|
534
564
|
proc = null;
|
|
@@ -562,6 +592,8 @@ export function createSoxRollingCapture({
|
|
|
562
592
|
start,
|
|
563
593
|
snapshot,
|
|
564
594
|
peekTail,
|
|
595
|
+
pause: pauseCapture,
|
|
596
|
+
resume: resumeCapture,
|
|
565
597
|
stopFinal,
|
|
566
598
|
cancel,
|
|
567
599
|
dispose,
|
|
@@ -690,6 +722,11 @@ export function createStreamingController({
|
|
|
690
722
|
let coalesced = false;
|
|
691
723
|
let finalizing = false;
|
|
692
724
|
let generation = 0;
|
|
725
|
+
// Dictation pause: ticks are skipped (no inference cost) and the mic is
|
|
726
|
+
// SIGSTOPped so paused spans never enter the ring. The tracker needs no
|
|
727
|
+
// changes: the first post-resume snapshot re-reads pre-pause audio it
|
|
728
|
+
// already absorbed, so overlap-join dedupes it.
|
|
729
|
+
let paused = false;
|
|
693
730
|
let lastError = null;
|
|
694
731
|
let fallbackAudio = null;
|
|
695
732
|
let fallbackCoverage = null;
|
|
@@ -836,6 +873,12 @@ export function createStreamingController({
|
|
|
836
873
|
|
|
837
874
|
async function tick() {
|
|
838
875
|
if (status !== "streaming" || finalizing) return;
|
|
876
|
+
if (paused) {
|
|
877
|
+
// Mic frozen, nothing new to transcribe: keep the cadence timer alive
|
|
878
|
+
// for an instant resume, with zero inference cost while paused.
|
|
879
|
+
schedule(cadenceMs);
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
839
882
|
if (inFlight) {
|
|
840
883
|
// Exactly one in-flight request: coalesce to the newest audio instead
|
|
841
884
|
// of queueing a stale snapshot behind the slow one.
|
|
@@ -971,6 +1014,7 @@ export function createStreamingController({
|
|
|
971
1014
|
function start() {
|
|
972
1015
|
if (status === "streaming" || status === "starting" || status === "stopping") return false;
|
|
973
1016
|
generation += 1; // invalidate any ancient late responses
|
|
1017
|
+
paused = false; // a parked-then-reused lease never resumes paused
|
|
974
1018
|
abortActiveRequest(); // defensive: no lingering signal from a dead session
|
|
975
1019
|
stopPromise = null;
|
|
976
1020
|
tracker = trackerFactory();
|
|
@@ -1071,6 +1115,7 @@ export function createStreamingController({
|
|
|
1071
1115
|
async function stop() {
|
|
1072
1116
|
if (status === "idle" || status === "cancelled") return currentTranscript();
|
|
1073
1117
|
if (stopPromise) return stopPromise;
|
|
1118
|
+
paused = false;
|
|
1074
1119
|
const stoppingFrom = status;
|
|
1075
1120
|
const myGen = generation;
|
|
1076
1121
|
setStatus("stopping");
|
|
@@ -1213,12 +1258,44 @@ export function createStreamingController({
|
|
|
1213
1258
|
return await stopFromActive(myGen, "streaming", fin);
|
|
1214
1259
|
}
|
|
1215
1260
|
|
|
1261
|
+
// Pause: freeze the mic (SIGSTOP) and skip recognition ticks so paused
|
|
1262
|
+
// spans cost no inference and never enter the transcript. Only valid
|
|
1263
|
+
// while streaming; teardown paths (stop/cancel/dispose) clear it, and the
|
|
1264
|
+
// capture resumes the mic first so teardown can never hang on SIGINT.
|
|
1265
|
+
// An in-flight transcription is left to settle - it is pre-pause speech
|
|
1266
|
+
// the overlap tracker already handles.
|
|
1267
|
+
function pause() {
|
|
1268
|
+
if (status !== "streaming" || paused) return false;
|
|
1269
|
+
paused = true;
|
|
1270
|
+
try {
|
|
1271
|
+
capture?.pause?.();
|
|
1272
|
+
} catch {}
|
|
1273
|
+
try {
|
|
1274
|
+
onStateChange?.("paused");
|
|
1275
|
+
} catch {}
|
|
1276
|
+
return true;
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
function resume() {
|
|
1280
|
+
if (status !== "streaming" || !paused) return false;
|
|
1281
|
+
try {
|
|
1282
|
+
capture?.resume?.();
|
|
1283
|
+
} catch {}
|
|
1284
|
+
paused = false;
|
|
1285
|
+
try {
|
|
1286
|
+
onStateChange?.("resumed");
|
|
1287
|
+
} catch {}
|
|
1288
|
+
schedule(0);
|
|
1289
|
+
return true;
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1216
1292
|
// Cancel: prompt, bounded, invalidates late responses via generation.
|
|
1217
1293
|
// Aborts the physical in-flight request (no overlapping inference on
|
|
1218
1294
|
// restart), stops capture, and wins over any concurrent stop/error path
|
|
1219
1295
|
// via the generation bump. Never waits for inference.
|
|
1220
1296
|
async function cancel() {
|
|
1221
1297
|
if (status === "idle") return false;
|
|
1298
|
+
paused = false;
|
|
1222
1299
|
generation += 1;
|
|
1223
1300
|
abortActiveRequest();
|
|
1224
1301
|
setStatus("cancelled");
|
|
@@ -1236,6 +1313,7 @@ export function createStreamingController({
|
|
|
1236
1313
|
|
|
1237
1314
|
async function dispose() {
|
|
1238
1315
|
generation += 1;
|
|
1316
|
+
paused = false;
|
|
1239
1317
|
abortActiveRequest();
|
|
1240
1318
|
stopPromise = null;
|
|
1241
1319
|
startupPromise = null;
|
|
@@ -1309,6 +1387,7 @@ export function createStreamingController({
|
|
|
1309
1387
|
stableText: t.stableText,
|
|
1310
1388
|
tentativeText: t.tentativeText,
|
|
1311
1389
|
inFlight,
|
|
1390
|
+
paused,
|
|
1312
1391
|
lastError,
|
|
1313
1392
|
hasFallbackAudio: fallbackAudio !== null,
|
|
1314
1393
|
fallbackCoverage,
|
|
@@ -1320,6 +1399,8 @@ export function createStreamingController({
|
|
|
1320
1399
|
return {
|
|
1321
1400
|
start,
|
|
1322
1401
|
stop,
|
|
1402
|
+
pause,
|
|
1403
|
+
resume,
|
|
1323
1404
|
cancel,
|
|
1324
1405
|
dispose,
|
|
1325
1406
|
setModel,
|
|
@@ -41,6 +41,9 @@
|
|
|
41
41
|
// prefix it still shares, so no word is ever emitted twice.
|
|
42
42
|
//
|
|
43
43
|
// Update rule summary:
|
|
44
|
+
// 0. Final stop-tail decode covering the utterance from t=0 supersedes the
|
|
45
|
+
// rolling assembly outright (tentative = hypothesis); guarded by length
|
|
46
|
+
// so a collapsed decode can never wipe a good transcript.
|
|
44
47
|
// 1. Empty hypothesis: keep stable, clear the tail (pause/silence).
|
|
45
48
|
// 2. c = common word-prefix(assembled, hypothesis), k = overlap(assembled,
|
|
46
49
|
// hypothesis). If c > k: revision path (see above).
|
|
@@ -51,16 +54,29 @@
|
|
|
51
54
|
// 4. The first hypothesis only fills tentative (nothing is confirmed by a
|
|
52
55
|
// single observation).
|
|
53
56
|
//
|
|
54
|
-
// Comparison is word level, punctuation/case-insensitive; original
|
|
57
|
+
// Comparison is word level, punctuation/case/diacritic-insensitive; original
|
|
58
|
+
// words are preserved for display. Pure punctuation tokens must match exactly
|
|
59
|
+
// to count.
|
|
55
60
|
// preserved for display. Pure punctuation tokens must match exactly to count.
|
|
56
61
|
//
|
|
57
62
|
// Memory: O(transcript words). The tracker holds stable words, the tentative
|
|
58
63
|
// tail, and the last window hints - no history list, no per-tick
|
|
59
64
|
// accumulation. Long dictations grow only the transcript itself (the output).
|
|
65
|
+
import { sttLog } from "./stt-log.js";
|
|
60
66
|
|
|
61
67
|
export function normalizeStabilityWord(word) {
|
|
68
|
+
// Diacritic-insensitive: whisper re-decodes shared Vietnamese audio with
|
|
69
|
+
// shifting tone marks between windows, so exact matching breaks anchors on
|
|
70
|
+
// every tick (nothing stabilizes) or appends whole hypotheses as
|
|
71
|
+
// duplicates. Strip combining marks after NFD; Vietnamese đ/Đ do not
|
|
72
|
+
// decompose, so map them explicitly (after lowercase, only đ occurs).
|
|
73
|
+
// Genuine repetitions still work: overlap needs multi-word contiguous
|
|
74
|
+
// runs, not single words.
|
|
62
75
|
return String(word || "")
|
|
63
76
|
.toLowerCase()
|
|
77
|
+
.normalize("NFD")
|
|
78
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
79
|
+
.replace(/đ/g, "d")
|
|
64
80
|
.replace(/[^\p{L}\p{N}]/gu, "");
|
|
65
81
|
}
|
|
66
82
|
|
|
@@ -132,11 +148,13 @@ export function stripStablePrefix(stableWords, hypothesisWords) {
|
|
|
132
148
|
* streaming controller - this stays a pure text function for testability.
|
|
133
149
|
*
|
|
134
150
|
* `update(hypothesisText, windowHints)` accepts optional snapshot metadata
|
|
135
|
-
* ({ absoluteStartMs, absoluteEndMs, seq }) describing which audio
|
|
136
|
-
* hypothesis was decoded from. The hints are retained for
|
|
137
|
-
* accounting (see getState().lastWindow) and future alignment
|
|
138
|
-
* assembly itself is overlap-based because snapshot-relative
|
|
139
|
-
* cannot anchor a cross-window commit pointer.
|
|
151
|
+
* ({ absoluteStartMs, absoluteEndMs, seq, final }) describing which audio
|
|
152
|
+
* window the hypothesis was decoded from. The hints are retained for
|
|
153
|
+
* coverage accounting (see getState().lastWindow) and future alignment
|
|
154
|
+
* work; text assembly itself is overlap-based because snapshot-relative
|
|
155
|
+
* segment times cannot anchor a cross-window commit pointer. One exception:
|
|
156
|
+
* a final stop-tail decode covering the utterance from t=0 supersedes the
|
|
157
|
+
* rolling assembly outright (see update) instead of joining it.
|
|
140
158
|
*/
|
|
141
159
|
export function createStabilityTracker() {
|
|
142
160
|
let stableWords = [];
|
|
@@ -158,6 +176,44 @@ export function createStabilityTracker() {
|
|
|
158
176
|
return snapshot();
|
|
159
177
|
}
|
|
160
178
|
|
|
179
|
+
// Full-utterance stop-tail decode supersedes the rolling assembly.
|
|
180
|
+
// update() is also fed the final transcription (hints.final) decoded
|
|
181
|
+
// from the stop-tail WAV; when that window covers the utterance from t=0
|
|
182
|
+
// (spool-backed, or a ring tail starting at 0 for short dictations), a
|
|
183
|
+
// single full-context decode beats stitched windows. Joining it would
|
|
184
|
+
// append nearly the whole transcript again whenever the re-decode
|
|
185
|
+
// rewords the head and breaks every anchor - so replace instead.
|
|
186
|
+
// Guarded by length so a collapsed model output can never wipe a good
|
|
187
|
+
// transcript. A full-window final shorter than the live assembly is a
|
|
188
|
+
// re-decode that dropped words: the preview the user watched was right,
|
|
189
|
+
// so keep it instead of submitting a shrunken transcript. Tail-only
|
|
190
|
+
// finals still go through the normal join below.
|
|
191
|
+
if (
|
|
192
|
+
windowHints &&
|
|
193
|
+
windowHints.final === true &&
|
|
194
|
+
(windowHints.absoluteStartMs ?? Infinity) <= 1000
|
|
195
|
+
) {
|
|
196
|
+
const total = stableWords.length + tentativeWords.length;
|
|
197
|
+
if (total === 0 || hypoWords.length >= total) {
|
|
198
|
+
try {
|
|
199
|
+
sttLog(
|
|
200
|
+
"transcript.final-replace",
|
|
201
|
+
`hypoWords=${hypoWords.length} assembledWords=${total} absoluteStartMs=${windowHints.absoluteStartMs}`,
|
|
202
|
+
);
|
|
203
|
+
} catch {}
|
|
204
|
+
stableWords = [];
|
|
205
|
+
tentativeWords = hypoWords.slice();
|
|
206
|
+
return snapshot();
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
sttLog(
|
|
210
|
+
"transcript.final-keep",
|
|
211
|
+
`hypoWords=${hypoWords.length} assembledWords=${total} absoluteStartMs=${windowHints.absoluteStartMs}`,
|
|
212
|
+
);
|
|
213
|
+
} catch {}
|
|
214
|
+
return snapshot();
|
|
215
|
+
}
|
|
216
|
+
|
|
161
217
|
const assembled = stableWords.concat(tentativeWords);
|
|
162
218
|
|
|
163
219
|
// First observation of the session: everything is tentative.
|