@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.
@@ -5236,7 +5236,7 @@ async function prepareResponsesTools({
5236
5236
  toolNameMapping,
5237
5237
  customProviderToolNames
5238
5238
  }) {
5239
- var _a, _b, _c;
5239
+ var _a, _b, _c, _d;
5240
5240
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
5241
5241
  const toolWarnings = [];
5242
5242
  if (tools == null) {
@@ -5245,6 +5245,20 @@ async function prepareResponsesTools({
5245
5245
  const openaiTools = [];
5246
5246
  const namespaceTools = /* @__PURE__ */ new Map();
5247
5247
  const resolvedCustomProviderToolNames = customProviderToolNames != null ? customProviderToolNames : /* @__PURE__ */ new Set();
5248
+ const allowedToolResolutions = /* @__PURE__ */ new Map();
5249
+ const allowedToolAliases = /* @__PURE__ */ new Map();
5250
+ const recordAllowedTool = (toolName, resolution, canonicalName) => {
5251
+ allowedToolResolutions.set(toolName, resolution);
5252
+ if (canonicalName == null || canonicalName === toolName) {
5253
+ return;
5254
+ }
5255
+ const existingAlias = allowedToolAliases.get(canonicalName);
5256
+ if (existingAlias == null) {
5257
+ allowedToolAliases.set(canonicalName, resolution);
5258
+ } else if (existingAlias !== "ambiguous" && !isSameAllowedTool(existingAlias, resolution)) {
5259
+ allowedToolAliases.set(canonicalName, "ambiguous");
5260
+ }
5261
+ };
5248
5262
  for (const tool of tools) {
5249
5263
  switch (tool.type) {
5250
5264
  case "function": {
@@ -5274,9 +5288,24 @@ async function prepareResponsesTools({
5274
5288
  }
5275
5289
  namespaceTool.tools.push(openaiFunctionTool);
5276
5290
  }
5291
+ recordAllowedTool(
5292
+ tool.name,
5293
+ namespace != null ? {
5294
+ supported: false,
5295
+ reason: "tools inside an OpenAI tool namespace are not visible to tool_choice.allowed_tools"
5296
+ } : (openaiOptions == null ? void 0 : openaiOptions.deferLoading) === true ? {
5297
+ supported: false,
5298
+ reason: "deferred tools are not visible to tool_choice.allowed_tools"
5299
+ } : {
5300
+ supported: true,
5301
+ entry: { type: "function", name: tool.name }
5302
+ },
5303
+ void 0
5304
+ );
5277
5305
  break;
5278
5306
  }
5279
5307
  case "provider": {
5308
+ const openaiToolCountBefore = openaiTools.length;
5280
5309
  switch (tool.id) {
5281
5310
  case "openai.file_search": {
5282
5311
  const args = await validateTypes2({
@@ -5437,6 +5466,14 @@ async function prepareResponsesTools({
5437
5466
  break;
5438
5467
  }
5439
5468
  }
5469
+ if (openaiTools.length > openaiToolCountBefore) {
5470
+ const openaiTool = openaiTools[openaiToolCountBefore];
5471
+ recordAllowedTool(
5472
+ tool.name,
5473
+ toAllowedToolResolution(openaiTool),
5474
+ toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(tool.name)
5475
+ );
5476
+ }
5440
5477
  break;
5441
5478
  }
5442
5479
  default:
@@ -5448,18 +5485,63 @@ async function prepareResponsesTools({
5448
5485
  }
5449
5486
  }
5450
5487
  if (allowedTools != null) {
5488
+ const allowedToolEntries = [];
5489
+ const droppedToolNames = [];
5490
+ for (const name of allowedTools.toolNames) {
5491
+ const directResolution = allowedToolResolutions.get(name);
5492
+ const resolution = directResolution != null ? directResolution : allowedToolAliases.get(name);
5493
+ if (directResolution != null && allowedToolAliases.has(name)) {
5494
+ toolWarnings.push({
5495
+ type: "unsupported",
5496
+ feature: `allowedTools entry "${name}"`,
5497
+ 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"
5498
+ });
5499
+ }
5500
+ if (resolution === "ambiguous") {
5501
+ toolWarnings.push({
5502
+ type: "unsupported",
5503
+ feature: `allowedTools entry "${name}"`,
5504
+ details: "several tools in this request share this provider tool name; use the tool name from the tools for this request instead"
5505
+ });
5506
+ droppedToolNames.push(name);
5507
+ continue;
5508
+ }
5509
+ if (resolution == null) {
5510
+ toolWarnings.push({
5511
+ type: "unsupported",
5512
+ feature: `allowedTools entry "${name}"`,
5513
+ details: "the tool is not part of the tools for this request and is sent as a function tool"
5514
+ });
5515
+ allowedToolEntries.push({
5516
+ type: "function",
5517
+ name: (_b = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(name)) != null ? _b : name
5518
+ });
5519
+ continue;
5520
+ }
5521
+ if (!resolution.supported) {
5522
+ toolWarnings.push({
5523
+ type: "unsupported",
5524
+ feature: `allowedTools entry "${name}"`,
5525
+ details: `${resolution.reason}; the tool is removed from the allowed tools`
5526
+ });
5527
+ droppedToolNames.push(name);
5528
+ continue;
5529
+ }
5530
+ allowedToolEntries.push(resolution.entry);
5531
+ }
5532
+ if (allowedToolEntries.length === 0) {
5533
+ throw new UnsupportedFunctionalityError5({
5534
+ functionality: `allowedTools with only tools that cannot be allow-listed (${droppedToolNames.join(
5535
+ ", "
5536
+ )})`
5537
+ });
5538
+ }
5451
5539
  return {
5452
5540
  tools: openaiTools,
5453
5541
  toolChoice: {
5454
5542
  type: "allowed_tools",
5455
- mode: (_b = allowedTools.mode) != null ? _b : "auto",
5456
- tools: allowedTools.toolNames.map((name) => {
5457
- var _a2;
5458
- return {
5459
- type: "function",
5460
- name: (_a2 = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(name)) != null ? _a2 : name
5461
- };
5462
- })
5543
+ mode: (_c = allowedTools.mode) != null ? _c : "auto",
5544
+ tools: allowedToolEntries
5463
5545
  },
5464
5546
  toolWarnings
5465
5547
  };
@@ -5474,7 +5556,7 @@ async function prepareResponsesTools({
5474
5556
  case "required":
5475
5557
  return { tools: openaiTools, toolChoice: type, toolWarnings };
5476
5558
  case "tool": {
5477
- const resolvedToolName = (_c = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(toolChoice.toolName)) != null ? _c : toolChoice.toolName;
5559
+ const resolvedToolName = (_d = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(toolChoice.toolName)) != null ? _d : toolChoice.toolName;
5478
5560
  return {
5479
5561
  tools: openaiTools,
5480
5562
  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 },
@@ -5489,6 +5571,51 @@ async function prepareResponsesTools({
5489
5571
  }
5490
5572
  }
5491
5573
  }
5574
+ function allowedToolKey(entry) {
5575
+ switch (entry.type) {
5576
+ case "mcp":
5577
+ return `mcp:${entry.server_label}`;
5578
+ case "function":
5579
+ case "custom":
5580
+ return `${entry.type}:${entry.name}`;
5581
+ default:
5582
+ return entry.type;
5583
+ }
5584
+ }
5585
+ function isSameAllowedTool(a, b) {
5586
+ if (a.supported && b.supported) {
5587
+ return allowedToolKey(a.entry) === allowedToolKey(b.entry);
5588
+ }
5589
+ if (!a.supported && !b.supported) {
5590
+ return a.reason === b.reason;
5591
+ }
5592
+ return false;
5593
+ }
5594
+ function toAllowedToolResolution(tool) {
5595
+ switch (tool.type) {
5596
+ case "custom":
5597
+ return { supported: true, entry: { type: "custom", name: tool.name } };
5598
+ case "mcp":
5599
+ return {
5600
+ supported: true,
5601
+ entry: { type: "mcp", server_label: tool.server_label }
5602
+ };
5603
+ case "file_search":
5604
+ case "web_search":
5605
+ case "web_search_preview":
5606
+ case "image_generation":
5607
+ case "code_interpreter":
5608
+ case "apply_patch":
5609
+ case "shell":
5610
+ case "local_shell":
5611
+ return { supported: true, entry: { type: tool.type } };
5612
+ default:
5613
+ return {
5614
+ supported: false,
5615
+ reason: `OpenAI does not support ${tool.type} tools in tool_choice.allowed_tools`
5616
+ };
5617
+ }
5618
+ }
5492
5619
  function prepareFunctionTool({
5493
5620
  tool,
5494
5621
  options