@charivo/realtime 0.16.1 → 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.
- package/README.md +8 -0
- package/dist/{chunk-ILIKZUGF.mjs → chunk-EXLERHST.mjs} +50 -20
- package/dist/chunk-EXLERHST.mjs.map +1 -0
- package/dist/{chunk-PJMINYYR.mjs → chunk-U7ER2VBF.mjs} +62 -16
- package/dist/chunk-U7ER2VBF.mjs.map +1 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +52 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +52 -18
- package/dist/index.mjs.map +1 -1
- package/dist/openai/index.d.mts +1 -1
- package/dist/openai/index.d.ts +1 -1
- package/dist/openai/index.js +48 -18
- package/dist/openai/index.js.map +1 -1
- package/dist/openai/index.mjs +1 -1
- package/dist/openai-agents/index.d.mts +1 -1
- package/dist/openai-agents/index.d.ts +1 -1
- package/dist/openai-agents/index.js +60 -14
- package/dist/openai-agents/index.js.map +1 -1
- package/dist/openai-agents/index.mjs +1 -1
- package/dist/remote/index.d.mts +1 -1
- package/dist/remote/index.d.ts +1 -1
- package/dist/remote/index.js +108 -32
- package/dist/remote/index.js.map +1 -1
- package/dist/remote/index.mjs +2 -2
- package/dist/{types-DFJPDaQo.d.mts → types-D96RmHO5.d.mts} +9 -1
- package/dist/{types-DFJPDaQo.d.ts → types-D96RmHO5.d.ts} +9 -1
- package/package.json +2 -2
- package/dist/chunk-ILIKZUGF.mjs.map +0 -1
- package/dist/chunk-PJMINYYR.mjs.map +0 -1
package/dist/remote/index.js
CHANGED
|
@@ -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.
|
|
541
|
-
this.
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
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
|
-
|
|
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,12 +1171,18 @@ var OpenAIRealtimeClient = class {
|
|
|
1120
1171
|
throw new Error("Realtime transport reconnecting");
|
|
1121
1172
|
}
|
|
1122
1173
|
const dc = this.requireOpenDataChannel();
|
|
1123
|
-
if (
|
|
1124
|
-
|
|
1174
|
+
if (this.isResponseInProgress || this.activeResponseAcked) {
|
|
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;
|
|
1181
|
+
this.isResponseInProgress = false;
|
|
1182
|
+
}
|
|
1183
|
+
if (this.hasStartedAudioOutput) {
|
|
1184
|
+
dc.send(JSON.stringify({ type: "output_audio_buffer.clear" }));
|
|
1125
1185
|
}
|
|
1126
|
-
dc.send(JSON.stringify({ type: "response.cancel" }));
|
|
1127
|
-
this.isResponseInProgress = false;
|
|
1128
|
-
this.cancelInFlight = true;
|
|
1129
1186
|
}
|
|
1130
1187
|
onEvent(callback) {
|
|
1131
1188
|
this.eventCallbacks.add(callback);
|
|
@@ -1143,9 +1200,16 @@ var OpenAIRealtimeClient = class {
|
|
|
1143
1200
|
case "session.updated":
|
|
1144
1201
|
this.resolvePendingSessionUpdate();
|
|
1145
1202
|
return;
|
|
1203
|
+
case "response.created":
|
|
1204
|
+
if (this.staleCreatedsExpected > 0) {
|
|
1205
|
+
this.staleCreatedsExpected -= 1;
|
|
1206
|
+
return;
|
|
1207
|
+
}
|
|
1208
|
+
this.activeResponseAcked = true;
|
|
1209
|
+
return;
|
|
1146
1210
|
case "response.audio.delta":
|
|
1147
1211
|
case "output_audio_buffer.started":
|
|
1148
|
-
if (this.
|
|
1212
|
+
if (this.staleDonesExpected > 0) {
|
|
1149
1213
|
return;
|
|
1150
1214
|
}
|
|
1151
1215
|
if (!this.hasStartedAudioOutput) {
|
|
@@ -1164,7 +1228,7 @@ var OpenAIRealtimeClient = class {
|
|
|
1164
1228
|
case "output_audio_buffer.stopped":
|
|
1165
1229
|
case "output_audio_buffer.cleared":
|
|
1166
1230
|
this.lipSyncAnalyzer.pause();
|
|
1167
|
-
if (this.
|
|
1231
|
+
if (this.staleDonesExpected > 0) {
|
|
1168
1232
|
this.hasStartedAudioOutput = false;
|
|
1169
1233
|
return;
|
|
1170
1234
|
}
|
|
@@ -1172,13 +1236,14 @@ var OpenAIRealtimeClient = class {
|
|
|
1172
1236
|
this.hasStartedAudioOutput = false;
|
|
1173
1237
|
return;
|
|
1174
1238
|
case "response.done":
|
|
1175
|
-
this.
|
|
1176
|
-
|
|
1177
|
-
this.cancelInFlight = false;
|
|
1239
|
+
if (this.staleDonesExpected > 0) {
|
|
1240
|
+
this.staleDonesExpected -= 1;
|
|
1178
1241
|
this.hasStartedAudioOutput = false;
|
|
1179
1242
|
this.resetResponseTracking();
|
|
1180
1243
|
return;
|
|
1181
1244
|
}
|
|
1245
|
+
this.activeResponseAcked = false;
|
|
1246
|
+
this.isResponseInProgress = false;
|
|
1182
1247
|
if (!this.assistantText) {
|
|
1183
1248
|
return;
|
|
1184
1249
|
}
|
|
@@ -1194,7 +1259,7 @@ var OpenAIRealtimeClient = class {
|
|
|
1194
1259
|
case "response.audio_transcript.delta":
|
|
1195
1260
|
case "response.output_audio_transcript.delta":
|
|
1196
1261
|
case "response.output_text.delta":
|
|
1197
|
-
if (this.
|
|
1262
|
+
if (this.staleDonesExpected > 0 || !event.delta) {
|
|
1198
1263
|
return;
|
|
1199
1264
|
}
|
|
1200
1265
|
this.ensureAssistantResponseStarted();
|
|
@@ -1205,13 +1270,13 @@ var OpenAIRealtimeClient = class {
|
|
|
1205
1270
|
});
|
|
1206
1271
|
return;
|
|
1207
1272
|
case "response.output_text.done":
|
|
1208
|
-
if (this.
|
|
1273
|
+
if (this.staleDonesExpected > 0 || !event.text) {
|
|
1209
1274
|
return;
|
|
1210
1275
|
}
|
|
1211
1276
|
this.emitFinalAssistantText(event.text);
|
|
1212
1277
|
return;
|
|
1213
1278
|
case "response.output_audio_transcript.done":
|
|
1214
|
-
if (this.
|
|
1279
|
+
if (this.staleDonesExpected > 0 || !event.transcript) {
|
|
1215
1280
|
return;
|
|
1216
1281
|
}
|
|
1217
1282
|
this.emitFinalAssistantText(event.transcript);
|
|
@@ -1228,8 +1293,11 @@ var OpenAIRealtimeClient = class {
|
|
|
1228
1293
|
this.handleToolCallDone(event);
|
|
1229
1294
|
return;
|
|
1230
1295
|
case "error":
|
|
1296
|
+
if (isCancelWithoutActiveResponse2(event.error)) {
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1231
1299
|
this.isResponseInProgress = false;
|
|
1232
|
-
this.
|
|
1300
|
+
this.resetResponseSuppression();
|
|
1233
1301
|
{
|
|
1234
1302
|
const error = new Error(event.error?.message || "Unknown error");
|
|
1235
1303
|
if (this.isPendingSessionUpdateError(event)) {
|
|
@@ -1395,7 +1463,7 @@ var OpenAIRealtimeClient = class {
|
|
|
1395
1463
|
}
|
|
1396
1464
|
this.connectionLossNotified = true;
|
|
1397
1465
|
this.isResponseInProgress = false;
|
|
1398
|
-
this.
|
|
1466
|
+
this.resetResponseSuppression();
|
|
1399
1467
|
this.hasStartedAudioOutput = false;
|
|
1400
1468
|
this.resetResponseTracking();
|
|
1401
1469
|
this.lipSyncAnalyzer.stop();
|
|
@@ -1442,7 +1510,7 @@ var OpenAIRealtimeClient = class {
|
|
|
1442
1510
|
}
|
|
1443
1511
|
this.audioSender = null;
|
|
1444
1512
|
this.isResponseInProgress = false;
|
|
1445
|
-
this.
|
|
1513
|
+
this.resetResponseSuppression();
|
|
1446
1514
|
this.connectionLossNotified = false;
|
|
1447
1515
|
this.isCleaningUp = false;
|
|
1448
1516
|
this.hasStartedAudioOutput = false;
|
|
@@ -1451,7 +1519,7 @@ var OpenAIRealtimeClient = class {
|
|
|
1451
1519
|
beginResponseRequest() {
|
|
1452
1520
|
this.resetResponseTracking();
|
|
1453
1521
|
this.isResponseInProgress = true;
|
|
1454
|
-
this.
|
|
1522
|
+
this.activeResponseAcked = false;
|
|
1455
1523
|
}
|
|
1456
1524
|
requireOpenDataChannel() {
|
|
1457
1525
|
if (this.isRecovering) {
|
|
@@ -1548,6 +1616,11 @@ var OpenAIRealtimeClient = class {
|
|
|
1548
1616
|
this.assistantText = "";
|
|
1549
1617
|
this.hasStartedAssistantResponse = false;
|
|
1550
1618
|
}
|
|
1619
|
+
resetResponseSuppression() {
|
|
1620
|
+
this.staleDonesExpected = 0;
|
|
1621
|
+
this.staleCreatedsExpected = 0;
|
|
1622
|
+
this.activeResponseAcked = false;
|
|
1623
|
+
}
|
|
1551
1624
|
ensureAssistantResponseStarted() {
|
|
1552
1625
|
if (!this.hasStartedAssistantResponse) {
|
|
1553
1626
|
this.hasStartedAssistantResponse = true;
|
|
@@ -1584,6 +1657,9 @@ var OpenAIRealtimeClient = class {
|
|
|
1584
1657
|
return eventType.startsWith("response.output_text.");
|
|
1585
1658
|
}
|
|
1586
1659
|
};
|
|
1660
|
+
function isCancelWithoutActiveResponse2(error) {
|
|
1661
|
+
return error?.code === "response_cancel_not_active" || (error?.message?.toLowerCase().includes("no active response") ?? false);
|
|
1662
|
+
}
|
|
1587
1663
|
function createOpenAIRealtimeClient(options) {
|
|
1588
1664
|
return new OpenAIRealtimeClient(options);
|
|
1589
1665
|
}
|