@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/cli.js CHANGED
@@ -3359,7 +3359,7 @@ var init_tools_shell = __esm({
3359
3359
 
3360
3360
  // cli/cli.ts
3361
3361
  import { createInterface } from "readline/promises";
3362
- import { existsSync as existsSync9, readFileSync as readFileSync10, appendFileSync, mkdirSync as mkdirSync12, writeFileSync as writeFileSync9, readdirSync as readdirSync4, statSync as statSync8, unlinkSync as unlinkSync5 } from "fs";
3362
+ import { existsSync as existsSync9, readFileSync as readFileSync10, appendFileSync as appendFileSync2, mkdirSync as mkdirSync12, writeFileSync as writeFileSync9, readdirSync as readdirSync4, statSync as statSync8, unlinkSync as unlinkSync5 } from "fs";
3363
3363
  import { homedir as homedir10 } from "os";
3364
3364
  import { spawnSync as spawnSync7 } from "child_process";
3365
3365
 
@@ -5612,7 +5612,7 @@ var Agent = class _Agent {
5612
5612
  let lastProgressAt = start;
5613
5613
  const turnUser = this.transcript[this.transcript.length - 1];
5614
5614
  const kill = (finishReason) => {
5615
- 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` : ""})`);
5615
+ 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` : ""})`);
5616
5616
  this.jobs.killAll(`run ended: ${finishReason}`, true);
5617
5617
  const claims = this.survivingClaims();
5618
5618
  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 } : {} };
@@ -7235,6 +7235,117 @@ init_llm();
7235
7235
  init_logging();
7236
7236
  init_models();
7237
7237
 
7238
+ // node_modules/@bod.ee/voice/src/core/types.ts
7239
+ var STT_SAMPLE_RATE = 16e3;
7240
+ var TTS_SAMPLE_RATE = 44100;
7241
+ async function resolveAuth(auth) {
7242
+ return typeof auth === "function" ? await auth() : auth;
7243
+ }
7244
+
7245
+ // node_modules/@bod.ee/voice/src/core/splice-sink.ts
7246
+ var BYTES_PER_MS = TTS_SAMPLE_RATE * 2 / 1e3;
7247
+ var toBytes = (ms) => Math.max(0, Math.round(ms * TTS_SAMPLE_RATE / 1e3)) * 2;
7248
+ var SpliceSinkOptions = class {
7249
+ /** The rest resumes this much before the pause point — a human re-enters a little before where they stopped. */
7250
+ backoffMs = 150;
7251
+ /** Tape kept behind the playhead (older audio is dropped; a splice only needs the unplayed rest + the backoff). */
7252
+ keepMs = 5e3;
7253
+ };
7254
+ var SpliceSink = class {
7255
+ constructor(sink, options) {
7256
+ this.sink = sink;
7257
+ this.options = { ...new SpliceSinkOptions(), ...options };
7258
+ }
7259
+ sink;
7260
+ options;
7261
+ tape = [];
7262
+ tapeStart = 0;
7263
+ // content bytes dropped from the head of `tape`
7264
+ /** Active splice: the sink's raw timeline = clip (clipMs) then the content from `fromMs`. */
7265
+ clipMs = 0;
7266
+ fromMs = 0;
7267
+ spliced = false;
7268
+ markTurn() {
7269
+ this.reset();
7270
+ this.sink.markTurn();
7271
+ }
7272
+ kill() {
7273
+ this.reset();
7274
+ this.sink.kill();
7275
+ }
7276
+ pause() {
7277
+ this.sink.pause?.();
7278
+ }
7279
+ resume() {
7280
+ this.sink.resume?.();
7281
+ }
7282
+ get canPause() {
7283
+ return !!this.sink.pause && !!this.sink.resume;
7284
+ }
7285
+ write(pcm) {
7286
+ this.tape.push(pcm);
7287
+ this.trim();
7288
+ this.sink.write(pcm);
7289
+ }
7290
+ drainMs() {
7291
+ return this.sink.drainMs();
7292
+ }
7293
+ /** Reply-content ms played: holds at the splice point while the clip plays. */
7294
+ playedMs() {
7295
+ const raw = this.sink.playedMs();
7296
+ return this.spliced ? Math.max(this.fromMs, raw - this.clipMs + this.fromMs) : raw;
7297
+ }
7298
+ /** Reply-content ms written so far (played + still queued) — exact even mid-clip (anchors a new sub-turn). */
7299
+ endMs() {
7300
+ const raw = Math.max(0, this.sink.playedMs() + this.sink.drainMs());
7301
+ return this.spliced ? Math.max(0, raw - this.clipMs + this.fromMs) : raw;
7302
+ }
7303
+ /** Play `clip` (s16le mono at TTS_SAMPLE_RATE) now, then the reply from where it was (less backoffMs). Unpauses a
7304
+ * paused sink (the flush does). Returns the content ms the rest resumes from. */
7305
+ splice(clip2) {
7306
+ const at = Math.max(0, this.playedMs() - this.options.backoffMs);
7307
+ const from = Math.max(at, this.tapeStart / BYTES_PER_MS);
7308
+ const rest = this.slice(toBytes(from));
7309
+ this.sink.kill();
7310
+ this.sink.write(clip2);
7311
+ if (rest.length) this.sink.write(rest);
7312
+ this.spliced = true;
7313
+ this.clipMs = clip2.length / BYTES_PER_MS;
7314
+ this.fromMs = from;
7315
+ return from;
7316
+ }
7317
+ slice(fromByte) {
7318
+ let off = this.tapeStart;
7319
+ const parts = [];
7320
+ for (const c of this.tape) {
7321
+ const end = off + c.length;
7322
+ if (end > fromByte) parts.push(off >= fromByte ? c : c.subarray(fromByte - off));
7323
+ off = end;
7324
+ }
7325
+ const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
7326
+ let i = 0;
7327
+ for (const p of parts) {
7328
+ out.set(p, i);
7329
+ i += p.length;
7330
+ }
7331
+ return out;
7332
+ }
7333
+ trim() {
7334
+ const keepFrom = toBytes(this.playedMs() - this.options.keepMs);
7335
+ while (this.tape.length > 1 && this.tapeStart + this.tape[0].length <= keepFrom) {
7336
+ const c = this.tape.shift();
7337
+ this.tapeStart += c.length;
7338
+ }
7339
+ }
7340
+ reset() {
7341
+ this.tape = [];
7342
+ this.tapeStart = 0;
7343
+ this.spliced = false;
7344
+ this.clipMs = 0;
7345
+ this.fromMs = 0;
7346
+ }
7347
+ };
7348
+
7238
7349
  // node_modules/@bod.ee/voice/src/core/logging.ts
7239
7350
  function consoleForComponent(name) {
7240
7351
  const debugEnv = typeof process !== "undefined" && process.env?.DEBUG || typeof localStorage !== "undefined" && localStorage.getItem("DEBUG") || "";
@@ -7369,7 +7480,11 @@ var ALIASES = {
7369
7480
  comedic: "joking"
7370
7481
  // Cartesia lists "joking/comedic" as one — canonicalize to joking
7371
7482
  };
7372
- var NONVERBAL = { laughter: "laughter", laughs: "laughter", laugh: "laughter", laughing: "laughter" };
7483
+ var LAUGH = "[laughter]";
7484
+ var BREATH = '<emotion value="scared"/>Hhh!';
7485
+ var NONVERBAL = { laughter: LAUGH, laughs: LAUGH, laugh: LAUGH, laughing: LAUGH, gasp: BREATH, gasps: BREATH, gasping: BREATH };
7486
+ var QUIET = '<volume ratio="0.5"/><speed ratio="0.8"/>';
7487
+ var DELIVERY = { quiet: QUIET, quietly: QUIET, soft: QUIET, softly: QUIET, whispers: QUIET };
7373
7488
  function normalizeEmotion(raw) {
7374
7489
  const k = raw.trim().toLowerCase();
7375
7490
  if (VALID.has(k)) return k;
@@ -7378,39 +7493,32 @@ function normalizeEmotion(raw) {
7378
7493
  function resolveTag(raw) {
7379
7494
  const k = raw.trim().toLowerCase();
7380
7495
  if (NONVERBAL[k]) return { kind: "nonverbal", value: NONVERBAL[k] };
7496
+ if (DELIVERY[k]) return { kind: "delivery", value: DELIVERY[k] };
7381
7497
  const e = normalizeEmotion(k);
7382
7498
  return e ? { kind: "emotion", value: e } : null;
7383
7499
  }
7384
7500
  var TAG_RE = /\[([a-zA-Z][a-zA-Z ]{0,24})\]/g;
7385
7501
  var PARTIAL_RE = /\[[a-zA-Z ]*$/;
7386
- var cartesiaTag = (t) => t.kind === "nonverbal" ? `[${t.value}]` : `<emotion value="${t.value}"/>`;
7502
+ var cartesiaTag = (t) => t.kind === "emotion" ? `<emotion value="${t.value}"/>` : t.value;
7387
7503
  function renderEmotions(text, opts = { show: true }) {
7388
- let speech = "", display = "", prose = "", last = 0;
7389
- TAG_RE.lastIndex = 0;
7390
- for (let m = TAG_RE.exec(text); m; m = TAG_RE.exec(text)) {
7391
- const before = text.slice(last, m.index);
7392
- speech += before;
7393
- display += before;
7394
- prose += before;
7395
- const tag = resolveTag(m[1]);
7396
- if (tag) {
7397
- speech += cartesiaTag(tag);
7398
- if (opts.show) display += m[0];
7399
- } else {
7400
- log13.debug(`dropping unknown emotion tag ${m[0]}`);
7401
- }
7402
- last = m.index + m[0].length;
7403
- }
7404
- const tail = text.slice(last);
7405
- return { speech: speech + tail, display: display + tail, prose: prose + tail };
7504
+ const s = new EmotionStream(opts.show), a = s.feed(text), b = s.flush();
7505
+ return { speech: a.speech + b.speech, display: a.display + b.display, prose: a.prose + b.prose };
7406
7506
  }
7407
7507
  var EmotionStream = class {
7508
+ // that tag, due on the next words after a gasp; an emotion tag in between replaces it
7408
7509
  constructor(show = true) {
7409
7510
  this.show = show;
7410
7511
  }
7411
7512
  show;
7412
7513
  buf = "";
7413
- pending = null;
7514
+ pending = "";
7515
+ emotion = "neutral";
7516
+ // the last emotion tag: what the words after a gasp's `scared` go back to
7517
+ restore = "";
7518
+ /** A new TTS context opens mid-reply: an emotion tag ended with the old one, so the next words carry it again. */
7519
+ resume() {
7520
+ if (this.emotion !== "neutral") this.restore = cartesiaTag({ kind: "emotion", value: this.emotion });
7521
+ }
7414
7522
  feed(delta) {
7415
7523
  this.buf += delta;
7416
7524
  return this.drain(false);
@@ -7437,7 +7545,17 @@ var EmotionStream = class {
7437
7545
  });
7438
7546
  const tag = resolveTag(m[1]);
7439
7547
  if (tag) {
7440
- this.pending = tag;
7548
+ if (tag.kind === "nonverbal") {
7549
+ speech += this.pending + tag.value;
7550
+ this.pending = "";
7551
+ if (tag.value === BREATH) this.restore = cartesiaTag({ kind: "emotion", value: this.emotion });
7552
+ } else {
7553
+ if (tag.kind === "emotion") {
7554
+ this.emotion = tag.value;
7555
+ this.restore = "";
7556
+ }
7557
+ this.pending += cartesiaTag(tag);
7558
+ }
7441
7559
  if (this.show) display += m[0];
7442
7560
  } else log13.debug(`dropping unknown emotion tag ${m[0]}`);
7443
7561
  last = m.index + m[0].length;
@@ -7453,9 +7571,9 @@ var EmotionStream = class {
7453
7571
  emit(text, sink) {
7454
7572
  if (!text) return;
7455
7573
  let speech = text;
7456
- if (this.pending && /[\p{L}\p{N}]/u.test(text)) {
7457
- speech = cartesiaTag(this.pending) + text;
7458
- this.pending = null;
7574
+ if ((this.pending || this.restore) && /[\p{L}\p{N}]/u.test(text)) {
7575
+ speech = this.restore + this.pending + text;
7576
+ this.pending = this.restore = "";
7459
7577
  }
7460
7578
  sink(speech, text, text);
7461
7579
  }
@@ -7585,8 +7703,66 @@ function computeHoldMs(ctx) {
7585
7703
  return Math.max(floor, Math.min(MAX_HOLD_MS, ms));
7586
7704
  }
7587
7705
 
7706
+ // node_modules/@bod.ee/voice/src/core/speakers.ts
7707
+ function sonioxWords(toks) {
7708
+ const out = [];
7709
+ let cur = null;
7710
+ for (const t of toks) {
7711
+ if (/^\s/.test(t.text) || !cur) {
7712
+ if (cur) out.push(cur);
7713
+ 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) } : {} };
7714
+ }
7715
+ cur.w += t.text;
7716
+ cur.t1 = t.end_ms / 1e3;
7717
+ cur.conf = Math.min(cur.conf, t.confidence ?? 1);
7718
+ }
7719
+ if (cur) out.push(cur);
7720
+ return out.map((w) => ({ ...w, w: w.w.trim() })).filter((w) => w.w);
7721
+ }
7722
+ var SpeakerRunsOptions = class {
7723
+ /** A run shorter than this (a diarization flap) joins its neighbour — the previous run, or the next for a leading one. */
7724
+ minRunWords = 2;
7725
+ };
7726
+ function speakerRuns(words4, options) {
7727
+ const { minRunWords } = { ...new SpeakerRunsOptions(), ...options };
7728
+ let runs = [];
7729
+ for (const w of words4) {
7730
+ const last = runs[runs.length - 1];
7731
+ const spk = w.spk ?? last?.spk ?? words4.find((x) => x.spk != null)?.spk ?? "";
7732
+ if (last?.spk === spk) last.words.push(w);
7733
+ else runs.push({ spk, words: [w] });
7734
+ }
7735
+ while (runs.length > 1) {
7736
+ const i = runs.findIndex((r) => r.words.length < minRunWords);
7737
+ if (i < 0) break;
7738
+ const into = i === 0 ? runs[1] : runs[i - 1];
7739
+ if (i === 0) into.words.unshift(...runs[0].words);
7740
+ else into.words.push(...runs[i].words);
7741
+ runs.splice(i, 1);
7742
+ runs = runs.reduce((acc, r) => {
7743
+ const p = acc[acc.length - 1];
7744
+ if (p?.spk === r.spk) p.words.push(...r.words);
7745
+ else acc.push(r);
7746
+ return acc;
7747
+ }, []);
7748
+ }
7749
+ return runs.map((r) => ({
7750
+ speaker: r.spk,
7751
+ text: r.words.map((w) => w.w).join(" "),
7752
+ startMs: Math.round(r.words[0].t0 * 1e3),
7753
+ endMs: Math.round(r.words[r.words.length - 1].t1 * 1e3)
7754
+ }));
7755
+ }
7756
+ function formatTurns(turns, name = (t) => `Speaker ${t.speaker}`) {
7757
+ return turns.map((t) => `${name(t)}: ${t.text}`).join("\n");
7758
+ }
7759
+
7588
7760
  // node_modules/@bod.ee/voice/src/core/engine.ts
7589
7761
  var log14 = forComponent2("VoiceEngine");
7762
+ var addLangs = (to, from) => {
7763
+ for (const k in from) to[k] = (to[k] || 0) + from[k];
7764
+ };
7765
+ var dominantLang = (l) => Object.keys(l).sort((a, b) => l[b] - l[a])[0];
7590
7766
  var realClock = {
7591
7767
  now: () => performance.now(),
7592
7768
  setTimeout: (fn, ms) => setTimeout(fn, ms),
@@ -7619,6 +7795,17 @@ function heardPrefix(text, spoken, lookahead = 4) {
7619
7795
  }
7620
7796
  return at;
7621
7797
  }
7798
+ var NumberedSpeakers = class {
7799
+ ids = /* @__PURE__ */ new Map();
7800
+ name(label) {
7801
+ let n = this.ids.get(label);
7802
+ if (!n) this.ids.set(label, n = `Speaker ${this.ids.size + 1}`);
7803
+ return n;
7804
+ }
7805
+ reset() {
7806
+ this.ids.clear();
7807
+ }
7808
+ };
7622
7809
  var VoiceEngineOptions = class {
7623
7810
  stt;
7624
7811
  tts;
@@ -7627,6 +7814,15 @@ var VoiceEngineOptions = class {
7627
7814
  * re-finalization of the utterance dispatched just before it: replace that turn, don't add one. */
7628
7815
  onUtterance = () => {
7629
7816
  };
7817
+ /** Label multi-speaker utterances (`Speaker 1: …\nSpeaker 2: …`) from the STT's diarized turns. Sticky: off until
7818
+ * the session hears ≥2 speakers with ≥3 words each, then every turn is labelled; solo use is unchanged. */
7819
+ speakerLabels = false;
7820
+ /** Maps raw speaker ids to display names (default `Speaker N` by first appearance). */
7821
+ speakerResolver = new NumberedSpeakers();
7822
+ /** Only the speakers `speakerResolver.keep` accepts are heard (a one-person call in a busy room): another speaker's
7823
+ * turns are cut from each utterance (all of them → no utterance), and a partial that is only theirs is ignored — no
7824
+ * interim, no barge-in. Needs a diarized STT; without speaker tags every word counts. */
7825
+ speakerFilter = false;
7630
7826
  /** live partial transcript while listening (host renders the 🎤 line) */
7631
7827
  onPartial = () => {
7632
7828
  };
@@ -7684,11 +7880,19 @@ var VoiceEngineOptions = class {
7684
7880
  * it now fires on every tier-2/3 hold, not only on `looksIncomplete` (`holdFiller` stays tier-1
7685
7881
  * only). See core/endpointing.ts. */
7686
7882
  adaptiveEndpointing = false;
7883
+ /** Flush the TTS at the reply's first clause (the first , . ; : ! ? … — cut there, never mid-phrase) so its speech starts there, not at the
7884
+ * provider's buffer limit (Cartesia: a sentence end or max_buffer_delay_ms) — a reply streamed word by word otherwise
7885
+ * starts speaking only once its whole first sentence is in. Needs a TTS with flush(). */
7886
+ flushFirstClause = false;
7687
7887
  /** Grace window (ms) after an utterance dispatches, during which the user's own trailing audio cannot
7688
7888
  * barge the reply it requested. Soniox keeps finalizing partials past <end>; without this they read
7689
7889
  * as a barge and abort the fresh turn (live: mid-sentence self-interruption + steps=1→steps=0 double
7690
7890
  * abort). Short enough that a genuine immediate barge ("no wait—") still lands right after. */
7691
7891
  bargeGraceMs = 600;
7892
+ /** A new final within this window of the last dispatch, while that dispatch is still unanswered (no reply beyond an
7893
+ * ack), is merged into it and dispatched as ONE line superseding it (meta.supersedes) — never a second queued turn.
7894
+ * 0 = off. */
7895
+ pendingMergeMs = 3e3;
7692
7896
  /** Barge-in (talk over the assistant to interrupt). true = full-duplex (needs echo cancellation, or
7693
7897
  * the assistant's own TTS bleeds back and self-interrupts). false = HALF-DUPLEX: the engine is deaf
7694
7898
  * while audible (speaking + drain tail), so echo can never become a phantom turn — the right mode
@@ -7735,6 +7939,12 @@ var VoiceEngineOptions = class {
7735
7939
  overlapDecideMs = 0;
7736
7940
  /** Longest overlap (words) the host decides; also how far an overlap the host kept may grow before it cedes. */
7737
7941
  overlapDecideMaxWords = 4;
7942
+ /** A second TTS for ASIDES (decideOverlap's `aside`: a short line said over a kept overlap — "oh yeah, definitely"
7943
+ * — while the reply is held, then the SAME reply resumes from its pause point). Its own client: the reply's
7944
+ * context may still be streaming, and one client has one live context. Absent → asides are skipped (resume only). */
7945
+ asideTts = null;
7946
+ /** An aside not synthesized within this long is dropped and the reply resumes (never a long held silence). */
7947
+ asideMaxMs = 1500;
7738
7948
  /** Speculative ENERGY pre-pause while speaking (AEC tier): two residue gate-passes within 350ms →
7739
7949
  * pause ~300ms before the STT tokens land. But energy CANNOT separate residue bursts from speech
7740
7950
  * (the documented whack-a-mole) — so a residue spike during loud playback false-pauses with NO user
@@ -7808,7 +8018,16 @@ var VoiceEngine = class _VoiceEngine {
7808
8018
  state = "idle";
7809
8019
  stt;
7810
8020
  tts;
7811
- player;
8021
+ sink;
8022
+ /** The playback sink, always behind a SpliceSink (asides play ahead of a held reply); assigning one wraps it. */
8023
+ get player() {
8024
+ return this.sink;
8025
+ }
8026
+ set player(p) {
8027
+ this.sink = p instanceof SpliceSink ? p : new SpliceSink(p);
8028
+ }
8029
+ asideEpoch = 0;
8030
+ // bumps on every aside start/cancel — a late aside of a dead hold never plays
7812
8031
  speaking = false;
7813
8032
  // audible (deltas flowing OR audio draining)
7814
8033
  ctxOpen = false;
@@ -7885,6 +8104,15 @@ var VoiceEngine = class _VoiceEngine {
7885
8104
  lastDispatchFlat = "";
7886
8105
  lastDispatchWords = [];
7887
8106
  lastDispatchText = "";
8107
+ lastDispatchLangs = {};
8108
+ // chars per language of lastDispatchText (a pending-reply continuation re-sends it)
8109
+ lastDispatchTurns = [];
8110
+ // speaker turns of lastDispatchText (same reason)
8111
+ lastDispatchOut = "";
8112
+ // the text the host actually received (labelled) — what `supersedes` names
8113
+ speakerWords = /* @__PURE__ */ new Map();
8114
+ // words dispatched per raw speaker id (sticky labelling)
8115
+ labelling = false;
7888
8116
  lastDispatchAt = 0;
7889
8117
  repliedSinceDispatch = false;
7890
8118
  dispatchReply = "";
@@ -7949,6 +8177,9 @@ var VoiceEngine = class _VoiceEngine {
7949
8177
  this.clock = o.clock;
7950
8178
  }
7951
8179
  async start() {
8180
+ this.speakerWords.clear();
8181
+ this.labelling = false;
8182
+ this.options.speakerResolver.reset?.();
7952
8183
  this.tts.onAudio = (c) => {
7953
8184
  if (!this.speaking && !this.bcActive) return;
7954
8185
  if (this.wordAnchorPending && this.speaking) this.anchorSubturn();
@@ -7967,11 +8198,34 @@ var VoiceEngine = class _VoiceEngine {
7967
8198
  });
7968
8199
  };
7969
8200
  }
7970
- this.stt.onPartial = (text) => this.handlePartial(text);
7971
- this.stt.onUtterance = (text) => this.handleUtterance(text);
8201
+ this.stt.onPartial = (text, turns) => {
8202
+ const k = this.keptTurns(text, turns);
8203
+ if (k) this.handlePartial(k.text);
8204
+ };
8205
+ this.stt.onUtterance = (text, _at, language, turns, ago) => {
8206
+ this.lastWordAt = ago === void 0 ? 0 : this.clock.now() - ago;
8207
+ if (turns?.length && (this.options.speakerLabels || this.options.speakerFilter)) {
8208
+ try {
8209
+ this.options.speakerResolver.observe?.(turns);
8210
+ } catch (e) {
8211
+ log14.warn(`speakerResolver.observe threw: ${e instanceof Error ? e.message : e}`);
8212
+ }
8213
+ }
8214
+ const k = this.keptTurns(text, turns);
8215
+ if (!k) {
8216
+ this.diag("speaker_dropped", { text });
8217
+ return;
8218
+ }
8219
+ this.handleUtterance(k.text, language ? { [language]: k.text.length } : void 0, k.turns);
8220
+ };
7972
8221
  this.stt.onLevel = (rms) => this.handleLevel(rms);
7973
- await Promise.all([this.tts.connect(), this.stt.start()]);
8222
+ await Promise.all([
8223
+ this.tts.connect(),
8224
+ this.stt.start(),
8225
+ Promise.resolve(this.options.asideTts?.connect()).catch((e) => log14.warn(`aside TTS connect failed \u2014 asides off until it reconnects: ${e instanceof Error ? e.message : e}`))
8226
+ ]);
7974
8227
  this.tts.warmup?.();
8228
+ this.options.asideTts?.warmup?.();
7975
8229
  this.setState("listening");
7976
8230
  if (this.options.bargeInMode === "aec" && !this.stt.usingAec)
7977
8231
  log14.warn("bargeInMode=aec but mic does not report echoCancellation applied \u2014 STT may transcribe the assistant");
@@ -8004,6 +8258,10 @@ var VoiceEngine = class _VoiceEngine {
8004
8258
  }
8005
8259
  }
8006
8260
  idleWaiters = [];
8261
+ /** When (engine clock) the last STT final's last word ended — UtteranceMeta.lastWordAgoMs. */
8262
+ lastWordAt = 0;
8263
+ /** '' until the open context's first clause is flushed (flushFirstClause); null = flushed. */
8264
+ clause = "";
8007
8265
  setState(s) {
8008
8266
  if (this.state === s) return;
8009
8267
  this.diag("state", { from: this.state, to: s });
@@ -8064,6 +8322,7 @@ var VoiceEngine = class _VoiceEngine {
8064
8322
  this.emo = this.options.emotions ? new EmotionStream(this.options.showEmotions) : null;
8065
8323
  this.echoWords = new Set(this.words(this.prevReply));
8066
8324
  this.tts.newContext();
8325
+ this.clause = "";
8067
8326
  this.clearAckTimer("new_turn");
8068
8327
  if (ack && this.options.ackPhrase) {
8069
8328
  this.diag("ack_fired", { phrase: this.options.ackPhrase, adaptive: false });
@@ -8138,7 +8397,15 @@ var VoiceEngine = class _VoiceEngine {
8138
8397
  this.dispatchReply += prose;
8139
8398
  }
8140
8399
  for (const w of this.words(this.reply)) this.echoWords.add(w);
8141
- this.tts.speak(forSpeech(speech), true);
8400
+ const cut = this.options.flushFirstClause && this.clause !== null && this.tts.flush ? speech.search(/[,.;:!?…](?=\s|$)/) : -1;
8401
+ if (cut < 0) this.tts.speak(forSpeech(speech), true);
8402
+ else {
8403
+ this.tts.speak(forSpeech(speech.slice(0, cut + 1)), true);
8404
+ this.clause = null;
8405
+ this.tts.flush();
8406
+ this.diag("tts_first_clause_flush", {});
8407
+ if (speech.slice(cut + 1)) this.tts.speak(forSpeech(speech.slice(cut + 1)), true);
8408
+ }
8142
8409
  if (!this.spokeDeltas && this.turnStartAt) log14.debug(`ttft: ${Math.round(this.clock.now() - this.turnStartAt)}ms`);
8143
8410
  this.spokeDeltas = true;
8144
8411
  this.setState("speaking");
@@ -8163,6 +8430,8 @@ var VoiceEngine = class _VoiceEngine {
8163
8430
  this.diag("tts_subturn_boundary", { playedMs: Math.round(Math.max(0, this.player.playedMs())) });
8164
8431
  this.tts.end();
8165
8432
  this.tts.newContext();
8433
+ this.clause = "";
8434
+ this.emo?.resume();
8166
8435
  this.wordAnchorPending = true;
8167
8436
  this.wordPending = [];
8168
8437
  }
@@ -8171,7 +8440,7 @@ var VoiceEngine = class _VoiceEngine {
8171
8440
  * content; fully drained, drainMs is 0 and playedMs has reached the scheduled total). */
8172
8441
  anchorSubturn() {
8173
8442
  this.wordAnchorPending = false;
8174
- this.wordOrigin = Math.max(0, this.player.playedMs() + this.player.drainMs()) / 1e3;
8443
+ this.wordOrigin = this.player.endMs() / 1e3;
8175
8444
  this.diag("reveal_anchor", { originSec: Number(this.wordOrigin.toFixed(3)), held: this.wordPending.length });
8176
8445
  for (const [w, s] of this.wordPending) {
8177
8446
  this.wordStarts.push(this.wordOrigin + s);
@@ -8254,7 +8523,7 @@ var VoiceEngine = class _VoiceEngine {
8254
8523
  this.stopWordReveal(true);
8255
8524
  if (this.turnStartAt) log14.debug(`turn: ${Math.round(this.clock.now() - this.turnStartAt)}ms (incl. playback)`);
8256
8525
  this.echoUntil = this.clock.now() + 2500;
8257
- if (!this.usingAec) this.stt.reset();
8526
+ if (!this.usingAec) this.echoHead = true;
8258
8527
  try {
8259
8528
  this.options.onSpeechEnd();
8260
8529
  } catch (e) {
@@ -8395,6 +8664,12 @@ var VoiceEngine = class _VoiceEngine {
8395
8664
  return phrase;
8396
8665
  }
8397
8666
  lastDispatchWasQuestion = false;
8667
+ /** Chars per STT language of the text that joined the pending turn / the overlap-held final — the dispatch takes the dominant. */
8668
+ pendingLangs = {};
8669
+ decideHeldLangs = {};
8670
+ /** Speaker turns riding with the pending / overlap-held text (like the langs). */
8671
+ pendingTurns = [];
8672
+ decideHeldTurns = [];
8398
8673
  dispatchRefinal = false;
8399
8674
  // the last dispatch re-finalized (superseded) the one before it — same logical turn
8400
8675
  ackSpent = false;
@@ -8436,6 +8711,7 @@ var VoiceEngine = class _VoiceEngine {
8436
8711
  this.drainTimer = null;
8437
8712
  }
8438
8713
  this.resetOverlap(false);
8714
+ this.cancelAside("interrupt");
8439
8715
  this.lastResumeAt = 0;
8440
8716
  const { heard, exact } = this.heardCut();
8441
8717
  if (this.reply || droppedQueued) this.lastInterrupted = { full: this.reply, heard, exact, queued, queuedTurn: this.queuedTurn };
@@ -8447,7 +8723,7 @@ var VoiceEngine = class _VoiceEngine {
8447
8723
  this.echoUntil = this.clock.now() + Math.max(2500, this.player.drainMs() + 3e3);
8448
8724
  this.tts.cancel();
8449
8725
  this.player.kill();
8450
- if (!this.usingAec) this.stt.reset();
8726
+ if (!this.usingAec) this.echoHead = true;
8451
8727
  if (this.reply) this.prevReply = this.reply;
8452
8728
  this.setState("listening");
8453
8729
  }
@@ -8474,9 +8750,11 @@ var VoiceEngine = class _VoiceEngine {
8474
8750
  if (this.drainTimer) this.clock.clearTimeout(this.drainTimer);
8475
8751
  if (this.specTimer) this.clock.clearTimeout(this.specTimer);
8476
8752
  this.bcSupersede();
8753
+ this.cancelAside("stop");
8477
8754
  this.stt.stop();
8478
8755
  this.player.kill();
8479
8756
  this.tts.close();
8757
+ this.options.asideTts?.close();
8480
8758
  this.setState("idle");
8481
8759
  }
8482
8760
  // --- listening side (STT-driven) ---
@@ -8515,6 +8793,22 @@ var VoiceEngine = class _VoiceEngine {
8515
8793
  "twenty"
8516
8794
  ].map((w, i) => [w, String(i)])
8517
8795
  );
8796
+ /** The next final may open with our own echo (a heuristic barge/settle kept the STT buffer, not reset it). */
8797
+ echoHead = false;
8798
+ /** `text` without its leading run of our own words (echoWords); all echo → unchanged (the echo gates judge it whole). */
8799
+ dropEchoHead(text) {
8800
+ const toks = text.trim().split(/\s+/);
8801
+ let i = 0;
8802
+ for (let k = 0; k < toks.length; k++) {
8803
+ const w = this.words(toks[k]);
8804
+ if (!w.length) continue;
8805
+ if (!w.every((x) => this.echoWords.has(x))) break;
8806
+ i = k + 1;
8807
+ }
8808
+ if (!i || i === toks.length) return text;
8809
+ this.diag("echo_head", { dropped: toks.slice(0, i).join(" ").slice(0, 60) });
8810
+ return toks.slice(i).join(" ");
8811
+ }
8518
8812
  novelWords(text) {
8519
8813
  return this.words(text).filter((w) => !this.echoWords.has(w));
8520
8814
  }
@@ -8547,6 +8841,22 @@ var VoiceEngine = class _VoiceEngine {
8547
8841
  for (let i = 1; i < w.length; i++) if (pairs.has(`${w[i - 1]} ${w[i]}`)) inRun[i - 1] = inRun[i] = true;
8548
8842
  return inRun.filter(Boolean).length * 2 >= w.length;
8549
8843
  }
8844
+ /** Display a partial — unless it's echo-shaped during/after our own speech (degraded-AEC bleed), or the STT's trailing
8845
+ * re-emit of the line just dispatched (Soniox emits partials after <end>): the host types partials into its input box,
8846
+ * so that tail would sit there after the line was sent (bare-browser #837). */
8847
+ showPartial(text) {
8848
+ if (this.echoActive() && !(this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) return;
8849
+ if (this.echoesDispatch(text, true) || this.tailWordOfDispatch(text)) {
8850
+ this.diag("dup_partial_hidden", { text: text.slice(0, 60) });
8851
+ return;
8852
+ }
8853
+ this.options.onPartial(text);
8854
+ }
8855
+ /** A lone word that is the just-dispatched line's last word (echoesDispatch needs ≥2): display-only filter. */
8856
+ tailWordOfDispatch(text) {
8857
+ const w = text.trim().split(/\s+/).map(normWord).filter(Boolean), d = this.lastDispatchWords.map(normWord).filter(Boolean);
8858
+ return w.length === 1 && d.length > 0 && this.clock.now() - this.lastDispatchAt < _VoiceEngine.DUP_FINAL_MS && d[d.length - 1].startsWith(w[0]);
8859
+ }
8550
8860
  handlePartial(text) {
8551
8861
  if (this.speaking) {
8552
8862
  if (!this.options.bargeIn) return;
@@ -8555,7 +8865,7 @@ var VoiceEngine = class _VoiceEngine {
8555
8865
  this.lastGraceDiag = this.bargeGraceUntil;
8556
8866
  this.diag("grace_suppress", { text: text.slice(0, 60) });
8557
8867
  }
8558
- if (!this.echoActive() || (this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) this.options.onPartial(text);
8868
+ this.showPartial(text);
8559
8869
  return;
8560
8870
  }
8561
8871
  if (this.echoesDispatch(text, true)) {
@@ -8620,7 +8930,7 @@ var VoiceEngine = class _VoiceEngine {
8620
8930
  }
8621
8931
  this.trackSpeculation(text);
8622
8932
  this.trackBackchannel(text);
8623
- if (!this.echoActive() || (this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) this.options.onPartial(text);
8933
+ this.showPartial(text);
8624
8934
  }
8625
8935
  /** Speculative-reflex trigger (listening side only — the speaking branch returns before this): a
8626
8936
  * partial that hasn't CHANGED for speculativeMs and has ≥ speculativeMinWords is stable → fire
@@ -8663,6 +8973,8 @@ var VoiceEngine = class _VoiceEngine {
8663
8973
  * 'Okay.'/'Right.' are fine here (unlike ACKS_NEUTRAL's no-Okay rule): a standalone user "Okay."
8664
8974
  * right after OUR blip is overwhelmingly the blip's echo — the squash guard eating it is the point. */
8665
8975
  static BACKCHANNELS = ["Mm-hm.", "Uh-huh.", "Right.", "Okay."];
8976
+ /** fireBackchannel renders inline tags (a host's bundle may predate it: it spoke them raw). */
8977
+ static TAGGED_BLIPS = true;
8666
8978
  /** Backchannel trigger (listening side only — the speaking branch returns before this): a partial
8667
8979
  * that reached a clause boundary and then stayed UNCHANGED for backchannelMs (a micro-pause, still
8668
8980
  * BEFORE the silence endpoint) fires a blip — if long enough (≥ backchannelMinWords), predominantly
@@ -8699,8 +9011,10 @@ var VoiceEngine = class _VoiceEngine {
8699
9011
  fireBackchannel() {
8700
9012
  const fresh = _VoiceEngine.BACKCHANNELS.filter((x) => !this.recentBc.includes(x));
8701
9013
  const pool = fresh.length ? fresh : _VoiceEngine.BACKCHANNELS;
8702
- const phrase = pool[Math.floor(Math.random() * pool.length)];
8703
- this.recentBc.push(phrase);
9014
+ const line = pool[Math.floor(Math.random() * pool.length)];
9015
+ const r = this.options.emotions ? renderEmotions(line, { show: false }) : { speech: line, prose: line };
9016
+ const phrase = r.prose.trim() || line;
9017
+ this.recentBc.push(line);
8704
9018
  if (this.recentBc.length > 2) this.recentBc.shift();
8705
9019
  const now4 = this.clock.now();
8706
9020
  for (const w of this.words(phrase)) this.echoWords.add(w);
@@ -8719,7 +9033,7 @@ var VoiceEngine = class _VoiceEngine {
8719
9033
  this.bcActive = false;
8720
9034
  };
8721
9035
  this.tts.newContext();
8722
- this.tts.speak(phrase, false);
9036
+ this.tts.speak(r.speech, false);
8723
9037
  log14.debug(`backchannel: "${phrase}"`);
8724
9038
  this.diag("backchannel", { phrase });
8725
9039
  this.options.onBackchannel(phrase);
@@ -8774,10 +9088,29 @@ var VoiceEngine = class _VoiceEngine {
8774
9088
  }
8775
9089
  if (pfx && norm4(nw[pw.length - 1]).startsWith(norm4(pw[pw.length - 1]))) return next;
8776
9090
  }
9091
+ let common = 0;
9092
+ while (common < pw.length && common < nw.length && norm4(pw[common]) === norm4(nw[common])) common++;
9093
+ if (common === nw.length && common >= 4 && common >= 0.75 * pw.length) {
9094
+ this.diag("merge_restated", { dropped: prev.slice(0, 60), revised: true });
9095
+ return next;
9096
+ }
8777
9097
  if (_VoiceEngine.restates(prev, next)) {
8778
9098
  this.diag("merge_restated", { dropped: prev.slice(0, 60) });
8779
9099
  return next;
8780
9100
  }
9101
+ const head = norm4(nw[0] ?? "");
9102
+ if (head.length >= 2 && nw.length >= 4) {
9103
+ for (let s = pw.length - 4; s >= 0; s--) {
9104
+ const w = norm4(pw[s]);
9105
+ if (w.length <= head.length || !w.endsWith(head)) continue;
9106
+ let ok = true;
9107
+ for (let x = 1; x <= 3 && ok; x++) ok = s + x < pw.length && norm4(pw[s + x]) === norm4(nw[x]);
9108
+ if (ok) {
9109
+ this.diag("merge_restated", { dropped: pw.slice(s + 1).join(" ").slice(0, 60), clipped: true });
9110
+ return [...pw.slice(0, s + 1), ...nw.slice(1)].join(" ");
9111
+ }
9112
+ }
9113
+ }
8781
9114
  const max = Math.min(pw.length, nw.length);
8782
9115
  for (let k = max; k > 0; k--) {
8783
9116
  let match = true;
@@ -8786,6 +9119,14 @@ var VoiceEngine = class _VoiceEngine {
8786
9119
  break;
8787
9120
  }
8788
9121
  if (match) return [...pw, ...nw.slice(k)].join(" ");
9122
+ if (k >= 3) {
9123
+ let grown = norm4(nw[k - 1]).startsWith(norm4(pw[pw.length - 1])) && norm4(pw[pw.length - 1]).length > 0;
9124
+ for (let i = 0; grown && i < k - 1; i++) if (norm4(pw[pw.length - k + i]) !== norm4(nw[i])) grown = false;
9125
+ if (grown) {
9126
+ this.diag("merge_restated", { dropped: pw.slice(-k).join(" ").slice(0, 60), grown: true });
9127
+ return [...pw.slice(0, pw.length - k), ...nw].join(" ");
9128
+ }
9129
+ }
8789
9130
  }
8790
9131
  return `${prev} ${next}`;
8791
9132
  }
@@ -8863,7 +9204,13 @@ var VoiceEngine = class _VoiceEngine {
8863
9204
  }
8864
9205
  lastDupPartial = "";
8865
9206
  // dup_partial diag once per distinct partial
8866
- 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;
9207
+ /** A trailing clause comma ("…survive the crash,") or a function word / open wh-word ("…is how") ends no thought. The
9208
+ * comma used to sit inside the word alternation, so it matched only after a space — "crash," dispatched at once (live
9209
+ * duplex 2026-10-14: the reflex answered "So let's say they did survive the crash," then answered again). */
9210
+ 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;
9211
+ /** A hypothetical set up without its consequence: "So let's say they survived." / "Suppose it fails" — the question
9212
+ * comes next. A comma inside ("If it fails, restart it.") or a question mark means the consequence is there. */
9213
+ 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;
8867
9214
  /** The user's line right after a barge cut our speech (or a re-final growing it), with no terminal punctuation:
8868
9215
  * they took the floor mid-thought — Soniox closes on the pause, not the thought (live 20260923-192543: "But isn't it"
8869
9216
  * → "…can be done" → "…with boxing" → "…or semantic?" dispatched 4 times). Held like an incomplete utterance. */
@@ -8875,9 +9222,69 @@ var VoiceEngine = class _VoiceEngine {
8875
9222
  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)");
8876
9223
  /** The utterance sounds like the user paused mid-thought (trailing conjunction/filler/comma). */
8877
9224
  looksIncomplete(text) {
8878
- return _VoiceEngine.TRAIL_RE.test(text.trim()) && !_VoiceEngine.CONTROL_RE.test(text.trim());
8879
- }
8880
- handleUtterance(text) {
9225
+ const t = text.trim();
9226
+ return (_VoiceEngine.TRAIL_RE.test(t) || _VoiceEngine.SETUP_RE.test(t)) && !_VoiceEngine.CONTROL_RE.test(t);
9227
+ }
9228
+ /** Diarized turns worth keeping: none unless labelling is on; while we are audible, a run with <2 novel words is our
9229
+ * own echo diarized as another speaker — dropped from the text too (only when another run remains). */
9230
+ acceptTurns(text, turns) {
9231
+ if (!this.options.speakerLabels || !turns?.length || turns.every((t) => !t.speaker)) return [text, []];
9232
+ if (turns.length >= 2 && this.echoActive()) {
9233
+ const kept = turns.filter((t) => this.novelWords(t.text).length >= 2);
9234
+ if (kept.length && kept.length < turns.length) {
9235
+ this.diag("echo_swallow", { guard: "echo_run", text: turns.filter((t) => !kept.includes(t)).map((t) => t.text).join(" ").slice(0, 60) });
9236
+ return [kept.map((t) => t.text).join(" "), kept];
9237
+ }
9238
+ }
9239
+ return [text, turns];
9240
+ }
9241
+ /** speakerFilter: the text of the turns `keep` accepts (unchanged when all are kept or there are no speaker tags);
9242
+ * null when no turn is kept. */
9243
+ keptTurns(text, turns) {
9244
+ const keep = this.options.speakerResolver.keep;
9245
+ if (!this.options.speakerFilter || !keep || !turns?.length || turns.every((t) => !t.speaker)) return { text, turns };
9246
+ const kept = turns.filter((t) => {
9247
+ try {
9248
+ return keep.call(this.options.speakerResolver, t.speaker, t);
9249
+ } catch {
9250
+ return true;
9251
+ }
9252
+ });
9253
+ if (kept.length === turns.length) return { text, turns };
9254
+ if (!kept.length) return null;
9255
+ return { text: kept.map((t) => t.text).join(" "), turns: kept };
9256
+ }
9257
+ /** Same-speaker neighbours (across merged finals) join into one run. */
9258
+ static joinRuns(turns) {
9259
+ return turns.reduce((acc, t) => {
9260
+ const p = acc[acc.length - 1];
9261
+ if (p?.speaker === t.speaker) acc[acc.length - 1] = { ...p, text: `${p.text} ${t.text}`, endMs: t.endMs };
9262
+ else acc.push({ ...t });
9263
+ return acc;
9264
+ }, []);
9265
+ }
9266
+ /** The dispatched text, labelled once the session is multi-speaker. Words never change: when the turns' words no longer
9267
+ * match the (merged/stripped) text, the whole text goes under the speaker with the most words. */
9268
+ labelTurns(text, turns) {
9269
+ if (!turns.length) return { out: text };
9270
+ const runs = _VoiceEngine.joinRuns(turns);
9271
+ for (const t of runs) this.speakerWords.set(t.speaker, (this.speakerWords.get(t.speaker) ?? 0) + this.words(t.text).length);
9272
+ if (!this.labelling && [...this.speakerWords.values()].filter((n) => n >= 3).length >= 2) {
9273
+ this.labelling = true;
9274
+ this.diag("speaker_labels", { on: true });
9275
+ }
9276
+ if (!this.labelling) return { out: text };
9277
+ 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 }];
9278
+ const names = lines.map((t) => this.options.speakerResolver.name(t.speaker, t));
9279
+ return { out: formatTurns(lines, (t) => names[lines.indexOf(t)]), speakers: [...new Set(names)] };
9280
+ }
9281
+ handleUtterance(text, langs, rawTurns) {
9282
+ let turns;
9283
+ if (this.echoHead) {
9284
+ this.echoHead = false;
9285
+ text = this.dropEchoHead(text);
9286
+ }
9287
+ [text, turns] = this.acceptTurns(text, rawTurns);
8881
9288
  if (this.speaking && this.echoesDispatch(text)) {
8882
9289
  this.diag("utterance_drop", { reason: "dup_final", text: text.slice(0, 60) });
8883
9290
  if (this.pausedAt) {
@@ -8888,6 +9295,8 @@ var VoiceEngine = class _VoiceEngine {
8888
9295
  }
8889
9296
  if (this.decideTimer) {
8890
9297
  this.decideHeldFinal = this.mergeUtterance(this.decideHeldFinal, text);
9298
+ addLangs(this.decideHeldLangs, langs);
9299
+ this.decideHeldTurns.push(...turns);
8891
9300
  this.diag("utterance_held", { reason: "overlap_decide", text: text.slice(0, 60) });
8892
9301
  return;
8893
9302
  }
@@ -8918,6 +9327,8 @@ var VoiceEngine = class _VoiceEngine {
8918
9327
  }
8919
9328
  if (this.pendingUtt) this.mergePath = "merged";
8920
9329
  this.pendingUtt = this.mergeUtterance(this.pendingUtt, text);
9330
+ addLangs(this.pendingLangs, langs);
9331
+ this.pendingTurns.push(...turns);
8921
9332
  if (this.pendingTimer) this.clock.clearTimeout(this.pendingTimer);
8922
9333
  if (this.options.adaptiveEndpointing) {
8923
9334
  const holdMs = computeHoldMs({
@@ -8967,8 +9378,12 @@ var VoiceEngine = class _VoiceEngine {
8967
9378
  }
8968
9379
  this.bcCount = 0;
8969
9380
  this.bcPartial = "";
8970
- const text = this.stripBackchannelEcho(this.pendingUtt);
9381
+ let text = this.stripBackchannelEcho(this.pendingUtt);
8971
9382
  this.pendingUtt = "";
9383
+ const langs = this.pendingLangs;
9384
+ this.pendingLangs = {};
9385
+ let turns = this.pendingTurns;
9386
+ this.pendingTurns = [];
8972
9387
  const path = this.mergePath;
8973
9388
  this.mergePath = "direct";
8974
9389
  const heldTier = this.holdTier;
@@ -8991,7 +9406,27 @@ var VoiceEngine = class _VoiceEngine {
8991
9406
  this.lastDispatchText = text;
8992
9407
  return;
8993
9408
  }
8994
- const meta = refinal ? { supersedes: this.lastDispatchText } : void 0;
9409
+ const cont = !refinal && !answered && !!this.lastDispatchText && this.clock.now() - this.lastDispatchAt < this.options.pendingMergeMs;
9410
+ if (cont) {
9411
+ if (this.speaking) this.interrupt();
9412
+ text = this.mergeUtterance(this.lastDispatchText, text);
9413
+ addLangs(langs, this.lastDispatchLangs);
9414
+ turns = [...this.lastDispatchTurns, ...turns];
9415
+ this.diag("utterance_merge", { reason: "pending_reply", text: text.slice(0, 60) });
9416
+ }
9417
+ const sup = refinal || cont;
9418
+ const lang = dominantLang(langs);
9419
+ this.lastDispatchLangs = langs;
9420
+ this.lastDispatchTurns = turns;
9421
+ const { out, speakers } = this.labelTurns(text, turns);
9422
+ const heardAgo = this.lastWordAt ? Math.max(0, Math.round(this.clock.now() - this.lastWordAt)) : void 0;
9423
+ const meta = sup || lang || speakers || heardAgo !== void 0 ? {
9424
+ ...sup ? { supersedes: this.lastDispatchOut } : {},
9425
+ ...lang ? { language: lang } : {},
9426
+ ...speakers ? { speakers, plain: text } : {},
9427
+ ...heardAgo !== void 0 ? { lastWordAgoMs: heardAgo } : {}
9428
+ } : void 0;
9429
+ this.lastDispatchOut = out;
8995
9430
  this.lastDispatchFlat = flat;
8996
9431
  this.lastDispatchWords = text.trim().split(/\s+/);
8997
9432
  this.lastDispatchText = text;
@@ -8999,18 +9434,28 @@ var VoiceEngine = class _VoiceEngine {
8999
9434
  this.repliedSinceDispatch = false;
9000
9435
  this.dispatchReply = "";
9001
9436
  this.lastDispatchWasQuestion = /\?\s*$/.test(text);
9002
- this.bargedBeforeDispatch = refinal ? this.bargedBeforeDispatch || this.bargedSinceDispatch : this.bargedSinceDispatch;
9437
+ this.bargedBeforeDispatch = sup ? this.bargedBeforeDispatch || this.bargedSinceDispatch : this.bargedSinceDispatch;
9003
9438
  this.bargedSinceDispatch = false;
9004
- this.dispatchRefinal = refinal;
9005
- if (!refinal) this.ackSpent = false;
9439
+ this.dispatchRefinal = sup;
9440
+ if (!sup) this.ackSpent = false;
9006
9441
  this.turnStartAt = this.clock.now();
9007
9442
  this.bargeGraceUntil = this.clock.now() + this.options.bargeGraceMs;
9008
- this.diag("dispatch", { text, path: refinal ? "refinalized" : path, heldTier, question: this.lastDispatchWasQuestion, ...this.bargedBeforeDispatch ? { barged: true } : {} });
9009
- this.options.onUtterance(text, meta);
9443
+ this.diag("dispatch", { text, path: refinal ? "refinalized" : cont ? "merged_pending" : path, heldTier, question: this.lastDispatchWasQuestion, ...this.bargedBeforeDispatch ? { barged: true } : {} });
9444
+ this.options.onUtterance(out, meta);
9010
9445
  }
9011
9446
  }
9447
+ takeHeldLangs() {
9448
+ const l = this.decideHeldLangs;
9449
+ this.decideHeldLangs = {};
9450
+ return l;
9451
+ }
9452
+ takeHeldTurns() {
9453
+ const t = this.decideHeldTurns;
9454
+ this.decideHeldTurns = [];
9455
+ return t;
9456
+ }
9012
9457
  get overlapCapable() {
9013
- return this.usingAec && this.options.overlapPause && !!this.player.pause && !!this.player.resume;
9458
+ return this.usingAec && this.options.overlapPause && this.player.canPause;
9014
9459
  }
9015
9460
  /** Stop/question words that make an overlap an obvious barge — never held for a host decision. */
9016
9461
  static OVERLAP_CEDE_WORDS = /* @__PURE__ */ new Set([
@@ -9040,12 +9485,12 @@ var VoiceEngine = class _VoiceEngine {
9040
9485
  /** Cede the floor to an overlap: interrupt, tell the host, then dispatch a final that was held while deciding. */
9041
9486
  cedeOverlap(trigger, txt) {
9042
9487
  const phase = this.ctxOpen ? "speaking" : "drain";
9043
- const held2 = this.decideHeldFinal;
9488
+ const held2 = this.decideHeldFinal, heldLangs = this.takeHeldLangs(), heldTurns = this.takeHeldTurns();
9044
9489
  this.decideHeldFinal = "";
9045
9490
  this.diag("barge_in", { phase, trigger, novel: this.novelWords(txt).length, total: this.words(txt).length });
9046
9491
  this.interrupt();
9047
9492
  this.options.onBargeIn(phase);
9048
- if (held2) this.handleUtterance(held2);
9493
+ if (held2) this.handleUtterance(held2, heldLangs, heldTurns);
9049
9494
  }
9050
9495
  /** Wait (bounded) for the host's verdict. `fresh` = a re-decision on grown text: it gets its own window — the host
9051
9496
  * was only just asked about it (live: "Oh," → "Oh, that" → "Oh, that— that's new" outgrew every verdict, and the
@@ -9077,7 +9522,7 @@ var VoiceEngine = class _VoiceEngine {
9077
9522
  * is dropped. Words beyond `heard` are a new overlap, decided afresh: if the partial/final has already grown past
9078
9523
  * it, the hold stays and `overlap_pause` is re-emitted for a new verdict (bounded by overlapDecideMs, else it
9079
9524
  * 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). */
9080
- decideOverlap(choice, heard) {
9525
+ decideOverlap(choice, heard, aside) {
9081
9526
  if (!this.pausedAt || !this.speaking) return false;
9082
9527
  if (choice === "yield") {
9083
9528
  this.cedeOverlap("host_yield", this.lastOverlapPartial);
@@ -9096,13 +9541,84 @@ var VoiceEngine = class _VoiceEngine {
9096
9541
  }
9097
9542
  const held2 = this.decideHeldFinal;
9098
9543
  this.decideHeldFinal = "";
9544
+ this.decideHeldLangs = {};
9545
+ this.decideHeldTurns = [];
9099
9546
  this.diag("overlap_resume", { heldMs: Math.round(this.clock.now() - this.pausedAt), reason: "host_keep" });
9100
9547
  if (held2) this.diag("utterance_drop", { reason: "overlap_kept", text: held2.slice(0, 60) });
9101
9548
  this.overlapKeptUntil = held2 ? 0 : this.clock.now() + 4e3;
9102
- this.resetOverlap(true);
9549
+ const asided = !!aside?.trim() && this.startAside(aside.trim());
9550
+ this.resetOverlap(!asided);
9103
9551
  this.lastResumeAt = 0;
9104
9552
  return true;
9105
9553
  }
9554
+ /** Aside (decideOverlap's 3rd arg): synthesize it on the aside TTS while the reply stays HELD, then play it ahead of
9555
+ * the reply, which resumes from its pause point (SpliceSink). Its words are pre-seeded as our own (echo) like a
9556
+ * blip — its mic echo never reads as the user. false = no aside TTS: the caller resumes as usual. */
9557
+ startAside(text) {
9558
+ const t = this.options.asideTts;
9559
+ if (!t || !/[\p{L}\p{N}]/u.test(text)) {
9560
+ this.diag("aside_skipped", { reason: t ? "empty" : "no_tts", text: text.slice(0, 40) });
9561
+ return false;
9562
+ }
9563
+ const epoch = ++this.asideEpoch, chunks = [], t0 = this.clock.now();
9564
+ const live = () => epoch === this.asideEpoch;
9565
+ const give = (reason) => {
9566
+ if (!live()) return;
9567
+ this.asideEpoch++;
9568
+ this.diag("aside_skipped", { reason, text: text.slice(0, 40) });
9569
+ try {
9570
+ t.cancel();
9571
+ } catch {
9572
+ }
9573
+ if (this.speaking && !this.pausedAt) this.player.resume();
9574
+ };
9575
+ const timer = this.clock.setTimeout(() => give("late"), this.options.asideMaxMs);
9576
+ for (const w of this.words(text)) this.echoWords.add(w);
9577
+ this.ackAt = t0;
9578
+ this.lastAck = text;
9579
+ this.echoUntil = Math.max(this.echoUntil, t0 + 2500 + this.options.asideMaxMs);
9580
+ t.onAudio = (c) => {
9581
+ if (live()) chunks.push(c);
9582
+ };
9583
+ t.onDone = () => {
9584
+ if (!live()) return;
9585
+ this.clock.clearTimeout(timer);
9586
+ const n = chunks.reduce((a, c) => a + c.length, 0);
9587
+ if (!n || !this.speaking || this.pausedAt) return give(!n ? "no_audio" : this.pausedAt ? "held_again" : "turn_over");
9588
+ this.asideEpoch++;
9589
+ const clip2 = new Uint8Array(n);
9590
+ let i = 0;
9591
+ for (const c of chunks) {
9592
+ clip2.set(c, i);
9593
+ i += c.length;
9594
+ }
9595
+ const from = this.player.splice(clip2);
9596
+ const now4 = this.clock.now();
9597
+ this.overlapKeptUntil = Math.max(this.overlapKeptUntil, now4 + 4e3);
9598
+ this.echoUntil = Math.max(this.echoUntil, now4 + 2500 + this.player.drainMs());
9599
+ this.diag("aside", { text: text.slice(0, 40), fromMs: Math.round(from), clipMs: Math.round(n / (TTS_SAMPLE_RATE * 2) * 1e3), synthMs: Math.round(now4 - t0) });
9600
+ };
9601
+ try {
9602
+ const lang = this.tts.options?.language, ao = t.options;
9603
+ if (lang && ao) ao.language = lang;
9604
+ t.newContext();
9605
+ t.speak(text, false);
9606
+ } catch (e) {
9607
+ this.clock.clearTimeout(timer);
9608
+ give(`error: ${e instanceof Error ? e.message : e}`);
9609
+ return true;
9610
+ }
9611
+ return true;
9612
+ }
9613
+ cancelAside(reason) {
9614
+ if (!this.options.asideTts) return;
9615
+ this.asideEpoch++;
9616
+ try {
9617
+ this.options.asideTts.cancel();
9618
+ } catch (e) {
9619
+ log14.debug(`aside cancel (${reason}): ${e instanceof Error ? e.message : e}`);
9620
+ }
9621
+ }
9106
9622
  armResume() {
9107
9623
  if (this.resumeTimer) this.clock.clearTimeout(this.resumeTimer);
9108
9624
  this.resumeTimer = this.clock.setTimeout(() => {
@@ -9123,11 +9639,13 @@ var VoiceEngine = class _VoiceEngine {
9123
9639
  this.decideTimer = null;
9124
9640
  }
9125
9641
  this.decideSince = 0;
9126
- const held2 = this.decideHeldFinal;
9642
+ const held2 = this.decideHeldFinal, heldLangs = this.takeHeldLangs(), heldTurns = this.takeHeldTurns();
9127
9643
  this.decideHeldFinal = "";
9128
9644
  if (held2) {
9129
9645
  this.diag("utterance_release", { reason: "overlap_hold_exit", text: held2.slice(0, 60) });
9130
9646
  this.pendingUtt = this.mergeUtterance(this.pendingUtt, held2);
9647
+ addLangs(this.pendingLangs, heldLangs);
9648
+ this.pendingTurns.push(...heldTurns);
9131
9649
  if (this.pendingTimer) this.clock.clearTimeout(this.pendingTimer);
9132
9650
  this.pendingTimer = this.clock.setTimeout(() => this.flushUtterance(), 0);
9133
9651
  }
@@ -9177,13 +9695,6 @@ var VoiceEngine = class _VoiceEngine {
9177
9695
  }
9178
9696
  };
9179
9697
 
9180
- // node_modules/@bod.ee/voice/src/core/types.ts
9181
- var STT_SAMPLE_RATE = 16e3;
9182
- var TTS_SAMPLE_RATE = 44100;
9183
- async function resolveAuth(auth) {
9184
- return typeof auth === "function" ? await auth() : auth;
9185
- }
9186
-
9187
9698
  // node_modules/@bod.ee/voice/src/core/spokenSplitter.ts
9188
9699
  var OPEN = "<spoken>";
9189
9700
  var CLOSE = "</spoken>";
@@ -9262,6 +9773,52 @@ var SpokenSplitter = class {
9262
9773
  }
9263
9774
  };
9264
9775
 
9776
+ // node_modules/@bod.ee/voice/src/core/pronounce.ts
9777
+ var PRONOUNCE = {
9778
+ TODO: "to-do",
9779
+ TODOs: "to-dos",
9780
+ ToDo: "to-do",
9781
+ Todo: "to-do",
9782
+ todo: "to-do",
9783
+ todos: "to-dos",
9784
+ DOING: "doing",
9785
+ BACKLOG: "backlog",
9786
+ DONE: "done",
9787
+ PARKED: "parked",
9788
+ BLOCKED: "blocked",
9789
+ FIXME: "fix-me",
9790
+ WIP: "work-in-progress"
9791
+ };
9792
+ var WORD = /[\p{L}\p{N}_]+/gu;
9793
+ var Pronouncer = class {
9794
+ constructor(map = PRONOUNCE) {
9795
+ this.map = map;
9796
+ this.prefixes = new Set(Object.keys(map).flatMap((k) => [...k].map((_, i) => k.slice(0, i + 1))));
9797
+ }
9798
+ map;
9799
+ tail = "";
9800
+ prefixes;
9801
+ /** Replace whole-word keys in a complete text (no hold). */
9802
+ apply(t) {
9803
+ return t.replace(WORD, (w) => Object.hasOwn(this.map, w) ? this.map[w] : w);
9804
+ }
9805
+ feed(delta) {
9806
+ const t = this.tail + delta;
9807
+ const last = /[\p{L}\p{N}_]+$/u.exec(t);
9808
+ const hold = last && this.prefixes.has(last[0]) ? last[0] : "";
9809
+ this.tail = hold;
9810
+ return this.apply(hold ? t.slice(0, -hold.length) : t);
9811
+ }
9812
+ flush() {
9813
+ const t = this.apply(this.tail);
9814
+ this.tail = "";
9815
+ return t;
9816
+ }
9817
+ reset() {
9818
+ this.tail = "";
9819
+ }
9820
+ };
9821
+
9265
9822
  // node_modules/@bod.ee/voice/src/adapters/soniox.ts
9266
9823
  var log15 = forComponent2("SonioxSTT");
9267
9824
  var now = () => performance.now();
@@ -9274,6 +9831,23 @@ var SonioxSTTOptions = class {
9274
9831
  * `stt-rt-v5` is live today (verified against GET https://api.soniox.com/v1/models). */
9275
9832
  model = "stt-rt-v5";
9276
9833
  languageHints = ["en"];
9834
+ /** Opt-in Soniox language identification (`enable_language_identification`): every token is tagged with a
9835
+ * `language` code, and onUtterance's 3rd arg reports the utterance's dominant one (by characters). */
9836
+ languageIdentification = false;
9837
+ /** Restrict recognition to `languageHints` (Soniox `language_hints_strict`, best-effort) AND drop any token written in a
9838
+ * script none of the hinted languages use (Latin always passes: names, brands). A he+en call heard Punjabi
9839
+ * ("ਹਾਂ ਜੀ।") in noise and the junk reached the turn + chat title (bare-browser #938). */
9840
+ languageHintsStrict = false;
9841
+ /** Drop words written in a script that is neither a hinted language's, Latin, nor the utterance's own main script —
9842
+ * stray noise transcribed in a third script ('ਹਾਂ ਜੀ। בסדר…' → 'בסדר…'). Unlike languageHintsStrict it never drops
9843
+ * the language the user actually speaks, hinted or not (a Hebrew speaker on an en-US phone). */
9844
+ dropStrayScripts = false;
9845
+ /** Opt-in Soniox speaker diarization (`enable_speaker_diarization`): onUtterance's 4th arg carries the
9846
+ * utterance split into per-speaker turns (raw Soniox speaker ids; flaps merged by speakerRuns). */
9847
+ diarization = false;
9848
+ /** Custom vocabulary (Soniox `context.terms`): rare words the model should prefer — names, brands,
9849
+ * projects ("Livshitz", not "Lead Sheets"). Empty = no context sent. Soniox caps context at ~8k tokens. */
9850
+ terms = [];
9277
9851
  /** Client-side endpoint: finalized text + no new tokens for this long = utterance (don't wait for
9278
9852
  * Soniox's semantic <end>, which adds 0.5-1.5s — the difference between ping-pong and lag). */
9279
9853
  silenceEndpointMs = 500;
@@ -9288,11 +9862,19 @@ var SonioxSTT = class {
9288
9862
  sourceStarted = false;
9289
9863
  onPartial = () => {
9290
9864
  };
9865
+ /** `language` = dominant spoken language of the utterance (only with `languageIdentification`). */
9291
9866
  onUtterance = () => {
9292
9867
  };
9293
9868
  /** mic energy (RMS) per chunk — drives the energy-based heuristic barge-in tier */
9294
9869
  onLevel = () => {
9295
9870
  };
9871
+ /** Audio tap on the Soniox clock: each chunk sent to the current connection, with `streamMs` = where it starts in that
9872
+ * connection's stream — the timeline token `start_ms`/`end_ms` (and so SpeakerTurn.startMs/endMs) refer to. A
9873
+ * reconnect starts a new stream at 0. Chunks dropped while disconnected are not reported. A throwing handler is
9874
+ * disabled (logged) — never affects transcription. */
9875
+ onAudio = null;
9876
+ sentBytes = 0;
9877
+ // s16le bytes sent on the current connection
9296
9878
  /** Unrecoverable: the mic source stopped delivering audio (Soniox starves → idle-timeout reconnect
9297
9879
  * loop). The host tears voice down instead of spinning forever. */
9298
9880
  onFatal = () => {
@@ -9323,6 +9905,14 @@ var SonioxSTT = class {
9323
9905
  endpointTimer = null;
9324
9906
  firstTokenAt = 0;
9325
9907
  // first speech token in current utterance
9908
+ lastWordEndMs = 0;
9909
+ // end_ms (Soniox stream clock) of the current utterance's last spoken token
9910
+ finalLangChars = {};
9911
+ // language → chars of finalized tokens (current utterance)
9912
+ partialLangChars = {};
9913
+ finalToks = [];
9914
+ // diarization only: finalized tokens of the current utterance
9915
+ partialToks = [];
9326
9916
  constructor(options) {
9327
9917
  this.options = { ...new SonioxSTTOptions(), ...options };
9328
9918
  }
@@ -9332,6 +9922,8 @@ var SonioxSTT = class {
9332
9922
  async connectWs() {
9333
9923
  const apiKey = await resolveAuth(this.options.auth);
9334
9924
  this.ws = new WebSocket("wss://stt-rt.soniox.com/transcribe-websocket");
9925
+ this.sentBytes = 0;
9926
+ this.lastWordEndMs = 0;
9335
9927
  await new Promise((res, rej) => {
9336
9928
  this.ws.onopen = () => res();
9337
9929
  this.ws.onerror = (e) => rej(new Error(`soniox ws: ${e.message || "connect failed"}`));
@@ -9344,7 +9936,11 @@ var SonioxSTT = class {
9344
9936
  sample_rate: STT_SAMPLE_RATE,
9345
9937
  num_channels: 1,
9346
9938
  language_hints: this.options.languageHints,
9347
- enable_endpoint_detection: true
9939
+ enable_endpoint_detection: true,
9940
+ ...this.options.languageHintsStrict ? { language_hints_strict: true } : {},
9941
+ ...this.options.languageIdentification ? { enable_language_identification: true } : {},
9942
+ ...this.options.diarization ? { enable_speaker_diarization: true } : {},
9943
+ ...this.options.terms?.length ? { context: { terms: this.options.terms } } : {}
9348
9944
  })
9349
9945
  );
9350
9946
  this.ws.onmessage = (ev) => this.handle(JSON.parse(String(ev.data)));
@@ -9367,8 +9963,10 @@ var SonioxSTT = class {
9367
9963
  const combined = (this.finalText + this.partialText).trim();
9368
9964
  if (!combined || now() - this.lastChangeAt < this.options.silenceEndpointMs) return;
9369
9965
  if (this.firstTokenAt) log15.debug(`stt: ${Math.round(now() - this.firstTokenAt)}ms first-token\u2192silence-endpoint, "${combined.slice(0, 60)}"`);
9966
+ const language = this.language(true);
9967
+ const turns = this.turns(true), ago = this.lastWordAgoMs();
9370
9968
  this.reset();
9371
- this.onUtterance(combined, now());
9969
+ this.onUtterance(this.stray(combined), now(), language, turns, ago);
9372
9970
  }, 120);
9373
9971
  this.endpointTimer.unref?.();
9374
9972
  this.startedChunksAt = now();
@@ -9395,7 +9993,19 @@ var SonioxSTT = class {
9395
9993
  sum += v * v;
9396
9994
  }
9397
9995
  this.onLevel(Math.sqrt(sum / (chunk.byteLength / 2)));
9398
- if (this.ws.readyState === WebSocket.OPEN) this.ws.send(chunk);
9996
+ if (this.ws.readyState === WebSocket.OPEN) {
9997
+ this.ws.send(chunk);
9998
+ const at = this.sentBytes / 2 / STT_SAMPLE_RATE * 1e3;
9999
+ this.sentBytes += chunk.byteLength;
10000
+ if (this.onAudio) {
10001
+ try {
10002
+ this.onAudio(chunk, at);
10003
+ } catch (e) {
10004
+ this.onAudio = null;
10005
+ log15.warn(`onAudio threw \u2014 audio tap disabled: ${e instanceof Error ? e.message : e}`);
10006
+ }
10007
+ }
10008
+ }
9399
10009
  });
9400
10010
  }
9401
10011
  handle(m) {
@@ -9404,30 +10014,78 @@ var SonioxSTT = class {
9404
10014
  return log15.error(`soniox: ${m.error_message}`);
9405
10015
  }
9406
10016
  let endpoint = false;
10017
+ const foreign = this.options.languageHintsStrict ? foreignScript(this.options.languageHints) : null;
10018
+ if (foreign && m.tokens?.length) m = { ...m, tokens: m.tokens.filter((t) => t.text === "<end>" || !foreign(t.text)) };
9407
10019
  for (const t of m.tokens ?? []) {
10020
+ if (t.text !== "<end>" && typeof t.end_ms === "number" && String(t.text).trim() && t.end_ms > this.lastWordEndMs) this.lastWordEndMs = t.end_ms;
9408
10021
  if (t.text === "<end>") endpoint = true;
9409
- else if (t.is_final) this.finalText += t.text;
10022
+ else if (t.is_final) {
10023
+ this.finalText += t.text;
10024
+ this.countLang(this.finalLangChars, t);
10025
+ if (this.options.diarization) this.finalToks.push(t);
10026
+ }
9410
10027
  }
9411
- this.partialText = (m.tokens ?? []).filter((t) => !t.is_final && t.text !== "<end>").map((t) => t.text).join("");
10028
+ const partials = (m.tokens ?? []).filter((t) => !t.is_final && t.text !== "<end>");
10029
+ this.partialText = partials.map((t) => t.text).join("");
10030
+ this.partialLangChars = {};
10031
+ for (const t of partials) this.countLang(this.partialLangChars, t);
10032
+ if (this.options.diarization) this.partialToks = partials;
9412
10033
  const combined = this.finalText + this.partialText;
9413
10034
  if (combined !== this.lastCombined) {
9414
10035
  this.lastCombined = combined;
9415
10036
  this.lastChangeAt = now();
9416
10037
  if (!this.firstTokenAt && combined.trim()) this.firstTokenAt = now();
9417
10038
  }
9418
- this.onPartial(combined);
10039
+ this.onPartial(this.stray(combined), this.turns(true));
9419
10040
  if (endpoint && this.finalText.trim()) {
9420
10041
  const utterance = this.finalText.trim();
9421
10042
  if (this.firstTokenAt) log15.debug(`stt: ${Math.round(now() - this.firstTokenAt)}ms first-token\u2192endpoint, "${utterance.slice(0, 60)}"`);
10043
+ const language = this.language(false);
10044
+ const turns = this.turns(false), ago = this.lastWordAgoMs();
9422
10045
  this.reset();
9423
- this.onUtterance(utterance, now());
10046
+ this.onUtterance(this.stray(utterance), now(), language, turns, ago);
10047
+ }
10048
+ }
10049
+ /** How long ago (ms, now) the utterance's last spoken word ended: the audio sent so far minus that token's end_ms on
10050
+ * Soniox's stream clock — the user's real last word, before Soniox's latency and the silence endpoint. */
10051
+ lastWordAgoMs() {
10052
+ if (!this.lastWordEndMs) return void 0;
10053
+ return Math.max(0, Math.round(this.sentBytes / 2 / STT_SAMPLE_RATE * 1e3 - this.lastWordEndMs));
10054
+ }
10055
+ stray(text) {
10056
+ return this.options.dropStrayScripts ? dropStrayScripts(text, this.options.languageHints) : text;
10057
+ }
10058
+ countLang(acc, t) {
10059
+ if (typeof t.language === "string" && t.language && String(t.text).trim()) acc[t.language] = (acc[t.language] ?? 0) + String(t.text).trim().length;
10060
+ }
10061
+ /** Dominant language (most characters) of the current utterance; undefined when Soniox tagged none. */
10062
+ language(withPartials) {
10063
+ const acc = { ...this.finalLangChars };
10064
+ if (withPartials) for (const [l, n] of Object.entries(this.partialLangChars)) acc[l] = (acc[l] ?? 0) + n;
10065
+ let best;
10066
+ let max = 0;
10067
+ for (const [l, n] of Object.entries(acc)) if (n > max) {
10068
+ best = l;
10069
+ max = n;
9424
10070
  }
10071
+ return best;
10072
+ }
10073
+ /** Per-speaker turns of the current utterance (diarization only; undefined otherwise or when Soniox tagged no speaker). */
10074
+ turns(withPartials) {
10075
+ if (!this.options.diarization) return void 0;
10076
+ const words4 = sonioxWords(withPartials ? [...this.finalToks, ...this.partialToks] : this.finalToks);
10077
+ return words4.some((w) => w.spk != null) ? speakerRuns(words4) : void 0;
9425
10078
  }
9426
10079
  reset() {
10080
+ this.finalToks = [];
10081
+ this.partialToks = [];
10082
+ this.finalLangChars = {};
10083
+ this.partialLangChars = {};
9427
10084
  this.finalText = "";
9428
10085
  this.partialText = "";
9429
10086
  this.lastCombined = "";
9430
10087
  this.firstTokenAt = 0;
10088
+ this.lastWordEndMs = 0;
9431
10089
  }
9432
10090
  stop() {
9433
10091
  this.stopped = true;
@@ -9438,6 +10096,65 @@ var SonioxSTT = class {
9438
10096
  this.ws?.close();
9439
10097
  }
9440
10098
  };
10099
+ var SCRIPTS = {
10100
+ he: ["Hebrew"],
10101
+ yi: ["Hebrew"],
10102
+ ar: ["Arabic"],
10103
+ fa: ["Arabic"],
10104
+ ur: ["Arabic"],
10105
+ ru: ["Cyrillic"],
10106
+ uk: ["Cyrillic"],
10107
+ bg: ["Cyrillic"],
10108
+ sr: ["Cyrillic"],
10109
+ el: ["Greek"],
10110
+ hi: ["Devanagari"],
10111
+ mr: ["Devanagari"],
10112
+ ne: ["Devanagari"],
10113
+ bn: ["Bengali"],
10114
+ pa: ["Gurmukhi"],
10115
+ gu: ["Gujarati"],
10116
+ ta: ["Tamil"],
10117
+ te: ["Telugu"],
10118
+ th: ["Thai"],
10119
+ ja: ["Hiragana", "Katakana", "Han"],
10120
+ zh: ["Han"],
10121
+ ko: ["Hangul"],
10122
+ ka: ["Georgian"],
10123
+ hy: ["Armenian"],
10124
+ am: ["Ethiopic"]
10125
+ };
10126
+ var SCRIPT_RES = [.../* @__PURE__ */ new Set(["Latin", ...Object.values(SCRIPTS).flat(), "Gurmukhi", "Devanagari"])].map((sc) => [sc, new RegExp(`\\p{Script=${sc}}`, "u")]);
10127
+ var LETTER = new RegExp("\\p{L}", "u");
10128
+ function mainScript(text) {
10129
+ const n = {};
10130
+ for (const ch of text) {
10131
+ if (!LETTER.test(ch)) continue;
10132
+ const sc = SCRIPT_RES.find(([, re]) => re.test(ch))?.[0] ?? "Other";
10133
+ n[sc] = (n[sc] ?? 0) + 1;
10134
+ }
10135
+ return Object.entries(n).sort((a, b) => b[1] - a[1])[0]?.[0] ?? "";
10136
+ }
10137
+ function dropStrayScripts(text, hints) {
10138
+ const main2 = mainScript(text), foreign = foreignScript(hints, main2 && main2 !== "Other" ? [main2] : []);
10139
+ if (!/\S/.test(text)) return text;
10140
+ const lead2 = /^\s/.test(text) ? " " : "", trail = /\s$/.test(text) ? " " : "";
10141
+ const kept = text.split(/\s+/).filter((w) => w && !foreign(w));
10142
+ return kept.length ? lead2 + kept.join(" ") + trail : "";
10143
+ }
10144
+ var foreignTests = /* @__PURE__ */ new Map();
10145
+ function foreignScript(hints, also = []) {
10146
+ const key = [...hints, "|", ...also].join(",");
10147
+ let f = foreignTests.get(key);
10148
+ if (f) return f;
10149
+ const scripts = [.../* @__PURE__ */ new Set(["Common", "Inherited", "Latin", ...also, ...hints.flatMap((h) => SCRIPTS[h.slice(0, 2).toLowerCase()] ?? [])])];
10150
+ const ok = new RegExp(`^[${scripts.map((sc) => `\\p{Script_Extensions=${sc}}`).join("")}]$`, "u");
10151
+ f = (text) => {
10152
+ for (const ch of text) if (!ok.test(ch)) return true;
10153
+ return false;
10154
+ };
10155
+ foreignTests.set(key, f);
10156
+ return f;
10157
+ }
9441
10158
 
9442
10159
  // node_modules/@bod.ee/voice/src/adapters/cartesia.ts
9443
10160
  var log16 = forComponent2("CartesiaTTS");
@@ -9448,12 +10165,34 @@ var CartesiaTTSOptions = class {
9448
10165
  model = "sonic-3.5";
9449
10166
  /** 'apiKey' (server/CLI) → `api_key=` URL param; 'token' (browser, BE-minted) → `access_token=`. */
9450
10167
  authMode = "apiKey";
10168
+ /** Cartesia `language` code ('en', 'he', …). Unset → field omitted (provider default). Mutable:
10169
+ * set `tts.options.language` before `newContext()` to switch per context (mixed-language docs) —
10170
+ * it is latched there, so every frame of one context carries the same language. The model must
10171
+ * support it (sonic-3/3.5: 42 languages incl. 'he'; sonic-2/turbo: no Hebrew). */
10172
+ language;
10173
+ /** Cartesia `max_buffer_delay_ms` (integer, 0-5000; provider default 3000): the longest Cartesia
10174
+ * holds `continue: true` text waiting for more before it starts generating. Unset → field omitted
10175
+ * (provider default). Cartesia buffers so tiny inputs (single words/tokens) don't come out choppy
10176
+ * or with unnatural prosody; its guidance is to lower it (0 = generate each frame at once) only
10177
+ * when the caller does its own buffering, i.e. feeds phrase/sentence-sized chunks. Measured: an
10178
+ * unflushed continue frame reaches first audio in ~3 s at the default vs ~0.2 s at 0. A
10179
+ * `continue: false` frame / `end()` flushes regardless. Mutable like `language`: set
10180
+ * `tts.options.maxBufferDelayMs` before `newContext()` — latched there, one value per context. */
10181
+ maxBufferDelayMs;
10182
+ /** Sonic generation_config.emotion ('excited', 'content', 'sad', …). Mutable: the next frame carries it. */
10183
+ emotion;
10184
+ /** Written token → spoken form, swapped before synthesis (whole word, case-sensitive; core/pronounce.ts).
10185
+ * Read at construction. `{}` = off. */
10186
+ pronounce = PRONOUNCE;
9451
10187
  };
9452
10188
  var CartesiaTTS = class _CartesiaTTS {
9453
10189
  options;
9454
10190
  ws;
9455
10191
  ctxSeq = 0;
9456
10192
  ctxId = "";
10193
+ ctxLanguage;
10194
+ ctxMaxBufferDelayMs;
10195
+ say;
9457
10196
  onAudio = () => {
9458
10197
  };
9459
10198
  onDone = () => {
@@ -9493,6 +10232,9 @@ var CartesiaTTS = class _CartesiaTTS {
9493
10232
  static CB_PROBE_MS = 3e4;
9494
10233
  constructor(options) {
9495
10234
  this.options = { ...new CartesiaTTSOptions(), ...options };
10235
+ this.say = new Pronouncer(this.options.pronounce);
10236
+ this.ctxLanguage = this.options.language;
10237
+ this.ctxMaxBufferDelayMs = this.options.maxBufferDelayMs;
9496
10238
  }
9497
10239
  closed = false;
9498
10240
  connecting = null;
@@ -9582,6 +10324,8 @@ var CartesiaTTS = class _CartesiaTTS {
9582
10324
  newContext() {
9583
10325
  this.ctxId = `ctx-${++this.ctxSeq}`;
9584
10326
  this.firstAudioAt = 0;
10327
+ this.ctxLanguage = this.options.language;
10328
+ this.ctxMaxBufferDelayMs = this.options.maxBufferDelayMs;
9585
10329
  return this.ctxId;
9586
10330
  }
9587
10331
  frame(transcript, cont) {
@@ -9589,26 +10333,42 @@ var CartesiaTTS = class _CartesiaTTS {
9589
10333
  model_id: this.options.model,
9590
10334
  transcript,
9591
10335
  voice: { mode: "id", id: this.options.voiceId },
10336
+ ...this.ctxLanguage ? { language: this.ctxLanguage } : {},
9592
10337
  output_format: { container: "raw", encoding: "pcm_s16le", sample_rate: TTS_SAMPLE_RATE },
9593
10338
  context_id: this.ctxId,
9594
10339
  continue: cont,
9595
- ...this.wantTimestamps ? { add_timestamps: true } : {}
10340
+ ...this.ctxMaxBufferDelayMs != null ? { max_buffer_delay_ms: this.ctxMaxBufferDelayMs } : {},
10341
+ // != null: 0 must be sent
10342
+ ...this.wantTimestamps ? { add_timestamps: true } : {},
10343
+ ...this.options.emotion ? { generation_config: { emotion: this.options.emotion } } : {}
9596
10344
  });
9597
10345
  }
9598
10346
  speak(text, cont) {
9599
10347
  if (this.down) return;
10348
+ text = cont ? this.say.feed(text) : this.say.feed(text) + this.say.flush();
9600
10349
  if (cont && !text) return;
9601
10350
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame(text, cont));
9602
10351
  else void this.ensureConnected().then(() => this.ws?.readyState === WebSocket.OPEN && this.ws.send(this.frame(text, cont)));
9603
10352
  }
10353
+ /** Generate what this context has buffered NOW (a `flush: true` frame) — the context stays open and later
10354
+ * continuations keep its prosody. A host streaming word-sized deltas flushes the reply's first clause, so speech
10355
+ * starts there instead of waiting for Cartesia's buffer (a sentence end, or max_buffer_delay_ms). */
10356
+ flush() {
10357
+ if (this.down || !this.ctxId || this.ws?.readyState !== WebSocket.OPEN) return;
10358
+ const held2 = this.say.flush();
10359
+ if (held2) this.ws.send(this.frame(held2, true));
10360
+ this.ws.send(JSON.stringify({ ...JSON.parse(this.frame("", true)), flush: true }));
10361
+ }
9604
10362
  end() {
10363
+ const held2 = this.say.flush();
9605
10364
  if (this.down) {
9606
10365
  this.onDone();
9607
10366
  return;
9608
10367
  }
9609
- if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame("", false));
10368
+ if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame(held2, false));
9610
10369
  }
9611
10370
  cancel() {
10371
+ this.say.reset();
9612
10372
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(JSON.stringify({ context_id: this.ctxId, cancel: true }));
9613
10373
  }
9614
10374
  startProbe() {
@@ -10401,7 +11161,7 @@ function pendingQuestion(text, lastWords = 6) {
10401
11161
  const t = sentence.slice(-lastWords).join(" ");
10402
11162
  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(" "));
10403
11163
  }
10404
- var SCRIPTS = [
11164
+ var SCRIPTS2 = [
10405
11165
  [new RegExp("\\p{Script=Hebrew}", "gu"), "he"],
10406
11166
  [new RegExp("\\p{Script=Arabic}", "gu"), "ar"],
10407
11167
  [new RegExp("\\p{Script=Cyrillic}", "gu"), "ru"],
@@ -10423,7 +11183,7 @@ var STOP3 = Object.fromEntries(Object.entries({
10423
11183
  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",
10424
11184
  nl: "hoi dank de het een en is ik je niet van dat met voor maar mijn wat"
10425
11185
  }).map(([k, v]) => [k, new Set(v.split(" "))]));
10426
- var scriptOf = (w) => SCRIPTS.find(([re]) => {
11186
+ var scriptOf = (w) => SCRIPTS2.find(([re]) => {
10427
11187
  re.lastIndex = 0;
10428
11188
  return re.test(w);
10429
11189
  })?.[1] ?? null;
@@ -10450,7 +11210,7 @@ var languageName = (lang) => {
10450
11210
  }
10451
11211
  };
10452
11212
  function fitsLanguage(line, lang) {
10453
- const own = SCRIPTS.find(([, l]) => l === lang)?.[0];
11213
+ const own = SCRIPTS2.find(([, l]) => l === lang)?.[0];
10454
11214
  if (own) return (line.match(own)?.length ?? 0) > 0 && !new RegExp("\\p{Script=Latin}{2,}", "u").test(line);
10455
11215
  if (/[^\p{Script=Latin}\p{M}\p{P}\p{Z}\p{N}\p{S}]/u.test(line)) return false;
10456
11216
  const mine = STOP3[lang];
@@ -10722,7 +11482,7 @@ var TurnGate = class {
10722
11482
  }
10723
11483
  /** An STT final. */
10724
11484
  final(text, sttId) {
10725
- let t = text.trim();
11485
+ let t = text.trim(), replaces;
10726
11486
  if (!t) return this.o.onDrop(text, "empty");
10727
11487
  if (this.held) {
10728
11488
  const h = this.held;
@@ -10730,6 +11490,7 @@ var TurnGate = class {
10730
11490
  this.held = void 0;
10731
11491
  if (!(norm3(t) + " ").startsWith(norm3(h.text) + " ")) t = `${h.text} ${t}`;
10732
11492
  sttId ??= h.sttId;
11493
+ replaces = h.replaces;
10733
11494
  }
10734
11495
  const key = norm3(t), now4 = this.o.now(), last = this.last;
10735
11496
  if (last) {
@@ -10743,10 +11504,18 @@ var TurnGate = class {
10743
11504
  const mid = endsMidPhrase(t);
10744
11505
  if (mid || this.o.settleMs && !/\?["')\]]*$/.test(t)) {
10745
11506
  const ms = mid ? this.o.holdMs : this.o.settleMs;
10746
- this.held = { text: t, sttId, ms, timer: setTimeout(() => this.flush(), ms) };
11507
+ this.held = { text: t, sttId, ms, replaces, timer: setTimeout(() => this.flush(), ms) };
10747
11508
  return this.o.onHold(t);
10748
11509
  }
10749
- this.send(t, key, sttId, "new");
11510
+ this.send(t, key, sttId, "new", replaces);
11511
+ }
11512
+ /** The user kept talking after turn `id` went out and before its reply made a sound: it was not over. Its words go back
11513
+ * to being held — the rest merges in and one whole turn replaces it; silence sends them again as they were. */
11514
+ reopen(id) {
11515
+ const l = this.last;
11516
+ if (l?.id !== id || this.held) return;
11517
+ this.last = void 0;
11518
+ this.held = { text: l.text, sttId: l.sttId, ms: this.o.holdMs, replaces: id, timer: setTimeout(() => this.flush(), this.o.holdMs) };
10750
11519
  }
10751
11520
  /** The user is speaking again (a partial): a held fragment waits for the rest. */
10752
11521
  speaking() {
@@ -10765,7 +11534,7 @@ var TurnGate = class {
10765
11534
  clearTimeout(h.timer);
10766
11535
  this.held = void 0;
10767
11536
  if (this.closed && isClosing(h.text)) return this.o.onDrop(h.text, "goodbye");
10768
- this.send(h.text, norm3(h.text), h.sttId, "new");
11537
+ this.send(h.text, norm3(h.text), h.sttId, "new", h.replaces);
10769
11538
  }
10770
11539
  end() {
10771
11540
  if (this.held) {
@@ -14143,6 +14912,7 @@ var SecondChannel = class {
14143
14912
  bridgeBlock() {
14144
14913
  const w = words3(this.heard), intent = turnIntent(this.heard);
14145
14914
  if (!this.lang) return "language unknown";
14915
+ if (this.ownWords) return this.blocked("bridge");
14146
14916
  if (intent === "closer") return "no answer due";
14147
14917
  if (this.late) return this.blocked("bridge");
14148
14918
  const why = intent === "ask" ? w.length <= 6 && isPhatic(this.heard, this.bank) ? "phatic" : "" : this.shortTurn(w.length);
@@ -14167,7 +14937,7 @@ var SecondChannel = class {
14167
14937
  if (!this.lang) return "silence";
14168
14938
  if (trigger === "working") return this.bridgeBlock() ? "silence" : "bridge";
14169
14939
  if (w.length <= this.o.shortTurnWords || w.length <= 6 && isPhatic(this.heard, this.bank)) return "silence";
14170
- 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;
14940
+ 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;
14171
14941
  return k && this.allowed(k) ? k : "silence";
14172
14942
  }
14173
14943
  /** Plan (or, with `heard`, revise) the backchannels of the user's turn — async, never on the hot path. */
@@ -14363,7 +15133,7 @@ var SecondChannel = class {
14363
15133
  this.bridgeDraft = null;
14364
15134
  this.askLeaf = null;
14365
15135
  this.bridgeReady = Promise.resolve();
14366
- 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;
15136
+ if (!this.o.generate || !(this.ownWords || this.o.generateFor.includes("bridge")) || !this.lang || !this.heard) return;
14367
15137
  const gen = this.gen, ac = new AbortController(), own = this.ownWords, heard = this.heard;
14368
15138
  const ctx = { kind: "bridge", heard: this.tail(), state: this.main, avoid: [...this.recent], lang: this.lang, ...own ? { ask: true } : {} };
14369
15139
  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) => {
@@ -14500,6 +15270,10 @@ function closedQuestion(reply) {
14500
15270
 
14501
15271
  // src/voice/call.ts
14502
15272
  var log30 = forComponent("VoiceCall");
15273
+ function newWords(text, turn) {
15274
+ const said = new Set(norm3(turn).split(" "));
15275
+ return norm3(text).split(" ").filter((w) => w && !said.has(w)).length;
15276
+ }
14503
15277
  var VoiceCallOptions = class {
14504
15278
  /** Host runs MAIN with exactly `text` (a replace: roll the `replaces` turn back first). */
14505
15279
  sendTurn = () => {
@@ -14626,9 +15400,10 @@ var VoiceCall = class {
14626
15400
  if (typeof aec === "boolean") this.fill.options.aec = aec;
14627
15401
  this.gate.speaking();
14628
15402
  const c = this.cur;
14629
- if (this.o.mode === "live" && c?.running && !c.worded && text.trim().split(/\s+/).length >= 2) {
15403
+ if (this.o.mode === "live" && c?.running && !c.worded && newWords(text, c.text) >= 2) {
14630
15404
  log30.debug("user resumed before the reply \u2014 yielded");
14631
15405
  this.barge();
15406
+ this.gate.reopen(c.id);
14632
15407
  }
14633
15408
  this.follow(text);
14634
15409
  this.fill.onInterim(text);
@@ -15931,7 +16706,7 @@ init_logging();
15931
16706
  init_llm();
15932
16707
  init_platform();
15933
16708
  import { spawn as spawn2, spawnSync as spawnSync3 } from "child_process";
15934
- import { existsSync as existsSync4, mkdirSync as mkdirSync5, readFileSync as readFileSync6, statSync as statSync6 } from "fs";
16709
+ import { appendFileSync, existsSync as existsSync4, mkdirSync as mkdirSync5, readFileSync as readFileSync6, statSync as statSync6 } from "fs";
15935
16710
  import { homedir as homedir3 } from "os";
15936
16711
  import { dirname as dirname4, join as join8 } from "path";
15937
16712
  import { fileURLToPath } from "url";
@@ -16285,7 +17060,7 @@ var AecDuplexAudio = class {
16285
17060
  stdin.write(pcm16k);
16286
17061
  }
16287
17062
  };
16288
- var matchVoiceCommand = (text) => {
17063
+ var matchVoiceCommand2 = (text) => {
16289
17064
  const t = text.toLowerCase().replace(/[^a-z ]/g, " ").replace(/\s+/g, " ").trim().replace(/^(?:hey|ok|okay|please) /, "").replace(/ (?:please|now)$/, "");
16290
17065
  return /^(voice off|mute|stop listening)( \1)*$/.test(t) ? "off" : null;
16291
17066
  };
@@ -16589,7 +17364,8 @@ function fakeVoiceParts(uttFile) {
16589
17364
  offset = buf.length;
16590
17365
  for (const line of fresh.split("\n")) {
16591
17366
  const t = line.trim();
16592
- if (t) stt.onUtterance(t, Date.now());
17367
+ if (t.startsWith("~")) stt.onPartial(t.slice(1).trim());
17368
+ else if (t) stt.onUtterance(t, Date.now());
16593
17369
  }
16594
17370
  }, 100);
16595
17371
  },
@@ -16602,11 +17378,13 @@ function fakeVoiceParts(uttFile) {
16602
17378
  }
16603
17379
  }
16604
17380
  };
17381
+ const said = process.env.AGENTX_VOICE_FAKE_LOG;
16605
17382
  const tts = { connect() {
16606
17383
  }, newContext() {
16607
- }, speak() {
17384
+ }, speak(text) {
17385
+ if (said) appendFileSync(said, JSON.stringify({ t: Date.now(), speak: text }) + "\n");
16608
17386
  }, end() {
16609
- tts.onDone?.();
17387
+ setTimeout(() => tts.onDone?.(), 20);
16610
17388
  }, cancel() {
16611
17389
  }, close() {
16612
17390
  }, onAudio: (_) => {
@@ -16676,6 +17454,38 @@ var JsonlDiagSink = class {
16676
17454
  }
16677
17455
  };
16678
17456
 
17457
+ // src/voice/greeting.ts
17458
+ init_logging();
17459
+ var log37 = forComponent("CallGreeting");
17460
+ var CallGreetingOptions = class {
17461
+ model = "groq/openai/gpt-oss-120b";
17462
+ persona = "";
17463
+ budgetMs = 900;
17464
+ fallback = "Hey, what's up?";
17465
+ };
17466
+ async function callGreeting(ai, options) {
17467
+ const o = { ...new CallGreetingOptions(), ...options }, ac = new AbortController();
17468
+ const prompt = `${o.persona ? `Your persona:
17469
+ ${o.persona}
17470
+
17471
+ ` : ""}You open a live voice call. Write ONE short spoken sentence that greets the caller and asks what they would like to do. Keep the persona's tone tags if it uses them. Output only the line.`;
17472
+ const reasoning = /gpt-oss/.test(o.model) ? { providerOptions: { reasoning_effort: "low" } } : {};
17473
+ let timer;
17474
+ const late = new Promise((r) => {
17475
+ timer = setTimeout(() => {
17476
+ ac.abort();
17477
+ r("");
17478
+ }, o.budgetMs);
17479
+ });
17480
+ const asked = Promise.resolve(ai.chat({ model: o.model, messages: [{ role: "user", content: prompt }], stream: false, signal: ac.signal, maxTokens: 200, ...reasoning })).then((r) => (r.content ?? "").trim().split("\n")[0].replace(/^["']|["']$/g, "")).catch((e) => {
17481
+ log37.warn(`greeting failed \u2014 canned line: ${e?.message ?? e}`);
17482
+ return "";
17483
+ });
17484
+ const line = await Promise.race([asked, late]);
17485
+ clearTimeout(timer);
17486
+ return line || o.fallback;
17487
+ }
17488
+
16679
17489
  // cli/config.ts
16680
17490
  import { homedir as homedir5 } from "os";
16681
17491
  import { existsSync as existsSync5, readFileSync as readFileSync7 } from "fs";
@@ -16823,7 +17633,7 @@ function applyOverlay(cfg, file) {
16823
17633
  // cli/hooks-config.ts
16824
17634
  import { spawnSync as spawnSync4 } from "child_process";
16825
17635
  init_platform();
16826
- var log37 = forComponent("hooks");
17636
+ var log38 = forComponent("hooks");
16827
17637
  var escapeRegex = (s) => s.replace(/[.+^${}()|[\]\\]/g, "\\$&");
16828
17638
  function ruleMatches(rule, toolName) {
16829
17639
  if (!rule.tool || rule.tool === "*") return true;
@@ -16842,7 +17652,7 @@ function runCmd(rule, env) {
16842
17652
  });
16843
17653
  return { code: r.status ?? 1, out: ((r.stdout ?? "") + (r.stderr ?? "")).trim() };
16844
17654
  } catch (e) {
16845
- log37.debug(`hook command failed: ${rule.command}`, e);
17655
+ log38.debug(`hook command failed: ${rule.command}`, e);
16846
17656
  return { code: 1, out: String(e?.message ?? e) };
16847
17657
  }
16848
17658
  }
@@ -16950,7 +17760,7 @@ import { existsSync as existsSync6, mkdirSync as mkdirSync8, readFileSync as rea
16950
17760
  import { homedir as homedir6 } from "os";
16951
17761
  import { join as join11 } from "path";
16952
17762
  init_platform();
16953
- var log38 = forComponent("session");
17763
+ var log39 = forComponent("session");
16954
17764
  var globalDir = () => join11(homedir6(), ".agent", "sessions");
16955
17765
  function indexLink(target, link2) {
16956
17766
  try {
@@ -17013,7 +17823,7 @@ var SessionStore = class {
17013
17823
  }
17014
17824
  load(id) {
17015
17825
  if (!this.safeId(id)) {
17016
- log38.debug(`rejecting unsafe session id: ${id}`);
17826
+ log39.debug(`rejecting unsafe session id: ${id}`);
17017
17827
  return void 0;
17018
17828
  }
17019
17829
  const path = join11(this.dir, `${id}.json`);
@@ -17021,7 +17831,7 @@ var SessionStore = class {
17021
17831
  try {
17022
17832
  return JSON.parse(readFileSync8(path, "utf8"));
17023
17833
  } catch (e) {
17024
- log38.debug(`unreadable session ${id} \u2014 ignoring`, e);
17834
+ log39.debug(`unreadable session ${id} \u2014 ignoring`, e);
17025
17835
  return void 0;
17026
17836
  }
17027
17837
  }
@@ -17034,7 +17844,7 @@ var SessionStore = class {
17034
17844
  try {
17035
17845
  metas.push(JSON.parse(readFileSync8(join11(this.dir, f), "utf8")).meta);
17036
17846
  } catch (e) {
17037
- log38.debug(`skipping unreadable session file ${f}`, e);
17847
+ log39.debug(`skipping unreadable session file ${f}`, e);
17038
17848
  }
17039
17849
  }
17040
17850
  return metas.sort((a, b) => b.updated - a.updated);
@@ -17466,7 +18276,7 @@ import { promisify } from "util";
17466
18276
  import { writeFileSync as writeFileSync6, mkdirSync as mkdirSync9 } from "fs";
17467
18277
  import { homedir as homedir7 } from "os";
17468
18278
  import { join as join12, resolve as resolve4, sep as sep3, parse as parsePath } from "path";
17469
- var log39 = forComponent("checkpoints");
18279
+ var log40 = forComponent("checkpoints");
17470
18280
  var exec = promisify(execFile4);
17471
18281
  var DEFAULT_EXCLUDE = [".agent/", ".git/", "node_modules/", "dist/", "build/", ".next/", "target/", ".venv/", "__pycache__/", "*.log"];
17472
18282
  function isBroadRoot(p) {
@@ -17514,7 +18324,7 @@ var ShadowRepo = class {
17514
18324
  writeFileSync6(join12(this.gitDir, "info", "exclude"), this.exclude.join("\n") + "\n");
17515
18325
  this.ready = true;
17516
18326
  } catch (e) {
17517
- log39.debug(`git checkpoints unavailable for ${this.workTree}`, e);
18327
+ log40.debug(`git checkpoints unavailable for ${this.workTree}`, e);
17518
18328
  this.ready = false;
17519
18329
  }
17520
18330
  return this.ready;
@@ -17525,7 +18335,7 @@ var ShadowRepo = class {
17525
18335
  }
17526
18336
  async commit(label, forced = []) {
17527
18337
  await this.run("add", "-A");
17528
- for (const p of forced) await this.run("add", "-f", "--", p).catch((e) => log39.debug(`force-add failed: ${p}`, e));
18338
+ for (const p of forced) await this.run("add", "-f", "--", p).catch((e) => log40.debug(`force-add failed: ${p}`, e));
17529
18339
  await this.run("commit", "--allow-empty", "-q", "-m", label);
17530
18340
  }
17531
18341
  /** Inject the CURRENT (pre-edit) content of `paths` into the turn-open restore point by amending it.
@@ -17533,8 +18343,8 @@ var ShadowRepo = class {
17533
18343
  * turn-boundary `add -A` would never have captured it. Amend (vs a new commit) keeps one restore
17534
18344
  * point per turn, so the REPL's turn↔frame mapping stays intact. */
17535
18345
  async amendForced(paths) {
17536
- for (const p of paths) await this.run("add", "-f", "--", p).catch((e) => log39.debug(`force-capture failed: ${p}`, e));
17537
- await this.run("commit", "--amend", "--no-edit", "-q", "--allow-empty").catch((e) => log39.debug("amend failed", e));
18346
+ for (const p of paths) await this.run("add", "-f", "--", p).catch((e) => log40.debug(`force-capture failed: ${p}`, e));
18347
+ await this.run("commit", "--amend", "--no-edit", "-q", "--allow-empty").catch((e) => log40.debug("amend failed", e));
17538
18348
  }
17539
18349
  /** Commits on `ref`, oldest-first (canonical index space). */
17540
18350
  async log(ref) {
@@ -17594,7 +18404,7 @@ var ShadowRepo = class {
17594
18404
  await this.run("gc", "--auto", "-q").catch(() => {
17595
18405
  });
17596
18406
  } catch (e) {
17597
- log39.debug("checkpoint prune failed", e);
18407
+ log40.debug("checkpoint prune failed", e);
17598
18408
  }
17599
18409
  }
17600
18410
  };
@@ -17662,7 +18472,7 @@ var GitCheckpoints = class {
17662
18472
  use(sessionId) {
17663
18473
  if (sessionId === this.session) return;
17664
18474
  this.session = sessionId;
17665
- if (this.started) for (const r of this.repos) void r.point(this.ref()).catch((e) => log39.debug("re-point failed", e));
18475
+ if (this.started) for (const r of this.repos) void r.point(this.ref()).catch((e) => log40.debug("re-point failed", e));
17666
18476
  }
17667
18477
  async begin(label) {
17668
18478
  if (!await this.start()) return;
@@ -17682,7 +18492,7 @@ var GitCheckpoints = class {
17682
18492
  process.stderr.write(`\x1B[2m checkpoints: '${this.repos[i].root}' too large to snapshot in ${Math.round(this.options.opTimeoutMs / 1e3)}s \u2014 file rewind disabled here\x1B[0m
17683
18493
  `);
17684
18494
  } else {
17685
- log39.debug("checkpoint commit failed", e);
18495
+ log40.debug("checkpoint commit failed", e);
17686
18496
  survivors.push(this.repos[i]);
17687
18497
  survivorCaches.push(this.caches[i] ?? []);
17688
18498
  }
@@ -17717,7 +18527,7 @@ var GitCheckpoints = class {
17717
18527
  if (this.forced.has(abs)) continue;
17718
18528
  this.forced.add(abs);
17719
18529
  const i = this.repoIndexFor(abs);
17720
- if (i >= 0) await this.repos[i].amendForced([abs]).catch((e) => log39.debug("amendForced failed", e));
18530
+ if (i >= 0) await this.repos[i].amendForced([abs]).catch((e) => log40.debug("amendForced failed", e));
17721
18531
  }
17722
18532
  }
17723
18533
  };
@@ -19507,7 +20317,7 @@ import { spawnSync as spawnSync6 } from "child_process";
19507
20317
  import { writeFileSync as writeFileSync8, mkdirSync as mkdirSync10, readdirSync as readdirSync2, unlinkSync as unlinkSync3, chmodSync, existsSync as existsSync7 } from "fs";
19508
20318
  import { homedir as homedir8 } from "os";
19509
20319
  import { join as join14 } from "path";
19510
- var log40 = forComponent("os-sched");
20320
+ var log41 = forComponent("os-sched");
19511
20321
  var OsScheduler = class {
19512
20322
  options;
19513
20323
  constructor(options) {
@@ -19569,7 +20379,7 @@ var OsScheduler = class {
19569
20379
  }
19570
20380
  }
19571
20381
  } catch (e) {
19572
- log40.debug(`cancel ${id}`, e);
20382
+ log41.debug(`cancel ${id}`, e);
19573
20383
  }
19574
20384
  for (const f of [`${id}.json`, `${id}.sh`]) {
19575
20385
  try {
@@ -19687,7 +20497,7 @@ import { existsSync as existsSync8, mkdirSync as mkdirSync11, unlinkSync as unli
19687
20497
  import { homedir as homedir9 } from "os";
19688
20498
  import { join as join15 } from "path";
19689
20499
  init_platform();
19690
- var log41 = forComponent("remote-trigger");
20500
+ var log42 = forComponent("remote-trigger");
19691
20501
  var TRIGGER_DIR = () => join15(homedir9(), ".agent", "triggers");
19692
20502
  var sockPath = (sessionId, dir = TRIGGER_DIR()) => join15(dir, `${sessionId}.sock`);
19693
20503
  var TriggerServer = class {
@@ -19725,13 +20535,13 @@ var TriggerServer = class {
19725
20535
  conn.end(JSON.stringify({ ok: false, error: String(e) }) + "\n");
19726
20536
  }
19727
20537
  });
19728
- conn.on("error", (e) => log41.debug("trigger conn error", e));
20538
+ conn.on("error", (e) => log42.debug("trigger conn error", e));
19729
20539
  });
19730
- this.server.on("error", (e) => log41.debug("trigger server error", e));
20540
+ this.server.on("error", (e) => log42.debug("trigger server error", e));
19731
20541
  this.server.listen(p);
19732
20542
  this.path = p;
19733
20543
  } catch (e) {
19734
- log41.debug("trigger server unavailable", e);
20544
+ log42.debug("trigger server unavailable", e);
19735
20545
  }
19736
20546
  }
19737
20547
  /** Re-bind on /resume (the session id changed). */
@@ -19863,7 +20673,7 @@ var strike = C("9");
19863
20673
  var link = (text, url) => useColor ? `\x1B]8;;${url}\x1B\\${cyan(text)}\x1B]8;;\x1B\\` : `${text} (${url})`;
19864
20674
  var mdPalette = { bold, dim, cyan, italic, strike, green, link };
19865
20675
  var err = (s) => process.stderr.write(s);
19866
- var log42 = forComponent("cli");
20676
+ var log43 = forComponent("cli");
19867
20677
  var VERSION = (() => {
19868
20678
  try {
19869
20679
  return JSON.parse(readFileSync10(new URL("../package.json", import.meta.url), "utf8")).version ?? "?";
@@ -20016,6 +20826,7 @@ function parseArgs(argv) {
20016
20826
  } else if (x === "--call") {
20017
20827
  a.call = true;
20018
20828
  a.duplex = true;
20829
+ a.lean = true;
20019
20830
  } else if (x === "--lean") a.lean = true;
20020
20831
  else if (x === "--keep-alive") a.keepAlive = true;
20021
20832
  else if (x === "--channel-only") a.channelOnly = true;
@@ -20765,7 +21576,7 @@ function turnCost(model, usage) {
20765
21576
  const priced = model.startsWith("claude-code/") ? `anthropic/${resolveModelAlias(model.slice("claude-code/".length))}` : model;
20766
21577
  return costOf(getModelInfo(priced)?.pricing, usage?.promptTokens ?? 0, usage?.completionTokens ?? 0, usage?.cacheCreationTokens ?? 0, usage?.cacheReadTokens ?? 0, priced);
20767
21578
  }
20768
- async function evaluateGoal(ai, condition, transcript, log43) {
21579
+ async function evaluateGoal(ai, condition, transcript, log44) {
20769
21580
  const recent = transcript.filter((m) => m.role === "assistant").slice(-8).map((m) => {
20770
21581
  const text = typeof m.content === "string" ? m.content : m.content.filter((p) => p.type === "text").map((p) => p.text).join(" ");
20771
21582
  return text.slice(0, 600);
@@ -20785,7 +21596,7 @@ ${recent}` }
20785
21596
  const match = r.content.match(/\{[\s\S]*\}/);
20786
21597
  if (match) return JSON.parse(match[0]);
20787
21598
  } catch (e) {
20788
- log43(dim(` (goal evaluator error: ${e?.message ?? e})
21599
+ log44(dim(` (goal evaluator error: ${e?.message ?? e})
20789
21600
  `));
20790
21601
  }
20791
21602
  return { met: false, reason: "evaluation unclear" };
@@ -21013,7 +21824,7 @@ function mcpAgentTools(mounted, opts) {
21013
21824
  return tools;
21014
21825
  }
21015
21826
  async function closeMcp(mounted) {
21016
- await Promise.all(mounted.map((m) => m.client.close().catch((e) => log42.debug("mcp close failed", e))));
21827
+ await Promise.all(mounted.map((m) => m.client.close().catch((e) => log43.debug("mcp close failed", e))));
21017
21828
  }
21018
21829
  var IMG_EXT = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp" };
21019
21830
  function mentionRefs(line) {
@@ -21072,7 +21883,7 @@ async function expandMentions(fs, line) {
21072
21883
  if (loaded.includes(ref) || missing.includes(ref)) continue;
21073
21884
  if (ref.includes(":") && mcpMentionResolver) {
21074
21885
  const body = await mcpMentionResolver(ref).catch((e) => {
21075
- log42.debug("mcp mention resolve failed", e);
21886
+ log43.debug("mcp mention resolve failed", e);
21076
21887
  return null;
21077
21888
  });
21078
21889
  if (body != null) {
@@ -21098,7 +21909,7 @@ ${await fs.readFile(path)}`);
21098
21909
  ${blocks.join("\n\n")}` : line, loaded, missing };
21099
21910
  }
21100
21911
  function applyTurnMode(agent, session, task, flags = {}, onModel) {
21101
- const p = parseDirectives(task, { strict: true, keys: ["model", "turns", "delegate", "advisor", "mode"], warn: (m) => log42.debug(m) });
21912
+ const p = parseDirectives(task, { strict: true, keys: ["model", "turns", "delegate", "advisor", "mode"], warn: (m) => log43.debug(m) });
21102
21913
  const d = p.directives, isMode = !!(d.delegate || d.advisor || d.modeOff);
21103
21914
  if (p.unresolvedModel && /->|<-|delegate:|advisor:/.test(task.slice(0, task.length - p.prompt.length))) {
21104
21915
  err(red(` \u2717 unknown model "${p.unresolvedModel}"${p.ambiguous ? ` (could be: ${p.ambiguous.join(", ")})` : ""} \u2014 turn not sent
@@ -21191,7 +22002,7 @@ async function runTurn(agent, store, session, task, cp, cwd = process.cwd(), sen
21191
22002
  try {
21192
22003
  store.save(session);
21193
22004
  } catch (ex) {
21194
- log42.debug("mid-turn session flush failed", ex);
22005
+ log43.debug("mid-turn session flush failed", ex);
21195
22006
  }
21196
22007
  }
21197
22008
  return origNotify(e);
@@ -21355,14 +22166,14 @@ var isCancelTeardown = (e) => {
21355
22166
  function installCancelGuards(mounted) {
21356
22167
  process.on("unhandledRejection", (e) => {
21357
22168
  if (isCancelTeardown(e)) {
21358
- log42.debug("suppressed unhandledRejection (cursor stream cancel)", e);
22169
+ log43.debug("suppressed unhandledRejection (cursor stream cancel)", e);
21359
22170
  return;
21360
22171
  }
21361
- log42.error("unhandledRejection", e);
22172
+ log43.error("unhandledRejection", e);
21362
22173
  });
21363
22174
  process.on("uncaughtException", (e) => {
21364
22175
  if (isCancelTeardown(e)) {
21365
- log42.debug("suppressed uncaughtException (cursor stream cancel)", e);
22176
+ log43.debug("suppressed uncaughtException (cursor stream cancel)", e);
21366
22177
  return;
21367
22178
  }
21368
22179
  console.error(e);
@@ -21687,16 +22498,20 @@ async function repl(args, ai, cfg, cwd) {
21687
22498
  const face = dx ? dx.voice : agent;
21688
22499
  let call;
21689
22500
  let callChain = Promise.resolve();
22501
+ let callFastModel = "";
22502
+ let callReplyOpen = false;
22503
+ let greetNote = "";
21690
22504
  const callTurns = /* @__PURE__ */ new Map();
21691
22505
  if (args.call) {
21692
22506
  if (args.voice || args.voicePersona) agent.options.systemPrompt += "\n\n" + (args.voicePersona ?? CALL_PROMPT);
21693
22507
  if (args.voiceSystemPrompt) agent.options.systemPrompt += "\n\n" + args.voiceSystemPrompt;
22508
+ const fast = callFast(args, ai, cfg);
22509
+ for (const n of fast.notes) err(yellow(` \u26A0 ${n}
22510
+ `));
22511
+ callFastModel = fast.options.fastModel;
21694
22512
  call = new VoiceCall({
21695
22513
  ai,
21696
- fastModel: args.voiceModel ?? process.env.AGENTX_CHANNEL_MODEL ?? new VoiceCallOptions().fastModel,
21697
- mode: args.channelMode,
21698
- channel: channelOptions(ai, args.voiceModel ?? process.env.AGENTX_CHANNEL_MODEL ?? new VoiceCallOptions().fastModel, args.voiceGender),
21699
- gender: args.voiceGender ?? "",
22514
+ ...fast.options,
21700
22515
  show: (t, kind) => {
21701
22516
  err("\r\x1B[0J" + dim(` \u21B3 ${kind === "progress" ? "\u22EF " : ""}${t}
21702
22517
  `));
@@ -21723,15 +22538,17 @@ async function repl(args, ai, cfg, cwd) {
21723
22538
  if (cut && keepHeard(face.transcript, cut)) voiceDiag?.push({ t: performance.now(), kind: "call_kept_heard", heard: cut.heard.length, full: cut.full.length });
21724
22539
  rec.running = true;
21725
22540
  rec.before = face.transcript.length;
21726
- voiceIO?.beginSpeech(true);
22541
+ const note = greetNote;
22542
+ greetNote = "";
22543
+ callReplyOpen = false;
21727
22544
  try {
21728
- const r = await dispatchLine(text, { id });
22545
+ const r = await dispatchLine(text + note, { id });
21729
22546
  if (r === "quit") {
21730
22547
  await voiceIO?.awaitIdle();
21731
22548
  editorRef?.abort();
21732
22549
  }
21733
22550
  } catch (e) {
21734
- log42.error("call turn failed", e);
22551
+ log43.error("call turn failed", e);
21735
22552
  } finally {
21736
22553
  rec.running = false;
21737
22554
  call?.main({ type: "done", id });
@@ -21748,8 +22565,13 @@ async function repl(args, ai, cfg, cwd) {
21748
22565
  if (e.kind === "text_delta" && voiceIO) {
21749
22566
  voiceReasonEnd();
21750
22567
  spinner.stop();
21751
- voiceEcho(voiceIO.speakDelta(e.message));
22568
+ if (!callReplyOpen) {
22569
+ callReplyOpen = true;
22570
+ voiceIO.beginSpeech();
22571
+ }
22572
+ const echo = voiceIO.speakDelta(e.message);
21752
22573
  editorRef?.suspend();
22574
+ voiceEcho(echo);
21753
22575
  return;
21754
22576
  }
21755
22577
  if (voiceIO && (e.kind === "turn_start" || e.kind === "thinking_delta")) return;
@@ -21863,7 +22685,7 @@ async function repl(args, ai, cfg, cwd) {
21863
22685
  try {
21864
22686
  mkdirSync12(dir, { recursive: true });
21865
22687
  } catch (e) {
21866
- log42.debug("paste image dir create failed", e);
22688
+ log43.debug("paste image dir create failed", e);
21867
22689
  }
21868
22690
  const img = grabClipboardImage(dir, String(Date.now()));
21869
22691
  return img ? { display: "Image", ref: "@" + img.path, path: img.path } : null;
@@ -21974,9 +22796,9 @@ Added entries are loadable now via the Skill/SlashCommand tools; removed ones ar
21974
22796
  const remember2 = (line) => {
21975
22797
  try {
21976
22798
  mkdirSync12(join16(cwd, ".agent"), { recursive: true });
21977
- appendFileSync(histPath, line + "\n");
22799
+ appendFileSync2(histPath, line + "\n");
21978
22800
  } catch (e) {
21979
- log42.debug("history write failed", e);
22801
+ log43.debug("history write failed", e);
21980
22802
  }
21981
22803
  };
21982
22804
  const ago = (t) => {
@@ -22048,7 +22870,7 @@ Added entries are loadable now via the Skill/SlashCommand tools; removed ones ar
22048
22870
  try {
22049
22871
  store.save(session);
22050
22872
  } catch (e) {
22051
- log42.debug("session save after rewind failed", e);
22873
+ log43.debug("session save after rewind failed", e);
22052
22874
  }
22053
22875
  err(green(" \u27F2 jumped back") + dim(` \u2014 ${face.transcript.length} message(s) kept; edit + resend
22054
22876
  `));
@@ -22083,7 +22905,7 @@ ${task}`;
22083
22905
  bangContext.length = 0;
22084
22906
  }
22085
22907
  const delta = await refreshCatalogs().catch((e) => {
22086
- log42.debug("catalog refresh failed", e);
22908
+ log43.debug("catalog refresh failed", e);
22087
22909
  return "";
22088
22910
  });
22089
22911
  if (delta) {
@@ -22353,7 +23175,7 @@ ${task}`;
22353
23175
  desc: "rescan skills/commands dirs and rebuild the system prompt (one cache miss) \u2014 picks up entries created mid-session",
22354
23176
  run: async () => {
22355
23177
  await refreshCatalogs().catch((e) => {
22356
- log42.debug("catalog refresh failed", e);
23178
+ log43.debug("catalog refresh failed", e);
22357
23179
  });
22358
23180
  face.reprepare();
22359
23181
  err(green(` \u2713 reloaded \u2014 ${skills.length} skill(s), ${cmds.length} command(s); system prompt rebuilds on next message
@@ -22856,7 +23678,7 @@ ${task}`;
22856
23678
  try {
22857
23679
  for (const def of (await loadAgents(fs2, d)).agents) if (!seen.has(def.name)) seen.set(def.name, { def, from: d });
22858
23680
  } catch (e) {
22859
- log42.debug(`loadAgents(${d}) failed`, e);
23681
+ log43.debug(`loadAgents(${d}) failed`, e);
22860
23682
  }
22861
23683
  }
22862
23684
  if (!seen.size) {
@@ -22944,7 +23766,7 @@ ${task}`;
22944
23766
  }
22945
23767
  if (idx >= 0) {
22946
23768
  const old = mounted.splice(idx, 1)[0];
22947
- await old.client.close().catch((e) => log42.debug("mcp close failed", e));
23769
+ await old.client.close().catch((e) => log43.debug("mcp close failed", e));
22948
23770
  }
22949
23771
  try {
22950
23772
  const m = await mountMcpServer(name, conf);
@@ -22972,7 +23794,7 @@ ${task}`;
22972
23794
  }
22973
23795
  const m = mounted.splice(idx, 1)[0];
22974
23796
  remountMcpTools();
22975
- await m.client.close().catch((e) => log42.debug("mcp close failed", e));
23797
+ await m.client.close().catch((e) => log43.debug("mcp close failed", e));
22976
23798
  err(dim(` removed "${name}"
22977
23799
  `));
22978
23800
  return;
@@ -23175,7 +23997,7 @@ ${task}`;
23175
23997
  try {
23176
23998
  return readdirSync4(join16(cwd, absDir.replace(/^\/+/, "")), { withFileTypes: true }).map((d) => ({ name: d.name, dir: d.isDirectory() }));
23177
23999
  } catch (e) {
23178
- log42.debug("completion readdir failed", absDir, e);
24000
+ log43.debug("completion readdir failed", absDir, e);
23179
24001
  return null;
23180
24002
  }
23181
24003
  };
@@ -23314,7 +24136,7 @@ ${out}
23314
24136
  return;
23315
24137
  }
23316
24138
  await refreshCatalogs().catch((e) => {
23317
- log42.debug("catalog refresh failed", e);
24139
+ log43.debug("catalog refresh failed", e);
23318
24140
  });
23319
24141
  const sk = skills.find((x) => x.name === name);
23320
24142
  if (sk) {
@@ -23353,7 +24175,7 @@ ${out}
23353
24175
  if (voiceIO) return true;
23354
24176
  if (greet && call) {
23355
24177
  bindClaudeCodeSession(agent, session);
23356
- await agent.prewarm().catch((e) => err(dim(` (prewarm failed: ${e?.message ?? e})
24178
+ callChain = agent.prewarm().catch((e) => err(dim(` (prewarm failed: ${e?.message ?? e})
23357
24179
  `)));
23358
24180
  }
23359
24181
  if (!(duplex || call) || !process.stdin.isTTY) {
@@ -23374,13 +24196,13 @@ ${out}
23374
24196
  liveSttTerms = sttTerms;
23375
24197
  voiceIO = new VoiceIO({
23376
24198
  ...fakeVoice ?? {},
23377
- ...call ? { adaptiveAckMs: 0 } : {},
23378
- // v3: the call's fill channel covers the wait — no engine ack on top
24199
+ // v3, as Para's NativeVoice: the fill channel covers the wait (no engine ack), TurnGate merges (no engine merge), the first clause speaks at once
24200
+ ...call ? { adaptiveAckMs: 0, adaptiveEndpointing: true, utteranceMergeMs: 0, flushFirstClause: true } : {},
23379
24201
  sttTerms,
23380
24202
  onDiag: (ev) => voiceDiag?.push(ev),
23381
24203
  emotions: emotionsOn,
23382
- showEmotions,
23383
- // local is authoritative (a /voice-emotions before mic-on still applies)
24204
+ showEmotions: !call && showEmotions,
24205
+ // local is authoritative (a /voice-emotions before mic-on still applies); a call's tags are speech, never text
23384
24206
  // No FIXED ack phrase by default: a fixed "Mm-hm," every turn reads robotic, and the
23385
24207
  // conversational register already opens with a natural reaction. Instead the ADAPTIVE ack
23386
24208
  // (VoiceIOOptions.adaptiveAckMs, default 600ms — a safety net, not the voice) speaks a short varied ack only when the reflex
@@ -23423,7 +24245,7 @@ ${out}
23423
24245
  onUtterance: (text, meta) => {
23424
24246
  voicePartial = "";
23425
24247
  if (!text.trim()) return;
23426
- if (matchVoiceCommand(text) === "off") {
24248
+ if (matchVoiceCommand2(text) === "off") {
23427
24249
  dx?.abortSpeculation();
23428
24250
  err(`\r\x1B[K ${bold(cyan("\u{1F3A4} \u203A"))} ${text}
23429
24251
  `);
@@ -23436,7 +24258,7 @@ ${out}
23436
24258
  return;
23437
24259
  }
23438
24260
  if (call) {
23439
- call.final(text);
24261
+ call.final(text, void 0, { ...meta?.lastWordAgoMs !== void 0 ? { lastWordAgoMs: meta.lastWordAgoMs } : {}, ...meta?.language ? { lang: meta.language } : {} });
23440
24262
  return;
23441
24263
  }
23442
24264
  const replaces = replacedVoiceTurn(lastVoiceTurn, meta);
@@ -23444,7 +24266,7 @@ ${out}
23444
24266
  if (delivery || cut) dx?.parkCutDelivery(delivery, !!cut, heard);
23445
24267
  const cutReply = cut && cut.full.length - cut.heard.length > 40 ? cut : null;
23446
24268
  const noteP = replaces ? replaces.note : !cutReply ? Promise.resolve("") : (instinct ? instinct.bargeIntent(text).catch((e) => {
23447
- log42.warn(`barge intent failed \u2014 model decides: ${e.message}`);
24269
+ log43.warn(`barge intent failed \u2014 model decides: ${e.message}`);
23448
24270
  return void 0;
23449
24271
  }) : Promise.resolve(void 0)).then(async (intent) => interruptionNote(cutReply, intent) + ackedNote(await instinct?.ackFor(text)));
23450
24272
  const cutIn = replaces ? replaces.cutIn : interjectNote;
@@ -23567,10 +24389,20 @@ ${out}
23567
24389
  }
23568
24390
  if (inDev?.virtual) err(yellow(` \u26A0 default input "${inDev.name}" looks like a virtual device \u2014 if it can't hear you, switch your Mac input to a real mic
23569
24391
  `));
23570
- if (greet) {
24392
+ if (greet && call && !session.messages.length) {
24393
+ const v = voiceIO, line = await callGreeting(ai, { model: callFastModel, persona: args.voicePersona ?? "" });
24394
+ const said = renderEmotions(line, { show: false }).prose.trim();
24395
+ v.speakFiller(line);
24396
+ err(`\r\x1B[K ${said}
24397
+ `);
24398
+ editorRef?.redrawNow();
24399
+ greetNote = `
24400
+
24401
+ <call_greeting>You opened this call with: "${said}". The user's words answer it.</call_greeting>`;
24402
+ } else if (greet) {
23571
24403
  const where = cwd.split("/").pop();
23572
24404
  const resumed = session.messages.length > 0;
23573
- const prompt = resumed ? `[session resumed] You're rejoining a live voice conversation already in progress \u2014 the full history, including the user's memory/preferences, is already in context, so do NOT call QuickLook again. In one short spoken line, welcome the user back and pick the thread up where it left off: if your last turn ended on an open question or offer, continue it naturally rather than asking what they'd like to do.` : call ? `[session started] Do not call any tool: answer in this one reply, from what you already know. Greet the user warmly in one short spoken sentence, as the opener of a live voice call, and ask what they'd like to do.` : `[session started] First call QuickLook with what:"memory" \u2014 if it knows the user's name or preferences, use them. Then greet the user warmly in one or two short sentences, as the opener of a live voice conversation. Context: working directory "${where}". Personalize from whatever you learned (memory). Then ask what they'd like to do.`;
24405
+ const prompt = resumed ? `[session resumed] You're rejoining a live voice conversation already in progress \u2014 the full history, including the user's memory/preferences, is already in context, so do NOT call QuickLook again. In one short spoken line, welcome the user back and pick the thread up where it left off: if your last turn ended on an open question or offer, continue it naturally rather than asking what they'd like to do.` : `[session started] First call QuickLook with what:"memory" \u2014 if it knows the user's name or preferences, use them. Then greet the user warmly in one or two short sentences, as the opener of a live voice conversation. Context: working directory "${where}". Personalize from whatever you learned (memory). Then ask what they'd like to do.`;
23574
24406
  void turn(prompt).finally(() => editorRef?.redrawNow());
23575
24407
  }
23576
24408
  return true;
@@ -23834,10 +24666,10 @@ function channelOptions(ai, model, gender) {
23834
24666
  // 0: the reactive live path
23835
24667
  pickLeaf: fastLeafPicker(ai, process.env.AGENTX_LEAF_MODEL || "groq/openai/gpt-oss-120b"),
23836
24668
  persona: process.env.AGENTX_CALL_PERSONA ?? "",
23837
- onTree: (e) => log42.debug(`voice tree: ${e.branches} branches in ${e.ms}ms${e.tokens ? ` tokens ${e.tokens.input}/${e.tokens.output}` : ""}${e.error ? ` error ${e.error}` : ""}`),
24669
+ onTree: (e) => log43.debug(`voice tree: ${e.branches} branches in ${e.ms}ms${e.tokens ? ` tokens ${e.tokens.input}/${e.tokens.output}` : ""}${e.error ? ` error ${e.error}` : ""}`),
23838
24670
  onEvent: (e) => {
23839
- if (e.moment) log42.debug(`voice leaf: ${e.moment} ${e.conf} "${e.text}" ${e.acted}`);
23840
- else log42.debug(`channel ${e.trigger}: rule=${e.rule} choice=${e.choice ?? "-"} \u2192 ${e.acted}${e.text ? ` "${e.text}"` : ""}${e.cue ? " (planned cue)" : ""} ${e.ms}ms`);
24671
+ if (e.moment) log43.debug(`voice leaf: ${e.moment} ${e.conf} "${e.text}" ${e.acted}`);
24672
+ else log43.debug(`channel ${e.trigger}: rule=${e.rule} choice=${e.choice ?? "-"} \u2192 ${e.acted}${e.text ? ` "${e.text}"` : ""}${e.cue ? " (planned cue)" : ""} ${e.ms}ms`);
23841
24673
  },
23842
24674
  // Banks in the user's language: generated once per language, cached + topped up across sessions (~/.agent/cache).
23843
24675
  banks: new ChannelBanks({ generate: fastBankGenerator(ai, model), cacheDir: join16(homedir10(), ".agent", "cache", "channel-banks") }),
@@ -23848,6 +24680,17 @@ function channelOptions(ai, model, gender) {
23848
24680
  ...process.env.AGENTX_CHANNEL_ANTICIPATE === "1" ? { anticipate: true } : {}
23849
24681
  };
23850
24682
  }
24683
+ function callFast(args, ai, cfg) {
24684
+ const { reflexModel, notes } = duplexModels({ voiceModel: args.voiceModel, reflexModel: cfg.reflexModel, thinkModel: false, actModel: String(args.model ?? cfg.model ?? ""), apiKeys: { ...cfg.apiKeys, ...apiKeysFromEnv() } });
24685
+ const model = reflexModel ?? new DuplexAgentOptions().reflexModel, line = process.env.AGENTX_CHANNEL_MODEL || model;
24686
+ return { model, notes, options: {
24687
+ fastModel: line,
24688
+ mode: args.channelMode,
24689
+ gender: args.voiceGender ?? "",
24690
+ progress: { generate: fastProgressGenerator(ai, line, args.voiceGender) },
24691
+ channel: channelOptions(ai, line, args.voiceGender)
24692
+ } };
24693
+ }
23851
24694
  function overlapResponder(ai, mode, out, aside) {
23852
24695
  const judge = softBarge(mode) ? new OverlapJudge({ model: overlapModelFor(ai) }) : void 0;
23853
24696
  return (m) => {
@@ -23919,7 +24762,7 @@ async function duplexServe(args, ai, cfg, cwd) {
23919
24762
  try {
23920
24763
  store.save(session);
23921
24764
  } catch (e) {
23922
- log42.warn("duplex session save failed", e);
24765
+ log43.warn("duplex session save failed", e);
23923
24766
  }
23924
24767
  };
23925
24768
  const overlap2 = overlapResponder(ai, args.duplexV2 ? args.channelMode : void 0, out);
@@ -23959,7 +24802,7 @@ async function duplexServe(args, ai, cfg, cwd) {
23959
24802
  done2();
23960
24803
  out({ type: "turn_done", id, finishReason: res?.finishReason ?? "error", ...why ? { error: why } : {} });
23961
24804
  }).catch((e) => {
23962
- log42.error("duplex turn failed", e);
24805
+ log43.error("duplex turn failed", e);
23963
24806
  done2();
23964
24807
  out({ type: "turn_done", id, finishReason: "error", error: String(e?.message ?? e) });
23965
24808
  });
@@ -23975,15 +24818,11 @@ async function duplexServe(args, ai, cfg, cwd) {
23975
24818
  }
23976
24819
  async function channelServe(args, ai, cfg) {
23977
24820
  const out = (o) => process.stdout.write(JSON.stringify(o) + "\n");
23978
- const { reflexModel, notes } = duplexModels({ voiceModel: args.voiceModel, reflexModel: cfg.reflexModel, thinkModel: false, actModel: String(args.model ?? cfg.model ?? ""), apiKeys: { ...cfg.apiKeys, ...apiKeysFromEnv() } });
23979
- const model = reflexModel ?? new DuplexAgentOptions().reflexModel;
24821
+ const { model, notes, options: fast } = callFast(args, ai, cfg);
23980
24822
  const call = new VoiceCall({
23981
- fastModel: model,
23982
- mode: args.channelMode,
23983
- gender: args.voiceGender ?? "",
23984
- progress: { generate: fastProgressGenerator(ai, process.env.AGENTX_CHANNEL_MODEL || model, args.voiceGender) },
24823
+ ...fast,
23985
24824
  // no `ai` here: the channel's model wiring is explicit
23986
- channel: { ...channelOptions(ai, process.env.AGENTX_CHANNEL_MODEL || model, args.voiceGender), onTree: (e) => out({ type: "metric", name: "tree", ms: e.ms, kind: `${e.tokens ? `${e.tokens.input}/${e.tokens.output} tok ` : ""}${e.error ?? (e.lines ?? []).join(" | ")}` }), onLeaf: (e) => out({ type: "metric", name: "leaf", ms: e.ms, kind: `${e.moment} ${e.conf} ${e.acted}`, text: e.text }) },
24825
+ channel: { ...fast.channel, onTree: (e) => out({ type: "metric", name: "tree", ms: e.ms, kind: `${e.tokens ? `${e.tokens.input}/${e.tokens.output} tok ` : ""}${e.error ?? (e.lines ?? []).join(" | ")}` }), onLeaf: (e) => out({ type: "metric", name: "leaf", ms: e.ms, kind: `${e.moment} ${e.conf} ${e.acted}`, text: e.text }) },
23987
24826
  speak: (line, kind) => {
23988
24827
  out(channelLine(line, kind));
23989
24828
  return true;