ai 7.0.106 → 7.0.107

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.
@@ -1721,6 +1721,8 @@ type ToolApprovalConfiguration<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Co
1721
1721
  [key in keyof TOOLS]?: ToolApprovalStatus | SingleToolApprovalFunction<InferToolInput<TOOLS[key]>, InferToolContext<TOOLS[key]>, RUNTIME_CONTEXT>;
1722
1722
  };
1723
1723
 
1724
+ type ResolvedToolCallers = Record<string, ReadonlyArray<string>>;
1725
+
1724
1726
  declare const symbol$1: unique symbol;
1725
1727
  declare class InvalidToolInputError extends AISDKError {
1726
1728
  private readonly [symbol$1];
@@ -2688,6 +2690,15 @@ declare function parseToolCall<TOOLS extends ToolSet>({ toolCall, tools, repairT
2688
2690
  messages: ModelMessage[];
2689
2691
  }): Promise<TypedToolCall<TOOLS>>;
2690
2692
 
2693
+ /** Create discovery state for one generation, never for a shared tool instance. */
2694
+ declare function createToolSearchState({ tools, toolCallers, }: {
2695
+ tools: ToolSet | undefined;
2696
+ toolCallers: ResolvedToolCallers | undefined;
2697
+ }): (activeTools: ToolSet | undefined, options?: {
2698
+ toolsContext?: InferToolSetContext<ToolSet>;
2699
+ experimental_sandbox?: Experimental_SandboxSession;
2700
+ }) => ToolSet | undefined;
2701
+
2691
2702
  type CollectedToolApprovals<TOOLS extends ToolSet> = {
2692
2703
  approvalRequest: ToolApprovalRequest;
2693
2704
  approvalResponse: ToolApprovalResponse;
@@ -2750,4 +2761,4 @@ declare function toResponseMessages<TOOLS extends ToolSet>({ content: inputConte
2750
2761
  tools: TOOLS | undefined;
2751
2762
  }): Promise<Array<AssistantModelMessage | ToolModelMessage>>;
2752
2763
 
2753
- export { type CollectedToolApprovals, DefaultStepResult, type DownloadFunction, addLanguageModelUsage, asLanguageModelUsage, collectToolApprovals, convertToLanguageModelPrompt, createAsyncIterableStream, createDefaultDownloadFunction, createNullLanguageModelUsage, createRestrictedTelemetryDispatcher, createTelemetryDispatcher, createToolModelOutput, downloadAssets, mapToolResultOutput, mergeAbortSignals, mergeCallbacks, parseToolCall, prepareLanguageModelCallOptions as prepareCallSettings, prepareLanguageModelCallOptions, prepareRetries, prepareToolChoice, prepareTools, resolveLanguageModel, signToolApproval, standardizePrompt, toResponseMessages, validateApprovedToolApprovals, verifyToolApprovalSignature };
2764
+ export { type CollectedToolApprovals, DefaultStepResult, type DownloadFunction, addLanguageModelUsage, asLanguageModelUsage, collectToolApprovals, convertToLanguageModelPrompt, createAsyncIterableStream, createDefaultDownloadFunction, createNullLanguageModelUsage, createRestrictedTelemetryDispatcher, createTelemetryDispatcher, createToolModelOutput, createToolSearchState, downloadAssets, mapToolResultOutput, mergeAbortSignals, mergeCallbacks, parseToolCall, prepareLanguageModelCallOptions as prepareCallSettings, prepareLanguageModelCallOptions, prepareRetries, prepareToolChoice, prepareTools, resolveLanguageModel, signToolApproval, standardizePrompt, toResponseMessages, validateApprovedToolApprovals, verifyToolApprovalSignature };
@@ -91,7 +91,7 @@ import {
91
91
  } from "@ai-sdk/provider-utils";
92
92
 
93
93
  // src/version.ts
94
- var VERSION = true ? "7.0.106" : "0.0.0-test";
94
+ var VERSION = true ? "7.0.107" : "0.0.0-test";
95
95
 
96
96
  // src/util/download/download.ts
97
97
  var download = async ({
@@ -1066,7 +1066,7 @@ async function createToolModelOutput({
1066
1066
  toolCallId,
1067
1067
  input,
1068
1068
  output,
1069
- tool,
1069
+ tool: tool2,
1070
1070
  errorMode
1071
1071
  }) {
1072
1072
  if (errorMode === "text") {
@@ -1074,8 +1074,8 @@ async function createToolModelOutput({
1074
1074
  } else if (errorMode === "json") {
1075
1075
  return { type: "error-json", value: toJSONValue(output) };
1076
1076
  }
1077
- if (tool == null ? void 0 : tool.toModelOutput) {
1078
- return await tool.toModelOutput({ toolCallId, input, output });
1077
+ if (tool2 == null ? void 0 : tool2.toModelOutput) {
1078
+ return await tool2.toModelOutput({ toolCallId, input, output });
1079
1079
  }
1080
1080
  return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
1081
1081
  }
@@ -1115,25 +1115,25 @@ async function prepareTools({
1115
1115
  return void 0;
1116
1116
  }
1117
1117
  const languageModelTools = [];
1118
- for (const [name12, tool] of orderToolEntries({ tools, toolOrder })) {
1119
- const toolType = tool.type;
1118
+ for (const [name12, tool2] of orderToolEntries({ tools, toolOrder })) {
1119
+ const toolType = tool2.type;
1120
1120
  switch (toolType) {
1121
1121
  case void 0:
1122
1122
  case "dynamic":
1123
1123
  case "function": {
1124
1124
  const description = resolveToolDescription({
1125
- tool,
1125
+ tool: tool2,
1126
1126
  toolName: name12,
1127
1127
  toolsContext,
1128
1128
  experimental_sandbox: sandbox
1129
1129
  });
1130
- const providerOptions = tool.providerOptions;
1131
- const inputExamples = tool.inputExamples;
1132
- const strict = tool.strict;
1130
+ const providerOptions = tool2.providerOptions;
1131
+ const inputExamples = tool2.inputExamples;
1132
+ const strict = tool2.strict;
1133
1133
  languageModelTools.push({
1134
1134
  type: "function",
1135
1135
  name: name12,
1136
- inputSchema: await asSchema(tool.inputSchema).jsonSchema,
1136
+ inputSchema: await asSchema(tool2.inputSchema).jsonSchema,
1137
1137
  ...description != null ? { description } : {},
1138
1138
  ...inputExamples != null ? { inputExamples } : {},
1139
1139
  ...providerOptions != null ? { providerOptions } : {},
@@ -1145,8 +1145,8 @@ async function prepareTools({
1145
1145
  languageModelTools.push({
1146
1146
  type: "provider",
1147
1147
  name: name12,
1148
- id: tool.id,
1149
- args: tool.args
1148
+ id: tool2.id,
1149
+ args: tool2.args
1150
1150
  });
1151
1151
  break;
1152
1152
  }
@@ -1173,12 +1173,12 @@ function orderToolEntries({
1173
1173
  return [...orderedTools, ...unorderedTools];
1174
1174
  }
1175
1175
  function resolveToolDescription({
1176
- tool,
1176
+ tool: tool2,
1177
1177
  toolName,
1178
1178
  toolsContext,
1179
1179
  experimental_sandbox: sandbox
1180
1180
  }) {
1181
- return tool.description === void 0 ? void 0 : typeof tool.description === "string" ? tool.description : tool.description({
1181
+ return tool2.description === void 0 ? void 0 : typeof tool2.description === "string" ? tool2.description : tool2.description({
1182
1182
  context: toolsContext[toolName],
1183
1183
  experimental_sandbox: sandbox
1184
1184
  });
@@ -3021,7 +3021,7 @@ async function parseToolCall({
3021
3021
  } catch (error) {
3022
3022
  const parsedInput = await safeParseJSON({ text: toolCall.input });
3023
3023
  const input = parsedInput.success ? parsedInput.value : toolCall.input;
3024
- const tool = getOwn(tools, toolCall.toolName);
3024
+ const tool2 = getOwn(tools, toolCall.toolName);
3025
3025
  return {
3026
3026
  type: "tool-call",
3027
3027
  toolCallId: toolCall.toolCallId,
@@ -3030,10 +3030,10 @@ async function parseToolCall({
3030
3030
  dynamic: true,
3031
3031
  invalid: true,
3032
3032
  error,
3033
- title: tool == null ? void 0 : tool.title,
3033
+ title: tool2 == null ? void 0 : tool2.title,
3034
3034
  providerExecuted: toolCall.providerExecuted,
3035
3035
  providerMetadata: toolCall.providerMetadata,
3036
- ...(tool == null ? void 0 : tool.metadata) != null ? { toolMetadata: tool.metadata } : {}
3036
+ ...(tool2 == null ? void 0 : tool2.metadata) != null ? { toolMetadata: tool2.metadata } : {}
3037
3037
  };
3038
3038
  }
3039
3039
  }
@@ -3074,8 +3074,8 @@ async function doParseToolCall({
3074
3074
  tools
3075
3075
  }) {
3076
3076
  const toolName = toolCall.toolName;
3077
- const tool = getOwn(tools, toolName);
3078
- if (tool == null) {
3077
+ const tool2 = getOwn(tools, toolName);
3078
+ if (tool2 == null) {
3079
3079
  if (toolCall.providerExecuted && toolCall.dynamic) {
3080
3080
  return await parseProviderExecutedDynamicToolCall(toolCall);
3081
3081
  }
@@ -3084,7 +3084,7 @@ async function doParseToolCall({
3084
3084
  availableTools: Object.keys(tools)
3085
3085
  });
3086
3086
  }
3087
- const schema = asSchema2(tool.inputSchema);
3087
+ const schema = asSchema2(tool2.inputSchema);
3088
3088
  const parseResult = toolCall.input.trim() === "" ? await safeValidateTypes2({ value: {}, schema }) : await safeParseJSON({ text: toolCall.input, schema });
3089
3089
  if (parseResult.success === false) {
3090
3090
  throw new InvalidToolInputError({
@@ -3093,16 +3093,16 @@ async function doParseToolCall({
3093
3093
  cause: parseResult.error
3094
3094
  });
3095
3095
  }
3096
- return tool.type === "dynamic" ? {
3096
+ return tool2.type === "dynamic" ? {
3097
3097
  type: "tool-call",
3098
3098
  toolCallId: toolCall.toolCallId,
3099
3099
  toolName: toolCall.toolName,
3100
3100
  input: parseResult.value,
3101
3101
  providerExecuted: toolCall.providerExecuted,
3102
3102
  providerMetadata: toolCall.providerMetadata,
3103
- ...tool.metadata != null ? { toolMetadata: tool.metadata } : {},
3103
+ ...tool2.metadata != null ? { toolMetadata: tool2.metadata } : {},
3104
3104
  dynamic: true,
3105
- title: tool.title
3105
+ title: tool2.title
3106
3106
  } : {
3107
3107
  type: "tool-call",
3108
3108
  toolCallId: toolCall.toolCallId,
@@ -3110,11 +3110,119 @@ async function doParseToolCall({
3110
3110
  input: parseResult.value,
3111
3111
  providerExecuted: toolCall.providerExecuted,
3112
3112
  providerMetadata: toolCall.providerMetadata,
3113
- ...tool.metadata != null ? { toolMetadata: tool.metadata } : {},
3114
- title: tool.title
3113
+ ...tool2.metadata != null ? { toolMetadata: tool2.metadata } : {},
3114
+ title: tool2.title
3115
3115
  };
3116
3116
  }
3117
3117
 
3118
+ // src/tool-search/prepare-tool-search.ts
3119
+ import {
3120
+ experimental_getToolCaller as experimental_getToolCaller2
3121
+ } from "@ai-sdk/provider-utils";
3122
+
3123
+ // src/generate-text/tool-caller-configuration.ts
3124
+ import {
3125
+ experimental_getToolCaller
3126
+ } from "@ai-sdk/provider-utils";
3127
+ var DIRECT_TOOL_CALL = "AI_SDK_DIRECT_TOOL_CALL";
3128
+
3129
+ // src/tool-search/tool-search.ts
3130
+ import { jsonSchema, tool } from "@ai-sdk/provider-utils";
3131
+ var toolSearchSymbol = /* @__PURE__ */ Symbol.for("vercel.ai.toolSearch");
3132
+ function isToolSearch(tool2) {
3133
+ return tool2[toolSearchSymbol] === true;
3134
+ }
3135
+
3136
+ // src/tool-search/prepare-tool-search.ts
3137
+ function createToolSearchState({
3138
+ tools,
3139
+ toolCallers
3140
+ }) {
3141
+ const searchTools = Object.entries(tools != null ? tools : {}).filter(
3142
+ ([, tool2]) => tool2.deferLoading || isToolSearch(tool2)
3143
+ );
3144
+ if (searchTools.length === 0) {
3145
+ return (activeTools) => activeTools;
3146
+ }
3147
+ const discovered = /* @__PURE__ */ new Set();
3148
+ const getCallers = (name12) => {
3149
+ var _a12;
3150
+ return (_a12 = getOwn(toolCallers, name12)) != null ? _a12 : [DIRECT_TOOL_CALL];
3151
+ };
3152
+ for (const [name12, tool2] of searchTools) {
3153
+ const callers = getCallers(name12);
3154
+ if (callers.some((name13) => {
3155
+ if (name13 === DIRECT_TOOL_CALL) {
3156
+ return false;
3157
+ }
3158
+ const caller = experimental_getToolCaller2(tools == null ? void 0 : tools[name13]);
3159
+ return (caller == null ? void 0 : caller.type) !== "local" || caller.prepareModelMessage == null;
3160
+ }) || isToolSearch(tool2) && tool2.deferLoading) {
3161
+ throw new InvalidArgumentError({
3162
+ parameter: "tools",
3163
+ value: name12,
3164
+ message: `tool "${name12}" must be callable directly or through code mode with toolDiscovery: 'conversation'. The search tool itself must not defer loading.`
3165
+ });
3166
+ }
3167
+ }
3168
+ return (activeTools, { toolsContext = {}, experimental_sandbox } = {}) => {
3169
+ if (activeTools == null) {
3170
+ return void 0;
3171
+ }
3172
+ const entries = Object.entries(activeTools);
3173
+ return Object.fromEntries(
3174
+ entries.filter(([name12, tool2]) => !tool2.deferLoading || discovered.has(name12)).map(([searchName, tool2]) => {
3175
+ if (!isToolSearch(tool2)) {
3176
+ return [searchName, tool2];
3177
+ }
3178
+ const callers = getCallers(searchName).filter(
3179
+ (name12) => name12 === DIRECT_TOOL_CALL || Object.hasOwn(activeTools, name12)
3180
+ );
3181
+ const candidates = entries.filter(
3182
+ ([name12, candidate]) => candidate.deferLoading && !isToolSearch(candidate) && callers.some((caller) => getCallers(name12).includes(caller))
3183
+ );
3184
+ return [
3185
+ searchName,
3186
+ {
3187
+ ...tool2,
3188
+ execute: ({ query }) => {
3189
+ const terms = [...new Set(tokenize(query))];
3190
+ const matches = candidates.map(([name12, candidate]) => {
3191
+ const description = resolveToolDescription({
3192
+ tool: candidate,
3193
+ toolName: name12,
3194
+ toolsContext,
3195
+ experimental_sandbox
3196
+ });
3197
+ const nameTerms = tokenize(name12);
3198
+ const descriptionTerms = tokenize(description != null ? description : "");
3199
+ const score = terms.reduce(
3200
+ (score2, term) => score2 + (nameTerms.includes(term) ? 2 : 0) + (descriptionTerms.includes(term) ? 1 : 0),
3201
+ 0
3202
+ );
3203
+ return { name: name12, description, score };
3204
+ }).filter((match) => match.score > 0).sort((a, b) => b.score - a.score).slice(0, 5);
3205
+ for (const { name: name12 } of matches) {
3206
+ discovered.add(name12);
3207
+ }
3208
+ return {
3209
+ tools: matches.map(({ name: name12, description }) => ({
3210
+ name: name12,
3211
+ ...description == null ? {} : { description }
3212
+ }))
3213
+ };
3214
+ }
3215
+ }
3216
+ ];
3217
+ })
3218
+ );
3219
+ };
3220
+ }
3221
+ function tokenize(text) {
3222
+ var _a12;
3223
+ return (_a12 = text.replace(/([a-z\d])([A-Z])/g, "$1 $2").toLowerCase().match(/[\p{L}\p{N}]+/gu)) != null ? _a12 : [];
3224
+ }
3225
+
3118
3226
  // src/generate-text/collect-tool-approvals.ts
3119
3227
  function collectToolApprovals({
3120
3228
  messages
@@ -3347,7 +3455,7 @@ async function resolveToolApproval({
3347
3455
  );
3348
3456
  }
3349
3457
  const toolName = toolCall.toolName;
3350
- const tool = getOwn(tools, toolName);
3458
+ const tool2 = getOwn(tools, toolName);
3351
3459
  const input = toolCall.input;
3352
3460
  const userDefinedToolApprovalStatus = getOwn(toolApproval, toolName);
3353
3461
  if (userDefinedToolApprovalStatus != null) {
@@ -3357,24 +3465,24 @@ async function resolveToolApproval({
3357
3465
  toolContext: await validateToolContext({
3358
3466
  toolName,
3359
3467
  context: getOwn(toolsContext, toolName),
3360
- contextSchema: tool == null ? void 0 : tool.contextSchema
3468
+ contextSchema: tool2 == null ? void 0 : tool2.contextSchema
3361
3469
  }),
3362
3470
  runtimeContext
3363
3471
  }) : userDefinedToolApprovalStatus;
3364
3472
  return normalizeToolApprovalStatus(approvalStatus);
3365
3473
  }
3366
- if ((tool == null ? void 0 : tool.needsApproval) == null) {
3474
+ if ((tool2 == null ? void 0 : tool2.needsApproval) == null) {
3367
3475
  return { type: "not-applicable" };
3368
3476
  }
3369
- const needsApproval = typeof tool.needsApproval === "function" ? await tool.needsApproval(input, {
3477
+ const needsApproval = typeof tool2.needsApproval === "function" ? await tool2.needsApproval(input, {
3370
3478
  toolCallId: toolCall.toolCallId,
3371
3479
  messages,
3372
3480
  context: await validateToolContext({
3373
3481
  toolName,
3374
3482
  context: getOwn(toolsContext, toolName),
3375
- contextSchema: tool == null ? void 0 : tool.contextSchema
3483
+ contextSchema: tool2 == null ? void 0 : tool2.contextSchema
3376
3484
  })
3377
- }) : tool.needsApproval;
3485
+ }) : tool2.needsApproval;
3378
3486
  return needsApproval ? { type: "user-approval" } : { type: "not-applicable" };
3379
3487
  }
3380
3488
  function normalizeToolApprovalStatus(status) {
@@ -3397,7 +3505,7 @@ async function validateApprovedToolApprovals({
3397
3505
  const invalid = [];
3398
3506
  for (const approval of approvedToolApprovals) {
3399
3507
  const { toolCall, approvalRequest } = approval;
3400
- const tool = getOwn(tools, toolCall.toolName);
3508
+ const tool2 = getOwn(tools, toolCall.toolName);
3401
3509
  if (toolApprovalSecret != null) {
3402
3510
  if (approvalRequest.signature == null) {
3403
3511
  throw new InvalidToolApprovalSignatureError({
@@ -3422,10 +3530,10 @@ async function validateApprovedToolApprovals({
3422
3530
  });
3423
3531
  }
3424
3532
  }
3425
- if (isExecutableTool(tool) && tool.inputSchema != null) {
3533
+ if (isExecutableTool(tool2) && tool2.inputSchema != null) {
3426
3534
  const validation = await safeValidateTypes3({
3427
3535
  value: toolCall.input,
3428
- schema: asSchema3(tool.inputSchema)
3536
+ schema: asSchema3(tool2.inputSchema)
3429
3537
  });
3430
3538
  if (!validation.success) {
3431
3539
  invalid.push({
@@ -3679,6 +3787,7 @@ export {
3679
3787
  createRestrictedTelemetryDispatcher,
3680
3788
  createTelemetryDispatcher,
3681
3789
  createToolModelOutput,
3790
+ createToolSearchState,
3682
3791
  downloadAssets,
3683
3792
  mapToolResultOutput,
3684
3793
  mergeAbortSignals,