@livekit/agents-plugin-openai 1.4.4 → 1.4.5

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.
@@ -129,7 +129,7 @@ class RealtimeModel extends llm.RealtimeModel {
129
129
  this._options = {
130
130
  ...DEFAULT_REALTIME_MODEL_OPTIONS,
131
131
  ...optionsWithoutModalities,
132
- baseURL: options.baseURL || BASE_URL,
132
+ baseURL: options.baseURL || process.env.OPENAI_BASE_URL || BASE_URL,
133
133
  apiKey,
134
134
  isAzure,
135
135
  model: options.model || DEFAULT_REALTIME_MODEL_OPTIONS.model,
@@ -278,6 +278,7 @@ class RealtimeSession extends llm.RealtimeSession {
278
278
  textModeRecoveryRetries = 0;
279
279
  itemCreateFutures = {};
280
280
  itemDeleteFutures = {};
281
+ inputTranscriptAccumulators = /* @__PURE__ */ new Map();
281
282
  // Track items that have real server-side audio (created in current session, not restored)
282
283
  // Items restored after reconnection are text-only and cannot be truncated
283
284
  audioCapableItemIds = /* @__PURE__ */ new Set();
@@ -748,6 +749,7 @@ class RealtimeSession extends llm.RealtimeSession {
748
749
  }
749
750
  this.responseCreatedFutures = {};
750
751
  this.audioCapableItemIds.clear();
752
+ this.inputTranscriptAccumulators.clear();
751
753
  const events = [];
752
754
  events.push(this.createSessionUpdateEvent());
753
755
  if (Object.keys(this._tools).length > 0) {
@@ -883,6 +885,9 @@ class RealtimeSession extends llm.RealtimeSession {
883
885
  case "conversation.item.deleted":
884
886
  this.handleConversationItemDeleted(event);
885
887
  break;
888
+ case "conversation.item.input_audio_transcription.delta":
889
+ this.handleConversationItemInputAudioTranscriptionDelta(event);
890
+ break;
886
891
  case "conversation.item.input_audio_transcription.completed":
887
892
  this.handleConversationItemInputAudioTranscriptionCompleted(event);
888
893
  break;
@@ -985,6 +990,7 @@ class RealtimeSession extends llm.RealtimeSession {
985
990
  }
986
991
  }
987
992
  this.itemDeleteFutures = {};
993
+ this.inputTranscriptAccumulators.clear();
988
994
  if (this.currentGeneration) {
989
995
  for (const gen of this.currentGeneration.messages.values()) {
990
996
  gen.textChannel.close();
@@ -1105,6 +1111,7 @@ class RealtimeSession extends llm.RealtimeSession {
1105
1111
  throw new Error("item_id is not set");
1106
1112
  }
1107
1113
  this.audioCapableItemIds.delete(event.item_id);
1114
+ this.inputTranscriptAccumulators.delete(event.item_id);
1108
1115
  try {
1109
1116
  this.remoteChatCtx.delete(event.item_id);
1110
1117
  } catch (error) {
@@ -1116,16 +1123,40 @@ class RealtimeSession extends llm.RealtimeSession {
1116
1123
  delete this.itemDeleteFutures[event.item_id];
1117
1124
  }
1118
1125
  }
1126
+ handleConversationItemInputAudioTranscriptionDelta(event) {
1127
+ if (!event.delta) return;
1128
+ const contentIndex = event.content_index ?? 0;
1129
+ let byIndex = this.inputTranscriptAccumulators.get(event.item_id);
1130
+ if (!byIndex) {
1131
+ byIndex = /* @__PURE__ */ new Map();
1132
+ this.inputTranscriptAccumulators.set(event.item_id, byIndex);
1133
+ }
1134
+ const accumulated = (byIndex.get(contentIndex) ?? "") + event.delta;
1135
+ byIndex.set(contentIndex, accumulated);
1136
+ this.emit("input_audio_transcription_completed", {
1137
+ itemId: event.item_id,
1138
+ transcript: accumulated,
1139
+ isFinal: false
1140
+ });
1141
+ }
1142
+ clearAccumulator(itemId, contentIndex) {
1143
+ const byIndex = this.inputTranscriptAccumulators.get(itemId);
1144
+ if (!byIndex) return void 0;
1145
+ const partial = byIndex.get(contentIndex);
1146
+ byIndex.delete(contentIndex);
1147
+ if (byIndex.size === 0) this.inputTranscriptAccumulators.delete(itemId);
1148
+ return partial;
1149
+ }
1119
1150
  handleConversationItemInputAudioTranscriptionCompleted(event) {
1151
+ this.clearAccumulator(event.item_id, event.content_index ?? 0);
1120
1152
  const remoteItem = this.remoteChatCtx.get(event.item_id);
1121
- if (!remoteItem) {
1122
- return;
1123
- }
1124
- const item = remoteItem.item;
1125
- if (item instanceof llm.ChatMessage) {
1126
- item.content.push(event.transcript);
1127
- } else {
1128
- throw new Error("item is not a chat message");
1153
+ if (remoteItem) {
1154
+ const item = remoteItem.item;
1155
+ if (item instanceof llm.ChatMessage) {
1156
+ item.content.push(event.transcript);
1157
+ } else {
1158
+ throw new Error("item is not a chat message");
1159
+ }
1129
1160
  }
1130
1161
  this.emit("input_audio_transcription_completed", {
1131
1162
  itemId: event.item_id,
@@ -1138,6 +1169,17 @@ class RealtimeSession extends llm.RealtimeSession {
1138
1169
  { error: event.error },
1139
1170
  "OpenAI Realtime API failed to transcribe input audio"
1140
1171
  );
1172
+ this.finalizePartialOnTranscriptionFailure(event.item_id, event.content_index ?? 0);
1173
+ }
1174
+ // Close any open partial stream so consumers waiting for isFinal don't hang.
1175
+ finalizePartialOnTranscriptionFailure(itemId, contentIndex) {
1176
+ const partial = this.clearAccumulator(itemId, contentIndex);
1177
+ if (partial === void 0) return;
1178
+ this.emit("input_audio_transcription_completed", {
1179
+ itemId,
1180
+ transcript: partial,
1181
+ isFinal: true
1182
+ });
1141
1183
  }
1142
1184
  handleResponseContentPartAdded(event) {
1143
1185
  if (!this.currentGeneration) {
@@ -1345,6 +1387,39 @@ class RealtimeSession extends llm.RealtimeSession {
1345
1387
  }
1346
1388
  };
1347
1389
  this.emit("metrics_collected", realtimeMetrics);
1390
+ this.handleResponseDoneButNotComplete(_event);
1391
+ }
1392
+ handleResponseDoneButNotComplete(event) {
1393
+ if (event.response.status === "completed") {
1394
+ return;
1395
+ }
1396
+ const statusDetails = event.response.status_details;
1397
+ if (event.response.status === "failed") {
1398
+ const errorBody = (statusDetails == null ? void 0 : statusDetails.type) === "failed" ? statusDetails.error : void 0;
1399
+ const errorTypeValue = errorBody && "type" in errorBody ? errorBody.type : void 0;
1400
+ const errorType = typeof errorTypeValue === "string" ? errorTypeValue : "unknown";
1401
+ this.emitError({
1402
+ error: new APIError(`OpenAI Realtime API response failed with error type: ${errorType}`, {
1403
+ body: errorBody ?? null,
1404
+ retryable: true
1405
+ }),
1406
+ recoverable: true
1407
+ });
1408
+ } else if (event.response.status === "cancelled" || event.response.status === "incomplete") {
1409
+ const statusType = statusDetails == null ? void 0 : statusDetails.type;
1410
+ const statusReason = statusDetails && "reason" in statusDetails ? statusDetails.reason : void 0;
1411
+ this.#logger.debug(
1412
+ {
1413
+ eventId: event.response.id,
1414
+ eventResponseStatus: event.response.status,
1415
+ eventResponseStatusType: statusType,
1416
+ eventResponseStatusReason: statusReason
1417
+ },
1418
+ `OpenAI Realtime API response done but not complete with status: ${event.response.status} (type=${statusType}, reason=${statusReason})`
1419
+ );
1420
+ } else {
1421
+ this.#logger.debug({ eventResponseStatus: event.response.status }, "Unknown response status");
1422
+ }
1348
1423
  }
1349
1424
  handleError(event) {
1350
1425
  if (event.error.message.startsWith("Cancellation failed")) {
@@ -1371,6 +1446,7 @@ class RealtimeSession extends llm.RealtimeSession {
1371
1446
  }
1372
1447
  emitError({ error, recoverable }) {
1373
1448
  this.emit("error", {
1449
+ type: "realtime_model_error",
1374
1450
  timestamp: Date.now(),
1375
1451
  // TODO(brian): add label
1376
1452
  label: "",