@ai-sdk/provider-utils 5.0.0-beta.3 → 5.0.0-beta.30
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 +284 -0
- package/dist/index.d.ts +1339 -813
- package/dist/index.js +837 -288
- package/dist/index.js.map +1 -1
- package/dist/test/index.d.ts +2 -1
- package/dist/test/index.js +18 -37
- package/dist/test/index.js.map +1 -1
- package/package.json +13 -13
- package/src/add-additional-properties-to-json-schema.ts +1 -1
- package/src/as-array.ts +12 -0
- package/src/convert-image-model-file-to-data-uri.ts +3 -3
- package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
- package/src/create-tool-name-mapping.ts +6 -22
- package/src/detect-media-type.ts +312 -0
- package/src/filter-nullable.ts +11 -0
- package/src/get-error-message.ts +1 -15
- package/src/get-from-api.ts +2 -2
- package/src/has-required-key.ts +6 -0
- package/src/index.ts +42 -12
- package/src/inject-json-instruction.ts +6 -6
- package/src/is-buffer.ts +9 -0
- package/src/is-json-serializable.ts +29 -0
- package/src/is-provider-reference.ts +21 -0
- package/src/is-url-supported.ts +17 -2
- package/src/load-api-key.ts +1 -1
- package/src/load-setting.ts +1 -1
- package/src/map-reasoning-to-provider.ts +108 -0
- package/src/maybe-promise-like.ts +3 -0
- package/src/parse-json-event-stream.ts +3 -3
- package/src/parse-json.ts +3 -3
- package/src/parse-provider-options.ts +1 -1
- package/src/post-to-api.ts +4 -4
- package/src/provider-defined-tool-factory.ts +129 -0
- package/src/provider-executed-tool-factory.ts +69 -0
- package/src/resolve-full-media-type.ts +49 -0
- package/src/resolve-provider-reference.ts +26 -0
- package/src/resolve.ts +16 -1
- package/src/response-handler.ts +3 -3
- package/src/schema.ts +6 -3
- package/src/secure-json-parse.ts +1 -1
- package/src/serialize-model-options.ts +63 -0
- package/src/streaming-tool-call-tracker.ts +241 -0
- package/src/test/convert-response-stream-to-array.ts +1 -1
- package/src/test/is-node-version.ts +22 -1
- package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
- package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +4 -4
- package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +5 -5
- package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
- package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
- package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +4 -4
- package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +9 -10
- package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
- package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
- package/src/types/assistant-model-message.ts +5 -3
- package/src/types/content-part.ts +102 -24
- package/src/types/context.ts +4 -0
- package/src/types/executable-tool.ts +17 -0
- package/src/types/execute-tool.ts +29 -9
- package/src/types/file-data.ts +48 -0
- package/src/types/index.ts +26 -11
- package/src/types/infer-tool-context.ts +12 -0
- package/src/types/infer-tool-input.ts +7 -0
- package/src/types/infer-tool-output.ts +7 -0
- package/src/types/infer-tool-set-context.ts +15 -0
- package/src/types/model-message.ts +4 -4
- package/src/types/never-optional.ts +7 -0
- package/src/types/provider-options.ts +2 -2
- package/src/types/provider-reference.ts +10 -0
- package/src/types/sensitive-context.ts +9 -0
- package/src/types/system-model-message.ts +1 -1
- package/src/types/tool-approval-request.ts +7 -0
- package/src/types/tool-execute-function.ts +50 -0
- package/src/types/tool-model-message.ts +3 -3
- package/src/types/tool-needs-approval-function.ts +39 -0
- package/src/types/tool-set.ts +22 -0
- package/src/types/tool.ts +251 -222
- package/src/types/user-model-message.ts +2 -2
- package/src/validate-download-url.ts +7 -2
- package/src/validate-types.ts +5 -3
- package/dist/index.d.mts +0 -1458
- package/dist/index.mjs +0 -2759
- package/dist/index.mjs.map +0 -1
- package/dist/test/index.d.mts +0 -17
- package/dist/test/index.mjs +0 -77
- package/dist/test/index.mjs.map +0 -1
- package/src/provider-tool-factory.ts +0 -125
package/src/types/tool.ts
CHANGED
|
@@ -1,86 +1,33 @@
|
|
|
1
|
-
import { JSONValue } from '@ai-sdk/provider';
|
|
2
|
-
import { FlexibleSchema } from '../schema';
|
|
3
|
-
import { ToolResultOutput } from './content-part';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import type { JSONValue, SharedV4ProviderMetadata } from '@ai-sdk/provider';
|
|
2
|
+
import type { FlexibleSchema } from '../schema';
|
|
3
|
+
import type { ToolResultOutput } from './content-part';
|
|
4
|
+
import type { Context } from './context';
|
|
5
|
+
import type { NeverOptional } from './never-optional';
|
|
6
|
+
import type { ProviderOptions } from './provider-options';
|
|
7
|
+
import type { SensitiveContext } from './sensitive-context';
|
|
8
|
+
import type {
|
|
9
|
+
ToolExecuteFunction,
|
|
10
|
+
ToolExecutionOptions,
|
|
11
|
+
} from './tool-execute-function';
|
|
12
|
+
import type { ToolNeedsApprovalFunction } from './tool-needs-approval-function';
|
|
6
13
|
|
|
7
14
|
/**
|
|
8
|
-
*
|
|
15
|
+
* Helper type to determine the outputSchema and execute function properties of a tool.
|
|
9
16
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Messages that were sent to the language model to initiate the response that contained the tool call.
|
|
18
|
-
* The messages **do not** include the system prompt nor the assistant response that contained the tool call.
|
|
19
|
-
*/
|
|
20
|
-
messages: ModelMessage[];
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* An optional abort signal that indicates that the overall operation should be aborted.
|
|
24
|
-
*/
|
|
25
|
-
abortSignal?: AbortSignal;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* User-defined context.
|
|
29
|
-
*
|
|
30
|
-
* Treat the context object as immutable inside tools.
|
|
31
|
-
* Mutating the context object can lead to race conditions and unexpected results
|
|
32
|
-
* when tools are called in parallel.
|
|
33
|
-
*
|
|
34
|
-
* If you need to mutate the context, analyze the tool calls and results
|
|
35
|
-
* in `prepareStep` and update it there.
|
|
36
|
-
*
|
|
37
|
-
* Experimental (can break in patch releases).
|
|
38
|
-
*/
|
|
39
|
-
experimental_context?: unknown;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Function that is called to determine if the tool needs approval before it can be executed.
|
|
44
|
-
*/
|
|
45
|
-
export type ToolNeedsApprovalFunction<INPUT> = (
|
|
46
|
-
input: INPUT,
|
|
47
|
-
options: {
|
|
48
|
-
/**
|
|
49
|
-
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
50
|
-
*/
|
|
51
|
-
toolCallId: string;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Messages that were sent to the language model to initiate the response that contained the tool call.
|
|
55
|
-
* The messages **do not** include the system prompt nor the assistant response that contained the tool call.
|
|
56
|
-
*/
|
|
57
|
-
messages: ModelMessage[];
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Additional context.
|
|
61
|
-
*
|
|
62
|
-
* Experimental (can break in patch releases).
|
|
63
|
-
*/
|
|
64
|
-
experimental_context?: unknown;
|
|
65
|
-
},
|
|
66
|
-
) => boolean | PromiseLike<boolean>;
|
|
67
|
-
|
|
68
|
-
export type ToolExecuteFunction<INPUT, OUTPUT> = (
|
|
69
|
-
input: INPUT,
|
|
70
|
-
options: ToolExecutionOptions,
|
|
71
|
-
) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
|
|
72
|
-
|
|
73
|
-
// 0 extends 1 & N checks for any
|
|
74
|
-
// [N] extends [never] checks for never
|
|
75
|
-
type NeverOptional<N, T> = 0 extends 1 & N
|
|
76
|
-
? Partial<T>
|
|
77
|
-
: [N] extends [never]
|
|
78
|
-
? Partial<Record<keyof T, undefined>>
|
|
79
|
-
: T;
|
|
80
|
-
|
|
81
|
-
type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<
|
|
17
|
+
type ToolOutputProperties<
|
|
18
|
+
INPUT,
|
|
19
|
+
OUTPUT,
|
|
20
|
+
CONTEXT extends Context | unknown | never,
|
|
21
|
+
> = NeverOptional<
|
|
82
22
|
OUTPUT,
|
|
83
23
|
| {
|
|
24
|
+
/**
|
|
25
|
+
* The optional schema of the output that the tool produces.
|
|
26
|
+
*
|
|
27
|
+
* If not provided, the output shape will be inferred from the execute function.
|
|
28
|
+
*/
|
|
29
|
+
outputSchema?: FlexibleSchema<OUTPUT>;
|
|
30
|
+
|
|
84
31
|
/**
|
|
85
32
|
* An async function that is called with the arguments from the tool call and produces a result.
|
|
86
33
|
* If not provided, the tool will not be executed automatically.
|
|
@@ -88,11 +35,14 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<
|
|
|
88
35
|
* @args is the input of the tool call.
|
|
89
36
|
* @options.abortSignal is a signal that can be used to abort the tool call.
|
|
90
37
|
*/
|
|
91
|
-
execute: ToolExecuteFunction<INPUT, OUTPUT>;
|
|
92
|
-
|
|
93
|
-
outputSchema?: FlexibleSchema<OUTPUT>;
|
|
38
|
+
execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
94
39
|
}
|
|
95
40
|
| {
|
|
41
|
+
/**
|
|
42
|
+
* The schema of the output that the tool produces.
|
|
43
|
+
*
|
|
44
|
+
* Required when no execute function is provided.
|
|
45
|
+
*/
|
|
96
46
|
outputSchema: FlexibleSchema<OUTPUT>;
|
|
97
47
|
|
|
98
48
|
execute?: never;
|
|
@@ -100,22 +50,13 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<
|
|
|
100
50
|
>;
|
|
101
51
|
|
|
102
52
|
/**
|
|
103
|
-
*
|
|
104
|
-
* This enables the language model to generate the input.
|
|
105
|
-
*
|
|
106
|
-
* The tool can also contain an optional execute function for the actual execution function of the tool.
|
|
53
|
+
* Common properties shared by all tool kinds.
|
|
107
54
|
*/
|
|
108
|
-
|
|
55
|
+
type BaseTool<
|
|
109
56
|
INPUT extends JSONValue | unknown | never = any,
|
|
110
57
|
OUTPUT extends JSONValue | unknown | never = any,
|
|
58
|
+
CONTEXT extends Context | unknown | never = any,
|
|
111
59
|
> = {
|
|
112
|
-
/**
|
|
113
|
-
* An optional description of what the tool does.
|
|
114
|
-
* Will be used by the language model to decide whether to use the tool.
|
|
115
|
-
* Not used for provider-defined tools.
|
|
116
|
-
*/
|
|
117
|
-
description?: string;
|
|
118
|
-
|
|
119
60
|
/**
|
|
120
61
|
* An optional title of the tool.
|
|
121
62
|
*/
|
|
@@ -128,6 +69,17 @@ export type Tool<
|
|
|
128
69
|
*/
|
|
129
70
|
providerOptions?: ProviderOptions;
|
|
130
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Optional metadata about the tool itself (e.g. its source).
|
|
74
|
+
*
|
|
75
|
+
* Unlike `providerOptions`, this metadata is not sent to the language
|
|
76
|
+
* model. Instead, it is propagated onto the resulting tool call's
|
|
77
|
+
* `providerMetadata` so consumers can read it from tool call / result
|
|
78
|
+
* parts and UI message parts. This is useful for sources of dynamic
|
|
79
|
+
* tools (e.g. an MCP server) to identify themselves.
|
|
80
|
+
*/
|
|
81
|
+
providerMetadata?: SharedV4ProviderMetadata;
|
|
82
|
+
|
|
131
83
|
/**
|
|
132
84
|
* The schema of the input that the tool expects.
|
|
133
85
|
* The language model will use this to generate the input.
|
|
@@ -138,39 +90,46 @@ export type Tool<
|
|
|
138
90
|
inputSchema: FlexibleSchema<INPUT>;
|
|
139
91
|
|
|
140
92
|
/**
|
|
141
|
-
* An optional
|
|
142
|
-
*
|
|
93
|
+
* An optional schema describing the context that the tool expects.
|
|
94
|
+
*
|
|
95
|
+
* The context is passed to execute function as part of the execution options.
|
|
143
96
|
*/
|
|
144
|
-
|
|
97
|
+
contextSchema?: FlexibleSchema<CONTEXT>;
|
|
145
98
|
|
|
146
99
|
/**
|
|
147
|
-
*
|
|
100
|
+
* Marks top-level context properties that contain sensitive data and should be excluded from telemetry.
|
|
101
|
+
* Properties marked as `true` are omitted from telemetry integrations.
|
|
148
102
|
*/
|
|
149
|
-
|
|
150
|
-
| boolean
|
|
151
|
-
| ToolNeedsApprovalFunction<[INPUT] extends [never] ? unknown : INPUT>;
|
|
103
|
+
sensitiveContext?: SensitiveContext<CONTEXT>;
|
|
152
104
|
|
|
153
105
|
/**
|
|
154
|
-
*
|
|
106
|
+
* Whether the tool needs approval before it can be executed.
|
|
155
107
|
*
|
|
156
|
-
*
|
|
157
|
-
* how the input should be generated. Strict mode will always produce
|
|
158
|
-
* valid inputs, but it might limit what input schemas are supported.
|
|
108
|
+
* @deprecated Tool approval is handled on a `generateText` / `streamText` level now.
|
|
159
109
|
*/
|
|
160
|
-
|
|
110
|
+
needsApproval?:
|
|
111
|
+
| boolean
|
|
112
|
+
| ToolNeedsApprovalFunction<
|
|
113
|
+
[INPUT] extends [never] ? unknown : INPUT,
|
|
114
|
+
NoInfer<CONTEXT>
|
|
115
|
+
>;
|
|
161
116
|
|
|
162
117
|
/**
|
|
163
118
|
* Optional function that is called when the argument streaming starts.
|
|
164
119
|
* Only called when the tool is used in a streaming context.
|
|
165
120
|
*/
|
|
166
|
-
onInputStart?: (
|
|
121
|
+
onInputStart?: (
|
|
122
|
+
options: ToolExecutionOptions<NoInfer<CONTEXT>>,
|
|
123
|
+
) => void | PromiseLike<void>;
|
|
167
124
|
|
|
168
125
|
/**
|
|
169
126
|
* Optional function that is called when an argument streaming delta is available.
|
|
170
127
|
* Only called when the tool is used in a streaming context.
|
|
171
128
|
*/
|
|
172
129
|
onInputDelta?: (
|
|
173
|
-
options: { inputTextDelta: string } & ToolExecutionOptions
|
|
130
|
+
options: { inputTextDelta: string } & ToolExecutionOptions<
|
|
131
|
+
NoInfer<CONTEXT>
|
|
132
|
+
>,
|
|
174
133
|
) => void | PromiseLike<void>;
|
|
175
134
|
|
|
176
135
|
/**
|
|
@@ -180,145 +139,215 @@ export type Tool<
|
|
|
180
139
|
onInputAvailable?: (
|
|
181
140
|
options: {
|
|
182
141
|
input: [INPUT] extends [never] ? unknown : INPUT;
|
|
183
|
-
} & ToolExecutionOptions
|
|
142
|
+
} & ToolExecutionOptions<NoInfer<CONTEXT>>,
|
|
184
143
|
) => void | PromiseLike<void>;
|
|
185
|
-
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
147
|
+
*
|
|
148
|
+
* If not provided, the tool result will be sent as a JSON object.
|
|
149
|
+
*
|
|
150
|
+
* This function is invoked on the server by `convertToModelMessages`, so ensure that you pass the same "tools" (ToolSet) to both "convertToModelMessages" and "streamText" (or other generation APIs).
|
|
151
|
+
*/
|
|
152
|
+
toModelOutput?: (options: {
|
|
186
153
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* If not provided, the tool result will be sent as a JSON object.
|
|
154
|
+
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
190
155
|
*/
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
194
|
-
*/
|
|
195
|
-
toolCallId: string;
|
|
156
|
+
toolCallId: string;
|
|
196
157
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
158
|
+
/**
|
|
159
|
+
* The input of the tool call.
|
|
160
|
+
*/
|
|
161
|
+
input: [INPUT] extends [never] ? unknown : INPUT;
|
|
201
162
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
163
|
+
/**
|
|
164
|
+
* The output of the tool call.
|
|
165
|
+
*/
|
|
166
|
+
output: 0 extends 1 & OUTPUT
|
|
167
|
+
? any
|
|
168
|
+
: [OUTPUT] extends [never]
|
|
206
169
|
? any
|
|
207
|
-
:
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}) => ToolResultOutput | PromiseLike<ToolResultOutput>;
|
|
211
|
-
} & (
|
|
212
|
-
| {
|
|
213
|
-
/**
|
|
214
|
-
* Tool with user-defined input and output schemas.
|
|
215
|
-
*/
|
|
216
|
-
type?: undefined | 'function';
|
|
217
|
-
}
|
|
218
|
-
| {
|
|
219
|
-
/**
|
|
220
|
-
* Tool that is defined at runtime (e.g. an MCP tool).
|
|
221
|
-
* The types of input and output are not known at development time.
|
|
222
|
-
*/
|
|
223
|
-
type: 'dynamic';
|
|
224
|
-
}
|
|
225
|
-
| {
|
|
226
|
-
/**
|
|
227
|
-
* Tool with provider-defined input and output schemas.
|
|
228
|
-
*/
|
|
229
|
-
type: 'provider';
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
|
|
233
|
-
*/
|
|
234
|
-
id: `${string}.${string}`;
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
|
|
238
|
-
*/
|
|
239
|
-
args: Record<string, unknown>;
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Whether this provider-executed tool supports deferred results.
|
|
243
|
-
*
|
|
244
|
-
* When true, the tool result may not be returned in the same turn as the
|
|
245
|
-
* tool call (e.g., when using programmatic tool calling where a server tool
|
|
246
|
-
* triggers a client-executed tool, and the server tool's result is deferred
|
|
247
|
-
* until the client tool is resolved).
|
|
248
|
-
*
|
|
249
|
-
* This flag allows the AI SDK to handle tool results that arrive without
|
|
250
|
-
* a matching tool call in the current response.
|
|
251
|
-
*
|
|
252
|
-
* @default false
|
|
253
|
-
*/
|
|
254
|
-
supportsDeferredResults?: boolean;
|
|
255
|
-
}
|
|
256
|
-
);
|
|
170
|
+
: NoInfer<OUTPUT>;
|
|
171
|
+
}) => ToolResultOutput | PromiseLike<ToolResultOutput>;
|
|
172
|
+
} & ToolOutputProperties<INPUT, OUTPUT, NoInfer<CONTEXT>>;
|
|
257
173
|
|
|
258
174
|
/**
|
|
259
|
-
*
|
|
175
|
+
* Common properties shared by function-style tools.
|
|
260
176
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
177
|
+
type BaseFunctionTool<
|
|
178
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
179
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
180
|
+
CONTEXT extends Context | unknown | never = any,
|
|
181
|
+
> = BaseTool<INPUT, OUTPUT, CONTEXT> & {
|
|
182
|
+
/**
|
|
183
|
+
* An optional description of what the tool does.
|
|
184
|
+
* Will be used by the language model to decide whether to use the tool.
|
|
185
|
+
*/
|
|
186
|
+
description?: string;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Strict mode setting for the tool.
|
|
190
|
+
*
|
|
191
|
+
* Providers that support strict mode will use this setting to determine
|
|
192
|
+
* how the input should be generated. Strict mode will always produce
|
|
193
|
+
* valid inputs, but it might limit what input schemas are supported.
|
|
194
|
+
*/
|
|
195
|
+
strict?: boolean;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* An optional list of input examples that show the language
|
|
199
|
+
* model what the input should look like.
|
|
200
|
+
*/
|
|
201
|
+
inputExamples?: Array<{ input: NoInfer<INPUT> }>;
|
|
202
|
+
|
|
203
|
+
// make all properties available to improve usage dx
|
|
204
|
+
id?: never;
|
|
205
|
+
isProviderExecuted?: never;
|
|
206
|
+
args?: never;
|
|
207
|
+
supportsDeferredResults?: never;
|
|
208
|
+
};
|
|
263
209
|
|
|
264
210
|
/**
|
|
265
|
-
*
|
|
211
|
+
* Tool with user-defined input and output schemas.
|
|
266
212
|
*/
|
|
267
|
-
export type
|
|
268
|
-
|
|
213
|
+
export type FunctionTool<
|
|
214
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
215
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
216
|
+
CONTEXT extends Context | unknown | never = any,
|
|
217
|
+
> = BaseFunctionTool<INPUT, OUTPUT, CONTEXT> & {
|
|
218
|
+
type?: undefined | 'function';
|
|
219
|
+
};
|
|
269
220
|
|
|
270
221
|
/**
|
|
271
|
-
*
|
|
222
|
+
* Tool that is defined at runtime (e.g. an MCP tool).
|
|
223
|
+
* The types of input and output are not known at development time.
|
|
272
224
|
*/
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
export function tool(tool: any): any {
|
|
281
|
-
return tool;
|
|
282
|
-
}
|
|
225
|
+
export type DynamicTool<
|
|
226
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
227
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
228
|
+
CONTEXT extends Context | unknown | never = any,
|
|
229
|
+
> = BaseFunctionTool<INPUT, OUTPUT, CONTEXT> & {
|
|
230
|
+
type: 'dynamic';
|
|
231
|
+
};
|
|
283
232
|
|
|
284
233
|
/**
|
|
285
|
-
*
|
|
234
|
+
* Common properties shared by provider tools.
|
|
286
235
|
*/
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
236
|
+
type BaseProviderTool<
|
|
237
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
238
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
239
|
+
CONTEXT extends Context | unknown | never = any,
|
|
240
|
+
> = BaseTool<INPUT, OUTPUT, CONTEXT> & {
|
|
241
|
+
type: 'provider';
|
|
293
242
|
|
|
294
243
|
/**
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
* If not provided, the tool result will be sent as a JSON object.
|
|
244
|
+
* The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
|
|
298
245
|
*/
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
302
|
-
*/
|
|
303
|
-
toolCallId: string;
|
|
246
|
+
id: `${string}.${string}`;
|
|
304
247
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
248
|
+
/**
|
|
249
|
+
* The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
|
|
250
|
+
*/
|
|
251
|
+
args: Record<string, unknown>;
|
|
309
252
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
253
|
+
// make all properties available to improve usage dx
|
|
254
|
+
description?: never;
|
|
255
|
+
strict?: never;
|
|
256
|
+
inputExamples?: never;
|
|
257
|
+
};
|
|
315
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Tool with provider-defined input and output schemas that is executed by the
|
|
261
|
+
* user.
|
|
262
|
+
*/
|
|
263
|
+
export type ProviderDefinedTool<
|
|
264
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
265
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
266
|
+
CONTEXT extends Context | unknown | never = any,
|
|
267
|
+
> = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
|
|
316
268
|
/**
|
|
317
|
-
*
|
|
269
|
+
* Flag that indicates whether the tool is executed by the provider.
|
|
318
270
|
*/
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
271
|
+
isProviderExecuted: false;
|
|
272
|
+
|
|
273
|
+
// make all properties available to improve usage dx
|
|
274
|
+
supportsDeferredResults?: never;
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Tool with provider-defined input and output schemas that is executed by the
|
|
279
|
+
* provider.
|
|
280
|
+
*/
|
|
281
|
+
export type ProviderExecutedTool<
|
|
282
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
283
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
284
|
+
CONTEXT extends Context | unknown | never = any,
|
|
285
|
+
> = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
|
|
286
|
+
/**
|
|
287
|
+
* Flag that indicates whether the tool is executed by the provider.
|
|
288
|
+
*/
|
|
289
|
+
isProviderExecuted: true;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Whether this provider-executed tool supports deferred results.
|
|
293
|
+
*
|
|
294
|
+
* When true, the tool result may not be returned in the same turn as the
|
|
295
|
+
* tool call (e.g., when using programmatic tool calling where a server tool
|
|
296
|
+
* triggers a client-executed tool, and the server tool's result is deferred
|
|
297
|
+
* until the client tool is resolved).
|
|
298
|
+
*
|
|
299
|
+
* This flag allows the AI SDK to handle tool results that arrive without
|
|
300
|
+
* a matching tool call in the current response.
|
|
301
|
+
*
|
|
302
|
+
* @default false
|
|
303
|
+
*/
|
|
304
|
+
supportsDeferredResults?: boolean;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* A tool can either be user-defined or provider-defined.
|
|
309
|
+
*
|
|
310
|
+
* It contains the schemas and metadata needed for the language model to call
|
|
311
|
+
* the tool and can include an execute function for tools that are executed by
|
|
312
|
+
* the AI SDK.
|
|
313
|
+
*/
|
|
314
|
+
export type Tool<
|
|
315
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
316
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
317
|
+
CONTEXT extends Context | unknown | never = any,
|
|
318
|
+
> =
|
|
319
|
+
| FunctionTool<INPUT, OUTPUT, CONTEXT>
|
|
320
|
+
| DynamicTool<INPUT, OUTPUT, CONTEXT>
|
|
321
|
+
| ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>
|
|
322
|
+
| ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Infer the tool type from a tool object.
|
|
326
|
+
*
|
|
327
|
+
* This is useful for type inference when working with tool objects.
|
|
328
|
+
*/
|
|
329
|
+
// Note: overload order is important for auto-completion
|
|
330
|
+
export function tool<INPUT, OUTPUT, CONTEXT extends Context>(
|
|
331
|
+
tool: Tool<INPUT, OUTPUT, CONTEXT>,
|
|
332
|
+
): Tool<INPUT, OUTPUT, CONTEXT>;
|
|
333
|
+
export function tool<INPUT, CONTEXT extends Context>(
|
|
334
|
+
tool: Tool<INPUT, never, CONTEXT>,
|
|
335
|
+
): Tool<INPUT, never, CONTEXT>;
|
|
336
|
+
export function tool<OUTPUT, CONTEXT extends Context>(
|
|
337
|
+
tool: Tool<never, OUTPUT, CONTEXT>,
|
|
338
|
+
): Tool<never, OUTPUT, CONTEXT>;
|
|
339
|
+
export function tool<CONTEXT extends Context>(
|
|
340
|
+
tool: Tool<never, never, CONTEXT>,
|
|
341
|
+
): Tool<never, never, CONTEXT>;
|
|
342
|
+
export function tool(tool: any): any {
|
|
343
|
+
return tool;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Define a dynamic tool.
|
|
348
|
+
*/
|
|
349
|
+
export function dynamicTool(
|
|
350
|
+
tool: Omit<DynamicTool<unknown, unknown, Context>, 'type'>,
|
|
351
|
+
): DynamicTool<unknown, unknown, Context> {
|
|
352
|
+
return { ...tool, type: 'dynamic' } as DynamicTool<unknown, unknown, Context>;
|
|
324
353
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FilePart, ImagePart, TextPart } from './content-part';
|
|
2
|
-
import { ProviderOptions } from './provider-options';
|
|
1
|
+
import type { FilePart, ImagePart, TextPart } from './content-part';
|
|
2
|
+
import type { ProviderOptions } from './provider-options';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* A user message. It can contain text or a combination of text and images.
|
|
@@ -18,11 +18,16 @@ export function validateDownloadUrl(url: string): void {
|
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
//
|
|
21
|
+
// data: URLs are inline content, so they do not trigger a network fetch or SSRF risk.
|
|
22
|
+
if (parsed.protocol === 'data:') {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Only allow http and https network protocols
|
|
22
27
|
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
23
28
|
throw new DownloadError({
|
|
24
29
|
url,
|
|
25
|
-
message: `URL scheme must be http or
|
|
30
|
+
message: `URL scheme must be http, https, or data, got ${parsed.protocol}`,
|
|
26
31
|
});
|
|
27
32
|
}
|
|
28
33
|
|
package/src/validate-types.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
TypeValidationError,
|
|
3
|
+
type TypeValidationContext,
|
|
4
|
+
} from '@ai-sdk/provider';
|
|
5
|
+
import { asSchema, type FlexibleSchema } from './schema';
|
|
4
6
|
/**
|
|
5
7
|
* Validates the types of an unknown object using a schema and
|
|
6
8
|
* return a strongly-typed object.
|