@voicethere/agent 0.10.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -0
- package/dist/agent.js +175 -5
- package/dist/build-bundle.d.ts.map +1 -1
- package/dist/build-bundle.js +1 -0
- package/dist/build-bundle.js.map +1 -1
- package/dist/host/index.d.ts +4 -0
- package/dist/host/index.d.ts.map +1 -0
- package/dist/host/index.js +3 -0
- package/dist/host/index.js.map +1 -0
- package/dist/host/session-speak-controller.d.ts +70 -0
- package/dist/host/session-speak-controller.d.ts.map +1 -0
- package/dist/host/session-speak-controller.js +296 -0
- package/dist/host/session-speak-controller.js.map +1 -0
- package/dist/host/speak-text.d.ts +11 -0
- package/dist/host/speak-text.d.ts.map +1 -0
- package/dist/host/speak-text.js +21 -0
- package/dist/host/speak-text.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/llm/agent/config.d.ts +376 -0
- package/dist/llm/agent/config.d.ts.map +1 -0
- package/dist/llm/agent/config.js +678 -0
- package/dist/llm/agent/config.js.map +1 -0
- package/dist/llm/agent/create-llm-agent.d.ts +22 -0
- package/dist/llm/agent/create-llm-agent.d.ts.map +1 -0
- package/dist/llm/agent/create-llm-agent.js +720 -0
- package/dist/llm/agent/create-llm-agent.js.map +1 -0
- package/dist/llm/agent/define-tool.d.ts +8 -0
- package/dist/llm/agent/define-tool.d.ts.map +1 -0
- package/dist/llm/agent/define-tool.js +9 -0
- package/dist/llm/agent/define-tool.js.map +1 -0
- package/dist/llm/agent/history.d.ts +35 -0
- package/dist/llm/agent/history.d.ts.map +1 -0
- package/dist/llm/agent/history.js +79 -0
- package/dist/llm/agent/history.js.map +1 -0
- package/dist/llm/agent/hooks.d.ts +50 -0
- package/dist/llm/agent/hooks.d.ts.map +1 -0
- package/dist/llm/agent/hooks.js +2 -0
- package/dist/llm/agent/hooks.js.map +1 -0
- package/dist/llm/agent/sentence-splitter.d.ts +10 -0
- package/dist/llm/agent/sentence-splitter.d.ts.map +1 -0
- package/dist/llm/agent/sentence-splitter.js +92 -0
- package/dist/llm/agent/sentence-splitter.js.map +1 -0
- package/dist/llm/anthropic-provider.d.ts +7 -0
- package/dist/llm/anthropic-provider.d.ts.map +1 -0
- package/dist/llm/anthropic-provider.js +115 -0
- package/dist/llm/anthropic-provider.js.map +1 -0
- package/dist/llm/bounded-provider.d.ts +12 -0
- package/dist/llm/bounded-provider.d.ts.map +1 -0
- package/dist/llm/bounded-provider.js +132 -0
- package/dist/llm/bounded-provider.js.map +1 -0
- package/dist/llm/catalog.d.ts +9 -0
- package/dist/llm/catalog.d.ts.map +1 -0
- package/dist/llm/catalog.js +35 -0
- package/dist/llm/catalog.js.map +1 -0
- package/dist/llm/gemini-provider.d.ts +7 -0
- package/dist/llm/gemini-provider.d.ts.map +1 -0
- package/dist/llm/gemini-provider.js +120 -0
- package/dist/llm/gemini-provider.js.map +1 -0
- package/dist/llm/index.d.ts +18 -0
- package/dist/llm/index.d.ts.map +1 -0
- package/dist/llm/index.js +11 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/mcp/index.d.ts +5 -0
- package/dist/llm/mcp/index.d.ts.map +1 -0
- package/dist/llm/mcp/index.js +3 -0
- package/dist/llm/mcp/index.js.map +1 -0
- package/dist/llm/mcp/mcp-tool-source.d.ts +65 -0
- package/dist/llm/mcp/mcp-tool-source.d.ts.map +1 -0
- package/dist/llm/mcp/mcp-tool-source.js +328 -0
- package/dist/llm/mcp/mcp-tool-source.js.map +1 -0
- package/dist/llm/mcp/tool-registry.d.ts +44 -0
- package/dist/llm/mcp/tool-registry.d.ts.map +1 -0
- package/dist/llm/mcp/tool-registry.js +147 -0
- package/dist/llm/mcp/tool-registry.js.map +1 -0
- package/dist/llm/openai-provider.d.ts +8 -0
- package/dist/llm/openai-provider.d.ts.map +1 -0
- package/dist/llm/openai-provider.js +146 -0
- package/dist/llm/openai-provider.js.map +1 -0
- package/dist/llm/providers.d.ts +10 -0
- package/dist/llm/providers.d.ts.map +1 -0
- package/dist/llm/providers.js +32 -0
- package/dist/llm/providers.js.map +1 -0
- package/dist/llm/types.d.ts +70 -0
- package/dist/llm/types.d.ts.map +1 -0
- package/dist/llm/types.js +11 -0
- package/dist/llm/types.js.map +1 -0
- package/dist/protocol.d.ts +95 -2
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/runtime.d.ts +69 -6
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +274 -7
- package/dist/runtime.js.map +1 -1
- package/dist/templates/echo/agent.js +178 -7
- package/dist/templates/echo-dc/agent.js +85 -2
- package/dist/templates/game-sync/agent.js +85 -2
- package/dist/templates/language-switch/agent.js +173 -5
- package/dist/templates/llm-agent/agent.js +121378 -0
- package/dist/templates/positional-tts/agent.js +173 -5
- package/dist/templates/recording-consent/agent.js +173 -5
- package/dist/templates/registry.d.ts +1 -1
- package/dist/templates/registry.d.ts.map +1 -1
- package/dist/templates/registry.js +24 -0
- package/dist/templates/registry.js.map +1 -1
- package/dist/templates/spatial-showcase/agent.js +173 -5
- package/dist/templates/voice-showcase/agent.js +178 -7
- package/dist/templates/voice-starter/agent.js +176 -5
- package/dist/templates/webhooks/agent.js +176 -5
- package/dist/templates/webhooks-redis/agent.js +85 -2
- package/dist/templates/world-sync/agent.js +85 -2
- package/dist/templates/world-sync-binary/agent.js +85 -2
- package/eject/llm/loop.ts +913 -0
- package/package.json +54 -4
- package/schema/voicethere.llm.schema.json +325 -0
- package/templates/llm-agent/README.md +24 -0
- package/templates/llm-agent/agent.ts +8 -0
- package/templates/llm-agent/hooks.ts +21 -0
- package/templates/llm-agent/md.d.ts +4 -0
- package/templates/llm-agent/prompt.md +11 -0
- package/templates/llm-agent/tools.ts +51 -0
- package/templates/llm-agent/voicethere.llm.json +21 -0
|
@@ -155,6 +155,18 @@ var SessionSerialQueue = class {
|
|
|
155
155
|
var SESSION_START_INIT_DELAY_ENABLED_ENV = "AGENT_SESSION_START_INIT_DELAY_ENABLED";
|
|
156
156
|
var SESSION_START_INIT_DELAY_MS_ENV = "AGENT_SESSION_START_INIT_DELAY_MS";
|
|
157
157
|
var DEFAULT_SESSION_START_INIT_DELAY_MS = 500;
|
|
158
|
+
function isSpeakResultMessage(value) {
|
|
159
|
+
if (!value || typeof value !== "object")
|
|
160
|
+
return false;
|
|
161
|
+
const msg = value;
|
|
162
|
+
return msg.type === "speak_result" && typeof msg.requestId === "string";
|
|
163
|
+
}
|
|
164
|
+
function isBargeInControlAckMessage(value) {
|
|
165
|
+
if (!value || typeof value !== "object")
|
|
166
|
+
return false;
|
|
167
|
+
const msg = value;
|
|
168
|
+
return msg.type === "barge_in_control_ack" && typeof msg.requestId === "string";
|
|
169
|
+
}
|
|
158
170
|
function isPlayAckMessage(value) {
|
|
159
171
|
if (!value || typeof value !== "object")
|
|
160
172
|
return false;
|
|
@@ -263,7 +275,7 @@ function isParentMessage(value) {
|
|
|
263
275
|
if (!value || typeof value !== "object")
|
|
264
276
|
return false;
|
|
265
277
|
const msg = value;
|
|
266
|
-
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";
|
|
278
|
+
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";
|
|
267
279
|
}
|
|
268
280
|
function isSessionScopedParentMessage(value) {
|
|
269
281
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -282,8 +294,62 @@ var mixAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
|
282
294
|
var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
283
295
|
var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
|
|
284
296
|
var ttsPoseAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
297
|
+
var speakControlAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
298
|
+
var speakControlWarnedSessionIds = /* @__PURE__ */ new Set();
|
|
285
299
|
var endedSessionIds = /* @__PURE__ */ new Set();
|
|
286
300
|
var PLAY_CONTROL_ACK_TIMEOUT_MS = 5e3;
|
|
301
|
+
var SPEAK_RESULT_SAFETY_TIMEOUT_MS = 6e5;
|
|
302
|
+
var pendingSpeakResults = /* @__PURE__ */ new Map();
|
|
303
|
+
var pendingBargeInAcks = /* @__PURE__ */ new Map();
|
|
304
|
+
function handleSpeakResult(message) {
|
|
305
|
+
const pending = pendingSpeakResults.get(message.requestId);
|
|
306
|
+
if (!pending)
|
|
307
|
+
return;
|
|
308
|
+
clearTimeout(pending.timer);
|
|
309
|
+
pendingSpeakResults.delete(message.requestId);
|
|
310
|
+
pending.resolve({
|
|
311
|
+
requestId: message.requestId,
|
|
312
|
+
...message.turnId !== void 0 ? { turnId: message.turnId } : {},
|
|
313
|
+
status: message.status,
|
|
314
|
+
playedMs: message.playedMs,
|
|
315
|
+
...message.synthesizedMs !== void 0 ? { synthesizedMs: message.synthesizedMs } : {},
|
|
316
|
+
...message.errorCode !== void 0 ? { errorCode: message.errorCode } : {}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
function handleBargeInControlAck(message) {
|
|
320
|
+
const pending = pendingBargeInAcks.get(message.requestId);
|
|
321
|
+
if (!pending)
|
|
322
|
+
return;
|
|
323
|
+
clearTimeout(pending.timer);
|
|
324
|
+
pendingBargeInAcks.delete(message.requestId);
|
|
325
|
+
pending.resolve({
|
|
326
|
+
ok: message.ok,
|
|
327
|
+
...message.reason ? { reason: message.reason } : {}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
331
|
+
for (const [requestId, pending] of pendingSpeakResults) {
|
|
332
|
+
if (pending.sessionId !== sessionId)
|
|
333
|
+
continue;
|
|
334
|
+
clearTimeout(pending.timer);
|
|
335
|
+
pendingSpeakResults.delete(requestId);
|
|
336
|
+
pending.resolve({
|
|
337
|
+
requestId,
|
|
338
|
+
...pending.turnId !== void 0 ? { turnId: pending.turnId } : {},
|
|
339
|
+
status: "cancelled",
|
|
340
|
+
playedMs: 0,
|
|
341
|
+
errorCode
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
for (const [requestId, pending] of pendingBargeInAcks) {
|
|
345
|
+
if (pending.sessionId !== sessionId)
|
|
346
|
+
continue;
|
|
347
|
+
clearTimeout(pending.timer);
|
|
348
|
+
pendingBargeInAcks.delete(requestId);
|
|
349
|
+
pending.resolve({ ok: false, reason: "session_ended" });
|
|
350
|
+
}
|
|
351
|
+
speakControlWarnedSessionIds.delete(sessionId);
|
|
352
|
+
}
|
|
287
353
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
288
354
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
289
355
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
@@ -642,6 +708,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
642
708
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
643
709
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
644
710
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
711
|
+
speakControlAvailableBySessionId.set(message.sessionId, message.speakControlAvailable ?? false);
|
|
645
712
|
if (message.voiceLanguageSwitch) {
|
|
646
713
|
voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
|
|
647
714
|
} else {
|
|
@@ -658,7 +725,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
658
725
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
659
726
|
mixAvailable: message.mixAvailable ?? false,
|
|
660
727
|
voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
|
|
661
|
-
ttsPoseAvailable: message.ttsPoseAvailable ?? false
|
|
728
|
+
ttsPoseAvailable: message.ttsPoseAvailable ?? false,
|
|
729
|
+
speakControlAvailable: message.speakControlAvailable ?? false
|
|
662
730
|
});
|
|
663
731
|
sendParentMessage({
|
|
664
732
|
type: "session_start_ack",
|
|
@@ -695,6 +763,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
695
763
|
}
|
|
696
764
|
}
|
|
697
765
|
break;
|
|
766
|
+
case "agent_interrupted": {
|
|
767
|
+
const { type: _type, ...interrupted } = message;
|
|
768
|
+
await handlers.onAgentInterrupted?.(interrupted);
|
|
769
|
+
break;
|
|
770
|
+
}
|
|
698
771
|
case "data_channel_message":
|
|
699
772
|
await handlers.onDataChannelMessage?.({
|
|
700
773
|
sessionId: message.sessionId,
|
|
@@ -724,6 +797,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
724
797
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
725
798
|
lastVoiceLanguageBySessionId.delete(message.sessionId);
|
|
726
799
|
ttsPoseAvailableBySessionId.delete(message.sessionId);
|
|
800
|
+
speakControlAvailableBySessionId.delete(message.sessionId);
|
|
801
|
+
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
727
802
|
await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
|
|
728
803
|
sessionId: message.sessionId
|
|
729
804
|
});
|
|
@@ -747,6 +822,14 @@ function defineAgent(handlers) {
|
|
|
747
822
|
handleConversationHistoryControlAck(message);
|
|
748
823
|
return;
|
|
749
824
|
}
|
|
825
|
+
if (isSpeakResultMessage(message)) {
|
|
826
|
+
handleSpeakResult(message);
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
if (isBargeInControlAckMessage(message)) {
|
|
830
|
+
handleBargeInControlAck(message);
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
750
833
|
if (isPlayAckMessage(message)) {
|
|
751
834
|
handlePlayAck(message);
|
|
752
835
|
return;
|
|
@@ -791,6 +874,7 @@ function defineAgent(handlers) {
|
|
|
791
874
|
return;
|
|
792
875
|
if (message.type === "session_end") {
|
|
793
876
|
endedSessionIds.add(message.sessionId);
|
|
877
|
+
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
794
878
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
795
879
|
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
796
880
|
inboundBySession.clear(message.sessionId);
|
|
@@ -917,12 +1001,96 @@ function buildIdleEnv(sessionId) {
|
|
|
917
1001
|
...process.env.IDLE_TIMEOUT_SEC ? { IDLE_TIMEOUT_SEC: process.env.IDLE_TIMEOUT_SEC } : {}
|
|
918
1002
|
};
|
|
919
1003
|
}
|
|
1004
|
+
function speakControlAvailableFor(sessionId) {
|
|
1005
|
+
return speakControlAvailableBySessionId.get(sessionId) ?? false;
|
|
1006
|
+
}
|
|
1007
|
+
function warnSpeakControlUnavailable(sessionId) {
|
|
1008
|
+
if (speakControlWarnedSessionIds.has(sessionId))
|
|
1009
|
+
return;
|
|
1010
|
+
speakControlWarnedSessionIds.add(sessionId);
|
|
1011
|
+
agentLog("warn", "speak control not available on this runner; speak results, cancel and barge-in control are disabled", sessionId);
|
|
1012
|
+
}
|
|
920
1013
|
function speak(sessionId, text, options) {
|
|
921
|
-
|
|
922
|
-
|
|
1014
|
+
const requestId = randomUUID();
|
|
1015
|
+
const handle = (done2) => ({
|
|
1016
|
+
requestId,
|
|
1017
|
+
done: Promise.resolve(done2),
|
|
1018
|
+
cancel: () => cancelSpeech(sessionId, { requestId })
|
|
1019
|
+
});
|
|
1020
|
+
if (!isVoicethereAgentChild()) {
|
|
1021
|
+
return handle({
|
|
1022
|
+
requestId,
|
|
1023
|
+
status: "unsupported",
|
|
1024
|
+
playedMs: 0,
|
|
1025
|
+
errorCode: "no_parent"
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
if (!allowOutboundForSession(sessionId)) {
|
|
1029
|
+
return handle({
|
|
1030
|
+
requestId,
|
|
1031
|
+
status: "cancelled",
|
|
1032
|
+
playedMs: 0,
|
|
1033
|
+
errorCode: "session_ended"
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
if (!speakControlAvailableFor(sessionId)) {
|
|
1037
|
+
sendParentMessage({
|
|
1038
|
+
type: "speak",
|
|
1039
|
+
sessionId,
|
|
1040
|
+
text,
|
|
1041
|
+
...options?.interruptible === false ? { interruptible: false } : {}
|
|
1042
|
+
});
|
|
1043
|
+
warnSpeakControlUnavailable(sessionId);
|
|
1044
|
+
return handle({ requestId, status: "unsupported", playedMs: 0 });
|
|
1045
|
+
}
|
|
1046
|
+
const turnId = options?.turnId;
|
|
1047
|
+
const inReplyTo = options?.inReplyTo;
|
|
1048
|
+
const done = new Promise((resolve) => {
|
|
1049
|
+
const timer = setTimeout(() => {
|
|
1050
|
+
pendingSpeakResults.delete(requestId);
|
|
1051
|
+
resolve({
|
|
1052
|
+
requestId,
|
|
1053
|
+
...turnId ? { turnId } : {},
|
|
1054
|
+
status: "failed",
|
|
1055
|
+
playedMs: 0,
|
|
1056
|
+
errorCode: "timeout"
|
|
1057
|
+
});
|
|
1058
|
+
}, SPEAK_RESULT_SAFETY_TIMEOUT_MS);
|
|
1059
|
+
pendingSpeakResults.set(requestId, {
|
|
1060
|
+
sessionId,
|
|
1061
|
+
...turnId ? { turnId } : {},
|
|
1062
|
+
resolve,
|
|
1063
|
+
timer
|
|
1064
|
+
});
|
|
1065
|
+
});
|
|
1066
|
+
sendParentMessage({
|
|
1067
|
+
type: "speak",
|
|
1068
|
+
sessionId,
|
|
1069
|
+
text,
|
|
1070
|
+
requestId,
|
|
1071
|
+
...options?.interruptible === false ? { interruptible: false } : {},
|
|
1072
|
+
...turnId ? { turnId } : {},
|
|
1073
|
+
...inReplyTo ? { inReplyTo } : {}
|
|
1074
|
+
});
|
|
1075
|
+
return {
|
|
1076
|
+
requestId,
|
|
1077
|
+
done,
|
|
1078
|
+
cancel: () => cancelSpeech(sessionId, { requestId })
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
function cancelSpeech(sessionId, target) {
|
|
1082
|
+
if (!isVoicethereAgentChild() || !allowOutboundForSession(sessionId))
|
|
1083
|
+
return;
|
|
1084
|
+
if (!speakControlAvailableFor(sessionId)) {
|
|
1085
|
+
warnSpeakControlUnavailable(sessionId);
|
|
923
1086
|
return;
|
|
924
1087
|
}
|
|
925
|
-
sendParentMessage({
|
|
1088
|
+
sendParentMessage({
|
|
1089
|
+
type: "speak_cancel",
|
|
1090
|
+
sessionId,
|
|
1091
|
+
...target?.requestId ? { requestId: target.requestId } : {},
|
|
1092
|
+
...target?.turnId ? { turnId: target.turnId } : {}
|
|
1093
|
+
});
|
|
926
1094
|
}
|
|
927
1095
|
function isMixAvailable(ctx) {
|
|
928
1096
|
return ctx.mixAvailable;
|
|
@@ -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,17 +970,101 @@ 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
|
-
|
|
888
|
-
|
|
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({
|
|
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
|
function speakAndChat(sessionId, text, options) {
|
|
894
1065
|
const trimmed = text.trim();
|
|
895
1066
|
if (!trimmed) {
|
|
896
|
-
return;
|
|
1067
|
+
return void 0;
|
|
897
1068
|
}
|
|
898
1069
|
const payload = { type: "chat_reply", text: trimmed };
|
|
899
1070
|
if (options?.stream) {
|
|
@@ -901,7 +1072,7 @@ function speakAndChat(sessionId, text, options) {
|
|
|
901
1072
|
payload.utteranceId = options.utteranceId ?? randomUUID();
|
|
902
1073
|
}
|
|
903
1074
|
sendToClient(sessionId, payload);
|
|
904
|
-
speak(sessionId, trimmed);
|
|
1075
|
+
return speak(sessionId, trimmed);
|
|
905
1076
|
}
|
|
906
1077
|
function sendToClient(sessionId, payload) {
|
|
907
1078
|
sendParentMessage({ type: "send_to_client", sessionId, payload });
|