@ai-sdk/provider-utils 5.0.0-beta.9 → 5.0.0-canary.32
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 +184 -0
- package/dist/index.d.ts +1346 -878
- package/dist/index.js +813 -325
- 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 -11
- 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 +1 -1
- package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
- package/src/create-tool-name-mapping.ts +1 -1
- 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 +39 -14
- package/src/inject-json-instruction.ts +1 -1
- 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 +4 -1
- 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-tool-factory.ts → provider-defined-tool-factory.ts} +22 -29
- 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 +3 -3
- package/src/types/content-part.ts +138 -19
- package/src/types/executable-tool.ts +17 -0
- package/src/types/execute-tool.ts +23 -23
- package/src/types/file-data.ts +48 -0
- package/src/types/index.ts +19 -3
- package/src/types/infer-tool-context.ts +7 -2
- package/src/types/infer-tool-set-context.ts +7 -9
- package/src/types/model-message.ts +4 -4
- package/src/types/never-optional.ts +7 -0
- package/src/types/provider-options.ts +1 -1
- 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.ts +236 -223
- package/src/types/user-model-message.ts +2 -2
- package/src/validate-types.ts +5 -3
- package/dist/index.d.mts +0 -1578
- package/dist/index.mjs +0 -2817
- 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/types/union-to-intersection.ts +0 -17
package/src/types/tool.ts
CHANGED
|
@@ -1,97 +1,33 @@
|
|
|
1
|
-
import { JSONValue } from '@ai-sdk/provider';
|
|
2
|
-
import { FlexibleSchema } from '../schema';
|
|
3
|
-
import { ToolResultOutput } from './content-part';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
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';
|
|
7
13
|
|
|
8
14
|
/**
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
export interface ToolExecutionOptions<CONTEXT extends Context> {
|
|
12
|
-
/**
|
|
13
|
-
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
14
|
-
*/
|
|
15
|
-
toolCallId: string;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Messages that were sent to the language model to initiate the response that contained the tool call.
|
|
19
|
-
* The messages **do not** include the system prompt nor the assistant response that contained the tool call.
|
|
20
|
-
*/
|
|
21
|
-
messages: ModelMessage[];
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* An optional abort signal that indicates that the overall operation should be aborted.
|
|
25
|
-
*/
|
|
26
|
-
abortSignal?: AbortSignal;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* User-defined context.
|
|
30
|
-
*
|
|
31
|
-
* Treat the context object as immutable inside tools.
|
|
32
|
-
* Mutating the context object can lead to race conditions and unexpected results
|
|
33
|
-
* when tools are called in parallel.
|
|
34
|
-
*
|
|
35
|
-
* If you need to mutate the context, analyze the tool calls and results
|
|
36
|
-
* in `prepareStep` and update it there.
|
|
37
|
-
*
|
|
38
|
-
* Experimental (can break in patch releases).
|
|
39
|
-
*/
|
|
40
|
-
experimental_context: CONTEXT;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Function that is called to determine if the tool needs approval before it can be executed.
|
|
45
|
-
*/
|
|
46
|
-
export type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context> = (
|
|
47
|
-
input: INPUT,
|
|
48
|
-
options: {
|
|
49
|
-
/**
|
|
50
|
-
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
51
|
-
*/
|
|
52
|
-
toolCallId: string;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Messages that were sent to the language model to initiate the response that contained the tool call.
|
|
56
|
-
* The messages **do not** include the system prompt nor the assistant response that contained the tool call.
|
|
57
|
-
*/
|
|
58
|
-
messages: ModelMessage[];
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Additional context.
|
|
62
|
-
*
|
|
63
|
-
* Experimental (can break in patch releases).
|
|
64
|
-
*/
|
|
65
|
-
experimental_context: CONTEXT;
|
|
66
|
-
},
|
|
67
|
-
) => boolean | PromiseLike<boolean>;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Function that executes the tool and returns either a single result or a stream of results.
|
|
71
|
-
*/
|
|
72
|
-
export type ToolExecuteFunction<INPUT, OUTPUT, CONTEXT extends Context> = (
|
|
73
|
-
input: INPUT,
|
|
74
|
-
options: ToolExecutionOptions<CONTEXT>,
|
|
75
|
-
) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
|
|
76
|
-
|
|
77
|
-
// 0 extends 1 & N checks for any
|
|
78
|
-
// [N] extends [never] checks for never
|
|
79
|
-
type NeverOptional<N, T> = 0 extends 1 & N
|
|
80
|
-
? Partial<T>
|
|
81
|
-
: [N] extends [never]
|
|
82
|
-
? Partial<Record<keyof T, undefined>>
|
|
83
|
-
: T;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Helper type to determine the output properties of a tool.
|
|
15
|
+
* Helper type to determine the outputSchema and execute function properties of a tool.
|
|
87
16
|
*/
|
|
88
17
|
type ToolOutputProperties<
|
|
89
18
|
INPUT,
|
|
90
19
|
OUTPUT,
|
|
91
|
-
CONTEXT extends Context,
|
|
20
|
+
CONTEXT extends Context | unknown | never,
|
|
92
21
|
> = NeverOptional<
|
|
93
22
|
OUTPUT,
|
|
94
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
|
+
|
|
95
31
|
/**
|
|
96
32
|
* An async function that is called with the arguments from the tool call and produces a result.
|
|
97
33
|
* If not provided, the tool will not be executed automatically.
|
|
@@ -100,10 +36,13 @@ type ToolOutputProperties<
|
|
|
100
36
|
* @options.abortSignal is a signal that can be used to abort the tool call.
|
|
101
37
|
*/
|
|
102
38
|
execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
103
|
-
|
|
104
|
-
outputSchema?: FlexibleSchema<OUTPUT>;
|
|
105
39
|
}
|
|
106
40
|
| {
|
|
41
|
+
/**
|
|
42
|
+
* The schema of the output that the tool produces.
|
|
43
|
+
*
|
|
44
|
+
* Required when no execute function is provided.
|
|
45
|
+
*/
|
|
107
46
|
outputSchema: FlexibleSchema<OUTPUT>;
|
|
108
47
|
|
|
109
48
|
execute?: never;
|
|
@@ -111,23 +50,13 @@ type ToolOutputProperties<
|
|
|
111
50
|
>;
|
|
112
51
|
|
|
113
52
|
/**
|
|
114
|
-
*
|
|
115
|
-
* This enables the language model to generate the input.
|
|
116
|
-
*
|
|
117
|
-
* 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.
|
|
118
54
|
*/
|
|
119
|
-
|
|
55
|
+
type BaseTool<
|
|
120
56
|
INPUT extends JSONValue | unknown | never = any,
|
|
121
57
|
OUTPUT extends JSONValue | unknown | never = any,
|
|
122
|
-
CONTEXT extends Context =
|
|
58
|
+
CONTEXT extends Context | unknown | never = any,
|
|
123
59
|
> = {
|
|
124
|
-
/**
|
|
125
|
-
* An optional description of what the tool does.
|
|
126
|
-
* Will be used by the language model to decide whether to use the tool.
|
|
127
|
-
* Not used for provider-defined tools.
|
|
128
|
-
*/
|
|
129
|
-
description?: string;
|
|
130
|
-
|
|
131
60
|
/**
|
|
132
61
|
* An optional title of the tool.
|
|
133
62
|
*/
|
|
@@ -140,6 +69,17 @@ export type Tool<
|
|
|
140
69
|
*/
|
|
141
70
|
providerOptions?: ProviderOptions;
|
|
142
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
|
+
|
|
143
83
|
/**
|
|
144
84
|
* The schema of the input that the tool expects.
|
|
145
85
|
* The language model will use this to generate the input.
|
|
@@ -149,12 +89,6 @@ export type Tool<
|
|
|
149
89
|
*/
|
|
150
90
|
inputSchema: FlexibleSchema<INPUT>;
|
|
151
91
|
|
|
152
|
-
/**
|
|
153
|
-
* An optional list of input examples that show the language
|
|
154
|
-
* model what the input should look like.
|
|
155
|
-
*/
|
|
156
|
-
inputExamples?: Array<{ input: NoInfer<INPUT> }>;
|
|
157
|
-
|
|
158
92
|
/**
|
|
159
93
|
* An optional schema describing the context that the tool expects.
|
|
160
94
|
*
|
|
@@ -162,8 +96,16 @@ export type Tool<
|
|
|
162
96
|
*/
|
|
163
97
|
contextSchema?: FlexibleSchema<CONTEXT>;
|
|
164
98
|
|
|
99
|
+
/**
|
|
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.
|
|
102
|
+
*/
|
|
103
|
+
sensitiveContext?: SensitiveContext<CONTEXT>;
|
|
104
|
+
|
|
165
105
|
/**
|
|
166
106
|
* Whether the tool needs approval before it can be executed.
|
|
107
|
+
*
|
|
108
|
+
* @deprecated Tool approval is handled on a `generateText` / `streamText` level now.
|
|
167
109
|
*/
|
|
168
110
|
needsApproval?:
|
|
169
111
|
| boolean
|
|
@@ -172,15 +114,6 @@ export type Tool<
|
|
|
172
114
|
NoInfer<CONTEXT>
|
|
173
115
|
>;
|
|
174
116
|
|
|
175
|
-
/**
|
|
176
|
-
* Strict mode setting for the tool.
|
|
177
|
-
*
|
|
178
|
-
* Providers that support strict mode will use this setting to determine
|
|
179
|
-
* how the input should be generated. Strict mode will always produce
|
|
180
|
-
* valid inputs, but it might limit what input schemas are supported.
|
|
181
|
-
*/
|
|
182
|
-
strict?: boolean;
|
|
183
|
-
|
|
184
117
|
/**
|
|
185
118
|
* Optional function that is called when the argument streaming starts.
|
|
186
119
|
* Only called when the tool is used in a streaming context.
|
|
@@ -208,83 +141,196 @@ export type Tool<
|
|
|
208
141
|
input: [INPUT] extends [never] ? unknown : INPUT;
|
|
209
142
|
} & ToolExecutionOptions<NoInfer<CONTEXT>>,
|
|
210
143
|
) => void | PromiseLike<void>;
|
|
211
|
-
|
|
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: {
|
|
212
153
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* If not provided, the tool result will be sent as a JSON object.
|
|
216
|
-
*
|
|
217
|
-
* 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).
|
|
154
|
+
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
218
155
|
*/
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
222
|
-
*/
|
|
223
|
-
toolCallId: string;
|
|
156
|
+
toolCallId: string;
|
|
224
157
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
158
|
+
/**
|
|
159
|
+
* The input of the tool call.
|
|
160
|
+
*/
|
|
161
|
+
input: [INPUT] extends [never] ? unknown : INPUT;
|
|
229
162
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
163
|
+
/**
|
|
164
|
+
* The output of the tool call.
|
|
165
|
+
*/
|
|
166
|
+
output: 0 extends 1 & OUTPUT
|
|
167
|
+
? any
|
|
168
|
+
: [OUTPUT] extends [never]
|
|
234
169
|
? any
|
|
235
|
-
:
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
170
|
+
: NoInfer<OUTPUT>;
|
|
171
|
+
}) => ToolResultOutput | PromiseLike<ToolResultOutput>;
|
|
172
|
+
} & ToolOutputProperties<INPUT, OUTPUT, NoInfer<CONTEXT>>;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Common properties shared by function-style tools.
|
|
176
|
+
*/
|
|
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
|
+
};
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Tool with user-defined input and output schemas that is executed by the AI SDK.
|
|
212
|
+
*/
|
|
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
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Tool that is defined at runtime.
|
|
223
|
+
* The types of input and output are not known at development time.
|
|
224
|
+
*
|
|
225
|
+
* For example, MCP tools that are not known at development time.
|
|
226
|
+
*/
|
|
227
|
+
export type DynamicTool<
|
|
228
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
229
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
230
|
+
CONTEXT extends Context | unknown | never = any,
|
|
231
|
+
> = BaseFunctionTool<INPUT, OUTPUT, CONTEXT> & {
|
|
232
|
+
type: 'dynamic';
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Common properties shared by provider tools.
|
|
237
|
+
*/
|
|
238
|
+
type BaseProviderTool<
|
|
239
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
240
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
241
|
+
CONTEXT extends Context | unknown | never = any,
|
|
242
|
+
> = BaseTool<INPUT, OUTPUT, CONTEXT> & {
|
|
243
|
+
type: 'provider';
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
|
|
247
|
+
*/
|
|
248
|
+
id: `${string}.${string}`;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
|
|
252
|
+
*/
|
|
253
|
+
args: Record<string, unknown>;
|
|
254
|
+
|
|
255
|
+
// make all properties available to improve usage dx
|
|
256
|
+
description?: never;
|
|
257
|
+
strict?: never;
|
|
258
|
+
inputExamples?: never;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Tool with provider-defined input and output schemas that is executed by the
|
|
263
|
+
* user.
|
|
264
|
+
*
|
|
265
|
+
* For example, shell tools that are executed in a local shell, but have provider-defined input and output schemas.
|
|
266
|
+
*/
|
|
267
|
+
export type ProviderDefinedTool<
|
|
268
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
269
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
270
|
+
CONTEXT extends Context | unknown | never = any,
|
|
271
|
+
> = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
|
|
272
|
+
/**
|
|
273
|
+
* Flag that indicates whether the tool is executed by the provider.
|
|
274
|
+
*/
|
|
275
|
+
isProviderExecuted: false;
|
|
276
|
+
|
|
277
|
+
// make all properties available to improve usage dx
|
|
278
|
+
supportsDeferredResults?: never;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Tool with provider-defined input and output schemas that is executed by the
|
|
283
|
+
* provider.
|
|
284
|
+
*
|
|
285
|
+
* For example, web search tools and code execution tools that are executed by the provider itself.
|
|
286
|
+
*/
|
|
287
|
+
export type ProviderExecutedTool<
|
|
288
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
289
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
290
|
+
CONTEXT extends Context | unknown | never = any,
|
|
291
|
+
> = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
|
|
292
|
+
/**
|
|
293
|
+
* Flag that indicates whether the tool is executed by the provider.
|
|
294
|
+
*/
|
|
295
|
+
isProviderExecuted: true;
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Whether this provider-executed tool supports deferred results.
|
|
299
|
+
*
|
|
300
|
+
* When true, the tool result may not be returned in the same turn as the
|
|
301
|
+
* tool call (e.g., when using programmatic tool calling where a server tool
|
|
302
|
+
* triggers a client-executed tool, and the server tool's result is deferred
|
|
303
|
+
* until the client tool is resolved).
|
|
304
|
+
*
|
|
305
|
+
* This flag allows the AI SDK to handle tool results that arrive without
|
|
306
|
+
* a matching tool call in the current response.
|
|
307
|
+
*
|
|
308
|
+
* @default false
|
|
309
|
+
*/
|
|
310
|
+
supportsDeferredResults?: boolean;
|
|
311
|
+
};
|
|
285
312
|
|
|
286
313
|
/**
|
|
287
|
-
*
|
|
314
|
+
* A tool can either be user-defined or provider-defined.
|
|
315
|
+
*
|
|
316
|
+
* It contains the schemas and metadata needed for the language model to call
|
|
317
|
+
* the tool and can include an execute function for tools that are executed by
|
|
318
|
+
* the AI SDK.
|
|
319
|
+
*/
|
|
320
|
+
export type Tool<
|
|
321
|
+
INPUT extends JSONValue | unknown | never = any,
|
|
322
|
+
OUTPUT extends JSONValue | unknown | never = any,
|
|
323
|
+
CONTEXT extends Context | unknown | never = any,
|
|
324
|
+
> =
|
|
325
|
+
| FunctionTool<INPUT, OUTPUT, CONTEXT>
|
|
326
|
+
| DynamicTool<INPUT, OUTPUT, CONTEXT>
|
|
327
|
+
| ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>
|
|
328
|
+
| ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Infer the tool type from a tool object.
|
|
332
|
+
*
|
|
333
|
+
* This is useful for type inference when working with tool objects.
|
|
288
334
|
*/
|
|
289
335
|
// Note: overload order is important for auto-completion
|
|
290
336
|
export function tool<INPUT, OUTPUT, CONTEXT extends Context>(
|
|
@@ -304,43 +350,10 @@ export function tool(tool: any): any {
|
|
|
304
350
|
}
|
|
305
351
|
|
|
306
352
|
/**
|
|
307
|
-
*
|
|
353
|
+
* Define a dynamic tool.
|
|
308
354
|
*/
|
|
309
|
-
export function dynamicTool(
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
inputSchema: FlexibleSchema<unknown>;
|
|
314
|
-
execute: ToolExecuteFunction<unknown, unknown, Context>;
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
318
|
-
*
|
|
319
|
-
* If not provided, the tool result will be sent as a JSON object.
|
|
320
|
-
*/
|
|
321
|
-
toModelOutput?: (options: {
|
|
322
|
-
/**
|
|
323
|
-
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
324
|
-
*/
|
|
325
|
-
toolCallId: string;
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* The input of the tool call.
|
|
329
|
-
*/
|
|
330
|
-
input: unknown;
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* The output of the tool call.
|
|
334
|
-
*/
|
|
335
|
-
output: unknown;
|
|
336
|
-
}) => ToolResultOutput | PromiseLike<ToolResultOutput>;
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Whether the tool needs approval before it can be executed.
|
|
340
|
-
*/
|
|
341
|
-
needsApproval?: boolean | ToolNeedsApprovalFunction<unknown, Context>;
|
|
342
|
-
}): Tool<unknown, unknown, Context> & {
|
|
343
|
-
type: 'dynamic';
|
|
344
|
-
} {
|
|
345
|
-
return { ...tool, type: 'dynamic' };
|
|
355
|
+
export function dynamicTool(
|
|
356
|
+
tool: Omit<DynamicTool<unknown, unknown, Context>, 'type'>,
|
|
357
|
+
): DynamicTool<unknown, unknown, Context> {
|
|
358
|
+
return { ...tool, type: 'dynamic' } as DynamicTool<unknown, unknown, Context>;
|
|
346
359
|
}
|
|
@@ -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.
|
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.
|