@todoforai/voiceloop 0.1.15 → 0.1.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@todoforai/voiceloop",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
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",
package/src/index.d.ts CHANGED
@@ -131,8 +131,8 @@ export interface VoiceAgentOptions {
131
131
  /** LLM model id sent to the backend `/llm` route; empty → the backend's VOICE_MODEL default. */
132
132
  model?: string;
133
133
  llm?: LLM;
134
- /** Transport seam for the built-in LLM adapter; defaults to global fetch. For tests and hosts
135
- * that must wrap every request (auth refresh, tracing). Unused when you pass your own `llm`. */
134
+ /** Transport seam for the built-in LLM adapter AND the STT usage reports; defaults to global
135
+ * fetch. For tests and hosts that must wrap every request (auth refresh, tracing). */
136
136
  fetchFn?: typeof fetch;
137
137
  apiKey?: string;
138
138
  /** Full endpoint URLs, not a base: voiceloop composes no routes, so a host can point the LLM and
package/src/stt.js CHANGED
@@ -48,7 +48,7 @@ const newSocket = (url, protocols) => {
48
48
  };
49
49
 
50
50
  // Common opts shape (provided by the VoiceAgent):
51
- // { apiKey, sttUrl, sttModel, sttLang, keyterms, sttTokenUrl, getToken, sttUsageUrl,
51
+ // { apiKey, sttUrl, sttModel, sttLang, keyterms, sttTokenUrl, getToken, sttUsageUrl, fetchFn (usage POSTs),
52
52
  // onPartial, onFinal, onError, onClose, isClosed }
53
53
  //
54
54
  // AUTH (cloud providers): the raw provider key must stay server-side. Either
@@ -107,12 +107,12 @@ async function mintSttToken(url, apiKey, onFatal, getToken) {
107
107
  // Accumulate streamed samples and flush them to /stt/usage — inline once USAGE_FLUSH_SECONDS is
108
108
  // crossed, and again whenever the caller flushes on close (billing is trust-but-verify: the client
109
109
  // reports what it streamed since the backend can't meter a direct browser↔provider socket).
110
- function makeUsageReporter(sttUsageUrl, apiKey, provider, extra = {}) {
110
+ function makeUsageReporter(sttUsageUrl, apiKey, provider, extra = {}, fetchFn = fetch) {
111
111
  let samples = 0;
112
112
  const flush = () => {
113
113
  const seconds = samples / 16000; samples = 0;
114
114
  if (seconds <= 0 || !sttUsageUrl) return;
115
- fetch(sttUsageUrl, {
115
+ fetchFn(sttUsageUrl, {
116
116
  method: 'POST', keepalive: true,
117
117
  headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey },
118
118
  body: JSON.stringify({ seconds, provider, ...extra }),
@@ -133,7 +133,7 @@ export function makeElevenLabsSTT(opts) {
133
133
  const sttUrl = opts.sttUrl || 'wss://api.elevenlabs.io/v1/speech-to-text/realtime';
134
134
  const sttModel = opts.sttModel || 'scribe_v2_realtime';
135
135
  let ws = null, opening = false, outbox = [], sentSinceCommit = false, sttStart = 0;
136
- const usage = makeUsageReporter(sttUsageUrl, apiKey, 'elevenlabs');
136
+ const usage = makeUsageReporter(sttUsageUrl, apiKey, 'elevenlabs', {}, opts.fetchFn);
137
137
 
138
138
  const sendAudio = (i16) => {
139
139
  if (!ws || ws.readyState !== 1) { outbox.push(i16); return; }
@@ -277,7 +277,7 @@ export function makeSpeechmaticsSTT(opts) {
277
277
  flushFinal(true);
278
278
  }, ms); };
279
279
 
280
- const usage = makeUsageReporter(sttUsageUrl, apiKey, 'speechmatics', { model: sttModel || 'standard' });
280
+ const usage = makeUsageReporter(sttUsageUrl, apiKey, 'speechmatics', { model: sttModel || 'standard' }, opts.fetchFn);
281
281
 
282
282
  const sendAudio = (i16) => {
283
283
  if (!ws || ws.readyState !== 1 || !started) { outbox.push(i16); return; }
@@ -485,7 +485,7 @@ export function makeDeepgramSTT(opts) {
485
485
  // TurnInfo.transcript is cumulative for the CURRENT turn (Flux restarts it on each new turn_index),
486
486
  // so `turnText` is simply the latest transcript — no committed/tail split needed.
487
487
  let turnText = '', audioSinceFinal = false, awaitingFinal = false, safetyTimer = null;
488
- const usage = makeUsageReporter(sttUsageUrl, apiKey, 'deepgram', { model });
488
+ const usage = makeUsageReporter(sttUsageUrl, apiKey, 'deepgram', { model }, opts.fetchFn);
489
489
 
490
490
  // Pre-open buffer is a bounded PREROLL (~1s), not a backlog: Flux is a real-time model — burst-
491
491
  // sending seconds of buffered audio after a slow token fetch/handshake makes its whole timeline lag
@@ -47,9 +47,10 @@ registerProcessor('capture-processor', class extends AudioWorkletProcessor {
47
47
  return true;
48
48
  }
49
49
  });`;
50
- const CAPTURE_WORKLET_URL = typeof URL !== 'undefined' && URL.createObjectURL
51
- ? URL.createObjectURL(new Blob([CAPTURE_WORKLET_SRC], { type: 'text/javascript' }))
52
- : '';
50
+ // Built lazily: React Native ships a URL.createObjectURL that throws ("not implemented"), and a
51
+ // native host (audioIO) never loads the worklet at all — so nothing may run at import time.
52
+ let captureWorkletUrl = null;
53
+ const CAPTURE_WORKLET_URL_GET = () => captureWorkletUrl ??= URL.createObjectURL(new Blob([CAPTURE_WORKLET_SRC], { type: 'text/javascript' }));
53
54
 
54
55
  // Compose the full system prompt: a persona (base voice persona by default, or a host-supplied one)
55
56
  // + the host's conversation context (if any).
@@ -202,7 +203,7 @@ export class VoiceAgent {
202
203
  // `getSttToken` is called with the provider actually running — the RESOLVED one, so a host
203
204
  // whose credential differs per provider never has to re-derive the webspeech downgrade
204
205
  // (that rule lives here, in resolveSttProvider, and must not be mirrored host-side).
205
- sttTokenUrl, getToken: getSttToken && (() => getSttToken(this.sttProvider)), sttUsageUrl, eotThreshold: sttEotThreshold,
206
+ sttTokenUrl, getToken: getSttToken && (() => getSttToken(this.sttProvider)), sttUsageUrl, fetchFn, eotThreshold: sttEotThreshold,
206
207
  onPartial: (text, ms, committed = '') => {
207
208
  const interim = `${committed} ${text}`.trim();
208
209
  this._turnText = interim; // latest interim (committed prefix + live tail) for the turn detector
@@ -438,7 +439,7 @@ export class VoiceAgent {
438
439
  try {
439
440
  ctx = new AudioContext({ sampleRate: 16000 });
440
441
  const src = ctx.createMediaStreamSource(stream);
441
- await ctx.audioWorklet.addModule(CAPTURE_WORKLET_URL);
442
+ await ctx.audioWorklet.addModule(CAPTURE_WORKLET_URL_GET());
442
443
  if (gen !== this._pipelineGen || this._closed) { stream.getTracks().forEach(t => t.stop()); ctx.close(); return; } // superseded/stopped while loading
443
444
  const node = new AudioWorkletNode(ctx, 'capture-processor');
444
445
  node.port.onmessage = e => this._onCapture(e.data); // Float32Array chunk from the audio thread