@voicethere/agent 0.18.2 → 0.20.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 (37) hide show
  1. package/README.md +32 -0
  2. package/dist/agent.js +89 -3
  3. package/dist/index.d.ts +2 -2
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/llm/agent/create-llm-agent.d.ts.map +1 -1
  8. package/dist/llm/agent/create-llm-agent.js +44 -25
  9. package/dist/llm/agent/create-llm-agent.js.map +1 -1
  10. package/dist/protocol.d.ts +58 -2
  11. package/dist/protocol.d.ts.map +1 -1
  12. package/dist/protocol.js.map +1 -1
  13. package/dist/runtime.d.ts +48 -1
  14. package/dist/runtime.d.ts.map +1 -1
  15. package/dist/runtime.js +178 -0
  16. package/dist/runtime.js.map +1 -1
  17. package/dist/templates/echo/agent.js +85 -3
  18. package/dist/templates/echo-dc/agent.js +85 -3
  19. package/dist/templates/game-sync/agent.js +85 -3
  20. package/dist/templates/language-switch/agent.js +85 -3
  21. package/dist/templates/llm-agent/agent.js +122 -29
  22. package/dist/templates/llm-mcp-service/agent.js +122 -29
  23. package/dist/templates/positional-tts/agent.js +85 -3
  24. package/dist/templates/recording-consent/agent.js +85 -3
  25. package/dist/templates/spatial-showcase/agent.js +85 -3
  26. package/dist/templates/voice-showcase/agent.js +85 -3
  27. package/dist/templates/voice-starter/agent.js +85 -3
  28. package/dist/templates/webhooks/agent.js +85 -3
  29. package/dist/templates/webhooks-redis/agent.js +85 -3
  30. package/dist/templates/world-sync/agent.js +85 -3
  31. package/dist/templates/world-sync-binary/agent.js +85 -3
  32. package/eject/llm/loop.ts +63 -26
  33. package/package.json +1 -1
  34. package/templates/llm-agent/README.md +5 -1
  35. package/templates/llm-agent/voicethere.llm.json +1 -1
  36. package/templates/llm-mcp-service/README.md +1 -1
  37. package/templates/llm-mcp-service/voicethere.llm.json +1 -1
@@ -30806,6 +30806,12 @@ function isBargeInControlAckMessage(value) {
30806
30806
  const msg = value;
30807
30807
  return msg.type === "barge_in_control_ack" && typeof msg.requestId === "string";
30808
30808
  }
30809
+ function isSessionTimeLimitControlAckMessage(value) {
30810
+ if (!value || typeof value !== "object")
30811
+ return false;
30812
+ const msg = value;
30813
+ return msg.type === "session_time_limit_control_ack" && typeof msg.requestId === "string";
30814
+ }
30809
30815
  function isPlayAckMessage(value) {
30810
30816
  if (!value || typeof value !== "object")
30811
30817
  return false;
@@ -30914,7 +30920,7 @@ function isParentMessage(value) {
30914
30920
  if (!value || typeof value !== "object")
30915
30921
  return false;
30916
30922
  const msg = value;
30917
- 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";
30923
+ 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 === "session_time_limit" || msg.type === "session_time_limit_control_ack" || 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";
30918
30924
  }
30919
30925
  function isSessionScopedParentMessage(value) {
30920
30926
  return isParentMessage(value) && !isWebhookMessage(value);
@@ -30934,12 +30940,30 @@ var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
30934
30940
  var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
30935
30941
  var ttsPoseAvailableBySessionId = /* @__PURE__ */ new Map();
30936
30942
  var speakControlAvailableBySessionId = /* @__PURE__ */ new Map();
30943
+ var sessionTimeLimitAvailableBySessionId = /* @__PURE__ */ new Map();
30944
+ var sessionTimeLimitWarnedSessionIds = /* @__PURE__ */ new Set();
30937
30945
  var speakControlWarnedSessionIds = /* @__PURE__ */ new Set();
30938
30946
  var endedSessionIds = /* @__PURE__ */ new Set();
30939
30947
  var BARGE_IN_CONTROL_ACK_TIMEOUT_MS = 5e3;
30940
30948
  var SPEAK_RESULT_SAFETY_TIMEOUT_MS = 6e5;
30941
30949
  var pendingSpeakResults = /* @__PURE__ */ new Map();
30942
30950
  var pendingBargeInAcks = /* @__PURE__ */ new Map();
30951
+ var pendingSessionTimeLimitAcks = /* @__PURE__ */ new Map();
30952
+ function handleSessionTimeLimitControlAck(message) {
30953
+ const pending = pendingSessionTimeLimitAcks.get(message.requestId);
30954
+ if (!pending)
30955
+ return;
30956
+ clearTimeout(pending.timer);
30957
+ pendingSessionTimeLimitAcks.delete(message.requestId);
30958
+ if (message.ok && message.state) {
30959
+ pending.resolve({ ok: true, state: message.state });
30960
+ return;
30961
+ }
30962
+ pending.resolve({
30963
+ ok: false,
30964
+ error: message.error ?? "unknown_session"
30965
+ });
30966
+ }
30943
30967
  function handleSpeakResult(message) {
30944
30968
  const pending = pendingSpeakResults.get(message.requestId);
30945
30969
  if (!pending)
@@ -30987,7 +31011,15 @@ function clearPendingSpeakForSession(sessionId, errorCode) {
30987
31011
  pendingBargeInAcks.delete(requestId);
30988
31012
  pending.resolve({ ok: false, reason: "session_ended" });
30989
31013
  }
31014
+ for (const [requestId, pending] of pendingSessionTimeLimitAcks) {
31015
+ if (pending.sessionId !== sessionId)
31016
+ continue;
31017
+ clearTimeout(pending.timer);
31018
+ pendingSessionTimeLimitAcks.delete(requestId);
31019
+ pending.resolve({ ok: false, error: "session_closing" });
31020
+ }
30990
31021
  speakControlWarnedSessionIds.delete(sessionId);
31022
+ sessionTimeLimitWarnedSessionIds.delete(sessionId);
30991
31023
  }
30992
31024
  var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
30993
31025
  var pendingRecordingAcks = /* @__PURE__ */ new Map();
@@ -31322,6 +31354,7 @@ async function handleParentMessage(message, handlers) {
31322
31354
  voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
31323
31355
  ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
31324
31356
  speakControlAvailableBySessionId.set(message.sessionId, message.speakControlAvailable ?? false);
31357
+ sessionTimeLimitAvailableBySessionId.set(message.sessionId, message.sessionTimeLimitAvailable ?? false);
31325
31358
  if (message.voiceLanguageSwitch) {
31326
31359
  voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
31327
31360
  } else {
@@ -31339,7 +31372,8 @@ async function handleParentMessage(message, handlers) {
31339
31372
  mixAvailable: message.mixAvailable ?? false,
31340
31373
  voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
31341
31374
  ttsPoseAvailable: message.ttsPoseAvailable ?? false,
31342
- speakControlAvailable: message.speakControlAvailable ?? false
31375
+ speakControlAvailable: message.speakControlAvailable ?? false,
31376
+ sessionTimeLimitAvailable: message.sessionTimeLimitAvailable ?? false
31343
31377
  });
31344
31378
  sendParentMessage({
31345
31379
  type: "session_start_ack",
@@ -31349,10 +31383,13 @@ async function handleParentMessage(message, handlers) {
31349
31383
  case "speech_event":
31350
31384
  await handlers.onSpeechEvent?.({ sessionId: message.sessionId }, message.event);
31351
31385
  if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
31386
+ const rawUtteranceId = message.event.utteranceId;
31387
+ const utteranceId = typeof rawUtteranceId === "string" ? rawUtteranceId.trim() : "";
31352
31388
  await handlers.onUserSpeechFinal?.({
31353
31389
  sessionId: message.sessionId,
31354
31390
  text: message.event.text.trim(),
31355
- ...message.event.replay === true ? { replay: true } : {}
31391
+ ...message.event.replay === true ? { replay: true } : {},
31392
+ ...utteranceId ? { utteranceId } : {}
31356
31393
  });
31357
31394
  }
31358
31395
  if (message.event.type === "user_language") {
@@ -31411,6 +31448,7 @@ async function handleParentMessage(message, handlers) {
31411
31448
  lastVoiceLanguageBySessionId.delete(message.sessionId);
31412
31449
  ttsPoseAvailableBySessionId.delete(message.sessionId);
31413
31450
  speakControlAvailableBySessionId.delete(message.sessionId);
31451
+ sessionTimeLimitAvailableBySessionId.delete(message.sessionId);
31414
31452
  clearPendingSpeakForSession(message.sessionId, "session_ended");
31415
31453
  await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
31416
31454
  sessionId: message.sessionId
@@ -31419,6 +31457,9 @@ async function handleParentMessage(message, handlers) {
31419
31457
  case "idle_timeout":
31420
31458
  await runIdleTimeoutHook(handlers, message);
31421
31459
  break;
31460
+ case "session_time_limit":
31461
+ await runSessionTimeLimitHook(handlers, message);
31462
+ break;
31422
31463
  }
31423
31464
  }
31424
31465
  function defineAgent(handlers) {
@@ -31443,6 +31484,10 @@ function defineAgent(handlers) {
31443
31484
  handleBargeInControlAck(message);
31444
31485
  return;
31445
31486
  }
31487
+ if (isSessionTimeLimitControlAckMessage(message)) {
31488
+ handleSessionTimeLimitControlAck(message);
31489
+ return;
31490
+ }
31446
31491
  if (isPlayAckMessage(message)) {
31447
31492
  handlePlayAck(message);
31448
31493
  return;
@@ -31606,6 +31651,43 @@ async function runIdleTimeoutHook(handlers, message) {
31606
31651
  });
31607
31652
  agentLog("info", error2 ? `idle_timeout_done ipc sent (onIdleTimeout error: ${error2})` : "idle_timeout_done ipc sent (onIdleTimeout completed)", message.sessionId);
31608
31653
  }
31654
+ async function runSessionTimeLimitHook(handlers, message) {
31655
+ const onSessionTimeLimit = handlers.onSessionTimeLimit;
31656
+ agentLog("info", `session_time_limit ipc received (maxGraceMs=${message.maxGraceMs}, onSessionTimeLimit=${typeof onSessionTimeLimit === "function"})`, message.sessionId);
31657
+ if (!onSessionTimeLimit) {
31658
+ sendParentMessage({
31659
+ type: "session_time_limit_done",
31660
+ sessionId: message.sessionId
31661
+ });
31662
+ return;
31663
+ }
31664
+ const env = peerEnvBySessionId.get(message.sessionId) ?? {
31665
+ SESSION_ID: message.sessionId,
31666
+ ...process.env.PROJECT_ID ? { PROJECT_ID: process.env.PROJECT_ID } : {},
31667
+ ...process.env.BUILD_ID ? { BUILD_ID: process.env.BUILD_ID } : {}
31668
+ };
31669
+ const ctx = {
31670
+ sessionId: message.sessionId,
31671
+ projectId: env.PROJECT_ID,
31672
+ buildId: env.BUILD_ID,
31673
+ env,
31674
+ limitMinutes: message.limitMinutes,
31675
+ elapsedMs: message.elapsedMs,
31676
+ graceMs: message.maxGraceMs
31677
+ };
31678
+ let error2;
31679
+ try {
31680
+ await onSessionTimeLimit(ctx);
31681
+ } catch (hookError) {
31682
+ error2 = hookError instanceof Error ? hookError.message : String(hookError);
31683
+ agentLog("error", `onSessionTimeLimit failed: ${error2}`, message.sessionId);
31684
+ }
31685
+ sendParentMessage({
31686
+ type: "session_time_limit_done",
31687
+ sessionId: message.sessionId,
31688
+ ...error2 !== void 0 ? { error: error2 } : {}
31689
+ });
31690
+ }
31609
31691
  function buildIdleEnv(sessionId) {
31610
31692
  return {
31611
31693
  SESSION_ID: sessionId,
@@ -35064,22 +35146,28 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
35064
35146
  s.announcing = Math.max(0, s.announcing - 1);
35065
35147
  });
35066
35148
  }
35149
+ function replyOpts(t) {
35150
+ return t.inReplyTo ? { turnId: t.turnId, inReplyTo: t.inReplyTo } : { turnId: t.turnId };
35151
+ }
35152
+ function sendReplyDelta(s, t, text) {
35153
+ sendToClient(s.sessionId, t.source === "chat" ? { type: "assistant_delta", turnId: t.turnId, text } : {
35154
+ type: "assistant_delta",
35155
+ turnId: t.turnId,
35156
+ text,
35157
+ spoken: true,
35158
+ ...t.inReplyTo ? { inReplyTo: t.inReplyTo } : {}
35159
+ });
35160
+ }
35067
35161
  function deliver(s, t, text, interruptible = true) {
35068
35162
  flushSpeech(s, t);
35069
35163
  t.spoken.push(text);
35070
35164
  markReplyStarted(s, t);
35071
- if (t.source === "chat") {
35072
- sendToClient(s.sessionId, {
35073
- type: "assistant_delta",
35074
- turnId: t.turnId,
35075
- text
35076
- });
35077
- }
35165
+ sendReplyDelta(s, t, text);
35078
35166
  if (t.source === "voice" || speakChat) {
35079
35167
  if (interruptible) {
35080
- speak(s.sessionId, text, { turnId: t.turnId });
35168
+ speak(s.sessionId, text, replyOpts(t));
35081
35169
  } else {
35082
- const h = announce(s.sessionId, text, { turnId: t.turnId });
35170
+ const h = announce(s.sessionId, text, replyOpts(t));
35083
35171
  trackAnnouncement(s, h.done);
35084
35172
  }
35085
35173
  }
@@ -35176,7 +35264,7 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
35176
35264
  t.pendingChars = 0;
35177
35265
  if (t.controller.signal.aborted || s.ended)
35178
35266
  return;
35179
- speak(s.sessionId, text, { turnId: t.turnId });
35267
+ speak(s.sessionId, text, replyOpts(t));
35180
35268
  }
35181
35269
  function deliverReply(s, t, text) {
35182
35270
  if (coalesceMs <= 0) {
@@ -35185,13 +35273,7 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
35185
35273
  }
35186
35274
  markReplyStarted(s, t);
35187
35275
  t.spoken.push(text);
35188
- if (t.source === "chat") {
35189
- sendToClient(s.sessionId, {
35190
- type: "assistant_delta",
35191
- turnId: t.turnId,
35192
- text
35193
- });
35194
- }
35276
+ sendReplyDelta(s, t, text);
35195
35277
  if (t.source !== "voice" && !speakChat)
35196
35278
  return;
35197
35279
  t.pending.push(text);
@@ -35711,6 +35793,16 @@ ${t.inputText}` : t.inputText, {
35711
35793
  });
35712
35794
  sendToClient(s.sessionId, { type: "chat_reply", text });
35713
35795
  }
35796
+ if (t.source === "voice" && t.spoken.length > 0) {
35797
+ sendToClient(s.sessionId, {
35798
+ type: "assistant_done",
35799
+ turnId: t.turnId,
35800
+ text: t.spoken.join(" "),
35801
+ spoken: true,
35802
+ status: summary.status,
35803
+ ...t.inReplyTo ? { inReplyTo: t.inReplyTo } : {}
35804
+ });
35805
+ }
35714
35806
  setAgentState(s.sessionId, "listening");
35715
35807
  if (hooks2.onTurnEnd) {
35716
35808
  await callHook("onTurnEnd", s.sessionId, () => hooks2.onTurnEnd(summary, ctx));
@@ -35726,7 +35818,7 @@ ${t.inputText}` : t.inputText, {
35726
35818
  thinkingCharsStripped: t.thinkingCharsStripped
35727
35819
  }, s.sessionId);
35728
35820
  }
35729
- function startTurn(s, text, source) {
35821
+ function startTurn(s, text, source, utteranceId) {
35730
35822
  const turnId = randomUUID3();
35731
35823
  const controller = new AbortController();
35732
35824
  const t = {
@@ -35736,6 +35828,7 @@ ${t.inputText}` : t.inputText, {
35736
35828
  controller,
35737
35829
  startedAt: now(),
35738
35830
  spoken: [],
35831
+ ...source === "voice" && utteranceId ? { inReplyTo: utteranceId } : {},
35739
35832
  toolCalls: [],
35740
35833
  llmCalls: 0,
35741
35834
  roundOpen: false,
@@ -35786,12 +35879,12 @@ ${t.inputText}` : t.inputText, {
35786
35879
  const q = s.queued;
35787
35880
  if (q && !s.ended && !s.active) {
35788
35881
  s.queued = void 0;
35789
- startTurn(s, q.text, q.source);
35882
+ startTurn(s, q.text, q.source, q.utteranceId);
35790
35883
  }
35791
35884
  }
35792
35885
  })();
35793
35886
  }
35794
- async function handleInput(s, text, source) {
35887
+ async function handleInput(s, text, source, utteranceId) {
35795
35888
  if (s.ended)
35796
35889
  return;
35797
35890
  if (source === "voice" && config2.speechDuringNonInterruptible === "discard" && s.announcing > 0) {
@@ -35821,7 +35914,7 @@ ${t.inputText}` : t.inputText, {
35821
35914
  const active = s.active;
35822
35915
  if (active) {
35823
35916
  if (policy === "finish") {
35824
- s.queued = { text, source };
35917
+ s.queued = { text, source, utteranceId };
35825
35918
  return;
35826
35919
  }
35827
35920
  cancelSpeech(s.sessionId, { turnId: active.turnId });
@@ -35841,14 +35934,14 @@ ${t.inputText}` : t.inputText, {
35841
35934
  if (s.ended)
35842
35935
  return;
35843
35936
  }
35844
- startTurn(s, text, source);
35937
+ startTurn(s, text, source, utteranceId);
35845
35938
  }
35846
- function submit(sessionId, text, source) {
35939
+ function submit(sessionId, text, source, utteranceId) {
35847
35940
  const s = sessions.get(sessionId);
35848
35941
  const trimmed = text.trim();
35849
35942
  if (!s || !trimmed)
35850
35943
  return;
35851
- s.chain = s.chain.then(() => handleInput(s, trimmed, source)).catch((err) => {
35944
+ s.chain = s.chain.then(() => handleInput(s, trimmed, source, utteranceId)).catch((err) => {
35852
35945
  const message = err instanceof Error ? err.message : String(err);
35853
35946
  agentLog("error", `llm input failed: ${message}`, sessionId);
35854
35947
  });
@@ -35894,7 +35987,7 @@ ${t.inputText}` : t.inputText, {
35894
35987
  }
35895
35988
  },
35896
35989
  onUserSpeechFinal(ctx) {
35897
- submit(ctx.sessionId, ctx.text, "voice");
35990
+ submit(ctx.sessionId, ctx.text, "voice", ctx.utteranceId);
35898
35991
  },
35899
35992
  onDataChannelMessage(ctx) {
35900
35993
  const text = parseChatText(ctx.message);
@@ -59423,7 +59516,7 @@ var voicethere_llm_default = {
59423
59516
  history: { turns: 20, interruptionNote: true },
59424
59517
  fillers: {
59425
59518
  thinking: {
59426
- phrases: ["Hmm.", "Okay.", "Mm-hm.", "Right."],
59519
+ phrases: ["Okay.", "Right.", "Sure.", "Let me see."],
59427
59520
  afterMs: 1500
59428
59521
  },
59429
59522
  toolCall: {
@@ -30806,6 +30806,12 @@ function isBargeInControlAckMessage(value) {
30806
30806
  const msg = value;
30807
30807
  return msg.type === "barge_in_control_ack" && typeof msg.requestId === "string";
30808
30808
  }
30809
+ function isSessionTimeLimitControlAckMessage(value) {
30810
+ if (!value || typeof value !== "object")
30811
+ return false;
30812
+ const msg = value;
30813
+ return msg.type === "session_time_limit_control_ack" && typeof msg.requestId === "string";
30814
+ }
30809
30815
  function isPlayAckMessage(value) {
30810
30816
  if (!value || typeof value !== "object")
30811
30817
  return false;
@@ -30914,7 +30920,7 @@ function isParentMessage(value) {
30914
30920
  if (!value || typeof value !== "object")
30915
30921
  return false;
30916
30922
  const msg = value;
30917
- 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";
30923
+ 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 === "session_time_limit" || msg.type === "session_time_limit_control_ack" || 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";
30918
30924
  }
30919
30925
  function isSessionScopedParentMessage(value) {
30920
30926
  return isParentMessage(value) && !isWebhookMessage(value);
@@ -30934,12 +30940,30 @@ var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
30934
30940
  var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
30935
30941
  var ttsPoseAvailableBySessionId = /* @__PURE__ */ new Map();
30936
30942
  var speakControlAvailableBySessionId = /* @__PURE__ */ new Map();
30943
+ var sessionTimeLimitAvailableBySessionId = /* @__PURE__ */ new Map();
30944
+ var sessionTimeLimitWarnedSessionIds = /* @__PURE__ */ new Set();
30937
30945
  var speakControlWarnedSessionIds = /* @__PURE__ */ new Set();
30938
30946
  var endedSessionIds = /* @__PURE__ */ new Set();
30939
30947
  var BARGE_IN_CONTROL_ACK_TIMEOUT_MS = 5e3;
30940
30948
  var SPEAK_RESULT_SAFETY_TIMEOUT_MS = 6e5;
30941
30949
  var pendingSpeakResults = /* @__PURE__ */ new Map();
30942
30950
  var pendingBargeInAcks = /* @__PURE__ */ new Map();
30951
+ var pendingSessionTimeLimitAcks = /* @__PURE__ */ new Map();
30952
+ function handleSessionTimeLimitControlAck(message) {
30953
+ const pending = pendingSessionTimeLimitAcks.get(message.requestId);
30954
+ if (!pending)
30955
+ return;
30956
+ clearTimeout(pending.timer);
30957
+ pendingSessionTimeLimitAcks.delete(message.requestId);
30958
+ if (message.ok && message.state) {
30959
+ pending.resolve({ ok: true, state: message.state });
30960
+ return;
30961
+ }
30962
+ pending.resolve({
30963
+ ok: false,
30964
+ error: message.error ?? "unknown_session"
30965
+ });
30966
+ }
30943
30967
  function handleSpeakResult(message) {
30944
30968
  const pending = pendingSpeakResults.get(message.requestId);
30945
30969
  if (!pending)
@@ -30987,7 +31011,15 @@ function clearPendingSpeakForSession(sessionId, errorCode) {
30987
31011
  pendingBargeInAcks.delete(requestId);
30988
31012
  pending.resolve({ ok: false, reason: "session_ended" });
30989
31013
  }
31014
+ for (const [requestId, pending] of pendingSessionTimeLimitAcks) {
31015
+ if (pending.sessionId !== sessionId)
31016
+ continue;
31017
+ clearTimeout(pending.timer);
31018
+ pendingSessionTimeLimitAcks.delete(requestId);
31019
+ pending.resolve({ ok: false, error: "session_closing" });
31020
+ }
30990
31021
  speakControlWarnedSessionIds.delete(sessionId);
31022
+ sessionTimeLimitWarnedSessionIds.delete(sessionId);
30991
31023
  }
30992
31024
  var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
30993
31025
  var pendingRecordingAcks = /* @__PURE__ */ new Map();
@@ -31322,6 +31354,7 @@ async function handleParentMessage(message, handlers) {
31322
31354
  voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
31323
31355
  ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
31324
31356
  speakControlAvailableBySessionId.set(message.sessionId, message.speakControlAvailable ?? false);
31357
+ sessionTimeLimitAvailableBySessionId.set(message.sessionId, message.sessionTimeLimitAvailable ?? false);
31325
31358
  if (message.voiceLanguageSwitch) {
31326
31359
  voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
31327
31360
  } else {
@@ -31339,7 +31372,8 @@ async function handleParentMessage(message, handlers) {
31339
31372
  mixAvailable: message.mixAvailable ?? false,
31340
31373
  voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
31341
31374
  ttsPoseAvailable: message.ttsPoseAvailable ?? false,
31342
- speakControlAvailable: message.speakControlAvailable ?? false
31375
+ speakControlAvailable: message.speakControlAvailable ?? false,
31376
+ sessionTimeLimitAvailable: message.sessionTimeLimitAvailable ?? false
31343
31377
  });
31344
31378
  sendParentMessage({
31345
31379
  type: "session_start_ack",
@@ -31349,10 +31383,13 @@ async function handleParentMessage(message, handlers) {
31349
31383
  case "speech_event":
31350
31384
  await handlers.onSpeechEvent?.({ sessionId: message.sessionId }, message.event);
31351
31385
  if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
31386
+ const rawUtteranceId = message.event.utteranceId;
31387
+ const utteranceId = typeof rawUtteranceId === "string" ? rawUtteranceId.trim() : "";
31352
31388
  await handlers.onUserSpeechFinal?.({
31353
31389
  sessionId: message.sessionId,
31354
31390
  text: message.event.text.trim(),
31355
- ...message.event.replay === true ? { replay: true } : {}
31391
+ ...message.event.replay === true ? { replay: true } : {},
31392
+ ...utteranceId ? { utteranceId } : {}
31356
31393
  });
31357
31394
  }
31358
31395
  if (message.event.type === "user_language") {
@@ -31411,6 +31448,7 @@ async function handleParentMessage(message, handlers) {
31411
31448
  lastVoiceLanguageBySessionId.delete(message.sessionId);
31412
31449
  ttsPoseAvailableBySessionId.delete(message.sessionId);
31413
31450
  speakControlAvailableBySessionId.delete(message.sessionId);
31451
+ sessionTimeLimitAvailableBySessionId.delete(message.sessionId);
31414
31452
  clearPendingSpeakForSession(message.sessionId, "session_ended");
31415
31453
  await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
31416
31454
  sessionId: message.sessionId
@@ -31419,6 +31457,9 @@ async function handleParentMessage(message, handlers) {
31419
31457
  case "idle_timeout":
31420
31458
  await runIdleTimeoutHook(handlers, message);
31421
31459
  break;
31460
+ case "session_time_limit":
31461
+ await runSessionTimeLimitHook(handlers, message);
31462
+ break;
31422
31463
  }
31423
31464
  }
31424
31465
  function defineAgent(handlers) {
@@ -31443,6 +31484,10 @@ function defineAgent(handlers) {
31443
31484
  handleBargeInControlAck(message);
31444
31485
  return;
31445
31486
  }
31487
+ if (isSessionTimeLimitControlAckMessage(message)) {
31488
+ handleSessionTimeLimitControlAck(message);
31489
+ return;
31490
+ }
31446
31491
  if (isPlayAckMessage(message)) {
31447
31492
  handlePlayAck(message);
31448
31493
  return;
@@ -31606,6 +31651,43 @@ async function runIdleTimeoutHook(handlers, message) {
31606
31651
  });
31607
31652
  agentLog("info", error2 ? `idle_timeout_done ipc sent (onIdleTimeout error: ${error2})` : "idle_timeout_done ipc sent (onIdleTimeout completed)", message.sessionId);
31608
31653
  }
31654
+ async function runSessionTimeLimitHook(handlers, message) {
31655
+ const onSessionTimeLimit = handlers.onSessionTimeLimit;
31656
+ agentLog("info", `session_time_limit ipc received (maxGraceMs=${message.maxGraceMs}, onSessionTimeLimit=${typeof onSessionTimeLimit === "function"})`, message.sessionId);
31657
+ if (!onSessionTimeLimit) {
31658
+ sendParentMessage({
31659
+ type: "session_time_limit_done",
31660
+ sessionId: message.sessionId
31661
+ });
31662
+ return;
31663
+ }
31664
+ const env = peerEnvBySessionId.get(message.sessionId) ?? {
31665
+ SESSION_ID: message.sessionId,
31666
+ ...process.env.PROJECT_ID ? { PROJECT_ID: process.env.PROJECT_ID } : {},
31667
+ ...process.env.BUILD_ID ? { BUILD_ID: process.env.BUILD_ID } : {}
31668
+ };
31669
+ const ctx = {
31670
+ sessionId: message.sessionId,
31671
+ projectId: env.PROJECT_ID,
31672
+ buildId: env.BUILD_ID,
31673
+ env,
31674
+ limitMinutes: message.limitMinutes,
31675
+ elapsedMs: message.elapsedMs,
31676
+ graceMs: message.maxGraceMs
31677
+ };
31678
+ let error2;
31679
+ try {
31680
+ await onSessionTimeLimit(ctx);
31681
+ } catch (hookError) {
31682
+ error2 = hookError instanceof Error ? hookError.message : String(hookError);
31683
+ agentLog("error", `onSessionTimeLimit failed: ${error2}`, message.sessionId);
31684
+ }
31685
+ sendParentMessage({
31686
+ type: "session_time_limit_done",
31687
+ sessionId: message.sessionId,
31688
+ ...error2 !== void 0 ? { error: error2 } : {}
31689
+ });
31690
+ }
31609
31691
  function buildIdleEnv(sessionId) {
31610
31692
  return {
31611
31693
  SESSION_ID: sessionId,
@@ -35064,22 +35146,28 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
35064
35146
  s.announcing = Math.max(0, s.announcing - 1);
35065
35147
  });
35066
35148
  }
35149
+ function replyOpts(t) {
35150
+ return t.inReplyTo ? { turnId: t.turnId, inReplyTo: t.inReplyTo } : { turnId: t.turnId };
35151
+ }
35152
+ function sendReplyDelta(s, t, text) {
35153
+ sendToClient(s.sessionId, t.source === "chat" ? { type: "assistant_delta", turnId: t.turnId, text } : {
35154
+ type: "assistant_delta",
35155
+ turnId: t.turnId,
35156
+ text,
35157
+ spoken: true,
35158
+ ...t.inReplyTo ? { inReplyTo: t.inReplyTo } : {}
35159
+ });
35160
+ }
35067
35161
  function deliver(s, t, text, interruptible = true) {
35068
35162
  flushSpeech(s, t);
35069
35163
  t.spoken.push(text);
35070
35164
  markReplyStarted(s, t);
35071
- if (t.source === "chat") {
35072
- sendToClient(s.sessionId, {
35073
- type: "assistant_delta",
35074
- turnId: t.turnId,
35075
- text
35076
- });
35077
- }
35165
+ sendReplyDelta(s, t, text);
35078
35166
  if (t.source === "voice" || speakChat) {
35079
35167
  if (interruptible) {
35080
- speak(s.sessionId, text, { turnId: t.turnId });
35168
+ speak(s.sessionId, text, replyOpts(t));
35081
35169
  } else {
35082
- const h = announce(s.sessionId, text, { turnId: t.turnId });
35170
+ const h = announce(s.sessionId, text, replyOpts(t));
35083
35171
  trackAnnouncement(s, h.done);
35084
35172
  }
35085
35173
  }
@@ -35176,7 +35264,7 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
35176
35264
  t.pendingChars = 0;
35177
35265
  if (t.controller.signal.aborted || s.ended)
35178
35266
  return;
35179
- speak(s.sessionId, text, { turnId: t.turnId });
35267
+ speak(s.sessionId, text, replyOpts(t));
35180
35268
  }
35181
35269
  function deliverReply(s, t, text) {
35182
35270
  if (coalesceMs <= 0) {
@@ -35185,13 +35273,7 @@ ${validated.errors.map((e) => `${e.path || "(root)"}: ${e.message}`).join("\n")}
35185
35273
  }
35186
35274
  markReplyStarted(s, t);
35187
35275
  t.spoken.push(text);
35188
- if (t.source === "chat") {
35189
- sendToClient(s.sessionId, {
35190
- type: "assistant_delta",
35191
- turnId: t.turnId,
35192
- text
35193
- });
35194
- }
35276
+ sendReplyDelta(s, t, text);
35195
35277
  if (t.source !== "voice" && !speakChat)
35196
35278
  return;
35197
35279
  t.pending.push(text);
@@ -35711,6 +35793,16 @@ ${t.inputText}` : t.inputText, {
35711
35793
  });
35712
35794
  sendToClient(s.sessionId, { type: "chat_reply", text });
35713
35795
  }
35796
+ if (t.source === "voice" && t.spoken.length > 0) {
35797
+ sendToClient(s.sessionId, {
35798
+ type: "assistant_done",
35799
+ turnId: t.turnId,
35800
+ text: t.spoken.join(" "),
35801
+ spoken: true,
35802
+ status: summary.status,
35803
+ ...t.inReplyTo ? { inReplyTo: t.inReplyTo } : {}
35804
+ });
35805
+ }
35714
35806
  setAgentState(s.sessionId, "listening");
35715
35807
  if (hooks2.onTurnEnd) {
35716
35808
  await callHook("onTurnEnd", s.sessionId, () => hooks2.onTurnEnd(summary, ctx));
@@ -35726,7 +35818,7 @@ ${t.inputText}` : t.inputText, {
35726
35818
  thinkingCharsStripped: t.thinkingCharsStripped
35727
35819
  }, s.sessionId);
35728
35820
  }
35729
- function startTurn(s, text, source) {
35821
+ function startTurn(s, text, source, utteranceId) {
35730
35822
  const turnId = randomUUID3();
35731
35823
  const controller = new AbortController();
35732
35824
  const t = {
@@ -35736,6 +35828,7 @@ ${t.inputText}` : t.inputText, {
35736
35828
  controller,
35737
35829
  startedAt: now(),
35738
35830
  spoken: [],
35831
+ ...source === "voice" && utteranceId ? { inReplyTo: utteranceId } : {},
35739
35832
  toolCalls: [],
35740
35833
  llmCalls: 0,
35741
35834
  roundOpen: false,
@@ -35786,12 +35879,12 @@ ${t.inputText}` : t.inputText, {
35786
35879
  const q = s.queued;
35787
35880
  if (q && !s.ended && !s.active) {
35788
35881
  s.queued = void 0;
35789
- startTurn(s, q.text, q.source);
35882
+ startTurn(s, q.text, q.source, q.utteranceId);
35790
35883
  }
35791
35884
  }
35792
35885
  })();
35793
35886
  }
35794
- async function handleInput(s, text, source) {
35887
+ async function handleInput(s, text, source, utteranceId) {
35795
35888
  if (s.ended)
35796
35889
  return;
35797
35890
  if (source === "voice" && config2.speechDuringNonInterruptible === "discard" && s.announcing > 0) {
@@ -35821,7 +35914,7 @@ ${t.inputText}` : t.inputText, {
35821
35914
  const active = s.active;
35822
35915
  if (active) {
35823
35916
  if (policy === "finish") {
35824
- s.queued = { text, source };
35917
+ s.queued = { text, source, utteranceId };
35825
35918
  return;
35826
35919
  }
35827
35920
  cancelSpeech(s.sessionId, { turnId: active.turnId });
@@ -35841,14 +35934,14 @@ ${t.inputText}` : t.inputText, {
35841
35934
  if (s.ended)
35842
35935
  return;
35843
35936
  }
35844
- startTurn(s, text, source);
35937
+ startTurn(s, text, source, utteranceId);
35845
35938
  }
35846
- function submit(sessionId, text, source) {
35939
+ function submit(sessionId, text, source, utteranceId) {
35847
35940
  const s = sessions.get(sessionId);
35848
35941
  const trimmed = text.trim();
35849
35942
  if (!s || !trimmed)
35850
35943
  return;
35851
- s.chain = s.chain.then(() => handleInput(s, trimmed, source)).catch((err) => {
35944
+ s.chain = s.chain.then(() => handleInput(s, trimmed, source, utteranceId)).catch((err) => {
35852
35945
  const message = err instanceof Error ? err.message : String(err);
35853
35946
  agentLog("error", `llm input failed: ${message}`, sessionId);
35854
35947
  });
@@ -35894,7 +35987,7 @@ ${t.inputText}` : t.inputText, {
35894
35987
  }
35895
35988
  },
35896
35989
  onUserSpeechFinal(ctx) {
35897
- submit(ctx.sessionId, ctx.text, "voice");
35990
+ submit(ctx.sessionId, ctx.text, "voice", ctx.utteranceId);
35898
35991
  },
35899
35992
  onDataChannelMessage(ctx) {
35900
35993
  const text = parseChatText(ctx.message);
@@ -59423,7 +59516,7 @@ var voicethere_llm_default = {
59423
59516
  history: { turns: 20, interruptionNote: true },
59424
59517
  fillers: {
59425
59518
  thinking: {
59426
- phrases: ["Hmm.", "Okay.", "Mm-hm.", "Right."],
59519
+ phrases: ["Okay.", "Right.", "Sure.", "Let me see."],
59427
59520
  afterMs: 1500
59428
59521
  },
59429
59522
  toolCall: {