@livekit/agents-plugin-openai 1.6.1 → 1.6.3
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/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/llm.cjs +4 -1
- package/dist/llm.cjs.map +1 -1
- package/dist/llm.d.cts +1 -0
- package/dist/llm.d.ts +1 -0
- package/dist/llm.d.ts.map +1 -1
- package/dist/llm.js +4 -1
- package/dist/llm.js.map +1 -1
- package/dist/llm.test.cjs +80 -0
- package/dist/llm.test.cjs.map +1 -1
- package/dist/llm.test.js +61 -3
- package/dist/llm.test.js.map +1 -1
- package/dist/models.cjs.map +1 -1
- package/dist/models.d.cts +2 -1
- package/dist/models.d.ts +2 -1
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js.map +1 -1
- package/dist/realtime/api_proto.cjs.map +1 -1
- package/dist/realtime/api_proto.d.cts +3 -0
- package/dist/realtime/api_proto.d.ts +3 -0
- package/dist/realtime/api_proto.d.ts.map +1 -1
- package/dist/realtime/api_proto.js.map +1 -1
- package/dist/realtime/realtime_model.cjs +71 -17
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +37 -11
- package/dist/realtime/realtime_model.d.ts +37 -11
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +70 -17
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model.test.cjs +132 -2
- package/dist/realtime/realtime_model.test.cjs.map +1 -1
- package/dist/realtime/realtime_model.test.js +132 -2
- package/dist/realtime/realtime_model.test.js.map +1 -1
- package/dist/responses/llm.cjs +3 -1
- package/dist/responses/llm.cjs.map +1 -1
- package/dist/responses/llm.d.ts.map +1 -1
- package/dist/responses/llm.js +3 -1
- package/dist/responses/llm.js.map +1 -1
- package/dist/responses/llm.test.cjs +64 -0
- package/dist/responses/llm.test.cjs.map +1 -1
- package/dist/responses/llm.test.js +64 -0
- package/dist/responses/llm.test.js.map +1 -1
- package/dist/stt.cjs +249 -83
- package/dist/stt.cjs.map +1 -1
- package/dist/stt.d.cts +37 -10
- package/dist/stt.d.ts +37 -10
- package/dist/stt.d.ts.map +1 -1
- package/dist/stt.js +249 -82
- package/dist/stt.js.map +1 -1
- package/dist/stt.test.cjs +406 -1
- package/dist/stt.test.cjs.map +1 -1
- package/dist/stt.test.js +413 -3
- package/dist/stt.test.js.map +1 -1
- package/dist/ws/llm.cjs +13 -4
- package/dist/ws/llm.cjs.map +1 -1
- package/dist/ws/llm.d.ts.map +1 -1
- package/dist/ws/llm.js +13 -4
- package/dist/ws/llm.js.map +1 -1
- package/dist/ws/llm.test.cjs +69 -0
- package/dist/ws/llm.test.cjs.map +1 -1
- package/dist/ws/llm.test.js +71 -2
- package/dist/ws/llm.test.js.map +1 -1
- package/package.json +5 -5
- package/src/llm.test.ts +74 -3
- package/src/llm.ts +5 -1
- package/src/models.ts +9 -10
- package/src/realtime/api_proto.ts +3 -0
- package/src/realtime/realtime_model.test.ts +177 -0
- package/src/realtime/realtime_model.ts +89 -32
- package/src/responses/llm.test.ts +88 -0
- package/src/responses/llm.ts +5 -1
- package/src/stt.test.ts +466 -3
- package/src/stt.ts +331 -96
- package/src/ws/llm.test.ts +94 -2
- package/src/ws/llm.ts +13 -4
|
@@ -61,7 +61,8 @@ interface RealtimeOptions {
|
|
|
61
61
|
modalities: Modality[];
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
/** @internal */
|
|
65
|
+
export interface MessageGeneration {
|
|
65
66
|
messageId: string;
|
|
66
67
|
textChannel: stream.StreamChannel<string | TimedString>;
|
|
67
68
|
audioChannel: stream.StreamChannel<AudioFrame>;
|
|
@@ -69,7 +70,8 @@ interface MessageGeneration {
|
|
|
69
70
|
modalities: Future<('text' | 'audio')[]>;
|
|
70
71
|
}
|
|
71
72
|
|
|
72
|
-
|
|
73
|
+
/** @internal */
|
|
74
|
+
export interface ResponseGeneration {
|
|
73
75
|
messageChannel: stream.StreamChannel<llm.MessageGeneration>;
|
|
74
76
|
functionChannel: stream.StreamChannel<llm.FunctionCall>;
|
|
75
77
|
messages: Map<string, MessageGeneration>;
|
|
@@ -82,7 +84,8 @@ interface ResponseGeneration {
|
|
|
82
84
|
_firstTokenTimestamp?: number;
|
|
83
85
|
}
|
|
84
86
|
|
|
85
|
-
|
|
87
|
+
/** @internal */
|
|
88
|
+
export class DiscardedGeneration {
|
|
86
89
|
readonly discarded = true;
|
|
87
90
|
}
|
|
88
91
|
|
|
@@ -446,7 +449,7 @@ export function processBaseURL({
|
|
|
446
449
|
*/
|
|
447
450
|
export class RealtimeSession extends llm.RealtimeSession {
|
|
448
451
|
private _tools: llm.ToolContext = llm.ToolContext.empty();
|
|
449
|
-
|
|
452
|
+
protected remoteChatCtx: llm.RemoteChatContext = new llm.RemoteChatContext();
|
|
450
453
|
private messageChannel = new Queue<api_proto.ClientEvent>();
|
|
451
454
|
private inputResampler?: AudioResampler;
|
|
452
455
|
private instructions?: string;
|
|
@@ -455,7 +458,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
455
458
|
// per-session copy of options so updateOptions can diff against the session's
|
|
456
459
|
// own state instead of the shared model-level state.
|
|
457
460
|
private _options: RealtimeOptions;
|
|
458
|
-
|
|
461
|
+
protected currentGeneration?: ResponseGeneration | DiscardedGeneration;
|
|
459
462
|
private responseCreatedFutures: { [id: string]: CreateResponseHandle } = {};
|
|
460
463
|
private discardedEventIds = new Set<string>();
|
|
461
464
|
|
|
@@ -463,6 +466,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
463
466
|
|
|
464
467
|
private itemCreateFutures: { [id: string]: Future } = {};
|
|
465
468
|
private itemDeleteFutures: { [id: string]: Future } = {};
|
|
469
|
+
private chatCtxEventFutures: { [id: string]: Future } = {};
|
|
466
470
|
|
|
467
471
|
private inputTranscriptAccumulators = new Map<string, Map<number, string>>();
|
|
468
472
|
|
|
@@ -616,10 +620,9 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
616
620
|
const futures: Future<void>[] = [];
|
|
617
621
|
const ownedCreateFutures: { [id: string]: Future<void> } = {};
|
|
618
622
|
const ownedDeleteFutures: { [id: string]: Future<void> } = {};
|
|
623
|
+
const ownedEventFutures: { [id: string]: Future<void> } = {};
|
|
619
624
|
|
|
620
|
-
const
|
|
621
|
-
// remove timed-out entries so late server acks
|
|
622
|
-
// don't resolve stale futures from a previous updateChatCtx call.
|
|
625
|
+
const cleanupFutures = () => {
|
|
623
626
|
for (const [itemId, future] of Object.entries(ownedDeleteFutures)) {
|
|
624
627
|
if (this.itemDeleteFutures[itemId] === future) {
|
|
625
628
|
delete this.itemDeleteFutures[itemId];
|
|
@@ -630,11 +633,17 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
630
633
|
delete this.itemCreateFutures[itemId];
|
|
631
634
|
}
|
|
632
635
|
}
|
|
636
|
+
for (const [eventId, future] of Object.entries(ownedEventFutures)) {
|
|
637
|
+
if (this.chatCtxEventFutures[eventId] === future) {
|
|
638
|
+
delete this.chatCtxEventFutures[eventId];
|
|
639
|
+
}
|
|
640
|
+
}
|
|
633
641
|
};
|
|
634
642
|
|
|
635
643
|
for (const event of events) {
|
|
644
|
+
let future: Future<void>;
|
|
636
645
|
if (event.type === 'conversation.item.create') {
|
|
637
|
-
|
|
646
|
+
future = new Future<void>();
|
|
638
647
|
futures.push(future);
|
|
639
648
|
this.itemCreateFutures[event.item.id] = future;
|
|
640
649
|
ownedCreateFutures[event.item.id] = future;
|
|
@@ -644,10 +653,17 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
644
653
|
futures.push(existingDeleteFuture);
|
|
645
654
|
continue;
|
|
646
655
|
}
|
|
647
|
-
|
|
656
|
+
future = new Future<void>();
|
|
648
657
|
futures.push(future);
|
|
649
658
|
this.itemDeleteFutures[event.item_id] = future;
|
|
650
659
|
ownedDeleteFutures[event.item_id] = future;
|
|
660
|
+
} else {
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
if (event.event_id) {
|
|
665
|
+
this.chatCtxEventFutures[event.event_id] = future;
|
|
666
|
+
ownedEventFutures[event.event_id] = future;
|
|
651
667
|
}
|
|
652
668
|
|
|
653
669
|
this.sendEvent(event);
|
|
@@ -661,13 +677,23 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
661
677
|
// Cancel the timeout branch once futures resolve to avoid stale cleanup.
|
|
662
678
|
const timeoutController = new AbortController();
|
|
663
679
|
const timeoutPromise = delay(5000, { signal: timeoutController.signal }).then(() => {
|
|
664
|
-
cleanupTimedOutFutures();
|
|
665
680
|
throw new llm.RealtimeError('update_chat_ctx timed out.');
|
|
666
681
|
});
|
|
667
682
|
|
|
668
683
|
try {
|
|
669
|
-
await Promise.race([
|
|
684
|
+
const results = await Promise.race([
|
|
685
|
+
Promise.allSettled(futures.map((fut) => fut.await)),
|
|
686
|
+
timeoutPromise,
|
|
687
|
+
]);
|
|
688
|
+
const rejected = results.filter((result) => result.status === 'rejected');
|
|
689
|
+
if (rejected.length > 0) {
|
|
690
|
+
this.#logger.warn(
|
|
691
|
+
{ errors: rejected.map((result) => String(result.reason)) },
|
|
692
|
+
`${this.oaiRealtimeModel.label()} rejected part of a chat context update`,
|
|
693
|
+
);
|
|
694
|
+
}
|
|
670
695
|
} finally {
|
|
696
|
+
cleanupFutures();
|
|
671
697
|
if (!timeoutController.signal.aborted) {
|
|
672
698
|
timeoutController.abort();
|
|
673
699
|
}
|
|
@@ -680,7 +706,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
680
706
|
}
|
|
681
707
|
}
|
|
682
708
|
|
|
683
|
-
|
|
709
|
+
protected async createChatCtxUpdateEvents(
|
|
684
710
|
chatCtx: llm.ChatContext,
|
|
685
711
|
addMockAudio: boolean = false,
|
|
686
712
|
): Promise<(api_proto.ConversationItemCreateEvent | api_proto.ConversationItemDeleteEvent)[]> {
|
|
@@ -956,12 +982,21 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
956
982
|
const audioEndMs = Number.isFinite(_options.audioEndMs)
|
|
957
983
|
? Math.max(0, Math.floor(_options.audioEndMs))
|
|
958
984
|
: 0;
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
985
|
+
if (audioEndMs > 0) {
|
|
986
|
+
this.sendEvent({
|
|
987
|
+
type: 'conversation.item.truncate',
|
|
988
|
+
content_index: 0,
|
|
989
|
+
item_id: _options.messageId,
|
|
990
|
+
audio_end_ms: audioEndMs,
|
|
991
|
+
} as api_proto.ConversationItemTruncateEvent);
|
|
992
|
+
} else {
|
|
993
|
+
// The API rejects truncating an item when no audio was played, so delete it instead.
|
|
994
|
+
this.sendEvent({
|
|
995
|
+
type: 'conversation.item.delete',
|
|
996
|
+
item_id: _options.messageId,
|
|
997
|
+
event_id: shortuuid('chat_ctx_delete_'),
|
|
998
|
+
} as api_proto.ConversationItemDeleteEvent);
|
|
999
|
+
}
|
|
965
1000
|
} else if (_options.audioTranscript !== undefined) {
|
|
966
1001
|
// sync it to the remote chat context
|
|
967
1002
|
const chatCtx = this.chatCtx.copy();
|
|
@@ -1006,7 +1041,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1006
1041
|
return untypedEvent;
|
|
1007
1042
|
}
|
|
1008
1043
|
|
|
1009
|
-
|
|
1044
|
+
protected closeCurrentGeneration(reason: string = 'provider abandonment'): void {
|
|
1010
1045
|
if (this.currentGeneration instanceof DiscardedGeneration) {
|
|
1011
1046
|
this.currentGeneration = undefined;
|
|
1012
1047
|
return;
|
|
@@ -1134,7 +1169,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1134
1169
|
|
|
1135
1170
|
// Clear audio-capable item tracking - restored items are text-only on the server
|
|
1136
1171
|
this.audioCapableItemIds.clear();
|
|
1137
|
-
this.
|
|
1172
|
+
this.resetInputTurnState();
|
|
1138
1173
|
|
|
1139
1174
|
const events: api_proto.ClientEvent[] = [];
|
|
1140
1175
|
|
|
@@ -1253,7 +1288,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1253
1288
|
break;
|
|
1254
1289
|
}
|
|
1255
1290
|
|
|
1256
|
-
if (lkOaiDebug) {
|
|
1291
|
+
if (lkOaiDebug && event.type !== 'input_audio_buffer.append') {
|
|
1257
1292
|
this.#logger.debug(this.loggableEvent(event), `(client) -> ${event.type}`);
|
|
1258
1293
|
}
|
|
1259
1294
|
|
|
@@ -1414,6 +1449,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1414
1449
|
}
|
|
1415
1450
|
|
|
1416
1451
|
async close() {
|
|
1452
|
+
this.resetInputTurnState();
|
|
1417
1453
|
super.close();
|
|
1418
1454
|
this.#closed = true;
|
|
1419
1455
|
await this.#task;
|
|
@@ -1437,14 +1473,17 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1437
1473
|
|
|
1438
1474
|
this.inputTranscriptAccumulators.clear();
|
|
1439
1475
|
|
|
1440
|
-
// Clean up current generation if exists
|
|
1441
1476
|
this.closeCurrentGeneration('Session closed');
|
|
1442
1477
|
|
|
1443
1478
|
// Clear the message queue
|
|
1444
1479
|
this.messageChannel.items.length = 0;
|
|
1445
1480
|
}
|
|
1446
1481
|
|
|
1447
|
-
|
|
1482
|
+
protected resetInputTurnState(): void {
|
|
1483
|
+
this.inputTranscriptAccumulators.clear();
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
protected handleInputAudioBufferSpeechStarted(
|
|
1448
1487
|
_event: api_proto.InputAudioBufferSpeechStartedEvent,
|
|
1449
1488
|
): void {
|
|
1450
1489
|
this.emit('input_speech_started', {} as llm.InputSpeechStartedEvent);
|
|
@@ -1458,7 +1497,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1458
1497
|
} as llm.InputSpeechStoppedEvent);
|
|
1459
1498
|
}
|
|
1460
1499
|
|
|
1461
|
-
|
|
1500
|
+
protected handleResponseCreated(event: api_proto.ResponseCreatedEvent): void {
|
|
1462
1501
|
if (!event.response.id) {
|
|
1463
1502
|
throw new Error('response.id is missing');
|
|
1464
1503
|
}
|
|
@@ -1558,7 +1597,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1558
1597
|
this.currentGeneration.messages.set(itemId, itemGeneration);
|
|
1559
1598
|
}
|
|
1560
1599
|
|
|
1561
|
-
|
|
1600
|
+
protected handleConversationItemCreated(event: api_proto.ConversationItemCreatedEvent): void {
|
|
1562
1601
|
if (!event.item.id) {
|
|
1563
1602
|
throw new Error('item.id is not set');
|
|
1564
1603
|
}
|
|
@@ -1582,7 +1621,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1582
1621
|
|
|
1583
1622
|
const fut = pendingCreateFuture;
|
|
1584
1623
|
if (fut) {
|
|
1585
|
-
fut.resolve();
|
|
1624
|
+
if (!fut.done) fut.resolve();
|
|
1586
1625
|
delete this.itemCreateFutures[event.item.id];
|
|
1587
1626
|
}
|
|
1588
1627
|
}
|
|
@@ -1604,7 +1643,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1604
1643
|
|
|
1605
1644
|
const fut = this.itemDeleteFutures[event.item_id];
|
|
1606
1645
|
if (fut) {
|
|
1607
|
-
fut.resolve();
|
|
1646
|
+
if (!fut.done) fut.resolve();
|
|
1608
1647
|
delete this.itemDeleteFutures[event.item_id];
|
|
1609
1648
|
}
|
|
1610
1649
|
}
|
|
@@ -1639,9 +1678,18 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1639
1678
|
return partial;
|
|
1640
1679
|
}
|
|
1641
1680
|
|
|
1642
|
-
|
|
1681
|
+
protected handleConversationItemInputAudioTranscriptionCompleted(
|
|
1643
1682
|
event: api_proto.ConversationItemInputAudioTranscriptionCompletedEvent,
|
|
1644
1683
|
): void {
|
|
1684
|
+
if (event.status === 'in_progress') {
|
|
1685
|
+
this.emit('input_audio_transcription_completed', {
|
|
1686
|
+
itemId: event.item_id,
|
|
1687
|
+
transcript: event.transcript,
|
|
1688
|
+
isFinal: false,
|
|
1689
|
+
});
|
|
1690
|
+
return;
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1645
1693
|
this.clearAccumulator(event.item_id, event.content_index ?? 0);
|
|
1646
1694
|
|
|
1647
1695
|
const remoteItem = this.remoteChatCtx.get(event.item_id);
|
|
@@ -1735,7 +1783,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1735
1783
|
// TODO(shubhra): handle text mode recovery
|
|
1736
1784
|
}
|
|
1737
1785
|
|
|
1738
|
-
|
|
1786
|
+
protected handleResponseTextDelta(event: api_proto.ResponseTextDeltaEvent): void {
|
|
1739
1787
|
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1740
1788
|
|
|
1741
1789
|
if (!this.currentGeneration) {
|
|
@@ -1792,7 +1840,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1792
1840
|
}
|
|
1793
1841
|
}
|
|
1794
1842
|
|
|
1795
|
-
|
|
1843
|
+
protected handleResponseAudioDelta(event: api_proto.ResponseAudioDeltaEvent): void {
|
|
1796
1844
|
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1797
1845
|
|
|
1798
1846
|
if (!this.currentGeneration) {
|
|
@@ -2020,12 +2068,21 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
2020
2068
|
}
|
|
2021
2069
|
|
|
2022
2070
|
private handleError(event: api_proto.ErrorEvent): void {
|
|
2071
|
+
const eventId = event.error.event_id;
|
|
2072
|
+
if (eventId) {
|
|
2073
|
+
const future = this.chatCtxEventFutures[eventId];
|
|
2074
|
+
if (future) {
|
|
2075
|
+
delete this.chatCtxEventFutures[eventId];
|
|
2076
|
+
if (!future.done) future.reject(new Error(event.error.message));
|
|
2077
|
+
if (!isFatalError(event.error)) return;
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2023
2081
|
if (event.error.message.startsWith('Cancellation failed')) {
|
|
2024
2082
|
return;
|
|
2025
2083
|
}
|
|
2026
2084
|
this.#logger.error({ error: event.error }, 'OpenAI Realtime API returned an error');
|
|
2027
2085
|
|
|
2028
|
-
const eventId = event.error.event_id;
|
|
2029
2086
|
if (eventId) {
|
|
2030
2087
|
const handle = this.responseCreatedFutures[eventId];
|
|
2031
2088
|
if (handle) {
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { ChatContext, initializeLogger } from '@livekit/agents';
|
|
4
5
|
import { llm, llmStrict } from '@livekit/agents-plugins-test';
|
|
6
|
+
import type OpenAI from 'openai';
|
|
5
7
|
import { describe, expect, it } from 'vitest';
|
|
6
8
|
import { wsServerEventSchema } from '../ws/types.js';
|
|
7
9
|
import { LLM } from './llm.js';
|
|
8
10
|
|
|
11
|
+
initializeLogger({ level: 'silent', pretty: false });
|
|
12
|
+
|
|
9
13
|
const hasOpenAIApiKey = Boolean(process.env.OPENAI_API_KEY);
|
|
10
14
|
|
|
11
15
|
describe('OpenAI Responses WebSocket', () => {
|
|
@@ -28,6 +32,90 @@ describe('OpenAI Responses WebSocket', () => {
|
|
|
28
32
|
});
|
|
29
33
|
});
|
|
30
34
|
|
|
35
|
+
type ResponsesEvent = Record<string, unknown>;
|
|
36
|
+
|
|
37
|
+
// opens every stream, on every provider
|
|
38
|
+
const RESPONSE_CREATED: ResponsesEvent = {
|
|
39
|
+
type: 'response.created',
|
|
40
|
+
response: { id: 'resp_1' },
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// a reasoning phase marker: a message item with no text of its own
|
|
44
|
+
const PHASE_METADATA: ResponsesEvent = {
|
|
45
|
+
type: 'response.output_item.done',
|
|
46
|
+
item: { type: 'message', phase: 'thinking' },
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const TEXT_DELTA: ResponsesEvent = {
|
|
50
|
+
type: 'response.output_text.delta',
|
|
51
|
+
delta: 'hello',
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Replays the given events, then dies the way a provider going quiet does.
|
|
56
|
+
* Returns how many times the request was attempted.
|
|
57
|
+
*/
|
|
58
|
+
async function attemptsUntilStall(events: ResponsesEvent[], maxRetry: number): Promise<number> {
|
|
59
|
+
let attempts = 0;
|
|
60
|
+
|
|
61
|
+
const client = {
|
|
62
|
+
responses: {
|
|
63
|
+
create: async () => {
|
|
64
|
+
attempts++;
|
|
65
|
+
return {
|
|
66
|
+
async *[Symbol.asyncIterator]() {
|
|
67
|
+
for (const event of events) {
|
|
68
|
+
yield event;
|
|
69
|
+
}
|
|
70
|
+
throw new Error('stalled mid-stream');
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const model = new LLM({
|
|
78
|
+
model: 'gpt-4.1',
|
|
79
|
+
apiKey: 'test-key',
|
|
80
|
+
useWebSocket: false,
|
|
81
|
+
client: client as unknown as OpenAI,
|
|
82
|
+
});
|
|
83
|
+
// the stream emits 'error' on every failed attempt; without a listener the
|
|
84
|
+
// EventEmitter turns it into an unhandled error and fails the run
|
|
85
|
+
model.on('error', () => {});
|
|
86
|
+
|
|
87
|
+
const chatCtx = ChatContext.empty();
|
|
88
|
+
chatCtx.addMessage({ role: 'user', content: 'hi' });
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
const stream = model.chat({
|
|
92
|
+
chatCtx,
|
|
93
|
+
connOptions: { maxRetry, retryIntervalMs: 0, timeoutMs: 5000 },
|
|
94
|
+
});
|
|
95
|
+
for await (const _chunk of stream) {
|
|
96
|
+
void _chunk;
|
|
97
|
+
}
|
|
98
|
+
} catch {
|
|
99
|
+
// the stall surfaces through the 'error' event; the iterator just ends
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return attempts;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
describe('OpenAI Responses HTTP retry eligibility', () => {
|
|
106
|
+
it('retries a stall that follows the stream-opening event alone', async () => {
|
|
107
|
+
expect(await attemptsUntilStall([RESPONSE_CREATED], 2)).toBe(3);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('retries a stall that follows a phase marker', async () => {
|
|
111
|
+
expect(await attemptsUntilStall([RESPONSE_CREATED, PHASE_METADATA], 2)).toBe(3);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('does not retry once generated text has reached the caller', async () => {
|
|
115
|
+
expect(await attemptsUntilStall([RESPONSE_CREATED, TEXT_DELTA], 2)).toBe(1);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
31
119
|
if (hasOpenAIApiKey) {
|
|
32
120
|
describe('OpenAI Responses', async () => {
|
|
33
121
|
await llm(
|
package/src/responses/llm.ts
CHANGED
|
@@ -226,7 +226,6 @@ class ResponsesHttpLLMStream extends llm.LLMStream {
|
|
|
226
226
|
);
|
|
227
227
|
|
|
228
228
|
for await (const event of stream) {
|
|
229
|
-
retryable = false;
|
|
230
229
|
let chunk: llm.ChatChunk | undefined;
|
|
231
230
|
|
|
232
231
|
switch (event.type) {
|
|
@@ -252,6 +251,11 @@ class ResponsesHttpLLMStream extends llm.LLMStream {
|
|
|
252
251
|
|
|
253
252
|
if (chunk) {
|
|
254
253
|
this.queue.put(chunk);
|
|
254
|
+
// a retry only duplicates output the caller has already seen; the
|
|
255
|
+
// stream-opening event and the usage-bearing one are neither
|
|
256
|
+
if (llm.hasResponse(chunk)) {
|
|
257
|
+
retryable = false;
|
|
258
|
+
}
|
|
255
259
|
}
|
|
256
260
|
}
|
|
257
261
|
} catch (error) {
|