@livx.cc/agentx 0.99.164 → 0.99.166

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/dist/index.js CHANGED
@@ -5231,12 +5231,17 @@ var Agent = class _Agent {
5231
5231
  * No model call; the transcript gains only that system message. Other models: nothing to warm. */
5232
5232
  async prewarm() {
5233
5233
  const o = this.options;
5234
- if (!o.model.startsWith("claude-code/") || !o.providerOptions?.claudeCodeSession) return;
5234
+ const api = o.model.startsWith("anthropic/") && o.promptCache;
5235
+ if (!api && (!o.model.startsWith("claude-code/") || !o.providerOptions?.claudeCodeSession)) return;
5235
5236
  await this.ensureFs();
5236
5237
  const systemPrompt = await this.prepare(), startCtx = await this.fireSessionStart();
5237
5238
  const sys = { role: "system", content: systemPrompt + (startCtx ? "\n\n" + startCtx : "") };
5238
5239
  if (this.transcript[0]?.role === "system") this.transcript[0] = sys;
5239
5240
  else this.transcript.unshift(sys);
5241
+ if (api) {
5242
+ await o.ai.chat({ model: o.model, messages: [sys, { role: "user", content: "." }], tools: toWireTools(this.activeTools), stream: false, maxTokens: 1, promptCache: true, ...o.providerOptions ? { providerOptions: o.providerOptions } : {} });
5243
+ return;
5244
+ }
5240
5245
  const po = this.claudeCodePo(), frag = reasoningToChatFragment(o.model, o.reasoning);
5241
5246
  const r = await o.ai.chat({ model: o.model, messages: this.trimContext(), tools: [], stream: true, ...frag, ...o.promptCache ? { promptCache: true } : {}, providerOptions: { ...frag.providerOptions, ...o.providerOptions, ...po, prewarm: true } });
5242
5247
  for await (const _ of r) {
@@ -8144,6 +8149,9 @@ var VoiceEngine = class _VoiceEngine {
8144
8149
  // bumps on every aside start/cancel — a late aside of a dead hold never plays
8145
8150
  speaking = false;
8146
8151
  // audible (deltas flowing OR audio draining)
8152
+ speechStartAt = 0;
8153
+ // clock time `speaking` last turned on / off
8154
+ speechEndAt = 0;
8147
8155
  ctxOpen = false;
8148
8156
  // the current TTS context still accepts deltas (false once end-frame sent)
8149
8157
  interrupted = false;
@@ -8341,11 +8349,13 @@ var VoiceEngine = class _VoiceEngine {
8341
8349
  this.handleUtterance(k.text, language ? { [language]: k.text.length } : void 0, k.turns);
8342
8350
  };
8343
8351
  this.stt.onLevel = (rms) => this.handleLevel(rms);
8352
+ this.stt.isAudible = (agoMs = 0) => this.speakingAt(this.clock.now() - agoMs);
8344
8353
  const stt = this.stt;
8345
8354
  this.agentText = stt.agentTextInput && stt.sendAgentText ? new AgentTextFeed({
8346
8355
  clock: this.clock,
8347
8356
  playedMs: () => Math.max(0, this.player.playedMs()),
8348
8357
  send: (text, end) => {
8358
+ this.diag("agent_text", { text: text.slice(0, 80), ...end ? { end: true } : {} });
8349
8359
  try {
8350
8360
  stt.sendAgentText(text, end);
8351
8361
  } catch (e) {
@@ -8437,7 +8447,10 @@ var VoiceEngine = class _VoiceEngine {
8437
8447
  }
8438
8448
  this.diag("tts_turn_begin", { ack, gapless: this.speaking });
8439
8449
  const gapless = this.speaking;
8440
- if (!this.speaking) this.player.markTurn();
8450
+ if (!this.speaking) {
8451
+ this.player.markTurn();
8452
+ this.speechStartAt = this.clock.now();
8453
+ }
8441
8454
  this.speaking = true;
8442
8455
  this.ctxOpen = true;
8443
8456
  this.spokeDeltas = false;
@@ -8662,6 +8675,7 @@ var VoiceEngine = class _VoiceEngine {
8662
8675
  }
8663
8676
  this.drainTimer = null;
8664
8677
  this.speaking = false;
8678
+ this.speechEndAt = this.clock.now();
8665
8679
  this.agentText?.end();
8666
8680
  this.stopWordReveal(true);
8667
8681
  if (this.turnStartAt) log15.debug(`turn: ${Math.round(this.clock.now() - this.turnStartAt)}ms (incl. playback)`);
@@ -8860,6 +8874,7 @@ var VoiceEngine = class _VoiceEngine {
8860
8874
  if (this.reply || droppedQueued) this.lastInterrupted = { full: this.reply, heard, exact, queued, queuedTurn: this.queuedTurn };
8861
8875
  if (heard.length < this.reply.length || droppedQueued) this.bargedSinceDispatch = true;
8862
8876
  this.speaking = false;
8877
+ this.speechEndAt = this.clock.now();
8863
8878
  this.ctxOpen = false;
8864
8879
  this.interrupted = true;
8865
8880
  this.suspectUntil = 0;
@@ -8969,6 +8984,14 @@ var VoiceEngine = class _VoiceEngine {
8969
8984
  novelWords(text) {
8970
8985
  return this.words(text).filter((w) => !this.echoWords.has(w));
8971
8986
  }
8987
+ /** Was the assistant speaking at clock time `t` (+ PHANTOM_TAIL_MS)? Narrower than echoActive's multi-second echo
8988
+ * tail on purpose: the STT phantom guard drops low-confidence short finals here, and a real "Yes." said right after
8989
+ * a question must not land inside it. speechEndAt already trails the audio by the settle margin (~300ms). */
8990
+ speakingAt(t) {
8991
+ if (t < this.speechStartAt) return false;
8992
+ return this.speaking || t < this.speechEndAt + _VoiceEngine.PHANTOM_TAIL_MS;
8993
+ }
8994
+ static PHANTOM_TAIL_MS = 300;
8972
8995
  echoActive() {
8973
8996
  return this.speaking || this.clock.now() < this.echoUntil;
8974
8997
  }
@@ -10038,7 +10061,18 @@ var SonioxSTTOptions = class {
10038
10061
  * same socket (`sendAgentText`, which VoiceEngine drives paced to playback), so Soniox filters the leftover echo
10039
10062
  * of our own voice out of the transcript. Complements AEC — keep echo cancellation on. */
10040
10063
  agentTextInput = false;
10064
+ /** Phantom guard: when its last word fell while the assistant was speaking (`isAudible`, wired by VoiceEngine), drop a
10065
+ * final of at most `phantomMaxWords` words, all listener tokens (`phantomTokens`), whose least-sure token is below
10066
+ * `phantomMinConfidence`. stt-rt-v6 makes up backchannels ("Okay.", "Yeah.", "Mm-hmm.") from echo residue under our
10067
+ * own voice: all at confMin 0.33-0.78 (agentx session 20261111-124649). Confidence alone can't separate them from a
10068
+ * real word said over our voice — `say` clips into live Soniox under a reply's residue: 12/19 soft interjections fell
10069
+ * below 0.8 — so words that act ("Stop.", "No.", "Yes.", "Wait.") always pass; a listener token over our own speech
10070
+ * is a no-op anyway. 0 = off. */
10071
+ phantomMinConfidence = 0.8;
10072
+ phantomMaxWords = 2;
10073
+ phantomTokens = /^(?:okay|ok|yeah|yep|yup|mm-?hmm|mhm|hmm+|mm+|uh-?huh|oh|ah|right)$/i;
10041
10074
  };
10075
+ var STD_TOKEN_KEYS = /* @__PURE__ */ new Set(["text", "start_ms", "end_ms", "confidence", "is_final", "language", "speaker"]);
10042
10076
  var SonioxSTT = class {
10043
10077
  options;
10044
10078
  ws;
@@ -10063,6 +10097,9 @@ var SonioxSTT = class {
10063
10097
  * loop). The host tears voice down instead of spinning forever. */
10064
10098
  onFatal = () => {
10065
10099
  };
10100
+ /** Was the assistant speaking `agoMs` ago (+ a short tail)? Set by VoiceEngine; gates the phantom guard, asked for the
10101
+ * moment the utterance's last word was said (not when its final landed, ~1s later). */
10102
+ isAudible = () => false;
10066
10103
  /** Diagnostic tap (provider errors, ws reconnects, no-audio watchdog). Fire-and-forget: a throwing
10067
10104
  * handler disables itself — never affects transcription. Wired by VoiceIO / the lab bridge. */
10068
10105
  onDiag = () => {
@@ -10097,6 +10134,12 @@ var SonioxSTT = class {
10097
10134
  finalToks = [];
10098
10135
  // diarization only: finalized tokens of the current utterance
10099
10136
  partialToks = [];
10137
+ uttToks = [];
10138
+ // stt_final diag: finalized tokens of the current utterance
10139
+ uttPartials = [];
10140
+ // stt_final diag: its current partial tokens
10141
+ levels = [];
10142
+ // stt_final diag: recent mic RMS per chunk (~12s)
10100
10143
  constructor(options) {
10101
10144
  this.options = { ...new SonioxSTTOptions(), ...options };
10102
10145
  }
@@ -10160,10 +10203,7 @@ var SonioxSTT = class {
10160
10203
  const combined = (this.finalText + this.partialText).trim();
10161
10204
  if (!combined || now() - this.lastChangeAt < this.options.silenceEndpointMs) return;
10162
10205
  if (this.firstTokenAt) log16.debug(`stt: ${Math.round(now() - this.firstTokenAt)}ms first-token\u2192silence-endpoint, "${combined.slice(0, 60)}"`);
10163
- const language = this.language(true);
10164
- const turns = this.turns(true), ago = this.lastWordAgoMs();
10165
- this.reset();
10166
- this.onUtterance(this.stray(combined), now(), language, turns, ago);
10206
+ this.emit(combined, "silence");
10167
10207
  }, 120);
10168
10208
  this.endpointTimer.unref?.();
10169
10209
  this.startedChunksAt = now();
@@ -10189,7 +10229,10 @@ var SonioxSTT = class {
10189
10229
  const v = view.getInt16(i, true);
10190
10230
  sum += v * v;
10191
10231
  }
10192
- this.onLevel(Math.sqrt(sum / (chunk.byteLength / 2)));
10232
+ const rms = Math.sqrt(sum / (chunk.byteLength / 2));
10233
+ this.levels.push({ at: this.lastChunkAt, rms });
10234
+ if (this.levels.length > 120) this.levels.shift();
10235
+ this.onLevel(rms);
10193
10236
  if (this.ws.readyState === WebSocket.OPEN) {
10194
10237
  this.ws.send(chunk);
10195
10238
  const at = this.sentBytes / 2 / STT_SAMPLE_RATE * 1e3;
@@ -10220,6 +10263,7 @@ var SonioxSTT = class {
10220
10263
  if (t.text !== "<end>" && typeof t.end_ms === "number" && String(t.text).trim() && t.end_ms > this.lastWordEndMs) this.lastWordEndMs = t.end_ms;
10221
10264
  if (t.text === "<end>") endpoint = true;
10222
10265
  else if (t.is_final) {
10266
+ this.uttToks.push(t);
10223
10267
  this.finalText += t.text;
10224
10268
  this.countLang(this.finalLangChars, t);
10225
10269
  if (this.options.diarization) this.finalToks.push(t);
@@ -10230,6 +10274,7 @@ var SonioxSTT = class {
10230
10274
  this.partialLangChars = {};
10231
10275
  for (const t of partials) this.countLang(this.partialLangChars, t);
10232
10276
  if (this.options.diarization) this.partialToks = partials;
10277
+ this.uttPartials = partials;
10233
10278
  const combined = this.finalText + this.partialText;
10234
10279
  if (combined !== this.lastCombined) {
10235
10280
  this.lastCombined = combined;
@@ -10240,10 +10285,32 @@ var SonioxSTT = class {
10240
10285
  if (endpoint && this.finalText.trim()) {
10241
10286
  const utterance = this.finalText.trim();
10242
10287
  if (this.firstTokenAt) log16.debug(`stt: ${Math.round(now() - this.firstTokenAt)}ms first-token\u2192endpoint, "${utterance.slice(0, 60)}"`);
10243
- const language = this.language(false);
10244
- const turns = this.turns(false), ago = this.lastWordAgoMs();
10245
- this.reset();
10246
- this.onUtterance(this.stray(utterance), now(), language, turns, ago);
10288
+ this.emit(utterance, "end");
10289
+ }
10290
+ }
10291
+ /** Close the current utterance: diag, reset, then hand it to onUtterance — unless the phantom guard drops it. */
10292
+ emit(text, endpoint) {
10293
+ const withPartials = endpoint === "silence";
10294
+ const language = this.language(withPartials);
10295
+ const turns = this.turns(withPartials), ago = this.lastWordAgoMs();
10296
+ const confMin = this.finalDiag(text, endpoint, ago);
10297
+ this.reset();
10298
+ if (this.phantom(text, confMin, ago)) {
10299
+ this.diag("stt_phantom_dropped", { text: text.slice(0, 80), confMin, lastWordAgoMs: ago });
10300
+ return;
10301
+ }
10302
+ this.onUtterance(this.stray(text), now(), language, turns, ago);
10303
+ }
10304
+ phantom(text, confMin, agoMs) {
10305
+ const { phantomMinConfidence: min, phantomMaxWords: max, phantomTokens: listener } = this.options;
10306
+ if (!min || confMin === void 0 || confMin >= min) return false;
10307
+ const words4 = text.split(/[\s,.!?…]+/).filter(Boolean);
10308
+ if (!words4.length || words4.length > max || !words4.every((w) => listener.test(w))) return false;
10309
+ try {
10310
+ return this.isAudible(agoMs ?? 0);
10311
+ } catch (e) {
10312
+ log16.warn(`isAudible threw: ${e instanceof Error ? e.message : e}`);
10313
+ return false;
10247
10314
  }
10248
10315
  }
10249
10316
  /** How long ago (ms, now) the utterance's last spoken word ended: the audio sent so far minus that token's end_ms on
@@ -10276,7 +10343,29 @@ var SonioxSTT = class {
10276
10343
  const words4 = sonioxWords(withPartials ? [...this.finalToks, ...this.partialToks] : this.finalToks);
10277
10344
  return words4.some((w) => w.spk != null) ? speakerRuns(words4) : void 0;
10278
10345
  }
10346
+ /** One `stt_final` diag per utterance: how sure Soniox was, the mic level over it, and any token fields beyond the
10347
+ * usual ones — to tell words the user said from ones the STT made up (a phantom "Okay." heard 186× over our own
10348
+ * speech, agentx session 20261111-105859). */
10349
+ finalDiag(text, endpoint, lastWordAgoMs) {
10350
+ const toks = [...this.uttToks, ...endpoint === "silence" ? this.uttPartials : []].filter((t) => String(t.text).trim());
10351
+ const conf = toks.map((t) => t.confidence).filter((c) => typeof c === "number");
10352
+ const since = (this.firstTokenAt || now()) - 1500;
10353
+ const rms = this.levels.filter((l) => l.at >= since).map((l) => l.rms).sort((a, b) => a - b);
10354
+ const extra = [...new Set(toks.flatMap((t) => Object.keys(t).filter((k) => !STD_TOKEN_KEYS.has(k))))];
10355
+ this.diag("stt_final", {
10356
+ text: text.slice(0, 80),
10357
+ endpoint,
10358
+ lastWordAgoMs,
10359
+ ...conf.length ? { confMin: +Math.min(...conf).toFixed(3), confAvg: +(conf.reduce((a, b) => a + b, 0) / conf.length).toFixed(3) } : {},
10360
+ ...rms.length ? { rmsPeak: Math.round(rms[rms.length - 1]), rmsMed: Math.round(rms[rms.length >> 1]) } : {},
10361
+ ...toks.length ? { startMs: toks[0].start_ms, endMs: toks[toks.length - 1].end_ms } : {},
10362
+ ...extra.length ? { tokenKeys: extra } : {}
10363
+ });
10364
+ return conf.length ? Math.min(...conf) : void 0;
10365
+ }
10279
10366
  reset() {
10367
+ this.uttToks = [];
10368
+ this.uttPartials = [];
10280
10369
  this.finalToks = [];
10281
10370
  this.partialToks = [];
10282
10371
  this.finalLangChars = {};
@@ -10379,7 +10468,7 @@ var now2 = () => performance.now();
10379
10468
  var CartesiaTTSOptions = class {
10380
10469
  auth = "";
10381
10470
  voiceId = "";
10382
- model = "sonic-3.5";
10471
+ model = "sonic-3.6";
10383
10472
  /** 'apiKey' (server/CLI) → `api_key=` URL param; 'token' (browser, BE-minted) → `access_token=`. */
10384
10473
  authMode = "apiKey";
10385
10474
  /** Cartesia `language` code ('en', 'he', …). Unset → field omitted (provider default). Mutable:
@@ -10481,6 +10570,16 @@ var CartesiaTTS = class _CartesiaTTS {
10481
10570
  };
10482
10571
  this.ws.onmessage = (ev) => {
10483
10572
  const m = JSON.parse(String(ev.data));
10573
+ const pr = m.context_id && this.priming.get(m.context_id);
10574
+ if (pr) {
10575
+ if (m.type === "chunk" && m.data) pr.chunks.push(base64ToBytes(m.data));
10576
+ else if (m.type === "done" || m.type === "error") {
10577
+ this.priming.delete(m.context_id);
10578
+ if (m.type === "done") this.primed.set(pr.key, pr.chunks);
10579
+ else this.primed.delete(pr.key);
10580
+ }
10581
+ return;
10582
+ }
10484
10583
  if (m.context_id && m.context_id !== this.ctxId) return;
10485
10584
  if (m.type === "chunk" && m.data) {
10486
10585
  this.consecutiveErrors = 0;
@@ -10538,7 +10637,40 @@ var CartesiaTTS = class _CartesiaTTS {
10538
10637
  this.newContext();
10539
10638
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame("Ok.", false));
10540
10639
  }
10640
+ /** Phrases synthesized ahead (prime): key → audio (null = still generating). */
10641
+ primed = /* @__PURE__ */ new Map();
10642
+ priming = /* @__PURE__ */ new Map();
10643
+ /** This context began with a primed phrase: its audio played from the cache; more text is synthesized after it. */
10644
+ ctxCached = false;
10645
+ ctxRest = "";
10646
+ ctxSent = false;
10647
+ static key(t) {
10648
+ return t.trim().replace(/[\s.,!?…]+$/u, "").replace(/\s+/g, " ");
10649
+ }
10650
+ /** Synthesize `text` now, in this voice and language, and keep its audio: a later utterance that is exactly this text
10651
+ * plays at once, with no TTS round trip (a call's planned opener — live: ~470ms to its first audio). */
10652
+ prime(text) {
10653
+ const key = _CartesiaTTS.key(text);
10654
+ if (!key || this.primed.has(key) || this.down) return;
10655
+ this.primed.set(key, null);
10656
+ const id = `prime-${++this.ctxSeq}`;
10657
+ const send = () => {
10658
+ const cur = this.ctxId, lang = this.ctxLanguage;
10659
+ this.ctxId = id;
10660
+ this.ctxLanguage = this.options.language;
10661
+ const f = this.frame(text, false);
10662
+ this.ctxId = cur;
10663
+ this.ctxLanguage = lang;
10664
+ this.priming.set(id, { key, chunks: [] });
10665
+ this.ws.send(f);
10666
+ };
10667
+ if (this.ws?.readyState === WebSocket.OPEN) send();
10668
+ else void this.ensureConnected().then(() => this.ws?.readyState === WebSocket.OPEN && send());
10669
+ }
10541
10670
  newContext() {
10671
+ this.ctxCached = false;
10672
+ this.ctxRest = "";
10673
+ this.ctxSent = false;
10542
10674
  this.ctxId = `ctx-${++this.ctxSeq}`;
10543
10675
  this.firstAudioAt = 0;
10544
10676
  this.ctxLanguage = this.options.language;
@@ -10564,6 +10696,19 @@ var CartesiaTTS = class _CartesiaTTS {
10564
10696
  if (this.down) return;
10565
10697
  text = cont ? this.say.feed(text) : this.say.feed(text) + this.say.flush();
10566
10698
  if (cont && !text) return;
10699
+ const hit = !this.firstAudioAt && !this.ctxCached && this.primed.get(_CartesiaTTS.key(text));
10700
+ if (hit) {
10701
+ this.ctxCached = true;
10702
+ this.firstAudioAt = now2();
10703
+ this.diag("tts_primed", { chars: text.length });
10704
+ for (const c of hit) this.onAudio(c);
10705
+ return;
10706
+ }
10707
+ if (this.ctxCached) {
10708
+ this.ctxRest += text;
10709
+ return;
10710
+ }
10711
+ this.ctxSent = true;
10567
10712
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame(text, cont));
10568
10713
  else void this.ensureConnected().then(() => this.ws?.readyState === WebSocket.OPEN && this.ws.send(this.frame(text, cont)));
10569
10714
  }
@@ -10577,11 +10722,27 @@ var CartesiaTTS = class _CartesiaTTS {
10577
10722
  this.ws.send(JSON.stringify({ ...JSON.parse(this.frame("", true)), flush: true }));
10578
10723
  }
10579
10724
  end() {
10580
- const held = this.say.flush();
10725
+ let held = this.say.flush();
10581
10726
  if (this.down) {
10582
10727
  this.onDone();
10583
10728
  return;
10584
10729
  }
10730
+ const hit = !this.ctxSent && !this.ctxCached && !this.firstAudioAt && this.primed.get(_CartesiaTTS.key(held));
10731
+ if (hit) {
10732
+ this.diag("tts_primed", { chars: held.length });
10733
+ this.firstAudioAt = now2();
10734
+ for (const c of hit) this.onAudio(c);
10735
+ queueMicrotask(() => this.onDone());
10736
+ return;
10737
+ }
10738
+ if (this.ctxCached) {
10739
+ held = (this.ctxRest + held).trim();
10740
+ this.ctxRest = "";
10741
+ if (!held) {
10742
+ queueMicrotask(() => this.onDone());
10743
+ return;
10744
+ }
10745
+ }
10585
10746
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame(held, false));
10586
10747
  }
10587
10748
  cancel() {