@voicethere/agent 0.9.5 → 0.9.6
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 +37 -15
- package/dist/agent.js +53 -3
- 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 +68 -2
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/runtime.d.ts +27 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +158 -1
- package/dist/runtime.js.map +1 -1
- package/dist/templates/echo/agent.js +54 -3
- package/dist/templates/echo-dc/agent.js +54 -3
- package/dist/templates/game-sync/agent.js +54 -3
- package/dist/templates/language-switch/agent.js +442 -63
- package/dist/templates/positional-tts/agent.js +54 -3
- package/dist/templates/recording-consent/agent.js +54 -3
- package/dist/templates/spatial-showcase/agent.js +54 -3
- package/dist/templates/voice-showcase/agent.js +54 -3
- package/dist/templates/voice-starter/agent.js +54 -3
- package/dist/templates/webhooks/agent.js +54 -3
- package/dist/templates/webhooks-redis/agent.js +54 -3
- package/dist/templates/world-sync/agent.js +54 -3
- package/dist/templates/world-sync-binary/agent.js +54 -3
- package/package.json +1 -1
- package/templates/language-switch/README.md +18 -11
- package/templates/language-switch/agent.ts +452 -101
|
@@ -187,6 +187,18 @@ function isVoiceLanguageControlAckMessage(value) {
|
|
|
187
187
|
const msg = value;
|
|
188
188
|
return msg.type === "voice_language_control_ack" && typeof msg.requestId === "string";
|
|
189
189
|
}
|
|
190
|
+
function isVoiceLanguagePrepareResultMessage(value) {
|
|
191
|
+
if (!value || typeof value !== "object")
|
|
192
|
+
return false;
|
|
193
|
+
const msg = value;
|
|
194
|
+
return msg.type === "voice_language_prepare_result" && typeof msg.requestId === "string";
|
|
195
|
+
}
|
|
196
|
+
function isVoiceReplayLastResultMessage(value) {
|
|
197
|
+
if (!value || typeof value !== "object")
|
|
198
|
+
return false;
|
|
199
|
+
const msg = value;
|
|
200
|
+
return msg.type === "voice_replay_last_result" && typeof msg.requestId === "string";
|
|
201
|
+
}
|
|
190
202
|
function isWebhookMessage(value) {
|
|
191
203
|
if (!value || typeof value !== "object")
|
|
192
204
|
return false;
|
|
@@ -229,7 +241,7 @@ function isParentMessage(value) {
|
|
|
229
241
|
if (!value || typeof value !== "object")
|
|
230
242
|
return false;
|
|
231
243
|
const msg = value;
|
|
232
|
-
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 === "webhook";
|
|
244
|
+
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";
|
|
233
245
|
}
|
|
234
246
|
function isSessionScopedParentMessage(value) {
|
|
235
247
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -259,6 +271,9 @@ var pendingPlayPoseAcks = /* @__PURE__ */ new Map();
|
|
|
259
271
|
var pendingMixAcks = /* @__PURE__ */ new Map();
|
|
260
272
|
var pendingSttAcks = /* @__PURE__ */ new Map();
|
|
261
273
|
var pendingVoiceLanguageAcks = /* @__PURE__ */ new Map();
|
|
274
|
+
var pendingVoiceLanguagePrepares = /* @__PURE__ */ new Map();
|
|
275
|
+
var pendingVoiceReplays = /* @__PURE__ */ new Map();
|
|
276
|
+
var voiceLanguageSwitchSettingsBySessionId = /* @__PURE__ */ new Map();
|
|
262
277
|
function handleRecordingControlAck(message) {
|
|
263
278
|
const pending = pendingRecordingAcks.get(message.requestId);
|
|
264
279
|
if (!pending)
|
|
@@ -382,6 +397,27 @@ function handleVoiceLanguageControlAck(message) {
|
|
|
382
397
|
...message.ttsProvider ? { ttsProvider: message.ttsProvider } : {}
|
|
383
398
|
});
|
|
384
399
|
}
|
|
400
|
+
function handleVoiceLanguagePrepareResult(message) {
|
|
401
|
+
const pending = pendingVoiceLanguagePrepares.get(message.requestId);
|
|
402
|
+
if (!pending)
|
|
403
|
+
return;
|
|
404
|
+
clearTimeout(pending.timer);
|
|
405
|
+
pendingVoiceLanguagePrepares.delete(message.requestId);
|
|
406
|
+
pending.resolve({
|
|
407
|
+
ok: message.ok,
|
|
408
|
+
ready: message.ready,
|
|
409
|
+
reason: message.reason,
|
|
410
|
+
...message.language ? { language: message.language } : {}
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
function handleVoiceReplayLastResult(message) {
|
|
414
|
+
const pending = pendingVoiceReplays.get(message.requestId);
|
|
415
|
+
if (!pending)
|
|
416
|
+
return;
|
|
417
|
+
clearTimeout(pending.timer);
|
|
418
|
+
pendingVoiceReplays.delete(message.requestId);
|
|
419
|
+
pending.resolve({ ok: message.ok, reason: message.reason });
|
|
420
|
+
}
|
|
385
421
|
function clearPendingRecordingAcksForSession(sessionId, reason) {
|
|
386
422
|
for (const [requestId, pending] of pendingRecordingAcks) {
|
|
387
423
|
if (pending.sessionId !== sessionId)
|
|
@@ -447,7 +483,7 @@ function allowOutboundForSession(sessionId) {
|
|
|
447
483
|
}
|
|
448
484
|
function sendParentMessage(message) {
|
|
449
485
|
const msgType = message && typeof message === "object" && "type" in message ? message.type : void 0;
|
|
450
|
-
if (msgType === "mix_control" || msgType === "stt_control" || msgType === "voice_language_control") {
|
|
486
|
+
if (msgType === "mix_control" || msgType === "stt_control" || msgType === "voice_language_control" || msgType === "voice_language_prepare" || msgType === "voice_replay_last") {
|
|
451
487
|
process.send?.(message);
|
|
452
488
|
return;
|
|
453
489
|
}
|
|
@@ -550,6 +586,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
550
586
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
551
587
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
552
588
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
589
|
+
if (message.voiceLanguageSwitch) {
|
|
590
|
+
voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
|
|
591
|
+
} else {
|
|
592
|
+
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
593
|
+
}
|
|
553
594
|
const sessionStartInitDelayMs = resolveSessionStartInitDelayMs();
|
|
554
595
|
if (sessionStartInitDelayMs > 0) {
|
|
555
596
|
await new Promise((resolve) => setTimeout(resolve, sessionStartInitDelayMs));
|
|
@@ -573,7 +614,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
573
614
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
574
615
|
await handlers.onUserSpeechFinal?.({
|
|
575
616
|
sessionId: message.sessionId,
|
|
576
|
-
text: message.event.text.trim()
|
|
617
|
+
text: message.event.text.trim(),
|
|
618
|
+
...message.event.replay === true ? { replay: true } : {}
|
|
577
619
|
});
|
|
578
620
|
}
|
|
579
621
|
if (message.event.type === "user_language") {
|
|
@@ -623,6 +665,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
623
665
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
624
666
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
625
667
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
668
|
+
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
626
669
|
lastVoiceLanguageBySessionId.delete(message.sessionId);
|
|
627
670
|
ttsPoseAvailableBySessionId.delete(message.sessionId);
|
|
628
671
|
await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
|
|
@@ -676,6 +719,14 @@ function defineAgent(handlers) {
|
|
|
676
719
|
handleVoiceLanguageControlAck(message);
|
|
677
720
|
return;
|
|
678
721
|
}
|
|
722
|
+
if (isVoiceLanguagePrepareResultMessage(message)) {
|
|
723
|
+
handleVoiceLanguagePrepareResult(message);
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
if (isVoiceReplayLastResultMessage(message)) {
|
|
727
|
+
handleVoiceReplayLastResult(message);
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
679
730
|
if (isWebhookMessage(message)) {
|
|
680
731
|
void agentStartReady.then(() => handleWebhookMessage(message, handlers));
|
|
681
732
|
return;
|
|
@@ -10939,6 +10939,18 @@ function isVoiceLanguageControlAckMessage(value) {
|
|
|
10939
10939
|
const msg = value;
|
|
10940
10940
|
return msg.type === "voice_language_control_ack" && typeof msg.requestId === "string";
|
|
10941
10941
|
}
|
|
10942
|
+
function isVoiceLanguagePrepareResultMessage(value) {
|
|
10943
|
+
if (!value || typeof value !== "object")
|
|
10944
|
+
return false;
|
|
10945
|
+
const msg = value;
|
|
10946
|
+
return msg.type === "voice_language_prepare_result" && typeof msg.requestId === "string";
|
|
10947
|
+
}
|
|
10948
|
+
function isVoiceReplayLastResultMessage(value) {
|
|
10949
|
+
if (!value || typeof value !== "object")
|
|
10950
|
+
return false;
|
|
10951
|
+
const msg = value;
|
|
10952
|
+
return msg.type === "voice_replay_last_result" && typeof msg.requestId === "string";
|
|
10953
|
+
}
|
|
10942
10954
|
function isWebhookMessage(value) {
|
|
10943
10955
|
if (!value || typeof value !== "object")
|
|
10944
10956
|
return false;
|
|
@@ -10981,7 +10993,7 @@ function isParentMessage(value) {
|
|
|
10981
10993
|
if (!value || typeof value !== "object")
|
|
10982
10994
|
return false;
|
|
10983
10995
|
const msg = value;
|
|
10984
|
-
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 === "webhook";
|
|
10996
|
+
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";
|
|
10985
10997
|
}
|
|
10986
10998
|
function isSessionScopedParentMessage(value) {
|
|
10987
10999
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -11011,6 +11023,9 @@ var pendingPlayPoseAcks = /* @__PURE__ */ new Map();
|
|
|
11011
11023
|
var pendingMixAcks = /* @__PURE__ */ new Map();
|
|
11012
11024
|
var pendingSttAcks = /* @__PURE__ */ new Map();
|
|
11013
11025
|
var pendingVoiceLanguageAcks = /* @__PURE__ */ new Map();
|
|
11026
|
+
var pendingVoiceLanguagePrepares = /* @__PURE__ */ new Map();
|
|
11027
|
+
var pendingVoiceReplays = /* @__PURE__ */ new Map();
|
|
11028
|
+
var voiceLanguageSwitchSettingsBySessionId = /* @__PURE__ */ new Map();
|
|
11014
11029
|
function handleRecordingControlAck(message) {
|
|
11015
11030
|
const pending = pendingRecordingAcks.get(message.requestId);
|
|
11016
11031
|
if (!pending)
|
|
@@ -11134,6 +11149,27 @@ function handleVoiceLanguageControlAck(message) {
|
|
|
11134
11149
|
...message.ttsProvider ? { ttsProvider: message.ttsProvider } : {}
|
|
11135
11150
|
});
|
|
11136
11151
|
}
|
|
11152
|
+
function handleVoiceLanguagePrepareResult(message) {
|
|
11153
|
+
const pending = pendingVoiceLanguagePrepares.get(message.requestId);
|
|
11154
|
+
if (!pending)
|
|
11155
|
+
return;
|
|
11156
|
+
clearTimeout(pending.timer);
|
|
11157
|
+
pendingVoiceLanguagePrepares.delete(message.requestId);
|
|
11158
|
+
pending.resolve({
|
|
11159
|
+
ok: message.ok,
|
|
11160
|
+
ready: message.ready,
|
|
11161
|
+
reason: message.reason,
|
|
11162
|
+
...message.language ? { language: message.language } : {}
|
|
11163
|
+
});
|
|
11164
|
+
}
|
|
11165
|
+
function handleVoiceReplayLastResult(message) {
|
|
11166
|
+
const pending = pendingVoiceReplays.get(message.requestId);
|
|
11167
|
+
if (!pending)
|
|
11168
|
+
return;
|
|
11169
|
+
clearTimeout(pending.timer);
|
|
11170
|
+
pendingVoiceReplays.delete(message.requestId);
|
|
11171
|
+
pending.resolve({ ok: message.ok, reason: message.reason });
|
|
11172
|
+
}
|
|
11137
11173
|
function clearPendingRecordingAcksForSession(sessionId, reason) {
|
|
11138
11174
|
for (const [requestId, pending] of pendingRecordingAcks) {
|
|
11139
11175
|
if (pending.sessionId !== sessionId)
|
|
@@ -11199,7 +11235,7 @@ function allowOutboundForSession(sessionId) {
|
|
|
11199
11235
|
}
|
|
11200
11236
|
function sendParentMessage(message) {
|
|
11201
11237
|
const msgType = message && typeof message === "object" && "type" in message ? message.type : void 0;
|
|
11202
|
-
if (msgType === "mix_control" || msgType === "stt_control" || msgType === "voice_language_control") {
|
|
11238
|
+
if (msgType === "mix_control" || msgType === "stt_control" || msgType === "voice_language_control" || msgType === "voice_language_prepare" || msgType === "voice_replay_last") {
|
|
11203
11239
|
process.send?.(message);
|
|
11204
11240
|
return;
|
|
11205
11241
|
}
|
|
@@ -11302,6 +11338,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
11302
11338
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
11303
11339
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
11304
11340
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
11341
|
+
if (message.voiceLanguageSwitch) {
|
|
11342
|
+
voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
|
|
11343
|
+
} else {
|
|
11344
|
+
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
11345
|
+
}
|
|
11305
11346
|
const sessionStartInitDelayMs = resolveSessionStartInitDelayMs();
|
|
11306
11347
|
if (sessionStartInitDelayMs > 0) {
|
|
11307
11348
|
await new Promise((resolve) => setTimeout(resolve, sessionStartInitDelayMs));
|
|
@@ -11325,7 +11366,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
11325
11366
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
11326
11367
|
await handlers.onUserSpeechFinal?.({
|
|
11327
11368
|
sessionId: message.sessionId,
|
|
11328
|
-
text: message.event.text.trim()
|
|
11369
|
+
text: message.event.text.trim(),
|
|
11370
|
+
...message.event.replay === true ? { replay: true } : {}
|
|
11329
11371
|
});
|
|
11330
11372
|
}
|
|
11331
11373
|
if (message.event.type === "user_language") {
|
|
@@ -11375,6 +11417,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
11375
11417
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
11376
11418
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
11377
11419
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
11420
|
+
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
11378
11421
|
lastVoiceLanguageBySessionId.delete(message.sessionId);
|
|
11379
11422
|
ttsPoseAvailableBySessionId.delete(message.sessionId);
|
|
11380
11423
|
await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
|
|
@@ -11428,6 +11471,14 @@ function defineAgent(handlers) {
|
|
|
11428
11471
|
handleVoiceLanguageControlAck(message);
|
|
11429
11472
|
return;
|
|
11430
11473
|
}
|
|
11474
|
+
if (isVoiceLanguagePrepareResultMessage(message)) {
|
|
11475
|
+
handleVoiceLanguagePrepareResult(message);
|
|
11476
|
+
return;
|
|
11477
|
+
}
|
|
11478
|
+
if (isVoiceReplayLastResultMessage(message)) {
|
|
11479
|
+
handleVoiceReplayLastResult(message);
|
|
11480
|
+
return;
|
|
11481
|
+
}
|
|
11431
11482
|
if (isWebhookMessage(message)) {
|
|
11432
11483
|
void agentStartReady.then(() => handleWebhookMessage(message, handlers));
|
|
11433
11484
|
return;
|
|
@@ -184,6 +184,18 @@ function isVoiceLanguageControlAckMessage(value) {
|
|
|
184
184
|
const msg = value;
|
|
185
185
|
return msg.type === "voice_language_control_ack" && typeof msg.requestId === "string";
|
|
186
186
|
}
|
|
187
|
+
function isVoiceLanguagePrepareResultMessage(value) {
|
|
188
|
+
if (!value || typeof value !== "object")
|
|
189
|
+
return false;
|
|
190
|
+
const msg = value;
|
|
191
|
+
return msg.type === "voice_language_prepare_result" && typeof msg.requestId === "string";
|
|
192
|
+
}
|
|
193
|
+
function isVoiceReplayLastResultMessage(value) {
|
|
194
|
+
if (!value || typeof value !== "object")
|
|
195
|
+
return false;
|
|
196
|
+
const msg = value;
|
|
197
|
+
return msg.type === "voice_replay_last_result" && typeof msg.requestId === "string";
|
|
198
|
+
}
|
|
187
199
|
function isWebhookMessage(value) {
|
|
188
200
|
if (!value || typeof value !== "object")
|
|
189
201
|
return false;
|
|
@@ -226,7 +238,7 @@ function isParentMessage(value) {
|
|
|
226
238
|
if (!value || typeof value !== "object")
|
|
227
239
|
return false;
|
|
228
240
|
const msg = value;
|
|
229
|
-
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 === "webhook";
|
|
241
|
+
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";
|
|
230
242
|
}
|
|
231
243
|
function isSessionScopedParentMessage(value) {
|
|
232
244
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -256,6 +268,9 @@ var pendingPlayPoseAcks = /* @__PURE__ */ new Map();
|
|
|
256
268
|
var pendingMixAcks = /* @__PURE__ */ new Map();
|
|
257
269
|
var pendingSttAcks = /* @__PURE__ */ new Map();
|
|
258
270
|
var pendingVoiceLanguageAcks = /* @__PURE__ */ new Map();
|
|
271
|
+
var pendingVoiceLanguagePrepares = /* @__PURE__ */ new Map();
|
|
272
|
+
var pendingVoiceReplays = /* @__PURE__ */ new Map();
|
|
273
|
+
var voiceLanguageSwitchSettingsBySessionId = /* @__PURE__ */ new Map();
|
|
259
274
|
function handleRecordingControlAck(message) {
|
|
260
275
|
const pending = pendingRecordingAcks.get(message.requestId);
|
|
261
276
|
if (!pending)
|
|
@@ -379,6 +394,27 @@ function handleVoiceLanguageControlAck(message) {
|
|
|
379
394
|
...message.ttsProvider ? { ttsProvider: message.ttsProvider } : {}
|
|
380
395
|
});
|
|
381
396
|
}
|
|
397
|
+
function handleVoiceLanguagePrepareResult(message) {
|
|
398
|
+
const pending = pendingVoiceLanguagePrepares.get(message.requestId);
|
|
399
|
+
if (!pending)
|
|
400
|
+
return;
|
|
401
|
+
clearTimeout(pending.timer);
|
|
402
|
+
pendingVoiceLanguagePrepares.delete(message.requestId);
|
|
403
|
+
pending.resolve({
|
|
404
|
+
ok: message.ok,
|
|
405
|
+
ready: message.ready,
|
|
406
|
+
reason: message.reason,
|
|
407
|
+
...message.language ? { language: message.language } : {}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
function handleVoiceReplayLastResult(message) {
|
|
411
|
+
const pending = pendingVoiceReplays.get(message.requestId);
|
|
412
|
+
if (!pending)
|
|
413
|
+
return;
|
|
414
|
+
clearTimeout(pending.timer);
|
|
415
|
+
pendingVoiceReplays.delete(message.requestId);
|
|
416
|
+
pending.resolve({ ok: message.ok, reason: message.reason });
|
|
417
|
+
}
|
|
382
418
|
function clearPendingRecordingAcksForSession(sessionId, reason) {
|
|
383
419
|
for (const [requestId, pending] of pendingRecordingAcks) {
|
|
384
420
|
if (pending.sessionId !== sessionId)
|
|
@@ -444,7 +480,7 @@ function allowOutboundForSession(sessionId) {
|
|
|
444
480
|
}
|
|
445
481
|
function sendParentMessage(message) {
|
|
446
482
|
const msgType = message && typeof message === "object" && "type" in message ? message.type : void 0;
|
|
447
|
-
if (msgType === "mix_control" || msgType === "stt_control" || msgType === "voice_language_control") {
|
|
483
|
+
if (msgType === "mix_control" || msgType === "stt_control" || msgType === "voice_language_control" || msgType === "voice_language_prepare" || msgType === "voice_replay_last") {
|
|
448
484
|
process.send?.(message);
|
|
449
485
|
return;
|
|
450
486
|
}
|
|
@@ -547,6 +583,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
547
583
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
548
584
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
549
585
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
586
|
+
if (message.voiceLanguageSwitch) {
|
|
587
|
+
voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
|
|
588
|
+
} else {
|
|
589
|
+
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
590
|
+
}
|
|
550
591
|
const sessionStartInitDelayMs = resolveSessionStartInitDelayMs();
|
|
551
592
|
if (sessionStartInitDelayMs > 0) {
|
|
552
593
|
await new Promise((resolve) => setTimeout(resolve, sessionStartInitDelayMs));
|
|
@@ -570,7 +611,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
570
611
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
571
612
|
await handlers.onUserSpeechFinal?.({
|
|
572
613
|
sessionId: message.sessionId,
|
|
573
|
-
text: message.event.text.trim()
|
|
614
|
+
text: message.event.text.trim(),
|
|
615
|
+
...message.event.replay === true ? { replay: true } : {}
|
|
574
616
|
});
|
|
575
617
|
}
|
|
576
618
|
if (message.event.type === "user_language") {
|
|
@@ -620,6 +662,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
620
662
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
621
663
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
622
664
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
665
|
+
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
623
666
|
lastVoiceLanguageBySessionId.delete(message.sessionId);
|
|
624
667
|
ttsPoseAvailableBySessionId.delete(message.sessionId);
|
|
625
668
|
await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
|
|
@@ -673,6 +716,14 @@ function defineAgent(handlers) {
|
|
|
673
716
|
handleVoiceLanguageControlAck(message);
|
|
674
717
|
return;
|
|
675
718
|
}
|
|
719
|
+
if (isVoiceLanguagePrepareResultMessage(message)) {
|
|
720
|
+
handleVoiceLanguagePrepareResult(message);
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
if (isVoiceReplayLastResultMessage(message)) {
|
|
724
|
+
handleVoiceReplayLastResult(message);
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
676
727
|
if (isWebhookMessage(message)) {
|
|
677
728
|
void agentStartReady.then(() => handleWebhookMessage(message, handlers));
|
|
678
729
|
return;
|
|
@@ -184,6 +184,18 @@ function isVoiceLanguageControlAckMessage(value) {
|
|
|
184
184
|
const msg = value;
|
|
185
185
|
return msg.type === "voice_language_control_ack" && typeof msg.requestId === "string";
|
|
186
186
|
}
|
|
187
|
+
function isVoiceLanguagePrepareResultMessage(value) {
|
|
188
|
+
if (!value || typeof value !== "object")
|
|
189
|
+
return false;
|
|
190
|
+
const msg = value;
|
|
191
|
+
return msg.type === "voice_language_prepare_result" && typeof msg.requestId === "string";
|
|
192
|
+
}
|
|
193
|
+
function isVoiceReplayLastResultMessage(value) {
|
|
194
|
+
if (!value || typeof value !== "object")
|
|
195
|
+
return false;
|
|
196
|
+
const msg = value;
|
|
197
|
+
return msg.type === "voice_replay_last_result" && typeof msg.requestId === "string";
|
|
198
|
+
}
|
|
187
199
|
function isWebhookMessage(value) {
|
|
188
200
|
if (!value || typeof value !== "object")
|
|
189
201
|
return false;
|
|
@@ -226,7 +238,7 @@ function isParentMessage(value) {
|
|
|
226
238
|
if (!value || typeof value !== "object")
|
|
227
239
|
return false;
|
|
228
240
|
const msg = value;
|
|
229
|
-
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 === "webhook";
|
|
241
|
+
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";
|
|
230
242
|
}
|
|
231
243
|
function isSessionScopedParentMessage(value) {
|
|
232
244
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -256,6 +268,9 @@ var pendingPlayPoseAcks = /* @__PURE__ */ new Map();
|
|
|
256
268
|
var pendingMixAcks = /* @__PURE__ */ new Map();
|
|
257
269
|
var pendingSttAcks = /* @__PURE__ */ new Map();
|
|
258
270
|
var pendingVoiceLanguageAcks = /* @__PURE__ */ new Map();
|
|
271
|
+
var pendingVoiceLanguagePrepares = /* @__PURE__ */ new Map();
|
|
272
|
+
var pendingVoiceReplays = /* @__PURE__ */ new Map();
|
|
273
|
+
var voiceLanguageSwitchSettingsBySessionId = /* @__PURE__ */ new Map();
|
|
259
274
|
function handleRecordingControlAck(message) {
|
|
260
275
|
const pending = pendingRecordingAcks.get(message.requestId);
|
|
261
276
|
if (!pending)
|
|
@@ -379,6 +394,27 @@ function handleVoiceLanguageControlAck(message) {
|
|
|
379
394
|
...message.ttsProvider ? { ttsProvider: message.ttsProvider } : {}
|
|
380
395
|
});
|
|
381
396
|
}
|
|
397
|
+
function handleVoiceLanguagePrepareResult(message) {
|
|
398
|
+
const pending = pendingVoiceLanguagePrepares.get(message.requestId);
|
|
399
|
+
if (!pending)
|
|
400
|
+
return;
|
|
401
|
+
clearTimeout(pending.timer);
|
|
402
|
+
pendingVoiceLanguagePrepares.delete(message.requestId);
|
|
403
|
+
pending.resolve({
|
|
404
|
+
ok: message.ok,
|
|
405
|
+
ready: message.ready,
|
|
406
|
+
reason: message.reason,
|
|
407
|
+
...message.language ? { language: message.language } : {}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
function handleVoiceReplayLastResult(message) {
|
|
411
|
+
const pending = pendingVoiceReplays.get(message.requestId);
|
|
412
|
+
if (!pending)
|
|
413
|
+
return;
|
|
414
|
+
clearTimeout(pending.timer);
|
|
415
|
+
pendingVoiceReplays.delete(message.requestId);
|
|
416
|
+
pending.resolve({ ok: message.ok, reason: message.reason });
|
|
417
|
+
}
|
|
382
418
|
function clearPendingRecordingAcksForSession(sessionId, reason) {
|
|
383
419
|
for (const [requestId, pending] of pendingRecordingAcks) {
|
|
384
420
|
if (pending.sessionId !== sessionId)
|
|
@@ -444,7 +480,7 @@ function allowOutboundForSession(sessionId) {
|
|
|
444
480
|
}
|
|
445
481
|
function sendParentMessage(message) {
|
|
446
482
|
const msgType = message && typeof message === "object" && "type" in message ? message.type : void 0;
|
|
447
|
-
if (msgType === "mix_control" || msgType === "stt_control" || msgType === "voice_language_control") {
|
|
483
|
+
if (msgType === "mix_control" || msgType === "stt_control" || msgType === "voice_language_control" || msgType === "voice_language_prepare" || msgType === "voice_replay_last") {
|
|
448
484
|
process.send?.(message);
|
|
449
485
|
return;
|
|
450
486
|
}
|
|
@@ -547,6 +583,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
547
583
|
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
548
584
|
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
549
585
|
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
586
|
+
if (message.voiceLanguageSwitch) {
|
|
587
|
+
voiceLanguageSwitchSettingsBySessionId.set(message.sessionId, message.voiceLanguageSwitch);
|
|
588
|
+
} else {
|
|
589
|
+
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
590
|
+
}
|
|
550
591
|
const sessionStartInitDelayMs = resolveSessionStartInitDelayMs();
|
|
551
592
|
if (sessionStartInitDelayMs > 0) {
|
|
552
593
|
await new Promise((resolve) => setTimeout(resolve, sessionStartInitDelayMs));
|
|
@@ -570,7 +611,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
570
611
|
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
571
612
|
await handlers.onUserSpeechFinal?.({
|
|
572
613
|
sessionId: message.sessionId,
|
|
573
|
-
text: message.event.text.trim()
|
|
614
|
+
text: message.event.text.trim(),
|
|
615
|
+
...message.event.replay === true ? { replay: true } : {}
|
|
574
616
|
});
|
|
575
617
|
}
|
|
576
618
|
if (message.event.type === "user_language") {
|
|
@@ -620,6 +662,7 @@ async function handleParentMessage(message, handlers) {
|
|
|
620
662
|
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
621
663
|
mixAvailableBySessionId.delete(message.sessionId);
|
|
622
664
|
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
665
|
+
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
623
666
|
lastVoiceLanguageBySessionId.delete(message.sessionId);
|
|
624
667
|
ttsPoseAvailableBySessionId.delete(message.sessionId);
|
|
625
668
|
await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
|
|
@@ -673,6 +716,14 @@ function defineAgent(handlers) {
|
|
|
673
716
|
handleVoiceLanguageControlAck(message);
|
|
674
717
|
return;
|
|
675
718
|
}
|
|
719
|
+
if (isVoiceLanguagePrepareResultMessage(message)) {
|
|
720
|
+
handleVoiceLanguagePrepareResult(message);
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
if (isVoiceReplayLastResultMessage(message)) {
|
|
724
|
+
handleVoiceReplayLastResult(message);
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
676
727
|
if (isWebhookMessage(message)) {
|
|
677
728
|
void agentStartReady.then(() => handleWebhookMessage(message, handlers));
|
|
678
729
|
return;
|
package/package.json
CHANGED
|
@@ -4,21 +4,28 @@ Spoken-language detection tells the agent which ISO 639-1 code it heard. It does
|
|
|
4
4
|
|
|
5
5
|
## Mode A — Manual switch (default)
|
|
6
6
|
|
|
7
|
-
Leave the project **spoken-language auto-switch** setting off.
|
|
7
|
+
Leave the project **spoken-language auto-switch** setting off. The agent runs the whole switch with three primitives, and the caller is not interrupted:
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
1. `onUserLanguage` fires while the caller is still talking. The agent calls `prepareVoiceLanguage` so the target voice and STT pools warm up, and waits.
|
|
10
|
+
2. The caller's next final is the switch utterance. The agent does not answer it. If the pools are ready it calls `setVoiceLanguage` at once. If not, it first speaks a wait message in the current voice (`One moment please, I'm switching to your language.`), awaits the pools, then swaps.
|
|
11
|
+
3. `replayLastUtterance` makes the runner run that utterance through the new STT. The replayed final arrives with `replay: true` and is answered in the new language.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
When the switch took at least `readyMessageMinMs` (default 2000) and no wait message was played, the agent speaks a short ready message in the new voice (`Okay, machen wir auf Deutsch weiter.`) before the replay. The project can also set `waitMessageMode`: `end_of_utterance` (default), `immediate` (wait message at detection, not interruptible) or `off`. The settings come from `getVoiceLanguageSwitchSettings`; without them (older runner) the defaults above apply.
|
|
13
14
|
|
|
14
|
-
The
|
|
15
|
+
The swap waits until the wait message has finished playing (at most 8 seconds). If the language is detected after the caller's last final is already unanswered, that final is the switch utterance and the switch starts at once. If prepare or the swap fails, the agent speaks a short fallback in the old language and stays. If only the replay fails, the voice is already the new one, so the agent asks the caller in the new language to repeat (`REPEAT_MESSAGES`) and stays in it. A second language event during a switch is ignored.
|
|
16
|
+
|
|
17
|
+
The built-in speech vendor is VoiceThere (id `local-sherpa`). Edit `PROFILES` in `agent.ts` to apply other vendors per target language, for example Deepgram for listening and ElevenLabs for speaking; the file has a commented example. Keys stay in project secrets.
|
|
18
|
+
|
|
19
|
+
Texts, in order of precedence: the env override, the project settings (`waitMessages`, `readyMessages`), then the maps in `agent.ts` (`WAIT_MESSAGES`, `READY_MESSAGES`, `FAILED_MESSAGES`; en, de, es, fr, it, pt, nl, pl, ru). Override per language with a JSON object in the session env, keyed by ISO 639-1 code:
|
|
15
20
|
|
|
16
21
|
- `LANGUAGE_SWITCH_WAIT_MESSAGES_JSON` — wait message, for example `{"en":"Hold on, switching languages."}`
|
|
17
22
|
- `LANGUAGE_SWITCH_FAILED_MESSAGES_JSON` — fallback after a failed switch
|
|
18
23
|
|
|
19
|
-
Invalid JSON or non-string values are ignored
|
|
24
|
+
Invalid JSON or non-string values are ignored. A language without a text falls back to English.
|
|
25
|
+
|
|
26
|
+
Every step logs a `language_switch.<decision>` line with structured fields and never the caller's words: `session_start`, `detected`, `prepare`, `wait_message`, `swap`, `ready_message`, `replay`, `committed`, `failed`.
|
|
20
27
|
|
|
21
|
-
A
|
|
28
|
+
A VoiceThere language with no STT id (`it`, `pt`, `nl`, `pl`, `hi`) fails the STT call and still switches TTS.
|
|
22
29
|
|
|
23
30
|
## Mode B — Runner auto-switch
|
|
24
31
|
|
|
@@ -30,13 +37,13 @@ Detection and chat commands are unchanged: `/tts` and `/stt` still call `setVoic
|
|
|
30
37
|
|
|
31
38
|
Chat commands change one vendor while the session stays connected. API keys are project secrets on the running deploy, not arguments:
|
|
32
39
|
|
|
33
|
-
- `/tts sherpa de` —
|
|
34
|
-
- `/stt sherpa de` —
|
|
40
|
+
- `/tts sherpa de` — VoiceThere TTS only
|
|
41
|
+
- `/stt sherpa de` — VoiceThere STT only
|
|
35
42
|
- `/tts elevenlabs` — ElevenLabs TTS, current STT stays
|
|
36
43
|
- `/stt deepgram de` — Deepgram STT, current voice stays
|
|
37
44
|
|
|
38
|
-
In manual mode,
|
|
45
|
+
In manual mode, finals are echoed as `you said: …` (in the current language) after a short wait, so a language event for the same utterance can cancel the echo. The switch utterance is not spoken back as an English transcript; its replay is answered in the new language.
|
|
39
46
|
|
|
40
|
-
`voice` and `stt` are
|
|
47
|
+
`voice` and `stt` are VoiceThere catalog ids (`de`, `en-lessac`, `en-small`), listed in the spoken-language docs. Vendor ids are `local-sherpa`, `openai`, `deepgram`, `assemblyai`, `google`, `elevenlabs`, and `cartesia`.
|
|
41
48
|
|
|
42
49
|
Entry: `templates/language-switch/agent.ts`.
|