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.
- package/CHANGELOG.md +37 -0
- package/README.md +3 -3
- package/error.d.ts.map +1 -1
- package/error.js +2 -1
- package/error.js.map +1 -1
- package/error.mjs +2 -1
- package/error.mjs.map +1 -1
- package/index.d.mts +7 -5
- package/index.d.ts +7 -5
- package/index.d.ts.map +1 -1
- package/index.js +5 -5
- package/index.js.map +1 -1
- package/index.mjs +5 -5
- package/index.mjs.map +1 -1
- package/lib/AbstractChatCompletionRunner.d.ts +1 -1
- package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
- package/lib/AbstractChatCompletionRunner.js +21 -6
- package/lib/AbstractChatCompletionRunner.js.map +1 -1
- package/lib/AbstractChatCompletionRunner.mjs +21 -6
- package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
- package/lib/ChatCompletionRunFunctions.test.js +823 -549
- package/lib/ChatCompletionRunFunctions.test.js.map +1 -1
- package/lib/ChatCompletionRunFunctions.test.mjs +824 -550
- package/lib/ChatCompletionRunFunctions.test.mjs.map +1 -1
- package/lib/ChatCompletionRunner.d.ts +1 -0
- package/lib/ChatCompletionRunner.d.ts.map +1 -1
- package/lib/ChatCompletionRunner.js +11 -2
- package/lib/ChatCompletionRunner.js.map +1 -1
- package/lib/ChatCompletionRunner.mjs +11 -2
- package/lib/ChatCompletionRunner.mjs.map +1 -1
- package/lib/ChatCompletionStreamingRunner.d.ts +1 -0
- package/lib/ChatCompletionStreamingRunner.d.ts.map +1 -1
- package/lib/ChatCompletionStreamingRunner.js +7 -4
- package/lib/ChatCompletionStreamingRunner.js.map +1 -1
- package/lib/ChatCompletionStreamingRunner.mjs +7 -4
- package/lib/ChatCompletionStreamingRunner.mjs.map +1 -1
- package/lib/RunnableFunction.d.ts +19 -3
- package/lib/RunnableFunction.d.ts.map +1 -1
- package/lib/RunnableFunction.js +14 -1
- package/lib/RunnableFunction.js.map +1 -1
- package/lib/RunnableFunction.mjs +12 -0
- package/lib/RunnableFunction.mjs.map +1 -1
- package/package.json +3 -2
- package/resources/audio/speech.d.ts +3 -1
- package/resources/audio/speech.d.ts.map +1 -1
- package/resources/audio/speech.js.map +1 -1
- package/resources/audio/speech.mjs.map +1 -1
- package/resources/beta/chat/completions.d.ts +2 -8
- package/resources/beta/chat/completions.d.ts.map +1 -1
- package/resources/beta/chat/completions.js +2 -1
- package/resources/beta/chat/completions.js.map +1 -1
- package/resources/beta/chat/completions.mjs +1 -1
- package/resources/beta/chat/completions.mjs.map +1 -1
- package/resources/chat/completions.d.ts +52 -34
- package/resources/chat/completions.d.ts.map +1 -1
- package/resources/chat/completions.js.map +1 -1
- package/resources/chat/completions.mjs.map +1 -1
- package/resources/completions.d.ts +2 -2
- package/resources/embeddings.d.ts +2 -1
- package/resources/embeddings.d.ts.map +1 -1
- package/resources/embeddings.js.map +1 -1
- package/resources/embeddings.mjs.map +1 -1
- package/resources/files.d.ts +3 -3
- package/resources/files.js +3 -3
- package/resources/files.mjs +3 -3
- package/resources/shared.d.ts +12 -14
- package/resources/shared.d.ts.map +1 -1
- package/src/error.ts +2 -1
- package/src/index.ts +8 -5
- package/src/lib/AbstractChatCompletionRunner.ts +37 -13
- package/src/lib/ChatCompletionRunFunctions.test.ts +828 -558
- package/src/lib/ChatCompletionRunner.ts +11 -2
- package/src/lib/ChatCompletionStreamingRunner.ts +11 -12
- package/src/lib/RunnableFunction.ts +33 -7
- package/src/resources/audio/speech.ts +3 -1
- package/src/resources/beta/chat/completions.ts +2 -7
- package/src/resources/chat/completions.ts +56 -35
- package/src/resources/completions.ts +2 -2
- package/src/resources/embeddings.ts +2 -1
- package/src/resources/files.ts +3 -3
- package/src/resources/shared.ts +13 -15
- package/src/streaming.ts +4 -1
- package/src/uploads.ts +2 -3
- package/src/version.ts +1 -1
- package/streaming.d.ts.map +1 -1
- package/streaming.js.map +1 -1
- package/streaming.mjs.map +1 -1
- package/uploads.d.ts.map +1 -1
- package/uploads.js +2 -1
- package/uploads.js.map +1 -1
- package/uploads.mjs +2 -1
- package/uploads.mjs.map +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- 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():
|
|
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
|
|
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)
|
|
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
|
|
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)
|
|
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)
|
|
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
|
-
|
|
631
|
-
|
|
632
|
-
|
|
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<{
|