@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.
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/llm.cjs +1 -1
- package/dist/llm.cjs.map +1 -1
- package/dist/llm.js +1 -1
- package/dist/llm.js.map +1 -1
- package/dist/models.cjs.map +1 -1
- package/dist/models.d.cts +2 -2
- package/dist/models.d.ts +2 -2
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js.map +1 -1
- package/dist/realtime/realtime_model.cjs +36 -31
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +1 -0
- package/dist/realtime/realtime_model.d.ts +1 -0
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +36 -31
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model.test.cjs +58 -0
- package/dist/realtime/realtime_model.test.cjs.map +1 -1
- package/dist/realtime/realtime_model.test.js +60 -2
- package/dist/realtime/realtime_model.test.js.map +1 -1
- package/package.json +7 -7
- package/src/llm.ts +1 -1
- package/src/models.ts +4 -14
- package/src/realtime/realtime_model.test.ts +91 -2
- package/src/realtime/realtime_model.ts +41 -34
|
@@ -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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
543
|
+
const currentToolChoice = toOaiToolChoice(this._options.toolChoice);
|
|
539
544
|
const nextToolChoice = toOaiToolChoice(toolChoice);
|
|
540
545
|
if (currentToolChoice === nextToolChoice) {
|
|
541
|
-
this.
|
|
546
|
+
this._options.toolChoice = toolChoice;
|
|
542
547
|
return;
|
|
543
548
|
}
|
|
544
|
-
const isLegacyAzure = this.
|
|
549
|
+
const isLegacyAzure = this._options.isAzure && !!this._options.apiVersion;
|
|
545
550
|
const options = {
|
|
546
551
|
...!isLegacyAzure && { type: "realtime" }
|
|
547
552
|
};
|
|
548
|
-
this.
|
|
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.
|
|
643
|
-
if (this.
|
|
644
|
-
headers.Authorization = `Bearer ${this.
|
|
645
|
-
} else if (this.
|
|
646
|
-
headers["api-key"] = this.
|
|
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.
|
|
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.
|
|
661
|
+
headers.Authorization = `Bearer ${this._options.apiKey}`;
|
|
657
662
|
}
|
|
658
663
|
const url = processBaseURL({
|
|
659
|
-
baseURL: this.
|
|
660
|
-
model: this.
|
|
661
|
-
isAzure: this.
|
|
662
|
-
apiVersion: this.
|
|
663
|
-
azureDeployment: this.
|
|
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.
|
|
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.
|
|
698
|
+
const maxRetries = this._options.connOptions.maxRetry;
|
|
694
699
|
const reconnect = async () => {
|
|
695
700
|
this.#logger.debug(
|
|
696
701
|
{
|
|
697
|
-
maxSessionDuration: this.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
1271
|
+
generation.modalities.resolve(this._options.modalities);
|
|
1267
1272
|
}
|
|
1268
1273
|
}
|
|
1269
1274
|
this.currentGeneration.functionChannel.close();
|