@livekit/agents-plugin-openai 1.4.9 → 1.5.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.cjs +4 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/llm.cjs +8 -2
- package/dist/llm.cjs.map +1 -1
- package/dist/llm.d.cts +4 -3
- package/dist/llm.d.ts +4 -3
- package/dist/llm.d.ts.map +1 -1
- package/dist/llm.js +8 -2
- package/dist/llm.js.map +1 -1
- package/dist/models.cjs +12 -3
- package/dist/models.cjs.map +1 -1
- package/dist/models.d.cts +7 -1
- package/dist/models.d.ts +7 -1
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +11 -3
- package/dist/models.js.map +1 -1
- package/dist/realtime/api_proto.cjs.map +1 -1
- package/dist/realtime/api_proto.d.cts +1 -0
- package/dist/realtime/api_proto.d.ts +1 -0
- package/dist/realtime/api_proto.d.ts.map +1 -1
- package/dist/realtime/api_proto.js.map +1 -1
- package/dist/realtime/realtime_model.cjs +85 -27
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +2 -1
- package/dist/realtime/realtime_model.d.ts +2 -1
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +85 -27
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model.test.cjs +28 -0
- package/dist/realtime/realtime_model.test.cjs.map +1 -1
- package/dist/realtime/realtime_model.test.js +28 -0
- package/dist/realtime/realtime_model.test.js.map +1 -1
- package/dist/responses/llm.cjs +15 -18
- package/dist/responses/llm.cjs.map +1 -1
- package/dist/responses/llm.d.cts +4 -2
- package/dist/responses/llm.d.ts +4 -2
- package/dist/responses/llm.d.ts.map +1 -1
- package/dist/responses/llm.js +15 -18
- package/dist/responses/llm.js.map +1 -1
- package/dist/tool_utils.cjs +53 -0
- package/dist/tool_utils.cjs.map +1 -0
- package/dist/tool_utils.d.cts +4 -0
- package/dist/tool_utils.d.ts +4 -0
- package/dist/tool_utils.d.ts.map +1 -0
- package/dist/tool_utils.js +29 -0
- package/dist/tool_utils.js.map +1 -0
- package/dist/tool_utils.test.cjs +77 -0
- package/dist/tool_utils.test.cjs.map +1 -0
- package/dist/tool_utils.test.d.cts +2 -0
- package/dist/tool_utils.test.d.ts +2 -0
- package/dist/tool_utils.test.d.ts.map +1 -0
- package/dist/tool_utils.test.js +76 -0
- package/dist/tool_utils.test.js.map +1 -0
- package/dist/tools.cjs +117 -0
- package/dist/tools.cjs.map +1 -0
- package/dist/tools.d.cts +88 -0
- package/dist/tools.d.ts +88 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +90 -0
- package/dist/tools.js.map +1 -0
- package/dist/ws/llm.cjs +15 -18
- package/dist/ws/llm.cjs.map +1 -1
- package/dist/ws/llm.d.cts +5 -3
- package/dist/ws/llm.d.ts +5 -3
- package/dist/ws/llm.d.ts.map +1 -1
- package/dist/ws/llm.js +15 -18
- package/dist/ws/llm.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +1 -0
- package/src/llm.ts +16 -3
- package/src/models.ts +18 -3
- package/src/realtime/api_proto.ts +1 -0
- package/src/realtime/realtime_model.test.ts +38 -0
- package/src/realtime/realtime_model.ts +104 -28
- package/src/responses/llm.ts +27 -23
- package/src/tool_utils.test.ts +84 -0
- package/src/tool_utils.ts +46 -0
- package/src/tools.ts +166 -0
- package/src/ws/llm.ts +25 -24
|
@@ -44,10 +44,13 @@ const SAMPLE_RATE = 24e3;
|
|
|
44
44
|
const NUM_CHANNELS = 1;
|
|
45
45
|
const BASE_URL = "https://api.openai.com/v1";
|
|
46
46
|
const MOCK_AUDIO_ID_PREFIX = "lk_mock_audio_item_";
|
|
47
|
+
class DiscardedGeneration {
|
|
48
|
+
discarded = true;
|
|
49
|
+
}
|
|
47
50
|
class CreateResponseHandle {
|
|
48
51
|
instructions;
|
|
49
52
|
doneFut;
|
|
50
|
-
|
|
53
|
+
timeout;
|
|
51
54
|
constructor({ instructions }) {
|
|
52
55
|
this.instructions = instructions;
|
|
53
56
|
this.doneFut = new import_agents.Future();
|
|
@@ -283,7 +286,7 @@ function processBaseURL({
|
|
|
283
286
|
return url.toString();
|
|
284
287
|
}
|
|
285
288
|
class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
286
|
-
_tools =
|
|
289
|
+
_tools = import_agents.llm.ToolContext.empty();
|
|
287
290
|
remoteChatCtx = new import_agents.llm.RemoteChatContext();
|
|
288
291
|
messageChannel = new import_agents.Queue();
|
|
289
292
|
inputResampler;
|
|
@@ -295,6 +298,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
295
298
|
_options;
|
|
296
299
|
currentGeneration;
|
|
297
300
|
responseCreatedFutures = {};
|
|
301
|
+
discardedEventIds = /* @__PURE__ */ new Set();
|
|
298
302
|
textModeRecoveryRetries = 0;
|
|
299
303
|
itemCreateFutures = {};
|
|
300
304
|
itemDeleteFutures = {};
|
|
@@ -379,7 +383,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
379
383
|
return this.remoteChatCtx.toChatCtx();
|
|
380
384
|
}
|
|
381
385
|
get tools() {
|
|
382
|
-
return
|
|
386
|
+
return this._tools.copy();
|
|
383
387
|
}
|
|
384
388
|
async updateChatCtx(_chatCtx) {
|
|
385
389
|
const unlock = await this.updateChatCtxLock.lock();
|
|
@@ -517,34 +521,31 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
517
521
|
throw new Error("Tools are missing in the session update event");
|
|
518
522
|
}
|
|
519
523
|
const retainedToolNames = new Set(ev.session.tools.map((tool) => tool.name));
|
|
520
|
-
const
|
|
521
|
-
|
|
522
|
-
([name, tool]) => import_agents.llm.isFunctionTool(tool) && retainedToolNames.has(name)
|
|
523
|
-
)
|
|
524
|
+
const retainedEntries = _tools.tools.filter(
|
|
525
|
+
(entry) => !import_agents.llm.isFunctionTool(entry) || retainedToolNames.has(entry.name)
|
|
524
526
|
);
|
|
525
|
-
this._tools =
|
|
527
|
+
this._tools = new import_agents.llm.ToolContext(retainedEntries);
|
|
526
528
|
unlock();
|
|
527
529
|
}
|
|
528
530
|
createToolsUpdateEvent(_tools) {
|
|
529
531
|
const oaiTools = [];
|
|
530
|
-
for (const
|
|
531
|
-
if (!import_agents.llm.isFunctionTool(
|
|
532
|
-
this.#logger.error({ name, tool }, "OpenAI Realtime API doesn't support this tool type");
|
|
533
|
-
continue;
|
|
534
|
-
}
|
|
535
|
-
const { parameters: toolParameters, description } = tool;
|
|
532
|
+
for (const t of _tools.flatten()) {
|
|
533
|
+
if (!import_agents.llm.isFunctionTool(t)) continue;
|
|
536
534
|
try {
|
|
537
535
|
const parameters = import_agents.llm.toJsonSchema(
|
|
538
|
-
|
|
536
|
+
t.parameters
|
|
539
537
|
);
|
|
540
538
|
oaiTools.push({
|
|
541
|
-
name,
|
|
542
|
-
description,
|
|
539
|
+
name: t.name,
|
|
540
|
+
description: t.description,
|
|
543
541
|
parameters,
|
|
544
542
|
type: "function"
|
|
545
543
|
});
|
|
546
544
|
} catch (e) {
|
|
547
|
-
this.#logger.error(
|
|
545
|
+
this.#logger.error(
|
|
546
|
+
{ name: t.name, tool: t },
|
|
547
|
+
"OpenAI Realtime API doesn't support this tool type"
|
|
548
|
+
);
|
|
548
549
|
continue;
|
|
549
550
|
}
|
|
550
551
|
}
|
|
@@ -618,7 +619,15 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
618
619
|
}
|
|
619
620
|
async generateReply(instructions, options = {}) {
|
|
620
621
|
var _a, _b, _c;
|
|
621
|
-
|
|
622
|
+
let responseInstructions = instructions;
|
|
623
|
+
if (instructions && this.instructions) {
|
|
624
|
+
responseInstructions = `${this.instructions}
|
|
625
|
+
${instructions}`;
|
|
626
|
+
}
|
|
627
|
+
const handle = this.createResponse({
|
|
628
|
+
instructions: responseInstructions,
|
|
629
|
+
userInitiated: true
|
|
630
|
+
});
|
|
622
631
|
this.textModeRecoveryRetries = 0;
|
|
623
632
|
const onAbort = () => {
|
|
624
633
|
var _a2;
|
|
@@ -627,7 +636,9 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
627
636
|
)) == null ? void 0 : _a2[0];
|
|
628
637
|
if (eventId) {
|
|
629
638
|
delete this.responseCreatedFutures[eventId];
|
|
639
|
+
this.discardedEventIds.add(eventId);
|
|
630
640
|
}
|
|
641
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
631
642
|
if (!handle.doneFut.done) {
|
|
632
643
|
handle.doneFut.reject(new Error("generateReply aborted"));
|
|
633
644
|
this.sendEvent({
|
|
@@ -774,13 +785,15 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
774
785
|
if (!handle.doneFut.done) {
|
|
775
786
|
handle.doneFut.reject(new Error("Session reconnected"));
|
|
776
787
|
}
|
|
788
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
777
789
|
}
|
|
778
790
|
this.responseCreatedFutures = {};
|
|
791
|
+
this.discardedEventIds.clear();
|
|
779
792
|
this.audioCapableItemIds.clear();
|
|
780
793
|
this.inputTranscriptAccumulators.clear();
|
|
781
794
|
const events = [];
|
|
782
795
|
events.push(this.createSessionUpdateEvent());
|
|
783
|
-
if (Object.keys(this._tools).length > 0) {
|
|
796
|
+
if (Object.keys(this._tools.functionTools).length > 0) {
|
|
784
797
|
events.push(this.createToolsUpdateEvent(this._tools));
|
|
785
798
|
}
|
|
786
799
|
const chatCtx = this.chatCtx.copy({
|
|
@@ -985,8 +998,13 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
985
998
|
});
|
|
986
999
|
try {
|
|
987
1000
|
const result = await Promise.race([wsTask.result, sendTask.result, waitReconnectTask.result]);
|
|
988
|
-
|
|
989
|
-
|
|
1001
|
+
const currentGeneration = this.currentGeneration;
|
|
1002
|
+
if (waitReconnectTask.done && currentGeneration) {
|
|
1003
|
+
if (currentGeneration instanceof DiscardedGeneration) {
|
|
1004
|
+
this.currentGeneration = void 0;
|
|
1005
|
+
} else {
|
|
1006
|
+
await currentGeneration._doneFut.await;
|
|
1007
|
+
}
|
|
990
1008
|
}
|
|
991
1009
|
if (result instanceof Error) {
|
|
992
1010
|
throw result;
|
|
@@ -1004,6 +1022,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1004
1022
|
if (!handle.doneFut.done) {
|
|
1005
1023
|
handle.doneFut.reject(new Error("Session closed"));
|
|
1006
1024
|
}
|
|
1025
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1007
1026
|
}
|
|
1008
1027
|
this.responseCreatedFutures = {};
|
|
1009
1028
|
for (const fut of Object.values(this.itemCreateFutures)) {
|
|
@@ -1019,7 +1038,9 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1019
1038
|
}
|
|
1020
1039
|
this.itemDeleteFutures = {};
|
|
1021
1040
|
this.inputTranscriptAccumulators.clear();
|
|
1022
|
-
if (this.currentGeneration) {
|
|
1041
|
+
if (this.currentGeneration instanceof DiscardedGeneration) {
|
|
1042
|
+
this.currentGeneration = void 0;
|
|
1043
|
+
} else if (this.currentGeneration) {
|
|
1023
1044
|
for (const gen of this.currentGeneration.messages.values()) {
|
|
1024
1045
|
gen.textChannel.close();
|
|
1025
1046
|
gen.audioChannel.close();
|
|
@@ -1050,24 +1071,36 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1050
1071
|
if (!event.response.id) {
|
|
1051
1072
|
throw new Error("response.id is missing");
|
|
1052
1073
|
}
|
|
1053
|
-
|
|
1074
|
+
const clientEventId = (_a = event.response.metadata) == null ? void 0 : _a.client_event_id;
|
|
1075
|
+
if (clientEventId && this.discardedEventIds.has(clientEventId)) {
|
|
1076
|
+
this.discardedEventIds.delete(clientEventId);
|
|
1077
|
+
this.sendEvent({
|
|
1078
|
+
type: "response.cancel",
|
|
1079
|
+
response_id: event.response.id
|
|
1080
|
+
});
|
|
1081
|
+
this.currentGeneration = new DiscardedGeneration();
|
|
1082
|
+
this.#logger.warn("discarding response that arrived after it was timed out or interrupted");
|
|
1083
|
+
return;
|
|
1084
|
+
}
|
|
1085
|
+
const currentGeneration = {
|
|
1054
1086
|
messageChannel: import_agents.stream.createStreamChannel(),
|
|
1055
1087
|
functionChannel: import_agents.stream.createStreamChannel(),
|
|
1056
1088
|
messages: /* @__PURE__ */ new Map(),
|
|
1057
1089
|
_doneFut: new import_agents.Future(),
|
|
1058
1090
|
_createdTimestamp: Date.now()
|
|
1059
1091
|
};
|
|
1092
|
+
this.currentGeneration = currentGeneration;
|
|
1060
1093
|
const generationEv = {
|
|
1061
|
-
messageStream:
|
|
1062
|
-
functionStream:
|
|
1094
|
+
messageStream: currentGeneration.messageChannel.stream(),
|
|
1095
|
+
functionStream: currentGeneration.functionChannel.stream(),
|
|
1063
1096
|
userInitiated: false,
|
|
1064
1097
|
responseId: event.response.id
|
|
1065
1098
|
};
|
|
1066
|
-
const clientEventId = (_a = event.response.metadata) == null ? void 0 : _a.client_event_id;
|
|
1067
1099
|
if (clientEventId) {
|
|
1068
1100
|
const handle = this.responseCreatedFutures[clientEventId];
|
|
1069
1101
|
if (handle) {
|
|
1070
1102
|
delete this.responseCreatedFutures[clientEventId];
|
|
1103
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1071
1104
|
generationEv.userInitiated = true;
|
|
1072
1105
|
if (!handle.doneFut.done) {
|
|
1073
1106
|
handle.doneFut.resolve(generationEv);
|
|
@@ -1077,6 +1110,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1077
1110
|
this.emit("generation_created", generationEv);
|
|
1078
1111
|
}
|
|
1079
1112
|
handleResponseOutputItemAdded(event) {
|
|
1113
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1080
1114
|
if (!this.currentGeneration) {
|
|
1081
1115
|
throw new Error("currentGeneration is not set");
|
|
1082
1116
|
}
|
|
@@ -1210,6 +1244,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1210
1244
|
});
|
|
1211
1245
|
}
|
|
1212
1246
|
handleResponseContentPartAdded(event) {
|
|
1247
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1213
1248
|
if (!this.currentGeneration) {
|
|
1214
1249
|
throw new Error("currentGeneration is not set");
|
|
1215
1250
|
}
|
|
@@ -1236,6 +1271,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1236
1271
|
}
|
|
1237
1272
|
}
|
|
1238
1273
|
handleResponseContentPartDone(event) {
|
|
1274
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1239
1275
|
if (!event.part) {
|
|
1240
1276
|
return;
|
|
1241
1277
|
}
|
|
@@ -1247,6 +1283,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1247
1283
|
}
|
|
1248
1284
|
}
|
|
1249
1285
|
handleResponseTextDelta(event) {
|
|
1286
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1250
1287
|
if (!this.currentGeneration) {
|
|
1251
1288
|
throw new Error("currentGeneration is not set");
|
|
1252
1289
|
}
|
|
@@ -1261,11 +1298,13 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1261
1298
|
itemGeneration.audioTranscript += event.delta;
|
|
1262
1299
|
}
|
|
1263
1300
|
handleResponseTextDone(_event) {
|
|
1301
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1264
1302
|
if (!this.currentGeneration) {
|
|
1265
1303
|
throw new Error("currentGeneration is not set");
|
|
1266
1304
|
}
|
|
1267
1305
|
}
|
|
1268
1306
|
handleResponseAudioTranscriptDelta(event) {
|
|
1307
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1269
1308
|
if (!this.currentGeneration) {
|
|
1270
1309
|
throw new Error("currentGeneration is not set");
|
|
1271
1310
|
}
|
|
@@ -1283,6 +1322,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1283
1322
|
}
|
|
1284
1323
|
}
|
|
1285
1324
|
handleResponseAudioDelta(event) {
|
|
1325
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1286
1326
|
if (!this.currentGeneration) {
|
|
1287
1327
|
throw new Error("currentGeneration is not set");
|
|
1288
1328
|
}
|
|
@@ -1312,16 +1352,19 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1312
1352
|
);
|
|
1313
1353
|
}
|
|
1314
1354
|
handleResponseAudioTranscriptDone(_event) {
|
|
1355
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1315
1356
|
if (!this.currentGeneration) {
|
|
1316
1357
|
throw new Error("currentGeneration is not set");
|
|
1317
1358
|
}
|
|
1318
1359
|
}
|
|
1319
1360
|
handleResponseAudioDone(_event) {
|
|
1361
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1320
1362
|
if (!this.currentGeneration) {
|
|
1321
1363
|
throw new Error("currentGeneration is not set");
|
|
1322
1364
|
}
|
|
1323
1365
|
}
|
|
1324
1366
|
handleResponseOutputItemDone(event) {
|
|
1367
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1325
1368
|
if (!this.currentGeneration) {
|
|
1326
1369
|
throw new Error("currentGeneration is not set");
|
|
1327
1370
|
}
|
|
@@ -1353,6 +1396,10 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1353
1396
|
}
|
|
1354
1397
|
handleResponseDone(_event) {
|
|
1355
1398
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
1399
|
+
if (this.currentGeneration instanceof DiscardedGeneration) {
|
|
1400
|
+
this.currentGeneration = void 0;
|
|
1401
|
+
return;
|
|
1402
|
+
}
|
|
1356
1403
|
if (!this.currentGeneration) {
|
|
1357
1404
|
return;
|
|
1358
1405
|
}
|
|
@@ -1465,6 +1512,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1465
1512
|
const handle = this.responseCreatedFutures[eventId];
|
|
1466
1513
|
if (handle) {
|
|
1467
1514
|
delete this.responseCreatedFutures[eventId];
|
|
1515
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1468
1516
|
if (!handle.doneFut.done) {
|
|
1469
1517
|
handle.doneFut.reject(new Error(event.error.message));
|
|
1470
1518
|
}
|
|
@@ -1501,8 +1549,18 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
|
|
|
1501
1549
|
handle.instructions = instructions;
|
|
1502
1550
|
}
|
|
1503
1551
|
const eventId = (0, import_agents.shortuuid)("response_create_");
|
|
1552
|
+
this.discardedEventIds ??= /* @__PURE__ */ new Set();
|
|
1504
1553
|
if (userInitiated) {
|
|
1505
1554
|
this.responseCreatedFutures[eventId] = handle;
|
|
1555
|
+
handle.timeout = setTimeout(() => {
|
|
1556
|
+
if (this.responseCreatedFutures[eventId] === handle) {
|
|
1557
|
+
delete this.responseCreatedFutures[eventId];
|
|
1558
|
+
}
|
|
1559
|
+
if (!handle.doneFut.done) {
|
|
1560
|
+
this.discardedEventIds.add(eventId);
|
|
1561
|
+
handle.doneFut.reject(new Error("generateReply timed out."));
|
|
1562
|
+
}
|
|
1563
|
+
}, 1e4);
|
|
1506
1564
|
}
|
|
1507
1565
|
const response = {};
|
|
1508
1566
|
if (instructions) response.instructions = instructions;
|