@ai-sdk/openai 4.0.51 → 4.0.52
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 +9 -0
- package/dist/index.js +101 -40
- package/dist/index.js.map +1 -1
- package/dist/internal/index.js +100 -39
- package/dist/internal/index.js.map +1 -1
- package/package.json +3 -3
- package/src/responses/convert-to-openai-responses-input.ts +102 -22
package/dist/internal/index.js
CHANGED
|
@@ -3565,26 +3565,38 @@ async function convertFunctionToolResultOutput({
|
|
|
3565
3565
|
output,
|
|
3566
3566
|
toolName,
|
|
3567
3567
|
outputSchemaToolNames,
|
|
3568
|
+
promptCacheBreakpoint,
|
|
3568
3569
|
providerOptionsName,
|
|
3569
3570
|
warnings
|
|
3570
3571
|
}) {
|
|
3571
3572
|
var _a2;
|
|
3572
3573
|
const hasOutputSchema = outputSchemaToolNames == null ? void 0 : outputSchemaToolNames.has(toolName);
|
|
3574
|
+
const convertScalarOutput = (value) => promptCacheBreakpoint == null ? value : [
|
|
3575
|
+
{
|
|
3576
|
+
type: "input_text",
|
|
3577
|
+
text: value,
|
|
3578
|
+
prompt_cache_breakpoint: promptCacheBreakpoint
|
|
3579
|
+
}
|
|
3580
|
+
];
|
|
3573
3581
|
switch (output.type) {
|
|
3574
3582
|
case "text":
|
|
3575
3583
|
case "error-text":
|
|
3576
|
-
return
|
|
3584
|
+
return convertScalarOutput(
|
|
3585
|
+
hasOutputSchema ? JSON.stringify(output.value) : output.value
|
|
3586
|
+
);
|
|
3577
3587
|
case "execution-denied": {
|
|
3578
3588
|
const reason = (_a2 = output.reason) != null ? _a2 : "Tool call execution denied.";
|
|
3579
|
-
return
|
|
3589
|
+
return convertScalarOutput(
|
|
3590
|
+
hasOutputSchema ? JSON.stringify(reason) : reason
|
|
3591
|
+
);
|
|
3580
3592
|
}
|
|
3581
3593
|
case "json":
|
|
3582
3594
|
case "error-json":
|
|
3583
|
-
return JSON.stringify(output.value);
|
|
3595
|
+
return convertScalarOutput(JSON.stringify(output.value));
|
|
3584
3596
|
case "content":
|
|
3585
3597
|
return output.value.map((item) => {
|
|
3586
3598
|
var _a3, _b, _c;
|
|
3587
|
-
const
|
|
3599
|
+
const promptCacheBreakpoint2 = getPromptCacheBreakpoint2(
|
|
3588
3600
|
item.providerOptions,
|
|
3589
3601
|
providerOptionsName
|
|
3590
3602
|
);
|
|
@@ -3593,8 +3605,8 @@ async function convertFunctionToolResultOutput({
|
|
|
3593
3605
|
return {
|
|
3594
3606
|
type: "input_text",
|
|
3595
3607
|
text: item.text,
|
|
3596
|
-
...
|
|
3597
|
-
prompt_cache_breakpoint:
|
|
3608
|
+
...promptCacheBreakpoint2 != null && {
|
|
3609
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3598
3610
|
}
|
|
3599
3611
|
};
|
|
3600
3612
|
}
|
|
@@ -3608,8 +3620,8 @@ async function convertFunctionToolResultOutput({
|
|
|
3608
3620
|
type: "input_image",
|
|
3609
3621
|
image_url: `data:${fullMediaType};base64,${convertToBase643(item.data.data)}`,
|
|
3610
3622
|
detail: imageDetail,
|
|
3611
|
-
...
|
|
3612
|
-
prompt_cache_breakpoint:
|
|
3623
|
+
...promptCacheBreakpoint2 != null && {
|
|
3624
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3613
3625
|
}
|
|
3614
3626
|
};
|
|
3615
3627
|
}
|
|
@@ -3617,8 +3629,8 @@ async function convertFunctionToolResultOutput({
|
|
|
3617
3629
|
type: "input_file",
|
|
3618
3630
|
filename: (_c = item.filename) != null ? _c : "data",
|
|
3619
3631
|
file_data: `data:${fullMediaType};base64,${convertToBase643(item.data.data)}`,
|
|
3620
|
-
...
|
|
3621
|
-
prompt_cache_breakpoint:
|
|
3632
|
+
...promptCacheBreakpoint2 != null && {
|
|
3633
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3622
3634
|
}
|
|
3623
3635
|
};
|
|
3624
3636
|
}
|
|
@@ -3628,16 +3640,16 @@ async function convertFunctionToolResultOutput({
|
|
|
3628
3640
|
type: "input_image",
|
|
3629
3641
|
image_url: item.data.url.toString(),
|
|
3630
3642
|
detail: imageDetail,
|
|
3631
|
-
...
|
|
3632
|
-
prompt_cache_breakpoint:
|
|
3643
|
+
...promptCacheBreakpoint2 != null && {
|
|
3644
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3633
3645
|
}
|
|
3634
3646
|
};
|
|
3635
3647
|
}
|
|
3636
3648
|
return {
|
|
3637
3649
|
type: "input_file",
|
|
3638
3650
|
file_url: item.data.url.toString(),
|
|
3639
|
-
...
|
|
3640
|
-
prompt_cache_breakpoint:
|
|
3651
|
+
...promptCacheBreakpoint2 != null && {
|
|
3652
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
3641
3653
|
}
|
|
3642
3654
|
};
|
|
3643
3655
|
}
|
|
@@ -3719,6 +3731,17 @@ function getPromptCacheBreakpoint2(providerOptions, providerOptionsName) {
|
|
|
3719
3731
|
var _a2;
|
|
3720
3732
|
return (_a2 = providerOptions == null ? void 0 : providerOptions[providerOptionsName]) == null ? void 0 : _a2.promptCacheBreakpoint;
|
|
3721
3733
|
}
|
|
3734
|
+
function getScalarToolResultPromptCacheBreakpoint({
|
|
3735
|
+
output,
|
|
3736
|
+
toolResultProviderOptions,
|
|
3737
|
+
providerOptionsName
|
|
3738
|
+
}) {
|
|
3739
|
+
var _a2;
|
|
3740
|
+
return output.type === "content" ? void 0 : (_a2 = getPromptCacheBreakpoint2(output.providerOptions, providerOptionsName)) != null ? _a2 : getPromptCacheBreakpoint2(
|
|
3741
|
+
toolResultProviderOptions,
|
|
3742
|
+
providerOptionsName
|
|
3743
|
+
);
|
|
3744
|
+
}
|
|
3722
3745
|
function isFileId(data, prefixes) {
|
|
3723
3746
|
if (!prefixes) return false;
|
|
3724
3747
|
return prefixes.some((prefix) => data.startsWith(prefix));
|
|
@@ -4366,24 +4389,43 @@ async function convertToOpenAIResponsesInput({
|
|
|
4366
4389
|
parallelToolResultGroup.metadata.toolCallId
|
|
4367
4390
|
);
|
|
4368
4391
|
const toolOutputs = await Promise.all(
|
|
4369
|
-
parallelToolResultGroup.results.map(
|
|
4370
|
-
|
|
4392
|
+
parallelToolResultGroup.results.map(async (result) => {
|
|
4393
|
+
const promptCacheBreakpoint = getScalarToolResultPromptCacheBreakpoint({
|
|
4371
4394
|
output: result.output,
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
|
|
4375
|
-
|
|
4376
|
-
|
|
4377
|
-
|
|
4395
|
+
toolResultProviderOptions: result.providerOptions,
|
|
4396
|
+
providerOptionsName
|
|
4397
|
+
});
|
|
4398
|
+
return {
|
|
4399
|
+
output: await convertFunctionToolResultOutput({
|
|
4400
|
+
output: result.output,
|
|
4401
|
+
toolName: result.toolName,
|
|
4402
|
+
outputSchemaToolNames,
|
|
4403
|
+
providerOptionsName,
|
|
4404
|
+
warnings
|
|
4405
|
+
}),
|
|
4406
|
+
promptCacheBreakpoint
|
|
4407
|
+
};
|
|
4408
|
+
})
|
|
4409
|
+
);
|
|
4410
|
+
const serializedToolOutputs = toolOutputs.map(
|
|
4411
|
+
({ output: output2 }) => typeof output2 === "string" ? output2 : JSON.stringify(output2)
|
|
4412
|
+
);
|
|
4413
|
+
const hasPromptCacheBreakpoint = toolOutputs.some(
|
|
4414
|
+
({ promptCacheBreakpoint }) => promptCacheBreakpoint != null
|
|
4378
4415
|
);
|
|
4379
4416
|
input.push({
|
|
4380
4417
|
type: "function_call_output",
|
|
4381
4418
|
call_id: parallelToolResultGroup.metadata.toolCallId,
|
|
4382
4419
|
// The internal wrapper returns one output containing the child
|
|
4383
4420
|
// results in the same order as the original tool_uses array.
|
|
4384
|
-
output:
|
|
4385
|
-
|
|
4386
|
-
|
|
4421
|
+
output: hasPromptCacheBreakpoint ? serializedToolOutputs.map((text, index) => ({
|
|
4422
|
+
type: "input_text",
|
|
4423
|
+
text: index === 0 ? text : `
|
|
4424
|
+
${text}`,
|
|
4425
|
+
...toolOutputs[index].promptCacheBreakpoint != null && {
|
|
4426
|
+
prompt_cache_breakpoint: toolOutputs[index].promptCacheBreakpoint
|
|
4427
|
+
}
|
|
4428
|
+
})) : serializedToolOutputs.join("\n")
|
|
4387
4429
|
});
|
|
4388
4430
|
}
|
|
4389
4431
|
continue;
|
|
@@ -4479,23 +4521,37 @@ async function convertToOpenAIResponsesInput({
|
|
|
4479
4521
|
continue;
|
|
4480
4522
|
}
|
|
4481
4523
|
if (customProviderToolNames == null ? void 0 : customProviderToolNames.has(resolvedToolName)) {
|
|
4524
|
+
const promptCacheBreakpoint = getScalarToolResultPromptCacheBreakpoint({
|
|
4525
|
+
output,
|
|
4526
|
+
toolResultProviderOptions: part.providerOptions,
|
|
4527
|
+
providerOptionsName
|
|
4528
|
+
});
|
|
4529
|
+
const convertScalarOutput = (value) => promptCacheBreakpoint == null ? value : [
|
|
4530
|
+
{
|
|
4531
|
+
type: "input_text",
|
|
4532
|
+
text: value,
|
|
4533
|
+
prompt_cache_breakpoint: promptCacheBreakpoint
|
|
4534
|
+
}
|
|
4535
|
+
];
|
|
4482
4536
|
let outputValue;
|
|
4483
4537
|
switch (output.type) {
|
|
4484
4538
|
case "text":
|
|
4485
4539
|
case "error-text":
|
|
4486
|
-
outputValue = output.value;
|
|
4540
|
+
outputValue = convertScalarOutput(output.value);
|
|
4487
4541
|
break;
|
|
4488
4542
|
case "execution-denied":
|
|
4489
|
-
outputValue = (
|
|
4543
|
+
outputValue = convertScalarOutput(
|
|
4544
|
+
(_I = output.reason) != null ? _I : "Tool call execution denied."
|
|
4545
|
+
);
|
|
4490
4546
|
break;
|
|
4491
4547
|
case "json":
|
|
4492
4548
|
case "error-json":
|
|
4493
|
-
outputValue = JSON.stringify(output.value);
|
|
4549
|
+
outputValue = convertScalarOutput(JSON.stringify(output.value));
|
|
4494
4550
|
break;
|
|
4495
4551
|
case "content":
|
|
4496
4552
|
outputValue = output.value.map((item) => {
|
|
4497
4553
|
var _a3, _b2, _c2;
|
|
4498
|
-
const
|
|
4554
|
+
const promptCacheBreakpoint2 = getPromptCacheBreakpoint2(
|
|
4499
4555
|
item.providerOptions,
|
|
4500
4556
|
providerOptionsName
|
|
4501
4557
|
);
|
|
@@ -4504,8 +4560,8 @@ async function convertToOpenAIResponsesInput({
|
|
|
4504
4560
|
return {
|
|
4505
4561
|
type: "input_text",
|
|
4506
4562
|
text: item.text,
|
|
4507
|
-
...
|
|
4508
|
-
prompt_cache_breakpoint:
|
|
4563
|
+
...promptCacheBreakpoint2 != null && {
|
|
4564
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4509
4565
|
}
|
|
4510
4566
|
};
|
|
4511
4567
|
case "file": {
|
|
@@ -4520,8 +4576,8 @@ async function convertToOpenAIResponsesInput({
|
|
|
4520
4576
|
type: "input_image",
|
|
4521
4577
|
image_url: `data:${fullMediaType};base64,${convertToBase643(item.data.data)}`,
|
|
4522
4578
|
detail: imageDetail,
|
|
4523
|
-
...
|
|
4524
|
-
prompt_cache_breakpoint:
|
|
4579
|
+
...promptCacheBreakpoint2 != null && {
|
|
4580
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4525
4581
|
}
|
|
4526
4582
|
};
|
|
4527
4583
|
}
|
|
@@ -4529,8 +4585,8 @@ async function convertToOpenAIResponsesInput({
|
|
|
4529
4585
|
type: "input_file",
|
|
4530
4586
|
filename: (_c2 = item.filename) != null ? _c2 : "data",
|
|
4531
4587
|
file_data: `data:${fullMediaType};base64,${convertToBase643(item.data.data)}`,
|
|
4532
|
-
...
|
|
4533
|
-
prompt_cache_breakpoint:
|
|
4588
|
+
...promptCacheBreakpoint2 != null && {
|
|
4589
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4534
4590
|
}
|
|
4535
4591
|
};
|
|
4536
4592
|
}
|
|
@@ -4540,16 +4596,16 @@ async function convertToOpenAIResponsesInput({
|
|
|
4540
4596
|
type: "input_image",
|
|
4541
4597
|
image_url: item.data.url.toString(),
|
|
4542
4598
|
detail: imageDetail,
|
|
4543
|
-
...
|
|
4544
|
-
prompt_cache_breakpoint:
|
|
4599
|
+
...promptCacheBreakpoint2 != null && {
|
|
4600
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4545
4601
|
}
|
|
4546
4602
|
};
|
|
4547
4603
|
}
|
|
4548
4604
|
return {
|
|
4549
4605
|
type: "input_file",
|
|
4550
4606
|
file_url: item.data.url.toString(),
|
|
4551
|
-
...
|
|
4552
|
-
prompt_cache_breakpoint:
|
|
4607
|
+
...promptCacheBreakpoint2 != null && {
|
|
4608
|
+
prompt_cache_breakpoint: promptCacheBreakpoint2
|
|
4553
4609
|
}
|
|
4554
4610
|
};
|
|
4555
4611
|
}
|
|
@@ -4582,6 +4638,11 @@ async function convertToOpenAIResponsesInput({
|
|
|
4582
4638
|
output,
|
|
4583
4639
|
toolName: part.toolName,
|
|
4584
4640
|
outputSchemaToolNames,
|
|
4641
|
+
promptCacheBreakpoint: getScalarToolResultPromptCacheBreakpoint({
|
|
4642
|
+
output,
|
|
4643
|
+
toolResultProviderOptions: part.providerOptions,
|
|
4644
|
+
providerOptionsName
|
|
4645
|
+
}),
|
|
4585
4646
|
providerOptionsName,
|
|
4586
4647
|
warnings
|
|
4587
4648
|
});
|