@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
package/dist/ws/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ws/types.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { z } from 'zod';\n\n// ============================================================================\n// Client → Server events\n// ============================================================================\n\nexport const wsResponseCreateEventSchema = z\n .object({\n type: z.literal('response.create'),\n model: z.string(),\n input: z.array(z.unknown()),\n tools: z.array(z.unknown()).optional(),\n previous_response_id: z.string().nullable().optional(),\n store: z.boolean().optional(),\n temperature: z.number().optional(),\n metadata: z.record(z.string(), z.string()).optional(),\n })\n .passthrough();\n\nexport type WsResponseCreateEvent = z.infer<typeof wsResponseCreateEventSchema>;\n\n// ============================================================================\n// Server → Client events\n// ============================================================================\n\nexport const wsResponseCreatedEventSchema = z.object({\n type: z.literal('response.created'),\n response: z\n .object({\n id: z.string(),\n })\n .passthrough(),\n});\n\nexport const wsFunctionCallItemSchema = z.object({\n type: z.literal('function_call'),\n call_id: z.string(),\n name: z.string(),\n arguments: z.string(),\n});\n\nexport const wsOutputItemSchema = z.discriminatedUnion('type', [\n wsFunctionCallItemSchema,\n z.object({ type: z.literal('message') }).passthrough(),\n z.object({ type: z.literal('reasoning') }).passthrough(),\n z.object({ type: z.literal('file') }).passthrough(),\n z.object({ type: z.literal('computer_call') }).passthrough(),\n z.object({ type: z.literal('web_search_call') }).passthrough(),\n]);\n\nexport const wsOutputItemDoneEventSchema = z.object({\n type: z.literal('response.output_item.done'),\n item: wsOutputItemSchema,\n});\n\nexport const wsOutputTextDeltaEventSchema = z.object({\n type: z.literal('response.output_text.delta'),\n delta: z.string(),\n});\n\nexport const wsResponseCompletedEventSchema = z.object({\n type: z.literal('response.completed'),\n response: z\n .object({\n id: z.string(),\n service_tier: z.string().nullable().optional(),\n usage: z\n .object({\n output_tokens: z.number(),\n input_tokens: z.number(),\n total_tokens: z.number(),\n input_tokens_details: z\n .object({\n cached_tokens: z.number(),\n })\n .passthrough(),\n })\n .optional(),\n })\n .passthrough(),\n});\n\nexport const wsResponseFailedEventSchema = z.object({\n type: z.literal('response.failed'),\n response: z\n .object({\n id: z.string().optional(),\n error: z\n .object({\n code: z.string().optional(),\n message: z.string().optional(),\n })\n .optional(),\n })\n .passthrough(),\n});\n\nexport const wsErrorEventSchema = z.object({\n type: z.literal('error'),\n status: z.number().optional(),\n error: z\n .object({\n type: z.string().optional(),\n code: z.string().optional(),\n message: z.string().optional(),\n param: z.string().optional(),\n })\n .optional(),\n message: z.string().optional(),\n});\n\nexport const wsServerEventSchema = z.discriminatedUnion('type', [\n wsResponseCreatedEventSchema,\n wsOutputItemDoneEventSchema,\n wsOutputTextDeltaEventSchema,\n wsResponseCompletedEventSchema,\n wsResponseFailedEventSchema,\n wsErrorEventSchema,\n]);\n\nexport type WsResponseCreatedEvent = z.infer<typeof wsResponseCreatedEventSchema>;\nexport type WsFunctionCallItem = z.infer<typeof wsFunctionCallItemSchema>;\nexport type WsOutputItem = z.infer<typeof wsOutputItemSchema>;\nexport type WsOutputItemDoneEvent = z.infer<typeof wsOutputItemDoneEventSchema>;\nexport type WsOutputTextDeltaEvent = z.infer<typeof wsOutputTextDeltaEventSchema>;\nexport type WsResponseCompletedEvent = z.infer<typeof wsResponseCompletedEventSchema>;\nexport type WsResponseFailedEvent = z.infer<typeof wsResponseFailedEventSchema>;\nexport type WsErrorEvent = z.infer<typeof wsErrorEventSchema>;\nexport type WsServerEvent = z.infer<typeof wsServerEventSchema>;\n"],"mappings":"AAGA,SAAS,SAAS;AAMX,MAAM,8BAA8B,EACxC,OAAO;AAAA,EACN,MAAM,EAAE,QAAQ,iBAAiB;AAAA,EACjC,OAAO,EAAE,OAAO;AAAA,EAChB,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC;AAAA,EAC1B,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,SAAS;AAAA,EACrC,sBAAsB,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS;AAAA,EACrD,OAAO,EAAE,QAAQ,EAAE,SAAS;AAAA,EAC5B,aAAa,EAAE,OAAO,EAAE,SAAS;AAAA,EACjC,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,OAAO,CAAC,EAAE,SAAS;AACtD,CAAC,EACA,YAAY;AAQR,MAAM,+BAA+B,EAAE,OAAO;AAAA,EACnD,MAAM,EAAE,QAAQ,kBAAkB;AAAA,EAClC,UAAU,EACP,OAAO;AAAA,IACN,IAAI,EAAE,OAAO;AAAA,EACf,CAAC,EACA,YAAY;AACjB,CAAC;AAEM,MAAM,2BAA2B,EAAE,OAAO;AAAA,EAC/C,MAAM,EAAE,QAAQ,eAAe;AAAA,EAC/B,SAAS,EAAE,OAAO;AAAA,EAClB,MAAM,EAAE,OAAO;AAAA,EACf,WAAW,EAAE,OAAO;AACtB,CAAC;AAEM,MAAM,qBAAqB,EAAE,mBAAmB,QAAQ;AAAA,EAC7D;AAAA,EACA,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,SAAS,EAAE,CAAC,EAAE,YAAY;AAAA,
|
|
1
|
+
{"version":3,"sources":["../../src/ws/types.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { z } from 'zod';\n\n// ============================================================================\n// Client → Server events\n// ============================================================================\n\nexport const wsResponseCreateEventSchema = z\n .object({\n type: z.literal('response.create'),\n model: z.string(),\n input: z.array(z.unknown()),\n tools: z.array(z.unknown()).optional(),\n previous_response_id: z.string().nullable().optional(),\n store: z.boolean().optional(),\n temperature: z.number().optional(),\n metadata: z.record(z.string(), z.string()).optional(),\n })\n .passthrough();\n\nexport type WsResponseCreateEvent = z.infer<typeof wsResponseCreateEventSchema>;\n\n// ============================================================================\n// Server → Client events\n// ============================================================================\n\nexport const wsResponseCreatedEventSchema = z.object({\n type: z.literal('response.created'),\n response: z\n .object({\n id: z.string(),\n })\n .passthrough(),\n});\n\nexport const wsFunctionCallItemSchema = z.object({\n type: z.literal('function_call'),\n call_id: z.string(),\n name: z.string(),\n arguments: z.string(),\n});\n\nexport const wsOutputItemSchema = z.discriminatedUnion('type', [\n wsFunctionCallItemSchema,\n z.object({ type: z.literal('message'), phase: z.string().optional() }).passthrough(),\n z.object({ type: z.literal('reasoning') }).passthrough(),\n z.object({ type: z.literal('file') }).passthrough(),\n z.object({ type: z.literal('computer_call') }).passthrough(),\n z.object({ type: z.literal('web_search_call') }).passthrough(),\n]);\n\nexport const wsOutputItemDoneEventSchema = z.object({\n type: z.literal('response.output_item.done'),\n item: wsOutputItemSchema,\n});\n\nexport const wsOutputTextDeltaEventSchema = z.object({\n type: z.literal('response.output_text.delta'),\n delta: z.string(),\n});\n\nexport const wsResponseCompletedEventSchema = z.object({\n type: z.literal('response.completed'),\n response: z\n .object({\n id: z.string(),\n service_tier: z.string().nullable().optional(),\n usage: z\n .object({\n output_tokens: z.number(),\n input_tokens: z.number(),\n total_tokens: z.number(),\n input_tokens_details: z\n .object({\n cached_tokens: z.number(),\n })\n .passthrough(),\n })\n .optional(),\n })\n .passthrough(),\n});\n\nexport const wsResponseFailedEventSchema = z.object({\n type: z.literal('response.failed'),\n response: z\n .object({\n id: z.string().optional(),\n error: z\n .object({\n code: z.string().optional(),\n message: z.string().optional(),\n })\n .optional(),\n })\n .passthrough(),\n});\n\nexport const wsErrorEventSchema = z.object({\n type: z.literal('error'),\n status: z.number().optional(),\n error: z\n .object({\n type: z.string().optional(),\n code: z.string().optional(),\n message: z.string().optional(),\n param: z.string().optional(),\n })\n .optional(),\n message: z.string().optional(),\n});\n\nexport const wsServerEventSchema = z.discriminatedUnion('type', [\n wsResponseCreatedEventSchema,\n wsOutputItemDoneEventSchema,\n wsOutputTextDeltaEventSchema,\n wsResponseCompletedEventSchema,\n wsResponseFailedEventSchema,\n wsErrorEventSchema,\n]);\n\nexport type WsResponseCreatedEvent = z.infer<typeof wsResponseCreatedEventSchema>;\nexport type WsFunctionCallItem = z.infer<typeof wsFunctionCallItemSchema>;\nexport type WsOutputItem = z.infer<typeof wsOutputItemSchema>;\nexport type WsOutputItemDoneEvent = z.infer<typeof wsOutputItemDoneEventSchema>;\nexport type WsOutputTextDeltaEvent = z.infer<typeof wsOutputTextDeltaEventSchema>;\nexport type WsResponseCompletedEvent = z.infer<typeof wsResponseCompletedEventSchema>;\nexport type WsResponseFailedEvent = z.infer<typeof wsResponseFailedEventSchema>;\nexport type WsErrorEvent = z.infer<typeof wsErrorEventSchema>;\nexport type WsServerEvent = z.infer<typeof wsServerEventSchema>;\n"],"mappings":"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","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livekit/agents-plugin-openai",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.6",
|
|
4
4
|
"description": "OpenAI plugin for LiveKit Node Agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"require": "dist/index.cjs",
|
|
@@ -25,14 +25,14 @@
|
|
|
25
25
|
"README.md"
|
|
26
26
|
],
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@livekit/rtc-node": "^0.13.
|
|
28
|
+
"@livekit/rtc-node": "^0.13.29",
|
|
29
29
|
"@microsoft/api-extractor": "^7.35.0",
|
|
30
30
|
"@types/ws": "^8.5.10",
|
|
31
31
|
"tsup": "^8.3.5",
|
|
32
32
|
"typescript": "^5.0.0",
|
|
33
|
-
"@livekit/agents": "1.4.
|
|
34
|
-
"@livekit/agents-plugin-silero": "1.4.
|
|
35
|
-
"@livekit/agents-plugins-test": "1.4.
|
|
33
|
+
"@livekit/agents": "1.4.6",
|
|
34
|
+
"@livekit/agents-plugin-silero": "1.4.6",
|
|
35
|
+
"@livekit/agents-plugins-test": "1.4.6"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@livekit/mutex": "^1.1.1",
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"ws": "^8.18.0"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@livekit/rtc-node": "^0.13.
|
|
43
|
+
"@livekit/rtc-node": "^0.13.29",
|
|
44
44
|
"zod": "^3.25.76 || ^4.1.8",
|
|
45
|
-
"@livekit/agents": "1.4.
|
|
45
|
+
"@livekit/agents": "1.4.6"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "tsup --onSuccess \"pnpm build:types\"",
|
|
@@ -10,6 +10,18 @@ export const OUT_FRAME_SIZE = 1200; // 50ms
|
|
|
10
10
|
export const BASE_URL = 'wss://api.openai.com/v1';
|
|
11
11
|
|
|
12
12
|
export type Model = 'gpt-4o-realtime-preview-2024-10-01' | string; // Open-ended, for future models
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Models that support the `reasoning` configuration on the Realtime API.
|
|
16
|
+
* Currently only the `gpt-realtime-2` family supports it.
|
|
17
|
+
*
|
|
18
|
+
* Ref: https://developers.openai.com/api/reference/resources/realtime/subresources/calls/methods/accept
|
|
19
|
+
*/
|
|
20
|
+
export type ReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'xhigh';
|
|
21
|
+
|
|
22
|
+
export interface Reasoning {
|
|
23
|
+
effort?: ReasoningEffort;
|
|
24
|
+
}
|
|
13
25
|
export type Voice =
|
|
14
26
|
| 'alloy'
|
|
15
27
|
| 'shimmer'
|
|
@@ -58,6 +70,7 @@ export type ServerEventType =
|
|
|
58
70
|
| 'input_audio_buffer.speech_stopped'
|
|
59
71
|
| 'conversation.item.added' // GA: renamed from conversation.item.created
|
|
60
72
|
| 'conversation.item.created' // Beta: kept for backward compatibility
|
|
73
|
+
| 'conversation.item.input_audio_transcription.delta'
|
|
61
74
|
| 'conversation.item.input_audio_transcription.completed'
|
|
62
75
|
| 'conversation.item.input_audio_transcription.failed'
|
|
63
76
|
| 'conversation.item.truncated'
|
|
@@ -267,6 +280,7 @@ export interface ConversationResource {
|
|
|
267
280
|
}
|
|
268
281
|
|
|
269
282
|
export type ResponseStatusDetails =
|
|
283
|
+
| string
|
|
270
284
|
| {
|
|
271
285
|
type: 'incomplete';
|
|
272
286
|
reason: 'max_output_tokens' | 'content_filter' | string;
|
|
@@ -328,6 +342,7 @@ export interface SessionUpdateEvent extends BaseClientEvent {
|
|
|
328
342
|
audio?: RealtimeAudioConfig; // GA: nested audio config
|
|
329
343
|
max_output_tokens?: number | 'inf'; // GA: renamed from max_response_output_tokens
|
|
330
344
|
tracing?: TracingConfig | null; // GA: tracing config
|
|
345
|
+
reasoning?: Reasoning | null; // GA: reasoning config (gpt-realtime-2 only)
|
|
331
346
|
// Common fields
|
|
332
347
|
model: Model;
|
|
333
348
|
instructions: string;
|
|
@@ -509,6 +524,13 @@ export interface ConversationItemAddedEvent extends BaseServerEvent {
|
|
|
509
524
|
item: ItemResource;
|
|
510
525
|
}
|
|
511
526
|
|
|
527
|
+
export interface ConversationItemInputAudioTranscriptionDeltaEvent extends BaseServerEvent {
|
|
528
|
+
type: 'conversation.item.input_audio_transcription.delta';
|
|
529
|
+
item_id: string;
|
|
530
|
+
content_index?: number;
|
|
531
|
+
delta?: string;
|
|
532
|
+
}
|
|
533
|
+
|
|
512
534
|
export interface ConversationItemInputAudioTranscriptionCompletedEvent extends BaseServerEvent {
|
|
513
535
|
type: 'conversation.item.input_audio_transcription.completed';
|
|
514
536
|
item_id: string;
|
|
@@ -670,6 +692,7 @@ export type ServerEvent =
|
|
|
670
692
|
| InputAudioBufferSpeechStoppedEvent
|
|
671
693
|
| ConversationItemCreatedEvent
|
|
672
694
|
| ConversationItemAddedEvent // GA: renamed from conversation.item.created
|
|
695
|
+
| ConversationItemInputAudioTranscriptionDeltaEvent
|
|
673
696
|
| ConversationItemInputAudioTranscriptionCompletedEvent
|
|
674
697
|
| ConversationItemInputAudioTranscriptionFailedEvent
|
|
675
698
|
| ConversationItemTruncatedEvent
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
import { llm } from '@livekit/agents';
|
|
4
|
+
import { APIError, Future, Task, llm, stream } from '@livekit/agents';
|
|
5
5
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
6
6
|
import type * as api_proto from './api_proto.js';
|
|
7
7
|
import {
|
|
@@ -18,6 +18,19 @@ type RealtimeSessionInternals = {
|
|
|
18
18
|
textModeRecoveryRetries: number;
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
+
type ResponseDoneSessionInternals = {
|
|
22
|
+
handleResponseDone: (event: api_proto.ResponseDoneEvent) => void;
|
|
23
|
+
on: (event: 'error', listener: (error: llm.RealtimeModelError) => void) => void;
|
|
24
|
+
currentGeneration: {
|
|
25
|
+
messageChannel: stream.StreamChannel<llm.MessageGeneration>;
|
|
26
|
+
functionChannel: stream.StreamChannel<llm.FunctionCall>;
|
|
27
|
+
messages: Map<string, never>;
|
|
28
|
+
_doneFut: Future;
|
|
29
|
+
_createdTimestamp: number;
|
|
30
|
+
_firstTokenTimestamp?: number;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
21
34
|
function createSessionForTest(): RealtimeSessionInternals {
|
|
22
35
|
const session = Object.create(RealtimeSession.prototype) as RealtimeSessionInternals;
|
|
23
36
|
session.responseCreatedFutures = {};
|
|
@@ -26,6 +39,15 @@ function createSessionForTest(): RealtimeSessionInternals {
|
|
|
26
39
|
return session;
|
|
27
40
|
}
|
|
28
41
|
|
|
42
|
+
function stubTaskRuntime(): void {
|
|
43
|
+
// Prevent background realtime tasks from opening network connections in unit tests.
|
|
44
|
+
vi.spyOn(Task, 'from').mockReturnValue({
|
|
45
|
+
cancel: vi.fn(),
|
|
46
|
+
done: true,
|
|
47
|
+
result: Promise.resolve(undefined),
|
|
48
|
+
} as unknown as Task<void>);
|
|
49
|
+
}
|
|
50
|
+
|
|
29
51
|
describe('RealtimeSession.generateReply', () => {
|
|
30
52
|
it('cancels an in-flight response when aborted before response.created', async () => {
|
|
31
53
|
const session = createSessionForTest();
|
|
@@ -43,6 +65,322 @@ describe('RealtimeSession.generateReply', () => {
|
|
|
43
65
|
});
|
|
44
66
|
});
|
|
45
67
|
|
|
68
|
+
describe('RealtimeSession response.done status handling', () => {
|
|
69
|
+
afterEach(() => {
|
|
70
|
+
vi.restoreAllMocks();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
function createResponseDoneSession(): ResponseDoneSessionInternals {
|
|
74
|
+
stubTaskRuntime();
|
|
75
|
+
|
|
76
|
+
const model = new RealtimeModel({ apiKey: 'test-key' });
|
|
77
|
+
const session = model.session() as unknown as ResponseDoneSessionInternals;
|
|
78
|
+
session.currentGeneration = {
|
|
79
|
+
messageChannel: stream.createStreamChannel<llm.MessageGeneration>(),
|
|
80
|
+
functionChannel: stream.createStreamChannel<llm.FunctionCall>(),
|
|
81
|
+
messages: new Map<string, never>(),
|
|
82
|
+
_doneFut: new Future(),
|
|
83
|
+
_createdTimestamp: Date.now(),
|
|
84
|
+
};
|
|
85
|
+
return session;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
it('emits a recoverable APIError when response.done reports failed', () => {
|
|
89
|
+
const session = createResponseDoneSession();
|
|
90
|
+
const errors: llm.RealtimeModelError[] = [];
|
|
91
|
+
session.on('error', (error) => errors.push(error));
|
|
92
|
+
|
|
93
|
+
session.handleResponseDone({
|
|
94
|
+
type: 'response.done',
|
|
95
|
+
event_id: 'evt_response_failed',
|
|
96
|
+
response: {
|
|
97
|
+
id: 'resp_failed',
|
|
98
|
+
object: 'realtime.response',
|
|
99
|
+
status: 'failed',
|
|
100
|
+
status_details: {
|
|
101
|
+
type: 'failed',
|
|
102
|
+
error: {
|
|
103
|
+
code: 'rate_limit_exceeded',
|
|
104
|
+
message: 'rate limited',
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
output: [],
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(errors).toHaveLength(1);
|
|
112
|
+
expect(errors[0]!.recoverable).toBe(true);
|
|
113
|
+
expect(errors[0]!.error).toBeInstanceOf(APIError);
|
|
114
|
+
expect(errors[0]!.error.message).toBe(
|
|
115
|
+
'OpenAI Realtime API response failed with error type: unknown',
|
|
116
|
+
);
|
|
117
|
+
expect((errors[0]!.error as APIError).body).toEqual({
|
|
118
|
+
code: 'rate_limit_exceeded',
|
|
119
|
+
message: 'rate limited',
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('handles string response.done status details', () => {
|
|
124
|
+
const session = createResponseDoneSession();
|
|
125
|
+
|
|
126
|
+
expect(() =>
|
|
127
|
+
session.handleResponseDone({
|
|
128
|
+
type: 'response.done',
|
|
129
|
+
event_id: 'evt_response_incomplete',
|
|
130
|
+
response: {
|
|
131
|
+
id: 'resp_incomplete',
|
|
132
|
+
object: 'realtime.response',
|
|
133
|
+
status: 'incomplete',
|
|
134
|
+
status_details: 'incomplete',
|
|
135
|
+
output: [],
|
|
136
|
+
},
|
|
137
|
+
}),
|
|
138
|
+
).not.toThrow();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe('RealtimeSession input_audio_transcription delta handling', () => {
|
|
143
|
+
type TranscriptionInternals = {
|
|
144
|
+
handleConversationItemInputAudioTranscriptionDelta: (
|
|
145
|
+
ev: api_proto.ConversationItemInputAudioTranscriptionDeltaEvent,
|
|
146
|
+
) => void;
|
|
147
|
+
handleConversationItemInputAudioTranscriptionCompleted: (
|
|
148
|
+
ev: api_proto.ConversationItemInputAudioTranscriptionCompletedEvent,
|
|
149
|
+
) => void;
|
|
150
|
+
finalizePartialOnTranscriptionFailure: (itemId: string, contentIndex: number) => void;
|
|
151
|
+
handleConversationItemDeleted: (ev: api_proto.ConversationItemDeletedEvent) => void;
|
|
152
|
+
inputTranscriptAccumulators: Map<string, Map<number, string>>;
|
|
153
|
+
audioCapableItemIds: Set<string>;
|
|
154
|
+
itemDeleteFutures: Record<string, never>;
|
|
155
|
+
remoteChatCtx: {
|
|
156
|
+
get: (id: string) => { item: llm.ChatMessage } | undefined;
|
|
157
|
+
delete: (id: string) => void;
|
|
158
|
+
};
|
|
159
|
+
on: (event: string, listener: (payload: llm.InputTranscriptionCompleted) => void) => void;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
function createTranscriptSession(opts?: {
|
|
163
|
+
chatItems?: Record<string, llm.ChatMessage>;
|
|
164
|
+
}): TranscriptionInternals {
|
|
165
|
+
const session = Object.create(RealtimeSession.prototype) as TranscriptionInternals;
|
|
166
|
+
session.inputTranscriptAccumulators = new Map<string, Map<number, string>>();
|
|
167
|
+
session.audioCapableItemIds = new Set<string>();
|
|
168
|
+
session.itemDeleteFutures = {};
|
|
169
|
+
const chatItems = new Map<string, llm.ChatMessage>(Object.entries(opts?.chatItems ?? {}));
|
|
170
|
+
session.remoteChatCtx = {
|
|
171
|
+
get: (id) => {
|
|
172
|
+
const item = chatItems.get(id);
|
|
173
|
+
return item ? { item } : undefined;
|
|
174
|
+
},
|
|
175
|
+
delete: (id) => {
|
|
176
|
+
chatItems.delete(id);
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
return session;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function delta(
|
|
183
|
+
item_id: string,
|
|
184
|
+
delta: string,
|
|
185
|
+
content_index = 0,
|
|
186
|
+
): api_proto.ConversationItemInputAudioTranscriptionDeltaEvent {
|
|
187
|
+
return {
|
|
188
|
+
type: 'conversation.item.input_audio_transcription.delta',
|
|
189
|
+
event_id: `evt_${item_id}_${delta}_${content_index}`,
|
|
190
|
+
item_id,
|
|
191
|
+
content_index,
|
|
192
|
+
delta,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function completed(
|
|
197
|
+
item_id: string,
|
|
198
|
+
transcript: string,
|
|
199
|
+
content_index = 0,
|
|
200
|
+
): api_proto.ConversationItemInputAudioTranscriptionCompletedEvent {
|
|
201
|
+
return {
|
|
202
|
+
type: 'conversation.item.input_audio_transcription.completed',
|
|
203
|
+
event_id: `evt_${item_id}_done`,
|
|
204
|
+
item_id,
|
|
205
|
+
content_index,
|
|
206
|
+
transcript,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
it('accumulates partial transcripts across delta events for the same item_id', () => {
|
|
211
|
+
const session = createTranscriptSession();
|
|
212
|
+
const emissions: llm.InputTranscriptionCompleted[] = [];
|
|
213
|
+
session.on('input_audio_transcription_completed', (ev) => emissions.push(ev));
|
|
214
|
+
|
|
215
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'Hello'));
|
|
216
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', ', world'));
|
|
217
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', '!'));
|
|
218
|
+
|
|
219
|
+
expect(emissions).toEqual([
|
|
220
|
+
{ itemId: 'item_a', transcript: 'Hello', isFinal: false },
|
|
221
|
+
{ itemId: 'item_a', transcript: 'Hello, world', isFinal: false },
|
|
222
|
+
{ itemId: 'item_a', transcript: 'Hello, world!', isFinal: false },
|
|
223
|
+
]);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('keeps accumulators isolated across concurrent item_ids', () => {
|
|
227
|
+
const session = createTranscriptSession();
|
|
228
|
+
const emissions: llm.InputTranscriptionCompleted[] = [];
|
|
229
|
+
session.on('input_audio_transcription_completed', (ev) => emissions.push(ev));
|
|
230
|
+
|
|
231
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'Aaa'));
|
|
232
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_b', 'Bbb'));
|
|
233
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'Aaa'));
|
|
234
|
+
|
|
235
|
+
expect(emissions).toEqual([
|
|
236
|
+
{ itemId: 'item_a', transcript: 'Aaa', isFinal: false },
|
|
237
|
+
{ itemId: 'item_b', transcript: 'Bbb', isFinal: false },
|
|
238
|
+
{ itemId: 'item_a', transcript: 'AaaAaa', isFinal: false },
|
|
239
|
+
]);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('keeps accumulators isolated across content_index within the same item_id', () => {
|
|
243
|
+
const session = createTranscriptSession();
|
|
244
|
+
const emissions: llm.InputTranscriptionCompleted[] = [];
|
|
245
|
+
session.on('input_audio_transcription_completed', (ev) => emissions.push(ev));
|
|
246
|
+
|
|
247
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'idx0-', 0));
|
|
248
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'idx1-', 1));
|
|
249
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'more0', 0));
|
|
250
|
+
|
|
251
|
+
expect(emissions).toEqual([
|
|
252
|
+
{ itemId: 'item_a', transcript: 'idx0-', isFinal: false },
|
|
253
|
+
{ itemId: 'item_a', transcript: 'idx1-', isFinal: false },
|
|
254
|
+
{ itemId: 'item_a', transcript: 'idx0-more0', isFinal: false },
|
|
255
|
+
]);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('clears the accumulator on .completed so a subsequent delta does not inherit prior state', () => {
|
|
259
|
+
const session = createTranscriptSession({
|
|
260
|
+
chatItems: { item_a: new llm.ChatMessage({ role: 'user', content: '', id: 'item_a' }) },
|
|
261
|
+
});
|
|
262
|
+
const emissions: llm.InputTranscriptionCompleted[] = [];
|
|
263
|
+
session.on('input_audio_transcription_completed', (ev) => emissions.push(ev));
|
|
264
|
+
|
|
265
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'first turn '));
|
|
266
|
+
session.handleConversationItemInputAudioTranscriptionCompleted(
|
|
267
|
+
completed('item_a', 'first turn complete.'),
|
|
268
|
+
);
|
|
269
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'second'));
|
|
270
|
+
|
|
271
|
+
expect(session.inputTranscriptAccumulators.get('item_a')?.get(0)).toBe('second');
|
|
272
|
+
expect(emissions).toEqual([
|
|
273
|
+
{ itemId: 'item_a', transcript: 'first turn ', isFinal: false },
|
|
274
|
+
{ itemId: 'item_a', transcript: 'first turn complete.', isFinal: true },
|
|
275
|
+
{ itemId: 'item_a', transcript: 'second', isFinal: false },
|
|
276
|
+
]);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('pushes the final transcript onto the matching ChatMessage exactly once on .completed', () => {
|
|
280
|
+
const chatMessage = new llm.ChatMessage({ role: 'user', content: '', id: 'item_a' });
|
|
281
|
+
const session = createTranscriptSession({ chatItems: { item_a: chatMessage } });
|
|
282
|
+
|
|
283
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'partial-only'));
|
|
284
|
+
expect(chatMessage.content).toEqual(['']);
|
|
285
|
+
|
|
286
|
+
session.handleConversationItemInputAudioTranscriptionCompleted(completed('item_a', 'final.'));
|
|
287
|
+
expect(chatMessage.content).toEqual(['', 'final.']);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('emits isFinal:true on .completed even when remoteChatCtx has no matching item', () => {
|
|
291
|
+
const session = createTranscriptSession();
|
|
292
|
+
const emissions: llm.InputTranscriptionCompleted[] = [];
|
|
293
|
+
session.on('input_audio_transcription_completed', (ev) => emissions.push(ev));
|
|
294
|
+
|
|
295
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'partial'));
|
|
296
|
+
session.handleConversationItemInputAudioTranscriptionCompleted(completed('item_a', 'final.'));
|
|
297
|
+
|
|
298
|
+
expect(emissions).toEqual([
|
|
299
|
+
{ itemId: 'item_a', transcript: 'partial', isFinal: false },
|
|
300
|
+
{ itemId: 'item_a', transcript: 'final.', isFinal: true },
|
|
301
|
+
]);
|
|
302
|
+
expect(session.inputTranscriptAccumulators.size).toBe(0);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('treats .completed as a no-op cleanup when no deltas preceded it (non-streaming STT model)', () => {
|
|
306
|
+
const session = createTranscriptSession({
|
|
307
|
+
chatItems: { item_a: new llm.ChatMessage({ role: 'user', content: '', id: 'item_a' }) },
|
|
308
|
+
});
|
|
309
|
+
const emissions: llm.InputTranscriptionCompleted[] = [];
|
|
310
|
+
session.on('input_audio_transcription_completed', (ev) => emissions.push(ev));
|
|
311
|
+
|
|
312
|
+
session.handleConversationItemInputAudioTranscriptionCompleted(
|
|
313
|
+
completed('item_a', 'one-shot whisper-1 transcript'),
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
expect(session.inputTranscriptAccumulators.size).toBe(0);
|
|
317
|
+
expect(emissions).toEqual([
|
|
318
|
+
{ itemId: 'item_a', transcript: 'one-shot whisper-1 transcript', isFinal: true },
|
|
319
|
+
]);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it('skips emission for empty or missing deltas and does not create an accumulator', () => {
|
|
323
|
+
const session = createTranscriptSession();
|
|
324
|
+
const emissions: llm.InputTranscriptionCompleted[] = [];
|
|
325
|
+
session.on('input_audio_transcription_completed', (ev) => emissions.push(ev));
|
|
326
|
+
|
|
327
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', ''));
|
|
328
|
+
session.handleConversationItemInputAudioTranscriptionDelta({
|
|
329
|
+
type: 'conversation.item.input_audio_transcription.delta',
|
|
330
|
+
event_id: 'evt_no_delta',
|
|
331
|
+
item_id: 'item_a',
|
|
332
|
+
});
|
|
333
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'real'));
|
|
334
|
+
|
|
335
|
+
expect(session.inputTranscriptAccumulators.get('item_a')?.get(0)).toBe('real');
|
|
336
|
+
expect(emissions).toEqual([{ itemId: 'item_a', transcript: 'real', isFinal: false }]);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('clears the accumulator and emits a closing isFinal:true on transcription failure when partials had streamed', () => {
|
|
340
|
+
const session = createTranscriptSession();
|
|
341
|
+
const emissions: llm.InputTranscriptionCompleted[] = [];
|
|
342
|
+
session.on('input_audio_transcription_completed', (ev) => emissions.push(ev));
|
|
343
|
+
|
|
344
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'partial '));
|
|
345
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'text'));
|
|
346
|
+
session.finalizePartialOnTranscriptionFailure('item_a', 0);
|
|
347
|
+
|
|
348
|
+
expect(session.inputTranscriptAccumulators.size).toBe(0);
|
|
349
|
+
expect(emissions).toEqual([
|
|
350
|
+
{ itemId: 'item_a', transcript: 'partial ', isFinal: false },
|
|
351
|
+
{ itemId: 'item_a', transcript: 'partial text', isFinal: false },
|
|
352
|
+
{ itemId: 'item_a', transcript: 'partial text', isFinal: true },
|
|
353
|
+
]);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('emits nothing on transcription failure when no partials had streamed for that item', () => {
|
|
357
|
+
const session = createTranscriptSession();
|
|
358
|
+
const emissions: llm.InputTranscriptionCompleted[] = [];
|
|
359
|
+
session.on('input_audio_transcription_completed', (ev) => emissions.push(ev));
|
|
360
|
+
|
|
361
|
+
session.finalizePartialOnTranscriptionFailure('item_a', 0);
|
|
362
|
+
|
|
363
|
+
expect(emissions).toEqual([]);
|
|
364
|
+
expect(session.inputTranscriptAccumulators.size).toBe(0);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it('clears the accumulator when the conversation item is deleted', () => {
|
|
368
|
+
const session = createTranscriptSession();
|
|
369
|
+
|
|
370
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'partial '));
|
|
371
|
+
session.handleConversationItemInputAudioTranscriptionDelta(delta('item_a', 'more', 1));
|
|
372
|
+
expect(session.inputTranscriptAccumulators.get('item_a')?.size).toBe(2);
|
|
373
|
+
|
|
374
|
+
session.handleConversationItemDeleted({
|
|
375
|
+
type: 'conversation.item.deleted',
|
|
376
|
+
event_id: 'evt_del',
|
|
377
|
+
item_id: 'item_a',
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
expect(session.inputTranscriptAccumulators.has('item_a')).toBe(false);
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
|
|
46
384
|
describe('livekitItemToOpenAIItem', () => {
|
|
47
385
|
describe('message items', () => {
|
|
48
386
|
it('should use output_text type for assistant messages', async () => {
|
|
@@ -285,17 +623,6 @@ describe('RealtimeSession.updateOptions', () => {
|
|
|
285
623
|
vi.restoreAllMocks();
|
|
286
624
|
});
|
|
287
625
|
|
|
288
|
-
const stubTaskRuntime = () => {
|
|
289
|
-
// Prevent background realtime tasks from opening network connections in unit tests.
|
|
290
|
-
vi.spyOn(llm, 'RealtimeSession', 'get');
|
|
291
|
-
const agentsModule = require('@livekit/agents') as typeof import('@livekit/agents'); // eslint-disable-line @typescript-eslint/no-var-requires
|
|
292
|
-
vi.spyOn(agentsModule.Task, 'from').mockReturnValue({
|
|
293
|
-
cancel: vi.fn(),
|
|
294
|
-
done: true,
|
|
295
|
-
result: Promise.resolve(undefined),
|
|
296
|
-
} as unknown as import('@livekit/agents').Task<void>);
|
|
297
|
-
};
|
|
298
|
-
|
|
299
626
|
it('emits session.update when toolChoice changes', () => {
|
|
300
627
|
stubTaskRuntime();
|
|
301
628
|
|