openai 4.20.1 → 4.22.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 (95) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +3 -3
  3. package/error.d.ts.map +1 -1
  4. package/error.js +2 -1
  5. package/error.js.map +1 -1
  6. package/error.mjs +2 -1
  7. package/error.mjs.map +1 -1
  8. package/index.d.mts +7 -5
  9. package/index.d.ts +7 -5
  10. package/index.d.ts.map +1 -1
  11. package/index.js +5 -5
  12. package/index.js.map +1 -1
  13. package/index.mjs +5 -5
  14. package/index.mjs.map +1 -1
  15. package/lib/AbstractChatCompletionRunner.d.ts +1 -1
  16. package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
  17. package/lib/AbstractChatCompletionRunner.js +21 -6
  18. package/lib/AbstractChatCompletionRunner.js.map +1 -1
  19. package/lib/AbstractChatCompletionRunner.mjs +21 -6
  20. package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
  21. package/lib/ChatCompletionRunFunctions.test.js +823 -549
  22. package/lib/ChatCompletionRunFunctions.test.js.map +1 -1
  23. package/lib/ChatCompletionRunFunctions.test.mjs +824 -550
  24. package/lib/ChatCompletionRunFunctions.test.mjs.map +1 -1
  25. package/lib/ChatCompletionRunner.d.ts +1 -0
  26. package/lib/ChatCompletionRunner.d.ts.map +1 -1
  27. package/lib/ChatCompletionRunner.js +11 -2
  28. package/lib/ChatCompletionRunner.js.map +1 -1
  29. package/lib/ChatCompletionRunner.mjs +11 -2
  30. package/lib/ChatCompletionRunner.mjs.map +1 -1
  31. package/lib/ChatCompletionStreamingRunner.d.ts +1 -0
  32. package/lib/ChatCompletionStreamingRunner.d.ts.map +1 -1
  33. package/lib/ChatCompletionStreamingRunner.js +7 -4
  34. package/lib/ChatCompletionStreamingRunner.js.map +1 -1
  35. package/lib/ChatCompletionStreamingRunner.mjs +7 -4
  36. package/lib/ChatCompletionStreamingRunner.mjs.map +1 -1
  37. package/lib/RunnableFunction.d.ts +19 -3
  38. package/lib/RunnableFunction.d.ts.map +1 -1
  39. package/lib/RunnableFunction.js +14 -1
  40. package/lib/RunnableFunction.js.map +1 -1
  41. package/lib/RunnableFunction.mjs +12 -0
  42. package/lib/RunnableFunction.mjs.map +1 -1
  43. package/package.json +3 -2
  44. package/resources/audio/speech.d.ts +3 -1
  45. package/resources/audio/speech.d.ts.map +1 -1
  46. package/resources/audio/speech.js.map +1 -1
  47. package/resources/audio/speech.mjs.map +1 -1
  48. package/resources/beta/chat/completions.d.ts +2 -8
  49. package/resources/beta/chat/completions.d.ts.map +1 -1
  50. package/resources/beta/chat/completions.js +2 -1
  51. package/resources/beta/chat/completions.js.map +1 -1
  52. package/resources/beta/chat/completions.mjs +1 -1
  53. package/resources/beta/chat/completions.mjs.map +1 -1
  54. package/resources/chat/completions.d.ts +52 -34
  55. package/resources/chat/completions.d.ts.map +1 -1
  56. package/resources/chat/completions.js.map +1 -1
  57. package/resources/chat/completions.mjs.map +1 -1
  58. package/resources/completions.d.ts +2 -2
  59. package/resources/embeddings.d.ts +2 -1
  60. package/resources/embeddings.d.ts.map +1 -1
  61. package/resources/embeddings.js.map +1 -1
  62. package/resources/embeddings.mjs.map +1 -1
  63. package/resources/files.d.ts +3 -3
  64. package/resources/files.js +3 -3
  65. package/resources/files.mjs +3 -3
  66. package/resources/shared.d.ts +12 -14
  67. package/resources/shared.d.ts.map +1 -1
  68. package/src/error.ts +2 -1
  69. package/src/index.ts +8 -5
  70. package/src/lib/AbstractChatCompletionRunner.ts +37 -13
  71. package/src/lib/ChatCompletionRunFunctions.test.ts +828 -558
  72. package/src/lib/ChatCompletionRunner.ts +11 -2
  73. package/src/lib/ChatCompletionStreamingRunner.ts +11 -12
  74. package/src/lib/RunnableFunction.ts +33 -7
  75. package/src/resources/audio/speech.ts +3 -1
  76. package/src/resources/beta/chat/completions.ts +2 -7
  77. package/src/resources/chat/completions.ts +56 -35
  78. package/src/resources/completions.ts +2 -2
  79. package/src/resources/embeddings.ts +2 -1
  80. package/src/resources/files.ts +3 -3
  81. package/src/resources/shared.ts +13 -15
  82. package/src/streaming.ts +4 -1
  83. package/src/uploads.ts +2 -3
  84. package/src/version.ts +1 -1
  85. package/streaming.d.ts.map +1 -1
  86. package/streaming.js.map +1 -1
  87. package/streaming.mjs.map +1 -1
  88. package/uploads.d.ts.map +1 -1
  89. package/uploads.js +2 -1
  90. package/uploads.js.map +1 -1
  91. package/uploads.mjs +2 -1
  92. package/uploads.mjs.map +1 -1
  93. package/version.d.ts +1 -1
  94. package/version.js +1 -1
  95. package/version.mjs +1 -1
@@ -6,7 +6,6 @@ import {
6
6
  type ChatCompletionMessage,
7
7
  type ChatCompletionMessageParam,
8
8
  type ChatCompletionCreateParams,
9
- type ChatCompletionAssistantMessageParam,
10
9
  type ChatCompletionTool,
11
10
  } from "../resources/chat/completions";
12
11
  import { APIUserAbortError, OpenAIError } from "../error";
@@ -90,7 +89,10 @@ export abstract class AbstractChatCompletionRunner<
90
89
  }
91
90
 
92
91
  protected _addMessage(message: ChatCompletionMessageParam, emit = true) {
92
+ if (!('content' in message)) message.content = null;
93
+
93
94
  this.messages.push(message);
95
+
94
96
  if (emit) {
95
97
  this._emit('message', message);
96
98
  if ((isFunctionMessage(message) || isToolMessage(message)) && message.content) {
@@ -213,7 +215,7 @@ export abstract class AbstractChatCompletionRunner<
213
215
  }
214
216
 
215
217
  #getFinalContent(): string | null {
216
- return this.#getFinalMessage().content;
218
+ return this.#getFinalMessage().content ?? null;
217
219
  }
218
220
 
219
221
  /**
@@ -225,12 +227,12 @@ export abstract class AbstractChatCompletionRunner<
225
227
  return this.#getFinalContent();
226
228
  }
227
229
 
228
- #getFinalMessage(): ChatCompletionAssistantMessageParam {
230
+ #getFinalMessage(): ChatCompletionMessage {
229
231
  let i = this.messages.length;
230
232
  while (i-- > 0) {
231
233
  const message = this.messages[i];
232
234
  if (isAssistantMessage(message)) {
233
- return message;
235
+ return { ...message, content: message.content ?? null };
234
236
  }
235
237
  }
236
238
  throw new OpenAIError('stream ended without producing a ChatCompletionMessage with role=assistant');
@@ -251,6 +253,9 @@ export abstract class AbstractChatCompletionRunner<
251
253
  if (isAssistantMessage(message) && message?.function_call) {
252
254
  return message.function_call;
253
255
  }
256
+ if (isAssistantMessage(message) && message?.tool_calls?.length) {
257
+ return message.tool_calls.at(-1)?.function;
258
+ }
254
259
  }
255
260
 
256
261
  return;
@@ -269,7 +274,18 @@ export abstract class AbstractChatCompletionRunner<
269
274
  for (let i = this.messages.length - 1; i >= 0; i--) {
270
275
  const message = this.messages[i];
271
276
  if (isFunctionMessage(message) && message.content != null) {
272
- return message.content as string;
277
+ return message.content;
278
+ }
279
+ if (
280
+ isToolMessage(message) &&
281
+ message.content != null &&
282
+ this.messages.some(
283
+ (x) =>
284
+ x.role === 'assistant' &&
285
+ x.tool_calls?.some((y) => y.type === 'function' && y.id === message.tool_call_id),
286
+ )
287
+ ) {
288
+ return message.content;
273
289
  }
274
290
  }
275
291
 
@@ -329,7 +345,9 @@ export abstract class AbstractChatCompletionRunner<
329
345
 
330
346
  protected _emit<Event extends keyof Events>(event: Event, ...args: EventParameters<Events, Event>) {
331
347
  // make sure we don't emit any events after end
332
- if (this.#ended) return;
348
+ if (this.#ended) {
349
+ return;
350
+ }
333
351
 
334
352
  if (event === 'end') {
335
353
  this.#ended = true;
@@ -375,7 +393,7 @@ export abstract class AbstractChatCompletionRunner<
375
393
  protected _emitFinal() {
376
394
  const completion = this._chatCompletions[this._chatCompletions.length - 1];
377
395
  if (completion) this._emit('finalChatCompletion', completion);
378
- const finalMessage = this.messages[this.messages.length - 1];
396
+ const finalMessage = this.#getFinalMessage();
379
397
  if (finalMessage) this._emit('finalMessage', finalMessage);
380
398
  const finalContent = this.#getFinalContent();
381
399
  if (finalContent) this._emit('finalContent', finalContent);
@@ -569,7 +587,9 @@ export abstract class AbstractChatCompletionRunner<
569
587
  if (!message) {
570
588
  throw new OpenAIError(`missing message in ChatCompletion response`);
571
589
  }
572
- if (!message.tool_calls) return;
590
+ if (!message.tool_calls) {
591
+ return;
592
+ }
573
593
 
574
594
  for (const tool_call of message.tool_calls) {
575
595
  if (tool_call.type !== 'function') continue;
@@ -607,9 +627,13 @@ export abstract class AbstractChatCompletionRunner<
607
627
  const content = this.#stringifyFunctionCallResult(rawContent);
608
628
  this._addMessage({ role, tool_call_id, content });
609
629
 
610
- if (singleFunctionToCall) return;
630
+ if (singleFunctionToCall) {
631
+ return;
632
+ }
611
633
  }
612
634
  }
635
+
636
+ return;
613
637
  }
614
638
 
615
639
  #stringifyFunctionCallResult(rawContent: unknown): string {
@@ -626,10 +650,10 @@ type CustomEvents<Event extends string> = {
626
650
  : (...args: any[]) => void;
627
651
  };
628
652
 
629
- type ListenerForEvent<
630
- Events extends CustomEvents<any>,
631
- Event extends keyof Events,
632
- > = Event extends keyof AbstractChatCompletionRunnerEvents ? AbstractChatCompletionRunnerEvents[Event]
653
+ type ListenerForEvent<Events extends CustomEvents<any>, Event extends keyof Events> = Event extends (
654
+ keyof AbstractChatCompletionRunnerEvents
655
+ ) ?
656
+ AbstractChatCompletionRunnerEvents[Event]
633
657
  : Events[Event];
634
658
 
635
659
  type ListenersForEvent<Events extends CustomEvents<any>, Event extends keyof Events> = Array<{