@charivo/realtime 0.17.0 → 0.18.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.
@@ -348,6 +348,13 @@ var OpenAIRealtimeAgentsClient = class {
348
348
  latestAssistantText = "";
349
349
  /** Whether the response that just finished left a tool follow-up outstanding. */
350
350
  expectsToolFollowUp = false;
351
+ /** Whether a `response.created` has arrived without its `response.done`. */
352
+ responseActive = false;
353
+ /** Marked at interrupt: the SDK only cancels an acknowledged response. */
354
+ condemnedActiveResponse = false;
355
+ // `agent_end` carries no identity, so its message's `response.done` marks the
356
+ // one to swallow. Never cleared in resetAssistantTracking(): every send calls it.
357
+ suppressStaleAgentEnd = false;
351
358
  currentSessionConfig;
352
359
  teardownTransportLifecycle;
353
360
  iceDisconnectDebouncer = createIceDisconnectDebouncer(() => {
@@ -482,6 +489,9 @@ var OpenAIRealtimeAgentsClient = class {
482
489
  throw new Error("Realtime session not active");
483
490
  }
484
491
  this.session.interrupt();
492
+ if (this.responseActive) {
493
+ this.condemnedActiveResponse = true;
494
+ }
485
495
  this.assistant.started = false;
486
496
  }
487
497
  onEvent(callback) {
@@ -512,6 +522,11 @@ var OpenAIRealtimeAgentsClient = class {
512
522
  this.latestAssistantText = this.extractLatestAssistantText(history);
513
523
  });
514
524
  session.on("error", ({ error }) => {
525
+ if (isCancelWithoutActiveResponse(error)) {
526
+ return;
527
+ }
528
+ this.responseActive = false;
529
+ this.condemnedActiveResponse = false;
515
530
  this.emitEvent({
516
531
  type: "error",
517
532
  error: error instanceof Error ? error : new Error(String(error))
@@ -537,15 +552,28 @@ var OpenAIRealtimeAgentsClient = class {
537
552
  text: event.transcript
538
553
  });
539
554
  }
540
- if (event.type === "response.done" && isRecord(event.response)) {
541
- this.expectsToolFollowUp = Array.isArray(event.response.output) ? event.response.output.some(
542
- (item) => isRecord(item) && item.type === "function_call"
543
- ) : false;
544
- this.assistantCompletionMetadata = {
545
- usage: isRecord(event.response.usage) ? event.response.usage : void 0,
546
- model: typeof event.response.model === "string" ? event.response.model : void 0,
547
- responseId: typeof event.response.id === "string" ? event.response.id : void 0
548
- };
555
+ if (event.type === "response.created") {
556
+ this.responseActive = true;
557
+ this.suppressStaleAgentEnd = false;
558
+ return;
559
+ }
560
+ if (event.type === "response.done") {
561
+ this.responseActive = false;
562
+ if (this.condemnedActiveResponse) {
563
+ this.condemnedActiveResponse = false;
564
+ this.suppressStaleAgentEnd = true;
565
+ return;
566
+ }
567
+ if (isRecord(event.response)) {
568
+ this.expectsToolFollowUp = Array.isArray(event.response.output) ? event.response.output.some(
569
+ (item) => isRecord(item) && item.type === "function_call"
570
+ ) : false;
571
+ this.assistantCompletionMetadata = {
572
+ usage: isRecord(event.response.usage) ? event.response.usage : void 0,
573
+ model: typeof event.response.model === "string" ? event.response.model : void 0,
574
+ responseId: typeof event.response.id === "string" ? event.response.id : void 0
575
+ };
576
+ }
549
577
  }
550
578
  });
551
579
  }
@@ -565,6 +593,9 @@ var OpenAIRealtimeAgentsClient = class {
565
593
  if (!event.delta) {
566
594
  return;
567
595
  }
596
+ if (this.condemnedActiveResponse) {
597
+ return;
598
+ }
568
599
  this.ensureAssistantStarted();
569
600
  this.assistant.text += event.delta;
570
601
  this.emitEvent({
@@ -572,11 +603,7 @@ var OpenAIRealtimeAgentsClient = class {
572
603
  text: event.delta
573
604
  });
574
605
  }
575
- /**
576
- * Open a playback segment: resume analysis and report the start. Idempotent,
577
- * so the WebSocket transport's `audio_start` and WebRTC's
578
- * `output_audio_buffer.started` can both feed it without double-reporting.
579
- */
606
+ /** Idempotent, so both transports' start signals can feed it. */
580
607
  openAudioOutputSegment() {
581
608
  if (this.audioOutputActive) {
582
609
  return;
@@ -593,6 +620,11 @@ var OpenAIRealtimeAgentsClient = class {
593
620
  this.emitEvent({ type: "assistant.response.started" });
594
621
  }
595
622
  finalizeAssistantResponse(output) {
623
+ if (this.suppressStaleAgentEnd) {
624
+ this.suppressStaleAgentEnd = false;
625
+ this.resetAssistantTracking();
626
+ return;
627
+ }
596
628
  const isEmptyTurn = !this.assistant.text && !output.trim();
597
629
  if (isEmptyTurn && this.expectsToolFollowUp) {
598
630
  return;
@@ -809,6 +841,7 @@ var OpenAIRealtimeAgentsClient = class {
809
841
  this.cleanupPendingToolCalls(
810
842
  new Error("Realtime session interrupted during reconnect")
811
843
  );
844
+ this.resetResponseSuppression();
812
845
  this.resetAssistantTracking();
813
846
  this.lipSyncAnalyzer.stop();
814
847
  this.emitEvent({
@@ -858,8 +891,14 @@ var OpenAIRealtimeAgentsClient = class {
858
891
  this.latestAssistantText = "";
859
892
  this.connectionLossNotified = false;
860
893
  this.isCleaningUp = false;
894
+ this.resetResponseSuppression();
861
895
  this.resetAssistantTracking();
862
896
  }
897
+ resetResponseSuppression() {
898
+ this.responseActive = false;
899
+ this.condemnedActiveResponse = false;
900
+ this.suppressStaleAgentEnd = false;
901
+ }
863
902
  resetAssistantTracking() {
864
903
  this.assistant = {
865
904
  text: "",
@@ -893,6 +932,13 @@ var OpenAIRealtimeAgentsClient = class {
893
932
  console.debug("[charivo/realtime/openai-agents]", ...args);
894
933
  }
895
934
  };
935
+ function isCancelWithoutActiveResponse(error) {
936
+ if (!isRecord(error) || !isRecord(error.error)) {
937
+ return false;
938
+ }
939
+ const serverError = error.error;
940
+ return serverError.code === "response_cancel_not_active" || typeof serverError.message === "string" && serverError.message.toLowerCase().includes("no active response");
941
+ }
896
942
  function createOpenAIRealtimeAgentsClient(options) {
897
943
  return new OpenAIRealtimeAgentsClient(options);
898
944
  }
@@ -929,7 +975,12 @@ var OpenAIRealtimeClient = class {
929
975
  onError: (error) => console.error("Failed to setup audio analysis:", error)
930
976
  });
931
977
  isResponseInProgress = false;
932
- cancelInFlight = false;
978
+ // A count, not a flag: a flag was overwritten by the next interrupt cycle.
979
+ // Every condemned response reports, so they drain in order; zero means live.
980
+ staleDonesExpected = 0;
981
+ staleCreatedsExpected = 0;
982
+ /** Whether a `response.created` has arrived without its `response.done`. */
983
+ activeResponseAcked = false;
933
984
  hasStartedAssistantResponse = false;
934
985
  hasStartedAudioOutput = false;
935
986
  assistantText = "";
@@ -1120,10 +1171,14 @@ var OpenAIRealtimeClient = class {
1120
1171
  throw new Error("Realtime transport reconnecting");
1121
1172
  }
1122
1173
  const dc = this.requireOpenDataChannel();
1123
- if (this.isResponseInProgress) {
1174
+ if (this.isResponseInProgress || this.activeResponseAcked) {
1124
1175
  dc.send(JSON.stringify({ type: "response.cancel" }));
1176
+ this.staleDonesExpected += 1;
1177
+ if (!this.activeResponseAcked) {
1178
+ this.staleCreatedsExpected += 1;
1179
+ }
1180
+ this.activeResponseAcked = false;
1125
1181
  this.isResponseInProgress = false;
1126
- this.cancelInFlight = true;
1127
1182
  }
1128
1183
  if (this.hasStartedAudioOutput) {
1129
1184
  dc.send(JSON.stringify({ type: "output_audio_buffer.clear" }));
@@ -1145,9 +1200,16 @@ var OpenAIRealtimeClient = class {
1145
1200
  case "session.updated":
1146
1201
  this.resolvePendingSessionUpdate();
1147
1202
  return;
1203
+ case "response.created":
1204
+ if (this.staleCreatedsExpected > 0) {
1205
+ this.staleCreatedsExpected -= 1;
1206
+ return;
1207
+ }
1208
+ this.activeResponseAcked = true;
1209
+ return;
1148
1210
  case "response.audio.delta":
1149
1211
  case "output_audio_buffer.started":
1150
- if (this.cancelInFlight) {
1212
+ if (this.staleDonesExpected > 0) {
1151
1213
  return;
1152
1214
  }
1153
1215
  if (!this.hasStartedAudioOutput) {
@@ -1166,7 +1228,7 @@ var OpenAIRealtimeClient = class {
1166
1228
  case "output_audio_buffer.stopped":
1167
1229
  case "output_audio_buffer.cleared":
1168
1230
  this.lipSyncAnalyzer.pause();
1169
- if (this.cancelInFlight) {
1231
+ if (this.staleDonesExpected > 0) {
1170
1232
  this.hasStartedAudioOutput = false;
1171
1233
  return;
1172
1234
  }
@@ -1174,13 +1236,14 @@ var OpenAIRealtimeClient = class {
1174
1236
  this.hasStartedAudioOutput = false;
1175
1237
  return;
1176
1238
  case "response.done":
1177
- this.isResponseInProgress = false;
1178
- if (this.cancelInFlight) {
1179
- this.cancelInFlight = false;
1239
+ if (this.staleDonesExpected > 0) {
1240
+ this.staleDonesExpected -= 1;
1180
1241
  this.hasStartedAudioOutput = false;
1181
1242
  this.resetResponseTracking();
1182
1243
  return;
1183
1244
  }
1245
+ this.activeResponseAcked = false;
1246
+ this.isResponseInProgress = false;
1184
1247
  if (!this.assistantText) {
1185
1248
  return;
1186
1249
  }
@@ -1196,7 +1259,7 @@ var OpenAIRealtimeClient = class {
1196
1259
  case "response.audio_transcript.delta":
1197
1260
  case "response.output_audio_transcript.delta":
1198
1261
  case "response.output_text.delta":
1199
- if (this.cancelInFlight || !event.delta) {
1262
+ if (this.staleDonesExpected > 0 || !event.delta) {
1200
1263
  return;
1201
1264
  }
1202
1265
  this.ensureAssistantResponseStarted();
@@ -1207,13 +1270,13 @@ var OpenAIRealtimeClient = class {
1207
1270
  });
1208
1271
  return;
1209
1272
  case "response.output_text.done":
1210
- if (this.cancelInFlight || !event.text) {
1273
+ if (this.staleDonesExpected > 0 || !event.text) {
1211
1274
  return;
1212
1275
  }
1213
1276
  this.emitFinalAssistantText(event.text);
1214
1277
  return;
1215
1278
  case "response.output_audio_transcript.done":
1216
- if (this.cancelInFlight || !event.transcript) {
1279
+ if (this.staleDonesExpected > 0 || !event.transcript) {
1217
1280
  return;
1218
1281
  }
1219
1282
  this.emitFinalAssistantText(event.transcript);
@@ -1230,8 +1293,11 @@ var OpenAIRealtimeClient = class {
1230
1293
  this.handleToolCallDone(event);
1231
1294
  return;
1232
1295
  case "error":
1296
+ if (isCancelWithoutActiveResponse2(event.error)) {
1297
+ return;
1298
+ }
1233
1299
  this.isResponseInProgress = false;
1234
- this.cancelInFlight = false;
1300
+ this.resetResponseSuppression();
1235
1301
  {
1236
1302
  const error = new Error(event.error?.message || "Unknown error");
1237
1303
  if (this.isPendingSessionUpdateError(event)) {
@@ -1397,7 +1463,7 @@ var OpenAIRealtimeClient = class {
1397
1463
  }
1398
1464
  this.connectionLossNotified = true;
1399
1465
  this.isResponseInProgress = false;
1400
- this.cancelInFlight = false;
1466
+ this.resetResponseSuppression();
1401
1467
  this.hasStartedAudioOutput = false;
1402
1468
  this.resetResponseTracking();
1403
1469
  this.lipSyncAnalyzer.stop();
@@ -1444,7 +1510,7 @@ var OpenAIRealtimeClient = class {
1444
1510
  }
1445
1511
  this.audioSender = null;
1446
1512
  this.isResponseInProgress = false;
1447
- this.cancelInFlight = false;
1513
+ this.resetResponseSuppression();
1448
1514
  this.connectionLossNotified = false;
1449
1515
  this.isCleaningUp = false;
1450
1516
  this.hasStartedAudioOutput = false;
@@ -1453,7 +1519,7 @@ var OpenAIRealtimeClient = class {
1453
1519
  beginResponseRequest() {
1454
1520
  this.resetResponseTracking();
1455
1521
  this.isResponseInProgress = true;
1456
- this.cancelInFlight = false;
1522
+ this.activeResponseAcked = false;
1457
1523
  }
1458
1524
  requireOpenDataChannel() {
1459
1525
  if (this.isRecovering) {
@@ -1550,6 +1616,11 @@ var OpenAIRealtimeClient = class {
1550
1616
  this.assistantText = "";
1551
1617
  this.hasStartedAssistantResponse = false;
1552
1618
  }
1619
+ resetResponseSuppression() {
1620
+ this.staleDonesExpected = 0;
1621
+ this.staleCreatedsExpected = 0;
1622
+ this.activeResponseAcked = false;
1623
+ }
1553
1624
  ensureAssistantResponseStarted() {
1554
1625
  if (!this.hasStartedAssistantResponse) {
1555
1626
  this.hasStartedAssistantResponse = true;
@@ -1586,6 +1657,9 @@ var OpenAIRealtimeClient = class {
1586
1657
  return eventType.startsWith("response.output_text.");
1587
1658
  }
1588
1659
  };
1660
+ function isCancelWithoutActiveResponse2(error) {
1661
+ return error?.code === "response_cancel_not_active" || (error?.message?.toLowerCase().includes("no active response") ?? false);
1662
+ }
1589
1663
  function createOpenAIRealtimeClient(options) {
1590
1664
  return new OpenAIRealtimeClient(options);
1591
1665
  }