@voicethere/agent 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -0
- package/dist/agent.js +175 -5
- 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/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/positional-tts/agent.js +173 -5
- package/dist/templates/recording-consent/agent.js +173 -5
- 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/package.json +7 -1
- package/templates/mix-smoke/agent.ts +11 -2
|
@@ -154,6 +154,18 @@ var SessionSerialQueue = class {
|
|
|
154
154
|
var SESSION_START_INIT_DELAY_ENABLED_ENV = "AGENT_SESSION_START_INIT_DELAY_ENABLED";
|
|
155
155
|
var SESSION_START_INIT_DELAY_MS_ENV = "AGENT_SESSION_START_INIT_DELAY_MS";
|
|
156
156
|
var DEFAULT_SESSION_START_INIT_DELAY_MS = 500;
|
|
157
|
+
function isSpeakResultMessage(value) {
|
|
158
|
+
if (!value || typeof value !== "object")
|
|
159
|
+
return false;
|
|
160
|
+
const msg = value;
|
|
161
|
+
return msg.type === "speak_result" && typeof msg.requestId === "string";
|
|
162
|
+
}
|
|
163
|
+
function isBargeInControlAckMessage(value) {
|
|
164
|
+
if (!value || typeof value !== "object")
|
|
165
|
+
return false;
|
|
166
|
+
const msg = value;
|
|
167
|
+
return msg.type === "barge_in_control_ack" && typeof msg.requestId === "string";
|
|
168
|
+
}
|
|
157
169
|
function isPlayAckMessage(value) {
|
|
158
170
|
if (!value || typeof value !== "object")
|
|
159
171
|
return false;
|
|
@@ -262,7 +274,7 @@ function isParentMessage(value) {
|
|
|
262
274
|
if (!value || typeof value !== "object")
|
|
263
275
|
return false;
|
|
264
276
|
const msg = value;
|
|
265
|
-
return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "webhook";
|
|
277
|
+
return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "speak_result" || msg.type === "barge_in_control_ack" || msg.type === "agent_interrupted" || msg.type === "webhook";
|
|
266
278
|
}
|
|
267
279
|
function isSessionScopedParentMessage(value) {
|
|
268
280
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -281,7 +293,61 @@ var mixAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
|
281
293
|
var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
282
294
|
var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
|
|
283
295
|
var ttsPoseAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
296
|
+
var speakControlAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
297
|
+
var speakControlWarnedSessionIds = /* @__PURE__ */ new Set();
|
|
284
298
|
var endedSessionIds = /* @__PURE__ */ new Set();
|
|
299
|
+
var SPEAK_RESULT_SAFETY_TIMEOUT_MS = 6e5;
|
|
300
|
+
var pendingSpeakResults = /* @__PURE__ */ new Map();
|
|
301
|
+
var pendingBargeInAcks = /* @__PURE__ */ new Map();
|
|
302
|
+
function handleSpeakResult(message) {
|
|
303
|
+
const pending = pendingSpeakResults.get(message.requestId);
|
|
304
|
+
if (!pending)
|
|
305
|
+
return;
|
|
306
|
+
clearTimeout(pending.timer);
|
|
307
|
+
pendingSpeakResults.delete(message.requestId);
|
|
308
|
+
pending.resolve({
|
|
309
|
+
requestId: message.requestId,
|
|
310
|
+
...message.turnId !== void 0 ? { turnId: message.turnId } : {},
|
|
311
|
+
status: message.status,
|
|
312
|
+
playedMs: message.playedMs,
|
|
313
|
+
...message.synthesizedMs !== void 0 ? { synthesizedMs: message.synthesizedMs } : {},
|
|
314
|
+
...message.errorCode !== void 0 ? { errorCode: message.errorCode } : {}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
function handleBargeInControlAck(message) {
|
|
318
|
+
const pending = pendingBargeInAcks.get(message.requestId);
|
|
319
|
+
if (!pending)
|
|
320
|
+
return;
|
|
321
|
+
clearTimeout(pending.timer);
|
|
322
|
+
pendingBargeInAcks.delete(message.requestId);
|
|
323
|
+
pending.resolve({
|
|
324
|
+
ok: message.ok,
|
|
325
|
+
...message.reason ? { reason: message.reason } : {}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
329
|
+
for (const [requestId, pending] of pendingSpeakResults) {
|
|
330
|
+
if (pending.sessionId !== sessionId)
|
|
331
|
+
continue;
|
|
332
|
+
clearTimeout(pending.timer);
|
|
333
|
+
pendingSpeakResults.delete(requestId);
|
|
334
|
+
pending.resolve({
|
|
335
|
+
requestId,
|
|
336
|
+
...pending.turnId !== void 0 ? { turnId: pending.turnId } : {},
|
|
337
|
+
status: "cancelled",
|
|
338
|
+
playedMs: 0,
|
|
339
|
+
errorCode
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
for (const [requestId, pending] of pendingBargeInAcks) {
|
|
343
|
+
if (pending.sessionId !== sessionId)
|
|
344
|
+
continue;
|
|
345
|
+
clearTimeout(pending.timer);
|
|
346
|
+
pendingBargeInAcks.delete(requestId);
|
|
347
|
+
pending.resolve({ ok: false, reason: "session_ended" });
|
|
348
|
+
}
|
|
349
|
+
speakControlWarnedSessionIds.delete(sessionId);
|
|
350
|
+
}
|
|
285
351
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
286
352
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
287
353
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
@@ -627,6 +693,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
627
693
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
628
694
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
629
695
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
696
|
+
speakControlAvailableBySessionId.set(message.sessionId, message.speakControlAvailable ?? false);
|
|
630
697
|
if (message.voiceLanguageSwitch) {
|
|
631
698
|
voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
|
|
632
699
|
} else {
|
|
@@ -643,7 +710,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
643
710
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
644
711
|
mixAvailable: message.mixAvailable ?? false,
|
|
645
712
|
voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
|
|
646
|
-
ttsPoseAvailable: message.ttsPoseAvailable ?? false
|
|
713
|
+
ttsPoseAvailable: message.ttsPoseAvailable ?? false,
|
|
714
|
+
speakControlAvailable: message.speakControlAvailable ?? false
|
|
647
715
|
});
|
|
648
716
|
sendParentMessage({
|
|
649
717
|
type: "session_start_ack",
|
|
@@ -680,6 +748,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
680
748
|
}
|
|
681
749
|
}
|
|
682
750
|
break;
|
|
751
|
+
case "agent_interrupted": {
|
|
752
|
+
const { type: _type, ...interrupted } = message;
|
|
753
|
+
await handlers.onAgentInterrupted?.(interrupted);
|
|
754
|
+
break;
|
|
755
|
+
}
|
|
683
756
|
case "data_channel_message":
|
|
684
757
|
await handlers.onDataChannelMessage?.({
|
|
685
758
|
sessionId: message.sessionId,
|
|
@@ -709,6 +782,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
709
782
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
710
783
|
lastVoiceLanguageBySessionId.delete(message.sessionId);
|
|
711
784
|
ttsPoseAvailableBySessionId.delete(message.sessionId);
|
|
785
|
+
speakControlAvailableBySessionId.delete(message.sessionId);
|
|
786
|
+
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
712
787
|
await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
|
|
713
788
|
sessionId: message.sessionId
|
|
714
789
|
});
|
|
@@ -732,6 +807,14 @@ function defineAgent(handlers) {
|
|
|
732
807
|
handleConversationHistoryControlAck(message);
|
|
733
808
|
return;
|
|
734
809
|
}
|
|
810
|
+
if (isSpeakResultMessage(message)) {
|
|
811
|
+
handleSpeakResult(message);
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
if (isBargeInControlAckMessage(message)) {
|
|
815
|
+
handleBargeInControlAck(message);
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
735
818
|
if (isPlayAckMessage(message)) {
|
|
736
819
|
handlePlayAck(message);
|
|
737
820
|
return;
|
|
@@ -776,6 +859,7 @@ function defineAgent(handlers) {
|
|
|
776
859
|
return;
|
|
777
860
|
if (message.type === "session_end") {
|
|
778
861
|
endedSessionIds.add(message.sessionId);
|
|
862
|
+
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
779
863
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
780
864
|
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
781
865
|
inboundBySession.clear(message.sessionId);
|
|
@@ -902,12 +986,96 @@ function buildIdleEnv(sessionId) {
|
|
|
902
986
|
...process.env.IDLE_TIMEOUT_SEC ? { IDLE_TIMEOUT_SEC: process.env.IDLE_TIMEOUT_SEC } : {}
|
|
903
987
|
};
|
|
904
988
|
}
|
|
989
|
+
function speakControlAvailableFor(sessionId) {
|
|
990
|
+
return speakControlAvailableBySessionId.get(sessionId) ?? false;
|
|
991
|
+
}
|
|
992
|
+
function warnSpeakControlUnavailable(sessionId) {
|
|
993
|
+
if (speakControlWarnedSessionIds.has(sessionId))
|
|
994
|
+
return;
|
|
995
|
+
speakControlWarnedSessionIds.add(sessionId);
|
|
996
|
+
agentLog("warn", "speak control not available on this runner; speak results, cancel and barge-in control are disabled", sessionId);
|
|
997
|
+
}
|
|
905
998
|
function speak(sessionId, text, options) {
|
|
906
|
-
|
|
907
|
-
|
|
999
|
+
const requestId = randomUUID();
|
|
1000
|
+
const handle = (done2) => ({
|
|
1001
|
+
requestId,
|
|
1002
|
+
done: Promise.resolve(done2),
|
|
1003
|
+
cancel: () => cancelSpeech(sessionId, { requestId })
|
|
1004
|
+
});
|
|
1005
|
+
if (!isVoicethereAgentChild()) {
|
|
1006
|
+
return handle({
|
|
1007
|
+
requestId,
|
|
1008
|
+
status: "unsupported",
|
|
1009
|
+
playedMs: 0,
|
|
1010
|
+
errorCode: "no_parent"
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
if (!allowOutboundForSession(sessionId)) {
|
|
1014
|
+
return handle({
|
|
1015
|
+
requestId,
|
|
1016
|
+
status: "cancelled",
|
|
1017
|
+
playedMs: 0,
|
|
1018
|
+
errorCode: "session_ended"
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
if (!speakControlAvailableFor(sessionId)) {
|
|
1022
|
+
sendParentMessage({
|
|
1023
|
+
type: "speak",
|
|
1024
|
+
sessionId,
|
|
1025
|
+
text,
|
|
1026
|
+
...options?.interruptible === false ? { interruptible: false } : {}
|
|
1027
|
+
});
|
|
1028
|
+
warnSpeakControlUnavailable(sessionId);
|
|
1029
|
+
return handle({ requestId, status: "unsupported", playedMs: 0 });
|
|
1030
|
+
}
|
|
1031
|
+
const turnId = options?.turnId;
|
|
1032
|
+
const inReplyTo = options?.inReplyTo;
|
|
1033
|
+
const done = new Promise((resolve) => {
|
|
1034
|
+
const timer = setTimeout(() => {
|
|
1035
|
+
pendingSpeakResults.delete(requestId);
|
|
1036
|
+
resolve({
|
|
1037
|
+
requestId,
|
|
1038
|
+
...turnId ? { turnId } : {},
|
|
1039
|
+
status: "failed",
|
|
1040
|
+
playedMs: 0,
|
|
1041
|
+
errorCode: "timeout"
|
|
1042
|
+
});
|
|
1043
|
+
}, SPEAK_RESULT_SAFETY_TIMEOUT_MS);
|
|
1044
|
+
pendingSpeakResults.set(requestId, {
|
|
1045
|
+
sessionId,
|
|
1046
|
+
...turnId ? { turnId } : {},
|
|
1047
|
+
resolve,
|
|
1048
|
+
timer
|
|
1049
|
+
});
|
|
1050
|
+
});
|
|
1051
|
+
sendParentMessage({
|
|
1052
|
+
type: "speak",
|
|
1053
|
+
sessionId,
|
|
1054
|
+
text,
|
|
1055
|
+
requestId,
|
|
1056
|
+
...options?.interruptible === false ? { interruptible: false } : {},
|
|
1057
|
+
...turnId ? { turnId } : {},
|
|
1058
|
+
...inReplyTo ? { inReplyTo } : {}
|
|
1059
|
+
});
|
|
1060
|
+
return {
|
|
1061
|
+
requestId,
|
|
1062
|
+
done,
|
|
1063
|
+
cancel: () => cancelSpeech(sessionId, { requestId })
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
function cancelSpeech(sessionId, target) {
|
|
1067
|
+
if (!isVoicethereAgentChild() || !allowOutboundForSession(sessionId))
|
|
1068
|
+
return;
|
|
1069
|
+
if (!speakControlAvailableFor(sessionId)) {
|
|
1070
|
+
warnSpeakControlUnavailable(sessionId);
|
|
908
1071
|
return;
|
|
909
1072
|
}
|
|
910
|
-
sendParentMessage({
|
|
1073
|
+
sendParentMessage({
|
|
1074
|
+
type: "speak_cancel",
|
|
1075
|
+
sessionId,
|
|
1076
|
+
...target?.requestId ? { requestId: target.requestId } : {},
|
|
1077
|
+
...target?.turnId ? { turnId: target.turnId } : {}
|
|
1078
|
+
});
|
|
911
1079
|
}
|
|
912
1080
|
function isTtsPoseAvailable(ctx) {
|
|
913
1081
|
return ctx.ttsPoseAvailable;
|
|
@@ -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,8 +290,62 @@ 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();
|
|
282
296
|
var RECORDING_CONTROL_ACK_TIMEOUT_MS = 5e3;
|
|
297
|
+
var SPEAK_RESULT_SAFETY_TIMEOUT_MS = 6e5;
|
|
298
|
+
var pendingSpeakResults = /* @__PURE__ */ new Map();
|
|
299
|
+
var pendingBargeInAcks = /* @__PURE__ */ new Map();
|
|
300
|
+
function handleSpeakResult(message) {
|
|
301
|
+
const pending = pendingSpeakResults.get(message.requestId);
|
|
302
|
+
if (!pending)
|
|
303
|
+
return;
|
|
304
|
+
clearTimeout(pending.timer);
|
|
305
|
+
pendingSpeakResults.delete(message.requestId);
|
|
306
|
+
pending.resolve({
|
|
307
|
+
requestId: message.requestId,
|
|
308
|
+
...message.turnId !== void 0 ? { turnId: message.turnId } : {},
|
|
309
|
+
status: message.status,
|
|
310
|
+
playedMs: message.playedMs,
|
|
311
|
+
...message.synthesizedMs !== void 0 ? { synthesizedMs: message.synthesizedMs } : {},
|
|
312
|
+
...message.errorCode !== void 0 ? { errorCode: message.errorCode } : {}
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
function handleBargeInControlAck(message) {
|
|
316
|
+
const pending = pendingBargeInAcks.get(message.requestId);
|
|
317
|
+
if (!pending)
|
|
318
|
+
return;
|
|
319
|
+
clearTimeout(pending.timer);
|
|
320
|
+
pendingBargeInAcks.delete(message.requestId);
|
|
321
|
+
pending.resolve({
|
|
322
|
+
ok: message.ok,
|
|
323
|
+
...message.reason ? { reason: message.reason } : {}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
327
|
+
for (const [requestId, pending] of pendingSpeakResults) {
|
|
328
|
+
if (pending.sessionId !== sessionId)
|
|
329
|
+
continue;
|
|
330
|
+
clearTimeout(pending.timer);
|
|
331
|
+
pendingSpeakResults.delete(requestId);
|
|
332
|
+
pending.resolve({
|
|
333
|
+
requestId,
|
|
334
|
+
...pending.turnId !== void 0 ? { turnId: pending.turnId } : {},
|
|
335
|
+
status: "cancelled",
|
|
336
|
+
playedMs: 0,
|
|
337
|
+
errorCode
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
for (const [requestId, pending] of pendingBargeInAcks) {
|
|
341
|
+
if (pending.sessionId !== sessionId)
|
|
342
|
+
continue;
|
|
343
|
+
clearTimeout(pending.timer);
|
|
344
|
+
pendingBargeInAcks.delete(requestId);
|
|
345
|
+
pending.resolve({ ok: false, reason: "session_ended" });
|
|
346
|
+
}
|
|
347
|
+
speakControlWarnedSessionIds.delete(sessionId);
|
|
348
|
+
}
|
|
283
349
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
284
350
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
285
351
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
@@ -612,6 +678,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
612
678
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
613
679
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
614
680
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
681
|
+
speakControlAvailableBySessionId.set(message.sessionId, message.speakControlAvailable ?? false);
|
|
615
682
|
if (message.voiceLanguageSwitch) {
|
|
616
683
|
voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
|
|
617
684
|
} else {
|
|
@@ -628,7 +695,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
628
695
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
629
696
|
mixAvailable: message.mixAvailable ?? false,
|
|
630
697
|
voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
|
|
631
|
-
ttsPoseAvailable: message.ttsPoseAvailable ?? false
|
|
698
|
+
ttsPoseAvailable: message.ttsPoseAvailable ?? false,
|
|
699
|
+
speakControlAvailable: message.speakControlAvailable ?? false
|
|
632
700
|
});
|
|
633
701
|
sendParentMessage({
|
|
634
702
|
type: "session_start_ack",
|
|
@@ -665,6 +733,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
665
733
|
}
|
|
666
734
|
}
|
|
667
735
|
break;
|
|
736
|
+
case "agent_interrupted": {
|
|
737
|
+
const { type: _type, ...interrupted } = message;
|
|
738
|
+
await handlers.onAgentInterrupted?.(interrupted);
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
668
741
|
case "data_channel_message":
|
|
669
742
|
await handlers.onDataChannelMessage?.({
|
|
670
743
|
sessionId: message.sessionId,
|
|
@@ -694,6 +767,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
694
767
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
695
768
|
lastVoiceLanguageBySessionId.delete(message.sessionId);
|
|
696
769
|
ttsPoseAvailableBySessionId.delete(message.sessionId);
|
|
770
|
+
speakControlAvailableBySessionId.delete(message.sessionId);
|
|
771
|
+
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
697
772
|
await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
|
|
698
773
|
sessionId: message.sessionId
|
|
699
774
|
});
|
|
@@ -717,6 +792,14 @@ function defineAgent(handlers) {
|
|
|
717
792
|
handleConversationHistoryControlAck(message);
|
|
718
793
|
return;
|
|
719
794
|
}
|
|
795
|
+
if (isSpeakResultMessage(message)) {
|
|
796
|
+
handleSpeakResult(message);
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
if (isBargeInControlAckMessage(message)) {
|
|
800
|
+
handleBargeInControlAck(message);
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
720
803
|
if (isPlayAckMessage(message)) {
|
|
721
804
|
handlePlayAck(message);
|
|
722
805
|
return;
|
|
@@ -761,6 +844,7 @@ function defineAgent(handlers) {
|
|
|
761
844
|
return;
|
|
762
845
|
if (message.type === "session_end") {
|
|
763
846
|
endedSessionIds.add(message.sessionId);
|
|
847
|
+
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
764
848
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
765
849
|
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
766
850
|
inboundBySession.clear(message.sessionId);
|
|
@@ -887,12 +971,96 @@ function buildIdleEnv(sessionId) {
|
|
|
887
971
|
...process.env.IDLE_TIMEOUT_SEC ? { IDLE_TIMEOUT_SEC: process.env.IDLE_TIMEOUT_SEC } : {}
|
|
888
972
|
};
|
|
889
973
|
}
|
|
974
|
+
function speakControlAvailableFor(sessionId) {
|
|
975
|
+
return speakControlAvailableBySessionId.get(sessionId) ?? false;
|
|
976
|
+
}
|
|
977
|
+
function warnSpeakControlUnavailable(sessionId) {
|
|
978
|
+
if (speakControlWarnedSessionIds.has(sessionId))
|
|
979
|
+
return;
|
|
980
|
+
speakControlWarnedSessionIds.add(sessionId);
|
|
981
|
+
agentLog("warn", "speak control not available on this runner; speak results, cancel and barge-in control are disabled", sessionId);
|
|
982
|
+
}
|
|
890
983
|
function speak(sessionId, text, options) {
|
|
891
|
-
|
|
892
|
-
|
|
984
|
+
const requestId = randomUUID();
|
|
985
|
+
const handle = (done2) => ({
|
|
986
|
+
requestId,
|
|
987
|
+
done: Promise.resolve(done2),
|
|
988
|
+
cancel: () => cancelSpeech(sessionId, { requestId })
|
|
989
|
+
});
|
|
990
|
+
if (!isVoicethereAgentChild()) {
|
|
991
|
+
return handle({
|
|
992
|
+
requestId,
|
|
993
|
+
status: "unsupported",
|
|
994
|
+
playedMs: 0,
|
|
995
|
+
errorCode: "no_parent"
|
|
996
|
+
});
|
|
997
|
+
}
|
|
998
|
+
if (!allowOutboundForSession(sessionId)) {
|
|
999
|
+
return handle({
|
|
1000
|
+
requestId,
|
|
1001
|
+
status: "cancelled",
|
|
1002
|
+
playedMs: 0,
|
|
1003
|
+
errorCode: "session_ended"
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
if (!speakControlAvailableFor(sessionId)) {
|
|
1007
|
+
sendParentMessage({
|
|
1008
|
+
type: "speak",
|
|
1009
|
+
sessionId,
|
|
1010
|
+
text,
|
|
1011
|
+
...options?.interruptible === false ? { interruptible: false } : {}
|
|
1012
|
+
});
|
|
1013
|
+
warnSpeakControlUnavailable(sessionId);
|
|
1014
|
+
return handle({ requestId, status: "unsupported", playedMs: 0 });
|
|
1015
|
+
}
|
|
1016
|
+
const turnId = options?.turnId;
|
|
1017
|
+
const inReplyTo = options?.inReplyTo;
|
|
1018
|
+
const done = new Promise((resolve) => {
|
|
1019
|
+
const timer = setTimeout(() => {
|
|
1020
|
+
pendingSpeakResults.delete(requestId);
|
|
1021
|
+
resolve({
|
|
1022
|
+
requestId,
|
|
1023
|
+
...turnId ? { turnId } : {},
|
|
1024
|
+
status: "failed",
|
|
1025
|
+
playedMs: 0,
|
|
1026
|
+
errorCode: "timeout"
|
|
1027
|
+
});
|
|
1028
|
+
}, SPEAK_RESULT_SAFETY_TIMEOUT_MS);
|
|
1029
|
+
pendingSpeakResults.set(requestId, {
|
|
1030
|
+
sessionId,
|
|
1031
|
+
...turnId ? { turnId } : {},
|
|
1032
|
+
resolve,
|
|
1033
|
+
timer
|
|
1034
|
+
});
|
|
1035
|
+
});
|
|
1036
|
+
sendParentMessage({
|
|
1037
|
+
type: "speak",
|
|
1038
|
+
sessionId,
|
|
1039
|
+
text,
|
|
1040
|
+
requestId,
|
|
1041
|
+
...options?.interruptible === false ? { interruptible: false } : {},
|
|
1042
|
+
...turnId ? { turnId } : {},
|
|
1043
|
+
...inReplyTo ? { inReplyTo } : {}
|
|
1044
|
+
});
|
|
1045
|
+
return {
|
|
1046
|
+
requestId,
|
|
1047
|
+
done,
|
|
1048
|
+
cancel: () => cancelSpeech(sessionId, { requestId })
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
function cancelSpeech(sessionId, target) {
|
|
1052
|
+
if (!isVoicethereAgentChild() || !allowOutboundForSession(sessionId))
|
|
1053
|
+
return;
|
|
1054
|
+
if (!speakControlAvailableFor(sessionId)) {
|
|
1055
|
+
warnSpeakControlUnavailable(sessionId);
|
|
893
1056
|
return;
|
|
894
1057
|
}
|
|
895
|
-
sendParentMessage({
|
|
1058
|
+
sendParentMessage({
|
|
1059
|
+
type: "speak_cancel",
|
|
1060
|
+
sessionId,
|
|
1061
|
+
...target?.requestId ? { requestId: target.requestId } : {},
|
|
1062
|
+
...target?.turnId ? { turnId: target.turnId } : {}
|
|
1063
|
+
});
|
|
896
1064
|
}
|
|
897
1065
|
async function sendRecordingControl(sessionId, action) {
|
|
898
1066
|
const requestId = randomUUID();
|