@voicethere/agent 0.26.0 → 0.27.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 +23 -0
- package/dist/agent.js +78 -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/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 +95 -4
- 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
|
@@ -10960,6 +10960,12 @@ function isConversationHistoryControlAckMessage(value) {
|
|
|
10960
10960
|
const msg = value;
|
|
10961
10961
|
return msg.type === "conversation_history_control_ack" && typeof msg.requestId === "string";
|
|
10962
10962
|
}
|
|
10963
|
+
function isUserHistoryResponseMessage(value) {
|
|
10964
|
+
if (!value || typeof value !== "object")
|
|
10965
|
+
return false;
|
|
10966
|
+
const msg = value;
|
|
10967
|
+
return msg.type === "user_history_response" && typeof msg.requestId === "string";
|
|
10968
|
+
}
|
|
10963
10969
|
function isMixControlAckMessage(value) {
|
|
10964
10970
|
if (!value || typeof value !== "object")
|
|
10965
10971
|
return false;
|
|
@@ -11032,7 +11038,7 @@ function isParentMessage(value) {
|
|
|
11032
11038
|
if (!value || typeof value !== "object")
|
|
11033
11039
|
return false;
|
|
11034
11040
|
const msg = value;
|
|
11035
|
-
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";
|
|
11041
|
+
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";
|
|
11036
11042
|
}
|
|
11037
11043
|
function isSessionScopedParentMessage(value) {
|
|
11038
11044
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -11047,6 +11053,7 @@ function parseDataChannelPayload(raw) {
|
|
|
11047
11053
|
var peerEnvBySessionId = /* @__PURE__ */ new Map();
|
|
11048
11054
|
var recordingAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
11049
11055
|
var conversationHistoryAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
11056
|
+
var endUserBySessionId = /* @__PURE__ */ new Map();
|
|
11050
11057
|
var mixAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
11051
11058
|
var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
11052
11059
|
var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
|
|
@@ -11134,6 +11141,7 @@ function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
|
11134
11141
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
11135
11142
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
11136
11143
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
11144
|
+
var pendingUserHistory = /* @__PURE__ */ new Map();
|
|
11137
11145
|
var pendingPlayAcks = /* @__PURE__ */ new Map();
|
|
11138
11146
|
var pendingPlayStatusAcks = /* @__PURE__ */ new Map();
|
|
11139
11147
|
var pendingPlayStopAcks = /* @__PURE__ */ new Map();
|
|
@@ -11168,6 +11176,27 @@ function handleConversationHistoryControlAck(message) {
|
|
|
11168
11176
|
requestId: message.requestId
|
|
11169
11177
|
});
|
|
11170
11178
|
}
|
|
11179
|
+
function handleUserHistoryResponse(message) {
|
|
11180
|
+
const pending = pendingUserHistory.get(message.requestId);
|
|
11181
|
+
if (!pending)
|
|
11182
|
+
return;
|
|
11183
|
+
clearTimeout(pending.timer);
|
|
11184
|
+
pendingUserHistory.delete(message.requestId);
|
|
11185
|
+
pending.resolve({
|
|
11186
|
+
ok: message.ok,
|
|
11187
|
+
...message.reason ? { reason: message.reason } : {},
|
|
11188
|
+
conversations: Array.isArray(message.conversations) ? message.conversations : []
|
|
11189
|
+
});
|
|
11190
|
+
}
|
|
11191
|
+
function clearPendingUserHistoryForSession(sessionId, reason) {
|
|
11192
|
+
for (const [requestId, pending] of pendingUserHistory) {
|
|
11193
|
+
if (pending.sessionId !== sessionId)
|
|
11194
|
+
continue;
|
|
11195
|
+
clearTimeout(pending.timer);
|
|
11196
|
+
pendingUserHistory.delete(requestId);
|
|
11197
|
+
pending.resolve({ ok: false, reason, conversations: [] });
|
|
11198
|
+
}
|
|
11199
|
+
}
|
|
11171
11200
|
function handlePlayAck(message) {
|
|
11172
11201
|
const pending = pendingPlayAcks.get(message.requestId);
|
|
11173
11202
|
if (!pending)
|
|
@@ -11485,6 +11514,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
11485
11514
|
peerEnvBySessionId.set(message.sessionId, message.env);
|
|
11486
11515
|
recordingAvailableBySessionId.set(message.sessionId, message.recordingAvailable ?? false);
|
|
11487
11516
|
conversationHistoryAvailableBySessionId.set(message.sessionId, message.conversationHistoryAvailable ?? false);
|
|
11517
|
+
const startUser = parseEndUserInfo(message.endUser) ?? parseEndUserEnv(message.env?.AGENT_END_USER);
|
|
11518
|
+
if (startUser)
|
|
11519
|
+
endUserBySessionId.set(message.sessionId, startUser);
|
|
11520
|
+
else
|
|
11521
|
+
endUserBySessionId.delete(message.sessionId);
|
|
11488
11522
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
11489
11523
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
11490
11524
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
@@ -11502,6 +11536,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
11502
11536
|
await (handlers.onClientJoin ?? handlers.onSessionStart)?.({
|
|
11503
11537
|
sessionId: message.sessionId,
|
|
11504
11538
|
env: message.env,
|
|
11539
|
+
...startUser ? { user: startUser } : {},
|
|
11505
11540
|
recordingAvailable: message.recordingAvailable ?? false,
|
|
11506
11541
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
11507
11542
|
mixAvailable: message.mixAvailable ?? false,
|
|
@@ -11515,6 +11550,27 @@ async function handleParentMessage(message, handlers) {
|
|
|
11515
11550
|
sessionId: message.sessionId
|
|
11516
11551
|
});
|
|
11517
11552
|
break;
|
|
11553
|
+
case "user_identified": {
|
|
11554
|
+
if (endedSessionIds.has(message.sessionId))
|
|
11555
|
+
break;
|
|
11556
|
+
const user = parseEndUserInfo(message.endUser);
|
|
11557
|
+
if (!user)
|
|
11558
|
+
break;
|
|
11559
|
+
endUserBySessionId.set(message.sessionId, user);
|
|
11560
|
+
await handlers.onUserIdentified?.({
|
|
11561
|
+
sessionId: message.sessionId,
|
|
11562
|
+
env: peerEnvBySessionId.get(message.sessionId) ?? {},
|
|
11563
|
+
recordingAvailable: recordingAvailableBySessionId.get(message.sessionId) ?? false,
|
|
11564
|
+
conversationHistoryAvailable: conversationHistoryAvailableBySessionId.get(message.sessionId) ?? false,
|
|
11565
|
+
mixAvailable: mixAvailableBySessionId.get(message.sessionId) ?? false,
|
|
11566
|
+
voiceLanguageSwitchAvailable: voiceLanguageSwitchAvailableBySessionId.get(message.sessionId) ?? false,
|
|
11567
|
+
ttsPoseAvailable: ttsPoseAvailableBySessionId.get(message.sessionId) ?? false,
|
|
11568
|
+
speakControlAvailable: speakControlAvailableBySessionId.get(message.sessionId) ?? false,
|
|
11569
|
+
sessionTimeLimitAvailable: sessionTimeLimitAvailableBySessionId.get(message.sessionId) ?? false,
|
|
11570
|
+
user
|
|
11571
|
+
});
|
|
11572
|
+
break;
|
|
11573
|
+
}
|
|
11518
11574
|
case "speech_event":
|
|
11519
11575
|
await handlers.onSpeechEvent?.({ sessionId: message.sessionId }, message.event);
|
|
11520
11576
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
@@ -11577,6 +11633,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
11577
11633
|
peerEnvBySessionId.delete(message.sessionId);
|
|
11578
11634
|
recordingAvailableBySessionId.delete(message.sessionId);
|
|
11579
11635
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
11636
|
+
endUserBySessionId.delete(message.sessionId);
|
|
11580
11637
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
11581
11638
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
11582
11639
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
@@ -11611,6 +11668,10 @@ function defineAgent(handlers) {
|
|
|
11611
11668
|
handleConversationHistoryControlAck(message);
|
|
11612
11669
|
return;
|
|
11613
11670
|
}
|
|
11671
|
+
if (isUserHistoryResponseMessage(message)) {
|
|
11672
|
+
handleUserHistoryResponse(message);
|
|
11673
|
+
return;
|
|
11674
|
+
}
|
|
11614
11675
|
if (isSpeakResultMessage(message)) {
|
|
11615
11676
|
handleSpeakResult(message);
|
|
11616
11677
|
return;
|
|
@@ -11670,6 +11731,7 @@ function defineAgent(handlers) {
|
|
|
11670
11731
|
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
11671
11732
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
11672
11733
|
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
11734
|
+
clearPendingUserHistoryForSession(message.sessionId, "session_ended");
|
|
11673
11735
|
inboundBySession.clear(message.sessionId);
|
|
11674
11736
|
}
|
|
11675
11737
|
if (message.type === "session_start") {
|
|
@@ -11742,6 +11804,30 @@ function parseCustomerContext(raw) {
|
|
|
11742
11804
|
}
|
|
11743
11805
|
return void 0;
|
|
11744
11806
|
}
|
|
11807
|
+
function parseEndUserInfo(value) {
|
|
11808
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
11809
|
+
return void 0;
|
|
11810
|
+
}
|
|
11811
|
+
const v = value;
|
|
11812
|
+
if (typeof v.id !== "string" || v.id.length === 0)
|
|
11813
|
+
return void 0;
|
|
11814
|
+
const isObj = (x) => !!x && typeof x === "object" && !Array.isArray(x);
|
|
11815
|
+
return {
|
|
11816
|
+
id: v.id,
|
|
11817
|
+
verified: v.verified === true,
|
|
11818
|
+
...isObj(v.attributes) ? { attributes: v.attributes } : {},
|
|
11819
|
+
...isObj(v.sessionContext) ? { sessionContext: v.sessionContext } : {}
|
|
11820
|
+
};
|
|
11821
|
+
}
|
|
11822
|
+
function parseEndUserEnv(raw) {
|
|
11823
|
+
if (!raw?.trim())
|
|
11824
|
+
return void 0;
|
|
11825
|
+
try {
|
|
11826
|
+
return parseEndUserInfo(JSON.parse(raw));
|
|
11827
|
+
} catch {
|
|
11828
|
+
return void 0;
|
|
11829
|
+
}
|
|
11830
|
+
}
|
|
11745
11831
|
async function runErrorHook(handlers, ctx) {
|
|
11746
11832
|
if (!handlers.errorHook)
|
|
11747
11833
|
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();
|
|
@@ -379,6 +386,7 @@ function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
|
379
386
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
380
387
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
381
388
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
389
|
+
var pendingUserHistory = /* @__PURE__ */ new Map();
|
|
382
390
|
var pendingPlayAcks = /* @__PURE__ */ new Map();
|
|
383
391
|
var pendingPlayStatusAcks = /* @__PURE__ */ new Map();
|
|
384
392
|
var pendingPlayStopAcks = /* @__PURE__ */ new Map();
|
|
@@ -413,6 +421,27 @@ function handleConversationHistoryControlAck(message) {
|
|
|
413
421
|
requestId: message.requestId
|
|
414
422
|
});
|
|
415
423
|
}
|
|
424
|
+
function handleUserHistoryResponse(message) {
|
|
425
|
+
const pending = pendingUserHistory.get(message.requestId);
|
|
426
|
+
if (!pending)
|
|
427
|
+
return;
|
|
428
|
+
clearTimeout(pending.timer);
|
|
429
|
+
pendingUserHistory.delete(message.requestId);
|
|
430
|
+
pending.resolve({
|
|
431
|
+
ok: message.ok,
|
|
432
|
+
...message.reason ? { reason: message.reason } : {},
|
|
433
|
+
conversations: Array.isArray(message.conversations) ? message.conversations : []
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
function clearPendingUserHistoryForSession(sessionId, reason) {
|
|
437
|
+
for (const [requestId, pending] of pendingUserHistory) {
|
|
438
|
+
if (pending.sessionId !== sessionId)
|
|
439
|
+
continue;
|
|
440
|
+
clearTimeout(pending.timer);
|
|
441
|
+
pendingUserHistory.delete(requestId);
|
|
442
|
+
pending.resolve({ ok: false, reason, conversations: [] });
|
|
443
|
+
}
|
|
444
|
+
}
|
|
416
445
|
function handlePlayAck(message) {
|
|
417
446
|
const pending = pendingPlayAcks.get(message.requestId);
|
|
418
447
|
if (!pending)
|
|
@@ -730,6 +759,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
730
759
|
peerEnvBySessionId.set(message.sessionId, message.env);
|
|
731
760
|
recordingAvailableBySessionId.set(message.sessionId, message.recordingAvailable ?? false);
|
|
732
761
|
conversationHistoryAvailableBySessionId.set(message.sessionId, message.conversationHistoryAvailable ?? false);
|
|
762
|
+
const startUser = parseEndUserInfo(message.endUser) ?? parseEndUserEnv(message.env?.AGENT_END_USER);
|
|
763
|
+
if (startUser)
|
|
764
|
+
endUserBySessionId.set(message.sessionId, startUser);
|
|
765
|
+
else
|
|
766
|
+
endUserBySessionId.delete(message.sessionId);
|
|
733
767
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
734
768
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
735
769
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
@@ -747,6 +781,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
747
781
|
await (handlers.onClientJoin ?? handlers.onSessionStart)?.({
|
|
748
782
|
sessionId: message.sessionId,
|
|
749
783
|
env: message.env,
|
|
784
|
+
...startUser ? { user: startUser } : {},
|
|
750
785
|
recordingAvailable: message.recordingAvailable ?? false,
|
|
751
786
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
752
787
|
mixAvailable: message.mixAvailable ?? false,
|
|
@@ -760,6 +795,27 @@ async function handleParentMessage(message, handlers) {
|
|
|
760
795
|
sessionId: message.sessionId
|
|
761
796
|
});
|
|
762
797
|
break;
|
|
798
|
+
case "user_identified": {
|
|
799
|
+
if (endedSessionIds.has(message.sessionId))
|
|
800
|
+
break;
|
|
801
|
+
const user = parseEndUserInfo(message.endUser);
|
|
802
|
+
if (!user)
|
|
803
|
+
break;
|
|
804
|
+
endUserBySessionId.set(message.sessionId, user);
|
|
805
|
+
await handlers.onUserIdentified?.({
|
|
806
|
+
sessionId: message.sessionId,
|
|
807
|
+
env: peerEnvBySessionId.get(message.sessionId) ?? {},
|
|
808
|
+
recordingAvailable: recordingAvailableBySessionId.get(message.sessionId) ?? false,
|
|
809
|
+
conversationHistoryAvailable: conversationHistoryAvailableBySessionId.get(message.sessionId) ?? false,
|
|
810
|
+
mixAvailable: mixAvailableBySessionId.get(message.sessionId) ?? false,
|
|
811
|
+
voiceLanguageSwitchAvailable: voiceLanguageSwitchAvailableBySessionId.get(message.sessionId) ?? false,
|
|
812
|
+
ttsPoseAvailable: ttsPoseAvailableBySessionId.get(message.sessionId) ?? false,
|
|
813
|
+
speakControlAvailable: speakControlAvailableBySessionId.get(message.sessionId) ?? false,
|
|
814
|
+
sessionTimeLimitAvailable: sessionTimeLimitAvailableBySessionId.get(message.sessionId) ?? false,
|
|
815
|
+
user
|
|
816
|
+
});
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
763
819
|
case "speech_event":
|
|
764
820
|
await handlers.onSpeechEvent?.({ sessionId: message.sessionId }, message.event);
|
|
765
821
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
@@ -822,6 +878,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
822
878
|
peerEnvBySessionId.delete(message.sessionId);
|
|
823
879
|
recordingAvailableBySessionId.delete(message.sessionId);
|
|
824
880
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
881
|
+
endUserBySessionId.delete(message.sessionId);
|
|
825
882
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
826
883
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
827
884
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
@@ -856,6 +913,10 @@ function defineAgent(handlers) {
|
|
|
856
913
|
handleConversationHistoryControlAck(message);
|
|
857
914
|
return;
|
|
858
915
|
}
|
|
916
|
+
if (isUserHistoryResponseMessage(message)) {
|
|
917
|
+
handleUserHistoryResponse(message);
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
859
920
|
if (isSpeakResultMessage(message)) {
|
|
860
921
|
handleSpeakResult(message);
|
|
861
922
|
return;
|
|
@@ -915,6 +976,7 @@ function defineAgent(handlers) {
|
|
|
915
976
|
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
916
977
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
917
978
|
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
979
|
+
clearPendingUserHistoryForSession(message.sessionId, "session_ended");
|
|
918
980
|
inboundBySession.clear(message.sessionId);
|
|
919
981
|
}
|
|
920
982
|
if (message.type === "session_start") {
|
|
@@ -987,6 +1049,30 @@ function parseCustomerContext(raw) {
|
|
|
987
1049
|
}
|
|
988
1050
|
return void 0;
|
|
989
1051
|
}
|
|
1052
|
+
function parseEndUserInfo(value) {
|
|
1053
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1054
|
+
return void 0;
|
|
1055
|
+
}
|
|
1056
|
+
const v = value;
|
|
1057
|
+
if (typeof v.id !== "string" || v.id.length === 0)
|
|
1058
|
+
return void 0;
|
|
1059
|
+
const isObj = (x) => !!x && typeof x === "object" && !Array.isArray(x);
|
|
1060
|
+
return {
|
|
1061
|
+
id: v.id,
|
|
1062
|
+
verified: v.verified === true,
|
|
1063
|
+
...isObj(v.attributes) ? { attributes: v.attributes } : {},
|
|
1064
|
+
...isObj(v.sessionContext) ? { sessionContext: v.sessionContext } : {}
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
1067
|
+
function parseEndUserEnv(raw) {
|
|
1068
|
+
if (!raw?.trim())
|
|
1069
|
+
return void 0;
|
|
1070
|
+
try {
|
|
1071
|
+
return parseEndUserInfo(JSON.parse(raw));
|
|
1072
|
+
} catch {
|
|
1073
|
+
return void 0;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
990
1076
|
async function runErrorHook(handlers, ctx) {
|
|
991
1077
|
if (!handlers.errorHook)
|
|
992
1078
|
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();
|
|
@@ -379,6 +386,7 @@ function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
|
379
386
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
380
387
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
381
388
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
389
|
+
var pendingUserHistory = /* @__PURE__ */ new Map();
|
|
382
390
|
var pendingPlayAcks = /* @__PURE__ */ new Map();
|
|
383
391
|
var pendingPlayStatusAcks = /* @__PURE__ */ new Map();
|
|
384
392
|
var pendingPlayStopAcks = /* @__PURE__ */ new Map();
|
|
@@ -413,6 +421,27 @@ function handleConversationHistoryControlAck(message) {
|
|
|
413
421
|
requestId: message.requestId
|
|
414
422
|
});
|
|
415
423
|
}
|
|
424
|
+
function handleUserHistoryResponse(message) {
|
|
425
|
+
const pending = pendingUserHistory.get(message.requestId);
|
|
426
|
+
if (!pending)
|
|
427
|
+
return;
|
|
428
|
+
clearTimeout(pending.timer);
|
|
429
|
+
pendingUserHistory.delete(message.requestId);
|
|
430
|
+
pending.resolve({
|
|
431
|
+
ok: message.ok,
|
|
432
|
+
...message.reason ? { reason: message.reason } : {},
|
|
433
|
+
conversations: Array.isArray(message.conversations) ? message.conversations : []
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
function clearPendingUserHistoryForSession(sessionId, reason) {
|
|
437
|
+
for (const [requestId, pending] of pendingUserHistory) {
|
|
438
|
+
if (pending.sessionId !== sessionId)
|
|
439
|
+
continue;
|
|
440
|
+
clearTimeout(pending.timer);
|
|
441
|
+
pendingUserHistory.delete(requestId);
|
|
442
|
+
pending.resolve({ ok: false, reason, conversations: [] });
|
|
443
|
+
}
|
|
444
|
+
}
|
|
416
445
|
function handlePlayAck(message) {
|
|
417
446
|
const pending = pendingPlayAcks.get(message.requestId);
|
|
418
447
|
if (!pending)
|
|
@@ -730,6 +759,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
730
759
|
peerEnvBySessionId.set(message.sessionId, message.env);
|
|
731
760
|
recordingAvailableBySessionId.set(message.sessionId, message.recordingAvailable ?? false);
|
|
732
761
|
conversationHistoryAvailableBySessionId.set(message.sessionId, message.conversationHistoryAvailable ?? false);
|
|
762
|
+
const startUser = parseEndUserInfo(message.endUser) ?? parseEndUserEnv(message.env?.AGENT_END_USER);
|
|
763
|
+
if (startUser)
|
|
764
|
+
endUserBySessionId.set(message.sessionId, startUser);
|
|
765
|
+
else
|
|
766
|
+
endUserBySessionId.delete(message.sessionId);
|
|
733
767
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
734
768
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
735
769
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
@@ -747,6 +781,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
747
781
|
await (handlers.onClientJoin ?? handlers.onSessionStart)?.({
|
|
748
782
|
sessionId: message.sessionId,
|
|
749
783
|
env: message.env,
|
|
784
|
+
...startUser ? { user: startUser } : {},
|
|
750
785
|
recordingAvailable: message.recordingAvailable ?? false,
|
|
751
786
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
752
787
|
mixAvailable: message.mixAvailable ?? false,
|
|
@@ -760,6 +795,27 @@ async function handleParentMessage(message, handlers) {
|
|
|
760
795
|
sessionId: message.sessionId
|
|
761
796
|
});
|
|
762
797
|
break;
|
|
798
|
+
case "user_identified": {
|
|
799
|
+
if (endedSessionIds.has(message.sessionId))
|
|
800
|
+
break;
|
|
801
|
+
const user = parseEndUserInfo(message.endUser);
|
|
802
|
+
if (!user)
|
|
803
|
+
break;
|
|
804
|
+
endUserBySessionId.set(message.sessionId, user);
|
|
805
|
+
await handlers.onUserIdentified?.({
|
|
806
|
+
sessionId: message.sessionId,
|
|
807
|
+
env: peerEnvBySessionId.get(message.sessionId) ?? {},
|
|
808
|
+
recordingAvailable: recordingAvailableBySessionId.get(message.sessionId) ?? false,
|
|
809
|
+
conversationHistoryAvailable: conversationHistoryAvailableBySessionId.get(message.sessionId) ?? false,
|
|
810
|
+
mixAvailable: mixAvailableBySessionId.get(message.sessionId) ?? false,
|
|
811
|
+
voiceLanguageSwitchAvailable: voiceLanguageSwitchAvailableBySessionId.get(message.sessionId) ?? false,
|
|
812
|
+
ttsPoseAvailable: ttsPoseAvailableBySessionId.get(message.sessionId) ?? false,
|
|
813
|
+
speakControlAvailable: speakControlAvailableBySessionId.get(message.sessionId) ?? false,
|
|
814
|
+
sessionTimeLimitAvailable: sessionTimeLimitAvailableBySessionId.get(message.sessionId) ?? false,
|
|
815
|
+
user
|
|
816
|
+
});
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
763
819
|
case "speech_event":
|
|
764
820
|
await handlers.onSpeechEvent?.({ sessionId: message.sessionId }, message.event);
|
|
765
821
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
@@ -822,6 +878,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
822
878
|
peerEnvBySessionId.delete(message.sessionId);
|
|
823
879
|
recordingAvailableBySessionId.delete(message.sessionId);
|
|
824
880
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
881
|
+
endUserBySessionId.delete(message.sessionId);
|
|
825
882
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
826
883
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
827
884
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
@@ -856,6 +913,10 @@ function defineAgent(handlers) {
|
|
|
856
913
|
handleConversationHistoryControlAck(message);
|
|
857
914
|
return;
|
|
858
915
|
}
|
|
916
|
+
if (isUserHistoryResponseMessage(message)) {
|
|
917
|
+
handleUserHistoryResponse(message);
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
859
920
|
if (isSpeakResultMessage(message)) {
|
|
860
921
|
handleSpeakResult(message);
|
|
861
922
|
return;
|
|
@@ -915,6 +976,7 @@ function defineAgent(handlers) {
|
|
|
915
976
|
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
916
977
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
917
978
|
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
979
|
+
clearPendingUserHistoryForSession(message.sessionId, "session_ended");
|
|
918
980
|
inboundBySession.clear(message.sessionId);
|
|
919
981
|
}
|
|
920
982
|
if (message.type === "session_start") {
|
|
@@ -987,6 +1049,30 @@ function parseCustomerContext(raw) {
|
|
|
987
1049
|
}
|
|
988
1050
|
return void 0;
|
|
989
1051
|
}
|
|
1052
|
+
function parseEndUserInfo(value) {
|
|
1053
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1054
|
+
return void 0;
|
|
1055
|
+
}
|
|
1056
|
+
const v = value;
|
|
1057
|
+
if (typeof v.id !== "string" || v.id.length === 0)
|
|
1058
|
+
return void 0;
|
|
1059
|
+
const isObj = (x) => !!x && typeof x === "object" && !Array.isArray(x);
|
|
1060
|
+
return {
|
|
1061
|
+
id: v.id,
|
|
1062
|
+
verified: v.verified === true,
|
|
1063
|
+
...isObj(v.attributes) ? { attributes: v.attributes } : {},
|
|
1064
|
+
...isObj(v.sessionContext) ? { sessionContext: v.sessionContext } : {}
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
1067
|
+
function parseEndUserEnv(raw) {
|
|
1068
|
+
if (!raw?.trim())
|
|
1069
|
+
return void 0;
|
|
1070
|
+
try {
|
|
1071
|
+
return parseEndUserInfo(JSON.parse(raw));
|
|
1072
|
+
} catch {
|
|
1073
|
+
return void 0;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
990
1076
|
async function runErrorHook(handlers, ctx) {
|
|
991
1077
|
if (!handlers.errorHook)
|
|
992
1078
|
return;
|
package/eject/llm/loop.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copied from @voicethere/agent 0.
|
|
1
|
+
// Copied from @voicethere/agent 0.27.0. Package updates no longer change this file.
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import {
|
|
4
4
|
type AgentHandlers,
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
agentLog,
|
|
7
7
|
announce,
|
|
8
8
|
cancelSpeech,
|
|
9
|
+
getSessionUser,
|
|
9
10
|
parseChatText,
|
|
10
11
|
sendToClient,
|
|
11
12
|
type SessionContext,
|
|
@@ -28,6 +29,7 @@ import {
|
|
|
28
29
|
type CustomTool,
|
|
29
30
|
DEFAULT_EMBEDDING_DIMS,
|
|
30
31
|
DEFAULT_EMBEDDING_MODEL,
|
|
32
|
+
endUserTools,
|
|
31
33
|
type ExtractedLink,
|
|
32
34
|
type KnowledgeBase,
|
|
33
35
|
type KnowledgeDocument,
|
|
@@ -65,6 +67,11 @@ export interface CreateLlmAgentOptions {
|
|
|
65
67
|
config: unknown;
|
|
66
68
|
systemPrompt: string;
|
|
67
69
|
tools?: CustomTool[];
|
|
70
|
+
/**
|
|
71
|
+
* Register `get_previous_conversations` and `get_current_user`. The first needs
|
|
72
|
+
* a verified user. Default false.
|
|
73
|
+
*/
|
|
74
|
+
endUserTools?: boolean;
|
|
68
75
|
hooks?: LlmAgentHooks;
|
|
69
76
|
/** Environment used to resolve secrets. Default `process.env`. */
|
|
70
77
|
env?: () => Record<string, string | undefined>;
|
|
@@ -766,6 +773,7 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
766
773
|
toolSources: config.toolSources ?? [],
|
|
767
774
|
customTools: [
|
|
768
775
|
...(opts.tools ?? []),
|
|
776
|
+
...(opts.endUserTools === true ? endUserTools() : []),
|
|
769
777
|
...(knowledgeTool ? [knowledgeTool] : []),
|
|
770
778
|
],
|
|
771
779
|
secretNames: [
|
|
@@ -845,6 +853,9 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
845
853
|
sessionId: s.sessionId,
|
|
846
854
|
turnId: t.turnId,
|
|
847
855
|
callId: call.id,
|
|
856
|
+
...(getSessionUser(s.sessionId)
|
|
857
|
+
? { user: getSessionUser(s.sessionId) }
|
|
858
|
+
: {}),
|
|
848
859
|
});
|
|
849
860
|
})();
|
|
850
861
|
const raced = await Promise.race([work, aborted]);
|
|
@@ -943,7 +954,11 @@ export function createLlmAgent(opts: CreateLlmAgentOptions): AgentHandlers {
|
|
|
943
954
|
model: resolveModel(config.provider, config.model),
|
|
944
955
|
system: opts.systemPrompt,
|
|
945
956
|
messages,
|
|
946
|
-
tools: registry
|
|
957
|
+
tools: registry
|
|
958
|
+
? registry
|
|
959
|
+
.list({ user: getSessionUser(s.sessionId) })
|
|
960
|
+
.map((e) => e.spec)
|
|
961
|
+
: [],
|
|
947
962
|
...(config.toolChoice === "required-first-round" && t.llmCalls === 1
|
|
948
963
|
? { toolChoice: "required" as const }
|
|
949
964
|
: {}),
|
package/package.json
CHANGED
package/templates/README.md
CHANGED
|
@@ -77,6 +77,7 @@ Each folder has its own README. Summary:
|
|
|
77
77
|
| `llm-agent` | `llm-agent/` | LLM voice and chat agent, tools, MCP, optional knowledge (RAG) |
|
|
78
78
|
| `llm-mcp-service` | `llm-mcp-service/` | `llm-agent` plus a project service that serves MCP tools |
|
|
79
79
|
| `llm-docs-rag` | `llm-docs-rag/` | `llm-agent` answering from your docs via the `docs-rag-mcp` image |
|
|
80
|
+
| `llm-support-sticky-user` | `llm-support-sticky-user/` | `llm-agent` for support: verified users get their tickets and earlier conversations |
|
|
80
81
|
|
|
81
82
|
## E2e templates
|
|
82
83
|
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# llm-support-sticky-user
|
|
2
|
+
|
|
3
|
+
A support agent that remembers its callers. It is the `llm-agent` template plus end-user identity: when your app tells VoiceThere who the user is, the agent can read and update that user's tickets in your CRM, and it picks up where the last conversation stopped. Source: `templates/llm-support-sticky-user/agent.ts`.
|
|
4
|
+
|
|
5
|
+
What it does:
|
|
6
|
+
|
|
7
|
+
- The four ticket tools (`get_open_tickets`, `get_ticket`, `add_troubleshooting_step`, `create_ticket`) use `requiresVerifiedUser`. The model does not see them, and a forced call is refused, until the session has a verified user.
|
|
8
|
+
- The tools never take a user id. They use `ctx.user.id` from the session, so a caller who says "I am user 456" gets nothing from another account.
|
|
9
|
+
- When a verified user is present at session start, or logs in mid-call (`onUserIdentified`), `hooks.ts` loads their open tickets and the last three conversations (`getUserHistory`) into the model's context. Mid-call, the agent also says a short confirmation. If history is `unverified`, `disabled` or `unavailable`, the agent carries on without it.
|
|
10
|
+
- Steps recorded with `add_troubleshooting_step` are written to the CRM when the call ends (`onSessionEnd`), for the user who recorded them only.
|
|
11
|
+
- Logs carry ids and error names only, never what was said.
|
|
12
|
+
|
|
13
|
+
## Run it locally
|
|
14
|
+
|
|
15
|
+
Without a CRM URL the agent uses `mock-crm.ts`: one user, `demo-user`, with an open ticket and two steps already tried. Edit it to try your own cases.
|
|
16
|
+
|
|
17
|
+
## Connect your CRM
|
|
18
|
+
|
|
19
|
+
Add project secrets (only names starting with `AGENT_` reach the agent):
|
|
20
|
+
|
|
21
|
+
- `AGENT_CRM_API_URL`: base URL of your CRM
|
|
22
|
+
- `AGENT_CRM_API_KEY`: sent as `Authorization: Bearer <key>`
|
|
23
|
+
|
|
24
|
+
`CRM_API_URL` and `CRM_API_KEY` work for local runs. Every request has a 3 second limit. `tools.ts` makes four calls, all scoped to the session user, so adjust `createHttpCrm` if your CRM looks different:
|
|
25
|
+
|
|
26
|
+
| Call | Request |
|
|
27
|
+
| ------------ | ------------------------------------------------------------------------------------------------ |
|
|
28
|
+
| open tickets | `GET /users/{userId}/tickets?status=open` returns `[{ id, title, summary, status, stepsTried }]` |
|
|
29
|
+
| one ticket | `GET /users/{userId}/tickets/{ticketId}`, 404 when missing |
|
|
30
|
+
| add a step | `POST /users/{userId}/tickets/{ticketId}/steps` with `{ "step": "..." }` |
|
|
31
|
+
| new ticket | `POST /users/{userId}/tickets` with `{ "title": "...", "summary": "..." }` returns the ticket |
|
|
32
|
+
|
|
33
|
+
## Identify users
|
|
34
|
+
|
|
35
|
+
Turn on end-user identification for the project in the dashboard and choose how tokens are checked: a server key you share with VoiceThere, your JWKS URL, or a callback URL that VoiceThere calls. In all three modes the client code is the same: your backend issues a signed token for the signed-in person and the browser passes it to `@voicethere/client`.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { startSession, connectBrowserSession } from "@voicethere/client";
|
|
39
|
+
|
|
40
|
+
const provision = await startSession({
|
|
41
|
+
apiBase: "https://sessions.example/v1",
|
|
42
|
+
projectId,
|
|
43
|
+
headers: { Authorization: `Bearer ${clientKey}` },
|
|
44
|
+
endUserToken, // from your backend
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const session = await connectBrowserSession({
|
|
48
|
+
mode: "voice",
|
|
49
|
+
credentials: provision.credentials,
|
|
50
|
+
apiBase: "https://sessions.example/v1",
|
|
51
|
+
projectId,
|
|
52
|
+
apiHeaders: { Authorization: `Bearer ${clientKey}` },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// The person signs in during the call; the agent's onUserIdentified runs:
|
|
56
|
+
await session.identify({ endUserToken: freshToken });
|
|
57
|
+
|
|
58
|
+
// The person signs out; this closes the session:
|
|
59
|
+
await session.logout();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
A session without a token still works. The agent answers general questions and asks the caller to log in for anything about an account.
|
|
63
|
+
|
|
64
|
+
## Make it yours
|
|
65
|
+
|
|
66
|
+
- Edit `prompt.md` for tone and policy.
|
|
67
|
+
- Set the provider, model and key as in `llm-agent`: OpenRouter with `anthropic/claude-haiku-5.5` and the secret `AGENT_OPENROUTER_API_KEY`. Thinking models count hidden reasoning against `maxOutputTokens`, so keep replies short in the prompt, not with a small cap.
|
|
68
|
+
- Conversation history for users must be enabled for the project; otherwise the agent works from tickets only.
|