@charivo/realtime 0.2.2 → 0.3.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.
@@ -183,18 +183,20 @@ var LipSyncAnalyzer = class {
183
183
  }
184
184
  };
185
185
 
186
+ // src/instructions.ts
187
+ var DEFAULT_REALTIME_VOICE = "marin";
188
+
186
189
  // src/openai-agents/session-config.ts
187
190
  var DEFAULT_AGENT_INSTRUCTIONS = "You are a realtime voice agent controlling a Live2D character.";
188
191
  var DEFAULT_MODEL = "gpt-realtime-mini";
189
- var DEFAULT_VOICE = "marin";
190
192
  function resolveInstructions(config) {
191
193
  return config?.instructions ?? DEFAULT_AGENT_INSTRUCTIONS;
192
194
  }
193
195
  function resolveVoice(config) {
194
- return config?.voice ?? DEFAULT_VOICE;
196
+ return config?.voice ?? DEFAULT_REALTIME_VOICE;
195
197
  }
196
198
  function toOpenAIRealtimeAgentsSessionConfig(config) {
197
- return {
199
+ const sessionConfig = {
198
200
  model: config?.model ?? DEFAULT_MODEL,
199
201
  instructions: resolveInstructions(config),
200
202
  toolChoice: config?.toolChoice ?? "auto",
@@ -205,6 +207,13 @@ function toOpenAIRealtimeAgentsSessionConfig(config) {
205
207
  }
206
208
  }
207
209
  };
210
+ if (config?.temperature !== void 0) {
211
+ sessionConfig.temperature = config.temperature;
212
+ }
213
+ if (config?.maxTokens !== void 0) {
214
+ sessionConfig.maxResponseOutputTokens = config.maxTokens;
215
+ }
216
+ return sessionConfig;
208
217
  }
209
218
 
210
219
  // src/openai-agents/tool-schema.ts
@@ -275,12 +284,7 @@ var OpenAIRealtimeAgentsClient = class {
275
284
  this.audioElement = document.createElement("audio");
276
285
  this.audioElement.autoplay = true;
277
286
  this.audioElement.setAttribute("playsinline", "true");
278
- const agent = new agentsRealtime.RealtimeAgent({
279
- name: "charivo-realtime-agent",
280
- instructions: resolveInstructions(config),
281
- tools: this.createProxyTools(config?.tools),
282
- voice: resolveVoice(config)
283
- });
287
+ const agent = this.createAgent(config);
284
288
  this.transport = new agentsRealtime.OpenAIRealtimeWebRTC(
285
289
  this.createTransportOptions(this.audioElement)
286
290
  );
@@ -316,6 +320,13 @@ var OpenAIRealtimeAgentsClient = class {
316
320
  this.log("Disconnecting OpenAI Agents Realtime WebRTC");
317
321
  this.cleanup();
318
322
  }
323
+ async updateSession(config) {
324
+ if (!this.session) {
325
+ throw new Error("Realtime session not active");
326
+ }
327
+ this.session.options.config = toOpenAIRealtimeAgentsSessionConfig(config);
328
+ await this.session.updateAgent(this.createAgent(config));
329
+ }
319
330
  async sendText(text) {
320
331
  if (!this.session) {
321
332
  throw new Error("Realtime session not active");
@@ -346,6 +357,14 @@ var OpenAIRealtimeAgentsClient = class {
346
357
  onEvent(callback) {
347
358
  this.eventCallbacks.add(callback);
348
359
  }
360
+ createAgent(config) {
361
+ return new agentsRealtime.RealtimeAgent({
362
+ name: "charivo-realtime-agent",
363
+ instructions: resolveInstructions(config),
364
+ tools: this.createProxyTools(config?.tools),
365
+ voice: resolveVoice(config)
366
+ });
367
+ }
349
368
  bindSessionEvents(session) {
350
369
  session.on("agent_start", () => {
351
370
  this.ensureAssistantStarted();
@@ -569,6 +588,7 @@ var DEBUG_EVENT_ALLOWLIST2 = /* @__PURE__ */ new Set([
569
588
  "conversation.item.input_audio_transcription.completed",
570
589
  "error"
571
590
  ]);
591
+ var DEFAULT_SESSION_UPDATE_TIMEOUT_MS = 5e3;
572
592
  var OpenAIRealtimeClient = class {
573
593
  constructor(options = {}) {
574
594
  this.options = options;
@@ -585,6 +605,8 @@ var OpenAIRealtimeClient = class {
585
605
  hasStartedAssistantResponse = false;
586
606
  hasStartedAudioOutput = false;
587
607
  assistantText = "";
608
+ nextSessionUpdateId = 1;
609
+ pendingSessionUpdate = null;
588
610
  eventCallbacks = /* @__PURE__ */ new Set();
589
611
  async connect(config) {
590
612
  try {
@@ -644,6 +666,40 @@ var OpenAIRealtimeClient = class {
644
666
  this.log("Disconnecting OpenAI Realtime WebRTC");
645
667
  this.cleanup();
646
668
  }
669
+ async updateSession(config) {
670
+ if (this.isResponseInProgress) {
671
+ throw new Error(
672
+ "Cannot update the realtime session while a response is in progress. Call interrupt() first."
673
+ );
674
+ }
675
+ const dc = this.requireOpenDataChannel();
676
+ const eventId = `charivo-session-update-${this.nextSessionUpdateId++}`;
677
+ if (this.pendingSessionUpdate) {
678
+ throw new Error("Realtime session update already in progress");
679
+ }
680
+ dc.send(
681
+ JSON.stringify({
682
+ type: "session.update",
683
+ event_id: eventId,
684
+ session: toOpenAIRealtimeSessionUpdate(config)
685
+ })
686
+ );
687
+ await new Promise((resolve, reject) => {
688
+ const timeoutId = setTimeout(() => {
689
+ this.rejectPendingSessionUpdate(
690
+ new Error(
691
+ `Timed out waiting for session.updated after ${this.getSessionUpdateTimeoutMs()}ms`
692
+ )
693
+ );
694
+ }, this.getSessionUpdateTimeoutMs());
695
+ this.pendingSessionUpdate = {
696
+ eventId,
697
+ resolve,
698
+ reject,
699
+ timeoutId
700
+ };
701
+ });
702
+ }
647
703
  async sendText(text) {
648
704
  if (!this.dc || this.dc.readyState !== "open") {
649
705
  throw new Error("DataChannel not ready");
@@ -708,7 +764,9 @@ var OpenAIRealtimeClient = class {
708
764
  }
709
765
  switch (event.type) {
710
766
  case "session.created":
767
+ return;
711
768
  case "session.updated":
769
+ this.resolvePendingSessionUpdate();
712
770
  return;
713
771
  case "response.audio.delta":
714
772
  case "output_audio_buffer.started":
@@ -814,10 +872,16 @@ var OpenAIRealtimeClient = class {
814
872
  case "error":
815
873
  this.isResponseInProgress = false;
816
874
  this.cancelInFlight = false;
817
- this.emitEvent({
818
- type: "error",
819
- error: new Error(event.error?.message || "Unknown error")
820
- });
875
+ {
876
+ const error = new Error(event.error?.message || "Unknown error");
877
+ if (this.isPendingSessionUpdateError(event)) {
878
+ this.rejectPendingSessionUpdate(error);
879
+ }
880
+ this.emitEvent({
881
+ type: "error",
882
+ error
883
+ });
884
+ }
821
885
  return;
822
886
  default:
823
887
  return;
@@ -928,6 +992,9 @@ var OpenAIRealtimeClient = class {
928
992
  }
929
993
  cleanup() {
930
994
  this.stopLipSyncAnalysis();
995
+ this.rejectPendingSessionUpdate(
996
+ new Error("Realtime session ended before session update completed")
997
+ );
931
998
  if (this.dc) {
932
999
  this.dc.close();
933
1000
  this.dc = null;
@@ -964,6 +1031,32 @@ var OpenAIRealtimeClient = class {
964
1031
  }
965
1032
  return this.dc;
966
1033
  }
1034
+ resolvePendingSessionUpdate() {
1035
+ if (!this.pendingSessionUpdate) {
1036
+ return;
1037
+ }
1038
+ clearTimeout(this.pendingSessionUpdate.timeoutId);
1039
+ this.pendingSessionUpdate.resolve();
1040
+ this.pendingSessionUpdate = null;
1041
+ }
1042
+ rejectPendingSessionUpdate(error) {
1043
+ if (!this.pendingSessionUpdate) {
1044
+ return;
1045
+ }
1046
+ clearTimeout(this.pendingSessionUpdate.timeoutId);
1047
+ this.pendingSessionUpdate.reject(error);
1048
+ this.pendingSessionUpdate = null;
1049
+ }
1050
+ isPendingSessionUpdateError(event) {
1051
+ const pendingUpdate = this.pendingSessionUpdate;
1052
+ if (!pendingUpdate) {
1053
+ return false;
1054
+ }
1055
+ return event.error?.event_id === pendingUpdate.eventId;
1056
+ }
1057
+ getSessionUpdateTimeoutMs() {
1058
+ return this.options.sessionUpdateTimeoutMs ?? DEFAULT_SESSION_UPDATE_TIMEOUT_MS;
1059
+ }
967
1060
  resetResponseTracking() {
968
1061
  this.assistantText = "";
969
1062
  this.hasStartedAssistantResponse = false;
@@ -989,6 +1082,29 @@ var OpenAIRealtimeClient = class {
989
1082
  function createOpenAIRealtimeClient(options) {
990
1083
  return new OpenAIRealtimeClient(options);
991
1084
  }
1085
+ function toOpenAIRealtimeSessionUpdate(config) {
1086
+ const session = {
1087
+ audio: {
1088
+ output: {
1089
+ voice: config?.voice ?? DEFAULT_REALTIME_VOICE
1090
+ }
1091
+ },
1092
+ tool_choice: config?.toolChoice ?? "auto"
1093
+ };
1094
+ if (config?.instructions) {
1095
+ session.instructions = config.instructions;
1096
+ }
1097
+ if (config?.temperature !== void 0) {
1098
+ session.temperature = config.temperature;
1099
+ }
1100
+ if (config?.maxTokens !== void 0) {
1101
+ session.max_response_output_tokens = config.maxTokens;
1102
+ }
1103
+ if (config?.tools?.length) {
1104
+ session.tools = config.tools;
1105
+ }
1106
+ return session;
1107
+ }
992
1108
 
993
1109
  // src/remote/index.ts
994
1110
  var DEFAULT_REMOTE_REALTIME_ADAPTERS = {
@@ -1037,6 +1153,9 @@ var RemoteRealtimeClient = class {
1037
1153
  throw error;
1038
1154
  }
1039
1155
  }
1156
+ async updateSession(config) {
1157
+ await this.getActiveTransportClient().updateSession(config);
1158
+ }
1040
1159
  async disconnect() {
1041
1160
  if (!this.transportClient) {
1042
1161
  return;