@livekit/agents-plugin-openai 1.6.2 → 1.6.4
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 +1 -1
- package/dist/llm.cjs.map +1 -1
- package/dist/llm.js +1 -1
- package/dist/llm.js.map +1 -1
- package/dist/llm.test.cjs +60 -0
- package/dist/llm.test.cjs.map +1 -1
- package/dist/llm.test.js +40 -2
- 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 +49 -3
- package/src/llm.ts +1 -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
package/src/ws/llm.test.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
import { initializeLogger } from '@livekit/agents';
|
|
4
|
+
import { ChatContext, ConnectionPool, initializeLogger, stream } from '@livekit/agents';
|
|
5
5
|
import { llm, llmStrict } from '@livekit/agents-plugins-test';
|
|
6
6
|
import { describe, expect, it } from 'vitest';
|
|
7
7
|
import { LLM } from '../responses/llm.js';
|
|
8
|
-
import {
|
|
8
|
+
import type { ResponsesWebSocket } from './llm.js';
|
|
9
|
+
import { WSLLM, WSLLMStream, buildResponsesWsUrl } from './llm.js';
|
|
10
|
+
import type { WsServerEvent } from './types.js';
|
|
9
11
|
|
|
10
12
|
initializeLogger({ level: 'silent', pretty: false });
|
|
11
13
|
|
|
@@ -81,6 +83,96 @@ describe('buildResponsesWsUrl', () => {
|
|
|
81
83
|
});
|
|
82
84
|
});
|
|
83
85
|
|
|
86
|
+
// opens every stream, on every provider
|
|
87
|
+
const RESPONSE_CREATED: WsServerEvent = {
|
|
88
|
+
type: 'response.created',
|
|
89
|
+
response: { id: 'resp_1' },
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// a reasoning phase marker: a message item with no text of its own
|
|
93
|
+
const PHASE_METADATA: WsServerEvent = {
|
|
94
|
+
type: 'response.output_item.done',
|
|
95
|
+
item: { type: 'message', phase: 'thinking' },
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const TEXT_DELTA: WsServerEvent = {
|
|
99
|
+
type: 'response.output_text.delta',
|
|
100
|
+
delta: 'hello',
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/** Replays the given frames, then dies the way a socket going quiet does. */
|
|
104
|
+
class StallingConnection {
|
|
105
|
+
attempts = 0;
|
|
106
|
+
|
|
107
|
+
constructor(private readonly frames: WsServerEvent[]) {}
|
|
108
|
+
|
|
109
|
+
sendRequest(): stream.StreamChannel<WsServerEvent> {
|
|
110
|
+
this.attempts++;
|
|
111
|
+
const channel = stream.createStreamChannel<WsServerEvent>();
|
|
112
|
+
|
|
113
|
+
void (async () => {
|
|
114
|
+
for (const frame of this.frames) {
|
|
115
|
+
await channel.write(frame);
|
|
116
|
+
}
|
|
117
|
+
await channel.abort(new Error('stalled mid-stream'));
|
|
118
|
+
})();
|
|
119
|
+
|
|
120
|
+
return channel;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
close(): void {}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Returns how many times the response was requested before the stream gave up. */
|
|
127
|
+
async function attemptsUntilStall(frames: WsServerEvent[], maxRetry: number): Promise<number> {
|
|
128
|
+
const wsLLM = new WSLLM({ model: 'gpt-4.1', apiKey: 'test-key' });
|
|
129
|
+
// the stream emits 'error' on every failed attempt; without a listener the
|
|
130
|
+
// EventEmitter turns it into an unhandled error and fails the run
|
|
131
|
+
wsLLM.on('error', () => {});
|
|
132
|
+
|
|
133
|
+
const conn = new StallingConnection(frames);
|
|
134
|
+
const pool = new ConnectionPool<ResponsesWebSocket>({
|
|
135
|
+
connectCb: async () => conn as unknown as ResponsesWebSocket,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const chatCtx = ChatContext.empty();
|
|
139
|
+
chatCtx.addMessage({ role: 'user', content: 'hi' });
|
|
140
|
+
|
|
141
|
+
const llmStream = new WSLLMStream(wsLLM, {
|
|
142
|
+
pool,
|
|
143
|
+
model: 'gpt-4.1',
|
|
144
|
+
chatCtx,
|
|
145
|
+
fullChatCtx: chatCtx,
|
|
146
|
+
connOptions: { maxRetry, retryIntervalMs: 0, timeoutMs: 5000 },
|
|
147
|
+
modelOptions: {},
|
|
148
|
+
strictToolSchema: true,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
for await (const _chunk of llmStream) {
|
|
153
|
+
void _chunk;
|
|
154
|
+
}
|
|
155
|
+
} catch {
|
|
156
|
+
// the stall surfaces through the 'error' event; the iterator just ends
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return conn.attempts;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
describe('OpenAI Responses WS retry eligibility', () => {
|
|
163
|
+
it('retries a stall that follows the stream-opening frame alone', async () => {
|
|
164
|
+
expect(await attemptsUntilStall([RESPONSE_CREATED], 2)).toBe(3);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('retries a stall that follows a phase marker', async () => {
|
|
168
|
+
expect(await attemptsUntilStall([RESPONSE_CREATED, PHASE_METADATA], 2)).toBe(3);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('does not retry once generated text has reached the caller', async () => {
|
|
172
|
+
expect(await attemptsUntilStall([RESPONSE_CREATED, TEXT_DELTA], 2)).toBe(1);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
84
176
|
if (hasOpenAIApiKey) {
|
|
85
177
|
describe('OpenAI Responses WS wrapper', async () => {
|
|
86
178
|
await llm(
|
package/src/ws/llm.ts
CHANGED
|
@@ -393,6 +393,12 @@ export class WSLLMStream extends llm.LLMStream {
|
|
|
393
393
|
#fullChatCtx: llm.ChatContext;
|
|
394
394
|
#responseId = '';
|
|
395
395
|
#pendingToolCalls = new Set<string>();
|
|
396
|
+
/**
|
|
397
|
+
* Whether this attempt may still be retried. Cleared once generation reaches the
|
|
398
|
+
* caller, which a retry would duplicate. Held on the instance because the loop
|
|
399
|
+
* that reads chunks sits in #runWithConn, one call down from run().
|
|
400
|
+
*/
|
|
401
|
+
#retryable = true;
|
|
396
402
|
|
|
397
403
|
constructor(
|
|
398
404
|
llm: WSLLM,
|
|
@@ -429,7 +435,7 @@ export class WSLLMStream extends llm.LLMStream {
|
|
|
429
435
|
}
|
|
430
436
|
|
|
431
437
|
protected async run(): Promise<void> {
|
|
432
|
-
|
|
438
|
+
this.#retryable = true;
|
|
433
439
|
|
|
434
440
|
try {
|
|
435
441
|
await this.#pool.withConnection(async (conn: ResponsesWebSocket) => {
|
|
@@ -438,7 +444,7 @@ export class WSLLMStream extends llm.LLMStream {
|
|
|
438
444
|
if (needsRetry) {
|
|
439
445
|
// previous_response_id was evicted from the server-side cache.
|
|
440
446
|
// Retry once on the same connection with the full context and no ID.
|
|
441
|
-
retryable = true;
|
|
447
|
+
this.#retryable = true;
|
|
442
448
|
await this.#runWithConn(conn, this.#fullChatCtx, undefined);
|
|
443
449
|
}
|
|
444
450
|
});
|
|
@@ -452,7 +458,7 @@ export class WSLLMStream extends llm.LLMStream {
|
|
|
452
458
|
}
|
|
453
459
|
throw new APIConnectionError({
|
|
454
460
|
message: toError(error).message,
|
|
455
|
-
options: { retryable },
|
|
461
|
+
options: { retryable: this.#retryable },
|
|
456
462
|
});
|
|
457
463
|
}
|
|
458
464
|
}
|
|
@@ -535,6 +541,9 @@ export class WSLLMStream extends llm.LLMStream {
|
|
|
535
541
|
|
|
536
542
|
if (chunk) {
|
|
537
543
|
this.queue.put(chunk);
|
|
544
|
+
if (llm.hasResponse(chunk)) {
|
|
545
|
+
this.#retryable = false;
|
|
546
|
+
}
|
|
538
547
|
}
|
|
539
548
|
}
|
|
540
549
|
} finally {
|
|
@@ -564,7 +573,7 @@ export class WSLLMStream extends llm.LLMStream {
|
|
|
564
573
|
this.#pool.invalidate();
|
|
565
574
|
throw new APIConnectionError({
|
|
566
575
|
message: event.error?.message ?? `WebSocket closed (${code})`,
|
|
567
|
-
options: { retryable:
|
|
576
|
+
options: { retryable: this.#retryable },
|
|
568
577
|
});
|
|
569
578
|
}
|
|
570
579
|
|