@livekit/agents-plugin-openai 1.4.4 → 1.4.6

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.
Files changed (42) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/realtime/api_proto.cjs.map +1 -1
  4. package/dist/realtime/api_proto.d.cts +20 -3
  5. package/dist/realtime/api_proto.d.ts +20 -3
  6. package/dist/realtime/api_proto.d.ts.map +1 -1
  7. package/dist/realtime/api_proto.js.map +1 -1
  8. package/dist/realtime/realtime_model.cjs +100 -11
  9. package/dist/realtime/realtime_model.cjs.map +1 -1
  10. package/dist/realtime/realtime_model.d.cts +7 -0
  11. package/dist/realtime/realtime_model.d.ts +7 -0
  12. package/dist/realtime/realtime_model.d.ts.map +1 -1
  13. package/dist/realtime/realtime_model.js +100 -11
  14. package/dist/realtime/realtime_model.js.map +1 -1
  15. package/dist/realtime/realtime_model.test.cjs +252 -9
  16. package/dist/realtime/realtime_model.test.cjs.map +1 -1
  17. package/dist/realtime/realtime_model.test.js +253 -10
  18. package/dist/realtime/realtime_model.test.js.map +1 -1
  19. package/dist/responses/llm.cjs +12 -1
  20. package/dist/responses/llm.cjs.map +1 -1
  21. package/dist/responses/llm.d.ts.map +1 -1
  22. package/dist/responses/llm.js +12 -1
  23. package/dist/responses/llm.js.map +1 -1
  24. package/dist/ws/llm.cjs +10 -1
  25. package/dist/ws/llm.cjs.map +1 -1
  26. package/dist/ws/llm.d.ts.map +1 -1
  27. package/dist/ws/llm.js +10 -1
  28. package/dist/ws/llm.js.map +1 -1
  29. package/dist/ws/types.cjs +1 -1
  30. package/dist/ws/types.cjs.map +1 -1
  31. package/dist/ws/types.d.cts +3 -0
  32. package/dist/ws/types.d.ts +3 -0
  33. package/dist/ws/types.d.ts.map +1 -1
  34. package/dist/ws/types.js +1 -1
  35. package/dist/ws/types.js.map +1 -1
  36. package/package.json +7 -7
  37. package/src/realtime/api_proto.ts +23 -0
  38. package/src/realtime/realtime_model.test.ts +339 -12
  39. package/src/realtime/realtime_model.ts +121 -13
  40. package/src/responses/llm.ts +14 -1
  41. package/src/ws/llm.ts +10 -1
  42. package/src/ws/types.ts +1 -1
@@ -149,7 +149,7 @@ class RealtimeModel extends import_agents.llm.RealtimeModel {
149
149
  this._options = {
150
150
  ...DEFAULT_REALTIME_MODEL_OPTIONS,
151
151
  ...optionsWithoutModalities,
152
- baseURL: options.baseURL || BASE_URL,
152
+ baseURL: options.baseURL || process.env.OPENAI_BASE_URL || BASE_URL,
153
153
  apiKey,
154
154
  isAzure,
155
155
  model: options.model || DEFAULT_REALTIME_MODEL_OPTIONS.model,
@@ -298,6 +298,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
298
298
  textModeRecoveryRetries = 0;
299
299
  itemCreateFutures = {};
300
300
  itemDeleteFutures = {};
301
+ inputTranscriptAccumulators = /* @__PURE__ */ new Map();
301
302
  // Track items that have real server-side audio (created in current session, not restored)
302
303
  // Items restored after reconnection are text-only and cannot be truncated
303
304
  audioCapableItemIds = /* @__PURE__ */ new Set();
@@ -346,6 +347,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
346
347
  }
347
348
  const audioFormat = { type: "audio/pcm", rate: SAMPLE_RATE };
348
349
  const modality = opts.modalities.includes("audio") ? "audio" : "text";
350
+ const includeReasoning = opts.reasoning && opts.model.startsWith("gpt-realtime-2");
349
351
  return {
350
352
  type: "session.update",
351
353
  session: {
@@ -368,7 +370,8 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
368
370
  max_output_tokens: maxOutputTokens,
369
371
  tool_choice: toOaiToolChoice(opts.toolChoice),
370
372
  tracing: opts.tracing,
371
- instructions: this.instructions
373
+ instructions: this.instructions,
374
+ ...includeReasoning ? { reasoning: opts.reasoning } : {}
372
375
  }
373
376
  };
374
377
  }
@@ -479,7 +482,12 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
479
482
  );
480
483
  }
481
484
  const events = [];
482
- const diffOps = import_agents.llm.computeChatCtxDiff(this.chatCtx, newChatCtx);
485
+ const remoteCtx = this.chatCtx;
486
+ const remoteIds = new Set(remoteCtx.items.map((item) => item.id));
487
+ newChatCtx.items = newChatCtx.items.filter(
488
+ (item) => item.type !== "message" || item.content.length > 0 || remoteIds.has(item.id)
489
+ );
490
+ const diffOps = import_agents.llm.computeChatCtxDiff(remoteCtx, newChatCtx);
483
491
  for (const op of diffOps.toRemove) {
484
492
  events.push({
485
493
  type: "conversation.item.delete",
@@ -768,6 +776,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
768
776
  }
769
777
  this.responseCreatedFutures = {};
770
778
  this.audioCapableItemIds.clear();
779
+ this.inputTranscriptAccumulators.clear();
771
780
  const events = [];
772
781
  events.push(this.createSessionUpdateEvent());
773
782
  if (Object.keys(this._tools).length > 0) {
@@ -903,6 +912,9 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
903
912
  case "conversation.item.deleted":
904
913
  this.handleConversationItemDeleted(event);
905
914
  break;
915
+ case "conversation.item.input_audio_transcription.delta":
916
+ this.handleConversationItemInputAudioTranscriptionDelta(event);
917
+ break;
906
918
  case "conversation.item.input_audio_transcription.completed":
907
919
  this.handleConversationItemInputAudioTranscriptionCompleted(event);
908
920
  break;
@@ -1005,6 +1017,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
1005
1017
  }
1006
1018
  }
1007
1019
  this.itemDeleteFutures = {};
1020
+ this.inputTranscriptAccumulators.clear();
1008
1021
  if (this.currentGeneration) {
1009
1022
  for (const gen of this.currentGeneration.messages.values()) {
1010
1023
  gen.textChannel.close();
@@ -1125,6 +1138,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
1125
1138
  throw new Error("item_id is not set");
1126
1139
  }
1127
1140
  this.audioCapableItemIds.delete(event.item_id);
1141
+ this.inputTranscriptAccumulators.delete(event.item_id);
1128
1142
  try {
1129
1143
  this.remoteChatCtx.delete(event.item_id);
1130
1144
  } catch (error) {
@@ -1136,16 +1150,40 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
1136
1150
  delete this.itemDeleteFutures[event.item_id];
1137
1151
  }
1138
1152
  }
1153
+ handleConversationItemInputAudioTranscriptionDelta(event) {
1154
+ if (!event.delta) return;
1155
+ const contentIndex = event.content_index ?? 0;
1156
+ let byIndex = this.inputTranscriptAccumulators.get(event.item_id);
1157
+ if (!byIndex) {
1158
+ byIndex = /* @__PURE__ */ new Map();
1159
+ this.inputTranscriptAccumulators.set(event.item_id, byIndex);
1160
+ }
1161
+ const accumulated = (byIndex.get(contentIndex) ?? "") + event.delta;
1162
+ byIndex.set(contentIndex, accumulated);
1163
+ this.emit("input_audio_transcription_completed", {
1164
+ itemId: event.item_id,
1165
+ transcript: accumulated,
1166
+ isFinal: false
1167
+ });
1168
+ }
1169
+ clearAccumulator(itemId, contentIndex) {
1170
+ const byIndex = this.inputTranscriptAccumulators.get(itemId);
1171
+ if (!byIndex) return void 0;
1172
+ const partial = byIndex.get(contentIndex);
1173
+ byIndex.delete(contentIndex);
1174
+ if (byIndex.size === 0) this.inputTranscriptAccumulators.delete(itemId);
1175
+ return partial;
1176
+ }
1139
1177
  handleConversationItemInputAudioTranscriptionCompleted(event) {
1178
+ this.clearAccumulator(event.item_id, event.content_index ?? 0);
1140
1179
  const remoteItem = this.remoteChatCtx.get(event.item_id);
1141
- if (!remoteItem) {
1142
- return;
1143
- }
1144
- const item = remoteItem.item;
1145
- if (item instanceof import_agents.llm.ChatMessage) {
1146
- item.content.push(event.transcript);
1147
- } else {
1148
- throw new Error("item is not a chat message");
1180
+ if (remoteItem) {
1181
+ const item = remoteItem.item;
1182
+ if (item instanceof import_agents.llm.ChatMessage) {
1183
+ item.content.push(event.transcript);
1184
+ } else {
1185
+ throw new Error("item is not a chat message");
1186
+ }
1149
1187
  }
1150
1188
  this.emit("input_audio_transcription_completed", {
1151
1189
  itemId: event.item_id,
@@ -1158,6 +1196,17 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
1158
1196
  { error: event.error },
1159
1197
  "OpenAI Realtime API failed to transcribe input audio"
1160
1198
  );
1199
+ this.finalizePartialOnTranscriptionFailure(event.item_id, event.content_index ?? 0);
1200
+ }
1201
+ // Close any open partial stream so consumers waiting for isFinal don't hang.
1202
+ finalizePartialOnTranscriptionFailure(itemId, contentIndex) {
1203
+ const partial = this.clearAccumulator(itemId, contentIndex);
1204
+ if (partial === void 0) return;
1205
+ this.emit("input_audio_transcription_completed", {
1206
+ itemId,
1207
+ transcript: partial,
1208
+ isFinal: true
1209
+ });
1161
1210
  }
1162
1211
  handleResponseContentPartAdded(event) {
1163
1212
  if (!this.currentGeneration) {
@@ -1365,6 +1414,45 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
1365
1414
  }
1366
1415
  };
1367
1416
  this.emit("metrics_collected", realtimeMetrics);
1417
+ this.handleResponseDoneButNotComplete(_event);
1418
+ }
1419
+ handleResponseDoneButNotComplete(event) {
1420
+ if (event.response.status === "completed") {
1421
+ return;
1422
+ }
1423
+ const statusDetails = event.response.status_details;
1424
+ if (event.response.status === "failed") {
1425
+ const errorBody = typeof statusDetails !== "string" && (statusDetails == null ? void 0 : statusDetails.type) === "failed" ? statusDetails.error : void 0;
1426
+ const errorTypeValue = errorBody && "type" in errorBody ? errorBody.type : void 0;
1427
+ const errorType = typeof errorTypeValue === "string" ? errorTypeValue : "unknown";
1428
+ this.emitError({
1429
+ error: new import_agents.APIError(`OpenAI Realtime API response failed with error type: ${errorType}`, {
1430
+ body: errorBody ?? null,
1431
+ retryable: true
1432
+ }),
1433
+ recoverable: true
1434
+ });
1435
+ } else if (event.response.status === "cancelled" || event.response.status === "incomplete") {
1436
+ let statusType;
1437
+ let statusReason;
1438
+ if (typeof statusDetails === "string") {
1439
+ statusType = statusDetails;
1440
+ } else {
1441
+ statusType = statusDetails == null ? void 0 : statusDetails.type;
1442
+ statusReason = statusDetails && "reason" in statusDetails ? statusDetails.reason : void 0;
1443
+ }
1444
+ this.#logger.debug(
1445
+ {
1446
+ eventId: event.response.id,
1447
+ eventResponseStatus: event.response.status,
1448
+ eventResponseStatusType: statusType,
1449
+ eventResponseStatusReason: statusReason
1450
+ },
1451
+ `OpenAI Realtime API response done but not complete with status: ${event.response.status} (type=${statusType}, reason=${statusReason})`
1452
+ );
1453
+ } else {
1454
+ this.#logger.debug({ eventResponseStatus: event.response.status }, "Unknown response status");
1455
+ }
1368
1456
  }
1369
1457
  handleError(event) {
1370
1458
  if (event.error.message.startsWith("Cancellation failed")) {
@@ -1391,6 +1479,7 @@ class RealtimeSession extends import_agents.llm.RealtimeSession {
1391
1479
  }
1392
1480
  emitError({ error, recoverable }) {
1393
1481
  this.emit("error", {
1482
+ type: "realtime_model_error",
1394
1483
  timestamp: Date.now(),
1395
1484
  // TODO(brian): add label
1396
1485
  label: "",