@charivo/realtime 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -26,7 +26,8 @@ function buildRealtimeSessionConfig({
26
26
  temperature: baseConfig?.temperature,
27
27
  maxTokens: baseConfig?.maxTokens,
28
28
  tools: baseConfig?.tools,
29
- toolChoice: baseConfig?.toolChoice ?? "auto"
29
+ toolChoice: baseConfig?.toolChoice ?? "auto",
30
+ inputAudioTranscription: baseConfig?.inputAudioTranscription
30
31
  };
31
32
  }
32
33
  function buildCharacterInstructions(character) {
@@ -54,10 +55,419 @@ function ensureSentenceFragment(text) {
54
55
  return text.trim().replace(/[.!?]+$/, "");
55
56
  }
56
57
 
58
+ // src/internal/audio-output.ts
59
+ var RealtimeAudioOutput = class {
60
+ constructor(emit) {
61
+ this.emit = emit;
62
+ }
63
+ active = false;
64
+ isActive() {
65
+ return this.active;
66
+ }
67
+ start() {
68
+ if (this.active) {
69
+ return;
70
+ }
71
+ this.active = true;
72
+ this.emit("tts:audio:start", {});
73
+ }
74
+ end() {
75
+ if (!this.active) {
76
+ return;
77
+ }
78
+ this.active = false;
79
+ this.emit("tts:lipsync:update", { rms: 0 });
80
+ this.emit("tts:audio:end", {});
81
+ }
82
+ };
83
+ var RealtimeBrowserLifecycle = class {
84
+ constructor(getState) {
85
+ this.getState = getState;
86
+ }
87
+ wakeLockSentinel = null;
88
+ teardownBrowserLifecycle;
89
+ install() {
90
+ if (this.teardownBrowserLifecycle) {
91
+ return;
92
+ }
93
+ this.teardownBrowserLifecycle = core.subscribeBrowserLifecycle({
94
+ onHidden: () => {
95
+ void this.releaseWakeLock();
96
+ },
97
+ onPageHide: () => {
98
+ void this.releaseWakeLock();
99
+ },
100
+ onPageShow: () => {
101
+ void this.requestWakeLock();
102
+ },
103
+ onVisible: () => {
104
+ void this.requestWakeLock();
105
+ }
106
+ });
107
+ }
108
+ dispose() {
109
+ this.teardownBrowserLifecycle?.();
110
+ this.teardownBrowserLifecycle = void 0;
111
+ void this.releaseWakeLock();
112
+ }
113
+ async requestWakeLock() {
114
+ const state = this.getState();
115
+ if (state.session.status !== "active" || state.connection === "disconnecting" || typeof navigator === "undefined" || typeof document === "undefined" || document.visibilityState !== "visible" || !("wakeLock" in navigator)) {
116
+ return;
117
+ }
118
+ if (this.wakeLockSentinel) {
119
+ return;
120
+ }
121
+ const wakeLockApi = navigator;
122
+ try {
123
+ this.wakeLockSentinel = await wakeLockApi.wakeLock?.request("screen");
124
+ this.wakeLockSentinel?.addEventListener?.(
125
+ "release",
126
+ () => {
127
+ this.wakeLockSentinel = null;
128
+ },
129
+ { once: true }
130
+ );
131
+ } catch {
132
+ }
133
+ }
134
+ async releaseWakeLock() {
135
+ if (!this.wakeLockSentinel) {
136
+ return;
137
+ }
138
+ const wakeLockSentinel = this.wakeLockSentinel;
139
+ this.wakeLockSentinel = null;
140
+ try {
141
+ await wakeLockSentinel.release();
142
+ } catch {
143
+ }
144
+ }
145
+ };
146
+
147
+ // src/internal/manager-state.ts
148
+ var nextFallbackSessionId = 1;
149
+ function mergeSessionConfig(baseConfig, overrideConfig) {
150
+ if (!baseConfig && !overrideConfig) {
151
+ return void 0;
152
+ }
153
+ const { tools: _ignoredOverrideTools, ...overrideWithoutTools } = overrideConfig ?? {};
154
+ return {
155
+ ...baseConfig,
156
+ ...overrideWithoutTools
157
+ };
158
+ }
159
+ function mergeRealtimeState(current, partial) {
160
+ return {
161
+ connection: partial.connection ?? current.connection,
162
+ session: {
163
+ status: partial.session?.status ?? current.session.status,
164
+ config: partial.session?.config ?? current.session.config,
165
+ characterId: partial.session?.characterId ?? current.session.characterId
166
+ },
167
+ response: {
168
+ status: partial.response?.status ?? current.response.status,
169
+ text: partial.response?.text ?? current.response.text
170
+ },
171
+ lastError: partial.lastError ?? current.lastError
172
+ };
173
+ }
174
+ function createRealtimeSessionId() {
175
+ const randomUuid = globalThis.crypto?.randomUUID?.();
176
+ if (randomUuid) {
177
+ return randomUuid;
178
+ }
179
+ return `realtime-session-${Date.now()}-${nextFallbackSessionId++}`;
180
+ }
181
+
182
+ // src/internal/timing.ts
183
+ function delay(ms) {
184
+ return new Promise((resolve) => {
185
+ setTimeout(resolve, ms);
186
+ });
187
+ }
188
+
189
+ // src/internal/tool-registry.ts
190
+ var RealtimeToolRegistry = class {
191
+ tools = /* @__PURE__ */ new Map();
192
+ register(tool) {
193
+ this.tools.set(tool.definition.name, tool);
194
+ }
195
+ unregister(name) {
196
+ this.tools.delete(name);
197
+ }
198
+ get(name) {
199
+ return this.tools.get(name);
200
+ }
201
+ getDefinitions() {
202
+ return Array.from(this.tools.values(), (tool) => ({
203
+ ...tool.definition,
204
+ parameters: {
205
+ ...tool.definition.parameters,
206
+ properties: { ...tool.definition.parameters.properties },
207
+ required: tool.definition.parameters.required ? [...tool.definition.parameters.required] : void 0
208
+ }
209
+ }));
210
+ }
211
+ };
212
+ function isRecord(value) {
213
+ return typeof value === "object" && value !== null;
214
+ }
215
+
216
+ // src/internal/tool-args-validation.ts
217
+ function validateToolArguments(definition, args) {
218
+ if (!isRecord(args) || Array.isArray(args)) {
219
+ throw new Error(
220
+ `Realtime tool "${definition.name}" arguments failed schema validation: arguments must be an object`
221
+ );
222
+ }
223
+ const { parameters } = definition;
224
+ const required = parameters.required ?? [];
225
+ for (const propertyName of required) {
226
+ if (!hasOwnProperty(args, propertyName)) {
227
+ throw new Error(
228
+ `Realtime tool "${definition.name}" arguments failed schema validation: missing required property "${propertyName}"`
229
+ );
230
+ }
231
+ }
232
+ for (const [propertyName, schema] of Object.entries(parameters.properties)) {
233
+ if (!hasOwnProperty(args, propertyName) || !isRecord(schema)) {
234
+ continue;
235
+ }
236
+ validateToolArgumentValue(
237
+ definition.name,
238
+ propertyName,
239
+ args[propertyName],
240
+ schema
241
+ );
242
+ }
243
+ }
244
+ function validateToolArgumentValue(toolName, propertyName, value, schema) {
245
+ const enumValues = schema.enum;
246
+ if (Array.isArray(enumValues) && !enumValues.some((item) => Object.is(item, value))) {
247
+ throw new Error(
248
+ `Realtime tool "${toolName}" arguments failed schema validation: property "${propertyName}" must be one of ${formatEnumValues(enumValues)}`
249
+ );
250
+ }
251
+ const expectedType = schema.type;
252
+ if (typeof expectedType !== "string") {
253
+ return;
254
+ }
255
+ if (!isSupportedSchemaType(expectedType)) {
256
+ throw new Error(
257
+ `Realtime tool "${toolName}" arguments failed schema validation: property "${propertyName}" uses unsupported schema type "${expectedType}"`
258
+ );
259
+ }
260
+ if (!matchesSchemaType(value, expectedType)) {
261
+ throw new Error(
262
+ `Realtime tool "${toolName}" arguments failed schema validation: property "${propertyName}" must be ${expectedType}`
263
+ );
264
+ }
265
+ }
266
+ function matchesSchemaType(value, expectedType) {
267
+ switch (expectedType) {
268
+ case "string":
269
+ return typeof value === "string";
270
+ case "number":
271
+ return typeof value === "number" && Number.isFinite(value);
272
+ case "integer":
273
+ return typeof value === "number" && Number.isInteger(value);
274
+ case "boolean":
275
+ return typeof value === "boolean";
276
+ case "object":
277
+ return isRecord(value) && !Array.isArray(value);
278
+ case "array":
279
+ return Array.isArray(value);
280
+ case "null":
281
+ return value === null;
282
+ }
283
+ return false;
284
+ }
285
+ function isSupportedSchemaType(expectedType) {
286
+ return expectedType === "string" || expectedType === "number" || expectedType === "integer" || expectedType === "boolean" || expectedType === "object" || expectedType === "array" || expectedType === "null";
287
+ }
288
+ function formatEnumValues(values) {
289
+ return values.map((value) => JSON.stringify(value)).join(", ");
290
+ }
291
+ function hasOwnProperty(record, propertyName) {
292
+ return Object.prototype.hasOwnProperty.call(record, propertyName);
293
+ }
294
+
295
+ // src/internal/tool-execution.ts
296
+ function createFailureOutput(error) {
297
+ return {
298
+ success: false,
299
+ error: error.message
300
+ };
301
+ }
302
+ async function withTimeout(promise, timeoutMs, toolName) {
303
+ let timeoutId;
304
+ try {
305
+ return await Promise.race([
306
+ promise,
307
+ new Promise((_, reject) => {
308
+ timeoutId = setTimeout(() => {
309
+ reject(
310
+ new Error(
311
+ `Realtime tool "${toolName}" timed out after ${timeoutMs}ms`
312
+ )
313
+ );
314
+ }, timeoutMs);
315
+ })
316
+ ]);
317
+ } finally {
318
+ if (timeoutId !== void 0) {
319
+ clearTimeout(timeoutId);
320
+ }
321
+ }
322
+ }
323
+
324
+ // src/internal/tool-runner.ts
325
+ async function executeRealtimeToolCall({
326
+ event,
327
+ tool,
328
+ client,
329
+ character,
330
+ state,
331
+ defaultToolTimeoutMs,
332
+ resultProjectors,
333
+ emit,
334
+ log
335
+ }) {
336
+ if (!event.callId) {
337
+ emitToolError(
338
+ event.name,
339
+ new Error(`Tool "${event.name}" is missing a call ID`),
340
+ event.callId,
341
+ emit,
342
+ log
343
+ );
344
+ return;
345
+ }
346
+ if (!tool) {
347
+ await handleToolExecutionFailure(
348
+ event.name,
349
+ event.callId,
350
+ new Error(`No realtime tool registered for "${event.name}"`),
351
+ client,
352
+ emit,
353
+ log
354
+ );
355
+ return;
356
+ }
357
+ try {
358
+ log("debug", "Realtime tool execution started", {
359
+ name: event.name,
360
+ callId: event.callId
361
+ });
362
+ const output = await runToolHandler({
363
+ tool,
364
+ event,
365
+ character,
366
+ state,
367
+ defaultToolTimeoutMs
368
+ });
369
+ await client.sendToolResult(event.callId, output);
370
+ emit("realtime:tool:result", {
371
+ name: event.name,
372
+ output,
373
+ callId: event.callId
374
+ });
375
+ projectToolResult(
376
+ event.name,
377
+ output,
378
+ resultProjectors,
379
+ emit,
380
+ log,
381
+ event.callId
382
+ );
383
+ log("info", "Realtime tool execution succeeded", {
384
+ name: event.name,
385
+ callId: event.callId
386
+ });
387
+ } catch (error) {
388
+ await handleToolExecutionFailure(
389
+ event.name,
390
+ event.callId,
391
+ error instanceof Error ? error : new Error(String(error)),
392
+ client,
393
+ emit,
394
+ log
395
+ );
396
+ }
397
+ }
398
+ async function runToolHandler({
399
+ tool,
400
+ event,
401
+ character,
402
+ state,
403
+ defaultToolTimeoutMs
404
+ }) {
405
+ validateToolArguments(tool.definition, event.args);
406
+ const result = await withTimeout(
407
+ tool.handler(event.args, {
408
+ character,
409
+ state,
410
+ callId: event.callId
411
+ }),
412
+ tool.timeoutMs ?? defaultToolTimeoutMs,
413
+ tool.definition.name
414
+ );
415
+ if (!isRecord(result)) {
416
+ throw new Error(
417
+ `Realtime tool "${tool.definition.name}" must return an object`
418
+ );
419
+ }
420
+ return result;
421
+ }
422
+ async function handleToolExecutionFailure(name, callId, error, client, emit, log) {
423
+ emitToolError(name, error, callId, emit, log);
424
+ try {
425
+ await client.sendToolResult(callId, createFailureOutput(error));
426
+ } catch (sendError) {
427
+ emitToolError(
428
+ name,
429
+ sendError instanceof Error ? sendError : new Error(String(sendError)),
430
+ callId,
431
+ emit,
432
+ log
433
+ );
434
+ }
435
+ }
436
+ function projectToolResult(name, output, resultProjectors, emit, log, callId) {
437
+ for (const projector of resultProjectors ?? []) {
438
+ try {
439
+ projector({
440
+ name,
441
+ output,
442
+ callId,
443
+ emit
444
+ });
445
+ } catch (error) {
446
+ const projectorError = error instanceof Error ? error : new Error(String(error));
447
+ log("warn", "Realtime result projector failed", {
448
+ name,
449
+ callId,
450
+ error: projectorError.message
451
+ });
452
+ }
453
+ }
454
+ }
455
+ function emitToolError(name, error, callId, emit, log) {
456
+ log("warn", "Realtime tool execution failed", {
457
+ name,
458
+ callId,
459
+ error: error.message
460
+ });
461
+ emit("realtime:tool:error", {
462
+ name,
463
+ error,
464
+ callId
465
+ });
466
+ }
467
+
57
468
  // src/realtime-manager.ts
58
469
  var DEFAULT_TOOL_TIMEOUT_MS = 1e4;
59
470
  var RECONNECT_DELAYS_MS = [500, 1e3, 2e3, 4e3, 5e3];
60
- var nextFallbackSessionId = 1;
61
471
  var RealtimeManagerImpl = class {
62
472
  constructor(client, options = {}) {
63
473
  this.client = client;
@@ -65,7 +475,7 @@ var RealtimeManagerImpl = class {
65
475
  for (const tool of options.tools ?? []) {
66
476
  this.registerTool(tool);
67
477
  }
68
- this.installBrowserLifecycleListeners();
478
+ this.browserLifecycle.install();
69
479
  this.client.onEvent((event) => {
70
480
  void this.handleClientEvent(event).catch((error) => {
71
481
  this.applyError(
@@ -80,17 +490,22 @@ var RealtimeManagerImpl = class {
80
490
  isStoppingSession = false;
81
491
  isRefreshingSession = false;
82
492
  isRecoveringConnection = false;
83
- isAudioPlaybackActive = false;
84
493
  sessionBaseConfig;
85
494
  refreshInFlight = null;
86
495
  reconnectInFlight = null;
87
496
  hasQueuedRefresh = false;
88
497
  stopRequestedDuringRefresh = false;
89
498
  reconnectToken = 0;
90
- wakeLockSentinel = null;
91
- teardownBrowserLifecycle;
499
+ responseOutstanding = false;
500
+ hasPendingToolRefresh = false;
92
501
  sessionId;
93
- toolRegistry = /* @__PURE__ */ new Map();
502
+ toolRegistry = new RealtimeToolRegistry();
503
+ audioOutput = new RealtimeAudioOutput((event, payload) => {
504
+ this.eventEmitter?.emit(event, payload);
505
+ });
506
+ browserLifecycle = new RealtimeBrowserLifecycle(
507
+ () => this.state
508
+ );
94
509
  state = {
95
510
  connection: "idle",
96
511
  session: {
@@ -118,20 +533,15 @@ var RealtimeManagerImpl = class {
118
533
  this.emitState();
119
534
  }
120
535
  registerTool(tool) {
121
- this.toolRegistry.set(tool.definition.name, tool);
536
+ this.toolRegistry.register(tool);
537
+ this.requestToolRefresh();
122
538
  }
123
539
  unregisterTool(name) {
124
- this.toolRegistry.delete(name);
540
+ this.toolRegistry.unregister(name);
541
+ this.requestToolRefresh();
125
542
  }
126
543
  getRegisteredTools() {
127
- return Array.from(this.toolRegistry.values(), (tool) => ({
128
- ...tool.definition,
129
- parameters: {
130
- ...tool.definition.parameters,
131
- properties: { ...tool.definition.parameters.properties },
132
- required: tool.definition.parameters.required ? [...tool.definition.parameters.required] : void 0
133
- }
134
- }));
544
+ return this.toolRegistry.getDefinitions();
135
545
  }
136
546
  getState() {
137
547
  return {
@@ -169,12 +579,12 @@ var RealtimeManagerImpl = class {
169
579
  },
170
580
  lastError: null
171
581
  };
172
- this.installBrowserLifecycleListeners();
582
+ this.browserLifecycle.install();
173
583
  this.emitState();
174
584
  try {
175
585
  await this.client.connect(sessionConfig);
176
586
  this.commitActiveSession(sessionConfig);
177
- void this.requestWakeLock();
587
+ void this.browserLifecycle.requestWakeLock();
178
588
  this.emitSessionStart("user");
179
589
  } catch (error) {
180
590
  const typedError = core.toCharivoError("transport", error);
@@ -240,7 +650,16 @@ var RealtimeManagerImpl = class {
240
650
  if (this.state.connection !== "connected") {
241
651
  throw new core.CharivoStateError("Realtime session is reconnecting");
242
652
  }
243
- await this.client.sendText(text).catch((error) => Promise.reject(core.toCharivoError("transport", error)));
653
+ if (this.state.response.status === "responding" || this.responseOutstanding) {
654
+ throw new core.CharivoStateError(
655
+ "Response already in progress. Call interrupt() before sending a new message."
656
+ );
657
+ }
658
+ this.responseOutstanding = true;
659
+ await this.client.sendText(text).catch((error) => {
660
+ this.responseOutstanding = false;
661
+ return Promise.reject(core.toCharivoError("transport", error));
662
+ });
244
663
  }
245
664
  async sendAudioChunk(audio) {
246
665
  if (this.state.session.status !== "active") {
@@ -259,6 +678,7 @@ var RealtimeManagerImpl = class {
259
678
  throw new core.CharivoStateError("Realtime session is reconnecting");
260
679
  }
261
680
  await this.client.interrupt().catch((error) => Promise.reject(core.toCharivoError("transport", error)));
681
+ this.responseOutstanding = false;
262
682
  this.state = {
263
683
  ...this.state,
264
684
  response: {
@@ -266,8 +686,9 @@ var RealtimeManagerImpl = class {
266
686
  status: "interrupted"
267
687
  }
268
688
  };
269
- this.emitAudioEnd();
689
+ this.audioOutput.end();
270
690
  this.emitState();
691
+ this.flushPendingToolRefresh();
271
692
  }
272
693
  async handleClientEvent(event) {
273
694
  switch (event.type) {
@@ -328,7 +749,9 @@ var RealtimeManagerImpl = class {
328
749
  this.emitState();
329
750
  return;
330
751
  case "assistant.response.completed": {
752
+ this.responseOutstanding = false;
331
753
  if (this.state.response.status === "interrupted") {
754
+ this.flushPendingToolRefresh();
332
755
  return;
333
756
  }
334
757
  const text = event.text || this.state.response.text;
@@ -342,17 +765,18 @@ var RealtimeManagerImpl = class {
342
765
  };
343
766
  this.eventEmitter?.emit("realtime:assistant:done", { text });
344
767
  this.emitState();
768
+ this.flushPendingToolRefresh();
345
769
  return;
346
770
  }
347
771
  case "audio.output.started":
348
- this.emitAudioStart();
772
+ this.audioOutput.start();
349
773
  return;
350
774
  case "audio.output.ended":
351
- this.emitAudioEnd();
775
+ this.audioOutput.end();
352
776
  return;
353
777
  case "audio.lipsync":
354
- if (event.rms > 1e-3 && !this.isAudioPlaybackActive) {
355
- this.emitAudioStart();
778
+ if (event.rms > 1e-3 && !this.audioOutput.isActive()) {
779
+ this.audioOutput.start();
356
780
  }
357
781
  this.eventEmitter?.emit("tts:lipsync:update", { rms: event.rms });
358
782
  return;
@@ -362,7 +786,21 @@ var RealtimeManagerImpl = class {
362
786
  args: event.args,
363
787
  callId: event.callId
364
788
  });
365
- await this.executeToolCall(event);
789
+ await executeRealtimeToolCall({
790
+ event,
791
+ tool: this.toolRegistry.get(event.name),
792
+ client: this.client,
793
+ character: this.character,
794
+ state: this.getState(),
795
+ defaultToolTimeoutMs: this.options.defaultToolTimeoutMs ?? DEFAULT_TOOL_TIMEOUT_MS,
796
+ resultProjectors: this.options.resultProjectors,
797
+ emit: (eventName, payload) => {
798
+ this.eventEmitter?.emit(eventName, payload);
799
+ },
800
+ log: (level, message, context) => {
801
+ this.log(level, message, context);
802
+ }
803
+ });
366
804
  return;
367
805
  case "tool.result":
368
806
  this.eventEmitter?.emit("realtime:tool:result", {
@@ -384,116 +822,21 @@ var RealtimeManagerImpl = class {
384
822
  this.emitState();
385
823
  return;
386
824
  }
825
+ this.responseOutstanding = false;
826
+ if (this.state.response.status === "responding") {
827
+ this.audioOutput.end();
828
+ this.state = {
829
+ ...this.state,
830
+ response: { status: "interrupted", text: this.state.response.text }
831
+ };
832
+ }
387
833
  this.applyError(event.error, this.state.connection);
834
+ if (this.state.session.status === "active") {
835
+ this.flushPendingToolRefresh();
836
+ }
388
837
  return;
389
838
  }
390
839
  }
391
- async executeToolCall(event) {
392
- const tool = this.toolRegistry.get(event.name);
393
- if (!event.callId) {
394
- this.emitToolError(
395
- event.name,
396
- new Error(`Tool "${event.name}" is missing a call ID`),
397
- event.callId
398
- );
399
- return;
400
- }
401
- if (!tool) {
402
- await this.handleToolExecutionFailure(
403
- event.name,
404
- event.callId,
405
- new Error(`No realtime tool registered for "${event.name}"`)
406
- );
407
- return;
408
- }
409
- try {
410
- this.log("debug", "Realtime tool execution started", {
411
- name: event.name,
412
- callId: event.callId
413
- });
414
- const output = await this.runToolHandler(tool, event);
415
- await this.client.sendToolResult(event.callId, output);
416
- this.eventEmitter?.emit("realtime:tool:result", {
417
- name: event.name,
418
- output,
419
- callId: event.callId
420
- });
421
- this.projectToolResult(event.name, output, event.callId);
422
- this.log("info", "Realtime tool execution succeeded", {
423
- name: event.name,
424
- callId: event.callId
425
- });
426
- } catch (error) {
427
- await this.handleToolExecutionFailure(
428
- event.name,
429
- event.callId,
430
- error instanceof Error ? error : new Error(String(error))
431
- );
432
- }
433
- }
434
- async runToolHandler(tool, event) {
435
- const timeoutMs = tool.timeoutMs ?? this.options.defaultToolTimeoutMs ?? DEFAULT_TOOL_TIMEOUT_MS;
436
- const result = await withTimeout(
437
- tool.handler(event.args, {
438
- character: this.character,
439
- state: this.getState(),
440
- callId: event.callId
441
- }),
442
- timeoutMs,
443
- tool.definition.name
444
- );
445
- if (!isRecord(result)) {
446
- throw new Error(
447
- `Realtime tool "${tool.definition.name}" must return an object`
448
- );
449
- }
450
- return result;
451
- }
452
- async handleToolExecutionFailure(name, callId, error) {
453
- this.emitToolError(name, error, callId);
454
- try {
455
- await this.client.sendToolResult(callId, createFailureOutput(error));
456
- } catch (sendError) {
457
- this.emitToolError(
458
- name,
459
- sendError instanceof Error ? sendError : new Error(String(sendError)),
460
- callId
461
- );
462
- }
463
- }
464
- projectToolResult(name, output, callId) {
465
- for (const projector of this.options.resultProjectors ?? []) {
466
- try {
467
- projector({
468
- name,
469
- output,
470
- callId,
471
- emit: (event, payload) => {
472
- this.eventEmitter?.emit(event, payload);
473
- }
474
- });
475
- } catch (error) {
476
- const projectorError = error instanceof Error ? error : new Error(String(error));
477
- this.log("warn", "Realtime result projector failed", {
478
- name,
479
- callId,
480
- error: projectorError.message
481
- });
482
- }
483
- }
484
- }
485
- emitToolError(name, error, callId) {
486
- this.log("warn", "Realtime tool execution failed", {
487
- name,
488
- callId,
489
- error: error.message
490
- });
491
- this.eventEmitter?.emit("realtime:tool:error", {
492
- name,
493
- error,
494
- callId
495
- });
496
- }
497
840
  ensureResponseStarted() {
498
841
  if (this.state.response.status === "responding") {
499
842
  return;
@@ -513,11 +856,13 @@ var RealtimeManagerImpl = class {
513
856
  if (this.state.session.status !== "active" || this.isStoppingSession || this.state.connection === "disconnecting" || this.isRecoveringConnection) {
514
857
  return;
515
858
  }
859
+ this.responseOutstanding = false;
860
+ this.hasPendingToolRefresh = false;
516
861
  this.isRecoveringConnection = true;
517
862
  this.reconnectToken += 1;
518
863
  const reconnectToken = this.reconnectToken;
519
864
  if (this.state.response.status === "responding") {
520
- this.emitAudioEnd();
865
+ this.audioOutput.end();
521
866
  this.state = {
522
867
  ...this.state,
523
868
  connection: "connecting",
@@ -586,7 +931,7 @@ var RealtimeManagerImpl = class {
586
931
  lastError: null
587
932
  };
588
933
  this.emitState();
589
- void this.requestWakeLock();
934
+ void this.browserLifecycle.requestWakeLock();
590
935
  this.eventEmitter?.emit("realtime:reconnect:success", {
591
936
  attempts: attempt,
592
937
  totalMs: Date.now() - startedAt,
@@ -640,7 +985,7 @@ var RealtimeManagerImpl = class {
640
985
  }
641
986
  applyError(error, connection) {
642
987
  const typedError = core.toCharivoError("transport", error);
643
- this.emitAudioEnd();
988
+ this.audioOutput.end();
644
989
  this.state = {
645
990
  ...this.state,
646
991
  connection,
@@ -653,71 +998,6 @@ var RealtimeManagerImpl = class {
653
998
  error: typedError.message
654
999
  });
655
1000
  }
656
- emitAudioStart() {
657
- if (this.isAudioPlaybackActive) {
658
- return;
659
- }
660
- this.isAudioPlaybackActive = true;
661
- this.eventEmitter?.emit("tts:audio:start", {});
662
- }
663
- emitAudioEnd() {
664
- if (!this.isAudioPlaybackActive) {
665
- return;
666
- }
667
- this.isAudioPlaybackActive = false;
668
- this.eventEmitter?.emit("tts:lipsync:update", { rms: 0 });
669
- this.eventEmitter?.emit("tts:audio:end", {});
670
- }
671
- installBrowserLifecycleListeners() {
672
- if (this.teardownBrowserLifecycle) {
673
- return;
674
- }
675
- this.teardownBrowserLifecycle = core.subscribeBrowserLifecycle({
676
- onHidden: () => {
677
- void this.releaseWakeLock();
678
- },
679
- onPageHide: () => {
680
- void this.releaseWakeLock();
681
- },
682
- onPageShow: () => {
683
- void this.requestWakeLock();
684
- },
685
- onVisible: () => {
686
- void this.requestWakeLock();
687
- }
688
- });
689
- }
690
- async requestWakeLock() {
691
- if (this.state.session.status !== "active" || this.state.connection === "disconnecting" || typeof navigator === "undefined" || typeof document === "undefined" || document.visibilityState !== "visible" || !("wakeLock" in navigator)) {
692
- return;
693
- }
694
- if (this.wakeLockSentinel) {
695
- return;
696
- }
697
- const wakeLockApi = navigator;
698
- try {
699
- this.wakeLockSentinel = await wakeLockApi.wakeLock?.request("screen");
700
- this.wakeLockSentinel?.addEventListener?.(
701
- "release",
702
- () => {
703
- this.wakeLockSentinel = null;
704
- },
705
- { once: true }
706
- );
707
- } catch {
708
- }
709
- }
710
- async releaseWakeLock() {
711
- if (!this.wakeLockSentinel) {
712
- return;
713
- }
714
- const wakeLockSentinel = this.wakeLockSentinel;
715
- this.wakeLockSentinel = null;
716
- try {
717
- await wakeLockSentinel.release();
718
- } catch {
719
- }
720
- }
721
1001
  resolveNextSessionBaseConfig(config) {
722
1002
  return mergeSessionConfig(
723
1003
  this.sessionBaseConfig ?? this.options.defaultSessionConfig,
@@ -783,6 +1063,20 @@ var RealtimeManagerImpl = class {
783
1063
  this.isStoppingSession = false;
784
1064
  }
785
1065
  }
1066
+ requestToolRefresh() {
1067
+ if (this.state.session.status !== "active") return;
1068
+ if (this.state.response.status === "responding" || this.responseOutstanding) {
1069
+ this.hasPendingToolRefresh = true;
1070
+ return;
1071
+ }
1072
+ this.updateSession().catch(() => {
1073
+ });
1074
+ }
1075
+ flushPendingToolRefresh() {
1076
+ if (!this.hasPendingToolRefresh) return;
1077
+ this.hasPendingToolRefresh = false;
1078
+ this.requestToolRefresh();
1079
+ }
786
1080
  async runRefreshLoop() {
787
1081
  do {
788
1082
  this.hasQueuedRefresh = false;
@@ -849,11 +1143,11 @@ var RealtimeManagerImpl = class {
849
1143
  });
850
1144
  }
851
1145
  finalizeFailedSession(error) {
1146
+ this.responseOutstanding = false;
1147
+ this.hasPendingToolRefresh = false;
852
1148
  this.cancelReconnectLoop();
853
- this.teardownBrowserLifecycle?.();
854
- this.teardownBrowserLifecycle = void 0;
855
- void this.releaseWakeLock();
856
- this.emitAudioEnd();
1149
+ this.browserLifecycle.dispose();
1150
+ this.audioOutput.end();
857
1151
  this.state = {
858
1152
  ...this.state,
859
1153
  connection: "error",
@@ -876,11 +1170,11 @@ var RealtimeManagerImpl = class {
876
1170
  this.sessionId = void 0;
877
1171
  }
878
1172
  finalizeStoppedSession(emitSessionEnd, reason) {
1173
+ this.responseOutstanding = false;
1174
+ this.hasPendingToolRefresh = false;
879
1175
  this.cancelReconnectLoop();
880
- this.teardownBrowserLifecycle?.();
881
- this.teardownBrowserLifecycle = void 0;
882
- void this.releaseWakeLock();
883
- this.emitAudioEnd();
1176
+ this.browserLifecycle.dispose();
1177
+ this.audioOutput.end();
884
1178
  this.state = {
885
1179
  ...this.state,
886
1180
  connection: "idle",
@@ -928,73 +1222,6 @@ var RealtimeManagerImpl = class {
928
1222
  function createRealtimeManager(client, options) {
929
1223
  return new RealtimeManagerImpl(client, options);
930
1224
  }
931
- function mergeSessionConfig(baseConfig, overrideConfig) {
932
- if (!baseConfig && !overrideConfig) {
933
- return void 0;
934
- }
935
- const { tools: _ignoredOverrideTools, ...overrideWithoutTools } = overrideConfig ?? {};
936
- return {
937
- ...baseConfig,
938
- ...overrideWithoutTools
939
- };
940
- }
941
- function mergeRealtimeState(current, partial) {
942
- return {
943
- connection: partial.connection ?? current.connection,
944
- session: {
945
- status: partial.session?.status ?? current.session.status,
946
- config: partial.session?.config ?? current.session.config,
947
- characterId: partial.session?.characterId ?? current.session.characterId
948
- },
949
- response: {
950
- status: partial.response?.status ?? current.response.status,
951
- text: partial.response?.text ?? current.response.text
952
- },
953
- lastError: partial.lastError ?? current.lastError
954
- };
955
- }
956
- function createRealtimeSessionId() {
957
- const randomUuid = globalThis.crypto?.randomUUID?.();
958
- if (randomUuid) {
959
- return randomUuid;
960
- }
961
- return `realtime-session-${Date.now()}-${nextFallbackSessionId++}`;
962
- }
963
- function isRecord(value) {
964
- return typeof value === "object" && value !== null;
965
- }
966
- function createFailureOutput(error) {
967
- return {
968
- success: false,
969
- error: error.message
970
- };
971
- }
972
- async function withTimeout(promise, timeoutMs, toolName) {
973
- let timeoutId;
974
- try {
975
- return await Promise.race([
976
- promise,
977
- new Promise((_, reject) => {
978
- timeoutId = setTimeout(() => {
979
- reject(
980
- new Error(
981
- `Realtime tool "${toolName}" timed out after ${timeoutMs}ms`
982
- )
983
- );
984
- }, timeoutMs);
985
- })
986
- ]);
987
- } finally {
988
- if (timeoutId !== void 0) {
989
- clearTimeout(timeoutId);
990
- }
991
- }
992
- }
993
- function delay(ms) {
994
- return new Promise((resolve) => {
995
- setTimeout(resolve, ms);
996
- });
997
- }
998
1225
 
999
1226
  exports.DEFAULT_REALTIME_AGENT_INSTRUCTIONS = DEFAULT_REALTIME_AGENT_INSTRUCTIONS;
1000
1227
  exports.RealtimeManagerImpl = RealtimeManagerImpl;