@livekit/agents-plugin-openai 1.4.8 → 1.4.11
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/realtime/api_proto.cjs.map +1 -1
- package/dist/realtime/api_proto.d.cts +1 -0
- package/dist/realtime/api_proto.d.ts +1 -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 +61 -8
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +1 -0
- package/dist/realtime/realtime_model.d.ts +1 -0
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +61 -8
- package/dist/realtime/realtime_model.js.map +1 -1
- package/package.json +5 -5
- package/src/realtime/api_proto.ts +1 -0
- package/src/realtime/realtime_model.ts +75 -9
|
@@ -82,10 +82,14 @@ interface ResponseGeneration {
|
|
|
82
82
|
_firstTokenTimestamp?: number;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
class DiscardedGeneration {
|
|
86
|
+
readonly discarded = true;
|
|
87
|
+
}
|
|
88
|
+
|
|
85
89
|
class CreateResponseHandle {
|
|
86
90
|
instructions?: string;
|
|
87
91
|
doneFut: Future<llm.GenerationCreatedEvent>;
|
|
88
|
-
|
|
92
|
+
timeout?: ReturnType<typeof setTimeout>;
|
|
89
93
|
constructor({ instructions }: { instructions?: string }) {
|
|
90
94
|
this.instructions = instructions;
|
|
91
95
|
this.doneFut = new Future();
|
|
@@ -435,8 +439,9 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
435
439
|
// per-session copy of options so updateOptions can diff against the session's
|
|
436
440
|
// own state instead of the shared model-level state.
|
|
437
441
|
private _options: RealtimeOptions;
|
|
438
|
-
private currentGeneration?: ResponseGeneration;
|
|
442
|
+
private currentGeneration?: ResponseGeneration | DiscardedGeneration;
|
|
439
443
|
private responseCreatedFutures: { [id: string]: CreateResponseHandle } = {};
|
|
444
|
+
private discardedEventIds = new Set<string>();
|
|
440
445
|
|
|
441
446
|
private textModeRecoveryRetries: number = 0;
|
|
442
447
|
|
|
@@ -851,7 +856,9 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
851
856
|
)?.[0];
|
|
852
857
|
if (eventId) {
|
|
853
858
|
delete this.responseCreatedFutures[eventId];
|
|
859
|
+
this.discardedEventIds.add(eventId);
|
|
854
860
|
}
|
|
861
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
855
862
|
if (!handle.doneFut.done) {
|
|
856
863
|
handle.doneFut.reject(new Error('generateReply aborted'));
|
|
857
864
|
this.sendEvent({
|
|
@@ -1043,8 +1050,10 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1043
1050
|
if (!handle.doneFut.done) {
|
|
1044
1051
|
handle.doneFut.reject(new Error('Session reconnected'));
|
|
1045
1052
|
}
|
|
1053
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1046
1054
|
}
|
|
1047
1055
|
this.responseCreatedFutures = {};
|
|
1056
|
+
this.discardedEventIds.clear();
|
|
1048
1057
|
|
|
1049
1058
|
// Clear audio-capable item tracking - restored items are text-only on the server
|
|
1050
1059
|
this.audioCapableItemIds.clear();
|
|
@@ -1295,8 +1304,13 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1295
1304
|
try {
|
|
1296
1305
|
const result = await Promise.race([wsTask.result, sendTask.result, waitReconnectTask.result]);
|
|
1297
1306
|
|
|
1298
|
-
|
|
1299
|
-
|
|
1307
|
+
const currentGeneration = this.currentGeneration;
|
|
1308
|
+
if (waitReconnectTask.done && currentGeneration) {
|
|
1309
|
+
if (currentGeneration instanceof DiscardedGeneration) {
|
|
1310
|
+
this.currentGeneration = undefined;
|
|
1311
|
+
} else {
|
|
1312
|
+
await currentGeneration._doneFut.await;
|
|
1313
|
+
}
|
|
1300
1314
|
}
|
|
1301
1315
|
|
|
1302
1316
|
if (result instanceof Error) {
|
|
@@ -1318,6 +1332,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1318
1332
|
if (!handle.doneFut.done) {
|
|
1319
1333
|
handle.doneFut.reject(new Error('Session closed'));
|
|
1320
1334
|
}
|
|
1335
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1321
1336
|
}
|
|
1322
1337
|
this.responseCreatedFutures = {};
|
|
1323
1338
|
|
|
@@ -1338,7 +1353,9 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1338
1353
|
this.inputTranscriptAccumulators.clear();
|
|
1339
1354
|
|
|
1340
1355
|
// Clean up current generation if exists
|
|
1341
|
-
if (this.currentGeneration) {
|
|
1356
|
+
if (this.currentGeneration instanceof DiscardedGeneration) {
|
|
1357
|
+
this.currentGeneration = undefined;
|
|
1358
|
+
} else if (this.currentGeneration) {
|
|
1342
1359
|
for (const gen of this.currentGeneration.messages.values()) {
|
|
1343
1360
|
gen.textChannel.close();
|
|
1344
1361
|
gen.audioChannel.close();
|
|
@@ -1378,28 +1395,41 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1378
1395
|
throw new Error('response.id is missing');
|
|
1379
1396
|
}
|
|
1380
1397
|
|
|
1381
|
-
|
|
1398
|
+
const clientEventId = event.response.metadata?.client_event_id;
|
|
1399
|
+
if (clientEventId && this.discardedEventIds.has(clientEventId)) {
|
|
1400
|
+
this.discardedEventIds.delete(clientEventId);
|
|
1401
|
+
this.sendEvent({
|
|
1402
|
+
type: 'response.cancel',
|
|
1403
|
+
response_id: event.response.id,
|
|
1404
|
+
} as api_proto.ResponseCancelEvent);
|
|
1405
|
+
this.currentGeneration = new DiscardedGeneration();
|
|
1406
|
+
this.#logger.warn('discarding response that arrived after it was timed out or interrupted');
|
|
1407
|
+
return;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
const currentGeneration: ResponseGeneration = {
|
|
1382
1411
|
messageChannel: stream.createStreamChannel<llm.MessageGeneration>(),
|
|
1383
1412
|
functionChannel: stream.createStreamChannel<llm.FunctionCall>(),
|
|
1384
1413
|
messages: new Map(),
|
|
1385
1414
|
_doneFut: new Future(),
|
|
1386
1415
|
_createdTimestamp: Date.now(),
|
|
1387
1416
|
};
|
|
1417
|
+
this.currentGeneration = currentGeneration;
|
|
1388
1418
|
|
|
1389
1419
|
// Build generation event and resolve client future (if any) before emitting,
|
|
1390
1420
|
// matching Python behavior.
|
|
1391
1421
|
const generationEv = {
|
|
1392
|
-
messageStream:
|
|
1393
|
-
functionStream:
|
|
1422
|
+
messageStream: currentGeneration.messageChannel.stream(),
|
|
1423
|
+
functionStream: currentGeneration.functionChannel.stream(),
|
|
1394
1424
|
userInitiated: false,
|
|
1395
1425
|
responseId: event.response.id,
|
|
1396
1426
|
} as llm.GenerationCreatedEvent;
|
|
1397
1427
|
|
|
1398
|
-
const clientEventId = event.response.metadata?.client_event_id;
|
|
1399
1428
|
if (clientEventId) {
|
|
1400
1429
|
const handle = this.responseCreatedFutures[clientEventId];
|
|
1401
1430
|
if (handle) {
|
|
1402
1431
|
delete this.responseCreatedFutures[clientEventId];
|
|
1432
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1403
1433
|
generationEv.userInitiated = true;
|
|
1404
1434
|
if (!handle.doneFut.done) {
|
|
1405
1435
|
handle.doneFut.resolve(generationEv);
|
|
@@ -1411,6 +1441,8 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1411
1441
|
}
|
|
1412
1442
|
|
|
1413
1443
|
private handleResponseOutputItemAdded(event: api_proto.ResponseOutputItemAddedEvent): void {
|
|
1444
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1445
|
+
|
|
1414
1446
|
if (!this.currentGeneration) {
|
|
1415
1447
|
throw new Error('currentGeneration is not set');
|
|
1416
1448
|
}
|
|
@@ -1583,6 +1615,8 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1583
1615
|
}
|
|
1584
1616
|
|
|
1585
1617
|
private handleResponseContentPartAdded(event: api_proto.ResponseContentPartAddedEvent): void {
|
|
1618
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1619
|
+
|
|
1586
1620
|
if (!this.currentGeneration) {
|
|
1587
1621
|
throw new Error('currentGeneration is not set');
|
|
1588
1622
|
}
|
|
@@ -1617,6 +1651,8 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1617
1651
|
}
|
|
1618
1652
|
|
|
1619
1653
|
private handleResponseContentPartDone(event: api_proto.ResponseContentPartDoneEvent): void {
|
|
1654
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1655
|
+
|
|
1620
1656
|
if (!event.part) {
|
|
1621
1657
|
return;
|
|
1622
1658
|
}
|
|
@@ -1632,6 +1668,8 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1632
1668
|
}
|
|
1633
1669
|
|
|
1634
1670
|
private handleResponseTextDelta(event: api_proto.ResponseTextDeltaEvent): void {
|
|
1671
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1672
|
+
|
|
1635
1673
|
if (!this.currentGeneration) {
|
|
1636
1674
|
throw new Error('currentGeneration is not set');
|
|
1637
1675
|
}
|
|
@@ -1653,6 +1691,8 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1653
1691
|
}
|
|
1654
1692
|
|
|
1655
1693
|
private handleResponseTextDone(_event: api_proto.ResponseTextDoneEvent): void {
|
|
1694
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1695
|
+
|
|
1656
1696
|
if (!this.currentGeneration) {
|
|
1657
1697
|
throw new Error('currentGeneration is not set');
|
|
1658
1698
|
}
|
|
@@ -1661,6 +1701,8 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1661
1701
|
private handleResponseAudioTranscriptDelta(
|
|
1662
1702
|
event: api_proto.ResponseAudioTranscriptDeltaEvent,
|
|
1663
1703
|
): void {
|
|
1704
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1705
|
+
|
|
1664
1706
|
if (!this.currentGeneration) {
|
|
1665
1707
|
throw new Error('currentGeneration is not set');
|
|
1666
1708
|
}
|
|
@@ -1683,6 +1725,8 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1683
1725
|
}
|
|
1684
1726
|
|
|
1685
1727
|
private handleResponseAudioDelta(event: api_proto.ResponseAudioDeltaEvent): void {
|
|
1728
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1729
|
+
|
|
1686
1730
|
if (!this.currentGeneration) {
|
|
1687
1731
|
throw new Error('currentGeneration is not set');
|
|
1688
1732
|
}
|
|
@@ -1720,18 +1764,24 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1720
1764
|
private handleResponseAudioTranscriptDone(
|
|
1721
1765
|
_event: api_proto.ResponseAudioTranscriptDoneEvent,
|
|
1722
1766
|
): void {
|
|
1767
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1768
|
+
|
|
1723
1769
|
if (!this.currentGeneration) {
|
|
1724
1770
|
throw new Error('currentGeneration is not set');
|
|
1725
1771
|
}
|
|
1726
1772
|
}
|
|
1727
1773
|
|
|
1728
1774
|
private handleResponseAudioDone(_event: api_proto.ResponseAudioDoneEvent): void {
|
|
1775
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1776
|
+
|
|
1729
1777
|
if (!this.currentGeneration) {
|
|
1730
1778
|
throw new Error('currentGeneration is not set');
|
|
1731
1779
|
}
|
|
1732
1780
|
}
|
|
1733
1781
|
|
|
1734
1782
|
private handleResponseOutputItemDone(event: api_proto.ResponseOutputItemDoneEvent): void {
|
|
1783
|
+
if (this.currentGeneration instanceof DiscardedGeneration) return;
|
|
1784
|
+
|
|
1735
1785
|
if (!this.currentGeneration) {
|
|
1736
1786
|
throw new Error('currentGeneration is not set');
|
|
1737
1787
|
}
|
|
@@ -1767,6 +1817,11 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1767
1817
|
}
|
|
1768
1818
|
|
|
1769
1819
|
private handleResponseDone(_event: api_proto.ResponseDoneEvent): void {
|
|
1820
|
+
if (this.currentGeneration instanceof DiscardedGeneration) {
|
|
1821
|
+
this.currentGeneration = undefined;
|
|
1822
|
+
return;
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1770
1825
|
if (!this.currentGeneration) {
|
|
1771
1826
|
// OpenAI has a race condition where we could receive response.done without any
|
|
1772
1827
|
// previous response.created (This happens generally during interruption)
|
|
@@ -1902,6 +1957,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1902
1957
|
const handle = this.responseCreatedFutures[eventId];
|
|
1903
1958
|
if (handle) {
|
|
1904
1959
|
delete this.responseCreatedFutures[eventId];
|
|
1960
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1905
1961
|
if (!handle.doneFut.done) {
|
|
1906
1962
|
handle.doneFut.reject(new Error(event.error.message));
|
|
1907
1963
|
}
|
|
@@ -1948,8 +2004,18 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1948
2004
|
}
|
|
1949
2005
|
|
|
1950
2006
|
const eventId = shortuuid('response_create_');
|
|
2007
|
+
this.discardedEventIds ??= new Set<string>();
|
|
1951
2008
|
if (userInitiated) {
|
|
1952
2009
|
this.responseCreatedFutures[eventId] = handle;
|
|
2010
|
+
handle.timeout = setTimeout(() => {
|
|
2011
|
+
if (this.responseCreatedFutures[eventId] === handle) {
|
|
2012
|
+
delete this.responseCreatedFutures[eventId];
|
|
2013
|
+
}
|
|
2014
|
+
if (!handle.doneFut.done) {
|
|
2015
|
+
this.discardedEventIds.add(eventId);
|
|
2016
|
+
handle.doneFut.reject(new Error('generateReply timed out.'));
|
|
2017
|
+
}
|
|
2018
|
+
}, 10000);
|
|
1953
2019
|
}
|
|
1954
2020
|
|
|
1955
2021
|
const response: api_proto.ResponseCreateEvent['response'] = {};
|