@voicethere/agent 0.8.3 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -20
- package/dist/agent.js +55 -2
- 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 +81 -3
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/runtime.d.ts +69 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +152 -1
- package/dist/runtime.js.map +1 -1
- package/dist/templates/echo/agent.js +51 -2
- package/dist/templates/echo-dc/agent.js +51 -2
- package/dist/templates/game-sync/agent.js +51 -2
- package/dist/templates/language-switch/agent.js +1112 -0
- package/dist/templates/positional-tts/agent.js +51 -2
- package/dist/templates/recording-consent/agent.js +51 -2
- package/dist/templates/registry.d.ts.map +1 -1
- package/dist/templates/registry.js +8 -0
- package/dist/templates/registry.js.map +1 -1
- package/dist/templates/spatial-showcase/agent.js +51 -2
- package/dist/templates/voice-showcase/agent.js +51 -2
- package/dist/templates/voice-starter/agent.js +51 -2
- package/dist/templates/webhooks/agent.js +51 -2
- package/dist/templates/webhooks-redis/agent.js +51 -2
- package/dist/templates/world-sync/agent.js +51 -2
- package/dist/templates/world-sync-binary/agent.js +51 -2
- package/package.json +1 -1
- package/templates/README.md +31 -30
- package/templates/language-switch/README.md +31 -0
- package/templates/language-switch/agent.ts +233 -0
- package/templates/voice-starter/agent.ts +3 -1
|
@@ -0,0 +1,1112 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
// dist/runtime.js
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
8
|
+
|
|
9
|
+
// dist/session-serial-queue.js
|
|
10
|
+
var SessionSerialQueueCancellationError = class extends Error {
|
|
11
|
+
sessionId;
|
|
12
|
+
generation;
|
|
13
|
+
code = "SESSION_SERIAL_QUEUE_CANCELLED";
|
|
14
|
+
constructor(sessionId, generation) {
|
|
15
|
+
super(`session serial queue cancelled (sessionId=${sessionId}, generation=${generation})`);
|
|
16
|
+
this.sessionId = sessionId;
|
|
17
|
+
this.generation = generation;
|
|
18
|
+
this.name = "SessionSerialQueueCancellationError";
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var SessionSerialQueue = class {
|
|
22
|
+
sessions = /* @__PURE__ */ new Map();
|
|
23
|
+
/** Monotonic token source — never reused across clear/enqueue cycles. */
|
|
24
|
+
nextGeneration = 1;
|
|
25
|
+
/**
|
|
26
|
+
* Enqueue work for `sessionId`. Returns a promise that settles when this
|
|
27
|
+
* task finishes, is skipped as stale, or is cancelled by {@link clear}.
|
|
28
|
+
* Fire-and-forget callers may ignore the return value (compat).
|
|
29
|
+
*/
|
|
30
|
+
enqueue(sessionId, task) {
|
|
31
|
+
const state = this.ensureState(sessionId);
|
|
32
|
+
const captured = state;
|
|
33
|
+
const generation = captured.generation;
|
|
34
|
+
captured.pending += 1;
|
|
35
|
+
const previous = captured.tail;
|
|
36
|
+
let taskAbort = null;
|
|
37
|
+
const settled = previous.catch(() => void 0).then(async () => {
|
|
38
|
+
if (this.sessions.get(sessionId) !== captured) {
|
|
39
|
+
return "cancelled";
|
|
40
|
+
}
|
|
41
|
+
const abort = new AbortController();
|
|
42
|
+
taskAbort = abort;
|
|
43
|
+
captured.abort = abort;
|
|
44
|
+
const context = {
|
|
45
|
+
sessionId,
|
|
46
|
+
generation,
|
|
47
|
+
signal: abort.signal
|
|
48
|
+
};
|
|
49
|
+
try {
|
|
50
|
+
await Promise.resolve(task(abort.signal, context));
|
|
51
|
+
if (this.sessions.get(sessionId) !== captured) {
|
|
52
|
+
return "cancelled";
|
|
53
|
+
}
|
|
54
|
+
return "completed";
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (abort.signal.aborted || this.sessions.get(sessionId) !== captured || error instanceof SessionSerialQueueCancellationError) {
|
|
57
|
+
return "cancelled";
|
|
58
|
+
}
|
|
59
|
+
return "failed";
|
|
60
|
+
} finally {
|
|
61
|
+
if (captured.abort === abort) {
|
|
62
|
+
captured.abort = null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}).finally(() => {
|
|
66
|
+
captured.pending = Math.max(0, captured.pending - 1);
|
|
67
|
+
if (taskAbort && captured.abort === taskAbort) {
|
|
68
|
+
captured.abort = null;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
captured.tail = settled;
|
|
72
|
+
return settled;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Invalidate queued + running work for `sessionId`. Aborts the live AbortSignal,
|
|
76
|
+
* drops the map entry, and leaves a fresh identity for the next enqueue.
|
|
77
|
+
* Old in-flight `finally` blocks only touch their captured state object.
|
|
78
|
+
*/
|
|
79
|
+
clear(sessionId) {
|
|
80
|
+
const old = this.sessions.get(sessionId);
|
|
81
|
+
if (!old)
|
|
82
|
+
return;
|
|
83
|
+
old.abort?.abort();
|
|
84
|
+
old.abort = null;
|
|
85
|
+
this.sessions.delete(sessionId);
|
|
86
|
+
}
|
|
87
|
+
clearAll() {
|
|
88
|
+
for (const sessionId of [...this.sessions.keys()]) {
|
|
89
|
+
this.clear(sessionId);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
hasPending(sessionId) {
|
|
93
|
+
return (this.sessions.get(sessionId)?.pending ?? 0) > 0;
|
|
94
|
+
}
|
|
95
|
+
/** Live generation token for a session, or `undefined` when cleared. */
|
|
96
|
+
generationOf(sessionId) {
|
|
97
|
+
return this.sessions.get(sessionId)?.generation;
|
|
98
|
+
}
|
|
99
|
+
/** True when `generation` is the live map entry for `sessionId`. */
|
|
100
|
+
isCurrentGeneration(sessionId, generation) {
|
|
101
|
+
return this.sessions.get(sessionId)?.generation === generation;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* True when a queue row exists for the session (registered since first
|
|
105
|
+
* enqueue, until {@link clear}). Not the same as {@link hasPending}.
|
|
106
|
+
*/
|
|
107
|
+
isLive(sessionId) {
|
|
108
|
+
return this.sessions.has(sessionId);
|
|
109
|
+
}
|
|
110
|
+
/** Registered session rows (must not grow without matching `clear` calls). */
|
|
111
|
+
get activeSessionCount() {
|
|
112
|
+
return this.sessions.size;
|
|
113
|
+
}
|
|
114
|
+
ensureState(sessionId) {
|
|
115
|
+
const existing = this.sessions.get(sessionId);
|
|
116
|
+
if (existing)
|
|
117
|
+
return existing;
|
|
118
|
+
const created = {
|
|
119
|
+
generation: this.nextGeneration++,
|
|
120
|
+
tail: Promise.resolve(),
|
|
121
|
+
abort: null,
|
|
122
|
+
pending: 0
|
|
123
|
+
};
|
|
124
|
+
this.sessions.set(sessionId, created);
|
|
125
|
+
return created;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// dist/runtime.js
|
|
130
|
+
var SESSION_START_INIT_DELAY_ENABLED_ENV = "AGENT_SESSION_START_INIT_DELAY_ENABLED";
|
|
131
|
+
var SESSION_START_INIT_DELAY_MS_ENV = "AGENT_SESSION_START_INIT_DELAY_MS";
|
|
132
|
+
var DEFAULT_SESSION_START_INIT_DELAY_MS = 500;
|
|
133
|
+
function isPlayAckMessage(value) {
|
|
134
|
+
if (!value || typeof value !== "object")
|
|
135
|
+
return false;
|
|
136
|
+
const msg = value;
|
|
137
|
+
return msg.type === "play_ack" && typeof msg.requestId === "string";
|
|
138
|
+
}
|
|
139
|
+
function isPlayStatusAckMessage(value) {
|
|
140
|
+
if (!value || typeof value !== "object")
|
|
141
|
+
return false;
|
|
142
|
+
const msg = value;
|
|
143
|
+
return msg.type === "play_status_ack" && typeof msg.requestId === "string";
|
|
144
|
+
}
|
|
145
|
+
function isPlayStopAckMessage(value) {
|
|
146
|
+
if (!value || typeof value !== "object")
|
|
147
|
+
return false;
|
|
148
|
+
const msg = value;
|
|
149
|
+
return msg.type === "play_stop_ack" && typeof msg.requestId === "string";
|
|
150
|
+
}
|
|
151
|
+
function isPlayPoseAckMessage(value) {
|
|
152
|
+
if (!value || typeof value !== "object")
|
|
153
|
+
return false;
|
|
154
|
+
const msg = value;
|
|
155
|
+
return msg.type === "play_pose_ack" && typeof msg.requestId === "string";
|
|
156
|
+
}
|
|
157
|
+
function isRecordingControlAckMessage(value) {
|
|
158
|
+
if (!value || typeof value !== "object")
|
|
159
|
+
return false;
|
|
160
|
+
const msg = value;
|
|
161
|
+
return msg.type === "recording_control_ack" && typeof msg.requestId === "string";
|
|
162
|
+
}
|
|
163
|
+
function isConversationHistoryControlAckMessage(value) {
|
|
164
|
+
if (!value || typeof value !== "object")
|
|
165
|
+
return false;
|
|
166
|
+
const msg = value;
|
|
167
|
+
return msg.type === "conversation_history_control_ack" && typeof msg.requestId === "string";
|
|
168
|
+
}
|
|
169
|
+
function isMixControlAckMessage(value) {
|
|
170
|
+
if (!value || typeof value !== "object")
|
|
171
|
+
return false;
|
|
172
|
+
const msg = value;
|
|
173
|
+
return msg.type === "mix_control_ack" && typeof msg.requestId === "string";
|
|
174
|
+
}
|
|
175
|
+
function isSttControlAckMessage(value) {
|
|
176
|
+
if (!value || typeof value !== "object")
|
|
177
|
+
return false;
|
|
178
|
+
const msg = value;
|
|
179
|
+
return msg.type === "stt_control_ack" && typeof msg.requestId === "string";
|
|
180
|
+
}
|
|
181
|
+
function isVoiceLanguageControlAckMessage(value) {
|
|
182
|
+
if (!value || typeof value !== "object")
|
|
183
|
+
return false;
|
|
184
|
+
const msg = value;
|
|
185
|
+
return msg.type === "voice_language_control_ack" && typeof msg.requestId === "string";
|
|
186
|
+
}
|
|
187
|
+
function isWebhookMessage(value) {
|
|
188
|
+
if (!value || typeof value !== "object")
|
|
189
|
+
return false;
|
|
190
|
+
const msg = value;
|
|
191
|
+
return msg.type === "webhook" && typeof msg.eventId === "string" && typeof msg.projectId === "string";
|
|
192
|
+
}
|
|
193
|
+
function coerceInboundBinary(value) {
|
|
194
|
+
if (!value)
|
|
195
|
+
return null;
|
|
196
|
+
if (Buffer.isBuffer(value))
|
|
197
|
+
return value;
|
|
198
|
+
if (value instanceof Uint8Array) {
|
|
199
|
+
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
200
|
+
}
|
|
201
|
+
if (value instanceof ArrayBuffer)
|
|
202
|
+
return Buffer.from(value);
|
|
203
|
+
if (ArrayBuffer.isView(value)) {
|
|
204
|
+
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
205
|
+
}
|
|
206
|
+
if (typeof value === "object") {
|
|
207
|
+
const maybeBufferLike = value;
|
|
208
|
+
if (maybeBufferLike.type === "Buffer" && Array.isArray(maybeBufferLike.data)) {
|
|
209
|
+
return Buffer.from(maybeBufferLike.data);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
function normalizeWebhookHeaders(value) {
|
|
215
|
+
if (!value || typeof value !== "object")
|
|
216
|
+
return {};
|
|
217
|
+
const out = {};
|
|
218
|
+
for (const [key, raw] of Object.entries(value)) {
|
|
219
|
+
if (typeof raw === "string") {
|
|
220
|
+
out[key] = raw;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
function isParentMessage(value) {
|
|
226
|
+
if (!value || typeof value !== "object")
|
|
227
|
+
return false;
|
|
228
|
+
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";
|
|
230
|
+
}
|
|
231
|
+
function isSessionScopedParentMessage(value) {
|
|
232
|
+
return isParentMessage(value) && !isWebhookMessage(value);
|
|
233
|
+
}
|
|
234
|
+
function parseDataChannelPayload(raw) {
|
|
235
|
+
try {
|
|
236
|
+
return JSON.parse(raw);
|
|
237
|
+
} catch {
|
|
238
|
+
return raw;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
var peerEnvBySessionId = /* @__PURE__ */ new Map();
|
|
242
|
+
var recordingAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
243
|
+
var conversationHistoryAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
244
|
+
var mixAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
245
|
+
var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
246
|
+
var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
|
|
247
|
+
var ttsPoseAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
248
|
+
var endedSessionIds = /* @__PURE__ */ new Set();
|
|
249
|
+
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
250
|
+
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
251
|
+
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
252
|
+
var pendingPlayAcks = /* @__PURE__ */ new Map();
|
|
253
|
+
var pendingPlayStatusAcks = /* @__PURE__ */ new Map();
|
|
254
|
+
var pendingPlayStopAcks = /* @__PURE__ */ new Map();
|
|
255
|
+
var pendingPlayPoseAcks = /* @__PURE__ */ new Map();
|
|
256
|
+
var pendingMixAcks = /* @__PURE__ */ new Map();
|
|
257
|
+
var pendingSttAcks = /* @__PURE__ */ new Map();
|
|
258
|
+
var VOICE_LANGUAGE_CONTROL_ACK_TIMEOUT_MS = 66e4;
|
|
259
|
+
var pendingVoiceLanguageAcks = /* @__PURE__ */ new Map();
|
|
260
|
+
function handleRecordingControlAck(message) {
|
|
261
|
+
const pending = pendingRecordingAcks.get(message.requestId);
|
|
262
|
+
if (!pending)
|
|
263
|
+
return;
|
|
264
|
+
clearTimeout(pending.timer);
|
|
265
|
+
pendingRecordingAcks.delete(message.requestId);
|
|
266
|
+
pending.resolve({
|
|
267
|
+
ok: message.ok,
|
|
268
|
+
reason: message.reason,
|
|
269
|
+
requestId: message.requestId
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
function handleConversationHistoryControlAck(message) {
|
|
273
|
+
const pending = pendingConversationHistoryAcks.get(message.requestId);
|
|
274
|
+
if (!pending)
|
|
275
|
+
return;
|
|
276
|
+
clearTimeout(pending.timer);
|
|
277
|
+
pendingConversationHistoryAcks.delete(message.requestId);
|
|
278
|
+
pending.resolve({
|
|
279
|
+
ok: message.ok,
|
|
280
|
+
reason: message.reason,
|
|
281
|
+
requestId: message.requestId
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
function handlePlayAck(message) {
|
|
285
|
+
const pending = pendingPlayAcks.get(message.requestId);
|
|
286
|
+
if (!pending)
|
|
287
|
+
return;
|
|
288
|
+
clearTimeout(pending.timer);
|
|
289
|
+
pendingPlayAcks.delete(message.requestId);
|
|
290
|
+
pending.resolve({
|
|
291
|
+
ok: message.ok,
|
|
292
|
+
...message.playId ? { playId: message.playId } : {},
|
|
293
|
+
reason: message.reason,
|
|
294
|
+
requestId: message.requestId
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
function handlePlayStatusAck(message) {
|
|
298
|
+
const pending = pendingPlayStatusAcks.get(message.requestId);
|
|
299
|
+
if (!pending)
|
|
300
|
+
return;
|
|
301
|
+
clearTimeout(pending.timer);
|
|
302
|
+
pendingPlayStatusAcks.delete(message.requestId);
|
|
303
|
+
pending.resolve({
|
|
304
|
+
ok: message.ok,
|
|
305
|
+
playId: message.playId,
|
|
306
|
+
...message.status ? { status: message.status } : {},
|
|
307
|
+
reason: message.reason,
|
|
308
|
+
requestId: message.requestId
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
function handlePlayStopAck(message) {
|
|
312
|
+
const pending = pendingPlayStopAcks.get(message.requestId);
|
|
313
|
+
if (!pending)
|
|
314
|
+
return;
|
|
315
|
+
clearTimeout(pending.timer);
|
|
316
|
+
pendingPlayStopAcks.delete(message.requestId);
|
|
317
|
+
pending.resolve({
|
|
318
|
+
ok: message.ok,
|
|
319
|
+
playId: message.playId,
|
|
320
|
+
reason: message.reason,
|
|
321
|
+
requestId: message.requestId
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
function handlePlayPoseAck(message) {
|
|
325
|
+
const pending = pendingPlayPoseAcks.get(message.requestId);
|
|
326
|
+
if (!pending)
|
|
327
|
+
return;
|
|
328
|
+
clearTimeout(pending.timer);
|
|
329
|
+
pendingPlayPoseAcks.delete(message.requestId);
|
|
330
|
+
pending.resolve({
|
|
331
|
+
ok: message.ok,
|
|
332
|
+
playId: message.playId,
|
|
333
|
+
reason: message.reason,
|
|
334
|
+
requestId: message.requestId
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
function handleMixControlAck(message) {
|
|
338
|
+
const pending = pendingMixAcks.get(message.requestId);
|
|
339
|
+
if (!pending)
|
|
340
|
+
return;
|
|
341
|
+
clearTimeout(pending.timer);
|
|
342
|
+
pendingMixAcks.delete(message.requestId);
|
|
343
|
+
pending.resolve({
|
|
344
|
+
ok: message.ok,
|
|
345
|
+
reason: message.reason,
|
|
346
|
+
requestId: message.requestId,
|
|
347
|
+
...message.statuses ? { statuses: message.statuses } : {}
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
function handleSttControlAck(message) {
|
|
351
|
+
const pending = pendingSttAcks.get(message.requestId);
|
|
352
|
+
if (!pending)
|
|
353
|
+
return;
|
|
354
|
+
clearTimeout(pending.timer);
|
|
355
|
+
pendingSttAcks.delete(message.requestId);
|
|
356
|
+
pending.resolve({
|
|
357
|
+
ok: message.ok,
|
|
358
|
+
reason: message.reason,
|
|
359
|
+
requestId: message.requestId
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
function handleVoiceLanguageControlAck(message) {
|
|
363
|
+
const pending = pendingVoiceLanguageAcks.get(message.requestId);
|
|
364
|
+
if (!pending)
|
|
365
|
+
return;
|
|
366
|
+
clearTimeout(pending.timer);
|
|
367
|
+
pendingVoiceLanguageAcks.delete(message.requestId);
|
|
368
|
+
if (message.ok && message.language?.trim()) {
|
|
369
|
+
lastVoiceLanguageBySessionId.set(message.sessionId, message.language.trim());
|
|
370
|
+
}
|
|
371
|
+
pending.resolve({
|
|
372
|
+
ok: message.ok,
|
|
373
|
+
reason: message.reason,
|
|
374
|
+
requestId: message.requestId,
|
|
375
|
+
...message.language ? { language: message.language } : {},
|
|
376
|
+
...message.voice ? { voice: message.voice } : {},
|
|
377
|
+
...message.stt ? { stt: message.stt } : {},
|
|
378
|
+
...message.scope ? { scope: message.scope } : {},
|
|
379
|
+
...message.sttProvider ? { sttProvider: message.sttProvider } : {},
|
|
380
|
+
...message.ttsProvider ? { ttsProvider: message.ttsProvider } : {}
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
function clearPendingRecordingAcksForSession(sessionId, reason) {
|
|
384
|
+
for (const [requestId, pending] of pendingRecordingAcks) {
|
|
385
|
+
if (pending.sessionId !== sessionId)
|
|
386
|
+
continue;
|
|
387
|
+
clearTimeout(pending.timer);
|
|
388
|
+
pendingRecordingAcks.delete(requestId);
|
|
389
|
+
pending.resolve({ ok: false, reason, requestId });
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
function clearPendingConversationHistoryAcksForSession(sessionId, reason) {
|
|
393
|
+
for (const [requestId, pending] of pendingConversationHistoryAcks) {
|
|
394
|
+
if (pending.sessionId !== sessionId)
|
|
395
|
+
continue;
|
|
396
|
+
clearTimeout(pending.timer);
|
|
397
|
+
pendingConversationHistoryAcks.delete(requestId);
|
|
398
|
+
pending.resolve({ ok: false, reason, requestId });
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
function isVoicethereAgentChild() {
|
|
402
|
+
return typeof process.send === "function" && process.connected !== false && typeof process.env.__CHILD_BUNDLE_PATH__ === "string" && process.env.__CHILD_BUNDLE_PATH__.trim().length > 0;
|
|
403
|
+
}
|
|
404
|
+
var sessionExecutionContext = new AsyncLocalStorage();
|
|
405
|
+
var agentLogSessionContext = new AsyncLocalStorage();
|
|
406
|
+
var inboundQueueAuthority = null;
|
|
407
|
+
var childUnhandledRejectionGuardInstalled = false;
|
|
408
|
+
function normalizeRejectionReason(reason) {
|
|
409
|
+
return reason instanceof Error ? reason : new Error(String(reason));
|
|
410
|
+
}
|
|
411
|
+
function installChildUnhandledRejectionGuard() {
|
|
412
|
+
if (childUnhandledRejectionGuardInstalled) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
childUnhandledRejectionGuardInstalled = true;
|
|
416
|
+
process.on("unhandledRejection", (reason) => {
|
|
417
|
+
const err = normalizeRejectionReason(reason);
|
|
418
|
+
const store = sessionExecutionContext.getStore();
|
|
419
|
+
const sessionId = store?.sessionId ?? agentLogSessionContext.getStore() ?? "";
|
|
420
|
+
if (!allowOutboundForSession(sessionId || void 0)) {
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
agentLog("error", `unhandledRejection: ${err.message}`, sessionId || void 0);
|
|
424
|
+
sendParentMessage({
|
|
425
|
+
type: "agent_error",
|
|
426
|
+
sessionId,
|
|
427
|
+
message: err.message,
|
|
428
|
+
stack: err.stack
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
function allowOutboundForSession(sessionId) {
|
|
433
|
+
if (!sessionId) {
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
const store = sessionExecutionContext.getStore();
|
|
437
|
+
const queue = inboundQueueAuthority;
|
|
438
|
+
if (store && store.sessionId === sessionId) {
|
|
439
|
+
if (!queue)
|
|
440
|
+
return !endedSessionIds.has(sessionId);
|
|
441
|
+
return queue.isCurrentGeneration(sessionId, store.generation);
|
|
442
|
+
}
|
|
443
|
+
if (endedSessionIds.has(sessionId))
|
|
444
|
+
return false;
|
|
445
|
+
if (!queue)
|
|
446
|
+
return true;
|
|
447
|
+
return queue.isLive(sessionId);
|
|
448
|
+
}
|
|
449
|
+
function sendParentMessage(message) {
|
|
450
|
+
const msgType = message && typeof message === "object" && "type" in message ? message.type : void 0;
|
|
451
|
+
if (msgType === "mix_control" || msgType === "stt_control" || msgType === "voice_language_control") {
|
|
452
|
+
process.send?.(message);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
if (msgType === "play" || msgType === "play_status" || msgType === "play_stop" || msgType === "play_pose") {
|
|
456
|
+
process.send?.(message);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const sessionId = message && typeof message === "object" && "sessionId" in message && typeof message.sessionId === "string" ? message.sessionId : void 0;
|
|
460
|
+
if (!allowOutboundForSession(sessionId))
|
|
461
|
+
return;
|
|
462
|
+
process.send?.(message);
|
|
463
|
+
}
|
|
464
|
+
function parseBooleanEnv(value, defaultValue) {
|
|
465
|
+
if (value === void 0)
|
|
466
|
+
return defaultValue;
|
|
467
|
+
const normalized = value.trim().toLowerCase();
|
|
468
|
+
if (normalized === "")
|
|
469
|
+
return defaultValue;
|
|
470
|
+
if (["0", "false", "off", "no"].includes(normalized))
|
|
471
|
+
return false;
|
|
472
|
+
if (["1", "true", "on", "yes"].includes(normalized))
|
|
473
|
+
return true;
|
|
474
|
+
return defaultValue;
|
|
475
|
+
}
|
|
476
|
+
function parseNonNegativeIntegerEnv(value, defaultValue) {
|
|
477
|
+
if (value === void 0)
|
|
478
|
+
return defaultValue;
|
|
479
|
+
const parsed = Number(value);
|
|
480
|
+
if (!Number.isFinite(parsed) || parsed < 0)
|
|
481
|
+
return defaultValue;
|
|
482
|
+
return Math.floor(parsed);
|
|
483
|
+
}
|
|
484
|
+
function resolveSessionStartInitDelayMs() {
|
|
485
|
+
const enabled = parseBooleanEnv(process.env[SESSION_START_INIT_DELAY_ENABLED_ENV], true);
|
|
486
|
+
if (!enabled)
|
|
487
|
+
return 0;
|
|
488
|
+
return parseNonNegativeIntegerEnv(process.env[SESSION_START_INIT_DELAY_MS_ENV], DEFAULT_SESSION_START_INIT_DELAY_MS);
|
|
489
|
+
}
|
|
490
|
+
async function handleWebhookMessage(message, handlers) {
|
|
491
|
+
if (!handlers.onWebhook)
|
|
492
|
+
return;
|
|
493
|
+
const body = coerceInboundBinary(message.body);
|
|
494
|
+
if (!body) {
|
|
495
|
+
agentLog("warn", "webhook ipc dropped: body is not binary");
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const ctx = {
|
|
499
|
+
eventId: message.eventId,
|
|
500
|
+
projectId: message.projectId,
|
|
501
|
+
method: typeof message.method === "string" ? message.method : "POST",
|
|
502
|
+
path: typeof message.path === "string" ? message.path : "",
|
|
503
|
+
headers: normalizeWebhookHeaders(message.headers),
|
|
504
|
+
body,
|
|
505
|
+
contentType: typeof message.contentType === "string" ? message.contentType : null,
|
|
506
|
+
receivedAt: typeof message.receivedAt === "string" ? message.receivedAt : "",
|
|
507
|
+
sessionIds: Array.isArray(message.sessionIds) ? message.sessionIds.filter((id) => typeof id === "string" && id.length > 0) : []
|
|
508
|
+
};
|
|
509
|
+
try {
|
|
510
|
+
const started = Date.now();
|
|
511
|
+
await handlers.onWebhook(ctx);
|
|
512
|
+
sendParentMessage({
|
|
513
|
+
type: "webhook_handled",
|
|
514
|
+
projectId: message.projectId,
|
|
515
|
+
eventId: message.eventId,
|
|
516
|
+
durationMs: Date.now() - started
|
|
517
|
+
});
|
|
518
|
+
} catch (error) {
|
|
519
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
520
|
+
await runErrorHook(handlers, {
|
|
521
|
+
sessionId: "",
|
|
522
|
+
projectId: message.projectId,
|
|
523
|
+
env: process.env,
|
|
524
|
+
error: err
|
|
525
|
+
});
|
|
526
|
+
sendParentMessage({
|
|
527
|
+
type: "agent_error",
|
|
528
|
+
sessionId: "",
|
|
529
|
+
message: err.message,
|
|
530
|
+
stack: err.stack
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
function resolveUserLanguageCode(event) {
|
|
535
|
+
const withLanguage = event;
|
|
536
|
+
const candidates = [withLanguage.language, event.text];
|
|
537
|
+
for (const value of candidates) {
|
|
538
|
+
if (typeof value === "string" && value.trim()) {
|
|
539
|
+
return value.trim();
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return void 0;
|
|
543
|
+
}
|
|
544
|
+
async function handleParentMessage(message, handlers) {
|
|
545
|
+
switch (message.type) {
|
|
546
|
+
case "session_start":
|
|
547
|
+
endedSessionIds.delete(message.sessionId);
|
|
548
|
+
peerEnvBySessionId.set(message.sessionId, message.env);
|
|
549
|
+
recordingAvailableBySessionId.set(message.sessionId, message.recordingAvailable ?? false);
|
|
550
|
+
conversationHistoryAvailableBySessionId.set(message.sessionId, message.conversationHistoryAvailable ?? false);
|
|
551
|
+
mixAvailableBySessionId.set(message.sessionId, message.mixAvailable ?? false);
|
|
552
|
+
voiceLanguageSwitchAvailableBySessionId.set(message.sessionId, message.voiceLanguageSwitchAvailable ?? false);
|
|
553
|
+
ttsPoseAvailableBySessionId.set(message.sessionId, message.ttsPoseAvailable ?? false);
|
|
554
|
+
const sessionStartInitDelayMs = resolveSessionStartInitDelayMs();
|
|
555
|
+
if (sessionStartInitDelayMs > 0) {
|
|
556
|
+
await new Promise((resolve) => setTimeout(resolve, sessionStartInitDelayMs));
|
|
557
|
+
}
|
|
558
|
+
await (handlers.onClientJoin ?? handlers.onSessionStart)?.({
|
|
559
|
+
sessionId: message.sessionId,
|
|
560
|
+
env: message.env,
|
|
561
|
+
recordingAvailable: message.recordingAvailable ?? false,
|
|
562
|
+
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
563
|
+
mixAvailable: message.mixAvailable ?? false,
|
|
564
|
+
voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
|
|
565
|
+
ttsPoseAvailable: message.ttsPoseAvailable ?? false
|
|
566
|
+
});
|
|
567
|
+
sendParentMessage({
|
|
568
|
+
type: "session_start_ack",
|
|
569
|
+
sessionId: message.sessionId
|
|
570
|
+
});
|
|
571
|
+
break;
|
|
572
|
+
case "speech_event":
|
|
573
|
+
await handlers.onSpeechEvent?.({ sessionId: message.sessionId }, message.event);
|
|
574
|
+
if (message.event.type === "user_speech_final" && typeof message.event.text === "string" && message.event.text.trim()) {
|
|
575
|
+
await handlers.onUserSpeechFinal?.({
|
|
576
|
+
sessionId: message.sessionId,
|
|
577
|
+
text: message.event.text.trim()
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
if (message.event.type === "user_language") {
|
|
581
|
+
const language = resolveUserLanguageCode(message.event);
|
|
582
|
+
if (language) {
|
|
583
|
+
lastVoiceLanguageBySessionId.set(message.sessionId, language);
|
|
584
|
+
await handlers.onUserLanguage?.({
|
|
585
|
+
sessionId: message.sessionId,
|
|
586
|
+
language
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (message.event.type === "voice_language_changed") {
|
|
591
|
+
const language = resolveUserLanguageCode(message.event);
|
|
592
|
+
if (language) {
|
|
593
|
+
lastVoiceLanguageBySessionId.set(message.sessionId, language);
|
|
594
|
+
await handlers.onVoiceLanguageChanged?.({
|
|
595
|
+
sessionId: message.sessionId,
|
|
596
|
+
language
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
break;
|
|
601
|
+
case "data_channel_message":
|
|
602
|
+
await handlers.onDataChannelMessage?.({
|
|
603
|
+
sessionId: message.sessionId,
|
|
604
|
+
message: parseDataChannelPayload(message.payload),
|
|
605
|
+
raw: message.payload,
|
|
606
|
+
rawBinary: null,
|
|
607
|
+
channel: "control"
|
|
608
|
+
});
|
|
609
|
+
break;
|
|
610
|
+
case "data_channel_binary":
|
|
611
|
+
await handlers.onDataChannelBinary?.({
|
|
612
|
+
sessionId: message.sessionId,
|
|
613
|
+
message: null,
|
|
614
|
+
raw: null,
|
|
615
|
+
rawBinary: message.data,
|
|
616
|
+
channel: message.channel ?? "sync"
|
|
617
|
+
});
|
|
618
|
+
break;
|
|
619
|
+
case "session_end":
|
|
620
|
+
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
621
|
+
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
622
|
+
peerEnvBySessionId.delete(message.sessionId);
|
|
623
|
+
recordingAvailableBySessionId.delete(message.sessionId);
|
|
624
|
+
conversationHistoryAvailableBySessionId.delete(message.sessionId);
|
|
625
|
+
mixAvailableBySessionId.delete(message.sessionId);
|
|
626
|
+
voiceLanguageSwitchAvailableBySessionId.delete(message.sessionId);
|
|
627
|
+
lastVoiceLanguageBySessionId.delete(message.sessionId);
|
|
628
|
+
ttsPoseAvailableBySessionId.delete(message.sessionId);
|
|
629
|
+
await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
|
|
630
|
+
sessionId: message.sessionId
|
|
631
|
+
});
|
|
632
|
+
break;
|
|
633
|
+
case "idle_timeout":
|
|
634
|
+
await runIdleTimeoutHook(handlers, message);
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
function defineAgent(handlers) {
|
|
639
|
+
installChildUnhandledRejectionGuard();
|
|
640
|
+
const inboundBySession = new SessionSerialQueue();
|
|
641
|
+
inboundQueueAuthority = inboundBySession;
|
|
642
|
+
const agentStartReady = runAgentStartHook(handlers);
|
|
643
|
+
process.on("message", (message) => {
|
|
644
|
+
if (isRecordingControlAckMessage(message)) {
|
|
645
|
+
handleRecordingControlAck(message);
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
if (isConversationHistoryControlAckMessage(message)) {
|
|
649
|
+
handleConversationHistoryControlAck(message);
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
if (isPlayAckMessage(message)) {
|
|
653
|
+
handlePlayAck(message);
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
if (isPlayStatusAckMessage(message)) {
|
|
657
|
+
handlePlayStatusAck(message);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
if (isPlayStopAckMessage(message)) {
|
|
661
|
+
handlePlayStopAck(message);
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
if (isPlayPoseAckMessage(message)) {
|
|
665
|
+
handlePlayPoseAck(message);
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
if (isMixControlAckMessage(message)) {
|
|
669
|
+
handleMixControlAck(message);
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
if (isSttControlAckMessage(message)) {
|
|
673
|
+
handleSttControlAck(message);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
if (isVoiceLanguageControlAckMessage(message)) {
|
|
677
|
+
handleVoiceLanguageControlAck(message);
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
if (isWebhookMessage(message)) {
|
|
681
|
+
void agentStartReady.then(() => handleWebhookMessage(message, handlers));
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
if (!isSessionScopedParentMessage(message))
|
|
685
|
+
return;
|
|
686
|
+
if (message.type === "session_end") {
|
|
687
|
+
endedSessionIds.add(message.sessionId);
|
|
688
|
+
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
689
|
+
clearPendingConversationHistoryAcksForSession(message.sessionId, "session_ended");
|
|
690
|
+
inboundBySession.clear(message.sessionId);
|
|
691
|
+
}
|
|
692
|
+
if (message.type === "session_start") {
|
|
693
|
+
endedSessionIds.delete(message.sessionId);
|
|
694
|
+
inboundBySession.clear(message.sessionId);
|
|
695
|
+
}
|
|
696
|
+
inboundBySession.enqueue(message.sessionId, async (_signal, context) => {
|
|
697
|
+
await agentStartReady;
|
|
698
|
+
try {
|
|
699
|
+
await sessionExecutionContext.run({
|
|
700
|
+
sessionId: message.sessionId,
|
|
701
|
+
generation: context.generation
|
|
702
|
+
}, async () => agentLogSessionContext.run(message.sessionId, async () => {
|
|
703
|
+
try {
|
|
704
|
+
await handleParentMessage(message, handlers);
|
|
705
|
+
} catch (error) {
|
|
706
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
707
|
+
const env = peerEnvBySessionId.get(message.sessionId) ?? buildIdleEnv(message.sessionId);
|
|
708
|
+
await runErrorHook(handlers, {
|
|
709
|
+
sessionId: message.sessionId,
|
|
710
|
+
projectId: env.PROJECT_ID,
|
|
711
|
+
buildId: env.BUILD_ID,
|
|
712
|
+
env,
|
|
713
|
+
error: err,
|
|
714
|
+
customerContext: parseCustomerContext(env.AGENT_CUSTOMER_CONTEXT)
|
|
715
|
+
});
|
|
716
|
+
sendParentMessage({
|
|
717
|
+
type: "agent_error",
|
|
718
|
+
sessionId: message.sessionId,
|
|
719
|
+
message: err.message,
|
|
720
|
+
stack: err.stack
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
}));
|
|
724
|
+
} finally {
|
|
725
|
+
if (message.type === "session_end" && inboundBySession.isCurrentGeneration(message.sessionId, context.generation)) {
|
|
726
|
+
inboundBySession.clear(message.sessionId);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
});
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
async function runAgentStartHook(handlers) {
|
|
733
|
+
if (!handlers.onAgentStart)
|
|
734
|
+
return;
|
|
735
|
+
try {
|
|
736
|
+
await handlers.onAgentStart({
|
|
737
|
+
env: process.env
|
|
738
|
+
});
|
|
739
|
+
} catch (error) {
|
|
740
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
741
|
+
agentLog("error", `onAgentStart failed: ${err.message}`);
|
|
742
|
+
sendParentMessage({
|
|
743
|
+
type: "agent_error",
|
|
744
|
+
sessionId: "",
|
|
745
|
+
message: err.message,
|
|
746
|
+
stack: err.stack
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
function parseCustomerContext(raw) {
|
|
751
|
+
if (!raw?.trim())
|
|
752
|
+
return void 0;
|
|
753
|
+
try {
|
|
754
|
+
const parsed = JSON.parse(raw);
|
|
755
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
756
|
+
return parsed;
|
|
757
|
+
}
|
|
758
|
+
} catch {
|
|
759
|
+
}
|
|
760
|
+
return void 0;
|
|
761
|
+
}
|
|
762
|
+
async function runErrorHook(handlers, ctx) {
|
|
763
|
+
if (!handlers.errorHook)
|
|
764
|
+
return;
|
|
765
|
+
try {
|
|
766
|
+
await handlers.errorHook(ctx);
|
|
767
|
+
} catch (hookError) {
|
|
768
|
+
const message = hookError instanceof Error ? hookError.message : String(hookError);
|
|
769
|
+
agentLog("error", `errorHook failed: ${message}`, ctx.sessionId);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
async function runIdleTimeoutHook(handlers, message) {
|
|
773
|
+
const onIdleTimeout = handlers.onIdleTimeout;
|
|
774
|
+
agentLog("info", `idle_timeout ipc received (maxGraceMs=${message.maxGraceMs}, onIdleTimeout=${typeof onIdleTimeout === "function"})`, message.sessionId);
|
|
775
|
+
if (!onIdleTimeout) {
|
|
776
|
+
sendParentMessage({
|
|
777
|
+
type: "idle_timeout_done",
|
|
778
|
+
sessionId: message.sessionId
|
|
779
|
+
});
|
|
780
|
+
agentLog("info", "idle_timeout_done ipc sent (no onIdleTimeout handler)", message.sessionId);
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
const env = peerEnvBySessionId.get(message.sessionId) ?? buildIdleEnv(message.sessionId);
|
|
784
|
+
const idleTimeoutSeconds = Number(env.IDLE_TIMEOUT_SEC) || 0;
|
|
785
|
+
const ctx = {
|
|
786
|
+
sessionId: message.sessionId,
|
|
787
|
+
projectId: env.PROJECT_ID,
|
|
788
|
+
buildId: env.BUILD_ID,
|
|
789
|
+
env,
|
|
790
|
+
idleTimeoutSeconds
|
|
791
|
+
};
|
|
792
|
+
let error;
|
|
793
|
+
try {
|
|
794
|
+
await onIdleTimeout(ctx);
|
|
795
|
+
} catch (hookError) {
|
|
796
|
+
error = hookError instanceof Error ? hookError.message : String(hookError);
|
|
797
|
+
agentLog("error", `onIdleTimeout failed: ${error}`, message.sessionId);
|
|
798
|
+
}
|
|
799
|
+
sendParentMessage({
|
|
800
|
+
type: "idle_timeout_done",
|
|
801
|
+
sessionId: message.sessionId,
|
|
802
|
+
error
|
|
803
|
+
});
|
|
804
|
+
agentLog("info", error ? `idle_timeout_done ipc sent (onIdleTimeout error: ${error})` : "idle_timeout_done ipc sent (onIdleTimeout completed)", message.sessionId);
|
|
805
|
+
}
|
|
806
|
+
function buildIdleEnv(sessionId) {
|
|
807
|
+
return {
|
|
808
|
+
SESSION_ID: sessionId,
|
|
809
|
+
...process.env.PROJECT_ID ? { PROJECT_ID: process.env.PROJECT_ID } : {},
|
|
810
|
+
...process.env.BUILD_ID ? { BUILD_ID: process.env.BUILD_ID } : {},
|
|
811
|
+
...process.env.IDLE_TIMEOUT_SEC ? { IDLE_TIMEOUT_SEC: process.env.IDLE_TIMEOUT_SEC } : {}
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
function speak(sessionId, text) {
|
|
815
|
+
sendParentMessage({ type: "speak", sessionId, text });
|
|
816
|
+
}
|
|
817
|
+
function isRunnerLidAutoSwitchEnabled(env) {
|
|
818
|
+
const raw = env.SHERPA_LID_AUTO_SWITCH?.trim().toLowerCase();
|
|
819
|
+
return raw === "1" || raw === "true" || raw === "yes" || raw === "on";
|
|
820
|
+
}
|
|
821
|
+
function cleanVendor(selection) {
|
|
822
|
+
const provider = selection?.provider?.trim();
|
|
823
|
+
if (!provider)
|
|
824
|
+
return void 0;
|
|
825
|
+
return {
|
|
826
|
+
provider,
|
|
827
|
+
...selection?.model?.trim() ? { model: selection.model.trim() } : {},
|
|
828
|
+
...selection?.voice?.trim() ? { voice: selection.voice.trim() } : {},
|
|
829
|
+
...selection?.language?.trim() ? { language: selection.language.trim() } : {}
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
function setVoiceLanguage(sessionId, options) {
|
|
833
|
+
const requestId = randomUUID();
|
|
834
|
+
const language = options.language?.trim() ?? "";
|
|
835
|
+
const scope = options.scope;
|
|
836
|
+
if (scope && scope !== "stt" && scope !== "tts" && scope !== "both") {
|
|
837
|
+
return Promise.resolve({
|
|
838
|
+
ok: false,
|
|
839
|
+
reason: "invalid_scope",
|
|
840
|
+
requestId
|
|
841
|
+
});
|
|
842
|
+
}
|
|
843
|
+
const sttVendor = cleanVendor(options.sttVendor);
|
|
844
|
+
const ttsVendor = cleanVendor(options.ttsVendor);
|
|
845
|
+
const hasWork = Boolean(language || options.voice?.trim() || options.stt?.trim() || sttVendor || ttsVendor);
|
|
846
|
+
if (!hasWork) {
|
|
847
|
+
return Promise.resolve({
|
|
848
|
+
ok: false,
|
|
849
|
+
reason: "nothing_to_apply",
|
|
850
|
+
requestId
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
if (!isVoicethereAgentChild()) {
|
|
854
|
+
return Promise.resolve({ ok: true, reason: "local_mock", requestId });
|
|
855
|
+
}
|
|
856
|
+
const ackTimeoutMs = options.timeoutMs ?? VOICE_LANGUAGE_CONTROL_ACK_TIMEOUT_MS;
|
|
857
|
+
return new Promise((resolve) => {
|
|
858
|
+
const timer = setTimeout(() => {
|
|
859
|
+
pendingVoiceLanguageAcks.delete(requestId);
|
|
860
|
+
resolve({ ok: false, reason: "timeout", requestId });
|
|
861
|
+
}, ackTimeoutMs);
|
|
862
|
+
pendingVoiceLanguageAcks.set(requestId, { resolve, timer });
|
|
863
|
+
sendParentMessage({
|
|
864
|
+
type: "voice_language_control",
|
|
865
|
+
requestId,
|
|
866
|
+
sessionId,
|
|
867
|
+
...language ? { language } : {},
|
|
868
|
+
...scope ? { scope } : {},
|
|
869
|
+
...options.voice?.trim() ? { voice: options.voice.trim() } : {},
|
|
870
|
+
...options.stt?.trim() ? { stt: options.stt.trim() } : {},
|
|
871
|
+
...sttVendor ? { sttVendor } : {},
|
|
872
|
+
...ttsVendor ? { ttsVendor } : {}
|
|
873
|
+
});
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
var AGENT_LOG_MESSAGE_MAX_CHARS = 2048;
|
|
877
|
+
var AGENT_LOG_FIELDS_MAX_CHARS = 8192;
|
|
878
|
+
function truncateAgentLogMessage(message) {
|
|
879
|
+
if (message.length <= AGENT_LOG_MESSAGE_MAX_CHARS) {
|
|
880
|
+
return message;
|
|
881
|
+
}
|
|
882
|
+
const suffix = "\u2026[truncated]";
|
|
883
|
+
return message.slice(0, AGENT_LOG_MESSAGE_MAX_CHARS - suffix.length) + suffix;
|
|
884
|
+
}
|
|
885
|
+
function isPlainObject(value) {
|
|
886
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
887
|
+
}
|
|
888
|
+
function sanitizeAgentLogFields(fields) {
|
|
889
|
+
if (Object.keys(fields).length === 0) {
|
|
890
|
+
return void 0;
|
|
891
|
+
}
|
|
892
|
+
try {
|
|
893
|
+
const serialized = JSON.stringify(fields);
|
|
894
|
+
if (serialized.length <= AGENT_LOG_FIELDS_MAX_CHARS) {
|
|
895
|
+
return fields;
|
|
896
|
+
}
|
|
897
|
+
return {
|
|
898
|
+
_agentLogFieldsTruncated: true,
|
|
899
|
+
_originalBytes: serialized.length,
|
|
900
|
+
_preview: serialized.slice(0, AGENT_LOG_FIELDS_MAX_CHARS - 80) + "\u2026[truncated]"
|
|
901
|
+
};
|
|
902
|
+
} catch {
|
|
903
|
+
return { _agentLogFieldsError: "not_serializable" };
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
function buildAgentLogPayload(level, message, fields, sessionId) {
|
|
907
|
+
const resolvedSessionId = sessionId ?? sessionExecutionContext.getStore()?.sessionId ?? agentLogSessionContext.getStore();
|
|
908
|
+
const sanitizedFields = fields ? sanitizeAgentLogFields(fields) : void 0;
|
|
909
|
+
return {
|
|
910
|
+
type: "log",
|
|
911
|
+
level,
|
|
912
|
+
message: truncateAgentLogMessage(message),
|
|
913
|
+
ts: Date.now(),
|
|
914
|
+
...resolvedSessionId ? { sessionId: resolvedSessionId } : {},
|
|
915
|
+
...sanitizedFields ? { fields: sanitizedFields } : {}
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
function agentLog(level, message, fieldsOrSessionId, sessionId) {
|
|
919
|
+
let fields;
|
|
920
|
+
let resolvedSessionId;
|
|
921
|
+
if (typeof fieldsOrSessionId === "string") {
|
|
922
|
+
resolvedSessionId = fieldsOrSessionId;
|
|
923
|
+
} else if (isPlainObject(fieldsOrSessionId)) {
|
|
924
|
+
fields = fieldsOrSessionId;
|
|
925
|
+
resolvedSessionId = sessionId;
|
|
926
|
+
} else {
|
|
927
|
+
resolvedSessionId = sessionId;
|
|
928
|
+
}
|
|
929
|
+
sendParentMessage(buildAgentLogPayload(level, message, fields, resolvedSessionId));
|
|
930
|
+
}
|
|
931
|
+
function parseChatText(message) {
|
|
932
|
+
if (!message || typeof message !== "object")
|
|
933
|
+
return null;
|
|
934
|
+
const record = message;
|
|
935
|
+
if (record.type !== "chat" || typeof record.text !== "string")
|
|
936
|
+
return null;
|
|
937
|
+
const trimmed = record.text.trim();
|
|
938
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
// templates/language-switch/agent.ts
|
|
942
|
+
var ECHO_PREFIX = "you said:";
|
|
943
|
+
var ECHO_WAIT_MS = 4e3;
|
|
944
|
+
var REPLIES = {
|
|
945
|
+
de: "Guten Tag. Ich antworte jetzt auf Deutsch.",
|
|
946
|
+
es: "Hola. Ahora respondo en espa\xF1ol.",
|
|
947
|
+
fr: "Bonjour. Je r\xE9ponds maintenant en fran\xE7ais.",
|
|
948
|
+
it: "Ciao. Adesso rispondo in italiano.",
|
|
949
|
+
pt: "Ol\xE1. Agora respondo em portugu\xEAs.",
|
|
950
|
+
nl: "Hallo. Ik antwoord nu in het Nederlands.",
|
|
951
|
+
pl: "Dzie\u0144 dobry. Teraz odpowiadam po polsku.",
|
|
952
|
+
ru: "\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435. \u0422\u0435\u043F\u0435\u0440\u044C \u044F \u043E\u0442\u0432\u0435\u0447\u0430\u044E \u043F\u043E-\u0440\u0443\u0441\u0441\u043A\u0438."
|
|
953
|
+
};
|
|
954
|
+
var sessions = /* @__PURE__ */ new Map();
|
|
955
|
+
function stateFor(sessionId, env) {
|
|
956
|
+
const existing = sessions.get(sessionId);
|
|
957
|
+
if (existing) return existing;
|
|
958
|
+
const created = {
|
|
959
|
+
language: "en",
|
|
960
|
+
runnerAutoSwitch: env ? isRunnerLidAutoSwitchEnabled(env) : false,
|
|
961
|
+
echoTimer: void 0,
|
|
962
|
+
suppressNextFinal: false
|
|
963
|
+
};
|
|
964
|
+
sessions.set(sessionId, created);
|
|
965
|
+
return created;
|
|
966
|
+
}
|
|
967
|
+
function replyFor(language) {
|
|
968
|
+
return REPLIES[language] ?? `Continuing in ${language}.`;
|
|
969
|
+
}
|
|
970
|
+
function logSwitch(sessionId, side, result) {
|
|
971
|
+
if (!result.ok) {
|
|
972
|
+
agentLog(
|
|
973
|
+
"warn",
|
|
974
|
+
`setVoiceLanguage ${side} failed: ${result.reason ?? "unknown"}`
|
|
975
|
+
);
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
const provider = side === "tts" ? result.ttsProvider : result.sttProvider;
|
|
979
|
+
const model = side === "tts" ? result.voice : result.stt;
|
|
980
|
+
agentLog(
|
|
981
|
+
"info",
|
|
982
|
+
`voice ${side} ${sessionId} ${result.language ?? ""} provider=${provider ?? "unchanged"} model=${model ?? "unchanged"}`
|
|
983
|
+
);
|
|
984
|
+
}
|
|
985
|
+
function prepareLanguageTransition(state) {
|
|
986
|
+
if (state.echoTimer) {
|
|
987
|
+
clearTimeout(state.echoTimer);
|
|
988
|
+
state.echoTimer = void 0;
|
|
989
|
+
} else {
|
|
990
|
+
state.suppressNextFinal = true;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
defineAgent({
|
|
994
|
+
onSessionStart({ sessionId, env }) {
|
|
995
|
+
const state = stateFor(sessionId, env);
|
|
996
|
+
if (state.runnerAutoSwitch) {
|
|
997
|
+
agentLog(
|
|
998
|
+
"info",
|
|
999
|
+
`language-switch session_start ${sessionId} runner LID auto-switch owns STT/TTS`
|
|
1000
|
+
);
|
|
1001
|
+
} else {
|
|
1002
|
+
agentLog(
|
|
1003
|
+
"info",
|
|
1004
|
+
`language-switch session_start ${sessionId} manual setVoiceLanguage`
|
|
1005
|
+
);
|
|
1006
|
+
}
|
|
1007
|
+
},
|
|
1008
|
+
async onUserLanguage({ sessionId, language }) {
|
|
1009
|
+
const state = stateFor(sessionId);
|
|
1010
|
+
if (!language || language === state.language) return;
|
|
1011
|
+
prepareLanguageTransition(state);
|
|
1012
|
+
if (state.runnerAutoSwitch) {
|
|
1013
|
+
agentLog(
|
|
1014
|
+
"info",
|
|
1015
|
+
`LID detected ${language}; runner auto-switch applies STT/TTS \u2014 agent updates prompts only`
|
|
1016
|
+
);
|
|
1017
|
+
state.language = language;
|
|
1018
|
+
speak(sessionId, replyFor(language));
|
|
1019
|
+
return;
|
|
1020
|
+
}
|
|
1021
|
+
const tts = await setVoiceLanguage(sessionId, {
|
|
1022
|
+
scope: "tts",
|
|
1023
|
+
language,
|
|
1024
|
+
voice: language
|
|
1025
|
+
});
|
|
1026
|
+
logSwitch(sessionId, "tts", tts);
|
|
1027
|
+
const stt = await setVoiceLanguage(sessionId, {
|
|
1028
|
+
scope: "stt",
|
|
1029
|
+
language,
|
|
1030
|
+
stt: language
|
|
1031
|
+
});
|
|
1032
|
+
logSwitch(sessionId, "stt", stt);
|
|
1033
|
+
if (!tts.ok) {
|
|
1034
|
+
state.suppressNextFinal = false;
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
1037
|
+
state.language = language;
|
|
1038
|
+
speak(sessionId, replyFor(language));
|
|
1039
|
+
},
|
|
1040
|
+
async onVoiceLanguageChanged({ sessionId, language }) {
|
|
1041
|
+
const state = stateFor(sessionId);
|
|
1042
|
+
if (!state.runnerAutoSwitch || !language || language === state.language) {
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
agentLog(
|
|
1046
|
+
"info",
|
|
1047
|
+
`runner committed voice language ${language} for ${sessionId}`
|
|
1048
|
+
);
|
|
1049
|
+
state.language = language;
|
|
1050
|
+
},
|
|
1051
|
+
async onDataChannelMessage(ctx) {
|
|
1052
|
+
const text = parseChatText(ctx.message);
|
|
1053
|
+
if (!text) return;
|
|
1054
|
+
const [command, vendor, language] = text.trim().split(/\s+/);
|
|
1055
|
+
if (command === "/tts" && vendor === "sherpa" && language) {
|
|
1056
|
+
const result = await setVoiceLanguage(ctx.sessionId, {
|
|
1057
|
+
scope: "tts",
|
|
1058
|
+
language,
|
|
1059
|
+
ttsVendor: { provider: "local-sherpa", voice: language }
|
|
1060
|
+
});
|
|
1061
|
+
logSwitch(ctx.sessionId, "tts", result);
|
|
1062
|
+
if (result.ok) speak(ctx.sessionId, replyFor(language));
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
if (command === "/stt" && vendor === "sherpa" && language) {
|
|
1066
|
+
const result = await setVoiceLanguage(ctx.sessionId, {
|
|
1067
|
+
scope: "stt",
|
|
1068
|
+
language,
|
|
1069
|
+
sttVendor: { provider: "local-sherpa", model: language }
|
|
1070
|
+
});
|
|
1071
|
+
logSwitch(ctx.sessionId, "stt", result);
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
if (command === "/tts" && vendor === "elevenlabs") {
|
|
1075
|
+
const result = await setVoiceLanguage(ctx.sessionId, {
|
|
1076
|
+
scope: "tts",
|
|
1077
|
+
ttsVendor: { provider: "elevenlabs", model: "eleven_multilingual_v2" }
|
|
1078
|
+
});
|
|
1079
|
+
logSwitch(ctx.sessionId, "tts", result);
|
|
1080
|
+
if (result.ok) speak(ctx.sessionId, "Speaking with ElevenLabs.");
|
|
1081
|
+
return;
|
|
1082
|
+
}
|
|
1083
|
+
if (command === "/stt" && vendor === "deepgram") {
|
|
1084
|
+
const result = await setVoiceLanguage(ctx.sessionId, {
|
|
1085
|
+
scope: "stt",
|
|
1086
|
+
sttVendor: {
|
|
1087
|
+
provider: "deepgram",
|
|
1088
|
+
model: "nova-3",
|
|
1089
|
+
language: language || "en"
|
|
1090
|
+
}
|
|
1091
|
+
});
|
|
1092
|
+
logSwitch(ctx.sessionId, "stt", result);
|
|
1093
|
+
}
|
|
1094
|
+
},
|
|
1095
|
+
onUserSpeechFinal({ sessionId, text }) {
|
|
1096
|
+
const state = stateFor(sessionId);
|
|
1097
|
+
if (state.suppressNextFinal) {
|
|
1098
|
+
state.suppressNextFinal = false;
|
|
1099
|
+
return;
|
|
1100
|
+
}
|
|
1101
|
+
if (state.echoTimer) clearTimeout(state.echoTimer);
|
|
1102
|
+
state.echoTimer = setTimeout(() => {
|
|
1103
|
+
state.echoTimer = void 0;
|
|
1104
|
+
speak(sessionId, `${ECHO_PREFIX} ${text}`.trim());
|
|
1105
|
+
}, ECHO_WAIT_MS);
|
|
1106
|
+
},
|
|
1107
|
+
onSessionEnd({ sessionId }) {
|
|
1108
|
+
const state = sessions.get(sessionId);
|
|
1109
|
+
if (state?.echoTimer) clearTimeout(state.echoTimer);
|
|
1110
|
+
sessions.delete(sessionId);
|
|
1111
|
+
}
|
|
1112
|
+
});
|