@voicethere/agent 0.26.0 → 0.27.1
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 +23 -0
- package/dist/agent.js +78 -1
- package/dist/flow/classify.d.ts +8 -2
- package/dist/flow/classify.d.ts.map +1 -1
- package/dist/flow/classify.js +18 -3
- package/dist/flow/classify.js.map +1 -1
- package/dist/flow/core/interpreter.d.ts.map +1 -1
- package/dist/flow/core/interpreter.js +4 -0
- package/dist/flow/core/interpreter.js.map +1 -1
- package/dist/flow/core/route.d.ts +1 -1
- package/dist/flow/core/route.d.ts.map +1 -1
- package/dist/flow/core/route.js +13 -4
- package/dist/flow/core/route.js.map +1 -1
- 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/create-llm-agent.d.ts +5 -0
- package/dist/llm/agent/create-llm-agent.d.ts.map +1 -1
- package/dist/llm/agent/create-llm-agent.js +11 -2
- package/dist/llm/agent/create-llm-agent.js.map +1 -1
- package/dist/llm/agent/define-tool.d.ts +2 -0
- package/dist/llm/agent/define-tool.d.ts.map +1 -1
- package/dist/llm/agent/define-tool.js +1 -0
- package/dist/llm/agent/define-tool.js.map +1 -1
- package/dist/llm/agent/redact.d.ts.map +1 -1
- package/dist/llm/agent/redact.js +1 -0
- package/dist/llm/agent/redact.js.map +1 -1
- package/dist/llm/end-user/end-user-tools.d.ts +10 -0
- package/dist/llm/end-user/end-user-tools.d.ts.map +1 -0
- package/dist/llm/end-user/end-user-tools.js +50 -0
- package/dist/llm/end-user/end-user-tools.js.map +1 -0
- package/dist/llm/index.d.ts +1 -0
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +1 -0
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/mcp/tool-registry.d.ts +10 -1
- package/dist/llm/mcp/tool-registry.d.ts.map +1 -1
- package/dist/llm/mcp/tool-registry.js +10 -3
- package/dist/llm/mcp/tool-registry.js.map +1 -1
- package/dist/llm/types.d.ts +3 -1
- package/dist/llm/types.d.ts.map +1 -1
- package/dist/llm/types.js.map +1 -1
- package/dist/protocol.d.ts +53 -3
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +2 -0
- package/dist/protocol.js.map +1 -1
- package/dist/runtime.d.ts +34 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +133 -0
- package/dist/runtime.js.map +1 -1
- package/dist/templates/echo/agent.js +87 -1
- package/dist/templates/echo-dc/agent.js +87 -1
- package/dist/templates/flow-agent/agent.js +131 -23
- package/dist/templates/game-sync/agent.js +87 -1
- package/dist/templates/language-switch/agent.js +87 -1
- package/dist/templates/llm-agent/agent.js +166 -6
- package/dist/templates/llm-docs-rag/agent.js +166 -6
- package/dist/templates/llm-mcp-service/agent.js +166 -6
- package/dist/templates/llm-support-sticky-user/agent.js +60690 -0
- package/dist/templates/positional-tts/agent.js +87 -1
- package/dist/templates/recording-consent/agent.js +87 -1
- package/dist/templates/registry.d.ts.map +1 -1
- package/dist/templates/registry.js +26 -0
- package/dist/templates/registry.js.map +1 -1
- package/dist/templates/spatial-showcase/agent.js +87 -1
- package/dist/templates/voice-showcase/agent.js +87 -1
- package/dist/templates/voice-starter/agent.js +87 -1
- package/dist/templates/webhooks/agent.js +87 -1
- package/dist/templates/webhooks-redis/agent.js +87 -1
- package/dist/templates/world-sync/agent.js +87 -1
- package/dist/templates/world-sync-binary/agent.js +87 -1
- package/eject/llm/loop.ts +17 -2
- package/package.json +1 -1
- package/templates/README.md +1 -0
- package/templates/llm-support-sticky-user/README.md +68 -0
- package/templates/llm-support-sticky-user/agent.ts +29 -0
- package/templates/llm-support-sticky-user/hooks.ts +188 -0
- package/templates/llm-support-sticky-user/md.d.ts +4 -0
- package/templates/llm-support-sticky-user/mock-crm.ts +75 -0
- package/templates/llm-support-sticky-user/prompt.md +21 -0
- package/templates/llm-support-sticky-user/tools.ts +244 -0
- package/templates/llm-support-sticky-user/voicethere.llm.json +52 -0
|
@@ -205,6 +205,12 @@ function isConversationHistoryControlAckMessage(value) {
|
|
|
205
205
|
const msg = value;
|
|
206
206
|
return msg.type === "conversation_history_control_ack" && typeof msg.requestId === "string";
|
|
207
207
|
}
|
|
208
|
+
function isUserHistoryResponseMessage(value) {
|
|
209
|
+
if (!value || typeof value !== "object")
|
|
210
|
+
return false;
|
|
211
|
+
const msg = value;
|
|
212
|
+
return msg.type === "user_history_response" && typeof msg.requestId === "string";
|
|
213
|
+
}
|
|
208
214
|
function isMixControlAckMessage(value) {
|
|
209
215
|
if (!value || typeof value !== "object")
|
|
210
216
|
return false;
|
|
@@ -277,7 +283,7 @@ function isParentMessage(value) {
|
|
|
277
283
|
if (!value || typeof value !== "object")
|
|
278
284
|
return false;
|
|
279
285
|
const msg = value;
|
|
280
|
-
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";
|
|
286
|
+
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 === "user_identified" || msg.type === "user_history_response" || 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";
|
|
281
287
|
}
|
|
282
288
|
function isSessionScopedParentMessage(value) {
|
|
283
289
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -292,6 +298,7 @@ function parseDataChannelPayload(raw) {
|
|
|
292
298
|
var peerEnvBySessionId = /* @__PURE__ */ new Map();
|
|
293
299
|
var recordingAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
294
300
|
var conversationHistoryAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
301
|
+
var endUserBySessionId = /* @__PURE__ */ new Map();
|
|
295
302
|
var mixAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
296
303
|
var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
297
304
|
var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
|
|
@@ -380,6 +387,7 @@ function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
|
380
387
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
381
388
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
382
389
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
390
|
+
var pendingUserHistory = /* @__PURE__ */ new Map();
|
|
383
391
|
var pendingPlayAcks = /* @__PURE__ */ new Map();
|
|
384
392
|
var pendingPlayStatusAcks = /* @__PURE__ */ new Map();
|
|
385
393
|
var pendingPlayStopAcks = /* @__PURE__ */ new Map();
|
|
@@ -414,6 +422,27 @@ function handleConversationHistoryControlAck(message) {
|
|
|
414
422
|
requestId: message.requestId
|
|
415
423
|
});
|
|
416
424
|
}
|
|
425
|
+
function handleUserHistoryResponse(message) {
|
|
426
|
+
const pending = pendingUserHistory.get(message.requestId);
|
|
427
|
+
if (!pending)
|
|
428
|
+
return;
|
|
429
|
+
clearTimeout(pending.timer);
|
|
430
|
+
pendingUserHistory.delete(message.requestId);
|
|
431
|
+
pending.resolve({
|
|
432
|
+
ok: message.ok,
|
|
433
|
+
...message.reason ? { reason: message.reason } : {},
|
|
434
|
+
conversations: Array.isArray(message.conversations) ? message.conversations : []
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
function clearPendingUserHistoryForSession(sessionId, reason) {
|
|
438
|
+
for (const [requestId, pending] of pendingUserHistory) {
|
|
439
|
+
if (pending.sessionId !== sessionId)
|
|
440
|
+
continue;
|
|
441
|
+
clearTimeout(pending.timer);
|
|
442
|
+
pendingUserHistory.delete(requestId);
|
|
443
|
+
pending.resolve({ ok: false, reason, conversations: [] });
|
|
444
|
+
}
|
|
445
|
+
}
|
|
417
446
|
function handlePlayAck(message) {
|
|
418
447
|
const pending = pendingPlayAcks.get(message.requestId);
|
|
419
448
|
if (!pending)
|
|
@@ -734,6 +763,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
734
763
|
peerEnvBySessionId.set(message.sessionId, message.env);
|
|
735
764
|
recordingAvailableBySessionId.set(message.sessionId, message.recordingAvailable ?? false);
|
|
736
765
|
conversationHistoryAvailableBySessionId.set(message.sessionId, message.conversationHistoryAvailable ?? false);
|
|
766
|
+
const startUser = parseEndUserInfo(message.endUser) ?? parseEndUserEnv(message.env?.AGENT_END_USER);
|
|
767
|
+
if (startUser)
|
|
768
|
+
endUserBySessionId.set(message.sessionId, startUser);
|
|
769
|
+
else
|
|
770
|
+
endUserBySessionId.delete(message.sessionId);
|
|
737
771
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
738
772
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
739
773
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
@@ -751,6 +785,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
751
785
|
await (handlers.onClientJoin ?? handlers.onSessionStart)?.({
|
|
752
786
|
sessionId: message.sessionId,
|
|
753
787
|
env: message.env,
|
|
788
|
+
...startUser ? { user: startUser } : {},
|
|
754
789
|
recordingAvailable: message.recordingAvailable ?? false,
|
|
755
790
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
756
791
|
mixAvailable: message.mixAvailable ?? false,
|
|
@@ -764,6 +799,27 @@ async function handleParentMessage(message, handlers) {
|
|
|
764
799
|
sessionId: message.sessionId
|
|
765
800
|
});
|
|
766
801
|
break;
|
|
802
|
+
case "user_identified": {
|
|
803
|
+
if (endedSessionIds.has(message.sessionId))
|
|
804
|
+
break;
|
|
805
|
+
const user = parseEndUserInfo(message.endUser);
|
|
806
|
+
if (!user)
|
|
807
|
+
break;
|
|
808
|
+
endUserBySessionId.set(message.sessionId, user);
|
|
809
|
+
await handlers.onUserIdentified?.({
|
|
810
|
+
sessionId: message.sessionId,
|
|
811
|
+
env: peerEnvBySessionId.get(message.sessionId) ?? {},
|
|
812
|
+
recordingAvailable: recordingAvailableBySessionId.get(message.sessionId) ?? false,
|
|
813
|
+
conversationHistoryAvailable: conversationHistoryAvailableBySessionId.get(message.sessionId) ?? false,
|
|
814
|
+
mixAvailable: mixAvailableBySessionId.get(message.sessionId) ?? false,
|
|
815
|
+
voiceLanguageSwitchAvailable: voiceLanguageSwitchAvailableBySessionId.get(message.sessionId) ?? false,
|
|
816
|
+
ttsPoseAvailable: ttsPoseAvailableBySessionId.get(message.sessionId) ?? false,
|
|
817
|
+
speakControlAvailable: speakControlAvailableBySessionId.get(message.sessionId) ?? false,
|
|
818
|
+
sessionTimeLimitAvailable: sessionTimeLimitAvailableBySessionId.get(message.sessionId) ?? false,
|
|
819
|
+
user
|
|
820
|
+
});
|
|
821
|
+
break;
|
|
822
|
+
}
|
|
767
823
|
case "speech_event":
|
|
768
824
|
await handlers.onSpeechEvent?.({ sessionId: message.sessionId }, message.event);
|
|
769
825
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
@@ -826,6 +882,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
826
882
|
peerEnvBySessionId.delete(message.sessionId);
|
|
827
883
|
recordingAvailableBySessionId.delete(message.sessionId);
|
|
828
884
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
885
|
+
endUserBySessionId.delete(message.sessionId);
|
|
829
886
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
830
887
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
831
888
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
@@ -860,6 +917,10 @@ function defineAgent(handlers) {
|
|
|
860
917
|
handleConversationHistoryControlAck(message);
|
|
861
918
|
return;
|
|
862
919
|
}
|
|
920
|
+
if (isUserHistoryResponseMessage(message)) {
|
|
921
|
+
handleUserHistoryResponse(message);
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
863
924
|
if (isSpeakResultMessage(message)) {
|
|
864
925
|
handleSpeakResult(message);
|
|
865
926
|
return;
|
|
@@ -919,6 +980,7 @@ function defineAgent(handlers) {
|
|
|
919
980
|
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
920
981
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
921
982
|
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
983
|
+
clearPendingUserHistoryForSession(message.sessionId, "session_ended");
|
|
922
984
|
inboundBySession.clear(message.sessionId);
|
|
923
985
|
}
|
|
924
986
|
if (message.type === "session_start") {
|
|
@@ -991,6 +1053,30 @@ function parseCustomerContext(raw) {
|
|
|
991
1053
|
}
|
|
992
1054
|
return void 0;
|
|
993
1055
|
}
|
|
1056
|
+
function parseEndUserInfo(value) {
|
|
1057
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1058
|
+
return void 0;
|
|
1059
|
+
}
|
|
1060
|
+
const v = value;
|
|
1061
|
+
if (typeof v.id !== "string" || v.id.length === 0)
|
|
1062
|
+
return void 0;
|
|
1063
|
+
const isObj = (x) => !!x && typeof x === "object" && !Array.isArray(x);
|
|
1064
|
+
return {
|
|
1065
|
+
id: v.id,
|
|
1066
|
+
verified: v.verified === true,
|
|
1067
|
+
...isObj(v.attributes) ? { attributes: v.attributes } : {},
|
|
1068
|
+
...isObj(v.sessionContext) ? { sessionContext: v.sessionContext } : {}
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
function parseEndUserEnv(raw) {
|
|
1072
|
+
if (!raw?.trim())
|
|
1073
|
+
return void 0;
|
|
1074
|
+
try {
|
|
1075
|
+
return parseEndUserInfo(JSON.parse(raw));
|
|
1076
|
+
} catch {
|
|
1077
|
+
return void 0;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
994
1080
|
async function runErrorHook(handlers, ctx) {
|
|
995
1081
|
if (!handlers.errorHook)
|
|
996
1082
|
return;
|
|
@@ -205,6 +205,12 @@ function isConversationHistoryControlAckMessage(value) {
|
|
|
205
205
|
const msg = value;
|
|
206
206
|
return msg.type === "conversation_history_control_ack" && typeof msg.requestId === "string";
|
|
207
207
|
}
|
|
208
|
+
function isUserHistoryResponseMessage(value) {
|
|
209
|
+
if (!value || typeof value !== "object")
|
|
210
|
+
return false;
|
|
211
|
+
const msg = value;
|
|
212
|
+
return msg.type === "user_history_response" && typeof msg.requestId === "string";
|
|
213
|
+
}
|
|
208
214
|
function isMixControlAckMessage(value) {
|
|
209
215
|
if (!value || typeof value !== "object")
|
|
210
216
|
return false;
|
|
@@ -277,7 +283,7 @@ function isParentMessage(value) {
|
|
|
277
283
|
if (!value || typeof value !== "object")
|
|
278
284
|
return false;
|
|
279
285
|
const msg = value;
|
|
280
|
-
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";
|
|
286
|
+
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 === "user_identified" || msg.type === "user_history_response" || 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";
|
|
281
287
|
}
|
|
282
288
|
function isSessionScopedParentMessage(value) {
|
|
283
289
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -292,6 +298,7 @@ function parseDataChannelPayload(raw) {
|
|
|
292
298
|
var peerEnvBySessionId = /* @__PURE__ */ new Map();
|
|
293
299
|
var recordingAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
294
300
|
var conversationHistoryAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
301
|
+
var endUserBySessionId = /* @__PURE__ */ new Map();
|
|
295
302
|
var mixAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
296
303
|
var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
297
304
|
var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
|
|
@@ -380,6 +387,7 @@ function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
|
380
387
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
381
388
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
382
389
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
390
|
+
var pendingUserHistory = /* @__PURE__ */ new Map();
|
|
383
391
|
var pendingPlayAcks = /* @__PURE__ */ new Map();
|
|
384
392
|
var pendingPlayStatusAcks = /* @__PURE__ */ new Map();
|
|
385
393
|
var pendingPlayStopAcks = /* @__PURE__ */ new Map();
|
|
@@ -414,6 +422,27 @@ function handleConversationHistoryControlAck(message) {
|
|
|
414
422
|
requestId: message.requestId
|
|
415
423
|
});
|
|
416
424
|
}
|
|
425
|
+
function handleUserHistoryResponse(message) {
|
|
426
|
+
const pending = pendingUserHistory.get(message.requestId);
|
|
427
|
+
if (!pending)
|
|
428
|
+
return;
|
|
429
|
+
clearTimeout(pending.timer);
|
|
430
|
+
pendingUserHistory.delete(message.requestId);
|
|
431
|
+
pending.resolve({
|
|
432
|
+
ok: message.ok,
|
|
433
|
+
...message.reason ? { reason: message.reason } : {},
|
|
434
|
+
conversations: Array.isArray(message.conversations) ? message.conversations : []
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
function clearPendingUserHistoryForSession(sessionId, reason) {
|
|
438
|
+
for (const [requestId, pending] of pendingUserHistory) {
|
|
439
|
+
if (pending.sessionId !== sessionId)
|
|
440
|
+
continue;
|
|
441
|
+
clearTimeout(pending.timer);
|
|
442
|
+
pendingUserHistory.delete(requestId);
|
|
443
|
+
pending.resolve({ ok: false, reason, conversations: [] });
|
|
444
|
+
}
|
|
445
|
+
}
|
|
417
446
|
function handlePlayAck(message) {
|
|
418
447
|
const pending = pendingPlayAcks.get(message.requestId);
|
|
419
448
|
if (!pending)
|
|
@@ -734,6 +763,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
734
763
|
peerEnvBySessionId.set(message.sessionId, message.env);
|
|
735
764
|
recordingAvailableBySessionId.set(message.sessionId, message.recordingAvailable ?? false);
|
|
736
765
|
conversationHistoryAvailableBySessionId.set(message.sessionId, message.conversationHistoryAvailable ?? false);
|
|
766
|
+
const startUser = parseEndUserInfo(message.endUser) ?? parseEndUserEnv(message.env?.AGENT_END_USER);
|
|
767
|
+
if (startUser)
|
|
768
|
+
endUserBySessionId.set(message.sessionId, startUser);
|
|
769
|
+
else
|
|
770
|
+
endUserBySessionId.delete(message.sessionId);
|
|
737
771
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
738
772
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
739
773
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
@@ -751,6 +785,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
751
785
|
await (handlers.onClientJoin ?? handlers.onSessionStart)?.({
|
|
752
786
|
sessionId: message.sessionId,
|
|
753
787
|
env: message.env,
|
|
788
|
+
...startUser ? { user: startUser } : {},
|
|
754
789
|
recordingAvailable: message.recordingAvailable ?? false,
|
|
755
790
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
756
791
|
mixAvailable: message.mixAvailable ?? false,
|
|
@@ -764,6 +799,27 @@ async function handleParentMessage(message, handlers) {
|
|
|
764
799
|
sessionId: message.sessionId
|
|
765
800
|
});
|
|
766
801
|
break;
|
|
802
|
+
case "user_identified": {
|
|
803
|
+
if (endedSessionIds.has(message.sessionId))
|
|
804
|
+
break;
|
|
805
|
+
const user = parseEndUserInfo(message.endUser);
|
|
806
|
+
if (!user)
|
|
807
|
+
break;
|
|
808
|
+
endUserBySessionId.set(message.sessionId, user);
|
|
809
|
+
await handlers.onUserIdentified?.({
|
|
810
|
+
sessionId: message.sessionId,
|
|
811
|
+
env: peerEnvBySessionId.get(message.sessionId) ?? {},
|
|
812
|
+
recordingAvailable: recordingAvailableBySessionId.get(message.sessionId) ?? false,
|
|
813
|
+
conversationHistoryAvailable: conversationHistoryAvailableBySessionId.get(message.sessionId) ?? false,
|
|
814
|
+
mixAvailable: mixAvailableBySessionId.get(message.sessionId) ?? false,
|
|
815
|
+
voiceLanguageSwitchAvailable: voiceLanguageSwitchAvailableBySessionId.get(message.sessionId) ?? false,
|
|
816
|
+
ttsPoseAvailable: ttsPoseAvailableBySessionId.get(message.sessionId) ?? false,
|
|
817
|
+
speakControlAvailable: speakControlAvailableBySessionId.get(message.sessionId) ?? false,
|
|
818
|
+
sessionTimeLimitAvailable: sessionTimeLimitAvailableBySessionId.get(message.sessionId) ?? false,
|
|
819
|
+
user
|
|
820
|
+
});
|
|
821
|
+
break;
|
|
822
|
+
}
|
|
767
823
|
case "speech_event":
|
|
768
824
|
await handlers.onSpeechEvent?.({ sessionId: message.sessionId }, message.event);
|
|
769
825
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
@@ -826,6 +882,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
826
882
|
peerEnvBySessionId.delete(message.sessionId);
|
|
827
883
|
recordingAvailableBySessionId.delete(message.sessionId);
|
|
828
884
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
885
|
+
endUserBySessionId.delete(message.sessionId);
|
|
829
886
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
830
887
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
831
888
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
@@ -860,6 +917,10 @@ function defineAgent(handlers) {
|
|
|
860
917
|
handleConversationHistoryControlAck(message);
|
|
861
918
|
return;
|
|
862
919
|
}
|
|
920
|
+
if (isUserHistoryResponseMessage(message)) {
|
|
921
|
+
handleUserHistoryResponse(message);
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
863
924
|
if (isSpeakResultMessage(message)) {
|
|
864
925
|
handleSpeakResult(message);
|
|
865
926
|
return;
|
|
@@ -919,6 +980,7 @@ function defineAgent(handlers) {
|
|
|
919
980
|
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
920
981
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
921
982
|
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
983
|
+
clearPendingUserHistoryForSession(message.sessionId, "session_ended");
|
|
922
984
|
inboundBySession.clear(message.sessionId);
|
|
923
985
|
}
|
|
924
986
|
if (message.type === "session_start") {
|
|
@@ -991,6 +1053,30 @@ function parseCustomerContext(raw) {
|
|
|
991
1053
|
}
|
|
992
1054
|
return void 0;
|
|
993
1055
|
}
|
|
1056
|
+
function parseEndUserInfo(value) {
|
|
1057
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1058
|
+
return void 0;
|
|
1059
|
+
}
|
|
1060
|
+
const v = value;
|
|
1061
|
+
if (typeof v.id !== "string" || v.id.length === 0)
|
|
1062
|
+
return void 0;
|
|
1063
|
+
const isObj = (x) => !!x && typeof x === "object" && !Array.isArray(x);
|
|
1064
|
+
return {
|
|
1065
|
+
id: v.id,
|
|
1066
|
+
verified: v.verified === true,
|
|
1067
|
+
...isObj(v.attributes) ? { attributes: v.attributes } : {},
|
|
1068
|
+
...isObj(v.sessionContext) ? { sessionContext: v.sessionContext } : {}
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
function parseEndUserEnv(raw) {
|
|
1072
|
+
if (!raw?.trim())
|
|
1073
|
+
return void 0;
|
|
1074
|
+
try {
|
|
1075
|
+
return parseEndUserInfo(JSON.parse(raw));
|
|
1076
|
+
} catch {
|
|
1077
|
+
return void 0;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
994
1080
|
async function runErrorHook(handlers, ctx) {
|
|
995
1081
|
if (!handlers.errorHook)
|
|
996
1082
|
return;
|
|
@@ -208,6 +208,12 @@ function isConversationHistoryControlAckMessage(value) {
|
|
|
208
208
|
const msg = value;
|
|
209
209
|
return msg.type === "conversation_history_control_ack" && typeof msg.requestId === "string";
|
|
210
210
|
}
|
|
211
|
+
function isUserHistoryResponseMessage(value) {
|
|
212
|
+
if (!value || typeof value !== "object")
|
|
213
|
+
return false;
|
|
214
|
+
const msg = value;
|
|
215
|
+
return msg.type === "user_history_response" && typeof msg.requestId === "string";
|
|
216
|
+
}
|
|
211
217
|
function isMixControlAckMessage(value) {
|
|
212
218
|
if (!value || typeof value !== "object")
|
|
213
219
|
return false;
|
|
@@ -280,7 +286,7 @@ function isParentMessage(value) {
|
|
|
280
286
|
if (!value || typeof value !== "object")
|
|
281
287
|
return false;
|
|
282
288
|
const msg = value;
|
|
283
|
-
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";
|
|
289
|
+
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 === "user_identified" || msg.type === "user_history_response" || 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";
|
|
284
290
|
}
|
|
285
291
|
function isSessionScopedParentMessage(value) {
|
|
286
292
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -295,6 +301,7 @@ function parseDataChannelPayload(raw) {
|
|
|
295
301
|
var peerEnvBySessionId = /* @__PURE__ */ new Map();
|
|
296
302
|
var recordingAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
297
303
|
var conversationHistoryAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
304
|
+
var endUserBySessionId = /* @__PURE__ */ new Map();
|
|
298
305
|
var mixAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
299
306
|
var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
300
307
|
var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
|
|
@@ -383,6 +390,7 @@ function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
|
383
390
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
384
391
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
385
392
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
393
|
+
var pendingUserHistory = /* @__PURE__ */ new Map();
|
|
386
394
|
var pendingPlayAcks = /* @__PURE__ */ new Map();
|
|
387
395
|
var pendingPlayStatusAcks = /* @__PURE__ */ new Map();
|
|
388
396
|
var pendingPlayStopAcks = /* @__PURE__ */ new Map();
|
|
@@ -417,6 +425,27 @@ function handleConversationHistoryControlAck(message) {
|
|
|
417
425
|
requestId: message.requestId
|
|
418
426
|
});
|
|
419
427
|
}
|
|
428
|
+
function handleUserHistoryResponse(message) {
|
|
429
|
+
const pending = pendingUserHistory.get(message.requestId);
|
|
430
|
+
if (!pending)
|
|
431
|
+
return;
|
|
432
|
+
clearTimeout(pending.timer);
|
|
433
|
+
pendingUserHistory.delete(message.requestId);
|
|
434
|
+
pending.resolve({
|
|
435
|
+
ok: message.ok,
|
|
436
|
+
...message.reason ? { reason: message.reason } : {},
|
|
437
|
+
conversations: Array.isArray(message.conversations) ? message.conversations : []
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
function clearPendingUserHistoryForSession(sessionId, reason) {
|
|
441
|
+
for (const [requestId, pending] of pendingUserHistory) {
|
|
442
|
+
if (pending.sessionId !== sessionId)
|
|
443
|
+
continue;
|
|
444
|
+
clearTimeout(pending.timer);
|
|
445
|
+
pendingUserHistory.delete(requestId);
|
|
446
|
+
pending.resolve({ ok: false, reason, conversations: [] });
|
|
447
|
+
}
|
|
448
|
+
}
|
|
420
449
|
function handlePlayAck(message) {
|
|
421
450
|
const pending = pendingPlayAcks.get(message.requestId);
|
|
422
451
|
if (!pending)
|
|
@@ -737,6 +766,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
737
766
|
peerEnvBySessionId.set(message.sessionId, message.env);
|
|
738
767
|
recordingAvailableBySessionId.set(message.sessionId, message.recordingAvailable ?? false);
|
|
739
768
|
conversationHistoryAvailableBySessionId.set(message.sessionId, message.conversationHistoryAvailable ?? false);
|
|
769
|
+
const startUser = parseEndUserInfo(message.endUser) ?? parseEndUserEnv(message.env?.AGENT_END_USER);
|
|
770
|
+
if (startUser)
|
|
771
|
+
endUserBySessionId.set(message.sessionId, startUser);
|
|
772
|
+
else
|
|
773
|
+
endUserBySessionId.delete(message.sessionId);
|
|
740
774
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
741
775
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
742
776
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
@@ -754,6 +788,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
754
788
|
await (handlers.onClientJoin ?? handlers.onSessionStart)?.({
|
|
755
789
|
sessionId: message.sessionId,
|
|
756
790
|
env: message.env,
|
|
791
|
+
...startUser ? { user: startUser } : {},
|
|
757
792
|
recordingAvailable: message.recordingAvailable ?? false,
|
|
758
793
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
759
794
|
mixAvailable: message.mixAvailable ?? false,
|
|
@@ -767,6 +802,27 @@ async function handleParentMessage(message, handlers) {
|
|
|
767
802
|
sessionId: message.sessionId
|
|
768
803
|
});
|
|
769
804
|
break;
|
|
805
|
+
case "user_identified": {
|
|
806
|
+
if (endedSessionIds.has(message.sessionId))
|
|
807
|
+
break;
|
|
808
|
+
const user = parseEndUserInfo(message.endUser);
|
|
809
|
+
if (!user)
|
|
810
|
+
break;
|
|
811
|
+
endUserBySessionId.set(message.sessionId, user);
|
|
812
|
+
await handlers.onUserIdentified?.({
|
|
813
|
+
sessionId: message.sessionId,
|
|
814
|
+
env: peerEnvBySessionId.get(message.sessionId) ?? {},
|
|
815
|
+
recordingAvailable: recordingAvailableBySessionId.get(message.sessionId) ?? false,
|
|
816
|
+
conversationHistoryAvailable: conversationHistoryAvailableBySessionId.get(message.sessionId) ?? false,
|
|
817
|
+
mixAvailable: mixAvailableBySessionId.get(message.sessionId) ?? false,
|
|
818
|
+
voiceLanguageSwitchAvailable: voiceLanguageSwitchAvailableBySessionId.get(message.sessionId) ?? false,
|
|
819
|
+
ttsPoseAvailable: ttsPoseAvailableBySessionId.get(message.sessionId) ?? false,
|
|
820
|
+
speakControlAvailable: speakControlAvailableBySessionId.get(message.sessionId) ?? false,
|
|
821
|
+
sessionTimeLimitAvailable: sessionTimeLimitAvailableBySessionId.get(message.sessionId) ?? false,
|
|
822
|
+
user
|
|
823
|
+
});
|
|
824
|
+
break;
|
|
825
|
+
}
|
|
770
826
|
case "speech_event":
|
|
771
827
|
await handlers.onSpeechEvent?.({ sessionId: message.sessionId }, message.event);
|
|
772
828
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
@@ -829,6 +885,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
829
885
|
peerEnvBySessionId.delete(message.sessionId);
|
|
830
886
|
recordingAvailableBySessionId.delete(message.sessionId);
|
|
831
887
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
888
|
+
endUserBySessionId.delete(message.sessionId);
|
|
832
889
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
833
890
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
834
891
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
@@ -863,6 +920,10 @@ function defineAgent(handlers) {
|
|
|
863
920
|
handleConversationHistoryControlAck(message);
|
|
864
921
|
return;
|
|
865
922
|
}
|
|
923
|
+
if (isUserHistoryResponseMessage(message)) {
|
|
924
|
+
handleUserHistoryResponse(message);
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
866
927
|
if (isSpeakResultMessage(message)) {
|
|
867
928
|
handleSpeakResult(message);
|
|
868
929
|
return;
|
|
@@ -922,6 +983,7 @@ function defineAgent(handlers) {
|
|
|
922
983
|
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
923
984
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
924
985
|
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
986
|
+
clearPendingUserHistoryForSession(message.sessionId, "session_ended");
|
|
925
987
|
inboundBySession.clear(message.sessionId);
|
|
926
988
|
}
|
|
927
989
|
if (message.type === "session_start") {
|
|
@@ -994,6 +1056,30 @@ function parseCustomerContext(raw) {
|
|
|
994
1056
|
}
|
|
995
1057
|
return void 0;
|
|
996
1058
|
}
|
|
1059
|
+
function parseEndUserInfo(value) {
|
|
1060
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1061
|
+
return void 0;
|
|
1062
|
+
}
|
|
1063
|
+
const v = value;
|
|
1064
|
+
if (typeof v.id !== "string" || v.id.length === 0)
|
|
1065
|
+
return void 0;
|
|
1066
|
+
const isObj = (x) => !!x && typeof x === "object" && !Array.isArray(x);
|
|
1067
|
+
return {
|
|
1068
|
+
id: v.id,
|
|
1069
|
+
verified: v.verified === true,
|
|
1070
|
+
...isObj(v.attributes) ? { attributes: v.attributes } : {},
|
|
1071
|
+
...isObj(v.sessionContext) ? { sessionContext: v.sessionContext } : {}
|
|
1072
|
+
};
|
|
1073
|
+
}
|
|
1074
|
+
function parseEndUserEnv(raw) {
|
|
1075
|
+
if (!raw?.trim())
|
|
1076
|
+
return void 0;
|
|
1077
|
+
try {
|
|
1078
|
+
return parseEndUserInfo(JSON.parse(raw));
|
|
1079
|
+
} catch {
|
|
1080
|
+
return void 0;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
997
1083
|
async function runErrorHook(handlers, ctx) {
|
|
998
1084
|
if (!handlers.errorHook)
|
|
999
1085
|
return;
|