@livekit/agents-plugin-openai 1.4.4 → 1.4.6
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 +20 -3
- package/dist/realtime/api_proto.d.ts +20 -3
- 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 +100 -11
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +7 -0
- package/dist/realtime/realtime_model.d.ts +7 -0
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +100 -11
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model.test.cjs +252 -9
- package/dist/realtime/realtime_model.test.cjs.map +1 -1
- package/dist/realtime/realtime_model.test.js +253 -10
- package/dist/realtime/realtime_model.test.js.map +1 -1
- package/dist/responses/llm.cjs +12 -1
- package/dist/responses/llm.cjs.map +1 -1
- package/dist/responses/llm.d.ts.map +1 -1
- package/dist/responses/llm.js +12 -1
- package/dist/responses/llm.js.map +1 -1
- package/dist/ws/llm.cjs +10 -1
- package/dist/ws/llm.cjs.map +1 -1
- package/dist/ws/llm.d.ts.map +1 -1
- package/dist/ws/llm.js +10 -1
- package/dist/ws/llm.js.map +1 -1
- package/dist/ws/types.cjs +1 -1
- package/dist/ws/types.cjs.map +1 -1
- package/dist/ws/types.d.cts +3 -0
- package/dist/ws/types.d.ts +3 -0
- package/dist/ws/types.d.ts.map +1 -1
- package/dist/ws/types.js +1 -1
- package/dist/ws/types.js.map +1 -1
- package/package.json +7 -7
- package/src/realtime/api_proto.ts +23 -0
- package/src/realtime/realtime_model.test.ts +339 -12
- package/src/realtime/realtime_model.ts +121 -13
- package/src/responses/llm.ts +14 -1
- package/src/ws/llm.ts +10 -1
- package/src/ws/types.ts +1 -1
|
@@ -48,6 +48,7 @@ interface RealtimeOptions {
|
|
|
48
48
|
maxResponseOutputTokens?: number | 'inf';
|
|
49
49
|
speed?: number;
|
|
50
50
|
tracing?: api_proto.TracingConfig | null;
|
|
51
|
+
reasoning?: api_proto.Reasoning;
|
|
51
52
|
apiKey?: string;
|
|
52
53
|
baseURL: string;
|
|
53
54
|
isAzure: boolean;
|
|
@@ -160,6 +161,7 @@ export class RealtimeModel extends llm.RealtimeModel {
|
|
|
160
161
|
constructor(
|
|
161
162
|
options: {
|
|
162
163
|
model?: string;
|
|
164
|
+
reasoning?: api_proto.Reasoning;
|
|
163
165
|
voice?: string;
|
|
164
166
|
/** @deprecated Unused in GA API (v1). Temperature is no longer supported. */
|
|
165
167
|
temperature?: number;
|
|
@@ -225,7 +227,7 @@ export class RealtimeModel extends llm.RealtimeModel {
|
|
|
225
227
|
this._options = {
|
|
226
228
|
...DEFAULT_REALTIME_MODEL_OPTIONS,
|
|
227
229
|
...optionsWithoutModalities,
|
|
228
|
-
baseURL: options.baseURL || BASE_URL,
|
|
230
|
+
baseURL: options.baseURL || process.env.OPENAI_BASE_URL || BASE_URL,
|
|
229
231
|
apiKey,
|
|
230
232
|
isAzure,
|
|
231
233
|
model: options.model || DEFAULT_REALTIME_MODEL_OPTIONS.model,
|
|
@@ -441,6 +443,8 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
441
443
|
private itemCreateFutures: { [id: string]: Future } = {};
|
|
442
444
|
private itemDeleteFutures: { [id: string]: Future } = {};
|
|
443
445
|
|
|
446
|
+
private inputTranscriptAccumulators = new Map<string, Map<number, string>>();
|
|
447
|
+
|
|
444
448
|
// Track items that have real server-side audio (created in current session, not restored)
|
|
445
449
|
// Items restored after reconnection are text-only and cannot be truncated
|
|
446
450
|
private audioCapableItemIds: Set<string> = new Set();
|
|
@@ -514,6 +518,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
514
518
|
// GA format (OpenAI or Azure GA)
|
|
515
519
|
const audioFormat: api_proto.AudioFormat = { type: 'audio/pcm', rate: SAMPLE_RATE };
|
|
516
520
|
const modality: Modality = opts.modalities.includes('audio') ? 'audio' : 'text';
|
|
521
|
+
const includeReasoning = opts.reasoning && opts.model.startsWith('gpt-realtime-2');
|
|
517
522
|
return {
|
|
518
523
|
type: 'session.update',
|
|
519
524
|
session: {
|
|
@@ -537,6 +542,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
537
542
|
tool_choice: toOaiToolChoice(opts.toolChoice),
|
|
538
543
|
tracing: opts.tracing,
|
|
539
544
|
instructions: this.instructions,
|
|
545
|
+
...(includeReasoning ? { reasoning: opts.reasoning } : {}),
|
|
540
546
|
},
|
|
541
547
|
};
|
|
542
548
|
}
|
|
@@ -672,7 +678,13 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
672
678
|
| api_proto.ConversationItemDeleteEvent
|
|
673
679
|
)[] = [];
|
|
674
680
|
|
|
675
|
-
const
|
|
681
|
+
const remoteCtx = this.chatCtx;
|
|
682
|
+
const remoteIds = new Set(remoteCtx.items.map((item) => item.id));
|
|
683
|
+
newChatCtx.items = newChatCtx.items.filter(
|
|
684
|
+
(item) => item.type !== 'message' || item.content.length > 0 || remoteIds.has(item.id),
|
|
685
|
+
);
|
|
686
|
+
|
|
687
|
+
const diffOps = llm.computeChatCtxDiff(remoteCtx, newChatCtx);
|
|
676
688
|
for (const op of diffOps.toRemove) {
|
|
677
689
|
events.push({
|
|
678
690
|
type: 'conversation.item.delete',
|
|
@@ -1030,6 +1042,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1030
1042
|
|
|
1031
1043
|
// Clear audio-capable item tracking - restored items are text-only on the server
|
|
1032
1044
|
this.audioCapableItemIds.clear();
|
|
1045
|
+
this.inputTranscriptAccumulators.clear();
|
|
1033
1046
|
|
|
1034
1047
|
const events: api_proto.ClientEvent[] = [];
|
|
1035
1048
|
|
|
@@ -1198,6 +1211,9 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1198
1211
|
case 'conversation.item.deleted':
|
|
1199
1212
|
this.handleConversationItemDeleted(event);
|
|
1200
1213
|
break;
|
|
1214
|
+
case 'conversation.item.input_audio_transcription.delta':
|
|
1215
|
+
this.handleConversationItemInputAudioTranscriptionDelta(event);
|
|
1216
|
+
break;
|
|
1201
1217
|
case 'conversation.item.input_audio_transcription.completed':
|
|
1202
1218
|
this.handleConversationItemInputAudioTranscriptionCompleted(event);
|
|
1203
1219
|
break;
|
|
@@ -1313,6 +1329,8 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1313
1329
|
}
|
|
1314
1330
|
this.itemDeleteFutures = {};
|
|
1315
1331
|
|
|
1332
|
+
this.inputTranscriptAccumulators.clear();
|
|
1333
|
+
|
|
1316
1334
|
// Clean up current generation if exists
|
|
1317
1335
|
if (this.currentGeneration) {
|
|
1318
1336
|
for (const gen of this.currentGeneration.messages.values()) {
|
|
@@ -1470,6 +1488,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1470
1488
|
|
|
1471
1489
|
// Clean up audio-capable tracking for deleted items
|
|
1472
1490
|
this.audioCapableItemIds.delete(event.item_id);
|
|
1491
|
+
this.inputTranscriptAccumulators.delete(event.item_id);
|
|
1473
1492
|
|
|
1474
1493
|
try {
|
|
1475
1494
|
this.remoteChatCtx.delete(event.item_id);
|
|
@@ -1484,26 +1503,56 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1484
1503
|
}
|
|
1485
1504
|
}
|
|
1486
1505
|
|
|
1506
|
+
private handleConversationItemInputAudioTranscriptionDelta(
|
|
1507
|
+
event: api_proto.ConversationItemInputAudioTranscriptionDeltaEvent,
|
|
1508
|
+
): void {
|
|
1509
|
+
if (!event.delta) return;
|
|
1510
|
+
|
|
1511
|
+
const contentIndex = event.content_index ?? 0;
|
|
1512
|
+
let byIndex = this.inputTranscriptAccumulators.get(event.item_id);
|
|
1513
|
+
if (!byIndex) {
|
|
1514
|
+
byIndex = new Map();
|
|
1515
|
+
this.inputTranscriptAccumulators.set(event.item_id, byIndex);
|
|
1516
|
+
}
|
|
1517
|
+
const accumulated = (byIndex.get(contentIndex) ?? '') + event.delta;
|
|
1518
|
+
byIndex.set(contentIndex, accumulated);
|
|
1519
|
+
|
|
1520
|
+
this.emit('input_audio_transcription_completed', {
|
|
1521
|
+
itemId: event.item_id,
|
|
1522
|
+
transcript: accumulated,
|
|
1523
|
+
isFinal: false,
|
|
1524
|
+
});
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
private clearAccumulator(itemId: string, contentIndex: number): string | undefined {
|
|
1528
|
+
const byIndex = this.inputTranscriptAccumulators.get(itemId);
|
|
1529
|
+
if (!byIndex) return undefined;
|
|
1530
|
+
const partial = byIndex.get(contentIndex);
|
|
1531
|
+
byIndex.delete(contentIndex);
|
|
1532
|
+
if (byIndex.size === 0) this.inputTranscriptAccumulators.delete(itemId);
|
|
1533
|
+
return partial;
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1487
1536
|
private handleConversationItemInputAudioTranscriptionCompleted(
|
|
1488
1537
|
event: api_proto.ConversationItemInputAudioTranscriptionCompletedEvent,
|
|
1489
1538
|
): void {
|
|
1490
|
-
|
|
1491
|
-
if (!remoteItem) {
|
|
1492
|
-
return;
|
|
1493
|
-
}
|
|
1539
|
+
this.clearAccumulator(event.item_id, event.content_index ?? 0);
|
|
1494
1540
|
|
|
1495
|
-
const
|
|
1496
|
-
if (
|
|
1497
|
-
item.
|
|
1498
|
-
|
|
1499
|
-
|
|
1541
|
+
const remoteItem = this.remoteChatCtx.get(event.item_id);
|
|
1542
|
+
if (remoteItem) {
|
|
1543
|
+
const item = remoteItem.item;
|
|
1544
|
+
if (item instanceof llm.ChatMessage) {
|
|
1545
|
+
item.content.push(event.transcript);
|
|
1546
|
+
} else {
|
|
1547
|
+
throw new Error('item is not a chat message');
|
|
1548
|
+
}
|
|
1500
1549
|
}
|
|
1501
1550
|
|
|
1502
1551
|
this.emit('input_audio_transcription_completed', {
|
|
1503
1552
|
itemId: event.item_id,
|
|
1504
1553
|
transcript: event.transcript,
|
|
1505
1554
|
isFinal: true,
|
|
1506
|
-
}
|
|
1555
|
+
});
|
|
1507
1556
|
}
|
|
1508
1557
|
|
|
1509
1558
|
private handleConversationItemInputAudioTranscriptionFailed(
|
|
@@ -1513,6 +1562,18 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1513
1562
|
{ error: event.error },
|
|
1514
1563
|
'OpenAI Realtime API failed to transcribe input audio',
|
|
1515
1564
|
);
|
|
1565
|
+
this.finalizePartialOnTranscriptionFailure(event.item_id, event.content_index ?? 0);
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
// Close any open partial stream so consumers waiting for isFinal don't hang.
|
|
1569
|
+
private finalizePartialOnTranscriptionFailure(itemId: string, contentIndex: number): void {
|
|
1570
|
+
const partial = this.clearAccumulator(itemId, contentIndex);
|
|
1571
|
+
if (partial === undefined) return;
|
|
1572
|
+
this.emit('input_audio_transcription_completed', {
|
|
1573
|
+
itemId,
|
|
1574
|
+
transcript: partial,
|
|
1575
|
+
isFinal: true,
|
|
1576
|
+
});
|
|
1516
1577
|
}
|
|
1517
1578
|
|
|
1518
1579
|
private handleResponseContentPartAdded(event: api_proto.ResponseContentPartAddedEvent): void {
|
|
@@ -1775,7 +1836,53 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1775
1836
|
};
|
|
1776
1837
|
|
|
1777
1838
|
this.emit('metrics_collected', realtimeMetrics);
|
|
1778
|
-
|
|
1839
|
+
this.handleResponseDoneButNotComplete(_event);
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
private handleResponseDoneButNotComplete(event: api_proto.ResponseDoneEvent): void {
|
|
1843
|
+
if (event.response.status === 'completed') {
|
|
1844
|
+
return;
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
const statusDetails = event.response.status_details;
|
|
1848
|
+
if (event.response.status === 'failed') {
|
|
1849
|
+
const errorBody =
|
|
1850
|
+
typeof statusDetails !== 'string' && statusDetails?.type === 'failed'
|
|
1851
|
+
? statusDetails.error
|
|
1852
|
+
: undefined;
|
|
1853
|
+
const errorTypeValue = errorBody && 'type' in errorBody ? errorBody.type : undefined;
|
|
1854
|
+
const errorType = typeof errorTypeValue === 'string' ? errorTypeValue : 'unknown';
|
|
1855
|
+
|
|
1856
|
+
this.emitError({
|
|
1857
|
+
error: new APIError(`OpenAI Realtime API response failed with error type: ${errorType}`, {
|
|
1858
|
+
body: errorBody ?? null,
|
|
1859
|
+
retryable: true,
|
|
1860
|
+
}),
|
|
1861
|
+
recoverable: true,
|
|
1862
|
+
});
|
|
1863
|
+
} else if (event.response.status === 'cancelled' || event.response.status === 'incomplete') {
|
|
1864
|
+
let statusType: string | undefined;
|
|
1865
|
+
let statusReason: string | undefined;
|
|
1866
|
+
if (typeof statusDetails === 'string') {
|
|
1867
|
+
statusType = statusDetails;
|
|
1868
|
+
} else {
|
|
1869
|
+
statusType = statusDetails?.type;
|
|
1870
|
+
statusReason =
|
|
1871
|
+
statusDetails && 'reason' in statusDetails ? statusDetails.reason : undefined;
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
this.#logger.debug(
|
|
1875
|
+
{
|
|
1876
|
+
eventId: event.response.id,
|
|
1877
|
+
eventResponseStatus: event.response.status,
|
|
1878
|
+
eventResponseStatusType: statusType,
|
|
1879
|
+
eventResponseStatusReason: statusReason,
|
|
1880
|
+
},
|
|
1881
|
+
`OpenAI Realtime API response done but not complete with status: ${event.response.status} (type=${statusType}, reason=${statusReason})`,
|
|
1882
|
+
);
|
|
1883
|
+
} else {
|
|
1884
|
+
this.#logger.debug({ eventResponseStatus: event.response.status }, 'Unknown response status');
|
|
1885
|
+
}
|
|
1779
1886
|
}
|
|
1780
1887
|
|
|
1781
1888
|
private handleError(event: api_proto.ErrorEvent): void {
|
|
@@ -1807,6 +1914,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1807
1914
|
private emitError({ error, recoverable }: { error: Error; recoverable: boolean }): void {
|
|
1808
1915
|
// IMPORTANT: only emit error if there are listeners; otherwise emit will throw an error
|
|
1809
1916
|
this.emit('error', {
|
|
1917
|
+
type: 'realtime_model_error',
|
|
1810
1918
|
timestamp: Date.now(),
|
|
1811
1919
|
// TODO(brian): add label
|
|
1812
1920
|
label: '',
|
package/src/responses/llm.ts
CHANGED
|
@@ -182,7 +182,7 @@ class ResponsesHttpLLMStream extends llm.LLMStream {
|
|
|
182
182
|
)) as OpenAI.Responses.ResponseInputItem[];
|
|
183
183
|
|
|
184
184
|
const tools = this.toolCtx
|
|
185
|
-
?
|
|
185
|
+
? llm.sortedToolEntries(this.toolCtx).map(([name, func]) => {
|
|
186
186
|
const oaiParams = {
|
|
187
187
|
type: 'function' as const,
|
|
188
188
|
name: name,
|
|
@@ -318,6 +318,19 @@ class ResponsesHttpLLMStream extends llm.LLMStream {
|
|
|
318
318
|
],
|
|
319
319
|
},
|
|
320
320
|
};
|
|
321
|
+
} else if (event.item.type === 'message') {
|
|
322
|
+
const phase = (event.item as OpenAI.Responses.ResponseOutputMessage & { phase?: string })
|
|
323
|
+
.phase;
|
|
324
|
+
if (phase === undefined) return undefined;
|
|
325
|
+
|
|
326
|
+
chunk = {
|
|
327
|
+
id: this.responseId,
|
|
328
|
+
delta: {
|
|
329
|
+
role: 'assistant',
|
|
330
|
+
content: undefined,
|
|
331
|
+
extra: { openai: { phase } },
|
|
332
|
+
},
|
|
333
|
+
};
|
|
321
334
|
}
|
|
322
335
|
return chunk;
|
|
323
336
|
}
|
package/src/ws/llm.ts
CHANGED
|
@@ -447,7 +447,7 @@ export class WSLLMStream extends llm.LLMStream {
|
|
|
447
447
|
)) as OpenAI.Responses.ResponseInputItem[];
|
|
448
448
|
|
|
449
449
|
const tools = this.toolCtx
|
|
450
|
-
?
|
|
450
|
+
? llm.sortedToolEntries(this.toolCtx).map(([name, func]) => {
|
|
451
451
|
const oaiParams = {
|
|
452
452
|
type: 'function' as const,
|
|
453
453
|
name,
|
|
@@ -593,6 +593,15 @@ export class WSLLMStream extends llm.LLMStream {
|
|
|
593
593
|
],
|
|
594
594
|
},
|
|
595
595
|
};
|
|
596
|
+
} else if (event.item.type === 'message' && event.item.phase !== undefined) {
|
|
597
|
+
return {
|
|
598
|
+
id: this.#responseId,
|
|
599
|
+
delta: {
|
|
600
|
+
role: 'assistant',
|
|
601
|
+
content: undefined,
|
|
602
|
+
extra: { openai: { phase: event.item.phase } },
|
|
603
|
+
},
|
|
604
|
+
};
|
|
596
605
|
}
|
|
597
606
|
return undefined;
|
|
598
607
|
}
|
package/src/ws/types.ts
CHANGED
|
@@ -44,7 +44,7 @@ export const wsFunctionCallItemSchema = z.object({
|
|
|
44
44
|
|
|
45
45
|
export const wsOutputItemSchema = z.discriminatedUnion('type', [
|
|
46
46
|
wsFunctionCallItemSchema,
|
|
47
|
-
z.object({ type: z.literal('message') }).passthrough(),
|
|
47
|
+
z.object({ type: z.literal('message'), phase: z.string().optional() }).passthrough(),
|
|
48
48
|
z.object({ type: z.literal('reasoning') }).passthrough(),
|
|
49
49
|
z.object({ type: z.literal('file') }).passthrough(),
|
|
50
50
|
z.object({ type: z.literal('computer_call') }).passthrough(),
|