@livx.cc/agentx 0.99.146 → 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];
@@ -10390,7 +11170,7 @@ var TurnGate = class {
10390
11170
  }
10391
11171
  /** An STT final. */
10392
11172
  final(text, sttId) {
10393
- let t = text.trim();
11173
+ let t = text.trim(), replaces;
10394
11174
  if (!t) return this.o.onDrop(text, "empty");
10395
11175
  if (this.held) {
10396
11176
  const h = this.held;
@@ -10398,6 +11178,7 @@ var TurnGate = class {
10398
11178
  this.held = void 0;
10399
11179
  if (!(norm3(t) + " ").startsWith(norm3(h.text) + " ")) t = `${h.text} ${t}`;
10400
11180
  sttId ??= h.sttId;
11181
+ replaces = h.replaces;
10401
11182
  }
10402
11183
  const key = norm3(t), now3 = this.o.now(), last = this.last;
10403
11184
  if (last) {
@@ -10411,10 +11192,18 @@ var TurnGate = class {
10411
11192
  const mid = endsMidPhrase(t);
10412
11193
  if (mid || this.o.settleMs && !/\?["')\]]*$/.test(t)) {
10413
11194
  const ms = mid ? this.o.holdMs : this.o.settleMs;
10414
- this.held = { text: t, sttId, ms, timer: setTimeout(() => this.flush(), ms) };
11195
+ this.held = { text: t, sttId, ms, replaces, timer: setTimeout(() => this.flush(), ms) };
10415
11196
  return this.o.onHold(t);
10416
11197
  }
10417
- 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) };
10418
11207
  }
10419
11208
  /** The user is speaking again (a partial): a held fragment waits for the rest. */
10420
11209
  speaking() {
@@ -10433,7 +11222,7 @@ var TurnGate = class {
10433
11222
  clearTimeout(h.timer);
10434
11223
  this.held = void 0;
10435
11224
  if (this.closed && isClosing(h.text)) return this.o.onDrop(h.text, "goodbye");
10436
- this.send(h.text, norm3(h.text), h.sttId, "new");
11225
+ this.send(h.text, norm3(h.text), h.sttId, "new", h.replaces);
10437
11226
  }
10438
11227
  end() {
10439
11228
  if (this.held) {
@@ -13734,6 +14523,7 @@ var SecondChannel = class {
13734
14523
  bridgeBlock() {
13735
14524
  const w = words2(this.heard), intent = turnIntent(this.heard);
13736
14525
  if (!this.lang) return "language unknown";
14526
+ if (this.ownWords) return this.blocked("bridge");
13737
14527
  if (intent === "closer") return "no answer due";
13738
14528
  if (this.late) return this.blocked("bridge");
13739
14529
  const why = intent === "ask" ? w.length <= 6 && isPhatic(this.heard, this.bank) ? "phatic" : "" : this.shortTurn(w.length);
@@ -13758,7 +14548,7 @@ var SecondChannel = class {
13758
14548
  if (!this.lang) return "silence";
13759
14549
  if (trigger === "working") return this.bridgeBlock() ? "silence" : "bridge";
13760
14550
  if (w.length <= this.o.shortTurnWords || w.length <= 6 && isPhatic(this.heard, this.bank)) return "silence";
13761
- 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;
13762
14552
  return k && this.allowed(k) ? k : "silence";
13763
14553
  }
13764
14554
  /** Plan (or, with `heard`, revise) the backchannels of the user's turn — async, never on the hot path. */
@@ -13954,7 +14744,7 @@ var SecondChannel = class {
13954
14744
  this.bridgeDraft = null;
13955
14745
  this.askLeaf = null;
13956
14746
  this.bridgeReady = Promise.resolve();
13957
- if (!this.o.generate || !(this.ownWords || this.o.generateFor.includes("bridge")) || !this.lang || !this.heard || this.ownWords && turnIntent(this.heard) !== "ask" && !requestOpener(this.heard)) return;
14747
+ if (!this.o.generate || !(this.ownWords || this.o.generateFor.includes("bridge")) || !this.lang || !this.heard) return;
13958
14748
  const gen = this.gen, ac = new AbortController(), own = this.ownWords, heard = this.heard;
13959
14749
  const ctx = { kind: "bridge", heard: this.tail(), state: this.main, avoid: [...this.recent], lang: this.lang, ...own ? { ask: true } : {} };
13960
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) => {
@@ -14087,6 +14877,10 @@ function closedQuestion(reply) {
14087
14877
 
14088
14878
  // src/voice/call.ts
14089
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
+ }
14090
14884
  var VoiceCallOptions = class {
14091
14885
  /** Host runs MAIN with exactly `text` (a replace: roll the `replaces` turn back first). */
14092
14886
  sendTurn = () => {
@@ -14213,9 +15007,10 @@ var VoiceCall = class {
14213
15007
  if (typeof aec === "boolean") this.fill.options.aec = aec;
14214
15008
  this.gate.speaking();
14215
15009
  const c = this.cur;
14216
- if (this.o.mode === "live" && c?.running && !c.worded && text.trim().split(/\s+/).length >= 2) {
15010
+ if (this.o.mode === "live" && c?.running && !c.worded && newWords(text, c.text) >= 2) {
14217
15011
  log31.debug("user resumed before the reply \u2014 yielded");
14218
15012
  this.barge();
15013
+ this.gate.reopen(c.id);
14219
15014
  }
14220
15015
  this.follow(text);
14221
15016
  this.fill.onInterim(text);