@livx.cc/agentx 0.99.147 → 0.99.149

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
@@ -10515,6 +10515,7 @@ var OverlapJudge = class {
10515
10515
  };
10516
10516
  const rule = overlapRule(heard);
10517
10517
  if (rule) return done(rule, "rule");
10518
+ if (norm2(heard).split(/[\s-]+/).filter(Boolean).length < 2) return done("keep", "lone");
10518
10519
  if (!this.o.model) return done("cut", "none");
10519
10520
  const ctl = new AbortController();
10520
10521
  let timer;
@@ -13981,9 +13982,6 @@ init_logging();
13981
13982
  init_logging();
13982
13983
  init_models();
13983
13984
 
13984
- // src/voice/progress.ts
13985
- init_logging();
13986
-
13987
13985
  // src/voice/convoTree.ts
13988
13986
  init_logging();
13989
13987
 
@@ -14243,10 +14241,13 @@ Answer only the number, or "none".`;
14243
14241
  }
14244
14242
 
14245
14243
  // src/voice/progress.ts
14244
+ init_logging();
14246
14245
  var log28 = forComponent("Progress");
14247
14246
  var HE_RESULT_CLAIM = /(?:^|\s)(?:מצאתי|סיימתי|בוצע|הנה|אין)(?=\s|$|[.,!…])/u;
14247
+ var PLUMBING = /toolsearch|^mcp__.*__(list_commands|call_info)$/;
14248
+ var isPlumbing = (tool) => PLUMBING.test(tool.toLowerCase());
14248
14249
  var TOPICS = [
14249
- [/toolsearch|^mcp__.*__(list_commands|call_info)$/, null],
14250
+ [PLUMBING, null],
14250
14251
  // tool plumbing — no line (and no generated one)
14251
14252
  [/kanban|board|card|\bkb\b|list_cards/, "board"],
14252
14253
  [/mail|gmail|inbox/, "mail"],
@@ -14328,8 +14329,10 @@ var ProgressOptions = class {
14328
14329
  generateBudgetMs = 800;
14329
14330
  /** Speak only when nothing has been audible for this long. */
14330
14331
  silenceMs = 3e3;
14331
- /** At most one progress line per this long. */
14332
+ /** At most one spoken work line (progress, bridge or aside) per this long. */
14332
14333
  gapMs = 4e3;
14334
+ /** One stage (a tool's topic) gets one line; a stage still running after this long may get another. 0 = 3 × gapMs. */
14335
+ stageRepeatMs = 0;
14333
14336
  now = () => performance.now();
14334
14337
  };
14335
14338
  function argsHint(args) {
@@ -14346,6 +14349,8 @@ var Progress = class {
14346
14349
  said = /* @__PURE__ */ new Set();
14347
14350
  lastAt = -Infinity;
14348
14351
  streamed = false;
14352
+ lines = [];
14353
+ stages = /* @__PURE__ */ new Map();
14349
14354
  get o() {
14350
14355
  return this.options;
14351
14356
  }
@@ -14353,20 +14358,43 @@ var Progress = class {
14353
14358
  turn() {
14354
14359
  this.streamed = false;
14355
14360
  }
14356
- /** MAIN's text streamed this turn: no more progress lines for it. */
14361
+ /** MAIN's text streamed this turn: no more progress lines, until a tool runs again. */
14357
14362
  text() {
14358
14363
  this.streamed = true;
14359
14364
  }
14360
- /** May a line play now? (the caller passes when the user last heard anything) */
14361
- due(lastAudibleAt) {
14362
- const now4 = this.o.now();
14363
- return !this.streamed && now4 - lastAudibleAt >= this.o.silenceMs && now4 - this.lastAt >= this.o.gapMs;
14365
+ /** A tool started: MAIN is working again. */
14366
+ tool() {
14367
+ this.streamed = false;
14364
14368
  }
14365
- /** Mark `line` spoken. */
14366
- spoke(line) {
14367
- this.said.add(norm3(line));
14369
+ /** May a line play now, or `leadMs` from now? (the caller passes when the user last heard anything) */
14370
+ due(lastAudibleAt, leadMs = 0, tool = "") {
14371
+ const now4 = this.o.now(), spoken = tool ? this.stages.get(this.stage(tool)) : void 0;
14372
+ if (spoken !== void 0 && now4 - spoken < (this.o.stageRepeatMs || 3 * this.o.gapMs) - leadMs) return false;
14373
+ return !this.streamed && now4 - lastAudibleAt >= this.o.silenceMs - leadMs && now4 - this.lastAt >= this.o.gapMs - leadMs;
14374
+ }
14375
+ /** Mark `line` spoken, for `tool`'s stage. */
14376
+ spoke(line, tool = "") {
14377
+ this.note(line);
14378
+ this.heard();
14379
+ if (tool) this.stages.set(this.stage(tool), this.o.now());
14380
+ }
14381
+ /** A work line played from elsewhere (a bridge, an aside): the same thought, so the gap counts from it. */
14382
+ heard() {
14368
14383
  this.lastAt = this.o.now();
14369
14384
  }
14385
+ stage(tool) {
14386
+ const n = tool.toLowerCase();
14387
+ return TOPICS.find(([re]) => re.test(n))?.[1] ?? n;
14388
+ }
14389
+ /** A line the user heard from elsewhere (an aside): progress never repeats it. */
14390
+ note(line) {
14391
+ this.said.add(norm3(line));
14392
+ this.lines = [...this.lines, line].slice(-6);
14393
+ }
14394
+ /** The last lines heard, for a writer to avoid. */
14395
+ get recent() {
14396
+ return [...this.lines];
14397
+ }
14370
14398
  /** `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). */
14371
14399
  async line(tool, args, lang, about = "") {
14372
14400
  if (this.streamed) return "";
@@ -14387,7 +14415,7 @@ var Progress = class {
14387
14415
  r(null);
14388
14416
  }, this.o.generateBudgetMs);
14389
14417
  });
14390
- const raw = await Promise.race([this.o.generate(tool, [about.slice(-80), argsHint(args)].filter(Boolean).join("; "), lang, ac.signal).catch((e) => {
14418
+ const raw = await Promise.race([this.o.generate(tool, [about.slice(-80), argsHint(args)].filter(Boolean).join("; "), lang, ac.signal, this.lines).catch((e) => {
14391
14419
  log28.warn(`progress line failed: ${e?.message ?? e}`);
14392
14420
  return null;
14393
14421
  }), late]).finally(() => clearTimeout(t));
@@ -14397,8 +14425,31 @@ var Progress = class {
14397
14425
  }
14398
14426
  };
14399
14427
  function fastProgressGenerator(ai, model, gender) {
14400
- return async (tool, hint, lang, signal) => {
14401
- 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.`;
14428
+ return async (tool, hint, lang, signal, avoid = []) => {
14429
+ 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.`;
14430
+ const reasoning = /gpt-oss/.test(model) ? { providerOptions: { reasoning_effort: "low" } } : {};
14431
+ const r = await ai.chat({ model, messages: [{ role: "user", content: prompt }], stream: false, signal, maxTokens: 150, ...reasoning });
14432
+ return r.content ?? "";
14433
+ };
14434
+ }
14435
+ var WORK_CLAIM = /\b(?:none|nothing|nobody|zero|\d+|one|two|three|four|five|many|several|some|looks like|seems|turns out|apparently|it'?s|they'?re|there'?s)\b|(?:^|\s)(?:כלום|אף|נראה)(?=\s|$|[.,!…])/iu;
14436
+ var claimsResult = (l) => RESULT_CLAIM_RE.test(l) || HE_RESULT_CLAIM.test(l) || WORK_CLAIM.test(l);
14437
+ function cleanWorkReply(raw, lang) {
14438
+ const l = cleanLine(raw.replace(/^\s*(?:stop|work)\s*:/i, ""), lang, 8);
14439
+ return claimsResult(l) ? "" : l;
14440
+ }
14441
+ function workAck(raw, lang, gender = "") {
14442
+ const l = cleanLine(raw.replace(/^\s*(?:stop|work)\s*:/i, ""), lang, 8);
14443
+ if (!l || !claimsResult(l)) return "";
14444
+ return (PROGRESS_TEMPLATES[gender ? `${lang}-${gender[0]}` : lang] ?? PROGRESS_TEMPLATES[lang])?.delegate[1] ?? "";
14445
+ }
14446
+ function fastWorkReply(ai, model, gender, persona = "") {
14447
+ return async (c, signal) => {
14448
+ const prompt = `${persona ? `Who you are on this call:
14449
+ ${persona.slice(0, 500)}
14450
+
14451
+ ` : ""}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)}"
14452
+ 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 and cannot know any: only acknowledge the work, never say what was or was not found, not even "nothing yet" or "none so far". ` + (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.`;
14402
14453
  const reasoning = /gpt-oss/.test(model) ? { providerOptions: { reasoning_effort: "low" } } : {};
14403
14454
  const r = await ai.chat({ model, messages: [{ role: "user", content: prompt }], stream: false, signal, maxTokens: 150, ...reasoning });
14404
14455
  return r.content ?? "";
@@ -14568,6 +14619,11 @@ var SecondChannelOptions = class {
14568
14619
  random = Math.random;
14569
14620
  };
14570
14621
  var words3 = (s) => s.trim() ? s.trim().split(/\s+/) : [];
14622
+ var HESITATION = /^(?:a+y*|a+i|e+[hr]*|erm+|u+[hm]*|h?m+|o+h+|ooh|like|so|well|אה+|אמ+|אה?ם+|ממ+)$/u;
14623
+ var onlyHesitation = (s) => {
14624
+ const w = words3(s).slice(-3).map((x) => x.toLowerCase().replace(/[^\p{L}]/gu, "")).filter(Boolean);
14625
+ return !!w.length && w.every((x) => HESITATION.test(x));
14626
+ };
14571
14627
  function withBudget(p, ms, what, ac) {
14572
14628
  let t;
14573
14629
  const late = new Promise((r) => {
@@ -15000,7 +15056,7 @@ var SecondChannel = class {
15000
15056
  this.breathTimer = setTimeout(() => {
15001
15057
  this.breathTimer = null;
15002
15058
  const n = words3(t).length, tree = this.tree;
15003
- if (n < this.o.backchannelMinWords || this.heard !== t || this.leafTimer || this.picking || tree.empty || this.leafGated()) return;
15059
+ if (!this.backchannelMoment(n, true) || this.heard !== t || this.leafTimer || this.picking || tree.empty || this.leafGated()) return;
15004
15060
  this.picking = true;
15005
15061
  const ac = new AbortController(), nodes = tree.nodes.filter((x) => x.moment !== "ask"), lines = nodes.map((x, i) => ({ i, line: x.line, when: x.when }));
15006
15062
  void withBudget(this.o.pickLeaf(t, lines, ac.signal), this.o.pickBudgetMs, "breath pick", ac).then((i) => {
@@ -15062,6 +15118,7 @@ var SecondChannel = class {
15062
15118
  * sentence end only deep into a story — a short request's end is the end of their turn (replay: "mm-hm" right after
15063
15119
  * "Check hacker news for recent news.", just before the final). */
15064
15120
  backchannelMoment(n, breath) {
15121
+ if (!cueWords(this.heard).some(isContent)) return false;
15065
15122
  if (/\?$/.test(this.heard) || pendingQuestion(this.heard)) return false;
15066
15123
  if (this.carried) n += this.carried;
15067
15124
  else if (n < this.o.requestHoldWords && requestOpener(this.heard)) return false;
@@ -15185,6 +15242,7 @@ var SecondChannel = class {
15185
15242
  return p;
15186
15243
  }
15187
15244
  say(kind, text) {
15245
+ if (kind === "backchannel" && onlyHesitation(this.heard)) return false;
15188
15246
  let r;
15189
15247
  try {
15190
15248
  r = this.o.speak(text, kind);
@@ -15281,6 +15339,14 @@ var VoiceCallOptions = class {
15281
15339
  /** Host aborts a running MAIN turn (barge-in). */
15282
15340
  abortTurn = () => {
15283
15341
  };
15342
+ /** Host keeps `note` for MAIN's next turn. With it (and `workReply`), what the user says while MAIN works is answered at
15343
+ * once by the fast model and MAIN is not aborted or restarted: a remark reaches MAIN as this note, a change or addition
15344
+ * runs as MAIN's next turn. Not injected mid-run: that makes MAIN run again just to hear a remark. Absent: such a line
15345
+ * barges in as before. */
15346
+ noteTurn;
15347
+ /** Writes that immediate answer (fastWorkReply). */
15348
+ workReply;
15349
+ workReplyBudgetMs = 1500;
15284
15350
  /** Host TTS (fill lane): resolves when played; `false` = could not play. */
15285
15351
  speak = () => false;
15286
15352
  /** Stop the in-flight fill line now (barge-in only). */
@@ -15313,6 +15379,13 @@ var VoiceCallOptions = class {
15313
15379
  now = () => performance.now();
15314
15380
  /** An aside (aside()) needs this long since the last one — a cue answered every time sounds scripted. */
15315
15381
  asideGapMs = 8e3;
15382
+ /** 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
15383
+ * not started. Later ones are a new thing said over the work. */
15384
+ resumeMs = 3e3;
15385
+ /** Progress waits while the user spoke this recently. */
15386
+ userQuietMs = 800;
15387
+ /** A progress line starts being written this long before it is due. */
15388
+ progressLeadMs = 1e3;
15316
15389
  };
15317
15390
  var VoiceCall = class {
15318
15391
  constructor(options) {
@@ -15348,7 +15421,7 @@ var VoiceCall = class {
15348
15421
  cancel: () => o.cancel(),
15349
15422
  now: o.now
15350
15423
  });
15351
- 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 });
15424
+ 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 });
15352
15425
  this.judge = new OverlapJudge({ ...ai ? { model: overlapModelFor(ai) } : {}, now: o.now });
15353
15426
  this.gate = new TurnGate({
15354
15427
  now: o.now,
@@ -15375,6 +15448,7 @@ var VoiceCall = class {
15375
15448
  earlyHeard = null;
15376
15449
  // a fill line heard while that turn was still held
15377
15450
  progressTimer = null;
15451
+ lastUserAt = -Infinity;
15378
15452
  ended = false;
15379
15453
  get o() {
15380
15454
  return this.options;
@@ -15387,6 +15461,11 @@ var VoiceCall = class {
15387
15461
  get turn() {
15388
15462
  return this.cur && { id: this.cur.id, text: this.cur.text, running: this.cur.running };
15389
15463
  }
15464
+ /** MAIN is on this turn and has the floor to itself: nothing the user says now stops it, unless they ask it to. */
15465
+ get working() {
15466
+ const c = this.cur;
15467
+ return !!c && c.running && !c.closed && (!c.worded || c.open > 0);
15468
+ }
15390
15469
  /** The host STT's language (BCP-47): the current language until the user's words say otherwise. */
15391
15470
  setLanguage(tag) {
15392
15471
  const l = primaryLang(tag);
@@ -15399,13 +15478,16 @@ var VoiceCall = class {
15399
15478
  if (this.ended || !text.trim()) return;
15400
15479
  if (typeof aec === "boolean") this.fill.options.aec = aec;
15401
15480
  this.gate.speaking();
15402
- const c = this.cur;
15403
- if (this.o.mode === "live" && c?.running && !c.worded && newWords(text, c.text) >= 2) {
15481
+ this.lastUserAt = this.o.now();
15482
+ const c = this.cur, steers = !!this.o.noteTurn && this.working;
15483
+ const resumed = !steers || c.marks.mainFirst === void 0 && this.o.now() - c.at < this.o.resumeMs;
15484
+ if (this.o.mode === "live" && c?.running && !c.worded && resumed && newWords(text, c.text) >= 2) {
15404
15485
  log30.debug("user resumed before the reply \u2014 yielded");
15405
- this.barge();
15486
+ this.giveWay(c);
15406
15487
  this.gate.reopen(c.id);
15407
15488
  }
15408
15489
  this.follow(text);
15490
+ if (this.o.noteTurn && this.working) return;
15409
15491
  this.fill.onInterim(text);
15410
15492
  }
15411
15493
  /** STT final → TurnGate. `lastWordAgoMs`: how long ago the user's last word of it ended (the STT's word clock,
@@ -15464,6 +15546,8 @@ var VoiceCall = class {
15464
15546
  if (!c.streamed) {
15465
15547
  c.streamed = true;
15466
15548
  this.gate.replyStarted(c.id);
15549
+ }
15550
+ if (c.open <= 0) {
15467
15551
  this.progress.text();
15468
15552
  this.clearProgress();
15469
15553
  }
@@ -15472,9 +15556,17 @@ var VoiceCall = class {
15472
15556
  case "tool":
15473
15557
  c.marks.mainFirst ??= this.o.now();
15474
15558
  this.gate.replyStarted(c.id);
15475
- void this.onTool(c, ev.name ?? "", ev.args);
15559
+ c.open++;
15560
+ c.streamed = false;
15561
+ if (ev.name && !isPlumbing(ev.name)) {
15562
+ c.tool = ev.name;
15563
+ c.args = ev.args;
15564
+ }
15565
+ this.progress.tool();
15566
+ this.beat(c);
15476
15567
  return;
15477
15568
  case "tool_done":
15569
+ c.open = Math.max(0, c.open - 1);
15478
15570
  return;
15479
15571
  case "audio": {
15480
15572
  const at = this.o.now() + Math.max(0, inMs);
@@ -15496,11 +15588,28 @@ var VoiceCall = class {
15496
15588
  this.clearProgress();
15497
15589
  this.fill.onAgentState("idle");
15498
15590
  if (c.worded) this.close(c, "answered");
15591
+ if (c.next) {
15592
+ const next = c.next;
15593
+ c.next = void 0;
15594
+ this.onTurn(next);
15595
+ }
15499
15596
  return;
15500
15597
  }
15501
15598
  }
15502
15599
  /** The user barged in (hard): cut fill + MAIN audio (the host cuts its own), abort a running MAIN turn. */
15503
15600
  barge() {
15601
+ if (this.o.noteTurn && this.working) {
15602
+ this.fill.onBargeIn();
15603
+ try {
15604
+ this.o.cancel();
15605
+ } catch (e) {
15606
+ log30.warn(`cancel threw: ${e.message}`);
15607
+ }
15608
+ return;
15609
+ }
15610
+ this.giveWay();
15611
+ }
15612
+ giveWay(c = this.cur) {
15504
15613
  this.fill.onBargeIn();
15505
15614
  this.clearProgress();
15506
15615
  try {
@@ -15508,11 +15617,11 @@ var VoiceCall = class {
15508
15617
  } catch (e) {
15509
15618
  log30.warn(`cancel threw: ${e.message}`);
15510
15619
  }
15511
- if (this.cur?.running) {
15512
- this.cur.running = false;
15513
- this.o.abortTurn(this.cur.id);
15620
+ if (c?.running) {
15621
+ c.running = false;
15622
+ this.o.abortTurn(c.id);
15514
15623
  }
15515
- if (this.cur && !this.cur.worded) this.cur.end ??= "cut";
15624
+ if (c && !c.worded) c.end ??= "cut";
15516
15625
  }
15517
15626
  /** Soft barge-in: keep (a listener cue — no turn) or cut (→ barge()). */
15518
15627
  async overlap(text) {
@@ -15558,6 +15667,7 @@ var VoiceCall = class {
15558
15667
  if (l) this.fill.setLanguage(l, true);
15559
15668
  }
15560
15669
  onTurn(t) {
15670
+ if (this.o.noteTurn && this.o.workReply && t.kind !== "replace" && this.working && !isClosing(t.text)) return void this.overWork(this.cur, t);
15561
15671
  const now4 = this.o.now(), lastWord = this.lastWordAt !== null;
15562
15672
  const at = this.lastWordAt ?? this.firstFinalAt ?? now4;
15563
15673
  const finalAt = this.finalAt || now4;
@@ -15566,7 +15676,7 @@ var VoiceCall = class {
15566
15676
  if (this.cur) this.close(this.cur, t.replaces === this.cur.id ? "replaced" : void 0);
15567
15677
  this.follow(t.text);
15568
15678
  this.clearProgress();
15569
- 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" };
15679
+ 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 };
15570
15680
  const early = this.earlyHeard;
15571
15681
  this.earlyHeard = null;
15572
15682
  if (early && early.at >= at) {
@@ -15598,12 +15708,14 @@ var VoiceCall = class {
15598
15708
  }
15599
15709
  if (r === false) return false;
15600
15710
  this.o.onMetric({ name: "fill", kind, text, lang: this.lang });
15711
+ if (kind !== "backchannel") this.progress.heard();
15601
15712
  const c = this.cur && !this.cur.closed ? this.cur : void 0;
15602
15713
  const forTurn = !!c && (kind === "bridge" || kind === "progress" || kind === "filler" && c.running && !c.sounded);
15603
15714
  if (c && forTurn && c.marks.fill === void 0) {
15604
15715
  c.marks.fill = this.o.now();
15605
15716
  c.fillKind = kind;
15606
15717
  }
15718
+ if (c && kind === "bridge" && this.o.mode === "live" && echoesHeard(text, c.text)) this.beat(c);
15607
15719
  if (this.o.hostAudio) return r;
15608
15720
  this.lastAudibleAt = this.o.now();
15609
15721
  if (c && forTurn) {
@@ -15641,27 +15753,78 @@ var VoiceCall = class {
15641
15753
  for (const k of Object.keys(tl)) if (tl[k] === void 0) delete tl[k];
15642
15754
  this.o.onMetric({ name: "timeline", id: c.id, text: c.text, timeline: tl });
15643
15755
  }
15644
- async onTool(c, name, args) {
15645
- const lang = this.lang;
15646
- if (!lang || c.streamed) return;
15647
- const line = await this.progress.line(name, args, lang, this.o.mode === "live" ? c.text : "");
15648
- if (!line && this.o.mode === "live") this.o.onMetric({ name: "drop", id: c.id, kind: `no progress line: ${name}` });
15649
- if (!line || this.cur !== c || c.streamed || !c.running || this.lang !== lang) return;
15756
+ /** The user spoke while MAIN works: the fast model answers now, the words reach MAIN as a note, MAIN keeps going. */
15757
+ async overWork(c, t) {
15758
+ const at = this.o.now();
15759
+ this.firstFinalAt = null;
15760
+ this.lastWordAt = null;
15761
+ this.follow(t.text);
15762
+ queueMicrotask(() => this.gate.replyStarted(t.id));
15763
+ this.o.onMetric({ name: "turn", id: t.id, kind: "aside", text: t.text });
15764
+ const ac = new AbortController(), late = setTimeout(() => ac.abort(), this.o.workReplyBudgetMs), lang = this.lang;
15765
+ 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) => {
15766
+ log30.warn(`work reply failed \u2014 MAIN only: ${e?.message ?? e}`);
15767
+ return "";
15768
+ }).finally(() => clearTimeout(late)) : "";
15769
+ const ack = workAck(raw, lang ?? "", this.o.gender), line = cleanWorkReply(raw, lang ?? "") || ack, stop = /^\s*stop\s*:/i.test(raw), change = !line || /^\s*work\s*:/i.test(raw);
15770
+ 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.]`;
15771
+ if (line && !this.ended) {
15772
+ for (let i = 0; i < 8 && this.say(line, "aside") === false; i++) {
15773
+ if (i === 7) {
15774
+ this.o.onMetric({ name: "drop", id: t.id, kind: "aside unplayed", text: line });
15775
+ break;
15776
+ }
15777
+ await new Promise((r) => setTimeout(r, 250));
15778
+ }
15779
+ if (this.lastAudibleAt >= at) {
15780
+ this.progress.note(line);
15781
+ this.o.show(line, "aside");
15782
+ }
15783
+ } else if (!line) this.o.onMetric({ name: "drop", id: t.id, kind: "no aside line" });
15784
+ if (stop) {
15785
+ this.o.noteTurn(noted);
15786
+ c.next = void 0;
15787
+ if (this.working && this.cur === c) this.giveWay(c);
15788
+ return;
15789
+ }
15790
+ if (change && this.cur === c && c.running) {
15791
+ c.next = c.next ? { ...c.next, text: `${c.next.text} ${t.text}` } : { ...t, kind: "new" };
15792
+ return;
15793
+ }
15794
+ if (change) return this.onTurn({ ...t, kind: "new" });
15795
+ this.o.noteTurn(noted);
15796
+ }
15797
+ /** While MAIN's tools run: a progress line once nothing was heard for `silenceMs`, then every `gapMs`, written just ahead
15798
+ * so it plays on time. Ticks are local (no network) and stop with the turn. */
15799
+ beat(c) {
15650
15800
  this.clearProgress();
15651
- const tryNow = () => {
15801
+ const tick = () => {
15652
15802
  this.progressTimer = null;
15653
- if (this.ended || this.cur !== c || c.streamed || !c.running) return;
15654
- if (!this.progress.due(this.lastAudibleAt)) {
15655
- this.progressTimer = setTimeout(tryNow, 250);
15656
- return;
15803
+ if (this.ended || this.cur !== c || !c.running || c.streamed && c.open <= 0) return;
15804
+ const lang = this.lang, now4 = this.o.now();
15805
+ if (lang && !c.draft && !c.drafting && now4 - c.draftAt >= 1e3 && this.progress.due(this.lastAudibleAt, this.o.progressLeadMs, c.tool)) {
15806
+ c.drafting = true;
15807
+ void this.progress.line(c.tool, c.args, lang, this.o.mode === "live" ? c.text : "").then((l) => {
15808
+ c.drafting = false;
15809
+ c.draft = l;
15810
+ if (!l) c.draftAt = this.o.now();
15811
+ if (!l && this.o.mode === "live") this.o.onMetric({ name: "drop", id: c.id, kind: `no progress line: ${c.tool}` });
15812
+ else sayDraft();
15813
+ });
15657
15814
  }
15658
- if (this.say(line, "progress") !== false) {
15659
- this.progress.spoke(line);
15815
+ sayDraft();
15816
+ this.progressTimer = setTimeout(tick, 250);
15817
+ };
15818
+ const sayDraft = () => {
15819
+ 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, 0, c.tool)) return;
15820
+ if (this.say(c.draft, "progress") !== false) {
15821
+ this.progress.spoke(c.draft, c.tool);
15660
15822
  this.fill.covered();
15661
- this.o.show(line, "progress");
15823
+ this.o.show(c.draft, "progress");
15824
+ c.draft = "";
15662
15825
  }
15663
15826
  };
15664
- tryNow();
15827
+ tick();
15665
15828
  }
15666
15829
  clearProgress() {
15667
15830
  if (this.progressTimer) {
@@ -17316,7 +17479,7 @@ var VoiceIO = class extends VoiceEngine {
17316
17479
  ...o,
17317
17480
  stt: o.stt ?? new SonioxSTT({ auth: o.sonioxApiKey, source: duplex ?? new NodeMicSource(), terms: o.sttTerms }),
17318
17481
  tts: o.tts ?? new CartesiaTTS({ auth: o.cartesiaApiKey, voiceId: o.cartesiaVoiceId }),
17319
- asideTts: o.tts ? null : new CartesiaTTS({ auth: o.cartesiaApiKey, voiceId: o.cartesiaVoiceId }),
17482
+ asideTts: o.tts ? o.asideTts : new CartesiaTTS({ auth: o.cartesiaApiKey, voiceId: o.cartesiaVoiceId }),
17320
17483
  // asides (VoiceCall.aside): a 2nd client — the reply's context may still stream
17321
17484
  player: o.player ?? duplex ?? new Player(),
17322
17485
  bargeRmsMult: Number(process.env.BARGE_RMS_MULT || o.bargeRmsMult),
@@ -17394,8 +17557,89 @@ function fakeVoiceParts(uttFile) {
17394
17557
  }, write() {
17395
17558
  }, drainMs: () => 0, playedMs: () => 0, kill() {
17396
17559
  } };
17560
+ if (process.env.AGENTX_VOICE_FAKE_TAPE) return { stt, ...fakeTape(process.env.AGENTX_VOICE_FAKE_TAPE, said) };
17397
17561
  return { stt, tts, player };
17398
17562
  }
17563
+ function fakeTape(heardFile, saidFile) {
17564
+ const CHAR = TTS_SAMPLE_RATE * 2 * 60 / 1e3;
17565
+ const makeTts = () => {
17566
+ const t = {
17567
+ connect() {
17568
+ },
17569
+ newContext() {
17570
+ },
17571
+ cancel() {
17572
+ },
17573
+ close() {
17574
+ },
17575
+ onAudio: (_) => {
17576
+ },
17577
+ onDone: () => {
17578
+ },
17579
+ speak(text, more = true) {
17580
+ if (saidFile) appendFileSync(saidFile, JSON.stringify({ t: Date.now(), speak: text }) + "\n");
17581
+ const pcm = new Uint8Array(text.length * CHAR), v = new DataView(pcm.buffer);
17582
+ for (let i = 0; i < text.length; i++) for (let b = 0; b < CHAR; b += 2) v.setUint16(i * CHAR + b, text.charCodeAt(i), true);
17583
+ setTimeout(() => {
17584
+ t.onAudio(pcm);
17585
+ if (!more) t.end();
17586
+ }, 5);
17587
+ },
17588
+ end() {
17589
+ setTimeout(() => t.onDone?.(), 20);
17590
+ }
17591
+ };
17592
+ return t;
17593
+ };
17594
+ let q2 = new Uint8Array(0), played = 0, paused = false, last = -1;
17595
+ const mark = (m) => appendFileSync(heardFile, m);
17596
+ const flush = () => {
17597
+ q2 = new Uint8Array(0);
17598
+ played = 0;
17599
+ paused = false;
17600
+ last = -1;
17601
+ };
17602
+ setInterval(() => {
17603
+ if (paused || !q2.length) return;
17604
+ const n = Math.min(TTS_SAMPLE_RATE * 2 * 20 / 1e3, q2.length);
17605
+ let out = "", p = played;
17606
+ for (let i = 0; i < n; i += 2, p += 2) if (Math.floor(p / CHAR) !== last) {
17607
+ last = Math.floor(p / CHAR);
17608
+ out += String.fromCharCode(q2[i] | q2[i + 1] << 8);
17609
+ }
17610
+ q2 = q2.subarray(n);
17611
+ played += n;
17612
+ if (out) mark(out);
17613
+ }, 20).unref();
17614
+ const player = {
17615
+ markTurn() {
17616
+ if (q2.length) mark("\u2702");
17617
+ flush();
17618
+ mark("\n");
17619
+ },
17620
+ write(c) {
17621
+ const a = new Uint8Array(q2.length + c.length);
17622
+ a.set(q2);
17623
+ a.set(c, q2.length);
17624
+ q2 = a;
17625
+ },
17626
+ drainMs: () => q2.length / (TTS_SAMPLE_RATE * 2 / 1e3),
17627
+ playedMs: () => played / (TTS_SAMPLE_RATE * 2 / 1e3),
17628
+ kill() {
17629
+ if (q2.length) mark("\u2702");
17630
+ flush();
17631
+ },
17632
+ pause() {
17633
+ paused = true;
17634
+ mark("\u23F8");
17635
+ },
17636
+ resume() {
17637
+ if (paused) mark("\u25B6");
17638
+ paused = false;
17639
+ }
17640
+ };
17641
+ return { tts: makeTts(), asideTts: makeTts(), player };
17642
+ }
17399
17643
 
17400
17644
  // src/voice/diagSink.ts
17401
17645
  init_logging();
@@ -17454,6 +17698,60 @@ var JsonlDiagSink = class {
17454
17698
  }
17455
17699
  };
17456
17700
 
17701
+ // src/voice/spokenFilter.ts
17702
+ var SPOKEN_TURN = "Your reply is spoken aloud: answer in one to three short plain sentences \u2014 no markdown, headings, lists, links or code, and no sources line even when a tool asks you to cite them (offer to send them if asked) \u2014 and offer more detail only if asked.";
17703
+ var HEADS = ["sources", "source", "references", "citations", "links"];
17704
+ var MARKS = String.raw`[#*_>\s-]*`;
17705
+ var SOURCES = new RegExp(String.raw`(^|\n|[.!?…]\s)${MARKS}(?:${HEADS.join("|")})${MARKS}[::]`, "i");
17706
+ var LINK = /\[([^\]\n]*)\]\([^)\s]*\)/g;
17707
+ var URL_RE = /(?:https?:\/\/|www\.)[^\s)\]]+/gi;
17708
+ var OPEN_LINK = /\[[^\]\n]*(?:\](?:\([^)\s]*)?)?$/;
17709
+ var OPEN_URL = /(?:^|\s)(?:h|ht|htt|http|https|https?:|https?:\/?|w|ww|www|(?:https?:\/\/|www\.)\S*)$/i;
17710
+ var OPEN_HEAD = new RegExp(String.raw`(?:^|\n|[.!?…]\s)${MARKS}([a-z]*)${MARKS}$`, "i");
17711
+ var SpokenFilter = class {
17712
+ held = "";
17713
+ ended = false;
17714
+ boundary = true;
17715
+ // what was spoken so far stops at a sentence or line end
17716
+ /** The next delta's speakable part ('' = nothing yet). */
17717
+ push(delta) {
17718
+ if (this.ended) return "";
17719
+ const text = this.held + delta;
17720
+ const sourced = this.scrub(text).match(SOURCES);
17721
+ if (sourced && (sourced[1] || this.boundary)) {
17722
+ this.ended = true;
17723
+ this.held = "";
17724
+ return this.spoke(this.scrub(text).slice(0, sourced.index + sourced[1].trimEnd().length));
17725
+ }
17726
+ const hold = this.holdFrom(text);
17727
+ this.held = text.slice(hold);
17728
+ return this.spoke(this.scrub(text.slice(0, hold)));
17729
+ }
17730
+ /** The reply ended: what was held, as far as it may be said. */
17731
+ end() {
17732
+ const rest = this.held;
17733
+ this.held = "";
17734
+ return this.ended ? "" : this.scrub(rest);
17735
+ }
17736
+ scrub(s) {
17737
+ return s.replace(LINK, "$1").replace(URL_RE, "");
17738
+ }
17739
+ spoke(s) {
17740
+ if (s.trim()) this.boundary = /[\n.!?…]\s*$/.test(s);
17741
+ return s;
17742
+ }
17743
+ holdFrom(s) {
17744
+ let at = s.length;
17745
+ const link2 = s.match(OPEN_LINK);
17746
+ if (link2) at = Math.min(at, link2.index);
17747
+ const url = s.match(OPEN_URL);
17748
+ if (url) at = Math.min(at, url.index);
17749
+ const head = s.match(OPEN_HEAD);
17750
+ if (head && (head[1] ? HEADS.some((h) => h.startsWith(head[1].toLowerCase())) : /[*_#>-]$/.test(s)) && (head.index > 0 || this.boundary)) at = Math.min(at, head.index);
17751
+ return at;
17752
+ }
17753
+ };
17754
+
17457
17755
  // src/voice/greeting.ts
17458
17756
  init_logging();
17459
17757
  var log37 = forComponent("CallGreeting");
@@ -20883,6 +21181,7 @@ function parseArgs(argv) {
20883
21181
  if (a.duplex && a.plan) throw new Error("--plan is not supported in --duplex (workers are non-interactive; a plan could never be approved)");
20884
21182
  if (a.voiceSystemPrompt && !a.duplex) throw new Error("--voice-system-prompt only applies with --duplex");
20885
21183
  if (a.voicePersona && !a.duplex && !a.channelOnly) throw new Error("--voice-persona only applies with --call or --channel-only");
21184
+ if (a.call) process.env.ENABLE_TOOL_SEARCH ??= "false";
20886
21185
  if (a.voicePersona) process.env.AGENTX_CALL_PERSONA = a.voicePersona;
20887
21186
  if (a.call && a.inputFormat) throw new Error("--call is interactive (mic/TTS here); a headless host runs --channel-only");
20888
21187
  if (a.channelMode && !a.duplexV2 && !a.channelOnly && !a.call) throw new Error("--channel-mode only applies with --duplex-v2 or --channel-only");
@@ -22500,10 +22799,13 @@ async function repl(args, ai, cfg, cwd) {
22500
22799
  let callChain = Promise.resolve();
22501
22800
  let callFastModel = "";
22502
22801
  let callReplyOpen = false;
22802
+ let spoken = new SpokenFilter();
22503
22803
  let greetNote = "";
22504
22804
  const callTurns = /* @__PURE__ */ new Map();
22805
+ const callTools = /* @__PURE__ */ new Set();
22505
22806
  if (args.call) {
22506
22807
  if (args.voice || args.voicePersona) agent.options.systemPrompt += "\n\n" + (args.voicePersona ?? CALL_PROMPT);
22808
+ agent.options.systemPrompt += "\n\n" + CALL_WEB_PROMPT;
22507
22809
  if (args.voiceSystemPrompt) agent.options.systemPrompt += "\n\n" + args.voiceSystemPrompt;
22508
22810
  const fast = callFast(args, ai, cfg);
22509
22811
  for (const n of fast.notes) err(yellow(` \u26A0 ${n}
@@ -22519,7 +22821,16 @@ async function repl(args, ai, cfg, cwd) {
22519
22821
  },
22520
22822
  onMetric: (m) => voiceDiag?.push({ t: performance.now(), ...m, kind: `call_${m.name}`, ...m.kind ? { what: m.kind } : {} }),
22521
22823
  abortTurn: (id) => {
22522
- if (callTurns.get(id)?.running) activeTurn?.abort();
22824
+ if (callTurns.get(id)?.running) {
22825
+ voiceDiag?.push({ t: performance.now(), kind: "call_abort", id });
22826
+ activeTurn?.abort();
22827
+ }
22828
+ },
22829
+ noteTurn: (note) => {
22830
+ greetNote += `
22831
+
22832
+ ${note}`;
22833
+ voiceDiag?.push({ t: performance.now(), kind: "call_note", note });
22523
22834
  },
22524
22835
  sendTurn: (text, { id, replaces }) => {
22525
22836
  const old = replaces ? callTurns.get(replaces) : void 0;
@@ -22529,7 +22840,7 @@ async function repl(args, ai, cfg, cwd) {
22529
22840
  }
22530
22841
  const rec = { skip: false, running: false };
22531
22842
  callTurns.set(id, rec);
22532
- err(`\r\x1B[K ${bold(cyan("\u{1F3A4} \u203A"))} ${text}
22843
+ err(`\r\x1B[0J ${bold(cyan("\u{1F3A4} \u203A"))} ${text}
22533
22844
  `);
22534
22845
  callChain = callChain.then(async () => {
22535
22846
  if (old?.before !== void 0) face.transcript.splice(old.before);
@@ -22541,8 +22852,11 @@ async function repl(args, ai, cfg, cwd) {
22541
22852
  const note = greetNote;
22542
22853
  greetNote = "";
22543
22854
  callReplyOpen = false;
22855
+ spoken = new SpokenFilter();
22544
22856
  try {
22545
- const r = await dispatchLine(text + note, { id });
22857
+ const r = await dispatchLine(`${text}${note}
22858
+
22859
+ ${SPOKEN_TURN}`, { id });
22546
22860
  if (r === "quit") {
22547
22861
  await voiceIO?.awaitIdle();
22548
22862
  editorRef?.abort();
@@ -22562,6 +22876,13 @@ async function repl(args, ai, cfg, cwd) {
22562
22876
  if (e.kind === "text_delta" && typeof e.message === "string") call.main({ type: "text", text: e.message });
22563
22877
  else if (e.kind === "tool_use") call.main({ type: "tool", name: String(e.name ?? ""), args: e.input });
22564
22878
  else if (e.kind === "tool_result") call.main({ type: "tool_done" });
22879
+ else if (e.kind === "tool_activity") {
22880
+ if (!callTools.has(e.id)) {
22881
+ callTools.add(e.id);
22882
+ call.main({ type: "tool", name: String(e.name ?? ""), args: e.input });
22883
+ }
22884
+ if (e.status !== "running") call.main({ type: "tool_done" });
22885
+ }
22565
22886
  if (e.kind === "text_delta" && voiceIO) {
22566
22887
  voiceReasonEnd();
22567
22888
  spinner.stop();
@@ -22569,12 +22890,18 @@ async function repl(args, ai, cfg, cwd) {
22569
22890
  callReplyOpen = true;
22570
22891
  voiceIO.beginSpeech();
22571
22892
  }
22572
- const echo = voiceIO.speakDelta(e.message);
22893
+ const say = spoken.push(e.message);
22894
+ if (!say) return;
22895
+ const echo = voiceIO.speakDelta(say);
22573
22896
  editorRef?.suspend();
22574
22897
  voiceEcho(echo);
22575
22898
  return;
22576
22899
  }
22577
22900
  if (voiceIO && (e.kind === "turn_start" || e.kind === "thinking_delta")) return;
22901
+ if (voiceIO) {
22902
+ voiceEchoEnd();
22903
+ editorRef?.suspend();
22904
+ }
22578
22905
  return notify(e);
22579
22906
  };
22580
22907
  }
@@ -22920,6 +23247,10 @@ ${task}`;
22920
23247
  task = t;
22921
23248
  }
22922
23249
  const r = await runTurn(face, store, session, task, duplex ? void 0 : checkpoints, cwd, sendVia ? (c) => sendVia(c, opts) : routeVia);
23250
+ if (call && voiceIO) {
23251
+ const rest = spoken.end();
23252
+ if (rest) voiceEcho(voiceIO.speakDelta(rest));
23253
+ }
22923
23254
  if (voiceIO) {
22924
23255
  voiceReasonEnd();
22925
23256
  voiceEchoEnd();
@@ -24236,7 +24567,7 @@ ${out}
24236
24567
  // Barge-in also parks any in-flight task's spoken delivery so a superseded topic can't keep talking
24237
24568
  // over the user's new one (debt-after-jokes regression) — the result still lands in the transcript.
24238
24569
  onBargeIn: (phase) => {
24239
- activeTurn?.abort();
24570
+ if (!call?.working) activeTurn?.abort();
24240
24571
  dx?.parkInFlightDeliveries();
24241
24572
  voiceReasonEnd();
24242
24573
  voiceEchoEnd();
@@ -24650,7 +24981,8 @@ function readPersona(file) {
24650
24981
  if (!text) throw new Error(`--voice-persona: ${file} is empty`);
24651
24982
  return text;
24652
24983
  }
24653
- 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.`;
24984
+ 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.`;
24985
+ var CALL_WEB_PROMPT = "For public web pages use WebSearch and WebFetch; touch the user's browser only when they ask for it.";
24654
24986
  var HOLD_RESUME_MS = 4e3;
24655
24987
  var leakDecider = () => voiceDecider();
24656
24988
  function makeSecondChannel(ai, reflexModel, mode = "normal", opts) {
@@ -24688,6 +25020,7 @@ function callFast(args, ai, cfg) {
24688
25020
  mode: args.channelMode,
24689
25021
  gender: args.voiceGender ?? "",
24690
25022
  progress: { generate: fastProgressGenerator(ai, line, args.voiceGender) },
25023
+ workReply: fastWorkReply(ai, line, args.voiceGender, args.voicePersona),
24691
25024
  channel: channelOptions(ai, line, args.voiceGender)
24692
25025
  } };
24693
25026
  }