@livx.cc/agentx 0.99.145 → 0.99.147
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cli.d.ts +2 -1
- package/dist/cli.js +1576 -311
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +7 -4
- package/dist/index.js +1234 -121
- package/dist/index.js.map +1 -1
- package/dist/{secondChannel-BFPgXN2z.d.ts → secondChannel-CJ1eYZKA.d.ts} +117 -0
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -2962,7 +2962,7 @@ async function psGroupCpuSeconds(pgid) {
|
|
|
2962
2962
|
try {
|
|
2963
2963
|
return await winTreeCpuSeconds(pgid);
|
|
2964
2964
|
} catch (e) {
|
|
2965
|
-
|
|
2965
|
+
log32.debug(`cpu probe failed for ${pgid}: ${e}`);
|
|
2966
2966
|
return void 0;
|
|
2967
2967
|
}
|
|
2968
2968
|
}
|
|
@@ -2971,7 +2971,7 @@ async function psGroupCpuSeconds(pgid) {
|
|
|
2971
2971
|
const out = await new Promise((res, rej) => execFile5("ps", ["-A", "-o", "pid=,ppid=,pgid=,time="], { timeout: 5e3, env: { ...process.env, LC_ALL: "C" } }, (e, so) => e ? rej(e) : res(String(so))));
|
|
2972
2972
|
return sumTreeCpu(out, pgid);
|
|
2973
2973
|
} catch (e) {
|
|
2974
|
-
|
|
2974
|
+
log32.debug(`ps cpu probe failed for group ${pgid}: ${e}`);
|
|
2975
2975
|
return void 0;
|
|
2976
2976
|
}
|
|
2977
2977
|
}
|
|
@@ -3121,7 +3121,7 @@ function makeRealShellTool(options) {
|
|
|
3121
3121
|
proc.stderr?.on("data", collect);
|
|
3122
3122
|
proc.on("error", (err2) => {
|
|
3123
3123
|
if (err2?.name === "AbortError" || ctl.signal.aborted) return finish(reasonFor(timedOut, timeoutMs, clean2(out)));
|
|
3124
|
-
|
|
3124
|
+
log32.debug("shell spawn error", err2);
|
|
3125
3125
|
finish(`[exit 1] ${err2?.message ?? err2}${out ? "\n" + clean2(out) : ""}`);
|
|
3126
3126
|
});
|
|
3127
3127
|
proc.on("close", (code) => {
|
|
@@ -3200,7 +3200,7 @@ ${clean2(out) || "(no output yet)"}`;
|
|
|
3200
3200
|
}
|
|
3201
3201
|
];
|
|
3202
3202
|
}
|
|
3203
|
-
var
|
|
3203
|
+
var log32, clean2, DETACHED, shellArgv, SECRET_ENV_RE, _spawn, ShellJobRegistry, shellStatus, NO_JOB2;
|
|
3204
3204
|
var init_tools_shell = __esm({
|
|
3205
3205
|
"src/tools.shell.ts"() {
|
|
3206
3206
|
"use strict";
|
|
@@ -3210,7 +3210,7 @@ var init_tools_shell = __esm({
|
|
|
3210
3210
|
init_shell_sandbox();
|
|
3211
3211
|
init_tools_jobs();
|
|
3212
3212
|
init_platform();
|
|
3213
|
-
|
|
3213
|
+
log32 = forComponent("shell");
|
|
3214
3214
|
clean2 = (s) => truncateOutput(redactSecrets(s.replace(/\n+$/, "")));
|
|
3215
3215
|
DETACHED = { stdio: ["ignore", "pipe", "pipe"], ...DETACH_OPTIONS };
|
|
3216
3216
|
shellArgv = (sh, command) => ({ bin: sh.bin, args: sh.args(command), ...sh.spawnOptions ? { spawnOptions: sh.spawnOptions } : {} });
|
|
@@ -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
|
|
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
|
|
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 === "
|
|
7502
|
+
var cartesiaTag = (t) => t.kind === "emotion" ? `<emotion value="${t.value}"/>` : t.value;
|
|
7387
7503
|
function renderEmotions(text, opts = { show: true }) {
|
|
7388
|
-
|
|
7389
|
-
|
|
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 =
|
|
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
|
-
|
|
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 =
|
|
7458
|
-
this.pending =
|
|
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
|
-
|
|
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) =>
|
|
7971
|
-
|
|
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([
|
|
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.
|
|
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 =
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
8703
|
-
this.
|
|
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(
|
|
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
|
-
|
|
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
|
-
|
|
8879
|
-
|
|
8880
|
-
|
|
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
|
-
|
|
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
|
|
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 =
|
|
9437
|
+
this.bargedBeforeDispatch = sup ? this.bargedBeforeDispatch || this.bargedSinceDispatch : this.bargedSinceDispatch;
|
|
9003
9438
|
this.bargedSinceDispatch = false;
|
|
9004
|
-
this.dispatchRefinal =
|
|
9005
|
-
if (!
|
|
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(
|
|
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 &&
|
|
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.
|
|
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)
|
|
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)
|
|
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
|
-
|
|
10028
|
+
const partials = (m.tokens ?? []).filter((t) => !t.is_final && t.text !== "<end>");
|
|
10029
|
+
this.partialText = partials.map((t) => t.text).join("");
|
|
10030
|
+
this.partialLangChars = {};
|
|
10031
|
+
for (const t of partials) this.countLang(this.partialLangChars, t);
|
|
10032
|
+
if (this.options.diarization) this.partialToks = partials;
|
|
9412
10033
|
const combined = this.finalText + this.partialText;
|
|
9413
10034
|
if (combined !== this.lastCombined) {
|
|
9414
10035
|
this.lastCombined = combined;
|
|
9415
10036
|
this.lastChangeAt = now();
|
|
9416
10037
|
if (!this.firstTokenAt && combined.trim()) this.firstTokenAt = now();
|
|
9417
10038
|
}
|
|
9418
|
-
this.onPartial(combined);
|
|
10039
|
+
this.onPartial(this.stray(combined), this.turns(true));
|
|
9419
10040
|
if (endpoint && this.finalText.trim()) {
|
|
9420
10041
|
const utterance = this.finalText.trim();
|
|
9421
10042
|
if (this.firstTokenAt) log15.debug(`stt: ${Math.round(now() - this.firstTokenAt)}ms first-token\u2192endpoint, "${utterance.slice(0, 60)}"`);
|
|
10043
|
+
const language = this.language(false);
|
|
10044
|
+
const turns = this.turns(false), ago = this.lastWordAgoMs();
|
|
9422
10045
|
this.reset();
|
|
9423
|
-
this.onUtterance(utterance, now());
|
|
10046
|
+
this.onUtterance(this.stray(utterance), now(), language, turns, ago);
|
|
10047
|
+
}
|
|
10048
|
+
}
|
|
10049
|
+
/** How long ago (ms, now) the utterance's last spoken word ended: the audio sent so far minus that token's end_ms on
|
|
10050
|
+
* Soniox's stream clock — the user's real last word, before Soniox's latency and the silence endpoint. */
|
|
10051
|
+
lastWordAgoMs() {
|
|
10052
|
+
if (!this.lastWordEndMs) return void 0;
|
|
10053
|
+
return Math.max(0, Math.round(this.sentBytes / 2 / STT_SAMPLE_RATE * 1e3 - this.lastWordEndMs));
|
|
10054
|
+
}
|
|
10055
|
+
stray(text) {
|
|
10056
|
+
return this.options.dropStrayScripts ? dropStrayScripts(text, this.options.languageHints) : text;
|
|
10057
|
+
}
|
|
10058
|
+
countLang(acc, t) {
|
|
10059
|
+
if (typeof t.language === "string" && t.language && String(t.text).trim()) acc[t.language] = (acc[t.language] ?? 0) + String(t.text).trim().length;
|
|
10060
|
+
}
|
|
10061
|
+
/** Dominant language (most characters) of the current utterance; undefined when Soniox tagged none. */
|
|
10062
|
+
language(withPartials) {
|
|
10063
|
+
const acc = { ...this.finalLangChars };
|
|
10064
|
+
if (withPartials) for (const [l, n] of Object.entries(this.partialLangChars)) acc[l] = (acc[l] ?? 0) + n;
|
|
10065
|
+
let best;
|
|
10066
|
+
let max = 0;
|
|
10067
|
+
for (const [l, n] of Object.entries(acc)) if (n > max) {
|
|
10068
|
+
best = l;
|
|
10069
|
+
max = n;
|
|
9424
10070
|
}
|
|
10071
|
+
return best;
|
|
10072
|
+
}
|
|
10073
|
+
/** Per-speaker turns of the current utterance (diarization only; undefined otherwise or when Soniox tagged no speaker). */
|
|
10074
|
+
turns(withPartials) {
|
|
10075
|
+
if (!this.options.diarization) return void 0;
|
|
10076
|
+
const words4 = sonioxWords(withPartials ? [...this.finalToks, ...this.partialToks] : this.finalToks);
|
|
10077
|
+
return words4.some((w) => w.spk != null) ? speakerRuns(words4) : void 0;
|
|
9425
10078
|
}
|
|
9426
10079
|
reset() {
|
|
10080
|
+
this.finalToks = [];
|
|
10081
|
+
this.partialToks = [];
|
|
10082
|
+
this.finalLangChars = {};
|
|
10083
|
+
this.partialLangChars = {};
|
|
9427
10084
|
this.finalText = "";
|
|
9428
10085
|
this.partialText = "";
|
|
9429
10086
|
this.lastCombined = "";
|
|
9430
10087
|
this.firstTokenAt = 0;
|
|
10088
|
+
this.lastWordEndMs = 0;
|
|
9431
10089
|
}
|
|
9432
10090
|
stop() {
|
|
9433
10091
|
this.stopped = true;
|
|
@@ -9438,6 +10096,65 @@ var SonioxSTT = class {
|
|
|
9438
10096
|
this.ws?.close();
|
|
9439
10097
|
}
|
|
9440
10098
|
};
|
|
10099
|
+
var SCRIPTS = {
|
|
10100
|
+
he: ["Hebrew"],
|
|
10101
|
+
yi: ["Hebrew"],
|
|
10102
|
+
ar: ["Arabic"],
|
|
10103
|
+
fa: ["Arabic"],
|
|
10104
|
+
ur: ["Arabic"],
|
|
10105
|
+
ru: ["Cyrillic"],
|
|
10106
|
+
uk: ["Cyrillic"],
|
|
10107
|
+
bg: ["Cyrillic"],
|
|
10108
|
+
sr: ["Cyrillic"],
|
|
10109
|
+
el: ["Greek"],
|
|
10110
|
+
hi: ["Devanagari"],
|
|
10111
|
+
mr: ["Devanagari"],
|
|
10112
|
+
ne: ["Devanagari"],
|
|
10113
|
+
bn: ["Bengali"],
|
|
10114
|
+
pa: ["Gurmukhi"],
|
|
10115
|
+
gu: ["Gujarati"],
|
|
10116
|
+
ta: ["Tamil"],
|
|
10117
|
+
te: ["Telugu"],
|
|
10118
|
+
th: ["Thai"],
|
|
10119
|
+
ja: ["Hiragana", "Katakana", "Han"],
|
|
10120
|
+
zh: ["Han"],
|
|
10121
|
+
ko: ["Hangul"],
|
|
10122
|
+
ka: ["Georgian"],
|
|
10123
|
+
hy: ["Armenian"],
|
|
10124
|
+
am: ["Ethiopic"]
|
|
10125
|
+
};
|
|
10126
|
+
var SCRIPT_RES = [.../* @__PURE__ */ new Set(["Latin", ...Object.values(SCRIPTS).flat(), "Gurmukhi", "Devanagari"])].map((sc) => [sc, new RegExp(`\\p{Script=${sc}}`, "u")]);
|
|
10127
|
+
var LETTER = new RegExp("\\p{L}", "u");
|
|
10128
|
+
function mainScript(text) {
|
|
10129
|
+
const n = {};
|
|
10130
|
+
for (const ch of text) {
|
|
10131
|
+
if (!LETTER.test(ch)) continue;
|
|
10132
|
+
const sc = SCRIPT_RES.find(([, re]) => re.test(ch))?.[0] ?? "Other";
|
|
10133
|
+
n[sc] = (n[sc] ?? 0) + 1;
|
|
10134
|
+
}
|
|
10135
|
+
return Object.entries(n).sort((a, b) => b[1] - a[1])[0]?.[0] ?? "";
|
|
10136
|
+
}
|
|
10137
|
+
function dropStrayScripts(text, hints) {
|
|
10138
|
+
const main2 = mainScript(text), foreign = foreignScript(hints, main2 && main2 !== "Other" ? [main2] : []);
|
|
10139
|
+
if (!/\S/.test(text)) return text;
|
|
10140
|
+
const lead2 = /^\s/.test(text) ? " " : "", trail = /\s$/.test(text) ? " " : "";
|
|
10141
|
+
const kept = text.split(/\s+/).filter((w) => w && !foreign(w));
|
|
10142
|
+
return kept.length ? lead2 + kept.join(" ") + trail : "";
|
|
10143
|
+
}
|
|
10144
|
+
var foreignTests = /* @__PURE__ */ new Map();
|
|
10145
|
+
function foreignScript(hints, also = []) {
|
|
10146
|
+
const key = [...hints, "|", ...also].join(",");
|
|
10147
|
+
let f = foreignTests.get(key);
|
|
10148
|
+
if (f) return f;
|
|
10149
|
+
const scripts = [.../* @__PURE__ */ new Set(["Common", "Inherited", "Latin", ...also, ...hints.flatMap((h) => SCRIPTS[h.slice(0, 2).toLowerCase()] ?? [])])];
|
|
10150
|
+
const ok = new RegExp(`^[${scripts.map((sc) => `\\p{Script_Extensions=${sc}}`).join("")}]$`, "u");
|
|
10151
|
+
f = (text) => {
|
|
10152
|
+
for (const ch of text) if (!ok.test(ch)) return true;
|
|
10153
|
+
return false;
|
|
10154
|
+
};
|
|
10155
|
+
foreignTests.set(key, f);
|
|
10156
|
+
return f;
|
|
10157
|
+
}
|
|
9441
10158
|
|
|
9442
10159
|
// node_modules/@bod.ee/voice/src/adapters/cartesia.ts
|
|
9443
10160
|
var log16 = forComponent2("CartesiaTTS");
|
|
@@ -9448,12 +10165,34 @@ var CartesiaTTSOptions = class {
|
|
|
9448
10165
|
model = "sonic-3.5";
|
|
9449
10166
|
/** 'apiKey' (server/CLI) → `api_key=` URL param; 'token' (browser, BE-minted) → `access_token=`. */
|
|
9450
10167
|
authMode = "apiKey";
|
|
10168
|
+
/** Cartesia `language` code ('en', 'he', …). Unset → field omitted (provider default). Mutable:
|
|
10169
|
+
* set `tts.options.language` before `newContext()` to switch per context (mixed-language docs) —
|
|
10170
|
+
* it is latched there, so every frame of one context carries the same language. The model must
|
|
10171
|
+
* support it (sonic-3/3.5: 42 languages incl. 'he'; sonic-2/turbo: no Hebrew). */
|
|
10172
|
+
language;
|
|
10173
|
+
/** Cartesia `max_buffer_delay_ms` (integer, 0-5000; provider default 3000): the longest Cartesia
|
|
10174
|
+
* holds `continue: true` text waiting for more before it starts generating. Unset → field omitted
|
|
10175
|
+
* (provider default). Cartesia buffers so tiny inputs (single words/tokens) don't come out choppy
|
|
10176
|
+
* or with unnatural prosody; its guidance is to lower it (0 = generate each frame at once) only
|
|
10177
|
+
* when the caller does its own buffering, i.e. feeds phrase/sentence-sized chunks. Measured: an
|
|
10178
|
+
* unflushed continue frame reaches first audio in ~3 s at the default vs ~0.2 s at 0. A
|
|
10179
|
+
* `continue: false` frame / `end()` flushes regardless. Mutable like `language`: set
|
|
10180
|
+
* `tts.options.maxBufferDelayMs` before `newContext()` — latched there, one value per context. */
|
|
10181
|
+
maxBufferDelayMs;
|
|
10182
|
+
/** Sonic generation_config.emotion ('excited', 'content', 'sad', …). Mutable: the next frame carries it. */
|
|
10183
|
+
emotion;
|
|
10184
|
+
/** Written token → spoken form, swapped before synthesis (whole word, case-sensitive; core/pronounce.ts).
|
|
10185
|
+
* Read at construction. `{}` = off. */
|
|
10186
|
+
pronounce = PRONOUNCE;
|
|
9451
10187
|
};
|
|
9452
10188
|
var CartesiaTTS = class _CartesiaTTS {
|
|
9453
10189
|
options;
|
|
9454
10190
|
ws;
|
|
9455
10191
|
ctxSeq = 0;
|
|
9456
10192
|
ctxId = "";
|
|
10193
|
+
ctxLanguage;
|
|
10194
|
+
ctxMaxBufferDelayMs;
|
|
10195
|
+
say;
|
|
9457
10196
|
onAudio = () => {
|
|
9458
10197
|
};
|
|
9459
10198
|
onDone = () => {
|
|
@@ -9493,6 +10232,9 @@ var CartesiaTTS = class _CartesiaTTS {
|
|
|
9493
10232
|
static CB_PROBE_MS = 3e4;
|
|
9494
10233
|
constructor(options) {
|
|
9495
10234
|
this.options = { ...new CartesiaTTSOptions(), ...options };
|
|
10235
|
+
this.say = new Pronouncer(this.options.pronounce);
|
|
10236
|
+
this.ctxLanguage = this.options.language;
|
|
10237
|
+
this.ctxMaxBufferDelayMs = this.options.maxBufferDelayMs;
|
|
9496
10238
|
}
|
|
9497
10239
|
closed = false;
|
|
9498
10240
|
connecting = null;
|
|
@@ -9582,6 +10324,8 @@ var CartesiaTTS = class _CartesiaTTS {
|
|
|
9582
10324
|
newContext() {
|
|
9583
10325
|
this.ctxId = `ctx-${++this.ctxSeq}`;
|
|
9584
10326
|
this.firstAudioAt = 0;
|
|
10327
|
+
this.ctxLanguage = this.options.language;
|
|
10328
|
+
this.ctxMaxBufferDelayMs = this.options.maxBufferDelayMs;
|
|
9585
10329
|
return this.ctxId;
|
|
9586
10330
|
}
|
|
9587
10331
|
frame(transcript, cont) {
|
|
@@ -9589,26 +10333,42 @@ var CartesiaTTS = class _CartesiaTTS {
|
|
|
9589
10333
|
model_id: this.options.model,
|
|
9590
10334
|
transcript,
|
|
9591
10335
|
voice: { mode: "id", id: this.options.voiceId },
|
|
10336
|
+
...this.ctxLanguage ? { language: this.ctxLanguage } : {},
|
|
9592
10337
|
output_format: { container: "raw", encoding: "pcm_s16le", sample_rate: TTS_SAMPLE_RATE },
|
|
9593
10338
|
context_id: this.ctxId,
|
|
9594
10339
|
continue: cont,
|
|
9595
|
-
...this.
|
|
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(
|
|
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
|
|
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) =>
|
|
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 =
|
|
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];
|
|
@@ -10459,6 +11219,7 @@ function fitsLanguage(line, lang) {
|
|
|
10459
11219
|
}
|
|
10460
11220
|
var norm3 = (s) => s.toLowerCase().replace(/[^\p{L}\p{N}' ]/gu, "").replace(/\s+/g, " ").trim();
|
|
10461
11221
|
function cleanLine(s, lang, maxWords = 6) {
|
|
11222
|
+
if (/^(?:gasp|quiet|laughter)[.!?…]*$/i.test(s.trim())) return "";
|
|
10462
11223
|
const first = s.trim().split("\n")[0];
|
|
10463
11224
|
const tags = [...first.matchAll(LINE_TAG)].map((m) => `[${m[1].toLowerCase()}]`).filter((t, i, a) => a.indexOf(t) === i).join(" ");
|
|
10464
11225
|
const raw = first.replace(LINE_TAG, " ").trim();
|
|
@@ -10721,7 +11482,7 @@ var TurnGate = class {
|
|
|
10721
11482
|
}
|
|
10722
11483
|
/** An STT final. */
|
|
10723
11484
|
final(text, sttId) {
|
|
10724
|
-
let t = text.trim();
|
|
11485
|
+
let t = text.trim(), replaces;
|
|
10725
11486
|
if (!t) return this.o.onDrop(text, "empty");
|
|
10726
11487
|
if (this.held) {
|
|
10727
11488
|
const h = this.held;
|
|
@@ -10729,6 +11490,7 @@ var TurnGate = class {
|
|
|
10729
11490
|
this.held = void 0;
|
|
10730
11491
|
if (!(norm3(t) + " ").startsWith(norm3(h.text) + " ")) t = `${h.text} ${t}`;
|
|
10731
11492
|
sttId ??= h.sttId;
|
|
11493
|
+
replaces = h.replaces;
|
|
10732
11494
|
}
|
|
10733
11495
|
const key = norm3(t), now4 = this.o.now(), last = this.last;
|
|
10734
11496
|
if (last) {
|
|
@@ -10742,10 +11504,18 @@ var TurnGate = class {
|
|
|
10742
11504
|
const mid = endsMidPhrase(t);
|
|
10743
11505
|
if (mid || this.o.settleMs && !/\?["')\]]*$/.test(t)) {
|
|
10744
11506
|
const ms = mid ? this.o.holdMs : this.o.settleMs;
|
|
10745
|
-
this.held = { text: t, sttId, ms, timer: setTimeout(() => this.flush(), ms) };
|
|
11507
|
+
this.held = { text: t, sttId, ms, replaces, timer: setTimeout(() => this.flush(), ms) };
|
|
10746
11508
|
return this.o.onHold(t);
|
|
10747
11509
|
}
|
|
10748
|
-
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) };
|
|
10749
11519
|
}
|
|
10750
11520
|
/** The user is speaking again (a partial): a held fragment waits for the rest. */
|
|
10751
11521
|
speaking() {
|
|
@@ -10764,7 +11534,7 @@ var TurnGate = class {
|
|
|
10764
11534
|
clearTimeout(h.timer);
|
|
10765
11535
|
this.held = void 0;
|
|
10766
11536
|
if (this.closed && isClosing(h.text)) return this.o.onDrop(h.text, "goodbye");
|
|
10767
|
-
this.send(h.text, norm3(h.text), h.sttId, "new");
|
|
11537
|
+
this.send(h.text, norm3(h.text), h.sttId, "new", h.replaces);
|
|
10768
11538
|
}
|
|
10769
11539
|
end() {
|
|
10770
11540
|
if (this.held) {
|
|
@@ -13213,7 +13983,267 @@ init_models();
|
|
|
13213
13983
|
|
|
13214
13984
|
// src/voice/progress.ts
|
|
13215
13985
|
init_logging();
|
|
13216
|
-
|
|
13986
|
+
|
|
13987
|
+
// src/voice/convoTree.ts
|
|
13988
|
+
init_logging();
|
|
13989
|
+
|
|
13990
|
+
// src/voice/backchannelPlan.ts
|
|
13991
|
+
init_logging();
|
|
13992
|
+
var log26 = forComponent("BackchannelPlan");
|
|
13993
|
+
var TURN_TYPES = ["story", "list", "complaint", "explanation", "short_command", "question", "other"];
|
|
13994
|
+
var DENSITIES = ["none", "low", "normal", "high"];
|
|
13995
|
+
var silentTurn = (p) => p.density === "none" || p.turn === "short_command" || p.turn === "question";
|
|
13996
|
+
var cueWords = (s) => s.toLowerCase().replace(/[^\p{L}\p{N}'\s]+/gu, " ").split(/\s+/).filter(Boolean);
|
|
13997
|
+
function parsePlan(raw, lang, maxCues = 8) {
|
|
13998
|
+
const m = raw.match(/\{[\s\S]*\}/);
|
|
13999
|
+
if (!m) return null;
|
|
14000
|
+
let o;
|
|
14001
|
+
try {
|
|
14002
|
+
o = JSON.parse(m[0]);
|
|
14003
|
+
} catch {
|
|
14004
|
+
return null;
|
|
14005
|
+
}
|
|
14006
|
+
const turn = String(o?.turn ?? "").toLowerCase().replace(/[\s-]+/g, "_");
|
|
14007
|
+
const density = String(o?.density ?? "").toLowerCase();
|
|
14008
|
+
if (!TURN_TYPES.includes(turn) || !DENSITIES.includes(density)) return null;
|
|
14009
|
+
const cues = [];
|
|
14010
|
+
for (const c of Array.isArray(o.cues) ? o.cues : []) {
|
|
14011
|
+
const when = (Array.isArray(c?.when) ? c.when : [c?.when]).filter((w) => typeof w === "string").map((w) => cueWords(w).join(" ")).filter((w) => w && w.split(" ").length <= 4);
|
|
14012
|
+
const line = typeof c?.line === "string" ? cleanLine(c.line, lang, 4) : "";
|
|
14013
|
+
if (when.length && line) cues.push({ when: [...new Set(when)], line });
|
|
14014
|
+
if (cues.length >= maxCues) break;
|
|
14015
|
+
}
|
|
14016
|
+
return { turn, density, cues };
|
|
14017
|
+
}
|
|
14018
|
+
function matchCue(plan, heard, from = 0) {
|
|
14019
|
+
if (!plan || silentTurn(plan)) return null;
|
|
14020
|
+
const w = cueWords(heard);
|
|
14021
|
+
for (let i = Math.max(0, from); i < w.length; i++) {
|
|
14022
|
+
for (const c of plan.cues) {
|
|
14023
|
+
for (const p of c.when) {
|
|
14024
|
+
const pw = p.split(" ");
|
|
14025
|
+
if (i + pw.length <= w.length && pw.every((x, j) => w[i + j] === x)) return c;
|
|
14026
|
+
}
|
|
14027
|
+
}
|
|
14028
|
+
}
|
|
14029
|
+
return null;
|
|
14030
|
+
}
|
|
14031
|
+
var BackchannelPlannerOptions = class {
|
|
14032
|
+
/** Fast model (Groq gpt-oss / the reflex). */
|
|
14033
|
+
model = "groq/openai/gpt-oss-120b";
|
|
14034
|
+
maxTokens = 900;
|
|
14035
|
+
};
|
|
14036
|
+
var BackchannelPlanner = class {
|
|
14037
|
+
constructor(ai, options) {
|
|
14038
|
+
this.ai = ai;
|
|
14039
|
+
this.options = options;
|
|
14040
|
+
this.options = { ...new BackchannelPlannerOptions(), ...options };
|
|
14041
|
+
}
|
|
14042
|
+
ai;
|
|
14043
|
+
options;
|
|
14044
|
+
async plan(c, signal) {
|
|
14045
|
+
const o = this.options;
|
|
14046
|
+
const name = languageName(c.lang);
|
|
14047
|
+
const prompt = `You plan a live voice assistant's LISTENER sounds ("mm-hm", "right", "oh no") for the user's ${c.heard ? "CURRENT" : "NEXT"} spoken turn.
|
|
14048
|
+
Assistant just said: "${c.agent.slice(-400)}"
|
|
14049
|
+
` + (c.user ? `User's previous turn ended: "${c.user.slice(-200)}"
|
|
14050
|
+
` : "") + (c.heard ? `User so far in this turn: "${c.heard.slice(-300)}"
|
|
14051
|
+
` : "") + `Predict the turn: turn = story | list | complaint | explanation | short_command | question | other; density = none (short request or a question: never interrupt) | low | normal | high (a long story wants frequent cues).
|
|
14052
|
+
cues: 3-8 short words/phrases the user is likely to say at a clause boundary where a listener cue fits (list markers like "first", "and then", "also"; narrative markers; emotional words; names/topics from the context), each with ONE fitting listener line of 1-3 words in ${name} that reacts to it (sympathy for bad news, "okay" for a list step) \u2014 never a question, never an answer.
|
|
14053
|
+
Reply JSON only: {"turn":"...","density":"...","cues":[{"when":["phrase",...],"line":"..."}]}. Phrases in the user's language.`;
|
|
14054
|
+
const reasoning = /gpt-oss/.test(o.model) ? { providerOptions: { reasoning_effort: "low" } } : {};
|
|
14055
|
+
const r = await this.ai.chat({ model: o.model, messages: [{ role: "user", content: prompt }], stream: false, signal, maxTokens: o.maxTokens, ...reasoning });
|
|
14056
|
+
const p = parsePlan(r.content ?? "", c.lang);
|
|
14057
|
+
if (!p) log26.debug(`unusable plan: ${(r.content ?? "").slice(0, 120)}`);
|
|
14058
|
+
return p;
|
|
14059
|
+
}
|
|
14060
|
+
};
|
|
14061
|
+
|
|
14062
|
+
// src/voice/convoTree.ts
|
|
14063
|
+
var log27 = forComponent("ConvoTree");
|
|
14064
|
+
var MOMENTS = ["turn_end", "barge_in", "in_between", "ask"];
|
|
14065
|
+
var MAX_LINE_WORDS = 5;
|
|
14066
|
+
var SOFT_CUE = "~";
|
|
14067
|
+
var STOP4 = new Set("about after again also always been before being could does doing down even every from going have here into just know like look make many more most much must never only other over really right same should some still such sure take than that their them then there these they thing think this those through very want well were what when where which while will with would your yeah okay".split(" "));
|
|
14068
|
+
var isContent = (w) => w.length >= 4 && !STOP4.has(w);
|
|
14069
|
+
function echoesHeard(line, heard) {
|
|
14070
|
+
const said = new Set(cueWords(heard));
|
|
14071
|
+
return cueWords(lineWords(line)).some((w) => isContent(w) && said.has(w));
|
|
14072
|
+
}
|
|
14073
|
+
var leafKey = (line) => cueWords(lineWords(line)).join(" ");
|
|
14074
|
+
function parseNodes(list, lang, depth, maxBranches) {
|
|
14075
|
+
const out = [];
|
|
14076
|
+
for (const n of Array.isArray(list) ? list : []) {
|
|
14077
|
+
const moment = String(n?.moment ?? "").toLowerCase().replace(/[\s-]+/g, "_");
|
|
14078
|
+
const when = (Array.isArray(n?.when) ? n.when : [n?.when]).filter((w) => typeof w === "string").map((w) => cueWords(w).join(" ")).filter((w) => w && w.split(" ").length <= 4 && w.split(" ").some(isContent));
|
|
14079
|
+
const names = (Array.isArray(n?.names) ? n.names : []).filter((w) => typeof w === "string").flatMap((w) => cueWords(w));
|
|
14080
|
+
const p = Number(n?.p);
|
|
14081
|
+
const line = typeof n?.line === "string" ? cleanLine(n.line, lang, MAX_LINE_WORDS) : "";
|
|
14082
|
+
if (!MOMENTS.includes(moment) || !when.length || !line || !(p > 0)) continue;
|
|
14083
|
+
out.push({ when: [...new Set(when)], p: Math.min(p, 1), moment, line, ...names.length ? { names } : {}, next: depth > 1 ? parseNodes(n.next, lang, depth - 1, maxBranches) : [] });
|
|
14084
|
+
if (out.length >= maxBranches) break;
|
|
14085
|
+
}
|
|
14086
|
+
return out;
|
|
14087
|
+
}
|
|
14088
|
+
function parseTree(raw, lang, depth = 2, maxBranches = 4) {
|
|
14089
|
+
const m = raw.match(/\{[\s\S]*\}/);
|
|
14090
|
+
if (!m) return [];
|
|
14091
|
+
try {
|
|
14092
|
+
const o = JSON.parse(m[0]), nodes = parseNodes(o?.branches, lang, depth, maxBranches);
|
|
14093
|
+
if (!nodes.length) return [];
|
|
14094
|
+
const soft = (Array.isArray(o?.soft) ? o.soft : []).filter((l) => typeof l === "string").map((l) => cleanLine(l, lang, 3)).filter(Boolean).slice(0, 2);
|
|
14095
|
+
return [...nodes, ...soft.map((line) => ({ when: [SOFT_CUE], p: 0.25, moment: "in_between", line, next: [] }))];
|
|
14096
|
+
} catch {
|
|
14097
|
+
return [];
|
|
14098
|
+
}
|
|
14099
|
+
}
|
|
14100
|
+
var ConvoTreeOptions = class {
|
|
14101
|
+
/** A leaf plays at this confidence or above. */
|
|
14102
|
+
threshold = 0.5;
|
|
14103
|
+
/** How much more likely a cue of one word / of two or more is if its branch is the true one. */
|
|
14104
|
+
cueEvidence = 3;
|
|
14105
|
+
phraseEvidence = 8;
|
|
14106
|
+
/** A hit weakens each sibling by this factor. */
|
|
14107
|
+
siblingDecay = 0.5;
|
|
14108
|
+
/** A cue counts only within the newest N words: older ones have passed, a reaction to them would be late. */
|
|
14109
|
+
cueWindow = 4;
|
|
14110
|
+
};
|
|
14111
|
+
var namesHeard = (n, said) => (n.names ?? []).every((w) => said.has(w));
|
|
14112
|
+
function echoHeard(node, heard) {
|
|
14113
|
+
if (node.when[0] === SOFT_CUE) return true;
|
|
14114
|
+
const said = new Set(cueWords(heard));
|
|
14115
|
+
return node.when.flatMap((w) => w.split(" ")).some((w) => isContent(w) && said.has(w)) && namesHeard(node, said);
|
|
14116
|
+
}
|
|
14117
|
+
var ConvoTree = class {
|
|
14118
|
+
// a turn_end leaf waiting for the turn to end
|
|
14119
|
+
constructor(options) {
|
|
14120
|
+
this.options = options;
|
|
14121
|
+
this.options = { ...new ConvoTreeOptions(), ...options };
|
|
14122
|
+
}
|
|
14123
|
+
options;
|
|
14124
|
+
live = [];
|
|
14125
|
+
from = 0;
|
|
14126
|
+
// word index after the last hit
|
|
14127
|
+
armed = null;
|
|
14128
|
+
get o() {
|
|
14129
|
+
return this.options;
|
|
14130
|
+
}
|
|
14131
|
+
get nodes() {
|
|
14132
|
+
return this.live;
|
|
14133
|
+
}
|
|
14134
|
+
get empty() {
|
|
14135
|
+
return !this.live.length;
|
|
14136
|
+
}
|
|
14137
|
+
/** A fresh tree. `heardWords`: words already said this turn (a tree grown mid-turn matches only what comes next). */
|
|
14138
|
+
grow(nodes, heardWords = 0) {
|
|
14139
|
+
this.live = nodes.map((n) => ({ ...n }));
|
|
14140
|
+
this.armed = null;
|
|
14141
|
+
this.from = heardWords;
|
|
14142
|
+
}
|
|
14143
|
+
/** Posterior of a node after seeing its cue. */
|
|
14144
|
+
confidence(p, words4) {
|
|
14145
|
+
const l = p * (words4 > 1 ? this.o.phraseEvidence : this.o.cueEvidence);
|
|
14146
|
+
return l / (l + 1 - p);
|
|
14147
|
+
}
|
|
14148
|
+
/** The partial's newest evidence: the strongest new cue hit (its children open, its siblings fade), or null. A turn_end
|
|
14149
|
+
* hit is armed for endTurn; the caller plays the others. Hits under the threshold still sharpen the tree. */
|
|
14150
|
+
step(heard) {
|
|
14151
|
+
const w = cueWords(heard), said = new Set(w);
|
|
14152
|
+
let best = null;
|
|
14153
|
+
for (let i = Math.max(this.from, w.length - this.o.cueWindow); i < w.length; i++) {
|
|
14154
|
+
for (const node2 of this.live) for (const cue of node2.when) {
|
|
14155
|
+
const pw = cue.split(" ");
|
|
14156
|
+
if (!namesHeard(node2, said) || i + pw.length > w.length || !pw.every((x, j) => w[i + j] === x)) continue;
|
|
14157
|
+
const conf2 = this.confidence(node2.p, pw.length);
|
|
14158
|
+
if (!best || conf2 > best.conf) best = { node: node2, conf: conf2, end: i + pw.length };
|
|
14159
|
+
}
|
|
14160
|
+
}
|
|
14161
|
+
if (!best) return null;
|
|
14162
|
+
const { node, conf } = best;
|
|
14163
|
+
this.from = best.end;
|
|
14164
|
+
this.take(node, conf);
|
|
14165
|
+
const hit = { node, conf };
|
|
14166
|
+
if ((node.moment === "turn_end" || node.moment === "ask") && conf >= (this.armed?.conf ?? 0)) {
|
|
14167
|
+
this.armed = hit;
|
|
14168
|
+
return null;
|
|
14169
|
+
}
|
|
14170
|
+
return conf >= this.o.threshold ? hit : null;
|
|
14171
|
+
}
|
|
14172
|
+
/** A node happened: it leaves the live set, its siblings fade, its children open. */
|
|
14173
|
+
take(node, conf) {
|
|
14174
|
+
this.live = [...this.live.filter((n) => n !== node).map((n) => ({ ...n, p: n.p * this.o.siblingDecay })), ...node.next.map((n) => ({ ...n, p: Math.min(1, n.p * Math.max(conf, 0.5) * 1.5) }))];
|
|
14175
|
+
}
|
|
14176
|
+
/** The turn ended: the armed leaf if it clears the threshold. The live set stays for a picker; `advance` clears it. */
|
|
14177
|
+
turnLeaf() {
|
|
14178
|
+
const a = this.armed;
|
|
14179
|
+
return a && a.conf >= this.o.threshold ? a : null;
|
|
14180
|
+
}
|
|
14181
|
+
/** A quick model named a node at the turn's end: its evidence counts as a phrase. Null when under the threshold. */
|
|
14182
|
+
picked(i, heard = "") {
|
|
14183
|
+
const node = this.live[i];
|
|
14184
|
+
if (!node || !echoHeard(node, heard)) return null;
|
|
14185
|
+
const conf = this.confidence(node.p, 2);
|
|
14186
|
+
return conf >= this.o.threshold ? { node, conf } : null;
|
|
14187
|
+
}
|
|
14188
|
+
/** Next turn: what followed a played/armed branch stays live until a fresh tree lands; the rest is gone. */
|
|
14189
|
+
advance() {
|
|
14190
|
+
this.live = this.live.filter((n) => n.p >= 0.05);
|
|
14191
|
+
this.armed = null;
|
|
14192
|
+
this.from = 0;
|
|
14193
|
+
}
|
|
14194
|
+
};
|
|
14195
|
+
var ConvoTreeGrowerOptions = class {
|
|
14196
|
+
/** gpt-oss on Groq: ~1.2s a tree. cursor/composer-2.5-fast took 10-15s (the Dessa rig), slower than the talk's turns. */
|
|
14197
|
+
model = "groq/openai/gpt-oss-120b";
|
|
14198
|
+
maxTokens = 1400;
|
|
14199
|
+
/** Branches and levels the model is asked for. */
|
|
14200
|
+
branches = 3;
|
|
14201
|
+
levels = 2;
|
|
14202
|
+
};
|
|
14203
|
+
var ConvoTreeGrower = class {
|
|
14204
|
+
constructor(ai, options) {
|
|
14205
|
+
this.ai = ai;
|
|
14206
|
+
this.options = options;
|
|
14207
|
+
this.options = { ...new ConvoTreeGrowerOptions(), ...options };
|
|
14208
|
+
}
|
|
14209
|
+
ai;
|
|
14210
|
+
options;
|
|
14211
|
+
async grow(c, signal) {
|
|
14212
|
+
const o = this.options;
|
|
14213
|
+
const prompt = `You predict a live spoken talk between "them" (a human) and "you".
|
|
14214
|
+
` + (c.persona ? `You are: ${c.persona.slice(0, 500)}
|
|
14215
|
+
` : "") + `The talk so far:
|
|
14216
|
+
${c.talk.join("\n") || "(it is just starting)"}
|
|
14217
|
+
|
|
14218
|
+
If their last line is marked (still talking), predict how THAT sentence goes on. Otherwise predict what THEY say next: ${o.branches} different likely evolutions, each with ${o.branches - 1} follow-ups of what they say after it (${o.levels} levels). Think of what real people say, in this talk, right now.
|
|
14219
|
+
Each branch: "when" = 4-6 single words or short phrases THEY would actually say that reveal this branch (specific words of the topic and feeling words, not "so" or "and"); "p" = its probability (siblings sum to about 1); "moment" = when a real person would react: "barge_in" (cuts in the instant they hear it: big news, a shock, a joke), "in_between" (a sound in their breath, they keep talking: the usual one), "turn_end" (right when they finish: rare), "ask" (they ask you to look up, check or do something: at least one branch is this, its line names the thing and says you are on it: "Hacker News, one sec.", up to 5 words); "line" = what you say then, 1-3 plain words in ${languageName(c.lang)} that react to THOSE words (an echo of their word is great: "Paris?!", "the jumper", "mm, okay", "oh no"). Vary them; no stock line twice. A line that names a topic lists it in "names" ("Paris?!": ["paris"]). At most ONE line in the whole tree may carry a sound, only for a real shock or a real joke: it begins with [gasp] or [laughter], [quiet] for something tender. A reaction, never an answer or a question that needs one.
|
|
14220
|
+
` + (c.recent.length ? `Already said, never reuse these or close variants: ${c.recent.map((r2) => `"${r2}"`).join(", ")}.
|
|
14221
|
+
` : "") + `Do not use any tools. Also "soft": 2 plain listener sounds that fit the feeling of this moment and name no topic ("mm", "oh wow", "right", "oh no", "ha").
|
|
14222
|
+
Reply JSON only: {"soft":["...","..."],"branches":[{"when":["..."],"p":0.5,"moment":"...","line":"...","names":["each topic word the line itself says, none for a feeling"],"next":[{...same, no next}]}]}`;
|
|
14223
|
+
const reasoning = /gpt-oss/.test(o.model) ? { providerOptions: { reasoning_effort: "low" } } : {};
|
|
14224
|
+
const r = await this.ai.chat({ model: o.model, messages: [{ role: "user", content: prompt }], stream: false, signal, maxTokens: o.maxTokens, ...reasoning });
|
|
14225
|
+
const seen = new Set(c.recent.map(leafKey));
|
|
14226
|
+
const fresh = (l) => l.filter((n) => !seen.has(leafKey(n.line))).map((n) => ({ ...n, next: fresh(n.next) }));
|
|
14227
|
+
const nodes = fresh(parseTree(r.content ?? "", c.lang, o.levels));
|
|
14228
|
+
if (!nodes.length) log27.debug(`unusable tree: ${(r.content ?? "").slice(0, 160)}`);
|
|
14229
|
+
return { nodes, ...r.usage ? { tokens: { input: r.usage.promptTokens, output: r.usage.completionTokens } } : {} };
|
|
14230
|
+
}
|
|
14231
|
+
};
|
|
14232
|
+
function fastLeafPicker(ai, model = "groq/openai/gpt-oss-120b") {
|
|
14233
|
+
return async (heard, lines, signal) => {
|
|
14234
|
+
const prompt = `Live spoken talk. They just said: "${heard.slice(-240)}"
|
|
14235
|
+
They may still be talking. A warm listener reacts often: does one of these fit the words they just said? It must fit those words.
|
|
14236
|
+
` + lines.map((l) => `${l.i}: ${l.line} (fits: ${l.when.join(", ").replace("~", "any moment, when no topic line fits")})`).join("\n") + `
|
|
14237
|
+
Answer only the number, or "none".`;
|
|
14238
|
+
const reasoning = /gpt-oss/.test(model) ? { providerOptions: { reasoning_effort: "low" } } : {};
|
|
14239
|
+
const r = await ai.chat({ model, messages: [{ role: "user", content: prompt }], stream: false, signal, temperature: 0, maxTokens: /gpt-oss/.test(model) ? 200 : 4, ...reasoning });
|
|
14240
|
+
const m = (r.content ?? "").trim().match(/^\d+/);
|
|
14241
|
+
return m ? Number(m[0]) : -1;
|
|
14242
|
+
};
|
|
14243
|
+
}
|
|
14244
|
+
|
|
14245
|
+
// src/voice/progress.ts
|
|
14246
|
+
var log28 = forComponent("Progress");
|
|
13217
14247
|
var HE_RESULT_CLAIM = /(?:^|\s)(?:מצאתי|סיימתי|בוצע|הנה|אין)(?=\s|$|[.,!…])/u;
|
|
13218
14248
|
var TOPICS = [
|
|
13219
14249
|
[/toolsearch|^mcp__.*__(list_commands|call_info)$/, null],
|
|
@@ -13337,8 +14367,8 @@ var Progress = class {
|
|
|
13337
14367
|
this.said.add(norm3(line));
|
|
13338
14368
|
this.lastAt = this.o.now();
|
|
13339
14369
|
}
|
|
13340
|
-
/** The line for this tool call in `lang`, or '' (no template and no/late/invalid generation, a repeat, a result claim). */
|
|
13341
|
-
async line(tool, args, lang) {
|
|
14370
|
+
/** `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
|
+
async line(tool, args, lang, about = "") {
|
|
13342
14372
|
if (this.streamed) return "";
|
|
13343
14373
|
const name = tool.toLowerCase();
|
|
13344
14374
|
const hit = TOPICS.find(([re]) => re.test(name));
|
|
@@ -13357,18 +14387,18 @@ var Progress = class {
|
|
|
13357
14387
|
r(null);
|
|
13358
14388
|
}, this.o.generateBudgetMs);
|
|
13359
14389
|
});
|
|
13360
|
-
const raw = await Promise.race([this.o.generate(tool, argsHint(args), lang, ac.signal).catch((e) => {
|
|
13361
|
-
|
|
14390
|
+
const raw = await Promise.race([this.o.generate(tool, [about.slice(-80), argsHint(args)].filter(Boolean).join("; "), lang, ac.signal).catch((e) => {
|
|
14391
|
+
log28.warn(`progress line failed: ${e?.message ?? e}`);
|
|
13362
14392
|
return null;
|
|
13363
14393
|
}), late]).finally(() => clearTimeout(t));
|
|
13364
14394
|
const l = cleanLine(raw ?? "", lang, 5);
|
|
13365
|
-
if (!l || RESULT_CLAIM_RE.test(l) || HE_RESULT_CLAIM.test(l) || this.said.has(norm3(l)) || this.streamed) return "";
|
|
14395
|
+
if (!l || about && !echoesHeard(l, `${about} ${argsHint(args)}`) || RESULT_CLAIM_RE.test(l) || HE_RESULT_CLAIM.test(l) || this.said.has(norm3(l)) || this.streamed) return "";
|
|
13366
14396
|
return l;
|
|
13367
14397
|
}
|
|
13368
14398
|
};
|
|
13369
14399
|
function fastProgressGenerator(ai, model, gender) {
|
|
13370
14400
|
return async (tool, hint, lang, signal) => {
|
|
13371
|
-
const prompt = `An assistant on a live voice call is running the tool "${tool}"${hint ? ` (about: "${hint}")` : ""}. Write ONE spoken line of 2-5 words, in ${languageName(lang)}, saying what it is doing right now (like "checking the board\u2026"). Never state a result or a finding. ${personaHint(gender)} Output only the line.`;
|
|
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.`;
|
|
13372
14402
|
const reasoning = /gpt-oss/.test(model) ? { providerOptions: { reasoning_effort: "low" } } : {};
|
|
13373
14403
|
const r = await ai.chat({ model, messages: [{ role: "user", content: prompt }], stream: false, signal, maxTokens: 150, ...reasoning });
|
|
13374
14404
|
return r.content ?? "";
|
|
@@ -13377,81 +14407,7 @@ function fastProgressGenerator(ai, model, gender) {
|
|
|
13377
14407
|
|
|
13378
14408
|
// src/voice/secondChannel.ts
|
|
13379
14409
|
init_logging();
|
|
13380
|
-
|
|
13381
|
-
// src/voice/backchannelPlan.ts
|
|
13382
|
-
init_logging();
|
|
13383
|
-
var log27 = forComponent("BackchannelPlan");
|
|
13384
|
-
var TURN_TYPES = ["story", "list", "complaint", "explanation", "short_command", "question", "other"];
|
|
13385
|
-
var DENSITIES = ["none", "low", "normal", "high"];
|
|
13386
|
-
var silentTurn = (p) => p.density === "none" || p.turn === "short_command" || p.turn === "question";
|
|
13387
|
-
var cueWords = (s) => s.toLowerCase().replace(/[^\p{L}\p{N}'\s]+/gu, " ").split(/\s+/).filter(Boolean);
|
|
13388
|
-
function parsePlan(raw, lang, maxCues = 8) {
|
|
13389
|
-
const m = raw.match(/\{[\s\S]*\}/);
|
|
13390
|
-
if (!m) return null;
|
|
13391
|
-
let o;
|
|
13392
|
-
try {
|
|
13393
|
-
o = JSON.parse(m[0]);
|
|
13394
|
-
} catch {
|
|
13395
|
-
return null;
|
|
13396
|
-
}
|
|
13397
|
-
const turn = String(o?.turn ?? "").toLowerCase().replace(/[\s-]+/g, "_");
|
|
13398
|
-
const density = String(o?.density ?? "").toLowerCase();
|
|
13399
|
-
if (!TURN_TYPES.includes(turn) || !DENSITIES.includes(density)) return null;
|
|
13400
|
-
const cues = [];
|
|
13401
|
-
for (const c of Array.isArray(o.cues) ? o.cues : []) {
|
|
13402
|
-
const when = (Array.isArray(c?.when) ? c.when : [c?.when]).filter((w) => typeof w === "string").map((w) => cueWords(w).join(" ")).filter((w) => w && w.split(" ").length <= 4);
|
|
13403
|
-
const line = typeof c?.line === "string" ? cleanLine(c.line, lang, 4) : "";
|
|
13404
|
-
if (when.length && line) cues.push({ when: [...new Set(when)], line });
|
|
13405
|
-
if (cues.length >= maxCues) break;
|
|
13406
|
-
}
|
|
13407
|
-
return { turn, density, cues };
|
|
13408
|
-
}
|
|
13409
|
-
function matchCue(plan, heard, from = 0) {
|
|
13410
|
-
if (!plan || silentTurn(plan)) return null;
|
|
13411
|
-
const w = cueWords(heard);
|
|
13412
|
-
for (let i = Math.max(0, from); i < w.length; i++) {
|
|
13413
|
-
for (const c of plan.cues) {
|
|
13414
|
-
for (const p of c.when) {
|
|
13415
|
-
const pw = p.split(" ");
|
|
13416
|
-
if (i + pw.length <= w.length && pw.every((x, j) => w[i + j] === x)) return c;
|
|
13417
|
-
}
|
|
13418
|
-
}
|
|
13419
|
-
}
|
|
13420
|
-
return null;
|
|
13421
|
-
}
|
|
13422
|
-
var BackchannelPlannerOptions = class {
|
|
13423
|
-
/** Fast model (Groq gpt-oss / the reflex). */
|
|
13424
|
-
model = "groq/openai/gpt-oss-120b";
|
|
13425
|
-
maxTokens = 900;
|
|
13426
|
-
};
|
|
13427
|
-
var BackchannelPlanner = class {
|
|
13428
|
-
constructor(ai, options) {
|
|
13429
|
-
this.ai = ai;
|
|
13430
|
-
this.options = options;
|
|
13431
|
-
this.options = { ...new BackchannelPlannerOptions(), ...options };
|
|
13432
|
-
}
|
|
13433
|
-
ai;
|
|
13434
|
-
options;
|
|
13435
|
-
async plan(c, signal) {
|
|
13436
|
-
const o = this.options;
|
|
13437
|
-
const name = languageName(c.lang);
|
|
13438
|
-
const prompt = `You plan a live voice assistant's LISTENER sounds ("mm-hm", "right", "oh no") for the user's ${c.heard ? "CURRENT" : "NEXT"} spoken turn.
|
|
13439
|
-
Assistant just said: "${c.agent.slice(-400)}"
|
|
13440
|
-
` + (c.user ? `User's previous turn ended: "${c.user.slice(-200)}"
|
|
13441
|
-
` : "") + (c.heard ? `User so far in this turn: "${c.heard.slice(-300)}"
|
|
13442
|
-
` : "") + `Predict the turn: turn = story | list | complaint | explanation | short_command | question | other; density = none (short request or a question: never interrupt) | low | normal | high (a long story wants frequent cues).
|
|
13443
|
-
cues: 3-8 short words/phrases the user is likely to say at a clause boundary where a listener cue fits (list markers like "first", "and then", "also"; narrative markers; emotional words; names/topics from the context), each with ONE fitting listener line of 1-3 words in ${name} that reacts to it (sympathy for bad news, "okay" for a list step) \u2014 never a question, never an answer.
|
|
13444
|
-
Reply JSON only: {"turn":"...","density":"...","cues":[{"when":["phrase",...],"line":"..."}]}. Phrases in the user's language.`;
|
|
13445
|
-
const reasoning = /gpt-oss/.test(o.model) ? { providerOptions: { reasoning_effort: "low" } } : {};
|
|
13446
|
-
const r = await this.ai.chat({ model: o.model, messages: [{ role: "user", content: prompt }], stream: false, signal, maxTokens: o.maxTokens, ...reasoning });
|
|
13447
|
-
const p = parsePlan(r.content ?? "", c.lang);
|
|
13448
|
-
if (!p) log27.debug(`unusable plan: ${(r.content ?? "").slice(0, 120)}`);
|
|
13449
|
-
return p;
|
|
13450
|
-
}
|
|
13451
|
-
};
|
|
13452
|
-
|
|
13453
|
-
// src/voice/secondChannel.ts
|
|
13454
|
-
var log28 = forComponent("SecondChannel");
|
|
14410
|
+
var log29 = forComponent("SecondChannel");
|
|
13455
14411
|
var CHANNEL_MODES = {
|
|
13456
14412
|
turn: { kinds: ["bridge"], bridgeDelayMs: 1500 },
|
|
13457
14413
|
normal: {},
|
|
@@ -13470,10 +14426,10 @@ var CHANNEL_MODES = {
|
|
|
13470
14426
|
maxPerWindow: 6
|
|
13471
14427
|
},
|
|
13472
14428
|
live: {
|
|
13473
|
-
kinds: ["backchannel"],
|
|
14429
|
+
kinds: ["backchannel", "bridge"],
|
|
13474
14430
|
anticipate: true,
|
|
13475
14431
|
cueOnly: false,
|
|
13476
|
-
generateFor: ["backchannel"],
|
|
14432
|
+
generateFor: ["backchannel", "bridge"],
|
|
13477
14433
|
reactionGapMs: 0,
|
|
13478
14434
|
generateBudgetMs: 400,
|
|
13479
14435
|
backchannelMinWords: 3,
|
|
@@ -13528,6 +14484,8 @@ var SecondChannelOptions = class {
|
|
|
13528
14484
|
decideEveryWords = 3;
|
|
13529
14485
|
decisionBudgetMs = 300;
|
|
13530
14486
|
generateBudgetMs = 500;
|
|
14487
|
+
/** `live`: how long the fast model may take on a bridge line; later = silence. */
|
|
14488
|
+
bridgeBudgetMs = 1200;
|
|
13531
14489
|
minGapMs = 2500;
|
|
13532
14490
|
/** Backchannel after backchannel needs this gap (~4-6 cues in a 30s monologue — Para phone call 2026-10-13: 5s left
|
|
13533
14491
|
* one cue in a long statement). */
|
|
@@ -13581,6 +14539,31 @@ var SecondChannelOptions = class {
|
|
|
13581
14539
|
cueOnly = true;
|
|
13582
14540
|
onPlan = () => {
|
|
13583
14541
|
};
|
|
14542
|
+
/** `live` only (convoTree.ts): grows the prediction tree after each reply. Once a tree is up it is the only source of
|
|
14543
|
+
* lines while the user talks: a leaf above its threshold, or silence. No tree yet / a failed one: the reactive path. */
|
|
14544
|
+
grow;
|
|
14545
|
+
/** The quick model at a turn's end with no cue hit: names a leaf or none, inside `pickBudgetMs`. */
|
|
14546
|
+
pickLeaf;
|
|
14547
|
+
pickBudgetMs = 700;
|
|
14548
|
+
growBudgetMs = 15e3;
|
|
14549
|
+
tree = {};
|
|
14550
|
+
/** The tree grower's persona (who is speaking). */
|
|
14551
|
+
persona = "";
|
|
14552
|
+
/** Lines of talk the grower sees. */
|
|
14553
|
+
talkLines = 8;
|
|
14554
|
+
/** Mid-turn: at this many words the tree regrows once from what they are saying now. */
|
|
14555
|
+
regrowAtWords = 6;
|
|
14556
|
+
/** A leaf is dropped once the talk moved this many words past its cue. */
|
|
14557
|
+
leafStaleWords = 3;
|
|
14558
|
+
/** Interims stop this long: the quick model may pick a leaf (shorter than the ~400ms word cadence, so most gaps count; leafGated spaces the calls). */
|
|
14559
|
+
leafBreathMs = 200;
|
|
14560
|
+
/** Played leaves the grower must avoid, and how many lines pass before a tag may play again. */
|
|
14561
|
+
leafMemory = 16;
|
|
14562
|
+
tagEvery = 4;
|
|
14563
|
+
onTree = () => {
|
|
14564
|
+
};
|
|
14565
|
+
onLeaf = () => {
|
|
14566
|
+
};
|
|
13584
14567
|
now = () => performance.now();
|
|
13585
14568
|
random = Math.random;
|
|
13586
14569
|
};
|
|
@@ -13589,15 +14572,15 @@ function withBudget(p, ms, what, ac) {
|
|
|
13589
14572
|
let t;
|
|
13590
14573
|
const late = new Promise((r) => {
|
|
13591
14574
|
t = setTimeout(() => {
|
|
13592
|
-
|
|
14575
|
+
log29.debug(`${what}: over the ${ms}ms budget \u2014 dropped`);
|
|
13593
14576
|
ac?.abort();
|
|
13594
14577
|
r(null);
|
|
13595
14578
|
}, ms);
|
|
13596
14579
|
});
|
|
13597
14580
|
return Promise.race([p.catch((e) => {
|
|
13598
14581
|
const msg = String(e?.message ?? e);
|
|
13599
|
-
if (e?.name === "AbortError" || /aborted/i.test(msg))
|
|
13600
|
-
else
|
|
14582
|
+
if (e?.name === "AbortError" || /aborted/i.test(msg)) log29.debug(`${what}: aborted after its budget`);
|
|
14583
|
+
else log29.warn(`${what} failed \u2014 falling back: ${msg}`);
|
|
13601
14584
|
return null;
|
|
13602
14585
|
}), late]).finally(() => clearTimeout(t));
|
|
13603
14586
|
}
|
|
@@ -13610,6 +14593,7 @@ var SecondChannel = class {
|
|
|
13610
14593
|
for (const k of Object.keys(preset)) if (!options || !(k in options)) this.normalTiming[k] = base[k];
|
|
13611
14594
|
this.startedAt = this.o.now();
|
|
13612
14595
|
if (this.o.lang) this.setLanguage(this.o.lang, true);
|
|
14596
|
+
if (this.o.mode === "live" && this.o.grow) this.tree = new ConvoTree(this.o.tree);
|
|
13613
14597
|
}
|
|
13614
14598
|
options;
|
|
13615
14599
|
heard = "";
|
|
@@ -13638,6 +14622,12 @@ var SecondChannel = class {
|
|
|
13638
14622
|
// one bridge per main turn
|
|
13639
14623
|
bridgeDraft = null;
|
|
13640
14624
|
// a bridge generated ahead, used only if ready in time
|
|
14625
|
+
bridgeReady = Promise.resolve();
|
|
14626
|
+
// live: that draft settled
|
|
14627
|
+
askLeaf = null;
|
|
14628
|
+
// live: this turn's ask leaf
|
|
14629
|
+
leafReady = Promise.resolve();
|
|
14630
|
+
// live: the turn-end leaf pick settled
|
|
13641
14631
|
recent = [];
|
|
13642
14632
|
picks = /* @__PURE__ */ new Map();
|
|
13643
14633
|
inflight = null;
|
|
@@ -13665,6 +14655,21 @@ var SecondChannel = class {
|
|
|
13665
14655
|
normalTiming = {};
|
|
13666
14656
|
// chatty without aec: the preset's keys back at their defaults
|
|
13667
14657
|
degradedLogged = false;
|
|
14658
|
+
tree = null;
|
|
14659
|
+
// live with a grower only
|
|
14660
|
+
growing = false;
|
|
14661
|
+
growTries = 0;
|
|
14662
|
+
// per turn
|
|
14663
|
+
replyGrown = false;
|
|
14664
|
+
// one tree per reply (idle comes more than once)
|
|
14665
|
+
talk = [];
|
|
14666
|
+
leaves = [];
|
|
14667
|
+
// leaves played this call
|
|
14668
|
+
sinceTag = Infinity;
|
|
14669
|
+
regrown = false;
|
|
14670
|
+
picking = false;
|
|
14671
|
+
breathTimer = null;
|
|
14672
|
+
leafTimer = null;
|
|
13668
14673
|
/** The language lines are spoken in (detected from the user's words, or set by the host). */
|
|
13669
14674
|
get language() {
|
|
13670
14675
|
return this.lang;
|
|
@@ -13676,7 +14681,7 @@ var SecondChannel = class {
|
|
|
13676
14681
|
if (host) this.pinned = true;
|
|
13677
14682
|
const lang = primaryLang(tag);
|
|
13678
14683
|
if (!lang || lang === this.lang) return;
|
|
13679
|
-
|
|
14684
|
+
log29.debug(`language ${this.lang} \u2192 ${lang}`);
|
|
13680
14685
|
this.lang = lang;
|
|
13681
14686
|
this.recent = [];
|
|
13682
14687
|
this.picks.clear();
|
|
@@ -13689,6 +14694,10 @@ var SecondChannel = class {
|
|
|
13689
14694
|
const l = detectLanguage(text, !!this.lang);
|
|
13690
14695
|
if (l) this.setLanguage(l);
|
|
13691
14696
|
}
|
|
14697
|
+
/** `live` speaks no stock bridge/filler, with or without aec. */
|
|
14698
|
+
get ownWords() {
|
|
14699
|
+
return this.options.mode === "live";
|
|
14700
|
+
}
|
|
13692
14701
|
get bank() {
|
|
13693
14702
|
return this.lang ? this.o.banks ? this.o.banks.get(this.lang) : this.lang === "en" ? EN_BANK : null : null;
|
|
13694
14703
|
}
|
|
@@ -13712,7 +14721,23 @@ var SecondChannel = class {
|
|
|
13712
14721
|
this.onPause(this.o.fillerPauseMs);
|
|
13713
14722
|
}, this.o.fillerPauseMs);
|
|
13714
14723
|
let cue = null;
|
|
13715
|
-
if (this.
|
|
14724
|
+
if (this.tree) {
|
|
14725
|
+
if (this.tree.empty && !this.growing && this.growTries < 2) this.requestTree();
|
|
14726
|
+
if (!this.tree.empty) {
|
|
14727
|
+
this.treeStep(t);
|
|
14728
|
+
if (!this.regrown && words3(t).length >= this.o.regrowAtWords) {
|
|
14729
|
+
this.regrown = true;
|
|
14730
|
+
this.requestTree(t);
|
|
14731
|
+
}
|
|
14732
|
+
this.armBreath(t);
|
|
14733
|
+
if (this.bcTimer) {
|
|
14734
|
+
clearTimeout(this.bcTimer);
|
|
14735
|
+
this.bcTimer = null;
|
|
14736
|
+
}
|
|
14737
|
+
return;
|
|
14738
|
+
}
|
|
14739
|
+
}
|
|
14740
|
+
if (this.o.anticipate && !this.tree) {
|
|
13716
14741
|
const n = words3(t).length;
|
|
13717
14742
|
if (!this.plan && !this.planning && this.planTries < 2) this.requestPlan();
|
|
13718
14743
|
else if (this.plan && !this.revised && n >= this.o.reviseAtWords) {
|
|
@@ -13757,11 +14782,24 @@ var SecondChannel = class {
|
|
|
13757
14782
|
this.decidedAt = 0;
|
|
13758
14783
|
this.bridged = false;
|
|
13759
14784
|
this.cueFrom = 0;
|
|
13760
|
-
if (this.
|
|
14785
|
+
if (this.tree) {
|
|
14786
|
+
if (this.reply.trim() && !this.replyGrown) {
|
|
14787
|
+
this.replyGrown = true;
|
|
14788
|
+
this.noteTalk("you", this.reply);
|
|
14789
|
+
this.requestTree();
|
|
14790
|
+
}
|
|
14791
|
+
} else if (this.o.anticipate && this.reply.trim() && !this.plan && !this.planning) this.requestPlan();
|
|
13761
14792
|
return;
|
|
13762
14793
|
}
|
|
13763
14794
|
if (s === "queued") {
|
|
13764
14795
|
this.turnNo++;
|
|
14796
|
+
this.growTries = 0;
|
|
14797
|
+
this.replyGrown = false;
|
|
14798
|
+
this.regrown = false;
|
|
14799
|
+
if (this.breathTimer) {
|
|
14800
|
+
clearTimeout(this.breathTimer);
|
|
14801
|
+
this.breathTimer = null;
|
|
14802
|
+
}
|
|
13765
14803
|
this.plan = null;
|
|
13766
14804
|
this.planning = false;
|
|
13767
14805
|
this.planTries = 0;
|
|
@@ -13775,7 +14813,7 @@ var SecondChannel = class {
|
|
|
13775
14813
|
if (this[k]) clearTimeout(this[k]);
|
|
13776
14814
|
this[k] = null;
|
|
13777
14815
|
}
|
|
13778
|
-
if (this.o.instantBridge) this.prefetchBridge();
|
|
14816
|
+
if (this.o.instantBridge || this.ownWords) this.prefetchBridge();
|
|
13779
14817
|
const wait = Math.max(this.o.bridgeDelayMs, this.o.minGapMs - (this.o.now() - this.lastAt));
|
|
13780
14818
|
if (wait > 0) this.bridgeTimer = setTimeout(() => {
|
|
13781
14819
|
this.bridgeTimer = null;
|
|
@@ -13802,6 +14840,10 @@ var SecondChannel = class {
|
|
|
13802
14840
|
this.detect(this.heard);
|
|
13803
14841
|
this.main = "idle";
|
|
13804
14842
|
this.onAgentState("queued");
|
|
14843
|
+
if (this.tree) {
|
|
14844
|
+
this.noteTalk("them", this.heard);
|
|
14845
|
+
this.leafReady = this.turnEndLeaf();
|
|
14846
|
+
}
|
|
13805
14847
|
}
|
|
13806
14848
|
/** The main answer's streamed text (before its audio): channel-1 is responding — no bridge from here on. */
|
|
13807
14849
|
onMainTokens(text) {
|
|
@@ -13821,7 +14863,7 @@ var SecondChannel = class {
|
|
|
13821
14863
|
try {
|
|
13822
14864
|
this.o.cancel();
|
|
13823
14865
|
} catch (e) {
|
|
13824
|
-
|
|
14866
|
+
log29.warn(`cancel threw: ${e.message}`);
|
|
13825
14867
|
}
|
|
13826
14868
|
}
|
|
13827
14869
|
}
|
|
@@ -13832,6 +14874,10 @@ var SecondChannel = class {
|
|
|
13832
14874
|
this.cutLine = null;
|
|
13833
14875
|
return !!c && c === text.trim();
|
|
13834
14876
|
}
|
|
14877
|
+
/** Another fill line (progress) already covers this turn's wait: no bridge after it. */
|
|
14878
|
+
covered() {
|
|
14879
|
+
this.bridged = true;
|
|
14880
|
+
}
|
|
13835
14881
|
/** Resolves when the in-flight line finished playing (immediately when none) — hosts enqueue channel-1 audio behind it. */
|
|
13836
14882
|
drain() {
|
|
13837
14883
|
return this.inflight ?? Promise.resolve();
|
|
@@ -13866,6 +14912,7 @@ var SecondChannel = class {
|
|
|
13866
14912
|
bridgeBlock() {
|
|
13867
14913
|
const w = words3(this.heard), intent = turnIntent(this.heard);
|
|
13868
14914
|
if (!this.lang) return "language unknown";
|
|
14915
|
+
if (this.ownWords) return this.blocked("bridge");
|
|
13869
14916
|
if (intent === "closer") return "no answer due";
|
|
13870
14917
|
if (this.late) return this.blocked("bridge");
|
|
13871
14918
|
const why = intent === "ask" ? w.length <= 6 && isPhatic(this.heard, this.bank) ? "phatic" : "" : this.shortTurn(w.length);
|
|
@@ -13890,7 +14937,7 @@ var SecondChannel = class {
|
|
|
13890
14937
|
if (!this.lang) return "silence";
|
|
13891
14938
|
if (trigger === "working") return this.bridgeBlock() ? "silence" : "bridge";
|
|
13892
14939
|
if (w.length <= this.o.shortTurnWords || w.length <= 6 && isPhatic(this.heard, this.bank)) return "silence";
|
|
13893
|
-
const k = trigger === "interim" ? this.backchannelMoment(w.length, breath) && (!this.o.anticipate || !this.plan || !silentTurn(this.plan) && (!!cue || !this.o.cueOnly && this.plan.density !== "low")) ? "backchannel" : null : trigger === "pause" ? pauseMs >= this.o.fillerPauseMs && w.length >= this.o.fillerMinWords ? "filler" : null : null;
|
|
14940
|
+
const k = trigger === "interim" ? this.backchannelMoment(w.length, breath) && (!this.o.anticipate || !this.plan || !silentTurn(this.plan) && (!!cue || !this.o.cueOnly && this.plan.density !== "low")) ? "backchannel" : null : trigger === "pause" ? pauseMs >= this.o.fillerPauseMs && w.length >= this.o.fillerMinWords && !(this.ownWords && /[.!?…]$/.test(this.heard)) ? "filler" : null : null;
|
|
13894
14941
|
return k && this.allowed(k) ? k : "silence";
|
|
13895
14942
|
}
|
|
13896
14943
|
/** Plan (or, with `heard`, revise) the backchannels of the user's turn — async, never on the hot path. */
|
|
@@ -13911,6 +14958,106 @@ var SecondChannel = class {
|
|
|
13911
14958
|
this.o.onPlan(p, !!heard, Math.round(this.o.now() - t0));
|
|
13912
14959
|
});
|
|
13913
14960
|
}
|
|
14961
|
+
noteTalk(who, text) {
|
|
14962
|
+
this.talk = [...this.talk, `${who}: ${text.trim().slice(-300)}`].slice(-this.o.talkLines);
|
|
14963
|
+
}
|
|
14964
|
+
/** Grow the tree off the hot path. A tree that lands after its turn is still the best guess: it replaces what is live. */
|
|
14965
|
+
requestTree(heard) {
|
|
14966
|
+
if (!this.tree || !this.o.grow || !this.lang || this.growing) return;
|
|
14967
|
+
this.growing = true;
|
|
14968
|
+
this.growTries++;
|
|
14969
|
+
const t0 = this.o.now(), ac = new AbortController(), tree = this.tree;
|
|
14970
|
+
const ctx = { talk: heard ? [...this.talk, `them (still talking): ${heard.slice(-240)}`] : [...this.talk], lang: detectLanguage(this.talk.join(" ").replace(/\b(them|you):/g, "")) ?? this.lang, recent: this.leaves.slice(-this.o.leafMemory), ...this.o.persona ? { persona: this.o.persona } : {} };
|
|
14971
|
+
const late = setTimeout(() => ac.abort(), this.o.growBudgetMs);
|
|
14972
|
+
void this.o.grow(ctx, ac.signal).then((r) => {
|
|
14973
|
+
if (r.nodes.length) tree.grow(r.nodes, this.userSpeaking ? words3(this.heard).length : 0);
|
|
14974
|
+
this.o.onTree({ ms: Math.round(this.o.now() - t0), branches: r.nodes.length, lines: r.nodes.flatMap((n) => [n.line, ...n.next.map((k) => k.line)]), ...r.tokens ? { tokens: r.tokens } : {} });
|
|
14975
|
+
}, (e) => {
|
|
14976
|
+
log29.warn(`tree failed: ${e?.message ?? e}`);
|
|
14977
|
+
this.o.onTree({ ms: Math.round(this.o.now() - t0), branches: 0, error: String(e?.message ?? e) });
|
|
14978
|
+
}).finally(() => {
|
|
14979
|
+
clearTimeout(late);
|
|
14980
|
+
this.growing = false;
|
|
14981
|
+
});
|
|
14982
|
+
}
|
|
14983
|
+
/** Text match on the partial (no model). barge_in plays now, in_between at the next breath; turn_end waits in the tree. */
|
|
14984
|
+
treeStep(t) {
|
|
14985
|
+
const hit = this.tree.step(t);
|
|
14986
|
+
if (!hit) return;
|
|
14987
|
+
if (hit.node.moment === "barge_in") return this.playLeaf(hit, "interim", "backchannel");
|
|
14988
|
+
if (this.leafTimer) clearTimeout(this.leafTimer);
|
|
14989
|
+
const at = words3(t).length;
|
|
14990
|
+
this.leafTimer = setTimeout(() => {
|
|
14991
|
+
this.leafTimer = null;
|
|
14992
|
+
if (this.userSpeaking && words3(this.heard).length - at < this.o.leafStaleWords) this.playLeaf(hit, "interim", "backchannel");
|
|
14993
|
+
}, this.o.cuePauseMs);
|
|
14994
|
+
}
|
|
14995
|
+
/** A breath with no cue hit: the quick model may name the leaf that fits the words just said. Its answer plays only if
|
|
14996
|
+
* they have not moved on. */
|
|
14997
|
+
armBreath(t) {
|
|
14998
|
+
if (this.breathTimer) clearTimeout(this.breathTimer);
|
|
14999
|
+
if (!this.o.pickLeaf || !this.o.aec) return;
|
|
15000
|
+
this.breathTimer = setTimeout(() => {
|
|
15001
|
+
this.breathTimer = null;
|
|
15002
|
+
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;
|
|
15004
|
+
this.picking = true;
|
|
15005
|
+
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
|
+
void withBudget(this.o.pickLeaf(t, lines, ac.signal), this.o.pickBudgetMs, "breath pick", ac).then((i) => {
|
|
15007
|
+
this.picking = false;
|
|
15008
|
+
const node = typeof i === "number" ? nodes[i] : void 0, hit = node && tree.nodes.includes(node) ? tree.picked(tree.nodes.indexOf(node), this.heard) : null;
|
|
15009
|
+
if (!hit || !this.userSpeaking || words3(this.heard).length - n >= this.o.leafStaleWords) return;
|
|
15010
|
+
tree.take(hit.node, hit.conf);
|
|
15011
|
+
this.playLeaf(hit, "interim", "backchannel");
|
|
15012
|
+
});
|
|
15013
|
+
}, this.o.leafBreathMs);
|
|
15014
|
+
}
|
|
15015
|
+
/** The user's turn went to MAIN: the armed leaf at once, else the quick model may name one in time. It plays only while
|
|
15016
|
+
* MAIN is still silent, as the turn's first sound. */
|
|
15017
|
+
async turnEndLeaf() {
|
|
15018
|
+
const tree = this.tree, gen = this.gen, heard = this.heard;
|
|
15019
|
+
let hit = tree.turnLeaf();
|
|
15020
|
+
if (!hit && this.o.pickLeaf && !tree.empty) {
|
|
15021
|
+
const ac = new AbortController(), lines = tree.nodes.map((n, i2) => ({ i: i2, line: n.line, when: n.when }));
|
|
15022
|
+
const i = await withBudget(this.o.pickLeaf(heard, lines, ac.signal), this.o.pickBudgetMs, "leaf pick", ac);
|
|
15023
|
+
if (typeof i === "number" && i >= 0) hit = tree.picked(i, heard);
|
|
15024
|
+
}
|
|
15025
|
+
tree.advance();
|
|
15026
|
+
if (hit?.node.moment === "ask") {
|
|
15027
|
+
if (gen === this.gen && echoesHeard(hit.node.line, heard)) this.askLeaf = { gen, text: hit.node.line };
|
|
15028
|
+
return;
|
|
15029
|
+
}
|
|
15030
|
+
if (hit && gen === this.gen && (this.main === "queued" || this.main === "thinking")) {
|
|
15031
|
+
this.playLeaf(hit, "working", "bridge");
|
|
15032
|
+
if (this.lastKind === "bridge" && this.bridged) {
|
|
15033
|
+
this.bridged = false;
|
|
15034
|
+
this.lastKind = null;
|
|
15035
|
+
if (this.bridgeTimer) clearTimeout(this.bridgeTimer);
|
|
15036
|
+
this.bridgeTimer = setTimeout(() => {
|
|
15037
|
+
this.bridgeTimer = null;
|
|
15038
|
+
void this.consider("working");
|
|
15039
|
+
}, this.o.minGapMs);
|
|
15040
|
+
}
|
|
15041
|
+
}
|
|
15042
|
+
}
|
|
15043
|
+
leafGated() {
|
|
15044
|
+
return !this.lang || this.audioLocked || !!this.inflight || this.o.now() - this.lastAt < this.o.minGapMs || this.capped();
|
|
15045
|
+
}
|
|
15046
|
+
/** A leaf's gates: never over MAIN's audio or another line, spaced, capped; never a repeat. */
|
|
15047
|
+
playLeaf(hit, trigger, kind) {
|
|
15048
|
+
const { moment } = hit.node, conf = Math.round(hit.conf * 100) / 100;
|
|
15049
|
+
const tagged = hit.node.line !== lineWords(hit.node.line);
|
|
15050
|
+
const line = tagged && this.sinceTag < this.o.tagEvery ? lineWords(hit.node.line) : hit.node.line;
|
|
15051
|
+
const gated = !line || this.leafGated() || this.leaves.includes(leafKey(line));
|
|
15052
|
+
const said = !gated && this.say(kind, line);
|
|
15053
|
+
if (said) {
|
|
15054
|
+
this.leaves = [...this.leaves, leafKey(line)].slice(-this.o.leafMemory);
|
|
15055
|
+
this.sinceTag = line === hit.node.line && tagged ? 0 : this.sinceTag + 1;
|
|
15056
|
+
}
|
|
15057
|
+
const e = { trigger, rule: kind, kind, text: line, ms: 0, moment, conf, acted: gated ? "gated" : said ? "spoken" : "skipped" };
|
|
15058
|
+
this.o.onEvent(e);
|
|
15059
|
+
this.o.onLeaf(e);
|
|
15060
|
+
}
|
|
13914
15061
|
/** A clause boundary mid-floor: a breath or a comma-like close. Not a question (it wants the answer, not "mm-hm"), and a
|
|
13915
15062
|
* sentence end only deep into a story — a short request's end is the end of their turn (replay: "mm-hm" right after
|
|
13916
15063
|
* "Check hacker news for recent news.", just before the final). */
|
|
@@ -13933,7 +15080,7 @@ var SecondChannel = class {
|
|
|
13933
15080
|
const opts = this.options;
|
|
13934
15081
|
if (opts.mode === "chatty" && !opts.aec && !this.degradedLogged) {
|
|
13935
15082
|
this.degradedLogged = true;
|
|
13936
|
-
|
|
15083
|
+
log29.info("chatty mode without echo cancellation \u2014 normal timing (our lines would re-trigger the mic)");
|
|
13937
15084
|
}
|
|
13938
15085
|
const rule = this.rule(trigger, pauseMs, breath, cue);
|
|
13939
15086
|
if (rule === "silence") {
|
|
@@ -13952,7 +15099,7 @@ var SecondChannel = class {
|
|
|
13952
15099
|
const d = await withBudget(this.o.decide(this.tail(), { ...this.state(pauseMs), rule }, ac.signal), this.o.decisionBudgetMs, "channel decision", ac);
|
|
13953
15100
|
choice = d?.choice;
|
|
13954
15101
|
if (choice && choice in CHANNEL_ACTIONS && (choice === "silence" || this.allowed(choice))) kind = choice;
|
|
13955
|
-
else if (choice && choice in CHANNEL_ACTIONS)
|
|
15102
|
+
else if (choice && choice in CHANNEL_ACTIONS) log29.debug(`decider chose ${choice} for a ${rule} moment \u2014 the rule stands`);
|
|
13956
15103
|
}
|
|
13957
15104
|
if (kind === "silence") return ev({ choice, acted: "silence" });
|
|
13958
15105
|
if (stale()) return ev({ choice, kind, acted: "stale" });
|
|
@@ -13984,15 +15131,28 @@ var SecondChannel = class {
|
|
|
13984
15131
|
/** Start writing this turn's bridge now, so it may be ready by bridgeDelayMs. The bridge never waits for it. */
|
|
13985
15132
|
prefetchBridge() {
|
|
13986
15133
|
this.bridgeDraft = null;
|
|
13987
|
-
|
|
13988
|
-
|
|
13989
|
-
|
|
13990
|
-
|
|
13991
|
-
|
|
13992
|
-
|
|
13993
|
-
|
|
15134
|
+
this.askLeaf = null;
|
|
15135
|
+
this.bridgeReady = Promise.resolve();
|
|
15136
|
+
if (!this.o.generate || !(this.ownWords || this.o.generateFor.includes("bridge")) || !this.lang || !this.heard) return;
|
|
15137
|
+
const gen = this.gen, ac = new AbortController(), own = this.ownWords, heard = this.heard;
|
|
15138
|
+
const ctx = { kind: "bridge", heard: this.tail(), state: this.main, avoid: [...this.recent], lang: this.lang, ...own ? { ask: true } : {} };
|
|
15139
|
+
this.bridgeReady = withBudget(this.o.generate(ctx, ac.signal), own ? this.o.bridgeBudgetMs : Math.max(this.o.generateBudgetMs, this.o.bridgeDelayMs), "bridge generate", ac).then((raw) => {
|
|
15140
|
+
const text = own && /^\s*none\b/i.test(raw ?? "") ? "" : cleanLine(raw ?? "", ctx.lang);
|
|
15141
|
+
if (text && gen === this.gen && (!own || echoesHeard(text, heard))) this.bridgeDraft = { gen, text };
|
|
15142
|
+
}).catch((e) => log29.debug(`bridge prefetch failed: ${e?.message ?? e}`));
|
|
13994
15143
|
}
|
|
13995
15144
|
async line(kind, react = true) {
|
|
15145
|
+
if (kind === "bridge" && this.ownWords) {
|
|
15146
|
+
const gen = this.gen;
|
|
15147
|
+
await Promise.all([this.leafReady, this.bridgeReady]);
|
|
15148
|
+
if (gen !== this.gen) return "";
|
|
15149
|
+
const leaf = this.askLeaf?.gen === gen ? this.askLeaf.text : "", d = this.bridgeDraft?.gen === gen ? this.bridgeDraft.text : "";
|
|
15150
|
+
if (leaf && !this.leaves.includes(leafKey(leaf))) {
|
|
15151
|
+
this.leaves = [...this.leaves, leafKey(leaf)].slice(-this.o.leafMemory);
|
|
15152
|
+
return leaf;
|
|
15153
|
+
}
|
|
15154
|
+
return d && !this.recent.some((r) => norm3(r) === norm3(d)) ? d : "";
|
|
15155
|
+
}
|
|
13996
15156
|
if (kind === "bridge" && this.o.instantBridge) {
|
|
13997
15157
|
const d = this.bridgeDraft?.gen === this.gen ? this.bridgeDraft.text : "";
|
|
13998
15158
|
this.bridgeDraft = null;
|
|
@@ -14006,13 +15166,14 @@ var SecondChannel = class {
|
|
|
14006
15166
|
if (raw === null) ac.abort();
|
|
14007
15167
|
const text = cleanLine(raw ?? "", this.lang);
|
|
14008
15168
|
if (text && !this.recent.some((r) => norm3(r) === norm3(text))) return text;
|
|
14009
|
-
if (text)
|
|
15169
|
+
if (text) log29.debug(`generated "${text}" repeats a recent line \u2014 canned`);
|
|
14010
15170
|
}
|
|
14011
15171
|
return this.pick(kind);
|
|
14012
15172
|
}
|
|
14013
15173
|
/** Canned rotation in the user's language: never one of the last `bankExclude` picks of this kind, nor a recent
|
|
14014
15174
|
* line. '' = no bank for this language yet (silence). */
|
|
14015
15175
|
pick(kind) {
|
|
15176
|
+
if (this.ownWords && kind !== "backchannel") return "";
|
|
14016
15177
|
const pool = this.bank?.[kind] ?? [];
|
|
14017
15178
|
if (!pool.length) return "";
|
|
14018
15179
|
const hist = this.picks.get(kind) ?? [];
|
|
@@ -14028,7 +15189,7 @@ var SecondChannel = class {
|
|
|
14028
15189
|
try {
|
|
14029
15190
|
r = this.o.speak(text, kind);
|
|
14030
15191
|
} catch (e) {
|
|
14031
|
-
|
|
15192
|
+
log29.warn(`speak threw \u2014 line dropped: ${e.message}`);
|
|
14032
15193
|
return false;
|
|
14033
15194
|
}
|
|
14034
15195
|
if (r === false) return false;
|
|
@@ -14042,7 +15203,7 @@ var SecondChannel = class {
|
|
|
14042
15203
|
if (words4) this.o.show(words4, kind);
|
|
14043
15204
|
const p = Promise.resolve(r).then(() => {
|
|
14044
15205
|
}, (e) => {
|
|
14045
|
-
|
|
15206
|
+
log29.warn(`channel line playback failed: ${e?.message ?? e}`);
|
|
14046
15207
|
}).finally(() => {
|
|
14047
15208
|
if (this.inflight === p) this.inflight = null;
|
|
14048
15209
|
});
|
|
@@ -14086,7 +15247,9 @@ function channelLine(line, kind) {
|
|
|
14086
15247
|
}
|
|
14087
15248
|
function fastLineGenerator(ai, model, gender) {
|
|
14088
15249
|
return async (c, signal) => {
|
|
14089
|
-
const prompt =
|
|
15250
|
+
const prompt = c.ask ? `A live voice call. The user just said: "${c.heard}"
|
|
15251
|
+
If this asks you to look up, check, find or do something that takes a while, write ONE spoken line of 2-5 words in ${languageName(c.lang)} that names the thing in their own words and says you are on it ("Hacker News, one sec."). If it is small talk, a statement, or something you can answer at once, write: none
|
|
15252
|
+
${personaHint(gender)} Output only the line.` : `You voice the tiny side-sounds of a live spoken conversation. Write ONE line of 1-5 words: ${KIND_HINT2[c.kind]}.
|
|
14090
15253
|
` + (c.react ? `Here the line may be ${REACT_HINT}.
|
|
14091
15254
|
` : "") + (c.tags ? `${TAG_HINT}
|
|
14092
15255
|
` : "") + `The user just said: "${c.heard}"
|
|
@@ -14106,7 +15269,11 @@ function closedQuestion(reply) {
|
|
|
14106
15269
|
}
|
|
14107
15270
|
|
|
14108
15271
|
// src/voice/call.ts
|
|
14109
|
-
var
|
|
15272
|
+
var log30 = forComponent("VoiceCall");
|
|
15273
|
+
function newWords(text, turn) {
|
|
15274
|
+
const said = new Set(norm3(turn).split(" "));
|
|
15275
|
+
return norm3(text).split(" ").filter((w) => w && !said.has(w)).length;
|
|
15276
|
+
}
|
|
14110
15277
|
var VoiceCallOptions = class {
|
|
14111
15278
|
/** Host runs MAIN with exactly `text` (a replace: roll the `replaces` turn back first). */
|
|
14112
15279
|
sendTurn = () => {
|
|
@@ -14181,7 +15348,7 @@ var VoiceCall = class {
|
|
|
14181
15348
|
cancel: () => o.cancel(),
|
|
14182
15349
|
now: o.now
|
|
14183
15350
|
});
|
|
14184
|
-
this.progress = new Progress({ ...ai ? { generate: fastProgressGenerator(ai, o.fastModel, o.gender) } : {}, gender: o.gender, now: o.now, ...o.progress });
|
|
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 });
|
|
14185
15352
|
this.judge = new OverlapJudge({ ...ai ? { model: overlapModelFor(ai) } : {}, now: o.now });
|
|
14186
15353
|
this.gate = new TurnGate({
|
|
14187
15354
|
now: o.now,
|
|
@@ -14233,9 +15400,10 @@ var VoiceCall = class {
|
|
|
14233
15400
|
if (typeof aec === "boolean") this.fill.options.aec = aec;
|
|
14234
15401
|
this.gate.speaking();
|
|
14235
15402
|
const c = this.cur;
|
|
14236
|
-
if (this.o.mode === "live" && c?.running && !c.worded && text.
|
|
14237
|
-
|
|
15403
|
+
if (this.o.mode === "live" && c?.running && !c.worded && newWords(text, c.text) >= 2) {
|
|
15404
|
+
log30.debug("user resumed before the reply \u2014 yielded");
|
|
14238
15405
|
this.barge();
|
|
15406
|
+
this.gate.reopen(c.id);
|
|
14239
15407
|
}
|
|
14240
15408
|
this.follow(text);
|
|
14241
15409
|
this.fill.onInterim(text);
|
|
@@ -14338,7 +15506,7 @@ var VoiceCall = class {
|
|
|
14338
15506
|
try {
|
|
14339
15507
|
this.o.cancel();
|
|
14340
15508
|
} catch (e) {
|
|
14341
|
-
|
|
15509
|
+
log30.warn(`cancel threw: ${e.message}`);
|
|
14342
15510
|
}
|
|
14343
15511
|
if (this.cur?.running) {
|
|
14344
15512
|
this.cur.running = false;
|
|
@@ -14425,7 +15593,7 @@ var VoiceCall = class {
|
|
|
14425
15593
|
try {
|
|
14426
15594
|
r = this.o.speak(text, kind);
|
|
14427
15595
|
} catch (e) {
|
|
14428
|
-
|
|
15596
|
+
log30.warn(`speak threw: ${e.message}`);
|
|
14429
15597
|
return false;
|
|
14430
15598
|
}
|
|
14431
15599
|
if (r === false) return false;
|
|
@@ -14476,7 +15644,8 @@ var VoiceCall = class {
|
|
|
14476
15644
|
async onTool(c, name, args) {
|
|
14477
15645
|
const lang = this.lang;
|
|
14478
15646
|
if (!lang || c.streamed) return;
|
|
14479
|
-
const line = await this.progress.line(name, args, lang);
|
|
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}` });
|
|
14480
15649
|
if (!line || this.cur !== c || c.streamed || !c.running || this.lang !== lang) return;
|
|
14481
15650
|
this.clearProgress();
|
|
14482
15651
|
const tryNow = () => {
|
|
@@ -14488,6 +15657,7 @@ var VoiceCall = class {
|
|
|
14488
15657
|
}
|
|
14489
15658
|
if (this.say(line, "progress") !== false) {
|
|
14490
15659
|
this.progress.spoke(line);
|
|
15660
|
+
this.fill.covered();
|
|
14491
15661
|
this.o.show(line, "progress");
|
|
14492
15662
|
}
|
|
14493
15663
|
};
|
|
@@ -14511,7 +15681,7 @@ init_logging();
|
|
|
14511
15681
|
init_platform();
|
|
14512
15682
|
import { spawn } from "child_process";
|
|
14513
15683
|
import { createHash } from "crypto";
|
|
14514
|
-
var
|
|
15684
|
+
var log31 = forComponent("mcp");
|
|
14515
15685
|
var PROTOCOL_VERSION = "2025-06-18";
|
|
14516
15686
|
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
14517
15687
|
var DEFAULT_CALL_TIMEOUT_MS = 30 * 6e4;
|
|
@@ -14543,7 +15713,7 @@ var StdioTransport = class {
|
|
|
14543
15713
|
proc.stdout.setEncoding("utf8");
|
|
14544
15714
|
proc.stdout.on("data", (chunk) => this.onData(chunk));
|
|
14545
15715
|
proc.stderr.setEncoding("utf8");
|
|
14546
|
-
proc.stderr.on("data", (chunk) =>
|
|
15716
|
+
proc.stderr.on("data", (chunk) => log31.debug(`[${command}] stderr:`, chunk.trimEnd()));
|
|
14547
15717
|
proc.on("exit", (code) => this.failAll(new Error(`MCP server "${command}" exited (code ${code})`)));
|
|
14548
15718
|
proc.on("error", (e) => this.failAll(e instanceof Error ? e : new Error(String(e))));
|
|
14549
15719
|
}
|
|
@@ -14557,7 +15727,7 @@ var StdioTransport = class {
|
|
|
14557
15727
|
try {
|
|
14558
15728
|
this.dispatch(JSON.parse(line));
|
|
14559
15729
|
} catch (e) {
|
|
14560
|
-
|
|
15730
|
+
log31.debug("dropping non-JSON line from MCP server:", line, e);
|
|
14561
15731
|
}
|
|
14562
15732
|
}
|
|
14563
15733
|
}
|
|
@@ -14567,7 +15737,7 @@ var StdioTransport = class {
|
|
|
14567
15737
|
try {
|
|
14568
15738
|
p2?.onProgress?.(msg.params ?? {});
|
|
14569
15739
|
} catch (e) {
|
|
14570
|
-
|
|
15740
|
+
log31.debug("onProgress handler threw", e);
|
|
14571
15741
|
}
|
|
14572
15742
|
return;
|
|
14573
15743
|
}
|
|
@@ -14605,7 +15775,7 @@ var StdioTransport = class {
|
|
|
14605
15775
|
const onAbort = () => {
|
|
14606
15776
|
if (!this.pending.delete(id)) return;
|
|
14607
15777
|
clearTimeout(timer);
|
|
14608
|
-
this.notify("notifications/cancelled", { requestId: id, reason: String(signal?.reason?.message ?? "cancelled") }).catch((e) =>
|
|
15778
|
+
this.notify("notifications/cancelled", { requestId: id, reason: String(signal?.reason?.message ?? "cancelled") }).catch((e) => log31.debug("cancel notify failed", e));
|
|
14609
15779
|
reject(abortError(signal));
|
|
14610
15780
|
};
|
|
14611
15781
|
const done = () => signal?.removeEventListener("abort", onAbort);
|
|
@@ -14629,7 +15799,7 @@ var StdioTransport = class {
|
|
|
14629
15799
|
try {
|
|
14630
15800
|
this.proc?.stdin?.end();
|
|
14631
15801
|
} catch (e) {
|
|
14632
|
-
|
|
15802
|
+
log31.debug("stdin end failed", e);
|
|
14633
15803
|
}
|
|
14634
15804
|
if (!(IS_WIN && killTree(this.proc?.pid, "SIGTERM"))) this.proc?.kill();
|
|
14635
15805
|
}
|
|
@@ -14664,7 +15834,7 @@ var HttpTransport = class {
|
|
|
14664
15834
|
return await this.post({ jsonrpc: "2.0", id, method, params: opts.onProgress ? withProgressToken(params, id) : params }, false, signal, opts.onProgress);
|
|
14665
15835
|
} catch (e) {
|
|
14666
15836
|
if (opts.signal?.aborted) {
|
|
14667
|
-
void this.notify("notifications/cancelled", { requestId: id, reason: String(opts.signal.reason?.message ?? "cancelled") }).catch((err2) =>
|
|
15837
|
+
void this.notify("notifications/cancelled", { requestId: id, reason: String(opts.signal.reason?.message ?? "cancelled") }).catch((err2) => log31.debug("cancel notify failed", err2));
|
|
14668
15838
|
throw abortError(opts.signal);
|
|
14669
15839
|
}
|
|
14670
15840
|
throw e;
|
|
@@ -14707,7 +15877,7 @@ function sseMessage(line) {
|
|
|
14707
15877
|
try {
|
|
14708
15878
|
return JSON.parse(trimmed.slice(5).trim());
|
|
14709
15879
|
} catch (e) {
|
|
14710
|
-
|
|
15880
|
+
log31.debug("skipping unparseable SSE data line", e);
|
|
14711
15881
|
return void 0;
|
|
14712
15882
|
}
|
|
14713
15883
|
}
|
|
@@ -14717,7 +15887,7 @@ async function readSseResponse(resp, id, onProgress) {
|
|
|
14717
15887
|
try {
|
|
14718
15888
|
onProgress?.(obj.params ?? {});
|
|
14719
15889
|
} catch (e) {
|
|
14720
|
-
|
|
15890
|
+
log31.debug("onProgress handler threw", e);
|
|
14721
15891
|
}
|
|
14722
15892
|
return void 0;
|
|
14723
15893
|
}
|
|
@@ -14816,7 +15986,7 @@ async function mountWithDeadline(name, cfg, mountTimeoutMs) {
|
|
|
14816
15986
|
return { name, client, tools, specs, serverInfo: init?.serverInfo, config: cfg };
|
|
14817
15987
|
})(), mountTimeoutMs, name);
|
|
14818
15988
|
} catch (e) {
|
|
14819
|
-
await client.close().catch((err2) =>
|
|
15989
|
+
await client.close().catch((err2) => log31.debug(`close after failed mount of "${name}": ${err2}`));
|
|
14820
15990
|
throw e;
|
|
14821
15991
|
}
|
|
14822
15992
|
}
|
|
@@ -14827,15 +15997,15 @@ function validEntries(servers) {
|
|
|
14827
15997
|
return Object.entries(servers).filter(([name, cfg]) => {
|
|
14828
15998
|
if (!cfg || cfg.disabled) return false;
|
|
14829
15999
|
if (!cfg.command && !cfg.url) {
|
|
14830
|
-
|
|
16000
|
+
log31.warn(`MCP server "${name}" needs a command (stdio) or url (http) \u2014 skipping`);
|
|
14831
16001
|
return false;
|
|
14832
16002
|
}
|
|
14833
16003
|
return true;
|
|
14834
16004
|
});
|
|
14835
16005
|
}
|
|
14836
16006
|
function logMountFailure(name, e) {
|
|
14837
|
-
if (e instanceof McpAuthError)
|
|
14838
|
-
else
|
|
16007
|
+
if (e instanceof McpAuthError) log31.warn(`MCP "${name}" needs-auth: HTTP ${e.status} \u2014 set bearerToken or headers in its config; skipping`);
|
|
16008
|
+
else log31.error(`MCP server "${name}" failed to mount: ${e?.message ?? e}`);
|
|
14839
16009
|
}
|
|
14840
16010
|
async function mountMcpServers(servers = {}, opts = {}) {
|
|
14841
16011
|
const entries = validEntries(servers);
|
|
@@ -14845,7 +16015,7 @@ async function mountMcpServers(servers = {}, opts = {}) {
|
|
|
14845
16015
|
const name = entries[i][0];
|
|
14846
16016
|
if (r.status === "fulfilled") {
|
|
14847
16017
|
out.push(r.value);
|
|
14848
|
-
|
|
16018
|
+
log31.debug(`MCP "${name}" mounted \u2014 ${r.value.tools.length} tool(s)${r.value.serverInfo?.name ? ` from ${r.value.serverInfo.name}` : ""}`);
|
|
14849
16019
|
} else logMountFailure(name, r.reason);
|
|
14850
16020
|
});
|
|
14851
16021
|
return out;
|
|
@@ -14885,7 +16055,7 @@ var McpPool = class {
|
|
|
14885
16055
|
const prev = this.warm.get(key);
|
|
14886
16056
|
if (prev) {
|
|
14887
16057
|
clearTimeout(prev.timer);
|
|
14888
|
-
if (prev.client !== client) void prev.client.close().catch((err2) =>
|
|
16058
|
+
if (prev.client !== client) void prev.client.close().catch((err2) => log31.debug(`warm-pool replace close failed: ${err2}`));
|
|
14889
16059
|
}
|
|
14890
16060
|
const e = { client, timer: void 0 };
|
|
14891
16061
|
this.warm.set(key, e);
|
|
@@ -14902,7 +16072,7 @@ var McpPool = class {
|
|
|
14902
16072
|
const e = this.warm.get(key);
|
|
14903
16073
|
if (!e) return;
|
|
14904
16074
|
this.warm.delete(key);
|
|
14905
|
-
await e.client.close().catch((err2) =>
|
|
16075
|
+
await e.client.close().catch((err2) => log31.debug(`warm-pool evict close failed: ${err2}`));
|
|
14906
16076
|
}
|
|
14907
16077
|
async closeAll() {
|
|
14908
16078
|
for (const e of this.warm.values()) {
|
|
@@ -15148,7 +16318,7 @@ init_tools_shell();
|
|
|
15148
16318
|
// src/tools.notify.ts
|
|
15149
16319
|
init_logging();
|
|
15150
16320
|
import { execFile as execFile2 } from "child_process";
|
|
15151
|
-
var
|
|
16321
|
+
var log33 = forComponent("notify");
|
|
15152
16322
|
var WIN_TOAST = [
|
|
15153
16323
|
"[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null",
|
|
15154
16324
|
"$t = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)",
|
|
@@ -15179,7 +16349,7 @@ function makeNotifyTool(opts = {}) {
|
|
|
15179
16349
|
return new Promise((resolve6) => {
|
|
15180
16350
|
run(argv[0], argv[1], { timeout: 5e3, ...extra }, (e) => {
|
|
15181
16351
|
if (e) {
|
|
15182
|
-
|
|
16352
|
+
log33.debug("notification failed", e);
|
|
15183
16353
|
resolve6(`Notification failed: ${e.message}`);
|
|
15184
16354
|
} else resolve6("Notification shown.");
|
|
15185
16355
|
});
|
|
@@ -15196,7 +16366,7 @@ init_logging();
|
|
|
15196
16366
|
import { existsSync as existsSync2, readFileSync as readFileSync5 } from "fs";
|
|
15197
16367
|
import { homedir } from "os";
|
|
15198
16368
|
import { delimiter as delimiter2 } from "path";
|
|
15199
|
-
var
|
|
16369
|
+
var log34 = forComponent("cli-util");
|
|
15200
16370
|
function extraDirs(env = process.env) {
|
|
15201
16371
|
return (env.AGENTX_EXTRA_DIRS || "").split(delimiter2).map((d) => d.trim()).filter(Boolean);
|
|
15202
16372
|
}
|
|
@@ -15216,7 +16386,7 @@ function parseJson(text, fallback, what = "json") {
|
|
|
15216
16386
|
try {
|
|
15217
16387
|
return JSON.parse(text);
|
|
15218
16388
|
} catch (e) {
|
|
15219
|
-
|
|
16389
|
+
log34.debug(`parseJson(${what}) failed: ${e.message}`);
|
|
15220
16390
|
return fallback;
|
|
15221
16391
|
}
|
|
15222
16392
|
}
|
|
@@ -15226,7 +16396,7 @@ function readJsonFile(path, fallback) {
|
|
|
15226
16396
|
try {
|
|
15227
16397
|
text = readFileSync5(path, "utf8");
|
|
15228
16398
|
} catch (e) {
|
|
15229
|
-
|
|
16399
|
+
log34.debug(`readJsonFile(${path}) unreadable: ${e.message}`);
|
|
15230
16400
|
return fallback;
|
|
15231
16401
|
}
|
|
15232
16402
|
return parseJson(text, fallback, path);
|
|
@@ -15536,11 +16706,11 @@ init_logging();
|
|
|
15536
16706
|
init_llm();
|
|
15537
16707
|
init_platform();
|
|
15538
16708
|
import { spawn as spawn2, spawnSync as spawnSync3 } from "child_process";
|
|
15539
|
-
import { existsSync as existsSync4, mkdirSync as mkdirSync5, readFileSync as readFileSync6, statSync as statSync6 } from "fs";
|
|
16709
|
+
import { appendFileSync, existsSync as existsSync4, mkdirSync as mkdirSync5, readFileSync as readFileSync6, statSync as statSync6 } from "fs";
|
|
15540
16710
|
import { homedir as homedir3 } from "os";
|
|
15541
16711
|
import { dirname as dirname4, join as join8 } from "path";
|
|
15542
16712
|
import { fileURLToPath } from "url";
|
|
15543
|
-
var
|
|
16713
|
+
var log35 = forComponent("VoiceIO");
|
|
15544
16714
|
var now3 = () => performance.now();
|
|
15545
16715
|
var Player = class {
|
|
15546
16716
|
proc = null;
|
|
@@ -15554,7 +16724,7 @@ var Player = class {
|
|
|
15554
16724
|
["-loglevel", "quiet", "-nodisp", "-fflags", "nobuffer", "-flags", "low_delay", "-probesize", "32", "-f", "s16le", "-ar", String(TTS_SAMPLE_RATE), "-ch_layout", "mono", "-i", "-"],
|
|
15555
16725
|
{ stdio: ["pipe", "ignore", "ignore"] }
|
|
15556
16726
|
);
|
|
15557
|
-
this.proc.on("error", (e) =>
|
|
16727
|
+
this.proc.on("error", (e) => log35.warn(`ffplay error: ${e.message}`));
|
|
15558
16728
|
this.proc.stdin.on("error", () => {
|
|
15559
16729
|
});
|
|
15560
16730
|
this.bytesWritten = 0;
|
|
@@ -15592,7 +16762,7 @@ function detectFfmpegMic() {
|
|
|
15592
16762
|
const devices = [...audio.matchAll(/\[(\d+)\] (.+)/g)].map(([, idx, name]) => ({ idx, name: name.trim() }));
|
|
15593
16763
|
const mic = devices.find((d) => /microphone|built-in/i.test(d.name) && !/teams|blackhole|loopback/i.test(d.name)) ?? devices[0];
|
|
15594
16764
|
if (!mic) throw new Error("no audio input device found");
|
|
15595
|
-
|
|
16765
|
+
log35.debug(`ffmpeg mic: [${mic.idx}] ${mic.name}`);
|
|
15596
16766
|
return `:${mic.idx}`;
|
|
15597
16767
|
}
|
|
15598
16768
|
function detectDshowMic() {
|
|
@@ -15603,7 +16773,7 @@ function detectDshowMic() {
|
|
|
15603
16773
|
const devices = [...(r.stderr ?? "").matchAll(/"([^"]+)"\s*\(audio\)/g)].map((m) => m[1]);
|
|
15604
16774
|
const mic = devices.find((d) => !/stereo mix|loopback|virtual|cable|teams/i.test(d)) ?? devices[0];
|
|
15605
16775
|
if (!mic) throw new Error("no audio input device found (set MIC_DEVICE to the DirectShow device name)");
|
|
15606
|
-
|
|
16776
|
+
log35.debug(`ffmpeg dshow mic: ${mic}`);
|
|
15607
16777
|
return `audio=${mic}`;
|
|
15608
16778
|
}
|
|
15609
16779
|
function detectedInputDevice() {
|
|
@@ -15639,15 +16809,15 @@ function resolveAecBinary() {
|
|
|
15639
16809
|
if (existsSync4(bin) && statSync6(bin).mtimeMs >= statSync6(src).mtimeMs) return bin;
|
|
15640
16810
|
if (spawnSync3("which", ["swiftc"]).status !== 0) return null;
|
|
15641
16811
|
mkdirSync5(cacheDir, { recursive: true });
|
|
15642
|
-
|
|
16812
|
+
log35.info("compiling AEC mic helper (first run)\u2026");
|
|
15643
16813
|
const build = spawnSync3("swiftc", ["-O", "-o", bin, src, "-Xlinker", "-sectcreate", "-Xlinker", "__TEXT", "-Xlinker", "__info_plist", "-Xlinker", plist], { encoding: "utf8" });
|
|
15644
16814
|
if (build.status !== 0) {
|
|
15645
|
-
|
|
16815
|
+
log35.warn(`AEC build failed: ${build.stderr?.slice(0, 400)}`);
|
|
15646
16816
|
return null;
|
|
15647
16817
|
}
|
|
15648
16818
|
const sign = spawnSync3("codesign", ["-fs", "-", bin], { encoding: "utf8" });
|
|
15649
16819
|
if (sign.status !== 0) {
|
|
15650
|
-
|
|
16820
|
+
log35.warn(`codesign failed: ${sign.stderr?.slice(0, 200)}`);
|
|
15651
16821
|
return null;
|
|
15652
16822
|
}
|
|
15653
16823
|
return bin;
|
|
@@ -15689,17 +16859,17 @@ var NodeMicSource = class {
|
|
|
15689
16859
|
this.proc = spawn2(this.bin, [], { stdio: ["ignore", "pipe", "ignore"] });
|
|
15690
16860
|
} else {
|
|
15691
16861
|
if (!hasCommand("ffmpeg")) throw new Error("voice I/O unavailable: no AEC helper and no ffmpeg on PATH");
|
|
15692
|
-
|
|
16862
|
+
log35.info("mic: raw capture (no AEC) \u2014 echo handled heuristically; headphones recommended");
|
|
15693
16863
|
const input = process.platform === "win32" ? ["-f", "dshow", "-i", detectDshowMic()] : ["-f", "avfoundation", "-i", detectFfmpegMic()];
|
|
15694
16864
|
this.proc = spawn2(
|
|
15695
16865
|
"ffmpeg",
|
|
15696
16866
|
["-loglevel", "error", ...input, "-ar", String(STT_SAMPLE_RATE), "-ac", "1", "-f", "s16le", "-"],
|
|
15697
16867
|
{ stdio: ["ignore", "pipe", "pipe"], ...process.platform === "win32" ? { windowsHide: true } : {} }
|
|
15698
16868
|
);
|
|
15699
|
-
this.proc.stderr.on("data", (d) =>
|
|
16869
|
+
this.proc.stderr.on("data", (d) => log35.warn(`ffmpeg: ${String(d).trim()}`));
|
|
15700
16870
|
}
|
|
15701
16871
|
this.proc.on("exit", (c) => {
|
|
15702
|
-
if (c && !this.stopped)
|
|
16872
|
+
if (c && !this.stopped) log35.error(`mic capture exited (${c}) \u2014 check mic permission / MIC_DEVICE / MIC_AEC=0`);
|
|
15703
16873
|
});
|
|
15704
16874
|
this.proc.stdout.on("data", (chunk) => onChunk(chunk));
|
|
15705
16875
|
}
|
|
@@ -15765,7 +16935,7 @@ var AecDuplexAudio = class {
|
|
|
15765
16935
|
this.proc.stdin.on("error", () => {
|
|
15766
16936
|
});
|
|
15767
16937
|
this.proc.on("exit", (c) => {
|
|
15768
|
-
if (c && !this.stopped)
|
|
16938
|
+
if (c && !this.stopped) log35.error(`aec duplex audio exited (${c}) \u2014 check mic permission / MIC_AEC=0`);
|
|
15769
16939
|
});
|
|
15770
16940
|
this.proc.stdout.on("data", (chunk) => {
|
|
15771
16941
|
this.gotChunk = true;
|
|
@@ -15781,7 +16951,7 @@ var AecDuplexAudio = class {
|
|
|
15781
16951
|
openMicSettings();
|
|
15782
16952
|
this.onFatal?.("microphone permission denied \u2014 enable it in System Settings \u2192 Privacy & Security \u2192 Microphone for your terminal, then restart it");
|
|
15783
16953
|
}
|
|
15784
|
-
} else
|
|
16954
|
+
} else log35.debug(`mic-aec: ${s}`);
|
|
15785
16955
|
}
|
|
15786
16956
|
});
|
|
15787
16957
|
if (!this.noVpio && !this.triedFallback) {
|
|
@@ -15790,7 +16960,7 @@ var AecDuplexAudio = class {
|
|
|
15790
16960
|
this.triedFallback = true;
|
|
15791
16961
|
this.noVpio = true;
|
|
15792
16962
|
this._aec = false;
|
|
15793
|
-
|
|
16963
|
+
log35.warn("mic-aec: VPIO delivered no audio in 2.5s \u2014 falling back to non-VPIO capture (no AEC \u2192 half-duplex, no barge-in)");
|
|
15794
16964
|
this.onDegrade?.();
|
|
15795
16965
|
this.killProc();
|
|
15796
16966
|
this.spawnHelper();
|
|
@@ -15890,7 +17060,7 @@ var AecDuplexAudio = class {
|
|
|
15890
17060
|
stdin.write(pcm16k);
|
|
15891
17061
|
}
|
|
15892
17062
|
};
|
|
15893
|
-
var
|
|
17063
|
+
var matchVoiceCommand2 = (text) => {
|
|
15894
17064
|
const t = text.toLowerCase().replace(/[^a-z ]/g, " ").replace(/\s+/g, " ").trim().replace(/^(?:hey|ok|okay|please) /, "").replace(/ (?:please|now)$/, "");
|
|
15895
17065
|
return /^(voice off|mute|stop listening)( \1)*$/.test(t) ? "off" : null;
|
|
15896
17066
|
};
|
|
@@ -15961,7 +17131,7 @@ function bindAckInstinct(v, instinct) {
|
|
|
15961
17131
|
if (ev.kind === "ack_fired") instinct.ackOutcome(String(ev.phrase));
|
|
15962
17132
|
else if (ev.kind === "ack_skipped" || ev.kind === "ack_cancelled") instinct.ackOutcome(null);
|
|
15963
17133
|
} catch (e) {
|
|
15964
|
-
|
|
17134
|
+
log35.warn(`ack outcome tap failed: ${e instanceof Error ? e.message : e}`);
|
|
15965
17135
|
}
|
|
15966
17136
|
onDiag(ev);
|
|
15967
17137
|
};
|
|
@@ -16083,7 +17253,7 @@ function speakBlip(v, text) {
|
|
|
16083
17253
|
v.fireBackchannel();
|
|
16084
17254
|
return true;
|
|
16085
17255
|
} catch (e) {
|
|
16086
|
-
|
|
17256
|
+
log35.warn(`backchannel blip failed \u2014 line dropped: ${e.message}`);
|
|
16087
17257
|
return false;
|
|
16088
17258
|
} finally {
|
|
16089
17259
|
E.BACKCHANNELS = pool;
|
|
@@ -16102,7 +17272,7 @@ function channelInput(sc, m) {
|
|
|
16102
17272
|
}
|
|
16103
17273
|
if (m.type === "agent_state") {
|
|
16104
17274
|
if (typeof m.state === "string" && MAIN_STATES.has(m.state)) sc.onAgentState(m.state);
|
|
16105
|
-
else
|
|
17275
|
+
else log35.warn(`agent_state: unknown state ${JSON.stringify(m.state)} \u2014 ignored`);
|
|
16106
17276
|
return true;
|
|
16107
17277
|
}
|
|
16108
17278
|
return false;
|
|
@@ -16194,7 +17364,8 @@ function fakeVoiceParts(uttFile) {
|
|
|
16194
17364
|
offset = buf.length;
|
|
16195
17365
|
for (const line of fresh.split("\n")) {
|
|
16196
17366
|
const t = line.trim();
|
|
16197
|
-
if (t) stt.
|
|
17367
|
+
if (t.startsWith("~")) stt.onPartial(t.slice(1).trim());
|
|
17368
|
+
else if (t) stt.onUtterance(t, Date.now());
|
|
16198
17369
|
}
|
|
16199
17370
|
}, 100);
|
|
16200
17371
|
},
|
|
@@ -16207,11 +17378,13 @@ function fakeVoiceParts(uttFile) {
|
|
|
16207
17378
|
}
|
|
16208
17379
|
}
|
|
16209
17380
|
};
|
|
17381
|
+
const said = process.env.AGENTX_VOICE_FAKE_LOG;
|
|
16210
17382
|
const tts = { connect() {
|
|
16211
17383
|
}, newContext() {
|
|
16212
|
-
}, speak() {
|
|
17384
|
+
}, speak(text) {
|
|
17385
|
+
if (said) appendFileSync(said, JSON.stringify({ t: Date.now(), speak: text }) + "\n");
|
|
16213
17386
|
}, end() {
|
|
16214
|
-
tts.onDone?.();
|
|
17387
|
+
setTimeout(() => tts.onDone?.(), 20);
|
|
16215
17388
|
}, cancel() {
|
|
16216
17389
|
}, close() {
|
|
16217
17390
|
}, onAudio: (_) => {
|
|
@@ -16228,7 +17401,7 @@ function fakeVoiceParts(uttFile) {
|
|
|
16228
17401
|
init_logging();
|
|
16229
17402
|
import { appendFile, mkdirSync as mkdirSync6 } from "fs";
|
|
16230
17403
|
import { dirname as dirname5 } from "path";
|
|
16231
|
-
var
|
|
17404
|
+
var log36 = forComponent("VoiceDiag");
|
|
16232
17405
|
var JsonlDiagSink = class {
|
|
16233
17406
|
constructor(path) {
|
|
16234
17407
|
this.path = path;
|
|
@@ -16269,7 +17442,7 @@ var JsonlDiagSink = class {
|
|
|
16269
17442
|
disable(e) {
|
|
16270
17443
|
this.disabled = true;
|
|
16271
17444
|
this.queue.length = 0;
|
|
16272
|
-
|
|
17445
|
+
log36.debug(`voice diag sink disabled (${this.path}): ${e instanceof Error ? e.message : e}`);
|
|
16273
17446
|
}
|
|
16274
17447
|
/** True once a failure permanently disabled the sink (tests/inspection). */
|
|
16275
17448
|
get isDisabled() {
|
|
@@ -16281,6 +17454,38 @@ var JsonlDiagSink = class {
|
|
|
16281
17454
|
}
|
|
16282
17455
|
};
|
|
16283
17456
|
|
|
17457
|
+
// src/voice/greeting.ts
|
|
17458
|
+
init_logging();
|
|
17459
|
+
var log37 = forComponent("CallGreeting");
|
|
17460
|
+
var CallGreetingOptions = class {
|
|
17461
|
+
model = "groq/openai/gpt-oss-120b";
|
|
17462
|
+
persona = "";
|
|
17463
|
+
budgetMs = 900;
|
|
17464
|
+
fallback = "Hey, what's up?";
|
|
17465
|
+
};
|
|
17466
|
+
async function callGreeting(ai, options) {
|
|
17467
|
+
const o = { ...new CallGreetingOptions(), ...options }, ac = new AbortController();
|
|
17468
|
+
const prompt = `${o.persona ? `Your persona:
|
|
17469
|
+
${o.persona}
|
|
17470
|
+
|
|
17471
|
+
` : ""}You open a live voice call. Write ONE short spoken sentence that greets the caller and asks what they would like to do. Keep the persona's tone tags if it uses them. Output only the line.`;
|
|
17472
|
+
const reasoning = /gpt-oss/.test(o.model) ? { providerOptions: { reasoning_effort: "low" } } : {};
|
|
17473
|
+
let timer;
|
|
17474
|
+
const late = new Promise((r) => {
|
|
17475
|
+
timer = setTimeout(() => {
|
|
17476
|
+
ac.abort();
|
|
17477
|
+
r("");
|
|
17478
|
+
}, o.budgetMs);
|
|
17479
|
+
});
|
|
17480
|
+
const asked = Promise.resolve(ai.chat({ model: o.model, messages: [{ role: "user", content: prompt }], stream: false, signal: ac.signal, maxTokens: 200, ...reasoning })).then((r) => (r.content ?? "").trim().split("\n")[0].replace(/^["']|["']$/g, "")).catch((e) => {
|
|
17481
|
+
log37.warn(`greeting failed \u2014 canned line: ${e?.message ?? e}`);
|
|
17482
|
+
return "";
|
|
17483
|
+
});
|
|
17484
|
+
const line = await Promise.race([asked, late]);
|
|
17485
|
+
clearTimeout(timer);
|
|
17486
|
+
return line || o.fallback;
|
|
17487
|
+
}
|
|
17488
|
+
|
|
16284
17489
|
// cli/config.ts
|
|
16285
17490
|
import { homedir as homedir5 } from "os";
|
|
16286
17491
|
import { existsSync as existsSync5, readFileSync as readFileSync7 } from "fs";
|
|
@@ -16428,7 +17633,7 @@ function applyOverlay(cfg, file) {
|
|
|
16428
17633
|
// cli/hooks-config.ts
|
|
16429
17634
|
import { spawnSync as spawnSync4 } from "child_process";
|
|
16430
17635
|
init_platform();
|
|
16431
|
-
var
|
|
17636
|
+
var log38 = forComponent("hooks");
|
|
16432
17637
|
var escapeRegex = (s) => s.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
16433
17638
|
function ruleMatches(rule, toolName) {
|
|
16434
17639
|
if (!rule.tool || rule.tool === "*") return true;
|
|
@@ -16447,7 +17652,7 @@ function runCmd(rule, env) {
|
|
|
16447
17652
|
});
|
|
16448
17653
|
return { code: r.status ?? 1, out: ((r.stdout ?? "") + (r.stderr ?? "")).trim() };
|
|
16449
17654
|
} catch (e) {
|
|
16450
|
-
|
|
17655
|
+
log38.debug(`hook command failed: ${rule.command}`, e);
|
|
16451
17656
|
return { code: 1, out: String(e?.message ?? e) };
|
|
16452
17657
|
}
|
|
16453
17658
|
}
|
|
@@ -16555,7 +17760,7 @@ import { existsSync as existsSync6, mkdirSync as mkdirSync8, readFileSync as rea
|
|
|
16555
17760
|
import { homedir as homedir6 } from "os";
|
|
16556
17761
|
import { join as join11 } from "path";
|
|
16557
17762
|
init_platform();
|
|
16558
|
-
var
|
|
17763
|
+
var log39 = forComponent("session");
|
|
16559
17764
|
var globalDir = () => join11(homedir6(), ".agent", "sessions");
|
|
16560
17765
|
function indexLink(target, link2) {
|
|
16561
17766
|
try {
|
|
@@ -16618,7 +17823,7 @@ var SessionStore = class {
|
|
|
16618
17823
|
}
|
|
16619
17824
|
load(id) {
|
|
16620
17825
|
if (!this.safeId(id)) {
|
|
16621
|
-
|
|
17826
|
+
log39.debug(`rejecting unsafe session id: ${id}`);
|
|
16622
17827
|
return void 0;
|
|
16623
17828
|
}
|
|
16624
17829
|
const path = join11(this.dir, `${id}.json`);
|
|
@@ -16626,7 +17831,7 @@ var SessionStore = class {
|
|
|
16626
17831
|
try {
|
|
16627
17832
|
return JSON.parse(readFileSync8(path, "utf8"));
|
|
16628
17833
|
} catch (e) {
|
|
16629
|
-
|
|
17834
|
+
log39.debug(`unreadable session ${id} \u2014 ignoring`, e);
|
|
16630
17835
|
return void 0;
|
|
16631
17836
|
}
|
|
16632
17837
|
}
|
|
@@ -16639,7 +17844,7 @@ var SessionStore = class {
|
|
|
16639
17844
|
try {
|
|
16640
17845
|
metas.push(JSON.parse(readFileSync8(join11(this.dir, f), "utf8")).meta);
|
|
16641
17846
|
} catch (e) {
|
|
16642
|
-
|
|
17847
|
+
log39.debug(`skipping unreadable session file ${f}`, e);
|
|
16643
17848
|
}
|
|
16644
17849
|
}
|
|
16645
17850
|
return metas.sort((a, b) => b.updated - a.updated);
|
|
@@ -17071,7 +18276,7 @@ import { promisify } from "util";
|
|
|
17071
18276
|
import { writeFileSync as writeFileSync6, mkdirSync as mkdirSync9 } from "fs";
|
|
17072
18277
|
import { homedir as homedir7 } from "os";
|
|
17073
18278
|
import { join as join12, resolve as resolve4, sep as sep3, parse as parsePath } from "path";
|
|
17074
|
-
var
|
|
18279
|
+
var log40 = forComponent("checkpoints");
|
|
17075
18280
|
var exec = promisify(execFile4);
|
|
17076
18281
|
var DEFAULT_EXCLUDE = [".agent/", ".git/", "node_modules/", "dist/", "build/", ".next/", "target/", ".venv/", "__pycache__/", "*.log"];
|
|
17077
18282
|
function isBroadRoot(p) {
|
|
@@ -17119,7 +18324,7 @@ var ShadowRepo = class {
|
|
|
17119
18324
|
writeFileSync6(join12(this.gitDir, "info", "exclude"), this.exclude.join("\n") + "\n");
|
|
17120
18325
|
this.ready = true;
|
|
17121
18326
|
} catch (e) {
|
|
17122
|
-
|
|
18327
|
+
log40.debug(`git checkpoints unavailable for ${this.workTree}`, e);
|
|
17123
18328
|
this.ready = false;
|
|
17124
18329
|
}
|
|
17125
18330
|
return this.ready;
|
|
@@ -17130,7 +18335,7 @@ var ShadowRepo = class {
|
|
|
17130
18335
|
}
|
|
17131
18336
|
async commit(label, forced = []) {
|
|
17132
18337
|
await this.run("add", "-A");
|
|
17133
|
-
for (const p of forced) await this.run("add", "-f", "--", p).catch((e) =>
|
|
18338
|
+
for (const p of forced) await this.run("add", "-f", "--", p).catch((e) => log40.debug(`force-add failed: ${p}`, e));
|
|
17134
18339
|
await this.run("commit", "--allow-empty", "-q", "-m", label);
|
|
17135
18340
|
}
|
|
17136
18341
|
/** Inject the CURRENT (pre-edit) content of `paths` into the turn-open restore point by amending it.
|
|
@@ -17138,8 +18343,8 @@ var ShadowRepo = class {
|
|
|
17138
18343
|
* turn-boundary `add -A` would never have captured it. Amend (vs a new commit) keeps one restore
|
|
17139
18344
|
* point per turn, so the REPL's turn↔frame mapping stays intact. */
|
|
17140
18345
|
async amendForced(paths) {
|
|
17141
|
-
for (const p of paths) await this.run("add", "-f", "--", p).catch((e) =>
|
|
17142
|
-
await this.run("commit", "--amend", "--no-edit", "-q", "--allow-empty").catch((e) =>
|
|
18346
|
+
for (const p of paths) await this.run("add", "-f", "--", p).catch((e) => log40.debug(`force-capture failed: ${p}`, e));
|
|
18347
|
+
await this.run("commit", "--amend", "--no-edit", "-q", "--allow-empty").catch((e) => log40.debug("amend failed", e));
|
|
17143
18348
|
}
|
|
17144
18349
|
/** Commits on `ref`, oldest-first (canonical index space). */
|
|
17145
18350
|
async log(ref) {
|
|
@@ -17199,7 +18404,7 @@ var ShadowRepo = class {
|
|
|
17199
18404
|
await this.run("gc", "--auto", "-q").catch(() => {
|
|
17200
18405
|
});
|
|
17201
18406
|
} catch (e) {
|
|
17202
|
-
|
|
18407
|
+
log40.debug("checkpoint prune failed", e);
|
|
17203
18408
|
}
|
|
17204
18409
|
}
|
|
17205
18410
|
};
|
|
@@ -17267,7 +18472,7 @@ var GitCheckpoints = class {
|
|
|
17267
18472
|
use(sessionId) {
|
|
17268
18473
|
if (sessionId === this.session) return;
|
|
17269
18474
|
this.session = sessionId;
|
|
17270
|
-
if (this.started) for (const r of this.repos) void r.point(this.ref()).catch((e) =>
|
|
18475
|
+
if (this.started) for (const r of this.repos) void r.point(this.ref()).catch((e) => log40.debug("re-point failed", e));
|
|
17271
18476
|
}
|
|
17272
18477
|
async begin(label) {
|
|
17273
18478
|
if (!await this.start()) return;
|
|
@@ -17287,7 +18492,7 @@ var GitCheckpoints = class {
|
|
|
17287
18492
|
process.stderr.write(`\x1B[2m checkpoints: '${this.repos[i].root}' too large to snapshot in ${Math.round(this.options.opTimeoutMs / 1e3)}s \u2014 file rewind disabled here\x1B[0m
|
|
17288
18493
|
`);
|
|
17289
18494
|
} else {
|
|
17290
|
-
|
|
18495
|
+
log40.debug("checkpoint commit failed", e);
|
|
17291
18496
|
survivors.push(this.repos[i]);
|
|
17292
18497
|
survivorCaches.push(this.caches[i] ?? []);
|
|
17293
18498
|
}
|
|
@@ -17322,7 +18527,7 @@ var GitCheckpoints = class {
|
|
|
17322
18527
|
if (this.forced.has(abs)) continue;
|
|
17323
18528
|
this.forced.add(abs);
|
|
17324
18529
|
const i = this.repoIndexFor(abs);
|
|
17325
|
-
if (i >= 0) await this.repos[i].amendForced([abs]).catch((e) =>
|
|
18530
|
+
if (i >= 0) await this.repos[i].amendForced([abs]).catch((e) => log40.debug("amendForced failed", e));
|
|
17326
18531
|
}
|
|
17327
18532
|
}
|
|
17328
18533
|
};
|
|
@@ -19112,7 +20317,7 @@ import { spawnSync as spawnSync6 } from "child_process";
|
|
|
19112
20317
|
import { writeFileSync as writeFileSync8, mkdirSync as mkdirSync10, readdirSync as readdirSync2, unlinkSync as unlinkSync3, chmodSync, existsSync as existsSync7 } from "fs";
|
|
19113
20318
|
import { homedir as homedir8 } from "os";
|
|
19114
20319
|
import { join as join14 } from "path";
|
|
19115
|
-
var
|
|
20320
|
+
var log41 = forComponent("os-sched");
|
|
19116
20321
|
var OsScheduler = class {
|
|
19117
20322
|
options;
|
|
19118
20323
|
constructor(options) {
|
|
@@ -19174,7 +20379,7 @@ var OsScheduler = class {
|
|
|
19174
20379
|
}
|
|
19175
20380
|
}
|
|
19176
20381
|
} catch (e) {
|
|
19177
|
-
|
|
20382
|
+
log41.debug(`cancel ${id}`, e);
|
|
19178
20383
|
}
|
|
19179
20384
|
for (const f of [`${id}.json`, `${id}.sh`]) {
|
|
19180
20385
|
try {
|
|
@@ -19292,7 +20497,7 @@ import { existsSync as existsSync8, mkdirSync as mkdirSync11, unlinkSync as unli
|
|
|
19292
20497
|
import { homedir as homedir9 } from "os";
|
|
19293
20498
|
import { join as join15 } from "path";
|
|
19294
20499
|
init_platform();
|
|
19295
|
-
var
|
|
20500
|
+
var log42 = forComponent("remote-trigger");
|
|
19296
20501
|
var TRIGGER_DIR = () => join15(homedir9(), ".agent", "triggers");
|
|
19297
20502
|
var sockPath = (sessionId, dir = TRIGGER_DIR()) => join15(dir, `${sessionId}.sock`);
|
|
19298
20503
|
var TriggerServer = class {
|
|
@@ -19330,13 +20535,13 @@ var TriggerServer = class {
|
|
|
19330
20535
|
conn.end(JSON.stringify({ ok: false, error: String(e) }) + "\n");
|
|
19331
20536
|
}
|
|
19332
20537
|
});
|
|
19333
|
-
conn.on("error", (e) =>
|
|
20538
|
+
conn.on("error", (e) => log42.debug("trigger conn error", e));
|
|
19334
20539
|
});
|
|
19335
|
-
this.server.on("error", (e) =>
|
|
20540
|
+
this.server.on("error", (e) => log42.debug("trigger server error", e));
|
|
19336
20541
|
this.server.listen(p);
|
|
19337
20542
|
this.path = p;
|
|
19338
20543
|
} catch (e) {
|
|
19339
|
-
|
|
20544
|
+
log42.debug("trigger server unavailable", e);
|
|
19340
20545
|
}
|
|
19341
20546
|
}
|
|
19342
20547
|
/** Re-bind on /resume (the session id changed). */
|
|
@@ -19468,7 +20673,7 @@ var strike = C("9");
|
|
|
19468
20673
|
var link = (text, url) => useColor ? `\x1B]8;;${url}\x1B\\${cyan(text)}\x1B]8;;\x1B\\` : `${text} (${url})`;
|
|
19469
20674
|
var mdPalette = { bold, dim, cyan, italic, strike, green, link };
|
|
19470
20675
|
var err = (s) => process.stderr.write(s);
|
|
19471
|
-
var
|
|
20676
|
+
var log43 = forComponent("cli");
|
|
19472
20677
|
var VERSION = (() => {
|
|
19473
20678
|
try {
|
|
19474
20679
|
return JSON.parse(readFileSync10(new URL("../package.json", import.meta.url), "utf8")).version ?? "?";
|
|
@@ -19621,6 +20826,7 @@ function parseArgs(argv) {
|
|
|
19621
20826
|
} else if (x === "--call") {
|
|
19622
20827
|
a.call = true;
|
|
19623
20828
|
a.duplex = true;
|
|
20829
|
+
a.lean = true;
|
|
19624
20830
|
} else if (x === "--lean") a.lean = true;
|
|
19625
20831
|
else if (x === "--keep-alive") a.keepAlive = true;
|
|
19626
20832
|
else if (x === "--channel-only") a.channelOnly = true;
|
|
@@ -19640,6 +20846,7 @@ function parseArgs(argv) {
|
|
|
19640
20846
|
else if (x === "--disallowedTools" || x === "--disallowed-tools") a.disallowedTools = val(++i, x).split(",").map((s) => s.trim()).filter(Boolean);
|
|
19641
20847
|
else if (x === "--append-system-prompt") a.appendSystemPrompt = val(++i, x);
|
|
19642
20848
|
else if (x === "--voice-system-prompt") a.voiceSystemPrompt = val(++i, x);
|
|
20849
|
+
else if (x === "--voice-persona") a.voicePersona = readPersona(val(++i, x));
|
|
19643
20850
|
else if (x === "--add-dir") (a.addDirs ??= []).push(val(++i, x));
|
|
19644
20851
|
else if (x === "--mcp-config") a.mcpConfig = val(++i, x);
|
|
19645
20852
|
else if (x === "--reasoning") a.reasoning = parseReasoning(val(++i, x));
|
|
@@ -19675,6 +20882,8 @@ function parseArgs(argv) {
|
|
|
19675
20882
|
if (a.channelOnly && !a.inputFormat) throw new Error("--channel-only needs --input-format stream-json --output-format stream-json");
|
|
19676
20883
|
if (a.duplex && a.plan) throw new Error("--plan is not supported in --duplex (workers are non-interactive; a plan could never be approved)");
|
|
19677
20884
|
if (a.voiceSystemPrompt && !a.duplex) throw new Error("--voice-system-prompt only applies with --duplex");
|
|
20885
|
+
if (a.voicePersona && !a.duplex && !a.channelOnly) throw new Error("--voice-persona only applies with --call or --channel-only");
|
|
20886
|
+
if (a.voicePersona) process.env.AGENTX_CALL_PERSONA = a.voicePersona;
|
|
19678
20887
|
if (a.call && a.inputFormat) throw new Error("--call is interactive (mic/TTS here); a headless host runs --channel-only");
|
|
19679
20888
|
if (a.channelMode && !a.duplexV2 && !a.channelOnly && !a.call) throw new Error("--channel-mode only applies with --duplex-v2 or --channel-only");
|
|
19680
20889
|
a.channelMode ??= parseChannelMode(process.env.AGENTX_CHANNEL_MODE);
|
|
@@ -19736,6 +20945,8 @@ Flags:
|
|
|
19736
20945
|
enter the agent's history (headless: stdin interim/pause/agent_state, stdout channel)
|
|
19737
20946
|
--channel-only headless (--input-format stream-json): ONLY the second channel, for a host whose main
|
|
19738
20947
|
answer comes from elsewhere (stdin user/text/barge/interim/pause/agent_state, stdout channel)
|
|
20948
|
+
--voice-persona <f> call persona from a file: replaces the default call style in MAIN's prompt and tells the live
|
|
20949
|
+
prediction tree who is speaking (--call / --channel-only; as Para's Settings > Call persona)
|
|
19739
20950
|
--channel-mode <m> second-channel talkativeness: turn (only a bridge when the answer is slow) | normal
|
|
19740
20951
|
(default) | chatty (frequent cues + content reactions; needs echo cancellation)
|
|
19741
20952
|
env fallback: AGENTX_CHANNEL_MODE
|
|
@@ -20365,7 +21576,7 @@ function turnCost(model, usage) {
|
|
|
20365
21576
|
const priced = model.startsWith("claude-code/") ? `anthropic/${resolveModelAlias(model.slice("claude-code/".length))}` : model;
|
|
20366
21577
|
return costOf(getModelInfo(priced)?.pricing, usage?.promptTokens ?? 0, usage?.completionTokens ?? 0, usage?.cacheCreationTokens ?? 0, usage?.cacheReadTokens ?? 0, priced);
|
|
20367
21578
|
}
|
|
20368
|
-
async function evaluateGoal(ai, condition, transcript,
|
|
21579
|
+
async function evaluateGoal(ai, condition, transcript, log44) {
|
|
20369
21580
|
const recent = transcript.filter((m) => m.role === "assistant").slice(-8).map((m) => {
|
|
20370
21581
|
const text = typeof m.content === "string" ? m.content : m.content.filter((p) => p.type === "text").map((p) => p.text).join(" ");
|
|
20371
21582
|
return text.slice(0, 600);
|
|
@@ -20385,7 +21596,7 @@ ${recent}` }
|
|
|
20385
21596
|
const match = r.content.match(/\{[\s\S]*\}/);
|
|
20386
21597
|
if (match) return JSON.parse(match[0]);
|
|
20387
21598
|
} catch (e) {
|
|
20388
|
-
|
|
21599
|
+
log44(dim(` (goal evaluator error: ${e?.message ?? e})
|
|
20389
21600
|
`));
|
|
20390
21601
|
}
|
|
20391
21602
|
return { met: false, reason: "evaluation unclear" };
|
|
@@ -20613,7 +21824,7 @@ function mcpAgentTools(mounted, opts) {
|
|
|
20613
21824
|
return tools;
|
|
20614
21825
|
}
|
|
20615
21826
|
async function closeMcp(mounted) {
|
|
20616
|
-
await Promise.all(mounted.map((m) => m.client.close().catch((e) =>
|
|
21827
|
+
await Promise.all(mounted.map((m) => m.client.close().catch((e) => log43.debug("mcp close failed", e))));
|
|
20617
21828
|
}
|
|
20618
21829
|
var IMG_EXT = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp" };
|
|
20619
21830
|
function mentionRefs(line) {
|
|
@@ -20672,7 +21883,7 @@ async function expandMentions(fs, line) {
|
|
|
20672
21883
|
if (loaded.includes(ref) || missing.includes(ref)) continue;
|
|
20673
21884
|
if (ref.includes(":") && mcpMentionResolver) {
|
|
20674
21885
|
const body = await mcpMentionResolver(ref).catch((e) => {
|
|
20675
|
-
|
|
21886
|
+
log43.debug("mcp mention resolve failed", e);
|
|
20676
21887
|
return null;
|
|
20677
21888
|
});
|
|
20678
21889
|
if (body != null) {
|
|
@@ -20698,7 +21909,7 @@ ${await fs.readFile(path)}`);
|
|
|
20698
21909
|
${blocks.join("\n\n")}` : line, loaded, missing };
|
|
20699
21910
|
}
|
|
20700
21911
|
function applyTurnMode(agent, session, task, flags = {}, onModel) {
|
|
20701
|
-
const p = parseDirectives(task, { strict: true, keys: ["model", "turns", "delegate", "advisor", "mode"], warn: (m) =>
|
|
21912
|
+
const p = parseDirectives(task, { strict: true, keys: ["model", "turns", "delegate", "advisor", "mode"], warn: (m) => log43.debug(m) });
|
|
20702
21913
|
const d = p.directives, isMode = !!(d.delegate || d.advisor || d.modeOff);
|
|
20703
21914
|
if (p.unresolvedModel && /->|<-|delegate:|advisor:/.test(task.slice(0, task.length - p.prompt.length))) {
|
|
20704
21915
|
err(red(` \u2717 unknown model "${p.unresolvedModel}"${p.ambiguous ? ` (could be: ${p.ambiguous.join(", ")})` : ""} \u2014 turn not sent
|
|
@@ -20791,7 +22002,7 @@ async function runTurn(agent, store, session, task, cp, cwd = process.cwd(), sen
|
|
|
20791
22002
|
try {
|
|
20792
22003
|
store.save(session);
|
|
20793
22004
|
} catch (ex) {
|
|
20794
|
-
|
|
22005
|
+
log43.debug("mid-turn session flush failed", ex);
|
|
20795
22006
|
}
|
|
20796
22007
|
}
|
|
20797
22008
|
return origNotify(e);
|
|
@@ -20955,14 +22166,14 @@ var isCancelTeardown = (e) => {
|
|
|
20955
22166
|
function installCancelGuards(mounted) {
|
|
20956
22167
|
process.on("unhandledRejection", (e) => {
|
|
20957
22168
|
if (isCancelTeardown(e)) {
|
|
20958
|
-
|
|
22169
|
+
log43.debug("suppressed unhandledRejection (cursor stream cancel)", e);
|
|
20959
22170
|
return;
|
|
20960
22171
|
}
|
|
20961
|
-
|
|
22172
|
+
log43.error("unhandledRejection", e);
|
|
20962
22173
|
});
|
|
20963
22174
|
process.on("uncaughtException", (e) => {
|
|
20964
22175
|
if (isCancelTeardown(e)) {
|
|
20965
|
-
|
|
22176
|
+
log43.debug("suppressed uncaughtException (cursor stream cancel)", e);
|
|
20966
22177
|
return;
|
|
20967
22178
|
}
|
|
20968
22179
|
console.error(e);
|
|
@@ -21287,16 +22498,20 @@ async function repl(args, ai, cfg, cwd) {
|
|
|
21287
22498
|
const face = dx ? dx.voice : agent;
|
|
21288
22499
|
let call;
|
|
21289
22500
|
let callChain = Promise.resolve();
|
|
22501
|
+
let callFastModel = "";
|
|
22502
|
+
let callReplyOpen = false;
|
|
22503
|
+
let greetNote = "";
|
|
21290
22504
|
const callTurns = /* @__PURE__ */ new Map();
|
|
21291
22505
|
if (args.call) {
|
|
21292
|
-
if (args.voice) agent.options.systemPrompt += "\n\n" + CALL_PROMPT;
|
|
22506
|
+
if (args.voice || args.voicePersona) agent.options.systemPrompt += "\n\n" + (args.voicePersona ?? CALL_PROMPT);
|
|
21293
22507
|
if (args.voiceSystemPrompt) agent.options.systemPrompt += "\n\n" + args.voiceSystemPrompt;
|
|
22508
|
+
const fast = callFast(args, ai, cfg);
|
|
22509
|
+
for (const n of fast.notes) err(yellow(` \u26A0 ${n}
|
|
22510
|
+
`));
|
|
22511
|
+
callFastModel = fast.options.fastModel;
|
|
21294
22512
|
call = new VoiceCall({
|
|
21295
22513
|
ai,
|
|
21296
|
-
|
|
21297
|
-
mode: args.channelMode,
|
|
21298
|
-
channel: channelOptions(ai, args.voiceModel ?? process.env.AGENTX_CHANNEL_MODEL ?? new VoiceCallOptions().fastModel, args.voiceGender),
|
|
21299
|
-
gender: args.voiceGender ?? "",
|
|
22514
|
+
...fast.options,
|
|
21300
22515
|
show: (t, kind) => {
|
|
21301
22516
|
err("\r\x1B[0J" + dim(` \u21B3 ${kind === "progress" ? "\u22EF " : ""}${t}
|
|
21302
22517
|
`));
|
|
@@ -21323,15 +22538,17 @@ async function repl(args, ai, cfg, cwd) {
|
|
|
21323
22538
|
if (cut && keepHeard(face.transcript, cut)) voiceDiag?.push({ t: performance.now(), kind: "call_kept_heard", heard: cut.heard.length, full: cut.full.length });
|
|
21324
22539
|
rec.running = true;
|
|
21325
22540
|
rec.before = face.transcript.length;
|
|
21326
|
-
|
|
22541
|
+
const note = greetNote;
|
|
22542
|
+
greetNote = "";
|
|
22543
|
+
callReplyOpen = false;
|
|
21327
22544
|
try {
|
|
21328
|
-
const r = await dispatchLine(text, { id });
|
|
22545
|
+
const r = await dispatchLine(text + note, { id });
|
|
21329
22546
|
if (r === "quit") {
|
|
21330
22547
|
await voiceIO?.awaitIdle();
|
|
21331
22548
|
editorRef?.abort();
|
|
21332
22549
|
}
|
|
21333
22550
|
} catch (e) {
|
|
21334
|
-
|
|
22551
|
+
log43.error("call turn failed", e);
|
|
21335
22552
|
} finally {
|
|
21336
22553
|
rec.running = false;
|
|
21337
22554
|
call?.main({ type: "done", id });
|
|
@@ -21348,8 +22565,13 @@ async function repl(args, ai, cfg, cwd) {
|
|
|
21348
22565
|
if (e.kind === "text_delta" && voiceIO) {
|
|
21349
22566
|
voiceReasonEnd();
|
|
21350
22567
|
spinner.stop();
|
|
21351
|
-
|
|
22568
|
+
if (!callReplyOpen) {
|
|
22569
|
+
callReplyOpen = true;
|
|
22570
|
+
voiceIO.beginSpeech();
|
|
22571
|
+
}
|
|
22572
|
+
const echo = voiceIO.speakDelta(e.message);
|
|
21352
22573
|
editorRef?.suspend();
|
|
22574
|
+
voiceEcho(echo);
|
|
21353
22575
|
return;
|
|
21354
22576
|
}
|
|
21355
22577
|
if (voiceIO && (e.kind === "turn_start" || e.kind === "thinking_delta")) return;
|
|
@@ -21463,7 +22685,7 @@ async function repl(args, ai, cfg, cwd) {
|
|
|
21463
22685
|
try {
|
|
21464
22686
|
mkdirSync12(dir, { recursive: true });
|
|
21465
22687
|
} catch (e) {
|
|
21466
|
-
|
|
22688
|
+
log43.debug("paste image dir create failed", e);
|
|
21467
22689
|
}
|
|
21468
22690
|
const img = grabClipboardImage(dir, String(Date.now()));
|
|
21469
22691
|
return img ? { display: "Image", ref: "@" + img.path, path: img.path } : null;
|
|
@@ -21574,9 +22796,9 @@ Added entries are loadable now via the Skill/SlashCommand tools; removed ones ar
|
|
|
21574
22796
|
const remember2 = (line) => {
|
|
21575
22797
|
try {
|
|
21576
22798
|
mkdirSync12(join16(cwd, ".agent"), { recursive: true });
|
|
21577
|
-
|
|
22799
|
+
appendFileSync2(histPath, line + "\n");
|
|
21578
22800
|
} catch (e) {
|
|
21579
|
-
|
|
22801
|
+
log43.debug("history write failed", e);
|
|
21580
22802
|
}
|
|
21581
22803
|
};
|
|
21582
22804
|
const ago = (t) => {
|
|
@@ -21648,7 +22870,7 @@ Added entries are loadable now via the Skill/SlashCommand tools; removed ones ar
|
|
|
21648
22870
|
try {
|
|
21649
22871
|
store.save(session);
|
|
21650
22872
|
} catch (e) {
|
|
21651
|
-
|
|
22873
|
+
log43.debug("session save after rewind failed", e);
|
|
21652
22874
|
}
|
|
21653
22875
|
err(green(" \u27F2 jumped back") + dim(` \u2014 ${face.transcript.length} message(s) kept; edit + resend
|
|
21654
22876
|
`));
|
|
@@ -21683,7 +22905,7 @@ ${task}`;
|
|
|
21683
22905
|
bangContext.length = 0;
|
|
21684
22906
|
}
|
|
21685
22907
|
const delta = await refreshCatalogs().catch((e) => {
|
|
21686
|
-
|
|
22908
|
+
log43.debug("catalog refresh failed", e);
|
|
21687
22909
|
return "";
|
|
21688
22910
|
});
|
|
21689
22911
|
if (delta) {
|
|
@@ -21953,7 +23175,7 @@ ${task}`;
|
|
|
21953
23175
|
desc: "rescan skills/commands dirs and rebuild the system prompt (one cache miss) \u2014 picks up entries created mid-session",
|
|
21954
23176
|
run: async () => {
|
|
21955
23177
|
await refreshCatalogs().catch((e) => {
|
|
21956
|
-
|
|
23178
|
+
log43.debug("catalog refresh failed", e);
|
|
21957
23179
|
});
|
|
21958
23180
|
face.reprepare();
|
|
21959
23181
|
err(green(` \u2713 reloaded \u2014 ${skills.length} skill(s), ${cmds.length} command(s); system prompt rebuilds on next message
|
|
@@ -22456,7 +23678,7 @@ ${task}`;
|
|
|
22456
23678
|
try {
|
|
22457
23679
|
for (const def of (await loadAgents(fs2, d)).agents) if (!seen.has(def.name)) seen.set(def.name, { def, from: d });
|
|
22458
23680
|
} catch (e) {
|
|
22459
|
-
|
|
23681
|
+
log43.debug(`loadAgents(${d}) failed`, e);
|
|
22460
23682
|
}
|
|
22461
23683
|
}
|
|
22462
23684
|
if (!seen.size) {
|
|
@@ -22544,7 +23766,7 @@ ${task}`;
|
|
|
22544
23766
|
}
|
|
22545
23767
|
if (idx >= 0) {
|
|
22546
23768
|
const old = mounted.splice(idx, 1)[0];
|
|
22547
|
-
await old.client.close().catch((e) =>
|
|
23769
|
+
await old.client.close().catch((e) => log43.debug("mcp close failed", e));
|
|
22548
23770
|
}
|
|
22549
23771
|
try {
|
|
22550
23772
|
const m = await mountMcpServer(name, conf);
|
|
@@ -22572,7 +23794,7 @@ ${task}`;
|
|
|
22572
23794
|
}
|
|
22573
23795
|
const m = mounted.splice(idx, 1)[0];
|
|
22574
23796
|
remountMcpTools();
|
|
22575
|
-
await m.client.close().catch((e) =>
|
|
23797
|
+
await m.client.close().catch((e) => log43.debug("mcp close failed", e));
|
|
22576
23798
|
err(dim(` removed "${name}"
|
|
22577
23799
|
`));
|
|
22578
23800
|
return;
|
|
@@ -22775,7 +23997,7 @@ ${task}`;
|
|
|
22775
23997
|
try {
|
|
22776
23998
|
return readdirSync4(join16(cwd, absDir.replace(/^\/+/, "")), { withFileTypes: true }).map((d) => ({ name: d.name, dir: d.isDirectory() }));
|
|
22777
23999
|
} catch (e) {
|
|
22778
|
-
|
|
24000
|
+
log43.debug("completion readdir failed", absDir, e);
|
|
22779
24001
|
return null;
|
|
22780
24002
|
}
|
|
22781
24003
|
};
|
|
@@ -22914,7 +24136,7 @@ ${out}
|
|
|
22914
24136
|
return;
|
|
22915
24137
|
}
|
|
22916
24138
|
await refreshCatalogs().catch((e) => {
|
|
22917
|
-
|
|
24139
|
+
log43.debug("catalog refresh failed", e);
|
|
22918
24140
|
});
|
|
22919
24141
|
const sk = skills.find((x) => x.name === name);
|
|
22920
24142
|
if (sk) {
|
|
@@ -22951,6 +24173,11 @@ ${out}
|
|
|
22951
24173
|
let partialRedraw = null;
|
|
22952
24174
|
const startVoice = async (greet) => {
|
|
22953
24175
|
if (voiceIO) return true;
|
|
24176
|
+
if (greet && call) {
|
|
24177
|
+
bindClaudeCodeSession(agent, session);
|
|
24178
|
+
callChain = agent.prewarm().catch((e) => err(dim(` (prewarm failed: ${e?.message ?? e})
|
|
24179
|
+
`)));
|
|
24180
|
+
}
|
|
22954
24181
|
if (!(duplex || call) || !process.stdin.isTTY) {
|
|
22955
24182
|
err(dim(" (voice needs --call on a TTY)\n"));
|
|
22956
24183
|
return false;
|
|
@@ -22969,13 +24196,13 @@ ${out}
|
|
|
22969
24196
|
liveSttTerms = sttTerms;
|
|
22970
24197
|
voiceIO = new VoiceIO({
|
|
22971
24198
|
...fakeVoice ?? {},
|
|
22972
|
-
|
|
22973
|
-
|
|
24199
|
+
// v3, as Para's NativeVoice: the fill channel covers the wait (no engine ack), TurnGate merges (no engine merge), the first clause speaks at once
|
|
24200
|
+
...call ? { adaptiveAckMs: 0, adaptiveEndpointing: true, utteranceMergeMs: 0, flushFirstClause: true } : {},
|
|
22974
24201
|
sttTerms,
|
|
22975
24202
|
onDiag: (ev) => voiceDiag?.push(ev),
|
|
22976
24203
|
emotions: emotionsOn,
|
|
22977
|
-
showEmotions,
|
|
22978
|
-
// local is authoritative (a /voice-emotions before mic-on still applies)
|
|
24204
|
+
showEmotions: !call && showEmotions,
|
|
24205
|
+
// local is authoritative (a /voice-emotions before mic-on still applies); a call's tags are speech, never text
|
|
22979
24206
|
// No FIXED ack phrase by default: a fixed "Mm-hm," every turn reads robotic, and the
|
|
22980
24207
|
// conversational register already opens with a natural reaction. Instead the ADAPTIVE ack
|
|
22981
24208
|
// (VoiceIOOptions.adaptiveAckMs, default 600ms — a safety net, not the voice) speaks a short varied ack only when the reflex
|
|
@@ -23018,7 +24245,7 @@ ${out}
|
|
|
23018
24245
|
onUtterance: (text, meta) => {
|
|
23019
24246
|
voicePartial = "";
|
|
23020
24247
|
if (!text.trim()) return;
|
|
23021
|
-
if (
|
|
24248
|
+
if (matchVoiceCommand2(text) === "off") {
|
|
23022
24249
|
dx?.abortSpeculation();
|
|
23023
24250
|
err(`\r\x1B[K ${bold(cyan("\u{1F3A4} \u203A"))} ${text}
|
|
23024
24251
|
`);
|
|
@@ -23031,7 +24258,7 @@ ${out}
|
|
|
23031
24258
|
return;
|
|
23032
24259
|
}
|
|
23033
24260
|
if (call) {
|
|
23034
|
-
call.final(text);
|
|
24261
|
+
call.final(text, void 0, { ...meta?.lastWordAgoMs !== void 0 ? { lastWordAgoMs: meta.lastWordAgoMs } : {}, ...meta?.language ? { lang: meta.language } : {} });
|
|
23035
24262
|
return;
|
|
23036
24263
|
}
|
|
23037
24264
|
const replaces = replacedVoiceTurn(lastVoiceTurn, meta);
|
|
@@ -23039,7 +24266,7 @@ ${out}
|
|
|
23039
24266
|
if (delivery || cut) dx?.parkCutDelivery(delivery, !!cut, heard);
|
|
23040
24267
|
const cutReply = cut && cut.full.length - cut.heard.length > 40 ? cut : null;
|
|
23041
24268
|
const noteP = replaces ? replaces.note : !cutReply ? Promise.resolve("") : (instinct ? instinct.bargeIntent(text).catch((e) => {
|
|
23042
|
-
|
|
24269
|
+
log43.warn(`barge intent failed \u2014 model decides: ${e.message}`);
|
|
23043
24270
|
return void 0;
|
|
23044
24271
|
}) : Promise.resolve(void 0)).then(async (intent) => interruptionNote(cutReply, intent) + ackedNote(await instinct?.ackFor(text)));
|
|
23045
24272
|
const cutIn = replaces ? replaces.cutIn : interjectNote;
|
|
@@ -23162,7 +24389,17 @@ ${out}
|
|
|
23162
24389
|
}
|
|
23163
24390
|
if (inDev?.virtual) err(yellow(` \u26A0 default input "${inDev.name}" looks like a virtual device \u2014 if it can't hear you, switch your Mac input to a real mic
|
|
23164
24391
|
`));
|
|
23165
|
-
if (greet) {
|
|
24392
|
+
if (greet && call && !session.messages.length) {
|
|
24393
|
+
const v = voiceIO, line = await callGreeting(ai, { model: callFastModel, persona: args.voicePersona ?? "" });
|
|
24394
|
+
const said = renderEmotions(line, { show: false }).prose.trim();
|
|
24395
|
+
v.speakFiller(line);
|
|
24396
|
+
err(`\r\x1B[K ${said}
|
|
24397
|
+
`);
|
|
24398
|
+
editorRef?.redrawNow();
|
|
24399
|
+
greetNote = `
|
|
24400
|
+
|
|
24401
|
+
<call_greeting>You opened this call with: "${said}". The user's words answer it.</call_greeting>`;
|
|
24402
|
+
} else if (greet) {
|
|
23166
24403
|
const where = cwd.split("/").pop();
|
|
23167
24404
|
const resumed = session.messages.length > 0;
|
|
23168
24405
|
const prompt = resumed ? `[session resumed] You're rejoining a live voice conversation already in progress \u2014 the full history, including the user's memory/preferences, is already in context, so do NOT call QuickLook again. In one short spoken line, welcome the user back and pick the thread up where it left off: if your last turn ended on an open question or offer, continue it naturally rather than asking what they'd like to do.` : `[session started] First call QuickLook with what:"memory" \u2014 if it knows the user's name or preferences, use them. Then greet the user warmly in one or two short sentences, as the opener of a live voice conversation. Context: working directory "${where}". Personalize from whatever you learned (memory). Then ask what they'd like to do.`;
|
|
@@ -23403,6 +24640,16 @@ function duplexError(e, reflexModel) {
|
|
|
23403
24640
|
const code = e?.statusCode;
|
|
23404
24641
|
return code && !msg.includes(String(code)) ? `${msg} (${code})` : msg;
|
|
23405
24642
|
}
|
|
24643
|
+
function readPersona(file) {
|
|
24644
|
+
let text = "";
|
|
24645
|
+
try {
|
|
24646
|
+
text = readFileSync10(file, "utf8").trim();
|
|
24647
|
+
} catch (e) {
|
|
24648
|
+
throw new Error(`--voice-persona: can't read ${file}: ${e.message}`);
|
|
24649
|
+
}
|
|
24650
|
+
if (!text) throw new Error(`--voice-persona: ${file} is empty`);
|
|
24651
|
+
return text;
|
|
24652
|
+
}
|
|
23406
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.`;
|
|
23407
24654
|
var HOLD_RESUME_MS = 4e3;
|
|
23408
24655
|
var leakDecider = () => voiceDecider();
|
|
@@ -23413,17 +24660,37 @@ function channelOptions(ai, model, gender) {
|
|
|
23413
24660
|
const decider = leakDecider();
|
|
23414
24661
|
const instinct = decider ? new Instinct({ decider }) : void 0;
|
|
23415
24662
|
const planner = new BackchannelPlanner(ai, { model });
|
|
24663
|
+
const grower = new ConvoTreeGrower(ai, { ...process.env.AGENTX_TREE_MODEL ? { model: process.env.AGENTX_TREE_MODEL } : {} });
|
|
23416
24664
|
return {
|
|
24665
|
+
...process.env.AGENTX_TREE === "0" ? {} : { grow: (c, s) => grower.grow(c, s) },
|
|
24666
|
+
// 0: the reactive live path
|
|
24667
|
+
pickLeaf: fastLeafPicker(ai, process.env.AGENTX_LEAF_MODEL || "groq/openai/gpt-oss-120b"),
|
|
24668
|
+
persona: process.env.AGENTX_CALL_PERSONA ?? "",
|
|
24669
|
+
onTree: (e) => log43.debug(`voice tree: ${e.branches} branches in ${e.ms}ms${e.tokens ? ` tokens ${e.tokens.input}/${e.tokens.output}` : ""}${e.error ? ` error ${e.error}` : ""}`),
|
|
24670
|
+
onEvent: (e) => {
|
|
24671
|
+
if (e.moment) log43.debug(`voice leaf: ${e.moment} ${e.conf} "${e.text}" ${e.acted}`);
|
|
24672
|
+
else log43.debug(`channel ${e.trigger}: rule=${e.rule} choice=${e.choice ?? "-"} \u2192 ${e.acted}${e.text ? ` "${e.text}"` : ""}${e.cue ? " (planned cue)" : ""} ${e.ms}ms`);
|
|
24673
|
+
},
|
|
23417
24674
|
// Banks in the user's language: generated once per language, cached + topped up across sessions (~/.agent/cache).
|
|
23418
24675
|
banks: new ChannelBanks({ generate: fastBankGenerator(ai, model), cacheDir: join16(homedir10(), ".agent", "cache", "channel-banks") }),
|
|
23419
24676
|
decide: channelDecide(ai, instinct),
|
|
23420
24677
|
generate: fastLineGenerator(ai, model, gender),
|
|
23421
24678
|
// Anticipated backchannels: on in chatty mode (its preset), else off until the offline replay proves them (tests/voice-bc-anticipate-eval.ts)
|
|
23422
24679
|
plan: (c, s) => planner.plan(c, s),
|
|
23423
|
-
...process.env.AGENTX_CHANNEL_ANTICIPATE === "1" ? { anticipate: true } : {}
|
|
23424
|
-
onEvent: (e) => log41.debug(`channel ${e.trigger}: rule=${e.rule} choice=${e.choice ?? "-"} \u2192 ${e.acted}${e.text ? ` "${e.text}"` : ""}${e.cue ? " (planned cue)" : ""} ${e.ms}ms`)
|
|
24680
|
+
...process.env.AGENTX_CHANNEL_ANTICIPATE === "1" ? { anticipate: true } : {}
|
|
23425
24681
|
};
|
|
23426
24682
|
}
|
|
24683
|
+
function callFast(args, ai, cfg) {
|
|
24684
|
+
const { reflexModel, notes } = duplexModels({ voiceModel: args.voiceModel, reflexModel: cfg.reflexModel, thinkModel: false, actModel: String(args.model ?? cfg.model ?? ""), apiKeys: { ...cfg.apiKeys, ...apiKeysFromEnv() } });
|
|
24685
|
+
const model = reflexModel ?? new DuplexAgentOptions().reflexModel, line = process.env.AGENTX_CHANNEL_MODEL || model;
|
|
24686
|
+
return { model, notes, options: {
|
|
24687
|
+
fastModel: line,
|
|
24688
|
+
mode: args.channelMode,
|
|
24689
|
+
gender: args.voiceGender ?? "",
|
|
24690
|
+
progress: { generate: fastProgressGenerator(ai, line, args.voiceGender) },
|
|
24691
|
+
channel: channelOptions(ai, line, args.voiceGender)
|
|
24692
|
+
} };
|
|
24693
|
+
}
|
|
23427
24694
|
function overlapResponder(ai, mode, out, aside) {
|
|
23428
24695
|
const judge = softBarge(mode) ? new OverlapJudge({ model: overlapModelFor(ai) }) : void 0;
|
|
23429
24696
|
return (m) => {
|
|
@@ -23495,7 +24762,7 @@ async function duplexServe(args, ai, cfg, cwd) {
|
|
|
23495
24762
|
try {
|
|
23496
24763
|
store.save(session);
|
|
23497
24764
|
} catch (e) {
|
|
23498
|
-
|
|
24765
|
+
log43.warn("duplex session save failed", e);
|
|
23499
24766
|
}
|
|
23500
24767
|
};
|
|
23501
24768
|
const overlap2 = overlapResponder(ai, args.duplexV2 ? args.channelMode : void 0, out);
|
|
@@ -23535,7 +24802,7 @@ async function duplexServe(args, ai, cfg, cwd) {
|
|
|
23535
24802
|
done2();
|
|
23536
24803
|
out({ type: "turn_done", id, finishReason: res?.finishReason ?? "error", ...why ? { error: why } : {} });
|
|
23537
24804
|
}).catch((e) => {
|
|
23538
|
-
|
|
24805
|
+
log43.error("duplex turn failed", e);
|
|
23539
24806
|
done2();
|
|
23540
24807
|
out({ type: "turn_done", id, finishReason: "error", error: String(e?.message ?? e) });
|
|
23541
24808
|
});
|
|
@@ -23551,13 +24818,11 @@ async function duplexServe(args, ai, cfg, cwd) {
|
|
|
23551
24818
|
}
|
|
23552
24819
|
async function channelServe(args, ai, cfg) {
|
|
23553
24820
|
const out = (o) => process.stdout.write(JSON.stringify(o) + "\n");
|
|
23554
|
-
const {
|
|
23555
|
-
const model = reflexModel ?? new DuplexAgentOptions().reflexModel;
|
|
24821
|
+
const { model, notes, options: fast } = callFast(args, ai, cfg);
|
|
23556
24822
|
const call = new VoiceCall({
|
|
23557
|
-
|
|
23558
|
-
|
|
23559
|
-
|
|
23560
|
-
channel: channelOptions(ai, process.env.AGENTX_CHANNEL_MODEL || model, args.voiceGender),
|
|
24823
|
+
...fast,
|
|
24824
|
+
// no `ai` here: the channel's model wiring is explicit
|
|
24825
|
+
channel: { ...fast.channel, onTree: (e) => out({ type: "metric", name: "tree", ms: e.ms, kind: `${e.tokens ? `${e.tokens.input}/${e.tokens.output} tok ` : ""}${e.error ?? (e.lines ?? []).join(" | ")}` }), onLeaf: (e) => out({ type: "metric", name: "leaf", ms: e.ms, kind: `${e.moment} ${e.conf} ${e.acted}`, text: e.text }) },
|
|
23561
24826
|
speak: (line, kind) => {
|
|
23562
24827
|
out(channelLine(line, kind));
|
|
23563
24828
|
return true;
|