@ai-sdk/openai 3.0.96 → 3.0.98
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 +27 -0
- package/dist/index.js +138 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +138 -11
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +137 -10
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +137 -10
- package/dist/internal/index.mjs.map +1 -1
- package/docs/03-openai.mdx +57 -0
- package/package.json +2 -2
- package/src/responses/openai-responses-api.ts +15 -0
- package/src/responses/openai-responses-prepare-tools.ts +186 -5
package/dist/internal/index.js
CHANGED
|
@@ -5158,7 +5158,7 @@ async function prepareResponsesTools({
|
|
|
5158
5158
|
toolNameMapping,
|
|
5159
5159
|
customProviderToolNames
|
|
5160
5160
|
}) {
|
|
5161
|
-
var _a, _b, _c;
|
|
5161
|
+
var _a, _b, _c, _d;
|
|
5162
5162
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
5163
5163
|
const toolWarnings = [];
|
|
5164
5164
|
if (tools == null) {
|
|
@@ -5167,6 +5167,20 @@ async function prepareResponsesTools({
|
|
|
5167
5167
|
const openaiTools = [];
|
|
5168
5168
|
const namespaceTools = /* @__PURE__ */ new Map();
|
|
5169
5169
|
const resolvedCustomProviderToolNames = customProviderToolNames != null ? customProviderToolNames : /* @__PURE__ */ new Set();
|
|
5170
|
+
const allowedToolResolutions = /* @__PURE__ */ new Map();
|
|
5171
|
+
const allowedToolAliases = /* @__PURE__ */ new Map();
|
|
5172
|
+
const recordAllowedTool = (toolName, resolution, canonicalName) => {
|
|
5173
|
+
allowedToolResolutions.set(toolName, resolution);
|
|
5174
|
+
if (canonicalName == null || canonicalName === toolName) {
|
|
5175
|
+
return;
|
|
5176
|
+
}
|
|
5177
|
+
const existingAlias = allowedToolAliases.get(canonicalName);
|
|
5178
|
+
if (existingAlias == null) {
|
|
5179
|
+
allowedToolAliases.set(canonicalName, resolution);
|
|
5180
|
+
} else if (existingAlias !== "ambiguous" && !isSameAllowedTool(existingAlias, resolution)) {
|
|
5181
|
+
allowedToolAliases.set(canonicalName, "ambiguous");
|
|
5182
|
+
}
|
|
5183
|
+
};
|
|
5170
5184
|
for (const tool of tools) {
|
|
5171
5185
|
switch (tool.type) {
|
|
5172
5186
|
case "function": {
|
|
@@ -5196,9 +5210,24 @@ async function prepareResponsesTools({
|
|
|
5196
5210
|
}
|
|
5197
5211
|
namespaceTool.tools.push(openaiFunctionTool);
|
|
5198
5212
|
}
|
|
5213
|
+
recordAllowedTool(
|
|
5214
|
+
tool.name,
|
|
5215
|
+
namespace != null ? {
|
|
5216
|
+
supported: false,
|
|
5217
|
+
reason: "tools inside an OpenAI tool namespace are not visible to tool_choice.allowed_tools"
|
|
5218
|
+
} : (openaiOptions == null ? void 0 : openaiOptions.deferLoading) === true ? {
|
|
5219
|
+
supported: false,
|
|
5220
|
+
reason: "deferred tools are not visible to tool_choice.allowed_tools"
|
|
5221
|
+
} : {
|
|
5222
|
+
supported: true,
|
|
5223
|
+
entry: { type: "function", name: tool.name }
|
|
5224
|
+
},
|
|
5225
|
+
void 0
|
|
5226
|
+
);
|
|
5199
5227
|
break;
|
|
5200
5228
|
}
|
|
5201
5229
|
case "provider": {
|
|
5230
|
+
const openaiToolCountBefore = openaiTools.length;
|
|
5202
5231
|
switch (tool.id) {
|
|
5203
5232
|
case "openai.file_search": {
|
|
5204
5233
|
const args = await (0, import_provider_utils34.validateTypes)({
|
|
@@ -5359,6 +5388,14 @@ async function prepareResponsesTools({
|
|
|
5359
5388
|
break;
|
|
5360
5389
|
}
|
|
5361
5390
|
}
|
|
5391
|
+
if (openaiTools.length > openaiToolCountBefore) {
|
|
5392
|
+
const openaiTool = openaiTools[openaiToolCountBefore];
|
|
5393
|
+
recordAllowedTool(
|
|
5394
|
+
tool.name,
|
|
5395
|
+
toAllowedToolResolution(openaiTool),
|
|
5396
|
+
toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(tool.name)
|
|
5397
|
+
);
|
|
5398
|
+
}
|
|
5362
5399
|
break;
|
|
5363
5400
|
}
|
|
5364
5401
|
default:
|
|
@@ -5370,18 +5407,63 @@ async function prepareResponsesTools({
|
|
|
5370
5407
|
}
|
|
5371
5408
|
}
|
|
5372
5409
|
if (allowedTools != null) {
|
|
5410
|
+
const allowedToolEntries = [];
|
|
5411
|
+
const droppedToolNames = [];
|
|
5412
|
+
for (const name of allowedTools.toolNames) {
|
|
5413
|
+
const directResolution = allowedToolResolutions.get(name);
|
|
5414
|
+
const resolution = directResolution != null ? directResolution : allowedToolAliases.get(name);
|
|
5415
|
+
if (directResolution != null && allowedToolAliases.has(name)) {
|
|
5416
|
+
toolWarnings.push({
|
|
5417
|
+
type: "unsupported",
|
|
5418
|
+
feature: `allowedTools entry "${name}"`,
|
|
5419
|
+
details: "this name is both a tool name and the provider tool name of another tool in this request; the tool with this name is allowed"
|
|
5420
|
+
});
|
|
5421
|
+
}
|
|
5422
|
+
if (resolution === "ambiguous") {
|
|
5423
|
+
toolWarnings.push({
|
|
5424
|
+
type: "unsupported",
|
|
5425
|
+
feature: `allowedTools entry "${name}"`,
|
|
5426
|
+
details: "several tools in this request share this provider tool name; use the tool name from the tools for this request instead"
|
|
5427
|
+
});
|
|
5428
|
+
droppedToolNames.push(name);
|
|
5429
|
+
continue;
|
|
5430
|
+
}
|
|
5431
|
+
if (resolution == null) {
|
|
5432
|
+
toolWarnings.push({
|
|
5433
|
+
type: "unsupported",
|
|
5434
|
+
feature: `allowedTools entry "${name}"`,
|
|
5435
|
+
details: "the tool is not part of the tools for this request and is sent as a function tool"
|
|
5436
|
+
});
|
|
5437
|
+
allowedToolEntries.push({
|
|
5438
|
+
type: "function",
|
|
5439
|
+
name: (_b = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(name)) != null ? _b : name
|
|
5440
|
+
});
|
|
5441
|
+
continue;
|
|
5442
|
+
}
|
|
5443
|
+
if (!resolution.supported) {
|
|
5444
|
+
toolWarnings.push({
|
|
5445
|
+
type: "unsupported",
|
|
5446
|
+
feature: `allowedTools entry "${name}"`,
|
|
5447
|
+
details: `${resolution.reason}; the tool is removed from the allowed tools`
|
|
5448
|
+
});
|
|
5449
|
+
droppedToolNames.push(name);
|
|
5450
|
+
continue;
|
|
5451
|
+
}
|
|
5452
|
+
allowedToolEntries.push(resolution.entry);
|
|
5453
|
+
}
|
|
5454
|
+
if (allowedToolEntries.length === 0) {
|
|
5455
|
+
throw new import_provider8.UnsupportedFunctionalityError({
|
|
5456
|
+
functionality: `allowedTools with only tools that cannot be allow-listed (${droppedToolNames.join(
|
|
5457
|
+
", "
|
|
5458
|
+
)})`
|
|
5459
|
+
});
|
|
5460
|
+
}
|
|
5373
5461
|
return {
|
|
5374
5462
|
tools: openaiTools,
|
|
5375
5463
|
toolChoice: {
|
|
5376
5464
|
type: "allowed_tools",
|
|
5377
|
-
mode: (
|
|
5378
|
-
tools:
|
|
5379
|
-
var _a2;
|
|
5380
|
-
return {
|
|
5381
|
-
type: "function",
|
|
5382
|
-
name: (_a2 = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(name)) != null ? _a2 : name
|
|
5383
|
-
};
|
|
5384
|
-
})
|
|
5465
|
+
mode: (_c = allowedTools.mode) != null ? _c : "auto",
|
|
5466
|
+
tools: allowedToolEntries
|
|
5385
5467
|
},
|
|
5386
5468
|
toolWarnings
|
|
5387
5469
|
};
|
|
@@ -5396,7 +5478,7 @@ async function prepareResponsesTools({
|
|
|
5396
5478
|
case "required":
|
|
5397
5479
|
return { tools: openaiTools, toolChoice: type, toolWarnings };
|
|
5398
5480
|
case "tool": {
|
|
5399
|
-
const resolvedToolName = (
|
|
5481
|
+
const resolvedToolName = (_d = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(toolChoice.toolName)) != null ? _d : toolChoice.toolName;
|
|
5400
5482
|
return {
|
|
5401
5483
|
tools: openaiTools,
|
|
5402
5484
|
toolChoice: resolvedToolName === "code_interpreter" || resolvedToolName === "file_search" || resolvedToolName === "image_generation" || resolvedToolName === "web_search_preview" || resolvedToolName === "web_search" || resolvedToolName === "mcp" || resolvedToolName === "apply_patch" ? { type: resolvedToolName } : resolvedCustomProviderToolNames.has(resolvedToolName) ? { type: "custom", name: resolvedToolName } : { type: "function", name: resolvedToolName },
|
|
@@ -5411,6 +5493,51 @@ async function prepareResponsesTools({
|
|
|
5411
5493
|
}
|
|
5412
5494
|
}
|
|
5413
5495
|
}
|
|
5496
|
+
function allowedToolKey(entry) {
|
|
5497
|
+
switch (entry.type) {
|
|
5498
|
+
case "mcp":
|
|
5499
|
+
return `mcp:${entry.server_label}`;
|
|
5500
|
+
case "function":
|
|
5501
|
+
case "custom":
|
|
5502
|
+
return `${entry.type}:${entry.name}`;
|
|
5503
|
+
default:
|
|
5504
|
+
return entry.type;
|
|
5505
|
+
}
|
|
5506
|
+
}
|
|
5507
|
+
function isSameAllowedTool(a, b) {
|
|
5508
|
+
if (a.supported && b.supported) {
|
|
5509
|
+
return allowedToolKey(a.entry) === allowedToolKey(b.entry);
|
|
5510
|
+
}
|
|
5511
|
+
if (!a.supported && !b.supported) {
|
|
5512
|
+
return a.reason === b.reason;
|
|
5513
|
+
}
|
|
5514
|
+
return false;
|
|
5515
|
+
}
|
|
5516
|
+
function toAllowedToolResolution(tool) {
|
|
5517
|
+
switch (tool.type) {
|
|
5518
|
+
case "custom":
|
|
5519
|
+
return { supported: true, entry: { type: "custom", name: tool.name } };
|
|
5520
|
+
case "mcp":
|
|
5521
|
+
return {
|
|
5522
|
+
supported: true,
|
|
5523
|
+
entry: { type: "mcp", server_label: tool.server_label }
|
|
5524
|
+
};
|
|
5525
|
+
case "file_search":
|
|
5526
|
+
case "web_search":
|
|
5527
|
+
case "web_search_preview":
|
|
5528
|
+
case "image_generation":
|
|
5529
|
+
case "code_interpreter":
|
|
5530
|
+
case "apply_patch":
|
|
5531
|
+
case "shell":
|
|
5532
|
+
case "local_shell":
|
|
5533
|
+
return { supported: true, entry: { type: tool.type } };
|
|
5534
|
+
default:
|
|
5535
|
+
return {
|
|
5536
|
+
supported: false,
|
|
5537
|
+
reason: `OpenAI does not support ${tool.type} tools in tool_choice.allowed_tools`
|
|
5538
|
+
};
|
|
5539
|
+
}
|
|
5540
|
+
}
|
|
5414
5541
|
function prepareFunctionTool({
|
|
5415
5542
|
tool,
|
|
5416
5543
|
options
|