ai 7.0.105 → 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.
- package/CHANGELOG.md +42 -0
- package/dist/index.js +278 -88
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +15 -3
- package/dist/internal/index.js +153 -46
- package/dist/internal/index.js.map +1 -1
- package/docs/03-ai-sdk-core/19-tool-search.mdx +4 -2
- package/docs/07-reference/01-ai-sdk-core/23-tool-search.mdx +3 -2
- package/package.json +11 -11
- package/src/batch/batch.ts +1 -0
- package/src/generate-object/generate-object.ts +1 -0
- package/src/generate-object/stream-object.ts +1 -0
- package/src/generate-speech/generate-speech.ts +44 -4
- package/src/generate-text/generate-text.ts +28 -15
- package/src/generate-text/invoke-tool-callbacks-from-stream.ts +68 -16
- package/src/generate-text/prune-messages.ts +13 -0
- package/src/generate-text/stream-language-model-call.ts +1 -0
- package/src/generate-text/stream-text.ts +29 -12
- package/src/generate-video/generate-video.ts +61 -10
- package/src/middleware/extract-json-middleware.ts +35 -9
- package/src/middleware/simulate-streaming-middleware.ts +7 -1
- package/src/middleware/wrap-provider.ts +2 -0
- package/src/prompt/convert-to-language-model-prompt.ts +4 -2
- package/src/prompt/create-tool-model-output.ts +20 -11
- package/src/test/mock-server-response.ts +19 -3
- package/src/text-stream/pipe-text-stream-to-response.ts +3 -5
- package/src/ui/http-chat-transport.ts +12 -2
- package/src/ui/last-assistant-message-is-complete-with-approval-responses.ts +1 -1
- package/src/ui/last-assistant-message-is-complete-with-tool-calls.ts +2 -1
- package/src/ui-message-stream/pipe-ui-message-stream-to-response.ts +1 -3
- package/src/ui-message-stream/read-ui-message-stream.ts +13 -4
- package/src/util/data-url.ts +13 -1
- package/src/util/download/download-function.ts +5 -2
- package/src/util/write-to-server-response.ts +7 -3
package/dist/internal/index.d.ts
CHANGED
|
@@ -787,7 +787,7 @@ type DownloadFunction = (options: Array<{
|
|
|
787
787
|
* Default download function.
|
|
788
788
|
* Downloads the file if it is not supported by the model.
|
|
789
789
|
*/
|
|
790
|
-
declare const createDefaultDownloadFunction: (download?: typeof download) => DownloadFunction;
|
|
790
|
+
declare const createDefaultDownloadFunction: (download?: typeof download, abortSignal?: AbortSignal) => DownloadFunction;
|
|
791
791
|
|
|
792
792
|
/**
|
|
793
793
|
* A generated file.
|
|
@@ -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];
|
|
@@ -2550,10 +2552,11 @@ declare global {
|
|
|
2550
2552
|
var AI_SDK_TELEMETRY_INTEGRATIONS: Telemetry[] | undefined;
|
|
2551
2553
|
}
|
|
2552
2554
|
|
|
2553
|
-
declare function convertToLanguageModelPrompt({ prompt, supportedUrls, download, provider, }: {
|
|
2555
|
+
declare function convertToLanguageModelPrompt({ prompt, supportedUrls, download, abortSignal, provider, }: {
|
|
2554
2556
|
prompt: StandardizedPrompt;
|
|
2555
2557
|
supportedUrls: Record<string, RegExp[]>;
|
|
2556
2558
|
download: DownloadFunction | undefined;
|
|
2559
|
+
abortSignal?: AbortSignal;
|
|
2557
2560
|
provider?: string;
|
|
2558
2561
|
}): Promise<LanguageModelV4Prompt>;
|
|
2559
2562
|
/**
|
|
@@ -2687,6 +2690,15 @@ declare function parseToolCall<TOOLS extends ToolSet>({ toolCall, tools, repairT
|
|
|
2687
2690
|
messages: ModelMessage[];
|
|
2688
2691
|
}): Promise<TypedToolCall<TOOLS>>;
|
|
2689
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
|
+
|
|
2690
2702
|
type CollectedToolApprovals<TOOLS extends ToolSet> = {
|
|
2691
2703
|
approvalRequest: ToolApprovalRequest;
|
|
2692
2704
|
approvalResponse: ToolApprovalResponse;
|
|
@@ -2749,4 +2761,4 @@ declare function toResponseMessages<TOOLS extends ToolSet>({ content: inputConte
|
|
|
2749
2761
|
tools: TOOLS | undefined;
|
|
2750
2762
|
}): Promise<Array<AssistantModelMessage | ToolModelMessage>>;
|
|
2751
2763
|
|
|
2752
|
-
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 };
|
package/dist/internal/index.js
CHANGED
|
@@ -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.
|
|
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 ({
|
|
@@ -138,9 +138,9 @@ var download = async ({
|
|
|
138
138
|
};
|
|
139
139
|
|
|
140
140
|
// src/util/download/download-function.ts
|
|
141
|
-
var createDefaultDownloadFunction = (download2 = download) => (requestedDownloads) => Promise.all(
|
|
141
|
+
var createDefaultDownloadFunction = (download2 = download, abortSignal) => (requestedDownloads) => Promise.all(
|
|
142
142
|
requestedDownloads.map(
|
|
143
|
-
async (requestedDownload) => requestedDownload.isUrlSupportedByModel ? null : await download2(requestedDownload)
|
|
143
|
+
async (requestedDownload) => requestedDownload.isUrlSupportedByModel ? null : await download2({ ...requestedDownload, abortSignal })
|
|
144
144
|
)
|
|
145
145
|
);
|
|
146
146
|
|
|
@@ -430,14 +430,15 @@ var MissingToolResultsError = class extends (_b3 = AISDKError3, _a3 = symbol3, _
|
|
|
430
430
|
async function convertToLanguageModelPrompt({
|
|
431
431
|
prompt,
|
|
432
432
|
supportedUrls,
|
|
433
|
-
download: download2
|
|
433
|
+
download: download2,
|
|
434
|
+
abortSignal,
|
|
434
435
|
// `provider` is only needed here to convert legacy tool output types via `mapToolResultOutput`.
|
|
435
436
|
// TODO: remove in v8 when "file-id" and "image-file-id" types are removed
|
|
436
437
|
provider
|
|
437
438
|
}) {
|
|
438
439
|
const downloadedAssets = await downloadAssets(
|
|
439
440
|
prompt.messages,
|
|
440
|
-
download2,
|
|
441
|
+
download2 != null ? download2 : createDefaultDownloadFunction(void 0, abortSignal),
|
|
441
442
|
supportedUrls
|
|
442
443
|
);
|
|
443
444
|
const approvalIdToToolCallId = /* @__PURE__ */ new Map();
|
|
@@ -1061,32 +1062,29 @@ function getMediaTypeFromUrl(url, fallbackMediaType = "application/octet-stream"
|
|
|
1061
1062
|
|
|
1062
1063
|
// src/prompt/create-tool-model-output.ts
|
|
1063
1064
|
import { getErrorMessage } from "@ai-sdk/provider";
|
|
1064
|
-
import {
|
|
1065
|
-
parseJSON
|
|
1066
|
-
} from "@ai-sdk/provider-utils";
|
|
1067
1065
|
async function createToolModelOutput({
|
|
1068
1066
|
toolCallId,
|
|
1069
1067
|
input,
|
|
1070
1068
|
output,
|
|
1071
|
-
tool,
|
|
1069
|
+
tool: tool2,
|
|
1072
1070
|
errorMode
|
|
1073
1071
|
}) {
|
|
1074
1072
|
if (errorMode === "text") {
|
|
1075
1073
|
return { type: "error-text", value: getErrorMessage(output) };
|
|
1076
1074
|
} else if (errorMode === "json") {
|
|
1077
|
-
return { type: "error-json", value:
|
|
1075
|
+
return { type: "error-json", value: toJSONValue(output) };
|
|
1078
1076
|
}
|
|
1079
|
-
if (
|
|
1080
|
-
return await
|
|
1077
|
+
if (tool2 == null ? void 0 : tool2.toModelOutput) {
|
|
1078
|
+
return await tool2.toModelOutput({ toolCallId, input, output });
|
|
1081
1079
|
}
|
|
1082
|
-
return typeof output === "string" ? { type: "text", value: output } : { type: "json", value:
|
|
1080
|
+
return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
|
|
1083
1081
|
}
|
|
1084
|
-
|
|
1082
|
+
function toJSONValue(value) {
|
|
1085
1083
|
if (value === void 0) {
|
|
1086
1084
|
return null;
|
|
1087
1085
|
}
|
|
1088
1086
|
const serialized = JSON.stringify(value);
|
|
1089
|
-
return serialized === void 0 ? null :
|
|
1087
|
+
return serialized === void 0 ? null : JSON.parse(serialized);
|
|
1090
1088
|
}
|
|
1091
1089
|
|
|
1092
1090
|
// src/prompt/prepare-tool-choice.ts
|
|
@@ -1117,25 +1115,25 @@ async function prepareTools({
|
|
|
1117
1115
|
return void 0;
|
|
1118
1116
|
}
|
|
1119
1117
|
const languageModelTools = [];
|
|
1120
|
-
for (const [name12,
|
|
1121
|
-
const toolType =
|
|
1118
|
+
for (const [name12, tool2] of orderToolEntries({ tools, toolOrder })) {
|
|
1119
|
+
const toolType = tool2.type;
|
|
1122
1120
|
switch (toolType) {
|
|
1123
1121
|
case void 0:
|
|
1124
1122
|
case "dynamic":
|
|
1125
1123
|
case "function": {
|
|
1126
1124
|
const description = resolveToolDescription({
|
|
1127
|
-
tool,
|
|
1125
|
+
tool: tool2,
|
|
1128
1126
|
toolName: name12,
|
|
1129
1127
|
toolsContext,
|
|
1130
1128
|
experimental_sandbox: sandbox
|
|
1131
1129
|
});
|
|
1132
|
-
const providerOptions =
|
|
1133
|
-
const inputExamples =
|
|
1134
|
-
const strict =
|
|
1130
|
+
const providerOptions = tool2.providerOptions;
|
|
1131
|
+
const inputExamples = tool2.inputExamples;
|
|
1132
|
+
const strict = tool2.strict;
|
|
1135
1133
|
languageModelTools.push({
|
|
1136
1134
|
type: "function",
|
|
1137
1135
|
name: name12,
|
|
1138
|
-
inputSchema: await asSchema(
|
|
1136
|
+
inputSchema: await asSchema(tool2.inputSchema).jsonSchema,
|
|
1139
1137
|
...description != null ? { description } : {},
|
|
1140
1138
|
...inputExamples != null ? { inputExamples } : {},
|
|
1141
1139
|
...providerOptions != null ? { providerOptions } : {},
|
|
@@ -1147,8 +1145,8 @@ async function prepareTools({
|
|
|
1147
1145
|
languageModelTools.push({
|
|
1148
1146
|
type: "provider",
|
|
1149
1147
|
name: name12,
|
|
1150
|
-
id:
|
|
1151
|
-
args:
|
|
1148
|
+
id: tool2.id,
|
|
1149
|
+
args: tool2.args
|
|
1152
1150
|
});
|
|
1153
1151
|
break;
|
|
1154
1152
|
}
|
|
@@ -1175,12 +1173,12 @@ function orderToolEntries({
|
|
|
1175
1173
|
return [...orderedTools, ...unorderedTools];
|
|
1176
1174
|
}
|
|
1177
1175
|
function resolveToolDescription({
|
|
1178
|
-
tool,
|
|
1176
|
+
tool: tool2,
|
|
1179
1177
|
toolName,
|
|
1180
1178
|
toolsContext,
|
|
1181
1179
|
experimental_sandbox: sandbox
|
|
1182
1180
|
}) {
|
|
1183
|
-
return
|
|
1181
|
+
return tool2.description === void 0 ? void 0 : typeof tool2.description === "string" ? tool2.description : tool2.description({
|
|
1184
1182
|
context: toolsContext[toolName],
|
|
1185
1183
|
experimental_sandbox: sandbox
|
|
1186
1184
|
});
|
|
@@ -3023,7 +3021,7 @@ async function parseToolCall({
|
|
|
3023
3021
|
} catch (error) {
|
|
3024
3022
|
const parsedInput = await safeParseJSON({ text: toolCall.input });
|
|
3025
3023
|
const input = parsedInput.success ? parsedInput.value : toolCall.input;
|
|
3026
|
-
const
|
|
3024
|
+
const tool2 = getOwn(tools, toolCall.toolName);
|
|
3027
3025
|
return {
|
|
3028
3026
|
type: "tool-call",
|
|
3029
3027
|
toolCallId: toolCall.toolCallId,
|
|
@@ -3032,10 +3030,10 @@ async function parseToolCall({
|
|
|
3032
3030
|
dynamic: true,
|
|
3033
3031
|
invalid: true,
|
|
3034
3032
|
error,
|
|
3035
|
-
title:
|
|
3033
|
+
title: tool2 == null ? void 0 : tool2.title,
|
|
3036
3034
|
providerExecuted: toolCall.providerExecuted,
|
|
3037
3035
|
providerMetadata: toolCall.providerMetadata,
|
|
3038
|
-
...(
|
|
3036
|
+
...(tool2 == null ? void 0 : tool2.metadata) != null ? { toolMetadata: tool2.metadata } : {}
|
|
3039
3037
|
};
|
|
3040
3038
|
}
|
|
3041
3039
|
}
|
|
@@ -3076,8 +3074,8 @@ async function doParseToolCall({
|
|
|
3076
3074
|
tools
|
|
3077
3075
|
}) {
|
|
3078
3076
|
const toolName = toolCall.toolName;
|
|
3079
|
-
const
|
|
3080
|
-
if (
|
|
3077
|
+
const tool2 = getOwn(tools, toolName);
|
|
3078
|
+
if (tool2 == null) {
|
|
3081
3079
|
if (toolCall.providerExecuted && toolCall.dynamic) {
|
|
3082
3080
|
return await parseProviderExecutedDynamicToolCall(toolCall);
|
|
3083
3081
|
}
|
|
@@ -3086,7 +3084,7 @@ async function doParseToolCall({
|
|
|
3086
3084
|
availableTools: Object.keys(tools)
|
|
3087
3085
|
});
|
|
3088
3086
|
}
|
|
3089
|
-
const schema = asSchema2(
|
|
3087
|
+
const schema = asSchema2(tool2.inputSchema);
|
|
3090
3088
|
const parseResult = toolCall.input.trim() === "" ? await safeValidateTypes2({ value: {}, schema }) : await safeParseJSON({ text: toolCall.input, schema });
|
|
3091
3089
|
if (parseResult.success === false) {
|
|
3092
3090
|
throw new InvalidToolInputError({
|
|
@@ -3095,16 +3093,16 @@ async function doParseToolCall({
|
|
|
3095
3093
|
cause: parseResult.error
|
|
3096
3094
|
});
|
|
3097
3095
|
}
|
|
3098
|
-
return
|
|
3096
|
+
return tool2.type === "dynamic" ? {
|
|
3099
3097
|
type: "tool-call",
|
|
3100
3098
|
toolCallId: toolCall.toolCallId,
|
|
3101
3099
|
toolName: toolCall.toolName,
|
|
3102
3100
|
input: parseResult.value,
|
|
3103
3101
|
providerExecuted: toolCall.providerExecuted,
|
|
3104
3102
|
providerMetadata: toolCall.providerMetadata,
|
|
3105
|
-
...
|
|
3103
|
+
...tool2.metadata != null ? { toolMetadata: tool2.metadata } : {},
|
|
3106
3104
|
dynamic: true,
|
|
3107
|
-
title:
|
|
3105
|
+
title: tool2.title
|
|
3108
3106
|
} : {
|
|
3109
3107
|
type: "tool-call",
|
|
3110
3108
|
toolCallId: toolCall.toolCallId,
|
|
@@ -3112,9 +3110,117 @@ async function doParseToolCall({
|
|
|
3112
3110
|
input: parseResult.value,
|
|
3113
3111
|
providerExecuted: toolCall.providerExecuted,
|
|
3114
3112
|
providerMetadata: toolCall.providerMetadata,
|
|
3115
|
-
...
|
|
3116
|
-
title:
|
|
3113
|
+
...tool2.metadata != null ? { toolMetadata: tool2.metadata } : {},
|
|
3114
|
+
title: tool2.title
|
|
3115
|
+
};
|
|
3116
|
+
}
|
|
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];
|
|
3117
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 : [];
|
|
3118
3224
|
}
|
|
3119
3225
|
|
|
3120
3226
|
// src/generate-text/collect-tool-approvals.ts
|
|
@@ -3349,7 +3455,7 @@ async function resolveToolApproval({
|
|
|
3349
3455
|
);
|
|
3350
3456
|
}
|
|
3351
3457
|
const toolName = toolCall.toolName;
|
|
3352
|
-
const
|
|
3458
|
+
const tool2 = getOwn(tools, toolName);
|
|
3353
3459
|
const input = toolCall.input;
|
|
3354
3460
|
const userDefinedToolApprovalStatus = getOwn(toolApproval, toolName);
|
|
3355
3461
|
if (userDefinedToolApprovalStatus != null) {
|
|
@@ -3359,24 +3465,24 @@ async function resolveToolApproval({
|
|
|
3359
3465
|
toolContext: await validateToolContext({
|
|
3360
3466
|
toolName,
|
|
3361
3467
|
context: getOwn(toolsContext, toolName),
|
|
3362
|
-
contextSchema:
|
|
3468
|
+
contextSchema: tool2 == null ? void 0 : tool2.contextSchema
|
|
3363
3469
|
}),
|
|
3364
3470
|
runtimeContext
|
|
3365
3471
|
}) : userDefinedToolApprovalStatus;
|
|
3366
3472
|
return normalizeToolApprovalStatus(approvalStatus);
|
|
3367
3473
|
}
|
|
3368
|
-
if ((
|
|
3474
|
+
if ((tool2 == null ? void 0 : tool2.needsApproval) == null) {
|
|
3369
3475
|
return { type: "not-applicable" };
|
|
3370
3476
|
}
|
|
3371
|
-
const needsApproval = typeof
|
|
3477
|
+
const needsApproval = typeof tool2.needsApproval === "function" ? await tool2.needsApproval(input, {
|
|
3372
3478
|
toolCallId: toolCall.toolCallId,
|
|
3373
3479
|
messages,
|
|
3374
3480
|
context: await validateToolContext({
|
|
3375
3481
|
toolName,
|
|
3376
3482
|
context: getOwn(toolsContext, toolName),
|
|
3377
|
-
contextSchema:
|
|
3483
|
+
contextSchema: tool2 == null ? void 0 : tool2.contextSchema
|
|
3378
3484
|
})
|
|
3379
|
-
}) :
|
|
3485
|
+
}) : tool2.needsApproval;
|
|
3380
3486
|
return needsApproval ? { type: "user-approval" } : { type: "not-applicable" };
|
|
3381
3487
|
}
|
|
3382
3488
|
function normalizeToolApprovalStatus(status) {
|
|
@@ -3399,7 +3505,7 @@ async function validateApprovedToolApprovals({
|
|
|
3399
3505
|
const invalid = [];
|
|
3400
3506
|
for (const approval of approvedToolApprovals) {
|
|
3401
3507
|
const { toolCall, approvalRequest } = approval;
|
|
3402
|
-
const
|
|
3508
|
+
const tool2 = getOwn(tools, toolCall.toolName);
|
|
3403
3509
|
if (toolApprovalSecret != null) {
|
|
3404
3510
|
if (approvalRequest.signature == null) {
|
|
3405
3511
|
throw new InvalidToolApprovalSignatureError({
|
|
@@ -3424,10 +3530,10 @@ async function validateApprovedToolApprovals({
|
|
|
3424
3530
|
});
|
|
3425
3531
|
}
|
|
3426
3532
|
}
|
|
3427
|
-
if (isExecutableTool(
|
|
3533
|
+
if (isExecutableTool(tool2) && tool2.inputSchema != null) {
|
|
3428
3534
|
const validation = await safeValidateTypes3({
|
|
3429
3535
|
value: toolCall.input,
|
|
3430
|
-
schema: asSchema3(
|
|
3536
|
+
schema: asSchema3(tool2.inputSchema)
|
|
3431
3537
|
});
|
|
3432
3538
|
if (!validation.success) {
|
|
3433
3539
|
invalid.push({
|
|
@@ -3681,6 +3787,7 @@ export {
|
|
|
3681
3787
|
createRestrictedTelemetryDispatcher,
|
|
3682
3788
|
createTelemetryDispatcher,
|
|
3683
3789
|
createToolModelOutput,
|
|
3790
|
+
createToolSearchState,
|
|
3684
3791
|
downloadAssets,
|
|
3685
3792
|
mapToolResultOutput,
|
|
3686
3793
|
mergeAbortSignals,
|