@livekit/agents-plugin-openai 1.5.2 → 1.5.3
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/realtime/realtime_model.cjs +197 -159
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +4 -0
- package/dist/realtime/realtime_model.d.ts +4 -0
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +196 -159
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model.test.cjs +176 -0
- package/dist/realtime/realtime_model.test.cjs.map +1 -1
- package/dist/realtime/realtime_model.test.js +177 -0
- package/dist/realtime/realtime_model.test.js.map +1 -1
- package/dist/ws/llm.cjs +5 -2
- package/dist/ws/llm.cjs.map +1 -1
- package/dist/ws/llm.d.cts +4 -5
- package/dist/ws/llm.d.ts +4 -5
- package/dist/ws/llm.d.ts.map +1 -1
- package/dist/ws/llm.js +5 -2
- package/dist/ws/llm.js.map +1 -1
- package/dist/ws/llm.test.cjs +22 -2
- package/dist/ws/llm.test.cjs.map +1 -1
- package/dist/ws/llm.test.js +22 -2
- package/dist/ws/llm.test.js.map +1 -1
- package/package.json +5 -5
- package/src/realtime/realtime_model.test.ts +210 -0
- package/src/realtime/realtime_model.ts +207 -163
- package/src/ws/llm.test.ts +28 -2
- package/src/ws/llm.ts +12 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"realtime_model.d.ts","sourceRoot":"","sources":["../../src/realtime/realtime_model.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,iBAAiB,EAatB,GAAG,EAKJ,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,UAAU,EAAsB,MAAM,mBAAmB,CAAC;AAEnE,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAW5C,KAAK,QAAQ,GAAG,MAAM,GAAG,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AAC,aAAa;IAuBrB,OAAO,CAAC,sBAAsB;IAyB9B,OAAO,CAAC,4BAA4B;YAUtB,YAAY;YAwMZ,KAAK;IA4Kb,KAAK;IA+BX,OAAO,CAAC,mCAAmC;IAM3C,OAAO,CAAC,mCAAmC;IAQ3C,OAAO,CAAC,qBAAqB;IAkD7B,OAAO,CAAC,6BAA6B;IAkDrC,OAAO,CAAC,6BAA6B;IA6BrC,OAAO,CAAC,6BAA6B;IAsBrC,OAAO,CAAC,kDAAkD;IAqB1D,OAAO,CAAC,gBAAgB;IASxB,OAAO,CAAC,sDAAsD;IAsB9D,OAAO,CAAC,mDAAmD;IAW3D,OAAO,CAAC,qCAAqC;IAU7C,OAAO,CAAC,8BAA8B;IAoCtC,OAAO,CAAC,6BAA6B;IAiBrC,OAAO,CAAC,uBAAuB;IAuB/B,OAAO,CAAC,sBAAsB;IAQ9B,OAAO,CAAC,kCAAkC;IA0B1C,OAAO,CAAC,wBAAwB;IAqChC,OAAO,CAAC,iCAAiC;IAUzC,OAAO,CAAC,uBAAuB;IAQ/B,OAAO,CAAC,4BAA4B;IAqCpC,OAAO,CAAC,kBAAkB;IAoF1B,OAAO,CAAC,gCAAgC;IAmDxC,OAAO,CAAC,WAAW;IA6BnB,OAAO,CAAC,SAAS;IAYjB,OAAO,CAAE,aAAa;IAItB,OAAO,CAAC,cAAc;CAyCvB;AAED,8CAA8C;AAC9C,wBAAsB,uBAAuB,CAAC,IAAI,EAAE,GAAG,CAAC,QAAQ,GAAG,OAAO,CAAC,SAAS,CAAC,YAAY,CAAC,CA8DjG"}
|
|
@@ -27,6 +27,18 @@ const MOCK_AUDIO_ID_PREFIX = "lk_mock_audio_item_";
|
|
|
27
27
|
class DiscardedGeneration {
|
|
28
28
|
discarded = true;
|
|
29
29
|
}
|
|
30
|
+
const FATAL_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
31
|
+
"insufficient_quota",
|
|
32
|
+
"invalid_api_key",
|
|
33
|
+
"account_deactivated",
|
|
34
|
+
"billing_hard_limit_reached"
|
|
35
|
+
]);
|
|
36
|
+
function isFatalError(error) {
|
|
37
|
+
if (error === null || typeof error !== "object") return false;
|
|
38
|
+
const { code, type } = error;
|
|
39
|
+
const errorCode = typeof code === "string" && code.length > 0 ? code : type;
|
|
40
|
+
return typeof errorCode === "string" && FATAL_ERROR_CODES.has(errorCode);
|
|
41
|
+
}
|
|
30
42
|
class CreateResponseHandle {
|
|
31
43
|
instructions;
|
|
32
44
|
doneFut;
|
|
@@ -690,6 +702,37 @@ ${instructions}`;
|
|
|
690
702
|
}
|
|
691
703
|
return untypedEvent;
|
|
692
704
|
}
|
|
705
|
+
closeCurrentGeneration(reason) {
|
|
706
|
+
if (this.currentGeneration instanceof DiscardedGeneration) {
|
|
707
|
+
this.currentGeneration = void 0;
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
if (!this.currentGeneration) return;
|
|
711
|
+
for (const gen of this.currentGeneration.messages.values()) {
|
|
712
|
+
gen.textChannel.close();
|
|
713
|
+
gen.audioChannel.close();
|
|
714
|
+
if (!gen.modalities.done) {
|
|
715
|
+
gen.modalities.resolve(this._options.modalities);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
this.currentGeneration.messages.clear();
|
|
719
|
+
this.currentGeneration.messageChannel.close();
|
|
720
|
+
this.currentGeneration.functionChannel.close();
|
|
721
|
+
if (!this.currentGeneration._doneFut.done) {
|
|
722
|
+
this.currentGeneration._doneFut.resolve();
|
|
723
|
+
}
|
|
724
|
+
this.#logger.warn(`In-progress generation discarded due to ${reason}`);
|
|
725
|
+
this.currentGeneration = void 0;
|
|
726
|
+
}
|
|
727
|
+
rejectResponseCreatedFutures(reason) {
|
|
728
|
+
for (const handle of Object.values(this.responseCreatedFutures)) {
|
|
729
|
+
if (!handle.doneFut.done) {
|
|
730
|
+
handle.doneFut.reject(new Error(reason));
|
|
731
|
+
}
|
|
732
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
733
|
+
}
|
|
734
|
+
this.responseCreatedFutures = {};
|
|
735
|
+
}
|
|
693
736
|
async createWsConn() {
|
|
694
737
|
const headers = {
|
|
695
738
|
"User-Agent": "LiveKit-Agents-JS"
|
|
@@ -761,14 +804,9 @@ ${instructions}`;
|
|
|
761
804
|
if (!fut.done) fut.reject(new Error("Session reconnected"));
|
|
762
805
|
}
|
|
763
806
|
this.itemDeleteFutures = {};
|
|
764
|
-
|
|
765
|
-
if (!handle.doneFut.done) {
|
|
766
|
-
handle.doneFut.reject(new Error("Session reconnected"));
|
|
767
|
-
}
|
|
768
|
-
if (handle.timeout) clearTimeout(handle.timeout);
|
|
769
|
-
}
|
|
770
|
-
this.responseCreatedFutures = {};
|
|
807
|
+
this.rejectResponseCreatedFutures("Session reconnected");
|
|
771
808
|
this.discardedEventIds.clear();
|
|
809
|
+
this.closeCurrentGeneration("session reconnection");
|
|
772
810
|
this.audioCapableItemIds.clear();
|
|
773
811
|
this.inputTranscriptAccumulators.clear();
|
|
774
812
|
const events = [];
|
|
@@ -802,51 +840,56 @@ ${instructions}`;
|
|
|
802
840
|
this.emit("session_reconnected", {});
|
|
803
841
|
};
|
|
804
842
|
reconnecting = false;
|
|
805
|
-
|
|
806
|
-
this.#
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
try {
|
|
810
|
-
if (reconnecting) {
|
|
811
|
-
await reconnect();
|
|
812
|
-
if (signal.aborted) break;
|
|
813
|
-
numRetries = 0;
|
|
814
|
-
}
|
|
815
|
-
await this.runWs(wsConn);
|
|
843
|
+
try {
|
|
844
|
+
while (!this.#closed && !signal.aborted) {
|
|
845
|
+
this.#logger.debug("Creating WebSocket connection to OpenAI Realtime API");
|
|
846
|
+
wsConn = await this.createWsConn();
|
|
816
847
|
if (signal.aborted) break;
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
this.
|
|
824
|
-
|
|
825
|
-
}
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
848
|
+
try {
|
|
849
|
+
if (reconnecting) {
|
|
850
|
+
await reconnect();
|
|
851
|
+
if (signal.aborted) break;
|
|
852
|
+
numRetries = 0;
|
|
853
|
+
}
|
|
854
|
+
await this.runWs(wsConn);
|
|
855
|
+
if (signal.aborted) break;
|
|
856
|
+
} catch (error) {
|
|
857
|
+
if (!isAPIError(error)) {
|
|
858
|
+
this.emitError({ error, recoverable: false });
|
|
859
|
+
throw error;
|
|
860
|
+
}
|
|
861
|
+
if (maxRetries === 0 || !error.retryable) {
|
|
862
|
+
this.emitError({ error, recoverable: false });
|
|
863
|
+
throw error;
|
|
864
|
+
}
|
|
865
|
+
if (numRetries === maxRetries) {
|
|
866
|
+
this.emitError({ error, recoverable: false });
|
|
867
|
+
throw new APIConnectionError({
|
|
868
|
+
message: `OpenAI Realtime API connection failed after ${numRetries} attempts`,
|
|
869
|
+
options: {
|
|
870
|
+
body: error,
|
|
871
|
+
retryable: false
|
|
872
|
+
}
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
this.emitError({ error, recoverable: true });
|
|
876
|
+
const retryInterval = numRetries === 0 ? DEFAULT_FIRST_RETRY_INTERVAL_MS : this._options.connOptions.retryIntervalMs;
|
|
877
|
+
this.#logger.warn(
|
|
878
|
+
{
|
|
879
|
+
attempt: numRetries,
|
|
880
|
+
maxRetries,
|
|
881
|
+
error
|
|
882
|
+
},
|
|
883
|
+
`OpenAI Realtime API connection failed, retrying in ${retryInterval / 1e3}s`
|
|
884
|
+
);
|
|
885
|
+
await delay(retryInterval);
|
|
886
|
+
numRetries++;
|
|
835
887
|
}
|
|
836
|
-
|
|
837
|
-
const retryInterval = numRetries === 0 ? DEFAULT_FIRST_RETRY_INTERVAL_MS : this._options.connOptions.retryIntervalMs;
|
|
838
|
-
this.#logger.warn(
|
|
839
|
-
{
|
|
840
|
-
attempt: numRetries,
|
|
841
|
-
maxRetries,
|
|
842
|
-
error
|
|
843
|
-
},
|
|
844
|
-
`OpenAI Realtime API connection failed, retrying in ${retryInterval / 1e3}s`
|
|
845
|
-
);
|
|
846
|
-
await delay(retryInterval);
|
|
847
|
-
numRetries++;
|
|
888
|
+
reconnecting = true;
|
|
848
889
|
}
|
|
849
|
-
|
|
890
|
+
} finally {
|
|
891
|
+
this.closeCurrentGeneration("session close");
|
|
892
|
+
this.rejectResponseCreatedFutures("Realtime session closed");
|
|
850
893
|
}
|
|
851
894
|
}
|
|
852
895
|
async runWs(wsConn) {
|
|
@@ -886,79 +929,88 @@ ${instructions}`;
|
|
|
886
929
|
if (lkOaiDebug) {
|
|
887
930
|
this.#logger.debug(this.loggableEvent(event), `(server) <- ${event.type}`);
|
|
888
931
|
}
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
932
|
+
try {
|
|
933
|
+
switch (event.type) {
|
|
934
|
+
case "input_audio_buffer.speech_started":
|
|
935
|
+
this.handleInputAudioBufferSpeechStarted(event);
|
|
936
|
+
break;
|
|
937
|
+
case "input_audio_buffer.speech_stopped":
|
|
938
|
+
this.handleInputAudioBufferSpeechStopped(event);
|
|
939
|
+
break;
|
|
940
|
+
case "response.created":
|
|
941
|
+
this.handleResponseCreated(event);
|
|
942
|
+
break;
|
|
943
|
+
case "response.output_item.added":
|
|
944
|
+
this.handleResponseOutputItemAdded(event);
|
|
945
|
+
break;
|
|
946
|
+
case "conversation.item.added":
|
|
947
|
+
case "conversation.item.created":
|
|
948
|
+
this.handleConversationItemCreated(event);
|
|
949
|
+
break;
|
|
950
|
+
case "conversation.item.deleted":
|
|
951
|
+
this.handleConversationItemDeleted(event);
|
|
952
|
+
break;
|
|
953
|
+
case "conversation.item.input_audio_transcription.delta":
|
|
954
|
+
this.handleConversationItemInputAudioTranscriptionDelta(event);
|
|
955
|
+
break;
|
|
956
|
+
case "conversation.item.input_audio_transcription.completed":
|
|
957
|
+
this.handleConversationItemInputAudioTranscriptionCompleted(event);
|
|
958
|
+
break;
|
|
959
|
+
case "conversation.item.input_audio_transcription.failed":
|
|
960
|
+
this.handleConversationItemInputAudioTranscriptionFailed(event);
|
|
961
|
+
break;
|
|
962
|
+
case "response.content_part.added":
|
|
963
|
+
this.handleResponseContentPartAdded(event);
|
|
964
|
+
break;
|
|
965
|
+
case "response.content_part.done":
|
|
966
|
+
this.handleResponseContentPartDone(event);
|
|
967
|
+
break;
|
|
968
|
+
case "response.output_text.delta":
|
|
969
|
+
case "response.text.delta":
|
|
970
|
+
this.handleResponseTextDelta(event);
|
|
971
|
+
break;
|
|
972
|
+
case "response.output_text.done":
|
|
973
|
+
case "response.text.done":
|
|
974
|
+
this.handleResponseTextDone(event);
|
|
975
|
+
break;
|
|
976
|
+
case "response.output_audio_transcript.delta":
|
|
977
|
+
case "response.audio_transcript.delta":
|
|
978
|
+
this.handleResponseAudioTranscriptDelta(event);
|
|
979
|
+
break;
|
|
980
|
+
case "response.output_audio.delta":
|
|
981
|
+
case "response.audio.delta":
|
|
982
|
+
this.handleResponseAudioDelta(event);
|
|
983
|
+
break;
|
|
984
|
+
case "response.output_audio_transcript.done":
|
|
985
|
+
case "response.audio_transcript.done":
|
|
986
|
+
this.handleResponseAudioTranscriptDone(event);
|
|
987
|
+
break;
|
|
988
|
+
case "response.output_audio.done":
|
|
989
|
+
case "response.audio.done":
|
|
990
|
+
this.handleResponseAudioDone(event);
|
|
991
|
+
break;
|
|
992
|
+
case "response.output_item.done":
|
|
993
|
+
this.handleResponseOutputItemDone(event);
|
|
994
|
+
break;
|
|
995
|
+
case "response.done":
|
|
996
|
+
this.handleResponseDone(event);
|
|
997
|
+
break;
|
|
998
|
+
case "error":
|
|
999
|
+
this.handleError(event);
|
|
1000
|
+
break;
|
|
1001
|
+
default:
|
|
1002
|
+
if (lkOaiDebug) {
|
|
1003
|
+
this.#logger.debug(`unhandled event: ${event.type}`);
|
|
1004
|
+
}
|
|
1005
|
+
break;
|
|
1006
|
+
}
|
|
1007
|
+
} catch (error) {
|
|
1008
|
+
if (isAPIError(error) && !error.retryable) {
|
|
1009
|
+
wsCloseFuture.resolve(error);
|
|
1010
|
+
wsConn.close();
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
1013
|
+
this.#logger.error({ error, event }, "Failed to handle OpenAI Realtime API event");
|
|
962
1014
|
}
|
|
963
1015
|
};
|
|
964
1016
|
const sendTask = Task.from(({ signal }) => forwardEvents(signal));
|
|
@@ -998,13 +1050,7 @@ ${instructions}`;
|
|
|
998
1050
|
super.close();
|
|
999
1051
|
this.#closed = true;
|
|
1000
1052
|
await this.#task;
|
|
1001
|
-
|
|
1002
|
-
if (!handle.doneFut.done) {
|
|
1003
|
-
handle.doneFut.reject(new Error("Session closed"));
|
|
1004
|
-
}
|
|
1005
|
-
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1006
|
-
}
|
|
1007
|
-
this.responseCreatedFutures = {};
|
|
1053
|
+
this.rejectResponseCreatedFutures("Session closed");
|
|
1008
1054
|
for (const fut of Object.values(this.itemCreateFutures)) {
|
|
1009
1055
|
if (!fut.done) {
|
|
1010
1056
|
fut.reject(new Error("Session closed"));
|
|
@@ -1018,24 +1064,7 @@ ${instructions}`;
|
|
|
1018
1064
|
}
|
|
1019
1065
|
this.itemDeleteFutures = {};
|
|
1020
1066
|
this.inputTranscriptAccumulators.clear();
|
|
1021
|
-
|
|
1022
|
-
this.currentGeneration = void 0;
|
|
1023
|
-
} else if (this.currentGeneration) {
|
|
1024
|
-
for (const gen of this.currentGeneration.messages.values()) {
|
|
1025
|
-
gen.textChannel.close();
|
|
1026
|
-
gen.audioChannel.close();
|
|
1027
|
-
if (!gen.modalities.done) {
|
|
1028
|
-
gen.modalities.resolve(this._options.modalities);
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
this.currentGeneration.messages.clear();
|
|
1032
|
-
this.currentGeneration.messageChannel.close();
|
|
1033
|
-
this.currentGeneration.functionChannel.close();
|
|
1034
|
-
if (!this.currentGeneration._doneFut.done) {
|
|
1035
|
-
this.currentGeneration._doneFut.resolve();
|
|
1036
|
-
}
|
|
1037
|
-
this.currentGeneration = void 0;
|
|
1038
|
-
}
|
|
1067
|
+
this.closeCurrentGeneration("Session closed");
|
|
1039
1068
|
this.messageChannel.items.length = 0;
|
|
1040
1069
|
}
|
|
1041
1070
|
handleInputAudioBufferSpeechStarted(_event) {
|
|
@@ -1453,13 +1482,18 @@ ${instructions}`;
|
|
|
1453
1482
|
const errorBody = typeof statusDetails !== "string" && (statusDetails == null ? void 0 : statusDetails.type) === "failed" ? statusDetails.error : void 0;
|
|
1454
1483
|
const errorTypeValue = errorBody && "type" in errorBody ? errorBody.type : void 0;
|
|
1455
1484
|
const errorType = typeof errorTypeValue === "string" ? errorTypeValue : "unknown";
|
|
1456
|
-
|
|
1457
|
-
|
|
1485
|
+
const recoverable = !isFatalError(errorBody);
|
|
1486
|
+
const error = new APIError(
|
|
1487
|
+
`OpenAI Realtime API response failed with error type: ${errorType}`,
|
|
1488
|
+
{
|
|
1458
1489
|
body: errorBody ?? null,
|
|
1459
|
-
retryable:
|
|
1460
|
-
}
|
|
1461
|
-
|
|
1462
|
-
|
|
1490
|
+
retryable: recoverable
|
|
1491
|
+
}
|
|
1492
|
+
);
|
|
1493
|
+
if (!recoverable) {
|
|
1494
|
+
throw error;
|
|
1495
|
+
}
|
|
1496
|
+
this.emitError({ error, recoverable: true });
|
|
1463
1497
|
} else if (event.response.status === "cancelled" || event.response.status === "incomplete") {
|
|
1464
1498
|
let statusType;
|
|
1465
1499
|
let statusReason;
|
|
@@ -1498,13 +1532,15 @@ ${instructions}`;
|
|
|
1498
1532
|
}
|
|
1499
1533
|
}
|
|
1500
1534
|
}
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
}),
|
|
1506
|
-
recoverable: true
|
|
1535
|
+
const recoverable = !isFatalError(event.error);
|
|
1536
|
+
const error = new APIError(event.error.message, {
|
|
1537
|
+
body: event.error,
|
|
1538
|
+
retryable: recoverable
|
|
1507
1539
|
});
|
|
1540
|
+
if (!recoverable) {
|
|
1541
|
+
throw error;
|
|
1542
|
+
}
|
|
1543
|
+
this.emitError({ error, recoverable: true });
|
|
1508
1544
|
}
|
|
1509
1545
|
emitError({ error, recoverable }) {
|
|
1510
1546
|
this.emit("error", {
|
|
@@ -1684,6 +1720,7 @@ function toOaiToolChoice(toolChoice) {
|
|
|
1684
1720
|
export {
|
|
1685
1721
|
RealtimeModel,
|
|
1686
1722
|
RealtimeSession,
|
|
1723
|
+
isFatalError,
|
|
1687
1724
|
livekitItemToOpenAIItem,
|
|
1688
1725
|
processBaseURL
|
|
1689
1726
|
};
|