@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/stt.js
CHANGED
|
@@ -282,6 +282,9 @@ let pipelineGeneration = 0;
|
|
|
282
282
|
// conversation loop instead of running the one-shot pipeline.
|
|
283
283
|
|
|
284
284
|
let conversationHooks = { isActive: null, onKey: null };
|
|
285
|
+
let conversationStreamingEnabled = false;
|
|
286
|
+
let conversationStreamingStopSec = null;
|
|
287
|
+
let conversationStreamingOnStopped = null;
|
|
285
288
|
|
|
286
289
|
export function __setConversationHooks(hooks) {
|
|
287
290
|
conversationHooks = { ...conversationHooks, ...hooks };
|
|
@@ -549,6 +552,8 @@ let streamingToastFn = null;
|
|
|
549
552
|
let streamingToastBase = "";
|
|
550
553
|
let streamingToastStartMs = null;
|
|
551
554
|
let streamingLastPartialMs = null;
|
|
555
|
+
let lastPartialText = null;
|
|
556
|
+
let lastAudioActiveMs = null;
|
|
552
557
|
let streamingSlowAfterMs = 2000;
|
|
553
558
|
const STREAMING_TOAST_DURATION = 3000;
|
|
554
559
|
const STREAMING_TOAST_INTERVAL = 2500;
|
|
@@ -566,6 +571,8 @@ export function __clearStreamingToastState() {
|
|
|
566
571
|
streamingToastBase = "";
|
|
567
572
|
streamingToastStartMs = null;
|
|
568
573
|
streamingLastPartialMs = null;
|
|
574
|
+
lastAudioActiveMs = null;
|
|
575
|
+
lastPartialText = null;
|
|
569
576
|
}
|
|
570
577
|
|
|
571
578
|
export function isStreamingToastActive() {
|
|
@@ -601,6 +608,8 @@ export function showStreamingToast(message, { slowAfterMs } = {}) {
|
|
|
601
608
|
streamingToastBase = message;
|
|
602
609
|
streamingToastStartMs = Date.now();
|
|
603
610
|
streamingLastPartialMs = null;
|
|
611
|
+
lastAudioActiveMs = null;
|
|
612
|
+
lastPartialText = null;
|
|
604
613
|
if (Number(slowAfterMs) > 0) streamingSlowAfterMs = Math.floor(Number(slowAfterMs));
|
|
605
614
|
if (!streamingToastFn) return;
|
|
606
615
|
sttLog("toast.streaming", `base=${streamingToastBase} polling=${toastPolling}`);
|
|
@@ -620,7 +629,13 @@ export function updateStreamingToast(message) {
|
|
|
620
629
|
emitStreamingToast();
|
|
621
630
|
}
|
|
622
631
|
|
|
623
|
-
export function noteStreamingPartial() {
|
|
632
|
+
export function noteStreamingPartial(partial) {
|
|
633
|
+
// Don't reset the silence timer if the partial is identical to the
|
|
634
|
+
// previous one - whisper hallucinating on silence (repeating the same
|
|
635
|
+
// phrase) must not keep the auto-end timer alive.
|
|
636
|
+
const text = partial ? `${partial.stableText || ""} ${partial.tentativeText || ""}`.trim() : "";
|
|
637
|
+
if (text && text === lastPartialText) return;
|
|
638
|
+
lastPartialText = text;
|
|
624
639
|
streamingLastPartialMs = Date.now();
|
|
625
640
|
}
|
|
626
641
|
|
|
@@ -632,6 +647,7 @@ export function clearStreamingToast() {
|
|
|
632
647
|
streamingToastBase = "";
|
|
633
648
|
streamingToastStartMs = null;
|
|
634
649
|
streamingLastPartialMs = null;
|
|
650
|
+
lastAudioActiveMs = null;
|
|
635
651
|
}
|
|
636
652
|
|
|
637
653
|
export function formatElapsed(ms) {
|
|
@@ -922,7 +938,15 @@ function forceKillSox(logger) {
|
|
|
922
938
|
} catch {}
|
|
923
939
|
}
|
|
924
940
|
|
|
925
|
-
function startRecording(
|
|
941
|
+
function startRecording(
|
|
942
|
+
kv,
|
|
943
|
+
backend,
|
|
944
|
+
toast,
|
|
945
|
+
logger,
|
|
946
|
+
trimSilence = true,
|
|
947
|
+
silenceStopSec = null,
|
|
948
|
+
onStopped = null,
|
|
949
|
+
) {
|
|
926
950
|
if (soxProc) {
|
|
927
951
|
logger?.log("STT", "Start recording skipped: sox already running", "debug");
|
|
928
952
|
return;
|
|
@@ -940,11 +964,16 @@ function startRecording(kv, backend, toast, logger, trimSilence = true) {
|
|
|
940
964
|
const spawnT0 = Date.now();
|
|
941
965
|
logger?.log(
|
|
942
966
|
"STT",
|
|
943
|
-
`Starting recording backend=${backend} mic=${mic || "system default"}`,
|
|
967
|
+
`Starting recording backend=${backend} mic=${mic || "system default"} silenceStop=${silenceStopSec ?? "off"}`,
|
|
944
968
|
"debug",
|
|
945
969
|
);
|
|
946
970
|
|
|
971
|
+
// Trim leading silence, then (optionally) auto-stop after N seconds of
|
|
972
|
+
// silence so hands-free modes (conversation) can finalize without a keypress.
|
|
947
973
|
const silenceArgs = trimSilence ? ["silence", "1", "0.05", "1%"] : [];
|
|
974
|
+
if (silenceStopSec && silenceStopSec > 0) {
|
|
975
|
+
silenceArgs.push("1", String(silenceStopSec), "1%");
|
|
976
|
+
}
|
|
948
977
|
soxProc = spawn(
|
|
949
978
|
"sox",
|
|
950
979
|
[...inputArgs, "-r", "16000", "-c", "1", "-b", "16", wavFile, ...silenceArgs],
|
|
@@ -991,7 +1020,15 @@ function startRecording(kv, backend, toast, logger, trimSilence = true) {
|
|
|
991
1020
|
"error",
|
|
992
1021
|
);
|
|
993
1022
|
} else {
|
|
994
|
-
|
|
1023
|
+
// Normal exit (including silence auto-stop): fire the onStopped
|
|
1024
|
+
// callback if registered, otherwise show the generic toast.
|
|
1025
|
+
if (typeof onStopped === "function") {
|
|
1026
|
+
try {
|
|
1027
|
+
onStopped();
|
|
1028
|
+
} catch {}
|
|
1029
|
+
} else {
|
|
1030
|
+
toast("Recording stopped unexpectedly", "warning");
|
|
1031
|
+
}
|
|
995
1032
|
}
|
|
996
1033
|
}
|
|
997
1034
|
});
|
|
@@ -1454,6 +1491,27 @@ async function transcribeApi(kv, logger) {
|
|
|
1454
1491
|
return transcribeApiFile(wavFile, sttApiEndpoint, model, sttApiKeyEnv, logger);
|
|
1455
1492
|
}
|
|
1456
1493
|
|
|
1494
|
+
/**
|
|
1495
|
+
* Which path owns silence auto-end for the current streaming session.
|
|
1496
|
+
* Conversation mode always wins: the plain dictation path submits the text
|
|
1497
|
+
* directly, which bypasses the conversation turn (no reply wait, no speech,
|
|
1498
|
+
* UI stuck on "Listening"). While a conversation is active the timer either
|
|
1499
|
+
* finishes the turn through the conversation callback or stays off - it never
|
|
1500
|
+
* falls back to that submit.
|
|
1501
|
+
*/
|
|
1502
|
+
export function streamAutoEndRoute({
|
|
1503
|
+
conversation,
|
|
1504
|
+
conversationStopSec,
|
|
1505
|
+
conversationOnStopped,
|
|
1506
|
+
previewAutoEnd,
|
|
1507
|
+
}) {
|
|
1508
|
+
if (conversation) {
|
|
1509
|
+
const enabled = conversationStopSec > 0 || previewAutoEnd === true;
|
|
1510
|
+
return enabled && typeof conversationOnStopped === "function" ? "conversation" : "off";
|
|
1511
|
+
}
|
|
1512
|
+
return previewAutoEnd === true ? "dictation" : "off";
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1457
1515
|
export function insertIntoFocusedInput(renderer, text, submit = false) {
|
|
1458
1516
|
const focused = renderer?.currentFocusedRenderable;
|
|
1459
1517
|
if (!focused || typeof focused.insertText !== "function") return false;
|
|
@@ -1701,6 +1759,12 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
1701
1759
|
const client = api.client;
|
|
1702
1760
|
const renderer = opts?.focusMode === "primary" ? null : api.renderer;
|
|
1703
1761
|
const preview = api.voicePreview;
|
|
1762
|
+
// Seed engine from opts if the host delivered it.
|
|
1763
|
+
try {
|
|
1764
|
+
if (opts?.ttsEngine && kv.get("tts.engine") == null) {
|
|
1765
|
+
kv.set("tts.engine", opts.ttsEngine);
|
|
1766
|
+
}
|
|
1767
|
+
} catch {}
|
|
1704
1768
|
__setToastPolling(!preview);
|
|
1705
1769
|
const bootMode = resolveSttMode(opts, kv);
|
|
1706
1770
|
let modeSource = "default-batch";
|
|
@@ -1790,9 +1854,23 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
1790
1854
|
|
|
1791
1855
|
// Start a recording for the voice-conversation loop. Returns false when busy.
|
|
1792
1856
|
|
|
1857
|
+
// Conversation-mode silence auto-finalize: when set, the recording uses
|
|
1858
|
+
// sox silence detection to auto-stop after this many seconds of silence,
|
|
1859
|
+
// and the onStopped callback fires. Default null = manual stop only.
|
|
1860
|
+
let conversationSilenceStopSec = null;
|
|
1861
|
+
let conversationOnStopped = null;
|
|
1862
|
+
|
|
1793
1863
|
function startLoopRecording() {
|
|
1794
1864
|
if (recording || processing || streamingDictationActive) return false;
|
|
1795
|
-
startRecording(
|
|
1865
|
+
startRecording(
|
|
1866
|
+
kv,
|
|
1867
|
+
backend,
|
|
1868
|
+
toast,
|
|
1869
|
+
logger,
|
|
1870
|
+
opts?.trimSilence,
|
|
1871
|
+
conversationSilenceStopSec,
|
|
1872
|
+
conversationOnStopped,
|
|
1873
|
+
);
|
|
1796
1874
|
if (recording) {
|
|
1797
1875
|
showRecordingToast();
|
|
1798
1876
|
// Fetch title + recent turns while the user speaks, so the context is
|
|
@@ -1837,6 +1915,102 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
1837
1915
|
let streamEditor = null;
|
|
1838
1916
|
let streamTarget = null;
|
|
1839
1917
|
let streamStarting = false;
|
|
1918
|
+
// Auto-end-of-speech: when enabled, a timer checks if no new partial has
|
|
1919
|
+
// arrived for AUTO_END_SILENCE_MS and triggers finalize automatically.
|
|
1920
|
+
let streamAutoEndTimer = null;
|
|
1921
|
+
const AUTO_END_SILENCE_MS = 3000;
|
|
1922
|
+
|
|
1923
|
+
function enableStreamAutoEnd() {
|
|
1924
|
+
disableStreamAutoEnd();
|
|
1925
|
+
streamAutoEndTimer = setInterval(() => {
|
|
1926
|
+
if (!streamingDictationActive || !streamController) {
|
|
1927
|
+
disableStreamAutoEnd();
|
|
1928
|
+
return;
|
|
1929
|
+
}
|
|
1930
|
+
const route = streamAutoEndRoute({
|
|
1931
|
+
conversation: conversationActive(),
|
|
1932
|
+
conversationStopSec: conversationStreamingStopSec,
|
|
1933
|
+
conversationOnStopped: conversationStreamingOnStopped,
|
|
1934
|
+
previewAutoEnd: preview?.getAutoEndOfSpeech?.() === true,
|
|
1935
|
+
});
|
|
1936
|
+
if (route === "off") return;
|
|
1937
|
+
const useConv = route === "conversation";
|
|
1938
|
+
const convSec = conversationStreamingStopSec;
|
|
1939
|
+
let audioSilent = false;
|
|
1940
|
+
let rms = 0;
|
|
1941
|
+
try {
|
|
1942
|
+
const pcm = streamController.peekPcm?.(STREAM_WAVE_BYTES);
|
|
1943
|
+
if (pcm && pcm.length >= 2) {
|
|
1944
|
+
let sum = 0;
|
|
1945
|
+
const samples = Math.floor(pcm.length / 2);
|
|
1946
|
+
for (let i = 0; i + 1 < pcm.length; i += 2) {
|
|
1947
|
+
const s = pcm.readInt16LE(i) / 32768;
|
|
1948
|
+
sum += s * s;
|
|
1949
|
+
}
|
|
1950
|
+
rms = Math.sqrt(sum / samples);
|
|
1951
|
+
if (rms < 0.01) audioSilent = true;
|
|
1952
|
+
}
|
|
1953
|
+
} catch {}
|
|
1954
|
+
if (audioSilent) {
|
|
1955
|
+
if (lastAudioActiveMs === null) lastAudioActiveMs = Date.now();
|
|
1956
|
+
} else {
|
|
1957
|
+
lastAudioActiveMs = null;
|
|
1958
|
+
}
|
|
1959
|
+
const audioSilentMs = lastAudioActiveMs ? Date.now() - lastAudioActiveMs : 0;
|
|
1960
|
+
const last = streamingLastPartialMs;
|
|
1961
|
+
const elapsed = last ? Date.now() - last : 0;
|
|
1962
|
+
const threshold = useConv && convSec > 0 ? convSec * 1000 : AUTO_END_SILENCE_MS;
|
|
1963
|
+
if (useConv) {
|
|
1964
|
+
if (audioSilent && audioSilentMs > threshold) {
|
|
1965
|
+
sttLog(
|
|
1966
|
+
"stream.autoEnd",
|
|
1967
|
+
`conversation triggered rms=${rms.toFixed(4)} silentMs=${audioSilentMs}ms`,
|
|
1968
|
+
);
|
|
1969
|
+
disableStreamAutoEnd();
|
|
1970
|
+
try {
|
|
1971
|
+
conversationStreamingOnStopped?.();
|
|
1972
|
+
} catch {}
|
|
1973
|
+
return;
|
|
1974
|
+
}
|
|
1975
|
+
if (last && elapsed > threshold * 2) {
|
|
1976
|
+
sttLog(
|
|
1977
|
+
"stream.autoEnd",
|
|
1978
|
+
`conversation triggered (partial) rms=${rms.toFixed(4)} elapsed=${elapsed}ms`,
|
|
1979
|
+
);
|
|
1980
|
+
disableStreamAutoEnd();
|
|
1981
|
+
try {
|
|
1982
|
+
conversationStreamingOnStopped?.();
|
|
1983
|
+
} catch {}
|
|
1984
|
+
}
|
|
1985
|
+
return;
|
|
1986
|
+
}
|
|
1987
|
+
if (audioSilent && audioSilentMs > threshold) {
|
|
1988
|
+
sttLog(
|
|
1989
|
+
"stream.autoEnd",
|
|
1990
|
+
`triggered by silence (audio) rms=${rms.toFixed(4)} silentMs=${audioSilentMs}ms`,
|
|
1991
|
+
);
|
|
1992
|
+
disableStreamAutoEnd();
|
|
1993
|
+
void finalizeStreamingDictation({ submit: preview?.getAutoSubmit?.() ?? false });
|
|
1994
|
+
return;
|
|
1995
|
+
}
|
|
1996
|
+
if (last && elapsed > threshold * 2) {
|
|
1997
|
+
sttLog(
|
|
1998
|
+
"stream.autoEnd",
|
|
1999
|
+
`triggered by silence (partial only) rms=${rms.toFixed(4)} elapsed=${elapsed}ms`,
|
|
2000
|
+
);
|
|
2001
|
+
disableStreamAutoEnd();
|
|
2002
|
+
void finalizeStreamingDictation({ submit: preview?.getAutoSubmit?.() ?? false });
|
|
2003
|
+
}
|
|
2004
|
+
}, 500);
|
|
2005
|
+
if (typeof streamAutoEndTimer.unref === "function") streamAutoEndTimer.unref();
|
|
2006
|
+
}
|
|
2007
|
+
|
|
2008
|
+
function disableStreamAutoEnd() {
|
|
2009
|
+
if (streamAutoEndTimer) {
|
|
2010
|
+
clearInterval(streamAutoEndTimer);
|
|
2011
|
+
streamAutoEndTimer = null;
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
1840
2014
|
// Warm server lease retained across dictations: the idle controller (mic
|
|
1841
2015
|
// fully stopped, server still loaded) parks here between sessions. At most
|
|
1842
2016
|
// one warm lease ever exists; the next start reuses it when the model key
|
|
@@ -1896,11 +2070,10 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
1896
2070
|
toast("Live notes recording - stop it first (/voice-notes-stop)", "warning");
|
|
1897
2071
|
return false;
|
|
1898
2072
|
}
|
|
1899
|
-
if (conversationActive()) {
|
|
2073
|
+
if (conversationActive() && !conversationStreamingEnabled) {
|
|
1900
2074
|
toast("Conversation mode is on - use the conversation key to exit", "warning");
|
|
1901
2075
|
return false;
|
|
1902
2076
|
}
|
|
1903
|
-
streamStarting = true;
|
|
1904
2077
|
const myGen = ++streamGeneration;
|
|
1905
2078
|
// Model/language change tears down the warm lease first so the next
|
|
1906
2079
|
// start loads the new model instead of reusing the stale server. The
|
|
@@ -1928,6 +2101,54 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
1928
2101
|
if (preview) await preview.begin();
|
|
1929
2102
|
else editor.begin(target);
|
|
1930
2103
|
sttLog("stream.begin", `preview=${preview ? "yes" : "no"}`);
|
|
2104
|
+
// Clickable pause lives on the preview line (v2 slot). The toggle
|
|
2105
|
+
// action funnels through the same pause/resume path as the command.
|
|
2106
|
+
try {
|
|
2107
|
+
preview?.setToggleAction?.(() => toggleStreamingPause());
|
|
2108
|
+
} catch {}
|
|
2109
|
+
// Restore persisted toggle states from kv so they survive restarts.
|
|
2110
|
+
try {
|
|
2111
|
+
const savedAutoSubmit = kv.get("stt.autoSubmit");
|
|
2112
|
+
if (savedAutoSubmit === true || savedAutoSubmit === false) {
|
|
2113
|
+
preview?.setAutoSubmit?.(savedAutoSubmit);
|
|
2114
|
+
}
|
|
2115
|
+
} catch {}
|
|
2116
|
+
try {
|
|
2117
|
+
const savedAutoEnd = kv.get("stt.autoEndOfSpeech");
|
|
2118
|
+
if (savedAutoEnd === true || savedAutoEnd === false) {
|
|
2119
|
+
preview?.setAutoEndOfSpeech?.(savedAutoEnd);
|
|
2120
|
+
}
|
|
2121
|
+
} catch {}
|
|
2122
|
+
try {
|
|
2123
|
+
preview?.setAutoSubmitToggleAction?.(() => {
|
|
2124
|
+
const next = !preview.getAutoSubmit?.();
|
|
2125
|
+
preview.setAutoSubmit?.(next);
|
|
2126
|
+
// Persist in kv so it survives restarts.
|
|
2127
|
+
try {
|
|
2128
|
+
kv.set("stt.autoSubmit", next);
|
|
2129
|
+
} catch {}
|
|
2130
|
+
sttLog("stream.autoSubmit", `toggled to ${next}`);
|
|
2131
|
+
});
|
|
2132
|
+
} catch {}
|
|
2133
|
+
try {
|
|
2134
|
+
preview?.setAutoEndToggleAction?.(() => {
|
|
2135
|
+
const next = !preview.getAutoEndOfSpeech?.();
|
|
2136
|
+
preview.setAutoEndOfSpeech?.(next);
|
|
2137
|
+
// Persist so it survives restarts.
|
|
2138
|
+
try {
|
|
2139
|
+
kv.set("stt.autoEndOfSpeech", next);
|
|
2140
|
+
} catch {}
|
|
2141
|
+
sttLog("stream.autoEnd", `toggled to ${next}`);
|
|
2142
|
+
// If recording is active, enable/disable silence auto-finalize.
|
|
2143
|
+
if (streamingDictationActive && streamController) {
|
|
2144
|
+
if (next) {
|
|
2145
|
+
enableStreamAutoEnd();
|
|
2146
|
+
} else {
|
|
2147
|
+
disableStreamAutoEnd();
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
});
|
|
2151
|
+
} catch {}
|
|
1931
2152
|
if (streamDisposed || myGen !== streamGeneration) {
|
|
1932
2153
|
preview?.clear();
|
|
1933
2154
|
return false;
|
|
@@ -1936,7 +2157,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
1936
2157
|
onPartial: (p) => {
|
|
1937
2158
|
if (streamDisposed || myGen !== streamGeneration) return;
|
|
1938
2159
|
try {
|
|
1939
|
-
noteStreamingPartial();
|
|
2160
|
+
noteStreamingPartial(p);
|
|
1940
2161
|
} catch {}
|
|
1941
2162
|
try {
|
|
1942
2163
|
ed.applyPartial(p);
|
|
@@ -2015,6 +2236,9 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
2015
2236
|
}
|
|
2016
2237
|
return pcm ? waveLevelsFromPcm(pcm) : null;
|
|
2017
2238
|
});
|
|
2239
|
+
// Auto-end: always run the timer; it checks the preview's toggle
|
|
2240
|
+
// state internally so toggling mid-recording works without restart.
|
|
2241
|
+
enableStreamAutoEnd();
|
|
2018
2242
|
}
|
|
2019
2243
|
// Sticky, never silent: covers the cold model load ("Loading…") until
|
|
2020
2244
|
// the controller reports streaming, then live capture with elapsed
|
|
@@ -2053,6 +2277,60 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
2053
2277
|
}
|
|
2054
2278
|
}
|
|
2055
2279
|
|
|
2280
|
+
// Pause/resume for live dictation: freezes the mic (SIGSTOP) and skips
|
|
2281
|
+
// recognition ticks so paused spans cost nothing and never enter the
|
|
2282
|
+
// transcript. The record key resumes a paused session instead of
|
|
2283
|
+
// finalizing it; explicit stop stays on /stt-stop.
|
|
2284
|
+
function toggleStreamingPause() {
|
|
2285
|
+
if (!streamingDictationActive || !streamController) return false;
|
|
2286
|
+
let pausedNow = false;
|
|
2287
|
+
try {
|
|
2288
|
+
pausedNow = !!streamController.getState?.().paused;
|
|
2289
|
+
} catch {
|
|
2290
|
+
return false;
|
|
2291
|
+
}
|
|
2292
|
+
if (pausedNow) {
|
|
2293
|
+
let ok = false;
|
|
2294
|
+
try {
|
|
2295
|
+
ok = streamController.resume() === true;
|
|
2296
|
+
} catch {
|
|
2297
|
+
ok = false;
|
|
2298
|
+
}
|
|
2299
|
+
if (ok) {
|
|
2300
|
+
preview?.setPaused?.(false);
|
|
2301
|
+
updateStreamingToast("● Streaming dictation — listening");
|
|
2302
|
+
sttLog("stream.resume", "ok");
|
|
2303
|
+
}
|
|
2304
|
+
return ok;
|
|
2305
|
+
}
|
|
2306
|
+
let ok = false;
|
|
2307
|
+
try {
|
|
2308
|
+
ok = streamController.pause?.() === true;
|
|
2309
|
+
} catch {
|
|
2310
|
+
ok = false;
|
|
2311
|
+
}
|
|
2312
|
+
if (ok) {
|
|
2313
|
+
preview?.setPaused?.(true);
|
|
2314
|
+
updateStreamingToast("⏸ Dictation paused — press record or click ▶ to resume");
|
|
2315
|
+
sttLog("stream.pause", "ok");
|
|
2316
|
+
}
|
|
2317
|
+
return ok;
|
|
2318
|
+
}
|
|
2319
|
+
|
|
2320
|
+
// Pause/resume entry point shared by the /stt-pause command. Returns false
|
|
2321
|
+
// with a hint toast when there is nothing pausable.
|
|
2322
|
+
async function pauseStreamingDictation() {
|
|
2323
|
+
if (!streamingDictationActive || !streamController) {
|
|
2324
|
+
toast("No streaming dictation in progress", "warning");
|
|
2325
|
+
return false;
|
|
2326
|
+
}
|
|
2327
|
+
if (!toggleStreamingPause()) {
|
|
2328
|
+
toast("Pause unavailable right now - use /stt-stop to cancel", "warning");
|
|
2329
|
+
return false;
|
|
2330
|
+
}
|
|
2331
|
+
return true;
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2056
2334
|
// Finalize: stop the controller (single tail transcription, no LLM),
|
|
2057
2335
|
// commit the editor range, then insert EXACTLY once via the focused-input
|
|
2058
2336
|
// helper. Never falls through to primary-chat appendPrompt. When `submit`
|
|
@@ -2061,9 +2339,10 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
2061
2339
|
// server lease is RETAINED across dictations (parked as the single warm
|
|
2062
2340
|
// handle) and released only when the stop result proves the server itself
|
|
2063
2341
|
// is at fault, on model/language change, or on plugin unload.
|
|
2064
|
-
async function finalizeStreamingDictation({ submit = false } = {}) {
|
|
2342
|
+
async function finalizeStreamingDictation({ submit = false, insert = true } = {}) {
|
|
2065
2343
|
if (!streamingDictationActive || !streamController) return null;
|
|
2066
|
-
sttLog("stream.finalize", `submit=${submit}`);
|
|
2344
|
+
sttLog("stream.finalize", `submit=${submit} insert=${insert}`);
|
|
2345
|
+
disableStreamAutoEnd();
|
|
2067
2346
|
const controller = streamController;
|
|
2068
2347
|
const editor = streamEditor;
|
|
2069
2348
|
const target = streamTarget;
|
|
@@ -2143,7 +2422,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
2143
2422
|
}
|
|
2144
2423
|
if (fin?.inserted) {
|
|
2145
2424
|
logger?.log("STT", `Streaming finalized via owned range chars=${text.length}`, "debug");
|
|
2146
|
-
if (submit || autoSubmitNow()) {
|
|
2425
|
+
if (submit === true || (submit !== false && autoSubmitNow())) {
|
|
2147
2426
|
if (target && typeof target.submit === "function") {
|
|
2148
2427
|
try {
|
|
2149
2428
|
target.submit();
|
|
@@ -2165,41 +2444,45 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
2165
2444
|
// missing/unusable target retains the transcript via toast instead of
|
|
2166
2445
|
// redirecting into chat or the current focus.
|
|
2167
2446
|
let inserted = false;
|
|
2168
|
-
if (
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2447
|
+
if (insert) {
|
|
2448
|
+
if (target && typeof target.insertText === "function") {
|
|
2449
|
+
try {
|
|
2450
|
+
target.insertText(text);
|
|
2451
|
+
inserted = true;
|
|
2452
|
+
} catch {
|
|
2453
|
+
inserted = false;
|
|
2454
|
+
}
|
|
2455
|
+
} else {
|
|
2456
|
+
inserted = insertIntoFocusedInput(renderer, text, false);
|
|
2174
2457
|
}
|
|
2175
|
-
} else {
|
|
2176
|
-
inserted = insertIntoFocusedInput(renderer, text, false);
|
|
2177
2458
|
}
|
|
2178
|
-
if (!inserted) {
|
|
2459
|
+
if (insert && !inserted) {
|
|
2179
2460
|
logger?.log("STT", "Streaming finalize: no editable field, transcript retained", "warn");
|
|
2180
2461
|
toast("No editable field - dictated text kept (not inserted elsewhere)", "warning");
|
|
2181
2462
|
if (preview) processing = false;
|
|
2182
2463
|
retainWarmStreamController(controller);
|
|
2183
2464
|
return { text, inserted: false };
|
|
2184
2465
|
}
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
if (
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2466
|
+
if (insert) {
|
|
2467
|
+
logger?.log("STT", `Streaming finalized chars=${text.length}`, "debug");
|
|
2468
|
+
if (submit === true || (submit !== false && autoSubmitNow())) {
|
|
2469
|
+
if (target && typeof target.submit === "function") {
|
|
2470
|
+
try {
|
|
2471
|
+
target.submit();
|
|
2472
|
+
toast("Streaming transcription submitted", "success");
|
|
2473
|
+
} catch (err) {
|
|
2474
|
+
toast(`Streaming submit failed: ${err?.message || err}`, "error");
|
|
2475
|
+
}
|
|
2476
|
+
} else {
|
|
2477
|
+
toast("Target cannot submit - text inserted, not submitted", "warning");
|
|
2193
2478
|
}
|
|
2194
2479
|
} else {
|
|
2195
|
-
toast("
|
|
2480
|
+
toast("Streaming transcription added", "success");
|
|
2196
2481
|
}
|
|
2197
|
-
} else {
|
|
2198
|
-
toast("Streaming transcription added", "success");
|
|
2199
2482
|
}
|
|
2200
2483
|
if (preview) processing = false;
|
|
2201
2484
|
retainWarmStreamController(controller);
|
|
2202
|
-
return { text, inserted
|
|
2485
|
+
return { text, inserted };
|
|
2203
2486
|
}
|
|
2204
2487
|
|
|
2205
2488
|
// Cancel: drop the session and remove ONLY plugin-owned dictated text.
|
|
@@ -2209,6 +2492,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
2209
2492
|
async function cancelStreamingDictation() {
|
|
2210
2493
|
streamGeneration += 1;
|
|
2211
2494
|
sttLog("stream.cancel");
|
|
2495
|
+
disableStreamAutoEnd();
|
|
2212
2496
|
preview?.clear();
|
|
2213
2497
|
if (preview && processing) cancelRecording(logger);
|
|
2214
2498
|
if (finalizingController) {
|
|
@@ -2283,7 +2567,22 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
2283
2567
|
isRecording: () => recording,
|
|
2284
2568
|
isProcessing: () => processing,
|
|
2285
2569
|
isStreaming,
|
|
2570
|
+
log: (msg) => sttLog("CONV", msg),
|
|
2286
2571
|
start: startLoopRecording,
|
|
2572
|
+
setSilenceAutoFinalize: (sec, onStopped) => {
|
|
2573
|
+
conversationSilenceStopSec = sec && sec > 0 ? sec : null;
|
|
2574
|
+
conversationOnStopped = typeof onStopped === "function" ? onStopped : null;
|
|
2575
|
+
},
|
|
2576
|
+
setConversationStreamingFinalize: (sec, onStopped) => {
|
|
2577
|
+
conversationStreamingStopSec = sec && sec > 0 ? sec : null;
|
|
2578
|
+
conversationStreamingOnStopped = typeof onStopped === "function" ? onStopped : null;
|
|
2579
|
+
if (conversationStreamingStopSec) enableStreamAutoEnd();
|
|
2580
|
+
else disableStreamAutoEnd();
|
|
2581
|
+
},
|
|
2582
|
+
setConversationStreamingEnabled: (v) => {
|
|
2583
|
+
conversationStreamingEnabled = !!v;
|
|
2584
|
+
},
|
|
2585
|
+
finalizeStreaming: (opts) => finalizeStreamingDictation(opts || {}),
|
|
2287
2586
|
setStopHint: (hint) => __setStopHint(hint),
|
|
2288
2587
|
cancel: () => cancelRecording(logger),
|
|
2289
2588
|
discard: () => discardTurn(),
|
|
@@ -2321,11 +2620,18 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
2321
2620
|
...(kb("stt.record") ? { keybind: kb("stt.record") } : {}),
|
|
2322
2621
|
slash: { name: "stt-record" },
|
|
2323
2622
|
onSelect() {
|
|
2324
|
-
// Streaming mode: toggle streaming record/finalize
|
|
2325
|
-
//
|
|
2623
|
+
// Streaming mode: toggle streaming record/finalize, or resume a
|
|
2624
|
+
// paused session (cheaper than re-speaking; explicit stop stays on
|
|
2625
|
+
// /stt-stop). Batch default below is behavior-identical to before.
|
|
2326
2626
|
sttLog("toggle", `sttMode=${currentSttMode()} streamingActive=${streamingDictationActive}`);
|
|
2327
2627
|
if (currentSttMode() === "streaming") {
|
|
2328
2628
|
if (streamingDictationActive) {
|
|
2629
|
+
try {
|
|
2630
|
+
if (streamController?.getState?.().paused) {
|
|
2631
|
+
toggleStreamingPause();
|
|
2632
|
+
return;
|
|
2633
|
+
}
|
|
2634
|
+
} catch {}
|
|
2329
2635
|
void finalizeStreamingDictation({ submit: false });
|
|
2330
2636
|
return;
|
|
2331
2637
|
}
|
|
@@ -2473,6 +2779,22 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
|
|
|
2473
2779
|
}
|
|
2474
2780
|
},
|
|
2475
2781
|
},
|
|
2782
|
+
{
|
|
2783
|
+
title: "STT: pause / resume streaming dictation",
|
|
2784
|
+
value: "stt.pause",
|
|
2785
|
+
category: "opencode-voice",
|
|
2786
|
+
description: "Freeze the mic mid-dictation (skips noise); run again to resume",
|
|
2787
|
+
slash: { name: "stt-pause" },
|
|
2788
|
+
onSelect() {
|
|
2789
|
+
// Batch mode has no pausable session (one-shot record); the mic
|
|
2790
|
+
// toggle is start/stop there.
|
|
2791
|
+
if (currentSttMode() !== "streaming" && !streamingDictationActive) {
|
|
2792
|
+
toast("Pause needs streaming mode - use /stt-mode to switch", "warning");
|
|
2793
|
+
return;
|
|
2794
|
+
}
|
|
2795
|
+
void pauseStreamingDictation();
|
|
2796
|
+
},
|
|
2797
|
+
},
|
|
2476
2798
|
{
|
|
2477
2799
|
title: "STT: select dictation mode",
|
|
2478
2800
|
value: "stt.mode",
|
package/lib/tts-player.js
CHANGED
|
@@ -80,7 +80,9 @@ export function measureWavDurationSec(wav) {
|
|
|
80
80
|
// header at load), so sox needs no `-t raw` format declaration and the
|
|
81
81
|
// argv here is the complete command.
|
|
82
82
|
export function buildPlayArgs({ offsetSec = 0, rate = 1 } = {}) {
|
|
83
|
-
|
|
83
|
+
// Small read-ahead buffer so SIGSTOP lands closer to the audible cursor;
|
|
84
|
+
// the default (8192) lets ~0.4s of audio queue ahead of the process.
|
|
85
|
+
const argv = ["-q", "--buffer", "1024", "-"];
|
|
84
86
|
const offset = Number(offsetSec) || 0;
|
|
85
87
|
if (offset > 0) argv.push("trim", String(offset));
|
|
86
88
|
const speed = Number(rate) || 1;
|