@livekit/agents-plugin-openai 1.7.0 → 1.8.0

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 (46) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/realtime/realtime_model.cjs +4 -1
  4. package/dist/realtime/realtime_model.cjs.map +1 -1
  5. package/dist/realtime/realtime_model.d.ts.map +1 -1
  6. package/dist/realtime/realtime_model.js +4 -1
  7. package/dist/realtime/realtime_model.js.map +1 -1
  8. package/dist/realtime/realtime_model.test.cjs +29 -0
  9. package/dist/realtime/realtime_model.test.cjs.map +1 -1
  10. package/dist/realtime/realtime_model.test.js +29 -0
  11. package/dist/realtime/realtime_model.test.js.map +1 -1
  12. package/dist/responses/llm.cjs +10 -0
  13. package/dist/responses/llm.cjs.map +1 -1
  14. package/dist/responses/llm.d.ts.map +1 -1
  15. package/dist/responses/llm.js +10 -0
  16. package/dist/responses/llm.js.map +1 -1
  17. package/dist/responses/llm.test.cjs +51 -0
  18. package/dist/responses/llm.test.cjs.map +1 -1
  19. package/dist/responses/llm.test.js +52 -1
  20. package/dist/responses/llm.test.js.map +1 -1
  21. package/dist/ws/llm.cjs +25 -7
  22. package/dist/ws/llm.cjs.map +1 -1
  23. package/dist/ws/llm.d.cts +2 -2
  24. package/dist/ws/llm.d.ts +2 -2
  25. package/dist/ws/llm.d.ts.map +1 -1
  26. package/dist/ws/llm.js +25 -7
  27. package/dist/ws/llm.js.map +1 -1
  28. package/dist/ws/llm.test.cjs +117 -0
  29. package/dist/ws/llm.test.cjs.map +1 -1
  30. package/dist/ws/llm.test.js +126 -3
  31. package/dist/ws/llm.test.js.map +1 -1
  32. package/dist/ws/types.cjs +12 -0
  33. package/dist/ws/types.cjs.map +1 -1
  34. package/dist/ws/types.d.cts +18 -0
  35. package/dist/ws/types.d.ts +18 -0
  36. package/dist/ws/types.d.ts.map +1 -1
  37. package/dist/ws/types.js +11 -0
  38. package/dist/ws/types.js.map +1 -1
  39. package/package.json +5 -5
  40. package/src/realtime/realtime_model.test.ts +32 -0
  41. package/src/realtime/realtime_model.ts +4 -1
  42. package/src/responses/llm.test.ts +55 -1
  43. package/src/responses/llm.ts +10 -0
  44. package/src/ws/llm.test.ts +141 -5
  45. package/src/ws/llm.ts +29 -8
  46. package/src/ws/types.ts +17 -0
package/src/ws/llm.ts CHANGED
@@ -24,6 +24,7 @@ import type {
24
24
  WsResponseCreateEvent,
25
25
  WsResponseCreatedEvent,
26
26
  WsResponseFailedEvent,
27
+ WsResponseIncompleteEvent,
27
28
  WsServerEvent,
28
29
  } from './types.js';
29
30
  import { wsServerEventSchema } from './types.js';
@@ -68,7 +69,7 @@ export function buildResponsesWsUrl(baseURL: string | undefined, model: string):
68
69
  // StreamChannels — one per outstanding response.create request — and
69
70
  // dispatches every incoming server-event to the front of the queue.
70
71
  // A response is terminated (and its channel closed) when the service sends
71
- // response.completed, response.failed, or error.
72
+ // response.completed, response.failed, response.incomplete, or error.
72
73
  //
73
74
  // ============================================================================
74
75
 
@@ -103,6 +104,7 @@ export class ResponsesWebSocket {
103
104
  if (
104
105
  event.type === 'response.completed' ||
105
106
  event.type === 'response.failed' ||
107
+ event.type === 'response.incomplete' ||
106
108
  event.type === 'error'
107
109
  ) {
108
110
  void current.close();
@@ -255,9 +257,9 @@ export class WSLLM extends llm.LLM {
255
257
  await this.close();
256
258
  }
257
259
 
258
- /** Called by LLMStream once response.created fires to atomically persist both the
260
+ /** Called by LLMStream once response.completed fires to atomically persist both the
259
261
  * response ID and its corresponding chat context for the next turn's diff. */
260
- _onResponseCreated(responseId: string, chatCtx: llm.ChatContext): void {
262
+ _onResponseCompleted(responseId: string, chatCtx: llm.ChatContext): void {
261
263
  this.#prevResponseId = responseId;
262
264
  this.#prevChatCtx = chatCtx;
263
265
  }
@@ -436,18 +438,28 @@ export class WSLLMStream extends llm.LLMStream {
436
438
 
437
439
  protected async run(): Promise<void> {
438
440
  this.#retryable = true;
441
+ let responseError: APIStatusError | undefined;
439
442
 
440
443
  try {
441
444
  await this.#pool.withConnection(async (conn: ResponsesWebSocket) => {
442
- const needsRetry = await this.#runWithConn(conn, this.chatCtx, this.#prevResponseId);
445
+ const result = await this.#runWithConn(conn, this.chatCtx, this.#prevResponseId);
443
446
 
444
- if (needsRetry) {
447
+ if (result instanceof APIStatusError) {
448
+ responseError = result;
449
+ return;
450
+ }
451
+
452
+ if (result) {
445
453
  // previous_response_id was evicted from the server-side cache.
446
454
  // Retry once on the same connection with the full context and no ID.
447
455
  this.#retryable = true;
448
- await this.#runWithConn(conn, this.#fullChatCtx, undefined);
456
+ const retryResult = await this.#runWithConn(conn, this.#fullChatCtx, undefined);
457
+ if (retryResult instanceof APIStatusError) {
458
+ responseError = retryResult;
459
+ }
449
460
  }
450
461
  });
462
+ if (responseError) throw responseError;
451
463
  } catch (error) {
452
464
  if (
453
465
  error instanceof APIStatusError ||
@@ -472,7 +484,7 @@ export class WSLLMStream extends llm.LLMStream {
472
484
  conn: ResponsesWebSocket,
473
485
  chatCtx: llm.ChatContext,
474
486
  prevResponseId: string | undefined,
475
- ): Promise<boolean> {
487
+ ): Promise<boolean | APIStatusError> {
476
488
  const messages = (await chatCtx.toProviderFormat(
477
489
  'openai.responses',
478
490
  )) as OpenAI.Responses.ResponseInputItem[];
@@ -535,6 +547,8 @@ export class WSLLMStream extends llm.LLMStream {
535
547
  case 'response.failed':
536
548
  this.#handleResponseFailed(event);
537
549
  break;
550
+ case 'response.incomplete':
551
+ return this.#handleResponseIncomplete(event);
538
552
  default:
539
553
  break;
540
554
  }
@@ -588,7 +602,6 @@ export class WSLLMStream extends llm.LLMStream {
588
602
 
589
603
  #handleResponseCreated(event: WsResponseCreatedEvent): void {
590
604
  this.#responseId = event.response.id;
591
- this.#llm._onResponseCreated(event.response.id, this.#fullChatCtx);
592
605
  }
593
606
 
594
607
  #handleOutputItemDone(event: WsOutputItemDoneEvent): llm.ChatChunk | undefined {
@@ -632,6 +645,7 @@ export class WSLLMStream extends llm.LLMStream {
632
645
  }
633
646
 
634
647
  #handleResponseCompleted(event: WsResponseCompletedEvent): llm.ChatChunk | undefined {
648
+ this.#llm._onResponseCompleted(event.response.id, this.#fullChatCtx);
635
649
  this.#llm._setPendingToolCalls(this.#pendingToolCalls);
636
650
 
637
651
  if (event.response.usage) {
@@ -655,6 +669,13 @@ export class WSLLMStream extends llm.LLMStream {
655
669
  options: { statusCode: -1, retryable: false },
656
670
  });
657
671
  }
672
+
673
+ #handleResponseIncomplete(event: WsResponseIncompleteEvent): APIStatusError {
674
+ return new APIStatusError({
675
+ message: `response incomplete: ${event.response.incomplete_details?.reason ?? 'reason unavailable'}`,
676
+ options: { statusCode: -1, retryable: false },
677
+ });
678
+ }
658
679
  }
659
680
 
660
681
  // ============================================================================
package/src/ws/types.ts CHANGED
@@ -98,6 +98,21 @@ export const wsResponseFailedEventSchema = z.object({
98
98
  .passthrough(),
99
99
  });
100
100
 
101
+ export const wsResponseIncompleteEventSchema = z.object({
102
+ type: z.literal('response.incomplete'),
103
+ response: z
104
+ .object({
105
+ id: z.string(),
106
+ incomplete_details: z
107
+ .object({
108
+ reason: z.string().optional(),
109
+ })
110
+ .nullable()
111
+ .optional(),
112
+ })
113
+ .passthrough(),
114
+ });
115
+
101
116
  export const wsErrorEventSchema = z.object({
102
117
  type: z.literal('error'),
103
118
  status: z.number().optional(),
@@ -120,6 +135,7 @@ export const wsServerEventSchema = z.discriminatedUnion('type', [
120
135
  wsOutputTextDeltaEventSchema,
121
136
  wsResponseCompletedEventSchema,
122
137
  wsResponseFailedEventSchema,
138
+ wsResponseIncompleteEventSchema,
123
139
  wsErrorEventSchema,
124
140
  ]);
125
141
 
@@ -130,5 +146,6 @@ export type WsOutputItemDoneEvent = z.infer<typeof wsOutputItemDoneEventSchema>;
130
146
  export type WsOutputTextDeltaEvent = z.infer<typeof wsOutputTextDeltaEventSchema>;
131
147
  export type WsResponseCompletedEvent = z.infer<typeof wsResponseCompletedEventSchema>;
132
148
  export type WsResponseFailedEvent = z.infer<typeof wsResponseFailedEventSchema>;
149
+ export type WsResponseIncompleteEvent = z.infer<typeof wsResponseIncompleteEventSchema>;
133
150
  export type WsErrorEvent = z.infer<typeof wsErrorEventSchema>;
134
151
  export type WsServerEvent = z.infer<typeof wsServerEventSchema>;