@livx.cc/agentx 0.99.146 → 0.99.148

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);
9424
10047
  }
9425
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;
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;
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) {
@@ -13212,9 +13981,6 @@ init_logging();
13212
13981
  init_logging();
13213
13982
  init_models();
13214
13983
 
13215
- // src/voice/progress.ts
13216
- init_logging();
13217
-
13218
13984
  // src/voice/convoTree.ts
13219
13985
  init_logging();
13220
13986
 
@@ -13474,10 +14240,13 @@ Answer only the number, or "none".`;
13474
14240
  }
13475
14241
 
13476
14242
  // src/voice/progress.ts
14243
+ init_logging();
13477
14244
  var log28 = forComponent("Progress");
13478
14245
  var HE_RESULT_CLAIM = /(?:^|\s)(?:מצאתי|סיימתי|בוצע|הנה|אין)(?=\s|$|[.,!…])/u;
14246
+ var PLUMBING = /toolsearch|^mcp__.*__(list_commands|call_info)$/;
14247
+ var isPlumbing = (tool) => PLUMBING.test(tool.toLowerCase());
13479
14248
  var TOPICS = [
13480
- [/toolsearch|^mcp__.*__(list_commands|call_info)$/, null],
14249
+ [PLUMBING, null],
13481
14250
  // tool plumbing — no line (and no generated one)
13482
14251
  [/kanban|board|card|\bkb\b|list_cards/, "board"],
13483
14252
  [/mail|gmail|inbox/, "mail"],
@@ -13577,6 +14346,7 @@ var Progress = class {
13577
14346
  said = /* @__PURE__ */ new Set();
13578
14347
  lastAt = -Infinity;
13579
14348
  streamed = false;
14349
+ lines = [];
13580
14350
  get o() {
13581
14351
  return this.options;
13582
14352
  }
@@ -13584,20 +14354,33 @@ var Progress = class {
13584
14354
  turn() {
13585
14355
  this.streamed = false;
13586
14356
  }
13587
- /** MAIN's text streamed this turn: no more progress lines for it. */
14357
+ /** MAIN's text streamed this turn: no more progress lines, until a tool runs again. */
13588
14358
  text() {
13589
14359
  this.streamed = true;
13590
14360
  }
13591
- /** May a line play now? (the caller passes when the user last heard anything) */
13592
- due(lastAudibleAt) {
14361
+ /** A tool started: MAIN is working again. */
14362
+ tool() {
14363
+ this.streamed = false;
14364
+ }
14365
+ /** May a line play now, or `leadMs` from now? (the caller passes when the user last heard anything) */
14366
+ due(lastAudibleAt, leadMs = 0) {
13593
14367
  const now4 = this.o.now();
13594
- return !this.streamed && now4 - lastAudibleAt >= this.o.silenceMs && now4 - this.lastAt >= this.o.gapMs;
14368
+ return !this.streamed && now4 - lastAudibleAt >= this.o.silenceMs - leadMs && now4 - this.lastAt >= this.o.gapMs - leadMs;
13595
14369
  }
13596
14370
  /** Mark `line` spoken. */
13597
14371
  spoke(line) {
13598
- this.said.add(norm3(line));
14372
+ this.note(line);
13599
14373
  this.lastAt = this.o.now();
13600
14374
  }
14375
+ /** A line the user heard from elsewhere (an aside): progress never repeats it. */
14376
+ note(line) {
14377
+ this.said.add(norm3(line));
14378
+ this.lines = [...this.lines, line].slice(-6);
14379
+ }
14380
+ /** The last lines heard, for a writer to avoid. */
14381
+ get recent() {
14382
+ return [...this.lines];
14383
+ }
13601
14384
  /** `about`: the user's request, for a generated line. The line for this tool call in `lang`, or '' (no template and no/late/invalid generation, a repeat, a result claim). */
13602
14385
  async line(tool, args, lang, about = "") {
13603
14386
  if (this.streamed) return "";
@@ -13618,7 +14401,7 @@ var Progress = class {
13618
14401
  r(null);
13619
14402
  }, this.o.generateBudgetMs);
13620
14403
  });
13621
- const raw = await Promise.race([this.o.generate(tool, [about.slice(-80), argsHint(args)].filter(Boolean).join("; "), lang, ac.signal).catch((e) => {
14404
+ const raw = await Promise.race([this.o.generate(tool, [about.slice(-80), argsHint(args)].filter(Boolean).join("; "), lang, ac.signal, this.lines).catch((e) => {
13622
14405
  log28.warn(`progress line failed: ${e?.message ?? e}`);
13623
14406
  return null;
13624
14407
  }), late]).finally(() => clearTimeout(t));
@@ -13628,8 +14411,24 @@ var Progress = class {
13628
14411
  }
13629
14412
  };
13630
14413
  function fastProgressGenerator(ai, model, gender) {
13631
- return async (tool, hint, lang, signal) => {
13632
- const prompt = `An assistant on a live voice call is running the tool "${tool}"${hint ? ` (about: "${hint}")` : ""}. Write ONE spoken line of 2-5 words, in ${languageName(lang)}, saying what it is doing right now (like "checking the board\u2026"). ${hint ? "Use the words of what it is about. " : ""}Never state a result or a finding. ${personaHint(gender)} Output only the line.`;
14414
+ return async (tool, hint, lang, signal, avoid = []) => {
14415
+ const prompt = `An assistant on a live voice call is running the tool "${tool}"${hint ? ` (about: "${hint}")` : ""}. Write ONE spoken line of 2-5 words, in ${languageName(lang)}, saying what it is doing right now (like "checking the board\u2026"). ${hint ? "Use the words of what it is about. " : ""}${avoid.length ? `It already said ${avoid.map((a) => `"${a}"`).join(", ")}: say something new, a different angle on the same work. ` : ""}Never state a result or a finding. ${personaHint(gender)} Output only the line.`;
14416
+ const reasoning = /gpt-oss/.test(model) ? { providerOptions: { reasoning_effort: "low" } } : {};
14417
+ const r = await ai.chat({ model, messages: [{ role: "user", content: prompt }], stream: false, signal, maxTokens: 150, ...reasoning });
14418
+ return r.content ?? "";
14419
+ };
14420
+ }
14421
+ function cleanWorkReply(raw, lang) {
14422
+ const l = cleanLine(raw.replace(/^\s*(?:stop|work)\s*:/i, ""), lang, 8);
14423
+ return RESULT_CLAIM_RE.test(l) || HE_RESULT_CLAIM.test(l) ? "" : l;
14424
+ }
14425
+ function fastWorkReply(ai, model, gender, persona = "") {
14426
+ return async (c, signal) => {
14427
+ const prompt = `${persona ? `Who you are on this call:
14428
+ ${persona.slice(0, 500)}
14429
+
14430
+ ` : ""}You are on a live voice call, in the middle of doing this for them: "${c.request.slice(-160)}"${c.tool ? ` (right now: ${c.tool}${c.hint ? ` on ${c.hint}` : ""})` : ""}. They just said, while you work: "${c.heard.slice(-200)}"
14431
+ Answer them at once with ONE short spoken line of 2-7 words in ${languageName(c.lang)}, the way a person busy on it would ("yeah, looking now"). You have no results yet: state none, promise none. ` + (c.avoid.length ? `Do not reuse: ${c.avoid.map((a) => `"${a}"`).join(", ")}. ` : "") + `If they tell you to stop or drop the work, start the line with "stop:". If they change the request or add to it, start it with "work:". ${personaHint(gender)} Output only the line, no tone tag.`;
13633
14432
  const reasoning = /gpt-oss/.test(model) ? { providerOptions: { reasoning_effort: "low" } } : {};
13634
14433
  const r = await ai.chat({ model, messages: [{ role: "user", content: prompt }], stream: false, signal, maxTokens: 150, ...reasoning });
13635
14434
  return r.content ?? "";
@@ -14143,6 +14942,7 @@ var SecondChannel = class {
14143
14942
  bridgeBlock() {
14144
14943
  const w = words3(this.heard), intent = turnIntent(this.heard);
14145
14944
  if (!this.lang) return "language unknown";
14945
+ if (this.ownWords) return this.blocked("bridge");
14146
14946
  if (intent === "closer") return "no answer due";
14147
14947
  if (this.late) return this.blocked("bridge");
14148
14948
  const why = intent === "ask" ? w.length <= 6 && isPhatic(this.heard, this.bank) ? "phatic" : "" : this.shortTurn(w.length);
@@ -14167,7 +14967,7 @@ var SecondChannel = class {
14167
14967
  if (!this.lang) return "silence";
14168
14968
  if (trigger === "working") return this.bridgeBlock() ? "silence" : "bridge";
14169
14969
  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;
14970
+ 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
14971
  return k && this.allowed(k) ? k : "silence";
14172
14972
  }
14173
14973
  /** Plan (or, with `heard`, revise) the backchannels of the user's turn — async, never on the hot path. */
@@ -14230,7 +15030,7 @@ var SecondChannel = class {
14230
15030
  this.breathTimer = setTimeout(() => {
14231
15031
  this.breathTimer = null;
14232
15032
  const n = words3(t).length, tree = this.tree;
14233
- if (n < this.o.backchannelMinWords || this.heard !== t || this.leafTimer || this.picking || tree.empty || this.leafGated()) return;
15033
+ if (!this.backchannelMoment(n, true) || this.heard !== t || this.leafTimer || this.picking || tree.empty || this.leafGated()) return;
14234
15034
  this.picking = true;
14235
15035
  const ac = new AbortController(), nodes = tree.nodes.filter((x) => x.moment !== "ask"), lines = nodes.map((x, i) => ({ i, line: x.line, when: x.when }));
14236
15036
  void withBudget(this.o.pickLeaf(t, lines, ac.signal), this.o.pickBudgetMs, "breath pick", ac).then((i) => {
@@ -14292,6 +15092,7 @@ var SecondChannel = class {
14292
15092
  * sentence end only deep into a story — a short request's end is the end of their turn (replay: "mm-hm" right after
14293
15093
  * "Check hacker news for recent news.", just before the final). */
14294
15094
  backchannelMoment(n, breath) {
15095
+ if (!cueWords(this.heard).some(isContent)) return false;
14295
15096
  if (/\?$/.test(this.heard) || pendingQuestion(this.heard)) return false;
14296
15097
  if (this.carried) n += this.carried;
14297
15098
  else if (n < this.o.requestHoldWords && requestOpener(this.heard)) return false;
@@ -14363,7 +15164,7 @@ var SecondChannel = class {
14363
15164
  this.bridgeDraft = null;
14364
15165
  this.askLeaf = null;
14365
15166
  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;
15167
+ if (!this.o.generate || !(this.ownWords || this.o.generateFor.includes("bridge")) || !this.lang || !this.heard) return;
14367
15168
  const gen = this.gen, ac = new AbortController(), own = this.ownWords, heard = this.heard;
14368
15169
  const ctx = { kind: "bridge", heard: this.tail(), state: this.main, avoid: [...this.recent], lang: this.lang, ...own ? { ask: true } : {} };
14369
15170
  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 +15301,10 @@ function closedQuestion(reply) {
14500
15301
 
14501
15302
  // src/voice/call.ts
14502
15303
  var log30 = forComponent("VoiceCall");
15304
+ function newWords(text, turn) {
15305
+ const said = new Set(norm3(turn).split(" "));
15306
+ return norm3(text).split(" ").filter((w) => w && !said.has(w)).length;
15307
+ }
14503
15308
  var VoiceCallOptions = class {
14504
15309
  /** Host runs MAIN with exactly `text` (a replace: roll the `replaces` turn back first). */
14505
15310
  sendTurn = () => {
@@ -14507,6 +15312,14 @@ var VoiceCallOptions = class {
14507
15312
  /** Host aborts a running MAIN turn (barge-in). */
14508
15313
  abortTurn = () => {
14509
15314
  };
15315
+ /** Host keeps `note` for MAIN's next turn. With it (and `workReply`), what the user says while MAIN works is answered at
15316
+ * once by the fast model and MAIN is not aborted or restarted: a remark reaches MAIN as this note, a change or addition
15317
+ * runs as MAIN's next turn. Not injected mid-run: that makes MAIN run again just to hear a remark. Absent: such a line
15318
+ * barges in as before. */
15319
+ noteTurn;
15320
+ /** Writes that immediate answer (fastWorkReply). */
15321
+ workReply;
15322
+ workReplyBudgetMs = 1500;
14510
15323
  /** Host TTS (fill lane): resolves when played; `false` = could not play. */
14511
15324
  speak = () => false;
14512
15325
  /** Stop the in-flight fill line now (barge-in only). */
@@ -14539,6 +15352,13 @@ var VoiceCallOptions = class {
14539
15352
  now = () => performance.now();
14540
15353
  /** An aside (aside()) needs this long since the last one — a cue answered every time sounds scripted. */
14541
15354
  asideGapMs = 8e3;
15355
+ /** After a turn went out, new words this soon are the same turn going on (it gives way to the fuller one), if MAIN has
15356
+ * not started. Later ones are a new thing said over the work. */
15357
+ resumeMs = 3e3;
15358
+ /** Progress waits while the user spoke this recently. */
15359
+ userQuietMs = 800;
15360
+ /** A progress line starts being written this long before it is due. */
15361
+ progressLeadMs = 1e3;
14542
15362
  };
14543
15363
  var VoiceCall = class {
14544
15364
  constructor(options) {
@@ -14574,7 +15394,7 @@ var VoiceCall = class {
14574
15394
  cancel: () => o.cancel(),
14575
15395
  now: o.now
14576
15396
  });
14577
- this.progress = new Progress({ ...o.mode === "live" ? { templates: {}, generateBudgetMs: 1500 } : {}, ...ai ? { generate: fastProgressGenerator(ai, o.fastModel, o.gender) } : {}, gender: o.gender, now: o.now, ...o.progress });
15397
+ this.progress = new Progress({ ...o.mode === "live" ? { templates: {}, generateBudgetMs: 1500, gapMs: 5e3 } : {}, ...ai ? { generate: fastProgressGenerator(ai, o.fastModel, o.gender) } : {}, gender: o.gender, now: o.now, ...o.progress });
14578
15398
  this.judge = new OverlapJudge({ ...ai ? { model: overlapModelFor(ai) } : {}, now: o.now });
14579
15399
  this.gate = new TurnGate({
14580
15400
  now: o.now,
@@ -14601,6 +15421,7 @@ var VoiceCall = class {
14601
15421
  earlyHeard = null;
14602
15422
  // a fill line heard while that turn was still held
14603
15423
  progressTimer = null;
15424
+ lastUserAt = -Infinity;
14604
15425
  ended = false;
14605
15426
  get o() {
14606
15427
  return this.options;
@@ -14613,6 +15434,11 @@ var VoiceCall = class {
14613
15434
  get turn() {
14614
15435
  return this.cur && { id: this.cur.id, text: this.cur.text, running: this.cur.running };
14615
15436
  }
15437
+ /** MAIN is on this turn and has the floor to itself: nothing the user says now stops it, unless they ask it to. */
15438
+ get working() {
15439
+ const c = this.cur;
15440
+ return !!c && c.running && !c.closed && (!c.worded || c.open > 0);
15441
+ }
14616
15442
  /** The host STT's language (BCP-47): the current language until the user's words say otherwise. */
14617
15443
  setLanguage(tag) {
14618
15444
  const l = primaryLang(tag);
@@ -14625,12 +15451,16 @@ var VoiceCall = class {
14625
15451
  if (this.ended || !text.trim()) return;
14626
15452
  if (typeof aec === "boolean") this.fill.options.aec = aec;
14627
15453
  this.gate.speaking();
14628
- const c = this.cur;
14629
- if (this.o.mode === "live" && c?.running && !c.worded && text.trim().split(/\s+/).length >= 2) {
15454
+ this.lastUserAt = this.o.now();
15455
+ const c = this.cur, steers = !!this.o.noteTurn && this.working;
15456
+ const resumed = !steers || c.marks.mainFirst === void 0 && this.o.now() - c.at < this.o.resumeMs;
15457
+ if (this.o.mode === "live" && c?.running && !c.worded && resumed && newWords(text, c.text) >= 2) {
14630
15458
  log30.debug("user resumed before the reply \u2014 yielded");
14631
- this.barge();
15459
+ this.giveWay(c);
15460
+ this.gate.reopen(c.id);
14632
15461
  }
14633
15462
  this.follow(text);
15463
+ if (this.o.noteTurn && this.working) return;
14634
15464
  this.fill.onInterim(text);
14635
15465
  }
14636
15466
  /** STT final → TurnGate. `lastWordAgoMs`: how long ago the user's last word of it ended (the STT's word clock,
@@ -14689,6 +15519,8 @@ var VoiceCall = class {
14689
15519
  if (!c.streamed) {
14690
15520
  c.streamed = true;
14691
15521
  this.gate.replyStarted(c.id);
15522
+ }
15523
+ if (c.open <= 0) {
14692
15524
  this.progress.text();
14693
15525
  this.clearProgress();
14694
15526
  }
@@ -14697,9 +15529,17 @@ var VoiceCall = class {
14697
15529
  case "tool":
14698
15530
  c.marks.mainFirst ??= this.o.now();
14699
15531
  this.gate.replyStarted(c.id);
14700
- void this.onTool(c, ev.name ?? "", ev.args);
15532
+ c.open++;
15533
+ c.streamed = false;
15534
+ if (ev.name && !isPlumbing(ev.name)) {
15535
+ c.tool = ev.name;
15536
+ c.args = ev.args;
15537
+ }
15538
+ this.progress.tool();
15539
+ this.beat(c);
14701
15540
  return;
14702
15541
  case "tool_done":
15542
+ c.open = Math.max(0, c.open - 1);
14703
15543
  return;
14704
15544
  case "audio": {
14705
15545
  const at = this.o.now() + Math.max(0, inMs);
@@ -14721,11 +15561,28 @@ var VoiceCall = class {
14721
15561
  this.clearProgress();
14722
15562
  this.fill.onAgentState("idle");
14723
15563
  if (c.worded) this.close(c, "answered");
15564
+ if (c.next) {
15565
+ const next = c.next;
15566
+ c.next = void 0;
15567
+ this.onTurn(next);
15568
+ }
14724
15569
  return;
14725
15570
  }
14726
15571
  }
14727
15572
  /** The user barged in (hard): cut fill + MAIN audio (the host cuts its own), abort a running MAIN turn. */
14728
15573
  barge() {
15574
+ if (this.o.noteTurn && this.working) {
15575
+ this.fill.onBargeIn();
15576
+ try {
15577
+ this.o.cancel();
15578
+ } catch (e) {
15579
+ log30.warn(`cancel threw: ${e.message}`);
15580
+ }
15581
+ return;
15582
+ }
15583
+ this.giveWay();
15584
+ }
15585
+ giveWay(c = this.cur) {
14729
15586
  this.fill.onBargeIn();
14730
15587
  this.clearProgress();
14731
15588
  try {
@@ -14733,11 +15590,11 @@ var VoiceCall = class {
14733
15590
  } catch (e) {
14734
15591
  log30.warn(`cancel threw: ${e.message}`);
14735
15592
  }
14736
- if (this.cur?.running) {
14737
- this.cur.running = false;
14738
- this.o.abortTurn(this.cur.id);
15593
+ if (c?.running) {
15594
+ c.running = false;
15595
+ this.o.abortTurn(c.id);
14739
15596
  }
14740
- if (this.cur && !this.cur.worded) this.cur.end ??= "cut";
15597
+ if (c && !c.worded) c.end ??= "cut";
14741
15598
  }
14742
15599
  /** Soft barge-in: keep (a listener cue — no turn) or cut (→ barge()). */
14743
15600
  async overlap(text) {
@@ -14783,6 +15640,7 @@ var VoiceCall = class {
14783
15640
  if (l) this.fill.setLanguage(l, true);
14784
15641
  }
14785
15642
  onTurn(t) {
15643
+ if (this.o.noteTurn && this.o.workReply && t.kind !== "replace" && this.working && !isClosing(t.text)) return void this.overWork(this.cur, t);
14786
15644
  const now4 = this.o.now(), lastWord = this.lastWordAt !== null;
14787
15645
  const at = this.lastWordAt ?? this.firstFinalAt ?? now4;
14788
15646
  const finalAt = this.finalAt || now4;
@@ -14791,7 +15649,7 @@ var VoiceCall = class {
14791
15649
  if (this.cur) this.close(this.cur, t.replaces === this.cur.id ? "replaced" : void 0);
14792
15650
  this.follow(t.text);
14793
15651
  this.clearProgress();
14794
- this.cur = { id: t.id, text: t.text, at, finalAt, lastWord, running: true, streamed: false, sounded: false, worded: false, done: false, marks: { sent: now4 }, closed: false, due: turnIntent(t.text) !== "closer" };
15652
+ this.cur = { id: t.id, text: t.text, at, finalAt, lastWord, running: true, streamed: false, sounded: false, worded: false, done: false, marks: { sent: now4 }, closed: false, due: turnIntent(t.text) !== "closer", open: 0, tool: "", draft: "", drafting: false, draftAt: -Infinity };
14795
15653
  const early = this.earlyHeard;
14796
15654
  this.earlyHeard = null;
14797
15655
  if (early && early.at >= at) {
@@ -14829,6 +15687,7 @@ var VoiceCall = class {
14829
15687
  c.marks.fill = this.o.now();
14830
15688
  c.fillKind = kind;
14831
15689
  }
15690
+ if (c && kind === "bridge" && this.o.mode === "live" && echoesHeard(text, c.text)) this.beat(c);
14832
15691
  if (this.o.hostAudio) return r;
14833
15692
  this.lastAudibleAt = this.o.now();
14834
15693
  if (c && forTurn) {
@@ -14866,27 +15725,78 @@ var VoiceCall = class {
14866
15725
  for (const k of Object.keys(tl)) if (tl[k] === void 0) delete tl[k];
14867
15726
  this.o.onMetric({ name: "timeline", id: c.id, text: c.text, timeline: tl });
14868
15727
  }
14869
- async onTool(c, name, args) {
14870
- const lang = this.lang;
14871
- if (!lang || c.streamed) return;
14872
- const line = await this.progress.line(name, args, lang, this.o.mode === "live" ? c.text : "");
14873
- if (!line && this.o.mode === "live") this.o.onMetric({ name: "drop", id: c.id, kind: `no progress line: ${name}` });
14874
- if (!line || this.cur !== c || c.streamed || !c.running || this.lang !== lang) return;
15728
+ /** The user spoke while MAIN works: the fast model answers now, the words reach MAIN as a note, MAIN keeps going. */
15729
+ async overWork(c, t) {
15730
+ const at = this.o.now();
15731
+ this.firstFinalAt = null;
15732
+ this.lastWordAt = null;
15733
+ this.follow(t.text);
15734
+ queueMicrotask(() => this.gate.replyStarted(t.id));
15735
+ this.o.onMetric({ name: "turn", id: t.id, kind: "aside", text: t.text });
15736
+ const ac = new AbortController(), late = setTimeout(() => ac.abort(), this.o.workReplyBudgetMs), lang = this.lang;
15737
+ const raw = lang ? await this.o.workReply({ request: c.text, heard: t.text, tool: c.tool, hint: argsHint(c.args), lang, avoid: this.progress.recent }, ac.signal).catch((e) => {
15738
+ log30.warn(`work reply failed \u2014 MAIN only: ${e?.message ?? e}`);
15739
+ return "";
15740
+ }).finally(() => clearTimeout(late)) : "";
15741
+ const line = cleanWorkReply(raw, lang ?? ""), stop = /^\s*stop\s*:/i.test(raw), change = !line || /^\s*work\s*:/i.test(raw);
15742
+ const noted = `[The user said this on the call while you worked: "${t.text}".${line ? ` You answered aloud: "${line}".` : ""}${stop ? " That stopped the work." : ""} Take it into account; do not answer it again.]`;
15743
+ if (line && !this.ended) {
15744
+ for (let i = 0; i < 8 && this.say(line, "aside") === false; i++) {
15745
+ if (i === 7) {
15746
+ this.o.onMetric({ name: "drop", id: t.id, kind: "aside unplayed", text: line });
15747
+ break;
15748
+ }
15749
+ await new Promise((r) => setTimeout(r, 250));
15750
+ }
15751
+ if (this.lastAudibleAt >= at) {
15752
+ this.progress.note(line);
15753
+ this.o.show(line, "aside");
15754
+ }
15755
+ } else if (!line) this.o.onMetric({ name: "drop", id: t.id, kind: "no aside line" });
15756
+ if (stop) {
15757
+ this.o.noteTurn(noted);
15758
+ c.next = void 0;
15759
+ if (this.working && this.cur === c) this.giveWay(c);
15760
+ return;
15761
+ }
15762
+ if (change && this.cur === c && c.running) {
15763
+ c.next = c.next ? { ...c.next, text: `${c.next.text} ${t.text}` } : { ...t, kind: "new" };
15764
+ return;
15765
+ }
15766
+ if (change) return this.onTurn({ ...t, kind: "new" });
15767
+ this.o.noteTurn(noted);
15768
+ }
15769
+ /** While MAIN's tools run: a progress line once nothing was heard for `silenceMs`, then every `gapMs`, written just ahead
15770
+ * so it plays on time. Ticks are local (no network) and stop with the turn. */
15771
+ beat(c) {
14875
15772
  this.clearProgress();
14876
- const tryNow = () => {
15773
+ const tick = () => {
14877
15774
  this.progressTimer = null;
14878
- if (this.ended || this.cur !== c || c.streamed || !c.running) return;
14879
- if (!this.progress.due(this.lastAudibleAt)) {
14880
- this.progressTimer = setTimeout(tryNow, 250);
14881
- return;
15775
+ if (this.ended || this.cur !== c || !c.running || c.streamed && c.open <= 0) return;
15776
+ const lang = this.lang, now4 = this.o.now();
15777
+ if (lang && !c.draft && !c.drafting && now4 - c.draftAt >= 1e3 && this.progress.due(this.lastAudibleAt, this.o.progressLeadMs)) {
15778
+ c.drafting = true;
15779
+ void this.progress.line(c.tool, c.args, lang, this.o.mode === "live" ? c.text : "").then((l) => {
15780
+ c.drafting = false;
15781
+ c.draft = l;
15782
+ if (!l) c.draftAt = this.o.now();
15783
+ if (!l && this.o.mode === "live") this.o.onMetric({ name: "drop", id: c.id, kind: `no progress line: ${c.tool}` });
15784
+ else sayDraft();
15785
+ });
14882
15786
  }
14883
- if (this.say(line, "progress") !== false) {
14884
- this.progress.spoke(line);
15787
+ sayDraft();
15788
+ this.progressTimer = setTimeout(tick, 250);
15789
+ };
15790
+ const sayDraft = () => {
15791
+ if (!c.draft || this.ended || this.cur !== c || !c.running || c.streamed && c.open <= 0 || this.o.now() - this.lastUserAt < this.o.userQuietMs || !this.progress.due(this.lastAudibleAt)) return;
15792
+ if (this.say(c.draft, "progress") !== false) {
15793
+ this.progress.spoke(c.draft);
14885
15794
  this.fill.covered();
14886
- this.o.show(line, "progress");
15795
+ this.o.show(c.draft, "progress");
15796
+ c.draft = "";
14887
15797
  }
14888
15798
  };
14889
- tryNow();
15799
+ tick();
14890
15800
  }
14891
15801
  clearProgress() {
14892
15802
  if (this.progressTimer) {
@@ -15931,7 +16841,7 @@ init_logging();
15931
16841
  init_llm();
15932
16842
  init_platform();
15933
16843
  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";
16844
+ import { appendFileSync, existsSync as existsSync4, mkdirSync as mkdirSync5, readFileSync as readFileSync6, statSync as statSync6 } from "fs";
15935
16845
  import { homedir as homedir3 } from "os";
15936
16846
  import { dirname as dirname4, join as join8 } from "path";
15937
16847
  import { fileURLToPath } from "url";
@@ -16285,7 +17195,7 @@ var AecDuplexAudio = class {
16285
17195
  stdin.write(pcm16k);
16286
17196
  }
16287
17197
  };
16288
- var matchVoiceCommand = (text) => {
17198
+ var matchVoiceCommand2 = (text) => {
16289
17199
  const t = text.toLowerCase().replace(/[^a-z ]/g, " ").replace(/\s+/g, " ").trim().replace(/^(?:hey|ok|okay|please) /, "").replace(/ (?:please|now)$/, "");
16290
17200
  return /^(voice off|mute|stop listening)( \1)*$/.test(t) ? "off" : null;
16291
17201
  };
@@ -16589,7 +17499,8 @@ function fakeVoiceParts(uttFile) {
16589
17499
  offset = buf.length;
16590
17500
  for (const line of fresh.split("\n")) {
16591
17501
  const t = line.trim();
16592
- if (t) stt.onUtterance(t, Date.now());
17502
+ if (t.startsWith("~")) stt.onPartial(t.slice(1).trim());
17503
+ else if (t) stt.onUtterance(t, Date.now());
16593
17504
  }
16594
17505
  }, 100);
16595
17506
  },
@@ -16602,11 +17513,13 @@ function fakeVoiceParts(uttFile) {
16602
17513
  }
16603
17514
  }
16604
17515
  };
17516
+ const said = process.env.AGENTX_VOICE_FAKE_LOG;
16605
17517
  const tts = { connect() {
16606
17518
  }, newContext() {
16607
- }, speak() {
17519
+ }, speak(text) {
17520
+ if (said) appendFileSync(said, JSON.stringify({ t: Date.now(), speak: text }) + "\n");
16608
17521
  }, end() {
16609
- tts.onDone?.();
17522
+ setTimeout(() => tts.onDone?.(), 20);
16610
17523
  }, cancel() {
16611
17524
  }, close() {
16612
17525
  }, onAudio: (_) => {
@@ -16676,6 +17589,38 @@ var JsonlDiagSink = class {
16676
17589
  }
16677
17590
  };
16678
17591
 
17592
+ // src/voice/greeting.ts
17593
+ init_logging();
17594
+ var log37 = forComponent("CallGreeting");
17595
+ var CallGreetingOptions = class {
17596
+ model = "groq/openai/gpt-oss-120b";
17597
+ persona = "";
17598
+ budgetMs = 900;
17599
+ fallback = "Hey, what's up?";
17600
+ };
17601
+ async function callGreeting(ai, options) {
17602
+ const o = { ...new CallGreetingOptions(), ...options }, ac = new AbortController();
17603
+ const prompt = `${o.persona ? `Your persona:
17604
+ ${o.persona}
17605
+
17606
+ ` : ""}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.`;
17607
+ const reasoning = /gpt-oss/.test(o.model) ? { providerOptions: { reasoning_effort: "low" } } : {};
17608
+ let timer;
17609
+ const late = new Promise((r) => {
17610
+ timer = setTimeout(() => {
17611
+ ac.abort();
17612
+ r("");
17613
+ }, o.budgetMs);
17614
+ });
17615
+ 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) => {
17616
+ log37.warn(`greeting failed \u2014 canned line: ${e?.message ?? e}`);
17617
+ return "";
17618
+ });
17619
+ const line = await Promise.race([asked, late]);
17620
+ clearTimeout(timer);
17621
+ return line || o.fallback;
17622
+ }
17623
+
16679
17624
  // cli/config.ts
16680
17625
  import { homedir as homedir5 } from "os";
16681
17626
  import { existsSync as existsSync5, readFileSync as readFileSync7 } from "fs";
@@ -16823,7 +17768,7 @@ function applyOverlay(cfg, file) {
16823
17768
  // cli/hooks-config.ts
16824
17769
  import { spawnSync as spawnSync4 } from "child_process";
16825
17770
  init_platform();
16826
- var log37 = forComponent("hooks");
17771
+ var log38 = forComponent("hooks");
16827
17772
  var escapeRegex = (s) => s.replace(/[.+^${}()|[\]\\]/g, "\\$&");
16828
17773
  function ruleMatches(rule, toolName) {
16829
17774
  if (!rule.tool || rule.tool === "*") return true;
@@ -16842,7 +17787,7 @@ function runCmd(rule, env) {
16842
17787
  });
16843
17788
  return { code: r.status ?? 1, out: ((r.stdout ?? "") + (r.stderr ?? "")).trim() };
16844
17789
  } catch (e) {
16845
- log37.debug(`hook command failed: ${rule.command}`, e);
17790
+ log38.debug(`hook command failed: ${rule.command}`, e);
16846
17791
  return { code: 1, out: String(e?.message ?? e) };
16847
17792
  }
16848
17793
  }
@@ -16950,7 +17895,7 @@ import { existsSync as existsSync6, mkdirSync as mkdirSync8, readFileSync as rea
16950
17895
  import { homedir as homedir6 } from "os";
16951
17896
  import { join as join11 } from "path";
16952
17897
  init_platform();
16953
- var log38 = forComponent("session");
17898
+ var log39 = forComponent("session");
16954
17899
  var globalDir = () => join11(homedir6(), ".agent", "sessions");
16955
17900
  function indexLink(target, link2) {
16956
17901
  try {
@@ -17013,7 +17958,7 @@ var SessionStore = class {
17013
17958
  }
17014
17959
  load(id) {
17015
17960
  if (!this.safeId(id)) {
17016
- log38.debug(`rejecting unsafe session id: ${id}`);
17961
+ log39.debug(`rejecting unsafe session id: ${id}`);
17017
17962
  return void 0;
17018
17963
  }
17019
17964
  const path = join11(this.dir, `${id}.json`);
@@ -17021,7 +17966,7 @@ var SessionStore = class {
17021
17966
  try {
17022
17967
  return JSON.parse(readFileSync8(path, "utf8"));
17023
17968
  } catch (e) {
17024
- log38.debug(`unreadable session ${id} \u2014 ignoring`, e);
17969
+ log39.debug(`unreadable session ${id} \u2014 ignoring`, e);
17025
17970
  return void 0;
17026
17971
  }
17027
17972
  }
@@ -17034,7 +17979,7 @@ var SessionStore = class {
17034
17979
  try {
17035
17980
  metas.push(JSON.parse(readFileSync8(join11(this.dir, f), "utf8")).meta);
17036
17981
  } catch (e) {
17037
- log38.debug(`skipping unreadable session file ${f}`, e);
17982
+ log39.debug(`skipping unreadable session file ${f}`, e);
17038
17983
  }
17039
17984
  }
17040
17985
  return metas.sort((a, b) => b.updated - a.updated);
@@ -17466,7 +18411,7 @@ import { promisify } from "util";
17466
18411
  import { writeFileSync as writeFileSync6, mkdirSync as mkdirSync9 } from "fs";
17467
18412
  import { homedir as homedir7 } from "os";
17468
18413
  import { join as join12, resolve as resolve4, sep as sep3, parse as parsePath } from "path";
17469
- var log39 = forComponent("checkpoints");
18414
+ var log40 = forComponent("checkpoints");
17470
18415
  var exec = promisify(execFile4);
17471
18416
  var DEFAULT_EXCLUDE = [".agent/", ".git/", "node_modules/", "dist/", "build/", ".next/", "target/", ".venv/", "__pycache__/", "*.log"];
17472
18417
  function isBroadRoot(p) {
@@ -17514,7 +18459,7 @@ var ShadowRepo = class {
17514
18459
  writeFileSync6(join12(this.gitDir, "info", "exclude"), this.exclude.join("\n") + "\n");
17515
18460
  this.ready = true;
17516
18461
  } catch (e) {
17517
- log39.debug(`git checkpoints unavailable for ${this.workTree}`, e);
18462
+ log40.debug(`git checkpoints unavailable for ${this.workTree}`, e);
17518
18463
  this.ready = false;
17519
18464
  }
17520
18465
  return this.ready;
@@ -17525,7 +18470,7 @@ var ShadowRepo = class {
17525
18470
  }
17526
18471
  async commit(label, forced = []) {
17527
18472
  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));
18473
+ for (const p of forced) await this.run("add", "-f", "--", p).catch((e) => log40.debug(`force-add failed: ${p}`, e));
17529
18474
  await this.run("commit", "--allow-empty", "-q", "-m", label);
17530
18475
  }
17531
18476
  /** Inject the CURRENT (pre-edit) content of `paths` into the turn-open restore point by amending it.
@@ -17533,8 +18478,8 @@ var ShadowRepo = class {
17533
18478
  * turn-boundary `add -A` would never have captured it. Amend (vs a new commit) keeps one restore
17534
18479
  * point per turn, so the REPL's turn↔frame mapping stays intact. */
17535
18480
  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));
18481
+ for (const p of paths) await this.run("add", "-f", "--", p).catch((e) => log40.debug(`force-capture failed: ${p}`, e));
18482
+ await this.run("commit", "--amend", "--no-edit", "-q", "--allow-empty").catch((e) => log40.debug("amend failed", e));
17538
18483
  }
17539
18484
  /** Commits on `ref`, oldest-first (canonical index space). */
17540
18485
  async log(ref) {
@@ -17594,7 +18539,7 @@ var ShadowRepo = class {
17594
18539
  await this.run("gc", "--auto", "-q").catch(() => {
17595
18540
  });
17596
18541
  } catch (e) {
17597
- log39.debug("checkpoint prune failed", e);
18542
+ log40.debug("checkpoint prune failed", e);
17598
18543
  }
17599
18544
  }
17600
18545
  };
@@ -17662,7 +18607,7 @@ var GitCheckpoints = class {
17662
18607
  use(sessionId) {
17663
18608
  if (sessionId === this.session) return;
17664
18609
  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));
18610
+ if (this.started) for (const r of this.repos) void r.point(this.ref()).catch((e) => log40.debug("re-point failed", e));
17666
18611
  }
17667
18612
  async begin(label) {
17668
18613
  if (!await this.start()) return;
@@ -17682,7 +18627,7 @@ var GitCheckpoints = class {
17682
18627
  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
18628
  `);
17684
18629
  } else {
17685
- log39.debug("checkpoint commit failed", e);
18630
+ log40.debug("checkpoint commit failed", e);
17686
18631
  survivors.push(this.repos[i]);
17687
18632
  survivorCaches.push(this.caches[i] ?? []);
17688
18633
  }
@@ -17717,7 +18662,7 @@ var GitCheckpoints = class {
17717
18662
  if (this.forced.has(abs)) continue;
17718
18663
  this.forced.add(abs);
17719
18664
  const i = this.repoIndexFor(abs);
17720
- if (i >= 0) await this.repos[i].amendForced([abs]).catch((e) => log39.debug("amendForced failed", e));
18665
+ if (i >= 0) await this.repos[i].amendForced([abs]).catch((e) => log40.debug("amendForced failed", e));
17721
18666
  }
17722
18667
  }
17723
18668
  };
@@ -19507,7 +20452,7 @@ import { spawnSync as spawnSync6 } from "child_process";
19507
20452
  import { writeFileSync as writeFileSync8, mkdirSync as mkdirSync10, readdirSync as readdirSync2, unlinkSync as unlinkSync3, chmodSync, existsSync as existsSync7 } from "fs";
19508
20453
  import { homedir as homedir8 } from "os";
19509
20454
  import { join as join14 } from "path";
19510
- var log40 = forComponent("os-sched");
20455
+ var log41 = forComponent("os-sched");
19511
20456
  var OsScheduler = class {
19512
20457
  options;
19513
20458
  constructor(options) {
@@ -19569,7 +20514,7 @@ var OsScheduler = class {
19569
20514
  }
19570
20515
  }
19571
20516
  } catch (e) {
19572
- log40.debug(`cancel ${id}`, e);
20517
+ log41.debug(`cancel ${id}`, e);
19573
20518
  }
19574
20519
  for (const f of [`${id}.json`, `${id}.sh`]) {
19575
20520
  try {
@@ -19687,7 +20632,7 @@ import { existsSync as existsSync8, mkdirSync as mkdirSync11, unlinkSync as unli
19687
20632
  import { homedir as homedir9 } from "os";
19688
20633
  import { join as join15 } from "path";
19689
20634
  init_platform();
19690
- var log41 = forComponent("remote-trigger");
20635
+ var log42 = forComponent("remote-trigger");
19691
20636
  var TRIGGER_DIR = () => join15(homedir9(), ".agent", "triggers");
19692
20637
  var sockPath = (sessionId, dir = TRIGGER_DIR()) => join15(dir, `${sessionId}.sock`);
19693
20638
  var TriggerServer = class {
@@ -19725,13 +20670,13 @@ var TriggerServer = class {
19725
20670
  conn.end(JSON.stringify({ ok: false, error: String(e) }) + "\n");
19726
20671
  }
19727
20672
  });
19728
- conn.on("error", (e) => log41.debug("trigger conn error", e));
20673
+ conn.on("error", (e) => log42.debug("trigger conn error", e));
19729
20674
  });
19730
- this.server.on("error", (e) => log41.debug("trigger server error", e));
20675
+ this.server.on("error", (e) => log42.debug("trigger server error", e));
19731
20676
  this.server.listen(p);
19732
20677
  this.path = p;
19733
20678
  } catch (e) {
19734
- log41.debug("trigger server unavailable", e);
20679
+ log42.debug("trigger server unavailable", e);
19735
20680
  }
19736
20681
  }
19737
20682
  /** Re-bind on /resume (the session id changed). */
@@ -19863,7 +20808,7 @@ var strike = C("9");
19863
20808
  var link = (text, url) => useColor ? `\x1B]8;;${url}\x1B\\${cyan(text)}\x1B]8;;\x1B\\` : `${text} (${url})`;
19864
20809
  var mdPalette = { bold, dim, cyan, italic, strike, green, link };
19865
20810
  var err = (s) => process.stderr.write(s);
19866
- var log42 = forComponent("cli");
20811
+ var log43 = forComponent("cli");
19867
20812
  var VERSION = (() => {
19868
20813
  try {
19869
20814
  return JSON.parse(readFileSync10(new URL("../package.json", import.meta.url), "utf8")).version ?? "?";
@@ -20016,6 +20961,7 @@ function parseArgs(argv) {
20016
20961
  } else if (x === "--call") {
20017
20962
  a.call = true;
20018
20963
  a.duplex = true;
20964
+ a.lean = true;
20019
20965
  } else if (x === "--lean") a.lean = true;
20020
20966
  else if (x === "--keep-alive") a.keepAlive = true;
20021
20967
  else if (x === "--channel-only") a.channelOnly = true;
@@ -20072,6 +21018,7 @@ function parseArgs(argv) {
20072
21018
  if (a.duplex && a.plan) throw new Error("--plan is not supported in --duplex (workers are non-interactive; a plan could never be approved)");
20073
21019
  if (a.voiceSystemPrompt && !a.duplex) throw new Error("--voice-system-prompt only applies with --duplex");
20074
21020
  if (a.voicePersona && !a.duplex && !a.channelOnly) throw new Error("--voice-persona only applies with --call or --channel-only");
21021
+ if (a.call) process.env.ENABLE_TOOL_SEARCH ??= "false";
20075
21022
  if (a.voicePersona) process.env.AGENTX_CALL_PERSONA = a.voicePersona;
20076
21023
  if (a.call && a.inputFormat) throw new Error("--call is interactive (mic/TTS here); a headless host runs --channel-only");
20077
21024
  if (a.channelMode && !a.duplexV2 && !a.channelOnly && !a.call) throw new Error("--channel-mode only applies with --duplex-v2 or --channel-only");
@@ -20765,7 +21712,7 @@ function turnCost(model, usage) {
20765
21712
  const priced = model.startsWith("claude-code/") ? `anthropic/${resolveModelAlias(model.slice("claude-code/".length))}` : model;
20766
21713
  return costOf(getModelInfo(priced)?.pricing, usage?.promptTokens ?? 0, usage?.completionTokens ?? 0, usage?.cacheCreationTokens ?? 0, usage?.cacheReadTokens ?? 0, priced);
20767
21714
  }
20768
- async function evaluateGoal(ai, condition, transcript, log43) {
21715
+ async function evaluateGoal(ai, condition, transcript, log44) {
20769
21716
  const recent = transcript.filter((m) => m.role === "assistant").slice(-8).map((m) => {
20770
21717
  const text = typeof m.content === "string" ? m.content : m.content.filter((p) => p.type === "text").map((p) => p.text).join(" ");
20771
21718
  return text.slice(0, 600);
@@ -20785,7 +21732,7 @@ ${recent}` }
20785
21732
  const match = r.content.match(/\{[\s\S]*\}/);
20786
21733
  if (match) return JSON.parse(match[0]);
20787
21734
  } catch (e) {
20788
- log43(dim(` (goal evaluator error: ${e?.message ?? e})
21735
+ log44(dim(` (goal evaluator error: ${e?.message ?? e})
20789
21736
  `));
20790
21737
  }
20791
21738
  return { met: false, reason: "evaluation unclear" };
@@ -21013,7 +21960,7 @@ function mcpAgentTools(mounted, opts) {
21013
21960
  return tools;
21014
21961
  }
21015
21962
  async function closeMcp(mounted) {
21016
- await Promise.all(mounted.map((m) => m.client.close().catch((e) => log42.debug("mcp close failed", e))));
21963
+ await Promise.all(mounted.map((m) => m.client.close().catch((e) => log43.debug("mcp close failed", e))));
21017
21964
  }
21018
21965
  var IMG_EXT = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp" };
21019
21966
  function mentionRefs(line) {
@@ -21072,7 +22019,7 @@ async function expandMentions(fs, line) {
21072
22019
  if (loaded.includes(ref) || missing.includes(ref)) continue;
21073
22020
  if (ref.includes(":") && mcpMentionResolver) {
21074
22021
  const body = await mcpMentionResolver(ref).catch((e) => {
21075
- log42.debug("mcp mention resolve failed", e);
22022
+ log43.debug("mcp mention resolve failed", e);
21076
22023
  return null;
21077
22024
  });
21078
22025
  if (body != null) {
@@ -21098,7 +22045,7 @@ ${await fs.readFile(path)}`);
21098
22045
  ${blocks.join("\n\n")}` : line, loaded, missing };
21099
22046
  }
21100
22047
  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) });
22048
+ const p = parseDirectives(task, { strict: true, keys: ["model", "turns", "delegate", "advisor", "mode"], warn: (m) => log43.debug(m) });
21102
22049
  const d = p.directives, isMode = !!(d.delegate || d.advisor || d.modeOff);
21103
22050
  if (p.unresolvedModel && /->|<-|delegate:|advisor:/.test(task.slice(0, task.length - p.prompt.length))) {
21104
22051
  err(red(` \u2717 unknown model "${p.unresolvedModel}"${p.ambiguous ? ` (could be: ${p.ambiguous.join(", ")})` : ""} \u2014 turn not sent
@@ -21191,7 +22138,7 @@ async function runTurn(agent, store, session, task, cp, cwd = process.cwd(), sen
21191
22138
  try {
21192
22139
  store.save(session);
21193
22140
  } catch (ex) {
21194
- log42.debug("mid-turn session flush failed", ex);
22141
+ log43.debug("mid-turn session flush failed", ex);
21195
22142
  }
21196
22143
  }
21197
22144
  return origNotify(e);
@@ -21355,14 +22302,14 @@ var isCancelTeardown = (e) => {
21355
22302
  function installCancelGuards(mounted) {
21356
22303
  process.on("unhandledRejection", (e) => {
21357
22304
  if (isCancelTeardown(e)) {
21358
- log42.debug("suppressed unhandledRejection (cursor stream cancel)", e);
22305
+ log43.debug("suppressed unhandledRejection (cursor stream cancel)", e);
21359
22306
  return;
21360
22307
  }
21361
- log42.error("unhandledRejection", e);
22308
+ log43.error("unhandledRejection", e);
21362
22309
  });
21363
22310
  process.on("uncaughtException", (e) => {
21364
22311
  if (isCancelTeardown(e)) {
21365
- log42.debug("suppressed uncaughtException (cursor stream cancel)", e);
22312
+ log43.debug("suppressed uncaughtException (cursor stream cancel)", e);
21366
22313
  return;
21367
22314
  }
21368
22315
  console.error(e);
@@ -21687,16 +22634,22 @@ async function repl(args, ai, cfg, cwd) {
21687
22634
  const face = dx ? dx.voice : agent;
21688
22635
  let call;
21689
22636
  let callChain = Promise.resolve();
22637
+ let callFastModel = "";
22638
+ let callReplyOpen = false;
22639
+ let greetNote = "";
21690
22640
  const callTurns = /* @__PURE__ */ new Map();
22641
+ const callTools = /* @__PURE__ */ new Set();
21691
22642
  if (args.call) {
21692
22643
  if (args.voice || args.voicePersona) agent.options.systemPrompt += "\n\n" + (args.voicePersona ?? CALL_PROMPT);
22644
+ agent.options.systemPrompt += "\n\n" + CALL_WEB_PROMPT;
21693
22645
  if (args.voiceSystemPrompt) agent.options.systemPrompt += "\n\n" + args.voiceSystemPrompt;
22646
+ const fast = callFast(args, ai, cfg);
22647
+ for (const n of fast.notes) err(yellow(` \u26A0 ${n}
22648
+ `));
22649
+ callFastModel = fast.options.fastModel;
21694
22650
  call = new VoiceCall({
21695
22651
  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 ?? "",
22652
+ ...fast.options,
21700
22653
  show: (t, kind) => {
21701
22654
  err("\r\x1B[0J" + dim(` \u21B3 ${kind === "progress" ? "\u22EF " : ""}${t}
21702
22655
  `));
@@ -21704,7 +22657,16 @@ async function repl(args, ai, cfg, cwd) {
21704
22657
  },
21705
22658
  onMetric: (m) => voiceDiag?.push({ t: performance.now(), ...m, kind: `call_${m.name}`, ...m.kind ? { what: m.kind } : {} }),
21706
22659
  abortTurn: (id) => {
21707
- if (callTurns.get(id)?.running) activeTurn?.abort();
22660
+ if (callTurns.get(id)?.running) {
22661
+ voiceDiag?.push({ t: performance.now(), kind: "call_abort", id });
22662
+ activeTurn?.abort();
22663
+ }
22664
+ },
22665
+ noteTurn: (note) => {
22666
+ greetNote += `
22667
+
22668
+ ${note}`;
22669
+ voiceDiag?.push({ t: performance.now(), kind: "call_note", note });
21708
22670
  },
21709
22671
  sendTurn: (text, { id, replaces }) => {
21710
22672
  const old = replaces ? callTurns.get(replaces) : void 0;
@@ -21723,15 +22685,17 @@ async function repl(args, ai, cfg, cwd) {
21723
22685
  if (cut && keepHeard(face.transcript, cut)) voiceDiag?.push({ t: performance.now(), kind: "call_kept_heard", heard: cut.heard.length, full: cut.full.length });
21724
22686
  rec.running = true;
21725
22687
  rec.before = face.transcript.length;
21726
- voiceIO?.beginSpeech(true);
22688
+ const note = greetNote;
22689
+ greetNote = "";
22690
+ callReplyOpen = false;
21727
22691
  try {
21728
- const r = await dispatchLine(text, { id });
22692
+ const r = await dispatchLine(text + note, { id });
21729
22693
  if (r === "quit") {
21730
22694
  await voiceIO?.awaitIdle();
21731
22695
  editorRef?.abort();
21732
22696
  }
21733
22697
  } catch (e) {
21734
- log42.error("call turn failed", e);
22698
+ log43.error("call turn failed", e);
21735
22699
  } finally {
21736
22700
  rec.running = false;
21737
22701
  call?.main({ type: "done", id });
@@ -21745,11 +22709,23 @@ async function repl(args, ai, cfg, cwd) {
21745
22709
  if (e.kind === "text_delta" && typeof e.message === "string") call.main({ type: "text", text: e.message });
21746
22710
  else if (e.kind === "tool_use") call.main({ type: "tool", name: String(e.name ?? ""), args: e.input });
21747
22711
  else if (e.kind === "tool_result") call.main({ type: "tool_done" });
22712
+ else if (e.kind === "tool_activity") {
22713
+ if (!callTools.has(e.id)) {
22714
+ callTools.add(e.id);
22715
+ call.main({ type: "tool", name: String(e.name ?? ""), args: e.input });
22716
+ }
22717
+ if (e.status !== "running") call.main({ type: "tool_done" });
22718
+ }
21748
22719
  if (e.kind === "text_delta" && voiceIO) {
21749
22720
  voiceReasonEnd();
21750
22721
  spinner.stop();
21751
- voiceEcho(voiceIO.speakDelta(e.message));
22722
+ if (!callReplyOpen) {
22723
+ callReplyOpen = true;
22724
+ voiceIO.beginSpeech();
22725
+ }
22726
+ const echo = voiceIO.speakDelta(e.message);
21752
22727
  editorRef?.suspend();
22728
+ voiceEcho(echo);
21753
22729
  return;
21754
22730
  }
21755
22731
  if (voiceIO && (e.kind === "turn_start" || e.kind === "thinking_delta")) return;
@@ -21863,7 +22839,7 @@ async function repl(args, ai, cfg, cwd) {
21863
22839
  try {
21864
22840
  mkdirSync12(dir, { recursive: true });
21865
22841
  } catch (e) {
21866
- log42.debug("paste image dir create failed", e);
22842
+ log43.debug("paste image dir create failed", e);
21867
22843
  }
21868
22844
  const img = grabClipboardImage(dir, String(Date.now()));
21869
22845
  return img ? { display: "Image", ref: "@" + img.path, path: img.path } : null;
@@ -21974,9 +22950,9 @@ Added entries are loadable now via the Skill/SlashCommand tools; removed ones ar
21974
22950
  const remember2 = (line) => {
21975
22951
  try {
21976
22952
  mkdirSync12(join16(cwd, ".agent"), { recursive: true });
21977
- appendFileSync(histPath, line + "\n");
22953
+ appendFileSync2(histPath, line + "\n");
21978
22954
  } catch (e) {
21979
- log42.debug("history write failed", e);
22955
+ log43.debug("history write failed", e);
21980
22956
  }
21981
22957
  };
21982
22958
  const ago = (t) => {
@@ -22048,7 +23024,7 @@ Added entries are loadable now via the Skill/SlashCommand tools; removed ones ar
22048
23024
  try {
22049
23025
  store.save(session);
22050
23026
  } catch (e) {
22051
- log42.debug("session save after rewind failed", e);
23027
+ log43.debug("session save after rewind failed", e);
22052
23028
  }
22053
23029
  err(green(" \u27F2 jumped back") + dim(` \u2014 ${face.transcript.length} message(s) kept; edit + resend
22054
23030
  `));
@@ -22083,7 +23059,7 @@ ${task}`;
22083
23059
  bangContext.length = 0;
22084
23060
  }
22085
23061
  const delta = await refreshCatalogs().catch((e) => {
22086
- log42.debug("catalog refresh failed", e);
23062
+ log43.debug("catalog refresh failed", e);
22087
23063
  return "";
22088
23064
  });
22089
23065
  if (delta) {
@@ -22353,7 +23329,7 @@ ${task}`;
22353
23329
  desc: "rescan skills/commands dirs and rebuild the system prompt (one cache miss) \u2014 picks up entries created mid-session",
22354
23330
  run: async () => {
22355
23331
  await refreshCatalogs().catch((e) => {
22356
- log42.debug("catalog refresh failed", e);
23332
+ log43.debug("catalog refresh failed", e);
22357
23333
  });
22358
23334
  face.reprepare();
22359
23335
  err(green(` \u2713 reloaded \u2014 ${skills.length} skill(s), ${cmds.length} command(s); system prompt rebuilds on next message
@@ -22856,7 +23832,7 @@ ${task}`;
22856
23832
  try {
22857
23833
  for (const def of (await loadAgents(fs2, d)).agents) if (!seen.has(def.name)) seen.set(def.name, { def, from: d });
22858
23834
  } catch (e) {
22859
- log42.debug(`loadAgents(${d}) failed`, e);
23835
+ log43.debug(`loadAgents(${d}) failed`, e);
22860
23836
  }
22861
23837
  }
22862
23838
  if (!seen.size) {
@@ -22944,7 +23920,7 @@ ${task}`;
22944
23920
  }
22945
23921
  if (idx >= 0) {
22946
23922
  const old = mounted.splice(idx, 1)[0];
22947
- await old.client.close().catch((e) => log42.debug("mcp close failed", e));
23923
+ await old.client.close().catch((e) => log43.debug("mcp close failed", e));
22948
23924
  }
22949
23925
  try {
22950
23926
  const m = await mountMcpServer(name, conf);
@@ -22972,7 +23948,7 @@ ${task}`;
22972
23948
  }
22973
23949
  const m = mounted.splice(idx, 1)[0];
22974
23950
  remountMcpTools();
22975
- await m.client.close().catch((e) => log42.debug("mcp close failed", e));
23951
+ await m.client.close().catch((e) => log43.debug("mcp close failed", e));
22976
23952
  err(dim(` removed "${name}"
22977
23953
  `));
22978
23954
  return;
@@ -23175,7 +24151,7 @@ ${task}`;
23175
24151
  try {
23176
24152
  return readdirSync4(join16(cwd, absDir.replace(/^\/+/, "")), { withFileTypes: true }).map((d) => ({ name: d.name, dir: d.isDirectory() }));
23177
24153
  } catch (e) {
23178
- log42.debug("completion readdir failed", absDir, e);
24154
+ log43.debug("completion readdir failed", absDir, e);
23179
24155
  return null;
23180
24156
  }
23181
24157
  };
@@ -23314,7 +24290,7 @@ ${out}
23314
24290
  return;
23315
24291
  }
23316
24292
  await refreshCatalogs().catch((e) => {
23317
- log42.debug("catalog refresh failed", e);
24293
+ log43.debug("catalog refresh failed", e);
23318
24294
  });
23319
24295
  const sk = skills.find((x) => x.name === name);
23320
24296
  if (sk) {
@@ -23353,7 +24329,7 @@ ${out}
23353
24329
  if (voiceIO) return true;
23354
24330
  if (greet && call) {
23355
24331
  bindClaudeCodeSession(agent, session);
23356
- await agent.prewarm().catch((e) => err(dim(` (prewarm failed: ${e?.message ?? e})
24332
+ callChain = agent.prewarm().catch((e) => err(dim(` (prewarm failed: ${e?.message ?? e})
23357
24333
  `)));
23358
24334
  }
23359
24335
  if (!(duplex || call) || !process.stdin.isTTY) {
@@ -23374,13 +24350,13 @@ ${out}
23374
24350
  liveSttTerms = sttTerms;
23375
24351
  voiceIO = new VoiceIO({
23376
24352
  ...fakeVoice ?? {},
23377
- ...call ? { adaptiveAckMs: 0 } : {},
23378
- // v3: the call's fill channel covers the wait — no engine ack on top
24353
+ // 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
24354
+ ...call ? { adaptiveAckMs: 0, adaptiveEndpointing: true, utteranceMergeMs: 0, flushFirstClause: true } : {},
23379
24355
  sttTerms,
23380
24356
  onDiag: (ev) => voiceDiag?.push(ev),
23381
24357
  emotions: emotionsOn,
23382
- showEmotions,
23383
- // local is authoritative (a /voice-emotions before mic-on still applies)
24358
+ showEmotions: !call && showEmotions,
24359
+ // local is authoritative (a /voice-emotions before mic-on still applies); a call's tags are speech, never text
23384
24360
  // No FIXED ack phrase by default: a fixed "Mm-hm," every turn reads robotic, and the
23385
24361
  // conversational register already opens with a natural reaction. Instead the ADAPTIVE ack
23386
24362
  // (VoiceIOOptions.adaptiveAckMs, default 600ms — a safety net, not the voice) speaks a short varied ack only when the reflex
@@ -23414,7 +24390,7 @@ ${out}
23414
24390
  // Barge-in also parks any in-flight task's spoken delivery so a superseded topic can't keep talking
23415
24391
  // over the user's new one (debt-after-jokes regression) — the result still lands in the transcript.
23416
24392
  onBargeIn: (phase) => {
23417
- activeTurn?.abort();
24393
+ if (!call?.working) activeTurn?.abort();
23418
24394
  dx?.parkInFlightDeliveries();
23419
24395
  voiceReasonEnd();
23420
24396
  voiceEchoEnd();
@@ -23423,7 +24399,7 @@ ${out}
23423
24399
  onUtterance: (text, meta) => {
23424
24400
  voicePartial = "";
23425
24401
  if (!text.trim()) return;
23426
- if (matchVoiceCommand(text) === "off") {
24402
+ if (matchVoiceCommand2(text) === "off") {
23427
24403
  dx?.abortSpeculation();
23428
24404
  err(`\r\x1B[K ${bold(cyan("\u{1F3A4} \u203A"))} ${text}
23429
24405
  `);
@@ -23436,7 +24412,7 @@ ${out}
23436
24412
  return;
23437
24413
  }
23438
24414
  if (call) {
23439
- call.final(text);
24415
+ call.final(text, void 0, { ...meta?.lastWordAgoMs !== void 0 ? { lastWordAgoMs: meta.lastWordAgoMs } : {}, ...meta?.language ? { lang: meta.language } : {} });
23440
24416
  return;
23441
24417
  }
23442
24418
  const replaces = replacedVoiceTurn(lastVoiceTurn, meta);
@@ -23444,7 +24420,7 @@ ${out}
23444
24420
  if (delivery || cut) dx?.parkCutDelivery(delivery, !!cut, heard);
23445
24421
  const cutReply = cut && cut.full.length - cut.heard.length > 40 ? cut : null;
23446
24422
  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}`);
24423
+ log43.warn(`barge intent failed \u2014 model decides: ${e.message}`);
23448
24424
  return void 0;
23449
24425
  }) : Promise.resolve(void 0)).then(async (intent) => interruptionNote(cutReply, intent) + ackedNote(await instinct?.ackFor(text)));
23450
24426
  const cutIn = replaces ? replaces.cutIn : interjectNote;
@@ -23567,10 +24543,20 @@ ${out}
23567
24543
  }
23568
24544
  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
24545
  `));
23570
- if (greet) {
24546
+ if (greet && call && !session.messages.length) {
24547
+ const v = voiceIO, line = await callGreeting(ai, { model: callFastModel, persona: args.voicePersona ?? "" });
24548
+ const said = renderEmotions(line, { show: false }).prose.trim();
24549
+ v.speakFiller(line);
24550
+ err(`\r\x1B[K ${said}
24551
+ `);
24552
+ editorRef?.redrawNow();
24553
+ greetNote = `
24554
+
24555
+ <call_greeting>You opened this call with: "${said}". The user's words answer it.</call_greeting>`;
24556
+ } else if (greet) {
23571
24557
  const where = cwd.split("/").pop();
23572
24558
  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.`;
24559
+ 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
24560
  void turn(prompt).finally(() => editorRef?.redrawNow());
23575
24561
  }
23576
24562
  return true;
@@ -23818,7 +24804,8 @@ function readPersona(file) {
23818
24804
  if (!text) throw new Error(`--voice-persona: ${file} is empty`);
23819
24805
  return text;
23820
24806
  }
23821
- var CALL_PROMPT = `This is a live VOICE call: your text is spoken aloud. Reply in the language of the user's last message. Open with one short sentence (it is spoken while you continue), then keep it brief and conversational \u2014 no markdown, lists, code or URLs read aloud. Short listener sounds ("mm-hm", "let me check\u2026") are played for you while you work; do not repeat or mention them.`;
24807
+ var CALL_PROMPT = `This is a live VOICE call: your text is spoken aloud. Reply in the language of the user's last message. Open with one short sentence (it is spoken while you continue), then keep it brief and conversational \u2014 no markdown, lists, code or URLs read aloud. When they react or push back ("what?", "are you serious?"), react first, like a person, then give the fact. Short listener sounds ("mm-hm", "let me check\u2026") are played for you while you work; do not repeat or mention them.`;
24808
+ var CALL_WEB_PROMPT = "For public web pages use WebSearch and WebFetch; touch the user's browser only when they ask for it.";
23822
24809
  var HOLD_RESUME_MS = 4e3;
23823
24810
  var leakDecider = () => voiceDecider();
23824
24811
  function makeSecondChannel(ai, reflexModel, mode = "normal", opts) {
@@ -23834,10 +24821,10 @@ function channelOptions(ai, model, gender) {
23834
24821
  // 0: the reactive live path
23835
24822
  pickLeaf: fastLeafPicker(ai, process.env.AGENTX_LEAF_MODEL || "groq/openai/gpt-oss-120b"),
23836
24823
  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}` : ""}`),
24824
+ 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
24825
  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`);
24826
+ if (e.moment) log43.debug(`voice leaf: ${e.moment} ${e.conf} "${e.text}" ${e.acted}`);
24827
+ 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
24828
  },
23842
24829
  // Banks in the user's language: generated once per language, cached + topped up across sessions (~/.agent/cache).
23843
24830
  banks: new ChannelBanks({ generate: fastBankGenerator(ai, model), cacheDir: join16(homedir10(), ".agent", "cache", "channel-banks") }),
@@ -23848,6 +24835,18 @@ function channelOptions(ai, model, gender) {
23848
24835
  ...process.env.AGENTX_CHANNEL_ANTICIPATE === "1" ? { anticipate: true } : {}
23849
24836
  };
23850
24837
  }
24838
+ function callFast(args, ai, cfg) {
24839
+ const { reflexModel, notes } = duplexModels({ voiceModel: args.voiceModel, reflexModel: cfg.reflexModel, thinkModel: false, actModel: String(args.model ?? cfg.model ?? ""), apiKeys: { ...cfg.apiKeys, ...apiKeysFromEnv() } });
24840
+ const model = reflexModel ?? new DuplexAgentOptions().reflexModel, line = process.env.AGENTX_CHANNEL_MODEL || model;
24841
+ return { model, notes, options: {
24842
+ fastModel: line,
24843
+ mode: args.channelMode,
24844
+ gender: args.voiceGender ?? "",
24845
+ progress: { generate: fastProgressGenerator(ai, line, args.voiceGender) },
24846
+ workReply: fastWorkReply(ai, line, args.voiceGender, args.voicePersona),
24847
+ channel: channelOptions(ai, line, args.voiceGender)
24848
+ } };
24849
+ }
23851
24850
  function overlapResponder(ai, mode, out, aside) {
23852
24851
  const judge = softBarge(mode) ? new OverlapJudge({ model: overlapModelFor(ai) }) : void 0;
23853
24852
  return (m) => {
@@ -23919,7 +24918,7 @@ async function duplexServe(args, ai, cfg, cwd) {
23919
24918
  try {
23920
24919
  store.save(session);
23921
24920
  } catch (e) {
23922
- log42.warn("duplex session save failed", e);
24921
+ log43.warn("duplex session save failed", e);
23923
24922
  }
23924
24923
  };
23925
24924
  const overlap2 = overlapResponder(ai, args.duplexV2 ? args.channelMode : void 0, out);
@@ -23959,7 +24958,7 @@ async function duplexServe(args, ai, cfg, cwd) {
23959
24958
  done2();
23960
24959
  out({ type: "turn_done", id, finishReason: res?.finishReason ?? "error", ...why ? { error: why } : {} });
23961
24960
  }).catch((e) => {
23962
- log42.error("duplex turn failed", e);
24961
+ log43.error("duplex turn failed", e);
23963
24962
  done2();
23964
24963
  out({ type: "turn_done", id, finishReason: "error", error: String(e?.message ?? e) });
23965
24964
  });
@@ -23975,15 +24974,11 @@ async function duplexServe(args, ai, cfg, cwd) {
23975
24974
  }
23976
24975
  async function channelServe(args, ai, cfg) {
23977
24976
  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;
24977
+ const { model, notes, options: fast } = callFast(args, ai, cfg);
23980
24978
  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) },
24979
+ ...fast,
23985
24980
  // 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 }) },
24981
+ 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
24982
  speak: (line, kind) => {
23988
24983
  out(channelLine(line, kind));
23989
24984
  return true;