@voicethere/agent 0.31.0 → 0.31.2

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.
@@ -36009,6 +36009,45 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36009
36009
  ...link.label ? { label: link.label } : {}
36010
36010
  });
36011
36011
  }
36012
+ function trackSpeech(s, h, meta2) {
36013
+ const entry = {
36014
+ requestId: h.requestId,
36015
+ ...meta2
36016
+ };
36017
+ s.speech.push(entry);
36018
+ void h.done.then(() => {
36019
+ const i = s.speech.indexOf(entry);
36020
+ if (i >= 0)
36021
+ s.speech.splice(i, 1);
36022
+ });
36023
+ return h;
36024
+ }
36025
+ function flushSpeechForChat(s, keepTurnId) {
36026
+ const targets = s.speech.filter((e) => e.turnId !== keepTurnId);
36027
+ if (targets.length === 0)
36028
+ return;
36029
+ for (const e of targets) {
36030
+ cancelSpeech(s.sessionId, { requestId: e.requestId });
36031
+ }
36032
+ s.speech = s.speech.filter((e) => !targets.includes(e));
36033
+ agentLog("info", `chat message flushed ${targets.length} pending speech item(s)`, s.sessionId);
36034
+ const byTurn = /* @__PURE__ */ new Map();
36035
+ for (const e of targets) {
36036
+ if (!e.sentences)
36037
+ continue;
36038
+ const list = byTurn.get(e.turnId) ?? [];
36039
+ list.push(e);
36040
+ byTurn.set(e.turnId, list);
36041
+ }
36042
+ for (const [turnId, cut] of byTurn) {
36043
+ const sentences = cut[0].sentences;
36044
+ const cutIndexes = new Set(cut.flatMap((e) => e.indexes ?? []));
36045
+ const playedText = sentences.filter((_, i) => !cutIndexes.has(i)).join(" ");
36046
+ const unplayedText = cut.map((e) => e.text).join(" ");
36047
+ s.lastInterruption = { turnId, playedText, unplayedText };
36048
+ s.history.replaceAssistantText(turnId, playedText, useNote ? interruptionNote(playedText, unplayedText) : void 0);
36049
+ }
36050
+ }
36012
36051
  function deliver(s, t, text, interruptible = true) {
36013
36052
  flushSpeech(s, t);
36014
36053
  t.spoken.push(text);
@@ -36016,7 +36055,12 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36016
36055
  sendReplyDelta(s, t, text);
36017
36056
  if (t.source === "voice" || speakChat) {
36018
36057
  if (interruptible) {
36019
- speak(s.sessionId, text, replyOpts(t));
36058
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36059
+ turnId: t.turnId,
36060
+ text,
36061
+ sentences: t.spoken,
36062
+ indexes: [t.spoken.length - 1]
36063
+ });
36020
36064
  } else {
36021
36065
  const h = announce(s.sessionId, text, replyOpts(t));
36022
36066
  trackAnnouncement(s, h.done);
@@ -36029,7 +36073,11 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36029
36073
  function speakAnnouncement(s, turnId, text, interruptible, fillerKind) {
36030
36074
  const opts2 = fillerKind ? { turnId, kind: "filler", fillerKind } : { turnId };
36031
36075
  if (interruptible) {
36032
- return speak(s.sessionId, text, opts2);
36076
+ return trackSpeech(s, speak(s.sessionId, text, opts2), {
36077
+ turnId,
36078
+ text,
36079
+ ...fillerKind ? {} : { sentences: [text], indexes: [0] }
36080
+ });
36033
36081
  }
36034
36082
  const h = announce(s.sessionId, text, opts2);
36035
36083
  trackAnnouncement(s, h.done);
@@ -36117,11 +36165,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36117
36165
  if (t.pending.length === 0)
36118
36166
  return;
36119
36167
  const text = t.pending.join(" ");
36168
+ const base = t.spoken.length - t.pending.length;
36169
+ const indexes = t.pending.map((_, i) => base + i);
36120
36170
  t.pending = [];
36121
36171
  t.pendingChars = 0;
36122
36172
  if (t.controller.signal.aborted || s.ended)
36123
36173
  return;
36124
- speak(s.sessionId, text, replyOpts(t));
36174
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36175
+ turnId: t.turnId,
36176
+ text,
36177
+ sentences: t.spoken,
36178
+ indexes
36179
+ });
36125
36180
  }
36126
36181
  function deliverReply(s, t, text) {
36127
36182
  if (coalesceMs <= 0) {
@@ -36768,12 +36823,16 @@ ${t.inputText}` : t.inputText, {
36768
36823
  return;
36769
36824
  }
36770
36825
  const active = s.active;
36826
+ if (source === "chat" && policy === "cancel" && config2.bargeIn?.mode !== "off") {
36827
+ flushSpeechForChat(s, active?.turnId);
36828
+ }
36771
36829
  if (active) {
36772
36830
  if (policy === "finish") {
36773
36831
  s.queued = { text, source, utteranceId };
36774
36832
  return;
36775
36833
  }
36776
36834
  cancelSpeech(s.sessionId, { turnId: active.turnId });
36835
+ s.speech = s.speech.filter((e) => e.turnId !== active.turnId);
36777
36836
  active.state.abortStatus = "cancelled";
36778
36837
  active.state.controller.abort();
36779
36838
  clearFillerTimer(active.state);
@@ -36826,6 +36885,7 @@ ${t.inputText}` : t.inputText, {
36826
36885
  const s = {
36827
36886
  sessionId: ctx.sessionId,
36828
36887
  history: new ConversationHistory(historyTurns),
36888
+ speech: [],
36829
36889
  announcing: 0,
36830
36890
  chain: Promise.resolve(),
36831
36891
  ended: false
@@ -36894,6 +36954,7 @@ ${t.inputText}` : t.inputText, {
36894
36954
  return;
36895
36955
  s.ended = true;
36896
36956
  s.queued = void 0;
36957
+ s.speech.length = 0;
36897
36958
  if (s.active) {
36898
36959
  s.active.state.abortStatus = "cancelled";
36899
36960
  s.active.state.controller.abort();
@@ -60378,7 +60439,7 @@ var voicethere_llm_default = {
60378
60439
  $schema: "./node_modules/@voicethere/agent/schema/voicethere.llm.schema.json",
60379
60440
  version: 1,
60380
60441
  provider: "openrouter",
60381
- model: "amazon/nova-micro-v1",
60442
+ model: "anthropic/claude-haiku-5.5",
60382
60443
  apiKeySecret: "AGENT_OPENROUTER_API_KEY",
60383
60444
  maxOutputTokens: 512,
60384
60445
  greeting: {
@@ -60425,13 +60486,21 @@ var voicethere_llm_default = {
60425
60486
  };
60426
60487
 
60427
60488
  // templates/llm-board/prompt.md
60428
- var prompt_default = `You are the facilitator of a shared brainstorming board. The people in the room put sticky notes on it, and you can read, add and change notes with your tools. People talk to you by voice, so everything you say is read aloud.
60489
+ var prompt_default = `You are the facilitator of a shared brainstorming board. The people in the room put sticky notes on it, and you can read, add and change notes with your tools. People talk to you by voice, so everything you say is read aloud. What they say reaches you as a transcript that can mishear words, for example "node" for "note"; when the board makes the meaning clear, write the word they meant.
60429
60490
 
60430
60491
  Keep spoken replies to one or two short sentences. Do not use markdown, lists, emoji or special characters.
60431
60492
 
60493
+ A note has only its text, a color and a position. It has no separate title or name, so naming, renaming or rewording a note means changing its text.
60494
+
60432
60495
  Call list_notes before you group, compare or summarise notes, and before you change one. Never guess what is on the board.
60433
60496
 
60434
- Add notes only when someone asks you to, or when you are asked to summarise; then add one short note per theme with add_note. Use update_note to fix a note's text, color or position when asked. Say briefly what you did, for example "I added a note about pricing."
60497
+ To change a note that is already on the board, use update_note with that note's id. That covers new wording, a new name, a fix, a new color or a move. Never use add_note to change a note: it would leave the old note and a second one.
60498
+
60499
+ Use add_note only for a note that is not on the board yet: when someone asks you to add one, or when you summarise. When you summarise, write one short note per theme yourself; do not ask anyone to name or word it.
60500
+
60501
+ If you asked a question about a note that is on the board, such as "What should I call it?", the answer is meant for that note, even in the middle of a summary: put it on that note with update_note and do not add a new note.
60502
+
60503
+ After a change, say briefly what you did, for example "I renamed the note to Basic note." Ask a follow-up question only when you need the answer to finish the request.
60435
60504
 
60436
60505
  If a tool says the board is full or a note does not exist, say so plainly and suggest what to do next.
60437
60506
 
@@ -60564,6 +60633,46 @@ function parseBoardMessage(msg) {
60564
60633
  }
60565
60634
  }
60566
60635
 
60636
+ // templates/llm-board/tool-specs.ts
60637
+ var colorSchema = { type: "string", enum: [...NOTE_COLORS] };
60638
+ var BOARD_TOOL_SPECS = {
60639
+ list_notes: {
60640
+ name: "list_notes",
60641
+ description: "List every note on the shared board.",
60642
+ inputSchema: {
60643
+ type: "object",
60644
+ properties: {},
60645
+ additionalProperties: false
60646
+ }
60647
+ },
60648
+ add_note: {
60649
+ name: "add_note",
60650
+ description: "Add a new note to the shared board. Only for a note that is not on the board yet; to rename, reword, recolor or move an existing note use update_note. Text is 1 to 200 characters.",
60651
+ inputSchema: {
60652
+ type: "object",
60653
+ properties: { text: { type: "string" }, color: colorSchema },
60654
+ required: ["text"],
60655
+ additionalProperties: false
60656
+ }
60657
+ },
60658
+ update_note: {
60659
+ name: "update_note",
60660
+ description: "Change an existing note by id: its text (also to rename or reword it), color or position (x and y between 0 and 1). Get the id from list_notes.",
60661
+ inputSchema: {
60662
+ type: "object",
60663
+ properties: {
60664
+ id: { type: "string" },
60665
+ text: { type: "string" },
60666
+ color: colorSchema,
60667
+ x: { type: "number" },
60668
+ y: { type: "number" }
60669
+ },
60670
+ required: ["id"],
60671
+ additionalProperties: false
60672
+ }
60673
+ }
60674
+ };
60675
+
60567
60676
  // templates/llm-board/agent.ts
60568
60677
  var boards = /* @__PURE__ */ new Map();
60569
60678
  var roomOf = /* @__PURE__ */ new Map();
@@ -60659,11 +60768,8 @@ function apply(sessionId, m) {
60659
60768
  }
60660
60769
  broadcast(sessionId);
60661
60770
  }
60662
- var colorSchema = { type: "string", enum: [...NOTE_COLORS] };
60663
60771
  var listNotes = defineTool({
60664
- name: "list_notes",
60665
- description: "List every note on the shared board.",
60666
- inputSchema: { type: "object", properties: {}, additionalProperties: false },
60772
+ ...BOARD_TOOL_SPECS.list_notes,
60667
60773
  handler: (_args, ctx) => {
60668
60774
  const board = boardFor(ctx.sessionId);
60669
60775
  if (!board) return SESSION_ENDED;
@@ -60673,14 +60779,7 @@ var listNotes = defineTool({
60673
60779
  }
60674
60780
  });
60675
60781
  var addNote = defineTool({
60676
- name: "add_note",
60677
- description: "Add a note to the shared board. Text is 1 to 200 characters.",
60678
- inputSchema: {
60679
- type: "object",
60680
- properties: { text: { type: "string" }, color: colorSchema },
60681
- required: ["text"],
60682
- additionalProperties: false
60683
- },
60782
+ ...BOARD_TOOL_SPECS.add_note,
60684
60783
  handler: ({ text, color: color2 }, ctx) => {
60685
60784
  const board = boardFor(ctx.sessionId);
60686
60785
  if (!board) return SESSION_ENDED;
@@ -60691,20 +60790,7 @@ var addNote = defineTool({
60691
60790
  }
60692
60791
  });
60693
60792
  var updateNote = defineTool({
60694
- name: "update_note",
60695
- description: "Change a note's text, color or position (x and y between 0 and 1).",
60696
- inputSchema: {
60697
- type: "object",
60698
- properties: {
60699
- id: { type: "string" },
60700
- text: { type: "string" },
60701
- color: colorSchema,
60702
- x: { type: "number" },
60703
- y: { type: "number" }
60704
- },
60705
- required: ["id"],
60706
- additionalProperties: false
60707
- },
60793
+ ...BOARD_TOOL_SPECS.update_note,
60708
60794
  handler: ({ id, ...patch }, ctx) => {
60709
60795
  const board = boardFor(ctx.sessionId);
60710
60796
  if (!board) return SESSION_ENDED;
@@ -36009,6 +36009,45 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36009
36009
  ...link.label ? { label: link.label } : {}
36010
36010
  });
36011
36011
  }
36012
+ function trackSpeech(s, h, meta2) {
36013
+ const entry = {
36014
+ requestId: h.requestId,
36015
+ ...meta2
36016
+ };
36017
+ s.speech.push(entry);
36018
+ void h.done.then(() => {
36019
+ const i = s.speech.indexOf(entry);
36020
+ if (i >= 0)
36021
+ s.speech.splice(i, 1);
36022
+ });
36023
+ return h;
36024
+ }
36025
+ function flushSpeechForChat(s, keepTurnId) {
36026
+ const targets = s.speech.filter((e) => e.turnId !== keepTurnId);
36027
+ if (targets.length === 0)
36028
+ return;
36029
+ for (const e of targets) {
36030
+ cancelSpeech(s.sessionId, { requestId: e.requestId });
36031
+ }
36032
+ s.speech = s.speech.filter((e) => !targets.includes(e));
36033
+ agentLog("info", `chat message flushed ${targets.length} pending speech item(s)`, s.sessionId);
36034
+ const byTurn = /* @__PURE__ */ new Map();
36035
+ for (const e of targets) {
36036
+ if (!e.sentences)
36037
+ continue;
36038
+ const list = byTurn.get(e.turnId) ?? [];
36039
+ list.push(e);
36040
+ byTurn.set(e.turnId, list);
36041
+ }
36042
+ for (const [turnId, cut] of byTurn) {
36043
+ const sentences = cut[0].sentences;
36044
+ const cutIndexes = new Set(cut.flatMap((e) => e.indexes ?? []));
36045
+ const playedText = sentences.filter((_, i) => !cutIndexes.has(i)).join(" ");
36046
+ const unplayedText = cut.map((e) => e.text).join(" ");
36047
+ s.lastInterruption = { turnId, playedText, unplayedText };
36048
+ s.history.replaceAssistantText(turnId, playedText, useNote ? interruptionNote(playedText, unplayedText) : void 0);
36049
+ }
36050
+ }
36012
36051
  function deliver(s, t, text, interruptible = true) {
36013
36052
  flushSpeech(s, t);
36014
36053
  t.spoken.push(text);
@@ -36016,7 +36055,12 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36016
36055
  sendReplyDelta(s, t, text);
36017
36056
  if (t.source === "voice" || speakChat) {
36018
36057
  if (interruptible) {
36019
- speak(s.sessionId, text, replyOpts(t));
36058
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36059
+ turnId: t.turnId,
36060
+ text,
36061
+ sentences: t.spoken,
36062
+ indexes: [t.spoken.length - 1]
36063
+ });
36020
36064
  } else {
36021
36065
  const h = announce(s.sessionId, text, replyOpts(t));
36022
36066
  trackAnnouncement(s, h.done);
@@ -36029,7 +36073,11 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36029
36073
  function speakAnnouncement(s, turnId, text, interruptible, fillerKind) {
36030
36074
  const opts2 = fillerKind ? { turnId, kind: "filler", fillerKind } : { turnId };
36031
36075
  if (interruptible) {
36032
- return speak(s.sessionId, text, opts2);
36076
+ return trackSpeech(s, speak(s.sessionId, text, opts2), {
36077
+ turnId,
36078
+ text,
36079
+ ...fillerKind ? {} : { sentences: [text], indexes: [0] }
36080
+ });
36033
36081
  }
36034
36082
  const h = announce(s.sessionId, text, opts2);
36035
36083
  trackAnnouncement(s, h.done);
@@ -36117,11 +36165,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36117
36165
  if (t.pending.length === 0)
36118
36166
  return;
36119
36167
  const text = t.pending.join(" ");
36168
+ const base = t.spoken.length - t.pending.length;
36169
+ const indexes = t.pending.map((_, i) => base + i);
36120
36170
  t.pending = [];
36121
36171
  t.pendingChars = 0;
36122
36172
  if (t.controller.signal.aborted || s.ended)
36123
36173
  return;
36124
- speak(s.sessionId, text, replyOpts(t));
36174
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36175
+ turnId: t.turnId,
36176
+ text,
36177
+ sentences: t.spoken,
36178
+ indexes
36179
+ });
36125
36180
  }
36126
36181
  function deliverReply(s, t, text) {
36127
36182
  if (coalesceMs <= 0) {
@@ -36768,12 +36823,16 @@ ${t.inputText}` : t.inputText, {
36768
36823
  return;
36769
36824
  }
36770
36825
  const active = s.active;
36826
+ if (source === "chat" && policy === "cancel" && config2.bargeIn?.mode !== "off") {
36827
+ flushSpeechForChat(s, active?.turnId);
36828
+ }
36771
36829
  if (active) {
36772
36830
  if (policy === "finish") {
36773
36831
  s.queued = { text, source, utteranceId };
36774
36832
  return;
36775
36833
  }
36776
36834
  cancelSpeech(s.sessionId, { turnId: active.turnId });
36835
+ s.speech = s.speech.filter((e) => e.turnId !== active.turnId);
36777
36836
  active.state.abortStatus = "cancelled";
36778
36837
  active.state.controller.abort();
36779
36838
  clearFillerTimer(active.state);
@@ -36826,6 +36885,7 @@ ${t.inputText}` : t.inputText, {
36826
36885
  const s = {
36827
36886
  sessionId: ctx.sessionId,
36828
36887
  history: new ConversationHistory(historyTurns),
36888
+ speech: [],
36829
36889
  announcing: 0,
36830
36890
  chain: Promise.resolve(),
36831
36891
  ended: false
@@ -36894,6 +36954,7 @@ ${t.inputText}` : t.inputText, {
36894
36954
  return;
36895
36955
  s.ended = true;
36896
36956
  s.queued = void 0;
36957
+ s.speech.length = 0;
36897
36958
  if (s.active) {
36898
36959
  s.active.state.abortStatus = "cancelled";
36899
36960
  s.active.state.controller.abort();
@@ -36009,6 +36009,45 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36009
36009
  ...link.label ? { label: link.label } : {}
36010
36010
  });
36011
36011
  }
36012
+ function trackSpeech(s, h, meta2) {
36013
+ const entry = {
36014
+ requestId: h.requestId,
36015
+ ...meta2
36016
+ };
36017
+ s.speech.push(entry);
36018
+ void h.done.then(() => {
36019
+ const i = s.speech.indexOf(entry);
36020
+ if (i >= 0)
36021
+ s.speech.splice(i, 1);
36022
+ });
36023
+ return h;
36024
+ }
36025
+ function flushSpeechForChat(s, keepTurnId) {
36026
+ const targets = s.speech.filter((e) => e.turnId !== keepTurnId);
36027
+ if (targets.length === 0)
36028
+ return;
36029
+ for (const e of targets) {
36030
+ cancelSpeech(s.sessionId, { requestId: e.requestId });
36031
+ }
36032
+ s.speech = s.speech.filter((e) => !targets.includes(e));
36033
+ agentLog("info", `chat message flushed ${targets.length} pending speech item(s)`, s.sessionId);
36034
+ const byTurn = /* @__PURE__ */ new Map();
36035
+ for (const e of targets) {
36036
+ if (!e.sentences)
36037
+ continue;
36038
+ const list = byTurn.get(e.turnId) ?? [];
36039
+ list.push(e);
36040
+ byTurn.set(e.turnId, list);
36041
+ }
36042
+ for (const [turnId, cut] of byTurn) {
36043
+ const sentences = cut[0].sentences;
36044
+ const cutIndexes = new Set(cut.flatMap((e) => e.indexes ?? []));
36045
+ const playedText = sentences.filter((_, i) => !cutIndexes.has(i)).join(" ");
36046
+ const unplayedText = cut.map((e) => e.text).join(" ");
36047
+ s.lastInterruption = { turnId, playedText, unplayedText };
36048
+ s.history.replaceAssistantText(turnId, playedText, useNote ? interruptionNote(playedText, unplayedText) : void 0);
36049
+ }
36050
+ }
36012
36051
  function deliver(s, t, text, interruptible = true) {
36013
36052
  flushSpeech(s, t);
36014
36053
  t.spoken.push(text);
@@ -36016,7 +36055,12 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36016
36055
  sendReplyDelta(s, t, text);
36017
36056
  if (t.source === "voice" || speakChat) {
36018
36057
  if (interruptible) {
36019
- speak(s.sessionId, text, replyOpts(t));
36058
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36059
+ turnId: t.turnId,
36060
+ text,
36061
+ sentences: t.spoken,
36062
+ indexes: [t.spoken.length - 1]
36063
+ });
36020
36064
  } else {
36021
36065
  const h = announce(s.sessionId, text, replyOpts(t));
36022
36066
  trackAnnouncement(s, h.done);
@@ -36029,7 +36073,11 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36029
36073
  function speakAnnouncement(s, turnId, text, interruptible, fillerKind) {
36030
36074
  const opts2 = fillerKind ? { turnId, kind: "filler", fillerKind } : { turnId };
36031
36075
  if (interruptible) {
36032
- return speak(s.sessionId, text, opts2);
36076
+ return trackSpeech(s, speak(s.sessionId, text, opts2), {
36077
+ turnId,
36078
+ text,
36079
+ ...fillerKind ? {} : { sentences: [text], indexes: [0] }
36080
+ });
36033
36081
  }
36034
36082
  const h = announce(s.sessionId, text, opts2);
36035
36083
  trackAnnouncement(s, h.done);
@@ -36117,11 +36165,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36117
36165
  if (t.pending.length === 0)
36118
36166
  return;
36119
36167
  const text = t.pending.join(" ");
36168
+ const base = t.spoken.length - t.pending.length;
36169
+ const indexes = t.pending.map((_, i) => base + i);
36120
36170
  t.pending = [];
36121
36171
  t.pendingChars = 0;
36122
36172
  if (t.controller.signal.aborted || s.ended)
36123
36173
  return;
36124
- speak(s.sessionId, text, replyOpts(t));
36174
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36175
+ turnId: t.turnId,
36176
+ text,
36177
+ sentences: t.spoken,
36178
+ indexes
36179
+ });
36125
36180
  }
36126
36181
  function deliverReply(s, t, text) {
36127
36182
  if (coalesceMs <= 0) {
@@ -36768,12 +36823,16 @@ ${t.inputText}` : t.inputText, {
36768
36823
  return;
36769
36824
  }
36770
36825
  const active = s.active;
36826
+ if (source === "chat" && policy === "cancel" && config2.bargeIn?.mode !== "off") {
36827
+ flushSpeechForChat(s, active?.turnId);
36828
+ }
36771
36829
  if (active) {
36772
36830
  if (policy === "finish") {
36773
36831
  s.queued = { text, source, utteranceId };
36774
36832
  return;
36775
36833
  }
36776
36834
  cancelSpeech(s.sessionId, { turnId: active.turnId });
36835
+ s.speech = s.speech.filter((e) => e.turnId !== active.turnId);
36777
36836
  active.state.abortStatus = "cancelled";
36778
36837
  active.state.controller.abort();
36779
36838
  clearFillerTimer(active.state);
@@ -36826,6 +36885,7 @@ ${t.inputText}` : t.inputText, {
36826
36885
  const s = {
36827
36886
  sessionId: ctx.sessionId,
36828
36887
  history: new ConversationHistory(historyTurns),
36888
+ speech: [],
36829
36889
  announcing: 0,
36830
36890
  chain: Promise.resolve(),
36831
36891
  ended: false
@@ -36894,6 +36954,7 @@ ${t.inputText}` : t.inputText, {
36894
36954
  return;
36895
36955
  s.ended = true;
36896
36956
  s.queued = void 0;
36957
+ s.speech.length = 0;
36897
36958
  if (s.active) {
36898
36959
  s.active.state.abortStatus = "cancelled";
36899
36960
  s.active.state.controller.abort();
@@ -36009,6 +36009,45 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36009
36009
  ...link.label ? { label: link.label } : {}
36010
36010
  });
36011
36011
  }
36012
+ function trackSpeech(s, h, meta2) {
36013
+ const entry = {
36014
+ requestId: h.requestId,
36015
+ ...meta2
36016
+ };
36017
+ s.speech.push(entry);
36018
+ void h.done.then(() => {
36019
+ const i = s.speech.indexOf(entry);
36020
+ if (i >= 0)
36021
+ s.speech.splice(i, 1);
36022
+ });
36023
+ return h;
36024
+ }
36025
+ function flushSpeechForChat(s, keepTurnId) {
36026
+ const targets = s.speech.filter((e) => e.turnId !== keepTurnId);
36027
+ if (targets.length === 0)
36028
+ return;
36029
+ for (const e of targets) {
36030
+ cancelSpeech(s.sessionId, { requestId: e.requestId });
36031
+ }
36032
+ s.speech = s.speech.filter((e) => !targets.includes(e));
36033
+ agentLog("info", `chat message flushed ${targets.length} pending speech item(s)`, s.sessionId);
36034
+ const byTurn = /* @__PURE__ */ new Map();
36035
+ for (const e of targets) {
36036
+ if (!e.sentences)
36037
+ continue;
36038
+ const list = byTurn.get(e.turnId) ?? [];
36039
+ list.push(e);
36040
+ byTurn.set(e.turnId, list);
36041
+ }
36042
+ for (const [turnId, cut] of byTurn) {
36043
+ const sentences = cut[0].sentences;
36044
+ const cutIndexes = new Set(cut.flatMap((e) => e.indexes ?? []));
36045
+ const playedText = sentences.filter((_, i) => !cutIndexes.has(i)).join(" ");
36046
+ const unplayedText = cut.map((e) => e.text).join(" ");
36047
+ s.lastInterruption = { turnId, playedText, unplayedText };
36048
+ s.history.replaceAssistantText(turnId, playedText, useNote ? interruptionNote(playedText, unplayedText) : void 0);
36049
+ }
36050
+ }
36012
36051
  function deliver(s, t, text, interruptible = true) {
36013
36052
  flushSpeech(s, t);
36014
36053
  t.spoken.push(text);
@@ -36016,7 +36055,12 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36016
36055
  sendReplyDelta(s, t, text);
36017
36056
  if (t.source === "voice" || speakChat) {
36018
36057
  if (interruptible) {
36019
- speak(s.sessionId, text, replyOpts(t));
36058
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36059
+ turnId: t.turnId,
36060
+ text,
36061
+ sentences: t.spoken,
36062
+ indexes: [t.spoken.length - 1]
36063
+ });
36020
36064
  } else {
36021
36065
  const h = announce(s.sessionId, text, replyOpts(t));
36022
36066
  trackAnnouncement(s, h.done);
@@ -36029,7 +36073,11 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36029
36073
  function speakAnnouncement(s, turnId, text, interruptible, fillerKind) {
36030
36074
  const opts2 = fillerKind ? { turnId, kind: "filler", fillerKind } : { turnId };
36031
36075
  if (interruptible) {
36032
- return speak(s.sessionId, text, opts2);
36076
+ return trackSpeech(s, speak(s.sessionId, text, opts2), {
36077
+ turnId,
36078
+ text,
36079
+ ...fillerKind ? {} : { sentences: [text], indexes: [0] }
36080
+ });
36033
36081
  }
36034
36082
  const h = announce(s.sessionId, text, opts2);
36035
36083
  trackAnnouncement(s, h.done);
@@ -36117,11 +36165,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36117
36165
  if (t.pending.length === 0)
36118
36166
  return;
36119
36167
  const text = t.pending.join(" ");
36168
+ const base = t.spoken.length - t.pending.length;
36169
+ const indexes = t.pending.map((_, i) => base + i);
36120
36170
  t.pending = [];
36121
36171
  t.pendingChars = 0;
36122
36172
  if (t.controller.signal.aborted || s.ended)
36123
36173
  return;
36124
- speak(s.sessionId, text, replyOpts(t));
36174
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36175
+ turnId: t.turnId,
36176
+ text,
36177
+ sentences: t.spoken,
36178
+ indexes
36179
+ });
36125
36180
  }
36126
36181
  function deliverReply(s, t, text) {
36127
36182
  if (coalesceMs <= 0) {
@@ -36768,12 +36823,16 @@ ${t.inputText}` : t.inputText, {
36768
36823
  return;
36769
36824
  }
36770
36825
  const active = s.active;
36826
+ if (source === "chat" && policy === "cancel" && config2.bargeIn?.mode !== "off") {
36827
+ flushSpeechForChat(s, active?.turnId);
36828
+ }
36771
36829
  if (active) {
36772
36830
  if (policy === "finish") {
36773
36831
  s.queued = { text, source, utteranceId };
36774
36832
  return;
36775
36833
  }
36776
36834
  cancelSpeech(s.sessionId, { turnId: active.turnId });
36835
+ s.speech = s.speech.filter((e) => e.turnId !== active.turnId);
36777
36836
  active.state.abortStatus = "cancelled";
36778
36837
  active.state.controller.abort();
36779
36838
  clearFillerTimer(active.state);
@@ -36826,6 +36885,7 @@ ${t.inputText}` : t.inputText, {
36826
36885
  const s = {
36827
36886
  sessionId: ctx.sessionId,
36828
36887
  history: new ConversationHistory(historyTurns),
36888
+ speech: [],
36829
36889
  announcing: 0,
36830
36890
  chain: Promise.resolve(),
36831
36891
  ended: false
@@ -36894,6 +36954,7 @@ ${t.inputText}` : t.inputText, {
36894
36954
  return;
36895
36955
  s.ended = true;
36896
36956
  s.queued = void 0;
36957
+ s.speech.length = 0;
36897
36958
  if (s.active) {
36898
36959
  s.active.state.abortStatus = "cancelled";
36899
36960
  s.active.state.controller.abort();