@livx.cc/agentx 0.99.145 → 0.99.147

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
@@ -5503,7 +5503,7 @@ var Agent = class _Agent {
5503
5503
  let lastProgressAt = start;
5504
5504
  const turnUser = this.transcript[this.transcript.length - 1];
5505
5505
  const kill = (finishReason) => {
5506
- log9.warn(`kill-switch: ${finishReason} (steps=${steps}, tokens=${usage.totalTokens}, budgetTokens=${Math.round(spent)}, ms=${Date.now() - start - this.parkedMs}${finishReason === "timeout" ? `, idle=${Date.now() - lastProgressAt - this.parkedMs}ms` : ""}${this.parkedMs ? ` +${this.parkedMs} parked` : ""})`);
5506
+ log9[finishReason === "aborted" ? "debug" : "warn"](`kill-switch: ${finishReason} (steps=${steps}, tokens=${usage.totalTokens}, budgetTokens=${Math.round(spent)}, ms=${Date.now() - start - this.parkedMs}${finishReason === "timeout" ? `, idle=${Date.now() - lastProgressAt - this.parkedMs}ms` : ""}${this.parkedMs ? ` +${this.parkedMs} parked` : ""})`);
5507
5507
  this.jobs.killAll(`run ended: ${finishReason}`, true);
5508
5508
  const claims = this.survivingClaims();
5509
5509
  return { text: lastAssistantText(this.transcript, turnUser), steps, finishReason, messages: this.transcript, usage, usageEstimated, ...finishReason === "budget" ? { budget: { spent: Math.round(spent), limit: o.maxTokens, stepPrompt } } : {}, ...claims.length ? { fabricatedToolClaims: claims } : {} };
@@ -7231,6 +7231,117 @@ init_llm();
7231
7231
  init_logging();
7232
7232
  init_models();
7233
7233
 
7234
+ // node_modules/@bod.ee/voice/src/core/types.ts
7235
+ var STT_SAMPLE_RATE = 16e3;
7236
+ var TTS_SAMPLE_RATE = 44100;
7237
+ async function resolveAuth(auth) {
7238
+ return typeof auth === "function" ? await auth() : auth;
7239
+ }
7240
+
7241
+ // node_modules/@bod.ee/voice/src/core/splice-sink.ts
7242
+ var BYTES_PER_MS = TTS_SAMPLE_RATE * 2 / 1e3;
7243
+ var toBytes = (ms) => Math.max(0, Math.round(ms * TTS_SAMPLE_RATE / 1e3)) * 2;
7244
+ var SpliceSinkOptions = class {
7245
+ /** The rest resumes this much before the pause point — a human re-enters a little before where they stopped. */
7246
+ backoffMs = 150;
7247
+ /** Tape kept behind the playhead (older audio is dropped; a splice only needs the unplayed rest + the backoff). */
7248
+ keepMs = 5e3;
7249
+ };
7250
+ var SpliceSink = class {
7251
+ constructor(sink, options) {
7252
+ this.sink = sink;
7253
+ this.options = { ...new SpliceSinkOptions(), ...options };
7254
+ }
7255
+ sink;
7256
+ options;
7257
+ tape = [];
7258
+ tapeStart = 0;
7259
+ // content bytes dropped from the head of `tape`
7260
+ /** Active splice: the sink's raw timeline = clip (clipMs) then the content from `fromMs`. */
7261
+ clipMs = 0;
7262
+ fromMs = 0;
7263
+ spliced = false;
7264
+ markTurn() {
7265
+ this.reset();
7266
+ this.sink.markTurn();
7267
+ }
7268
+ kill() {
7269
+ this.reset();
7270
+ this.sink.kill();
7271
+ }
7272
+ pause() {
7273
+ this.sink.pause?.();
7274
+ }
7275
+ resume() {
7276
+ this.sink.resume?.();
7277
+ }
7278
+ get canPause() {
7279
+ return !!this.sink.pause && !!this.sink.resume;
7280
+ }
7281
+ write(pcm) {
7282
+ this.tape.push(pcm);
7283
+ this.trim();
7284
+ this.sink.write(pcm);
7285
+ }
7286
+ drainMs() {
7287
+ return this.sink.drainMs();
7288
+ }
7289
+ /** Reply-content ms played: holds at the splice point while the clip plays. */
7290
+ playedMs() {
7291
+ const raw = this.sink.playedMs();
7292
+ return this.spliced ? Math.max(this.fromMs, raw - this.clipMs + this.fromMs) : raw;
7293
+ }
7294
+ /** Reply-content ms written so far (played + still queued) — exact even mid-clip (anchors a new sub-turn). */
7295
+ endMs() {
7296
+ const raw = Math.max(0, this.sink.playedMs() + this.sink.drainMs());
7297
+ return this.spliced ? Math.max(0, raw - this.clipMs + this.fromMs) : raw;
7298
+ }
7299
+ /** Play `clip` (s16le mono at TTS_SAMPLE_RATE) now, then the reply from where it was (less backoffMs). Unpauses a
7300
+ * paused sink (the flush does). Returns the content ms the rest resumes from. */
7301
+ splice(clip2) {
7302
+ const at = Math.max(0, this.playedMs() - this.options.backoffMs);
7303
+ const from = Math.max(at, this.tapeStart / BYTES_PER_MS);
7304
+ const rest = this.slice(toBytes(from));
7305
+ this.sink.kill();
7306
+ this.sink.write(clip2);
7307
+ if (rest.length) this.sink.write(rest);
7308
+ this.spliced = true;
7309
+ this.clipMs = clip2.length / BYTES_PER_MS;
7310
+ this.fromMs = from;
7311
+ return from;
7312
+ }
7313
+ slice(fromByte) {
7314
+ let off = this.tapeStart;
7315
+ const parts = [];
7316
+ for (const c of this.tape) {
7317
+ const end = off + c.length;
7318
+ if (end > fromByte) parts.push(off >= fromByte ? c : c.subarray(fromByte - off));
7319
+ off = end;
7320
+ }
7321
+ const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
7322
+ let i = 0;
7323
+ for (const p of parts) {
7324
+ out.set(p, i);
7325
+ i += p.length;
7326
+ }
7327
+ return out;
7328
+ }
7329
+ trim() {
7330
+ const keepFrom = toBytes(this.playedMs() - this.options.keepMs);
7331
+ while (this.tape.length > 1 && this.tapeStart + this.tape[0].length <= keepFrom) {
7332
+ const c = this.tape.shift();
7333
+ this.tapeStart += c.length;
7334
+ }
7335
+ }
7336
+ reset() {
7337
+ this.tape = [];
7338
+ this.tapeStart = 0;
7339
+ this.spliced = false;
7340
+ this.clipMs = 0;
7341
+ this.fromMs = 0;
7342
+ }
7343
+ };
7344
+
7234
7345
  // node_modules/@bod.ee/voice/src/core/logging.ts
7235
7346
  function consoleForComponent(name) {
7236
7347
  const debugEnv = typeof process !== "undefined" && process.env?.DEBUG || typeof localStorage !== "undefined" && localStorage.getItem("DEBUG") || "";
@@ -7365,7 +7476,11 @@ var ALIASES = {
7365
7476
  comedic: "joking"
7366
7477
  // Cartesia lists "joking/comedic" as one — canonicalize to joking
7367
7478
  };
7368
- var NONVERBAL = { laughter: "laughter", laughs: "laughter", laugh: "laughter", laughing: "laughter" };
7479
+ var LAUGH = "[laughter]";
7480
+ var BREATH = '<emotion value="scared"/>Hhh!';
7481
+ var NONVERBAL = { laughter: LAUGH, laughs: LAUGH, laugh: LAUGH, laughing: LAUGH, gasp: BREATH, gasps: BREATH, gasping: BREATH };
7482
+ var QUIET = '<volume ratio="0.5"/><speed ratio="0.8"/>';
7483
+ var DELIVERY = { quiet: QUIET, quietly: QUIET, soft: QUIET, softly: QUIET, whispers: QUIET };
7369
7484
  function normalizeEmotion(raw) {
7370
7485
  const k = raw.trim().toLowerCase();
7371
7486
  if (VALID.has(k)) return k;
@@ -7374,19 +7489,32 @@ function normalizeEmotion(raw) {
7374
7489
  function resolveTag(raw) {
7375
7490
  const k = raw.trim().toLowerCase();
7376
7491
  if (NONVERBAL[k]) return { kind: "nonverbal", value: NONVERBAL[k] };
7492
+ if (DELIVERY[k]) return { kind: "delivery", value: DELIVERY[k] };
7377
7493
  const e = normalizeEmotion(k);
7378
7494
  return e ? { kind: "emotion", value: e } : null;
7379
7495
  }
7380
7496
  var TAG_RE = /\[([a-zA-Z][a-zA-Z ]{0,24})\]/g;
7381
7497
  var PARTIAL_RE = /\[[a-zA-Z ]*$/;
7382
- var cartesiaTag = (t) => t.kind === "nonverbal" ? `[${t.value}]` : `<emotion value="${t.value}"/>`;
7498
+ var cartesiaTag = (t) => t.kind === "emotion" ? `<emotion value="${t.value}"/>` : t.value;
7499
+ function renderEmotions(text, opts = { show: true }) {
7500
+ const s = new EmotionStream(opts.show), a = s.feed(text), b = s.flush();
7501
+ return { speech: a.speech + b.speech, display: a.display + b.display, prose: a.prose + b.prose };
7502
+ }
7383
7503
  var EmotionStream = class {
7504
+ // that tag, due on the next words after a gasp; an emotion tag in between replaces it
7384
7505
  constructor(show = true) {
7385
7506
  this.show = show;
7386
7507
  }
7387
7508
  show;
7388
7509
  buf = "";
7389
- pending = null;
7510
+ pending = "";
7511
+ emotion = "neutral";
7512
+ // the last emotion tag: what the words after a gasp's `scared` go back to
7513
+ restore = "";
7514
+ /** A new TTS context opens mid-reply: an emotion tag ended with the old one, so the next words carry it again. */
7515
+ resume() {
7516
+ if (this.emotion !== "neutral") this.restore = cartesiaTag({ kind: "emotion", value: this.emotion });
7517
+ }
7390
7518
  feed(delta) {
7391
7519
  this.buf += delta;
7392
7520
  return this.drain(false);
@@ -7413,7 +7541,17 @@ var EmotionStream = class {
7413
7541
  });
7414
7542
  const tag = resolveTag(m[1]);
7415
7543
  if (tag) {
7416
- this.pending = tag;
7544
+ if (tag.kind === "nonverbal") {
7545
+ speech += this.pending + tag.value;
7546
+ this.pending = "";
7547
+ if (tag.value === BREATH) this.restore = cartesiaTag({ kind: "emotion", value: this.emotion });
7548
+ } else {
7549
+ if (tag.kind === "emotion") {
7550
+ this.emotion = tag.value;
7551
+ this.restore = "";
7552
+ }
7553
+ this.pending += cartesiaTag(tag);
7554
+ }
7417
7555
  if (this.show) display += m[0];
7418
7556
  } else log14.debug(`dropping unknown emotion tag ${m[0]}`);
7419
7557
  last = m.index + m[0].length;
@@ -7429,9 +7567,9 @@ var EmotionStream = class {
7429
7567
  emit(text, sink) {
7430
7568
  if (!text) return;
7431
7569
  let speech = text;
7432
- if (this.pending && /[\p{L}\p{N}]/u.test(text)) {
7433
- speech = cartesiaTag(this.pending) + text;
7434
- this.pending = null;
7570
+ if ((this.pending || this.restore) && /[\p{L}\p{N}]/u.test(text)) {
7571
+ speech = this.restore + this.pending + text;
7572
+ this.pending = this.restore = "";
7435
7573
  }
7436
7574
  sink(speech, text, text);
7437
7575
  }
@@ -7561,8 +7699,66 @@ function computeHoldMs(ctx) {
7561
7699
  return Math.max(floor, Math.min(MAX_HOLD_MS, ms));
7562
7700
  }
7563
7701
 
7702
+ // node_modules/@bod.ee/voice/src/core/speakers.ts
7703
+ function sonioxWords(toks) {
7704
+ const out = [];
7705
+ let cur = null;
7706
+ for (const t of toks) {
7707
+ if (/^\s/.test(t.text) || !cur) {
7708
+ if (cur) out.push(cur);
7709
+ cur = { w: "", t0: t.start_ms / 1e3, t1: t.end_ms / 1e3, conf: t.confidence ?? 1, ...t.speaker != null ? { spk: String(t.speaker) } : {}, ...t.language ? { lang: String(t.language) } : {} };
7710
+ }
7711
+ cur.w += t.text;
7712
+ cur.t1 = t.end_ms / 1e3;
7713
+ cur.conf = Math.min(cur.conf, t.confidence ?? 1);
7714
+ }
7715
+ if (cur) out.push(cur);
7716
+ return out.map((w) => ({ ...w, w: w.w.trim() })).filter((w) => w.w);
7717
+ }
7718
+ var SpeakerRunsOptions = class {
7719
+ /** A run shorter than this (a diarization flap) joins its neighbour — the previous run, or the next for a leading one. */
7720
+ minRunWords = 2;
7721
+ };
7722
+ function speakerRuns(words3, options) {
7723
+ const { minRunWords } = { ...new SpeakerRunsOptions(), ...options };
7724
+ let runs = [];
7725
+ for (const w of words3) {
7726
+ const last = runs[runs.length - 1];
7727
+ const spk = w.spk ?? last?.spk ?? words3.find((x) => x.spk != null)?.spk ?? "";
7728
+ if (last?.spk === spk) last.words.push(w);
7729
+ else runs.push({ spk, words: [w] });
7730
+ }
7731
+ while (runs.length > 1) {
7732
+ const i = runs.findIndex((r) => r.words.length < minRunWords);
7733
+ if (i < 0) break;
7734
+ const into = i === 0 ? runs[1] : runs[i - 1];
7735
+ if (i === 0) into.words.unshift(...runs[0].words);
7736
+ else into.words.push(...runs[i].words);
7737
+ runs.splice(i, 1);
7738
+ runs = runs.reduce((acc, r) => {
7739
+ const p = acc[acc.length - 1];
7740
+ if (p?.spk === r.spk) p.words.push(...r.words);
7741
+ else acc.push(r);
7742
+ return acc;
7743
+ }, []);
7744
+ }
7745
+ return runs.map((r) => ({
7746
+ speaker: r.spk,
7747
+ text: r.words.map((w) => w.w).join(" "),
7748
+ startMs: Math.round(r.words[0].t0 * 1e3),
7749
+ endMs: Math.round(r.words[r.words.length - 1].t1 * 1e3)
7750
+ }));
7751
+ }
7752
+ function formatTurns(turns, name = (t) => `Speaker ${t.speaker}`) {
7753
+ return turns.map((t) => `${name(t)}: ${t.text}`).join("\n");
7754
+ }
7755
+
7564
7756
  // node_modules/@bod.ee/voice/src/core/engine.ts
7565
7757
  var log15 = forComponent2("VoiceEngine");
7758
+ var addLangs = (to, from) => {
7759
+ for (const k in from) to[k] = (to[k] || 0) + from[k];
7760
+ };
7761
+ var dominantLang = (l) => Object.keys(l).sort((a, b) => l[b] - l[a])[0];
7566
7762
  var realClock = {
7567
7763
  now: () => performance.now(),
7568
7764
  setTimeout: (fn, ms) => setTimeout(fn, ms),
@@ -7595,6 +7791,17 @@ function heardPrefix(text, spoken, lookahead = 4) {
7595
7791
  }
7596
7792
  return at;
7597
7793
  }
7794
+ var NumberedSpeakers = class {
7795
+ ids = /* @__PURE__ */ new Map();
7796
+ name(label) {
7797
+ let n = this.ids.get(label);
7798
+ if (!n) this.ids.set(label, n = `Speaker ${this.ids.size + 1}`);
7799
+ return n;
7800
+ }
7801
+ reset() {
7802
+ this.ids.clear();
7803
+ }
7804
+ };
7598
7805
  var VoiceEngineOptions = class {
7599
7806
  stt;
7600
7807
  tts;
@@ -7603,6 +7810,15 @@ var VoiceEngineOptions = class {
7603
7810
  * re-finalization of the utterance dispatched just before it: replace that turn, don't add one. */
7604
7811
  onUtterance = () => {
7605
7812
  };
7813
+ /** Label multi-speaker utterances (`Speaker 1: …\nSpeaker 2: …`) from the STT's diarized turns. Sticky: off until
7814
+ * the session hears ≥2 speakers with ≥3 words each, then every turn is labelled; solo use is unchanged. */
7815
+ speakerLabels = false;
7816
+ /** Maps raw speaker ids to display names (default `Speaker N` by first appearance). */
7817
+ speakerResolver = new NumberedSpeakers();
7818
+ /** Only the speakers `speakerResolver.keep` accepts are heard (a one-person call in a busy room): another speaker's
7819
+ * turns are cut from each utterance (all of them → no utterance), and a partial that is only theirs is ignored — no
7820
+ * interim, no barge-in. Needs a diarized STT; without speaker tags every word counts. */
7821
+ speakerFilter = false;
7606
7822
  /** live partial transcript while listening (host renders the 🎤 line) */
7607
7823
  onPartial = () => {
7608
7824
  };
@@ -7660,11 +7876,19 @@ var VoiceEngineOptions = class {
7660
7876
  * it now fires on every tier-2/3 hold, not only on `looksIncomplete` (`holdFiller` stays tier-1
7661
7877
  * only). See core/endpointing.ts. */
7662
7878
  adaptiveEndpointing = false;
7879
+ /** Flush the TTS at the reply's first clause (the first , . ; : ! ? … — cut there, never mid-phrase) so its speech starts there, not at the
7880
+ * provider's buffer limit (Cartesia: a sentence end or max_buffer_delay_ms) — a reply streamed word by word otherwise
7881
+ * starts speaking only once its whole first sentence is in. Needs a TTS with flush(). */
7882
+ flushFirstClause = false;
7663
7883
  /** Grace window (ms) after an utterance dispatches, during which the user's own trailing audio cannot
7664
7884
  * barge the reply it requested. Soniox keeps finalizing partials past <end>; without this they read
7665
7885
  * as a barge and abort the fresh turn (live: mid-sentence self-interruption + steps=1→steps=0 double
7666
7886
  * abort). Short enough that a genuine immediate barge ("no wait—") still lands right after. */
7667
7887
  bargeGraceMs = 600;
7888
+ /** A new final within this window of the last dispatch, while that dispatch is still unanswered (no reply beyond an
7889
+ * ack), is merged into it and dispatched as ONE line superseding it (meta.supersedes) — never a second queued turn.
7890
+ * 0 = off. */
7891
+ pendingMergeMs = 3e3;
7668
7892
  /** Barge-in (talk over the assistant to interrupt). true = full-duplex (needs echo cancellation, or
7669
7893
  * the assistant's own TTS bleeds back and self-interrupts). false = HALF-DUPLEX: the engine is deaf
7670
7894
  * while audible (speaking + drain tail), so echo can never become a phantom turn — the right mode
@@ -7711,6 +7935,12 @@ var VoiceEngineOptions = class {
7711
7935
  overlapDecideMs = 0;
7712
7936
  /** Longest overlap (words) the host decides; also how far an overlap the host kept may grow before it cedes. */
7713
7937
  overlapDecideMaxWords = 4;
7938
+ /** A second TTS for ASIDES (decideOverlap's `aside`: a short line said over a kept overlap — "oh yeah, definitely"
7939
+ * — while the reply is held, then the SAME reply resumes from its pause point). Its own client: the reply's
7940
+ * context may still be streaming, and one client has one live context. Absent → asides are skipped (resume only). */
7941
+ asideTts = null;
7942
+ /** An aside not synthesized within this long is dropped and the reply resumes (never a long held silence). */
7943
+ asideMaxMs = 1500;
7714
7944
  /** Speculative ENERGY pre-pause while speaking (AEC tier): two residue gate-passes within 350ms →
7715
7945
  * pause ~300ms before the STT tokens land. But energy CANNOT separate residue bursts from speech
7716
7946
  * (the documented whack-a-mole) — so a residue spike during loud playback false-pauses with NO user
@@ -7784,7 +8014,16 @@ var VoiceEngine = class _VoiceEngine {
7784
8014
  state = "idle";
7785
8015
  stt;
7786
8016
  tts;
7787
- player;
8017
+ sink;
8018
+ /** The playback sink, always behind a SpliceSink (asides play ahead of a held reply); assigning one wraps it. */
8019
+ get player() {
8020
+ return this.sink;
8021
+ }
8022
+ set player(p) {
8023
+ this.sink = p instanceof SpliceSink ? p : new SpliceSink(p);
8024
+ }
8025
+ asideEpoch = 0;
8026
+ // bumps on every aside start/cancel — a late aside of a dead hold never plays
7788
8027
  speaking = false;
7789
8028
  // audible (deltas flowing OR audio draining)
7790
8029
  ctxOpen = false;
@@ -7861,6 +8100,15 @@ var VoiceEngine = class _VoiceEngine {
7861
8100
  lastDispatchFlat = "";
7862
8101
  lastDispatchWords = [];
7863
8102
  lastDispatchText = "";
8103
+ lastDispatchLangs = {};
8104
+ // chars per language of lastDispatchText (a pending-reply continuation re-sends it)
8105
+ lastDispatchTurns = [];
8106
+ // speaker turns of lastDispatchText (same reason)
8107
+ lastDispatchOut = "";
8108
+ // the text the host actually received (labelled) — what `supersedes` names
8109
+ speakerWords = /* @__PURE__ */ new Map();
8110
+ // words dispatched per raw speaker id (sticky labelling)
8111
+ labelling = false;
7864
8112
  lastDispatchAt = 0;
7865
8113
  repliedSinceDispatch = false;
7866
8114
  dispatchReply = "";
@@ -7925,6 +8173,9 @@ var VoiceEngine = class _VoiceEngine {
7925
8173
  this.clock = o.clock;
7926
8174
  }
7927
8175
  async start() {
8176
+ this.speakerWords.clear();
8177
+ this.labelling = false;
8178
+ this.options.speakerResolver.reset?.();
7928
8179
  this.tts.onAudio = (c) => {
7929
8180
  if (!this.speaking && !this.bcActive) return;
7930
8181
  if (this.wordAnchorPending && this.speaking) this.anchorSubturn();
@@ -7943,11 +8194,34 @@ var VoiceEngine = class _VoiceEngine {
7943
8194
  });
7944
8195
  };
7945
8196
  }
7946
- this.stt.onPartial = (text) => this.handlePartial(text);
7947
- this.stt.onUtterance = (text) => this.handleUtterance(text);
8197
+ this.stt.onPartial = (text, turns) => {
8198
+ const k = this.keptTurns(text, turns);
8199
+ if (k) this.handlePartial(k.text);
8200
+ };
8201
+ this.stt.onUtterance = (text, _at, language, turns, ago) => {
8202
+ this.lastWordAt = ago === void 0 ? 0 : this.clock.now() - ago;
8203
+ if (turns?.length && (this.options.speakerLabels || this.options.speakerFilter)) {
8204
+ try {
8205
+ this.options.speakerResolver.observe?.(turns);
8206
+ } catch (e) {
8207
+ log15.warn(`speakerResolver.observe threw: ${e instanceof Error ? e.message : e}`);
8208
+ }
8209
+ }
8210
+ const k = this.keptTurns(text, turns);
8211
+ if (!k) {
8212
+ this.diag("speaker_dropped", { text });
8213
+ return;
8214
+ }
8215
+ this.handleUtterance(k.text, language ? { [language]: k.text.length } : void 0, k.turns);
8216
+ };
7948
8217
  this.stt.onLevel = (rms) => this.handleLevel(rms);
7949
- await Promise.all([this.tts.connect(), this.stt.start()]);
8218
+ await Promise.all([
8219
+ this.tts.connect(),
8220
+ this.stt.start(),
8221
+ Promise.resolve(this.options.asideTts?.connect()).catch((e) => log15.warn(`aside TTS connect failed \u2014 asides off until it reconnects: ${e instanceof Error ? e.message : e}`))
8222
+ ]);
7950
8223
  this.tts.warmup?.();
8224
+ this.options.asideTts?.warmup?.();
7951
8225
  this.setState("listening");
7952
8226
  if (this.options.bargeInMode === "aec" && !this.stt.usingAec)
7953
8227
  log15.warn("bargeInMode=aec but mic does not report echoCancellation applied \u2014 STT may transcribe the assistant");
@@ -7980,6 +8254,10 @@ var VoiceEngine = class _VoiceEngine {
7980
8254
  }
7981
8255
  }
7982
8256
  idleWaiters = [];
8257
+ /** When (engine clock) the last STT final's last word ended — UtteranceMeta.lastWordAgoMs. */
8258
+ lastWordAt = 0;
8259
+ /** '' until the open context's first clause is flushed (flushFirstClause); null = flushed. */
8260
+ clause = "";
7983
8261
  setState(s) {
7984
8262
  if (this.state === s) return;
7985
8263
  this.diag("state", { from: this.state, to: s });
@@ -8040,6 +8318,7 @@ var VoiceEngine = class _VoiceEngine {
8040
8318
  this.emo = this.options.emotions ? new EmotionStream(this.options.showEmotions) : null;
8041
8319
  this.echoWords = new Set(this.words(this.prevReply));
8042
8320
  this.tts.newContext();
8321
+ this.clause = "";
8043
8322
  this.clearAckTimer("new_turn");
8044
8323
  if (ack && this.options.ackPhrase) {
8045
8324
  this.diag("ack_fired", { phrase: this.options.ackPhrase, adaptive: false });
@@ -8114,7 +8393,15 @@ var VoiceEngine = class _VoiceEngine {
8114
8393
  this.dispatchReply += prose;
8115
8394
  }
8116
8395
  for (const w of this.words(this.reply)) this.echoWords.add(w);
8117
- this.tts.speak(forSpeech(speech), true);
8396
+ const cut = this.options.flushFirstClause && this.clause !== null && this.tts.flush ? speech.search(/[,.;:!?…](?=\s|$)/) : -1;
8397
+ if (cut < 0) this.tts.speak(forSpeech(speech), true);
8398
+ else {
8399
+ this.tts.speak(forSpeech(speech.slice(0, cut + 1)), true);
8400
+ this.clause = null;
8401
+ this.tts.flush();
8402
+ this.diag("tts_first_clause_flush", {});
8403
+ if (speech.slice(cut + 1)) this.tts.speak(forSpeech(speech.slice(cut + 1)), true);
8404
+ }
8118
8405
  if (!this.spokeDeltas && this.turnStartAt) log15.debug(`ttft: ${Math.round(this.clock.now() - this.turnStartAt)}ms`);
8119
8406
  this.spokeDeltas = true;
8120
8407
  this.setState("speaking");
@@ -8139,6 +8426,8 @@ var VoiceEngine = class _VoiceEngine {
8139
8426
  this.diag("tts_subturn_boundary", { playedMs: Math.round(Math.max(0, this.player.playedMs())) });
8140
8427
  this.tts.end();
8141
8428
  this.tts.newContext();
8429
+ this.clause = "";
8430
+ this.emo?.resume();
8142
8431
  this.wordAnchorPending = true;
8143
8432
  this.wordPending = [];
8144
8433
  }
@@ -8147,7 +8436,7 @@ var VoiceEngine = class _VoiceEngine {
8147
8436
  * content; fully drained, drainMs is 0 and playedMs has reached the scheduled total). */
8148
8437
  anchorSubturn() {
8149
8438
  this.wordAnchorPending = false;
8150
- this.wordOrigin = Math.max(0, this.player.playedMs() + this.player.drainMs()) / 1e3;
8439
+ this.wordOrigin = this.player.endMs() / 1e3;
8151
8440
  this.diag("reveal_anchor", { originSec: Number(this.wordOrigin.toFixed(3)), held: this.wordPending.length });
8152
8441
  for (const [w, s] of this.wordPending) {
8153
8442
  this.wordStarts.push(this.wordOrigin + s);
@@ -8230,7 +8519,7 @@ var VoiceEngine = class _VoiceEngine {
8230
8519
  this.stopWordReveal(true);
8231
8520
  if (this.turnStartAt) log15.debug(`turn: ${Math.round(this.clock.now() - this.turnStartAt)}ms (incl. playback)`);
8232
8521
  this.echoUntil = this.clock.now() + 2500;
8233
- if (!this.usingAec) this.stt.reset();
8522
+ if (!this.usingAec) this.echoHead = true;
8234
8523
  try {
8235
8524
  this.options.onSpeechEnd();
8236
8525
  } catch (e) {
@@ -8371,6 +8660,12 @@ var VoiceEngine = class _VoiceEngine {
8371
8660
  return phrase;
8372
8661
  }
8373
8662
  lastDispatchWasQuestion = false;
8663
+ /** Chars per STT language of the text that joined the pending turn / the overlap-held final — the dispatch takes the dominant. */
8664
+ pendingLangs = {};
8665
+ decideHeldLangs = {};
8666
+ /** Speaker turns riding with the pending / overlap-held text (like the langs). */
8667
+ pendingTurns = [];
8668
+ decideHeldTurns = [];
8374
8669
  dispatchRefinal = false;
8375
8670
  // the last dispatch re-finalized (superseded) the one before it — same logical turn
8376
8671
  ackSpent = false;
@@ -8412,6 +8707,7 @@ var VoiceEngine = class _VoiceEngine {
8412
8707
  this.drainTimer = null;
8413
8708
  }
8414
8709
  this.resetOverlap(false);
8710
+ this.cancelAside("interrupt");
8415
8711
  this.lastResumeAt = 0;
8416
8712
  const { heard, exact } = this.heardCut();
8417
8713
  if (this.reply || droppedQueued) this.lastInterrupted = { full: this.reply, heard, exact, queued, queuedTurn: this.queuedTurn };
@@ -8423,7 +8719,7 @@ var VoiceEngine = class _VoiceEngine {
8423
8719
  this.echoUntil = this.clock.now() + Math.max(2500, this.player.drainMs() + 3e3);
8424
8720
  this.tts.cancel();
8425
8721
  this.player.kill();
8426
- if (!this.usingAec) this.stt.reset();
8722
+ if (!this.usingAec) this.echoHead = true;
8427
8723
  if (this.reply) this.prevReply = this.reply;
8428
8724
  this.setState("listening");
8429
8725
  }
@@ -8450,9 +8746,11 @@ var VoiceEngine = class _VoiceEngine {
8450
8746
  if (this.drainTimer) this.clock.clearTimeout(this.drainTimer);
8451
8747
  if (this.specTimer) this.clock.clearTimeout(this.specTimer);
8452
8748
  this.bcSupersede();
8749
+ this.cancelAside("stop");
8453
8750
  this.stt.stop();
8454
8751
  this.player.kill();
8455
8752
  this.tts.close();
8753
+ this.options.asideTts?.close();
8456
8754
  this.setState("idle");
8457
8755
  }
8458
8756
  // --- listening side (STT-driven) ---
@@ -8491,6 +8789,22 @@ var VoiceEngine = class _VoiceEngine {
8491
8789
  "twenty"
8492
8790
  ].map((w, i) => [w, String(i)])
8493
8791
  );
8792
+ /** The next final may open with our own echo (a heuristic barge/settle kept the STT buffer, not reset it). */
8793
+ echoHead = false;
8794
+ /** `text` without its leading run of our own words (echoWords); all echo → unchanged (the echo gates judge it whole). */
8795
+ dropEchoHead(text) {
8796
+ const toks = text.trim().split(/\s+/);
8797
+ let i = 0;
8798
+ for (let k = 0; k < toks.length; k++) {
8799
+ const w = this.words(toks[k]);
8800
+ if (!w.length) continue;
8801
+ if (!w.every((x) => this.echoWords.has(x))) break;
8802
+ i = k + 1;
8803
+ }
8804
+ if (!i || i === toks.length) return text;
8805
+ this.diag("echo_head", { dropped: toks.slice(0, i).join(" ").slice(0, 60) });
8806
+ return toks.slice(i).join(" ");
8807
+ }
8494
8808
  novelWords(text) {
8495
8809
  return this.words(text).filter((w) => !this.echoWords.has(w));
8496
8810
  }
@@ -8523,6 +8837,22 @@ var VoiceEngine = class _VoiceEngine {
8523
8837
  for (let i = 1; i < w.length; i++) if (pairs.has(`${w[i - 1]} ${w[i]}`)) inRun[i - 1] = inRun[i] = true;
8524
8838
  return inRun.filter(Boolean).length * 2 >= w.length;
8525
8839
  }
8840
+ /** Display a partial — unless it's echo-shaped during/after our own speech (degraded-AEC bleed), or the STT's trailing
8841
+ * re-emit of the line just dispatched (Soniox emits partials after <end>): the host types partials into its input box,
8842
+ * so that tail would sit there after the line was sent (bare-browser #837). */
8843
+ showPartial(text) {
8844
+ if (this.echoActive() && !(this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) return;
8845
+ if (this.echoesDispatch(text, true) || this.tailWordOfDispatch(text)) {
8846
+ this.diag("dup_partial_hidden", { text: text.slice(0, 60) });
8847
+ return;
8848
+ }
8849
+ this.options.onPartial(text);
8850
+ }
8851
+ /** A lone word that is the just-dispatched line's last word (echoesDispatch needs ≥2): display-only filter. */
8852
+ tailWordOfDispatch(text) {
8853
+ const w = text.trim().split(/\s+/).map(normWord).filter(Boolean), d = this.lastDispatchWords.map(normWord).filter(Boolean);
8854
+ return w.length === 1 && d.length > 0 && this.clock.now() - this.lastDispatchAt < _VoiceEngine.DUP_FINAL_MS && d[d.length - 1].startsWith(w[0]);
8855
+ }
8526
8856
  handlePartial(text) {
8527
8857
  if (this.speaking) {
8528
8858
  if (!this.options.bargeIn) return;
@@ -8531,7 +8861,7 @@ var VoiceEngine = class _VoiceEngine {
8531
8861
  this.lastGraceDiag = this.bargeGraceUntil;
8532
8862
  this.diag("grace_suppress", { text: text.slice(0, 60) });
8533
8863
  }
8534
- if (!this.echoActive() || (this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) this.options.onPartial(text);
8864
+ this.showPartial(text);
8535
8865
  return;
8536
8866
  }
8537
8867
  if (this.echoesDispatch(text, true)) {
@@ -8596,7 +8926,7 @@ var VoiceEngine = class _VoiceEngine {
8596
8926
  }
8597
8927
  this.trackSpeculation(text);
8598
8928
  this.trackBackchannel(text);
8599
- if (!this.echoActive() || (this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) this.options.onPartial(text);
8929
+ this.showPartial(text);
8600
8930
  }
8601
8931
  /** Speculative-reflex trigger (listening side only — the speaking branch returns before this): a
8602
8932
  * partial that hasn't CHANGED for speculativeMs and has ≥ speculativeMinWords is stable → fire
@@ -8639,6 +8969,8 @@ var VoiceEngine = class _VoiceEngine {
8639
8969
  * 'Okay.'/'Right.' are fine here (unlike ACKS_NEUTRAL's no-Okay rule): a standalone user "Okay."
8640
8970
  * right after OUR blip is overwhelmingly the blip's echo — the squash guard eating it is the point. */
8641
8971
  static BACKCHANNELS = ["Mm-hm.", "Uh-huh.", "Right.", "Okay."];
8972
+ /** fireBackchannel renders inline tags (a host's bundle may predate it: it spoke them raw). */
8973
+ static TAGGED_BLIPS = true;
8642
8974
  /** Backchannel trigger (listening side only — the speaking branch returns before this): a partial
8643
8975
  * that reached a clause boundary and then stayed UNCHANGED for backchannelMs (a micro-pause, still
8644
8976
  * BEFORE the silence endpoint) fires a blip — if long enough (≥ backchannelMinWords), predominantly
@@ -8675,8 +9007,10 @@ var VoiceEngine = class _VoiceEngine {
8675
9007
  fireBackchannel() {
8676
9008
  const fresh = _VoiceEngine.BACKCHANNELS.filter((x) => !this.recentBc.includes(x));
8677
9009
  const pool = fresh.length ? fresh : _VoiceEngine.BACKCHANNELS;
8678
- const phrase = pool[Math.floor(Math.random() * pool.length)];
8679
- this.recentBc.push(phrase);
9010
+ const line = pool[Math.floor(Math.random() * pool.length)];
9011
+ const r = this.options.emotions ? renderEmotions(line, { show: false }) : { speech: line, prose: line };
9012
+ const phrase = r.prose.trim() || line;
9013
+ this.recentBc.push(line);
8680
9014
  if (this.recentBc.length > 2) this.recentBc.shift();
8681
9015
  const now3 = this.clock.now();
8682
9016
  for (const w of this.words(phrase)) this.echoWords.add(w);
@@ -8695,7 +9029,7 @@ var VoiceEngine = class _VoiceEngine {
8695
9029
  this.bcActive = false;
8696
9030
  };
8697
9031
  this.tts.newContext();
8698
- this.tts.speak(phrase, false);
9032
+ this.tts.speak(r.speech, false);
8699
9033
  log15.debug(`backchannel: "${phrase}"`);
8700
9034
  this.diag("backchannel", { phrase });
8701
9035
  this.options.onBackchannel(phrase);
@@ -8750,10 +9084,29 @@ var VoiceEngine = class _VoiceEngine {
8750
9084
  }
8751
9085
  if (pfx && norm4(nw[pw.length - 1]).startsWith(norm4(pw[pw.length - 1]))) return next;
8752
9086
  }
9087
+ let common = 0;
9088
+ while (common < pw.length && common < nw.length && norm4(pw[common]) === norm4(nw[common])) common++;
9089
+ if (common === nw.length && common >= 4 && common >= 0.75 * pw.length) {
9090
+ this.diag("merge_restated", { dropped: prev.slice(0, 60), revised: true });
9091
+ return next;
9092
+ }
8753
9093
  if (_VoiceEngine.restates(prev, next)) {
8754
9094
  this.diag("merge_restated", { dropped: prev.slice(0, 60) });
8755
9095
  return next;
8756
9096
  }
9097
+ const head = norm4(nw[0] ?? "");
9098
+ if (head.length >= 2 && nw.length >= 4) {
9099
+ for (let s = pw.length - 4; s >= 0; s--) {
9100
+ const w = norm4(pw[s]);
9101
+ if (w.length <= head.length || !w.endsWith(head)) continue;
9102
+ let ok = true;
9103
+ for (let x = 1; x <= 3 && ok; x++) ok = s + x < pw.length && norm4(pw[s + x]) === norm4(nw[x]);
9104
+ if (ok) {
9105
+ this.diag("merge_restated", { dropped: pw.slice(s + 1).join(" ").slice(0, 60), clipped: true });
9106
+ return [...pw.slice(0, s + 1), ...nw.slice(1)].join(" ");
9107
+ }
9108
+ }
9109
+ }
8757
9110
  const max = Math.min(pw.length, nw.length);
8758
9111
  for (let k = max; k > 0; k--) {
8759
9112
  let match = true;
@@ -8762,6 +9115,14 @@ var VoiceEngine = class _VoiceEngine {
8762
9115
  break;
8763
9116
  }
8764
9117
  if (match) return [...pw, ...nw.slice(k)].join(" ");
9118
+ if (k >= 3) {
9119
+ let grown = norm4(nw[k - 1]).startsWith(norm4(pw[pw.length - 1])) && norm4(pw[pw.length - 1]).length > 0;
9120
+ for (let i = 0; grown && i < k - 1; i++) if (norm4(pw[pw.length - k + i]) !== norm4(nw[i])) grown = false;
9121
+ if (grown) {
9122
+ this.diag("merge_restated", { dropped: pw.slice(-k).join(" ").slice(0, 60), grown: true });
9123
+ return [...pw.slice(0, pw.length - k), ...nw].join(" ");
9124
+ }
9125
+ }
8765
9126
  }
8766
9127
  return `${prev} ${next}`;
8767
9128
  }
@@ -8839,7 +9200,13 @@ var VoiceEngine = class _VoiceEngine {
8839
9200
  }
8840
9201
  lastDupPartial = "";
8841
9202
  // dup_partial diag once per distinct partial
8842
- static TRAIL_RE = /(?:^|\s)(?:and|but|or|so|to|the|a|an|of|in|for|with|that|if|uh|um|like|about|from|into|on|is|are|was|were|,)$/i;
9203
+ /** A trailing clause comma ("…survive the crash,") or a function word / open wh-word ("…is how") ends no thought. The
9204
+ * comma used to sit inside the word alternation, so it matched only after a space — "crash," dispatched at once (live
9205
+ * duplex 2026-10-14: the reflex answered "So let's say they did survive the crash," then answered again). */
9206
+ static TRAIL_RE = /(?:[,;:–—-]|(?:^|\s)(?:and|but|or|so|because|then|than|to|the|a|an|my|your|our|their|this|of|in|for|with|at|by|as|that|if|when|how|what|why|where|which|who|uh|um|like|about|from|into|on|is|are|was|were))$/i;
9207
+ /** A hypothetical set up without its consequence: "So let's say they survived." / "Suppose it fails" — the question
9208
+ * comes next. A comma inside ("If it fails, restart it.") or a question mark means the consequence is there. */
9209
+ static SETUP_RE = /^(?:(?:so|and|but|okay|ok|well|now)[,\s]+)*(?:let'?s say|let us say|suppose|supposing|imagine|assuming|if|what if)\b[^,;?]*[.…]?$/i;
8843
9210
  /** The user's line right after a barge cut our speech (or a re-final growing it), with no terminal punctuation:
8844
9211
  * they took the floor mid-thought — Soniox closes on the pause, not the thought (live 20260923-192543: "But isn't it"
8845
9212
  * → "…can be done" → "…with boxing" → "…or semantic?" dispatched 4 times). Held like an incomplete utterance. */
@@ -8851,9 +9218,69 @@ var VoiceEngine = class _VoiceEngine {
8851
9218
  static CONTROL_RE = ((w) => new RegExp(`^${w}(?:[\\s,]+${w})*[.!?\u2026]*$`, "i"))("(?:stop|wait|yes|yeah|yep|no|nope|okay|ok|go on|continue|never ?mind)");
8852
9219
  /** The utterance sounds like the user paused mid-thought (trailing conjunction/filler/comma). */
8853
9220
  looksIncomplete(text) {
8854
- return _VoiceEngine.TRAIL_RE.test(text.trim()) && !_VoiceEngine.CONTROL_RE.test(text.trim());
8855
- }
8856
- handleUtterance(text) {
9221
+ const t = text.trim();
9222
+ return (_VoiceEngine.TRAIL_RE.test(t) || _VoiceEngine.SETUP_RE.test(t)) && !_VoiceEngine.CONTROL_RE.test(t);
9223
+ }
9224
+ /** Diarized turns worth keeping: none unless labelling is on; while we are audible, a run with <2 novel words is our
9225
+ * own echo diarized as another speaker — dropped from the text too (only when another run remains). */
9226
+ acceptTurns(text, turns) {
9227
+ if (!this.options.speakerLabels || !turns?.length || turns.every((t) => !t.speaker)) return [text, []];
9228
+ if (turns.length >= 2 && this.echoActive()) {
9229
+ const kept = turns.filter((t) => this.novelWords(t.text).length >= 2);
9230
+ if (kept.length && kept.length < turns.length) {
9231
+ this.diag("echo_swallow", { guard: "echo_run", text: turns.filter((t) => !kept.includes(t)).map((t) => t.text).join(" ").slice(0, 60) });
9232
+ return [kept.map((t) => t.text).join(" "), kept];
9233
+ }
9234
+ }
9235
+ return [text, turns];
9236
+ }
9237
+ /** speakerFilter: the text of the turns `keep` accepts (unchanged when all are kept or there are no speaker tags);
9238
+ * null when no turn is kept. */
9239
+ keptTurns(text, turns) {
9240
+ const keep = this.options.speakerResolver.keep;
9241
+ if (!this.options.speakerFilter || !keep || !turns?.length || turns.every((t) => !t.speaker)) return { text, turns };
9242
+ const kept = turns.filter((t) => {
9243
+ try {
9244
+ return keep.call(this.options.speakerResolver, t.speaker, t);
9245
+ } catch {
9246
+ return true;
9247
+ }
9248
+ });
9249
+ if (kept.length === turns.length) return { text, turns };
9250
+ if (!kept.length) return null;
9251
+ return { text: kept.map((t) => t.text).join(" "), turns: kept };
9252
+ }
9253
+ /** Same-speaker neighbours (across merged finals) join into one run. */
9254
+ static joinRuns(turns) {
9255
+ return turns.reduce((acc, t) => {
9256
+ const p = acc[acc.length - 1];
9257
+ if (p?.speaker === t.speaker) acc[acc.length - 1] = { ...p, text: `${p.text} ${t.text}`, endMs: t.endMs };
9258
+ else acc.push({ ...t });
9259
+ return acc;
9260
+ }, []);
9261
+ }
9262
+ /** The dispatched text, labelled once the session is multi-speaker. Words never change: when the turns' words no longer
9263
+ * match the (merged/stripped) text, the whole text goes under the speaker with the most words. */
9264
+ labelTurns(text, turns) {
9265
+ if (!turns.length) return { out: text };
9266
+ const runs = _VoiceEngine.joinRuns(turns);
9267
+ for (const t of runs) this.speakerWords.set(t.speaker, (this.speakerWords.get(t.speaker) ?? 0) + this.words(t.text).length);
9268
+ if (!this.labelling && [...this.speakerWords.values()].filter((n) => n >= 3).length >= 2) {
9269
+ this.labelling = true;
9270
+ this.diag("speaker_labels", { on: true });
9271
+ }
9272
+ if (!this.labelling) return { out: text };
9273
+ const lines = normWord(runs.map((t) => t.text).join(" ")) === normWord(text) ? runs : [{ ...runs.reduce((a, b) => this.words(b.text).length > this.words(a.text).length ? b : a), text }];
9274
+ const names = lines.map((t) => this.options.speakerResolver.name(t.speaker, t));
9275
+ return { out: formatTurns(lines, (t) => names[lines.indexOf(t)]), speakers: [...new Set(names)] };
9276
+ }
9277
+ handleUtterance(text, langs, rawTurns) {
9278
+ let turns;
9279
+ if (this.echoHead) {
9280
+ this.echoHead = false;
9281
+ text = this.dropEchoHead(text);
9282
+ }
9283
+ [text, turns] = this.acceptTurns(text, rawTurns);
8857
9284
  if (this.speaking && this.echoesDispatch(text)) {
8858
9285
  this.diag("utterance_drop", { reason: "dup_final", text: text.slice(0, 60) });
8859
9286
  if (this.pausedAt) {
@@ -8864,6 +9291,8 @@ var VoiceEngine = class _VoiceEngine {
8864
9291
  }
8865
9292
  if (this.decideTimer) {
8866
9293
  this.decideHeldFinal = this.mergeUtterance(this.decideHeldFinal, text);
9294
+ addLangs(this.decideHeldLangs, langs);
9295
+ this.decideHeldTurns.push(...turns);
8867
9296
  this.diag("utterance_held", { reason: "overlap_decide", text: text.slice(0, 60) });
8868
9297
  return;
8869
9298
  }
@@ -8894,6 +9323,8 @@ var VoiceEngine = class _VoiceEngine {
8894
9323
  }
8895
9324
  if (this.pendingUtt) this.mergePath = "merged";
8896
9325
  this.pendingUtt = this.mergeUtterance(this.pendingUtt, text);
9326
+ addLangs(this.pendingLangs, langs);
9327
+ this.pendingTurns.push(...turns);
8897
9328
  if (this.pendingTimer) this.clock.clearTimeout(this.pendingTimer);
8898
9329
  if (this.options.adaptiveEndpointing) {
8899
9330
  const holdMs = computeHoldMs({
@@ -8943,8 +9374,12 @@ var VoiceEngine = class _VoiceEngine {
8943
9374
  }
8944
9375
  this.bcCount = 0;
8945
9376
  this.bcPartial = "";
8946
- const text = this.stripBackchannelEcho(this.pendingUtt);
9377
+ let text = this.stripBackchannelEcho(this.pendingUtt);
8947
9378
  this.pendingUtt = "";
9379
+ const langs = this.pendingLangs;
9380
+ this.pendingLangs = {};
9381
+ let turns = this.pendingTurns;
9382
+ this.pendingTurns = [];
8948
9383
  const path = this.mergePath;
8949
9384
  this.mergePath = "direct";
8950
9385
  const heldTier = this.holdTier;
@@ -8967,7 +9402,27 @@ var VoiceEngine = class _VoiceEngine {
8967
9402
  this.lastDispatchText = text;
8968
9403
  return;
8969
9404
  }
8970
- const meta = refinal ? { supersedes: this.lastDispatchText } : void 0;
9405
+ const cont = !refinal && !answered && !!this.lastDispatchText && this.clock.now() - this.lastDispatchAt < this.options.pendingMergeMs;
9406
+ if (cont) {
9407
+ if (this.speaking) this.interrupt();
9408
+ text = this.mergeUtterance(this.lastDispatchText, text);
9409
+ addLangs(langs, this.lastDispatchLangs);
9410
+ turns = [...this.lastDispatchTurns, ...turns];
9411
+ this.diag("utterance_merge", { reason: "pending_reply", text: text.slice(0, 60) });
9412
+ }
9413
+ const sup = refinal || cont;
9414
+ const lang = dominantLang(langs);
9415
+ this.lastDispatchLangs = langs;
9416
+ this.lastDispatchTurns = turns;
9417
+ const { out, speakers } = this.labelTurns(text, turns);
9418
+ const heardAgo = this.lastWordAt ? Math.max(0, Math.round(this.clock.now() - this.lastWordAt)) : void 0;
9419
+ const meta = sup || lang || speakers || heardAgo !== void 0 ? {
9420
+ ...sup ? { supersedes: this.lastDispatchOut } : {},
9421
+ ...lang ? { language: lang } : {},
9422
+ ...speakers ? { speakers, plain: text } : {},
9423
+ ...heardAgo !== void 0 ? { lastWordAgoMs: heardAgo } : {}
9424
+ } : void 0;
9425
+ this.lastDispatchOut = out;
8971
9426
  this.lastDispatchFlat = flat;
8972
9427
  this.lastDispatchWords = text.trim().split(/\s+/);
8973
9428
  this.lastDispatchText = text;
@@ -8975,18 +9430,28 @@ var VoiceEngine = class _VoiceEngine {
8975
9430
  this.repliedSinceDispatch = false;
8976
9431
  this.dispatchReply = "";
8977
9432
  this.lastDispatchWasQuestion = /\?\s*$/.test(text);
8978
- this.bargedBeforeDispatch = refinal ? this.bargedBeforeDispatch || this.bargedSinceDispatch : this.bargedSinceDispatch;
9433
+ this.bargedBeforeDispatch = sup ? this.bargedBeforeDispatch || this.bargedSinceDispatch : this.bargedSinceDispatch;
8979
9434
  this.bargedSinceDispatch = false;
8980
- this.dispatchRefinal = refinal;
8981
- if (!refinal) this.ackSpent = false;
9435
+ this.dispatchRefinal = sup;
9436
+ if (!sup) this.ackSpent = false;
8982
9437
  this.turnStartAt = this.clock.now();
8983
9438
  this.bargeGraceUntil = this.clock.now() + this.options.bargeGraceMs;
8984
- this.diag("dispatch", { text, path: refinal ? "refinalized" : path, heldTier, question: this.lastDispatchWasQuestion, ...this.bargedBeforeDispatch ? { barged: true } : {} });
8985
- this.options.onUtterance(text, meta);
9439
+ this.diag("dispatch", { text, path: refinal ? "refinalized" : cont ? "merged_pending" : path, heldTier, question: this.lastDispatchWasQuestion, ...this.bargedBeforeDispatch ? { barged: true } : {} });
9440
+ this.options.onUtterance(out, meta);
8986
9441
  }
8987
9442
  }
9443
+ takeHeldLangs() {
9444
+ const l = this.decideHeldLangs;
9445
+ this.decideHeldLangs = {};
9446
+ return l;
9447
+ }
9448
+ takeHeldTurns() {
9449
+ const t = this.decideHeldTurns;
9450
+ this.decideHeldTurns = [];
9451
+ return t;
9452
+ }
8988
9453
  get overlapCapable() {
8989
- return this.usingAec && this.options.overlapPause && !!this.player.pause && !!this.player.resume;
9454
+ return this.usingAec && this.options.overlapPause && this.player.canPause;
8990
9455
  }
8991
9456
  /** Stop/question words that make an overlap an obvious barge — never held for a host decision. */
8992
9457
  static OVERLAP_CEDE_WORDS = /* @__PURE__ */ new Set([
@@ -9016,12 +9481,12 @@ var VoiceEngine = class _VoiceEngine {
9016
9481
  /** Cede the floor to an overlap: interrupt, tell the host, then dispatch a final that was held while deciding. */
9017
9482
  cedeOverlap(trigger, txt) {
9018
9483
  const phase = this.ctxOpen ? "speaking" : "drain";
9019
- const held = this.decideHeldFinal;
9484
+ const held = this.decideHeldFinal, heldLangs = this.takeHeldLangs(), heldTurns = this.takeHeldTurns();
9020
9485
  this.decideHeldFinal = "";
9021
9486
  this.diag("barge_in", { phase, trigger, novel: this.novelWords(txt).length, total: this.words(txt).length });
9022
9487
  this.interrupt();
9023
9488
  this.options.onBargeIn(phase);
9024
- if (held) this.handleUtterance(held);
9489
+ if (held) this.handleUtterance(held, heldLangs, heldTurns);
9025
9490
  }
9026
9491
  /** Wait (bounded) for the host's verdict. `fresh` = a re-decision on grown text: it gets its own window — the host
9027
9492
  * was only just asked about it (live: "Oh," → "Oh, that" → "Oh, that— that's new" outgrew every verdict, and the
@@ -9053,7 +9518,7 @@ var VoiceEngine = class _VoiceEngine {
9053
9518
  * is dropped. Words beyond `heard` are a new overlap, decided afresh: if the partial/final has already grown past
9054
9519
  * it, the hold stays and `overlap_pause` is re-emitted for a new verdict (bounded by overlapDecideMs, else it
9055
9520
  * cedes; a re-decision gets its own window, the whole hold capped at 3× it) and this returns false. Also false when no overlap hold is live (the engine already decided). */
9056
- decideOverlap(choice, heard) {
9521
+ decideOverlap(choice, heard, aside) {
9057
9522
  if (!this.pausedAt || !this.speaking) return false;
9058
9523
  if (choice === "yield") {
9059
9524
  this.cedeOverlap("host_yield", this.lastOverlapPartial);
@@ -9072,13 +9537,84 @@ var VoiceEngine = class _VoiceEngine {
9072
9537
  }
9073
9538
  const held = this.decideHeldFinal;
9074
9539
  this.decideHeldFinal = "";
9540
+ this.decideHeldLangs = {};
9541
+ this.decideHeldTurns = [];
9075
9542
  this.diag("overlap_resume", { heldMs: Math.round(this.clock.now() - this.pausedAt), reason: "host_keep" });
9076
9543
  if (held) this.diag("utterance_drop", { reason: "overlap_kept", text: held.slice(0, 60) });
9077
9544
  this.overlapKeptUntil = held ? 0 : this.clock.now() + 4e3;
9078
- this.resetOverlap(true);
9545
+ const asided = !!aside?.trim() && this.startAside(aside.trim());
9546
+ this.resetOverlap(!asided);
9079
9547
  this.lastResumeAt = 0;
9080
9548
  return true;
9081
9549
  }
9550
+ /** Aside (decideOverlap's 3rd arg): synthesize it on the aside TTS while the reply stays HELD, then play it ahead of
9551
+ * the reply, which resumes from its pause point (SpliceSink). Its words are pre-seeded as our own (echo) like a
9552
+ * blip — its mic echo never reads as the user. false = no aside TTS: the caller resumes as usual. */
9553
+ startAside(text) {
9554
+ const t = this.options.asideTts;
9555
+ if (!t || !/[\p{L}\p{N}]/u.test(text)) {
9556
+ this.diag("aside_skipped", { reason: t ? "empty" : "no_tts", text: text.slice(0, 40) });
9557
+ return false;
9558
+ }
9559
+ const epoch = ++this.asideEpoch, chunks = [], t0 = this.clock.now();
9560
+ const live = () => epoch === this.asideEpoch;
9561
+ const give = (reason) => {
9562
+ if (!live()) return;
9563
+ this.asideEpoch++;
9564
+ this.diag("aside_skipped", { reason, text: text.slice(0, 40) });
9565
+ try {
9566
+ t.cancel();
9567
+ } catch {
9568
+ }
9569
+ if (this.speaking && !this.pausedAt) this.player.resume();
9570
+ };
9571
+ const timer = this.clock.setTimeout(() => give("late"), this.options.asideMaxMs);
9572
+ for (const w of this.words(text)) this.echoWords.add(w);
9573
+ this.ackAt = t0;
9574
+ this.lastAck = text;
9575
+ this.echoUntil = Math.max(this.echoUntil, t0 + 2500 + this.options.asideMaxMs);
9576
+ t.onAudio = (c) => {
9577
+ if (live()) chunks.push(c);
9578
+ };
9579
+ t.onDone = () => {
9580
+ if (!live()) return;
9581
+ this.clock.clearTimeout(timer);
9582
+ const n = chunks.reduce((a, c) => a + c.length, 0);
9583
+ if (!n || !this.speaking || this.pausedAt) return give(!n ? "no_audio" : this.pausedAt ? "held_again" : "turn_over");
9584
+ this.asideEpoch++;
9585
+ const clip2 = new Uint8Array(n);
9586
+ let i = 0;
9587
+ for (const c of chunks) {
9588
+ clip2.set(c, i);
9589
+ i += c.length;
9590
+ }
9591
+ const from = this.player.splice(clip2);
9592
+ const now3 = this.clock.now();
9593
+ this.overlapKeptUntil = Math.max(this.overlapKeptUntil, now3 + 4e3);
9594
+ this.echoUntil = Math.max(this.echoUntil, now3 + 2500 + this.player.drainMs());
9595
+ this.diag("aside", { text: text.slice(0, 40), fromMs: Math.round(from), clipMs: Math.round(n / (TTS_SAMPLE_RATE * 2) * 1e3), synthMs: Math.round(now3 - t0) });
9596
+ };
9597
+ try {
9598
+ const lang = this.tts.options?.language, ao = t.options;
9599
+ if (lang && ao) ao.language = lang;
9600
+ t.newContext();
9601
+ t.speak(text, false);
9602
+ } catch (e) {
9603
+ this.clock.clearTimeout(timer);
9604
+ give(`error: ${e instanceof Error ? e.message : e}`);
9605
+ return true;
9606
+ }
9607
+ return true;
9608
+ }
9609
+ cancelAside(reason) {
9610
+ if (!this.options.asideTts) return;
9611
+ this.asideEpoch++;
9612
+ try {
9613
+ this.options.asideTts.cancel();
9614
+ } catch (e) {
9615
+ log15.debug(`aside cancel (${reason}): ${e instanceof Error ? e.message : e}`);
9616
+ }
9617
+ }
9082
9618
  armResume() {
9083
9619
  if (this.resumeTimer) this.clock.clearTimeout(this.resumeTimer);
9084
9620
  this.resumeTimer = this.clock.setTimeout(() => {
@@ -9099,11 +9635,13 @@ var VoiceEngine = class _VoiceEngine {
9099
9635
  this.decideTimer = null;
9100
9636
  }
9101
9637
  this.decideSince = 0;
9102
- const held = this.decideHeldFinal;
9638
+ const held = this.decideHeldFinal, heldLangs = this.takeHeldLangs(), heldTurns = this.takeHeldTurns();
9103
9639
  this.decideHeldFinal = "";
9104
9640
  if (held) {
9105
9641
  this.diag("utterance_release", { reason: "overlap_hold_exit", text: held.slice(0, 60) });
9106
9642
  this.pendingUtt = this.mergeUtterance(this.pendingUtt, held);
9643
+ addLangs(this.pendingLangs, heldLangs);
9644
+ this.pendingTurns.push(...heldTurns);
9107
9645
  if (this.pendingTimer) this.clock.clearTimeout(this.pendingTimer);
9108
9646
  this.pendingTimer = this.clock.setTimeout(() => this.flushUtterance(), 0);
9109
9647
  }
@@ -9153,13 +9691,6 @@ var VoiceEngine = class _VoiceEngine {
9153
9691
  }
9154
9692
  };
9155
9693
 
9156
- // node_modules/@bod.ee/voice/src/core/types.ts
9157
- var STT_SAMPLE_RATE = 16e3;
9158
- var TTS_SAMPLE_RATE = 44100;
9159
- async function resolveAuth(auth) {
9160
- return typeof auth === "function" ? await auth() : auth;
9161
- }
9162
-
9163
9694
  // node_modules/@bod.ee/voice/src/core/spokenSplitter.ts
9164
9695
  var OPEN = "<spoken>";
9165
9696
  var CLOSE = "</spoken>";
@@ -9238,6 +9769,52 @@ var SpokenSplitter = class {
9238
9769
  }
9239
9770
  };
9240
9771
 
9772
+ // node_modules/@bod.ee/voice/src/core/pronounce.ts
9773
+ var PRONOUNCE = {
9774
+ TODO: "to-do",
9775
+ TODOs: "to-dos",
9776
+ ToDo: "to-do",
9777
+ Todo: "to-do",
9778
+ todo: "to-do",
9779
+ todos: "to-dos",
9780
+ DOING: "doing",
9781
+ BACKLOG: "backlog",
9782
+ DONE: "done",
9783
+ PARKED: "parked",
9784
+ BLOCKED: "blocked",
9785
+ FIXME: "fix-me",
9786
+ WIP: "work-in-progress"
9787
+ };
9788
+ var WORD = /[\p{L}\p{N}_]+/gu;
9789
+ var Pronouncer = class {
9790
+ constructor(map = PRONOUNCE) {
9791
+ this.map = map;
9792
+ this.prefixes = new Set(Object.keys(map).flatMap((k) => [...k].map((_, i) => k.slice(0, i + 1))));
9793
+ }
9794
+ map;
9795
+ tail = "";
9796
+ prefixes;
9797
+ /** Replace whole-word keys in a complete text (no hold). */
9798
+ apply(t) {
9799
+ return t.replace(WORD, (w) => Object.hasOwn(this.map, w) ? this.map[w] : w);
9800
+ }
9801
+ feed(delta) {
9802
+ const t = this.tail + delta;
9803
+ const last = /[\p{L}\p{N}_]+$/u.exec(t);
9804
+ const hold = last && this.prefixes.has(last[0]) ? last[0] : "";
9805
+ this.tail = hold;
9806
+ return this.apply(hold ? t.slice(0, -hold.length) : t);
9807
+ }
9808
+ flush() {
9809
+ const t = this.apply(this.tail);
9810
+ this.tail = "";
9811
+ return t;
9812
+ }
9813
+ reset() {
9814
+ this.tail = "";
9815
+ }
9816
+ };
9817
+
9241
9818
  // node_modules/@bod.ee/voice/src/adapters/soniox.ts
9242
9819
  var log16 = forComponent2("SonioxSTT");
9243
9820
  var now = () => performance.now();
@@ -9250,6 +9827,23 @@ var SonioxSTTOptions = class {
9250
9827
  * `stt-rt-v5` is live today (verified against GET https://api.soniox.com/v1/models). */
9251
9828
  model = "stt-rt-v5";
9252
9829
  languageHints = ["en"];
9830
+ /** Opt-in Soniox language identification (`enable_language_identification`): every token is tagged with a
9831
+ * `language` code, and onUtterance's 3rd arg reports the utterance's dominant one (by characters). */
9832
+ languageIdentification = false;
9833
+ /** Restrict recognition to `languageHints` (Soniox `language_hints_strict`, best-effort) AND drop any token written in a
9834
+ * script none of the hinted languages use (Latin always passes: names, brands). A he+en call heard Punjabi
9835
+ * ("ਹਾਂ ਜੀ।") in noise and the junk reached the turn + chat title (bare-browser #938). */
9836
+ languageHintsStrict = false;
9837
+ /** Drop words written in a script that is neither a hinted language's, Latin, nor the utterance's own main script —
9838
+ * stray noise transcribed in a third script ('ਹਾਂ ਜੀ। בסדר…' → 'בסדר…'). Unlike languageHintsStrict it never drops
9839
+ * the language the user actually speaks, hinted or not (a Hebrew speaker on an en-US phone). */
9840
+ dropStrayScripts = false;
9841
+ /** Opt-in Soniox speaker diarization (`enable_speaker_diarization`): onUtterance's 4th arg carries the
9842
+ * utterance split into per-speaker turns (raw Soniox speaker ids; flaps merged by speakerRuns). */
9843
+ diarization = false;
9844
+ /** Custom vocabulary (Soniox `context.terms`): rare words the model should prefer — names, brands,
9845
+ * projects ("Livshitz", not "Lead Sheets"). Empty = no context sent. Soniox caps context at ~8k tokens. */
9846
+ terms = [];
9253
9847
  /** Client-side endpoint: finalized text + no new tokens for this long = utterance (don't wait for
9254
9848
  * Soniox's semantic <end>, which adds 0.5-1.5s — the difference between ping-pong and lag). */
9255
9849
  silenceEndpointMs = 500;
@@ -9264,11 +9858,19 @@ var SonioxSTT = class {
9264
9858
  sourceStarted = false;
9265
9859
  onPartial = () => {
9266
9860
  };
9861
+ /** `language` = dominant spoken language of the utterance (only with `languageIdentification`). */
9267
9862
  onUtterance = () => {
9268
9863
  };
9269
9864
  /** mic energy (RMS) per chunk — drives the energy-based heuristic barge-in tier */
9270
9865
  onLevel = () => {
9271
9866
  };
9867
+ /** Audio tap on the Soniox clock: each chunk sent to the current connection, with `streamMs` = where it starts in that
9868
+ * connection's stream — the timeline token `start_ms`/`end_ms` (and so SpeakerTurn.startMs/endMs) refer to. A
9869
+ * reconnect starts a new stream at 0. Chunks dropped while disconnected are not reported. A throwing handler is
9870
+ * disabled (logged) — never affects transcription. */
9871
+ onAudio = null;
9872
+ sentBytes = 0;
9873
+ // s16le bytes sent on the current connection
9272
9874
  /** Unrecoverable: the mic source stopped delivering audio (Soniox starves → idle-timeout reconnect
9273
9875
  * loop). The host tears voice down instead of spinning forever. */
9274
9876
  onFatal = () => {
@@ -9299,6 +9901,14 @@ var SonioxSTT = class {
9299
9901
  endpointTimer = null;
9300
9902
  firstTokenAt = 0;
9301
9903
  // first speech token in current utterance
9904
+ lastWordEndMs = 0;
9905
+ // end_ms (Soniox stream clock) of the current utterance's last spoken token
9906
+ finalLangChars = {};
9907
+ // language → chars of finalized tokens (current utterance)
9908
+ partialLangChars = {};
9909
+ finalToks = [];
9910
+ // diarization only: finalized tokens of the current utterance
9911
+ partialToks = [];
9302
9912
  constructor(options) {
9303
9913
  this.options = { ...new SonioxSTTOptions(), ...options };
9304
9914
  }
@@ -9308,6 +9918,8 @@ var SonioxSTT = class {
9308
9918
  async connectWs() {
9309
9919
  const apiKey = await resolveAuth(this.options.auth);
9310
9920
  this.ws = new WebSocket("wss://stt-rt.soniox.com/transcribe-websocket");
9921
+ this.sentBytes = 0;
9922
+ this.lastWordEndMs = 0;
9311
9923
  await new Promise((res, rej) => {
9312
9924
  this.ws.onopen = () => res();
9313
9925
  this.ws.onerror = (e) => rej(new Error(`soniox ws: ${e.message || "connect failed"}`));
@@ -9320,7 +9932,11 @@ var SonioxSTT = class {
9320
9932
  sample_rate: STT_SAMPLE_RATE,
9321
9933
  num_channels: 1,
9322
9934
  language_hints: this.options.languageHints,
9323
- enable_endpoint_detection: true
9935
+ enable_endpoint_detection: true,
9936
+ ...this.options.languageHintsStrict ? { language_hints_strict: true } : {},
9937
+ ...this.options.languageIdentification ? { enable_language_identification: true } : {},
9938
+ ...this.options.diarization ? { enable_speaker_diarization: true } : {},
9939
+ ...this.options.terms?.length ? { context: { terms: this.options.terms } } : {}
9324
9940
  })
9325
9941
  );
9326
9942
  this.ws.onmessage = (ev) => this.handle(JSON.parse(String(ev.data)));
@@ -9343,8 +9959,10 @@ var SonioxSTT = class {
9343
9959
  const combined = (this.finalText + this.partialText).trim();
9344
9960
  if (!combined || now() - this.lastChangeAt < this.options.silenceEndpointMs) return;
9345
9961
  if (this.firstTokenAt) log16.debug(`stt: ${Math.round(now() - this.firstTokenAt)}ms first-token\u2192silence-endpoint, "${combined.slice(0, 60)}"`);
9962
+ const language = this.language(true);
9963
+ const turns = this.turns(true), ago = this.lastWordAgoMs();
9346
9964
  this.reset();
9347
- this.onUtterance(combined, now());
9965
+ this.onUtterance(this.stray(combined), now(), language, turns, ago);
9348
9966
  }, 120);
9349
9967
  this.endpointTimer.unref?.();
9350
9968
  this.startedChunksAt = now();
@@ -9371,7 +9989,19 @@ var SonioxSTT = class {
9371
9989
  sum += v * v;
9372
9990
  }
9373
9991
  this.onLevel(Math.sqrt(sum / (chunk.byteLength / 2)));
9374
- if (this.ws.readyState === WebSocket.OPEN) this.ws.send(chunk);
9992
+ if (this.ws.readyState === WebSocket.OPEN) {
9993
+ this.ws.send(chunk);
9994
+ const at = this.sentBytes / 2 / STT_SAMPLE_RATE * 1e3;
9995
+ this.sentBytes += chunk.byteLength;
9996
+ if (this.onAudio) {
9997
+ try {
9998
+ this.onAudio(chunk, at);
9999
+ } catch (e) {
10000
+ this.onAudio = null;
10001
+ log16.warn(`onAudio threw \u2014 audio tap disabled: ${e instanceof Error ? e.message : e}`);
10002
+ }
10003
+ }
10004
+ }
9375
10005
  });
9376
10006
  }
9377
10007
  handle(m) {
@@ -9380,30 +10010,78 @@ var SonioxSTT = class {
9380
10010
  return log16.error(`soniox: ${m.error_message}`);
9381
10011
  }
9382
10012
  let endpoint = false;
10013
+ const foreign = this.options.languageHintsStrict ? foreignScript(this.options.languageHints) : null;
10014
+ if (foreign && m.tokens?.length) m = { ...m, tokens: m.tokens.filter((t) => t.text === "<end>" || !foreign(t.text)) };
9383
10015
  for (const t of m.tokens ?? []) {
10016
+ if (t.text !== "<end>" && typeof t.end_ms === "number" && String(t.text).trim() && t.end_ms > this.lastWordEndMs) this.lastWordEndMs = t.end_ms;
9384
10017
  if (t.text === "<end>") endpoint = true;
9385
- else if (t.is_final) this.finalText += t.text;
10018
+ else if (t.is_final) {
10019
+ this.finalText += t.text;
10020
+ this.countLang(this.finalLangChars, t);
10021
+ if (this.options.diarization) this.finalToks.push(t);
10022
+ }
9386
10023
  }
9387
- this.partialText = (m.tokens ?? []).filter((t) => !t.is_final && t.text !== "<end>").map((t) => t.text).join("");
10024
+ const partials = (m.tokens ?? []).filter((t) => !t.is_final && t.text !== "<end>");
10025
+ this.partialText = partials.map((t) => t.text).join("");
10026
+ this.partialLangChars = {};
10027
+ for (const t of partials) this.countLang(this.partialLangChars, t);
10028
+ if (this.options.diarization) this.partialToks = partials;
9388
10029
  const combined = this.finalText + this.partialText;
9389
10030
  if (combined !== this.lastCombined) {
9390
10031
  this.lastCombined = combined;
9391
10032
  this.lastChangeAt = now();
9392
10033
  if (!this.firstTokenAt && combined.trim()) this.firstTokenAt = now();
9393
10034
  }
9394
- this.onPartial(combined);
10035
+ this.onPartial(this.stray(combined), this.turns(true));
9395
10036
  if (endpoint && this.finalText.trim()) {
9396
10037
  const utterance = this.finalText.trim();
9397
10038
  if (this.firstTokenAt) log16.debug(`stt: ${Math.round(now() - this.firstTokenAt)}ms first-token\u2192endpoint, "${utterance.slice(0, 60)}"`);
10039
+ const language = this.language(false);
10040
+ const turns = this.turns(false), ago = this.lastWordAgoMs();
9398
10041
  this.reset();
9399
- this.onUtterance(utterance, now());
10042
+ this.onUtterance(this.stray(utterance), now(), language, turns, ago);
9400
10043
  }
9401
10044
  }
10045
+ /** How long ago (ms, now) the utterance's last spoken word ended: the audio sent so far minus that token's end_ms on
10046
+ * Soniox's stream clock — the user's real last word, before Soniox's latency and the silence endpoint. */
10047
+ lastWordAgoMs() {
10048
+ if (!this.lastWordEndMs) return void 0;
10049
+ return Math.max(0, Math.round(this.sentBytes / 2 / STT_SAMPLE_RATE * 1e3 - this.lastWordEndMs));
10050
+ }
10051
+ stray(text) {
10052
+ return this.options.dropStrayScripts ? dropStrayScripts(text, this.options.languageHints) : text;
10053
+ }
10054
+ countLang(acc, t) {
10055
+ if (typeof t.language === "string" && t.language && String(t.text).trim()) acc[t.language] = (acc[t.language] ?? 0) + String(t.text).trim().length;
10056
+ }
10057
+ /** Dominant language (most characters) of the current utterance; undefined when Soniox tagged none. */
10058
+ language(withPartials) {
10059
+ const acc = { ...this.finalLangChars };
10060
+ if (withPartials) for (const [l, n] of Object.entries(this.partialLangChars)) acc[l] = (acc[l] ?? 0) + n;
10061
+ let best;
10062
+ let max = 0;
10063
+ for (const [l, n] of Object.entries(acc)) if (n > max) {
10064
+ best = l;
10065
+ max = n;
10066
+ }
10067
+ return best;
10068
+ }
10069
+ /** Per-speaker turns of the current utterance (diarization only; undefined otherwise or when Soniox tagged no speaker). */
10070
+ turns(withPartials) {
10071
+ if (!this.options.diarization) return void 0;
10072
+ const words3 = sonioxWords(withPartials ? [...this.finalToks, ...this.partialToks] : this.finalToks);
10073
+ return words3.some((w) => w.spk != null) ? speakerRuns(words3) : void 0;
10074
+ }
9402
10075
  reset() {
10076
+ this.finalToks = [];
10077
+ this.partialToks = [];
10078
+ this.finalLangChars = {};
10079
+ this.partialLangChars = {};
9403
10080
  this.finalText = "";
9404
10081
  this.partialText = "";
9405
10082
  this.lastCombined = "";
9406
10083
  this.firstTokenAt = 0;
10084
+ this.lastWordEndMs = 0;
9407
10085
  }
9408
10086
  stop() {
9409
10087
  this.stopped = true;
@@ -9414,6 +10092,65 @@ var SonioxSTT = class {
9414
10092
  this.ws?.close();
9415
10093
  }
9416
10094
  };
10095
+ var SCRIPTS = {
10096
+ he: ["Hebrew"],
10097
+ yi: ["Hebrew"],
10098
+ ar: ["Arabic"],
10099
+ fa: ["Arabic"],
10100
+ ur: ["Arabic"],
10101
+ ru: ["Cyrillic"],
10102
+ uk: ["Cyrillic"],
10103
+ bg: ["Cyrillic"],
10104
+ sr: ["Cyrillic"],
10105
+ el: ["Greek"],
10106
+ hi: ["Devanagari"],
10107
+ mr: ["Devanagari"],
10108
+ ne: ["Devanagari"],
10109
+ bn: ["Bengali"],
10110
+ pa: ["Gurmukhi"],
10111
+ gu: ["Gujarati"],
10112
+ ta: ["Tamil"],
10113
+ te: ["Telugu"],
10114
+ th: ["Thai"],
10115
+ ja: ["Hiragana", "Katakana", "Han"],
10116
+ zh: ["Han"],
10117
+ ko: ["Hangul"],
10118
+ ka: ["Georgian"],
10119
+ hy: ["Armenian"],
10120
+ am: ["Ethiopic"]
10121
+ };
10122
+ var SCRIPT_RES = [.../* @__PURE__ */ new Set(["Latin", ...Object.values(SCRIPTS).flat(), "Gurmukhi", "Devanagari"])].map((sc) => [sc, new RegExp(`\\p{Script=${sc}}`, "u")]);
10123
+ var LETTER = new RegExp("\\p{L}", "u");
10124
+ function mainScript(text) {
10125
+ const n = {};
10126
+ for (const ch of text) {
10127
+ if (!LETTER.test(ch)) continue;
10128
+ const sc = SCRIPT_RES.find(([, re]) => re.test(ch))?.[0] ?? "Other";
10129
+ n[sc] = (n[sc] ?? 0) + 1;
10130
+ }
10131
+ return Object.entries(n).sort((a, b) => b[1] - a[1])[0]?.[0] ?? "";
10132
+ }
10133
+ function dropStrayScripts(text, hints) {
10134
+ const main = mainScript(text), foreign = foreignScript(hints, main && main !== "Other" ? [main] : []);
10135
+ if (!/\S/.test(text)) return text;
10136
+ const lead2 = /^\s/.test(text) ? " " : "", trail = /\s$/.test(text) ? " " : "";
10137
+ const kept = text.split(/\s+/).filter((w) => w && !foreign(w));
10138
+ return kept.length ? lead2 + kept.join(" ") + trail : "";
10139
+ }
10140
+ var foreignTests = /* @__PURE__ */ new Map();
10141
+ function foreignScript(hints, also = []) {
10142
+ const key = [...hints, "|", ...also].join(",");
10143
+ let f = foreignTests.get(key);
10144
+ if (f) return f;
10145
+ const scripts = [.../* @__PURE__ */ new Set(["Common", "Inherited", "Latin", ...also, ...hints.flatMap((h) => SCRIPTS[h.slice(0, 2).toLowerCase()] ?? [])])];
10146
+ const ok = new RegExp(`^[${scripts.map((sc) => `\\p{Script_Extensions=${sc}}`).join("")}]$`, "u");
10147
+ f = (text) => {
10148
+ for (const ch of text) if (!ok.test(ch)) return true;
10149
+ return false;
10150
+ };
10151
+ foreignTests.set(key, f);
10152
+ return f;
10153
+ }
9417
10154
 
9418
10155
  // node_modules/@bod.ee/voice/src/adapters/cartesia.ts
9419
10156
  var log17 = forComponent2("CartesiaTTS");
@@ -9424,12 +10161,34 @@ var CartesiaTTSOptions = class {
9424
10161
  model = "sonic-3.5";
9425
10162
  /** 'apiKey' (server/CLI) → `api_key=` URL param; 'token' (browser, BE-minted) → `access_token=`. */
9426
10163
  authMode = "apiKey";
10164
+ /** Cartesia `language` code ('en', 'he', …). Unset → field omitted (provider default). Mutable:
10165
+ * set `tts.options.language` before `newContext()` to switch per context (mixed-language docs) —
10166
+ * it is latched there, so every frame of one context carries the same language. The model must
10167
+ * support it (sonic-3/3.5: 42 languages incl. 'he'; sonic-2/turbo: no Hebrew). */
10168
+ language;
10169
+ /** Cartesia `max_buffer_delay_ms` (integer, 0-5000; provider default 3000): the longest Cartesia
10170
+ * holds `continue: true` text waiting for more before it starts generating. Unset → field omitted
10171
+ * (provider default). Cartesia buffers so tiny inputs (single words/tokens) don't come out choppy
10172
+ * or with unnatural prosody; its guidance is to lower it (0 = generate each frame at once) only
10173
+ * when the caller does its own buffering, i.e. feeds phrase/sentence-sized chunks. Measured: an
10174
+ * unflushed continue frame reaches first audio in ~3 s at the default vs ~0.2 s at 0. A
10175
+ * `continue: false` frame / `end()` flushes regardless. Mutable like `language`: set
10176
+ * `tts.options.maxBufferDelayMs` before `newContext()` — latched there, one value per context. */
10177
+ maxBufferDelayMs;
10178
+ /** Sonic generation_config.emotion ('excited', 'content', 'sad', …). Mutable: the next frame carries it. */
10179
+ emotion;
10180
+ /** Written token → spoken form, swapped before synthesis (whole word, case-sensitive; core/pronounce.ts).
10181
+ * Read at construction. `{}` = off. */
10182
+ pronounce = PRONOUNCE;
9427
10183
  };
9428
10184
  var CartesiaTTS = class _CartesiaTTS {
9429
10185
  options;
9430
10186
  ws;
9431
10187
  ctxSeq = 0;
9432
10188
  ctxId = "";
10189
+ ctxLanguage;
10190
+ ctxMaxBufferDelayMs;
10191
+ say;
9433
10192
  onAudio = () => {
9434
10193
  };
9435
10194
  onDone = () => {
@@ -9469,6 +10228,9 @@ var CartesiaTTS = class _CartesiaTTS {
9469
10228
  static CB_PROBE_MS = 3e4;
9470
10229
  constructor(options) {
9471
10230
  this.options = { ...new CartesiaTTSOptions(), ...options };
10231
+ this.say = new Pronouncer(this.options.pronounce);
10232
+ this.ctxLanguage = this.options.language;
10233
+ this.ctxMaxBufferDelayMs = this.options.maxBufferDelayMs;
9472
10234
  }
9473
10235
  closed = false;
9474
10236
  connecting = null;
@@ -9558,6 +10320,8 @@ var CartesiaTTS = class _CartesiaTTS {
9558
10320
  newContext() {
9559
10321
  this.ctxId = `ctx-${++this.ctxSeq}`;
9560
10322
  this.firstAudioAt = 0;
10323
+ this.ctxLanguage = this.options.language;
10324
+ this.ctxMaxBufferDelayMs = this.options.maxBufferDelayMs;
9561
10325
  return this.ctxId;
9562
10326
  }
9563
10327
  frame(transcript, cont) {
@@ -9565,26 +10329,42 @@ var CartesiaTTS = class _CartesiaTTS {
9565
10329
  model_id: this.options.model,
9566
10330
  transcript,
9567
10331
  voice: { mode: "id", id: this.options.voiceId },
10332
+ ...this.ctxLanguage ? { language: this.ctxLanguage } : {},
9568
10333
  output_format: { container: "raw", encoding: "pcm_s16le", sample_rate: TTS_SAMPLE_RATE },
9569
10334
  context_id: this.ctxId,
9570
10335
  continue: cont,
9571
- ...this.wantTimestamps ? { add_timestamps: true } : {}
10336
+ ...this.ctxMaxBufferDelayMs != null ? { max_buffer_delay_ms: this.ctxMaxBufferDelayMs } : {},
10337
+ // != null: 0 must be sent
10338
+ ...this.wantTimestamps ? { add_timestamps: true } : {},
10339
+ ...this.options.emotion ? { generation_config: { emotion: this.options.emotion } } : {}
9572
10340
  });
9573
10341
  }
9574
10342
  speak(text, cont) {
9575
10343
  if (this.down) return;
10344
+ text = cont ? this.say.feed(text) : this.say.feed(text) + this.say.flush();
9576
10345
  if (cont && !text) return;
9577
10346
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame(text, cont));
9578
10347
  else void this.ensureConnected().then(() => this.ws?.readyState === WebSocket.OPEN && this.ws.send(this.frame(text, cont)));
9579
10348
  }
10349
+ /** Generate what this context has buffered NOW (a `flush: true` frame) — the context stays open and later
10350
+ * continuations keep its prosody. A host streaming word-sized deltas flushes the reply's first clause, so speech
10351
+ * starts there instead of waiting for Cartesia's buffer (a sentence end, or max_buffer_delay_ms). */
10352
+ flush() {
10353
+ if (this.down || !this.ctxId || this.ws?.readyState !== WebSocket.OPEN) return;
10354
+ const held = this.say.flush();
10355
+ if (held) this.ws.send(this.frame(held, true));
10356
+ this.ws.send(JSON.stringify({ ...JSON.parse(this.frame("", true)), flush: true }));
10357
+ }
9580
10358
  end() {
10359
+ const held = this.say.flush();
9581
10360
  if (this.down) {
9582
10361
  this.onDone();
9583
10362
  return;
9584
10363
  }
9585
- if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame("", false));
10364
+ if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame(held, false));
9586
10365
  }
9587
10366
  cancel() {
10367
+ this.say.reset();
9588
10368
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(JSON.stringify({ context_id: this.ctxId, cancel: true }));
9589
10369
  }
9590
10370
  startProbe() {
@@ -10069,7 +10849,7 @@ function pendingQuestion(text, lastWords = 6) {
10069
10849
  const t = sentence.slice(-lastWords).join(" ");
10070
10850
  return /\b(can|could|would|will|do|does|did|is|are|should)\s+(you|we|i|it|there|they|he|she)\b/.test(t) || /(^|\s)(האם|אתה יכול|את יכולה)(\s|$)/u.test(t) || /\b(what|why|how|where|when|who|which)\s+(do|does|did|is|are|should|would|can|could|will)\s+(you|we|i|it|they|he|she)\b/.test(sentence.slice(-4 * lastWords).join(" "));
10071
10851
  }
10072
- var SCRIPTS = [
10852
+ var SCRIPTS2 = [
10073
10853
  [new RegExp("\\p{Script=Hebrew}", "gu"), "he"],
10074
10854
  [new RegExp("\\p{Script=Arabic}", "gu"), "ar"],
10075
10855
  [new RegExp("\\p{Script=Cyrillic}", "gu"), "ru"],
@@ -10091,7 +10871,7 @@ var STOP3 = Object.fromEntries(Object.entries({
10091
10871
  pt: "ol\xE1 oi sim obrigado obrigada o a os as que \xE9 e de em um uma n\xE3o para com mas meu eu voc\xEA",
10092
10872
  nl: "hoi dank de het een en is ik je niet van dat met voor maar mijn wat"
10093
10873
  }).map(([k, v]) => [k, new Set(v.split(" "))]));
10094
- var scriptOf = (w) => SCRIPTS.find(([re]) => {
10874
+ var scriptOf = (w) => SCRIPTS2.find(([re]) => {
10095
10875
  re.lastIndex = 0;
10096
10876
  return re.test(w);
10097
10877
  })?.[1] ?? null;
@@ -10118,7 +10898,7 @@ var languageName = (lang) => {
10118
10898
  }
10119
10899
  };
10120
10900
  function fitsLanguage(line, lang) {
10121
- const own = SCRIPTS.find(([, l]) => l === lang)?.[0];
10901
+ const own = SCRIPTS2.find(([, l]) => l === lang)?.[0];
10122
10902
  if (own) return (line.match(own)?.length ?? 0) > 0 && !new RegExp("\\p{Script=Latin}{2,}", "u").test(line);
10123
10903
  if (/[^\p{Script=Latin}\p{M}\p{P}\p{Z}\p{N}\p{S}]/u.test(line)) return false;
10124
10904
  const mine = STOP3[lang];
@@ -10127,6 +10907,7 @@ function fitsLanguage(line, lang) {
10127
10907
  }
10128
10908
  var norm3 = (s) => s.toLowerCase().replace(/[^\p{L}\p{N}' ]/gu, "").replace(/\s+/g, " ").trim();
10129
10909
  function cleanLine(s, lang, maxWords = 6) {
10910
+ if (/^(?:gasp|quiet|laughter)[.!?…]*$/i.test(s.trim())) return "";
10130
10911
  const first = s.trim().split("\n")[0];
10131
10912
  const tags = [...first.matchAll(LINE_TAG)].map((m) => `[${m[1].toLowerCase()}]`).filter((t, i, a) => a.indexOf(t) === i).join(" ");
10132
10913
  const raw = first.replace(LINE_TAG, " ").trim();
@@ -10389,7 +11170,7 @@ var TurnGate = class {
10389
11170
  }
10390
11171
  /** An STT final. */
10391
11172
  final(text, sttId) {
10392
- let t = text.trim();
11173
+ let t = text.trim(), replaces;
10393
11174
  if (!t) return this.o.onDrop(text, "empty");
10394
11175
  if (this.held) {
10395
11176
  const h = this.held;
@@ -10397,6 +11178,7 @@ var TurnGate = class {
10397
11178
  this.held = void 0;
10398
11179
  if (!(norm3(t) + " ").startsWith(norm3(h.text) + " ")) t = `${h.text} ${t}`;
10399
11180
  sttId ??= h.sttId;
11181
+ replaces = h.replaces;
10400
11182
  }
10401
11183
  const key = norm3(t), now3 = this.o.now(), last = this.last;
10402
11184
  if (last) {
@@ -10410,10 +11192,18 @@ var TurnGate = class {
10410
11192
  const mid = endsMidPhrase(t);
10411
11193
  if (mid || this.o.settleMs && !/\?["')\]]*$/.test(t)) {
10412
11194
  const ms = mid ? this.o.holdMs : this.o.settleMs;
10413
- this.held = { text: t, sttId, ms, timer: setTimeout(() => this.flush(), ms) };
11195
+ this.held = { text: t, sttId, ms, replaces, timer: setTimeout(() => this.flush(), ms) };
10414
11196
  return this.o.onHold(t);
10415
11197
  }
10416
- this.send(t, key, sttId, "new");
11198
+ this.send(t, key, sttId, "new", replaces);
11199
+ }
11200
+ /** The user kept talking after turn `id` went out and before its reply made a sound: it was not over. Its words go back
11201
+ * to being held — the rest merges in and one whole turn replaces it; silence sends them again as they were. */
11202
+ reopen(id) {
11203
+ const l = this.last;
11204
+ if (l?.id !== id || this.held) return;
11205
+ this.last = void 0;
11206
+ this.held = { text: l.text, sttId: l.sttId, ms: this.o.holdMs, replaces: id, timer: setTimeout(() => this.flush(), this.o.holdMs) };
10417
11207
  }
10418
11208
  /** The user is speaking again (a partial): a held fragment waits for the rest. */
10419
11209
  speaking() {
@@ -10432,7 +11222,7 @@ var TurnGate = class {
10432
11222
  clearTimeout(h.timer);
10433
11223
  this.held = void 0;
10434
11224
  if (this.closed && isClosing(h.text)) return this.o.onDrop(h.text, "goodbye");
10435
- this.send(h.text, norm3(h.text), h.sttId, "new");
11225
+ this.send(h.text, norm3(h.text), h.sttId, "new", h.replaces);
10436
11226
  }
10437
11227
  end() {
10438
11228
  if (this.held) {
@@ -12939,7 +13729,137 @@ init_models();
12939
13729
 
12940
13730
  // src/voice/progress.ts
12941
13731
  init_logging();
12942
- var log27 = forComponent("Progress");
13732
+
13733
+ // src/voice/convoTree.ts
13734
+ init_logging();
13735
+
13736
+ // src/voice/backchannelPlan.ts
13737
+ init_logging();
13738
+ var log27 = forComponent("BackchannelPlan");
13739
+ var silentTurn = (p) => p.density === "none" || p.turn === "short_command" || p.turn === "question";
13740
+ var cueWords = (s) => s.toLowerCase().replace(/[^\p{L}\p{N}'\s]+/gu, " ").split(/\s+/).filter(Boolean);
13741
+ function matchCue(plan, heard, from = 0) {
13742
+ if (!plan || silentTurn(plan)) return null;
13743
+ const w = cueWords(heard);
13744
+ for (let i = Math.max(0, from); i < w.length; i++) {
13745
+ for (const c of plan.cues) {
13746
+ for (const p of c.when) {
13747
+ const pw = p.split(" ");
13748
+ if (i + pw.length <= w.length && pw.every((x, j) => w[i + j] === x)) return c;
13749
+ }
13750
+ }
13751
+ }
13752
+ return null;
13753
+ }
13754
+
13755
+ // src/voice/convoTree.ts
13756
+ var log28 = forComponent("ConvoTree");
13757
+ var SOFT_CUE = "~";
13758
+ var STOP4 = new Set("about after again also always been before being could does doing down even every from going have here into just know like look make many more most much must never only other over really right same should some still such sure take than that their them then there these they thing think this those through very want well were what when where which while will with would your yeah okay".split(" "));
13759
+ var isContent = (w) => w.length >= 4 && !STOP4.has(w);
13760
+ function echoesHeard(line, heard) {
13761
+ const said = new Set(cueWords(heard));
13762
+ return cueWords(lineWords(line)).some((w) => isContent(w) && said.has(w));
13763
+ }
13764
+ var leafKey = (line) => cueWords(lineWords(line)).join(" ");
13765
+ var ConvoTreeOptions = class {
13766
+ /** A leaf plays at this confidence or above. */
13767
+ threshold = 0.5;
13768
+ /** How much more likely a cue of one word / of two or more is if its branch is the true one. */
13769
+ cueEvidence = 3;
13770
+ phraseEvidence = 8;
13771
+ /** A hit weakens each sibling by this factor. */
13772
+ siblingDecay = 0.5;
13773
+ /** A cue counts only within the newest N words: older ones have passed, a reaction to them would be late. */
13774
+ cueWindow = 4;
13775
+ };
13776
+ var namesHeard = (n, said) => (n.names ?? []).every((w) => said.has(w));
13777
+ function echoHeard(node, heard) {
13778
+ if (node.when[0] === SOFT_CUE) return true;
13779
+ const said = new Set(cueWords(heard));
13780
+ return node.when.flatMap((w) => w.split(" ")).some((w) => isContent(w) && said.has(w)) && namesHeard(node, said);
13781
+ }
13782
+ var ConvoTree = class {
13783
+ // a turn_end leaf waiting for the turn to end
13784
+ constructor(options) {
13785
+ this.options = options;
13786
+ this.options = { ...new ConvoTreeOptions(), ...options };
13787
+ }
13788
+ options;
13789
+ live = [];
13790
+ from = 0;
13791
+ // word index after the last hit
13792
+ armed = null;
13793
+ get o() {
13794
+ return this.options;
13795
+ }
13796
+ get nodes() {
13797
+ return this.live;
13798
+ }
13799
+ get empty() {
13800
+ return !this.live.length;
13801
+ }
13802
+ /** A fresh tree. `heardWords`: words already said this turn (a tree grown mid-turn matches only what comes next). */
13803
+ grow(nodes, heardWords = 0) {
13804
+ this.live = nodes.map((n) => ({ ...n }));
13805
+ this.armed = null;
13806
+ this.from = heardWords;
13807
+ }
13808
+ /** Posterior of a node after seeing its cue. */
13809
+ confidence(p, words3) {
13810
+ const l = p * (words3 > 1 ? this.o.phraseEvidence : this.o.cueEvidence);
13811
+ return l / (l + 1 - p);
13812
+ }
13813
+ /** The partial's newest evidence: the strongest new cue hit (its children open, its siblings fade), or null. A turn_end
13814
+ * hit is armed for endTurn; the caller plays the others. Hits under the threshold still sharpen the tree. */
13815
+ step(heard) {
13816
+ const w = cueWords(heard), said = new Set(w);
13817
+ let best = null;
13818
+ for (let i = Math.max(this.from, w.length - this.o.cueWindow); i < w.length; i++) {
13819
+ for (const node2 of this.live) for (const cue of node2.when) {
13820
+ const pw = cue.split(" ");
13821
+ if (!namesHeard(node2, said) || i + pw.length > w.length || !pw.every((x, j) => w[i + j] === x)) continue;
13822
+ const conf2 = this.confidence(node2.p, pw.length);
13823
+ if (!best || conf2 > best.conf) best = { node: node2, conf: conf2, end: i + pw.length };
13824
+ }
13825
+ }
13826
+ if (!best) return null;
13827
+ const { node, conf } = best;
13828
+ this.from = best.end;
13829
+ this.take(node, conf);
13830
+ const hit = { node, conf };
13831
+ if ((node.moment === "turn_end" || node.moment === "ask") && conf >= (this.armed?.conf ?? 0)) {
13832
+ this.armed = hit;
13833
+ return null;
13834
+ }
13835
+ return conf >= this.o.threshold ? hit : null;
13836
+ }
13837
+ /** A node happened: it leaves the live set, its siblings fade, its children open. */
13838
+ take(node, conf) {
13839
+ this.live = [...this.live.filter((n) => n !== node).map((n) => ({ ...n, p: n.p * this.o.siblingDecay })), ...node.next.map((n) => ({ ...n, p: Math.min(1, n.p * Math.max(conf, 0.5) * 1.5) }))];
13840
+ }
13841
+ /** The turn ended: the armed leaf if it clears the threshold. The live set stays for a picker; `advance` clears it. */
13842
+ turnLeaf() {
13843
+ const a = this.armed;
13844
+ return a && a.conf >= this.o.threshold ? a : null;
13845
+ }
13846
+ /** A quick model named a node at the turn's end: its evidence counts as a phrase. Null when under the threshold. */
13847
+ picked(i, heard = "") {
13848
+ const node = this.live[i];
13849
+ if (!node || !echoHeard(node, heard)) return null;
13850
+ const conf = this.confidence(node.p, 2);
13851
+ return conf >= this.o.threshold ? { node, conf } : null;
13852
+ }
13853
+ /** Next turn: what followed a played/armed branch stays live until a fresh tree lands; the rest is gone. */
13854
+ advance() {
13855
+ this.live = this.live.filter((n) => n.p >= 0.05);
13856
+ this.armed = null;
13857
+ this.from = 0;
13858
+ }
13859
+ };
13860
+
13861
+ // src/voice/progress.ts
13862
+ var log29 = forComponent("Progress");
12943
13863
  var HE_RESULT_CLAIM = /(?:^|\s)(?:מצאתי|סיימתי|בוצע|הנה|אין)(?=\s|$|[.,!…])/u;
12944
13864
  var TOPICS = [
12945
13865
  [/toolsearch|^mcp__.*__(list_commands|call_info)$/, null],
@@ -13063,8 +13983,8 @@ var Progress = class {
13063
13983
  this.said.add(norm3(line));
13064
13984
  this.lastAt = this.o.now();
13065
13985
  }
13066
- /** The line for this tool call in `lang`, or '' (no template and no/late/invalid generation, a repeat, a result claim). */
13067
- async line(tool, args, lang) {
13986
+ /** `about`: the user's request, for a generated line. The line for this tool call in `lang`, or '' (no template and no/late/invalid generation, a repeat, a result claim). */
13987
+ async line(tool, args, lang, about = "") {
13068
13988
  if (this.streamed) return "";
13069
13989
  const name = tool.toLowerCase();
13070
13990
  const hit = TOPICS.find(([re]) => re.test(name));
@@ -13083,18 +14003,18 @@ var Progress = class {
13083
14003
  r(null);
13084
14004
  }, this.o.generateBudgetMs);
13085
14005
  });
13086
- const raw = await Promise.race([this.o.generate(tool, argsHint(args), lang, ac.signal).catch((e) => {
13087
- log27.warn(`progress line failed: ${e?.message ?? e}`);
14006
+ const raw = await Promise.race([this.o.generate(tool, [about.slice(-80), argsHint(args)].filter(Boolean).join("; "), lang, ac.signal).catch((e) => {
14007
+ log29.warn(`progress line failed: ${e?.message ?? e}`);
13088
14008
  return null;
13089
14009
  }), late]).finally(() => clearTimeout(t));
13090
14010
  const l = cleanLine(raw ?? "", lang, 5);
13091
- if (!l || RESULT_CLAIM_RE.test(l) || HE_RESULT_CLAIM.test(l) || this.said.has(norm3(l)) || this.streamed) return "";
14011
+ if (!l || about && !echoesHeard(l, `${about} ${argsHint(args)}`) || RESULT_CLAIM_RE.test(l) || HE_RESULT_CLAIM.test(l) || this.said.has(norm3(l)) || this.streamed) return "";
13092
14012
  return l;
13093
14013
  }
13094
14014
  };
13095
14015
  function fastProgressGenerator(ai, model, gender) {
13096
14016
  return async (tool, hint, lang, signal) => {
13097
- const prompt = `An assistant on a live voice call is running the tool "${tool}"${hint ? ` (about: "${hint}")` : ""}. Write ONE spoken line of 2-5 words, in ${languageName(lang)}, saying what it is doing right now (like "checking the board\u2026"). Never state a result or a finding. ${personaHint(gender)} Output only the line.`;
14017
+ const prompt = `An assistant on a live voice call is running the tool "${tool}"${hint ? ` (about: "${hint}")` : ""}. Write ONE spoken line of 2-5 words, in ${languageName(lang)}, saying what it is doing right now (like "checking the board\u2026"). ${hint ? "Use the words of what it is about. " : ""}Never state a result or a finding. ${personaHint(gender)} Output only the line.`;
13098
14018
  const reasoning = /gpt-oss/.test(model) ? { providerOptions: { reasoning_effort: "low" } } : {};
13099
14019
  const r = await ai.chat({ model, messages: [{ role: "user", content: prompt }], stream: false, signal, maxTokens: 150, ...reasoning });
13100
14020
  return r.content ?? "";
@@ -13103,28 +14023,7 @@ function fastProgressGenerator(ai, model, gender) {
13103
14023
 
13104
14024
  // src/voice/secondChannel.ts
13105
14025
  init_logging();
13106
-
13107
- // src/voice/backchannelPlan.ts
13108
- init_logging();
13109
- var log28 = forComponent("BackchannelPlan");
13110
- var silentTurn = (p) => p.density === "none" || p.turn === "short_command" || p.turn === "question";
13111
- var cueWords = (s) => s.toLowerCase().replace(/[^\p{L}\p{N}'\s]+/gu, " ").split(/\s+/).filter(Boolean);
13112
- function matchCue(plan, heard, from = 0) {
13113
- if (!plan || silentTurn(plan)) return null;
13114
- const w = cueWords(heard);
13115
- for (let i = Math.max(0, from); i < w.length; i++) {
13116
- for (const c of plan.cues) {
13117
- for (const p of c.when) {
13118
- const pw = p.split(" ");
13119
- if (i + pw.length <= w.length && pw.every((x, j) => w[i + j] === x)) return c;
13120
- }
13121
- }
13122
- }
13123
- return null;
13124
- }
13125
-
13126
- // src/voice/secondChannel.ts
13127
- var log29 = forComponent("SecondChannel");
14026
+ var log30 = forComponent("SecondChannel");
13128
14027
  var CHANNEL_MODES = {
13129
14028
  turn: { kinds: ["bridge"], bridgeDelayMs: 1500 },
13130
14029
  normal: {},
@@ -13143,10 +14042,10 @@ var CHANNEL_MODES = {
13143
14042
  maxPerWindow: 6
13144
14043
  },
13145
14044
  live: {
13146
- kinds: ["backchannel"],
14045
+ kinds: ["backchannel", "bridge"],
13147
14046
  anticipate: true,
13148
14047
  cueOnly: false,
13149
- generateFor: ["backchannel"],
14048
+ generateFor: ["backchannel", "bridge"],
13150
14049
  reactionGapMs: 0,
13151
14050
  generateBudgetMs: 400,
13152
14051
  backchannelMinWords: 3,
@@ -13196,6 +14095,8 @@ var SecondChannelOptions = class {
13196
14095
  decideEveryWords = 3;
13197
14096
  decisionBudgetMs = 300;
13198
14097
  generateBudgetMs = 500;
14098
+ /** `live`: how long the fast model may take on a bridge line; later = silence. */
14099
+ bridgeBudgetMs = 1200;
13199
14100
  minGapMs = 2500;
13200
14101
  /** Backchannel after backchannel needs this gap (~4-6 cues in a 30s monologue — Para phone call 2026-10-13: 5s left
13201
14102
  * one cue in a long statement). */
@@ -13249,6 +14150,31 @@ var SecondChannelOptions = class {
13249
14150
  cueOnly = true;
13250
14151
  onPlan = () => {
13251
14152
  };
14153
+ /** `live` only (convoTree.ts): grows the prediction tree after each reply. Once a tree is up it is the only source of
14154
+ * lines while the user talks: a leaf above its threshold, or silence. No tree yet / a failed one: the reactive path. */
14155
+ grow;
14156
+ /** The quick model at a turn's end with no cue hit: names a leaf or none, inside `pickBudgetMs`. */
14157
+ pickLeaf;
14158
+ pickBudgetMs = 700;
14159
+ growBudgetMs = 15e3;
14160
+ tree = {};
14161
+ /** The tree grower's persona (who is speaking). */
14162
+ persona = "";
14163
+ /** Lines of talk the grower sees. */
14164
+ talkLines = 8;
14165
+ /** Mid-turn: at this many words the tree regrows once from what they are saying now. */
14166
+ regrowAtWords = 6;
14167
+ /** A leaf is dropped once the talk moved this many words past its cue. */
14168
+ leafStaleWords = 3;
14169
+ /** Interims stop this long: the quick model may pick a leaf (shorter than the ~400ms word cadence, so most gaps count; leafGated spaces the calls). */
14170
+ leafBreathMs = 200;
14171
+ /** Played leaves the grower must avoid, and how many lines pass before a tag may play again. */
14172
+ leafMemory = 16;
14173
+ tagEvery = 4;
14174
+ onTree = () => {
14175
+ };
14176
+ onLeaf = () => {
14177
+ };
13252
14178
  now = () => performance.now();
13253
14179
  random = Math.random;
13254
14180
  };
@@ -13257,15 +14183,15 @@ function withBudget(p, ms, what, ac) {
13257
14183
  let t;
13258
14184
  const late = new Promise((r) => {
13259
14185
  t = setTimeout(() => {
13260
- log29.debug(`${what}: over the ${ms}ms budget \u2014 dropped`);
14186
+ log30.debug(`${what}: over the ${ms}ms budget \u2014 dropped`);
13261
14187
  ac?.abort();
13262
14188
  r(null);
13263
14189
  }, ms);
13264
14190
  });
13265
14191
  return Promise.race([p.catch((e) => {
13266
14192
  const msg = String(e?.message ?? e);
13267
- if (e?.name === "AbortError" || /aborted/i.test(msg)) log29.debug(`${what}: aborted after its budget`);
13268
- else log29.warn(`${what} failed \u2014 falling back: ${msg}`);
14193
+ if (e?.name === "AbortError" || /aborted/i.test(msg)) log30.debug(`${what}: aborted after its budget`);
14194
+ else log30.warn(`${what} failed \u2014 falling back: ${msg}`);
13269
14195
  return null;
13270
14196
  }), late]).finally(() => clearTimeout(t));
13271
14197
  }
@@ -13278,6 +14204,7 @@ var SecondChannel = class {
13278
14204
  for (const k of Object.keys(preset)) if (!options || !(k in options)) this.normalTiming[k] = base[k];
13279
14205
  this.startedAt = this.o.now();
13280
14206
  if (this.o.lang) this.setLanguage(this.o.lang, true);
14207
+ if (this.o.mode === "live" && this.o.grow) this.tree = new ConvoTree(this.o.tree);
13281
14208
  }
13282
14209
  options;
13283
14210
  heard = "";
@@ -13306,6 +14233,12 @@ var SecondChannel = class {
13306
14233
  // one bridge per main turn
13307
14234
  bridgeDraft = null;
13308
14235
  // a bridge generated ahead, used only if ready in time
14236
+ bridgeReady = Promise.resolve();
14237
+ // live: that draft settled
14238
+ askLeaf = null;
14239
+ // live: this turn's ask leaf
14240
+ leafReady = Promise.resolve();
14241
+ // live: the turn-end leaf pick settled
13309
14242
  recent = [];
13310
14243
  picks = /* @__PURE__ */ new Map();
13311
14244
  inflight = null;
@@ -13333,6 +14266,21 @@ var SecondChannel = class {
13333
14266
  normalTiming = {};
13334
14267
  // chatty without aec: the preset's keys back at their defaults
13335
14268
  degradedLogged = false;
14269
+ tree = null;
14270
+ // live with a grower only
14271
+ growing = false;
14272
+ growTries = 0;
14273
+ // per turn
14274
+ replyGrown = false;
14275
+ // one tree per reply (idle comes more than once)
14276
+ talk = [];
14277
+ leaves = [];
14278
+ // leaves played this call
14279
+ sinceTag = Infinity;
14280
+ regrown = false;
14281
+ picking = false;
14282
+ breathTimer = null;
14283
+ leafTimer = null;
13336
14284
  /** The language lines are spoken in (detected from the user's words, or set by the host). */
13337
14285
  get language() {
13338
14286
  return this.lang;
@@ -13344,7 +14292,7 @@ var SecondChannel = class {
13344
14292
  if (host) this.pinned = true;
13345
14293
  const lang = primaryLang(tag);
13346
14294
  if (!lang || lang === this.lang) return;
13347
- log29.debug(`language ${this.lang} \u2192 ${lang}`);
14295
+ log30.debug(`language ${this.lang} \u2192 ${lang}`);
13348
14296
  this.lang = lang;
13349
14297
  this.recent = [];
13350
14298
  this.picks.clear();
@@ -13357,6 +14305,10 @@ var SecondChannel = class {
13357
14305
  const l = detectLanguage(text, !!this.lang);
13358
14306
  if (l) this.setLanguage(l);
13359
14307
  }
14308
+ /** `live` speaks no stock bridge/filler, with or without aec. */
14309
+ get ownWords() {
14310
+ return this.options.mode === "live";
14311
+ }
13360
14312
  get bank() {
13361
14313
  return this.lang ? this.o.banks ? this.o.banks.get(this.lang) : this.lang === "en" ? EN_BANK : null : null;
13362
14314
  }
@@ -13380,7 +14332,23 @@ var SecondChannel = class {
13380
14332
  this.onPause(this.o.fillerPauseMs);
13381
14333
  }, this.o.fillerPauseMs);
13382
14334
  let cue = null;
13383
- if (this.o.anticipate) {
14335
+ if (this.tree) {
14336
+ if (this.tree.empty && !this.growing && this.growTries < 2) this.requestTree();
14337
+ if (!this.tree.empty) {
14338
+ this.treeStep(t);
14339
+ if (!this.regrown && words2(t).length >= this.o.regrowAtWords) {
14340
+ this.regrown = true;
14341
+ this.requestTree(t);
14342
+ }
14343
+ this.armBreath(t);
14344
+ if (this.bcTimer) {
14345
+ clearTimeout(this.bcTimer);
14346
+ this.bcTimer = null;
14347
+ }
14348
+ return;
14349
+ }
14350
+ }
14351
+ if (this.o.anticipate && !this.tree) {
13384
14352
  const n = words2(t).length;
13385
14353
  if (!this.plan && !this.planning && this.planTries < 2) this.requestPlan();
13386
14354
  else if (this.plan && !this.revised && n >= this.o.reviseAtWords) {
@@ -13425,11 +14393,24 @@ var SecondChannel = class {
13425
14393
  this.decidedAt = 0;
13426
14394
  this.bridged = false;
13427
14395
  this.cueFrom = 0;
13428
- if (this.o.anticipate && this.reply.trim() && !this.plan && !this.planning) this.requestPlan();
14396
+ if (this.tree) {
14397
+ if (this.reply.trim() && !this.replyGrown) {
14398
+ this.replyGrown = true;
14399
+ this.noteTalk("you", this.reply);
14400
+ this.requestTree();
14401
+ }
14402
+ } else if (this.o.anticipate && this.reply.trim() && !this.plan && !this.planning) this.requestPlan();
13429
14403
  return;
13430
14404
  }
13431
14405
  if (s === "queued") {
13432
14406
  this.turnNo++;
14407
+ this.growTries = 0;
14408
+ this.replyGrown = false;
14409
+ this.regrown = false;
14410
+ if (this.breathTimer) {
14411
+ clearTimeout(this.breathTimer);
14412
+ this.breathTimer = null;
14413
+ }
13433
14414
  this.plan = null;
13434
14415
  this.planning = false;
13435
14416
  this.planTries = 0;
@@ -13443,7 +14424,7 @@ var SecondChannel = class {
13443
14424
  if (this[k]) clearTimeout(this[k]);
13444
14425
  this[k] = null;
13445
14426
  }
13446
- if (this.o.instantBridge) this.prefetchBridge();
14427
+ if (this.o.instantBridge || this.ownWords) this.prefetchBridge();
13447
14428
  const wait = Math.max(this.o.bridgeDelayMs, this.o.minGapMs - (this.o.now() - this.lastAt));
13448
14429
  if (wait > 0) this.bridgeTimer = setTimeout(() => {
13449
14430
  this.bridgeTimer = null;
@@ -13470,6 +14451,10 @@ var SecondChannel = class {
13470
14451
  this.detect(this.heard);
13471
14452
  this.main = "idle";
13472
14453
  this.onAgentState("queued");
14454
+ if (this.tree) {
14455
+ this.noteTalk("them", this.heard);
14456
+ this.leafReady = this.turnEndLeaf();
14457
+ }
13473
14458
  }
13474
14459
  /** The main answer's streamed text (before its audio): channel-1 is responding — no bridge from here on. */
13475
14460
  onMainTokens(text) {
@@ -13489,7 +14474,7 @@ var SecondChannel = class {
13489
14474
  try {
13490
14475
  this.o.cancel();
13491
14476
  } catch (e) {
13492
- log29.warn(`cancel threw: ${e.message}`);
14477
+ log30.warn(`cancel threw: ${e.message}`);
13493
14478
  }
13494
14479
  }
13495
14480
  }
@@ -13500,6 +14485,10 @@ var SecondChannel = class {
13500
14485
  this.cutLine = null;
13501
14486
  return !!c && c === text.trim();
13502
14487
  }
14488
+ /** Another fill line (progress) already covers this turn's wait: no bridge after it. */
14489
+ covered() {
14490
+ this.bridged = true;
14491
+ }
13503
14492
  /** Resolves when the in-flight line finished playing (immediately when none) — hosts enqueue channel-1 audio behind it. */
13504
14493
  drain() {
13505
14494
  return this.inflight ?? Promise.resolve();
@@ -13534,6 +14523,7 @@ var SecondChannel = class {
13534
14523
  bridgeBlock() {
13535
14524
  const w = words2(this.heard), intent = turnIntent(this.heard);
13536
14525
  if (!this.lang) return "language unknown";
14526
+ if (this.ownWords) return this.blocked("bridge");
13537
14527
  if (intent === "closer") return "no answer due";
13538
14528
  if (this.late) return this.blocked("bridge");
13539
14529
  const why = intent === "ask" ? w.length <= 6 && isPhatic(this.heard, this.bank) ? "phatic" : "" : this.shortTurn(w.length);
@@ -13558,7 +14548,7 @@ var SecondChannel = class {
13558
14548
  if (!this.lang) return "silence";
13559
14549
  if (trigger === "working") return this.bridgeBlock() ? "silence" : "bridge";
13560
14550
  if (w.length <= this.o.shortTurnWords || w.length <= 6 && isPhatic(this.heard, this.bank)) return "silence";
13561
- const k = trigger === "interim" ? this.backchannelMoment(w.length, breath) && (!this.o.anticipate || !this.plan || !silentTurn(this.plan) && (!!cue || !this.o.cueOnly && this.plan.density !== "low")) ? "backchannel" : null : trigger === "pause" ? pauseMs >= this.o.fillerPauseMs && w.length >= this.o.fillerMinWords ? "filler" : null : null;
14551
+ const k = trigger === "interim" ? this.backchannelMoment(w.length, breath) && (!this.o.anticipate || !this.plan || !silentTurn(this.plan) && (!!cue || !this.o.cueOnly && this.plan.density !== "low")) ? "backchannel" : null : trigger === "pause" ? pauseMs >= this.o.fillerPauseMs && w.length >= this.o.fillerMinWords && !(this.ownWords && /[.!?…]$/.test(this.heard)) ? "filler" : null : null;
13562
14552
  return k && this.allowed(k) ? k : "silence";
13563
14553
  }
13564
14554
  /** Plan (or, with `heard`, revise) the backchannels of the user's turn — async, never on the hot path. */
@@ -13579,6 +14569,106 @@ var SecondChannel = class {
13579
14569
  this.o.onPlan(p, !!heard, Math.round(this.o.now() - t0));
13580
14570
  });
13581
14571
  }
14572
+ noteTalk(who, text) {
14573
+ this.talk = [...this.talk, `${who}: ${text.trim().slice(-300)}`].slice(-this.o.talkLines);
14574
+ }
14575
+ /** Grow the tree off the hot path. A tree that lands after its turn is still the best guess: it replaces what is live. */
14576
+ requestTree(heard) {
14577
+ if (!this.tree || !this.o.grow || !this.lang || this.growing) return;
14578
+ this.growing = true;
14579
+ this.growTries++;
14580
+ const t0 = this.o.now(), ac = new AbortController(), tree = this.tree;
14581
+ const ctx = { talk: heard ? [...this.talk, `them (still talking): ${heard.slice(-240)}`] : [...this.talk], lang: detectLanguage(this.talk.join(" ").replace(/\b(them|you):/g, "")) ?? this.lang, recent: this.leaves.slice(-this.o.leafMemory), ...this.o.persona ? { persona: this.o.persona } : {} };
14582
+ const late = setTimeout(() => ac.abort(), this.o.growBudgetMs);
14583
+ void this.o.grow(ctx, ac.signal).then((r) => {
14584
+ if (r.nodes.length) tree.grow(r.nodes, this.userSpeaking ? words2(this.heard).length : 0);
14585
+ this.o.onTree({ ms: Math.round(this.o.now() - t0), branches: r.nodes.length, lines: r.nodes.flatMap((n) => [n.line, ...n.next.map((k) => k.line)]), ...r.tokens ? { tokens: r.tokens } : {} });
14586
+ }, (e) => {
14587
+ log30.warn(`tree failed: ${e?.message ?? e}`);
14588
+ this.o.onTree({ ms: Math.round(this.o.now() - t0), branches: 0, error: String(e?.message ?? e) });
14589
+ }).finally(() => {
14590
+ clearTimeout(late);
14591
+ this.growing = false;
14592
+ });
14593
+ }
14594
+ /** Text match on the partial (no model). barge_in plays now, in_between at the next breath; turn_end waits in the tree. */
14595
+ treeStep(t) {
14596
+ const hit = this.tree.step(t);
14597
+ if (!hit) return;
14598
+ if (hit.node.moment === "barge_in") return this.playLeaf(hit, "interim", "backchannel");
14599
+ if (this.leafTimer) clearTimeout(this.leafTimer);
14600
+ const at = words2(t).length;
14601
+ this.leafTimer = setTimeout(() => {
14602
+ this.leafTimer = null;
14603
+ if (this.userSpeaking && words2(this.heard).length - at < this.o.leafStaleWords) this.playLeaf(hit, "interim", "backchannel");
14604
+ }, this.o.cuePauseMs);
14605
+ }
14606
+ /** A breath with no cue hit: the quick model may name the leaf that fits the words just said. Its answer plays only if
14607
+ * they have not moved on. */
14608
+ armBreath(t) {
14609
+ if (this.breathTimer) clearTimeout(this.breathTimer);
14610
+ if (!this.o.pickLeaf || !this.o.aec) return;
14611
+ this.breathTimer = setTimeout(() => {
14612
+ this.breathTimer = null;
14613
+ const n = words2(t).length, tree = this.tree;
14614
+ if (n < this.o.backchannelMinWords || this.heard !== t || this.leafTimer || this.picking || tree.empty || this.leafGated()) return;
14615
+ this.picking = true;
14616
+ const ac = new AbortController(), nodes = tree.nodes.filter((x) => x.moment !== "ask"), lines = nodes.map((x, i) => ({ i, line: x.line, when: x.when }));
14617
+ void withBudget(this.o.pickLeaf(t, lines, ac.signal), this.o.pickBudgetMs, "breath pick", ac).then((i) => {
14618
+ this.picking = false;
14619
+ const node = typeof i === "number" ? nodes[i] : void 0, hit = node && tree.nodes.includes(node) ? tree.picked(tree.nodes.indexOf(node), this.heard) : null;
14620
+ if (!hit || !this.userSpeaking || words2(this.heard).length - n >= this.o.leafStaleWords) return;
14621
+ tree.take(hit.node, hit.conf);
14622
+ this.playLeaf(hit, "interim", "backchannel");
14623
+ });
14624
+ }, this.o.leafBreathMs);
14625
+ }
14626
+ /** The user's turn went to MAIN: the armed leaf at once, else the quick model may name one in time. It plays only while
14627
+ * MAIN is still silent, as the turn's first sound. */
14628
+ async turnEndLeaf() {
14629
+ const tree = this.tree, gen = this.gen, heard = this.heard;
14630
+ let hit = tree.turnLeaf();
14631
+ if (!hit && this.o.pickLeaf && !tree.empty) {
14632
+ const ac = new AbortController(), lines = tree.nodes.map((n, i2) => ({ i: i2, line: n.line, when: n.when }));
14633
+ const i = await withBudget(this.o.pickLeaf(heard, lines, ac.signal), this.o.pickBudgetMs, "leaf pick", ac);
14634
+ if (typeof i === "number" && i >= 0) hit = tree.picked(i, heard);
14635
+ }
14636
+ tree.advance();
14637
+ if (hit?.node.moment === "ask") {
14638
+ if (gen === this.gen && echoesHeard(hit.node.line, heard)) this.askLeaf = { gen, text: hit.node.line };
14639
+ return;
14640
+ }
14641
+ if (hit && gen === this.gen && (this.main === "queued" || this.main === "thinking")) {
14642
+ this.playLeaf(hit, "working", "bridge");
14643
+ if (this.lastKind === "bridge" && this.bridged) {
14644
+ this.bridged = false;
14645
+ this.lastKind = null;
14646
+ if (this.bridgeTimer) clearTimeout(this.bridgeTimer);
14647
+ this.bridgeTimer = setTimeout(() => {
14648
+ this.bridgeTimer = null;
14649
+ void this.consider("working");
14650
+ }, this.o.minGapMs);
14651
+ }
14652
+ }
14653
+ }
14654
+ leafGated() {
14655
+ return !this.lang || this.audioLocked || !!this.inflight || this.o.now() - this.lastAt < this.o.minGapMs || this.capped();
14656
+ }
14657
+ /** A leaf's gates: never over MAIN's audio or another line, spaced, capped; never a repeat. */
14658
+ playLeaf(hit, trigger, kind) {
14659
+ const { moment } = hit.node, conf = Math.round(hit.conf * 100) / 100;
14660
+ const tagged = hit.node.line !== lineWords(hit.node.line);
14661
+ const line = tagged && this.sinceTag < this.o.tagEvery ? lineWords(hit.node.line) : hit.node.line;
14662
+ const gated = !line || this.leafGated() || this.leaves.includes(leafKey(line));
14663
+ const said = !gated && this.say(kind, line);
14664
+ if (said) {
14665
+ this.leaves = [...this.leaves, leafKey(line)].slice(-this.o.leafMemory);
14666
+ this.sinceTag = line === hit.node.line && tagged ? 0 : this.sinceTag + 1;
14667
+ }
14668
+ const e = { trigger, rule: kind, kind, text: line, ms: 0, moment, conf, acted: gated ? "gated" : said ? "spoken" : "skipped" };
14669
+ this.o.onEvent(e);
14670
+ this.o.onLeaf(e);
14671
+ }
13582
14672
  /** A clause boundary mid-floor: a breath or a comma-like close. Not a question (it wants the answer, not "mm-hm"), and a
13583
14673
  * sentence end only deep into a story — a short request's end is the end of their turn (replay: "mm-hm" right after
13584
14674
  * "Check hacker news for recent news.", just before the final). */
@@ -13601,7 +14691,7 @@ var SecondChannel = class {
13601
14691
  const opts = this.options;
13602
14692
  if (opts.mode === "chatty" && !opts.aec && !this.degradedLogged) {
13603
14693
  this.degradedLogged = true;
13604
- log29.info("chatty mode without echo cancellation \u2014 normal timing (our lines would re-trigger the mic)");
14694
+ log30.info("chatty mode without echo cancellation \u2014 normal timing (our lines would re-trigger the mic)");
13605
14695
  }
13606
14696
  const rule = this.rule(trigger, pauseMs, breath, cue);
13607
14697
  if (rule === "silence") {
@@ -13620,7 +14710,7 @@ var SecondChannel = class {
13620
14710
  const d = await withBudget(this.o.decide(this.tail(), { ...this.state(pauseMs), rule }, ac.signal), this.o.decisionBudgetMs, "channel decision", ac);
13621
14711
  choice = d?.choice;
13622
14712
  if (choice && choice in CHANNEL_ACTIONS && (choice === "silence" || this.allowed(choice))) kind = choice;
13623
- else if (choice && choice in CHANNEL_ACTIONS) log29.debug(`decider chose ${choice} for a ${rule} moment \u2014 the rule stands`);
14713
+ else if (choice && choice in CHANNEL_ACTIONS) log30.debug(`decider chose ${choice} for a ${rule} moment \u2014 the rule stands`);
13624
14714
  }
13625
14715
  if (kind === "silence") return ev({ choice, acted: "silence" });
13626
14716
  if (stale()) return ev({ choice, kind, acted: "stale" });
@@ -13652,15 +14742,28 @@ var SecondChannel = class {
13652
14742
  /** Start writing this turn's bridge now, so it may be ready by bridgeDelayMs. The bridge never waits for it. */
13653
14743
  prefetchBridge() {
13654
14744
  this.bridgeDraft = null;
13655
- if (!this.o.generate || !this.o.generateFor.includes("bridge") || !this.lang || !this.heard) return;
13656
- const gen = this.gen, ac = new AbortController();
13657
- const ctx = { kind: "bridge", heard: this.tail(), state: this.main, avoid: [...this.recent], lang: this.lang };
13658
- void withBudget(this.o.generate(ctx, ac.signal), Math.max(this.o.generateBudgetMs, this.o.bridgeDelayMs), "bridge generate", ac).then((raw) => {
13659
- const text = cleanLine(raw ?? "", ctx.lang);
13660
- if (text && gen === this.gen) this.bridgeDraft = { gen, text };
13661
- }).catch((e) => log29.debug(`bridge prefetch failed: ${e?.message ?? e}`));
14745
+ this.askLeaf = null;
14746
+ this.bridgeReady = Promise.resolve();
14747
+ if (!this.o.generate || !(this.ownWords || this.o.generateFor.includes("bridge")) || !this.lang || !this.heard) return;
14748
+ const gen = this.gen, ac = new AbortController(), own = this.ownWords, heard = this.heard;
14749
+ const ctx = { kind: "bridge", heard: this.tail(), state: this.main, avoid: [...this.recent], lang: this.lang, ...own ? { ask: true } : {} };
14750
+ this.bridgeReady = withBudget(this.o.generate(ctx, ac.signal), own ? this.o.bridgeBudgetMs : Math.max(this.o.generateBudgetMs, this.o.bridgeDelayMs), "bridge generate", ac).then((raw) => {
14751
+ const text = own && /^\s*none\b/i.test(raw ?? "") ? "" : cleanLine(raw ?? "", ctx.lang);
14752
+ if (text && gen === this.gen && (!own || echoesHeard(text, heard))) this.bridgeDraft = { gen, text };
14753
+ }).catch((e) => log30.debug(`bridge prefetch failed: ${e?.message ?? e}`));
13662
14754
  }
13663
14755
  async line(kind, react = true) {
14756
+ if (kind === "bridge" && this.ownWords) {
14757
+ const gen = this.gen;
14758
+ await Promise.all([this.leafReady, this.bridgeReady]);
14759
+ if (gen !== this.gen) return "";
14760
+ const leaf = this.askLeaf?.gen === gen ? this.askLeaf.text : "", d = this.bridgeDraft?.gen === gen ? this.bridgeDraft.text : "";
14761
+ if (leaf && !this.leaves.includes(leafKey(leaf))) {
14762
+ this.leaves = [...this.leaves, leafKey(leaf)].slice(-this.o.leafMemory);
14763
+ return leaf;
14764
+ }
14765
+ return d && !this.recent.some((r) => norm3(r) === norm3(d)) ? d : "";
14766
+ }
13664
14767
  if (kind === "bridge" && this.o.instantBridge) {
13665
14768
  const d = this.bridgeDraft?.gen === this.gen ? this.bridgeDraft.text : "";
13666
14769
  this.bridgeDraft = null;
@@ -13674,13 +14777,14 @@ var SecondChannel = class {
13674
14777
  if (raw === null) ac.abort();
13675
14778
  const text = cleanLine(raw ?? "", this.lang);
13676
14779
  if (text && !this.recent.some((r) => norm3(r) === norm3(text))) return text;
13677
- if (text) log29.debug(`generated "${text}" repeats a recent line \u2014 canned`);
14780
+ if (text) log30.debug(`generated "${text}" repeats a recent line \u2014 canned`);
13678
14781
  }
13679
14782
  return this.pick(kind);
13680
14783
  }
13681
14784
  /** Canned rotation in the user's language: never one of the last `bankExclude` picks of this kind, nor a recent
13682
14785
  * line. '' = no bank for this language yet (silence). */
13683
14786
  pick(kind) {
14787
+ if (this.ownWords && kind !== "backchannel") return "";
13684
14788
  const pool = this.bank?.[kind] ?? [];
13685
14789
  if (!pool.length) return "";
13686
14790
  const hist = this.picks.get(kind) ?? [];
@@ -13696,7 +14800,7 @@ var SecondChannel = class {
13696
14800
  try {
13697
14801
  r = this.o.speak(text, kind);
13698
14802
  } catch (e) {
13699
- log29.warn(`speak threw \u2014 line dropped: ${e.message}`);
14803
+ log30.warn(`speak threw \u2014 line dropped: ${e.message}`);
13700
14804
  return false;
13701
14805
  }
13702
14806
  if (r === false) return false;
@@ -13710,7 +14814,7 @@ var SecondChannel = class {
13710
14814
  if (words3) this.o.show(words3, kind);
13711
14815
  const p = Promise.resolve(r).then(() => {
13712
14816
  }, (e) => {
13713
- log29.warn(`channel line playback failed: ${e?.message ?? e}`);
14817
+ log30.warn(`channel line playback failed: ${e?.message ?? e}`);
13714
14818
  }).finally(() => {
13715
14819
  if (this.inflight === p) this.inflight = null;
13716
14820
  });
@@ -13750,7 +14854,9 @@ var REACT_HINT = 'a short reaction to their LAST words instead: feeling ("oh no"
13750
14854
  var TAG_HINT = 'Real listeners often react without words: the line may be, or begin with, [laughter] or [gasp], and [quiet] before words breathes them under the voice ("[gasp]", "[laughter]", "[gasp] [quiet] no way"). Use a sound when it fits what they said.';
13751
14855
  function fastLineGenerator(ai, model, gender) {
13752
14856
  return async (c, signal) => {
13753
- const prompt = `You voice the tiny side-sounds of a live spoken conversation. Write ONE line of 1-5 words: ${KIND_HINT2[c.kind]}.
14857
+ const prompt = c.ask ? `A live voice call. The user just said: "${c.heard}"
14858
+ If this asks you to look up, check, find or do something that takes a while, write ONE spoken line of 2-5 words in ${languageName(c.lang)} that names the thing in their own words and says you are on it ("Hacker News, one sec."). If it is small talk, a statement, or something you can answer at once, write: none
14859
+ ${personaHint(gender)} Output only the line.` : `You voice the tiny side-sounds of a live spoken conversation. Write ONE line of 1-5 words: ${KIND_HINT2[c.kind]}.
13754
14860
  ` + (c.react ? `Here the line may be ${REACT_HINT}.
13755
14861
  ` : "") + (c.tags ? `${TAG_HINT}
13756
14862
  ` : "") + `The user just said: "${c.heard}"
@@ -13770,7 +14876,11 @@ function closedQuestion(reply) {
13770
14876
  }
13771
14877
 
13772
14878
  // src/voice/call.ts
13773
- var log30 = forComponent("VoiceCall");
14879
+ var log31 = forComponent("VoiceCall");
14880
+ function newWords(text, turn) {
14881
+ const said = new Set(norm3(turn).split(" "));
14882
+ return norm3(text).split(" ").filter((w) => w && !said.has(w)).length;
14883
+ }
13774
14884
  var VoiceCallOptions = class {
13775
14885
  /** Host runs MAIN with exactly `text` (a replace: roll the `replaces` turn back first). */
13776
14886
  sendTurn = () => {
@@ -13845,7 +14955,7 @@ var VoiceCall = class {
13845
14955
  cancel: () => o.cancel(),
13846
14956
  now: o.now
13847
14957
  });
13848
- this.progress = new Progress({ ...ai ? { generate: fastProgressGenerator(ai, o.fastModel, o.gender) } : {}, gender: o.gender, now: o.now, ...o.progress });
14958
+ this.progress = new Progress({ ...o.mode === "live" ? { templates: {}, generateBudgetMs: 1500 } : {}, ...ai ? { generate: fastProgressGenerator(ai, o.fastModel, o.gender) } : {}, gender: o.gender, now: o.now, ...o.progress });
13849
14959
  this.judge = new OverlapJudge({ ...ai ? { model: overlapModelFor(ai) } : {}, now: o.now });
13850
14960
  this.gate = new TurnGate({
13851
14961
  now: o.now,
@@ -13897,9 +15007,10 @@ var VoiceCall = class {
13897
15007
  if (typeof aec === "boolean") this.fill.options.aec = aec;
13898
15008
  this.gate.speaking();
13899
15009
  const c = this.cur;
13900
- if (this.o.mode === "live" && c?.running && !c.worded && text.trim().split(/\s+/).length >= 2) {
13901
- log30.debug("user resumed before the reply \u2014 yielded");
15010
+ if (this.o.mode === "live" && c?.running && !c.worded && newWords(text, c.text) >= 2) {
15011
+ log31.debug("user resumed before the reply \u2014 yielded");
13902
15012
  this.barge();
15013
+ this.gate.reopen(c.id);
13903
15014
  }
13904
15015
  this.follow(text);
13905
15016
  this.fill.onInterim(text);
@@ -14002,7 +15113,7 @@ var VoiceCall = class {
14002
15113
  try {
14003
15114
  this.o.cancel();
14004
15115
  } catch (e) {
14005
- log30.warn(`cancel threw: ${e.message}`);
15116
+ log31.warn(`cancel threw: ${e.message}`);
14006
15117
  }
14007
15118
  if (this.cur?.running) {
14008
15119
  this.cur.running = false;
@@ -14089,7 +15200,7 @@ var VoiceCall = class {
14089
15200
  try {
14090
15201
  r = this.o.speak(text, kind);
14091
15202
  } catch (e) {
14092
- log30.warn(`speak threw: ${e.message}`);
15203
+ log31.warn(`speak threw: ${e.message}`);
14093
15204
  return false;
14094
15205
  }
14095
15206
  if (r === false) return false;
@@ -14140,7 +15251,8 @@ var VoiceCall = class {
14140
15251
  async onTool(c, name, args) {
14141
15252
  const lang = this.lang;
14142
15253
  if (!lang || c.streamed) return;
14143
- const line = await this.progress.line(name, args, lang);
15254
+ const line = await this.progress.line(name, args, lang, this.o.mode === "live" ? c.text : "");
15255
+ if (!line && this.o.mode === "live") this.o.onMetric({ name: "drop", id: c.id, kind: `no progress line: ${name}` });
14144
15256
  if (!line || this.cur !== c || c.streamed || !c.running || this.lang !== lang) return;
14145
15257
  this.clearProgress();
14146
15258
  const tryNow = () => {
@@ -14152,6 +15264,7 @@ var VoiceCall = class {
14152
15264
  }
14153
15265
  if (this.say(line, "progress") !== false) {
14154
15266
  this.progress.spoke(line);
15267
+ this.fill.covered();
14155
15268
  this.o.show(line, "progress");
14156
15269
  }
14157
15270
  };