@ai-sdk/openai 4.0.51 → 4.0.53
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 +15 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +129 -42
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +9 -3
- package/dist/internal/index.js +126 -40
- package/dist/internal/index.js.map +1 -1
- package/package.json +3 -3
- package/src/openai-config.ts +41 -1
- package/src/openai-provider.ts +1 -0
- package/src/openai-responses-batch.ts +8 -5
- package/src/responses/convert-to-openai-responses-input.ts +102 -22
- package/src/responses/openai-responses-language-model.ts +10 -4
package/dist/internal/index.d.ts
CHANGED
|
@@ -101,13 +101,18 @@ declare class OpenAICompletionLanguageModel implements LanguageModelV4 {
|
|
|
101
101
|
doStream(options: LanguageModelV4CallOptions): Promise<LanguageModelV4StreamResult>;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
type OpenAIHeaders = Record<string, string | undefined>;
|
|
105
|
+
type SerializedOpenAIConfig = Omit<Partial<OpenAIConfig>, 'headers'> & {
|
|
106
|
+
headers?: (() => OpenAIHeaders) | OpenAIHeaders;
|
|
107
|
+
};
|
|
104
108
|
type OpenAIConfig = {
|
|
105
109
|
provider: string;
|
|
110
|
+
baseURL?: string;
|
|
106
111
|
url: (options: {
|
|
107
112
|
modelId: string;
|
|
108
113
|
path: string;
|
|
109
114
|
}) => string;
|
|
110
|
-
headers?: () =>
|
|
115
|
+
headers?: () => OpenAIHeaders;
|
|
111
116
|
fetch?: FetchFunction;
|
|
112
117
|
webSocket?: WebSocketConstructor;
|
|
113
118
|
generateId?: () => string;
|
|
@@ -121,6 +126,7 @@ type OpenAIConfig = {
|
|
|
121
126
|
*/
|
|
122
127
|
fileIdPrefixes?: readonly string[];
|
|
123
128
|
};
|
|
129
|
+
declare function prepareOpenAIConfigForWorkflowDeserialize(config: SerializedOpenAIConfig): OpenAIConfig;
|
|
124
130
|
|
|
125
131
|
type OpenAIEmbeddingModelId = 'text-embedding-3-small' | 'text-embedding-3-large' | 'text-embedding-ada-002' | (string & {});
|
|
126
132
|
declare const openaiEmbeddingModelOptions: _ai_sdk_provider_utils.LazySchema<{
|
|
@@ -1598,8 +1604,8 @@ declare class OpenAIResponsesLanguageModel implements LanguageModelV4 {
|
|
|
1598
1604
|
config: _ai_sdk_provider.JSONObject;
|
|
1599
1605
|
};
|
|
1600
1606
|
static [WORKFLOW_DESERIALIZE](options: {
|
|
1601
|
-
modelId:
|
|
1602
|
-
config:
|
|
1607
|
+
modelId: string;
|
|
1608
|
+
config: Parameters<typeof prepareOpenAIConfigForWorkflowDeserialize>[0];
|
|
1603
1609
|
}): OpenAIResponsesLanguageModel;
|
|
1604
1610
|
constructor(modelId: OpenAIResponsesModelId, config: OpenAIConfig);
|
|
1605
1611
|
readonly supportedUrls: Record<string, RegExp[]>;
|
package/dist/internal/index.js
CHANGED
|
@@ -3072,6 +3072,28 @@ import {
|
|
|
3072
3072
|
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE7
|
|
3073
3073
|
} from "@ai-sdk/provider-utils";
|
|
3074
3074
|
|
|
3075
|
+
// src/openai-config.ts
|
|
3076
|
+
function prepareOpenAIConfigForWorkflowDeserialize(config) {
|
|
3077
|
+
if (config.provider == null) {
|
|
3078
|
+
throw new Error(
|
|
3079
|
+
"OpenAI model is missing provider after workflow deserialization."
|
|
3080
|
+
);
|
|
3081
|
+
}
|
|
3082
|
+
return {
|
|
3083
|
+
...config,
|
|
3084
|
+
provider: config.provider,
|
|
3085
|
+
url: typeof config.url === "function" ? config.url : ({ path }) => {
|
|
3086
|
+
if (config.baseURL == null) {
|
|
3087
|
+
throw new Error(
|
|
3088
|
+
"OpenAI model is missing baseURL after workflow deserialization."
|
|
3089
|
+
);
|
|
3090
|
+
}
|
|
3091
|
+
return `${config.baseURL}${path}`;
|
|
3092
|
+
},
|
|
3093
|
+
headers: typeof config.headers === "function" ? config.headers : config.headers == null ? void 0 : () => config.headers
|
|
3094
|
+
};
|
|
3095
|
+
}
|
|
3096
|
+
|
|
3075
3097
|
// src/responses/convert-openai-responses-usage.ts
|
|
3076
3098
|
import { createNullLanguageModelUsage as createNullLanguageModelUsage3 } from "@ai-sdk/provider-utils";
|
|
3077
3099
|
function convertOpenAIResponsesUsage(usage) {
|
|
@@ -3565,26 +3587,38 @@ async function convertFunctionToolResultOutput({
|
|
|
3565
3587
|
output,
|
|
3566
3588
|
toolName,
|
|
3567
3589
|
outputSchemaToolNames,
|
|
3590
|
+
promptCacheBreakpoint,
|
|
3568
3591
|
providerOptionsName,
|
|
3569
3592
|
warnings
|
|
3570
3593
|
}) {
|
|
3571
3594
|
var _a2;
|
|
3572
3595
|
const hasOutputSchema = outputSchemaToolNames == null ? void 0 : outputSchemaToolNames.has(toolName);
|
|
3596
|
+
const convertScalarOutput = (value) => promptCacheBreakpoint == null ? value : [
|
|
3597
|
+
{
|
|
3598
|
+
type: "input_text",
|
|
3599
|
+
text: value,
|
|
3600
|
+
prompt_cache_breakpoint: promptCacheBreakpoint
|
|
3601
|
+
}
|
|
3602
|
+
];
|
|
3573
3603
|
switch (output.type) {
|
|
3574
3604
|
case "text":
|
|
3575
3605
|
case "error-text":
|
|
3576
|
-
return
|
|
3606
|
+
return convertScalarOutput(
|
|
3607
|
+
hasOutputSchema ? JSON.stringify(output.value) : output.value
|
|
3608
|
+
);
|
|
3577
3609
|
case "execution-denied": {
|
|
3578
3610
|
const reason = (_a2 = output.reason) != null ? _a2 : "Tool call execution denied.";
|
|
3579
|
-
return
|
|
3611
|
+
return convertScalarOutput(
|
|
3612
|
+
hasOutputSchema ? JSON.stringify(reason) : reason
|
|
3613
|
+
);
|
|
3580
3614
|
}
|
|
3581
3615
|
case "json":
|
|
3582
3616
|
case "error-json":
|
|
3583
|
-
return JSON.stringify(output.value);
|
|
3617
|
+
return convertScalarOutput(JSON.stringify(output.value));
|
|
3584
3618
|
case "content":
|
|
3585
3619
|
return output.value.map((item) => {
|
|
3586
3620
|
var _a3, _b, _c;
|
|
3587
|
-
const
|
|
3621
|
+
const promptCacheBreakpoint2 = getPromptCacheBreakpoint2(
|
|
3588
3622
|
item.providerOptions,
|
|
3589
3623
|
providerOptionsName
|
|
3590
3624
|
);
|
|
@@ -3593,8 +3627,8 @@ async function convertFunctionToolResultOutput({
|
|
|
3593
3627
|
return {
|
|
3594
3628
|
type: "input_text",
|
|
3595
3629
|
text: item.text,
|
|
3596
|
-
...
|
|
3597
|
-
prompt_cache_breakpoint:
|
|
3630
|
+
...promptCacheBreakpoint2 != null && {
|
|
3631
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3598
3632
|
}
|
|
3599
3633
|
};
|
|
3600
3634
|
}
|
|
@@ -3608,8 +3642,8 @@ async function convertFunctionToolResultOutput({
|
|
|
3608
3642
|
type: "input_image",
|
|
3609
3643
|
image_url: `data:${fullMediaType};base64,${convertToBase643(item.data.data)}`,
|
|
3610
3644
|
detail: imageDetail,
|
|
3611
|
-
...
|
|
3612
|
-
prompt_cache_breakpoint:
|
|
3645
|
+
...promptCacheBreakpoint2 != null && {
|
|
3646
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3613
3647
|
}
|
|
3614
3648
|
};
|
|
3615
3649
|
}
|
|
@@ -3617,8 +3651,8 @@ async function convertFunctionToolResultOutput({
|
|
|
3617
3651
|
type: "input_file",
|
|
3618
3652
|
filename: (_c = item.filename) != null ? _c : "data",
|
|
3619
3653
|
file_data: `data:${fullMediaType};base64,${convertToBase643(item.data.data)}`,
|
|
3620
|
-
...
|
|
3621
|
-
prompt_cache_breakpoint:
|
|
3654
|
+
...promptCacheBreakpoint2 != null && {
|
|
3655
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3622
3656
|
}
|
|
3623
3657
|
};
|
|
3624
3658
|
}
|
|
@@ -3628,16 +3662,16 @@ async function convertFunctionToolResultOutput({
|
|
|
3628
3662
|
type: "input_image",
|
|
3629
3663
|
image_url: item.data.url.toString(),
|
|
3630
3664
|
detail: imageDetail,
|
|
3631
|
-
...
|
|
3632
|
-
prompt_cache_breakpoint:
|
|
3665
|
+
...promptCacheBreakpoint2 != null && {
|
|
3666
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3633
3667
|
}
|
|
3634
3668
|
};
|
|
3635
3669
|
}
|
|
3636
3670
|
return {
|
|
3637
3671
|
type: "input_file",
|
|
3638
3672
|
file_url: item.data.url.toString(),
|
|
3639
|
-
...
|
|
3640
|
-
prompt_cache_breakpoint:
|
|
3673
|
+
...promptCacheBreakpoint2 != null && {
|
|
3674
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3641
3675
|
}
|
|
3642
3676
|
};
|
|
3643
3677
|
}
|
|
@@ -3719,6 +3753,17 @@ function getPromptCacheBreakpoint2(providerOptions, providerOptionsName) {
|
|
|
3719
3753
|
var _a2;
|
|
3720
3754
|
return (_a2 = providerOptions == null ? void 0 : providerOptions[providerOptionsName]) == null ? void 0 : _a2.promptCacheBreakpoint;
|
|
3721
3755
|
}
|
|
3756
|
+
function getScalarToolResultPromptCacheBreakpoint({
|
|
3757
|
+
output,
|
|
3758
|
+
toolResultProviderOptions,
|
|
3759
|
+
providerOptionsName
|
|
3760
|
+
}) {
|
|
3761
|
+
var _a2;
|
|
3762
|
+
return output.type === "content" ? void 0 : (_a2 = getPromptCacheBreakpoint2(output.providerOptions, providerOptionsName)) != null ? _a2 : getPromptCacheBreakpoint2(
|
|
3763
|
+
toolResultProviderOptions,
|
|
3764
|
+
providerOptionsName
|
|
3765
|
+
);
|
|
3766
|
+
}
|
|
3722
3767
|
function isFileId(data, prefixes) {
|
|
3723
3768
|
if (!prefixes) return false;
|
|
3724
3769
|
return prefixes.some((prefix) => data.startsWith(prefix));
|
|
@@ -4366,24 +4411,43 @@ async function convertToOpenAIResponsesInput({
|
|
|
4366
4411
|
parallelToolResultGroup.metadata.toolCallId
|
|
4367
4412
|
);
|
|
4368
4413
|
const toolOutputs = await Promise.all(
|
|
4369
|
-
parallelToolResultGroup.results.map(
|
|
4370
|
-
|
|
4414
|
+
parallelToolResultGroup.results.map(async (result) => {
|
|
4415
|
+
const promptCacheBreakpoint = getScalarToolResultPromptCacheBreakpoint({
|
|
4371
4416
|
output: result.output,
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
|
|
4375
|
-
|
|
4376
|
-
|
|
4377
|
-
|
|
4417
|
+
toolResultProviderOptions: result.providerOptions,
|
|
4418
|
+
providerOptionsName
|
|
4419
|
+
});
|
|
4420
|
+
return {
|
|
4421
|
+
output: await convertFunctionToolResultOutput({
|
|
4422
|
+
output: result.output,
|
|
4423
|
+
toolName: result.toolName,
|
|
4424
|
+
outputSchemaToolNames,
|
|
4425
|
+
providerOptionsName,
|
|
4426
|
+
warnings
|
|
4427
|
+
}),
|
|
4428
|
+
promptCacheBreakpoint
|
|
4429
|
+
};
|
|
4430
|
+
})
|
|
4431
|
+
);
|
|
4432
|
+
const serializedToolOutputs = toolOutputs.map(
|
|
4433
|
+
({ output: output2 }) => typeof output2 === "string" ? output2 : JSON.stringify(output2)
|
|
4434
|
+
);
|
|
4435
|
+
const hasPromptCacheBreakpoint = toolOutputs.some(
|
|
4436
|
+
({ promptCacheBreakpoint }) => promptCacheBreakpoint != null
|
|
4378
4437
|
);
|
|
4379
4438
|
input.push({
|
|
4380
4439
|
type: "function_call_output",
|
|
4381
4440
|
call_id: parallelToolResultGroup.metadata.toolCallId,
|
|
4382
4441
|
// The internal wrapper returns one output containing the child
|
|
4383
4442
|
// results in the same order as the original tool_uses array.
|
|
4384
|
-
output:
|
|
4385
|
-
|
|
4386
|
-
|
|
4443
|
+
output: hasPromptCacheBreakpoint ? serializedToolOutputs.map((text, index) => ({
|
|
4444
|
+
type: "input_text",
|
|
4445
|
+
text: index === 0 ? text : `
|
|
4446
|
+
${text}`,
|
|
4447
|
+
...toolOutputs[index].promptCacheBreakpoint != null && {
|
|
4448
|
+
prompt_cache_breakpoint: toolOutputs[index].promptCacheBreakpoint
|
|
4449
|
+
}
|
|
4450
|
+
})) : serializedToolOutputs.join("\n")
|
|
4387
4451
|
});
|
|
4388
4452
|
}
|
|
4389
4453
|
continue;
|
|
@@ -4479,23 +4543,37 @@ async function convertToOpenAIResponsesInput({
|
|
|
4479
4543
|
continue;
|
|
4480
4544
|
}
|
|
4481
4545
|
if (customProviderToolNames == null ? void 0 : customProviderToolNames.has(resolvedToolName)) {
|
|
4546
|
+
const promptCacheBreakpoint = getScalarToolResultPromptCacheBreakpoint({
|
|
4547
|
+
output,
|
|
4548
|
+
toolResultProviderOptions: part.providerOptions,
|
|
4549
|
+
providerOptionsName
|
|
4550
|
+
});
|
|
4551
|
+
const convertScalarOutput = (value) => promptCacheBreakpoint == null ? value : [
|
|
4552
|
+
{
|
|
4553
|
+
type: "input_text",
|
|
4554
|
+
text: value,
|
|
4555
|
+
prompt_cache_breakpoint: promptCacheBreakpoint
|
|
4556
|
+
}
|
|
4557
|
+
];
|
|
4482
4558
|
let outputValue;
|
|
4483
4559
|
switch (output.type) {
|
|
4484
4560
|
case "text":
|
|
4485
4561
|
case "error-text":
|
|
4486
|
-
outputValue = output.value;
|
|
4562
|
+
outputValue = convertScalarOutput(output.value);
|
|
4487
4563
|
break;
|
|
4488
4564
|
case "execution-denied":
|
|
4489
|
-
outputValue = (
|
|
4565
|
+
outputValue = convertScalarOutput(
|
|
4566
|
+
(_I = output.reason) != null ? _I : "Tool call execution denied."
|
|
4567
|
+
);
|
|
4490
4568
|
break;
|
|
4491
4569
|
case "json":
|
|
4492
4570
|
case "error-json":
|
|
4493
|
-
outputValue = JSON.stringify(output.value);
|
|
4571
|
+
outputValue = convertScalarOutput(JSON.stringify(output.value));
|
|
4494
4572
|
break;
|
|
4495
4573
|
case "content":
|
|
4496
4574
|
outputValue = output.value.map((item) => {
|
|
4497
4575
|
var _a3, _b2, _c2;
|
|
4498
|
-
const
|
|
4576
|
+
const promptCacheBreakpoint2 = getPromptCacheBreakpoint2(
|
|
4499
4577
|
item.providerOptions,
|
|
4500
4578
|
providerOptionsName
|
|
4501
4579
|
);
|
|
@@ -4504,8 +4582,8 @@ async function convertToOpenAIResponsesInput({
|
|
|
4504
4582
|
return {
|
|
4505
4583
|
type: "input_text",
|
|
4506
4584
|
text: item.text,
|
|
4507
|
-
...
|
|
4508
|
-
prompt_cache_breakpoint:
|
|
4585
|
+
...promptCacheBreakpoint2 != null && {
|
|
4586
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4509
4587
|
}
|
|
4510
4588
|
};
|
|
4511
4589
|
case "file": {
|
|
@@ -4520,8 +4598,8 @@ async function convertToOpenAIResponsesInput({
|
|
|
4520
4598
|
type: "input_image",
|
|
4521
4599
|
image_url: `data:${fullMediaType};base64,${convertToBase643(item.data.data)}`,
|
|
4522
4600
|
detail: imageDetail,
|
|
4523
|
-
...
|
|
4524
|
-
prompt_cache_breakpoint:
|
|
4601
|
+
...promptCacheBreakpoint2 != null && {
|
|
4602
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4525
4603
|
}
|
|
4526
4604
|
};
|
|
4527
4605
|
}
|
|
@@ -4529,8 +4607,8 @@ async function convertToOpenAIResponsesInput({
|
|
|
4529
4607
|
type: "input_file",
|
|
4530
4608
|
filename: (_c2 = item.filename) != null ? _c2 : "data",
|
|
4531
4609
|
file_data: `data:${fullMediaType};base64,${convertToBase643(item.data.data)}`,
|
|
4532
|
-
...
|
|
4533
|
-
prompt_cache_breakpoint:
|
|
4610
|
+
...promptCacheBreakpoint2 != null && {
|
|
4611
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4534
4612
|
}
|
|
4535
4613
|
};
|
|
4536
4614
|
}
|
|
@@ -4540,16 +4618,16 @@ async function convertToOpenAIResponsesInput({
|
|
|
4540
4618
|
type: "input_image",
|
|
4541
4619
|
image_url: item.data.url.toString(),
|
|
4542
4620
|
detail: imageDetail,
|
|
4543
|
-
...
|
|
4544
|
-
prompt_cache_breakpoint:
|
|
4621
|
+
...promptCacheBreakpoint2 != null && {
|
|
4622
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4545
4623
|
}
|
|
4546
4624
|
};
|
|
4547
4625
|
}
|
|
4548
4626
|
return {
|
|
4549
4627
|
type: "input_file",
|
|
4550
4628
|
file_url: item.data.url.toString(),
|
|
4551
|
-
...
|
|
4552
|
-
prompt_cache_breakpoint:
|
|
4629
|
+
...promptCacheBreakpoint2 != null && {
|
|
4630
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4553
4631
|
}
|
|
4554
4632
|
};
|
|
4555
4633
|
}
|
|
@@ -4582,6 +4660,11 @@ async function convertToOpenAIResponsesInput({
|
|
|
4582
4660
|
output,
|
|
4583
4661
|
toolName: part.toolName,
|
|
4584
4662
|
outputSchemaToolNames,
|
|
4663
|
+
promptCacheBreakpoint: getScalarToolResultPromptCacheBreakpoint({
|
|
4664
|
+
output,
|
|
4665
|
+
toolResultProviderOptions: part.providerOptions,
|
|
4666
|
+
providerOptionsName
|
|
4667
|
+
}),
|
|
4585
4668
|
providerOptionsName,
|
|
4586
4669
|
warnings
|
|
4587
4670
|
});
|
|
@@ -6923,7 +7006,10 @@ var OpenAIResponsesLanguageModel = class _OpenAIResponsesLanguageModel {
|
|
|
6923
7006
|
});
|
|
6924
7007
|
}
|
|
6925
7008
|
static [WORKFLOW_DESERIALIZE7](options) {
|
|
6926
|
-
return new _OpenAIResponsesLanguageModel(
|
|
7009
|
+
return new _OpenAIResponsesLanguageModel(
|
|
7010
|
+
options.modelId,
|
|
7011
|
+
prepareOpenAIConfigForWorkflowDeserialize(options.config)
|
|
7012
|
+
);
|
|
6927
7013
|
}
|
|
6928
7014
|
get provider() {
|
|
6929
7015
|
return this.config.provider;
|