@charivo/realtime 0.3.0 → 0.5.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.
Files changed (36) hide show
  1. package/README.md +23 -0
  2. package/dist/{chunk-FABRM56W.mjs → chunk-3O3COMUY.mjs} +216 -17
  3. package/dist/chunk-3O3COMUY.mjs.map +1 -0
  4. package/dist/{chunk-WLTIMNHQ.mjs → chunk-J5L3BPMG.mjs} +248 -13
  5. package/dist/chunk-J5L3BPMG.mjs.map +1 -0
  6. package/dist/chunk-ZJZO4QYA.mjs +123 -0
  7. package/dist/chunk-ZJZO4QYA.mjs.map +1 -0
  8. package/dist/index.d.mts +15 -3
  9. package/dist/index.d.ts +15 -3
  10. package/dist/index.js +240 -21
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +237 -21
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/openai/index.d.mts +29 -1
  15. package/dist/openai/index.d.ts +29 -1
  16. package/dist/openai/index.js +326 -13
  17. package/dist/openai/index.js.map +1 -1
  18. package/dist/openai/index.mjs +2 -2
  19. package/dist/openai-agents/index.d.mts +24 -1
  20. package/dist/openai-agents/index.d.ts +24 -1
  21. package/dist/openai-agents/index.js +293 -17
  22. package/dist/openai-agents/index.js.map +1 -1
  23. package/dist/openai-agents/index.mjs +2 -2
  24. package/dist/remote/index.d.mts +2 -1
  25. package/dist/remote/index.d.ts +2 -1
  26. package/dist/remote/index.js +572 -54
  27. package/dist/remote/index.js.map +1 -1
  28. package/dist/remote/index.mjs +38 -31
  29. package/dist/remote/index.mjs.map +1 -1
  30. package/dist/{types-Bc2tcYr1.d.mts → types-bmYuc_Vs.d.mts} +6 -1
  31. package/dist/{types-Bc2tcYr1.d.ts → types-bmYuc_Vs.d.ts} +6 -1
  32. package/package.json +2 -2
  33. package/dist/chunk-FABRM56W.mjs.map +0 -1
  34. package/dist/chunk-WLTIMNHQ.mjs.map +0 -1
  35. package/dist/chunk-XJNT4ZTF.mjs +0 -41
  36. package/dist/chunk-XJNT4ZTF.mjs.map +0 -1
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
- import { R as RealtimeTransportClient } from './types-Bc2tcYr1.mjs';
2
- export { b as RealtimeClient, a as RealtimeTransportEvent } from './types-Bc2tcYr1.mjs';
1
+ import { R as RealtimeTransportClient } from './types-bmYuc_Vs.mjs';
2
+ export { b as RealtimeClient, a as RealtimeTransportEvent } from './types-bmYuc_Vs.mjs';
3
3
  import { RealtimeSessionConfig, RealtimeToolRegistration, RealtimeManager, CharivoEventEmitter, Character, RealtimeTool, RealtimeState, MotionSelection, GazeCoordinates, AvatarControlCatalog } from '@charivo/core';
4
- export { Character, CharivoEventEmitter, RealtimeConnectionState, RealtimeManager, RealtimeProvider, RealtimeResponseStatus, RealtimeSessionBootstrap, RealtimeSessionConfig, RealtimeSessionRequest, RealtimeSessionStatus, RealtimeSessionTransitionReason, RealtimeState, RealtimeTool, RealtimeToolChoice, RealtimeToolContext, RealtimeToolHandler, RealtimeToolRegistration, RealtimeTransportKind } from '@charivo/core';
4
+ export { Character, CharivoEventEmitter, RealtimeConnectionState, RealtimeManager, RealtimeProvider, RealtimeReconnectCause, RealtimeResponseStatus, RealtimeSessionBootstrap, RealtimeSessionConfig, RealtimeSessionRequest, RealtimeSessionStatus, RealtimeSessionTransitionReason, RealtimeState, RealtimeTool, RealtimeToolChoice, RealtimeToolContext, RealtimeToolHandler, RealtimeToolRegistration, RealtimeTransportKind } from '@charivo/core';
5
5
 
6
6
  interface RealtimeManagerOptions {
7
7
  defaultSessionConfig?: Omit<RealtimeSessionConfig, "tools">;
@@ -22,11 +22,16 @@ declare class RealtimeManagerImpl implements RealtimeManager {
22
22
  private character;
23
23
  private isStoppingSession;
24
24
  private isRefreshingSession;
25
+ private isRecoveringConnection;
25
26
  private isAudioPlaybackActive;
26
27
  private sessionBaseConfig?;
27
28
  private refreshInFlight;
29
+ private reconnectInFlight;
28
30
  private hasQueuedRefresh;
29
31
  private stopRequestedDuringRefresh;
32
+ private reconnectToken;
33
+ private wakeLockSentinel;
34
+ private teardownBrowserLifecycle?;
30
35
  private readonly toolRegistry;
31
36
  private state;
32
37
  constructor(client: RealtimeTransportClient, options?: RealtimeManagerOptions);
@@ -49,10 +54,17 @@ declare class RealtimeManagerImpl implements RealtimeManager {
49
54
  private postProcessToolResult;
50
55
  private emitToolError;
51
56
  private ensureResponseStarted;
57
+ private beginReconnect;
58
+ private runReconnectLoop;
59
+ private cancelReconnectLoop;
60
+ private isReconnectTokenActive;
52
61
  private emitState;
53
62
  private applyError;
54
63
  private emitAudioStart;
55
64
  private emitAudioEnd;
65
+ private installBrowserLifecycleListeners;
66
+ private requestWakeLock;
67
+ private releaseWakeLock;
56
68
  private resolveNextSessionBaseConfig;
57
69
  private buildEffectiveSessionConfig;
58
70
  private commitActiveSession;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { R as RealtimeTransportClient } from './types-Bc2tcYr1.js';
2
- export { b as RealtimeClient, a as RealtimeTransportEvent } from './types-Bc2tcYr1.js';
1
+ import { R as RealtimeTransportClient } from './types-bmYuc_Vs.js';
2
+ export { b as RealtimeClient, a as RealtimeTransportEvent } from './types-bmYuc_Vs.js';
3
3
  import { RealtimeSessionConfig, RealtimeToolRegistration, RealtimeManager, CharivoEventEmitter, Character, RealtimeTool, RealtimeState, MotionSelection, GazeCoordinates, AvatarControlCatalog } from '@charivo/core';
4
- export { Character, CharivoEventEmitter, RealtimeConnectionState, RealtimeManager, RealtimeProvider, RealtimeResponseStatus, RealtimeSessionBootstrap, RealtimeSessionConfig, RealtimeSessionRequest, RealtimeSessionStatus, RealtimeSessionTransitionReason, RealtimeState, RealtimeTool, RealtimeToolChoice, RealtimeToolContext, RealtimeToolHandler, RealtimeToolRegistration, RealtimeTransportKind } from '@charivo/core';
4
+ export { Character, CharivoEventEmitter, RealtimeConnectionState, RealtimeManager, RealtimeProvider, RealtimeReconnectCause, RealtimeResponseStatus, RealtimeSessionBootstrap, RealtimeSessionConfig, RealtimeSessionRequest, RealtimeSessionStatus, RealtimeSessionTransitionReason, RealtimeState, RealtimeTool, RealtimeToolChoice, RealtimeToolContext, RealtimeToolHandler, RealtimeToolRegistration, RealtimeTransportKind } from '@charivo/core';
5
5
 
6
6
  interface RealtimeManagerOptions {
7
7
  defaultSessionConfig?: Omit<RealtimeSessionConfig, "tools">;
@@ -22,11 +22,16 @@ declare class RealtimeManagerImpl implements RealtimeManager {
22
22
  private character;
23
23
  private isStoppingSession;
24
24
  private isRefreshingSession;
25
+ private isRecoveringConnection;
25
26
  private isAudioPlaybackActive;
26
27
  private sessionBaseConfig?;
27
28
  private refreshInFlight;
29
+ private reconnectInFlight;
28
30
  private hasQueuedRefresh;
29
31
  private stopRequestedDuringRefresh;
32
+ private reconnectToken;
33
+ private wakeLockSentinel;
34
+ private teardownBrowserLifecycle?;
30
35
  private readonly toolRegistry;
31
36
  private state;
32
37
  constructor(client: RealtimeTransportClient, options?: RealtimeManagerOptions);
@@ -49,10 +54,17 @@ declare class RealtimeManagerImpl implements RealtimeManager {
49
54
  private postProcessToolResult;
50
55
  private emitToolError;
51
56
  private ensureResponseStarted;
57
+ private beginReconnect;
58
+ private runReconnectLoop;
59
+ private cancelReconnectLoop;
60
+ private isReconnectTokenActive;
52
61
  private emitState;
53
62
  private applyError;
54
63
  private emitAudioStart;
55
64
  private emitAudioEnd;
65
+ private installBrowserLifecycleListeners;
66
+ private requestWakeLock;
67
+ private releaseWakeLock;
56
68
  private resolveNextSessionBaseConfig;
57
69
  private buildEffectiveSessionConfig;
58
70
  private commitActiveSession;
package/dist/index.js CHANGED
@@ -1,5 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ var core = require('@charivo/core');
4
+
5
+ // src/realtime-manager.ts
6
+
3
7
  // src/instructions.ts
4
8
  var DEFAULT_PROVIDER = "openai";
5
9
  var DEFAULT_MODEL = "gpt-realtime-mini";
@@ -201,6 +205,7 @@ function clamp(value, min, max) {
201
205
 
202
206
  // src/realtime-manager.ts
203
207
  var DEFAULT_TOOL_TIMEOUT_MS = 1e4;
208
+ var RECONNECT_DELAYS_MS = [500, 1e3, 2e3, 4e3, 5e3];
204
209
  var RealtimeManagerImpl = class {
205
210
  constructor(client, options = {}) {
206
211
  this.client = client;
@@ -208,6 +213,7 @@ var RealtimeManagerImpl = class {
208
213
  for (const tool of options.tools ?? []) {
209
214
  this.registerTool(tool);
210
215
  }
216
+ this.installBrowserLifecycleListeners();
211
217
  this.client.onEvent((event) => {
212
218
  void this.handleClientEvent(event).catch((error) => {
213
219
  this.applyError(
@@ -221,11 +227,16 @@ var RealtimeManagerImpl = class {
221
227
  character = null;
222
228
  isStoppingSession = false;
223
229
  isRefreshingSession = false;
230
+ isRecoveringConnection = false;
224
231
  isAudioPlaybackActive = false;
225
232
  sessionBaseConfig;
226
233
  refreshInFlight = null;
234
+ reconnectInFlight = null;
227
235
  hasQueuedRefresh = false;
228
236
  stopRequestedDuringRefresh = false;
237
+ reconnectToken = 0;
238
+ wakeLockSentinel = null;
239
+ teardownBrowserLifecycle;
229
240
  toolRegistry = /* @__PURE__ */ new Map();
230
241
  state = {
231
242
  connection: "idle",
@@ -285,8 +296,8 @@ var RealtimeManagerImpl = class {
285
296
  };
286
297
  }
287
298
  async startSession(config) {
288
- if (this.state.session.status === "active" || this.state.session.status === "starting" || this.refreshInFlight) {
289
- throw new Error("Realtime session already active");
299
+ if (this.state.session.status === "active" || this.state.session.status === "starting" || this.refreshInFlight || this.reconnectInFlight) {
300
+ throw new core.CharivoStateError("Realtime session already active");
290
301
  }
291
302
  this.sessionBaseConfig = this.resolveNextSessionBaseConfig(config);
292
303
  const sessionConfig = this.buildEffectiveSessionConfig();
@@ -304,16 +315,17 @@ var RealtimeManagerImpl = class {
304
315
  },
305
316
  lastError: null
306
317
  };
318
+ this.installBrowserLifecycleListeners();
307
319
  this.emitState();
308
320
  try {
309
321
  await this.client.connect(sessionConfig);
310
322
  this.commitActiveSession(sessionConfig);
323
+ void this.requestWakeLock();
311
324
  this.emitSessionStart("user");
312
325
  } catch (error) {
313
- this.finalizeFailedSession(
314
- error instanceof Error ? error : new Error(String(error))
315
- );
316
- throw error;
326
+ const typedError = core.toCharivoError("transport", error);
327
+ this.finalizeFailedSession(typedError);
328
+ throw typedError;
317
329
  }
318
330
  }
319
331
  updateSession(config) {
@@ -321,6 +333,9 @@ var RealtimeManagerImpl = class {
321
333
  if (this.state.session.status !== "active") {
322
334
  return Promise.resolve();
323
335
  }
336
+ if (this.isRecoveringConnection) {
337
+ return Promise.resolve();
338
+ }
324
339
  if (this.refreshInFlight) {
325
340
  this.hasQueuedRefresh = true;
326
341
  return this.refreshInFlight;
@@ -335,6 +350,14 @@ var RealtimeManagerImpl = class {
335
350
  return this.refreshInFlight;
336
351
  }
337
352
  async stopSession() {
353
+ const reconnectInFlight = this.reconnectInFlight;
354
+ this.cancelReconnectLoop();
355
+ if (reconnectInFlight) {
356
+ try {
357
+ await reconnectInFlight;
358
+ } catch {
359
+ }
360
+ }
338
361
  if (this.refreshInFlight) {
339
362
  this.stopRequestedDuringRefresh = true;
340
363
  this.hasQueuedRefresh = false;
@@ -343,32 +366,45 @@ var RealtimeManagerImpl = class {
343
366
  } catch {
344
367
  }
345
368
  if (this.state.session.status === "active") {
346
- await this.performStopSession();
369
+ await this.performStopSession().catch((error) => {
370
+ throw core.toCharivoError("transport", error);
371
+ });
347
372
  }
348
373
  return;
349
374
  }
350
375
  if (this.state.session.status !== "active") {
351
376
  return;
352
377
  }
353
- await this.performStopSession();
378
+ await this.performStopSession().catch((error) => {
379
+ throw core.toCharivoError("transport", error);
380
+ });
354
381
  }
355
382
  async sendMessage(text) {
356
383
  if (this.state.session.status !== "active") {
357
- throw new Error("Realtime session not active");
384
+ throw new core.CharivoStateError("Realtime session not active");
385
+ }
386
+ if (this.state.connection !== "connected") {
387
+ throw new core.CharivoStateError("Realtime session is reconnecting");
358
388
  }
359
- await this.client.sendText(text);
389
+ await this.client.sendText(text).catch((error) => Promise.reject(core.toCharivoError("transport", error)));
360
390
  }
361
391
  async sendAudioChunk(audio) {
362
392
  if (this.state.session.status !== "active") {
363
- throw new Error("Realtime session not active");
393
+ throw new core.CharivoStateError("Realtime session not active");
394
+ }
395
+ if (this.state.connection !== "connected") {
396
+ throw new core.CharivoStateError("Realtime session is reconnecting");
364
397
  }
365
- await this.client.sendAudio(audio);
398
+ await this.client.sendAudio(audio).catch((error) => Promise.reject(core.toCharivoError("transport", error)));
366
399
  }
367
400
  async interrupt() {
368
401
  if (this.state.session.status !== "active") {
369
- throw new Error("Realtime session not active");
402
+ throw new core.CharivoStateError("Realtime session not active");
370
403
  }
371
- await this.client.interrupt();
404
+ if (this.state.connection !== "connected") {
405
+ throw new core.CharivoStateError("Realtime session is reconnecting");
406
+ }
407
+ await this.client.interrupt().catch((error) => Promise.reject(core.toCharivoError("transport", error)));
372
408
  this.state = {
373
409
  ...this.state,
374
410
  response: {
@@ -382,7 +418,7 @@ var RealtimeManagerImpl = class {
382
418
  async handleClientEvent(event) {
383
419
  switch (event.type) {
384
420
  case "session.started":
385
- if (this.state.connection === "connecting" || this.isRefreshingSession) {
421
+ if (this.state.connection === "connecting" || this.isRefreshingSession || this.isRecoveringConnection) {
386
422
  return;
387
423
  }
388
424
  this.state = mergeRealtimeState(this.state, {
@@ -396,11 +432,14 @@ var RealtimeManagerImpl = class {
396
432
  this.emitState();
397
433
  return;
398
434
  case "session.ended":
399
- if (this.isStoppingSession || this.isRefreshingSession || this.state.connection === "disconnecting") {
435
+ if (this.isStoppingSession || this.isRefreshingSession || this.isRecoveringConnection || this.state.connection === "disconnecting") {
400
436
  return;
401
437
  }
402
438
  this.finalizeStoppedSession(true, "user");
403
439
  return;
440
+ case "connection.lost":
441
+ this.beginReconnect(event.cause, event.error);
442
+ return;
404
443
  case "user.transcript":
405
444
  this.eventEmitter?.emit("realtime:user:transcript", {
406
445
  text: event.text
@@ -482,6 +521,14 @@ var RealtimeManagerImpl = class {
482
521
  this.emitState();
483
522
  return;
484
523
  case "error":
524
+ if (this.isRecoveringConnection && this.state.connection === "connecting") {
525
+ this.state = {
526
+ ...this.state,
527
+ lastError: event.error
528
+ };
529
+ this.emitState();
530
+ return;
531
+ }
485
532
  this.applyError(event.error, this.state.connection);
486
533
  return;
487
534
  }
@@ -605,19 +652,124 @@ var RealtimeManagerImpl = class {
605
652
  state: this.getState()
606
653
  });
607
654
  }
655
+ beginReconnect(cause, error) {
656
+ if (this.state.session.status !== "active" || this.isStoppingSession || this.state.connection === "disconnecting" || this.isRecoveringConnection) {
657
+ return;
658
+ }
659
+ this.isRecoveringConnection = true;
660
+ this.reconnectToken += 1;
661
+ const reconnectToken = this.reconnectToken;
662
+ if (this.state.response.status === "responding") {
663
+ this.emitAudioEnd();
664
+ this.state = {
665
+ ...this.state,
666
+ connection: "connecting",
667
+ response: {
668
+ status: "interrupted",
669
+ text: this.state.response.text
670
+ },
671
+ lastError: error ?? this.state.lastError
672
+ };
673
+ } else {
674
+ this.state = {
675
+ ...this.state,
676
+ connection: "connecting",
677
+ lastError: error ?? this.state.lastError
678
+ };
679
+ }
680
+ this.emitState();
681
+ this.reconnectInFlight = this.runReconnectLoop(reconnectToken, cause).catch((reconnectError) => {
682
+ this.finalizeFailedSession(reconnectError);
683
+ }).finally(() => {
684
+ if (this.reconnectToken === reconnectToken) {
685
+ this.reconnectInFlight = null;
686
+ this.isRecoveringConnection = false;
687
+ }
688
+ });
689
+ }
690
+ async runReconnectLoop(reconnectToken, cause) {
691
+ const startedAt = Date.now();
692
+ let lastError = new Error("Realtime reconnect exhausted");
693
+ for (let index = 0; index < RECONNECT_DELAYS_MS.length; index += 1) {
694
+ const attempt = index + 1;
695
+ const delayMs = RECONNECT_DELAYS_MS[index];
696
+ this.eventEmitter?.emit("realtime:reconnect:attempt", {
697
+ attempt,
698
+ delayMs,
699
+ cause
700
+ });
701
+ await delay(delayMs);
702
+ if (!this.isReconnectTokenActive(reconnectToken)) {
703
+ return;
704
+ }
705
+ const sessionConfig = this.buildEffectiveSessionConfig();
706
+ try {
707
+ await this.client.recover(sessionConfig);
708
+ if (!this.isReconnectTokenActive(reconnectToken)) {
709
+ return;
710
+ }
711
+ this.state = {
712
+ ...this.state,
713
+ connection: "connected",
714
+ session: {
715
+ ...this.state.session,
716
+ status: "active",
717
+ config: sessionConfig,
718
+ characterId: this.character?.id
719
+ },
720
+ lastError: null
721
+ };
722
+ this.emitState();
723
+ void this.requestWakeLock();
724
+ this.eventEmitter?.emit("realtime:reconnect:success", {
725
+ attempts: attempt,
726
+ totalMs: Date.now() - startedAt,
727
+ cause
728
+ });
729
+ return;
730
+ } catch (error) {
731
+ lastError = error instanceof Error ? error : new Error(String(error));
732
+ this.state = {
733
+ ...this.state,
734
+ connection: "connecting",
735
+ lastError
736
+ };
737
+ this.emitState();
738
+ }
739
+ }
740
+ if (!this.isReconnectTokenActive(reconnectToken)) {
741
+ return;
742
+ }
743
+ this.eventEmitter?.emit("realtime:reconnect:exhausted", {
744
+ attempts: RECONNECT_DELAYS_MS.length,
745
+ totalMs: Date.now() - startedAt,
746
+ cause,
747
+ lastError
748
+ });
749
+ throw lastError;
750
+ }
751
+ cancelReconnectLoop() {
752
+ this.reconnectToken += 1;
753
+ this.isRecoveringConnection = false;
754
+ this.reconnectInFlight = null;
755
+ }
756
+ isReconnectTokenActive(reconnectToken) {
757
+ return this.reconnectToken === reconnectToken && this.state.session.status === "active" && !this.isStoppingSession;
758
+ }
608
759
  emitState() {
609
760
  this.eventEmitter?.emit("realtime:state", {
610
761
  state: this.getState()
611
762
  });
612
763
  }
613
764
  applyError(error, connection) {
765
+ const typedError = core.toCharivoError("transport", error);
614
766
  this.emitAudioEnd();
615
767
  this.state = {
616
768
  ...this.state,
617
769
  connection,
618
- lastError: error
770
+ lastError: typedError
619
771
  };
620
- this.eventEmitter?.emit("realtime:error", { error });
772
+ this.eventEmitter?.emit("realtime:error", { error: typedError });
621
773
  this.emitState();
622
774
  }
623
775
  emitAudioStart() {
@@ -635,6 +787,56 @@ var RealtimeManagerImpl = class {
635
787
  this.eventEmitter?.emit("tts:lipsync:update", { rms: 0 });
636
788
  this.eventEmitter?.emit("tts:audio:end", {});
637
789
  }
790
+ installBrowserLifecycleListeners() {
791
+ if (this.teardownBrowserLifecycle) {
792
+ return;
793
+ }
794
+ this.teardownBrowserLifecycle = core.subscribeBrowserLifecycle({
795
+ onHidden: () => {
796
+ void this.releaseWakeLock();
797
+ },
798
+ onPageHide: () => {
799
+ void this.releaseWakeLock();
800
+ },
801
+ onPageShow: () => {
802
+ void this.requestWakeLock();
803
+ },
804
+ onVisible: () => {
805
+ void this.requestWakeLock();
806
+ }
807
+ });
808
+ }
809
+ async requestWakeLock() {
810
+ if (this.state.session.status !== "active" || this.state.connection === "disconnecting" || typeof navigator === "undefined" || typeof document === "undefined" || document.visibilityState !== "visible" || !("wakeLock" in navigator)) {
811
+ return;
812
+ }
813
+ if (this.wakeLockSentinel) {
814
+ return;
815
+ }
816
+ const wakeLockApi = navigator;
817
+ try {
818
+ this.wakeLockSentinel = await wakeLockApi.wakeLock?.request("screen");
819
+ this.wakeLockSentinel?.addEventListener?.(
820
+ "release",
821
+ () => {
822
+ this.wakeLockSentinel = null;
823
+ },
824
+ { once: true }
825
+ );
826
+ } catch {
827
+ }
828
+ }
829
+ async releaseWakeLock() {
830
+ if (!this.wakeLockSentinel) {
831
+ return;
832
+ }
833
+ const wakeLockSentinel = this.wakeLockSentinel;
834
+ this.wakeLockSentinel = null;
835
+ try {
836
+ await wakeLockSentinel.release();
837
+ } catch {
838
+ }
839
+ }
638
840
  resolveNextSessionBaseConfig(config) {
639
841
  return mergeSessionConfig(
640
842
  this.sessionBaseConfig ?? this.options.defaultSessionConfig,
@@ -722,10 +924,14 @@ var RealtimeManagerImpl = class {
722
924
  this.commitPatchedSession(sessionConfig);
723
925
  } catch (error) {
724
926
  const refreshError = error instanceof Error ? error : new Error(String(error));
725
- if (this.state.lastError !== refreshError) {
726
- this.applyError(refreshError, "connected");
927
+ const typedRefreshError = core.toCharivoError("transport", refreshError);
928
+ if (this.isRecoveringConnection) {
929
+ throw typedRefreshError;
930
+ }
931
+ if (this.state.lastError !== typedRefreshError && this.state.lastError?.cause !== refreshError) {
932
+ this.applyError(typedRefreshError, "connected");
727
933
  }
728
- throw refreshError;
934
+ throw typedRefreshError;
729
935
  } finally {
730
936
  this.isRefreshingSession = false;
731
937
  }
@@ -743,6 +949,10 @@ var RealtimeManagerImpl = class {
743
949
  });
744
950
  }
745
951
  finalizeFailedSession(error) {
952
+ this.cancelReconnectLoop();
953
+ this.teardownBrowserLifecycle?.();
954
+ this.teardownBrowserLifecycle = void 0;
955
+ void this.releaseWakeLock();
746
956
  this.emitAudioEnd();
747
957
  this.state = {
748
958
  ...this.state,
@@ -762,6 +972,10 @@ var RealtimeManagerImpl = class {
762
972
  this.emitState();
763
973
  }
764
974
  finalizeStoppedSession(emitSessionEnd, reason) {
975
+ this.cancelReconnectLoop();
976
+ this.teardownBrowserLifecycle?.();
977
+ this.teardownBrowserLifecycle = void 0;
978
+ void this.releaseWakeLock();
765
979
  this.emitAudioEnd();
766
980
  this.state = {
767
981
  ...this.state,
@@ -849,6 +1063,11 @@ async function withTimeout(promise, timeoutMs, toolName) {
849
1063
  }
850
1064
  }
851
1065
  }
1066
+ function delay(ms) {
1067
+ return new Promise((resolve) => {
1068
+ setTimeout(resolve, ms);
1069
+ });
1070
+ }
852
1071
 
853
1072
  exports.AVATAR_CONTROL_TOOL_NAMES = AVATAR_CONTROL_TOOL_NAMES;
854
1073
  exports.DEFAULT_REALTIME_AGENT_INSTRUCTIONS = DEFAULT_REALTIME_AGENT_INSTRUCTIONS;