@ai-sdk/provider-utils 3.0.0-beta.1 → 3.0.0-beta.10

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 CHANGED
@@ -1,5 +1,63 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 3.0.0-beta.10
4
+
5
+ ### Patch Changes
6
+
7
+ - 88a8ee5: fix (ai): support abort during retry waits
8
+
9
+ ## 3.0.0-beta.9
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [27deb4d]
14
+ - @ai-sdk/provider@2.0.0-beta.2
15
+
16
+ ## 3.0.0-beta.8
17
+
18
+ ### Patch Changes
19
+
20
+ - dd5fd43: feat (ai): support dynamic tools in Chat onToolCall
21
+
22
+ ## 3.0.0-beta.7
23
+
24
+ ### Patch Changes
25
+
26
+ - e7fcc86: feat (ai): introduce dynamic tools
27
+
28
+ ## 3.0.0-beta.6
29
+
30
+ ### Patch Changes
31
+
32
+ - ac34802: Add clear object function to StructuredObject
33
+
34
+ ## 3.0.0-beta.5
35
+
36
+ ### Patch Changes
37
+
38
+ - 57edfcb: Adds support for async zod validators
39
+ - 383cbfa: feat (ai): add isAborted to onFinish callback for ui message streams
40
+
41
+ ## 3.0.0-beta.4
42
+
43
+ ### Patch Changes
44
+
45
+ - 205077b: fix: improve Zod compatibility
46
+
47
+ ## 3.0.0-beta.3
48
+
49
+ ### Patch Changes
50
+
51
+ - 05d2819: feat: allow zod 4.x as peer dependency
52
+
53
+ ## 3.0.0-beta.2
54
+
55
+ ### Patch Changes
56
+
57
+ - 0571b98: chore (provider-utils): update eventsource-parser to 3.0.3
58
+ - 39a4fab: fix (provider-utils): detect failed fetch in browser environments
59
+ - d1a034f: feature: using Zod 4 for internal stuff
60
+
3
61
  ## 3.0.0-beta.1
4
62
 
5
63
  ### Major Changes
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
- import { JSONSchema7, JSONValue, JSONParseError, TypeValidationError, APICallError, SharedV2ProviderOptions, LanguageModelV2ToolResultOutput, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
2
- import { ZodSchema, z } from 'zod';
1
+ import { JSONValue, JSONParseError, TypeValidationError, APICallError, JSONSchema7, SharedV2ProviderOptions, LanguageModelV2ToolResultOutput, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
2
+ import * as z4 from 'zod/v4';
3
+ import { ZodType, z } from 'zod/v4';
3
4
  import * as z3 from 'zod/v3';
4
- import * as z4 from 'zod/v4/core';
5
5
  import { StandardSchemaV1 } from '@standard-schema/spec';
6
6
  export * from '@standard-schema/spec';
7
7
  export { EventSourceMessage, EventSourceParserStream } from 'eventsource-parser/stream';
@@ -20,9 +20,13 @@ declare function convertAsyncIteratorToReadableStream<T>(iterator: AsyncIterator
20
20
  /**
21
21
  * Creates a Promise that resolves after a specified delay
22
22
  * @param delayInMs - The delay duration in milliseconds. If null or undefined, resolves immediately.
23
+ * @param signal - Optional AbortSignal to cancel the delay
23
24
  * @returns A Promise that resolves after the specified delay
25
+ * @throws {DOMException} When the signal is aborted
24
26
  */
25
- declare function delay(delayInMs?: number | null): Promise<void>;
27
+ declare function delay(delayInMs?: number | null, options?: {
28
+ abortSignal?: AbortSignal;
29
+ }): Promise<void>;
26
30
 
27
31
  /**
28
32
  Extracts the headers from a response object and returns them as a key-value object.
@@ -96,45 +100,8 @@ type Validator<OBJECT = unknown> = {
96
100
  */
97
101
  declare function validator<OBJECT>(validate?: undefined | ((value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>)): Validator<OBJECT>;
98
102
  declare function isValidator(value: unknown): value is Validator;
99
- declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<OBJECT>): Validator<OBJECT>;
100
- declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<OBJECT>): Validator<OBJECT>;
101
-
102
- /**
103
- * Used to mark schemas so we can support both Zod and custom schemas.
104
- */
105
- declare const schemaSymbol: unique symbol;
106
- type Schema<OBJECT = unknown> = Validator<OBJECT> & {
107
- /**
108
- * Used to mark schemas so we can support both Zod and custom schemas.
109
- */
110
- [schemaSymbol]: true;
111
- /**
112
- * Schema type for inference.
113
- */
114
- _type: OBJECT;
115
- /**
116
- * The JSON Schema for the schema. It is passed to the providers.
117
- */
118
- readonly jsonSchema: JSONSchema7;
119
- };
120
- type FlexibleSchema<T> = z4.$ZodType<T> | z3.Schema<T> | Schema<T>;
121
- type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.$ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
122
- /**
123
- * Create a schema using a JSON Schema.
124
- *
125
- * @param jsonSchema The JSON Schema for the schema.
126
- * @param options.validate Optional. A validation function for the schema.
127
- */
128
- declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
129
- validate?: (value: unknown) => {
130
- success: true;
131
- value: OBJECT;
132
- } | {
133
- success: false;
134
- error: Error;
135
- };
136
- }): Schema<OBJECT>;
137
- declare function asSchema<OBJECT>(schema: z4.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
103
+ declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
104
+ declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
138
105
 
139
106
  /**
140
107
  * Parses a JSON string into an unknown object.
@@ -154,10 +121,10 @@ declare function parseJSON(options: {
154
121
  * @param {Validator<T>} schema - The schema to use for parsing the JSON.
155
122
  * @returns {Promise<T>} - The parsed object.
156
123
  */
157
- declare function parseJSON<VALIDATOR extends z4.$ZodType | z3.Schema | Validator, VALUE = InferSchema<VALIDATOR>>(options: {
124
+ declare function parseJSON<T>(options: {
158
125
  text: string;
159
- schema: VALIDATOR;
160
- }): Promise<VALUE>;
126
+ schema: z4.core.$ZodType<T> | z3.Schema<T> | Validator<T>;
127
+ }): Promise<T>;
161
128
  type ParseResult<T> = {
162
129
  success: true;
163
130
  value: T;
@@ -185,10 +152,10 @@ declare function safeParseJSON(options: {
185
152
  * @param {Validator<T>} schema - The schema to use for parsing the JSON.
186
153
  * @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
187
154
  */
188
- declare function safeParseJSON<SCHEMA extends z4.$ZodType | z3.Schema | Validator, VALUE = InferSchema<SCHEMA>>(options: {
155
+ declare function safeParseJSON<T>(options: {
189
156
  text: string;
190
- schema: SCHEMA;
191
- }): Promise<ParseResult<VALUE>>;
157
+ schema: z4.core.$ZodType<T> | z3.Schema<T> | Validator<T>;
158
+ }): Promise<ParseResult<T>>;
192
159
  declare function isParsableJson(input: string): boolean;
193
160
 
194
161
  type ResponseHandler<RETURN_TYPE> = (options: {
@@ -201,13 +168,13 @@ type ResponseHandler<RETURN_TYPE> = (options: {
201
168
  responseHeaders?: Record<string, string>;
202
169
  }>;
203
170
  declare const createJsonErrorResponseHandler: <T>({ errorSchema, errorToMessage, isRetryable, }: {
204
- errorSchema: ZodSchema<T>;
171
+ errorSchema: ZodType<T>;
205
172
  errorToMessage: (error: T) => string;
206
173
  isRetryable?: (response: Response, error?: T) => boolean;
207
174
  }) => ResponseHandler<APICallError>;
208
- declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodSchema<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
209
- declare const createJsonStreamResponseHandler: <T>(chunkSchema: ZodSchema<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
210
- declare const createJsonResponseHandler: <T>(responseSchema: ZodSchema<T>) => ResponseHandler<T>;
175
+ declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
176
+ declare const createJsonStreamResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
177
+ declare const createJsonResponseHandler: <T>(responseSchema: ZodType<T>) => ResponseHandler<T>;
211
178
  declare const createBinaryResponseHandler: () => ResponseHandler<Uint8Array>;
212
179
  declare const createStatusCodeErrorResponseHandler: () => ResponseHandler<APICallError>;
213
180
 
@@ -283,13 +250,13 @@ declare function loadSetting({ settingValue, environmentVariableName, settingNam
283
250
  */
284
251
  declare function parseJsonEventStream<T>({ stream, schema, }: {
285
252
  stream: ReadableStream<Uint8Array>;
286
- schema: ZodSchema<T>;
253
+ schema: ZodType<T>;
287
254
  }): ReadableStream<ParseResult<T>>;
288
255
 
289
256
  declare function parseProviderOptions<T>({ provider, providerOptions, schema, }: {
290
257
  provider: string;
291
258
  providerOptions: Record<string, unknown> | undefined;
292
- schema: z.ZodSchema<T>;
259
+ schema: z.core.$ZodType<T, any>;
293
260
  }): Promise<T | undefined>;
294
261
 
295
262
  declare const postJsonToApi: <T>({ url, headers, body, failedResponseHandler, successfulResponseHandler, abortSignal, fetch, }: {
@@ -335,6 +302,37 @@ declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, fa
335
302
  responseHeaders?: Record<string, string>;
336
303
  }>;
337
304
 
305
+ /**
306
+ * Used to mark schemas so we can support both Zod and custom schemas.
307
+ */
308
+ declare const schemaSymbol: unique symbol;
309
+ type Schema<OBJECT = unknown> = Validator<OBJECT> & {
310
+ /**
311
+ * Used to mark schemas so we can support both Zod and custom schemas.
312
+ */
313
+ [schemaSymbol]: true;
314
+ /**
315
+ * Schema type for inference.
316
+ */
317
+ _type: OBJECT;
318
+ /**
319
+ * The JSON Schema for the schema. It is passed to the providers.
320
+ */
321
+ readonly jsonSchema: JSONSchema7;
322
+ };
323
+ type FlexibleSchema<T> = z4.core.$ZodType<T> | z3.Schema<T> | Schema<T>;
324
+ type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.core.$ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
325
+ /**
326
+ * Create a schema using a JSON Schema.
327
+ *
328
+ * @param jsonSchema The JSON Schema for the schema.
329
+ * @param options.validate Optional. A validation function for the schema.
330
+ */
331
+ declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
332
+ validate?: (value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>;
333
+ }): Schema<OBJECT>;
334
+ declare function asSchema<OBJECT>(schema: z4.core.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
335
+
338
336
  /**
339
337
  Additional provider-specific options.
340
338
 
@@ -650,12 +648,18 @@ If not provided, the tool will not be executed automatically.
650
648
  execute?: never;
651
649
  })> & ({
652
650
  /**
653
- Function tool.
651
+ Tool with user-defined input and output schemas.
654
652
  */
655
653
  type?: undefined | 'function';
656
654
  } | {
657
655
  /**
658
- Provider-defined tool.
656
+ Tool that is defined at runtime (e.g. an MCP tool).
657
+ The types of input and output are not known at development time.
658
+ */
659
+ type: 'dynamic';
660
+ } | {
661
+ /**
662
+ Tool with provider-defined input and output schemas.
659
663
  */
660
664
  type: 'provider-defined';
661
665
  /**
@@ -686,6 +690,18 @@ declare function tool<INPUT, OUTPUT>(tool: Tool<INPUT, OUTPUT>): Tool<INPUT, OUT
686
690
  declare function tool<INPUT>(tool: Tool<INPUT, never>): Tool<INPUT, never>;
687
691
  declare function tool<OUTPUT>(tool: Tool<never, OUTPUT>): Tool<never, OUTPUT>;
688
692
  declare function tool(tool: Tool<never, never>): Tool<never, never>;
693
+ /**
694
+ Helper function for defining a dynamic tool.
695
+ */
696
+ declare function dynamicTool(tool: {
697
+ description?: string;
698
+ providerOptions?: ProviderOptions;
699
+ inputSchema: FlexibleSchema<unknown>;
700
+ execute: ToolExecuteFunction<unknown, unknown>;
701
+ toModelOutput?: (output: unknown) => LanguageModelV2ToolResultPart['output'];
702
+ }): Tool<unknown, unknown> & {
703
+ type: 'dynamic';
704
+ };
689
705
 
690
706
  type ProviderDefinedToolFactory<INPUT, ARGS extends object> = <OUTPUT>(options: ARGS & {
691
707
  execute?: ToolExecuteFunction<INPUT, OUTPUT>;
@@ -742,7 +758,7 @@ declare function convertToBase64(value: string | Uint8Array): string;
742
758
  */
743
759
  declare function validateTypes<OBJECT>({ value, schema, }: {
744
760
  value: unknown;
745
- schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
761
+ schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
746
762
  }): Promise<OBJECT>;
747
763
  /**
748
764
  * Safely validates the types of an unknown object using a schema and
@@ -755,7 +771,7 @@ declare function validateTypes<OBJECT>({ value, schema, }: {
755
771
  */
756
772
  declare function safeValidateTypes<OBJECT>({ value, schema, }: {
757
773
  value: unknown;
758
- schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
774
+ schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
759
775
  }): Promise<{
760
776
  success: true;
761
777
  value: OBJECT;
@@ -768,7 +784,7 @@ declare function safeValidateTypes<OBJECT>({ value, schema, }: {
768
784
 
769
785
  declare function withoutTrailingSlash(url: string | undefined): string | undefined;
770
786
 
771
- declare function zodSchema<OBJECT>(zodSchema: z4.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
787
+ declare function zodSchema<OBJECT>(zodSchema: z4.core.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
772
788
  /**
773
789
  * Enables support for references in the schema.
774
790
  * This is required for recursive schemas, e.g. with `z.lazy`.
@@ -800,6 +816,10 @@ interface ToolCall<NAME extends string, INPUT> {
800
816
  * If this flag is not set or is false, the tool call will be executed by the client.
801
817
  */
802
818
  providerExecuted?: boolean;
819
+ /**
820
+ * Whether the tool is dynamic.
821
+ */
822
+ dynamic?: boolean;
803
823
  }
804
824
 
805
825
  /**
@@ -823,6 +843,14 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
823
843
  Result of the tool call. This is the result of the tool's execution.
824
844
  */
825
845
  output: OUTPUT;
846
+ /**
847
+ * Whether the tool result has been executed by the provider.
848
+ */
849
+ providerExecuted?: boolean;
850
+ /**
851
+ * Whether the tool is dynamic.
852
+ */
853
+ dynamic?: boolean;
826
854
  }
827
855
 
828
- export { type AssistantContent, type AssistantModelMessage, type DataContent, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, type ValidationResult, type Validator, asSchema, asValidator, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, extractResponseHeaders, generateId, getErrorMessage, getFromApi, isAbortError, isParsableJson, isUrlSupported, isValidator, jsonSchema, loadApiKey, loadOptionalSetting, loadSetting, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateTypes, validator, validatorSymbol, withoutTrailingSlash, zodSchema };
856
+ export { type AssistantContent, type AssistantModelMessage, type DataContent, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, type ValidationResult, type Validator, asSchema, asValidator, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, isAbortError, isParsableJson, isUrlSupported, isValidator, jsonSchema, loadApiKey, loadOptionalSetting, loadSetting, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateTypes, validator, validatorSymbol, withoutTrailingSlash, zodSchema };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { JSONSchema7, JSONValue, JSONParseError, TypeValidationError, APICallError, SharedV2ProviderOptions, LanguageModelV2ToolResultOutput, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
2
- import { ZodSchema, z } from 'zod';
1
+ import { JSONValue, JSONParseError, TypeValidationError, APICallError, JSONSchema7, SharedV2ProviderOptions, LanguageModelV2ToolResultOutput, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
2
+ import * as z4 from 'zod/v4';
3
+ import { ZodType, z } from 'zod/v4';
3
4
  import * as z3 from 'zod/v3';
4
- import * as z4 from 'zod/v4/core';
5
5
  import { StandardSchemaV1 } from '@standard-schema/spec';
6
6
  export * from '@standard-schema/spec';
7
7
  export { EventSourceMessage, EventSourceParserStream } from 'eventsource-parser/stream';
@@ -20,9 +20,13 @@ declare function convertAsyncIteratorToReadableStream<T>(iterator: AsyncIterator
20
20
  /**
21
21
  * Creates a Promise that resolves after a specified delay
22
22
  * @param delayInMs - The delay duration in milliseconds. If null or undefined, resolves immediately.
23
+ * @param signal - Optional AbortSignal to cancel the delay
23
24
  * @returns A Promise that resolves after the specified delay
25
+ * @throws {DOMException} When the signal is aborted
24
26
  */
25
- declare function delay(delayInMs?: number | null): Promise<void>;
27
+ declare function delay(delayInMs?: number | null, options?: {
28
+ abortSignal?: AbortSignal;
29
+ }): Promise<void>;
26
30
 
27
31
  /**
28
32
  Extracts the headers from a response object and returns them as a key-value object.
@@ -96,45 +100,8 @@ type Validator<OBJECT = unknown> = {
96
100
  */
97
101
  declare function validator<OBJECT>(validate?: undefined | ((value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>)): Validator<OBJECT>;
98
102
  declare function isValidator(value: unknown): value is Validator;
99
- declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<OBJECT>): Validator<OBJECT>;
100
- declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<OBJECT>): Validator<OBJECT>;
101
-
102
- /**
103
- * Used to mark schemas so we can support both Zod and custom schemas.
104
- */
105
- declare const schemaSymbol: unique symbol;
106
- type Schema<OBJECT = unknown> = Validator<OBJECT> & {
107
- /**
108
- * Used to mark schemas so we can support both Zod and custom schemas.
109
- */
110
- [schemaSymbol]: true;
111
- /**
112
- * Schema type for inference.
113
- */
114
- _type: OBJECT;
115
- /**
116
- * The JSON Schema for the schema. It is passed to the providers.
117
- */
118
- readonly jsonSchema: JSONSchema7;
119
- };
120
- type FlexibleSchema<T> = z4.$ZodType<T> | z3.Schema<T> | Schema<T>;
121
- type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.$ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
122
- /**
123
- * Create a schema using a JSON Schema.
124
- *
125
- * @param jsonSchema The JSON Schema for the schema.
126
- * @param options.validate Optional. A validation function for the schema.
127
- */
128
- declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
129
- validate?: (value: unknown) => {
130
- success: true;
131
- value: OBJECT;
132
- } | {
133
- success: false;
134
- error: Error;
135
- };
136
- }): Schema<OBJECT>;
137
- declare function asSchema<OBJECT>(schema: z4.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
103
+ declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
104
+ declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
138
105
 
139
106
  /**
140
107
  * Parses a JSON string into an unknown object.
@@ -154,10 +121,10 @@ declare function parseJSON(options: {
154
121
  * @param {Validator<T>} schema - The schema to use for parsing the JSON.
155
122
  * @returns {Promise<T>} - The parsed object.
156
123
  */
157
- declare function parseJSON<VALIDATOR extends z4.$ZodType | z3.Schema | Validator, VALUE = InferSchema<VALIDATOR>>(options: {
124
+ declare function parseJSON<T>(options: {
158
125
  text: string;
159
- schema: VALIDATOR;
160
- }): Promise<VALUE>;
126
+ schema: z4.core.$ZodType<T> | z3.Schema<T> | Validator<T>;
127
+ }): Promise<T>;
161
128
  type ParseResult<T> = {
162
129
  success: true;
163
130
  value: T;
@@ -185,10 +152,10 @@ declare function safeParseJSON(options: {
185
152
  * @param {Validator<T>} schema - The schema to use for parsing the JSON.
186
153
  * @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
187
154
  */
188
- declare function safeParseJSON<SCHEMA extends z4.$ZodType | z3.Schema | Validator, VALUE = InferSchema<SCHEMA>>(options: {
155
+ declare function safeParseJSON<T>(options: {
189
156
  text: string;
190
- schema: SCHEMA;
191
- }): Promise<ParseResult<VALUE>>;
157
+ schema: z4.core.$ZodType<T> | z3.Schema<T> | Validator<T>;
158
+ }): Promise<ParseResult<T>>;
192
159
  declare function isParsableJson(input: string): boolean;
193
160
 
194
161
  type ResponseHandler<RETURN_TYPE> = (options: {
@@ -201,13 +168,13 @@ type ResponseHandler<RETURN_TYPE> = (options: {
201
168
  responseHeaders?: Record<string, string>;
202
169
  }>;
203
170
  declare const createJsonErrorResponseHandler: <T>({ errorSchema, errorToMessage, isRetryable, }: {
204
- errorSchema: ZodSchema<T>;
171
+ errorSchema: ZodType<T>;
205
172
  errorToMessage: (error: T) => string;
206
173
  isRetryable?: (response: Response, error?: T) => boolean;
207
174
  }) => ResponseHandler<APICallError>;
208
- declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodSchema<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
209
- declare const createJsonStreamResponseHandler: <T>(chunkSchema: ZodSchema<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
210
- declare const createJsonResponseHandler: <T>(responseSchema: ZodSchema<T>) => ResponseHandler<T>;
175
+ declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
176
+ declare const createJsonStreamResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
177
+ declare const createJsonResponseHandler: <T>(responseSchema: ZodType<T>) => ResponseHandler<T>;
211
178
  declare const createBinaryResponseHandler: () => ResponseHandler<Uint8Array>;
212
179
  declare const createStatusCodeErrorResponseHandler: () => ResponseHandler<APICallError>;
213
180
 
@@ -283,13 +250,13 @@ declare function loadSetting({ settingValue, environmentVariableName, settingNam
283
250
  */
284
251
  declare function parseJsonEventStream<T>({ stream, schema, }: {
285
252
  stream: ReadableStream<Uint8Array>;
286
- schema: ZodSchema<T>;
253
+ schema: ZodType<T>;
287
254
  }): ReadableStream<ParseResult<T>>;
288
255
 
289
256
  declare function parseProviderOptions<T>({ provider, providerOptions, schema, }: {
290
257
  provider: string;
291
258
  providerOptions: Record<string, unknown> | undefined;
292
- schema: z.ZodSchema<T>;
259
+ schema: z.core.$ZodType<T, any>;
293
260
  }): Promise<T | undefined>;
294
261
 
295
262
  declare const postJsonToApi: <T>({ url, headers, body, failedResponseHandler, successfulResponseHandler, abortSignal, fetch, }: {
@@ -335,6 +302,37 @@ declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, fa
335
302
  responseHeaders?: Record<string, string>;
336
303
  }>;
337
304
 
305
+ /**
306
+ * Used to mark schemas so we can support both Zod and custom schemas.
307
+ */
308
+ declare const schemaSymbol: unique symbol;
309
+ type Schema<OBJECT = unknown> = Validator<OBJECT> & {
310
+ /**
311
+ * Used to mark schemas so we can support both Zod and custom schemas.
312
+ */
313
+ [schemaSymbol]: true;
314
+ /**
315
+ * Schema type for inference.
316
+ */
317
+ _type: OBJECT;
318
+ /**
319
+ * The JSON Schema for the schema. It is passed to the providers.
320
+ */
321
+ readonly jsonSchema: JSONSchema7;
322
+ };
323
+ type FlexibleSchema<T> = z4.core.$ZodType<T> | z3.Schema<T> | Schema<T>;
324
+ type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.core.$ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
325
+ /**
326
+ * Create a schema using a JSON Schema.
327
+ *
328
+ * @param jsonSchema The JSON Schema for the schema.
329
+ * @param options.validate Optional. A validation function for the schema.
330
+ */
331
+ declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
332
+ validate?: (value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>;
333
+ }): Schema<OBJECT>;
334
+ declare function asSchema<OBJECT>(schema: z4.core.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
335
+
338
336
  /**
339
337
  Additional provider-specific options.
340
338
 
@@ -650,12 +648,18 @@ If not provided, the tool will not be executed automatically.
650
648
  execute?: never;
651
649
  })> & ({
652
650
  /**
653
- Function tool.
651
+ Tool with user-defined input and output schemas.
654
652
  */
655
653
  type?: undefined | 'function';
656
654
  } | {
657
655
  /**
658
- Provider-defined tool.
656
+ Tool that is defined at runtime (e.g. an MCP tool).
657
+ The types of input and output are not known at development time.
658
+ */
659
+ type: 'dynamic';
660
+ } | {
661
+ /**
662
+ Tool with provider-defined input and output schemas.
659
663
  */
660
664
  type: 'provider-defined';
661
665
  /**
@@ -686,6 +690,18 @@ declare function tool<INPUT, OUTPUT>(tool: Tool<INPUT, OUTPUT>): Tool<INPUT, OUT
686
690
  declare function tool<INPUT>(tool: Tool<INPUT, never>): Tool<INPUT, never>;
687
691
  declare function tool<OUTPUT>(tool: Tool<never, OUTPUT>): Tool<never, OUTPUT>;
688
692
  declare function tool(tool: Tool<never, never>): Tool<never, never>;
693
+ /**
694
+ Helper function for defining a dynamic tool.
695
+ */
696
+ declare function dynamicTool(tool: {
697
+ description?: string;
698
+ providerOptions?: ProviderOptions;
699
+ inputSchema: FlexibleSchema<unknown>;
700
+ execute: ToolExecuteFunction<unknown, unknown>;
701
+ toModelOutput?: (output: unknown) => LanguageModelV2ToolResultPart['output'];
702
+ }): Tool<unknown, unknown> & {
703
+ type: 'dynamic';
704
+ };
689
705
 
690
706
  type ProviderDefinedToolFactory<INPUT, ARGS extends object> = <OUTPUT>(options: ARGS & {
691
707
  execute?: ToolExecuteFunction<INPUT, OUTPUT>;
@@ -742,7 +758,7 @@ declare function convertToBase64(value: string | Uint8Array): string;
742
758
  */
743
759
  declare function validateTypes<OBJECT>({ value, schema, }: {
744
760
  value: unknown;
745
- schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
761
+ schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
746
762
  }): Promise<OBJECT>;
747
763
  /**
748
764
  * Safely validates the types of an unknown object using a schema and
@@ -755,7 +771,7 @@ declare function validateTypes<OBJECT>({ value, schema, }: {
755
771
  */
756
772
  declare function safeValidateTypes<OBJECT>({ value, schema, }: {
757
773
  value: unknown;
758
- schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
774
+ schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
759
775
  }): Promise<{
760
776
  success: true;
761
777
  value: OBJECT;
@@ -768,7 +784,7 @@ declare function safeValidateTypes<OBJECT>({ value, schema, }: {
768
784
 
769
785
  declare function withoutTrailingSlash(url: string | undefined): string | undefined;
770
786
 
771
- declare function zodSchema<OBJECT>(zodSchema: z4.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
787
+ declare function zodSchema<OBJECT>(zodSchema: z4.core.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
772
788
  /**
773
789
  * Enables support for references in the schema.
774
790
  * This is required for recursive schemas, e.g. with `z.lazy`.
@@ -800,6 +816,10 @@ interface ToolCall<NAME extends string, INPUT> {
800
816
  * If this flag is not set or is false, the tool call will be executed by the client.
801
817
  */
802
818
  providerExecuted?: boolean;
819
+ /**
820
+ * Whether the tool is dynamic.
821
+ */
822
+ dynamic?: boolean;
803
823
  }
804
824
 
805
825
  /**
@@ -823,6 +843,14 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
823
843
  Result of the tool call. This is the result of the tool's execution.
824
844
  */
825
845
  output: OUTPUT;
846
+ /**
847
+ * Whether the tool result has been executed by the provider.
848
+ */
849
+ providerExecuted?: boolean;
850
+ /**
851
+ * Whether the tool is dynamic.
852
+ */
853
+ dynamic?: boolean;
826
854
  }
827
855
 
828
- export { type AssistantContent, type AssistantModelMessage, type DataContent, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, type ValidationResult, type Validator, asSchema, asValidator, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, extractResponseHeaders, generateId, getErrorMessage, getFromApi, isAbortError, isParsableJson, isUrlSupported, isValidator, jsonSchema, loadApiKey, loadOptionalSetting, loadSetting, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateTypes, validator, validatorSymbol, withoutTrailingSlash, zodSchema };
856
+ export { type AssistantContent, type AssistantModelMessage, type DataContent, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, type ValidationResult, type Validator, asSchema, asValidator, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, isAbortError, isParsableJson, isUrlSupported, isValidator, jsonSchema, loadApiKey, loadOptionalSetting, loadSetting, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateTypes, validator, validatorSymbol, withoutTrailingSlash, zodSchema };