@livekit/agents-plugin-openai 1.3.0 → 1.3.2

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.
@@ -264,6 +264,10 @@ class RealtimeSession extends llm.RealtimeSession {
264
264
  inputResampler;
265
265
  instructions;
266
266
  oaiRealtimeModel;
267
+ // Ref: python livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/realtime_model.py - 795-797 lines
268
+ // per-session copy of options so updateOptions can diff against the session's
269
+ // own state instead of the shared model-level state.
270
+ _options;
267
271
  currentGeneration;
268
272
  responseCreatedFutures = {};
269
273
  textModeRecoveryRetries = 0;
@@ -283,6 +287,7 @@ class RealtimeSession extends llm.RealtimeSession {
283
287
  constructor(realtimeModel) {
284
288
  super(realtimeModel);
285
289
  this.oaiRealtimeModel = realtimeModel;
290
+ this._options = { ...realtimeModel._options };
286
291
  this.#task = Task.from(({ signal }) => this.#mainTask(signal));
287
292
  this.sendEvent(this.createSessionUpdateEvent());
288
293
  }
@@ -293,7 +298,7 @@ class RealtimeSession extends llm.RealtimeSession {
293
298
  this.messageChannel.put(command);
294
299
  }
295
300
  createSessionUpdateEvent() {
296
- const opts = this.oaiRealtimeModel._options;
301
+ const opts = this._options;
297
302
  const maxOutputTokens = opts.maxResponseOutputTokens === Infinity ? "inf" : opts.maxResponseOutputTokens;
298
303
  if (opts.isAzure && opts.apiVersion) {
299
304
  const modalities = opts.modalities.includes("audio") ? ["text", "audio"] : ["text"];
@@ -510,19 +515,19 @@ class RealtimeSession extends llm.RealtimeSession {
510
515
  continue;
511
516
  }
512
517
  }
513
- const isLegacyAzure = this.oaiRealtimeModel._options.isAzure && !!this.oaiRealtimeModel._options.apiVersion;
518
+ const isLegacyAzure = this._options.isAzure && !!this._options.apiVersion;
514
519
  return {
515
520
  type: "session.update",
516
521
  session: {
517
522
  ...!isLegacyAzure && { type: "realtime" },
518
- model: this.oaiRealtimeModel._options.model,
523
+ model: this._options.model,
519
524
  tools: oaiTools
520
525
  },
521
526
  event_id: shortuuid("tools_update_")
522
527
  };
523
528
  }
524
529
  async updateInstructions(_instructions) {
525
- const isLegacyAzure = this.oaiRealtimeModel._options.isAzure && !!this.oaiRealtimeModel._options.apiVersion;
530
+ const isLegacyAzure = this._options.isAzure && !!this._options.apiVersion;
526
531
  const eventId = shortuuid("instructions_update_");
527
532
  this.sendEvent({
528
533
  type: "session.update",
@@ -535,17 +540,17 @@ class RealtimeSession extends llm.RealtimeSession {
535
540
  this.instructions = _instructions;
536
541
  }
537
542
  updateOptions({ toolChoice }) {
538
- const currentToolChoice = toOaiToolChoice(this.oaiRealtimeModel._options.toolChoice);
543
+ const currentToolChoice = toOaiToolChoice(this._options.toolChoice);
539
544
  const nextToolChoice = toOaiToolChoice(toolChoice);
540
545
  if (currentToolChoice === nextToolChoice) {
541
- this.oaiRealtimeModel._options.toolChoice = toolChoice;
546
+ this._options.toolChoice = toolChoice;
542
547
  return;
543
548
  }
544
- const isLegacyAzure = this.oaiRealtimeModel._options.isAzure && !!this.oaiRealtimeModel._options.apiVersion;
549
+ const isLegacyAzure = this._options.isAzure && !!this._options.apiVersion;
545
550
  const options = {
546
551
  ...!isLegacyAzure && { type: "realtime" }
547
552
  };
548
- this.oaiRealtimeModel._options.toolChoice = toolChoice;
553
+ this._options.toolChoice = toolChoice;
549
554
  options.tool_choice = toOaiToolChoice(toolChoice);
550
555
  this.sendEvent({
551
556
  type: "session.update",
@@ -639,28 +644,28 @@ class RealtimeSession extends llm.RealtimeSession {
639
644
  const headers = {
640
645
  "User-Agent": "LiveKit-Agents-JS"
641
646
  };
642
- if (this.oaiRealtimeModel._options.isAzure) {
643
- if (this.oaiRealtimeModel._options.entraToken) {
644
- headers.Authorization = `Bearer ${this.oaiRealtimeModel._options.entraToken}`;
645
- } else if (this.oaiRealtimeModel._options.apiKey) {
646
- headers["api-key"] = this.oaiRealtimeModel._options.apiKey;
647
+ if (this._options.isAzure) {
648
+ if (this._options.entraToken) {
649
+ headers.Authorization = `Bearer ${this._options.entraToken}`;
650
+ } else if (this._options.apiKey) {
651
+ headers["api-key"] = this._options.apiKey;
647
652
  } else {
648
653
  throw new Error("Microsoft API key or entraToken is required");
649
654
  }
650
655
  } else {
651
- if (!this.oaiRealtimeModel._options.apiKey) {
656
+ if (!this._options.apiKey) {
652
657
  throw new Error(
653
658
  "OpenAI API key is required but not set. Check OPENAI_API_KEY environment variable."
654
659
  );
655
660
  }
656
- headers.Authorization = `Bearer ${this.oaiRealtimeModel._options.apiKey}`;
661
+ headers.Authorization = `Bearer ${this._options.apiKey}`;
657
662
  }
658
663
  const url = processBaseURL({
659
- baseURL: this.oaiRealtimeModel._options.baseURL,
660
- model: this.oaiRealtimeModel._options.model,
661
- isAzure: this.oaiRealtimeModel._options.isAzure,
662
- apiVersion: this.oaiRealtimeModel._options.apiVersion,
663
- azureDeployment: this.oaiRealtimeModel._options.azureDeployment
664
+ baseURL: this._options.baseURL,
665
+ model: this._options.model,
666
+ isAzure: this._options.isAzure,
667
+ apiVersion: this._options.apiVersion,
668
+ azureDeployment: this._options.azureDeployment
664
669
  });
665
670
  if (lkOaiDebug) {
666
671
  this.#logger.debug(`Connecting to OpenAI Realtime API at ${url}`);
@@ -671,7 +676,7 @@ class RealtimeSession extends llm.RealtimeSession {
671
676
  const timeout = setTimeout(() => {
672
677
  ws.close();
673
678
  reject(new Error("WebSocket connection timeout"));
674
- }, this.oaiRealtimeModel._options.connOptions.timeoutMs);
679
+ }, this._options.connOptions.timeoutMs);
675
680
  ws.once("open", () => {
676
681
  if (!waiting) return;
677
682
  waiting = false;
@@ -690,11 +695,11 @@ class RealtimeSession extends llm.RealtimeSession {
690
695
  let reconnecting = false;
691
696
  let numRetries = 0;
692
697
  let wsConn = null;
693
- const maxRetries = this.oaiRealtimeModel._options.connOptions.maxRetry;
698
+ const maxRetries = this._options.connOptions.maxRetry;
694
699
  const reconnect = async () => {
695
700
  this.#logger.debug(
696
701
  {
697
- maxSessionDuration: this.oaiRealtimeModel._options.maxSessionDuration
702
+ maxSessionDuration: this._options.maxSessionDuration
698
703
  },
699
704
  "Reconnecting to OpenAI Realtime API"
700
705
  );
@@ -729,7 +734,7 @@ class RealtimeSession extends llm.RealtimeSession {
729
734
  try {
730
735
  for (const ev of events) {
731
736
  this.emit("openai_client_event_queued", ev);
732
- if (this.oaiRealtimeModel._options.isAzure && this.oaiRealtimeModel._options.apiVersion) {
737
+ if (this._options.isAzure && this._options.apiVersion) {
733
738
  normalizeAzureClientEvent(ev);
734
739
  }
735
740
  wsConn.send(JSON.stringify(ev));
@@ -776,7 +781,7 @@ class RealtimeSession extends llm.RealtimeSession {
776
781
  });
777
782
  }
778
783
  this.emitError({ error, recoverable: true });
779
- const retryInterval = numRetries === 0 ? DEFAULT_FIRST_RETRY_INTERVAL_MS : this.oaiRealtimeModel._options.connOptions.retryIntervalMs;
784
+ const retryInterval = numRetries === 0 ? DEFAULT_FIRST_RETRY_INTERVAL_MS : this._options.connOptions.retryIntervalMs;
780
785
  this.#logger.warn(
781
786
  {
782
787
  attempt: numRetries,
@@ -805,7 +810,7 @@ class RealtimeSession extends llm.RealtimeSession {
805
810
  this.#logger.debug(this.loggableEvent(event), `(client) -> ${event.type}`);
806
811
  }
807
812
  this.emit("openai_client_event_queued", event);
808
- if (this.oaiRealtimeModel._options.isAzure && this.oaiRealtimeModel._options.apiVersion) {
813
+ if (this._options.isAzure && this._options.apiVersion) {
809
814
  normalizeAzureClientEvent(event);
810
815
  }
811
816
  wsConn.send(JSON.stringify(event));
@@ -910,7 +915,7 @@ class RealtimeSession extends llm.RealtimeSession {
910
915
  return Promise.race([wsCloseFuture.await, abortPromise]);
911
916
  });
912
917
  const waitReconnectTask = Task.from(async ({ signal }) => {
913
- await delay(this.oaiRealtimeModel._options.maxSessionDuration, { signal });
918
+ await delay(this._options.maxSessionDuration, { signal });
914
919
  return new APIConnectionError({
915
920
  message: "OpenAI Realtime API connection timeout"
916
921
  });
@@ -955,7 +960,7 @@ class RealtimeSession extends llm.RealtimeSession {
955
960
  gen.textChannel.close();
956
961
  gen.audioChannel.close();
957
962
  if (!gen.modalities.done) {
958
- gen.modalities.resolve(this.oaiRealtimeModel._options.modalities);
963
+ gen.modalities.resolve(this._options.modalities);
959
964
  }
960
965
  }
961
966
  this.currentGeneration.messages.clear();
@@ -973,7 +978,7 @@ class RealtimeSession extends llm.RealtimeSession {
973
978
  }
974
979
  handleInputAudioBufferSpeechStopped(_event) {
975
980
  this.emit("input_speech_stopped", {
976
- userTranscriptionEnabled: this.oaiRealtimeModel._options.inputAudioTranscription !== null
981
+ userTranscriptionEnabled: this._options.inputAudioTranscription !== null
977
982
  });
978
983
  }
979
984
  handleResponseCreated(event) {
@@ -1242,7 +1247,7 @@ class RealtimeSession extends llm.RealtimeSession {
1242
1247
  itemGeneration.textChannel.close();
1243
1248
  itemGeneration.audioChannel.close();
1244
1249
  if (!itemGeneration.modalities.done) {
1245
- itemGeneration.modalities.resolve(this.oaiRealtimeModel._options.modalities);
1250
+ itemGeneration.modalities.resolve(this._options.modalities);
1246
1251
  }
1247
1252
  }
1248
1253
  }
@@ -1263,7 +1268,7 @@ class RealtimeSession extends llm.RealtimeSession {
1263
1268
  generation.textChannel.close();
1264
1269
  generation.audioChannel.close();
1265
1270
  if (!generation.modalities.done) {
1266
- generation.modalities.resolve(this.oaiRealtimeModel._options.modalities);
1271
+ generation.modalities.resolve(this._options.modalities);
1267
1272
  }
1268
1273
  }
1269
1274
  this.currentGeneration.functionChannel.close();