@bojackduy/opencode-voice 0.10.1 → 0.11.1

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
@@ -1371,12 +1371,15 @@ export function insertIntoFocusedInput(renderer, text, submit = false) {
1371
1371
  }
1372
1372
  }
1373
1373
 
1374
- async function appendTranscription(client, renderer, text, submit) {
1374
+ async function appendTranscription(client, renderer, text, submit, isCurrent) {
1375
1375
  if (insertIntoFocusedInput(renderer, text, submit)) {
1376
- return;
1376
+ return true;
1377
1377
  }
1378
1378
 
1379
- let appendResult = await client.tui.appendPrompt({ body: { text } });
1379
+ // Only the submit-only adapter consumes this fence; v1 request bodies
1380
+ // remain unchanged. Re-check after an asynchronous home session create.
1381
+ const fence = client.tui.submitOnly ? { isCurrent } : {};
1382
+ let appendResult = await client.tui.appendPrompt({ body: { text }, ...fence });
1380
1383
 
1381
1384
  if (appendResult?.error?.data?.message === "Expected object, got undefined") {
1382
1385
  appendResult = await client.tui.appendPrompt({ text });
@@ -1391,6 +1394,7 @@ async function appendTranscription(client, renderer, text, submit) {
1391
1394
  if (submit) {
1392
1395
  await client.tui.submitPrompt();
1393
1396
  }
1397
+ return appendResult;
1394
1398
  }
1395
1399
 
1396
1400
  // Transcribe the finished recording and normalize it, without touching the
@@ -1481,8 +1485,9 @@ async function transcribeTurn(kv, complete, client, api, toast, systemPrompt, lo
1481
1485
  }
1482
1486
 
1483
1487
  async function submitTurnText(client, renderer, toast, text, logger) {
1488
+ const myGen = pipelineGeneration;
1484
1489
  try {
1485
- await appendTranscription(client, renderer, text, true);
1490
+ await appendTranscription(client, renderer, text, true, () => myGen === pipelineGeneration);
1486
1491
  clearProcessingToast();
1487
1492
  logger?.log("STT", `Turn submitted chars=${text.length}`, "debug");
1488
1493
  toast("Transcription submitted", "success");
@@ -1499,11 +1504,27 @@ async function submitTurnText(client, renderer, toast, text, logger) {
1499
1504
  }
1500
1505
 
1501
1506
  async function appendTurnText(client, renderer, toast, text, logger) {
1507
+ const myGen = pipelineGeneration;
1502
1508
  try {
1503
- await appendTranscription(client, renderer, text, false);
1509
+ const appendResult = await appendTranscription(
1510
+ client,
1511
+ renderer,
1512
+ text,
1513
+ false,
1514
+ () => myGen === pipelineGeneration,
1515
+ );
1504
1516
  clearProcessingToast();
1505
1517
  logger?.log("STT", `Turn appended chars=${text.length}`, "debug");
1506
- toast("Transcription added to prompt", "success");
1518
+ // Submit-only hosts (OpenCode v2: no append-to-composer API) submit via
1519
+ // the client shim, which marks the result — so the toast must not claim
1520
+ // the text was "added to prompt" for editing there. v1 results carry no
1521
+ // marker and keep the original toast.
1522
+ toast(
1523
+ appendResult?.submitted === true
1524
+ ? "Transcription submitted as-is (submit-only mode)"
1525
+ : "Transcription added to prompt",
1526
+ "success",
1527
+ );
1507
1528
  return { text };
1508
1529
  } catch (err) {
1509
1530
  clearProcessingToast();
@@ -1579,6 +1600,10 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1579
1600
  }
1580
1601
  const client = api.client;
1581
1602
  const renderer = opts?.focusMode === "primary" ? null : api.renderer;
1603
+ const preview = api.voicePreview;
1604
+ let streamGeneration = 0;
1605
+ let streamDisposed = false;
1606
+ let finalizingController = null;
1582
1607
  const systemPrompt = selectSttSystemPrompt(sttNormalizeMode, prompts?.stt);
1583
1608
  const backend = detectAudioBackend();
1584
1609
  logger?.log("STT", `Audio backend=${backend}`, "debug");
@@ -1723,11 +1748,12 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1723
1748
  }
1724
1749
 
1725
1750
  function isStreaming() {
1726
- return streamingDictationActive;
1751
+ return streamingDictationActive || (preview && (streamStarting || !!finalizingController));
1727
1752
  }
1728
1753
 
1729
1754
  async function startStreamingDictation() {
1730
- if (streamingDictationActive || streamStarting) return false;
1755
+ if (streamingDictationActive || streamStarting || finalizingController || streamDisposed)
1756
+ return false;
1731
1757
  if (recording || processing) {
1732
1758
  toast("STT busy, please wait...");
1733
1759
  return false;
@@ -1741,6 +1767,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1741
1767
  return false;
1742
1768
  }
1743
1769
  streamStarting = true;
1770
+ const myGen = ++streamGeneration;
1744
1771
  // Model/language change tears down the warm lease first so the next
1745
1772
  // start loads the new model instead of reusing the stale server. The
1746
1773
  // key carries the bound server port, so a lease on a different port is
@@ -1758,13 +1785,21 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1758
1785
  const { createStreamingEditorAdapter } = await import("./streaming-editor.js");
1759
1786
  const focused = renderer?.currentFocusedRenderable || null;
1760
1787
  const target = focused && typeof focused.insertText === "function" ? focused : null;
1761
- const editor = createStreamingEditorAdapter({
1762
- toast: (message, variant) => api.ui.toast({ message, variant, duration: 2500 }),
1763
- getFocused: () => renderer?.currentFocusedRenderable || null,
1764
- });
1765
- editor.begin(target);
1788
+ const editor =
1789
+ preview ||
1790
+ createStreamingEditorAdapter({
1791
+ toast: (message, variant) => api.ui.toast({ message, variant, duration: 2500 }),
1792
+ getFocused: () => renderer?.currentFocusedRenderable || null,
1793
+ });
1794
+ if (preview) await preview.begin();
1795
+ else editor.begin(target);
1796
+ if (streamDisposed || myGen !== streamGeneration) {
1797
+ preview?.clear();
1798
+ return false;
1799
+ }
1766
1800
  const sessionCallbacks = (ed) => ({
1767
1801
  onPartial: (p) => {
1802
+ if (streamDisposed || myGen !== streamGeneration) return;
1768
1803
  try {
1769
1804
  noteStreamingPartial();
1770
1805
  } catch {}
@@ -1835,6 +1870,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1835
1870
  logger?.log("STT", "Streaming dictation started", "debug");
1836
1871
  return true;
1837
1872
  } catch (err) {
1873
+ preview?.clear();
1838
1874
  logger?.log("STT", `Streaming start failed: ${err?.message || err}`, "error");
1839
1875
  try {
1840
1876
  clearStreamingToast();
@@ -1874,6 +1910,9 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1874
1910
  const controller = streamController;
1875
1911
  const editor = streamEditor;
1876
1912
  const target = streamTarget;
1913
+ const myGen = streamGeneration;
1914
+ finalizingController = controller;
1915
+ if (preview) processing = true;
1877
1916
  streamController = null;
1878
1917
  streamEditor = null;
1879
1918
  streamTarget = null;
@@ -1887,6 +1926,8 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1887
1926
  try {
1888
1927
  done = await controller.stop();
1889
1928
  } catch (err) {
1929
+ finalizingController = null;
1930
+ if (preview) processing = false;
1890
1931
  logger?.log("STT", `Streaming stop failed: ${err?.message || err}`, "error");
1891
1932
  try {
1892
1933
  clearStreamingToast();
@@ -1897,12 +1938,20 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1897
1938
  retainWarmStreamController(controller);
1898
1939
  return null;
1899
1940
  }
1941
+ finalizingController = null;
1942
+ if (streamDisposed || myGen !== streamGeneration) {
1943
+ preview?.clear();
1944
+ if (preview) processing = false;
1945
+ await controller.dispose?.();
1946
+ return null;
1947
+ }
1900
1948
  // The stop-tail resolved: the sticky status served its purpose (no
1901
1949
  // silent gap during the wait). Clear before the terminal toasts below.
1902
1950
  try {
1903
1951
  clearStreamingToast();
1904
1952
  } catch {}
1905
1953
  if (done?.error && isStreamingServerFault(done.error)) {
1954
+ if (preview) processing = false;
1906
1955
  // Server at fault (not ready / transcribe failure): release the lease
1907
1956
  // so the next start cold-loads from a clean handle instead of reusing
1908
1957
  // a dead server.
@@ -1930,10 +1979,20 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1930
1979
  // Every path below retains the warm lease: the mic stopped, the server
1931
1980
  // stays loaded for the next dictation.
1932
1981
  if (!text) {
1982
+ if (preview) processing = false;
1933
1983
  toast("No speech detected", "warning");
1934
1984
  retainWarmStreamController(controller);
1935
1985
  return { text: "" };
1936
1986
  }
1987
+ if (preview) {
1988
+ // v2 has a visible preview but no composer append. Finalize submits
1989
+ // once through the same home/create/cancellation fence as batch STT.
1990
+ processing = true;
1991
+ const result = await submitTurnText(client, null, toast, text, logger);
1992
+ if (!streamDisposed && myGen === streamGeneration) retainWarmStreamController(controller);
1993
+ else await controller.dispose?.();
1994
+ return { ...result, submitted: !result.error };
1995
+ }
1937
1996
  if (fin?.inserted) {
1938
1997
  logger?.log("STT", `Streaming finalized via owned range chars=${text.length}`, "debug");
1939
1998
  if (submit || autoSubmit) {
@@ -1997,6 +2056,18 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
1997
2056
  // is fully stopped by cancel(); the server lease is retained warm for the
1998
2057
  // next dictation (user cancel is per-dictation, never a server fault).
1999
2058
  async function cancelStreamingDictation() {
2059
+ streamGeneration += 1;
2060
+ preview?.clear();
2061
+ if (preview && processing) cancelRecording(logger);
2062
+ if (finalizingController) {
2063
+ const finishing = finalizingController;
2064
+ finalizingController = null;
2065
+ cancelRecording(logger);
2066
+ await finishing.cancel();
2067
+ processing = false;
2068
+ clearStreamingToast();
2069
+ return true;
2070
+ }
2000
2071
  if (!streamingDictationActive || !streamController) return false;
2001
2072
  const controller = streamController;
2002
2073
  const editor = streamEditor;
@@ -2021,6 +2092,14 @@ export function registerSTT(api, kv, complete, prompts, opts, logger, deps = {})
2021
2092
  }
2022
2093
 
2023
2094
  api.lifecycle?.onDispose?.(() => {
2095
+ streamDisposed = true;
2096
+ streamGeneration += 1;
2097
+ preview?.clear();
2098
+ if (finalizingController) {
2099
+ const finishing = finalizingController;
2100
+ finalizingController = null;
2101
+ void finishing.dispose?.();
2102
+ }
2024
2103
  // Unload mid-recording must not orphan the sox child: cancel kills it,
2025
2104
  // bumps the pipeline generation (late transcriptions are dropped), and
2026
2105
  // clears the sticky toast. processing is not owned by cancel, reset it
package/lib/tts.js CHANGED
@@ -209,7 +209,7 @@ Rules:
209
209
 
210
210
  // ---- Session helpers ----
211
211
 
212
- async function getTurnAssistantText(client, api) {
212
+ export async function getTurnAssistantText(client, api) {
213
213
  const route = api.route.current;
214
214
  if (route.name !== "session") return null;
215
215