@todoforai/voiceloop 0.1.10 → 0.1.11

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/voice-agent.js +20 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@todoforai/voiceloop",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Fast, interruptible browser voice agent: VAD → STT → LLM → TTS with real barge-in, echo filtering, streaming first-sentence TTS and tap-to-seek. Zero runtime dependencies.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -21,7 +21,7 @@
21
21
  "index.html"
22
22
  ],
23
23
  "scripts": {
24
- "test": "node --test src/*.test.js"
24
+ "test": "node --test src/*.test.js bench/*.test.js bench/blackbox/energy.test.js"
25
25
  },
26
26
  "keywords": [
27
27
  "voice",
@@ -982,7 +982,15 @@ export class VoiceAgent {
982
982
  // call (llm.executesTools mode) must collapse too, or it would execute the tool a second time.
983
983
  const dup = calls.some((c) => callKey(c) === callKey(item) ||
984
984
  ((!c.id || !item.id) && c.tool === item.tool && callArgs(c) === callArgs(item)));
985
- if (!dup) { calls.push(item); self._runTool(item); }
985
+ if (!dup) {
986
+ calls.push(item); self._runTool(item);
987
+ // Late outcome: the turn already closed (barge-in mid-execution) and its ledger is
988
+ // written, so this result would reach the transcript chip but never the model. Relay it
989
+ // as a soft [TOOL RESULT] turn — the same channel out-of-band receipts use — so the next
990
+ // reply knows what the tool returned instead of seeing a call that vanished.
991
+ if (finalized && ('result' in item || 'error' in item))
992
+ self.notify(`[TOOL RESULT ${item.tool}] ${callArgs(item)}${ledgerOutcome(item)}`);
993
+ }
986
994
  continue;
987
995
  }
988
996
  if (self.state === 'thinking') self._set('speaking'); // first token → TTS stage
@@ -1641,7 +1649,7 @@ export class StreamingTTS {
1641
1649
  // predict()s, and presynth() below may fire while a previous reply's chain is still draining.
1642
1650
  // Returns a promise that RESOLVES (wav or null-on-error) — callers surface errors per-entry.
1643
1651
  _enqueueSynth(text, signal) {
1644
- const run = this._synthQ.then(() => this._synth(stripMd(text) || text, signal), () => null); // || raw: a pure-syntax chunk strips to empty — voice the raw rather than abort
1652
+ const run = this._synthQ.then(() => signal?.aborted ? null : this._synth(stripMd(text) || text, signal), () => null); // || raw: a pure-syntax chunk strips to empty — voice the raw rather than abort
1645
1653
  this._synthQ = run.catch(() => null);
1646
1654
  return run;
1647
1655
  }
@@ -1822,7 +1830,14 @@ export class StreamingTTS {
1822
1830
  catch (e) { if (e?.name !== 'AbortError') streamErr = e; } // LLM/iterator failure → surface via awaitEntry, not a silent truncation
1823
1831
  finally { streamDone = true; bump(); }
1824
1832
  })();
1825
- if (signal) signal.addEventListener('abort', bump, { once: true }); // wake a parked consumer the instant we abort
1833
+ // Stop waiting for an in-flight synth on abort, without releasing the serial synth queue:
1834
+ // Piper inference (and speculative clips without a signal) can finish only in their own time.
1835
+ let onAbort;
1836
+ const aborted = signal && new Promise((resolve) => { onAbort = () => { resolve(null); bump(); }; });
1837
+ if (signal) {
1838
+ signal.addEventListener('abort', onAbort, { once: true });
1839
+ if (signal.aborted) onAbort();
1840
+ }
1826
1841
  // Wait until the tape has an entry at `i` — or the stream ends, we abort, or a seek arrives (a
1827
1842
  // backward tap while we're parked waiting for a FUTURE clip must be honored now, not after the
1828
1843
  // stream advances). Producer runs ahead, so this usually returns immediately; it only blocks when
@@ -1859,7 +1874,7 @@ export class StreamingTTS {
1859
1874
  while (true) {
1860
1875
  if (idx >= tape.length) { await awaitEntry(idx); if (takePending()) continue; if (idx >= tape.length) break; } // wait for the producer to reach this sentence (stream end → done)
1861
1876
  const e = tape[idx];
1862
- const buf = await e.blobP;
1877
+ const buf = await (aborted ? Promise.race([e.blobP, aborted]) : e.blobP);
1863
1878
  // Barge-in while this clip was mid-synth: the abort cancels the synth fetch, which rejects with
1864
1879
  // AbortError ("signal is aborted without reason"). That's the cancellation we asked for, not a
1865
1880
  // failed render — stop quietly, exactly like an abort during playback.
@@ -1888,7 +1903,7 @@ export class StreamingTTS {
1888
1903
  idx++; startNw = 0; // natural end → next sentence
1889
1904
  }
1890
1905
  } finally {
1891
- signal?.removeEventListener('abort', bump);
1906
+ signal?.removeEventListener('abort', onAbort);
1892
1907
  // A finished stream is joined (it's already done — this just collects a trailing synth). An
1893
1908
  // ABORTED one is DETACHED: the producer sits in it.next() on the HOST's llm generator, which
1894
1909
  // may ignore the abort signal and never yield again, so awaiting it here would never return