@ai-sdk/openai 3.0.97 → 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/dist/index.mjs CHANGED
@@ -4991,7 +4991,7 @@ async function prepareResponsesTools({
4991
4991
  toolNameMapping,
4992
4992
  customProviderToolNames
4993
4993
  }) {
4994
- var _a, _b, _c;
4994
+ var _a, _b, _c, _d;
4995
4995
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
4996
4996
  const toolWarnings = [];
4997
4997
  if (tools == null) {
@@ -5000,6 +5000,20 @@ async function prepareResponsesTools({
5000
5000
  const openaiTools2 = [];
5001
5001
  const namespaceTools = /* @__PURE__ */ new Map();
5002
5002
  const resolvedCustomProviderToolNames = customProviderToolNames != null ? customProviderToolNames : /* @__PURE__ */ new Set();
5003
+ const allowedToolResolutions = /* @__PURE__ */ new Map();
5004
+ const allowedToolAliases = /* @__PURE__ */ new Map();
5005
+ const recordAllowedTool = (toolName, resolution, canonicalName) => {
5006
+ allowedToolResolutions.set(toolName, resolution);
5007
+ if (canonicalName == null || canonicalName === toolName) {
5008
+ return;
5009
+ }
5010
+ const existingAlias = allowedToolAliases.get(canonicalName);
5011
+ if (existingAlias == null) {
5012
+ allowedToolAliases.set(canonicalName, resolution);
5013
+ } else if (existingAlias !== "ambiguous" && !isSameAllowedTool(existingAlias, resolution)) {
5014
+ allowedToolAliases.set(canonicalName, "ambiguous");
5015
+ }
5016
+ };
5003
5017
  for (const tool of tools) {
5004
5018
  switch (tool.type) {
5005
5019
  case "function": {
@@ -5029,9 +5043,24 @@ async function prepareResponsesTools({
5029
5043
  }
5030
5044
  namespaceTool.tools.push(openaiFunctionTool);
5031
5045
  }
5046
+ recordAllowedTool(
5047
+ tool.name,
5048
+ namespace != null ? {
5049
+ supported: false,
5050
+ reason: "tools inside an OpenAI tool namespace are not visible to tool_choice.allowed_tools"
5051
+ } : (openaiOptions == null ? void 0 : openaiOptions.deferLoading) === true ? {
5052
+ supported: false,
5053
+ reason: "deferred tools are not visible to tool_choice.allowed_tools"
5054
+ } : {
5055
+ supported: true,
5056
+ entry: { type: "function", name: tool.name }
5057
+ },
5058
+ void 0
5059
+ );
5032
5060
  break;
5033
5061
  }
5034
5062
  case "provider": {
5063
+ const openaiToolCountBefore = openaiTools2.length;
5035
5064
  switch (tool.id) {
5036
5065
  case "openai.file_search": {
5037
5066
  const args = await validateTypes2({
@@ -5192,6 +5221,14 @@ async function prepareResponsesTools({
5192
5221
  break;
5193
5222
  }
5194
5223
  }
5224
+ if (openaiTools2.length > openaiToolCountBefore) {
5225
+ const openaiTool = openaiTools2[openaiToolCountBefore];
5226
+ recordAllowedTool(
5227
+ tool.name,
5228
+ toAllowedToolResolution(openaiTool),
5229
+ toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(tool.name)
5230
+ );
5231
+ }
5195
5232
  break;
5196
5233
  }
5197
5234
  default:
@@ -5203,18 +5240,63 @@ async function prepareResponsesTools({
5203
5240
  }
5204
5241
  }
5205
5242
  if (allowedTools != null) {
5243
+ const allowedToolEntries = [];
5244
+ const droppedToolNames = [];
5245
+ for (const name of allowedTools.toolNames) {
5246
+ const directResolution = allowedToolResolutions.get(name);
5247
+ const resolution = directResolution != null ? directResolution : allowedToolAliases.get(name);
5248
+ if (directResolution != null && allowedToolAliases.has(name)) {
5249
+ toolWarnings.push({
5250
+ type: "unsupported",
5251
+ feature: `allowedTools entry "${name}"`,
5252
+ 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"
5253
+ });
5254
+ }
5255
+ if (resolution === "ambiguous") {
5256
+ toolWarnings.push({
5257
+ type: "unsupported",
5258
+ feature: `allowedTools entry "${name}"`,
5259
+ details: "several tools in this request share this provider tool name; use the tool name from the tools for this request instead"
5260
+ });
5261
+ droppedToolNames.push(name);
5262
+ continue;
5263
+ }
5264
+ if (resolution == null) {
5265
+ toolWarnings.push({
5266
+ type: "unsupported",
5267
+ feature: `allowedTools entry "${name}"`,
5268
+ details: "the tool is not part of the tools for this request and is sent as a function tool"
5269
+ });
5270
+ allowedToolEntries.push({
5271
+ type: "function",
5272
+ name: (_b = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(name)) != null ? _b : name
5273
+ });
5274
+ continue;
5275
+ }
5276
+ if (!resolution.supported) {
5277
+ toolWarnings.push({
5278
+ type: "unsupported",
5279
+ feature: `allowedTools entry "${name}"`,
5280
+ details: `${resolution.reason}; the tool is removed from the allowed tools`
5281
+ });
5282
+ droppedToolNames.push(name);
5283
+ continue;
5284
+ }
5285
+ allowedToolEntries.push(resolution.entry);
5286
+ }
5287
+ if (allowedToolEntries.length === 0) {
5288
+ throw new UnsupportedFunctionalityError5({
5289
+ functionality: `allowedTools with only tools that cannot be allow-listed (${droppedToolNames.join(
5290
+ ", "
5291
+ )})`
5292
+ });
5293
+ }
5206
5294
  return {
5207
5295
  tools: openaiTools2,
5208
5296
  toolChoice: {
5209
5297
  type: "allowed_tools",
5210
- mode: (_b = allowedTools.mode) != null ? _b : "auto",
5211
- tools: allowedTools.toolNames.map((name) => {
5212
- var _a2;
5213
- return {
5214
- type: "function",
5215
- name: (_a2 = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(name)) != null ? _a2 : name
5216
- };
5217
- })
5298
+ mode: (_c = allowedTools.mode) != null ? _c : "auto",
5299
+ tools: allowedToolEntries
5218
5300
  },
5219
5301
  toolWarnings
5220
5302
  };
@@ -5229,7 +5311,7 @@ async function prepareResponsesTools({
5229
5311
  case "required":
5230
5312
  return { tools: openaiTools2, toolChoice: type, toolWarnings };
5231
5313
  case "tool": {
5232
- const resolvedToolName = (_c = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(toolChoice.toolName)) != null ? _c : toolChoice.toolName;
5314
+ const resolvedToolName = (_d = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(toolChoice.toolName)) != null ? _d : toolChoice.toolName;
5233
5315
  return {
5234
5316
  tools: openaiTools2,
5235
5317
  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 },
@@ -5244,6 +5326,51 @@ async function prepareResponsesTools({
5244
5326
  }
5245
5327
  }
5246
5328
  }
5329
+ function allowedToolKey(entry) {
5330
+ switch (entry.type) {
5331
+ case "mcp":
5332
+ return `mcp:${entry.server_label}`;
5333
+ case "function":
5334
+ case "custom":
5335
+ return `${entry.type}:${entry.name}`;
5336
+ default:
5337
+ return entry.type;
5338
+ }
5339
+ }
5340
+ function isSameAllowedTool(a, b) {
5341
+ if (a.supported && b.supported) {
5342
+ return allowedToolKey(a.entry) === allowedToolKey(b.entry);
5343
+ }
5344
+ if (!a.supported && !b.supported) {
5345
+ return a.reason === b.reason;
5346
+ }
5347
+ return false;
5348
+ }
5349
+ function toAllowedToolResolution(tool) {
5350
+ switch (tool.type) {
5351
+ case "custom":
5352
+ return { supported: true, entry: { type: "custom", name: tool.name } };
5353
+ case "mcp":
5354
+ return {
5355
+ supported: true,
5356
+ entry: { type: "mcp", server_label: tool.server_label }
5357
+ };
5358
+ case "file_search":
5359
+ case "web_search":
5360
+ case "web_search_preview":
5361
+ case "image_generation":
5362
+ case "code_interpreter":
5363
+ case "apply_patch":
5364
+ case "shell":
5365
+ case "local_shell":
5366
+ return { supported: true, entry: { type: tool.type } };
5367
+ default:
5368
+ return {
5369
+ supported: false,
5370
+ reason: `OpenAI does not support ${tool.type} tools in tool_choice.allowed_tools`
5371
+ };
5372
+ }
5373
+ }
5247
5374
  function prepareFunctionTool({
5248
5375
  tool,
5249
5376
  options
@@ -7478,7 +7605,7 @@ var OpenAITranscriptionModel = class {
7478
7605
  };
7479
7606
 
7480
7607
  // src/version.ts
7481
- var VERSION = true ? "3.0.97" : "0.0.0-test";
7608
+ var VERSION = true ? "3.0.98" : "0.0.0-test";
7482
7609
 
7483
7610
  // src/openai-provider.ts
7484
7611
  function createOpenAI(options = {}) {