@charivo/realtime 0.7.1 → 0.7.2
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 +7 -0
- package/dist/{chunk-Q5T2AV7Q.mjs → chunk-E656Y2PO.mjs} +5 -44
- package/dist/chunk-E656Y2PO.mjs.map +1 -0
- package/dist/{chunk-D4BW4H2X.mjs → chunk-FRTYFCPV.mjs} +4 -3
- package/dist/chunk-FRTYFCPV.mjs.map +1 -0
- package/dist/{chunk-GSRVMEKE.mjs → chunk-GJ2NA3ZT.mjs} +4 -3
- package/dist/chunk-GJ2NA3ZT.mjs.map +1 -0
- package/dist/{chunk-AIQJUH3Y.mjs → chunk-X6YRHDTW.mjs} +43 -3
- package/dist/chunk-X6YRHDTW.mjs.map +1 -0
- package/dist/index.d.mts +11 -21
- package/dist/index.d.ts +11 -21
- package/dist/index.js +450 -273
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +450 -275
- package/dist/index.mjs.map +1 -1
- package/dist/openai/index.mjs +3 -3
- package/dist/openai-agents/index.mjs +3 -3
- package/dist/remote/index.mjs +4 -4
- package/package.json +1 -1
- package/dist/chunk-AIQJUH3Y.mjs.map +0 -1
- package/dist/chunk-D4BW4H2X.mjs.map +0 -1
- package/dist/chunk-GSRVMEKE.mjs.map +0 -1
- package/dist/chunk-Q5T2AV7Q.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,417 @@
|
|
|
1
|
-
import { buildRealtimeSessionConfig } from './chunk-
|
|
2
|
-
export { DEFAULT_REALTIME_AGENT_INSTRUCTIONS, buildRealtimeSessionConfig } from './chunk-
|
|
1
|
+
import { buildRealtimeSessionConfig, isRecord } from './chunk-X6YRHDTW.mjs';
|
|
2
|
+
export { DEFAULT_REALTIME_AGENT_INSTRUCTIONS, buildRealtimeSessionConfig } from './chunk-X6YRHDTW.mjs';
|
|
3
3
|
import { CharivoStateError, toCharivoError, subscribeBrowserLifecycle } from '@charivo/core';
|
|
4
4
|
|
|
5
|
+
// src/internal/audio-output.ts
|
|
6
|
+
var RealtimeAudioOutput = class {
|
|
7
|
+
constructor(emit) {
|
|
8
|
+
this.emit = emit;
|
|
9
|
+
}
|
|
10
|
+
active = false;
|
|
11
|
+
isActive() {
|
|
12
|
+
return this.active;
|
|
13
|
+
}
|
|
14
|
+
start() {
|
|
15
|
+
if (this.active) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
this.active = true;
|
|
19
|
+
this.emit("tts:audio:start", {});
|
|
20
|
+
}
|
|
21
|
+
end() {
|
|
22
|
+
if (!this.active) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
this.active = false;
|
|
26
|
+
this.emit("tts:lipsync:update", { rms: 0 });
|
|
27
|
+
this.emit("tts:audio:end", {});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var RealtimeBrowserLifecycle = class {
|
|
31
|
+
constructor(getState) {
|
|
32
|
+
this.getState = getState;
|
|
33
|
+
}
|
|
34
|
+
wakeLockSentinel = null;
|
|
35
|
+
teardownBrowserLifecycle;
|
|
36
|
+
install() {
|
|
37
|
+
if (this.teardownBrowserLifecycle) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
this.teardownBrowserLifecycle = subscribeBrowserLifecycle({
|
|
41
|
+
onHidden: () => {
|
|
42
|
+
void this.releaseWakeLock();
|
|
43
|
+
},
|
|
44
|
+
onPageHide: () => {
|
|
45
|
+
void this.releaseWakeLock();
|
|
46
|
+
},
|
|
47
|
+
onPageShow: () => {
|
|
48
|
+
void this.requestWakeLock();
|
|
49
|
+
},
|
|
50
|
+
onVisible: () => {
|
|
51
|
+
void this.requestWakeLock();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
dispose() {
|
|
56
|
+
this.teardownBrowserLifecycle?.();
|
|
57
|
+
this.teardownBrowserLifecycle = void 0;
|
|
58
|
+
void this.releaseWakeLock();
|
|
59
|
+
}
|
|
60
|
+
async requestWakeLock() {
|
|
61
|
+
const state = this.getState();
|
|
62
|
+
if (state.session.status !== "active" || state.connection === "disconnecting" || typeof navigator === "undefined" || typeof document === "undefined" || document.visibilityState !== "visible" || !("wakeLock" in navigator)) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (this.wakeLockSentinel) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const wakeLockApi = navigator;
|
|
69
|
+
try {
|
|
70
|
+
this.wakeLockSentinel = await wakeLockApi.wakeLock?.request("screen");
|
|
71
|
+
this.wakeLockSentinel?.addEventListener?.(
|
|
72
|
+
"release",
|
|
73
|
+
() => {
|
|
74
|
+
this.wakeLockSentinel = null;
|
|
75
|
+
},
|
|
76
|
+
{ once: true }
|
|
77
|
+
);
|
|
78
|
+
} catch {
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async releaseWakeLock() {
|
|
82
|
+
if (!this.wakeLockSentinel) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const wakeLockSentinel = this.wakeLockSentinel;
|
|
86
|
+
this.wakeLockSentinel = null;
|
|
87
|
+
try {
|
|
88
|
+
await wakeLockSentinel.release();
|
|
89
|
+
} catch {
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// src/internal/manager-state.ts
|
|
95
|
+
var nextFallbackSessionId = 1;
|
|
96
|
+
function mergeSessionConfig(baseConfig, overrideConfig) {
|
|
97
|
+
if (!baseConfig && !overrideConfig) {
|
|
98
|
+
return void 0;
|
|
99
|
+
}
|
|
100
|
+
const { tools: _ignoredOverrideTools, ...overrideWithoutTools } = overrideConfig ?? {};
|
|
101
|
+
return {
|
|
102
|
+
...baseConfig,
|
|
103
|
+
...overrideWithoutTools
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function mergeRealtimeState(current, partial) {
|
|
107
|
+
return {
|
|
108
|
+
connection: partial.connection ?? current.connection,
|
|
109
|
+
session: {
|
|
110
|
+
status: partial.session?.status ?? current.session.status,
|
|
111
|
+
config: partial.session?.config ?? current.session.config,
|
|
112
|
+
characterId: partial.session?.characterId ?? current.session.characterId
|
|
113
|
+
},
|
|
114
|
+
response: {
|
|
115
|
+
status: partial.response?.status ?? current.response.status,
|
|
116
|
+
text: partial.response?.text ?? current.response.text
|
|
117
|
+
},
|
|
118
|
+
lastError: partial.lastError ?? current.lastError
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function createRealtimeSessionId() {
|
|
122
|
+
const randomUuid = globalThis.crypto?.randomUUID?.();
|
|
123
|
+
if (randomUuid) {
|
|
124
|
+
return randomUuid;
|
|
125
|
+
}
|
|
126
|
+
return `realtime-session-${Date.now()}-${nextFallbackSessionId++}`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/internal/timing.ts
|
|
130
|
+
function delay(ms) {
|
|
131
|
+
return new Promise((resolve) => {
|
|
132
|
+
setTimeout(resolve, ms);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/internal/tool-registry.ts
|
|
137
|
+
var RealtimeToolRegistry = class {
|
|
138
|
+
tools = /* @__PURE__ */ new Map();
|
|
139
|
+
register(tool) {
|
|
140
|
+
this.tools.set(tool.definition.name, tool);
|
|
141
|
+
}
|
|
142
|
+
unregister(name) {
|
|
143
|
+
this.tools.delete(name);
|
|
144
|
+
}
|
|
145
|
+
get(name) {
|
|
146
|
+
return this.tools.get(name);
|
|
147
|
+
}
|
|
148
|
+
getDefinitions() {
|
|
149
|
+
return Array.from(this.tools.values(), (tool) => ({
|
|
150
|
+
...tool.definition,
|
|
151
|
+
parameters: {
|
|
152
|
+
...tool.definition.parameters,
|
|
153
|
+
properties: { ...tool.definition.parameters.properties },
|
|
154
|
+
required: tool.definition.parameters.required ? [...tool.definition.parameters.required] : void 0
|
|
155
|
+
}
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// src/internal/tool-args-validation.ts
|
|
161
|
+
function validateToolArguments(definition, args) {
|
|
162
|
+
if (!isRecord(args) || Array.isArray(args)) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`Realtime tool "${definition.name}" arguments failed schema validation: arguments must be an object`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
const { parameters } = definition;
|
|
168
|
+
const required = parameters.required ?? [];
|
|
169
|
+
for (const propertyName of required) {
|
|
170
|
+
if (!hasOwnProperty(args, propertyName)) {
|
|
171
|
+
throw new Error(
|
|
172
|
+
`Realtime tool "${definition.name}" arguments failed schema validation: missing required property "${propertyName}"`
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (const [propertyName, schema] of Object.entries(parameters.properties)) {
|
|
177
|
+
if (!hasOwnProperty(args, propertyName) || !isRecord(schema)) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
validateToolArgumentValue(
|
|
181
|
+
definition.name,
|
|
182
|
+
propertyName,
|
|
183
|
+
args[propertyName],
|
|
184
|
+
schema
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function validateToolArgumentValue(toolName, propertyName, value, schema) {
|
|
189
|
+
const enumValues = schema.enum;
|
|
190
|
+
if (Array.isArray(enumValues) && !enumValues.some((item) => Object.is(item, value))) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
`Realtime tool "${toolName}" arguments failed schema validation: property "${propertyName}" must be one of ${formatEnumValues(enumValues)}`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
const expectedType = schema.type;
|
|
196
|
+
if (typeof expectedType !== "string") {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (!isSupportedSchemaType(expectedType)) {
|
|
200
|
+
throw new Error(
|
|
201
|
+
`Realtime tool "${toolName}" arguments failed schema validation: property "${propertyName}" uses unsupported schema type "${expectedType}"`
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
if (!matchesSchemaType(value, expectedType)) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`Realtime tool "${toolName}" arguments failed schema validation: property "${propertyName}" must be ${expectedType}`
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function matchesSchemaType(value, expectedType) {
|
|
211
|
+
switch (expectedType) {
|
|
212
|
+
case "string":
|
|
213
|
+
return typeof value === "string";
|
|
214
|
+
case "number":
|
|
215
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
216
|
+
case "integer":
|
|
217
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
218
|
+
case "boolean":
|
|
219
|
+
return typeof value === "boolean";
|
|
220
|
+
case "object":
|
|
221
|
+
return isRecord(value) && !Array.isArray(value);
|
|
222
|
+
case "array":
|
|
223
|
+
return Array.isArray(value);
|
|
224
|
+
case "null":
|
|
225
|
+
return value === null;
|
|
226
|
+
}
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
function isSupportedSchemaType(expectedType) {
|
|
230
|
+
return expectedType === "string" || expectedType === "number" || expectedType === "integer" || expectedType === "boolean" || expectedType === "object" || expectedType === "array" || expectedType === "null";
|
|
231
|
+
}
|
|
232
|
+
function formatEnumValues(values) {
|
|
233
|
+
return values.map((value) => JSON.stringify(value)).join(", ");
|
|
234
|
+
}
|
|
235
|
+
function hasOwnProperty(record, propertyName) {
|
|
236
|
+
return Object.prototype.hasOwnProperty.call(record, propertyName);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/internal/tool-execution.ts
|
|
240
|
+
function createFailureOutput(error) {
|
|
241
|
+
return {
|
|
242
|
+
success: false,
|
|
243
|
+
error: error.message
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
async function withTimeout(promise, timeoutMs, toolName) {
|
|
247
|
+
let timeoutId;
|
|
248
|
+
try {
|
|
249
|
+
return await Promise.race([
|
|
250
|
+
promise,
|
|
251
|
+
new Promise((_, reject) => {
|
|
252
|
+
timeoutId = setTimeout(() => {
|
|
253
|
+
reject(
|
|
254
|
+
new Error(
|
|
255
|
+
`Realtime tool "${toolName}" timed out after ${timeoutMs}ms`
|
|
256
|
+
)
|
|
257
|
+
);
|
|
258
|
+
}, timeoutMs);
|
|
259
|
+
})
|
|
260
|
+
]);
|
|
261
|
+
} finally {
|
|
262
|
+
if (timeoutId !== void 0) {
|
|
263
|
+
clearTimeout(timeoutId);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/internal/tool-runner.ts
|
|
269
|
+
async function executeRealtimeToolCall({
|
|
270
|
+
event,
|
|
271
|
+
tool,
|
|
272
|
+
client,
|
|
273
|
+
character,
|
|
274
|
+
state,
|
|
275
|
+
defaultToolTimeoutMs,
|
|
276
|
+
resultProjectors,
|
|
277
|
+
emit,
|
|
278
|
+
log
|
|
279
|
+
}) {
|
|
280
|
+
if (!event.callId) {
|
|
281
|
+
emitToolError(
|
|
282
|
+
event.name,
|
|
283
|
+
new Error(`Tool "${event.name}" is missing a call ID`),
|
|
284
|
+
event.callId,
|
|
285
|
+
emit,
|
|
286
|
+
log
|
|
287
|
+
);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (!tool) {
|
|
291
|
+
await handleToolExecutionFailure(
|
|
292
|
+
event.name,
|
|
293
|
+
event.callId,
|
|
294
|
+
new Error(`No realtime tool registered for "${event.name}"`),
|
|
295
|
+
client,
|
|
296
|
+
emit,
|
|
297
|
+
log
|
|
298
|
+
);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
log("debug", "Realtime tool execution started", {
|
|
303
|
+
name: event.name,
|
|
304
|
+
callId: event.callId
|
|
305
|
+
});
|
|
306
|
+
const output = await runToolHandler({
|
|
307
|
+
tool,
|
|
308
|
+
event,
|
|
309
|
+
character,
|
|
310
|
+
state,
|
|
311
|
+
defaultToolTimeoutMs
|
|
312
|
+
});
|
|
313
|
+
await client.sendToolResult(event.callId, output);
|
|
314
|
+
emit("realtime:tool:result", {
|
|
315
|
+
name: event.name,
|
|
316
|
+
output,
|
|
317
|
+
callId: event.callId
|
|
318
|
+
});
|
|
319
|
+
projectToolResult(
|
|
320
|
+
event.name,
|
|
321
|
+
output,
|
|
322
|
+
resultProjectors,
|
|
323
|
+
emit,
|
|
324
|
+
log,
|
|
325
|
+
event.callId
|
|
326
|
+
);
|
|
327
|
+
log("info", "Realtime tool execution succeeded", {
|
|
328
|
+
name: event.name,
|
|
329
|
+
callId: event.callId
|
|
330
|
+
});
|
|
331
|
+
} catch (error) {
|
|
332
|
+
await handleToolExecutionFailure(
|
|
333
|
+
event.name,
|
|
334
|
+
event.callId,
|
|
335
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
336
|
+
client,
|
|
337
|
+
emit,
|
|
338
|
+
log
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
async function runToolHandler({
|
|
343
|
+
tool,
|
|
344
|
+
event,
|
|
345
|
+
character,
|
|
346
|
+
state,
|
|
347
|
+
defaultToolTimeoutMs
|
|
348
|
+
}) {
|
|
349
|
+
validateToolArguments(tool.definition, event.args);
|
|
350
|
+
const result = await withTimeout(
|
|
351
|
+
tool.handler(event.args, {
|
|
352
|
+
character,
|
|
353
|
+
state,
|
|
354
|
+
callId: event.callId
|
|
355
|
+
}),
|
|
356
|
+
tool.timeoutMs ?? defaultToolTimeoutMs,
|
|
357
|
+
tool.definition.name
|
|
358
|
+
);
|
|
359
|
+
if (!isRecord(result)) {
|
|
360
|
+
throw new Error(
|
|
361
|
+
`Realtime tool "${tool.definition.name}" must return an object`
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
return result;
|
|
365
|
+
}
|
|
366
|
+
async function handleToolExecutionFailure(name, callId, error, client, emit, log) {
|
|
367
|
+
emitToolError(name, error, callId, emit, log);
|
|
368
|
+
try {
|
|
369
|
+
await client.sendToolResult(callId, createFailureOutput(error));
|
|
370
|
+
} catch (sendError) {
|
|
371
|
+
emitToolError(
|
|
372
|
+
name,
|
|
373
|
+
sendError instanceof Error ? sendError : new Error(String(sendError)),
|
|
374
|
+
callId,
|
|
375
|
+
emit,
|
|
376
|
+
log
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function projectToolResult(name, output, resultProjectors, emit, log, callId) {
|
|
381
|
+
for (const projector of resultProjectors ?? []) {
|
|
382
|
+
try {
|
|
383
|
+
projector({
|
|
384
|
+
name,
|
|
385
|
+
output,
|
|
386
|
+
callId,
|
|
387
|
+
emit
|
|
388
|
+
});
|
|
389
|
+
} catch (error) {
|
|
390
|
+
const projectorError = error instanceof Error ? error : new Error(String(error));
|
|
391
|
+
log("warn", "Realtime result projector failed", {
|
|
392
|
+
name,
|
|
393
|
+
callId,
|
|
394
|
+
error: projectorError.message
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
function emitToolError(name, error, callId, emit, log) {
|
|
400
|
+
log("warn", "Realtime tool execution failed", {
|
|
401
|
+
name,
|
|
402
|
+
callId,
|
|
403
|
+
error: error.message
|
|
404
|
+
});
|
|
405
|
+
emit("realtime:tool:error", {
|
|
406
|
+
name,
|
|
407
|
+
error,
|
|
408
|
+
callId
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// src/realtime-manager.ts
|
|
5
413
|
var DEFAULT_TOOL_TIMEOUT_MS = 1e4;
|
|
6
414
|
var RECONNECT_DELAYS_MS = [500, 1e3, 2e3, 4e3, 5e3];
|
|
7
|
-
var nextFallbackSessionId = 1;
|
|
8
415
|
var RealtimeManagerImpl = class {
|
|
9
416
|
constructor(client, options = {}) {
|
|
10
417
|
this.client = client;
|
|
@@ -12,7 +419,7 @@ var RealtimeManagerImpl = class {
|
|
|
12
419
|
for (const tool of options.tools ?? []) {
|
|
13
420
|
this.registerTool(tool);
|
|
14
421
|
}
|
|
15
|
-
this.
|
|
422
|
+
this.browserLifecycle.install();
|
|
16
423
|
this.client.onEvent((event) => {
|
|
17
424
|
void this.handleClientEvent(event).catch((error) => {
|
|
18
425
|
this.applyError(
|
|
@@ -27,17 +434,20 @@ var RealtimeManagerImpl = class {
|
|
|
27
434
|
isStoppingSession = false;
|
|
28
435
|
isRefreshingSession = false;
|
|
29
436
|
isRecoveringConnection = false;
|
|
30
|
-
isAudioPlaybackActive = false;
|
|
31
437
|
sessionBaseConfig;
|
|
32
438
|
refreshInFlight = null;
|
|
33
439
|
reconnectInFlight = null;
|
|
34
440
|
hasQueuedRefresh = false;
|
|
35
441
|
stopRequestedDuringRefresh = false;
|
|
36
442
|
reconnectToken = 0;
|
|
37
|
-
wakeLockSentinel = null;
|
|
38
|
-
teardownBrowserLifecycle;
|
|
39
443
|
sessionId;
|
|
40
|
-
toolRegistry =
|
|
444
|
+
toolRegistry = new RealtimeToolRegistry();
|
|
445
|
+
audioOutput = new RealtimeAudioOutput((event, payload) => {
|
|
446
|
+
this.eventEmitter?.emit(event, payload);
|
|
447
|
+
});
|
|
448
|
+
browserLifecycle = new RealtimeBrowserLifecycle(
|
|
449
|
+
() => this.state
|
|
450
|
+
);
|
|
41
451
|
state = {
|
|
42
452
|
connection: "idle",
|
|
43
453
|
session: {
|
|
@@ -65,20 +475,13 @@ var RealtimeManagerImpl = class {
|
|
|
65
475
|
this.emitState();
|
|
66
476
|
}
|
|
67
477
|
registerTool(tool) {
|
|
68
|
-
this.toolRegistry.
|
|
478
|
+
this.toolRegistry.register(tool);
|
|
69
479
|
}
|
|
70
480
|
unregisterTool(name) {
|
|
71
|
-
this.toolRegistry.
|
|
481
|
+
this.toolRegistry.unregister(name);
|
|
72
482
|
}
|
|
73
483
|
getRegisteredTools() {
|
|
74
|
-
return
|
|
75
|
-
...tool.definition,
|
|
76
|
-
parameters: {
|
|
77
|
-
...tool.definition.parameters,
|
|
78
|
-
properties: { ...tool.definition.parameters.properties },
|
|
79
|
-
required: tool.definition.parameters.required ? [...tool.definition.parameters.required] : void 0
|
|
80
|
-
}
|
|
81
|
-
}));
|
|
484
|
+
return this.toolRegistry.getDefinitions();
|
|
82
485
|
}
|
|
83
486
|
getState() {
|
|
84
487
|
return {
|
|
@@ -116,12 +519,12 @@ var RealtimeManagerImpl = class {
|
|
|
116
519
|
},
|
|
117
520
|
lastError: null
|
|
118
521
|
};
|
|
119
|
-
this.
|
|
522
|
+
this.browserLifecycle.install();
|
|
120
523
|
this.emitState();
|
|
121
524
|
try {
|
|
122
525
|
await this.client.connect(sessionConfig);
|
|
123
526
|
this.commitActiveSession(sessionConfig);
|
|
124
|
-
void this.requestWakeLock();
|
|
527
|
+
void this.browserLifecycle.requestWakeLock();
|
|
125
528
|
this.emitSessionStart("user");
|
|
126
529
|
} catch (error) {
|
|
127
530
|
const typedError = toCharivoError("transport", error);
|
|
@@ -213,7 +616,7 @@ var RealtimeManagerImpl = class {
|
|
|
213
616
|
status: "interrupted"
|
|
214
617
|
}
|
|
215
618
|
};
|
|
216
|
-
this.
|
|
619
|
+
this.audioOutput.end();
|
|
217
620
|
this.emitState();
|
|
218
621
|
}
|
|
219
622
|
async handleClientEvent(event) {
|
|
@@ -292,14 +695,14 @@ var RealtimeManagerImpl = class {
|
|
|
292
695
|
return;
|
|
293
696
|
}
|
|
294
697
|
case "audio.output.started":
|
|
295
|
-
this.
|
|
698
|
+
this.audioOutput.start();
|
|
296
699
|
return;
|
|
297
700
|
case "audio.output.ended":
|
|
298
|
-
this.
|
|
701
|
+
this.audioOutput.end();
|
|
299
702
|
return;
|
|
300
703
|
case "audio.lipsync":
|
|
301
|
-
if (event.rms > 1e-3 && !this.
|
|
302
|
-
this.
|
|
704
|
+
if (event.rms > 1e-3 && !this.audioOutput.isActive()) {
|
|
705
|
+
this.audioOutput.start();
|
|
303
706
|
}
|
|
304
707
|
this.eventEmitter?.emit("tts:lipsync:update", { rms: event.rms });
|
|
305
708
|
return;
|
|
@@ -309,7 +712,21 @@ var RealtimeManagerImpl = class {
|
|
|
309
712
|
args: event.args,
|
|
310
713
|
callId: event.callId
|
|
311
714
|
});
|
|
312
|
-
await
|
|
715
|
+
await executeRealtimeToolCall({
|
|
716
|
+
event,
|
|
717
|
+
tool: this.toolRegistry.get(event.name),
|
|
718
|
+
client: this.client,
|
|
719
|
+
character: this.character,
|
|
720
|
+
state: this.getState(),
|
|
721
|
+
defaultToolTimeoutMs: this.options.defaultToolTimeoutMs ?? DEFAULT_TOOL_TIMEOUT_MS,
|
|
722
|
+
resultProjectors: this.options.resultProjectors,
|
|
723
|
+
emit: (eventName, payload) => {
|
|
724
|
+
this.eventEmitter?.emit(eventName, payload);
|
|
725
|
+
},
|
|
726
|
+
log: (level, message, context) => {
|
|
727
|
+
this.log(level, message, context);
|
|
728
|
+
}
|
|
729
|
+
});
|
|
313
730
|
return;
|
|
314
731
|
case "tool.result":
|
|
315
732
|
this.eventEmitter?.emit("realtime:tool:result", {
|
|
@@ -335,112 +752,6 @@ var RealtimeManagerImpl = class {
|
|
|
335
752
|
return;
|
|
336
753
|
}
|
|
337
754
|
}
|
|
338
|
-
async executeToolCall(event) {
|
|
339
|
-
const tool = this.toolRegistry.get(event.name);
|
|
340
|
-
if (!event.callId) {
|
|
341
|
-
this.emitToolError(
|
|
342
|
-
event.name,
|
|
343
|
-
new Error(`Tool "${event.name}" is missing a call ID`),
|
|
344
|
-
event.callId
|
|
345
|
-
);
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
if (!tool) {
|
|
349
|
-
await this.handleToolExecutionFailure(
|
|
350
|
-
event.name,
|
|
351
|
-
event.callId,
|
|
352
|
-
new Error(`No realtime tool registered for "${event.name}"`)
|
|
353
|
-
);
|
|
354
|
-
return;
|
|
355
|
-
}
|
|
356
|
-
try {
|
|
357
|
-
this.log("debug", "Realtime tool execution started", {
|
|
358
|
-
name: event.name,
|
|
359
|
-
callId: event.callId
|
|
360
|
-
});
|
|
361
|
-
const output = await this.runToolHandler(tool, event);
|
|
362
|
-
await this.client.sendToolResult(event.callId, output);
|
|
363
|
-
this.eventEmitter?.emit("realtime:tool:result", {
|
|
364
|
-
name: event.name,
|
|
365
|
-
output,
|
|
366
|
-
callId: event.callId
|
|
367
|
-
});
|
|
368
|
-
this.projectToolResult(event.name, output, event.callId);
|
|
369
|
-
this.log("info", "Realtime tool execution succeeded", {
|
|
370
|
-
name: event.name,
|
|
371
|
-
callId: event.callId
|
|
372
|
-
});
|
|
373
|
-
} catch (error) {
|
|
374
|
-
await this.handleToolExecutionFailure(
|
|
375
|
-
event.name,
|
|
376
|
-
event.callId,
|
|
377
|
-
error instanceof Error ? error : new Error(String(error))
|
|
378
|
-
);
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
async runToolHandler(tool, event) {
|
|
382
|
-
const timeoutMs = tool.timeoutMs ?? this.options.defaultToolTimeoutMs ?? DEFAULT_TOOL_TIMEOUT_MS;
|
|
383
|
-
const result = await withTimeout(
|
|
384
|
-
tool.handler(event.args, {
|
|
385
|
-
character: this.character,
|
|
386
|
-
state: this.getState(),
|
|
387
|
-
callId: event.callId
|
|
388
|
-
}),
|
|
389
|
-
timeoutMs,
|
|
390
|
-
tool.definition.name
|
|
391
|
-
);
|
|
392
|
-
if (!isRecord(result)) {
|
|
393
|
-
throw new Error(
|
|
394
|
-
`Realtime tool "${tool.definition.name}" must return an object`
|
|
395
|
-
);
|
|
396
|
-
}
|
|
397
|
-
return result;
|
|
398
|
-
}
|
|
399
|
-
async handleToolExecutionFailure(name, callId, error) {
|
|
400
|
-
this.emitToolError(name, error, callId);
|
|
401
|
-
try {
|
|
402
|
-
await this.client.sendToolResult(callId, createFailureOutput(error));
|
|
403
|
-
} catch (sendError) {
|
|
404
|
-
this.emitToolError(
|
|
405
|
-
name,
|
|
406
|
-
sendError instanceof Error ? sendError : new Error(String(sendError)),
|
|
407
|
-
callId
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
projectToolResult(name, output, callId) {
|
|
412
|
-
for (const projector of this.options.resultProjectors ?? []) {
|
|
413
|
-
try {
|
|
414
|
-
projector({
|
|
415
|
-
name,
|
|
416
|
-
output,
|
|
417
|
-
callId,
|
|
418
|
-
emit: (event, payload) => {
|
|
419
|
-
this.eventEmitter?.emit(event, payload);
|
|
420
|
-
}
|
|
421
|
-
});
|
|
422
|
-
} catch (error) {
|
|
423
|
-
const projectorError = error instanceof Error ? error : new Error(String(error));
|
|
424
|
-
this.log("warn", "Realtime result projector failed", {
|
|
425
|
-
name,
|
|
426
|
-
callId,
|
|
427
|
-
error: projectorError.message
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
emitToolError(name, error, callId) {
|
|
433
|
-
this.log("warn", "Realtime tool execution failed", {
|
|
434
|
-
name,
|
|
435
|
-
callId,
|
|
436
|
-
error: error.message
|
|
437
|
-
});
|
|
438
|
-
this.eventEmitter?.emit("realtime:tool:error", {
|
|
439
|
-
name,
|
|
440
|
-
error,
|
|
441
|
-
callId
|
|
442
|
-
});
|
|
443
|
-
}
|
|
444
755
|
ensureResponseStarted() {
|
|
445
756
|
if (this.state.response.status === "responding") {
|
|
446
757
|
return;
|
|
@@ -464,7 +775,7 @@ var RealtimeManagerImpl = class {
|
|
|
464
775
|
this.reconnectToken += 1;
|
|
465
776
|
const reconnectToken = this.reconnectToken;
|
|
466
777
|
if (this.state.response.status === "responding") {
|
|
467
|
-
this.
|
|
778
|
+
this.audioOutput.end();
|
|
468
779
|
this.state = {
|
|
469
780
|
...this.state,
|
|
470
781
|
connection: "connecting",
|
|
@@ -533,7 +844,7 @@ var RealtimeManagerImpl = class {
|
|
|
533
844
|
lastError: null
|
|
534
845
|
};
|
|
535
846
|
this.emitState();
|
|
536
|
-
void this.requestWakeLock();
|
|
847
|
+
void this.browserLifecycle.requestWakeLock();
|
|
537
848
|
this.eventEmitter?.emit("realtime:reconnect:success", {
|
|
538
849
|
attempts: attempt,
|
|
539
850
|
totalMs: Date.now() - startedAt,
|
|
@@ -587,7 +898,7 @@ var RealtimeManagerImpl = class {
|
|
|
587
898
|
}
|
|
588
899
|
applyError(error, connection) {
|
|
589
900
|
const typedError = toCharivoError("transport", error);
|
|
590
|
-
this.
|
|
901
|
+
this.audioOutput.end();
|
|
591
902
|
this.state = {
|
|
592
903
|
...this.state,
|
|
593
904
|
connection,
|
|
@@ -600,71 +911,6 @@ var RealtimeManagerImpl = class {
|
|
|
600
911
|
error: typedError.message
|
|
601
912
|
});
|
|
602
913
|
}
|
|
603
|
-
emitAudioStart() {
|
|
604
|
-
if (this.isAudioPlaybackActive) {
|
|
605
|
-
return;
|
|
606
|
-
}
|
|
607
|
-
this.isAudioPlaybackActive = true;
|
|
608
|
-
this.eventEmitter?.emit("tts:audio:start", {});
|
|
609
|
-
}
|
|
610
|
-
emitAudioEnd() {
|
|
611
|
-
if (!this.isAudioPlaybackActive) {
|
|
612
|
-
return;
|
|
613
|
-
}
|
|
614
|
-
this.isAudioPlaybackActive = false;
|
|
615
|
-
this.eventEmitter?.emit("tts:lipsync:update", { rms: 0 });
|
|
616
|
-
this.eventEmitter?.emit("tts:audio:end", {});
|
|
617
|
-
}
|
|
618
|
-
installBrowserLifecycleListeners() {
|
|
619
|
-
if (this.teardownBrowserLifecycle) {
|
|
620
|
-
return;
|
|
621
|
-
}
|
|
622
|
-
this.teardownBrowserLifecycle = subscribeBrowserLifecycle({
|
|
623
|
-
onHidden: () => {
|
|
624
|
-
void this.releaseWakeLock();
|
|
625
|
-
},
|
|
626
|
-
onPageHide: () => {
|
|
627
|
-
void this.releaseWakeLock();
|
|
628
|
-
},
|
|
629
|
-
onPageShow: () => {
|
|
630
|
-
void this.requestWakeLock();
|
|
631
|
-
},
|
|
632
|
-
onVisible: () => {
|
|
633
|
-
void this.requestWakeLock();
|
|
634
|
-
}
|
|
635
|
-
});
|
|
636
|
-
}
|
|
637
|
-
async requestWakeLock() {
|
|
638
|
-
if (this.state.session.status !== "active" || this.state.connection === "disconnecting" || typeof navigator === "undefined" || typeof document === "undefined" || document.visibilityState !== "visible" || !("wakeLock" in navigator)) {
|
|
639
|
-
return;
|
|
640
|
-
}
|
|
641
|
-
if (this.wakeLockSentinel) {
|
|
642
|
-
return;
|
|
643
|
-
}
|
|
644
|
-
const wakeLockApi = navigator;
|
|
645
|
-
try {
|
|
646
|
-
this.wakeLockSentinel = await wakeLockApi.wakeLock?.request("screen");
|
|
647
|
-
this.wakeLockSentinel?.addEventListener?.(
|
|
648
|
-
"release",
|
|
649
|
-
() => {
|
|
650
|
-
this.wakeLockSentinel = null;
|
|
651
|
-
},
|
|
652
|
-
{ once: true }
|
|
653
|
-
);
|
|
654
|
-
} catch {
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
async releaseWakeLock() {
|
|
658
|
-
if (!this.wakeLockSentinel) {
|
|
659
|
-
return;
|
|
660
|
-
}
|
|
661
|
-
const wakeLockSentinel = this.wakeLockSentinel;
|
|
662
|
-
this.wakeLockSentinel = null;
|
|
663
|
-
try {
|
|
664
|
-
await wakeLockSentinel.release();
|
|
665
|
-
} catch {
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
914
|
resolveNextSessionBaseConfig(config) {
|
|
669
915
|
return mergeSessionConfig(
|
|
670
916
|
this.sessionBaseConfig ?? this.options.defaultSessionConfig,
|
|
@@ -797,10 +1043,8 @@ var RealtimeManagerImpl = class {
|
|
|
797
1043
|
}
|
|
798
1044
|
finalizeFailedSession(error) {
|
|
799
1045
|
this.cancelReconnectLoop();
|
|
800
|
-
this.
|
|
801
|
-
this.
|
|
802
|
-
void this.releaseWakeLock();
|
|
803
|
-
this.emitAudioEnd();
|
|
1046
|
+
this.browserLifecycle.dispose();
|
|
1047
|
+
this.audioOutput.end();
|
|
804
1048
|
this.state = {
|
|
805
1049
|
...this.state,
|
|
806
1050
|
connection: "error",
|
|
@@ -824,10 +1068,8 @@ var RealtimeManagerImpl = class {
|
|
|
824
1068
|
}
|
|
825
1069
|
finalizeStoppedSession(emitSessionEnd, reason) {
|
|
826
1070
|
this.cancelReconnectLoop();
|
|
827
|
-
this.
|
|
828
|
-
this.
|
|
829
|
-
void this.releaseWakeLock();
|
|
830
|
-
this.emitAudioEnd();
|
|
1071
|
+
this.browserLifecycle.dispose();
|
|
1072
|
+
this.audioOutput.end();
|
|
831
1073
|
this.state = {
|
|
832
1074
|
...this.state,
|
|
833
1075
|
connection: "idle",
|
|
@@ -875,73 +1117,6 @@ var RealtimeManagerImpl = class {
|
|
|
875
1117
|
function createRealtimeManager(client, options) {
|
|
876
1118
|
return new RealtimeManagerImpl(client, options);
|
|
877
1119
|
}
|
|
878
|
-
function mergeSessionConfig(baseConfig, overrideConfig) {
|
|
879
|
-
if (!baseConfig && !overrideConfig) {
|
|
880
|
-
return void 0;
|
|
881
|
-
}
|
|
882
|
-
const { tools: _ignoredOverrideTools, ...overrideWithoutTools } = overrideConfig ?? {};
|
|
883
|
-
return {
|
|
884
|
-
...baseConfig,
|
|
885
|
-
...overrideWithoutTools
|
|
886
|
-
};
|
|
887
|
-
}
|
|
888
|
-
function mergeRealtimeState(current, partial) {
|
|
889
|
-
return {
|
|
890
|
-
connection: partial.connection ?? current.connection,
|
|
891
|
-
session: {
|
|
892
|
-
status: partial.session?.status ?? current.session.status,
|
|
893
|
-
config: partial.session?.config ?? current.session.config,
|
|
894
|
-
characterId: partial.session?.characterId ?? current.session.characterId
|
|
895
|
-
},
|
|
896
|
-
response: {
|
|
897
|
-
status: partial.response?.status ?? current.response.status,
|
|
898
|
-
text: partial.response?.text ?? current.response.text
|
|
899
|
-
},
|
|
900
|
-
lastError: partial.lastError ?? current.lastError
|
|
901
|
-
};
|
|
902
|
-
}
|
|
903
|
-
function createRealtimeSessionId() {
|
|
904
|
-
const randomUuid = globalThis.crypto?.randomUUID?.();
|
|
905
|
-
if (randomUuid) {
|
|
906
|
-
return randomUuid;
|
|
907
|
-
}
|
|
908
|
-
return `realtime-session-${Date.now()}-${nextFallbackSessionId++}`;
|
|
909
|
-
}
|
|
910
|
-
function isRecord(value) {
|
|
911
|
-
return typeof value === "object" && value !== null;
|
|
912
|
-
}
|
|
913
|
-
function createFailureOutput(error) {
|
|
914
|
-
return {
|
|
915
|
-
success: false,
|
|
916
|
-
error: error.message
|
|
917
|
-
};
|
|
918
|
-
}
|
|
919
|
-
async function withTimeout(promise, timeoutMs, toolName) {
|
|
920
|
-
let timeoutId;
|
|
921
|
-
try {
|
|
922
|
-
return await Promise.race([
|
|
923
|
-
promise,
|
|
924
|
-
new Promise((_, reject) => {
|
|
925
|
-
timeoutId = setTimeout(() => {
|
|
926
|
-
reject(
|
|
927
|
-
new Error(
|
|
928
|
-
`Realtime tool "${toolName}" timed out after ${timeoutMs}ms`
|
|
929
|
-
)
|
|
930
|
-
);
|
|
931
|
-
}, timeoutMs);
|
|
932
|
-
})
|
|
933
|
-
]);
|
|
934
|
-
} finally {
|
|
935
|
-
if (timeoutId !== void 0) {
|
|
936
|
-
clearTimeout(timeoutId);
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
|
-
function delay(ms) {
|
|
941
|
-
return new Promise((resolve) => {
|
|
942
|
-
setTimeout(resolve, ms);
|
|
943
|
-
});
|
|
944
|
-
}
|
|
945
1120
|
|
|
946
1121
|
export { RealtimeManagerImpl, createRealtimeManager };
|
|
947
1122
|
//# sourceMappingURL=index.mjs.map
|