@charivo/realtime 0.2.2 → 0.4.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/README.md +32 -7
- package/dist/{chunk-IIKECRXN.mjs → chunk-DHNRDHST.mjs} +350 -17
- package/dist/chunk-DHNRDHST.mjs.map +1 -0
- package/dist/chunk-PDODY2T7.mjs +65 -0
- package/dist/chunk-PDODY2T7.mjs.map +1 -0
- package/dist/{chunk-JGJZKKMH.mjs → chunk-PTFZ7CML.mjs} +235 -22
- package/dist/chunk-PTFZ7CML.mjs.map +1 -0
- package/dist/chunk-Y7JO2FFX.mjs +121 -0
- package/dist/chunk-Y7JO2FFX.mjs.map +1 -0
- package/dist/index.d.mts +16 -4
- package/dist/index.d.ts +16 -4
- package/dist/index.js +240 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +238 -98
- package/dist/index.mjs.map +1 -1
- package/dist/openai/index.d.mts +37 -1
- package/dist/openai/index.d.ts +37 -1
- package/dist/openai/index.js +427 -14
- package/dist/openai/index.js.map +1 -1
- package/dist/openai/index.mjs +3 -2
- package/dist/openai-agents/index.d.mts +26 -1
- package/dist/openai-agents/index.d.ts +26 -1
- package/dist/openai-agents/index.js +311 -18
- package/dist/openai-agents/index.js.map +1 -1
- package/dist/openai-agents/index.mjs +3 -2
- package/dist/remote/index.d.mts +3 -1
- package/dist/remote/index.d.ts +3 -1
- package/dist/remote/index.js +663 -32
- package/dist/remote/index.js.map +1 -1
- package/dist/remote/index.mjs +10 -3
- package/dist/remote/index.mjs.map +1 -1
- package/dist/{types-DntfJo0o.d.mts → types-bmYuc_Vs.d.mts} +7 -1
- package/dist/{types-DntfJo0o.d.ts → types-bmYuc_Vs.d.ts} +7 -1
- package/package.json +3 -3
- package/dist/chunk-IIKECRXN.mjs.map +0 -1
- package/dist/chunk-JGJZKKMH.mjs.map +0 -1
- package/dist/chunk-XJNT4ZTF.mjs +0 -41
- package/dist/chunk-XJNT4ZTF.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,64 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var DEFAULT_VOICE = "marin";
|
|
5
|
-
var SET_EXPRESSION_TOOL_DESCRIPTION = "Change the avatar's facial expression only when the emotional beat clearly shifts or should linger across the reply. Do not use this for every polite or lightweight reaction.";
|
|
6
|
-
var PLAY_MOTION_TOOL_DESCRIPTION = "Play a noticeable body motion for greetings, emphasis, or bigger reaction beats. Prefer this over stacking multiple smaller actions when the moment needs one clear accent. Usually use at most one motion in a reply.";
|
|
7
|
-
var LOOK_AT_TOOL_DESCRIPTION = `Shift the avatar's gaze for subtle attention changes or conversational focus. Trigger this on natural phrases like "glance", "look over", "peek at", or directional cues. Prefer this before "setExpression" when a lightweight reaction is enough.`;
|
|
8
|
-
var DEFAULT_REALTIME_AGENT_INSTRUCTIONS = `
|
|
9
|
-
You are speaking in a realtime voice conversation through a Live2D avatar.
|
|
10
|
-
Respond naturally and keep replies concise enough for spoken delivery.
|
|
11
|
-
Use avatar tools only when they add something meaningful to the moment.
|
|
12
|
-
Use "setExpression" when the emotional beat clearly shifts, especially when the expression should linger across the reply.
|
|
13
|
-
Do not use "setExpression" for every friendly, polite, or mildly positive reply.
|
|
14
|
-
Use "playMotion" for greetings, emphasis, or bigger reaction beats, usually at most once in a reply.
|
|
15
|
-
Use "lookAt" for subtle attention shifts or conversational focus, and prefer it before "setExpression" when a lightweight reaction is enough.
|
|
16
|
-
Prefer a single meaningful avatar action over chaining multiple actions on simple replies.
|
|
17
|
-
Many turns should use no avatar tool at all.
|
|
18
|
-
Never break character. Never refer to yourself as an AI, model, or assistant.
|
|
19
|
-
Do not mention tool calls in the spoken response.
|
|
20
|
-
Speak only the words you want heard aloud. Use avatar tools for actions. Never say tool names or tool arguments out loud. Do not output bracketed or parenthetical action notes. If a tool is unavailable, omit the action entirely rather than narrating it.
|
|
21
|
-
`.trim();
|
|
22
|
-
function buildRealtimeSessionConfig({
|
|
23
|
-
character,
|
|
24
|
-
baseConfig
|
|
25
|
-
} = {}) {
|
|
26
|
-
return {
|
|
27
|
-
provider: baseConfig?.provider ?? DEFAULT_PROVIDER,
|
|
28
|
-
transport: baseConfig?.transport ?? "webrtc",
|
|
29
|
-
model: baseConfig?.model ?? DEFAULT_MODEL,
|
|
30
|
-
voice: baseConfig?.voice ?? character?.voice?.voiceId ?? DEFAULT_VOICE,
|
|
31
|
-
instructions: baseConfig?.instructions ?? buildCharacterInstructions(character),
|
|
32
|
-
temperature: baseConfig?.temperature,
|
|
33
|
-
maxTokens: baseConfig?.maxTokens,
|
|
34
|
-
tools: baseConfig?.tools,
|
|
35
|
-
toolChoice: baseConfig?.toolChoice ?? "auto"
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
function buildCharacterInstructions(character) {
|
|
39
|
-
if (!character) {
|
|
40
|
-
return DEFAULT_REALTIME_AGENT_INSTRUCTIONS;
|
|
41
|
-
}
|
|
42
|
-
const identityLines = [
|
|
43
|
-
`You are ${character.name}.`,
|
|
44
|
-
"Stay fully in character during the conversation."
|
|
45
|
-
];
|
|
46
|
-
if (character.description) {
|
|
47
|
-
identityLines.push(ensureSentence(character.description));
|
|
48
|
-
}
|
|
49
|
-
if (character.personality) {
|
|
50
|
-
identityLines.push(
|
|
51
|
-
`Your personality is ${ensureSentenceFragment(character.personality)}.`
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
return [...identityLines, DEFAULT_REALTIME_AGENT_INSTRUCTIONS].join("\n");
|
|
55
|
-
}
|
|
56
|
-
function ensureSentence(text) {
|
|
57
|
-
return /[.!?]$/.test(text) ? text : `${text}.`;
|
|
58
|
-
}
|
|
59
|
-
function ensureSentenceFragment(text) {
|
|
60
|
-
return text.trim().replace(/[.!?]+$/, "");
|
|
61
|
-
}
|
|
1
|
+
import { SET_EXPRESSION_TOOL_DESCRIPTION, PLAY_MOTION_TOOL_DESCRIPTION, LOOK_AT_TOOL_DESCRIPTION, buildRealtimeSessionConfig } from './chunk-PDODY2T7.mjs';
|
|
2
|
+
export { DEFAULT_REALTIME_AGENT_INSTRUCTIONS, buildRealtimeSessionConfig } from './chunk-PDODY2T7.mjs';
|
|
3
|
+
import { subscribeBrowserLifecycle } from '@charivo/core';
|
|
62
4
|
|
|
63
5
|
// src/tools.ts
|
|
64
6
|
var MIN_GAZE = -1;
|
|
@@ -199,6 +141,7 @@ function clamp(value, min, max) {
|
|
|
199
141
|
|
|
200
142
|
// src/realtime-manager.ts
|
|
201
143
|
var DEFAULT_TOOL_TIMEOUT_MS = 1e4;
|
|
144
|
+
var RECONNECT_DELAYS_MS = [500, 1e3, 2e3, 4e3, 5e3];
|
|
202
145
|
var RealtimeManagerImpl = class {
|
|
203
146
|
constructor(client, options = {}) {
|
|
204
147
|
this.client = client;
|
|
@@ -206,6 +149,7 @@ var RealtimeManagerImpl = class {
|
|
|
206
149
|
for (const tool of options.tools ?? []) {
|
|
207
150
|
this.registerTool(tool);
|
|
208
151
|
}
|
|
152
|
+
this.installBrowserLifecycleListeners();
|
|
209
153
|
this.client.onEvent((event) => {
|
|
210
154
|
void this.handleClientEvent(event).catch((error) => {
|
|
211
155
|
this.applyError(
|
|
@@ -219,12 +163,16 @@ var RealtimeManagerImpl = class {
|
|
|
219
163
|
character = null;
|
|
220
164
|
isStoppingSession = false;
|
|
221
165
|
isRefreshingSession = false;
|
|
166
|
+
isRecoveringConnection = false;
|
|
222
167
|
isAudioPlaybackActive = false;
|
|
223
168
|
sessionBaseConfig;
|
|
224
169
|
refreshInFlight = null;
|
|
170
|
+
reconnectInFlight = null;
|
|
225
171
|
hasQueuedRefresh = false;
|
|
226
172
|
stopRequestedDuringRefresh = false;
|
|
227
|
-
|
|
173
|
+
reconnectToken = 0;
|
|
174
|
+
wakeLockSentinel = null;
|
|
175
|
+
teardownBrowserLifecycle;
|
|
228
176
|
toolRegistry = /* @__PURE__ */ new Map();
|
|
229
177
|
state = {
|
|
230
178
|
connection: "idle",
|
|
@@ -284,7 +232,7 @@ var RealtimeManagerImpl = class {
|
|
|
284
232
|
};
|
|
285
233
|
}
|
|
286
234
|
async startSession(config) {
|
|
287
|
-
if (this.state.session.status === "active" || this.state.session.status === "starting" || this.refreshInFlight) {
|
|
235
|
+
if (this.state.session.status === "active" || this.state.session.status === "starting" || this.refreshInFlight || this.reconnectInFlight) {
|
|
288
236
|
throw new Error("Realtime session already active");
|
|
289
237
|
}
|
|
290
238
|
this.sessionBaseConfig = this.resolveNextSessionBaseConfig(config);
|
|
@@ -303,10 +251,12 @@ var RealtimeManagerImpl = class {
|
|
|
303
251
|
},
|
|
304
252
|
lastError: null
|
|
305
253
|
};
|
|
254
|
+
this.installBrowserLifecycleListeners();
|
|
306
255
|
this.emitState();
|
|
307
256
|
try {
|
|
308
257
|
await this.client.connect(sessionConfig);
|
|
309
258
|
this.commitActiveSession(sessionConfig);
|
|
259
|
+
void this.requestWakeLock();
|
|
310
260
|
this.emitSessionStart("user");
|
|
311
261
|
} catch (error) {
|
|
312
262
|
this.finalizeFailedSession(
|
|
@@ -320,22 +270,31 @@ var RealtimeManagerImpl = class {
|
|
|
320
270
|
if (this.state.session.status !== "active") {
|
|
321
271
|
return Promise.resolve();
|
|
322
272
|
}
|
|
273
|
+
if (this.isRecoveringConnection) {
|
|
274
|
+
return Promise.resolve();
|
|
275
|
+
}
|
|
323
276
|
if (this.refreshInFlight) {
|
|
324
277
|
this.hasQueuedRefresh = true;
|
|
325
278
|
return this.refreshInFlight;
|
|
326
279
|
}
|
|
327
280
|
this.stopRequestedDuringRefresh = false;
|
|
328
281
|
this.hasQueuedRefresh = false;
|
|
329
|
-
this.emittedUserStopDuringRefresh = false;
|
|
330
282
|
this.refreshInFlight = this.runRefreshLoop().finally(() => {
|
|
331
283
|
this.refreshInFlight = null;
|
|
332
284
|
this.hasQueuedRefresh = false;
|
|
333
285
|
this.stopRequestedDuringRefresh = false;
|
|
334
|
-
this.emittedUserStopDuringRefresh = false;
|
|
335
286
|
});
|
|
336
287
|
return this.refreshInFlight;
|
|
337
288
|
}
|
|
338
289
|
async stopSession() {
|
|
290
|
+
const reconnectInFlight = this.reconnectInFlight;
|
|
291
|
+
this.cancelReconnectLoop();
|
|
292
|
+
if (reconnectInFlight) {
|
|
293
|
+
try {
|
|
294
|
+
await reconnectInFlight;
|
|
295
|
+
} catch {
|
|
296
|
+
}
|
|
297
|
+
}
|
|
339
298
|
if (this.refreshInFlight) {
|
|
340
299
|
this.stopRequestedDuringRefresh = true;
|
|
341
300
|
this.hasQueuedRefresh = false;
|
|
@@ -345,8 +304,6 @@ var RealtimeManagerImpl = class {
|
|
|
345
304
|
}
|
|
346
305
|
if (this.state.session.status === "active") {
|
|
347
306
|
await this.performStopSession();
|
|
348
|
-
} else if (this.stopRequestedDuringRefresh && !this.emittedUserStopDuringRefresh) {
|
|
349
|
-
this.emitSessionEnd("user");
|
|
350
307
|
}
|
|
351
308
|
return;
|
|
352
309
|
}
|
|
@@ -359,18 +316,27 @@ var RealtimeManagerImpl = class {
|
|
|
359
316
|
if (this.state.session.status !== "active") {
|
|
360
317
|
throw new Error("Realtime session not active");
|
|
361
318
|
}
|
|
319
|
+
if (this.state.connection !== "connected") {
|
|
320
|
+
throw new Error("Realtime session is reconnecting");
|
|
321
|
+
}
|
|
362
322
|
await this.client.sendText(text);
|
|
363
323
|
}
|
|
364
324
|
async sendAudioChunk(audio) {
|
|
365
325
|
if (this.state.session.status !== "active") {
|
|
366
326
|
throw new Error("Realtime session not active");
|
|
367
327
|
}
|
|
328
|
+
if (this.state.connection !== "connected") {
|
|
329
|
+
throw new Error("Realtime session is reconnecting");
|
|
330
|
+
}
|
|
368
331
|
await this.client.sendAudio(audio);
|
|
369
332
|
}
|
|
370
333
|
async interrupt() {
|
|
371
334
|
if (this.state.session.status !== "active") {
|
|
372
335
|
throw new Error("Realtime session not active");
|
|
373
336
|
}
|
|
337
|
+
if (this.state.connection !== "connected") {
|
|
338
|
+
throw new Error("Realtime session is reconnecting");
|
|
339
|
+
}
|
|
374
340
|
await this.client.interrupt();
|
|
375
341
|
this.state = {
|
|
376
342
|
...this.state,
|
|
@@ -385,7 +351,7 @@ var RealtimeManagerImpl = class {
|
|
|
385
351
|
async handleClientEvent(event) {
|
|
386
352
|
switch (event.type) {
|
|
387
353
|
case "session.started":
|
|
388
|
-
if (this.state.connection === "connecting" || this.isRefreshingSession) {
|
|
354
|
+
if (this.state.connection === "connecting" || this.isRefreshingSession || this.isRecoveringConnection) {
|
|
389
355
|
return;
|
|
390
356
|
}
|
|
391
357
|
this.state = mergeRealtimeState(this.state, {
|
|
@@ -399,11 +365,14 @@ var RealtimeManagerImpl = class {
|
|
|
399
365
|
this.emitState();
|
|
400
366
|
return;
|
|
401
367
|
case "session.ended":
|
|
402
|
-
if (this.isStoppingSession || this.isRefreshingSession || this.state.connection === "disconnecting") {
|
|
368
|
+
if (this.isStoppingSession || this.isRefreshingSession || this.isRecoveringConnection || this.state.connection === "disconnecting") {
|
|
403
369
|
return;
|
|
404
370
|
}
|
|
405
371
|
this.finalizeStoppedSession(true, "user");
|
|
406
372
|
return;
|
|
373
|
+
case "connection.lost":
|
|
374
|
+
this.beginReconnect(event.cause, event.error);
|
|
375
|
+
return;
|
|
407
376
|
case "user.transcript":
|
|
408
377
|
this.eventEmitter?.emit("realtime:user:transcript", {
|
|
409
378
|
text: event.text
|
|
@@ -485,6 +454,14 @@ var RealtimeManagerImpl = class {
|
|
|
485
454
|
this.emitState();
|
|
486
455
|
return;
|
|
487
456
|
case "error":
|
|
457
|
+
if (this.isRecoveringConnection && this.state.connection === "connecting") {
|
|
458
|
+
this.state = {
|
|
459
|
+
...this.state,
|
|
460
|
+
lastError: event.error
|
|
461
|
+
};
|
|
462
|
+
this.emitState();
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
488
465
|
this.applyError(event.error, this.state.connection);
|
|
489
466
|
return;
|
|
490
467
|
}
|
|
@@ -608,6 +585,110 @@ var RealtimeManagerImpl = class {
|
|
|
608
585
|
state: this.getState()
|
|
609
586
|
});
|
|
610
587
|
}
|
|
588
|
+
beginReconnect(cause, error) {
|
|
589
|
+
if (this.state.session.status !== "active" || this.isStoppingSession || this.state.connection === "disconnecting" || this.isRecoveringConnection) {
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
this.isRecoveringConnection = true;
|
|
593
|
+
this.reconnectToken += 1;
|
|
594
|
+
const reconnectToken = this.reconnectToken;
|
|
595
|
+
if (this.state.response.status === "responding") {
|
|
596
|
+
this.emitAudioEnd();
|
|
597
|
+
this.state = {
|
|
598
|
+
...this.state,
|
|
599
|
+
connection: "connecting",
|
|
600
|
+
response: {
|
|
601
|
+
status: "interrupted",
|
|
602
|
+
text: this.state.response.text
|
|
603
|
+
},
|
|
604
|
+
lastError: error ?? this.state.lastError
|
|
605
|
+
};
|
|
606
|
+
} else {
|
|
607
|
+
this.state = {
|
|
608
|
+
...this.state,
|
|
609
|
+
connection: "connecting",
|
|
610
|
+
lastError: error ?? this.state.lastError
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
this.emitState();
|
|
614
|
+
this.reconnectInFlight = this.runReconnectLoop(reconnectToken, cause).catch((reconnectError) => {
|
|
615
|
+
this.finalizeFailedSession(reconnectError);
|
|
616
|
+
}).finally(() => {
|
|
617
|
+
if (this.reconnectToken === reconnectToken) {
|
|
618
|
+
this.reconnectInFlight = null;
|
|
619
|
+
this.isRecoveringConnection = false;
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
async runReconnectLoop(reconnectToken, cause) {
|
|
624
|
+
const startedAt = Date.now();
|
|
625
|
+
let lastError = new Error("Realtime reconnect exhausted");
|
|
626
|
+
for (let index = 0; index < RECONNECT_DELAYS_MS.length; index += 1) {
|
|
627
|
+
const attempt = index + 1;
|
|
628
|
+
const delayMs = RECONNECT_DELAYS_MS[index];
|
|
629
|
+
this.eventEmitter?.emit("realtime:reconnect:attempt", {
|
|
630
|
+
attempt,
|
|
631
|
+
delayMs,
|
|
632
|
+
cause
|
|
633
|
+
});
|
|
634
|
+
await delay(delayMs);
|
|
635
|
+
if (!this.isReconnectTokenActive(reconnectToken)) {
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
const sessionConfig = this.buildEffectiveSessionConfig();
|
|
639
|
+
try {
|
|
640
|
+
await this.client.recover(sessionConfig);
|
|
641
|
+
if (!this.isReconnectTokenActive(reconnectToken)) {
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
this.state = {
|
|
645
|
+
...this.state,
|
|
646
|
+
connection: "connected",
|
|
647
|
+
session: {
|
|
648
|
+
...this.state.session,
|
|
649
|
+
status: "active",
|
|
650
|
+
config: sessionConfig,
|
|
651
|
+
characterId: this.character?.id
|
|
652
|
+
},
|
|
653
|
+
lastError: null
|
|
654
|
+
};
|
|
655
|
+
this.emitState();
|
|
656
|
+
void this.requestWakeLock();
|
|
657
|
+
this.eventEmitter?.emit("realtime:reconnect:success", {
|
|
658
|
+
attempts: attempt,
|
|
659
|
+
totalMs: Date.now() - startedAt,
|
|
660
|
+
cause
|
|
661
|
+
});
|
|
662
|
+
return;
|
|
663
|
+
} catch (error) {
|
|
664
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
665
|
+
this.state = {
|
|
666
|
+
...this.state,
|
|
667
|
+
connection: "connecting",
|
|
668
|
+
lastError
|
|
669
|
+
};
|
|
670
|
+
this.emitState();
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
if (!this.isReconnectTokenActive(reconnectToken)) {
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
this.eventEmitter?.emit("realtime:reconnect:exhausted", {
|
|
677
|
+
attempts: RECONNECT_DELAYS_MS.length,
|
|
678
|
+
totalMs: Date.now() - startedAt,
|
|
679
|
+
cause,
|
|
680
|
+
lastError
|
|
681
|
+
});
|
|
682
|
+
throw lastError;
|
|
683
|
+
}
|
|
684
|
+
cancelReconnectLoop() {
|
|
685
|
+
this.reconnectToken += 1;
|
|
686
|
+
this.isRecoveringConnection = false;
|
|
687
|
+
this.reconnectInFlight = null;
|
|
688
|
+
}
|
|
689
|
+
isReconnectTokenActive(reconnectToken) {
|
|
690
|
+
return this.reconnectToken === reconnectToken && this.state.session.status === "active" && !this.isStoppingSession;
|
|
691
|
+
}
|
|
611
692
|
emitState() {
|
|
612
693
|
this.eventEmitter?.emit("realtime:state", {
|
|
613
694
|
state: this.getState()
|
|
@@ -638,6 +719,56 @@ var RealtimeManagerImpl = class {
|
|
|
638
719
|
this.eventEmitter?.emit("tts:lipsync:update", { rms: 0 });
|
|
639
720
|
this.eventEmitter?.emit("tts:audio:end", {});
|
|
640
721
|
}
|
|
722
|
+
installBrowserLifecycleListeners() {
|
|
723
|
+
if (this.teardownBrowserLifecycle) {
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
this.teardownBrowserLifecycle = subscribeBrowserLifecycle({
|
|
727
|
+
onHidden: () => {
|
|
728
|
+
void this.releaseWakeLock();
|
|
729
|
+
},
|
|
730
|
+
onPageHide: () => {
|
|
731
|
+
void this.releaseWakeLock();
|
|
732
|
+
},
|
|
733
|
+
onPageShow: () => {
|
|
734
|
+
void this.requestWakeLock();
|
|
735
|
+
},
|
|
736
|
+
onVisible: () => {
|
|
737
|
+
void this.requestWakeLock();
|
|
738
|
+
}
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
async requestWakeLock() {
|
|
742
|
+
if (this.state.session.status !== "active" || this.state.connection === "disconnecting" || typeof navigator === "undefined" || typeof document === "undefined" || document.visibilityState !== "visible" || !("wakeLock" in navigator)) {
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
if (this.wakeLockSentinel) {
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
const wakeLockApi = navigator;
|
|
749
|
+
try {
|
|
750
|
+
this.wakeLockSentinel = await wakeLockApi.wakeLock?.request("screen");
|
|
751
|
+
this.wakeLockSentinel?.addEventListener?.(
|
|
752
|
+
"release",
|
|
753
|
+
() => {
|
|
754
|
+
this.wakeLockSentinel = null;
|
|
755
|
+
},
|
|
756
|
+
{ once: true }
|
|
757
|
+
);
|
|
758
|
+
} catch {
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
async releaseWakeLock() {
|
|
762
|
+
if (!this.wakeLockSentinel) {
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
const wakeLockSentinel = this.wakeLockSentinel;
|
|
766
|
+
this.wakeLockSentinel = null;
|
|
767
|
+
try {
|
|
768
|
+
await wakeLockSentinel.release();
|
|
769
|
+
} catch {
|
|
770
|
+
}
|
|
771
|
+
}
|
|
641
772
|
resolveNextSessionBaseConfig(config) {
|
|
642
773
|
return mergeSessionConfig(
|
|
643
774
|
this.sessionBaseConfig ?? this.options.defaultSessionConfig,
|
|
@@ -670,6 +801,20 @@ var RealtimeManagerImpl = class {
|
|
|
670
801
|
};
|
|
671
802
|
this.emitState();
|
|
672
803
|
}
|
|
804
|
+
commitPatchedSession(config) {
|
|
805
|
+
this.state = {
|
|
806
|
+
...this.state,
|
|
807
|
+
connection: "connected",
|
|
808
|
+
session: {
|
|
809
|
+
...this.state.session,
|
|
810
|
+
status: "active",
|
|
811
|
+
config,
|
|
812
|
+
characterId: this.character?.id
|
|
813
|
+
},
|
|
814
|
+
lastError: null
|
|
815
|
+
};
|
|
816
|
+
this.emitState();
|
|
817
|
+
}
|
|
673
818
|
async performStopSession() {
|
|
674
819
|
this.isStoppingSession = true;
|
|
675
820
|
this.state = {
|
|
@@ -696,46 +841,28 @@ var RealtimeManagerImpl = class {
|
|
|
696
841
|
} while (this.hasQueuedRefresh && !this.stopRequestedDuringRefresh);
|
|
697
842
|
}
|
|
698
843
|
async performSessionRefresh() {
|
|
699
|
-
const previousState = this.getState();
|
|
700
844
|
const sessionConfig = this.buildEffectiveSessionConfig();
|
|
701
845
|
this.isRefreshingSession = true;
|
|
702
846
|
this.state = {
|
|
703
847
|
...this.state,
|
|
704
|
-
connection: "disconnecting",
|
|
705
848
|
lastError: null
|
|
706
849
|
};
|
|
707
850
|
this.emitState();
|
|
708
851
|
try {
|
|
709
|
-
await this.client.
|
|
710
|
-
if (this.stopRequestedDuringRefresh) {
|
|
711
|
-
this.emittedUserStopDuringRefresh = true;
|
|
712
|
-
this.finalizeStoppedSession(true, "user");
|
|
713
|
-
return;
|
|
714
|
-
}
|
|
715
|
-
this.state = {
|
|
716
|
-
...this.state,
|
|
717
|
-
connection: "connecting",
|
|
718
|
-
response: {
|
|
719
|
-
status: "idle",
|
|
720
|
-
text: ""
|
|
721
|
-
}
|
|
722
|
-
};
|
|
723
|
-
this.emitState();
|
|
724
|
-
await this.client.connect(sessionConfig);
|
|
852
|
+
await this.client.updateSession(sessionConfig);
|
|
725
853
|
if (this.stopRequestedDuringRefresh) {
|
|
726
|
-
await this.client.disconnect();
|
|
727
|
-
this.emittedUserStopDuringRefresh = true;
|
|
728
|
-
this.finalizeStoppedSession(true, "user");
|
|
729
854
|
return;
|
|
730
855
|
}
|
|
731
|
-
this.
|
|
732
|
-
this.emitSessionEnd("refresh", previousState);
|
|
733
|
-
this.emitSessionStart("refresh");
|
|
856
|
+
this.commitPatchedSession(sessionConfig);
|
|
734
857
|
} catch (error) {
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
858
|
+
const refreshError = error instanceof Error ? error : new Error(String(error));
|
|
859
|
+
if (this.isRecoveringConnection) {
|
|
860
|
+
throw refreshError;
|
|
861
|
+
}
|
|
862
|
+
if (this.state.lastError !== refreshError) {
|
|
863
|
+
this.applyError(refreshError, "connected");
|
|
864
|
+
}
|
|
865
|
+
throw refreshError;
|
|
739
866
|
} finally {
|
|
740
867
|
this.isRefreshingSession = false;
|
|
741
868
|
}
|
|
@@ -753,6 +880,10 @@ var RealtimeManagerImpl = class {
|
|
|
753
880
|
});
|
|
754
881
|
}
|
|
755
882
|
finalizeFailedSession(error) {
|
|
883
|
+
this.cancelReconnectLoop();
|
|
884
|
+
this.teardownBrowserLifecycle?.();
|
|
885
|
+
this.teardownBrowserLifecycle = void 0;
|
|
886
|
+
void this.releaseWakeLock();
|
|
756
887
|
this.emitAudioEnd();
|
|
757
888
|
this.state = {
|
|
758
889
|
...this.state,
|
|
@@ -772,6 +903,10 @@ var RealtimeManagerImpl = class {
|
|
|
772
903
|
this.emitState();
|
|
773
904
|
}
|
|
774
905
|
finalizeStoppedSession(emitSessionEnd, reason) {
|
|
906
|
+
this.cancelReconnectLoop();
|
|
907
|
+
this.teardownBrowserLifecycle?.();
|
|
908
|
+
this.teardownBrowserLifecycle = void 0;
|
|
909
|
+
void this.releaseWakeLock();
|
|
775
910
|
this.emitAudioEnd();
|
|
776
911
|
this.state = {
|
|
777
912
|
...this.state,
|
|
@@ -859,7 +994,12 @@ async function withTimeout(promise, timeoutMs, toolName) {
|
|
|
859
994
|
}
|
|
860
995
|
}
|
|
861
996
|
}
|
|
997
|
+
function delay(ms) {
|
|
998
|
+
return new Promise((resolve) => {
|
|
999
|
+
setTimeout(resolve, ms);
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
862
1002
|
|
|
863
|
-
export { AVATAR_CONTROL_TOOL_NAMES,
|
|
1003
|
+
export { AVATAR_CONTROL_TOOL_NAMES, LOOK_AT_TOOL_NAME, PLAY_MOTION_TOOL_NAME, RealtimeManagerImpl, SET_EXPRESSION_TOOL_NAME, createAvatarControlTools, createRealtimeManager };
|
|
864
1004
|
//# sourceMappingURL=index.mjs.map
|
|
865
1005
|
//# sourceMappingURL=index.mjs.map
|