@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.
@@ -34680,9 +34680,16 @@ function validateLlmAgentConfig(input) {
34680
34680
  if (root.bargeIn !== void 0) {
34681
34681
  const b = c.object(root.bargeIn, "bargeIn");
34682
34682
  if (b) {
34683
- c.unknownKeys(b, ["mode"], "bargeIn");
34683
+ c.unknownKeys(b, ["mode", "typedMessages"], "bargeIn");
34684
34684
  if (b.mode === "project" || b.mode === "on" || b.mode === "off") {
34685
34685
  config2.bargeIn = { mode: b.mode };
34686
+ if (b.typedMessages !== void 0) {
34687
+ if (typeof b.typedMessages === "boolean") {
34688
+ config2.bargeIn.typedMessages = b.typedMessages;
34689
+ } else {
34690
+ c.err("bargeIn.typedMessages", "must be a boolean");
34691
+ }
34692
+ }
34686
34693
  } else {
34687
34694
  c.err("bargeIn.mode", 'must be one of "project", "on", "off"');
34688
34695
  }
@@ -34962,7 +34969,14 @@ var LLM_AGENT_CONFIG_JSON_SCHEMA = {
34962
34969
  type: "object",
34963
34970
  additionalProperties: false,
34964
34971
  required: ["mode"],
34965
- properties: { mode: { enum: ["project", "on", "off"] } },
34972
+ properties: {
34973
+ mode: { enum: ["project", "on", "off"] },
34974
+ typedMessages: {
34975
+ type: "boolean",
34976
+ default: true,
34977
+ 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."
34978
+ }
34979
+ },
34966
34980
  default: { mode: "project" }
34967
34981
  },
34968
34982
  speechDuringNonInterruptible: {
@@ -36009,6 +36023,45 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36009
36023
  ...link.label ? { label: link.label } : {}
36010
36024
  });
36011
36025
  }
36026
+ function trackSpeech(s, h, meta2) {
36027
+ const entry = {
36028
+ requestId: h.requestId,
36029
+ ...meta2
36030
+ };
36031
+ s.speech.push(entry);
36032
+ void h.done.then(() => {
36033
+ const i = s.speech.indexOf(entry);
36034
+ if (i >= 0)
36035
+ s.speech.splice(i, 1);
36036
+ });
36037
+ return h;
36038
+ }
36039
+ function flushSpeechForChat(s, keepTurnId) {
36040
+ const targets = s.speech.filter((e) => e.turnId !== keepTurnId);
36041
+ if (targets.length === 0)
36042
+ return;
36043
+ for (const e of targets) {
36044
+ cancelSpeech(s.sessionId, { requestId: e.requestId });
36045
+ }
36046
+ s.speech = s.speech.filter((e) => !targets.includes(e));
36047
+ agentLog("info", `chat message flushed ${targets.length} pending speech item(s)`, s.sessionId);
36048
+ const byTurn = /* @__PURE__ */ new Map();
36049
+ for (const e of targets) {
36050
+ if (!e.sentences)
36051
+ continue;
36052
+ const list = byTurn.get(e.turnId) ?? [];
36053
+ list.push(e);
36054
+ byTurn.set(e.turnId, list);
36055
+ }
36056
+ for (const [turnId, cut] of byTurn) {
36057
+ const sentences = cut[0].sentences;
36058
+ const cutIndexes = new Set(cut.flatMap((e) => e.indexes ?? []));
36059
+ const playedText = sentences.filter((_, i) => !cutIndexes.has(i)).join(" ");
36060
+ const unplayedText = cut.map((e) => e.text).join(" ");
36061
+ s.lastInterruption = { turnId, playedText, unplayedText };
36062
+ s.history.replaceAssistantText(turnId, playedText, useNote ? interruptionNote(playedText, unplayedText) : void 0);
36063
+ }
36064
+ }
36012
36065
  function deliver(s, t, text, interruptible = true) {
36013
36066
  flushSpeech(s, t);
36014
36067
  t.spoken.push(text);
@@ -36016,7 +36069,12 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36016
36069
  sendReplyDelta(s, t, text);
36017
36070
  if (t.source === "voice" || speakChat) {
36018
36071
  if (interruptible) {
36019
- speak(s.sessionId, text, replyOpts(t));
36072
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36073
+ turnId: t.turnId,
36074
+ text,
36075
+ sentences: t.spoken,
36076
+ indexes: [t.spoken.length - 1]
36077
+ });
36020
36078
  } else {
36021
36079
  const h = announce(s.sessionId, text, replyOpts(t));
36022
36080
  trackAnnouncement(s, h.done);
@@ -36029,7 +36087,11 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36029
36087
  function speakAnnouncement(s, turnId, text, interruptible, fillerKind) {
36030
36088
  const opts2 = fillerKind ? { turnId, kind: "filler", fillerKind } : { turnId };
36031
36089
  if (interruptible) {
36032
- return speak(s.sessionId, text, opts2);
36090
+ return trackSpeech(s, speak(s.sessionId, text, opts2), {
36091
+ turnId,
36092
+ text,
36093
+ ...fillerKind ? {} : { sentences: [text], indexes: [0] }
36094
+ });
36033
36095
  }
36034
36096
  const h = announce(s.sessionId, text, opts2);
36035
36097
  trackAnnouncement(s, h.done);
@@ -36117,11 +36179,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36117
36179
  if (t.pending.length === 0)
36118
36180
  return;
36119
36181
  const text = t.pending.join(" ");
36182
+ const base = t.spoken.length - t.pending.length;
36183
+ const indexes = t.pending.map((_, i) => base + i);
36120
36184
  t.pending = [];
36121
36185
  t.pendingChars = 0;
36122
36186
  if (t.controller.signal.aborted || s.ended)
36123
36187
  return;
36124
- speak(s.sessionId, text, replyOpts(t));
36188
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36189
+ turnId: t.turnId,
36190
+ text,
36191
+ sentences: t.spoken,
36192
+ indexes
36193
+ });
36125
36194
  }
36126
36195
  function deliverReply(s, t, text) {
36127
36196
  if (coalesceMs <= 0) {
@@ -36768,12 +36837,16 @@ ${t.inputText}` : t.inputText, {
36768
36837
  return;
36769
36838
  }
36770
36839
  const active = s.active;
36840
+ if (source === "chat" && policy === "cancel" && config2.bargeIn?.mode !== "off" && config2.bargeIn?.typedMessages !== false) {
36841
+ flushSpeechForChat(s, active?.turnId);
36842
+ }
36771
36843
  if (active) {
36772
36844
  if (policy === "finish") {
36773
36845
  s.queued = { text, source, utteranceId };
36774
36846
  return;
36775
36847
  }
36776
36848
  cancelSpeech(s.sessionId, { turnId: active.turnId });
36849
+ s.speech = s.speech.filter((e) => e.turnId !== active.turnId);
36777
36850
  active.state.abortStatus = "cancelled";
36778
36851
  active.state.controller.abort();
36779
36852
  clearFillerTimer(active.state);
@@ -36826,6 +36899,7 @@ ${t.inputText}` : t.inputText, {
36826
36899
  const s = {
36827
36900
  sessionId: ctx.sessionId,
36828
36901
  history: new ConversationHistory(historyTurns),
36902
+ speech: [],
36829
36903
  announcing: 0,
36830
36904
  chain: Promise.resolve(),
36831
36905
  ended: false
@@ -36894,6 +36968,7 @@ ${t.inputText}` : t.inputText, {
36894
36968
  return;
36895
36969
  s.ended = true;
36896
36970
  s.queued = void 0;
36971
+ s.speech.length = 0;
36897
36972
  if (s.active) {
36898
36973
  s.active.state.abortStatus = "cancelled";
36899
36974
  s.active.state.controller.abort();
@@ -34680,9 +34680,16 @@ function validateLlmAgentConfig(input) {
34680
34680
  if (root.bargeIn !== void 0) {
34681
34681
  const b = c.object(root.bargeIn, "bargeIn");
34682
34682
  if (b) {
34683
- c.unknownKeys(b, ["mode"], "bargeIn");
34683
+ c.unknownKeys(b, ["mode", "typedMessages"], "bargeIn");
34684
34684
  if (b.mode === "project" || b.mode === "on" || b.mode === "off") {
34685
34685
  config2.bargeIn = { mode: b.mode };
34686
+ if (b.typedMessages !== void 0) {
34687
+ if (typeof b.typedMessages === "boolean") {
34688
+ config2.bargeIn.typedMessages = b.typedMessages;
34689
+ } else {
34690
+ c.err("bargeIn.typedMessages", "must be a boolean");
34691
+ }
34692
+ }
34686
34693
  } else {
34687
34694
  c.err("bargeIn.mode", 'must be one of "project", "on", "off"');
34688
34695
  }
@@ -34962,7 +34969,14 @@ var LLM_AGENT_CONFIG_JSON_SCHEMA = {
34962
34969
  type: "object",
34963
34970
  additionalProperties: false,
34964
34971
  required: ["mode"],
34965
- properties: { mode: { enum: ["project", "on", "off"] } },
34972
+ properties: {
34973
+ mode: { enum: ["project", "on", "off"] },
34974
+ typedMessages: {
34975
+ type: "boolean",
34976
+ default: true,
34977
+ 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."
34978
+ }
34979
+ },
34966
34980
  default: { mode: "project" }
34967
34981
  },
34968
34982
  speechDuringNonInterruptible: {
@@ -36009,6 +36023,45 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36009
36023
  ...link.label ? { label: link.label } : {}
36010
36024
  });
36011
36025
  }
36026
+ function trackSpeech(s, h, meta2) {
36027
+ const entry = {
36028
+ requestId: h.requestId,
36029
+ ...meta2
36030
+ };
36031
+ s.speech.push(entry);
36032
+ void h.done.then(() => {
36033
+ const i = s.speech.indexOf(entry);
36034
+ if (i >= 0)
36035
+ s.speech.splice(i, 1);
36036
+ });
36037
+ return h;
36038
+ }
36039
+ function flushSpeechForChat(s, keepTurnId) {
36040
+ const targets = s.speech.filter((e) => e.turnId !== keepTurnId);
36041
+ if (targets.length === 0)
36042
+ return;
36043
+ for (const e of targets) {
36044
+ cancelSpeech(s.sessionId, { requestId: e.requestId });
36045
+ }
36046
+ s.speech = s.speech.filter((e) => !targets.includes(e));
36047
+ agentLog("info", `chat message flushed ${targets.length} pending speech item(s)`, s.sessionId);
36048
+ const byTurn = /* @__PURE__ */ new Map();
36049
+ for (const e of targets) {
36050
+ if (!e.sentences)
36051
+ continue;
36052
+ const list = byTurn.get(e.turnId) ?? [];
36053
+ list.push(e);
36054
+ byTurn.set(e.turnId, list);
36055
+ }
36056
+ for (const [turnId, cut] of byTurn) {
36057
+ const sentences = cut[0].sentences;
36058
+ const cutIndexes = new Set(cut.flatMap((e) => e.indexes ?? []));
36059
+ const playedText = sentences.filter((_, i) => !cutIndexes.has(i)).join(" ");
36060
+ const unplayedText = cut.map((e) => e.text).join(" ");
36061
+ s.lastInterruption = { turnId, playedText, unplayedText };
36062
+ s.history.replaceAssistantText(turnId, playedText, useNote ? interruptionNote(playedText, unplayedText) : void 0);
36063
+ }
36064
+ }
36012
36065
  function deliver(s, t, text, interruptible = true) {
36013
36066
  flushSpeech(s, t);
36014
36067
  t.spoken.push(text);
@@ -36016,7 +36069,12 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36016
36069
  sendReplyDelta(s, t, text);
36017
36070
  if (t.source === "voice" || speakChat) {
36018
36071
  if (interruptible) {
36019
- speak(s.sessionId, text, replyOpts(t));
36072
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36073
+ turnId: t.turnId,
36074
+ text,
36075
+ sentences: t.spoken,
36076
+ indexes: [t.spoken.length - 1]
36077
+ });
36020
36078
  } else {
36021
36079
  const h = announce(s.sessionId, text, replyOpts(t));
36022
36080
  trackAnnouncement(s, h.done);
@@ -36029,7 +36087,11 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36029
36087
  function speakAnnouncement(s, turnId, text, interruptible, fillerKind) {
36030
36088
  const opts2 = fillerKind ? { turnId, kind: "filler", fillerKind } : { turnId };
36031
36089
  if (interruptible) {
36032
- return speak(s.sessionId, text, opts2);
36090
+ return trackSpeech(s, speak(s.sessionId, text, opts2), {
36091
+ turnId,
36092
+ text,
36093
+ ...fillerKind ? {} : { sentences: [text], indexes: [0] }
36094
+ });
36033
36095
  }
36034
36096
  const h = announce(s.sessionId, text, opts2);
36035
36097
  trackAnnouncement(s, h.done);
@@ -36117,11 +36179,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36117
36179
  if (t.pending.length === 0)
36118
36180
  return;
36119
36181
  const text = t.pending.join(" ");
36182
+ const base = t.spoken.length - t.pending.length;
36183
+ const indexes = t.pending.map((_, i) => base + i);
36120
36184
  t.pending = [];
36121
36185
  t.pendingChars = 0;
36122
36186
  if (t.controller.signal.aborted || s.ended)
36123
36187
  return;
36124
- speak(s.sessionId, text, replyOpts(t));
36188
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36189
+ turnId: t.turnId,
36190
+ text,
36191
+ sentences: t.spoken,
36192
+ indexes
36193
+ });
36125
36194
  }
36126
36195
  function deliverReply(s, t, text) {
36127
36196
  if (coalesceMs <= 0) {
@@ -36768,12 +36837,16 @@ ${t.inputText}` : t.inputText, {
36768
36837
  return;
36769
36838
  }
36770
36839
  const active = s.active;
36840
+ if (source === "chat" && policy === "cancel" && config2.bargeIn?.mode !== "off" && config2.bargeIn?.typedMessages !== false) {
36841
+ flushSpeechForChat(s, active?.turnId);
36842
+ }
36771
36843
  if (active) {
36772
36844
  if (policy === "finish") {
36773
36845
  s.queued = { text, source, utteranceId };
36774
36846
  return;
36775
36847
  }
36776
36848
  cancelSpeech(s.sessionId, { turnId: active.turnId });
36849
+ s.speech = s.speech.filter((e) => e.turnId !== active.turnId);
36777
36850
  active.state.abortStatus = "cancelled";
36778
36851
  active.state.controller.abort();
36779
36852
  clearFillerTimer(active.state);
@@ -36826,6 +36899,7 @@ ${t.inputText}` : t.inputText, {
36826
36899
  const s = {
36827
36900
  sessionId: ctx.sessionId,
36828
36901
  history: new ConversationHistory(historyTurns),
36902
+ speech: [],
36829
36903
  announcing: 0,
36830
36904
  chain: Promise.resolve(),
36831
36905
  ended: false
@@ -36894,6 +36968,7 @@ ${t.inputText}` : t.inputText, {
36894
36968
  return;
36895
36969
  s.ended = true;
36896
36970
  s.queued = void 0;
36971
+ s.speech.length = 0;
36897
36972
  if (s.active) {
36898
36973
  s.active.state.abortStatus = "cancelled";
36899
36974
  s.active.state.controller.abort();
@@ -34680,9 +34680,16 @@ function validateLlmAgentConfig(input) {
34680
34680
  if (root.bargeIn !== void 0) {
34681
34681
  const b = c.object(root.bargeIn, "bargeIn");
34682
34682
  if (b) {
34683
- c.unknownKeys(b, ["mode"], "bargeIn");
34683
+ c.unknownKeys(b, ["mode", "typedMessages"], "bargeIn");
34684
34684
  if (b.mode === "project" || b.mode === "on" || b.mode === "off") {
34685
34685
  config2.bargeIn = { mode: b.mode };
34686
+ if (b.typedMessages !== void 0) {
34687
+ if (typeof b.typedMessages === "boolean") {
34688
+ config2.bargeIn.typedMessages = b.typedMessages;
34689
+ } else {
34690
+ c.err("bargeIn.typedMessages", "must be a boolean");
34691
+ }
34692
+ }
34686
34693
  } else {
34687
34694
  c.err("bargeIn.mode", 'must be one of "project", "on", "off"');
34688
34695
  }
@@ -34962,7 +34969,14 @@ var LLM_AGENT_CONFIG_JSON_SCHEMA = {
34962
34969
  type: "object",
34963
34970
  additionalProperties: false,
34964
34971
  required: ["mode"],
34965
- properties: { mode: { enum: ["project", "on", "off"] } },
34972
+ properties: {
34973
+ mode: { enum: ["project", "on", "off"] },
34974
+ typedMessages: {
34975
+ type: "boolean",
34976
+ default: true,
34977
+ 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."
34978
+ }
34979
+ },
34966
34980
  default: { mode: "project" }
34967
34981
  },
34968
34982
  speechDuringNonInterruptible: {
@@ -36009,6 +36023,45 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36009
36023
  ...link.label ? { label: link.label } : {}
36010
36024
  });
36011
36025
  }
36026
+ function trackSpeech(s, h, meta2) {
36027
+ const entry = {
36028
+ requestId: h.requestId,
36029
+ ...meta2
36030
+ };
36031
+ s.speech.push(entry);
36032
+ void h.done.then(() => {
36033
+ const i = s.speech.indexOf(entry);
36034
+ if (i >= 0)
36035
+ s.speech.splice(i, 1);
36036
+ });
36037
+ return h;
36038
+ }
36039
+ function flushSpeechForChat(s, keepTurnId) {
36040
+ const targets = s.speech.filter((e) => e.turnId !== keepTurnId);
36041
+ if (targets.length === 0)
36042
+ return;
36043
+ for (const e of targets) {
36044
+ cancelSpeech(s.sessionId, { requestId: e.requestId });
36045
+ }
36046
+ s.speech = s.speech.filter((e) => !targets.includes(e));
36047
+ agentLog("info", `chat message flushed ${targets.length} pending speech item(s)`, s.sessionId);
36048
+ const byTurn = /* @__PURE__ */ new Map();
36049
+ for (const e of targets) {
36050
+ if (!e.sentences)
36051
+ continue;
36052
+ const list = byTurn.get(e.turnId) ?? [];
36053
+ list.push(e);
36054
+ byTurn.set(e.turnId, list);
36055
+ }
36056
+ for (const [turnId, cut] of byTurn) {
36057
+ const sentences = cut[0].sentences;
36058
+ const cutIndexes = new Set(cut.flatMap((e) => e.indexes ?? []));
36059
+ const playedText = sentences.filter((_, i) => !cutIndexes.has(i)).join(" ");
36060
+ const unplayedText = cut.map((e) => e.text).join(" ");
36061
+ s.lastInterruption = { turnId, playedText, unplayedText };
36062
+ s.history.replaceAssistantText(turnId, playedText, useNote ? interruptionNote(playedText, unplayedText) : void 0);
36063
+ }
36064
+ }
36012
36065
  function deliver(s, t, text, interruptible = true) {
36013
36066
  flushSpeech(s, t);
36014
36067
  t.spoken.push(text);
@@ -36016,7 +36069,12 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36016
36069
  sendReplyDelta(s, t, text);
36017
36070
  if (t.source === "voice" || speakChat) {
36018
36071
  if (interruptible) {
36019
- speak(s.sessionId, text, replyOpts(t));
36072
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36073
+ turnId: t.turnId,
36074
+ text,
36075
+ sentences: t.spoken,
36076
+ indexes: [t.spoken.length - 1]
36077
+ });
36020
36078
  } else {
36021
36079
  const h = announce(s.sessionId, text, replyOpts(t));
36022
36080
  trackAnnouncement(s, h.done);
@@ -36029,7 +36087,11 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36029
36087
  function speakAnnouncement(s, turnId, text, interruptible, fillerKind) {
36030
36088
  const opts2 = fillerKind ? { turnId, kind: "filler", fillerKind } : { turnId };
36031
36089
  if (interruptible) {
36032
- return speak(s.sessionId, text, opts2);
36090
+ return trackSpeech(s, speak(s.sessionId, text, opts2), {
36091
+ turnId,
36092
+ text,
36093
+ ...fillerKind ? {} : { sentences: [text], indexes: [0] }
36094
+ });
36033
36095
  }
36034
36096
  const h = announce(s.sessionId, text, opts2);
36035
36097
  trackAnnouncement(s, h.done);
@@ -36117,11 +36179,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36117
36179
  if (t.pending.length === 0)
36118
36180
  return;
36119
36181
  const text = t.pending.join(" ");
36182
+ const base = t.spoken.length - t.pending.length;
36183
+ const indexes = t.pending.map((_, i) => base + i);
36120
36184
  t.pending = [];
36121
36185
  t.pendingChars = 0;
36122
36186
  if (t.controller.signal.aborted || s.ended)
36123
36187
  return;
36124
- speak(s.sessionId, text, replyOpts(t));
36188
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36189
+ turnId: t.turnId,
36190
+ text,
36191
+ sentences: t.spoken,
36192
+ indexes
36193
+ });
36125
36194
  }
36126
36195
  function deliverReply(s, t, text) {
36127
36196
  if (coalesceMs <= 0) {
@@ -36768,12 +36837,16 @@ ${t.inputText}` : t.inputText, {
36768
36837
  return;
36769
36838
  }
36770
36839
  const active = s.active;
36840
+ if (source === "chat" && policy === "cancel" && config2.bargeIn?.mode !== "off" && config2.bargeIn?.typedMessages !== false) {
36841
+ flushSpeechForChat(s, active?.turnId);
36842
+ }
36771
36843
  if (active) {
36772
36844
  if (policy === "finish") {
36773
36845
  s.queued = { text, source, utteranceId };
36774
36846
  return;
36775
36847
  }
36776
36848
  cancelSpeech(s.sessionId, { turnId: active.turnId });
36849
+ s.speech = s.speech.filter((e) => e.turnId !== active.turnId);
36777
36850
  active.state.abortStatus = "cancelled";
36778
36851
  active.state.controller.abort();
36779
36852
  clearFillerTimer(active.state);
@@ -36826,6 +36899,7 @@ ${t.inputText}` : t.inputText, {
36826
36899
  const s = {
36827
36900
  sessionId: ctx.sessionId,
36828
36901
  history: new ConversationHistory(historyTurns),
36902
+ speech: [],
36829
36903
  announcing: 0,
36830
36904
  chain: Promise.resolve(),
36831
36905
  ended: false
@@ -36894,6 +36968,7 @@ ${t.inputText}` : t.inputText, {
36894
36968
  return;
36895
36969
  s.ended = true;
36896
36970
  s.queued = void 0;
36971
+ s.speech.length = 0;
36897
36972
  if (s.active) {
36898
36973
  s.active.state.abortStatus = "cancelled";
36899
36974
  s.active.state.controller.abort();
@@ -34680,9 +34680,16 @@ function validateLlmAgentConfig(input) {
34680
34680
  if (root.bargeIn !== void 0) {
34681
34681
  const b = c.object(root.bargeIn, "bargeIn");
34682
34682
  if (b) {
34683
- c.unknownKeys(b, ["mode"], "bargeIn");
34683
+ c.unknownKeys(b, ["mode", "typedMessages"], "bargeIn");
34684
34684
  if (b.mode === "project" || b.mode === "on" || b.mode === "off") {
34685
34685
  config2.bargeIn = { mode: b.mode };
34686
+ if (b.typedMessages !== void 0) {
34687
+ if (typeof b.typedMessages === "boolean") {
34688
+ config2.bargeIn.typedMessages = b.typedMessages;
34689
+ } else {
34690
+ c.err("bargeIn.typedMessages", "must be a boolean");
34691
+ }
34692
+ }
34686
34693
  } else {
34687
34694
  c.err("bargeIn.mode", 'must be one of "project", "on", "off"');
34688
34695
  }
@@ -34962,7 +34969,14 @@ var LLM_AGENT_CONFIG_JSON_SCHEMA = {
34962
34969
  type: "object",
34963
34970
  additionalProperties: false,
34964
34971
  required: ["mode"],
34965
- properties: { mode: { enum: ["project", "on", "off"] } },
34972
+ properties: {
34973
+ mode: { enum: ["project", "on", "off"] },
34974
+ typedMessages: {
34975
+ type: "boolean",
34976
+ default: true,
34977
+ 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."
34978
+ }
34979
+ },
34966
34980
  default: { mode: "project" }
34967
34981
  },
34968
34982
  speechDuringNonInterruptible: {
@@ -36009,6 +36023,45 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36009
36023
  ...link.label ? { label: link.label } : {}
36010
36024
  });
36011
36025
  }
36026
+ function trackSpeech(s, h, meta2) {
36027
+ const entry = {
36028
+ requestId: h.requestId,
36029
+ ...meta2
36030
+ };
36031
+ s.speech.push(entry);
36032
+ void h.done.then(() => {
36033
+ const i = s.speech.indexOf(entry);
36034
+ if (i >= 0)
36035
+ s.speech.splice(i, 1);
36036
+ });
36037
+ return h;
36038
+ }
36039
+ function flushSpeechForChat(s, keepTurnId) {
36040
+ const targets = s.speech.filter((e) => e.turnId !== keepTurnId);
36041
+ if (targets.length === 0)
36042
+ return;
36043
+ for (const e of targets) {
36044
+ cancelSpeech(s.sessionId, { requestId: e.requestId });
36045
+ }
36046
+ s.speech = s.speech.filter((e) => !targets.includes(e));
36047
+ agentLog("info", `chat message flushed ${targets.length} pending speech item(s)`, s.sessionId);
36048
+ const byTurn = /* @__PURE__ */ new Map();
36049
+ for (const e of targets) {
36050
+ if (!e.sentences)
36051
+ continue;
36052
+ const list = byTurn.get(e.turnId) ?? [];
36053
+ list.push(e);
36054
+ byTurn.set(e.turnId, list);
36055
+ }
36056
+ for (const [turnId, cut] of byTurn) {
36057
+ const sentences = cut[0].sentences;
36058
+ const cutIndexes = new Set(cut.flatMap((e) => e.indexes ?? []));
36059
+ const playedText = sentences.filter((_, i) => !cutIndexes.has(i)).join(" ");
36060
+ const unplayedText = cut.map((e) => e.text).join(" ");
36061
+ s.lastInterruption = { turnId, playedText, unplayedText };
36062
+ s.history.replaceAssistantText(turnId, playedText, useNote ? interruptionNote(playedText, unplayedText) : void 0);
36063
+ }
36064
+ }
36012
36065
  function deliver(s, t, text, interruptible = true) {
36013
36066
  flushSpeech(s, t);
36014
36067
  t.spoken.push(text);
@@ -36016,7 +36069,12 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36016
36069
  sendReplyDelta(s, t, text);
36017
36070
  if (t.source === "voice" || speakChat) {
36018
36071
  if (interruptible) {
36019
- speak(s.sessionId, text, replyOpts(t));
36072
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36073
+ turnId: t.turnId,
36074
+ text,
36075
+ sentences: t.spoken,
36076
+ indexes: [t.spoken.length - 1]
36077
+ });
36020
36078
  } else {
36021
36079
  const h = announce(s.sessionId, text, replyOpts(t));
36022
36080
  trackAnnouncement(s, h.done);
@@ -36029,7 +36087,11 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36029
36087
  function speakAnnouncement(s, turnId, text, interruptible, fillerKind) {
36030
36088
  const opts2 = fillerKind ? { turnId, kind: "filler", fillerKind } : { turnId };
36031
36089
  if (interruptible) {
36032
- return speak(s.sessionId, text, opts2);
36090
+ return trackSpeech(s, speak(s.sessionId, text, opts2), {
36091
+ turnId,
36092
+ text,
36093
+ ...fillerKind ? {} : { sentences: [text], indexes: [0] }
36094
+ });
36033
36095
  }
36034
36096
  const h = announce(s.sessionId, text, opts2);
36035
36097
  trackAnnouncement(s, h.done);
@@ -36117,11 +36179,18 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
36117
36179
  if (t.pending.length === 0)
36118
36180
  return;
36119
36181
  const text = t.pending.join(" ");
36182
+ const base = t.spoken.length - t.pending.length;
36183
+ const indexes = t.pending.map((_, i) => base + i);
36120
36184
  t.pending = [];
36121
36185
  t.pendingChars = 0;
36122
36186
  if (t.controller.signal.aborted || s.ended)
36123
36187
  return;
36124
- speak(s.sessionId, text, replyOpts(t));
36188
+ trackSpeech(s, speak(s.sessionId, text, replyOpts(t)), {
36189
+ turnId: t.turnId,
36190
+ text,
36191
+ sentences: t.spoken,
36192
+ indexes
36193
+ });
36125
36194
  }
36126
36195
  function deliverReply(s, t, text) {
36127
36196
  if (coalesceMs <= 0) {
@@ -36768,12 +36837,16 @@ ${t.inputText}` : t.inputText, {
36768
36837
  return;
36769
36838
  }
36770
36839
  const active = s.active;
36840
+ if (source === "chat" && policy === "cancel" && config2.bargeIn?.mode !== "off" && config2.bargeIn?.typedMessages !== false) {
36841
+ flushSpeechForChat(s, active?.turnId);
36842
+ }
36771
36843
  if (active) {
36772
36844
  if (policy === "finish") {
36773
36845
  s.queued = { text, source, utteranceId };
36774
36846
  return;
36775
36847
  }
36776
36848
  cancelSpeech(s.sessionId, { turnId: active.turnId });
36849
+ s.speech = s.speech.filter((e) => e.turnId !== active.turnId);
36777
36850
  active.state.abortStatus = "cancelled";
36778
36851
  active.state.controller.abort();
36779
36852
  clearFillerTimer(active.state);
@@ -36826,6 +36899,7 @@ ${t.inputText}` : t.inputText, {
36826
36899
  const s = {
36827
36900
  sessionId: ctx.sessionId,
36828
36901
  history: new ConversationHistory(historyTurns),
36902
+ speech: [],
36829
36903
  announcing: 0,
36830
36904
  chain: Promise.resolve(),
36831
36905
  ended: false
@@ -36894,6 +36968,7 @@ ${t.inputText}` : t.inputText, {
36894
36968
  return;
36895
36969
  s.ended = true;
36896
36970
  s.queued = void 0;
36971
+ s.speech.length = 0;
36897
36972
  if (s.active) {
36898
36973
  s.active.state.abortStatus = "cancelled";
36899
36974
  s.active.state.controller.abort();