@livekit/agents-plugin-openai 1.5.1 → 1.5.3

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.
Files changed (42) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/realtime/realtime_model.cjs +197 -159
  4. package/dist/realtime/realtime_model.cjs.map +1 -1
  5. package/dist/realtime/realtime_model.d.cts +4 -0
  6. package/dist/realtime/realtime_model.d.ts +4 -0
  7. package/dist/realtime/realtime_model.d.ts.map +1 -1
  8. package/dist/realtime/realtime_model.js +196 -159
  9. package/dist/realtime/realtime_model.js.map +1 -1
  10. package/dist/realtime/realtime_model.test.cjs +176 -0
  11. package/dist/realtime/realtime_model.test.cjs.map +1 -1
  12. package/dist/realtime/realtime_model.test.js +177 -0
  13. package/dist/realtime/realtime_model.test.js.map +1 -1
  14. package/dist/responses/llm.test.cjs +17 -0
  15. package/dist/responses/llm.test.cjs.map +1 -1
  16. package/dist/responses/llm.test.js +18 -1
  17. package/dist/responses/llm.test.js.map +1 -1
  18. package/dist/ws/llm.cjs +6 -3
  19. package/dist/ws/llm.cjs.map +1 -1
  20. package/dist/ws/llm.d.cts +4 -5
  21. package/dist/ws/llm.d.ts +4 -5
  22. package/dist/ws/llm.d.ts.map +1 -1
  23. package/dist/ws/llm.js +6 -3
  24. package/dist/ws/llm.js.map +1 -1
  25. package/dist/ws/llm.test.cjs +22 -2
  26. package/dist/ws/llm.test.cjs.map +1 -1
  27. package/dist/ws/llm.test.js +22 -2
  28. package/dist/ws/llm.test.js.map +1 -1
  29. package/dist/ws/types.cjs +2 -0
  30. package/dist/ws/types.cjs.map +1 -1
  31. package/dist/ws/types.d.cts +4 -0
  32. package/dist/ws/types.d.ts +4 -0
  33. package/dist/ws/types.d.ts.map +1 -1
  34. package/dist/ws/types.js +2 -0
  35. package/dist/ws/types.js.map +1 -1
  36. package/package.json +5 -5
  37. package/src/realtime/realtime_model.test.ts +210 -0
  38. package/src/realtime/realtime_model.ts +207 -163
  39. package/src/responses/llm.test.ts +22 -1
  40. package/src/ws/llm.test.ts +28 -2
  41. package/src/ws/llm.ts +13 -9
  42. package/src/ws/types.ts +2 -0
@@ -2,11 +2,32 @@
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  import { llm, llmStrict } from '@livekit/agents-plugins-test';
5
- import { describe, it } from 'vitest';
5
+ import { describe, expect, it } from 'vitest';
6
+ import { wsServerEventSchema } from '../ws/types.js';
6
7
  import { LLM } from './llm.js';
7
8
 
8
9
  const hasOpenAIApiKey = Boolean(process.env.OPENAI_API_KEY);
9
10
 
11
+ describe('OpenAI Responses WebSocket', () => {
12
+ it('preserves top-level code and param on error frames', () => {
13
+ const frame = {
14
+ type: 'error',
15
+ message:
16
+ "Invalid type for 'reasoning.mode': expected one of 'standard' or 'pro', but got null instead.",
17
+ code: 'invalid_type',
18
+ param: 'reasoning.mode',
19
+ status: 400,
20
+ };
21
+
22
+ const parsed = wsServerEventSchema.parse(frame);
23
+
24
+ expect(parsed.type).toBe('error');
25
+ if (parsed.type !== 'error') throw new Error('expected error event');
26
+ expect(parsed.message).toBe(frame.message);
27
+ expect(parsed.param).toBe('reasoning.mode');
28
+ });
29
+ });
30
+
10
31
  if (hasOpenAIApiKey) {
11
32
  describe('OpenAI Responses', async () => {
12
33
  await llm(
@@ -12,13 +12,13 @@ initializeLogger({ level: 'silent', pretty: false });
12
12
  const hasOpenAIApiKey = Boolean(process.env.OPENAI_API_KEY);
13
13
 
14
14
  describe('buildResponsesWsUrl', () => {
15
- it('points at the OpenAI Responses WS endpoint with model when no baseURL is set', () => {
15
+ it('points at the OpenAI Responses WS endpoint without model when no baseURL is set', () => {
16
16
  const url = new URL(buildResponsesWsUrl(undefined, 'gpt-4.1'));
17
17
 
18
18
  expect(url.protocol).toBe('wss:');
19
19
  expect(url.host).toBe('api.openai.com');
20
20
  expect(url.pathname).toBe('/v1/responses');
21
- expect(url.searchParams.get('model')).toBe('gpt-4.1');
21
+ expect(url.searchParams.get('model')).toBe(null);
22
22
  });
23
23
 
24
24
  it('rewrites https baseURL to wss and appends /responses with the model', () => {
@@ -30,6 +30,32 @@ describe('buildResponsesWsUrl', () => {
30
30
  expect(url.searchParams.get('model')).toBe('gpt-4o');
31
31
  });
32
32
 
33
+ it('rewrites full https Responses endpoint to wss without duplicating /responses', () => {
34
+ const url = new URL(buildResponsesWsUrl('https://gateway.example.com/v1/responses', 'gpt-4o'));
35
+
36
+ expect(url.protocol).toBe('wss:');
37
+ expect(url.host).toBe('gateway.example.com');
38
+ expect(url.pathname).toBe('/v1/responses');
39
+ expect(url.searchParams.get('model')).toBe('gpt-4o');
40
+ });
41
+
42
+ it('uses full wss Responses endpoint with the model', () => {
43
+ const url = new URL(buildResponsesWsUrl('wss://gateway.example.com/v1/responses', 'gpt-4o'));
44
+
45
+ expect(url.protocol).toBe('wss:');
46
+ expect(url.host).toBe('gateway.example.com');
47
+ expect(url.pathname).toBe('/v1/responses');
48
+ expect(url.searchParams.get('model')).toBe('gpt-4o');
49
+ });
50
+
51
+ it('omits the model when an explicit baseURL still points at api.openai.com', () => {
52
+ const url = new URL(buildResponsesWsUrl('https://api.openai.com/v1', 'gpt-4.1'));
53
+
54
+ expect(url.host).toBe('api.openai.com');
55
+ expect(url.pathname).toBe('/v1/responses');
56
+ expect(url.searchParams.get('model')).toBe(null);
57
+ });
58
+
33
59
  it('strips a trailing slash on baseURL before appending /responses', () => {
34
60
  const url = new URL(buildResponsesWsUrl('https://gateway.example.com/v1/', 'gpt-4o-mini'));
35
61
 
package/src/ws/llm.ts CHANGED
@@ -36,12 +36,11 @@ const WS_MAX_SESSION_DURATION = 3_600_000;
36
36
  /**
37
37
  * Build the Responses-API WebSocket URL.
38
38
  *
39
- * Includes the model on the upgrade URL so OpenAI-compatible gateways
39
+ * Includes the model on the upgrade URL for OpenAI-compatible gateways
40
40
  * (which can only see the URL at the WebSocket upgrade, not the subsequent
41
- * `response.create` frame) can route by model. Mirrors the existing
42
- * convention in `realtime/realtime_model.ts` for the conversational
43
- * Realtime API. OpenAI's native endpoint accepts and ignores the
44
- * parameter, so this is a no-op for direct connections.
41
+ * `response.create` frame) so they can route by model. OpenAI's native
42
+ * endpoint gets the model in the `response.create` frame, so the query
43
+ * parameter is intentionally omitted for direct connections.
45
44
  *
46
45
  * The scheme of `baseURL` is respected: `http://` maps to `ws://`
47
46
  * and `https://` maps to `wss://`.
@@ -49,11 +48,16 @@ const WS_MAX_SESSION_DURATION = 3_600_000;
49
48
  * @internal
50
49
  */
51
50
  export function buildResponsesWsUrl(baseURL: string | undefined, model: string): string {
52
- const base = baseURL
53
- ? `${baseURL.replace(/^http(s?):/, 'ws$1:').replace(/\/+$/, '')}/responses`
51
+ const normalizedBaseURL = baseURL?.replace(/^http(s?):/, 'ws$1:').replace(/\/+$/, '');
52
+ const base = normalizedBaseURL
53
+ ? normalizedBaseURL.endsWith('/responses')
54
+ ? normalizedBaseURL
55
+ : `${normalizedBaseURL}/responses`
54
56
  : OPENAI_RESPONSES_WS_URL;
55
57
  const url = new URL(base);
56
- url.searchParams.set('model', model);
58
+ if (url.hostname !== 'api.openai.com') {
59
+ url.searchParams.set('model', model);
60
+ }
57
61
  return url.toString();
58
62
  }
59
63
 
@@ -544,7 +548,7 @@ export class WSLLMStream extends llm.LLMStream {
544
548
  * (`previous_response_not_found`), throws for all other errors.
545
549
  */
546
550
  #handleError(event: WsServerEvent & { type: 'error' }, conn: ResponsesWebSocket): boolean {
547
- const code = event.error?.code;
551
+ const code = event.error?.code ?? event.code;
548
552
 
549
553
  if (code === 'previous_response_not_found') {
550
554
  // The server-side in-memory cache was evicted (e.g. after a failed turn
package/src/ws/types.ts CHANGED
@@ -101,6 +101,8 @@ export const wsResponseFailedEventSchema = z.object({
101
101
  export const wsErrorEventSchema = z.object({
102
102
  type: z.literal('error'),
103
103
  status: z.number().optional(),
104
+ code: z.string().optional(),
105
+ param: z.string().optional(),
104
106
  error: z
105
107
  .object({
106
108
  type: z.string().optional(),