@charivo/realtime 0.2.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 ADDED
@@ -0,0 +1,874 @@
1
+ 'use strict';
2
+
3
+ // src/instructions.ts
4
+ var DEFAULT_PROVIDER = "openai";
5
+ var DEFAULT_MODEL = "gpt-realtime-mini";
6
+ var DEFAULT_VOICE = "marin";
7
+ var SET_EXPRESSION_TOOL_DESCRIPTION = "Change the avatar's facial expression when the emotional beat clearly shifts. Usually keep the same expression for a few turns.";
8
+ var PLAY_MOTION_TOOL_DESCRIPTION = "Play a noticeable body motion for greetings, emphasis, or bigger reaction beats. Usually use at most one motion in a reply.";
9
+ var LOOK_AT_TOOL_DESCRIPTION = "Shift the avatar's gaze for subtle attention changes or conversational focus. Prefer this when a lightweight reaction is enough.";
10
+ var DEFAULT_REALTIME_AGENT_INSTRUCTIONS = `
11
+ You are speaking in a realtime voice conversation through a Live2D avatar.
12
+ Respond naturally and keep replies concise enough for spoken delivery.
13
+ Use avatar tools only when they add something meaningful to the moment.
14
+ Use "setExpression" only when the emotional beat clearly shifts, usually not more than once in a few turns.
15
+ Use "playMotion" for greetings, emphasis, or bigger reaction beats, usually at most once in a reply.
16
+ Use "lookAt" for subtle attention shifts or conversational focus, and prefer it when a lightweight reaction is enough.
17
+ Prefer a single meaningful avatar action over chaining multiple actions on simple replies.
18
+ Many turns should use no avatar tool at all.
19
+ Never break character. Never refer to yourself as an AI, model, or assistant.
20
+ Do not mention tool calls in the spoken response.
21
+ Do not narrate your facial expressions, motions, or gaze. Let the avatar show them.
22
+ `.trim();
23
+ function buildRealtimeSessionConfig({
24
+ character,
25
+ baseConfig
26
+ } = {}) {
27
+ return {
28
+ provider: baseConfig?.provider ?? DEFAULT_PROVIDER,
29
+ transport: baseConfig?.transport ?? "webrtc",
30
+ model: baseConfig?.model ?? DEFAULT_MODEL,
31
+ voice: baseConfig?.voice ?? character?.voice?.voiceId ?? DEFAULT_VOICE,
32
+ instructions: baseConfig?.instructions ?? buildCharacterInstructions(character),
33
+ temperature: baseConfig?.temperature,
34
+ maxTokens: baseConfig?.maxTokens,
35
+ tools: baseConfig?.tools,
36
+ toolChoice: baseConfig?.toolChoice ?? "auto"
37
+ };
38
+ }
39
+ function buildCharacterInstructions(character) {
40
+ if (!character) {
41
+ return DEFAULT_REALTIME_AGENT_INSTRUCTIONS;
42
+ }
43
+ const identityLines = [
44
+ `You are ${character.name}.`,
45
+ "Stay fully in character during the conversation."
46
+ ];
47
+ if (character.description) {
48
+ identityLines.push(ensureSentence(character.description));
49
+ }
50
+ if (character.personality) {
51
+ identityLines.push(
52
+ `Your personality is ${ensureSentenceFragment(character.personality)}.`
53
+ );
54
+ }
55
+ return [...identityLines, DEFAULT_REALTIME_AGENT_INSTRUCTIONS].join("\n");
56
+ }
57
+ function ensureSentence(text) {
58
+ return /[.!?]$/.test(text) ? text : `${text}.`;
59
+ }
60
+ function ensureSentenceFragment(text) {
61
+ return text.trim().replace(/[.!?]+$/, "");
62
+ }
63
+
64
+ // src/tools.ts
65
+ var MIN_GAZE = -1;
66
+ var MAX_GAZE = 1;
67
+ var SET_EXPRESSION_TOOL_NAME = "setExpression";
68
+ var PLAY_MOTION_TOOL_NAME = "playMotion";
69
+ var LOOK_AT_TOOL_NAME = "lookAt";
70
+ var AVATAR_CONTROL_TOOL_NAMES = [
71
+ SET_EXPRESSION_TOOL_NAME,
72
+ PLAY_MOTION_TOOL_NAME,
73
+ LOOK_AT_TOOL_NAME
74
+ ];
75
+ function createAvatarControlTools(catalog) {
76
+ const tools = [];
77
+ if (catalog.expressions.length > 0) {
78
+ const expressionValues = [...catalog.expressions];
79
+ tools.push({
80
+ definition: {
81
+ type: "function",
82
+ name: SET_EXPRESSION_TOOL_NAME,
83
+ description: SET_EXPRESSION_TOOL_DESCRIPTION,
84
+ parameters: {
85
+ type: "object",
86
+ properties: {
87
+ expressionId: {
88
+ type: "string",
89
+ description: "Expression ID from the loaded Live2D model.",
90
+ enum: expressionValues
91
+ }
92
+ },
93
+ required: ["expressionId"]
94
+ }
95
+ },
96
+ async handler(args) {
97
+ const expressionId = args.expressionId;
98
+ if (typeof expressionId !== "string" || !expressionValues.includes(expressionId)) {
99
+ throw new Error(
100
+ 'setExpression requires a valid "expressionId" from the model catalog'
101
+ );
102
+ }
103
+ return {
104
+ success: true,
105
+ expressionId
106
+ };
107
+ }
108
+ });
109
+ }
110
+ const motionGroups = Object.keys(catalog.motions);
111
+ if (motionGroups.length > 0) {
112
+ tools.push({
113
+ definition: {
114
+ type: "function",
115
+ name: PLAY_MOTION_TOOL_NAME,
116
+ description: PLAY_MOTION_TOOL_DESCRIPTION,
117
+ parameters: {
118
+ type: "object",
119
+ properties: {
120
+ group: {
121
+ type: "string",
122
+ description: "Motion group from the loaded Live2D model.",
123
+ enum: motionGroups
124
+ },
125
+ index: {
126
+ type: "number",
127
+ description: "Zero-based motion index within the selected motion group."
128
+ }
129
+ },
130
+ required: ["group", "index"]
131
+ }
132
+ },
133
+ async handler(args) {
134
+ const group = args.group;
135
+ const index = args.index;
136
+ if (typeof group !== "string" || !motionGroups.includes(group)) {
137
+ throw new Error(
138
+ 'playMotion requires a valid "group" from the model catalog'
139
+ );
140
+ }
141
+ if (!Number.isInteger(index)) {
142
+ throw new Error('playMotion requires an integer "index"');
143
+ }
144
+ const count = catalog.motions[group] ?? 0;
145
+ const motionIndex = index;
146
+ if (motionIndex < 0 || motionIndex >= count) {
147
+ throw new Error(
148
+ `playMotion index ${String(motionIndex)} is out of range for group "${group}"`
149
+ );
150
+ }
151
+ return {
152
+ success: true,
153
+ group,
154
+ index: motionIndex
155
+ };
156
+ }
157
+ });
158
+ }
159
+ tools.push({
160
+ definition: {
161
+ type: "function",
162
+ name: LOOK_AT_TOOL_NAME,
163
+ description: LOOK_AT_TOOL_DESCRIPTION,
164
+ parameters: {
165
+ type: "object",
166
+ properties: {
167
+ x: {
168
+ type: "number",
169
+ description: "Normalized horizontal gaze target from -1 to 1."
170
+ },
171
+ y: {
172
+ type: "number",
173
+ description: "Normalized vertical gaze target from -1 to 1."
174
+ }
175
+ },
176
+ required: ["x", "y"]
177
+ }
178
+ },
179
+ async handler(args) {
180
+ const x = args.x;
181
+ const y = args.y;
182
+ if (typeof x !== "number" || Number.isNaN(x)) {
183
+ throw new Error('lookAt requires a numeric "x"');
184
+ }
185
+ if (typeof y !== "number" || Number.isNaN(y)) {
186
+ throw new Error('lookAt requires a numeric "y"');
187
+ }
188
+ return {
189
+ success: true,
190
+ x: clamp(x, MIN_GAZE, MAX_GAZE),
191
+ y: clamp(y, MIN_GAZE, MAX_GAZE)
192
+ };
193
+ }
194
+ });
195
+ return tools;
196
+ }
197
+ function clamp(value, min, max) {
198
+ return Math.max(min, Math.min(max, value));
199
+ }
200
+
201
+ // src/realtime-manager.ts
202
+ var DEFAULT_TOOL_TIMEOUT_MS = 1e4;
203
+ var RealtimeManagerImpl = class {
204
+ constructor(client, options = {}) {
205
+ this.client = client;
206
+ this.options = options;
207
+ for (const tool of options.tools ?? []) {
208
+ this.registerTool(tool);
209
+ }
210
+ this.client.onEvent((event) => {
211
+ void this.handleClientEvent(event).catch((error) => {
212
+ this.applyError(
213
+ error instanceof Error ? error : new Error(String(error)),
214
+ this.state.connection === "idle" ? "error" : this.state.connection
215
+ );
216
+ });
217
+ });
218
+ }
219
+ eventEmitter;
220
+ character = null;
221
+ isStoppingSession = false;
222
+ isRefreshingSession = false;
223
+ isAudioPlaybackActive = false;
224
+ sessionBaseConfig;
225
+ refreshInFlight = null;
226
+ hasQueuedRefresh = false;
227
+ stopRequestedDuringRefresh = false;
228
+ emittedUserStopDuringRefresh = false;
229
+ toolRegistry = /* @__PURE__ */ new Map();
230
+ state = {
231
+ connection: "idle",
232
+ session: {
233
+ status: "idle",
234
+ config: null
235
+ },
236
+ response: {
237
+ status: "idle",
238
+ text: ""
239
+ },
240
+ lastError: null
241
+ };
242
+ setEventEmitter(eventEmitter) {
243
+ this.eventEmitter = eventEmitter;
244
+ }
245
+ setCharacter(character) {
246
+ this.character = character;
247
+ this.state = {
248
+ ...this.state,
249
+ session: {
250
+ ...this.state.session,
251
+ characterId: character.id
252
+ }
253
+ };
254
+ this.emitState();
255
+ }
256
+ registerTool(tool) {
257
+ this.toolRegistry.set(tool.definition.name, tool);
258
+ }
259
+ unregisterTool(name) {
260
+ this.toolRegistry.delete(name);
261
+ }
262
+ getRegisteredTools() {
263
+ return Array.from(this.toolRegistry.values(), (tool) => ({
264
+ ...tool.definition,
265
+ parameters: {
266
+ ...tool.definition.parameters,
267
+ properties: { ...tool.definition.parameters.properties },
268
+ required: tool.definition.parameters.required ? [...tool.definition.parameters.required] : void 0
269
+ }
270
+ }));
271
+ }
272
+ getState() {
273
+ return {
274
+ ...this.state,
275
+ session: {
276
+ ...this.state.session,
277
+ config: this.state.session.config ? {
278
+ ...this.state.session.config,
279
+ tools: this.state.session.config.tools ? [...this.state.session.config.tools] : void 0
280
+ } : null
281
+ },
282
+ response: {
283
+ ...this.state.response
284
+ }
285
+ };
286
+ }
287
+ 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");
290
+ }
291
+ this.sessionBaseConfig = this.resolveNextSessionBaseConfig(config);
292
+ const sessionConfig = this.buildEffectiveSessionConfig();
293
+ this.state = {
294
+ ...this.state,
295
+ connection: "connecting",
296
+ session: {
297
+ status: "starting",
298
+ config: null,
299
+ characterId: this.character?.id
300
+ },
301
+ response: {
302
+ status: "idle",
303
+ text: ""
304
+ },
305
+ lastError: null
306
+ };
307
+ this.emitState();
308
+ try {
309
+ await this.client.connect(sessionConfig);
310
+ this.commitActiveSession(sessionConfig);
311
+ this.emitSessionStart("user");
312
+ } catch (error) {
313
+ this.finalizeFailedSession(
314
+ error instanceof Error ? error : new Error(String(error))
315
+ );
316
+ throw error;
317
+ }
318
+ }
319
+ updateSession(config) {
320
+ this.sessionBaseConfig = this.resolveNextSessionBaseConfig(config);
321
+ if (this.state.session.status !== "active") {
322
+ return Promise.resolve();
323
+ }
324
+ if (this.refreshInFlight) {
325
+ this.hasQueuedRefresh = true;
326
+ return this.refreshInFlight;
327
+ }
328
+ this.stopRequestedDuringRefresh = false;
329
+ this.hasQueuedRefresh = false;
330
+ this.emittedUserStopDuringRefresh = false;
331
+ this.refreshInFlight = this.runRefreshLoop().finally(() => {
332
+ this.refreshInFlight = null;
333
+ this.hasQueuedRefresh = false;
334
+ this.stopRequestedDuringRefresh = false;
335
+ this.emittedUserStopDuringRefresh = false;
336
+ });
337
+ return this.refreshInFlight;
338
+ }
339
+ async stopSession() {
340
+ if (this.refreshInFlight) {
341
+ this.stopRequestedDuringRefresh = true;
342
+ this.hasQueuedRefresh = false;
343
+ try {
344
+ await this.refreshInFlight;
345
+ } catch {
346
+ }
347
+ if (this.state.session.status === "active") {
348
+ await this.performStopSession();
349
+ } else if (this.stopRequestedDuringRefresh && !this.emittedUserStopDuringRefresh) {
350
+ this.emitSessionEnd("user");
351
+ }
352
+ return;
353
+ }
354
+ if (this.state.session.status !== "active") {
355
+ return;
356
+ }
357
+ await this.performStopSession();
358
+ }
359
+ async sendMessage(text) {
360
+ if (this.state.session.status !== "active") {
361
+ throw new Error("Realtime session not active");
362
+ }
363
+ await this.client.sendText(text);
364
+ }
365
+ async sendAudioChunk(audio) {
366
+ if (this.state.session.status !== "active") {
367
+ throw new Error("Realtime session not active");
368
+ }
369
+ await this.client.sendAudio(audio);
370
+ }
371
+ async interrupt() {
372
+ if (this.state.session.status !== "active") {
373
+ throw new Error("Realtime session not active");
374
+ }
375
+ await this.client.interrupt();
376
+ this.state = {
377
+ ...this.state,
378
+ response: {
379
+ ...this.state.response,
380
+ status: "interrupted"
381
+ }
382
+ };
383
+ this.emitAudioEnd();
384
+ this.emitState();
385
+ }
386
+ async handleClientEvent(event) {
387
+ switch (event.type) {
388
+ case "session.started":
389
+ if (this.state.connection === "connecting" || this.isRefreshingSession) {
390
+ return;
391
+ }
392
+ this.state = mergeRealtimeState(this.state, {
393
+ connection: "connected",
394
+ session: {
395
+ status: this.state.session.status === "starting" ? "active" : this.state.session.status,
396
+ config: this.state.session.config,
397
+ characterId: this.character?.id
398
+ }
399
+ });
400
+ this.emitState();
401
+ return;
402
+ case "session.ended":
403
+ if (this.isStoppingSession || this.isRefreshingSession || this.state.connection === "disconnecting") {
404
+ return;
405
+ }
406
+ this.finalizeStoppedSession(true, "user");
407
+ return;
408
+ case "user.transcript":
409
+ this.eventEmitter?.emit("realtime:user:transcript", {
410
+ text: event.text
411
+ });
412
+ return;
413
+ case "assistant.response.started":
414
+ this.state = {
415
+ ...this.state,
416
+ response: {
417
+ status: "responding",
418
+ text: ""
419
+ }
420
+ };
421
+ this.eventEmitter?.emit("realtime:assistant:start", {
422
+ state: this.getState()
423
+ });
424
+ this.emitState();
425
+ return;
426
+ case "assistant.text.delta":
427
+ this.ensureResponseStarted();
428
+ this.state = {
429
+ ...this.state,
430
+ response: {
431
+ status: "responding",
432
+ text: this.state.response.text + event.text
433
+ }
434
+ };
435
+ this.eventEmitter?.emit("realtime:text:delta", { text: event.text });
436
+ this.eventEmitter?.emit("realtime:assistant:delta", {
437
+ text: event.text
438
+ });
439
+ this.emitState();
440
+ return;
441
+ case "assistant.response.completed": {
442
+ if (this.state.response.status === "interrupted") {
443
+ return;
444
+ }
445
+ const text = event.text || this.state.response.text;
446
+ this.state = {
447
+ ...this.state,
448
+ response: {
449
+ status: "completed",
450
+ text
451
+ }
452
+ };
453
+ this.eventEmitter?.emit("realtime:assistant:done", { text });
454
+ this.emitState();
455
+ return;
456
+ }
457
+ case "audio.output.started":
458
+ this.emitAudioStart();
459
+ return;
460
+ case "audio.output.ended":
461
+ this.emitAudioEnd();
462
+ return;
463
+ case "audio.lipsync":
464
+ if (event.rms > 1e-3 && !this.isAudioPlaybackActive) {
465
+ this.emitAudioStart();
466
+ }
467
+ this.eventEmitter?.emit("tts:lipsync:update", { rms: event.rms });
468
+ return;
469
+ case "tool.call":
470
+ this.eventEmitter?.emit("realtime:tool:call", {
471
+ name: event.name,
472
+ args: event.args,
473
+ callId: event.callId
474
+ });
475
+ await this.executeToolCall(event);
476
+ return;
477
+ case "tool.result":
478
+ this.eventEmitter?.emit("realtime:tool:result", {
479
+ name: event.name,
480
+ output: event.output,
481
+ callId: event.callId
482
+ });
483
+ return;
484
+ case "state":
485
+ this.state = mergeRealtimeState(this.state, event.state);
486
+ this.emitState();
487
+ return;
488
+ case "error":
489
+ this.applyError(event.error, this.state.connection);
490
+ return;
491
+ }
492
+ }
493
+ async executeToolCall(event) {
494
+ const tool = this.toolRegistry.get(event.name);
495
+ if (!event.callId) {
496
+ this.emitToolError(
497
+ event.name,
498
+ new Error(`Tool "${event.name}" is missing a call ID`),
499
+ event.callId
500
+ );
501
+ return;
502
+ }
503
+ if (!tool) {
504
+ await this.handleToolExecutionFailure(
505
+ event.name,
506
+ event.callId,
507
+ new Error(`No realtime tool registered for "${event.name}"`)
508
+ );
509
+ return;
510
+ }
511
+ try {
512
+ const output = await this.runToolHandler(tool, event);
513
+ await this.client.sendToolResult(event.callId, output);
514
+ this.postProcessToolResult(event.name, output);
515
+ this.eventEmitter?.emit("realtime:tool:result", {
516
+ name: event.name,
517
+ output,
518
+ callId: event.callId
519
+ });
520
+ } catch (error) {
521
+ await this.handleToolExecutionFailure(
522
+ event.name,
523
+ event.callId,
524
+ error instanceof Error ? error : new Error(String(error))
525
+ );
526
+ }
527
+ }
528
+ async runToolHandler(tool, event) {
529
+ const timeoutMs = tool.timeoutMs ?? this.options.defaultToolTimeoutMs ?? DEFAULT_TOOL_TIMEOUT_MS;
530
+ const result = await withTimeout(
531
+ tool.handler(event.args, {
532
+ character: this.character,
533
+ state: this.getState(),
534
+ callId: event.callId
535
+ }),
536
+ timeoutMs,
537
+ tool.definition.name
538
+ );
539
+ if (!isRecord(result)) {
540
+ throw new Error(
541
+ `Realtime tool "${tool.definition.name}" must return an object`
542
+ );
543
+ }
544
+ return result;
545
+ }
546
+ async handleToolExecutionFailure(name, callId, error) {
547
+ this.emitToolError(name, error, callId);
548
+ try {
549
+ await this.client.sendToolResult(callId, createFailureOutput(error));
550
+ } catch (sendError) {
551
+ this.emitToolError(
552
+ name,
553
+ sendError instanceof Error ? sendError : new Error(String(sendError)),
554
+ callId
555
+ );
556
+ }
557
+ }
558
+ postProcessToolResult(name, output) {
559
+ switch (name) {
560
+ case SET_EXPRESSION_TOOL_NAME: {
561
+ const expressionId = output.expressionId;
562
+ if (typeof expressionId === "string") {
563
+ this.eventEmitter?.emit("realtime:expression", { expressionId });
564
+ }
565
+ return;
566
+ }
567
+ case PLAY_MOTION_TOOL_NAME: {
568
+ const group = output.group;
569
+ const index = output.index;
570
+ if (typeof group === "string" && Number.isInteger(index)) {
571
+ const motionIndex = index;
572
+ this.eventEmitter?.emit("realtime:motion", {
573
+ group,
574
+ index: motionIndex
575
+ });
576
+ }
577
+ return;
578
+ }
579
+ case LOOK_AT_TOOL_NAME: {
580
+ const coords = readGazeCoordinates(output);
581
+ if (coords) {
582
+ this.eventEmitter?.emit("realtime:gaze", coords);
583
+ }
584
+ return;
585
+ }
586
+ default:
587
+ return;
588
+ }
589
+ }
590
+ emitToolError(name, error, callId) {
591
+ this.eventEmitter?.emit("realtime:tool:error", {
592
+ name,
593
+ error,
594
+ callId
595
+ });
596
+ }
597
+ ensureResponseStarted() {
598
+ if (this.state.response.status === "responding") {
599
+ return;
600
+ }
601
+ this.state = {
602
+ ...this.state,
603
+ response: {
604
+ status: "responding",
605
+ text: this.state.response.text
606
+ }
607
+ };
608
+ this.eventEmitter?.emit("realtime:assistant:start", {
609
+ state: this.getState()
610
+ });
611
+ }
612
+ emitState() {
613
+ this.eventEmitter?.emit("realtime:state", {
614
+ state: this.getState()
615
+ });
616
+ }
617
+ applyError(error, connection) {
618
+ this.emitAudioEnd();
619
+ this.state = {
620
+ ...this.state,
621
+ connection,
622
+ lastError: error
623
+ };
624
+ this.eventEmitter?.emit("realtime:error", { error });
625
+ this.emitState();
626
+ }
627
+ emitAudioStart() {
628
+ if (this.isAudioPlaybackActive) {
629
+ return;
630
+ }
631
+ this.isAudioPlaybackActive = true;
632
+ this.eventEmitter?.emit("tts:audio:start", {});
633
+ }
634
+ emitAudioEnd() {
635
+ if (!this.isAudioPlaybackActive) {
636
+ return;
637
+ }
638
+ this.isAudioPlaybackActive = false;
639
+ this.eventEmitter?.emit("tts:lipsync:update", { rms: 0 });
640
+ this.eventEmitter?.emit("tts:audio:end", {});
641
+ }
642
+ resolveNextSessionBaseConfig(config) {
643
+ return mergeSessionConfig(
644
+ this.sessionBaseConfig ?? this.options.defaultSessionConfig,
645
+ config
646
+ );
647
+ }
648
+ buildEffectiveSessionConfig() {
649
+ return buildRealtimeSessionConfig({
650
+ character: this.character,
651
+ baseConfig: {
652
+ ...this.sessionBaseConfig ?? {},
653
+ tools: this.getRegisteredTools()
654
+ }
655
+ });
656
+ }
657
+ commitActiveSession(config) {
658
+ this.state = {
659
+ ...this.state,
660
+ connection: "connected",
661
+ session: {
662
+ status: "active",
663
+ config,
664
+ characterId: this.character?.id
665
+ },
666
+ response: {
667
+ status: "idle",
668
+ text: ""
669
+ },
670
+ lastError: null
671
+ };
672
+ this.emitState();
673
+ }
674
+ async performStopSession() {
675
+ this.isStoppingSession = true;
676
+ this.state = {
677
+ ...this.state,
678
+ connection: "disconnecting"
679
+ };
680
+ this.emitState();
681
+ try {
682
+ await this.client.disconnect();
683
+ this.finalizeStoppedSession(true, "user");
684
+ } catch (error) {
685
+ this.finalizeFailedSession(
686
+ error instanceof Error ? error : new Error(String(error))
687
+ );
688
+ throw error;
689
+ } finally {
690
+ this.isStoppingSession = false;
691
+ }
692
+ }
693
+ async runRefreshLoop() {
694
+ do {
695
+ this.hasQueuedRefresh = false;
696
+ await this.performSessionRefresh();
697
+ } while (this.hasQueuedRefresh && !this.stopRequestedDuringRefresh);
698
+ }
699
+ async performSessionRefresh() {
700
+ const previousState = this.getState();
701
+ const sessionConfig = this.buildEffectiveSessionConfig();
702
+ this.isRefreshingSession = true;
703
+ this.state = {
704
+ ...this.state,
705
+ connection: "disconnecting",
706
+ lastError: null
707
+ };
708
+ this.emitState();
709
+ try {
710
+ await this.client.disconnect();
711
+ if (this.stopRequestedDuringRefresh) {
712
+ this.emittedUserStopDuringRefresh = true;
713
+ this.finalizeStoppedSession(true, "user");
714
+ return;
715
+ }
716
+ this.state = {
717
+ ...this.state,
718
+ connection: "connecting",
719
+ response: {
720
+ status: "idle",
721
+ text: ""
722
+ }
723
+ };
724
+ this.emitState();
725
+ await this.client.connect(sessionConfig);
726
+ if (this.stopRequestedDuringRefresh) {
727
+ await this.client.disconnect();
728
+ this.emittedUserStopDuringRefresh = true;
729
+ this.finalizeStoppedSession(true, "user");
730
+ return;
731
+ }
732
+ this.commitActiveSession(sessionConfig);
733
+ this.emitSessionEnd("refresh", previousState);
734
+ this.emitSessionStart("refresh");
735
+ } catch (error) {
736
+ this.finalizeFailedSession(
737
+ error instanceof Error ? error : new Error(String(error))
738
+ );
739
+ throw error;
740
+ } finally {
741
+ this.isRefreshingSession = false;
742
+ }
743
+ }
744
+ emitSessionStart(reason, state = this.getState()) {
745
+ this.eventEmitter?.emit("realtime:session:start", {
746
+ state,
747
+ reason
748
+ });
749
+ }
750
+ emitSessionEnd(reason, state = this.getState()) {
751
+ this.eventEmitter?.emit("realtime:session:end", {
752
+ state,
753
+ reason
754
+ });
755
+ }
756
+ finalizeFailedSession(error) {
757
+ this.emitAudioEnd();
758
+ this.state = {
759
+ ...this.state,
760
+ connection: "error",
761
+ session: {
762
+ status: "stopped",
763
+ config: null,
764
+ characterId: this.character?.id
765
+ },
766
+ response: {
767
+ status: "idle",
768
+ text: ""
769
+ },
770
+ lastError: error
771
+ };
772
+ this.eventEmitter?.emit("realtime:error", { error });
773
+ this.emitState();
774
+ }
775
+ finalizeStoppedSession(emitSessionEnd, reason) {
776
+ this.emitAudioEnd();
777
+ this.state = {
778
+ ...this.state,
779
+ connection: "idle",
780
+ session: {
781
+ status: "stopped",
782
+ config: null,
783
+ characterId: this.character?.id
784
+ },
785
+ response: {
786
+ status: "idle",
787
+ text: ""
788
+ },
789
+ lastError: null
790
+ };
791
+ this.emitState();
792
+ if (emitSessionEnd) {
793
+ this.emitSessionEnd(reason);
794
+ }
795
+ }
796
+ };
797
+ function readGazeCoordinates(output) {
798
+ const x = output.x;
799
+ const y = output.y;
800
+ if (typeof x !== "number" || typeof y !== "number") {
801
+ return null;
802
+ }
803
+ return { x, y };
804
+ }
805
+ function createRealtimeManager(client, options) {
806
+ return new RealtimeManagerImpl(client, options);
807
+ }
808
+ function mergeSessionConfig(baseConfig, overrideConfig) {
809
+ if (!baseConfig && !overrideConfig) {
810
+ return void 0;
811
+ }
812
+ const { tools: _ignoredOverrideTools, ...overrideWithoutTools } = overrideConfig ?? {};
813
+ return {
814
+ ...baseConfig,
815
+ ...overrideWithoutTools
816
+ };
817
+ }
818
+ function mergeRealtimeState(current, partial) {
819
+ return {
820
+ connection: partial.connection ?? current.connection,
821
+ session: {
822
+ status: partial.session?.status ?? current.session.status,
823
+ config: partial.session?.config ?? current.session.config,
824
+ characterId: partial.session?.characterId ?? current.session.characterId
825
+ },
826
+ response: {
827
+ status: partial.response?.status ?? current.response.status,
828
+ text: partial.response?.text ?? current.response.text
829
+ },
830
+ lastError: partial.lastError ?? current.lastError
831
+ };
832
+ }
833
+ function isRecord(value) {
834
+ return typeof value === "object" && value !== null;
835
+ }
836
+ function createFailureOutput(error) {
837
+ return {
838
+ success: false,
839
+ error: error.message
840
+ };
841
+ }
842
+ async function withTimeout(promise, timeoutMs, toolName) {
843
+ let timeoutId;
844
+ try {
845
+ return await Promise.race([
846
+ promise,
847
+ new Promise((_, reject) => {
848
+ timeoutId = setTimeout(() => {
849
+ reject(
850
+ new Error(
851
+ `Realtime tool "${toolName}" timed out after ${timeoutMs}ms`
852
+ )
853
+ );
854
+ }, timeoutMs);
855
+ })
856
+ ]);
857
+ } finally {
858
+ if (timeoutId !== void 0) {
859
+ clearTimeout(timeoutId);
860
+ }
861
+ }
862
+ }
863
+
864
+ exports.AVATAR_CONTROL_TOOL_NAMES = AVATAR_CONTROL_TOOL_NAMES;
865
+ exports.DEFAULT_REALTIME_AGENT_INSTRUCTIONS = DEFAULT_REALTIME_AGENT_INSTRUCTIONS;
866
+ exports.LOOK_AT_TOOL_NAME = LOOK_AT_TOOL_NAME;
867
+ exports.PLAY_MOTION_TOOL_NAME = PLAY_MOTION_TOOL_NAME;
868
+ exports.RealtimeManagerImpl = RealtimeManagerImpl;
869
+ exports.SET_EXPRESSION_TOOL_NAME = SET_EXPRESSION_TOOL_NAME;
870
+ exports.buildRealtimeSessionConfig = buildRealtimeSessionConfig;
871
+ exports.createAvatarControlTools = createAvatarControlTools;
872
+ exports.createRealtimeManager = createRealtimeManager;
873
+ //# sourceMappingURL=index.js.map
874
+ //# sourceMappingURL=index.js.map