@ai-sdk/provider-utils 5.0.0-beta.27 → 5.0.0-beta.29

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.
Files changed (82) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/index.d.ts +967 -843
  3. package/dist/index.js +340 -4
  4. package/dist/index.js.map +1 -1
  5. package/package.json +4 -3
  6. package/src/add-additional-properties-to-json-schema.ts +1 -1
  7. package/src/convert-image-model-file-to-data-uri.ts +1 -1
  8. package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
  9. package/src/create-tool-name-mapping.ts +1 -1
  10. package/src/detect-media-type.ts +312 -0
  11. package/src/get-from-api.ts +2 -2
  12. package/src/index.ts +8 -0
  13. package/src/inject-json-instruction.ts +1 -1
  14. package/src/is-buffer.ts +9 -0
  15. package/src/is-json-serializable.ts +1 -1
  16. package/src/is-provider-reference.ts +11 -8
  17. package/src/is-url-supported.ts +17 -2
  18. package/src/map-reasoning-to-provider.ts +4 -1
  19. package/src/parse-json-event-stream.ts +3 -3
  20. package/src/parse-json.ts +2 -2
  21. package/src/parse-provider-options.ts +1 -1
  22. package/src/post-to-api.ts +2 -2
  23. package/src/provider-defined-tool-factory.ts +10 -9
  24. package/src/provider-executed-tool-factory.ts +6 -7
  25. package/src/resolve-full-media-type.ts +49 -0
  26. package/src/resolve-provider-reference.ts +1 -2
  27. package/src/resolve.ts +1 -1
  28. package/src/response-handler.ts +2 -2
  29. package/src/schema.ts +6 -3
  30. package/src/serialize-model-options.ts +2 -2
  31. package/src/streaming-tool-call-tracker.ts +2 -2
  32. package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
  33. package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
  34. package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
  35. package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
  36. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
  37. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
  38. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
  39. package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +3 -3
  40. package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
  41. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
  42. package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
  43. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +4 -4
  44. package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
  45. package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
  46. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
  47. package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
  48. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
  49. package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
  50. package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
  51. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
  52. package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +4 -4
  53. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
  54. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
  55. package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +7 -8
  56. package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
  57. package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
  58. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
  59. package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
  60. package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
  61. package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
  62. package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
  63. package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
  64. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
  65. package/src/types/assistant-model-message.ts +3 -3
  66. package/src/types/content-part.ts +37 -14
  67. package/src/types/executable-tool.ts +1 -1
  68. package/src/types/execute-tool.ts +6 -5
  69. package/src/types/file-data.ts +48 -0
  70. package/src/types/index.ts +16 -3
  71. package/src/types/infer-tool-context.ts +1 -1
  72. package/src/types/model-message.ts +4 -4
  73. package/src/types/never-optional.ts +7 -0
  74. package/src/types/provider-options.ts +1 -1
  75. package/src/types/provider-reference.ts +1 -1
  76. package/src/types/system-model-message.ts +1 -1
  77. package/src/types/tool-execute-function.ts +50 -0
  78. package/src/types/tool-model-message.ts +3 -3
  79. package/src/types/tool-needs-approval-function.ts +39 -0
  80. package/src/types/tool.ts +200 -271
  81. package/src/types/user-model-message.ts +2 -2
  82. package/src/validate-types.ts +5 -3
package/src/types/tool.ts CHANGED
@@ -1,106 +1,18 @@
1
- import { JSONValue } from '@ai-sdk/provider';
2
- import { FlexibleSchema } from '../schema';
3
- import { ToolResultOutput } from './content-part';
4
- import { Context } from './context';
5
- import { ModelMessage } from './model-message';
6
- import { ProviderOptions } from './provider-options';
7
- import { SensitiveContext } from './sensitive-context';
1
+ import type { JSONValue } 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';
8
13
 
9
14
  /**
10
- * Additional options that are sent into each tool execution.
11
- */
12
- export interface ToolExecutionOptions<
13
- CONTEXT extends Context | unknown | never,
14
- > {
15
- /**
16
- * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
17
- */
18
- toolCallId: string;
19
-
20
- /**
21
- * Messages that were sent to the language model to initiate the response that contained the tool call.
22
- * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
23
- */
24
- messages: ModelMessage[];
25
-
26
- /**
27
- * An optional abort signal that indicates that the overall operation should be aborted.
28
- */
29
- abortSignal?: AbortSignal;
30
-
31
- /**
32
- * Tool context as defined by the tool's context schema.
33
- * The tool context is specific to the tool and is passed to the tool execution.
34
- *
35
- * Treat the context object as immutable inside tools.
36
- * Mutating the context object can lead to race conditions and unexpected results
37
- * when tools are called in parallel.
38
- *
39
- * If you need to mutate the context, analyze the tool calls and results
40
- * in `prepareStep` and update it there.
41
- */
42
- context: CONTEXT;
43
- }
44
-
45
- /**
46
- * Function that is called to determine if the tool needs approval before it can be executed.
47
- *
48
- * @deprecated Tool approval is handled on a `generateText` / `streamText` level now.
49
- */
50
- export type ToolNeedsApprovalFunction<
51
- INPUT,
52
- CONTEXT extends Context | unknown | never,
53
- > = (
54
- input: INPUT,
55
- options: {
56
- /**
57
- * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
58
- */
59
- toolCallId: string;
60
-
61
- /**
62
- * Messages that were sent to the language model to initiate the response that contained the tool call.
63
- * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
64
- */
65
- messages: ModelMessage[];
66
-
67
- /**
68
- * Tool context as defined by the tool's context schema.
69
- * The tool context is specific to the tool and is passed to the tool execution.
70
- *
71
- * Treat the context object as immutable inside tools.
72
- * Mutating the context object can lead to race conditions and unexpected results
73
- * when tools are called in parallel.
74
- *
75
- * If you need to mutate the context, analyze the tool calls and results
76
- * in `prepareStep` and update it there.
77
- */
78
- context: CONTEXT;
79
- },
80
- ) => boolean | PromiseLike<boolean>;
81
-
82
- /**
83
- * Function that executes the tool and returns either a single result or a stream of results.
84
- */
85
- export type ToolExecuteFunction<
86
- INPUT,
87
- OUTPUT,
88
- CONTEXT extends Context | unknown | never,
89
- > = (
90
- input: INPUT,
91
- options: ToolExecutionOptions<CONTEXT>,
92
- ) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
93
-
94
- // 0 extends 1 & N checks for any
95
- // [N] extends [never] checks for never
96
- type NeverOptional<N, T> = 0 extends 1 & N
97
- ? Partial<T>
98
- : [N] extends [never]
99
- ? Partial<Record<keyof T, undefined>>
100
- : T;
101
-
102
- /**
103
- * Helper type to determine the output properties of a tool.
15
+ * Helper type to determine the outputSchema and execute function properties of a tool.
104
16
  */
105
17
  type ToolOutputProperties<
106
18
  INPUT,
@@ -109,6 +21,13 @@ type ToolOutputProperties<
109
21
  > = NeverOptional<
110
22
  OUTPUT,
111
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
+
112
31
  /**
113
32
  * An async function that is called with the arguments from the tool call and produces a result.
114
33
  * If not provided, the tool will not be executed automatically.
@@ -117,15 +36,12 @@ type ToolOutputProperties<
117
36
  * @options.abortSignal is a signal that can be used to abort the tool call.
118
37
  */
119
38
  execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
120
-
121
- /**
122
- * The schema of the output that the tool produces.
123
- */
124
- outputSchema?: FlexibleSchema<OUTPUT>;
125
39
  }
126
40
  | {
127
41
  /**
128
42
  * The schema of the output that the tool produces.
43
+ *
44
+ * Required when no execute function is provided.
129
45
  */
130
46
  outputSchema: FlexibleSchema<OUTPUT>;
131
47
 
@@ -134,23 +50,13 @@ type ToolOutputProperties<
134
50
  >;
135
51
 
136
52
  /**
137
- * A tool contains the description and the schema of the input that the tool expects.
138
- * This enables the language model to generate the input.
139
- *
140
- * 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.
141
54
  */
142
- export type Tool<
55
+ type BaseTool<
143
56
  INPUT extends JSONValue | unknown | never = any,
144
57
  OUTPUT extends JSONValue | unknown | never = any,
145
58
  CONTEXT extends Context | unknown | never = any,
146
59
  > = {
147
- /**
148
- * An optional description of what the tool does.
149
- * Will be used by the language model to decide whether to use the tool.
150
- * Not used for provider-defined tools.
151
- */
152
- description?: string;
153
-
154
60
  /**
155
61
  * An optional title of the tool.
156
62
  */
@@ -172,12 +78,6 @@ export type Tool<
172
78
  */
173
79
  inputSchema: FlexibleSchema<INPUT>;
174
80
 
175
- /**
176
- * An optional list of input examples that show the language
177
- * model what the input should look like.
178
- */
179
- inputExamples?: Array<{ input: NoInfer<INPUT> }>;
180
-
181
81
  /**
182
82
  * An optional schema describing the context that the tool expects.
183
83
  *
@@ -203,15 +103,6 @@ export type Tool<
203
103
  NoInfer<CONTEXT>
204
104
  >;
205
105
 
206
- /**
207
- * Strict mode setting for the tool.
208
- *
209
- * Providers that support strict mode will use this setting to determine
210
- * how the input should be generated. Strict mode will always produce
211
- * valid inputs, but it might limit what input schemas are supported.
212
- */
213
- strict?: boolean;
214
-
215
106
  /**
216
107
  * Optional function that is called when the argument streaming starts.
217
108
  * Only called when the tool is used in a streaming context.
@@ -239,175 +130,213 @@ export type Tool<
239
130
  input: [INPUT] extends [never] ? unknown : INPUT;
240
131
  } & ToolExecutionOptions<NoInfer<CONTEXT>>,
241
132
  ) => void | PromiseLike<void>;
242
- } & ToolOutputProperties<INPUT, OUTPUT, NoInfer<CONTEXT>> & {
133
+
134
+ /**
135
+ * Optional conversion function that maps the tool result to an output that can be used by the language model.
136
+ *
137
+ * If not provided, the tool result will be sent as a JSON object.
138
+ *
139
+ * 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).
140
+ */
141
+ toModelOutput?: (options: {
243
142
  /**
244
- * Optional conversion function that maps the tool result to an output that can be used by the language model.
245
- *
246
- * If not provided, the tool result will be sent as a JSON object.
247
- *
248
- * 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).
143
+ * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
249
144
  */
250
- toModelOutput?: (options: {
251
- /**
252
- * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
253
- */
254
- toolCallId: string;
145
+ toolCallId: string;
255
146
 
256
- /**
257
- * The input of the tool call.
258
- */
259
- input: [INPUT] extends [never] ? unknown : INPUT;
147
+ /**
148
+ * The input of the tool call.
149
+ */
150
+ input: [INPUT] extends [never] ? unknown : INPUT;
260
151
 
261
- /**
262
- * The output of the tool call.
263
- */
264
- output: 0 extends 1 & OUTPUT
152
+ /**
153
+ * The output of the tool call.
154
+ */
155
+ output: 0 extends 1 & OUTPUT
156
+ ? any
157
+ : [OUTPUT] extends [never]
265
158
  ? any
266
- : [OUTPUT] extends [never]
267
- ? any
268
- : NoInfer<OUTPUT>;
269
- }) => ToolResultOutput | PromiseLike<ToolResultOutput>;
270
- } & (
271
- | {
272
- /**
273
- * Tool with user-defined input and output schemas.
274
- */
275
- type?: undefined | 'function';
276
- }
277
- | {
278
- /**
279
- * Tool that is defined at runtime (e.g. an MCP tool).
280
- * The types of input and output are not known at development time.
281
- */
282
- type: 'dynamic';
283
- }
284
- | {
285
- /**
286
- * Tool with provider-defined input and output schemas.
287
- */
288
- type: 'provider';
289
-
290
- /**
291
- * The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
292
- */
293
- id: `${string}.${string}`;
294
-
295
- /**
296
- * Flag that indicates whether the tool is executed by the provider.
297
- */
298
- isProviderExecuted: boolean;
299
-
300
- /**
301
- * The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
302
- */
303
- args: Record<string, unknown>;
304
-
305
- /**
306
- * Whether this provider-executed tool supports deferred results.
307
- *
308
- * When true, the tool result may not be returned in the same turn as the
309
- * tool call (e.g., when using programmatic tool calling where a server tool
310
- * triggers a client-executed tool, and the server tool's result is deferred
311
- * until the client tool is resolved).
312
- *
313
- * This flag allows the AI SDK to handle tool results that arrive without
314
- * a matching tool call in the current response.
315
- *
316
- * @default false
317
- */
318
- supportsDeferredResults?: boolean;
319
- }
320
- );
321
-
322
- /**
323
- * Helper function for inferring the execute args of a tool.
324
- */
325
- // Note: overload order is important for auto-completion
326
- export function tool<INPUT, OUTPUT, CONTEXT extends Context>(
327
- tool: Tool<INPUT, OUTPUT, CONTEXT>,
328
- ): Tool<INPUT, OUTPUT, CONTEXT>;
329
- export function tool<INPUT, CONTEXT extends Context>(
330
- tool: Tool<INPUT, never, CONTEXT>,
331
- ): Tool<INPUT, never, CONTEXT>;
332
- export function tool<OUTPUT, CONTEXT extends Context>(
333
- tool: Tool<never, OUTPUT, CONTEXT>,
334
- ): Tool<never, OUTPUT, CONTEXT>;
335
- export function tool<CONTEXT extends Context>(
336
- tool: Tool<never, never, CONTEXT>,
337
- ): Tool<never, never, CONTEXT>;
338
- export function tool(tool: any): any {
339
- return tool;
340
- }
159
+ : NoInfer<OUTPUT>;
160
+ }) => ToolResultOutput | PromiseLike<ToolResultOutput>;
161
+ } & ToolOutputProperties<INPUT, OUTPUT, NoInfer<CONTEXT>>;
341
162
 
342
163
  /**
343
- * Defines a dynamic tool.
164
+ * Common properties shared by function-style tools.
344
165
  */
345
- export function dynamicTool(tool: {
166
+ type BaseFunctionTool<
167
+ INPUT extends JSONValue | unknown | never = any,
168
+ OUTPUT extends JSONValue | unknown | never = any,
169
+ CONTEXT extends Context | unknown | never = any,
170
+ > = BaseTool<INPUT, OUTPUT, CONTEXT> & {
346
171
  /**
347
172
  * An optional description of what the tool does.
348
173
  * Will be used by the language model to decide whether to use the tool.
349
- * Not used for provider-defined tools.
350
174
  */
351
175
  description?: string;
352
176
 
353
177
  /**
354
- * An optional title of the tool.
178
+ * Strict mode setting for the tool.
179
+ *
180
+ * Providers that support strict mode will use this setting to determine
181
+ * how the input should be generated. Strict mode will always produce
182
+ * valid inputs, but it might limit what input schemas are supported.
355
183
  */
356
- title?: string;
184
+ strict?: boolean;
357
185
 
358
186
  /**
359
- * Additional provider-specific metadata. They are passed through
360
- * to the provider from the AI SDK and enable provider-specific
361
- * functionality that can be fully encapsulated in the provider.
187
+ * An optional list of input examples that show the language
188
+ * model what the input should look like.
362
189
  */
363
- providerOptions?: ProviderOptions;
190
+ inputExamples?: Array<{ input: NoInfer<INPUT> }>;
191
+
192
+ // make all properties available to improve usage dx
193
+ id?: never;
194
+ isProviderExecuted?: never;
195
+ args?: never;
196
+ supportsDeferredResults?: never;
197
+ };
198
+
199
+ /**
200
+ * Tool with user-defined input and output schemas.
201
+ */
202
+ export type FunctionTool<
203
+ INPUT extends JSONValue | unknown | never = any,
204
+ OUTPUT extends JSONValue | unknown | never = any,
205
+ CONTEXT extends Context | unknown | never = any,
206
+ > = BaseFunctionTool<INPUT, OUTPUT, CONTEXT> & {
207
+ type?: undefined | 'function';
208
+ };
209
+
210
+ /**
211
+ * Tool that is defined at runtime (e.g. an MCP tool).
212
+ * The types of input and output are not known at development time.
213
+ */
214
+ export type DynamicTool<
215
+ INPUT extends JSONValue | unknown | never = any,
216
+ OUTPUT extends JSONValue | unknown | never = any,
217
+ CONTEXT extends Context | unknown | never = any,
218
+ > = BaseFunctionTool<INPUT, OUTPUT, CONTEXT> & {
219
+ type: 'dynamic';
220
+ };
221
+
222
+ /**
223
+ * Common properties shared by provider tools.
224
+ */
225
+ type BaseProviderTool<
226
+ INPUT extends JSONValue | unknown | never = any,
227
+ OUTPUT extends JSONValue | unknown | never = any,
228
+ CONTEXT extends Context | unknown | never = any,
229
+ > = BaseTool<INPUT, OUTPUT, CONTEXT> & {
230
+ type: 'provider';
364
231
 
365
232
  /**
366
- * The schema of the input that the tool expects.
367
- * The language model will use this to generate the input.
368
- * It is also used to validate the output of the language model.
369
- *
370
- * You can use descriptions on the schema properties to make the input understandable for the language model.
233
+ * The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
371
234
  */
372
- inputSchema: FlexibleSchema<unknown>;
235
+ id: `${string}.${string}`;
373
236
 
374
237
  /**
375
- * An async function that is called with the arguments from the tool call and produces a result.
376
- * If not provided, the tool will not be executed automatically.
377
- *
378
- * @args is the input of the tool call.
379
- * @options.abortSignal is a signal that can be used to abort the tool call.
238
+ * The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
380
239
  */
381
- execute: ToolExecuteFunction<unknown, unknown, Context>;
240
+ args: Record<string, unknown>;
241
+
242
+ // make all properties available to improve usage dx
243
+ description?: never;
244
+ strict?: never;
245
+ inputExamples?: never;
246
+ };
382
247
 
248
+ /**
249
+ * Tool with provider-defined input and output schemas that is executed by the
250
+ * user.
251
+ */
252
+ export type ProviderDefinedTool<
253
+ INPUT extends JSONValue | unknown | never = any,
254
+ OUTPUT extends JSONValue | unknown | never = any,
255
+ CONTEXT extends Context | unknown | never = any,
256
+ > = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
383
257
  /**
384
- * Optional conversion function that maps the tool result to an output that can be used by the language model.
385
- *
386
- * If not provided, the tool result will be sent as a JSON object.
258
+ * Flag that indicates whether the tool is executed by the provider.
387
259
  */
388
- toModelOutput?: (options: {
389
- /**
390
- * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
391
- */
392
- toolCallId: string;
260
+ isProviderExecuted: false;
393
261
 
394
- /**
395
- * The input of the tool call.
396
- */
397
- input: unknown;
262
+ // make all properties available to improve usage dx
263
+ supportsDeferredResults?: never;
264
+ };
398
265
 
399
- /**
400
- * The output of the tool call.
401
- */
402
- output: unknown;
403
- }) => ToolResultOutput | PromiseLike<ToolResultOutput>;
266
+ /**
267
+ * Tool with provider-defined input and output schemas that is executed by the
268
+ * provider.
269
+ */
270
+ export type ProviderExecutedTool<
271
+ INPUT extends JSONValue | unknown | never = any,
272
+ OUTPUT extends JSONValue | unknown | never = any,
273
+ CONTEXT extends Context | unknown | never = any,
274
+ > = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
275
+ /**
276
+ * Flag that indicates whether the tool is executed by the provider.
277
+ */
278
+ isProviderExecuted: true;
404
279
 
405
280
  /**
406
- * Whether the tool needs approval before it can be executed.
281
+ * Whether this provider-executed tool supports deferred results.
282
+ *
283
+ * When true, the tool result may not be returned in the same turn as the
284
+ * tool call (e.g., when using programmatic tool calling where a server tool
285
+ * triggers a client-executed tool, and the server tool's result is deferred
286
+ * until the client tool is resolved).
287
+ *
288
+ * This flag allows the AI SDK to handle tool results that arrive without
289
+ * a matching tool call in the current response.
290
+ *
291
+ * @default false
407
292
  */
408
- needsApproval?: boolean | ToolNeedsApprovalFunction<unknown, Context>;
409
- }): Tool<unknown, unknown, Context> & {
410
- type: 'dynamic';
411
- } {
412
- return { ...tool, type: 'dynamic' };
293
+ supportsDeferredResults?: boolean;
294
+ };
295
+
296
+ /**
297
+ * A tool can either be user-defined or provider-defined.
298
+ *
299
+ * It contains the schemas and metadata needed for the language model to call
300
+ * the tool and can include an execute function for tools that are executed by
301
+ * the AI SDK.
302
+ */
303
+ export type Tool<
304
+ INPUT extends JSONValue | unknown | never = any,
305
+ OUTPUT extends JSONValue | unknown | never = any,
306
+ CONTEXT extends Context | unknown | never = any,
307
+ > =
308
+ | FunctionTool<INPUT, OUTPUT, CONTEXT>
309
+ | DynamicTool<INPUT, OUTPUT, CONTEXT>
310
+ | ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>
311
+ | ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
312
+
313
+ /**
314
+ * Infer the tool type from a tool object.
315
+ *
316
+ * This is useful for type inference when working with tool objects.
317
+ */
318
+ // Note: overload order is important for auto-completion
319
+ export function tool<INPUT, OUTPUT, CONTEXT extends Context>(
320
+ tool: Tool<INPUT, OUTPUT, CONTEXT>,
321
+ ): Tool<INPUT, OUTPUT, CONTEXT>;
322
+ export function tool<INPUT, CONTEXT extends Context>(
323
+ tool: Tool<INPUT, never, CONTEXT>,
324
+ ): Tool<INPUT, never, CONTEXT>;
325
+ export function tool<OUTPUT, CONTEXT extends Context>(
326
+ tool: Tool<never, OUTPUT, CONTEXT>,
327
+ ): Tool<never, OUTPUT, CONTEXT>;
328
+ export function tool<CONTEXT extends Context>(
329
+ tool: Tool<never, never, CONTEXT>,
330
+ ): Tool<never, never, CONTEXT>;
331
+ export function tool(tool: any): any {
332
+ return tool;
333
+ }
334
+
335
+ /**
336
+ * Define a dynamic tool.
337
+ */
338
+ export function dynamicTool(
339
+ tool: Omit<DynamicTool<unknown, unknown, Context>, 'type'>,
340
+ ): DynamicTool<unknown, unknown, Context> {
341
+ return { ...tool, type: 'dynamic' } as DynamicTool<unknown, unknown, Context>;
413
342
  }
@@ -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.
@@ -1,6 +1,8 @@
1
- import { TypeValidationContext, TypeValidationError } from '@ai-sdk/provider';
2
- import { FlexibleSchema, asSchema } from './schema';
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.