@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/lib/stt.js CHANGED
@@ -549,6 +549,7 @@ let streamingToastFn = null;
549
549
  let streamingToastBase = "";
550
550
  let streamingToastStartMs = null;
551
551
  let streamingLastPartialMs = null;
552
+ let lastPartialText = null;
552
553
  let streamingSlowAfterMs = 2000;
553
554
  const STREAMING_TOAST_DURATION = 3000;
554
555
  const STREAMING_TOAST_INTERVAL = 2500;
@@ -566,6 +567,7 @@ export function __clearStreamingToastState() {
566
567
  streamingToastBase = "";
567
568
  streamingToastStartMs = null;
568
569
  streamingLastPartialMs = null;
570
+ lastPartialText = null;
569
571
  }
570
572
 
571
573
  export function isStreamingToastActive() {
@@ -601,6 +603,7 @@ export function showStreamingToast(message, { slowAfterMs } = {}) {
601
603
  streamingToastBase = message;
602
604
  streamingToastStartMs = Date.now();
603
605
  streamingLastPartialMs = null;
606
+ lastPartialText = null;
604
607
  if (Number(slowAfterMs) > 0) streamingSlowAfterMs = Math.floor(Number(slowAfterMs));
605
608
  if (!streamingToastFn) return;
606
609
  sttLog("toast.streaming", `base=${streamingToastBase} polling=${toastPolling}`);
@@ -620,7 +623,13 @@ export function updateStreamingToast(message) {
620
623
  emitStreamingToast();
621
624
  }
622
625
 
623
- export function noteStreamingPartial() {
626
+ export function noteStreamingPartial(partial) {
627
+ // Don't reset the silence timer if the partial is identical to the
628
+ // previous one - whisper hallucinating on silence (repeating the same
629
+ // phrase) must not keep the auto-end timer alive.
630
+ const text = partial ? `${partial.stableText || ""} ${partial.tentativeText || ""}`.trim() : "";
631
+ if (text && text === lastPartialText) return;
632
+ lastPartialText = text;
624
633
  streamingLastPartialMs = Date.now();
625
634
  }
626
635
 
@@ -922,7 +931,15 @@ function forceKillSox(logger) {
922
931
  } catch {}
923
932
  }
924
933
 
925
- function startRecording(kv, backend, toast, logger, trimSilence = true) {
934
+ function startRecording(
935
+ kv,
936
+ backend,
937
+ toast,
938
+ logger,
939
+ trimSilence = true,
940
+ silenceStopSec = null,
941
+ onStopped = null,
942
+ ) {
926
943
  if (soxProc) {
927
944
  logger?.log("STT", "Start recording skipped: sox already running", "debug");
928
945
  return;
@@ -940,11 +957,16 @@ function startRecording(kv, backend, toast, logger, trimSilence = true) {
940
957
  const spawnT0 = Date.now();
941
958
  logger?.log(
942
959
  "STT",
943
- `Starting recording backend=${backend} mic=${mic || "system default"}`,
960
+ `Starting recording backend=${backend} mic=${mic || "system default"} silenceStop=${silenceStopSec ?? "off"}`,
944
961
  "debug",
945
962
  );
946
963
 
964
+ // Trim leading silence, then (optionally) auto-stop after N seconds of
965
+ // silence so hands-free modes (conversation) can finalize without a keypress.
947
966
  const silenceArgs = trimSilence ? ["silence", "1", "0.05", "1%"] : [];
967
+ if (silenceStopSec && silenceStopSec > 0) {
968
+ silenceArgs.push("1", String(silenceStopSec), "1%");
969
+ }
948
970
  soxProc = spawn(
949
971
  "sox",
950
972
  [...inputArgs, "-r", "16000", "-c", "1", "-b", "16", wavFile, ...silenceArgs],
@@ -991,7 +1013,15 @@ function startRecording(kv, backend, toast, logger, trimSilence = true) {
991
1013
  "error",
992
1014
  );
993
1015
  } else {
994
- toast("Recording stopped unexpectedly", "warning");
1016
+ // Normal exit (including silence auto-stop): fire the onStopped
1017
+ // callback if registered, otherwise show the generic toast.
1018
+ if (typeof onStopped === "function") {
1019
+ try {
1020
+ onStopped();
1021
+ } catch {}
1022
+ } else {
1023
+ toast("Recording stopped unexpectedly", "warning");
1024
+ }
995
1025
  }
996
1026
  }
997
1027
  });
@@ -1701,6 +1731,12 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1701
1731
  const client = api.client;
1702
1732
  const renderer = opts?.focusMode === "primary" ? null : api.renderer;
1703
1733
  const preview = api.voicePreview;
1734
+ // Seed engine from opts if the host delivered it.
1735
+ try {
1736
+ if (opts?.ttsEngine && kv.get("tts.engine") == null) {
1737
+ kv.set("tts.engine", opts.ttsEngine);
1738
+ }
1739
+ } catch {}
1704
1740
  __setToastPolling(!preview);
1705
1741
  const bootMode = resolveSttMode(opts, kv);
1706
1742
  let modeSource = "default-batch";
@@ -1790,9 +1826,23 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1790
1826
 
1791
1827
  // Start a recording for the voice-conversation loop. Returns false when busy.
1792
1828
 
1829
+ // Conversation-mode silence auto-finalize: when set, the recording uses
1830
+ // sox silence detection to auto-stop after this many seconds of silence,
1831
+ // and the onStopped callback fires. Default null = manual stop only.
1832
+ let conversationSilenceStopSec = null;
1833
+ let conversationOnStopped = null;
1834
+
1793
1835
  function startLoopRecording() {
1794
1836
  if (recording || processing || streamingDictationActive) return false;
1795
- startRecording(kv, backend, toast, logger, opts?.trimSilence);
1837
+ startRecording(
1838
+ kv,
1839
+ backend,
1840
+ toast,
1841
+ logger,
1842
+ opts?.trimSilence,
1843
+ conversationSilenceStopSec,
1844
+ conversationOnStopped,
1845
+ );
1796
1846
  if (recording) {
1797
1847
  showRecordingToast();
1798
1848
  // Fetch title + recent turns while the user speaks, so the context is
@@ -1837,6 +1887,67 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1837
1887
  let streamEditor = null;
1838
1888
  let streamTarget = null;
1839
1889
  let streamStarting = false;
1890
+ // Auto-end-of-speech: when enabled, a timer checks if no new partial has
1891
+ // arrived for AUTO_END_SILENCE_MS and triggers finalize automatically.
1892
+ let streamAutoEndTimer = null;
1893
+ const AUTO_END_SILENCE_MS = 3000;
1894
+
1895
+ function enableStreamAutoEnd() {
1896
+ disableStreamAutoEnd();
1897
+ streamAutoEndTimer = setInterval(() => {
1898
+ if (!streamingDictationActive || !streamController) {
1899
+ disableStreamAutoEnd();
1900
+ return;
1901
+ }
1902
+ // Only act if auto-end is actually enabled.
1903
+ if (!preview?.getAutoEndOfSpeech?.()) return;
1904
+ // Audio-level silence check: if the mic is picking up nothing,
1905
+ // the user has stopped speaking regardless of what whisper returns.
1906
+ let audioSilent = false;
1907
+ let rms = 0;
1908
+ try {
1909
+ const pcm = streamController.peekPcm?.(STREAM_WAVE_BYTES);
1910
+ if (pcm && pcm.length >= 2) {
1911
+ let sum = 0;
1912
+ const samples = Math.floor(pcm.length / 2);
1913
+ for (let i = 0; i + 1 < pcm.length; i += 2) {
1914
+ const s = pcm.readInt16LE(i) / 32768;
1915
+ sum += s * s;
1916
+ }
1917
+ rms = Math.sqrt(sum / samples);
1918
+ if (rms < 0.01) audioSilent = true;
1919
+ }
1920
+ } catch {}
1921
+ const last = streamingLastPartialMs;
1922
+ const elapsed = last ? Date.now() - last : 0;
1923
+ if (audioSilent && last && elapsed > AUTO_END_SILENCE_MS) {
1924
+ sttLog(
1925
+ "stream.autoEnd",
1926
+ `triggered by silence (audio+partial) rms=${rms.toFixed(4)} elapsed=${elapsed}ms`,
1927
+ );
1928
+ disableStreamAutoEnd();
1929
+ void finalizeStreamingDictation({ submit: preview?.getAutoSubmit?.() ?? false });
1930
+ return;
1931
+ }
1932
+ // Fallback: partial-based silence (no audio data available).
1933
+ if (last && elapsed > AUTO_END_SILENCE_MS * 2) {
1934
+ sttLog(
1935
+ "stream.autoEnd",
1936
+ `triggered by silence (partial only) rms=${rms.toFixed(4)} elapsed=${elapsed}ms`,
1937
+ );
1938
+ disableStreamAutoEnd();
1939
+ void finalizeStreamingDictation({ submit: preview?.getAutoSubmit?.() ?? false });
1940
+ }
1941
+ }, 500);
1942
+ if (typeof streamAutoEndTimer.unref === "function") streamAutoEndTimer.unref();
1943
+ }
1944
+
1945
+ function disableStreamAutoEnd() {
1946
+ if (streamAutoEndTimer) {
1947
+ clearInterval(streamAutoEndTimer);
1948
+ streamAutoEndTimer = null;
1949
+ }
1950
+ }
1840
1951
  // Warm server lease retained across dictations: the idle controller (mic
1841
1952
  // fully stopped, server still loaded) parks here between sessions. At most
1842
1953
  // one warm lease ever exists; the next start reuses it when the model key
@@ -1928,6 +2039,54 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1928
2039
  if (preview) await preview.begin();
1929
2040
  else editor.begin(target);
1930
2041
  sttLog("stream.begin", `preview=${preview ? "yes" : "no"}`);
2042
+ // Clickable pause lives on the preview line (v2 slot). The toggle
2043
+ // action funnels through the same pause/resume path as the command.
2044
+ try {
2045
+ preview?.setToggleAction?.(() => toggleStreamingPause());
2046
+ } catch {}
2047
+ // Restore persisted toggle states from kv so they survive restarts.
2048
+ try {
2049
+ const savedAutoSubmit = kv.get("stt.autoSubmit");
2050
+ if (savedAutoSubmit === true || savedAutoSubmit === false) {
2051
+ preview?.setAutoSubmit?.(savedAutoSubmit);
2052
+ }
2053
+ } catch {}
2054
+ try {
2055
+ const savedAutoEnd = kv.get("stt.autoEndOfSpeech");
2056
+ if (savedAutoEnd === true || savedAutoEnd === false) {
2057
+ preview?.setAutoEndOfSpeech?.(savedAutoEnd);
2058
+ }
2059
+ } catch {}
2060
+ try {
2061
+ preview?.setAutoSubmitToggleAction?.(() => {
2062
+ const next = !preview.getAutoSubmit?.();
2063
+ preview.setAutoSubmit?.(next);
2064
+ // Persist in kv so it survives restarts.
2065
+ try {
2066
+ kv.set("stt.autoSubmit", next);
2067
+ } catch {}
2068
+ sttLog("stream.autoSubmit", `toggled to ${next}`);
2069
+ });
2070
+ } catch {}
2071
+ try {
2072
+ preview?.setAutoEndToggleAction?.(() => {
2073
+ const next = !preview.getAutoEndOfSpeech?.();
2074
+ preview.setAutoEndOfSpeech?.(next);
2075
+ // Persist so it survives restarts.
2076
+ try {
2077
+ kv.set("stt.autoEndOfSpeech", next);
2078
+ } catch {}
2079
+ sttLog("stream.autoEnd", `toggled to ${next}`);
2080
+ // If recording is active, enable/disable silence auto-finalize.
2081
+ if (streamingDictationActive && streamController) {
2082
+ if (next) {
2083
+ enableStreamAutoEnd();
2084
+ } else {
2085
+ disableStreamAutoEnd();
2086
+ }
2087
+ }
2088
+ });
2089
+ } catch {}
1931
2090
  if (streamDisposed || myGen !== streamGeneration) {
1932
2091
  preview?.clear();
1933
2092
  return false;
@@ -1936,7 +2095,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1936
2095
  onPartial: (p) => {
1937
2096
  if (streamDisposed || myGen !== streamGeneration) return;
1938
2097
  try {
1939
- noteStreamingPartial();
2098
+ noteStreamingPartial(p);
1940
2099
  } catch {}
1941
2100
  try {
1942
2101
  ed.applyPartial(p);
@@ -2015,6 +2174,9 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2015
2174
  }
2016
2175
  return pcm ? waveLevelsFromPcm(pcm) : null;
2017
2176
  });
2177
+ // Auto-end: always run the timer; it checks the preview's toggle
2178
+ // state internally so toggling mid-recording works without restart.
2179
+ enableStreamAutoEnd();
2018
2180
  }
2019
2181
  // Sticky, never silent: covers the cold model load ("Loading…") until
2020
2182
  // the controller reports streaming, then live capture with elapsed
@@ -2053,6 +2215,60 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2053
2215
  }
2054
2216
  }
2055
2217
 
2218
+ // Pause/resume for live dictation: freezes the mic (SIGSTOP) and skips
2219
+ // recognition ticks so paused spans cost nothing and never enter the
2220
+ // transcript. The record key resumes a paused session instead of
2221
+ // finalizing it; explicit stop stays on /stt-stop.
2222
+ function toggleStreamingPause() {
2223
+ if (!streamingDictationActive || !streamController) return false;
2224
+ let pausedNow = false;
2225
+ try {
2226
+ pausedNow = !!streamController.getState?.().paused;
2227
+ } catch {
2228
+ return false;
2229
+ }
2230
+ if (pausedNow) {
2231
+ let ok = false;
2232
+ try {
2233
+ ok = streamController.resume() === true;
2234
+ } catch {
2235
+ ok = false;
2236
+ }
2237
+ if (ok) {
2238
+ preview?.setPaused?.(false);
2239
+ updateStreamingToast("● Streaming dictation — listening");
2240
+ sttLog("stream.resume", "ok");
2241
+ }
2242
+ return ok;
2243
+ }
2244
+ let ok = false;
2245
+ try {
2246
+ ok = streamController.pause?.() === true;
2247
+ } catch {
2248
+ ok = false;
2249
+ }
2250
+ if (ok) {
2251
+ preview?.setPaused?.(true);
2252
+ updateStreamingToast("⏸ Dictation paused — press record or click ▶ to resume");
2253
+ sttLog("stream.pause", "ok");
2254
+ }
2255
+ return ok;
2256
+ }
2257
+
2258
+ // Pause/resume entry point shared by the /stt-pause command. Returns false
2259
+ // with a hint toast when there is nothing pausable.
2260
+ async function pauseStreamingDictation() {
2261
+ if (!streamingDictationActive || !streamController) {
2262
+ toast("No streaming dictation in progress", "warning");
2263
+ return false;
2264
+ }
2265
+ if (!toggleStreamingPause()) {
2266
+ toast("Pause unavailable right now - use /stt-stop to cancel", "warning");
2267
+ return false;
2268
+ }
2269
+ return true;
2270
+ }
2271
+
2056
2272
  // Finalize: stop the controller (single tail transcription, no LLM),
2057
2273
  // commit the editor range, then insert EXACTLY once via the focused-input
2058
2274
  // helper. Never falls through to primary-chat appendPrompt. When `submit`
@@ -2064,6 +2280,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2064
2280
  async function finalizeStreamingDictation({ submit = false } = {}) {
2065
2281
  if (!streamingDictationActive || !streamController) return null;
2066
2282
  sttLog("stream.finalize", `submit=${submit}`);
2283
+ disableStreamAutoEnd();
2067
2284
  const controller = streamController;
2068
2285
  const editor = streamEditor;
2069
2286
  const target = streamTarget;
@@ -2209,6 +2426,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2209
2426
  async function cancelStreamingDictation() {
2210
2427
  streamGeneration += 1;
2211
2428
  sttLog("stream.cancel");
2429
+ disableStreamAutoEnd();
2212
2430
  preview?.clear();
2213
2431
  if (preview && processing) cancelRecording(logger);
2214
2432
  if (finalizingController) {
@@ -2284,6 +2502,10 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2284
2502
  isProcessing: () => processing,
2285
2503
  isStreaming,
2286
2504
  start: startLoopRecording,
2505
+ setSilenceAutoFinalize: (sec, onStopped) => {
2506
+ conversationSilenceStopSec = sec && sec > 0 ? sec : null;
2507
+ conversationOnStopped = typeof onStopped === "function" ? onStopped : null;
2508
+ },
2287
2509
  setStopHint: (hint) => __setStopHint(hint),
2288
2510
  cancel: () => cancelRecording(logger),
2289
2511
  discard: () => discardTurn(),
@@ -2321,11 +2543,18 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2321
2543
  ...(kb("stt.record") ? { keybind: kb("stt.record") } : {}),
2322
2544
  slash: { name: "stt-record" },
2323
2545
  onSelect() {
2324
- // Streaming mode: toggle streaming record/finalize. Batch default
2325
- // below is behavior-identical to before.
2546
+ // Streaming mode: toggle streaming record/finalize, or resume a
2547
+ // paused session (cheaper than re-speaking; explicit stop stays on
2548
+ // /stt-stop). Batch default below is behavior-identical to before.
2326
2549
  sttLog("toggle", `sttMode=${currentSttMode()} streamingActive=${streamingDictationActive}`);
2327
2550
  if (currentSttMode() === "streaming") {
2328
2551
  if (streamingDictationActive) {
2552
+ try {
2553
+ if (streamController?.getState?.().paused) {
2554
+ toggleStreamingPause();
2555
+ return;
2556
+ }
2557
+ } catch {}
2329
2558
  void finalizeStreamingDictation({ submit: false });
2330
2559
  return;
2331
2560
  }
@@ -2473,6 +2702,22 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2473
2702
  }
2474
2703
  },
2475
2704
  },
2705
+ {
2706
+ title: "STT: pause / resume streaming dictation",
2707
+ value: "stt.pause",
2708
+ category: "opencode-voice",
2709
+ description: "Freeze the mic mid-dictation (skips noise); run again to resume",
2710
+ slash: { name: "stt-pause" },
2711
+ onSelect() {
2712
+ // Batch mode has no pausable session (one-shot record); the mic
2713
+ // toggle is start/stop there.
2714
+ if (currentSttMode() !== "streaming" && !streamingDictationActive) {
2715
+ toast("Pause needs streaming mode - use /stt-mode to switch", "warning");
2716
+ return;
2717
+ }
2718
+ void pauseStreamingDictation();
2719
+ },
2720
+ },
2476
2721
  {
2477
2722
  title: "STT: select dictation mode",
2478
2723
  value: "stt.mode",