@voicethere/agent 0.10.0 → 0.11.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.
Files changed (40) hide show
  1. package/README.md +88 -0
  2. package/dist/agent.js +175 -5
  3. package/dist/host/index.d.ts +4 -0
  4. package/dist/host/index.d.ts.map +1 -0
  5. package/dist/host/index.js +3 -0
  6. package/dist/host/index.js.map +1 -0
  7. package/dist/host/session-speak-controller.d.ts +70 -0
  8. package/dist/host/session-speak-controller.d.ts.map +1 -0
  9. package/dist/host/session-speak-controller.js +296 -0
  10. package/dist/host/session-speak-controller.js.map +1 -0
  11. package/dist/host/speak-text.d.ts +11 -0
  12. package/dist/host/speak-text.d.ts.map +1 -0
  13. package/dist/host/speak-text.js +21 -0
  14. package/dist/host/speak-text.js.map +1 -0
  15. package/dist/index.d.ts +2 -2
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/protocol.d.ts +95 -2
  20. package/dist/protocol.d.ts.map +1 -1
  21. package/dist/protocol.js.map +1 -1
  22. package/dist/runtime.d.ts +69 -6
  23. package/dist/runtime.d.ts.map +1 -1
  24. package/dist/runtime.js +274 -7
  25. package/dist/runtime.js.map +1 -1
  26. package/dist/templates/echo/agent.js +178 -7
  27. package/dist/templates/echo-dc/agent.js +85 -2
  28. package/dist/templates/game-sync/agent.js +85 -2
  29. package/dist/templates/language-switch/agent.js +173 -5
  30. package/dist/templates/positional-tts/agent.js +173 -5
  31. package/dist/templates/recording-consent/agent.js +173 -5
  32. package/dist/templates/spatial-showcase/agent.js +173 -5
  33. package/dist/templates/voice-showcase/agent.js +178 -7
  34. package/dist/templates/voice-starter/agent.js +176 -5
  35. package/dist/templates/webhooks/agent.js +176 -5
  36. package/dist/templates/webhooks-redis/agent.js +85 -2
  37. package/dist/templates/world-sync/agent.js +85 -2
  38. package/dist/templates/world-sync-binary/agent.js +85 -2
  39. package/package.json +7 -1
  40. package/templates/mix-smoke/agent.ts +11 -2
@@ -151,6 +151,18 @@ var SessionSerialQueue = class {
151
151
  var SESSION_START_INIT_DELAY_ENABLED_ENV = "AGENT_SESSION_START_INIT_DELAY_ENABLED";
152
152
  var SESSION_START_INIT_DELAY_MS_ENV = "AGENT_SESSION_START_INIT_DELAY_MS";
153
153
  var DEFAULT_SESSION_START_INIT_DELAY_MS = 500;
154
+ function isSpeakResultMessage(value) {
155
+ if (!value || typeof value !== "object")
156
+ return false;
157
+ const msg = value;
158
+ return msg.type === "speak_result" && typeof msg.requestId === "string";
159
+ }
160
+ function isBargeInControlAckMessage(value) {
161
+ if (!value || typeof value !== "object")
162
+ return false;
163
+ const msg = value;
164
+ return msg.type === "barge_in_control_ack" && typeof msg.requestId === "string";
165
+ }
154
166
  function isPlayAckMessage(value) {
155
167
  if (!value || typeof value !== "object")
156
168
  return false;
@@ -259,7 +271,7 @@ function isParentMessage(value) {
259
271
  if (!value || typeof value !== "object")
260
272
  return false;
261
273
  const msg = value;
262
- return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "webhook";
274
+ return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "speak_result" || msg.type === "barge_in_control_ack" || msg.type === "agent_interrupted" || msg.type === "webhook";
263
275
  }
264
276
  function isSessionScopedParentMessage(value) {
265
277
  return isParentMessage(value) && !isWebhookMessage(value);
@@ -278,7 +290,61 @@ var mixAvailableBySessionId = /* @__PURE__ */ new Map();
278
290
  var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
279
291
  var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
280
292
  var ttsPoseAvailableBySessionId = /* @__PURE__ */ new Map();
293
+ var speakControlAvailableBySessionId = /* @__PURE__ */ new Map();
294
+ var speakControlWarnedSessionIds = /* @__PURE__ */ new Set();
281
295
  var endedSessionIds = /* @__PURE__ */ new Set();
296
+ var SPEAK_RESULT_SAFETY_TIMEOUT_MS = 6e5;
297
+ var pendingSpeakResults = /* @__PURE__ */ new Map();
298
+ var pendingBargeInAcks = /* @__PURE__ */ new Map();
299
+ function handleSpeakResult(message) {
300
+ const pending = pendingSpeakResults.get(message.requestId);
301
+ if (!pending)
302
+ return;
303
+ clearTimeout(pending.timer);
304
+ pendingSpeakResults.delete(message.requestId);
305
+ pending.resolve({
306
+ requestId: message.requestId,
307
+ ...message.turnId !== void 0 ? { turnId: message.turnId } : {},
308
+ status: message.status,
309
+ playedMs: message.playedMs,
310
+ ...message.synthesizedMs !== void 0 ? { synthesizedMs: message.synthesizedMs } : {},
311
+ ...message.errorCode !== void 0 ? { errorCode: message.errorCode } : {}
312
+ });
313
+ }
314
+ function handleBargeInControlAck(message) {
315
+ const pending = pendingBargeInAcks.get(message.requestId);
316
+ if (!pending)
317
+ return;
318
+ clearTimeout(pending.timer);
319
+ pendingBargeInAcks.delete(message.requestId);
320
+ pending.resolve({
321
+ ok: message.ok,
322
+ ...message.reason ? { reason: message.reason } : {}
323
+ });
324
+ }
325
+ function clearPendingSpeakForSession(sessionId, errorCode) {
326
+ for (const [requestId, pending] of pendingSpeakResults) {
327
+ if (pending.sessionId !== sessionId)
328
+ continue;
329
+ clearTimeout(pending.timer);
330
+ pendingSpeakResults.delete(requestId);
331
+ pending.resolve({
332
+ requestId,
333
+ ...pending.turnId !== void 0 ? { turnId: pending.turnId } : {},
334
+ status: "cancelled",
335
+ playedMs: 0,
336
+ errorCode
337
+ });
338
+ }
339
+ for (const [requestId, pending] of pendingBargeInAcks) {
340
+ if (pending.sessionId !== sessionId)
341
+ continue;
342
+ clearTimeout(pending.timer);
343
+ pendingBargeInAcks.delete(requestId);
344
+ pending.resolve({ ok: false, reason: "session_ended" });
345
+ }
346
+ speakControlWarnedSessionIds.delete(sessionId);
347
+ }
282
348
  var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
283
349
  var pendingRecordingAcks = /* @__PURE__ */ new Map();
284
350
  var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
@@ -458,6 +524,9 @@ function clearPendingConversationHistoryAcksForSession(sessionId, reason) {
458
524
  pending.resolve({ ok: false, reason, requestId });
459
525
  }
460
526
  }
527
+ function isVoicethereAgentChild() {
528
+ return typeof process.send === "function" && process.connected !== false && typeof process.env.__CHILD_BUNDLE_PATH__ === "string" && process.env.__CHILD_BUNDLE_PATH__.trim().length > 0;
529
+ }
461
530
  var sessionExecutionContext = new AsyncLocalStorage();
462
531
  var agentLogSessionContext = new AsyncLocalStorage();
463
532
  var inboundQueueAuthority = null;
@@ -608,6 +677,7 @@ async function handleParentMessage(message, handlers) {
608
677
  mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
609
678
  voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
610
679
  ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
680
+ speakControlAvailableBySessionId.set(message.sessionId, message.speakControlAvailable ?? false);
611
681
  if (message.voiceLanguageSwitch) {
612
682
  voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
613
683
  } else {
@@ -624,7 +694,8 @@ async function handleParentMessage(message, handlers) {
624
694
  conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
625
695
  mixAvailable: message.mixAvailable ?? false,
626
696
  voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
627
- ttsPoseAvailable: message.ttsPoseAvailable ?? false
697
+ ttsPoseAvailable: message.ttsPoseAvailable ?? false,
698
+ speakControlAvailable: message.speakControlAvailable ?? false
628
699
  });
629
700
  sendParentMessage({
630
701
  type: "session_start_ack",
@@ -661,6 +732,11 @@ async function handleParentMessage(message, handlers) {
661
732
  }
662
733
  }
663
734
  break;
735
+ case "agent_interrupted": {
736
+ const { type: _type, ...interrupted } = message;
737
+ await handlers.onAgentInterrupted?.(interrupted);
738
+ break;
739
+ }
664
740
  case "data_channel_message":
665
741
  await handlers.onDataChannelMessage?.({
666
742
  sessionId: message.sessionId,
@@ -690,6 +766,8 @@ async function handleParentMessage(message, handlers) {
690
766
  voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
691
767
  lastVoiceLanguageBySessionId.delete(message.sessionId);
692
768
  ttsPoseAvailableBySessionId.delete(message.sessionId);
769
+ speakControlAvailableBySessionId.delete(message.sessionId);
770
+ clearPendingSpeakForSession(message.sessionId, "session_ended");
693
771
  await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
694
772
  sessionId: message.sessionId
695
773
  });
@@ -713,6 +791,14 @@ function defineAgent(handlers) {
713
791
  handleConversationHistoryControlAck(message);
714
792
  return;
715
793
  }
794
+ if (isSpeakResultMessage(message)) {
795
+ handleSpeakResult(message);
796
+ return;
797
+ }
798
+ if (isBargeInControlAckMessage(message)) {
799
+ handleBargeInControlAck(message);
800
+ return;
801
+ }
716
802
  if (isPlayAckMessage(message)) {
717
803
  handlePlayAck(message);
718
804
  return;
@@ -757,6 +843,7 @@ function defineAgent(handlers) {
757
843
  return;
758
844
  if (message.type === "session_end") {
759
845
  endedSessionIds.add(message.sessionId);
846
+ clearPendingSpeakForSession(message.sessionId, "session_ended");
760
847
  clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
761
848
  clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
762
849
  inboundBySession.clear(message.sessionId);
@@ -883,12 +970,96 @@ function buildIdleEnv(sessionId) {
883
970
  ...process.env.IDLE_TIMEOUT_SEC ? { IDLE_TIMEOUT_SEC: process.env.IDLE_TIMEOUT_SEC } : {}
884
971
  };
885
972
  }
973
+ function speakControlAvailableFor(sessionId) {
974
+ return speakControlAvailableBySessionId.get(sessionId) ?? false;
975
+ }
976
+ function warnSpeakControlUnavailable(sessionId) {
977
+ if (speakControlWarnedSessionIds.has(sessionId))
978
+ return;
979
+ speakControlWarnedSessionIds.add(sessionId);
980
+ agentLog("warn", "speak control not available on this runner; speak results, cancel and barge-in control are disabled", sessionId);
981
+ }
886
982
  function speak(sessionId, text, options) {
887
- if (options?.interruptible === false) {
888
- sendParentMessage({ type: "speak", sessionId, text, interruptible: false });
983
+ const requestId = randomUUID();
984
+ const handle = (done2) => ({
985
+ requestId,
986
+ done: Promise.resolve(done2),
987
+ cancel: () => cancelSpeech(sessionId, { requestId })
988
+ });
989
+ if (!isVoicethereAgentChild()) {
990
+ return handle({
991
+ requestId,
992
+ status: "unsupported",
993
+ playedMs: 0,
994
+ errorCode: "no_parent"
995
+ });
996
+ }
997
+ if (!allowOutboundForSession(sessionId)) {
998
+ return handle({
999
+ requestId,
1000
+ status: "cancelled",
1001
+ playedMs: 0,
1002
+ errorCode: "session_ended"
1003
+ });
1004
+ }
1005
+ if (!speakControlAvailableFor(sessionId)) {
1006
+ sendParentMessage({
1007
+ type: "speak",
1008
+ sessionId,
1009
+ text,
1010
+ ...options?.interruptible === false ? { interruptible: false } : {}
1011
+ });
1012
+ warnSpeakControlUnavailable(sessionId);
1013
+ return handle({ requestId, status: "unsupported", playedMs: 0 });
1014
+ }
1015
+ const turnId = options?.turnId;
1016
+ const inReplyTo = options?.inReplyTo;
1017
+ const done = new Promise((resolve) => {
1018
+ const timer = setTimeout(() => {
1019
+ pendingSpeakResults.delete(requestId);
1020
+ resolve({
1021
+ requestId,
1022
+ ...turnId ? { turnId } : {},
1023
+ status: "failed",
1024
+ playedMs: 0,
1025
+ errorCode: "timeout"
1026
+ });
1027
+ }, SPEAK_RESULT_SAFETY_TIMEOUT_MS);
1028
+ pendingSpeakResults.set(requestId, {
1029
+ sessionId,
1030
+ ...turnId ? { turnId } : {},
1031
+ resolve,
1032
+ timer
1033
+ });
1034
+ });
1035
+ sendParentMessage({
1036
+ type: "speak",
1037
+ sessionId,
1038
+ text,
1039
+ requestId,
1040
+ ...options?.interruptible === false ? { interruptible: false } : {},
1041
+ ...turnId ? { turnId } : {},
1042
+ ...inReplyTo ? { inReplyTo } : {}
1043
+ });
1044
+ return {
1045
+ requestId,
1046
+ done,
1047
+ cancel: () => cancelSpeech(sessionId, { requestId })
1048
+ };
1049
+ }
1050
+ function cancelSpeech(sessionId, target) {
1051
+ if (!isVoicethereAgentChild() || !allowOutboundForSession(sessionId))
1052
+ return;
1053
+ if (!speakControlAvailableFor(sessionId)) {
1054
+ warnSpeakControlUnavailable(sessionId);
889
1055
  return;
890
1056
  }
891
- sendParentMessage({ type: "speak", sessionId, text });
1057
+ sendParentMessage({
1058
+ type: "speak_cancel",
1059
+ sessionId,
1060
+ ...target?.requestId ? { requestId: target.requestId } : {},
1061
+ ...target?.turnId ? { turnId: target.turnId } : {}
1062
+ });
892
1063
  }
893
1064
  var AGENT_LOG_MESSAGE_MAX_CHARS = 2048;
894
1065
  var AGENT_LOG_FIELDS_MAX_CHARS = 8192;
@@ -154,6 +154,18 @@ var SessionSerialQueue = class {
154
154
  var SESSION_START_INIT_DELAY_ENABLED_ENV = "AGENT_SESSION_START_INIT_DELAY_ENABLED";
155
155
  var SESSION_START_INIT_DELAY_MS_ENV = "AGENT_SESSION_START_INIT_DELAY_MS";
156
156
  var DEFAULT_SESSION_START_INIT_DELAY_MS = 500;
157
+ function isSpeakResultMessage(value) {
158
+ if (!value || typeof value !== "object")
159
+ return false;
160
+ const msg = value;
161
+ return msg.type === "speak_result" && typeof msg.requestId === "string";
162
+ }
163
+ function isBargeInControlAckMessage(value) {
164
+ if (!value || typeof value !== "object")
165
+ return false;
166
+ const msg = value;
167
+ return msg.type === "barge_in_control_ack" && typeof msg.requestId === "string";
168
+ }
157
169
  function isPlayAckMessage(value) {
158
170
  if (!value || typeof value !== "object")
159
171
  return false;
@@ -262,7 +274,7 @@ function isParentMessage(value) {
262
274
  if (!value || typeof value !== "object")
263
275
  return false;
264
276
  const msg = value;
265
- return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "webhook";
277
+ return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "speak_result" || msg.type === "barge_in_control_ack" || msg.type === "agent_interrupted" || msg.type === "webhook";
266
278
  }
267
279
  function isSessionScopedParentMessage(value) {
268
280
  return isParentMessage(value) && !isWebhookMessage(value);
@@ -281,7 +293,61 @@ var mixAvailableBySessionId = /* @__PURE__ */ new Map();
281
293
  var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
282
294
  var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
283
295
  var ttsPoseAvailableBySessionId = /* @__PURE__ */ new Map();
296
+ var speakControlAvailableBySessionId = /* @__PURE__ */ new Map();
297
+ var speakControlWarnedSessionIds = /* @__PURE__ */ new Set();
284
298
  var endedSessionIds = /* @__PURE__ */ new Set();
299
+ var SPEAK_RESULT_SAFETY_TIMEOUT_MS = 6e5;
300
+ var pendingSpeakResults = /* @__PURE__ */ new Map();
301
+ var pendingBargeInAcks = /* @__PURE__ */ new Map();
302
+ function handleSpeakResult(message) {
303
+ const pending = pendingSpeakResults.get(message.requestId);
304
+ if (!pending)
305
+ return;
306
+ clearTimeout(pending.timer);
307
+ pendingSpeakResults.delete(message.requestId);
308
+ pending.resolve({
309
+ requestId: message.requestId,
310
+ ...message.turnId !== void 0 ? { turnId: message.turnId } : {},
311
+ status: message.status,
312
+ playedMs: message.playedMs,
313
+ ...message.synthesizedMs !== void 0 ? { synthesizedMs: message.synthesizedMs } : {},
314
+ ...message.errorCode !== void 0 ? { errorCode: message.errorCode } : {}
315
+ });
316
+ }
317
+ function handleBargeInControlAck(message) {
318
+ const pending = pendingBargeInAcks.get(message.requestId);
319
+ if (!pending)
320
+ return;
321
+ clearTimeout(pending.timer);
322
+ pendingBargeInAcks.delete(message.requestId);
323
+ pending.resolve({
324
+ ok: message.ok,
325
+ ...message.reason ? { reason: message.reason } : {}
326
+ });
327
+ }
328
+ function clearPendingSpeakForSession(sessionId, errorCode) {
329
+ for (const [requestId, pending] of pendingSpeakResults) {
330
+ if (pending.sessionId !== sessionId)
331
+ continue;
332
+ clearTimeout(pending.timer);
333
+ pendingSpeakResults.delete(requestId);
334
+ pending.resolve({
335
+ requestId,
336
+ ...pending.turnId !== void 0 ? { turnId: pending.turnId } : {},
337
+ status: "cancelled",
338
+ playedMs: 0,
339
+ errorCode
340
+ });
341
+ }
342
+ for (const [requestId, pending] of pendingBargeInAcks) {
343
+ if (pending.sessionId !== sessionId)
344
+ continue;
345
+ clearTimeout(pending.timer);
346
+ pendingBargeInAcks.delete(requestId);
347
+ pending.resolve({ ok: false, reason: "session_ended" });
348
+ }
349
+ speakControlWarnedSessionIds.delete(sessionId);
350
+ }
285
351
  var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
286
352
  var pendingRecordingAcks = /* @__PURE__ */ new Map();
287
353
  var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
@@ -461,6 +527,9 @@ function clearPendingConversationHistoryAcksForSession(sessionId, reason) {
461
527
  pending.resolve({ ok: false, reason, requestId });
462
528
  }
463
529
  }
530
+ function isVoicethereAgentChild() {
531
+ return typeof process.send === "function" && process.connected !== false && typeof process.env.__CHILD_BUNDLE_PATH__ === "string" && process.env.__CHILD_BUNDLE_PATH__.trim().length > 0;
532
+ }
464
533
  var sessionExecutionContext = new AsyncLocalStorage();
465
534
  var agentLogSessionContext = new AsyncLocalStorage();
466
535
  var inboundQueueAuthority = null;
@@ -611,6 +680,7 @@ async function handleParentMessage(message, handlers) {
611
680
  mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
612
681
  voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
613
682
  ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
683
+ speakControlAvailableBySessionId.set(message.sessionId, message.speakControlAvailable ?? false);
614
684
  if (message.voiceLanguageSwitch) {
615
685
  voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
616
686
  } else {
@@ -627,7 +697,8 @@ async function handleParentMessage(message, handlers) {
627
697
  conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
628
698
  mixAvailable: message.mixAvailable ?? false,
629
699
  voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
630
- ttsPoseAvailable: message.ttsPoseAvailable ?? false
700
+ ttsPoseAvailable: message.ttsPoseAvailable ?? false,
701
+ speakControlAvailable: message.speakControlAvailable ?? false
631
702
  });
632
703
  sendParentMessage({
633
704
  type: "session_start_ack",
@@ -664,6 +735,11 @@ async function handleParentMessage(message, handlers) {
664
735
  }
665
736
  }
666
737
  break;
738
+ case "agent_interrupted": {
739
+ const { type: _type, ...interrupted } = message;
740
+ await handlers.onAgentInterrupted?.(interrupted);
741
+ break;
742
+ }
667
743
  case "data_channel_message":
668
744
  await handlers.onDataChannelMessage?.({
669
745
  sessionId: message.sessionId,
@@ -693,6 +769,8 @@ async function handleParentMessage(message, handlers) {
693
769
  voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
694
770
  lastVoiceLanguageBySessionId.delete(message.sessionId);
695
771
  ttsPoseAvailableBySessionId.delete(message.sessionId);
772
+ speakControlAvailableBySessionId.delete(message.sessionId);
773
+ clearPendingSpeakForSession(message.sessionId, "session_ended");
696
774
  await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
697
775
  sessionId: message.sessionId
698
776
  });
@@ -716,6 +794,14 @@ function defineAgent(handlers) {
716
794
  handleConversationHistoryControlAck(message);
717
795
  return;
718
796
  }
797
+ if (isSpeakResultMessage(message)) {
798
+ handleSpeakResult(message);
799
+ return;
800
+ }
801
+ if (isBargeInControlAckMessage(message)) {
802
+ handleBargeInControlAck(message);
803
+ return;
804
+ }
719
805
  if (isPlayAckMessage(message)) {
720
806
  handlePlayAck(message);
721
807
  return;
@@ -760,6 +846,7 @@ function defineAgent(handlers) {
760
846
  return;
761
847
  if (message.type === "session_end") {
762
848
  endedSessionIds.add(message.sessionId);
849
+ clearPendingSpeakForSession(message.sessionId, "session_ended");
763
850
  clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
764
851
  clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
765
852
  inboundBySession.clear(message.sessionId);
@@ -886,12 +973,96 @@ function buildIdleEnv(sessionId) {
886
973
  ...process.env.IDLE_TIMEOUT_SEC ? { IDLE_TIMEOUT_SEC: process.env.IDLE_TIMEOUT_SEC } : {}
887
974
  };
888
975
  }
976
+ function speakControlAvailableFor(sessionId) {
977
+ return speakControlAvailableBySessionId.get(sessionId) ?? false;
978
+ }
979
+ function warnSpeakControlUnavailable(sessionId) {
980
+ if (speakControlWarnedSessionIds.has(sessionId))
981
+ return;
982
+ speakControlWarnedSessionIds.add(sessionId);
983
+ agentLog("warn", "speak control not available on this runner; speak results, cancel and barge-in control are disabled", sessionId);
984
+ }
889
985
  function speak(sessionId, text, options) {
890
- if (options?.interruptible === false) {
891
- sendParentMessage({ type: "speak", sessionId, text, interruptible: false });
986
+ const requestId = randomUUID();
987
+ const handle = (done2) => ({
988
+ requestId,
989
+ done: Promise.resolve(done2),
990
+ cancel: () => cancelSpeech(sessionId, { requestId })
991
+ });
992
+ if (!isVoicethereAgentChild()) {
993
+ return handle({
994
+ requestId,
995
+ status: "unsupported",
996
+ playedMs: 0,
997
+ errorCode: "no_parent"
998
+ });
999
+ }
1000
+ if (!allowOutboundForSession(sessionId)) {
1001
+ return handle({
1002
+ requestId,
1003
+ status: "cancelled",
1004
+ playedMs: 0,
1005
+ errorCode: "session_ended"
1006
+ });
1007
+ }
1008
+ if (!speakControlAvailableFor(sessionId)) {
1009
+ sendParentMessage({
1010
+ type: "speak",
1011
+ sessionId,
1012
+ text,
1013
+ ...options?.interruptible === false ? { interruptible: false } : {}
1014
+ });
1015
+ warnSpeakControlUnavailable(sessionId);
1016
+ return handle({ requestId, status: "unsupported", playedMs: 0 });
1017
+ }
1018
+ const turnId = options?.turnId;
1019
+ const inReplyTo = options?.inReplyTo;
1020
+ const done = new Promise((resolve) => {
1021
+ const timer = setTimeout(() => {
1022
+ pendingSpeakResults.delete(requestId);
1023
+ resolve({
1024
+ requestId,
1025
+ ...turnId ? { turnId } : {},
1026
+ status: "failed",
1027
+ playedMs: 0,
1028
+ errorCode: "timeout"
1029
+ });
1030
+ }, SPEAK_RESULT_SAFETY_TIMEOUT_MS);
1031
+ pendingSpeakResults.set(requestId, {
1032
+ sessionId,
1033
+ ...turnId ? { turnId } : {},
1034
+ resolve,
1035
+ timer
1036
+ });
1037
+ });
1038
+ sendParentMessage({
1039
+ type: "speak",
1040
+ sessionId,
1041
+ text,
1042
+ requestId,
1043
+ ...options?.interruptible === false ? { interruptible: false } : {},
1044
+ ...turnId ? { turnId } : {},
1045
+ ...inReplyTo ? { inReplyTo } : {}
1046
+ });
1047
+ return {
1048
+ requestId,
1049
+ done,
1050
+ cancel: () => cancelSpeech(sessionId, { requestId })
1051
+ };
1052
+ }
1053
+ function cancelSpeech(sessionId, target) {
1054
+ if (!isVoicethereAgentChild() || !allowOutboundForSession(sessionId))
1055
+ return;
1056
+ if (!speakControlAvailableFor(sessionId)) {
1057
+ warnSpeakControlUnavailable(sessionId);
892
1058
  return;
893
1059
  }
894
- sendParentMessage({ type: "speak", sessionId, text });
1060
+ sendParentMessage({
1061
+ type: "speak_cancel",
1062
+ sessionId,
1063
+ ...target?.requestId ? { requestId: target.requestId } : {},
1064
+ ...target?.turnId ? { turnId: target.turnId } : {}
1065
+ });
895
1066
  }
896
1067
  function sendToClient(sessionId, payload) {
897
1068
  sendParentMessage({ type: "send_to_client", sessionId, payload });
@@ -10906,6 +10906,18 @@ var SessionSerialQueue = class {
10906
10906
  var SESSION_START_INIT_DELAY_ENABLED_ENV = "AGENT_SESSION_START_INIT_DELAY_ENABLED";
10907
10907
  var SESSION_START_INIT_DELAY_MS_ENV = "AGENT_SESSION_START_INIT_DELAY_MS";
10908
10908
  var DEFAULT_SESSION_START_INIT_DELAY_MS = 500;
10909
+ function isSpeakResultMessage(value) {
10910
+ if (!value || typeof value !== "object")
10911
+ return false;
10912
+ const msg = value;
10913
+ return msg.type === "speak_result" && typeof msg.requestId === "string";
10914
+ }
10915
+ function isBargeInControlAckMessage(value) {
10916
+ if (!value || typeof value !== "object")
10917
+ return false;
10918
+ const msg = value;
10919
+ return msg.type === "barge_in_control_ack" && typeof msg.requestId === "string";
10920
+ }
10909
10921
  function isPlayAckMessage(value) {
10910
10922
  if (!value || typeof value !== "object")
10911
10923
  return false;
@@ -11014,7 +11026,7 @@ function isParentMessage(value) {
11014
11026
  if (!value || typeof value !== "object")
11015
11027
  return false;
11016
11028
  const msg = value;
11017
- return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "webhook";
11029
+ return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "speak_result" || msg.type === "barge_in_control_ack" || msg.type === "agent_interrupted" || msg.type === "webhook";
11018
11030
  }
11019
11031
  function isSessionScopedParentMessage(value) {
11020
11032
  return isParentMessage(value) && !isWebhookMessage(value);
@@ -11033,7 +11045,60 @@ var mixAvailableBySessionId = /* @__PURE__ */ new Map();
11033
11045
  var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
11034
11046
  var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
11035
11047
  var ttsPoseAvailableBySessionId = /* @__PURE__ */ new Map();
11048
+ var speakControlAvailableBySessionId = /* @__PURE__ */ new Map();
11049
+ var speakControlWarnedSessionIds = /* @__PURE__ */ new Set();
11036
11050
  var endedSessionIds = /* @__PURE__ */ new Set();
11051
+ var pendingSpeakResults = /* @__PURE__ */ new Map();
11052
+ var pendingBargeInAcks = /* @__PURE__ */ new Map();
11053
+ function handleSpeakResult(message) {
11054
+ const pending = pendingSpeakResults.get(message.requestId);
11055
+ if (!pending)
11056
+ return;
11057
+ clearTimeout(pending.timer);
11058
+ pendingSpeakResults.delete(message.requestId);
11059
+ pending.resolve({
11060
+ requestId: message.requestId,
11061
+ ...message.turnId !== void 0 ? { turnId: message.turnId } : {},
11062
+ status: message.status,
11063
+ playedMs: message.playedMs,
11064
+ ...message.synthesizedMs !== void 0 ? { synthesizedMs: message.synthesizedMs } : {},
11065
+ ...message.errorCode !== void 0 ? { errorCode: message.errorCode } : {}
11066
+ });
11067
+ }
11068
+ function handleBargeInControlAck(message) {
11069
+ const pending = pendingBargeInAcks.get(message.requestId);
11070
+ if (!pending)
11071
+ return;
11072
+ clearTimeout(pending.timer);
11073
+ pendingBargeInAcks.delete(message.requestId);
11074
+ pending.resolve({
11075
+ ok: message.ok,
11076
+ ...message.reason ? { reason: message.reason } : {}
11077
+ });
11078
+ }
11079
+ function clearPendingSpeakForSession(sessionId, errorCode) {
11080
+ for (const [requestId, pending] of pendingSpeakResults) {
11081
+ if (pending.sessionId !== sessionId)
11082
+ continue;
11083
+ clearTimeout(pending.timer);
11084
+ pendingSpeakResults.delete(requestId);
11085
+ pending.resolve({
11086
+ requestId,
11087
+ ...pending.turnId !== void 0 ? { turnId: pending.turnId } : {},
11088
+ status: "cancelled",
11089
+ playedMs: 0,
11090
+ errorCode
11091
+ });
11092
+ }
11093
+ for (const [requestId, pending] of pendingBargeInAcks) {
11094
+ if (pending.sessionId !== sessionId)
11095
+ continue;
11096
+ clearTimeout(pending.timer);
11097
+ pendingBargeInAcks.delete(requestId);
11098
+ pending.resolve({ ok: false, reason: "session_ended" });
11099
+ }
11100
+ speakControlWarnedSessionIds.delete(sessionId);
11101
+ }
11037
11102
  var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
11038
11103
  var pendingRecordingAcks = /* @__PURE__ */ new Map();
11039
11104
  var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
@@ -11363,6 +11428,7 @@ async function handleParentMessage(message, handlers) {
11363
11428
  mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
11364
11429
  voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
11365
11430
  ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
11431
+ speakControlAvailableBySessionId.set(message.sessionId, message.speakControlAvailable ?? false);
11366
11432
  if (message.voiceLanguageSwitch) {
11367
11433
  voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
11368
11434
  } else {
@@ -11379,7 +11445,8 @@ async function handleParentMessage(message, handlers) {
11379
11445
  conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
11380
11446
  mixAvailable: message.mixAvailable ?? false,
11381
11447
  voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
11382
- ttsPoseAvailable: message.ttsPoseAvailable ?? false
11448
+ ttsPoseAvailable: message.ttsPoseAvailable ?? false,
11449
+ speakControlAvailable: message.speakControlAvailable ?? false
11383
11450
  });
11384
11451
  sendParentMessage({
11385
11452
  type: "session_start_ack",
@@ -11416,6 +11483,11 @@ async function handleParentMessage(message, handlers) {
11416
11483
  }
11417
11484
  }
11418
11485
  break;
11486
+ case "agent_interrupted": {
11487
+ const { type: _type, ...interrupted } = message;
11488
+ await handlers.onAgentInterrupted?.(interrupted);
11489
+ break;
11490
+ }
11419
11491
  case "data_channel_message":
11420
11492
  await handlers.onDataChannelMessage?.({
11421
11493
  sessionId: message.sessionId,
@@ -11445,6 +11517,8 @@ async function handleParentMessage(message, handlers) {
11445
11517
  voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
11446
11518
  lastVoiceLanguageBySessionId.delete(message.sessionId);
11447
11519
  ttsPoseAvailableBySessionId.delete(message.sessionId);
11520
+ speakControlAvailableBySessionId.delete(message.sessionId);
11521
+ clearPendingSpeakForSession(message.sessionId, "session_ended");
11448
11522
  await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
11449
11523
  sessionId: message.sessionId
11450
11524
  });
@@ -11468,6 +11542,14 @@ function defineAgent(handlers) {
11468
11542
  handleConversationHistoryControlAck(message);
11469
11543
  return;
11470
11544
  }
11545
+ if (isSpeakResultMessage(message)) {
11546
+ handleSpeakResult(message);
11547
+ return;
11548
+ }
11549
+ if (isBargeInControlAckMessage(message)) {
11550
+ handleBargeInControlAck(message);
11551
+ return;
11552
+ }
11471
11553
  if (isPlayAckMessage(message)) {
11472
11554
  handlePlayAck(message);
11473
11555
  return;
@@ -11512,6 +11594,7 @@ function defineAgent(handlers) {
11512
11594
  return;
11513
11595
  if (message.type === "session_end") {
11514
11596
  endedSessionIds.add(message.sessionId);
11597
+ clearPendingSpeakForSession(message.sessionId, "session_ended");
11515
11598
  clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
11516
11599
  clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
11517
11600
  inboundBySession.clear(message.sessionId);