@livekit/agents-plugin-openai 1.5.2 → 1.5.5
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/realtime_model.cjs +216 -160
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +4 -0
- package/dist/realtime/realtime_model.d.ts +4 -0
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +215 -160
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model.test.cjs +216 -0
- package/dist/realtime/realtime_model.test.cjs.map +1 -1
- package/dist/realtime/realtime_model.test.js +217 -0
- package/dist/realtime/realtime_model.test.js.map +1 -1
- package/dist/ws/llm.cjs +6 -3
- package/dist/ws/llm.cjs.map +1 -1
- package/dist/ws/llm.d.cts +4 -5
- package/dist/ws/llm.d.ts +4 -5
- package/dist/ws/llm.d.ts.map +1 -1
- package/dist/ws/llm.js +6 -3
- package/dist/ws/llm.js.map +1 -1
- package/dist/ws/llm.test.cjs +22 -2
- package/dist/ws/llm.test.cjs.map +1 -1
- package/dist/ws/llm.test.js +22 -2
- package/dist/ws/llm.test.js.map +1 -1
- package/package.json +5 -5
- package/src/realtime/realtime_model.test.ts +265 -0
- package/src/realtime/realtime_model.ts +229 -164
- package/src/ws/llm.test.ts +28 -2
- package/src/ws/llm.ts +13 -8
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
import { APIError, Future, Task, llm, stream } from '@livekit/agents';
|
|
5
|
+
import { once } from 'node:events';
|
|
5
6
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
7
|
+
import { WebSocketServer } from 'ws';
|
|
6
8
|
import type * as api_proto from './api_proto.js';
|
|
7
9
|
import {
|
|
8
10
|
RealtimeModel,
|
|
9
11
|
RealtimeSession,
|
|
12
|
+
isFatalError,
|
|
10
13
|
livekitItemToOpenAIItem,
|
|
11
14
|
processBaseURL,
|
|
12
15
|
} from './realtime_model.js';
|
|
@@ -26,6 +29,8 @@ type RealtimeSessionInternals = {
|
|
|
26
29
|
|
|
27
30
|
type ResponseDoneSessionInternals = {
|
|
28
31
|
handleResponseDone: (event: api_proto.ResponseDoneEvent) => void;
|
|
32
|
+
handleResponseDoneButNotComplete: (event: api_proto.ResponseDoneEvent) => void;
|
|
33
|
+
handleError: (event: api_proto.ErrorEvent) => void;
|
|
29
34
|
on: (event: 'error', listener: (error: llm.RealtimeModelError) => void) => void;
|
|
30
35
|
currentGeneration: {
|
|
31
36
|
messageChannel: stream.StreamChannel<llm.MessageGeneration>;
|
|
@@ -37,6 +42,11 @@ type ResponseDoneSessionInternals = {
|
|
|
37
42
|
};
|
|
38
43
|
};
|
|
39
44
|
|
|
45
|
+
type ErrorSessionInternals = {
|
|
46
|
+
handleError: (event: api_proto.ErrorEvent) => void;
|
|
47
|
+
on: (event: 'error', listener: (error: llm.RealtimeModelError) => void) => void;
|
|
48
|
+
};
|
|
49
|
+
|
|
40
50
|
function createSessionForTest(): RealtimeSessionInternals {
|
|
41
51
|
const session = Object.create(RealtimeSession.prototype) as RealtimeSessionInternals;
|
|
42
52
|
session.responseCreatedFutures = {};
|
|
@@ -177,6 +187,206 @@ describe('RealtimeSession response.done status handling', () => {
|
|
|
177
187
|
});
|
|
178
188
|
});
|
|
179
189
|
|
|
190
|
+
describe('RealtimeSession fatal error handling', () => {
|
|
191
|
+
afterEach(() => {
|
|
192
|
+
vi.restoreAllMocks();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
function createErrorSession(): ErrorSessionInternals {
|
|
196
|
+
stubTaskRuntime();
|
|
197
|
+
|
|
198
|
+
const model = new RealtimeModel({ apiKey: 'test-key' });
|
|
199
|
+
return model.session() as unknown as ErrorSessionInternals;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function createResponseDoneSession(): ResponseDoneSessionInternals {
|
|
203
|
+
stubTaskRuntime();
|
|
204
|
+
|
|
205
|
+
const model = new RealtimeModel({ apiKey: 'test-key' });
|
|
206
|
+
return model.session() as unknown as ResponseDoneSessionInternals;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
it('matches known fatal error codes', () => {
|
|
210
|
+
expect(isFatalError({ code: 'insufficient_quota' })).toBe(true);
|
|
211
|
+
expect(isFatalError({ code: undefined, type: 'invalid_api_key' })).toBe(true);
|
|
212
|
+
expect(isFatalError({ code: '', type: 'invalid_api_key' })).toBe(true);
|
|
213
|
+
expect(isFatalError({ code: 'server_error' })).toBe(false);
|
|
214
|
+
expect(isFatalError({})).toBe(false);
|
|
215
|
+
expect(isFatalError(null)).toBe(false);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('stops the websocket session after a fatal server error', async () => {
|
|
219
|
+
const server = new WebSocketServer({ host: '127.0.0.1', port: 0 });
|
|
220
|
+
await once(server, 'listening');
|
|
221
|
+
|
|
222
|
+
const address = server.address();
|
|
223
|
+
if (typeof address === 'string' || address === null) {
|
|
224
|
+
throw new Error('expected websocket server to listen on a TCP port');
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let connectionCount = 0;
|
|
228
|
+
server.on('connection', (socket) => {
|
|
229
|
+
connectionCount++;
|
|
230
|
+
socket.on('message', (data) => {
|
|
231
|
+
const event = JSON.parse(data.toString()) as { type?: string };
|
|
232
|
+
if (event.type !== 'response.create') return;
|
|
233
|
+
|
|
234
|
+
socket.send(
|
|
235
|
+
JSON.stringify({
|
|
236
|
+
type: 'error',
|
|
237
|
+
event_id: 'evt_quota',
|
|
238
|
+
error: {
|
|
239
|
+
type: 'invalid_request_error',
|
|
240
|
+
code: 'insufficient_quota',
|
|
241
|
+
message: 'quota exceeded',
|
|
242
|
+
param: '',
|
|
243
|
+
},
|
|
244
|
+
}),
|
|
245
|
+
);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
const model = new RealtimeModel({
|
|
250
|
+
apiKey: 'test-key',
|
|
251
|
+
baseURL: `http://127.0.0.1:${address.port}/v1`,
|
|
252
|
+
});
|
|
253
|
+
const session = model.session();
|
|
254
|
+
const errors: llm.RealtimeModelError[] = [];
|
|
255
|
+
session.on('error', (error) => errors.push(error));
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
await expect(session.generateReply()).rejects.toThrow('Realtime session closed');
|
|
259
|
+
expect(errors).toHaveLength(1);
|
|
260
|
+
expect(errors[0]!.recoverable).toBe(false);
|
|
261
|
+
expect(errors[0]!.error).toBeInstanceOf(APIError);
|
|
262
|
+
expect((errors[0]!.error as APIError).retryable).toBe(false);
|
|
263
|
+
|
|
264
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
265
|
+
expect(connectionCount).toBe(1);
|
|
266
|
+
} finally {
|
|
267
|
+
await session.close().catch(() => undefined);
|
|
268
|
+
await new Promise<void>((resolve, reject) => {
|
|
269
|
+
server.close((error) => (error ? reject(error) : resolve()));
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('raises non-retryable APIError on fatal server error events', () => {
|
|
275
|
+
const session = createErrorSession();
|
|
276
|
+
const errors: llm.RealtimeModelError[] = [];
|
|
277
|
+
session.on('error', (error) => errors.push(error));
|
|
278
|
+
|
|
279
|
+
expect(() =>
|
|
280
|
+
session.handleError({
|
|
281
|
+
type: 'error',
|
|
282
|
+
event_id: 'evt_quota',
|
|
283
|
+
error: {
|
|
284
|
+
type: 'invalid_request_error',
|
|
285
|
+
code: 'insufficient_quota',
|
|
286
|
+
message: 'quota exceeded',
|
|
287
|
+
param: '',
|
|
288
|
+
event_id: 'evt_quota',
|
|
289
|
+
},
|
|
290
|
+
}),
|
|
291
|
+
).toThrow(APIError);
|
|
292
|
+
expect(errors).toHaveLength(0);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('emits transient server error events as recoverable', () => {
|
|
296
|
+
const session = createErrorSession();
|
|
297
|
+
const errors: llm.RealtimeModelError[] = [];
|
|
298
|
+
session.on('error', (error) => errors.push(error));
|
|
299
|
+
|
|
300
|
+
session.handleError({
|
|
301
|
+
type: 'error',
|
|
302
|
+
event_id: 'evt_server_error',
|
|
303
|
+
error: {
|
|
304
|
+
type: 'server_error',
|
|
305
|
+
code: 'server_error',
|
|
306
|
+
message: 'server hiccup',
|
|
307
|
+
param: '',
|
|
308
|
+
event_id: 'evt_server_error',
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
expect(errors).toHaveLength(1);
|
|
313
|
+
expect(errors[0]!.recoverable).toBe(true);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('ignores cancellation failed server error events', () => {
|
|
317
|
+
const session = createErrorSession();
|
|
318
|
+
const errors: llm.RealtimeModelError[] = [];
|
|
319
|
+
session.on('error', (error) => errors.push(error));
|
|
320
|
+
|
|
321
|
+
session.handleError({
|
|
322
|
+
type: 'error',
|
|
323
|
+
event_id: 'evt_cancel_failed',
|
|
324
|
+
error: {
|
|
325
|
+
type: 'invalid_request_error',
|
|
326
|
+
message: 'Cancellation failed: no response',
|
|
327
|
+
param: '',
|
|
328
|
+
event_id: 'evt_cancel_failed',
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
expect(errors).toHaveLength(0);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it('raises non-retryable APIError on fatal response.done failures', () => {
|
|
336
|
+
const session = createResponseDoneSession();
|
|
337
|
+
const errors: llm.RealtimeModelError[] = [];
|
|
338
|
+
session.on('error', (error) => errors.push(error));
|
|
339
|
+
|
|
340
|
+
expect(() =>
|
|
341
|
+
session.handleResponseDoneButNotComplete({
|
|
342
|
+
type: 'response.done',
|
|
343
|
+
event_id: 'evt_response_failed',
|
|
344
|
+
response: {
|
|
345
|
+
id: 'resp_failed',
|
|
346
|
+
object: 'realtime.response',
|
|
347
|
+
status: 'failed',
|
|
348
|
+
status_details: {
|
|
349
|
+
type: 'failed',
|
|
350
|
+
error: {
|
|
351
|
+
code: 'insufficient_quota',
|
|
352
|
+
message: 'quota exceeded',
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
output: [],
|
|
356
|
+
},
|
|
357
|
+
}),
|
|
358
|
+
).toThrow(APIError);
|
|
359
|
+
expect(errors).toHaveLength(0);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it('emits transient response.done failures as recoverable', () => {
|
|
363
|
+
const session = createResponseDoneSession();
|
|
364
|
+
const errors: llm.RealtimeModelError[] = [];
|
|
365
|
+
session.on('error', (error) => errors.push(error));
|
|
366
|
+
|
|
367
|
+
session.handleResponseDoneButNotComplete({
|
|
368
|
+
type: 'response.done',
|
|
369
|
+
event_id: 'evt_response_failed',
|
|
370
|
+
response: {
|
|
371
|
+
id: 'resp_failed',
|
|
372
|
+
object: 'realtime.response',
|
|
373
|
+
status: 'failed',
|
|
374
|
+
status_details: {
|
|
375
|
+
type: 'failed',
|
|
376
|
+
error: {
|
|
377
|
+
code: 'rate_limit_exceeded',
|
|
378
|
+
message: 'rate limited',
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
output: [],
|
|
382
|
+
},
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
expect(errors).toHaveLength(1);
|
|
386
|
+
expect(errors[0]!.recoverable).toBe(true);
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
180
390
|
describe('RealtimeSession input_audio_transcription delta handling', () => {
|
|
181
391
|
type TranscriptionInternals = {
|
|
182
392
|
handleConversationItemInputAudioTranscriptionDelta: (
|
|
@@ -656,6 +866,61 @@ describe('livekitItemToOpenAIItem', () => {
|
|
|
656
866
|
});
|
|
657
867
|
});
|
|
658
868
|
|
|
869
|
+
describe('RealtimeSession chat context update events', () => {
|
|
870
|
+
type ChatCtxUpdateInternals = {
|
|
871
|
+
remoteChatCtx: llm.RemoteChatContext;
|
|
872
|
+
chatCtx: llm.ChatContext;
|
|
873
|
+
createChatCtxUpdateEvents: (
|
|
874
|
+
chatCtx: llm.ChatContext,
|
|
875
|
+
) => Promise<(api_proto.ConversationItemCreateEvent | api_proto.ConversationItemDeleteEvent)[]>;
|
|
876
|
+
};
|
|
877
|
+
|
|
878
|
+
function createChatCtxUpdateSession(remoteItem: llm.ChatMessage): ChatCtxUpdateInternals {
|
|
879
|
+
const session = Object.create(RealtimeSession.prototype) as ChatCtxUpdateInternals;
|
|
880
|
+
session.remoteChatCtx = new llm.RemoteChatContext();
|
|
881
|
+
session.remoteChatCtx.insert(undefined, remoteItem);
|
|
882
|
+
Object.defineProperty(session, 'chatCtx', {
|
|
883
|
+
get() {
|
|
884
|
+
return session.remoteChatCtx.toChatCtx();
|
|
885
|
+
},
|
|
886
|
+
});
|
|
887
|
+
return session;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
it('does not replace updated remote items with empty content', async () => {
|
|
891
|
+
const session = createChatCtxUpdateSession(
|
|
892
|
+
llm.ChatMessage.create({ id: 'item_1', role: 'assistant', content: [] }),
|
|
893
|
+
);
|
|
894
|
+
|
|
895
|
+
const events = await session.createChatCtxUpdateEvents(
|
|
896
|
+
new llm.ChatContext([
|
|
897
|
+
llm.ChatMessage.create({ id: 'item_1', role: 'assistant', content: ['hello'] }),
|
|
898
|
+
]),
|
|
899
|
+
);
|
|
900
|
+
|
|
901
|
+
expect(events).toEqual([]);
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
it('replaces updated remote text items', async () => {
|
|
905
|
+
const session = createChatCtxUpdateSession(
|
|
906
|
+
llm.ChatMessage.create({ id: 'item_1', role: 'assistant', content: ['old'] }),
|
|
907
|
+
);
|
|
908
|
+
|
|
909
|
+
const events = await session.createChatCtxUpdateEvents(
|
|
910
|
+
new llm.ChatContext([
|
|
911
|
+
llm.ChatMessage.create({ id: 'item_1', role: 'assistant', content: ['new'] }),
|
|
912
|
+
]),
|
|
913
|
+
);
|
|
914
|
+
|
|
915
|
+
expect(events.map((event) => event.type)).toEqual([
|
|
916
|
+
'conversation.item.delete',
|
|
917
|
+
'conversation.item.create',
|
|
918
|
+
]);
|
|
919
|
+
expect((events[0] as api_proto.ConversationItemDeleteEvent).item_id).toBe('item_1');
|
|
920
|
+
expect((events[1] as api_proto.ConversationItemCreateEvent).item.id).toBe('item_1');
|
|
921
|
+
});
|
|
922
|
+
});
|
|
923
|
+
|
|
659
924
|
describe('RealtimeSession.updateOptions', () => {
|
|
660
925
|
afterEach(() => {
|
|
661
926
|
vi.restoreAllMocks();
|