@ai-sdk/provider-utils 5.0.0-beta.4 → 5.0.0-beta.49

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 (117) hide show
  1. package/CHANGELOG.md +373 -11
  2. package/dist/index.d.ts +1460 -553
  3. package/dist/index.js +1044 -361
  4. package/dist/index.js.map +1 -1
  5. package/dist/test/index.d.ts +2 -1
  6. package/dist/test/index.js +18 -37
  7. package/dist/test/index.js.map +1 -1
  8. package/package.json +16 -16
  9. package/src/add-additional-properties-to-json-schema.ts +1 -1
  10. package/src/as-array.ts +12 -0
  11. package/src/cancel-response-body.ts +19 -0
  12. package/src/convert-image-model-file-to-data-uri.ts +1 -1
  13. package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
  14. package/src/create-tool-name-mapping.ts +1 -1
  15. package/src/detect-media-type.ts +312 -0
  16. package/src/download-blob.ts +8 -9
  17. package/src/extract-lines.ts +31 -0
  18. package/src/fetch-with-validated-redirects.ts +87 -0
  19. package/src/filter-nullable.ts +11 -0
  20. package/src/get-error-message.ts +1 -15
  21. package/src/get-from-api.ts +2 -2
  22. package/src/has-required-key.ts +6 -0
  23. package/src/index.ts +47 -12
  24. package/src/inject-json-instruction.ts +1 -1
  25. package/src/is-browser-runtime.ts +13 -0
  26. package/src/is-buffer.ts +9 -0
  27. package/src/is-json-serializable.ts +29 -0
  28. package/src/is-provider-reference.ts +21 -0
  29. package/src/is-same-origin.ts +19 -0
  30. package/src/is-url-supported.ts +17 -2
  31. package/src/load-api-key.ts +1 -1
  32. package/src/load-setting.ts +1 -1
  33. package/src/map-reasoning-to-provider.ts +108 -0
  34. package/src/maybe-promise-like.ts +3 -0
  35. package/src/parse-json-event-stream.ts +3 -3
  36. package/src/parse-json.ts +3 -3
  37. package/src/parse-provider-options.ts +1 -1
  38. package/src/post-to-api.ts +4 -4
  39. package/src/provider-defined-tool-factory.ts +129 -0
  40. package/src/provider-executed-tool-factory.ts +69 -0
  41. package/src/read-response-with-size-limit.ts +4 -0
  42. package/src/resolve-full-media-type.ts +49 -0
  43. package/src/resolve-provider-reference.ts +26 -0
  44. package/src/resolve.ts +16 -1
  45. package/src/response-handler.ts +3 -3
  46. package/src/schema.ts +11 -4
  47. package/src/secure-json-parse.ts +1 -1
  48. package/src/serialize-model-options.ts +63 -0
  49. package/src/streaming-tool-call-tracker.ts +241 -0
  50. package/src/test/convert-response-stream-to-array.ts +1 -1
  51. package/src/test/is-node-version.ts +22 -1
  52. package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
  53. package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
  54. package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
  55. package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
  56. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
  57. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
  58. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
  59. package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +4 -4
  60. package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
  61. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
  62. package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
  63. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +5 -5
  64. package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
  65. package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
  66. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
  67. package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
  68. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
  69. package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
  70. package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
  71. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
  72. package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +10 -8
  73. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
  74. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
  75. package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +9 -10
  76. package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
  77. package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
  78. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
  79. package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
  80. package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
  81. package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
  82. package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
  83. package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
  84. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
  85. package/src/types/assistant-model-message.ts +5 -3
  86. package/src/types/content-part.ts +158 -19
  87. package/src/types/context.ts +4 -0
  88. package/src/types/executable-tool.ts +17 -0
  89. package/src/types/execute-tool.ts +29 -9
  90. package/src/types/file-data.ts +48 -0
  91. package/src/types/index.ts +29 -11
  92. package/src/types/infer-tool-context.ts +41 -0
  93. package/src/types/infer-tool-input.ts +7 -0
  94. package/src/types/infer-tool-output.ts +7 -0
  95. package/src/types/infer-tool-set-context.ts +44 -0
  96. package/src/types/model-message.ts +4 -4
  97. package/src/types/never-optional.ts +7 -0
  98. package/src/types/provider-options.ts +1 -1
  99. package/src/types/provider-reference.ts +10 -0
  100. package/src/types/sandbox.ts +217 -0
  101. package/src/types/system-model-message.ts +1 -1
  102. package/src/types/tool-approval-request.ts +13 -0
  103. package/src/types/tool-execute-function.ts +56 -0
  104. package/src/types/tool-model-message.ts +3 -3
  105. package/src/types/tool-needs-approval-function.ts +39 -0
  106. package/src/types/tool-set.ts +22 -0
  107. package/src/types/tool.ts +278 -225
  108. package/src/types/user-model-message.ts +2 -2
  109. package/src/validate-download-url.ts +120 -33
  110. package/src/validate-types.ts +5 -3
  111. package/dist/index.d.mts +0 -1455
  112. package/dist/index.mjs +0 -2754
  113. package/dist/index.mjs.map +0 -1
  114. package/dist/test/index.d.mts +0 -17
  115. package/dist/test/index.mjs +0 -77
  116. package/dist/test/index.mjs.map +0 -1
  117. package/src/provider-tool-factory.ts +0 -125
package/src/types/tool.ts CHANGED
@@ -1,86 +1,34 @@
1
- import { JSONValue } from '@ai-sdk/provider';
2
- import { FlexibleSchema } from '../schema';
3
- import { ToolResultOutput } from './content-part';
4
- import { ModelMessage } from './model-message';
5
- import { ProviderOptions } from './provider-options';
1
+ import type { JSONValue, JSONObject } 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 { ExecutableTool } from './executable-tool';
6
+ import type { NeverOptional } from './never-optional';
7
+ import type { ProviderOptions } from './provider-options';
8
+ import type {
9
+ ToolExecuteFunction,
10
+ ToolExecutionOptions,
11
+ } from './tool-execute-function';
12
+ import type { ToolNeedsApprovalFunction } from './tool-needs-approval-function';
13
+ import type { SandboxSession } from './sandbox';
6
14
 
7
15
  /**
8
- * Additional options that are sent into each tool call.
16
+ * Helper type to determine the outputSchema and execute function properties of a tool.
9
17
  */
10
- export interface ToolExecutionOptions {
11
- /**
12
- * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
13
- */
14
- toolCallId: string;
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<
18
+ type ToolOutputProperties<
19
+ INPUT,
20
+ OUTPUT,
21
+ CONTEXT extends Context | unknown | never,
22
+ > = NeverOptional<
82
23
  OUTPUT,
83
24
  | {
25
+ /**
26
+ * The optional schema of the output that the tool produces.
27
+ *
28
+ * If not provided, the output shape will be inferred from the execute function.
29
+ */
30
+ outputSchema?: FlexibleSchema<OUTPUT>;
31
+
84
32
  /**
85
33
  * An async function that is called with the arguments from the tool call and produces a result.
86
34
  * If not provided, the tool will not be executed automatically.
@@ -88,11 +36,14 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<
88
36
  * @args is the input of the tool call.
89
37
  * @options.abortSignal is a signal that can be used to abort the tool call.
90
38
  */
91
- execute: ToolExecuteFunction<INPUT, OUTPUT>;
92
-
93
- outputSchema?: FlexibleSchema<OUTPUT>;
39
+ execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
94
40
  }
95
41
  | {
42
+ /**
43
+ * The schema of the output that the tool produces.
44
+ *
45
+ * Required when no execute function is provided.
46
+ */
96
47
  outputSchema: FlexibleSchema<OUTPUT>;
97
48
 
98
49
  execute?: never;
@@ -100,24 +51,17 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<
100
51
  >;
101
52
 
102
53
  /**
103
- * A tool contains the description and the schema of the input that the tool expects.
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.
54
+ * Common properties shared by all tool kinds.
107
55
  */
108
- export type Tool<
56
+ type BaseTool<
109
57
  INPUT extends JSONValue | unknown | never = any,
110
58
  OUTPUT extends JSONValue | unknown | never = any,
59
+ CONTEXT extends Context | unknown | never = any,
111
60
  > = {
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
61
  /**
120
62
  * An optional title of the tool.
63
+ *
64
+ * @deprecated Use `providerMetadata` for source-specific tool display metadata.
121
65
  */
122
66
  title?: string;
123
67
 
@@ -128,6 +72,17 @@ export type Tool<
128
72
  */
129
73
  providerOptions?: ProviderOptions;
130
74
 
75
+ /**
76
+ * Optional metadata about the tool itself (e.g. its source).
77
+ *
78
+ * Unlike `providerOptions`, this metadata is not sent to the language
79
+ * model. Instead, it is propagated onto the resulting tool call's
80
+ * `toolMetadata` so consumers can read it from tool call / result parts
81
+ * and UI message parts. This is useful for sources of dynamic tools (e.g.
82
+ * an MCP server) to identify themselves.
83
+ */
84
+ metadata?: JSONObject;
85
+
131
86
  /**
132
87
  * The schema of the input that the tool expects.
133
88
  * The language model will use this to generate the input.
@@ -138,39 +93,40 @@ export type Tool<
138
93
  inputSchema: FlexibleSchema<INPUT>;
139
94
 
140
95
  /**
141
- * An optional list of input examples that show the language
142
- * model what the input should look like.
96
+ * An optional schema describing the context that the tool expects.
97
+ *
98
+ * The context is passed to execute function as part of the execution options.
143
99
  */
144
- inputExamples?: Array<{ input: NoInfer<INPUT> }>;
100
+ contextSchema?: FlexibleSchema<CONTEXT>;
145
101
 
146
102
  /**
147
103
  * Whether the tool needs approval before it can be executed.
104
+ *
105
+ * @deprecated Tool approval is handled on a `generateText` / `streamText` level now.
148
106
  */
149
107
  needsApproval?:
150
108
  | boolean
151
- | ToolNeedsApprovalFunction<[INPUT] extends [never] ? unknown : INPUT>;
152
-
153
- /**
154
- * Strict mode setting for the tool.
155
- *
156
- * Providers that support strict mode will use this setting to determine
157
- * how the input should be generated. Strict mode will always produce
158
- * valid inputs, but it might limit what input schemas are supported.
159
- */
160
- strict?: boolean;
109
+ | ToolNeedsApprovalFunction<
110
+ [INPUT] extends [never] ? unknown : INPUT,
111
+ NoInfer<CONTEXT>
112
+ >;
161
113
 
162
114
  /**
163
115
  * Optional function that is called when the argument streaming starts.
164
116
  * Only called when the tool is used in a streaming context.
165
117
  */
166
- onInputStart?: (options: ToolExecutionOptions) => void | PromiseLike<void>;
118
+ onInputStart?: (
119
+ options: ToolExecutionOptions<NoInfer<CONTEXT>>,
120
+ ) => void | PromiseLike<void>;
167
121
 
168
122
  /**
169
123
  * Optional function that is called when an argument streaming delta is available.
170
124
  * Only called when the tool is used in a streaming context.
171
125
  */
172
126
  onInputDelta?: (
173
- options: { inputTextDelta: string } & ToolExecutionOptions,
127
+ options: { inputTextDelta: string } & ToolExecutionOptions<
128
+ NoInfer<CONTEXT>
129
+ >,
174
130
  ) => void | PromiseLike<void>;
175
131
 
176
132
  /**
@@ -180,147 +136,244 @@ export type Tool<
180
136
  onInputAvailable?: (
181
137
  options: {
182
138
  input: [INPUT] extends [never] ? unknown : INPUT;
183
- } & ToolExecutionOptions,
139
+ } & ToolExecutionOptions<NoInfer<CONTEXT>>,
184
140
  ) => void | PromiseLike<void>;
185
- } & ToolOutputProperties<INPUT, OUTPUT> & {
141
+
142
+ /**
143
+ * Optional conversion function that maps the tool result to an output that can be used by the language model.
144
+ *
145
+ * If not provided, the tool result will be sent as a JSON object.
146
+ *
147
+ * 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).
148
+ */
149
+ toModelOutput?: (options: {
186
150
  /**
187
- * Optional conversion function that maps the tool result to an output that can be used by the language model.
188
- *
189
- * If not provided, the tool result will be sent as a JSON object.
190
- *
191
- * 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
+ * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
192
152
  */
193
- toModelOutput?: (options: {
194
- /**
195
- * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
196
- */
197
- toolCallId: string;
153
+ toolCallId: string;
198
154
 
199
- /**
200
- * The input of the tool call.
201
- */
202
- input: [INPUT] extends [never] ? unknown : INPUT;
155
+ /**
156
+ * The input of the tool call.
157
+ */
158
+ input: [INPUT] extends [never] ? unknown : INPUT;
203
159
 
204
- /**
205
- * The output of the tool call.
206
- */
207
- output: 0 extends 1 & OUTPUT
160
+ /**
161
+ * The output of the tool call.
162
+ */
163
+ output: 0 extends 1 & OUTPUT
164
+ ? any
165
+ : [OUTPUT] extends [never]
208
166
  ? any
209
- : [OUTPUT] extends [never]
210
- ? any
211
- : NoInfer<OUTPUT>;
212
- }) => ToolResultOutput | PromiseLike<ToolResultOutput>;
213
- } & (
214
- | {
215
- /**
216
- * Tool with user-defined input and output schemas.
217
- */
218
- type?: undefined | 'function';
219
- }
220
- | {
221
- /**
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.
224
- */
225
- type: 'dynamic';
226
- }
227
- | {
228
- /**
229
- * Tool with provider-defined input and output schemas.
230
- */
231
- type: 'provider';
232
-
233
- /**
234
- * The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
235
- */
236
- id: `${string}.${string}`;
237
-
238
- /**
239
- * The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
240
- */
241
- args: Record<string, unknown>;
242
-
243
- /**
244
- * Whether this provider-executed tool supports deferred results.
245
- *
246
- * When true, the tool result may not be returned in the same turn as the
247
- * tool call (e.g., when using programmatic tool calling where a server tool
248
- * triggers a client-executed tool, and the server tool's result is deferred
249
- * until the client tool is resolved).
250
- *
251
- * This flag allows the AI SDK to handle tool results that arrive without
252
- * a matching tool call in the current response.
253
- *
254
- * @default false
255
- */
256
- supportsDeferredResults?: boolean;
257
- }
258
- );
167
+ : NoInfer<OUTPUT>;
168
+ }) => ToolResultOutput | PromiseLike<ToolResultOutput>;
169
+ } & ToolOutputProperties<INPUT, OUTPUT, NoInfer<CONTEXT>>;
259
170
 
260
171
  /**
261
- * Infer the input type of a tool.
172
+ * Common properties shared by function-style tools.
262
173
  */
263
- export type InferToolInput<TOOL extends Tool> =
264
- TOOL extends Tool<infer INPUT, any> ? INPUT : never;
174
+ type BaseFunctionTool<
175
+ INPUT extends JSONValue | unknown | never = any,
176
+ OUTPUT extends JSONValue | unknown | never = any,
177
+ CONTEXT extends Context | unknown | never = any,
178
+ > = BaseTool<INPUT, OUTPUT, CONTEXT> & {
179
+ /**
180
+ * Optional description of what the tool does.
181
+ *
182
+ * Included in the tool definition sent to the language model so it can
183
+ * decide when and how to call the tool.
184
+ *
185
+ * Provide a string for a fixed description, or a function that returns a
186
+ * string from the current `context` (and optional `experimental_sandbox`) when the
187
+ * description should vary per call.
188
+ */
189
+ description?:
190
+ | string
191
+ | ((options: {
192
+ context: NoInfer<CONTEXT>;
193
+ experimental_sandbox?: SandboxSession;
194
+ }) => string);
195
+
196
+ /**
197
+ * Strict mode setting for the tool.
198
+ *
199
+ * Providers that support strict mode will use this setting to determine
200
+ * how the input should be generated. Strict mode will always produce
201
+ * valid inputs, but it might limit what input schemas are supported.
202
+ */
203
+ strict?: boolean;
204
+
205
+ /**
206
+ * An optional list of input examples that show the language
207
+ * model what the input should look like.
208
+ */
209
+ inputExamples?: Array<{ input: NoInfer<INPUT> }>;
210
+
211
+ // make all properties available to improve usage dx
212
+ id?: never;
213
+ isProviderExecuted?: never;
214
+ args?: never;
215
+ supportsDeferredResults?: never;
216
+ };
265
217
 
266
218
  /**
267
- * Infer the output type of a tool.
219
+ * Tool with user-defined input and output schemas that is executed by the AI SDK.
268
220
  */
269
- export type InferToolOutput<TOOL extends Tool> =
270
- TOOL extends Tool<any, infer OUTPUT> ? OUTPUT : never;
221
+ export type FunctionTool<
222
+ INPUT extends JSONValue | unknown | never = any,
223
+ OUTPUT extends JSONValue | unknown | never = any,
224
+ CONTEXT extends Context | unknown | never = any,
225
+ > = BaseFunctionTool<INPUT, OUTPUT, CONTEXT> & {
226
+ type?: undefined | 'function';
227
+ };
271
228
 
272
229
  /**
273
- * Helper function for inferring the execute args of a tool.
230
+ * Tool that is defined at runtime.
231
+ * The types of input and output are not known at development time.
232
+ *
233
+ * For example, MCP tools that are not known at development time.
274
234
  */
275
- // Note: overload order is important for auto-completion
276
- export function tool<INPUT, OUTPUT>(
277
- tool: Tool<INPUT, OUTPUT>,
278
- ): Tool<INPUT, OUTPUT>;
279
- export function tool<INPUT>(tool: Tool<INPUT, never>): Tool<INPUT, never>;
280
- export function tool<OUTPUT>(tool: Tool<never, OUTPUT>): Tool<never, OUTPUT>;
281
- export function tool(tool: Tool<never, never>): Tool<never, never>;
282
- export function tool(tool: any): any {
283
- return tool;
284
- }
235
+ export type DynamicTool<
236
+ INPUT extends JSONValue | unknown | never = any,
237
+ OUTPUT extends JSONValue | unknown | never = any,
238
+ CONTEXT extends Context | unknown | never = any,
239
+ > = BaseFunctionTool<INPUT, OUTPUT, CONTEXT> & {
240
+ type: 'dynamic';
241
+ };
285
242
 
286
243
  /**
287
- * Defines a dynamic tool.
244
+ * Common properties shared by provider tools.
288
245
  */
289
- export function dynamicTool(tool: {
290
- description?: string;
291
- title?: string;
292
- providerOptions?: ProviderOptions;
293
- inputSchema: FlexibleSchema<unknown>;
294
- execute: ToolExecuteFunction<unknown, unknown>;
246
+ type BaseProviderTool<
247
+ INPUT extends JSONValue | unknown | never = any,
248
+ OUTPUT extends JSONValue | unknown | never = any,
249
+ CONTEXT extends Context | unknown | never = any,
250
+ > = BaseTool<INPUT, OUTPUT, CONTEXT> & {
251
+ type: 'provider';
295
252
 
296
253
  /**
297
- * Optional conversion function that maps the tool result to an output that can be used by the language model.
298
- *
299
- * If not provided, the tool result will be sent as a JSON object.
254
+ * The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
300
255
  */
301
- toModelOutput?: (options: {
302
- /**
303
- * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
304
- */
305
- toolCallId: string;
256
+ id: `${string}.${string}`;
306
257
 
307
- /**
308
- * The input of the tool call.
309
- */
310
- input: unknown;
258
+ /**
259
+ * The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
260
+ */
261
+ args: Record<string, unknown>;
311
262
 
312
- /**
313
- * The output of the tool call.
314
- */
315
- output: unknown;
316
- }) => ToolResultOutput | PromiseLike<ToolResultOutput>;
263
+ // make all properties available to improve usage dx
264
+ description?: never;
265
+ strict?: never;
266
+ inputExamples?: never;
267
+ };
317
268
 
269
+ /**
270
+ * Tool with provider-defined input and output schemas that is executed by the
271
+ * user.
272
+ *
273
+ * For example, shell tools that are executed in a local shell, but have provider-defined input and output schemas.
274
+ */
275
+ export type ProviderDefinedTool<
276
+ INPUT extends JSONValue | unknown | never = any,
277
+ OUTPUT extends JSONValue | unknown | never = any,
278
+ CONTEXT extends Context | unknown | never = any,
279
+ > = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
318
280
  /**
319
- * Whether the tool needs approval before it can be executed.
281
+ * Flag that indicates whether the tool is executed by the provider.
320
282
  */
321
- needsApproval?: boolean | ToolNeedsApprovalFunction<unknown>;
322
- }): Tool<unknown, unknown> & {
323
- type: 'dynamic';
324
- } {
325
- return { ...tool, type: 'dynamic' };
283
+ isProviderExecuted: false;
284
+
285
+ // make all properties available to improve usage dx
286
+ supportsDeferredResults?: never;
287
+ };
288
+
289
+ /**
290
+ * Tool with provider-defined input and output schemas that is executed by the
291
+ * provider.
292
+ *
293
+ * For example, web search tools and code execution tools that are executed by the provider itself.
294
+ */
295
+ export type ProviderExecutedTool<
296
+ INPUT extends JSONValue | unknown | never = any,
297
+ OUTPUT extends JSONValue | unknown | never = any,
298
+ CONTEXT extends Context | unknown | never = any,
299
+ > = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
300
+ /**
301
+ * Flag that indicates whether the tool is executed by the provider.
302
+ */
303
+ isProviderExecuted: true;
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
+ * A tool can either be user-defined or provider-defined.
323
+ *
324
+ * It contains the schemas and metadata needed for the language model to call
325
+ * the tool and can include an execute function for tools that are executed by
326
+ * the AI SDK.
327
+ */
328
+ export type Tool<
329
+ INPUT extends JSONValue | unknown | never = any,
330
+ OUTPUT extends JSONValue | unknown | never = any,
331
+ CONTEXT extends Context | unknown | never = any,
332
+ > =
333
+ | FunctionTool<INPUT, OUTPUT, CONTEXT>
334
+ | DynamicTool<INPUT, OUTPUT, CONTEXT>
335
+ | ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>
336
+ | ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
337
+
338
+ /**
339
+ * Infer the tool type from a tool object.
340
+ *
341
+ * This is useful for type inference when working with tool objects.
342
+ *
343
+ * When the input has an `execute` function, the return type narrows to
344
+ * `ExecutableTool<Tool<...>>` so that `.execute` is non-nullable without
345
+ * needing `isExecutableTool` or a `!` assertion at the call site.
346
+ */
347
+ // Note: overload order is important for auto-completion.
348
+ // The "with execute" overload comes first so calls that include an
349
+ // `execute` function get the narrowed return type. Calls without
350
+ // `execute` fall through to the overloads below.
351
+ export function tool<INPUT, OUTPUT, CONTEXT extends Context>(
352
+ tool: Tool<INPUT, OUTPUT, CONTEXT> & {
353
+ execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
354
+ },
355
+ ): ExecutableTool<Tool<INPUT, OUTPUT, CONTEXT>>;
356
+ export function tool<INPUT, OUTPUT, CONTEXT extends Context>(
357
+ tool: Tool<INPUT, OUTPUT, CONTEXT>,
358
+ ): Tool<INPUT, OUTPUT, CONTEXT>;
359
+ export function tool<INPUT, CONTEXT extends Context>(
360
+ tool: Tool<INPUT, never, CONTEXT>,
361
+ ): Tool<INPUT, never, CONTEXT>;
362
+ export function tool<OUTPUT, CONTEXT extends Context>(
363
+ tool: Tool<never, OUTPUT, CONTEXT>,
364
+ ): Tool<never, OUTPUT, CONTEXT>;
365
+ export function tool<CONTEXT extends Context>(
366
+ tool: Tool<never, never, CONTEXT>,
367
+ ): Tool<never, never, CONTEXT>;
368
+ export function tool(tool: any): any {
369
+ return tool;
370
+ }
371
+
372
+ /**
373
+ * Define a dynamic tool.
374
+ */
375
+ export function dynamicTool(
376
+ tool: Omit<DynamicTool<unknown, unknown, Context>, 'type'>,
377
+ ): DynamicTool<unknown, unknown, Context> {
378
+ return { ...tool, type: 'dynamic' } as DynamicTool<unknown, unknown, Context>;
326
379
  }
@@ -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.