@livekit/agents-plugin-openai 1.4.8 → 1.4.11

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.
@@ -24,10 +24,13 @@ const SAMPLE_RATE = 24e3;
24
24
  const NUM_CHANNELS = 1;
25
25
  const BASE_URL = "https://api.openai.com/v1";
26
26
  const MOCK_AUDIO_ID_PREFIX = "lk_mock_audio_item_";
27
+ class DiscardedGeneration {
28
+ discarded = true;
29
+ }
27
30
  class CreateResponseHandle {
28
31
  instructions;
29
32
  doneFut;
30
- // TODO(shubhra): add timeout
33
+ timeout;
31
34
  constructor({ instructions }) {
32
35
  this.instructions = instructions;
33
36
  this.doneFut = new Future();
@@ -275,6 +278,7 @@ class RealtimeSession extends llm.RealtimeSession {
275
278
  _options;
276
279
  currentGeneration;
277
280
  responseCreatedFutures = {};
281
+ discardedEventIds = /* @__PURE__ */ new Set();
278
282
  textModeRecoveryRetries = 0;
279
283
  itemCreateFutures = {};
280
284
  itemDeleteFutures = {};
@@ -607,7 +611,9 @@ class RealtimeSession extends llm.RealtimeSession {
607
611
  )) == null ? void 0 : _a2[0];
608
612
  if (eventId) {
609
613
  delete this.responseCreatedFutures[eventId];
614
+ this.discardedEventIds.add(eventId);
610
615
  }
616
+ if (handle.timeout) clearTimeout(handle.timeout);
611
617
  if (!handle.doneFut.done) {
612
618
  handle.doneFut.reject(new Error("generateReply aborted"));
613
619
  this.sendEvent({
@@ -754,8 +760,10 @@ class RealtimeSession extends llm.RealtimeSession {
754
760
  if (!handle.doneFut.done) {
755
761
  handle.doneFut.reject(new Error("Session reconnected"));
756
762
  }
763
+ if (handle.timeout) clearTimeout(handle.timeout);
757
764
  }
758
765
  this.responseCreatedFutures = {};
766
+ this.discardedEventIds.clear();
759
767
  this.audioCapableItemIds.clear();
760
768
  this.inputTranscriptAccumulators.clear();
761
769
  const events = [];
@@ -965,8 +973,13 @@ class RealtimeSession extends llm.RealtimeSession {
965
973
  });
966
974
  try {
967
975
  const result = await Promise.race([wsTask.result, sendTask.result, waitReconnectTask.result]);
968
- if (waitReconnectTask.done && this.currentGeneration) {
969
- await this.currentGeneration._doneFut.await;
976
+ const currentGeneration = this.currentGeneration;
977
+ if (waitReconnectTask.done && currentGeneration) {
978
+ if (currentGeneration instanceof DiscardedGeneration) {
979
+ this.currentGeneration = void 0;
980
+ } else {
981
+ await currentGeneration._doneFut.await;
982
+ }
970
983
  }
971
984
  if (result instanceof Error) {
972
985
  throw result;
@@ -984,6 +997,7 @@ class RealtimeSession extends llm.RealtimeSession {
984
997
  if (!handle.doneFut.done) {
985
998
  handle.doneFut.reject(new Error("Session closed"));
986
999
  }
1000
+ if (handle.timeout) clearTimeout(handle.timeout);
987
1001
  }
988
1002
  this.responseCreatedFutures = {};
989
1003
  for (const fut of Object.values(this.itemCreateFutures)) {
@@ -999,7 +1013,9 @@ class RealtimeSession extends llm.RealtimeSession {
999
1013
  }
1000
1014
  this.itemDeleteFutures = {};
1001
1015
  this.inputTranscriptAccumulators.clear();
1002
- if (this.currentGeneration) {
1016
+ if (this.currentGeneration instanceof DiscardedGeneration) {
1017
+ this.currentGeneration = void 0;
1018
+ } else if (this.currentGeneration) {
1003
1019
  for (const gen of this.currentGeneration.messages.values()) {
1004
1020
  gen.textChannel.close();
1005
1021
  gen.audioChannel.close();
@@ -1030,24 +1046,36 @@ class RealtimeSession extends llm.RealtimeSession {
1030
1046
  if (!event.response.id) {
1031
1047
  throw new Error("response.id is missing");
1032
1048
  }
1033
- this.currentGeneration = {
1049
+ const clientEventId = (_a = event.response.metadata) == null ? void 0 : _a.client_event_id;
1050
+ if (clientEventId && this.discardedEventIds.has(clientEventId)) {
1051
+ this.discardedEventIds.delete(clientEventId);
1052
+ this.sendEvent({
1053
+ type: "response.cancel",
1054
+ response_id: event.response.id
1055
+ });
1056
+ this.currentGeneration = new DiscardedGeneration();
1057
+ this.#logger.warn("discarding response that arrived after it was timed out or interrupted");
1058
+ return;
1059
+ }
1060
+ const currentGeneration = {
1034
1061
  messageChannel: stream.createStreamChannel(),
1035
1062
  functionChannel: stream.createStreamChannel(),
1036
1063
  messages: /* @__PURE__ */ new Map(),
1037
1064
  _doneFut: new Future(),
1038
1065
  _createdTimestamp: Date.now()
1039
1066
  };
1067
+ this.currentGeneration = currentGeneration;
1040
1068
  const generationEv = {
1041
- messageStream: this.currentGeneration.messageChannel.stream(),
1042
- functionStream: this.currentGeneration.functionChannel.stream(),
1069
+ messageStream: currentGeneration.messageChannel.stream(),
1070
+ functionStream: currentGeneration.functionChannel.stream(),
1043
1071
  userInitiated: false,
1044
1072
  responseId: event.response.id
1045
1073
  };
1046
- const clientEventId = (_a = event.response.metadata) == null ? void 0 : _a.client_event_id;
1047
1074
  if (clientEventId) {
1048
1075
  const handle = this.responseCreatedFutures[clientEventId];
1049
1076
  if (handle) {
1050
1077
  delete this.responseCreatedFutures[clientEventId];
1078
+ if (handle.timeout) clearTimeout(handle.timeout);
1051
1079
  generationEv.userInitiated = true;
1052
1080
  if (!handle.doneFut.done) {
1053
1081
  handle.doneFut.resolve(generationEv);
@@ -1057,6 +1085,7 @@ class RealtimeSession extends llm.RealtimeSession {
1057
1085
  this.emit("generation_created", generationEv);
1058
1086
  }
1059
1087
  handleResponseOutputItemAdded(event) {
1088
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1060
1089
  if (!this.currentGeneration) {
1061
1090
  throw new Error("currentGeneration is not set");
1062
1091
  }
@@ -1190,6 +1219,7 @@ class RealtimeSession extends llm.RealtimeSession {
1190
1219
  });
1191
1220
  }
1192
1221
  handleResponseContentPartAdded(event) {
1222
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1193
1223
  if (!this.currentGeneration) {
1194
1224
  throw new Error("currentGeneration is not set");
1195
1225
  }
@@ -1216,6 +1246,7 @@ class RealtimeSession extends llm.RealtimeSession {
1216
1246
  }
1217
1247
  }
1218
1248
  handleResponseContentPartDone(event) {
1249
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1219
1250
  if (!event.part) {
1220
1251
  return;
1221
1252
  }
@@ -1227,6 +1258,7 @@ class RealtimeSession extends llm.RealtimeSession {
1227
1258
  }
1228
1259
  }
1229
1260
  handleResponseTextDelta(event) {
1261
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1230
1262
  if (!this.currentGeneration) {
1231
1263
  throw new Error("currentGeneration is not set");
1232
1264
  }
@@ -1241,11 +1273,13 @@ class RealtimeSession extends llm.RealtimeSession {
1241
1273
  itemGeneration.audioTranscript += event.delta;
1242
1274
  }
1243
1275
  handleResponseTextDone(_event) {
1276
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1244
1277
  if (!this.currentGeneration) {
1245
1278
  throw new Error("currentGeneration is not set");
1246
1279
  }
1247
1280
  }
1248
1281
  handleResponseAudioTranscriptDelta(event) {
1282
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1249
1283
  if (!this.currentGeneration) {
1250
1284
  throw new Error("currentGeneration is not set");
1251
1285
  }
@@ -1263,6 +1297,7 @@ class RealtimeSession extends llm.RealtimeSession {
1263
1297
  }
1264
1298
  }
1265
1299
  handleResponseAudioDelta(event) {
1300
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1266
1301
  if (!this.currentGeneration) {
1267
1302
  throw new Error("currentGeneration is not set");
1268
1303
  }
@@ -1292,16 +1327,19 @@ class RealtimeSession extends llm.RealtimeSession {
1292
1327
  );
1293
1328
  }
1294
1329
  handleResponseAudioTranscriptDone(_event) {
1330
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1295
1331
  if (!this.currentGeneration) {
1296
1332
  throw new Error("currentGeneration is not set");
1297
1333
  }
1298
1334
  }
1299
1335
  handleResponseAudioDone(_event) {
1336
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1300
1337
  if (!this.currentGeneration) {
1301
1338
  throw new Error("currentGeneration is not set");
1302
1339
  }
1303
1340
  }
1304
1341
  handleResponseOutputItemDone(event) {
1342
+ if (this.currentGeneration instanceof DiscardedGeneration) return;
1305
1343
  if (!this.currentGeneration) {
1306
1344
  throw new Error("currentGeneration is not set");
1307
1345
  }
@@ -1333,6 +1371,10 @@ class RealtimeSession extends llm.RealtimeSession {
1333
1371
  }
1334
1372
  handleResponseDone(_event) {
1335
1373
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1374
+ if (this.currentGeneration instanceof DiscardedGeneration) {
1375
+ this.currentGeneration = void 0;
1376
+ return;
1377
+ }
1336
1378
  if (!this.currentGeneration) {
1337
1379
  return;
1338
1380
  }
@@ -1445,6 +1487,7 @@ class RealtimeSession extends llm.RealtimeSession {
1445
1487
  const handle = this.responseCreatedFutures[eventId];
1446
1488
  if (handle) {
1447
1489
  delete this.responseCreatedFutures[eventId];
1490
+ if (handle.timeout) clearTimeout(handle.timeout);
1448
1491
  if (!handle.doneFut.done) {
1449
1492
  handle.doneFut.reject(new Error(event.error.message));
1450
1493
  }
@@ -1481,8 +1524,18 @@ class RealtimeSession extends llm.RealtimeSession {
1481
1524
  handle.instructions = instructions;
1482
1525
  }
1483
1526
  const eventId = shortuuid("response_create_");
1527
+ this.discardedEventIds ??= /* @__PURE__ */ new Set();
1484
1528
  if (userInitiated) {
1485
1529
  this.responseCreatedFutures[eventId] = handle;
1530
+ handle.timeout = setTimeout(() => {
1531
+ if (this.responseCreatedFutures[eventId] === handle) {
1532
+ delete this.responseCreatedFutures[eventId];
1533
+ }
1534
+ if (!handle.doneFut.done) {
1535
+ this.discardedEventIds.add(eventId);
1536
+ handle.doneFut.reject(new Error("generateReply timed out."));
1537
+ }
1538
+ }, 1e4);
1486
1539
  }
1487
1540
  const response = {};
1488
1541
  if (instructions) response.instructions = instructions;