@voicethere/agent 0.31.1 → 0.32.0

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/eject/llm/loop.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Copied from @voicethere/agent 0.31.1. Package updates no longer change this file.
1
+ // Copied from @voicethere/agent 0.32.0. Package updates no longer change this file.
2
2
  import { randomUUID } from "node:crypto";
3
3
  import {
4
4
  type AgentHandlers,
@@ -160,8 +160,25 @@ interface ActiveTurn {
160
160
  promise: Promise<void>;
161
161
  }
162
162
 
163
+ /**
164
+ * Interruptible speech sent to the runner and not settled yet. A typed message
165
+ * flushes these like a voice barge-in does (`flushSpeechForChat`), unless
166
+ * `bargeIn.typedMessages` is `false` or `bargeIn.mode` is `off`.
167
+ */
168
+ interface PendingSpeech {
169
+ requestId: string;
170
+ turnId: string;
171
+ text: string;
172
+ /** Reply sentences of the turn, in order; undefined for fillers. */
173
+ sentences?: string[];
174
+ /** Indexes in `sentences` of the sentences `text` holds (coalesced speech holds several). */
175
+ indexes?: number[];
176
+ }
177
+
163
178
  interface SessionState {
164
179
  sessionId: string;
180
+ /** Interruptible speech still pending at the runner, oldest first. */
181
+ speech: PendingSpeech[];
165
182
  history: ConversationHistory;
166
183
  active?: ActiveTurn;
167
184
  queued?: { text: string; source: Source; utteranceId?: string };
@@ -435,6 +452,78 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
435
452
  });
436
453
  }
437
454
 
455
+ /** Remembers interruptible speech until the runner settles it. */
456
+ function trackSpeech(
457
+ s: SessionState,
458
+ h: SpeakHandle,
459
+ meta: {
460
+ turnId: string;
461
+ text: string;
462
+ sentences?: string[];
463
+ indexes?: number[];
464
+ },
465
+ ): SpeakHandle {
466
+ const entry: PendingSpeech = {
467
+ requestId: h.requestId,
468
+ ...meta,
469
+ };
470
+ s.speech.push(entry);
471
+ void h.done.then(() => {
472
+ const i = s.speech.indexOf(entry);
473
+ if (i >= 0) s.speech.splice(i, 1);
474
+ });
475
+ return h;
476
+ }
477
+
478
+ /**
479
+ * A typed message barges in like caller speech: interruptible speech of
480
+ * earlier turns is cancelled (speech the agent sent with
481
+ * `interruptible: false` keeps playing, as on a voice barge-in). Speech of
482
+ * `keepTurnId` (the running turn, cancelled by its own path) is left alone.
483
+ * The runner reports no played text for a cancel, so the history keeps the
484
+ * reply sentences already settled and gets the same interruption note as a
485
+ * voice barge-in, at sentence granularity: a sentence still pending (queued
486
+ * or playing) counts as not heard. Nothing waits for the runner, so the new
487
+ * reply starts at once.
488
+ */
489
+ function flushSpeechForChat(
490
+ s: SessionState,
491
+ keepTurnId: string | undefined,
492
+ ): void {
493
+ const targets = s.speech.filter((e) => e.turnId !== keepTurnId);
494
+ if (targets.length === 0) return;
495
+ for (const e of targets) {
496
+ cancelSpeech(s.sessionId, { requestId: e.requestId });
497
+ }
498
+ s.speech = s.speech.filter((e) => !targets.includes(e));
499
+ agentLog(
500
+ "info",
501
+ `chat message flushed ${targets.length} pending speech item(s)`,
502
+ s.sessionId,
503
+ );
504
+ const byTurn = new Map<string, PendingSpeech[]>();
505
+ for (const e of targets) {
506
+ if (!e.sentences) continue;
507
+ const list = byTurn.get(e.turnId) ?? [];
508
+ list.push(e);
509
+ byTurn.set(e.turnId, list);
510
+ }
511
+ for (const [turnId, cut] of byTurn) {
512
+ const sentences = cut[0].sentences as string[];
513
+ const cutIndexes = new Set(cut.flatMap((e) => e.indexes ?? []));
514
+ const playedText = sentences
515
+ .filter((_, i) => !cutIndexes.has(i))
516
+ .join(" ");
517
+ const unplayedText = cut.map((e) => e.text).join(" ");
518
+ s.lastInterruption = { turnId, playedText, unplayedText };
519
+ s.history.replaceAssistantText(
520
+ turnId,
521
+ playedText,
522
+ useNote ? interruptionNote(playedText, unplayedText) : undefined,
523
+ );
524
+ }
525
+ }
526
+
438
527
  /** The one place where a sentence of a reply leaves the agent. */
439
528
  function deliver(
440
529
  s: SessionState,
@@ -448,7 +537,12 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
448
537
  sendReplyDelta(s, t, text);
449
538
  if (t.source === "voice" || speakChat) {
450
539
  if (interruptible) {
451
- speak(s.sessionId, text, replyOpts(t));
540
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
541
+ turnId: t.turnId,
542
+ text,
543
+ sentences: t.spoken,
544
+ indexes: [t.spoken.length - 1],
545
+ });
452
546
  } else {
453
547
  const h = announce(s.sessionId, text, replyOpts(t));
454
548
  trackAnnouncement(s, h.done);
@@ -480,7 +574,11 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
480
574
  ? { turnId, kind: "filler" as const, fillerKind }
481
575
  : { turnId };
482
576
  if (interruptible) {
483
- return speak(s.sessionId, text, opts);
577
+ return trackSpeech(s, speak(s.sessionId, text, opts), {
578
+ turnId,
579
+ text,
580
+ ...(fillerKind ? {} : { sentences: [text], indexes: [0] }),
581
+ });
484
582
  }
485
583
  const h = announce(s.sessionId, text, opts);
486
584
  trackAnnouncement(s, h.done);
@@ -586,10 +684,17 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
586
684
  t.coalesceTimer = undefined;
587
685
  if (t.pending.length === 0) return;
588
686
  const text = t.pending.join(" ");
687
+ const base = t.spoken.length - t.pending.length;
688
+ const indexes = t.pending.map((_, i) => base + i);
589
689
  t.pending = [];
590
690
  t.pendingChars = 0;
591
691
  if (t.controller.signal.aborted || s.ended) return;
592
- speak(s.sessionId, text, replyOpts(t));
692
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
693
+ turnId: t.turnId,
694
+ text,
695
+ sentences: t.spoken,
696
+ indexes,
697
+ });
593
698
  }
594
699
 
595
700
  /** A sentence of the model's reply: chat delta now, speech coalesced. */
@@ -1412,12 +1517,21 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
1412
1517
  return;
1413
1518
  }
1414
1519
  const active = s.active;
1520
+ if (
1521
+ source === "chat" &&
1522
+ policy === "cancel" &&
1523
+ config.bargeIn?.mode !== "off" &&
1524
+ config.bargeIn?.typedMessages !== false
1525
+ ) {
1526
+ flushSpeechForChat(s, active?.turnId);
1527
+ }
1415
1528
  if (active) {
1416
1529
  if (policy === "finish") {
1417
1530
  s.queued = { text, source, utteranceId };
1418
1531
  return;
1419
1532
  }
1420
1533
  cancelSpeech(s.sessionId, { turnId: active.turnId });
1534
+ s.speech = s.speech.filter((e) => e.turnId !== active.turnId);
1421
1535
  active.state.abortStatus = "cancelled";
1422
1536
  active.state.controller.abort();
1423
1537
  clearFillerTimer(active.state);
@@ -1486,6 +1600,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
1486
1600
  const s: SessionState = {
1487
1601
  sessionId: ctx.sessionId,
1488
1602
  history: new ConversationHistory(historyTurns),
1603
+ speech: [],
1489
1604
  announcing: 0,
1490
1605
  chain: Promise.resolve(),
1491
1606
  ended: false,
@@ -1566,6 +1681,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
1566
1681
  if (!s) return;
1567
1682
  s.ended = true;
1568
1683
  s.queued = undefined;
1684
+ s.speech.length = 0;
1569
1685
  if (s.active) {
1570
1686
  s.active.state.abortStatus = "cancelled";
1571
1687
  s.active.state.controller.abort();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voicethere/agent",
3
- "version": "0.31.1",
3
+ "version": "0.32.0",
4
4
  "description": "VoiceThere customer agent SDK — IPC types and runtime helpers for sandboxed child bundles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -71,6 +71,11 @@
71
71
  "properties": {
72
72
  "mode": {
73
73
  "enum": ["project", "on", "off"]
74
+ },
75
+ "typedMessages": {
76
+ "type": "boolean",
77
+ "default": true,
78
+ "description": "A typed chat message cuts off the agent's earlier speech that is still queued or playing. Set false to let it finish. Mode off disables this too."
74
79
  }
75
80
  },
76
81
  "default": {