@ai-sdk/openai 2.0.0-canary.4 → 2.0.0-canary.6
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 +26 -0
- package/dist/index.d.mts +6 -67
- package/dist/index.d.ts +6 -67
- package/dist/index.js +421 -525
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +388 -492
- package/dist/index.mjs.map +1 -1
- package/{internal/dist → dist/internal}/index.d.mts +88 -80
- package/{internal/dist → dist/internal}/index.d.ts +88 -80
- package/{internal/dist → dist/internal}/index.js +420 -522
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +388 -491
- package/dist/internal/index.mjs.map +1 -0
- package/package.json +11 -12
- package/internal/dist/index.js.map +0 -1
- package/internal/dist/index.mjs.map +0 -1
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// src/openai-chat-language-model.ts
|
|
2
2
|
import {
|
|
3
|
-
InvalidResponseDataError
|
|
4
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
3
|
+
InvalidResponseDataError
|
|
5
4
|
} from "@ai-sdk/provider";
|
|
6
5
|
import {
|
|
7
6
|
combineHeaders,
|
|
@@ -9,17 +8,18 @@ import {
|
|
|
9
8
|
createJsonResponseHandler,
|
|
10
9
|
generateId,
|
|
11
10
|
isParsableJson,
|
|
12
|
-
postJsonToApi
|
|
11
|
+
postJsonToApi,
|
|
12
|
+
parseProviderOptions
|
|
13
13
|
} from "@ai-sdk/provider-utils";
|
|
14
|
-
import { z as
|
|
14
|
+
import { z as z3 } from "zod";
|
|
15
15
|
|
|
16
16
|
// src/convert-to-openai-chat-messages.ts
|
|
17
17
|
import {
|
|
18
18
|
UnsupportedFunctionalityError
|
|
19
19
|
} from "@ai-sdk/provider";
|
|
20
|
+
import { convertToBase64 } from "@ai-sdk/provider-utils";
|
|
20
21
|
function convertToOpenAIChatMessages({
|
|
21
22
|
prompt,
|
|
22
|
-
useLegacyFunctionCalling = false,
|
|
23
23
|
systemMessageMode = "system"
|
|
24
24
|
}) {
|
|
25
25
|
const messages = [];
|
|
@@ -71,7 +71,7 @@ function convertToOpenAIChatMessages({
|
|
|
71
71
|
return {
|
|
72
72
|
type: "image_url",
|
|
73
73
|
image_url: {
|
|
74
|
-
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`,
|
|
74
|
+
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`,
|
|
75
75
|
// OpenAI specific extension: image detail
|
|
76
76
|
detail: (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.imageDetail
|
|
77
77
|
}
|
|
@@ -86,14 +86,20 @@ function convertToOpenAIChatMessages({
|
|
|
86
86
|
case "audio/wav": {
|
|
87
87
|
return {
|
|
88
88
|
type: "input_audio",
|
|
89
|
-
input_audio: {
|
|
89
|
+
input_audio: {
|
|
90
|
+
data: convertToBase64(part.data),
|
|
91
|
+
format: "wav"
|
|
92
|
+
}
|
|
90
93
|
};
|
|
91
94
|
}
|
|
92
95
|
case "audio/mp3":
|
|
93
96
|
case "audio/mpeg": {
|
|
94
97
|
return {
|
|
95
98
|
type: "input_audio",
|
|
96
|
-
input_audio: {
|
|
99
|
+
input_audio: {
|
|
100
|
+
data: convertToBase64(part.data),
|
|
101
|
+
format: "mp3"
|
|
102
|
+
}
|
|
97
103
|
};
|
|
98
104
|
}
|
|
99
105
|
default: {
|
|
@@ -148,41 +154,20 @@ function convertToOpenAIChatMessages({
|
|
|
148
154
|
}
|
|
149
155
|
}
|
|
150
156
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
messages.push({
|
|
158
|
-
role: "assistant",
|
|
159
|
-
content: text,
|
|
160
|
-
function_call: toolCalls.length > 0 ? toolCalls[0].function : void 0
|
|
161
|
-
});
|
|
162
|
-
} else {
|
|
163
|
-
messages.push({
|
|
164
|
-
role: "assistant",
|
|
165
|
-
content: text,
|
|
166
|
-
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
167
|
-
});
|
|
168
|
-
}
|
|
157
|
+
messages.push({
|
|
158
|
+
role: "assistant",
|
|
159
|
+
content: text,
|
|
160
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
161
|
+
});
|
|
169
162
|
break;
|
|
170
163
|
}
|
|
171
164
|
case "tool": {
|
|
172
165
|
for (const toolResponse of content) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
});
|
|
179
|
-
} else {
|
|
180
|
-
messages.push({
|
|
181
|
-
role: "tool",
|
|
182
|
-
tool_call_id: toolResponse.toolCallId,
|
|
183
|
-
content: JSON.stringify(toolResponse.result)
|
|
184
|
-
});
|
|
185
|
-
}
|
|
166
|
+
messages.push({
|
|
167
|
+
role: "tool",
|
|
168
|
+
tool_call_id: toolResponse.toolCallId,
|
|
169
|
+
content: JSON.stringify(toolResponse.result)
|
|
170
|
+
});
|
|
186
171
|
}
|
|
187
172
|
break;
|
|
188
173
|
}
|
|
@@ -225,18 +210,69 @@ function mapOpenAIFinishReason(finishReason) {
|
|
|
225
210
|
}
|
|
226
211
|
}
|
|
227
212
|
|
|
228
|
-
// src/openai-
|
|
213
|
+
// src/openai-chat-options.ts
|
|
229
214
|
import { z } from "zod";
|
|
215
|
+
var openaiProviderOptions = z.object({
|
|
216
|
+
/**
|
|
217
|
+
* Modify the likelihood of specified tokens appearing in the completion.
|
|
218
|
+
*
|
|
219
|
+
* Accepts a JSON object that maps tokens (specified by their token ID in
|
|
220
|
+
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
|
221
|
+
*/
|
|
222
|
+
logitBias: z.record(z.coerce.number(), z.number()).optional(),
|
|
223
|
+
/**
|
|
224
|
+
* Return the log probabilities of the tokens.
|
|
225
|
+
*
|
|
226
|
+
* Setting to true will return the log probabilities of the tokens that
|
|
227
|
+
* were generated.
|
|
228
|
+
*
|
|
229
|
+
* Setting to a number will return the log probabilities of the top n
|
|
230
|
+
* tokens that were generated.
|
|
231
|
+
*/
|
|
232
|
+
logprobs: z.union([z.boolean(), z.number()]).optional(),
|
|
233
|
+
/**
|
|
234
|
+
* Whether to enable parallel function calling during tool use. Default to true.
|
|
235
|
+
*/
|
|
236
|
+
parallelToolCalls: z.boolean().optional(),
|
|
237
|
+
/**
|
|
238
|
+
* A unique identifier representing your end-user, which can help OpenAI to
|
|
239
|
+
* monitor and detect abuse.
|
|
240
|
+
*/
|
|
241
|
+
user: z.string().optional(),
|
|
242
|
+
/**
|
|
243
|
+
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
244
|
+
*/
|
|
245
|
+
reasoningEffort: z.enum(["low", "medium", "high"]).optional(),
|
|
246
|
+
/**
|
|
247
|
+
* Maximum number of completion tokens to generate. Useful for reasoning models.
|
|
248
|
+
*/
|
|
249
|
+
maxCompletionTokens: z.number().optional(),
|
|
250
|
+
/**
|
|
251
|
+
* Whether to enable persistence in responses API.
|
|
252
|
+
*/
|
|
253
|
+
store: z.boolean().optional(),
|
|
254
|
+
/**
|
|
255
|
+
* Metadata to associate with the request.
|
|
256
|
+
*/
|
|
257
|
+
metadata: z.record(z.string()).optional(),
|
|
258
|
+
/**
|
|
259
|
+
* Parameters for prediction mode.
|
|
260
|
+
*/
|
|
261
|
+
prediction: z.record(z.any()).optional()
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// src/openai-error.ts
|
|
265
|
+
import { z as z2 } from "zod";
|
|
230
266
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
231
|
-
var openaiErrorDataSchema =
|
|
232
|
-
error:
|
|
233
|
-
message:
|
|
267
|
+
var openaiErrorDataSchema = z2.object({
|
|
268
|
+
error: z2.object({
|
|
269
|
+
message: z2.string(),
|
|
234
270
|
// The additional information below is handled loosely to support
|
|
235
271
|
// OpenAI-compatible providers that have slightly different error
|
|
236
272
|
// responses:
|
|
237
|
-
type:
|
|
238
|
-
param:
|
|
239
|
-
code:
|
|
273
|
+
type: z2.string().nullish(),
|
|
274
|
+
param: z2.any().nullish(),
|
|
275
|
+
code: z2.union([z2.string(), z2.number()]).nullish()
|
|
240
276
|
})
|
|
241
277
|
});
|
|
242
278
|
var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
@@ -264,7 +300,6 @@ import {
|
|
|
264
300
|
function prepareTools({
|
|
265
301
|
tools,
|
|
266
302
|
toolChoice,
|
|
267
|
-
useLegacyFunctionCalling = false,
|
|
268
303
|
structuredOutputs
|
|
269
304
|
}) {
|
|
270
305
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
@@ -272,48 +307,6 @@ function prepareTools({
|
|
|
272
307
|
if (tools == null) {
|
|
273
308
|
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
274
309
|
}
|
|
275
|
-
if (useLegacyFunctionCalling) {
|
|
276
|
-
const openaiFunctions = [];
|
|
277
|
-
for (const tool of tools) {
|
|
278
|
-
if (tool.type === "provider-defined") {
|
|
279
|
-
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
280
|
-
} else {
|
|
281
|
-
openaiFunctions.push({
|
|
282
|
-
name: tool.name,
|
|
283
|
-
description: tool.description,
|
|
284
|
-
parameters: tool.parameters
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
if (toolChoice == null) {
|
|
289
|
-
return {
|
|
290
|
-
functions: openaiFunctions,
|
|
291
|
-
function_call: void 0,
|
|
292
|
-
toolWarnings
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
const type2 = toolChoice.type;
|
|
296
|
-
switch (type2) {
|
|
297
|
-
case "auto":
|
|
298
|
-
case "none":
|
|
299
|
-
case void 0:
|
|
300
|
-
return {
|
|
301
|
-
functions: openaiFunctions,
|
|
302
|
-
function_call: void 0,
|
|
303
|
-
toolWarnings
|
|
304
|
-
};
|
|
305
|
-
case "required":
|
|
306
|
-
throw new UnsupportedFunctionalityError2({
|
|
307
|
-
functionality: "useLegacyFunctionCalling and toolChoice: required"
|
|
308
|
-
});
|
|
309
|
-
default:
|
|
310
|
-
return {
|
|
311
|
-
functions: openaiFunctions,
|
|
312
|
-
function_call: { name: toolChoice.toolName },
|
|
313
|
-
toolWarnings
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
310
|
const openaiTools = [];
|
|
318
311
|
for (const tool of tools) {
|
|
319
312
|
if (tool.type === "provider-defined") {
|
|
@@ -385,7 +378,7 @@ var OpenAIChatLanguageModel = class {
|
|
|
385
378
|
}
|
|
386
379
|
getArgs({
|
|
387
380
|
prompt,
|
|
388
|
-
|
|
381
|
+
maxOutputTokens,
|
|
389
382
|
temperature,
|
|
390
383
|
topP,
|
|
391
384
|
topK,
|
|
@@ -398,8 +391,13 @@ var OpenAIChatLanguageModel = class {
|
|
|
398
391
|
toolChoice,
|
|
399
392
|
providerOptions
|
|
400
393
|
}) {
|
|
401
|
-
var _a, _b
|
|
394
|
+
var _a, _b;
|
|
402
395
|
const warnings = [];
|
|
396
|
+
const openaiOptions = (_a = parseProviderOptions({
|
|
397
|
+
provider: "openai",
|
|
398
|
+
providerOptions,
|
|
399
|
+
schema: openaiProviderOptions
|
|
400
|
+
})) != null ? _a : {};
|
|
403
401
|
if (topK != null) {
|
|
404
402
|
warnings.push({
|
|
405
403
|
type: "unsupported-setting",
|
|
@@ -413,21 +411,9 @@ var OpenAIChatLanguageModel = class {
|
|
|
413
411
|
details: "JSON response format schema is only supported with structuredOutputs"
|
|
414
412
|
});
|
|
415
413
|
}
|
|
416
|
-
const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
|
|
417
|
-
if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
|
|
418
|
-
throw new UnsupportedFunctionalityError3({
|
|
419
|
-
functionality: "useLegacyFunctionCalling with parallelToolCalls"
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
if (useLegacyFunctionCalling && this.supportsStructuredOutputs) {
|
|
423
|
-
throw new UnsupportedFunctionalityError3({
|
|
424
|
-
functionality: "structuredOutputs with useLegacyFunctionCalling"
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
414
|
const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages(
|
|
428
415
|
{
|
|
429
416
|
prompt,
|
|
430
|
-
useLegacyFunctionCalling,
|
|
431
417
|
systemMessageMode: getSystemMessageMode(this.modelId)
|
|
432
418
|
}
|
|
433
419
|
);
|
|
@@ -436,13 +422,13 @@ var OpenAIChatLanguageModel = class {
|
|
|
436
422
|
// model id:
|
|
437
423
|
model: this.modelId,
|
|
438
424
|
// model specific settings:
|
|
439
|
-
logit_bias:
|
|
440
|
-
logprobs:
|
|
441
|
-
top_logprobs: typeof
|
|
442
|
-
user:
|
|
443
|
-
parallel_tool_calls:
|
|
425
|
+
logit_bias: openaiOptions.logitBias,
|
|
426
|
+
logprobs: openaiOptions.logprobs === true || typeof openaiOptions.logprobs === "number" ? true : void 0,
|
|
427
|
+
top_logprobs: typeof openaiOptions.logprobs === "number" ? openaiOptions.logprobs : typeof openaiOptions.logprobs === "boolean" ? openaiOptions.logprobs ? 0 : void 0 : void 0,
|
|
428
|
+
user: openaiOptions.user,
|
|
429
|
+
parallel_tool_calls: openaiOptions.parallelToolCalls,
|
|
444
430
|
// standardized settings:
|
|
445
|
-
max_tokens:
|
|
431
|
+
max_tokens: maxOutputTokens,
|
|
446
432
|
temperature,
|
|
447
433
|
top_p: topP,
|
|
448
434
|
frequency_penalty: frequencyPenalty,
|
|
@@ -453,19 +439,19 @@ var OpenAIChatLanguageModel = class {
|
|
|
453
439
|
json_schema: {
|
|
454
440
|
schema: responseFormat.schema,
|
|
455
441
|
strict: true,
|
|
456
|
-
name: (
|
|
442
|
+
name: (_b = responseFormat.name) != null ? _b : "response",
|
|
457
443
|
description: responseFormat.description
|
|
458
444
|
}
|
|
459
445
|
} : { type: "json_object" } : void 0,
|
|
460
446
|
stop: stopSequences,
|
|
461
447
|
seed,
|
|
462
448
|
// openai specific settings:
|
|
463
|
-
// TODO remove in next major version; we auto-map
|
|
464
|
-
max_completion_tokens:
|
|
465
|
-
store:
|
|
466
|
-
metadata:
|
|
467
|
-
prediction:
|
|
468
|
-
reasoning_effort:
|
|
449
|
+
// TODO remove in next major version; we auto-map maxOutputTokens now
|
|
450
|
+
max_completion_tokens: openaiOptions.maxCompletionTokens,
|
|
451
|
+
store: openaiOptions.store,
|
|
452
|
+
metadata: openaiOptions.metadata,
|
|
453
|
+
prediction: openaiOptions.prediction,
|
|
454
|
+
reasoning_effort: openaiOptions.reasoningEffort,
|
|
469
455
|
// messages:
|
|
470
456
|
messages
|
|
471
457
|
};
|
|
@@ -529,26 +515,30 @@ var OpenAIChatLanguageModel = class {
|
|
|
529
515
|
}
|
|
530
516
|
baseArgs.max_tokens = void 0;
|
|
531
517
|
}
|
|
518
|
+
} else if (this.modelId.startsWith("gpt-4o-search-preview")) {
|
|
519
|
+
if (baseArgs.temperature != null) {
|
|
520
|
+
baseArgs.temperature = void 0;
|
|
521
|
+
warnings.push({
|
|
522
|
+
type: "unsupported-setting",
|
|
523
|
+
setting: "temperature",
|
|
524
|
+
details: "temperature is not supported for the gpt-4o-search-preview model and has been removed."
|
|
525
|
+
});
|
|
526
|
+
}
|
|
532
527
|
}
|
|
533
528
|
const {
|
|
534
529
|
tools: openaiTools,
|
|
535
530
|
toolChoice: openaiToolChoice,
|
|
536
|
-
functions,
|
|
537
|
-
function_call,
|
|
538
531
|
toolWarnings
|
|
539
532
|
} = prepareTools({
|
|
540
533
|
tools,
|
|
541
534
|
toolChoice,
|
|
542
|
-
useLegacyFunctionCalling,
|
|
543
535
|
structuredOutputs: this.supportsStructuredOutputs
|
|
544
536
|
});
|
|
545
537
|
return {
|
|
546
538
|
args: {
|
|
547
539
|
...baseArgs,
|
|
548
540
|
tools: openaiTools,
|
|
549
|
-
tool_choice: openaiToolChoice
|
|
550
|
-
functions,
|
|
551
|
-
function_call
|
|
541
|
+
tool_choice: openaiToolChoice
|
|
552
542
|
},
|
|
553
543
|
warnings: [...warnings, ...toolWarnings]
|
|
554
544
|
};
|
|
@@ -593,14 +583,7 @@ var OpenAIChatLanguageModel = class {
|
|
|
593
583
|
}
|
|
594
584
|
return {
|
|
595
585
|
text: (_c = choice.message.content) != null ? _c : void 0,
|
|
596
|
-
toolCalls:
|
|
597
|
-
{
|
|
598
|
-
toolCallType: "function",
|
|
599
|
-
toolCallId: generateId(),
|
|
600
|
-
toolName: choice.message.function_call.name,
|
|
601
|
-
args: choice.message.function_call.arguments
|
|
602
|
-
}
|
|
603
|
-
] : (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
|
|
586
|
+
toolCalls: (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
|
|
604
587
|
var _a2;
|
|
605
588
|
return {
|
|
606
589
|
toolCallType: "function",
|
|
@@ -611,11 +594,10 @@ var OpenAIChatLanguageModel = class {
|
|
|
611
594
|
}),
|
|
612
595
|
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
|
613
596
|
usage: {
|
|
614
|
-
|
|
615
|
-
|
|
597
|
+
inputTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : void 0,
|
|
598
|
+
outputTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : void 0
|
|
616
599
|
},
|
|
617
|
-
|
|
618
|
-
request: { body: JSON.stringify(body) },
|
|
600
|
+
request: { body },
|
|
619
601
|
response: {
|
|
620
602
|
...getResponseMetadata(response),
|
|
621
603
|
headers: responseHeaders,
|
|
@@ -627,49 +609,6 @@ var OpenAIChatLanguageModel = class {
|
|
|
627
609
|
};
|
|
628
610
|
}
|
|
629
611
|
async doStream(options) {
|
|
630
|
-
if (this.settings.simulateStreaming) {
|
|
631
|
-
const result = await this.doGenerate(options);
|
|
632
|
-
const simulatedStream = new ReadableStream({
|
|
633
|
-
start(controller) {
|
|
634
|
-
controller.enqueue({ type: "response-metadata", ...result.response });
|
|
635
|
-
if (result.text) {
|
|
636
|
-
controller.enqueue({
|
|
637
|
-
type: "text-delta",
|
|
638
|
-
textDelta: result.text
|
|
639
|
-
});
|
|
640
|
-
}
|
|
641
|
-
if (result.toolCalls) {
|
|
642
|
-
for (const toolCall of result.toolCalls) {
|
|
643
|
-
controller.enqueue({
|
|
644
|
-
type: "tool-call-delta",
|
|
645
|
-
toolCallType: "function",
|
|
646
|
-
toolCallId: toolCall.toolCallId,
|
|
647
|
-
toolName: toolCall.toolName,
|
|
648
|
-
argsTextDelta: toolCall.args
|
|
649
|
-
});
|
|
650
|
-
controller.enqueue({
|
|
651
|
-
type: "tool-call",
|
|
652
|
-
...toolCall
|
|
653
|
-
});
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
controller.enqueue({
|
|
657
|
-
type: "finish",
|
|
658
|
-
finishReason: result.finishReason,
|
|
659
|
-
usage: result.usage,
|
|
660
|
-
logprobs: result.logprobs,
|
|
661
|
-
providerMetadata: result.providerMetadata
|
|
662
|
-
});
|
|
663
|
-
controller.close();
|
|
664
|
-
}
|
|
665
|
-
});
|
|
666
|
-
return {
|
|
667
|
-
stream: simulatedStream,
|
|
668
|
-
rawCall: result.rawCall,
|
|
669
|
-
response: result.response,
|
|
670
|
-
warnings: result.warnings
|
|
671
|
-
};
|
|
672
|
-
}
|
|
673
612
|
const { args, warnings } = this.getArgs(options);
|
|
674
613
|
const body = {
|
|
675
614
|
...args,
|
|
@@ -694,13 +633,12 @@ var OpenAIChatLanguageModel = class {
|
|
|
694
633
|
const { messages: rawPrompt, ...rawSettings } = args;
|
|
695
634
|
const toolCalls = [];
|
|
696
635
|
let finishReason = "unknown";
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
636
|
+
const usage = {
|
|
637
|
+
inputTokens: void 0,
|
|
638
|
+
outputTokens: void 0
|
|
700
639
|
};
|
|
701
640
|
let logprobs;
|
|
702
641
|
let isFirstChunk = true;
|
|
703
|
-
const { useLegacyFunctionCalling } = this.settings;
|
|
704
642
|
const providerMetadata = { openai: {} };
|
|
705
643
|
return {
|
|
706
644
|
stream: response.pipeThrough(
|
|
@@ -732,10 +670,8 @@ var OpenAIChatLanguageModel = class {
|
|
|
732
670
|
prompt_tokens_details,
|
|
733
671
|
completion_tokens_details
|
|
734
672
|
} = value.usage;
|
|
735
|
-
usage =
|
|
736
|
-
|
|
737
|
-
completionTokens: completion_tokens != null ? completion_tokens : void 0
|
|
738
|
-
};
|
|
673
|
+
usage.inputTokens = prompt_tokens != null ? prompt_tokens : void 0;
|
|
674
|
+
usage.outputTokens = completion_tokens != null ? completion_tokens : void 0;
|
|
739
675
|
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
|
|
740
676
|
providerMetadata.openai.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
|
|
741
677
|
}
|
|
@@ -770,16 +706,8 @@ var OpenAIChatLanguageModel = class {
|
|
|
770
706
|
if (logprobs === void 0) logprobs = [];
|
|
771
707
|
logprobs.push(...mappedLogprobs);
|
|
772
708
|
}
|
|
773
|
-
|
|
774
|
-
{
|
|
775
|
-
type: "function",
|
|
776
|
-
id: generateId(),
|
|
777
|
-
function: delta.function_call,
|
|
778
|
-
index: 0
|
|
779
|
-
}
|
|
780
|
-
] : delta.tool_calls;
|
|
781
|
-
if (mappedToolCalls != null) {
|
|
782
|
-
for (const toolCallDelta of mappedToolCalls) {
|
|
709
|
+
if (delta.tool_calls != null) {
|
|
710
|
+
for (const toolCallDelta of delta.tool_calls) {
|
|
783
711
|
const index = toolCallDelta.index;
|
|
784
712
|
if (toolCalls[index] == null) {
|
|
785
713
|
if (toolCallDelta.type !== "function") {
|
|
@@ -861,125 +789,112 @@ var OpenAIChatLanguageModel = class {
|
|
|
861
789
|
}
|
|
862
790
|
},
|
|
863
791
|
flush(controller) {
|
|
864
|
-
var _a, _b;
|
|
865
792
|
controller.enqueue({
|
|
866
793
|
type: "finish",
|
|
867
794
|
finishReason,
|
|
868
795
|
logprobs,
|
|
869
|
-
usage
|
|
870
|
-
promptTokens: (_a = usage.promptTokens) != null ? _a : NaN,
|
|
871
|
-
completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
|
|
872
|
-
},
|
|
796
|
+
usage,
|
|
873
797
|
...providerMetadata != null ? { providerMetadata } : {}
|
|
874
798
|
});
|
|
875
799
|
}
|
|
876
800
|
})
|
|
877
801
|
),
|
|
878
|
-
|
|
802
|
+
request: { body },
|
|
879
803
|
response: { headers: responseHeaders },
|
|
880
|
-
request: { body: JSON.stringify(body) },
|
|
881
804
|
warnings
|
|
882
805
|
};
|
|
883
806
|
}
|
|
884
807
|
};
|
|
885
|
-
var openaiTokenUsageSchema =
|
|
886
|
-
prompt_tokens:
|
|
887
|
-
completion_tokens:
|
|
888
|
-
prompt_tokens_details:
|
|
889
|
-
cached_tokens:
|
|
808
|
+
var openaiTokenUsageSchema = z3.object({
|
|
809
|
+
prompt_tokens: z3.number().nullish(),
|
|
810
|
+
completion_tokens: z3.number().nullish(),
|
|
811
|
+
prompt_tokens_details: z3.object({
|
|
812
|
+
cached_tokens: z3.number().nullish()
|
|
890
813
|
}).nullish(),
|
|
891
|
-
completion_tokens_details:
|
|
892
|
-
reasoning_tokens:
|
|
893
|
-
accepted_prediction_tokens:
|
|
894
|
-
rejected_prediction_tokens:
|
|
814
|
+
completion_tokens_details: z3.object({
|
|
815
|
+
reasoning_tokens: z3.number().nullish(),
|
|
816
|
+
accepted_prediction_tokens: z3.number().nullish(),
|
|
817
|
+
rejected_prediction_tokens: z3.number().nullish()
|
|
895
818
|
}).nullish()
|
|
896
819
|
}).nullish();
|
|
897
|
-
var openaiChatResponseSchema =
|
|
898
|
-
id:
|
|
899
|
-
created:
|
|
900
|
-
model:
|
|
901
|
-
choices:
|
|
902
|
-
|
|
903
|
-
message:
|
|
904
|
-
role:
|
|
905
|
-
content:
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
type: z2.literal("function"),
|
|
914
|
-
function: z2.object({
|
|
915
|
-
name: z2.string(),
|
|
916
|
-
arguments: z2.string()
|
|
820
|
+
var openaiChatResponseSchema = z3.object({
|
|
821
|
+
id: z3.string().nullish(),
|
|
822
|
+
created: z3.number().nullish(),
|
|
823
|
+
model: z3.string().nullish(),
|
|
824
|
+
choices: z3.array(
|
|
825
|
+
z3.object({
|
|
826
|
+
message: z3.object({
|
|
827
|
+
role: z3.literal("assistant").nullish(),
|
|
828
|
+
content: z3.string().nullish(),
|
|
829
|
+
tool_calls: z3.array(
|
|
830
|
+
z3.object({
|
|
831
|
+
id: z3.string().nullish(),
|
|
832
|
+
type: z3.literal("function"),
|
|
833
|
+
function: z3.object({
|
|
834
|
+
name: z3.string(),
|
|
835
|
+
arguments: z3.string()
|
|
917
836
|
})
|
|
918
837
|
})
|
|
919
838
|
).nullish()
|
|
920
839
|
}),
|
|
921
|
-
index:
|
|
922
|
-
logprobs:
|
|
923
|
-
content:
|
|
924
|
-
|
|
925
|
-
token:
|
|
926
|
-
logprob:
|
|
927
|
-
top_logprobs:
|
|
928
|
-
|
|
929
|
-
token:
|
|
930
|
-
logprob:
|
|
840
|
+
index: z3.number(),
|
|
841
|
+
logprobs: z3.object({
|
|
842
|
+
content: z3.array(
|
|
843
|
+
z3.object({
|
|
844
|
+
token: z3.string(),
|
|
845
|
+
logprob: z3.number(),
|
|
846
|
+
top_logprobs: z3.array(
|
|
847
|
+
z3.object({
|
|
848
|
+
token: z3.string(),
|
|
849
|
+
logprob: z3.number()
|
|
931
850
|
})
|
|
932
851
|
)
|
|
933
852
|
})
|
|
934
853
|
).nullable()
|
|
935
854
|
}).nullish(),
|
|
936
|
-
finish_reason:
|
|
855
|
+
finish_reason: z3.string().nullish()
|
|
937
856
|
})
|
|
938
857
|
),
|
|
939
858
|
usage: openaiTokenUsageSchema
|
|
940
859
|
});
|
|
941
|
-
var openaiChatChunkSchema =
|
|
942
|
-
|
|
943
|
-
id:
|
|
944
|
-
created:
|
|
945
|
-
model:
|
|
946
|
-
choices:
|
|
947
|
-
|
|
948
|
-
delta:
|
|
949
|
-
role:
|
|
950
|
-
content:
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
type: z2.literal("function").optional(),
|
|
960
|
-
function: z2.object({
|
|
961
|
-
name: z2.string().nullish(),
|
|
962
|
-
arguments: z2.string().nullish()
|
|
860
|
+
var openaiChatChunkSchema = z3.union([
|
|
861
|
+
z3.object({
|
|
862
|
+
id: z3.string().nullish(),
|
|
863
|
+
created: z3.number().nullish(),
|
|
864
|
+
model: z3.string().nullish(),
|
|
865
|
+
choices: z3.array(
|
|
866
|
+
z3.object({
|
|
867
|
+
delta: z3.object({
|
|
868
|
+
role: z3.enum(["assistant"]).nullish(),
|
|
869
|
+
content: z3.string().nullish(),
|
|
870
|
+
tool_calls: z3.array(
|
|
871
|
+
z3.object({
|
|
872
|
+
index: z3.number(),
|
|
873
|
+
id: z3.string().nullish(),
|
|
874
|
+
type: z3.literal("function").optional(),
|
|
875
|
+
function: z3.object({
|
|
876
|
+
name: z3.string().nullish(),
|
|
877
|
+
arguments: z3.string().nullish()
|
|
963
878
|
})
|
|
964
879
|
})
|
|
965
880
|
).nullish()
|
|
966
881
|
}).nullish(),
|
|
967
|
-
logprobs:
|
|
968
|
-
content:
|
|
969
|
-
|
|
970
|
-
token:
|
|
971
|
-
logprob:
|
|
972
|
-
top_logprobs:
|
|
973
|
-
|
|
974
|
-
token:
|
|
975
|
-
logprob:
|
|
882
|
+
logprobs: z3.object({
|
|
883
|
+
content: z3.array(
|
|
884
|
+
z3.object({
|
|
885
|
+
token: z3.string(),
|
|
886
|
+
logprob: z3.number(),
|
|
887
|
+
top_logprobs: z3.array(
|
|
888
|
+
z3.object({
|
|
889
|
+
token: z3.string(),
|
|
890
|
+
logprob: z3.number()
|
|
976
891
|
})
|
|
977
892
|
)
|
|
978
893
|
})
|
|
979
894
|
).nullable()
|
|
980
895
|
}).nullish(),
|
|
981
|
-
finish_reason:
|
|
982
|
-
index:
|
|
896
|
+
finish_reason: z3.string().nullable().optional(),
|
|
897
|
+
index: z3.number()
|
|
983
898
|
})
|
|
984
899
|
),
|
|
985
900
|
usage: openaiTokenUsageSchema
|
|
@@ -1027,12 +942,12 @@ import {
|
|
|
1027
942
|
createJsonResponseHandler as createJsonResponseHandler2,
|
|
1028
943
|
postJsonToApi as postJsonToApi2
|
|
1029
944
|
} from "@ai-sdk/provider-utils";
|
|
1030
|
-
import { z as
|
|
945
|
+
import { z as z4 } from "zod";
|
|
1031
946
|
|
|
1032
947
|
// src/convert-to-openai-completion-prompt.ts
|
|
1033
948
|
import {
|
|
1034
949
|
InvalidPromptError,
|
|
1035
|
-
UnsupportedFunctionalityError as
|
|
950
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
1036
951
|
} from "@ai-sdk/provider";
|
|
1037
952
|
function convertToOpenAICompletionPrompt({
|
|
1038
953
|
prompt,
|
|
@@ -1079,7 +994,7 @@ ${userMessage}
|
|
|
1079
994
|
return part.text;
|
|
1080
995
|
}
|
|
1081
996
|
case "tool-call": {
|
|
1082
|
-
throw new
|
|
997
|
+
throw new UnsupportedFunctionalityError3({
|
|
1083
998
|
functionality: "tool-call messages"
|
|
1084
999
|
});
|
|
1085
1000
|
}
|
|
@@ -1092,7 +1007,7 @@ ${assistantMessage}
|
|
|
1092
1007
|
break;
|
|
1093
1008
|
}
|
|
1094
1009
|
case "tool": {
|
|
1095
|
-
throw new
|
|
1010
|
+
throw new UnsupportedFunctionalityError3({
|
|
1096
1011
|
functionality: "tool messages"
|
|
1097
1012
|
});
|
|
1098
1013
|
}
|
|
@@ -1140,7 +1055,7 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1140
1055
|
getArgs({
|
|
1141
1056
|
inputFormat,
|
|
1142
1057
|
prompt,
|
|
1143
|
-
|
|
1058
|
+
maxOutputTokens,
|
|
1144
1059
|
temperature,
|
|
1145
1060
|
topP,
|
|
1146
1061
|
topK,
|
|
@@ -1182,7 +1097,7 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1182
1097
|
suffix: this.settings.suffix,
|
|
1183
1098
|
user: this.settings.user,
|
|
1184
1099
|
// standardized settings:
|
|
1185
|
-
max_tokens:
|
|
1100
|
+
max_tokens: maxOutputTokens,
|
|
1186
1101
|
temperature,
|
|
1187
1102
|
top_p: topP,
|
|
1188
1103
|
frequency_penalty: frequencyPenalty,
|
|
@@ -1216,18 +1131,16 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1216
1131
|
abortSignal: options.abortSignal,
|
|
1217
1132
|
fetch: this.config.fetch
|
|
1218
1133
|
});
|
|
1219
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
|
1220
1134
|
const choice = response.choices[0];
|
|
1221
1135
|
return {
|
|
1222
1136
|
text: choice.text,
|
|
1223
1137
|
usage: {
|
|
1224
|
-
|
|
1225
|
-
|
|
1138
|
+
inputTokens: response.usage.prompt_tokens,
|
|
1139
|
+
outputTokens: response.usage.completion_tokens
|
|
1226
1140
|
},
|
|
1227
1141
|
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
|
1228
1142
|
logprobs: mapOpenAICompletionLogProbs(choice.logprobs),
|
|
1229
|
-
|
|
1230
|
-
request: { body: JSON.stringify(args) },
|
|
1143
|
+
request: { body: args },
|
|
1231
1144
|
response: {
|
|
1232
1145
|
...getResponseMetadata(response),
|
|
1233
1146
|
headers: responseHeaders,
|
|
@@ -1258,11 +1171,10 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1258
1171
|
abortSignal: options.abortSignal,
|
|
1259
1172
|
fetch: this.config.fetch
|
|
1260
1173
|
});
|
|
1261
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
|
1262
1174
|
let finishReason = "unknown";
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1175
|
+
const usage = {
|
|
1176
|
+
inputTokens: void 0,
|
|
1177
|
+
outputTokens: void 0
|
|
1266
1178
|
};
|
|
1267
1179
|
let logprobs;
|
|
1268
1180
|
let isFirstChunk = true;
|
|
@@ -1289,10 +1201,8 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1289
1201
|
});
|
|
1290
1202
|
}
|
|
1291
1203
|
if (value.usage != null) {
|
|
1292
|
-
usage =
|
|
1293
|
-
|
|
1294
|
-
completionTokens: value.usage.completion_tokens
|
|
1295
|
-
};
|
|
1204
|
+
usage.inputTokens = value.usage.prompt_tokens;
|
|
1205
|
+
usage.outputTokens = value.usage.completion_tokens;
|
|
1296
1206
|
}
|
|
1297
1207
|
const choice = value.choices[0];
|
|
1298
1208
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -1322,53 +1232,52 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1322
1232
|
}
|
|
1323
1233
|
})
|
|
1324
1234
|
),
|
|
1325
|
-
rawCall: { rawPrompt, rawSettings },
|
|
1326
1235
|
response: { headers: responseHeaders },
|
|
1327
1236
|
warnings,
|
|
1328
1237
|
request: { body: JSON.stringify(body) }
|
|
1329
1238
|
};
|
|
1330
1239
|
}
|
|
1331
1240
|
};
|
|
1332
|
-
var openaiCompletionResponseSchema =
|
|
1333
|
-
id:
|
|
1334
|
-
created:
|
|
1335
|
-
model:
|
|
1336
|
-
choices:
|
|
1337
|
-
|
|
1338
|
-
text:
|
|
1339
|
-
finish_reason:
|
|
1340
|
-
logprobs:
|
|
1341
|
-
tokens:
|
|
1342
|
-
token_logprobs:
|
|
1343
|
-
top_logprobs:
|
|
1241
|
+
var openaiCompletionResponseSchema = z4.object({
|
|
1242
|
+
id: z4.string().nullish(),
|
|
1243
|
+
created: z4.number().nullish(),
|
|
1244
|
+
model: z4.string().nullish(),
|
|
1245
|
+
choices: z4.array(
|
|
1246
|
+
z4.object({
|
|
1247
|
+
text: z4.string(),
|
|
1248
|
+
finish_reason: z4.string(),
|
|
1249
|
+
logprobs: z4.object({
|
|
1250
|
+
tokens: z4.array(z4.string()),
|
|
1251
|
+
token_logprobs: z4.array(z4.number()),
|
|
1252
|
+
top_logprobs: z4.array(z4.record(z4.string(), z4.number())).nullable()
|
|
1344
1253
|
}).nullish()
|
|
1345
1254
|
})
|
|
1346
1255
|
),
|
|
1347
|
-
usage:
|
|
1348
|
-
prompt_tokens:
|
|
1349
|
-
completion_tokens:
|
|
1256
|
+
usage: z4.object({
|
|
1257
|
+
prompt_tokens: z4.number(),
|
|
1258
|
+
completion_tokens: z4.number()
|
|
1350
1259
|
})
|
|
1351
1260
|
});
|
|
1352
|
-
var openaiCompletionChunkSchema =
|
|
1353
|
-
|
|
1354
|
-
id:
|
|
1355
|
-
created:
|
|
1356
|
-
model:
|
|
1357
|
-
choices:
|
|
1358
|
-
|
|
1359
|
-
text:
|
|
1360
|
-
finish_reason:
|
|
1361
|
-
index:
|
|
1362
|
-
logprobs:
|
|
1363
|
-
tokens:
|
|
1364
|
-
token_logprobs:
|
|
1365
|
-
top_logprobs:
|
|
1261
|
+
var openaiCompletionChunkSchema = z4.union([
|
|
1262
|
+
z4.object({
|
|
1263
|
+
id: z4.string().nullish(),
|
|
1264
|
+
created: z4.number().nullish(),
|
|
1265
|
+
model: z4.string().nullish(),
|
|
1266
|
+
choices: z4.array(
|
|
1267
|
+
z4.object({
|
|
1268
|
+
text: z4.string(),
|
|
1269
|
+
finish_reason: z4.string().nullish(),
|
|
1270
|
+
index: z4.number(),
|
|
1271
|
+
logprobs: z4.object({
|
|
1272
|
+
tokens: z4.array(z4.string()),
|
|
1273
|
+
token_logprobs: z4.array(z4.number()),
|
|
1274
|
+
top_logprobs: z4.array(z4.record(z4.string(), z4.number())).nullable()
|
|
1366
1275
|
}).nullish()
|
|
1367
1276
|
})
|
|
1368
1277
|
),
|
|
1369
|
-
usage:
|
|
1370
|
-
prompt_tokens:
|
|
1371
|
-
completion_tokens:
|
|
1278
|
+
usage: z4.object({
|
|
1279
|
+
prompt_tokens: z4.number(),
|
|
1280
|
+
completion_tokens: z4.number()
|
|
1372
1281
|
}).nullish()
|
|
1373
1282
|
}),
|
|
1374
1283
|
openaiErrorDataSchema
|
|
@@ -1383,7 +1292,7 @@ import {
|
|
|
1383
1292
|
createJsonResponseHandler as createJsonResponseHandler3,
|
|
1384
1293
|
postJsonToApi as postJsonToApi3
|
|
1385
1294
|
} from "@ai-sdk/provider-utils";
|
|
1386
|
-
import { z as
|
|
1295
|
+
import { z as z5 } from "zod";
|
|
1387
1296
|
var OpenAIEmbeddingModel = class {
|
|
1388
1297
|
constructor(modelId, settings, config) {
|
|
1389
1298
|
this.specificationVersion = "v1";
|
|
@@ -1442,9 +1351,9 @@ var OpenAIEmbeddingModel = class {
|
|
|
1442
1351
|
};
|
|
1443
1352
|
}
|
|
1444
1353
|
};
|
|
1445
|
-
var openaiTextEmbeddingResponseSchema =
|
|
1446
|
-
data:
|
|
1447
|
-
usage:
|
|
1354
|
+
var openaiTextEmbeddingResponseSchema = z5.object({
|
|
1355
|
+
data: z5.array(z5.object({ embedding: z5.array(z5.number()) })),
|
|
1356
|
+
usage: z5.object({ prompt_tokens: z5.number() }).nullish()
|
|
1448
1357
|
});
|
|
1449
1358
|
|
|
1450
1359
|
// src/openai-image-model.ts
|
|
@@ -1453,7 +1362,7 @@ import {
|
|
|
1453
1362
|
createJsonResponseHandler as createJsonResponseHandler4,
|
|
1454
1363
|
postJsonToApi as postJsonToApi4
|
|
1455
1364
|
} from "@ai-sdk/provider-utils";
|
|
1456
|
-
import { z as
|
|
1365
|
+
import { z as z6 } from "zod";
|
|
1457
1366
|
|
|
1458
1367
|
// src/openai-image-settings.ts
|
|
1459
1368
|
var modelMaxImagesPerCall = {
|
|
@@ -1531,8 +1440,8 @@ var OpenAIImageModel = class {
|
|
|
1531
1440
|
};
|
|
1532
1441
|
}
|
|
1533
1442
|
};
|
|
1534
|
-
var openaiImageResponseSchema =
|
|
1535
|
-
data:
|
|
1443
|
+
var openaiImageResponseSchema = z6.object({
|
|
1444
|
+
data: z6.array(z6.object({ b64_json: z6.string() }))
|
|
1536
1445
|
});
|
|
1537
1446
|
|
|
1538
1447
|
// src/openai-transcription-model.ts
|
|
@@ -1540,22 +1449,16 @@ import {
|
|
|
1540
1449
|
combineHeaders as combineHeaders5,
|
|
1541
1450
|
convertBase64ToUint8Array,
|
|
1542
1451
|
createJsonResponseHandler as createJsonResponseHandler5,
|
|
1543
|
-
parseProviderOptions,
|
|
1452
|
+
parseProviderOptions as parseProviderOptions2,
|
|
1544
1453
|
postFormDataToApi
|
|
1545
1454
|
} from "@ai-sdk/provider-utils";
|
|
1546
|
-
import { z as
|
|
1547
|
-
var OpenAIProviderOptionsSchema =
|
|
1548
|
-
include:
|
|
1549
|
-
|
|
1550
|
-
),
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
"An optional text to guide the model's style or continue a previous audio segment."
|
|
1554
|
-
),
|
|
1555
|
-
temperature: z6.number().min(0).max(1).optional().default(0).describe("The sampling temperature, between 0 and 1."),
|
|
1556
|
-
timestampGranularities: z6.array(z6.enum(["word", "segment"])).optional().default(["segment"]).describe(
|
|
1557
|
-
"The timestamp granularities to populate for this transcription."
|
|
1558
|
-
)
|
|
1455
|
+
import { z as z7 } from "zod";
|
|
1456
|
+
var OpenAIProviderOptionsSchema = z7.object({
|
|
1457
|
+
include: z7.array(z7.string()).nullish(),
|
|
1458
|
+
language: z7.string().nullish(),
|
|
1459
|
+
prompt: z7.string().nullish(),
|
|
1460
|
+
temperature: z7.number().min(0).max(1).nullish().default(0),
|
|
1461
|
+
timestampGranularities: z7.array(z7.enum(["word", "segment"])).nullish().default(["segment"])
|
|
1559
1462
|
});
|
|
1560
1463
|
var languageMap = {
|
|
1561
1464
|
afrikaans: "af",
|
|
@@ -1630,8 +1533,9 @@ var OpenAITranscriptionModel = class {
|
|
|
1630
1533
|
mediaType,
|
|
1631
1534
|
providerOptions
|
|
1632
1535
|
}) {
|
|
1536
|
+
var _a, _b, _c, _d, _e;
|
|
1633
1537
|
const warnings = [];
|
|
1634
|
-
const openAIOptions =
|
|
1538
|
+
const openAIOptions = parseProviderOptions2({
|
|
1635
1539
|
provider: "openai",
|
|
1636
1540
|
providerOptions,
|
|
1637
1541
|
schema: OpenAIProviderOptionsSchema
|
|
@@ -1642,16 +1546,16 @@ var OpenAITranscriptionModel = class {
|
|
|
1642
1546
|
formData.append("file", new File([blob], "audio", { type: mediaType }));
|
|
1643
1547
|
if (openAIOptions) {
|
|
1644
1548
|
const transcriptionModelOptions = {
|
|
1645
|
-
include: openAIOptions.include,
|
|
1646
|
-
language: openAIOptions.language,
|
|
1647
|
-
prompt: openAIOptions.prompt,
|
|
1648
|
-
temperature: openAIOptions.temperature,
|
|
1649
|
-
timestamp_granularities: openAIOptions.timestampGranularities
|
|
1549
|
+
include: (_a = openAIOptions.include) != null ? _a : void 0,
|
|
1550
|
+
language: (_b = openAIOptions.language) != null ? _b : void 0,
|
|
1551
|
+
prompt: (_c = openAIOptions.prompt) != null ? _c : void 0,
|
|
1552
|
+
temperature: (_d = openAIOptions.temperature) != null ? _d : void 0,
|
|
1553
|
+
timestamp_granularities: (_e = openAIOptions.timestampGranularities) != null ? _e : void 0
|
|
1650
1554
|
};
|
|
1651
1555
|
for (const key in transcriptionModelOptions) {
|
|
1652
1556
|
const value = transcriptionModelOptions[key];
|
|
1653
1557
|
if (value !== void 0) {
|
|
1654
|
-
formData.append(key, value);
|
|
1558
|
+
formData.append(key, String(value));
|
|
1655
1559
|
}
|
|
1656
1560
|
}
|
|
1657
1561
|
}
|
|
@@ -1702,15 +1606,15 @@ var OpenAITranscriptionModel = class {
|
|
|
1702
1606
|
};
|
|
1703
1607
|
}
|
|
1704
1608
|
};
|
|
1705
|
-
var openaiTranscriptionResponseSchema =
|
|
1706
|
-
text:
|
|
1707
|
-
language:
|
|
1708
|
-
duration:
|
|
1709
|
-
words:
|
|
1710
|
-
|
|
1711
|
-
word:
|
|
1712
|
-
start:
|
|
1713
|
-
end:
|
|
1609
|
+
var openaiTranscriptionResponseSchema = z7.object({
|
|
1610
|
+
text: z7.string(),
|
|
1611
|
+
language: z7.string().nullish(),
|
|
1612
|
+
duration: z7.number().nullish(),
|
|
1613
|
+
words: z7.array(
|
|
1614
|
+
z7.object({
|
|
1615
|
+
word: z7.string(),
|
|
1616
|
+
start: z7.number(),
|
|
1617
|
+
end: z7.number()
|
|
1714
1618
|
})
|
|
1715
1619
|
).nullish()
|
|
1716
1620
|
});
|
|
@@ -1721,14 +1625,14 @@ import {
|
|
|
1721
1625
|
createEventSourceResponseHandler as createEventSourceResponseHandler3,
|
|
1722
1626
|
createJsonResponseHandler as createJsonResponseHandler6,
|
|
1723
1627
|
generateId as generateId2,
|
|
1724
|
-
parseProviderOptions as
|
|
1628
|
+
parseProviderOptions as parseProviderOptions3,
|
|
1725
1629
|
postJsonToApi as postJsonToApi5
|
|
1726
1630
|
} from "@ai-sdk/provider-utils";
|
|
1727
|
-
import { z as
|
|
1631
|
+
import { z as z8 } from "zod";
|
|
1728
1632
|
|
|
1729
1633
|
// src/responses/convert-to-openai-responses-messages.ts
|
|
1730
1634
|
import {
|
|
1731
|
-
UnsupportedFunctionalityError as
|
|
1635
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError4
|
|
1732
1636
|
} from "@ai-sdk/provider";
|
|
1733
1637
|
function convertToOpenAIResponsesMessages({
|
|
1734
1638
|
prompt,
|
|
@@ -1784,7 +1688,7 @@ function convertToOpenAIResponsesMessages({
|
|
|
1784
1688
|
};
|
|
1785
1689
|
} else if (part.mediaType === "application/pdf") {
|
|
1786
1690
|
if (part.data instanceof URL) {
|
|
1787
|
-
throw new
|
|
1691
|
+
throw new UnsupportedFunctionalityError4({
|
|
1788
1692
|
functionality: "PDF file parts with URLs"
|
|
1789
1693
|
});
|
|
1790
1694
|
}
|
|
@@ -1794,7 +1698,7 @@ function convertToOpenAIResponsesMessages({
|
|
|
1794
1698
|
file_data: `data:application/pdf;base64,${part.data}`
|
|
1795
1699
|
};
|
|
1796
1700
|
} else {
|
|
1797
|
-
throw new
|
|
1701
|
+
throw new UnsupportedFunctionalityError4({
|
|
1798
1702
|
functionality: `file part media type ${part.mediaType}`
|
|
1799
1703
|
});
|
|
1800
1704
|
}
|
|
@@ -1866,7 +1770,7 @@ function mapOpenAIResponseFinishReason({
|
|
|
1866
1770
|
|
|
1867
1771
|
// src/responses/openai-responses-prepare-tools.ts
|
|
1868
1772
|
import {
|
|
1869
|
-
UnsupportedFunctionalityError as
|
|
1773
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError5
|
|
1870
1774
|
} from "@ai-sdk/provider";
|
|
1871
1775
|
function prepareResponsesTools({
|
|
1872
1776
|
tools,
|
|
@@ -1926,7 +1830,7 @@ function prepareResponsesTools({
|
|
|
1926
1830
|
};
|
|
1927
1831
|
default: {
|
|
1928
1832
|
const _exhaustiveCheck = type;
|
|
1929
|
-
throw new
|
|
1833
|
+
throw new UnsupportedFunctionalityError5({
|
|
1930
1834
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
1931
1835
|
});
|
|
1932
1836
|
}
|
|
@@ -1945,7 +1849,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1945
1849
|
return this.config.provider;
|
|
1946
1850
|
}
|
|
1947
1851
|
getArgs({
|
|
1948
|
-
|
|
1852
|
+
maxOutputTokens,
|
|
1949
1853
|
temperature,
|
|
1950
1854
|
stopSequences,
|
|
1951
1855
|
topP,
|
|
@@ -1988,7 +1892,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1988
1892
|
systemMessageMode: modelConfig.systemMessageMode
|
|
1989
1893
|
});
|
|
1990
1894
|
warnings.push(...messageWarnings);
|
|
1991
|
-
const openaiOptions =
|
|
1895
|
+
const openaiOptions = parseProviderOptions3({
|
|
1992
1896
|
provider: "openai",
|
|
1993
1897
|
providerOptions,
|
|
1994
1898
|
schema: openaiResponsesProviderOptionsSchema
|
|
@@ -1999,7 +1903,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1999
1903
|
input: messages,
|
|
2000
1904
|
temperature,
|
|
2001
1905
|
top_p: topP,
|
|
2002
|
-
max_output_tokens:
|
|
1906
|
+
max_output_tokens: maxOutputTokens,
|
|
2003
1907
|
...(responseFormat == null ? void 0 : responseFormat.type) === "json" && {
|
|
2004
1908
|
text: {
|
|
2005
1909
|
format: responseFormat.schema != null ? {
|
|
@@ -2078,49 +1982,49 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2078
1982
|
body,
|
|
2079
1983
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
2080
1984
|
successfulResponseHandler: createJsonResponseHandler6(
|
|
2081
|
-
|
|
2082
|
-
id:
|
|
2083
|
-
created_at:
|
|
2084
|
-
model:
|
|
2085
|
-
output:
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
type:
|
|
2089
|
-
role:
|
|
2090
|
-
content:
|
|
2091
|
-
|
|
2092
|
-
type:
|
|
2093
|
-
text:
|
|
2094
|
-
annotations:
|
|
2095
|
-
|
|
2096
|
-
type:
|
|
2097
|
-
start_index:
|
|
2098
|
-
end_index:
|
|
2099
|
-
url:
|
|
2100
|
-
title:
|
|
1985
|
+
z8.object({
|
|
1986
|
+
id: z8.string(),
|
|
1987
|
+
created_at: z8.number(),
|
|
1988
|
+
model: z8.string(),
|
|
1989
|
+
output: z8.array(
|
|
1990
|
+
z8.discriminatedUnion("type", [
|
|
1991
|
+
z8.object({
|
|
1992
|
+
type: z8.literal("message"),
|
|
1993
|
+
role: z8.literal("assistant"),
|
|
1994
|
+
content: z8.array(
|
|
1995
|
+
z8.object({
|
|
1996
|
+
type: z8.literal("output_text"),
|
|
1997
|
+
text: z8.string(),
|
|
1998
|
+
annotations: z8.array(
|
|
1999
|
+
z8.object({
|
|
2000
|
+
type: z8.literal("url_citation"),
|
|
2001
|
+
start_index: z8.number(),
|
|
2002
|
+
end_index: z8.number(),
|
|
2003
|
+
url: z8.string(),
|
|
2004
|
+
title: z8.string()
|
|
2101
2005
|
})
|
|
2102
2006
|
)
|
|
2103
2007
|
})
|
|
2104
2008
|
)
|
|
2105
2009
|
}),
|
|
2106
|
-
|
|
2107
|
-
type:
|
|
2108
|
-
call_id:
|
|
2109
|
-
name:
|
|
2110
|
-
arguments:
|
|
2010
|
+
z8.object({
|
|
2011
|
+
type: z8.literal("function_call"),
|
|
2012
|
+
call_id: z8.string(),
|
|
2013
|
+
name: z8.string(),
|
|
2014
|
+
arguments: z8.string()
|
|
2111
2015
|
}),
|
|
2112
|
-
|
|
2113
|
-
type:
|
|
2016
|
+
z8.object({
|
|
2017
|
+
type: z8.literal("web_search_call")
|
|
2114
2018
|
}),
|
|
2115
|
-
|
|
2116
|
-
type:
|
|
2019
|
+
z8.object({
|
|
2020
|
+
type: z8.literal("computer_call")
|
|
2117
2021
|
}),
|
|
2118
|
-
|
|
2119
|
-
type:
|
|
2022
|
+
z8.object({
|
|
2023
|
+
type: z8.literal("reasoning")
|
|
2120
2024
|
})
|
|
2121
2025
|
])
|
|
2122
2026
|
),
|
|
2123
|
-
incomplete_details:
|
|
2027
|
+
incomplete_details: z8.object({ reason: z8.string() }).nullable(),
|
|
2124
2028
|
usage: usageSchema
|
|
2125
2029
|
})
|
|
2126
2030
|
),
|
|
@@ -2153,16 +2057,10 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2153
2057
|
}),
|
|
2154
2058
|
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
2155
2059
|
usage: {
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
},
|
|
2159
|
-
rawCall: {
|
|
2160
|
-
rawPrompt: void 0,
|
|
2161
|
-
rawSettings: {}
|
|
2162
|
-
},
|
|
2163
|
-
request: {
|
|
2164
|
-
body: JSON.stringify(body)
|
|
2060
|
+
inputTokens: response.usage.input_tokens,
|
|
2061
|
+
outputTokens: response.usage.output_tokens
|
|
2165
2062
|
},
|
|
2063
|
+
request: { body },
|
|
2166
2064
|
response: {
|
|
2167
2065
|
id: response.id,
|
|
2168
2066
|
timestamp: new Date(response.created_at * 1e3),
|
|
@@ -2201,8 +2099,10 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2201
2099
|
});
|
|
2202
2100
|
const self = this;
|
|
2203
2101
|
let finishReason = "unknown";
|
|
2204
|
-
|
|
2205
|
-
|
|
2102
|
+
const usage = {
|
|
2103
|
+
inputTokens: void 0,
|
|
2104
|
+
outputTokens: void 0
|
|
2105
|
+
};
|
|
2206
2106
|
let cachedPromptTokens = null;
|
|
2207
2107
|
let reasoningTokens = null;
|
|
2208
2108
|
let responseId = null;
|
|
@@ -2272,8 +2172,8 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2272
2172
|
finishReason: (_a = value.response.incomplete_details) == null ? void 0 : _a.reason,
|
|
2273
2173
|
hasToolCalls
|
|
2274
2174
|
});
|
|
2275
|
-
|
|
2276
|
-
|
|
2175
|
+
usage.inputTokens = value.response.usage.input_tokens;
|
|
2176
|
+
usage.outputTokens = value.response.usage.output_tokens;
|
|
2277
2177
|
cachedPromptTokens = (_c = (_b = value.response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : cachedPromptTokens;
|
|
2278
2178
|
reasoningTokens = (_e = (_d = value.response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : reasoningTokens;
|
|
2279
2179
|
} else if (isResponseAnnotationAddedChunk(value)) {
|
|
@@ -2292,7 +2192,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2292
2192
|
controller.enqueue({
|
|
2293
2193
|
type: "finish",
|
|
2294
2194
|
finishReason,
|
|
2295
|
-
usage
|
|
2195
|
+
usage,
|
|
2296
2196
|
...(cachedPromptTokens != null || reasoningTokens != null) && {
|
|
2297
2197
|
providerMetadata: {
|
|
2298
2198
|
openai: {
|
|
@@ -2306,89 +2206,85 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2306
2206
|
}
|
|
2307
2207
|
})
|
|
2308
2208
|
),
|
|
2309
|
-
|
|
2310
|
-
rawPrompt: void 0,
|
|
2311
|
-
rawSettings: {}
|
|
2312
|
-
},
|
|
2313
|
-
request: { body: JSON.stringify(body) },
|
|
2209
|
+
request: { body },
|
|
2314
2210
|
response: { headers: responseHeaders },
|
|
2315
2211
|
warnings
|
|
2316
2212
|
};
|
|
2317
2213
|
}
|
|
2318
2214
|
};
|
|
2319
|
-
var usageSchema =
|
|
2320
|
-
input_tokens:
|
|
2321
|
-
input_tokens_details:
|
|
2322
|
-
output_tokens:
|
|
2323
|
-
output_tokens_details:
|
|
2215
|
+
var usageSchema = z8.object({
|
|
2216
|
+
input_tokens: z8.number(),
|
|
2217
|
+
input_tokens_details: z8.object({ cached_tokens: z8.number().nullish() }).nullish(),
|
|
2218
|
+
output_tokens: z8.number(),
|
|
2219
|
+
output_tokens_details: z8.object({ reasoning_tokens: z8.number().nullish() }).nullish()
|
|
2324
2220
|
});
|
|
2325
|
-
var textDeltaChunkSchema =
|
|
2326
|
-
type:
|
|
2327
|
-
delta:
|
|
2221
|
+
var textDeltaChunkSchema = z8.object({
|
|
2222
|
+
type: z8.literal("response.output_text.delta"),
|
|
2223
|
+
delta: z8.string()
|
|
2328
2224
|
});
|
|
2329
|
-
var responseFinishedChunkSchema =
|
|
2330
|
-
type:
|
|
2331
|
-
response:
|
|
2332
|
-
incomplete_details:
|
|
2225
|
+
var responseFinishedChunkSchema = z8.object({
|
|
2226
|
+
type: z8.enum(["response.completed", "response.incomplete"]),
|
|
2227
|
+
response: z8.object({
|
|
2228
|
+
incomplete_details: z8.object({ reason: z8.string() }).nullish(),
|
|
2333
2229
|
usage: usageSchema
|
|
2334
2230
|
})
|
|
2335
2231
|
});
|
|
2336
|
-
var responseCreatedChunkSchema =
|
|
2337
|
-
type:
|
|
2338
|
-
response:
|
|
2339
|
-
id:
|
|
2340
|
-
created_at:
|
|
2341
|
-
model:
|
|
2232
|
+
var responseCreatedChunkSchema = z8.object({
|
|
2233
|
+
type: z8.literal("response.created"),
|
|
2234
|
+
response: z8.object({
|
|
2235
|
+
id: z8.string(),
|
|
2236
|
+
created_at: z8.number(),
|
|
2237
|
+
model: z8.string()
|
|
2342
2238
|
})
|
|
2343
2239
|
});
|
|
2344
|
-
var responseOutputItemDoneSchema =
|
|
2345
|
-
type:
|
|
2346
|
-
output_index:
|
|
2347
|
-
item:
|
|
2348
|
-
|
|
2349
|
-
type:
|
|
2240
|
+
var responseOutputItemDoneSchema = z8.object({
|
|
2241
|
+
type: z8.literal("response.output_item.done"),
|
|
2242
|
+
output_index: z8.number(),
|
|
2243
|
+
item: z8.discriminatedUnion("type", [
|
|
2244
|
+
z8.object({
|
|
2245
|
+
type: z8.literal("message")
|
|
2350
2246
|
}),
|
|
2351
|
-
|
|
2352
|
-
type:
|
|
2353
|
-
id:
|
|
2354
|
-
call_id:
|
|
2355
|
-
name:
|
|
2356
|
-
arguments:
|
|
2357
|
-
status:
|
|
2247
|
+
z8.object({
|
|
2248
|
+
type: z8.literal("function_call"),
|
|
2249
|
+
id: z8.string(),
|
|
2250
|
+
call_id: z8.string(),
|
|
2251
|
+
name: z8.string(),
|
|
2252
|
+
arguments: z8.string(),
|
|
2253
|
+
status: z8.literal("completed")
|
|
2358
2254
|
})
|
|
2359
2255
|
])
|
|
2360
2256
|
});
|
|
2361
|
-
var responseFunctionCallArgumentsDeltaSchema =
|
|
2362
|
-
type:
|
|
2363
|
-
item_id:
|
|
2364
|
-
output_index:
|
|
2365
|
-
delta:
|
|
2257
|
+
var responseFunctionCallArgumentsDeltaSchema = z8.object({
|
|
2258
|
+
type: z8.literal("response.function_call_arguments.delta"),
|
|
2259
|
+
item_id: z8.string(),
|
|
2260
|
+
output_index: z8.number(),
|
|
2261
|
+
delta: z8.string()
|
|
2366
2262
|
});
|
|
2367
|
-
var responseOutputItemAddedSchema =
|
|
2368
|
-
type:
|
|
2369
|
-
output_index:
|
|
2370
|
-
item:
|
|
2371
|
-
|
|
2372
|
-
type:
|
|
2263
|
+
var responseOutputItemAddedSchema = z8.object({
|
|
2264
|
+
type: z8.literal("response.output_item.added"),
|
|
2265
|
+
output_index: z8.number(),
|
|
2266
|
+
item: z8.discriminatedUnion("type", [
|
|
2267
|
+
z8.object({
|
|
2268
|
+
type: z8.literal("message")
|
|
2373
2269
|
}),
|
|
2374
|
-
|
|
2375
|
-
type:
|
|
2376
|
-
id:
|
|
2377
|
-
call_id:
|
|
2378
|
-
name:
|
|
2379
|
-
arguments:
|
|
2270
|
+
z8.object({
|
|
2271
|
+
type: z8.literal("function_call"),
|
|
2272
|
+
id: z8.string(),
|
|
2273
|
+
call_id: z8.string(),
|
|
2274
|
+
name: z8.string(),
|
|
2275
|
+
arguments: z8.string()
|
|
2380
2276
|
})
|
|
2381
2277
|
])
|
|
2382
2278
|
});
|
|
2383
|
-
var responseAnnotationAddedSchema =
|
|
2384
|
-
type:
|
|
2385
|
-
annotation:
|
|
2386
|
-
type:
|
|
2387
|
-
url:
|
|
2388
|
-
title:
|
|
2279
|
+
var responseAnnotationAddedSchema = z8.object({
|
|
2280
|
+
type: z8.literal("response.output_text.annotation.added"),
|
|
2281
|
+
annotation: z8.object({
|
|
2282
|
+
type: z8.literal("url_citation"),
|
|
2283
|
+
url: z8.string(),
|
|
2284
|
+
title: z8.string()
|
|
2389
2285
|
})
|
|
2390
2286
|
});
|
|
2391
|
-
var openaiResponsesChunkSchema =
|
|
2287
|
+
var openaiResponsesChunkSchema = z8.union([
|
|
2392
2288
|
textDeltaChunkSchema,
|
|
2393
2289
|
responseFinishedChunkSchema,
|
|
2394
2290
|
responseCreatedChunkSchema,
|
|
@@ -2396,7 +2292,7 @@ var openaiResponsesChunkSchema = z7.union([
|
|
|
2396
2292
|
responseFunctionCallArgumentsDeltaSchema,
|
|
2397
2293
|
responseOutputItemAddedSchema,
|
|
2398
2294
|
responseAnnotationAddedSchema,
|
|
2399
|
-
|
|
2295
|
+
z8.object({ type: z8.string() }).passthrough()
|
|
2400
2296
|
// fallback for unknown chunks
|
|
2401
2297
|
]);
|
|
2402
2298
|
function isTextDeltaChunk(chunk) {
|
|
@@ -2441,15 +2337,15 @@ function getResponsesModelConfig(modelId) {
|
|
|
2441
2337
|
requiredAutoTruncation: false
|
|
2442
2338
|
};
|
|
2443
2339
|
}
|
|
2444
|
-
var openaiResponsesProviderOptionsSchema =
|
|
2445
|
-
metadata:
|
|
2446
|
-
parallelToolCalls:
|
|
2447
|
-
previousResponseId:
|
|
2448
|
-
store:
|
|
2449
|
-
user:
|
|
2450
|
-
reasoningEffort:
|
|
2451
|
-
strictSchemas:
|
|
2452
|
-
instructions:
|
|
2340
|
+
var openaiResponsesProviderOptionsSchema = z8.object({
|
|
2341
|
+
metadata: z8.any().nullish(),
|
|
2342
|
+
parallelToolCalls: z8.boolean().nullish(),
|
|
2343
|
+
previousResponseId: z8.string().nullish(),
|
|
2344
|
+
store: z8.boolean().nullish(),
|
|
2345
|
+
user: z8.string().nullish(),
|
|
2346
|
+
reasoningEffort: z8.string().nullish(),
|
|
2347
|
+
strictSchemas: z8.boolean().nullish(),
|
|
2348
|
+
instructions: z8.string().nullish()
|
|
2453
2349
|
});
|
|
2454
2350
|
export {
|
|
2455
2351
|
OpenAIChatLanguageModel,
|
|
@@ -2458,6 +2354,7 @@ export {
|
|
|
2458
2354
|
OpenAIImageModel,
|
|
2459
2355
|
OpenAIResponsesLanguageModel,
|
|
2460
2356
|
OpenAITranscriptionModel,
|
|
2461
|
-
modelMaxImagesPerCall
|
|
2357
|
+
modelMaxImagesPerCall,
|
|
2358
|
+
openaiProviderOptions
|
|
2462
2359
|
};
|
|
2463
2360
|
//# sourceMappingURL=index.mjs.map
|