@ai-sdk/provider-utils 5.0.0-beta.3 → 5.0.0-beta.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +284 -0
- package/dist/index.d.ts +1339 -813
- package/dist/index.js +837 -288
- package/dist/index.js.map +1 -1
- package/dist/test/index.d.ts +2 -1
- package/dist/test/index.js +18 -37
- package/dist/test/index.js.map +1 -1
- package/package.json +13 -13
- package/src/add-additional-properties-to-json-schema.ts +1 -1
- package/src/as-array.ts +12 -0
- package/src/convert-image-model-file-to-data-uri.ts +3 -3
- package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
- package/src/create-tool-name-mapping.ts +6 -22
- package/src/detect-media-type.ts +312 -0
- package/src/filter-nullable.ts +11 -0
- package/src/get-error-message.ts +1 -15
- package/src/get-from-api.ts +2 -2
- package/src/has-required-key.ts +6 -0
- package/src/index.ts +42 -12
- package/src/inject-json-instruction.ts +6 -6
- package/src/is-buffer.ts +9 -0
- package/src/is-json-serializable.ts +29 -0
- package/src/is-provider-reference.ts +21 -0
- package/src/is-url-supported.ts +17 -2
- package/src/load-api-key.ts +1 -1
- package/src/load-setting.ts +1 -1
- package/src/map-reasoning-to-provider.ts +108 -0
- package/src/maybe-promise-like.ts +3 -0
- package/src/parse-json-event-stream.ts +3 -3
- package/src/parse-json.ts +3 -3
- package/src/parse-provider-options.ts +1 -1
- package/src/post-to-api.ts +4 -4
- package/src/provider-defined-tool-factory.ts +129 -0
- package/src/provider-executed-tool-factory.ts +69 -0
- package/src/resolve-full-media-type.ts +49 -0
- package/src/resolve-provider-reference.ts +26 -0
- package/src/resolve.ts +16 -1
- package/src/response-handler.ts +3 -3
- package/src/schema.ts +6 -3
- package/src/secure-json-parse.ts +1 -1
- package/src/serialize-model-options.ts +63 -0
- package/src/streaming-tool-call-tracker.ts +241 -0
- package/src/test/convert-response-stream-to-array.ts +1 -1
- package/src/test/is-node-version.ts +22 -1
- package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
- package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +4 -4
- package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +5 -5
- package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
- package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
- package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +4 -4
- package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +9 -10
- package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
- package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
- package/src/types/assistant-model-message.ts +5 -3
- package/src/types/content-part.ts +102 -24
- package/src/types/context.ts +4 -0
- package/src/types/executable-tool.ts +17 -0
- package/src/types/execute-tool.ts +29 -9
- package/src/types/file-data.ts +48 -0
- package/src/types/index.ts +26 -11
- package/src/types/infer-tool-context.ts +12 -0
- package/src/types/infer-tool-input.ts +7 -0
- package/src/types/infer-tool-output.ts +7 -0
- package/src/types/infer-tool-set-context.ts +15 -0
- package/src/types/model-message.ts +4 -4
- package/src/types/never-optional.ts +7 -0
- package/src/types/provider-options.ts +2 -2
- package/src/types/provider-reference.ts +10 -0
- package/src/types/sensitive-context.ts +9 -0
- package/src/types/system-model-message.ts +1 -1
- package/src/types/tool-approval-request.ts +7 -0
- package/src/types/tool-execute-function.ts +50 -0
- package/src/types/tool-model-message.ts +3 -3
- package/src/types/tool-needs-approval-function.ts +39 -0
- package/src/types/tool-set.ts +22 -0
- package/src/types/tool.ts +251 -222
- package/src/types/user-model-message.ts +2 -2
- package/src/validate-download-url.ts +7 -2
- package/src/validate-types.ts +5 -3
- package/dist/index.d.mts +0 -1458
- package/dist/index.mjs +0 -2759
- package/dist/index.mjs.map +0 -1
- package/dist/test/index.d.mts +0 -17
- package/dist/test/index.mjs +0 -77
- package/dist/test/index.mjs.map +0 -1
- package/src/provider-tool-factory.ts +0 -125
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SharedV4FileDataUrl, SharedV4FileDataReference, SharedV4FileDataText, SharedV4ProviderOptions, SharedV4ProviderReference, JSONValue, ImageModelV4File, LanguageModelV4FunctionTool, LanguageModelV4ProviderTool, AISDKError, JSONSchema7, JSONParseError, TypeValidationError, APICallError, LanguageModelV4Prompt, LanguageModelV4CallOptions, SharedV4Warning, SharedV4ProviderMetadata, LanguageModelV4FilePart, JSONObject, LanguageModelV4StreamPart, TypeValidationContext } from '@ai-sdk/provider';
|
|
2
|
+
export { getErrorMessage } from '@ai-sdk/provider';
|
|
2
3
|
import { StandardSchemaV1, StandardJSONSchemaV1 } from '@standard-schema/spec';
|
|
3
4
|
export * from '@standard-schema/spec';
|
|
4
5
|
import * as z3 from 'zod/v3';
|
|
5
6
|
import * as z4 from 'zod/v4';
|
|
7
|
+
export { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE } from '@workflow/serde';
|
|
6
8
|
export { EventSourceMessage, EventSourceParserStream } from 'eventsource-parser/stream';
|
|
7
9
|
|
|
10
|
+
/**
|
|
11
|
+
* A value that can be provided either as a single item, an array of items,
|
|
12
|
+
* or be left undefined.
|
|
13
|
+
*/
|
|
14
|
+
type Arrayable<T> = T | T[] | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Normalizes a possibly undefined or non-array value into an array.
|
|
17
|
+
*/
|
|
18
|
+
declare function asArray<T>(value: Arrayable<T>): T[];
|
|
19
|
+
|
|
8
20
|
declare function combineHeaders(...headers: Array<Record<string, string | undefined> | undefined>): Record<string, string | undefined>;
|
|
9
21
|
|
|
10
22
|
/**
|
|
@@ -17,748 +29,310 @@ declare function combineHeaders(...headers: Array<Record<string, string | undefi
|
|
|
17
29
|
declare function convertAsyncIteratorToReadableStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
|
|
18
30
|
|
|
19
31
|
/**
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
interface ToolNameMapping {
|
|
23
|
-
/**
|
|
24
|
-
* Maps a custom tool name (used by the client) to the provider's tool name.
|
|
25
|
-
* If the custom tool name does not have a mapping, returns the input name.
|
|
26
|
-
*
|
|
27
|
-
* @param customToolName - The custom name of the tool defined by the client.
|
|
28
|
-
* @returns The corresponding provider tool name, or the input name if not mapped.
|
|
29
|
-
*/
|
|
30
|
-
toProviderToolName: (customToolName: string) => string;
|
|
31
|
-
/**
|
|
32
|
-
* Maps a provider tool name to the custom tool name used by the client.
|
|
33
|
-
* If the provider tool name does not have a mapping, returns the input name.
|
|
34
|
-
*
|
|
35
|
-
* @param providerToolName - The name of the tool as understood by the provider.
|
|
36
|
-
* @returns The corresponding custom tool name, or the input name if not mapped.
|
|
37
|
-
*/
|
|
38
|
-
toCustomToolName: (providerToolName: string) => string;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* @param tools - Tools that were passed to the language model.
|
|
42
|
-
* @param providerToolNames - Maps the provider tool ids to the provider tool names.
|
|
43
|
-
*/
|
|
44
|
-
declare function createToolNameMapping({ tools, providerToolNames, resolveProviderToolName, }: {
|
|
45
|
-
/**
|
|
46
|
-
* Tools that were passed to the language model.
|
|
47
|
-
*/
|
|
48
|
-
tools: Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderTool> | undefined;
|
|
49
|
-
/**
|
|
50
|
-
* Maps the provider tool ids to the provider tool names.
|
|
51
|
-
*/
|
|
52
|
-
providerToolNames: Record<`${string}.${string}`, string>;
|
|
53
|
-
/**
|
|
54
|
-
* Optional resolver for provider tool names that cannot be represented as
|
|
55
|
-
* static id -> name mappings (e.g. dynamic provider names).
|
|
56
|
-
*/
|
|
57
|
-
resolveProviderToolName?: (tool: LanguageModelV3ProviderTool) => string | undefined;
|
|
58
|
-
}): ToolNameMapping;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Creates a Promise that resolves after a specified delay
|
|
62
|
-
* @param delayInMs - The delay duration in milliseconds. If null or undefined, resolves immediately.
|
|
63
|
-
* @param signal - Optional AbortSignal to cancel the delay
|
|
64
|
-
* @returns A Promise that resolves after the specified delay
|
|
65
|
-
* @throws {DOMException} When the signal is aborted
|
|
32
|
+
* Data content. Can either be a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer.
|
|
66
33
|
*/
|
|
67
|
-
|
|
68
|
-
abortSignal?: AbortSignal;
|
|
69
|
-
}): Promise<void>;
|
|
34
|
+
type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
|
|
70
35
|
|
|
71
36
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
37
|
+
* File data variant containing raw bytes (`Uint8Array`, `ArrayBuffer`, or
|
|
38
|
+
* `Buffer`) or a base64-encoded string.
|
|
39
|
+
*
|
|
40
|
+
* This is slightly more permissive than `SharedV4FileDataData`.
|
|
75
41
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
private _resolve;
|
|
80
|
-
private _reject;
|
|
81
|
-
get promise(): Promise<T>;
|
|
82
|
-
resolve(value: T): void;
|
|
83
|
-
reject(error: unknown): void;
|
|
84
|
-
isResolved(): boolean;
|
|
85
|
-
isRejected(): boolean;
|
|
86
|
-
isPending(): boolean;
|
|
42
|
+
interface FileDataData {
|
|
43
|
+
type: 'data';
|
|
44
|
+
data: DataContent;
|
|
87
45
|
}
|
|
88
|
-
|
|
89
46
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* @param response - The response object to extract headers from.
|
|
93
|
-
* @returns The headers as a key-value object.
|
|
47
|
+
* File data variant containing a URL that points to the file.
|
|
94
48
|
*/
|
|
95
|
-
|
|
96
|
-
[k: string]: string;
|
|
97
|
-
};
|
|
98
|
-
|
|
49
|
+
type FileDataUrl = SharedV4FileDataUrl;
|
|
99
50
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* If the file is a URL, it returns the URL as-is.
|
|
103
|
-
* If the file is base64 data, it returns a data URI with the base64 data.
|
|
104
|
-
* If the file is a Uint8Array, it converts it to base64 and returns a data URI.
|
|
51
|
+
* File data variant containing a provider reference (`{ [provider]: id }`).
|
|
105
52
|
*/
|
|
106
|
-
|
|
107
|
-
|
|
53
|
+
type FileDataReference = SharedV4FileDataReference;
|
|
108
54
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* Handles the following cases:
|
|
112
|
-
* - `null` or `undefined` values are skipped
|
|
113
|
-
* - Arrays with a single element are appended as a single value
|
|
114
|
-
* - Arrays with multiple elements are appended with `[]` suffix (e.g., `image[]`)
|
|
115
|
-
* unless `useArrayBrackets` is set to `false`
|
|
116
|
-
* - All other values are appended directly
|
|
117
|
-
*
|
|
118
|
-
* @param input - The input object to convert. Use a generic type for type validation.
|
|
119
|
-
* @param options - Optional configuration object.
|
|
120
|
-
* @param options.useArrayBrackets - Whether to add `[]` suffix for multi-element arrays.
|
|
121
|
-
* Defaults to `true`. Set to `false` for APIs that expect repeated keys without brackets.
|
|
122
|
-
* @returns A FormData object containing the input values.
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* ```ts
|
|
126
|
-
* type MyInput = {
|
|
127
|
-
* model: string;
|
|
128
|
-
* prompt: string;
|
|
129
|
-
* images: Blob[];
|
|
130
|
-
* };
|
|
131
|
-
*
|
|
132
|
-
* const formData = convertToFormData<MyInput>({
|
|
133
|
-
* model: 'gpt-image-1',
|
|
134
|
-
* prompt: 'A cat',
|
|
135
|
-
* images: [blob1, blob2],
|
|
136
|
-
* });
|
|
137
|
-
* ```
|
|
55
|
+
* File data variant containing inline text content (e.g. an inline text
|
|
56
|
+
* document).
|
|
138
57
|
*/
|
|
139
|
-
|
|
140
|
-
useArrayBrackets?: boolean;
|
|
141
|
-
}): FormData;
|
|
142
|
-
|
|
58
|
+
type FileDataText = SharedV4FileDataText;
|
|
143
59
|
/**
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
* @param url - The URL to download from.
|
|
147
|
-
* @param options - Optional settings for the download.
|
|
148
|
-
* @param options.maxBytes - Maximum allowed download size in bytes. Defaults to 100 MiB.
|
|
149
|
-
* @param options.abortSignal - An optional abort signal to cancel the download.
|
|
150
|
-
* @returns A Promise that resolves to the downloaded Blob.
|
|
60
|
+
* File data as a tagged discriminated union:
|
|
151
61
|
*
|
|
152
|
-
*
|
|
62
|
+
* - `{ type: 'data', data }`: raw bytes (`Uint8Array`, `ArrayBuffer`, or
|
|
63
|
+
* `Buffer`) or a base64-encoded string.
|
|
64
|
+
* - `{ type: 'url', url }`: a URL that points to the file.
|
|
65
|
+
* - `{ type: 'reference', reference }`: a provider reference (`{ [provider]: id }`).
|
|
66
|
+
* - `{ type: 'text', text }`: inline text content (e.g. an inline text document).
|
|
153
67
|
*/
|
|
154
|
-
|
|
155
|
-
maxBytes?: number;
|
|
156
|
-
abortSignal?: AbortSignal;
|
|
157
|
-
}): Promise<Blob>;
|
|
158
|
-
|
|
159
|
-
declare const symbol: unique symbol;
|
|
160
|
-
declare class DownloadError extends AISDKError {
|
|
161
|
-
private readonly [symbol];
|
|
162
|
-
readonly url: string;
|
|
163
|
-
readonly statusCode?: number;
|
|
164
|
-
readonly statusText?: string;
|
|
165
|
-
constructor({ url, statusCode, statusText, cause, message, }: {
|
|
166
|
-
url: string;
|
|
167
|
-
statusCode?: number;
|
|
168
|
-
statusText?: string;
|
|
169
|
-
message?: string;
|
|
170
|
-
cause?: unknown;
|
|
171
|
-
});
|
|
172
|
-
static isInstance(error: unknown): error is DownloadError;
|
|
173
|
-
}
|
|
68
|
+
type FileData = FileDataData | FileDataUrl | FileDataReference | FileDataText;
|
|
174
69
|
|
|
175
70
|
/**
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
* `fetch().arrayBuffer()` has ~2x peak memory overhead (undici buffers the
|
|
179
|
-
* body internally, then creates the JS ArrayBuffer), so very large downloads
|
|
180
|
-
* risk exceeding the default V8 heap limit on 64-bit systems and terminating
|
|
181
|
-
* the process with an out-of-memory error.
|
|
71
|
+
* Additional provider-specific options.
|
|
182
72
|
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
73
|
+
* They are passed through to the provider from the AI SDK and enable
|
|
74
|
+
* provider-specific functionality that can be fully encapsulated in the provider.
|
|
185
75
|
*/
|
|
186
|
-
|
|
76
|
+
type ProviderOptions = SharedV4ProviderOptions;
|
|
77
|
+
|
|
187
78
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
* Checks the Content-Length header for early rejection, then reads the body
|
|
191
|
-
* incrementally via ReadableStream and aborts with a DownloadError when the
|
|
192
|
-
* limit is exceeded.
|
|
79
|
+
* A mapping of provider names to provider-specific file identifiers.
|
|
193
80
|
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
* @returns A Uint8Array containing the response body.
|
|
198
|
-
* @throws DownloadError if the response exceeds maxBytes.
|
|
81
|
+
* Provider references allow files to be identified across different
|
|
82
|
+
* providers without re-uploading, by storing each provider's own
|
|
83
|
+
* identifier for the same logical file.
|
|
199
84
|
*/
|
|
200
|
-
|
|
201
|
-
response: Response;
|
|
202
|
-
url: string;
|
|
203
|
-
maxBytes?: number;
|
|
204
|
-
}): Promise<Uint8Array>;
|
|
85
|
+
type ProviderReference = SharedV4ProviderReference;
|
|
205
86
|
|
|
206
87
|
/**
|
|
207
|
-
*
|
|
88
|
+
* Text content part of a prompt. It contains a string of text.
|
|
208
89
|
*/
|
|
209
|
-
|
|
210
|
-
|
|
90
|
+
interface TextPart {
|
|
91
|
+
type: 'text';
|
|
92
|
+
/**
|
|
93
|
+
* The text content.
|
|
94
|
+
*/
|
|
95
|
+
text: string;
|
|
96
|
+
/**
|
|
97
|
+
* Additional provider-specific metadata. They are passed through
|
|
98
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
99
|
+
* functionality that can be fully encapsulated in the provider.
|
|
100
|
+
*/
|
|
101
|
+
providerOptions?: ProviderOptions;
|
|
102
|
+
}
|
|
211
103
|
/**
|
|
212
|
-
*
|
|
213
|
-
* The total length of the ID is the sum of the prefix, separator, and random part length.
|
|
214
|
-
* Not cryptographically secure.
|
|
104
|
+
* Image content part of a prompt. It contains an image.
|
|
215
105
|
*
|
|
216
|
-
* @
|
|
217
|
-
*
|
|
218
|
-
* @param separator - The separator between the prefix and the random part of the ID. Default: '-'.
|
|
219
|
-
* @param size - The size of the random part of the ID to generate. Default: 16.
|
|
220
|
-
*/
|
|
221
|
-
declare const createIdGenerator: ({ prefix, size, alphabet, separator, }?: {
|
|
222
|
-
prefix?: string;
|
|
223
|
-
separator?: string;
|
|
224
|
-
size?: number;
|
|
225
|
-
alphabet?: string;
|
|
226
|
-
}) => IdGenerator;
|
|
227
|
-
/**
|
|
228
|
-
* A function that generates an ID.
|
|
229
|
-
*/
|
|
230
|
-
type IdGenerator = () => string;
|
|
231
|
-
/**
|
|
232
|
-
* Generates a 16-character random string to use for IDs.
|
|
233
|
-
* Not cryptographically secure.
|
|
106
|
+
* @deprecated Use `FilePart` with `mediaType: 'image'` instead:
|
|
107
|
+
* `{ type: 'file', mediaType: 'image', data: { type: 'data', data } }`.
|
|
234
108
|
*/
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
declare function getErrorMessage(error: unknown | undefined): string;
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Used to mark schemas so we can support both Zod and custom schemas.
|
|
241
|
-
*/
|
|
242
|
-
declare const schemaSymbol: unique symbol;
|
|
243
|
-
type ValidationResult<OBJECT> = {
|
|
244
|
-
success: true;
|
|
245
|
-
value: OBJECT;
|
|
246
|
-
} | {
|
|
247
|
-
success: false;
|
|
248
|
-
error: Error;
|
|
249
|
-
};
|
|
250
|
-
type Schema<OBJECT = unknown> = {
|
|
109
|
+
interface ImagePart {
|
|
110
|
+
type: 'image';
|
|
251
111
|
/**
|
|
252
|
-
*
|
|
112
|
+
* Image data. Can either be:
|
|
113
|
+
*
|
|
114
|
+
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
115
|
+
* - URL: a URL that points to the image
|
|
116
|
+
* - ProviderReference: a provider reference from `uploadFile`
|
|
253
117
|
*/
|
|
254
|
-
|
|
118
|
+
image: DataContent | URL | ProviderReference;
|
|
255
119
|
/**
|
|
256
|
-
*
|
|
120
|
+
* Optional IANA media type of the image.
|
|
121
|
+
*
|
|
122
|
+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
257
123
|
*/
|
|
258
|
-
|
|
124
|
+
mediaType?: string;
|
|
259
125
|
/**
|
|
260
|
-
*
|
|
261
|
-
*
|
|
126
|
+
* Additional provider-specific metadata. They are passed through
|
|
127
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
128
|
+
* functionality that can be fully encapsulated in the provider.
|
|
262
129
|
*/
|
|
263
|
-
|
|
130
|
+
providerOptions?: ProviderOptions;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* File content part of a prompt. It contains a file.
|
|
134
|
+
*/
|
|
135
|
+
interface FilePart {
|
|
136
|
+
type: 'file';
|
|
264
137
|
/**
|
|
265
|
-
*
|
|
138
|
+
* File data. Either a tagged shape or a bare shorthand:
|
|
139
|
+
*
|
|
140
|
+
* - `{ type: 'data', data }` or bare `DataContent`: raw bytes
|
|
141
|
+
* (base64 string, Uint8Array, ArrayBuffer, Buffer)
|
|
142
|
+
* - `{ type: 'url', url }` or bare `URL`: a URL that points to the file
|
|
143
|
+
* - `{ type: 'reference', reference }` or bare `ProviderReference`:
|
|
144
|
+
* a provider reference from `uploadFile`
|
|
145
|
+
* - `{ type: 'text', text }`: inline text content (tagged only)
|
|
266
146
|
*/
|
|
267
|
-
|
|
268
|
-
};
|
|
269
|
-
/**
|
|
270
|
-
* Creates a schema with deferred creation.
|
|
271
|
-
* This is important to reduce the startup time of the library
|
|
272
|
-
* and to avoid initializing unused validators.
|
|
273
|
-
*
|
|
274
|
-
* @param createValidator A function that creates a schema.
|
|
275
|
-
* @returns A function that returns a schema.
|
|
276
|
-
*/
|
|
277
|
-
declare function lazySchema<SCHEMA>(createSchema: () => Schema<SCHEMA>): LazySchema<SCHEMA>;
|
|
278
|
-
type LazySchema<SCHEMA> = () => Schema<SCHEMA>;
|
|
279
|
-
type ZodSchema<SCHEMA = any> = z3.Schema<SCHEMA, z3.ZodTypeDef, any> | z4.core.$ZodType<SCHEMA, any>;
|
|
280
|
-
type StandardSchema<SCHEMA = any> = StandardSchemaV1<unknown, SCHEMA> & StandardJSONSchemaV1<unknown, SCHEMA>;
|
|
281
|
-
type FlexibleSchema<SCHEMA = any> = Schema<SCHEMA> | LazySchema<SCHEMA> | ZodSchema<SCHEMA> | StandardSchema<SCHEMA>;
|
|
282
|
-
type InferSchema<SCHEMA> = SCHEMA extends ZodSchema<infer T> ? T : SCHEMA extends StandardSchema<infer T> ? T : SCHEMA extends LazySchema<infer T> ? T : SCHEMA extends Schema<infer T> ? T : never;
|
|
283
|
-
/**
|
|
284
|
-
* Create a schema using a JSON Schema.
|
|
285
|
-
*
|
|
286
|
-
* @param jsonSchema The JSON Schema for the schema.
|
|
287
|
-
* @param options.validate Optional. A validation function for the schema.
|
|
288
|
-
*/
|
|
289
|
-
declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7 | PromiseLike<JSONSchema7> | (() => JSONSchema7 | PromiseLike<JSONSchema7>), { validate, }?: {
|
|
290
|
-
validate?: (value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>;
|
|
291
|
-
}): Schema<OBJECT>;
|
|
292
|
-
declare function asSchema<OBJECT>(schema: FlexibleSchema<OBJECT> | undefined): Schema<OBJECT>;
|
|
293
|
-
declare function zodSchema<OBJECT>(zodSchema: z4.core.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
|
|
147
|
+
data: FileData | DataContent | URL | ProviderReference;
|
|
294
148
|
/**
|
|
295
|
-
*
|
|
296
|
-
* This is required for recursive schemas, e.g. with `z.lazy`.
|
|
297
|
-
* However, not all language models and providers support such references.
|
|
298
|
-
* Defaults to `false`.
|
|
149
|
+
* Optional filename of the file.
|
|
299
150
|
*/
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
151
|
+
filename?: string;
|
|
152
|
+
/**
|
|
153
|
+
* Either a full IANA media type (`type/subtype`, e.g. `image/png`) or just
|
|
154
|
+
* the top-level IANA segment (e.g. `image`, `audio`, `video`, `text`).
|
|
155
|
+
*
|
|
156
|
+
* `*`-subtype wildcards (e.g. `image/*`) are normalized as equivalent to the
|
|
157
|
+
* top-level segment alone (e.g. `image`). Providers can use the helpers in
|
|
158
|
+
* `@ai-sdk/provider-utils` (`isFullMediaType`, `getTopLevelMediaType`,
|
|
159
|
+
* `detectMediaType`) to resolve the field according to their API
|
|
160
|
+
* requirements.
|
|
161
|
+
*
|
|
162
|
+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
163
|
+
*/
|
|
164
|
+
mediaType: string;
|
|
165
|
+
/**
|
|
166
|
+
* Additional provider-specific metadata. They are passed through
|
|
167
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
168
|
+
* functionality that can be fully encapsulated in the provider.
|
|
169
|
+
*/
|
|
170
|
+
providerOptions?: ProviderOptions;
|
|
171
|
+
}
|
|
303
172
|
/**
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
* @param text - The JSON string to parse.
|
|
307
|
-
* @returns {JSONValue} - The parsed JSON object.
|
|
173
|
+
* Reasoning content part of a prompt. It contains a reasoning.
|
|
308
174
|
*/
|
|
309
|
-
|
|
175
|
+
interface ReasoningPart {
|
|
176
|
+
type: 'reasoning';
|
|
177
|
+
/**
|
|
178
|
+
* The reasoning text.
|
|
179
|
+
*/
|
|
310
180
|
text: string;
|
|
311
|
-
|
|
312
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Additional provider-specific metadata. They are passed through
|
|
183
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
184
|
+
* functionality that can be fully encapsulated in the provider.
|
|
185
|
+
*/
|
|
186
|
+
providerOptions?: ProviderOptions;
|
|
187
|
+
}
|
|
313
188
|
/**
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
* @template T - The type of the object to parse the JSON into.
|
|
317
|
-
* @param {string} text - The JSON string to parse.
|
|
318
|
-
* @param {Validator<T>} schema - The schema to use for parsing the JSON.
|
|
319
|
-
* @returns {Promise<T>} - The parsed object.
|
|
189
|
+
* Custom content part of a prompt. It contains no standardized payload beyond
|
|
190
|
+
* provider-specific options.
|
|
320
191
|
*/
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
192
|
+
interface CustomPart {
|
|
193
|
+
type: 'custom';
|
|
194
|
+
/**
|
|
195
|
+
* The kind of custom content, in the format `{provider}.{provider-type}`.
|
|
196
|
+
*/
|
|
197
|
+
kind: `${string}.${string}`;
|
|
198
|
+
/**
|
|
199
|
+
* Additional provider-specific metadata. They are passed through
|
|
200
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
201
|
+
* functionality that can be fully encapsulated in the provider.
|
|
202
|
+
*/
|
|
203
|
+
providerOptions?: ProviderOptions;
|
|
204
|
+
}
|
|
334
205
|
/**
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
* @param text - The JSON string to parse.
|
|
338
|
-
* @returns {Promise<object>} Either an object with `success: true` and the parsed data, or an object with `success: false` and the error that occurred.
|
|
206
|
+
* Reasoning file content part of a prompt. It contains a file generated as part of reasoning.
|
|
339
207
|
*/
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
208
|
+
interface ReasoningFilePart {
|
|
209
|
+
type: 'reasoning-file';
|
|
210
|
+
/**
|
|
211
|
+
* Reasoning file data.
|
|
212
|
+
*
|
|
213
|
+
* Reasoning files originate from a model's reasoning output and are always
|
|
214
|
+
* raw bytes or a fetchable URL. Unlike `FilePart.data`, the `reference` and
|
|
215
|
+
* `text` shapes are not supported here: provider references describe files
|
|
216
|
+
* uploaded by the user (not produced as model output), and reasoning text is
|
|
217
|
+
* carried by `ReasoningPart` rather than as a file.
|
|
218
|
+
*
|
|
219
|
+
* Either a tagged shape or a bare shorthand:
|
|
220
|
+
*
|
|
221
|
+
* - `{ type: 'data', data }` or bare `DataContent`: raw bytes
|
|
222
|
+
* (base64 string, Uint8Array, ArrayBuffer, Buffer)
|
|
223
|
+
* - `{ type: 'url', url }` or bare `URL`: a URL that points to the file
|
|
224
|
+
*/
|
|
225
|
+
data: FileDataData | FileDataUrl | DataContent | URL;
|
|
226
|
+
/**
|
|
227
|
+
* IANA media type of the file.
|
|
228
|
+
*
|
|
229
|
+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
230
|
+
*/
|
|
231
|
+
mediaType: string;
|
|
232
|
+
/**
|
|
233
|
+
* Additional provider-specific metadata. They are passed through
|
|
234
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
235
|
+
* functionality that can be fully encapsulated in the provider.
|
|
236
|
+
*/
|
|
237
|
+
providerOptions?: ProviderOptions;
|
|
238
|
+
}
|
|
344
239
|
/**
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
* @template T - The type of the object to parse the JSON into.
|
|
348
|
-
* @param {string} text - The JSON string to parse.
|
|
349
|
-
* @param {Validator<T>} schema - The schema to use for parsing the JSON.
|
|
350
|
-
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
|
240
|
+
* Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
|
|
351
241
|
*/
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
declare const getFromApi: <T>({ url, headers, successfulResponseHandler, failedResponseHandler, abortSignal, fetch, }: {
|
|
378
|
-
url: string;
|
|
379
|
-
headers?: Record<string, string | undefined>;
|
|
380
|
-
failedResponseHandler: ResponseHandler<Error>;
|
|
381
|
-
successfulResponseHandler: ResponseHandler<T>;
|
|
382
|
-
abortSignal?: AbortSignal;
|
|
383
|
-
fetch?: FetchFunction;
|
|
384
|
-
}) => Promise<{
|
|
385
|
-
value: T;
|
|
386
|
-
rawValue?: unknown;
|
|
387
|
-
responseHeaders?: Record<string, string>;
|
|
388
|
-
}>;
|
|
389
|
-
|
|
390
|
-
declare function getRuntimeEnvironmentUserAgent(globalThisAny?: any): string;
|
|
391
|
-
|
|
392
|
-
declare function injectJsonInstructionIntoMessages({ messages, schema, schemaPrefix, schemaSuffix, }: {
|
|
393
|
-
messages: LanguageModelV3Prompt;
|
|
394
|
-
schema?: JSONSchema7;
|
|
395
|
-
schemaPrefix?: string;
|
|
396
|
-
schemaSuffix?: string;
|
|
397
|
-
}): LanguageModelV3Prompt;
|
|
398
|
-
|
|
399
|
-
declare function isAbortError(error: unknown): error is Error;
|
|
400
|
-
|
|
242
|
+
interface ToolCallPart {
|
|
243
|
+
type: 'tool-call';
|
|
244
|
+
/**
|
|
245
|
+
* ID of the tool call. This ID is used to match the tool call with the tool result.
|
|
246
|
+
*/
|
|
247
|
+
toolCallId: string;
|
|
248
|
+
/**
|
|
249
|
+
* Name of the tool that is being called.
|
|
250
|
+
*/
|
|
251
|
+
toolName: string;
|
|
252
|
+
/**
|
|
253
|
+
* Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
|
254
|
+
*/
|
|
255
|
+
input: unknown;
|
|
256
|
+
/**
|
|
257
|
+
* Additional provider-specific metadata. They are passed through
|
|
258
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
259
|
+
* functionality that can be fully encapsulated in the provider.
|
|
260
|
+
*/
|
|
261
|
+
providerOptions?: ProviderOptions;
|
|
262
|
+
/**
|
|
263
|
+
* Whether the tool call was executed by the provider.
|
|
264
|
+
*/
|
|
265
|
+
providerExecuted?: boolean;
|
|
266
|
+
}
|
|
401
267
|
/**
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
* @template T - The type of the value to check.
|
|
405
|
-
* @param value - The value to check.
|
|
406
|
-
* @returns `true` if the value is neither `null` nor `undefined`, otherwise `false`.
|
|
268
|
+
* Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
|
|
407
269
|
*/
|
|
408
|
-
|
|
409
|
-
|
|
270
|
+
interface ToolResultPart {
|
|
271
|
+
type: 'tool-result';
|
|
272
|
+
/**
|
|
273
|
+
* ID of the tool call that this result is associated with.
|
|
274
|
+
*/
|
|
275
|
+
toolCallId: string;
|
|
276
|
+
/**
|
|
277
|
+
* Name of the tool that generated this result.
|
|
278
|
+
*/
|
|
279
|
+
toolName: string;
|
|
280
|
+
/**
|
|
281
|
+
* Result of the tool call. This is a JSON-serializable object.
|
|
282
|
+
*/
|
|
283
|
+
output: ToolResultOutput;
|
|
284
|
+
/**
|
|
285
|
+
* Additional provider-specific metadata. They are passed through
|
|
286
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
287
|
+
* functionality that can be fully encapsulated in the provider.
|
|
288
|
+
*/
|
|
289
|
+
providerOptions?: ProviderOptions;
|
|
290
|
+
}
|
|
410
291
|
/**
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
* @param mediaType - The media type of the URL. Case-sensitive.
|
|
414
|
-
* @param url - The URL to check.
|
|
415
|
-
* @param supportedUrls - A record where keys are case-sensitive media types (or '*')
|
|
416
|
-
* and values are arrays of RegExp patterns for URLs.
|
|
417
|
-
*
|
|
418
|
-
* @returns `true` if the URL matches a pattern under the specific media type
|
|
419
|
-
* or the wildcard '*', `false` otherwise.
|
|
292
|
+
* Output of a tool result.
|
|
420
293
|
*/
|
|
421
|
-
|
|
422
|
-
mediaType: string;
|
|
423
|
-
url: string;
|
|
424
|
-
supportedUrls: Record<string, RegExp[]>;
|
|
425
|
-
}): boolean;
|
|
426
|
-
|
|
427
|
-
declare function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterName, description, }: {
|
|
428
|
-
apiKey: string | undefined;
|
|
429
|
-
environmentVariableName: string;
|
|
430
|
-
apiKeyParameterName?: string;
|
|
431
|
-
description: string;
|
|
432
|
-
}): string;
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* Loads an optional `string` setting from the environment or a parameter.
|
|
436
|
-
*
|
|
437
|
-
* @param settingValue - The setting value.
|
|
438
|
-
* @param environmentVariableName - The environment variable name.
|
|
439
|
-
* @returns The setting value.
|
|
440
|
-
*/
|
|
441
|
-
declare function loadOptionalSetting({ settingValue, environmentVariableName, }: {
|
|
442
|
-
settingValue: string | undefined;
|
|
443
|
-
environmentVariableName: string;
|
|
444
|
-
}): string | undefined;
|
|
445
|
-
|
|
446
|
-
/**
|
|
447
|
-
* Loads a `string` setting from the environment or a parameter.
|
|
448
|
-
*
|
|
449
|
-
* @param settingValue - The setting value.
|
|
450
|
-
* @param environmentVariableName - The environment variable name.
|
|
451
|
-
* @param settingName - The setting name.
|
|
452
|
-
* @param description - The description of the setting.
|
|
453
|
-
* @returns The setting value.
|
|
454
|
-
*/
|
|
455
|
-
declare function loadSetting({ settingValue, environmentVariableName, settingName, description, }: {
|
|
456
|
-
settingValue: string | undefined;
|
|
457
|
-
environmentVariableName: string;
|
|
458
|
-
settingName: string;
|
|
459
|
-
description: string;
|
|
460
|
-
}): string;
|
|
461
|
-
|
|
462
|
-
type MaybePromiseLike<T> = T | PromiseLike<T>;
|
|
463
|
-
|
|
464
|
-
/**
|
|
465
|
-
* Maps a media type to its corresponding file extension.
|
|
466
|
-
* It was originally introduced to set a filename for audio file uploads
|
|
467
|
-
* in https://github.com/vercel/ai/pull/8159.
|
|
468
|
-
*
|
|
469
|
-
* @param mediaType The media type to map.
|
|
470
|
-
* @returns The corresponding file extension
|
|
471
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types
|
|
472
|
-
*/
|
|
473
|
-
declare function mediaTypeToExtension(mediaType: string): string;
|
|
474
|
-
|
|
475
|
-
/**
|
|
476
|
-
* Normalizes different header inputs into a plain record with lower-case keys.
|
|
477
|
-
* Entries with `undefined` or `null` values are removed.
|
|
478
|
-
*
|
|
479
|
-
* @param headers - Input headers (`Headers`, tuples array, plain record) to normalize.
|
|
480
|
-
* @returns A record containing the normalized header entries.
|
|
481
|
-
*/
|
|
482
|
-
declare function normalizeHeaders(headers: HeadersInit | Record<string, string | undefined> | Array<[string, string | undefined]> | undefined): Record<string, string>;
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* Parses a JSON event stream into a stream of parsed JSON objects.
|
|
486
|
-
*/
|
|
487
|
-
declare function parseJsonEventStream<T>({ stream, schema, }: {
|
|
488
|
-
stream: ReadableStream<Uint8Array>;
|
|
489
|
-
schema: FlexibleSchema<T>;
|
|
490
|
-
}): ReadableStream<ParseResult<T>>;
|
|
491
|
-
|
|
492
|
-
declare function parseProviderOptions<OPTIONS>({ provider, providerOptions, schema, }: {
|
|
493
|
-
provider: string;
|
|
494
|
-
providerOptions: Record<string, unknown> | undefined;
|
|
495
|
-
schema: FlexibleSchema<OPTIONS>;
|
|
496
|
-
}): Promise<OPTIONS | undefined>;
|
|
497
|
-
|
|
498
|
-
declare const postJsonToApi: <T>({ url, headers, body, failedResponseHandler, successfulResponseHandler, abortSignal, fetch, }: {
|
|
499
|
-
url: string;
|
|
500
|
-
headers?: Record<string, string | undefined>;
|
|
501
|
-
body: unknown;
|
|
502
|
-
failedResponseHandler: ResponseHandler<APICallError>;
|
|
503
|
-
successfulResponseHandler: ResponseHandler<T>;
|
|
504
|
-
abortSignal?: AbortSignal;
|
|
505
|
-
fetch?: FetchFunction;
|
|
506
|
-
}) => Promise<{
|
|
507
|
-
value: T;
|
|
508
|
-
rawValue?: unknown;
|
|
509
|
-
responseHeaders?: Record<string, string>;
|
|
510
|
-
}>;
|
|
511
|
-
declare const postFormDataToApi: <T>({ url, headers, formData, failedResponseHandler, successfulResponseHandler, abortSignal, fetch, }: {
|
|
512
|
-
url: string;
|
|
513
|
-
headers?: Record<string, string | undefined>;
|
|
514
|
-
formData: FormData;
|
|
515
|
-
failedResponseHandler: ResponseHandler<APICallError>;
|
|
516
|
-
successfulResponseHandler: ResponseHandler<T>;
|
|
517
|
-
abortSignal?: AbortSignal;
|
|
518
|
-
fetch?: FetchFunction;
|
|
519
|
-
}) => Promise<{
|
|
520
|
-
value: T;
|
|
521
|
-
rawValue?: unknown;
|
|
522
|
-
responseHeaders?: Record<string, string>;
|
|
523
|
-
}>;
|
|
524
|
-
declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, failedResponseHandler, abortSignal, fetch, }: {
|
|
525
|
-
url: string;
|
|
526
|
-
headers?: Record<string, string | undefined>;
|
|
527
|
-
body: {
|
|
528
|
-
content: string | FormData | Uint8Array;
|
|
529
|
-
values: unknown;
|
|
530
|
-
};
|
|
531
|
-
failedResponseHandler: ResponseHandler<Error>;
|
|
532
|
-
successfulResponseHandler: ResponseHandler<T>;
|
|
533
|
-
abortSignal?: AbortSignal;
|
|
534
|
-
fetch?: FetchFunction;
|
|
535
|
-
}) => Promise<{
|
|
536
|
-
value: T;
|
|
537
|
-
rawValue?: unknown;
|
|
538
|
-
responseHeaders?: Record<string, string>;
|
|
539
|
-
}>;
|
|
540
|
-
|
|
541
|
-
/**
|
|
542
|
-
* Data content. Can either be a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer.
|
|
543
|
-
*/
|
|
544
|
-
type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
|
|
545
|
-
|
|
546
|
-
/**
|
|
547
|
-
* Additional provider-specific options.
|
|
548
|
-
*
|
|
549
|
-
* They are passed through to the provider from the AI SDK and enable
|
|
550
|
-
* provider-specific functionality that can be fully encapsulated in the provider.
|
|
551
|
-
*/
|
|
552
|
-
type ProviderOptions = SharedV3ProviderOptions;
|
|
553
|
-
|
|
554
|
-
/**
|
|
555
|
-
* Text content part of a prompt. It contains a string of text.
|
|
556
|
-
*/
|
|
557
|
-
interface TextPart {
|
|
558
|
-
type: 'text';
|
|
294
|
+
type ToolResultOutput = {
|
|
559
295
|
/**
|
|
560
|
-
*
|
|
296
|
+
* Text tool output that should be directly sent to the API.
|
|
561
297
|
*/
|
|
562
|
-
|
|
298
|
+
type: 'text';
|
|
299
|
+
value: string;
|
|
563
300
|
/**
|
|
564
|
-
*
|
|
565
|
-
* to the provider from the AI SDK and enable provider-specific
|
|
566
|
-
* functionality that can be fully encapsulated in the provider.
|
|
301
|
+
* Provider-specific options.
|
|
567
302
|
*/
|
|
568
303
|
providerOptions?: ProviderOptions;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
*/
|
|
573
|
-
interface ImagePart {
|
|
574
|
-
type: 'image';
|
|
575
|
-
/**
|
|
576
|
-
* Image data. Can either be:
|
|
577
|
-
*
|
|
578
|
-
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
579
|
-
* - URL: a URL that points to the image
|
|
580
|
-
*/
|
|
581
|
-
image: DataContent | URL;
|
|
582
|
-
/**
|
|
583
|
-
* Optional IANA media type of the image.
|
|
584
|
-
*
|
|
585
|
-
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
586
|
-
*/
|
|
587
|
-
mediaType?: string;
|
|
304
|
+
} | {
|
|
305
|
+
type: 'json';
|
|
306
|
+
value: JSONValue;
|
|
588
307
|
/**
|
|
589
|
-
*
|
|
590
|
-
* to the provider from the AI SDK and enable provider-specific
|
|
591
|
-
* functionality that can be fully encapsulated in the provider.
|
|
308
|
+
* Provider-specific options.
|
|
592
309
|
*/
|
|
593
310
|
providerOptions?: ProviderOptions;
|
|
594
|
-
}
|
|
595
|
-
/**
|
|
596
|
-
* File content part of a prompt. It contains a file.
|
|
597
|
-
*/
|
|
598
|
-
interface FilePart {
|
|
599
|
-
type: 'file';
|
|
600
|
-
/**
|
|
601
|
-
* File data. Can either be:
|
|
602
|
-
*
|
|
603
|
-
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
604
|
-
* - URL: a URL that points to the image
|
|
605
|
-
*/
|
|
606
|
-
data: DataContent | URL;
|
|
607
|
-
/**
|
|
608
|
-
* Optional filename of the file.
|
|
609
|
-
*/
|
|
610
|
-
filename?: string;
|
|
611
|
-
/**
|
|
612
|
-
* IANA media type of the file.
|
|
613
|
-
*
|
|
614
|
-
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
615
|
-
*/
|
|
616
|
-
mediaType: string;
|
|
311
|
+
} | {
|
|
617
312
|
/**
|
|
618
|
-
*
|
|
619
|
-
* to the provider from the AI SDK and enable provider-specific
|
|
620
|
-
* functionality that can be fully encapsulated in the provider.
|
|
313
|
+
* Type when the user has denied the execution of the tool call.
|
|
621
314
|
*/
|
|
622
|
-
|
|
623
|
-
}
|
|
624
|
-
/**
|
|
625
|
-
* Reasoning content part of a prompt. It contains a reasoning.
|
|
626
|
-
*/
|
|
627
|
-
interface ReasoningPart {
|
|
628
|
-
type: 'reasoning';
|
|
315
|
+
type: 'execution-denied';
|
|
629
316
|
/**
|
|
630
|
-
*
|
|
317
|
+
* Optional reason for the execution denial.
|
|
631
318
|
*/
|
|
632
|
-
|
|
319
|
+
reason?: string;
|
|
633
320
|
/**
|
|
634
|
-
*
|
|
635
|
-
* to the provider from the AI SDK and enable provider-specific
|
|
636
|
-
* functionality that can be fully encapsulated in the provider.
|
|
321
|
+
* Provider-specific options.
|
|
637
322
|
*/
|
|
638
323
|
providerOptions?: ProviderOptions;
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
*/
|
|
643
|
-
interface ReasoningFilePart {
|
|
644
|
-
type: 'reasoning-file';
|
|
645
|
-
/**
|
|
646
|
-
* File data. Can either be:
|
|
647
|
-
*
|
|
648
|
-
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
649
|
-
* - URL: a URL that points to the file
|
|
650
|
-
*/
|
|
651
|
-
data: DataContent | URL;
|
|
652
|
-
/**
|
|
653
|
-
* IANA media type of the file.
|
|
654
|
-
*
|
|
655
|
-
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
656
|
-
*/
|
|
657
|
-
mediaType: string;
|
|
324
|
+
} | {
|
|
325
|
+
type: 'error-text';
|
|
326
|
+
value: string;
|
|
658
327
|
/**
|
|
659
|
-
*
|
|
660
|
-
* to the provider from the AI SDK and enable provider-specific
|
|
661
|
-
* functionality that can be fully encapsulated in the provider.
|
|
328
|
+
* Provider-specific options.
|
|
662
329
|
*/
|
|
663
330
|
providerOptions?: ProviderOptions;
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
*/
|
|
668
|
-
interface ToolCallPart {
|
|
669
|
-
type: 'tool-call';
|
|
670
|
-
/**
|
|
671
|
-
* ID of the tool call. This ID is used to match the tool call with the tool result.
|
|
672
|
-
*/
|
|
673
|
-
toolCallId: string;
|
|
674
|
-
/**
|
|
675
|
-
* Name of the tool that is being called.
|
|
676
|
-
*/
|
|
677
|
-
toolName: string;
|
|
678
|
-
/**
|
|
679
|
-
* Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
|
680
|
-
*/
|
|
681
|
-
input: unknown;
|
|
331
|
+
} | {
|
|
332
|
+
type: 'error-json';
|
|
333
|
+
value: JSONValue;
|
|
682
334
|
/**
|
|
683
|
-
*
|
|
684
|
-
* to the provider from the AI SDK and enable provider-specific
|
|
685
|
-
* functionality that can be fully encapsulated in the provider.
|
|
686
|
-
*/
|
|
687
|
-
providerOptions?: ProviderOptions;
|
|
688
|
-
/**
|
|
689
|
-
* Whether the tool call was executed by the provider.
|
|
690
|
-
*/
|
|
691
|
-
providerExecuted?: boolean;
|
|
692
|
-
}
|
|
693
|
-
/**
|
|
694
|
-
* Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
|
|
695
|
-
*/
|
|
696
|
-
interface ToolResultPart {
|
|
697
|
-
type: 'tool-result';
|
|
698
|
-
/**
|
|
699
|
-
* ID of the tool call that this result is associated with.
|
|
700
|
-
*/
|
|
701
|
-
toolCallId: string;
|
|
702
|
-
/**
|
|
703
|
-
* Name of the tool that generated this result.
|
|
704
|
-
*/
|
|
705
|
-
toolName: string;
|
|
706
|
-
/**
|
|
707
|
-
* Result of the tool call. This is a JSON-serializable object.
|
|
708
|
-
*/
|
|
709
|
-
output: ToolResultOutput;
|
|
710
|
-
/**
|
|
711
|
-
* Additional provider-specific metadata. They are passed through
|
|
712
|
-
* to the provider from the AI SDK and enable provider-specific
|
|
713
|
-
* functionality that can be fully encapsulated in the provider.
|
|
714
|
-
*/
|
|
715
|
-
providerOptions?: ProviderOptions;
|
|
716
|
-
}
|
|
717
|
-
/**
|
|
718
|
-
* Output of a tool result.
|
|
719
|
-
*/
|
|
720
|
-
type ToolResultOutput = {
|
|
721
|
-
/**
|
|
722
|
-
* Text tool output that should be directly sent to the API.
|
|
723
|
-
*/
|
|
724
|
-
type: 'text';
|
|
725
|
-
value: string;
|
|
726
|
-
/**
|
|
727
|
-
* Provider-specific options.
|
|
728
|
-
*/
|
|
729
|
-
providerOptions?: ProviderOptions;
|
|
730
|
-
} | {
|
|
731
|
-
type: 'json';
|
|
732
|
-
value: JSONValue;
|
|
733
|
-
/**
|
|
734
|
-
* Provider-specific options.
|
|
735
|
-
*/
|
|
736
|
-
providerOptions?: ProviderOptions;
|
|
737
|
-
} | {
|
|
738
|
-
/**
|
|
739
|
-
* Type when the user has denied the execution of the tool call.
|
|
740
|
-
*/
|
|
741
|
-
type: 'execution-denied';
|
|
742
|
-
/**
|
|
743
|
-
* Optional reason for the execution denial.
|
|
744
|
-
*/
|
|
745
|
-
reason?: string;
|
|
746
|
-
/**
|
|
747
|
-
* Provider-specific options.
|
|
748
|
-
*/
|
|
749
|
-
providerOptions?: ProviderOptions;
|
|
750
|
-
} | {
|
|
751
|
-
type: 'error-text';
|
|
752
|
-
value: string;
|
|
753
|
-
/**
|
|
754
|
-
* Provider-specific options.
|
|
755
|
-
*/
|
|
756
|
-
providerOptions?: ProviderOptions;
|
|
757
|
-
} | {
|
|
758
|
-
type: 'error-json';
|
|
759
|
-
value: JSONValue;
|
|
760
|
-
/**
|
|
761
|
-
* Provider-specific options.
|
|
335
|
+
* Provider-specific options.
|
|
762
336
|
*/
|
|
763
337
|
providerOptions?: ProviderOptions;
|
|
764
338
|
} | {
|
|
@@ -773,13 +347,6 @@ type ToolResultOutput = {
|
|
|
773
347
|
* Provider-specific options.
|
|
774
348
|
*/
|
|
775
349
|
providerOptions?: ProviderOptions;
|
|
776
|
-
} | {
|
|
777
|
-
/**
|
|
778
|
-
* @deprecated Use image-data or file-data instead.
|
|
779
|
-
*/
|
|
780
|
-
type: 'media';
|
|
781
|
-
data: string;
|
|
782
|
-
mediaType: string;
|
|
783
350
|
} | {
|
|
784
351
|
type: 'file-data';
|
|
785
352
|
/**
|
|
@@ -805,11 +372,19 @@ type ToolResultOutput = {
|
|
|
805
372
|
* URL of the file.
|
|
806
373
|
*/
|
|
807
374
|
url: string;
|
|
375
|
+
/**
|
|
376
|
+
* IANA media type.
|
|
377
|
+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
378
|
+
*/
|
|
379
|
+
mediaType?: string;
|
|
808
380
|
/**
|
|
809
381
|
* Provider-specific options.
|
|
810
382
|
*/
|
|
811
383
|
providerOptions?: ProviderOptions;
|
|
812
384
|
} | {
|
|
385
|
+
/**
|
|
386
|
+
* @deprecated Use file-reference instead.
|
|
387
|
+
*/
|
|
813
388
|
type: 'file-id';
|
|
814
389
|
/**
|
|
815
390
|
* ID of the file.
|
|
@@ -824,9 +399,20 @@ type ToolResultOutput = {
|
|
|
824
399
|
* Provider-specific options.
|
|
825
400
|
*/
|
|
826
401
|
providerOptions?: ProviderOptions;
|
|
402
|
+
} | {
|
|
403
|
+
type: 'file-reference';
|
|
404
|
+
/**
|
|
405
|
+
* Provider-specific references for the file.
|
|
406
|
+
* The key is the provider name, e.g. 'openai' or 'anthropic'.
|
|
407
|
+
*/
|
|
408
|
+
providerReference: ProviderReference;
|
|
409
|
+
/**
|
|
410
|
+
* Provider-specific options.
|
|
411
|
+
*/
|
|
412
|
+
providerOptions?: ProviderOptions;
|
|
827
413
|
} | {
|
|
828
414
|
/**
|
|
829
|
-
*
|
|
415
|
+
* @deprecated Use file-data instead.
|
|
830
416
|
*/
|
|
831
417
|
type: 'image-data';
|
|
832
418
|
/**
|
|
@@ -844,7 +430,7 @@ type ToolResultOutput = {
|
|
|
844
430
|
providerOptions?: ProviderOptions;
|
|
845
431
|
} | {
|
|
846
432
|
/**
|
|
847
|
-
*
|
|
433
|
+
* @deprecated Use file-url instead.
|
|
848
434
|
*/
|
|
849
435
|
type: 'image-url';
|
|
850
436
|
/**
|
|
@@ -857,7 +443,7 @@ type ToolResultOutput = {
|
|
|
857
443
|
providerOptions?: ProviderOptions;
|
|
858
444
|
} | {
|
|
859
445
|
/**
|
|
860
|
-
*
|
|
446
|
+
* @deprecated Use file-reference instead.
|
|
861
447
|
*/
|
|
862
448
|
type: 'image-file-id';
|
|
863
449
|
/**
|
|
@@ -873,6 +459,20 @@ type ToolResultOutput = {
|
|
|
873
459
|
* Provider-specific options.
|
|
874
460
|
*/
|
|
875
461
|
providerOptions?: ProviderOptions;
|
|
462
|
+
} | {
|
|
463
|
+
/**
|
|
464
|
+
* @deprecated Use file-reference instead.
|
|
465
|
+
*/
|
|
466
|
+
type: 'image-file-reference';
|
|
467
|
+
/**
|
|
468
|
+
* Provider-specific references for the image file.
|
|
469
|
+
* The key is the provider name, e.g. 'openai' or 'anthropic'.
|
|
470
|
+
*/
|
|
471
|
+
providerReference: ProviderReference;
|
|
472
|
+
/**
|
|
473
|
+
* Provider-specific options.
|
|
474
|
+
*/
|
|
475
|
+
providerOptions?: ProviderOptions;
|
|
876
476
|
} | {
|
|
877
477
|
/**
|
|
878
478
|
* Custom content part. This can be used to implement
|
|
@@ -886,6 +486,630 @@ type ToolResultOutput = {
|
|
|
886
486
|
}>;
|
|
887
487
|
};
|
|
888
488
|
|
|
489
|
+
type InlineFileData = Extract<FilePart['data'], {
|
|
490
|
+
type: 'data';
|
|
491
|
+
} | {
|
|
492
|
+
type: 'text';
|
|
493
|
+
}>;
|
|
494
|
+
/**
|
|
495
|
+
* Converts inline file data (a tagged `data` or `text` shape) into raw bytes.
|
|
496
|
+
*
|
|
497
|
+
* - `{ type: 'text', text }` → UTF-8 encoded bytes
|
|
498
|
+
* - `{ type: 'data', data: Uint8Array | Buffer }` → returned as-is
|
|
499
|
+
* - `{ type: 'data', data: ArrayBuffer }` → wrapped in a `Uint8Array`
|
|
500
|
+
* - `{ type: 'data', data: string }` → decoded as base64
|
|
501
|
+
*/
|
|
502
|
+
declare function convertInlineFileDataToUint8Array(data: InlineFileData): Uint8Array;
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Convert an ImageModelV4File to a URL or data URI string.
|
|
506
|
+
*
|
|
507
|
+
* If the file is a URL, it returns the URL as-is.
|
|
508
|
+
* If the file is base64 data, it returns a data URI with the base64 data.
|
|
509
|
+
* If the file is a Uint8Array, it converts it to base64 and returns a data URI.
|
|
510
|
+
*/
|
|
511
|
+
declare function convertImageModelFileToDataUri(file: ImageModelV4File): string;
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Converts an input object to FormData for multipart/form-data requests.
|
|
515
|
+
*
|
|
516
|
+
* Handles the following cases:
|
|
517
|
+
* - `null` or `undefined` values are skipped
|
|
518
|
+
* - Arrays with a single element are appended as a single value
|
|
519
|
+
* - Arrays with multiple elements are appended with `[]` suffix (e.g., `image[]`)
|
|
520
|
+
* unless `useArrayBrackets` is set to `false`
|
|
521
|
+
* - All other values are appended directly
|
|
522
|
+
*
|
|
523
|
+
* @param input - The input object to convert. Use a generic type for type validation.
|
|
524
|
+
* @param options - Optional configuration object.
|
|
525
|
+
* @param options.useArrayBrackets - Whether to add `[]` suffix for multi-element arrays.
|
|
526
|
+
* Defaults to `true`. Set to `false` for APIs that expect repeated keys without brackets.
|
|
527
|
+
* @returns A FormData object containing the input values.
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* ```ts
|
|
531
|
+
* type MyInput = {
|
|
532
|
+
* model: string;
|
|
533
|
+
* prompt: string;
|
|
534
|
+
* images: Blob[];
|
|
535
|
+
* };
|
|
536
|
+
*
|
|
537
|
+
* const formData = convertToFormData<MyInput>({
|
|
538
|
+
* model: 'gpt-image-1',
|
|
539
|
+
* prompt: 'A cat',
|
|
540
|
+
* images: [blob1, blob2],
|
|
541
|
+
* });
|
|
542
|
+
* ```
|
|
543
|
+
*/
|
|
544
|
+
declare function convertToFormData<T extends Record<string, unknown>>(input: T, options?: {
|
|
545
|
+
useArrayBrackets?: boolean;
|
|
546
|
+
}): FormData;
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Interface for mapping between custom tool names and provider tool names.
|
|
550
|
+
*/
|
|
551
|
+
interface ToolNameMapping {
|
|
552
|
+
/**
|
|
553
|
+
* Maps a custom tool name (used by the client) to the provider's tool name.
|
|
554
|
+
* If the custom tool name does not have a mapping, returns the input name.
|
|
555
|
+
*
|
|
556
|
+
* @param customToolName - The custom name of the tool defined by the client.
|
|
557
|
+
* @returns The corresponding provider tool name, or the input name if not mapped.
|
|
558
|
+
*/
|
|
559
|
+
toProviderToolName: (customToolName: string) => string;
|
|
560
|
+
/**
|
|
561
|
+
* Maps a provider tool name to the custom tool name used by the client.
|
|
562
|
+
* If the provider tool name does not have a mapping, returns the input name.
|
|
563
|
+
*
|
|
564
|
+
* @param providerToolName - The name of the tool as understood by the provider.
|
|
565
|
+
* @returns The corresponding custom tool name, or the input name if not mapped.
|
|
566
|
+
*/
|
|
567
|
+
toCustomToolName: (providerToolName: string) => string;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* @param tools - Tools that were passed to the language model.
|
|
571
|
+
* @param providerToolNames - Maps the provider tool ids to the provider tool names.
|
|
572
|
+
*/
|
|
573
|
+
declare function createToolNameMapping({ tools, providerToolNames, }: {
|
|
574
|
+
/**
|
|
575
|
+
* Tools that were passed to the language model.
|
|
576
|
+
*/
|
|
577
|
+
tools: Array<LanguageModelV4FunctionTool | LanguageModelV4ProviderTool> | undefined;
|
|
578
|
+
/**
|
|
579
|
+
* Maps the provider tool ids to the provider tool names.
|
|
580
|
+
*/
|
|
581
|
+
providerToolNames: Record<`${string}.${string}`, string>;
|
|
582
|
+
}): ToolNameMapping;
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Creates a Promise that resolves after a specified delay
|
|
586
|
+
* @param delayInMs - The delay duration in milliseconds. If null or undefined, resolves immediately.
|
|
587
|
+
* @param signal - Optional AbortSignal to cancel the delay
|
|
588
|
+
* @returns A Promise that resolves after the specified delay
|
|
589
|
+
* @throws {DOMException} When the signal is aborted
|
|
590
|
+
*/
|
|
591
|
+
declare function delay(delayInMs?: number | null, options?: {
|
|
592
|
+
abortSignal?: AbortSignal;
|
|
593
|
+
}): Promise<void>;
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Delayed promise. It is only constructed once the value is accessed.
|
|
597
|
+
* This is useful to avoid unhandled promise rejections when the promise is created
|
|
598
|
+
* but not accessed.
|
|
599
|
+
*/
|
|
600
|
+
declare class DelayedPromise<T> {
|
|
601
|
+
private status;
|
|
602
|
+
private _promise;
|
|
603
|
+
private _resolve;
|
|
604
|
+
private _reject;
|
|
605
|
+
get promise(): Promise<T>;
|
|
606
|
+
resolve(value: T): void;
|
|
607
|
+
reject(error: unknown): void;
|
|
608
|
+
isResolved(): boolean;
|
|
609
|
+
isRejected(): boolean;
|
|
610
|
+
isPending(): boolean;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Detect the IANA media type of a file from its raw bytes or base64 string.
|
|
615
|
+
*
|
|
616
|
+
* - When `topLevelType` is omitted, every known signature is considered
|
|
617
|
+
* (image, audio, video, and application). Returns `undefined` when the
|
|
618
|
+
* bytes do not match any known signature.
|
|
619
|
+
* - When `topLevelType` is provided, only signatures for that top-level
|
|
620
|
+
* segment are considered. Returns `undefined` for unsupported segments
|
|
621
|
+
* (e.g. `"text"`) or when no signature matches.
|
|
622
|
+
*/
|
|
623
|
+
declare function detectMediaType({ data, topLevelType, }: {
|
|
624
|
+
data: Uint8Array | string;
|
|
625
|
+
topLevelType?: string;
|
|
626
|
+
}): string | undefined;
|
|
627
|
+
/**
|
|
628
|
+
* Returns the top-level segment of a media type (the portion before `/`).
|
|
629
|
+
*
|
|
630
|
+
* Examples:
|
|
631
|
+
* - `"image/png"` -> `"image"`
|
|
632
|
+
* - `"image/*"` -> `"image"`
|
|
633
|
+
* - `"image"` -> `"image"`
|
|
634
|
+
* - `"image/"` -> `"image"`
|
|
635
|
+
* - `""` -> `""`
|
|
636
|
+
* - `"/"` -> `""`
|
|
637
|
+
*/
|
|
638
|
+
declare function getTopLevelMediaType(mediaType: string): string;
|
|
639
|
+
/**
|
|
640
|
+
* Returns `true` only when the given media type has a non-empty, non-wildcard
|
|
641
|
+
* subtype (i.e. matches the form `type/subtype`, and `subtype` is not `*`).
|
|
642
|
+
*
|
|
643
|
+
* Examples:
|
|
644
|
+
* - `"image/png"` -> `true`
|
|
645
|
+
* - `"image/*"` -> `false`
|
|
646
|
+
* - `"image"` -> `false`
|
|
647
|
+
* - `"image/"` -> `false`
|
|
648
|
+
* - `""` -> `false`
|
|
649
|
+
* - `"/"` -> `false`
|
|
650
|
+
*/
|
|
651
|
+
declare function isFullMediaType(mediaType: string): boolean;
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Download a file from a URL and return it as a Blob.
|
|
655
|
+
*
|
|
656
|
+
* @param url - The URL to download from.
|
|
657
|
+
* @param options - Optional settings for the download.
|
|
658
|
+
* @param options.maxBytes - Maximum allowed download size in bytes. Defaults to 100 MiB.
|
|
659
|
+
* @param options.abortSignal - An optional abort signal to cancel the download.
|
|
660
|
+
* @returns A Promise that resolves to the downloaded Blob.
|
|
661
|
+
*
|
|
662
|
+
* @throws DownloadError if the download fails or exceeds maxBytes.
|
|
663
|
+
*/
|
|
664
|
+
declare function downloadBlob(url: string, options?: {
|
|
665
|
+
maxBytes?: number;
|
|
666
|
+
abortSignal?: AbortSignal;
|
|
667
|
+
}): Promise<Blob>;
|
|
668
|
+
|
|
669
|
+
declare const symbol: unique symbol;
|
|
670
|
+
declare class DownloadError extends AISDKError {
|
|
671
|
+
private readonly [symbol];
|
|
672
|
+
readonly url: string;
|
|
673
|
+
readonly statusCode?: number;
|
|
674
|
+
readonly statusText?: string;
|
|
675
|
+
constructor({ url, statusCode, statusText, cause, message, }: {
|
|
676
|
+
url: string;
|
|
677
|
+
statusCode?: number;
|
|
678
|
+
statusText?: string;
|
|
679
|
+
message?: string;
|
|
680
|
+
cause?: unknown;
|
|
681
|
+
});
|
|
682
|
+
static isInstance(error: unknown): error is DownloadError;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* Extracts the headers from a response object and returns them as a key-value object.
|
|
687
|
+
*
|
|
688
|
+
* @param response - The response object to extract headers from.
|
|
689
|
+
* @returns The headers as a key-value object.
|
|
690
|
+
*/
|
|
691
|
+
declare function extractResponseHeaders(response: Response): {
|
|
692
|
+
[k: string]: string;
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* Fetch function type (standardizes the version of fetch used).
|
|
697
|
+
*/
|
|
698
|
+
type FetchFunction = typeof globalThis.fetch;
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Filters `null` and `undefined` values out of a list of values.
|
|
702
|
+
*
|
|
703
|
+
* @param values - The values to filter.
|
|
704
|
+
* @returns A new array containing only non-nullish values.
|
|
705
|
+
*/
|
|
706
|
+
declare function filterNullable<T>(...values: Array<T | undefined | null>): Array<T>;
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Creates an ID generator.
|
|
710
|
+
* The total length of the ID is the sum of the prefix, separator, and random part length.
|
|
711
|
+
* Not cryptographically secure.
|
|
712
|
+
*
|
|
713
|
+
* @param alphabet - The alphabet to use for the ID. Default: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.
|
|
714
|
+
* @param prefix - The prefix of the ID to generate. Optional.
|
|
715
|
+
* @param separator - The separator between the prefix and the random part of the ID. Default: '-'.
|
|
716
|
+
* @param size - The size of the random part of the ID to generate. Default: 16.
|
|
717
|
+
*/
|
|
718
|
+
declare const createIdGenerator: ({ prefix, size, alphabet, separator, }?: {
|
|
719
|
+
prefix?: string;
|
|
720
|
+
separator?: string;
|
|
721
|
+
size?: number;
|
|
722
|
+
alphabet?: string;
|
|
723
|
+
}) => IdGenerator;
|
|
724
|
+
/**
|
|
725
|
+
* A function that generates an ID.
|
|
726
|
+
*/
|
|
727
|
+
type IdGenerator = () => string;
|
|
728
|
+
/**
|
|
729
|
+
* Generates a 16-character random string to use for IDs.
|
|
730
|
+
* Not cryptographically secure.
|
|
731
|
+
*/
|
|
732
|
+
declare const generateId: IdGenerator;
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Used to mark schemas so we can support both Zod and custom schemas.
|
|
736
|
+
*/
|
|
737
|
+
declare const schemaSymbol: unique symbol;
|
|
738
|
+
type ValidationResult<OBJECT> = {
|
|
739
|
+
success: true;
|
|
740
|
+
value: OBJECT;
|
|
741
|
+
} | {
|
|
742
|
+
success: false;
|
|
743
|
+
error: Error;
|
|
744
|
+
};
|
|
745
|
+
type Schema<OBJECT = unknown> = {
|
|
746
|
+
/**
|
|
747
|
+
* Used to mark schemas so we can support both Zod and custom schemas.
|
|
748
|
+
*/
|
|
749
|
+
[schemaSymbol]: true;
|
|
750
|
+
/**
|
|
751
|
+
* Schema type for inference.
|
|
752
|
+
*/
|
|
753
|
+
_type: OBJECT;
|
|
754
|
+
/**
|
|
755
|
+
* Optional. Validates that the structure of a value matches this schema,
|
|
756
|
+
* and returns a typed version of the value if it does.
|
|
757
|
+
*/
|
|
758
|
+
readonly validate?: (value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>;
|
|
759
|
+
/**
|
|
760
|
+
* The JSON Schema for the schema. It is passed to the providers.
|
|
761
|
+
*/
|
|
762
|
+
readonly jsonSchema: JSONSchema7 | PromiseLike<JSONSchema7>;
|
|
763
|
+
};
|
|
764
|
+
/**
|
|
765
|
+
* Creates a schema with deferred creation.
|
|
766
|
+
* This is important to reduce the startup time of the library
|
|
767
|
+
* and to avoid initializing unused validators.
|
|
768
|
+
*
|
|
769
|
+
* @param createValidator A function that creates a schema.
|
|
770
|
+
* @returns A function that returns a schema.
|
|
771
|
+
*/
|
|
772
|
+
declare function lazySchema<SCHEMA>(createSchema: () => Schema<SCHEMA>): LazySchema<SCHEMA>;
|
|
773
|
+
type LazySchema<SCHEMA> = () => Schema<SCHEMA>;
|
|
774
|
+
type ZodSchema<SCHEMA = any> = z3.Schema<SCHEMA, z3.ZodTypeDef, any> | z4.core.$ZodType<SCHEMA, any>;
|
|
775
|
+
type StandardSchema<SCHEMA = any> = StandardSchemaV1<unknown, SCHEMA> & StandardJSONSchemaV1<unknown, SCHEMA>;
|
|
776
|
+
type FlexibleSchema<SCHEMA = any> = Schema<SCHEMA> | LazySchema<SCHEMA> | ZodSchema<SCHEMA> | StandardSchema<SCHEMA>;
|
|
777
|
+
type InferSchema<SCHEMA> = SCHEMA extends ZodSchema<infer T> ? T : SCHEMA extends StandardSchema<infer T> ? T : SCHEMA extends LazySchema<infer T> ? T : SCHEMA extends Schema<infer T> ? T : never;
|
|
778
|
+
/**
|
|
779
|
+
* Create a schema using a JSON Schema.
|
|
780
|
+
*
|
|
781
|
+
* @param jsonSchema The JSON Schema for the schema.
|
|
782
|
+
* @param options.validate Optional. A validation function for the schema.
|
|
783
|
+
*/
|
|
784
|
+
declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7 | PromiseLike<JSONSchema7> | (() => JSONSchema7 | PromiseLike<JSONSchema7>), { validate, }?: {
|
|
785
|
+
validate?: (value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>;
|
|
786
|
+
}): Schema<OBJECT>;
|
|
787
|
+
declare function asSchema<OBJECT>(schema: FlexibleSchema<OBJECT> | undefined): Schema<OBJECT>;
|
|
788
|
+
declare function zodSchema<OBJECT>(zodSchema: z4.core.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
|
|
789
|
+
/**
|
|
790
|
+
* Enables support for references in the schema.
|
|
791
|
+
* This is required for recursive schemas, e.g. with `z.lazy`.
|
|
792
|
+
* However, not all language models and providers support such references.
|
|
793
|
+
* Defaults to `false`.
|
|
794
|
+
*/
|
|
795
|
+
useReferences?: boolean;
|
|
796
|
+
}): Schema<OBJECT>;
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Parses a JSON string into an unknown object.
|
|
800
|
+
*
|
|
801
|
+
* @param text - The JSON string to parse.
|
|
802
|
+
* @returns {JSONValue} - The parsed JSON object.
|
|
803
|
+
*/
|
|
804
|
+
declare function parseJSON(options: {
|
|
805
|
+
text: string;
|
|
806
|
+
schema?: undefined;
|
|
807
|
+
}): Promise<JSONValue>;
|
|
808
|
+
/**
|
|
809
|
+
* Parses a JSON string into a strongly-typed object using the provided schema.
|
|
810
|
+
*
|
|
811
|
+
* @template T - The type of the object to parse the JSON into.
|
|
812
|
+
* @param {string} text - The JSON string to parse.
|
|
813
|
+
* @param {Validator<T>} schema - The schema to use for parsing the JSON.
|
|
814
|
+
* @returns {Promise<T>} - The parsed object.
|
|
815
|
+
*/
|
|
816
|
+
declare function parseJSON<T>(options: {
|
|
817
|
+
text: string;
|
|
818
|
+
schema: FlexibleSchema<T>;
|
|
819
|
+
}): Promise<T>;
|
|
820
|
+
type ParseResult<T> = {
|
|
821
|
+
success: true;
|
|
822
|
+
value: T;
|
|
823
|
+
rawValue: unknown;
|
|
824
|
+
} | {
|
|
825
|
+
success: false;
|
|
826
|
+
error: JSONParseError | TypeValidationError;
|
|
827
|
+
rawValue: unknown;
|
|
828
|
+
};
|
|
829
|
+
/**
|
|
830
|
+
* Safely parses a JSON string and returns the result as an object of type `unknown`.
|
|
831
|
+
*
|
|
832
|
+
* @param text - The JSON string to parse.
|
|
833
|
+
* @returns {Promise<object>} Either an object with `success: true` and the parsed data, or an object with `success: false` and the error that occurred.
|
|
834
|
+
*/
|
|
835
|
+
declare function safeParseJSON(options: {
|
|
836
|
+
text: string;
|
|
837
|
+
schema?: undefined;
|
|
838
|
+
}): Promise<ParseResult<JSONValue>>;
|
|
839
|
+
/**
|
|
840
|
+
* Safely parses a JSON string into a strongly-typed object, using a provided schema to validate the object.
|
|
841
|
+
*
|
|
842
|
+
* @template T - The type of the object to parse the JSON into.
|
|
843
|
+
* @param {string} text - The JSON string to parse.
|
|
844
|
+
* @param {Validator<T>} schema - The schema to use for parsing the JSON.
|
|
845
|
+
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
|
846
|
+
*/
|
|
847
|
+
declare function safeParseJSON<T>(options: {
|
|
848
|
+
text: string;
|
|
849
|
+
schema: FlexibleSchema<T>;
|
|
850
|
+
}): Promise<ParseResult<T>>;
|
|
851
|
+
declare function isParsableJson(input: string): boolean;
|
|
852
|
+
|
|
853
|
+
type ResponseHandler<RETURN_TYPE> = (options: {
|
|
854
|
+
url: string;
|
|
855
|
+
requestBodyValues: unknown;
|
|
856
|
+
response: Response;
|
|
857
|
+
}) => PromiseLike<{
|
|
858
|
+
value: RETURN_TYPE;
|
|
859
|
+
rawValue?: unknown;
|
|
860
|
+
responseHeaders?: Record<string, string>;
|
|
861
|
+
}>;
|
|
862
|
+
declare const createJsonErrorResponseHandler: <T>({ errorSchema, errorToMessage, isRetryable, }: {
|
|
863
|
+
errorSchema: FlexibleSchema<T>;
|
|
864
|
+
errorToMessage: (error: T) => string;
|
|
865
|
+
isRetryable?: (response: Response, error?: T) => boolean;
|
|
866
|
+
}) => ResponseHandler<APICallError>;
|
|
867
|
+
declare const createEventSourceResponseHandler: <T>(chunkSchema: FlexibleSchema<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
|
|
868
|
+
declare const createJsonResponseHandler: <T>(responseSchema: FlexibleSchema<T>) => ResponseHandler<T>;
|
|
869
|
+
declare const createBinaryResponseHandler: () => ResponseHandler<Uint8Array>;
|
|
870
|
+
declare const createStatusCodeErrorResponseHandler: () => ResponseHandler<APICallError>;
|
|
871
|
+
|
|
872
|
+
declare const getFromApi: <T>({ url, headers, successfulResponseHandler, failedResponseHandler, abortSignal, fetch, }: {
|
|
873
|
+
url: string;
|
|
874
|
+
headers?: Record<string, string | undefined>;
|
|
875
|
+
failedResponseHandler: ResponseHandler<Error>;
|
|
876
|
+
successfulResponseHandler: ResponseHandler<T>;
|
|
877
|
+
abortSignal?: AbortSignal;
|
|
878
|
+
fetch?: FetchFunction;
|
|
879
|
+
}) => Promise<{
|
|
880
|
+
value: T;
|
|
881
|
+
rawValue?: unknown;
|
|
882
|
+
responseHeaders?: Record<string, string>;
|
|
883
|
+
}>;
|
|
884
|
+
|
|
885
|
+
declare function getRuntimeEnvironmentUserAgent(globalThisAny?: any): string;
|
|
886
|
+
|
|
887
|
+
/**
|
|
888
|
+
* Checks if an object has required keys.
|
|
889
|
+
* @param OBJECT - The object to check.
|
|
890
|
+
* @returns True if the object has required keys, false otherwise.
|
|
891
|
+
*/
|
|
892
|
+
type HasRequiredKey<OBJECT> = {} extends OBJECT ? false : true;
|
|
893
|
+
|
|
894
|
+
declare function injectJsonInstructionIntoMessages({ messages, schema, schemaPrefix, schemaSuffix, }: {
|
|
895
|
+
messages: LanguageModelV4Prompt;
|
|
896
|
+
schema?: JSONSchema7;
|
|
897
|
+
schemaPrefix?: string;
|
|
898
|
+
schemaSuffix?: string;
|
|
899
|
+
}): LanguageModelV4Prompt;
|
|
900
|
+
|
|
901
|
+
declare function isAbortError(error: unknown): error is Error;
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* Type-guard for Node.js `Buffer` instances.
|
|
905
|
+
*
|
|
906
|
+
* Uses optional chaining on `globalThis.Buffer` so it returns `false` in
|
|
907
|
+
* runtimes where `Buffer` is not available (e.g. CloudFlare Workers).
|
|
908
|
+
*/
|
|
909
|
+
declare function isBuffer(value: unknown): value is Buffer;
|
|
910
|
+
|
|
911
|
+
/**
|
|
912
|
+
* Type guard that checks whether a value is not `null` or `undefined`.
|
|
913
|
+
*
|
|
914
|
+
* @template T - The type of the value to check.
|
|
915
|
+
* @param value - The value to check.
|
|
916
|
+
* @returns `true` if the value is neither `null` nor `undefined`, otherwise `false`.
|
|
917
|
+
*/
|
|
918
|
+
declare function isNonNullable<T>(value: T | undefined | null): value is NonNullable<T>;
|
|
919
|
+
|
|
920
|
+
/**
|
|
921
|
+
* Checks whether a value is a provider reference (a mapping of provider names
|
|
922
|
+
* to provider-specific identifiers) as opposed to raw bytes, a URL, or a
|
|
923
|
+
* tagged `{ type: ... }` object.
|
|
924
|
+
*/
|
|
925
|
+
declare function isProviderReference(data: unknown): data is SharedV4ProviderReference;
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Checks if the given URL is supported natively by the model.
|
|
929
|
+
*
|
|
930
|
+
* @param mediaType - The media type of the URL. Case-sensitive. May be a full
|
|
931
|
+
* `type/subtype`, a wildcard `type/*`, or just the
|
|
932
|
+
* top-level segment (e.g. `image`).
|
|
933
|
+
* @param url - The URL to check.
|
|
934
|
+
* @param supportedUrls - A record where keys are case-sensitive media types (or '*')
|
|
935
|
+
* and values are arrays of RegExp patterns for URLs.
|
|
936
|
+
*
|
|
937
|
+
* @returns `true` if the URL matches a pattern under the specific media type
|
|
938
|
+
* or the wildcard '*', `false` otherwise.
|
|
939
|
+
*/
|
|
940
|
+
declare function isUrlSupported({ mediaType, url, supportedUrls, }: {
|
|
941
|
+
mediaType: string;
|
|
942
|
+
url: string;
|
|
943
|
+
supportedUrls: Record<string, RegExp[]>;
|
|
944
|
+
}): boolean;
|
|
945
|
+
|
|
946
|
+
declare function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterName, description, }: {
|
|
947
|
+
apiKey: string | undefined;
|
|
948
|
+
environmentVariableName: string;
|
|
949
|
+
apiKeyParameterName?: string;
|
|
950
|
+
description: string;
|
|
951
|
+
}): string;
|
|
952
|
+
|
|
953
|
+
/**
|
|
954
|
+
* Loads an optional `string` setting from the environment or a parameter.
|
|
955
|
+
*
|
|
956
|
+
* @param settingValue - The setting value.
|
|
957
|
+
* @param environmentVariableName - The environment variable name.
|
|
958
|
+
* @returns The setting value.
|
|
959
|
+
*/
|
|
960
|
+
declare function loadOptionalSetting({ settingValue, environmentVariableName, }: {
|
|
961
|
+
settingValue: string | undefined;
|
|
962
|
+
environmentVariableName: string;
|
|
963
|
+
}): string | undefined;
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Loads a `string` setting from the environment or a parameter.
|
|
967
|
+
*
|
|
968
|
+
* @param settingValue - The setting value.
|
|
969
|
+
* @param environmentVariableName - The environment variable name.
|
|
970
|
+
* @param settingName - The setting name.
|
|
971
|
+
* @param description - The description of the setting.
|
|
972
|
+
* @returns The setting value.
|
|
973
|
+
*/
|
|
974
|
+
declare function loadSetting({ settingValue, environmentVariableName, settingName, description, }: {
|
|
975
|
+
settingValue: string | undefined;
|
|
976
|
+
environmentVariableName: string;
|
|
977
|
+
settingName: string;
|
|
978
|
+
description: string;
|
|
979
|
+
}): string;
|
|
980
|
+
|
|
981
|
+
type ReasoningLevel = Exclude<LanguageModelV4CallOptions['reasoning'], 'none' | 'provider-default' | undefined>;
|
|
982
|
+
declare function isCustomReasoning(reasoning: LanguageModelV4CallOptions['reasoning']): reasoning is Exclude<LanguageModelV4CallOptions['reasoning'], 'provider-default' | undefined>;
|
|
983
|
+
/**
|
|
984
|
+
* Maps a top-level reasoning level to a provider-specific effort string using
|
|
985
|
+
* the given effort map. Pushes a compatibility warning if the reasoning level
|
|
986
|
+
* maps to a different string, or an unsupported warning if the level is not
|
|
987
|
+
* present in the map.
|
|
988
|
+
*
|
|
989
|
+
* @returns The mapped effort string, or `undefined` if the level is not
|
|
990
|
+
* supported.
|
|
991
|
+
*/
|
|
992
|
+
declare function mapReasoningToProviderEffort<T extends string>({ reasoning, effortMap, warnings, }: {
|
|
993
|
+
reasoning: ReasoningLevel;
|
|
994
|
+
effortMap: Partial<Record<ReasoningLevel, T>>;
|
|
995
|
+
warnings: SharedV4Warning[];
|
|
996
|
+
}): T | undefined;
|
|
997
|
+
/**
|
|
998
|
+
* Maps a top-level reasoning level to an absolute token budget by multiplying
|
|
999
|
+
* the model's max output tokens by a percentage from the budget percentages
|
|
1000
|
+
* map. The result is clamped between `minReasoningBudget` (default 1024) and
|
|
1001
|
+
* `maxReasoningBudget`. Pushes an unsupported warning if the level is not
|
|
1002
|
+
* present in the budget percentages map.
|
|
1003
|
+
*
|
|
1004
|
+
* @returns The computed token budget, or `undefined` if the level is not
|
|
1005
|
+
* supported.
|
|
1006
|
+
*/
|
|
1007
|
+
declare function mapReasoningToProviderBudget({ reasoning, maxOutputTokens, maxReasoningBudget, minReasoningBudget, budgetPercentages, warnings, }: {
|
|
1008
|
+
reasoning: ReasoningLevel;
|
|
1009
|
+
maxOutputTokens: number;
|
|
1010
|
+
maxReasoningBudget: number;
|
|
1011
|
+
minReasoningBudget?: number;
|
|
1012
|
+
budgetPercentages?: Partial<Record<ReasoningLevel, number>>;
|
|
1013
|
+
warnings: SharedV4Warning[];
|
|
1014
|
+
}): number | undefined;
|
|
1015
|
+
|
|
1016
|
+
/**
|
|
1017
|
+
* A value that can be provided either synchronously or as a promise-like.
|
|
1018
|
+
*/
|
|
1019
|
+
type MaybePromiseLike<T> = T | PromiseLike<T>;
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* Maps a media type to its corresponding file extension.
|
|
1023
|
+
* It was originally introduced to set a filename for audio file uploads
|
|
1024
|
+
* in https://github.com/vercel/ai/pull/8159.
|
|
1025
|
+
*
|
|
1026
|
+
* @param mediaType The media type to map.
|
|
1027
|
+
* @returns The corresponding file extension
|
|
1028
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types
|
|
1029
|
+
*/
|
|
1030
|
+
declare function mediaTypeToExtension(mediaType: string): string;
|
|
1031
|
+
|
|
1032
|
+
/**
|
|
1033
|
+
* Normalizes different header inputs into a plain record with lower-case keys.
|
|
1034
|
+
* Entries with `undefined` or `null` values are removed.
|
|
1035
|
+
*
|
|
1036
|
+
* @param headers - Input headers (`Headers`, tuples array, plain record) to normalize.
|
|
1037
|
+
* @returns A record containing the normalized header entries.
|
|
1038
|
+
*/
|
|
1039
|
+
declare function normalizeHeaders(headers: HeadersInit | Record<string, string | undefined> | Array<[string, string | undefined]> | undefined): Record<string, string>;
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* Parses a JSON event stream into a stream of parsed JSON objects.
|
|
1043
|
+
*/
|
|
1044
|
+
declare function parseJsonEventStream<T>({ stream, schema, }: {
|
|
1045
|
+
stream: ReadableStream<Uint8Array>;
|
|
1046
|
+
schema: FlexibleSchema<T>;
|
|
1047
|
+
}): ReadableStream<ParseResult<T>>;
|
|
1048
|
+
|
|
1049
|
+
declare function parseProviderOptions<OPTIONS>({ provider, providerOptions, schema, }: {
|
|
1050
|
+
provider: string;
|
|
1051
|
+
providerOptions: Record<string, unknown> | undefined;
|
|
1052
|
+
schema: FlexibleSchema<OPTIONS>;
|
|
1053
|
+
}): Promise<OPTIONS | undefined>;
|
|
1054
|
+
|
|
1055
|
+
declare const postJsonToApi: <T>({ url, headers, body, failedResponseHandler, successfulResponseHandler, abortSignal, fetch, }: {
|
|
1056
|
+
url: string;
|
|
1057
|
+
headers?: Record<string, string | undefined>;
|
|
1058
|
+
body: unknown;
|
|
1059
|
+
failedResponseHandler: ResponseHandler<APICallError>;
|
|
1060
|
+
successfulResponseHandler: ResponseHandler<T>;
|
|
1061
|
+
abortSignal?: AbortSignal;
|
|
1062
|
+
fetch?: FetchFunction;
|
|
1063
|
+
}) => Promise<{
|
|
1064
|
+
value: T;
|
|
1065
|
+
rawValue?: unknown;
|
|
1066
|
+
responseHeaders?: Record<string, string>;
|
|
1067
|
+
}>;
|
|
1068
|
+
declare const postFormDataToApi: <T>({ url, headers, formData, failedResponseHandler, successfulResponseHandler, abortSignal, fetch, }: {
|
|
1069
|
+
url: string;
|
|
1070
|
+
headers?: Record<string, string | undefined>;
|
|
1071
|
+
formData: FormData;
|
|
1072
|
+
failedResponseHandler: ResponseHandler<APICallError>;
|
|
1073
|
+
successfulResponseHandler: ResponseHandler<T>;
|
|
1074
|
+
abortSignal?: AbortSignal;
|
|
1075
|
+
fetch?: FetchFunction;
|
|
1076
|
+
}) => Promise<{
|
|
1077
|
+
value: T;
|
|
1078
|
+
rawValue?: unknown;
|
|
1079
|
+
responseHeaders?: Record<string, string>;
|
|
1080
|
+
}>;
|
|
1081
|
+
declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, failedResponseHandler, abortSignal, fetch, }: {
|
|
1082
|
+
url: string;
|
|
1083
|
+
headers?: Record<string, string | undefined>;
|
|
1084
|
+
body: {
|
|
1085
|
+
content: string | FormData | Uint8Array;
|
|
1086
|
+
values: unknown;
|
|
1087
|
+
};
|
|
1088
|
+
failedResponseHandler: ResponseHandler<Error>;
|
|
1089
|
+
successfulResponseHandler: ResponseHandler<T>;
|
|
1090
|
+
abortSignal?: AbortSignal;
|
|
1091
|
+
fetch?: FetchFunction;
|
|
1092
|
+
}) => Promise<{
|
|
1093
|
+
value: T;
|
|
1094
|
+
rawValue?: unknown;
|
|
1095
|
+
responseHeaders?: Record<string, string>;
|
|
1096
|
+
}>;
|
|
1097
|
+
|
|
1098
|
+
/**
|
|
1099
|
+
* A context object that is passed into tool execution.
|
|
1100
|
+
*/
|
|
1101
|
+
type Context = Record<string, unknown>;
|
|
1102
|
+
|
|
1103
|
+
type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
|
|
1104
|
+
|
|
1105
|
+
/**
|
|
1106
|
+
* Top-level context properties that contain sensitive data and should be
|
|
1107
|
+
* excluded from telemetry.
|
|
1108
|
+
*/
|
|
1109
|
+
type SensitiveContext<CONTEXT extends Context | unknown | never> = {
|
|
1110
|
+
[KEY in keyof CONTEXT]?: boolean;
|
|
1111
|
+
} | undefined;
|
|
1112
|
+
|
|
889
1113
|
/**
|
|
890
1114
|
* Tool approval request prompt part.
|
|
891
1115
|
*/
|
|
@@ -899,6 +1123,12 @@ type ToolApprovalRequest = {
|
|
|
899
1123
|
* ID of the tool call that the approval request is for.
|
|
900
1124
|
*/
|
|
901
1125
|
toolCallId: string;
|
|
1126
|
+
/**
|
|
1127
|
+
* Flag indicating whether the tool was automatically approved or denied.
|
|
1128
|
+
*
|
|
1129
|
+
* @default false
|
|
1130
|
+
*/
|
|
1131
|
+
isAutomatic?: boolean;
|
|
902
1132
|
};
|
|
903
1133
|
|
|
904
1134
|
/**
|
|
@@ -918,7 +1148,7 @@ type AssistantModelMessage = {
|
|
|
918
1148
|
* Content of an assistant message.
|
|
919
1149
|
* It can be a string or an array of text, image, reasoning, redacted reasoning, and tool call parts.
|
|
920
1150
|
*/
|
|
921
|
-
type AssistantContent = string | Array<TextPart | FilePart | ReasoningPart | ReasoningFilePart | ToolCallPart | ToolResultPart | ToolApprovalRequest>;
|
|
1151
|
+
type AssistantContent = string | Array<TextPart | CustomPart | FilePart | ReasoningPart | ReasoningFilePart | ToolCallPart | ToolResultPart | ToolApprovalRequest>;
|
|
922
1152
|
|
|
923
1153
|
/**
|
|
924
1154
|
* A system message. It can contain system information.
|
|
@@ -1005,9 +1235,9 @@ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
|
|
|
1005
1235
|
type ModelMessage = SystemModelMessage | UserModelMessage | AssistantModelMessage | ToolModelMessage;
|
|
1006
1236
|
|
|
1007
1237
|
/**
|
|
1008
|
-
* Additional options that are sent into each tool
|
|
1238
|
+
* Additional options that are sent into each tool execution.
|
|
1009
1239
|
*/
|
|
1010
|
-
interface ToolExecutionOptions {
|
|
1240
|
+
interface ToolExecutionOptions<CONTEXT extends Context | unknown | never> {
|
|
1011
1241
|
/**
|
|
1012
1242
|
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
1013
1243
|
*/
|
|
@@ -1022,7 +1252,8 @@ interface ToolExecutionOptions {
|
|
|
1022
1252
|
*/
|
|
1023
1253
|
abortSignal?: AbortSignal;
|
|
1024
1254
|
/**
|
|
1025
|
-
*
|
|
1255
|
+
* Tool context as defined by the tool's context schema.
|
|
1256
|
+
* The tool context is specific to the tool and is passed to the tool execution.
|
|
1026
1257
|
*
|
|
1027
1258
|
* Treat the context object as immutable inside tools.
|
|
1028
1259
|
* Mutating the context object can lead to race conditions and unexpected results
|
|
@@ -1030,15 +1261,20 @@ interface ToolExecutionOptions {
|
|
|
1030
1261
|
*
|
|
1031
1262
|
* If you need to mutate the context, analyze the tool calls and results
|
|
1032
1263
|
* in `prepareStep` and update it there.
|
|
1033
|
-
*
|
|
1034
|
-
* Experimental (can break in patch releases).
|
|
1035
1264
|
*/
|
|
1036
|
-
|
|
1265
|
+
context: CONTEXT;
|
|
1037
1266
|
}
|
|
1267
|
+
/**
|
|
1268
|
+
* Function that executes the tool and returns either a single result or a stream of results.
|
|
1269
|
+
*/
|
|
1270
|
+
type ToolExecuteFunction<INPUT, OUTPUT, CONTEXT extends Context | unknown | never> = (input: INPUT, options: ToolExecutionOptions<CONTEXT>) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
|
|
1271
|
+
|
|
1038
1272
|
/**
|
|
1039
1273
|
* Function that is called to determine if the tool needs approval before it can be executed.
|
|
1274
|
+
*
|
|
1275
|
+
* @deprecated Tool approval is handled on a `generateText` / `streamText` level now.
|
|
1040
1276
|
*/
|
|
1041
|
-
type ToolNeedsApprovalFunction<INPUT> = (input: INPUT, options: {
|
|
1277
|
+
type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context | unknown | never> = (input: INPUT, options: {
|
|
1042
1278
|
/**
|
|
1043
1279
|
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
1044
1280
|
*/
|
|
@@ -1049,15 +1285,29 @@ type ToolNeedsApprovalFunction<INPUT> = (input: INPUT, options: {
|
|
|
1049
1285
|
*/
|
|
1050
1286
|
messages: ModelMessage[];
|
|
1051
1287
|
/**
|
|
1052
|
-
*
|
|
1288
|
+
* Tool context as defined by the tool's context schema.
|
|
1289
|
+
* The tool context is specific to the tool and is passed to the tool execution.
|
|
1290
|
+
*
|
|
1291
|
+
* Treat the context object as immutable inside tools.
|
|
1292
|
+
* Mutating the context object can lead to race conditions and unexpected results
|
|
1293
|
+
* when tools are called in parallel.
|
|
1053
1294
|
*
|
|
1054
|
-
*
|
|
1295
|
+
* If you need to mutate the context, analyze the tool calls and results
|
|
1296
|
+
* in `prepareStep` and update it there.
|
|
1055
1297
|
*/
|
|
1056
|
-
|
|
1298
|
+
context: CONTEXT;
|
|
1057
1299
|
}) => boolean | PromiseLike<boolean>;
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
type
|
|
1300
|
+
|
|
1301
|
+
/**
|
|
1302
|
+
* Helper type to determine the outputSchema and execute function properties of a tool.
|
|
1303
|
+
*/
|
|
1304
|
+
type ToolOutputProperties<INPUT, OUTPUT, CONTEXT extends Context | unknown | never> = NeverOptional<OUTPUT, {
|
|
1305
|
+
/**
|
|
1306
|
+
* The optional schema of the output that the tool produces.
|
|
1307
|
+
*
|
|
1308
|
+
* If not provided, the output shape will be inferred from the execute function.
|
|
1309
|
+
*/
|
|
1310
|
+
outputSchema?: FlexibleSchema<OUTPUT>;
|
|
1061
1311
|
/**
|
|
1062
1312
|
* An async function that is called with the arguments from the tool call and produces a result.
|
|
1063
1313
|
* If not provided, the tool will not be executed automatically.
|
|
@@ -1065,25 +1315,20 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<OUTPUT, {
|
|
|
1065
1315
|
* @args is the input of the tool call.
|
|
1066
1316
|
* @options.abortSignal is a signal that can be used to abort the tool call.
|
|
1067
1317
|
*/
|
|
1068
|
-
execute: ToolExecuteFunction<INPUT, OUTPUT>;
|
|
1069
|
-
outputSchema?: FlexibleSchema<OUTPUT>;
|
|
1318
|
+
execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1070
1319
|
} | {
|
|
1320
|
+
/**
|
|
1321
|
+
* The schema of the output that the tool produces.
|
|
1322
|
+
*
|
|
1323
|
+
* Required when no execute function is provided.
|
|
1324
|
+
*/
|
|
1071
1325
|
outputSchema: FlexibleSchema<OUTPUT>;
|
|
1072
1326
|
execute?: never;
|
|
1073
1327
|
}>;
|
|
1074
1328
|
/**
|
|
1075
|
-
*
|
|
1076
|
-
* This enables the language model to generate the input.
|
|
1077
|
-
*
|
|
1078
|
-
* The tool can also contain an optional execute function for the actual execution function of the tool.
|
|
1329
|
+
* Common properties shared by all tool kinds.
|
|
1079
1330
|
*/
|
|
1080
|
-
type
|
|
1081
|
-
/**
|
|
1082
|
-
* An optional description of what the tool does.
|
|
1083
|
-
* Will be used by the language model to decide whether to use the tool.
|
|
1084
|
-
* Not used for provider-defined tools.
|
|
1085
|
-
*/
|
|
1086
|
-
description?: string;
|
|
1331
|
+
type BaseTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = {
|
|
1087
1332
|
/**
|
|
1088
1333
|
* An optional title of the tool.
|
|
1089
1334
|
*/
|
|
@@ -1094,6 +1339,16 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1094
1339
|
* functionality that can be fully encapsulated in the provider.
|
|
1095
1340
|
*/
|
|
1096
1341
|
providerOptions?: ProviderOptions;
|
|
1342
|
+
/**
|
|
1343
|
+
* Optional metadata about the tool itself (e.g. its source).
|
|
1344
|
+
*
|
|
1345
|
+
* Unlike `providerOptions`, this metadata is not sent to the language
|
|
1346
|
+
* model. Instead, it is propagated onto the resulting tool call's
|
|
1347
|
+
* `providerMetadata` so consumers can read it from tool call / result
|
|
1348
|
+
* parts and UI message parts. This is useful for sources of dynamic
|
|
1349
|
+
* tools (e.g. an MCP server) to identify themselves.
|
|
1350
|
+
*/
|
|
1351
|
+
providerMetadata?: SharedV4ProviderMetadata;
|
|
1097
1352
|
/**
|
|
1098
1353
|
* The schema of the input that the tool expects.
|
|
1099
1354
|
* The language model will use this to generate the input.
|
|
@@ -1103,48 +1358,49 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1103
1358
|
*/
|
|
1104
1359
|
inputSchema: FlexibleSchema<INPUT>;
|
|
1105
1360
|
/**
|
|
1106
|
-
* An optional
|
|
1107
|
-
*
|
|
1361
|
+
* An optional schema describing the context that the tool expects.
|
|
1362
|
+
*
|
|
1363
|
+
* The context is passed to execute function as part of the execution options.
|
|
1108
1364
|
*/
|
|
1109
|
-
|
|
1110
|
-
input: NoInfer<INPUT>;
|
|
1111
|
-
}>;
|
|
1365
|
+
contextSchema?: FlexibleSchema<CONTEXT>;
|
|
1112
1366
|
/**
|
|
1113
|
-
*
|
|
1367
|
+
* Marks top-level context properties that contain sensitive data and should be excluded from telemetry.
|
|
1368
|
+
* Properties marked as `true` are omitted from telemetry integrations.
|
|
1114
1369
|
*/
|
|
1115
|
-
|
|
1370
|
+
sensitiveContext?: SensitiveContext<CONTEXT>;
|
|
1116
1371
|
/**
|
|
1117
|
-
*
|
|
1372
|
+
* Whether the tool needs approval before it can be executed.
|
|
1118
1373
|
*
|
|
1119
|
-
*
|
|
1120
|
-
* how the input should be generated. Strict mode will always produce
|
|
1121
|
-
* valid inputs, but it might limit what input schemas are supported.
|
|
1374
|
+
* @deprecated Tool approval is handled on a `generateText` / `streamText` level now.
|
|
1122
1375
|
*/
|
|
1123
|
-
|
|
1376
|
+
needsApproval?: boolean | ToolNeedsApprovalFunction<[
|
|
1377
|
+
INPUT
|
|
1378
|
+
] extends [never] ? unknown : INPUT, NoInfer<CONTEXT>>;
|
|
1124
1379
|
/**
|
|
1125
1380
|
* Optional function that is called when the argument streaming starts.
|
|
1126
1381
|
* Only called when the tool is used in a streaming context.
|
|
1127
1382
|
*/
|
|
1128
|
-
onInputStart?: (options: ToolExecutionOptions) => void | PromiseLike<void>;
|
|
1383
|
+
onInputStart?: (options: ToolExecutionOptions<NoInfer<CONTEXT>>) => void | PromiseLike<void>;
|
|
1129
1384
|
/**
|
|
1130
1385
|
* Optional function that is called when an argument streaming delta is available.
|
|
1131
1386
|
* Only called when the tool is used in a streaming context.
|
|
1132
1387
|
*/
|
|
1133
1388
|
onInputDelta?: (options: {
|
|
1134
1389
|
inputTextDelta: string;
|
|
1135
|
-
} & ToolExecutionOptions) => void | PromiseLike<void>;
|
|
1390
|
+
} & ToolExecutionOptions<NoInfer<CONTEXT>>) => void | PromiseLike<void>;
|
|
1136
1391
|
/**
|
|
1137
1392
|
* Optional function that is called when a tool call can be started,
|
|
1138
1393
|
* even if the execute function is not provided.
|
|
1139
1394
|
*/
|
|
1140
1395
|
onInputAvailable?: (options: {
|
|
1141
1396
|
input: [INPUT] extends [never] ? unknown : INPUT;
|
|
1142
|
-
} & ToolExecutionOptions) => void | PromiseLike<void>;
|
|
1143
|
-
} & ToolOutputProperties<INPUT, OUTPUT> & {
|
|
1397
|
+
} & ToolExecutionOptions<NoInfer<CONTEXT>>) => void | PromiseLike<void>;
|
|
1144
1398
|
/**
|
|
1145
1399
|
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
1146
1400
|
*
|
|
1147
1401
|
* If not provided, the tool result will be sent as a JSON object.
|
|
1402
|
+
*
|
|
1403
|
+
* 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).
|
|
1148
1404
|
*/
|
|
1149
1405
|
toModelOutput?: (options: {
|
|
1150
1406
|
/**
|
|
@@ -1160,21 +1416,53 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1160
1416
|
*/
|
|
1161
1417
|
output: 0 extends 1 & OUTPUT ? any : [OUTPUT] extends [never] ? any : NoInfer<OUTPUT>;
|
|
1162
1418
|
}) => ToolResultOutput | PromiseLike<ToolResultOutput>;
|
|
1163
|
-
} &
|
|
1419
|
+
} & ToolOutputProperties<INPUT, OUTPUT, NoInfer<CONTEXT>>;
|
|
1420
|
+
/**
|
|
1421
|
+
* Common properties shared by function-style tools.
|
|
1422
|
+
*/
|
|
1423
|
+
type BaseFunctionTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = BaseTool<INPUT, OUTPUT, CONTEXT> & {
|
|
1164
1424
|
/**
|
|
1165
|
-
*
|
|
1425
|
+
* An optional description of what the tool does.
|
|
1426
|
+
* Will be used by the language model to decide whether to use the tool.
|
|
1166
1427
|
*/
|
|
1167
|
-
|
|
1168
|
-
} | {
|
|
1428
|
+
description?: string;
|
|
1169
1429
|
/**
|
|
1170
|
-
*
|
|
1171
|
-
*
|
|
1430
|
+
* Strict mode setting for the tool.
|
|
1431
|
+
*
|
|
1432
|
+
* Providers that support strict mode will use this setting to determine
|
|
1433
|
+
* how the input should be generated. Strict mode will always produce
|
|
1434
|
+
* valid inputs, but it might limit what input schemas are supported.
|
|
1172
1435
|
*/
|
|
1173
|
-
|
|
1174
|
-
} | {
|
|
1436
|
+
strict?: boolean;
|
|
1175
1437
|
/**
|
|
1176
|
-
*
|
|
1438
|
+
* An optional list of input examples that show the language
|
|
1439
|
+
* model what the input should look like.
|
|
1177
1440
|
*/
|
|
1441
|
+
inputExamples?: Array<{
|
|
1442
|
+
input: NoInfer<INPUT>;
|
|
1443
|
+
}>;
|
|
1444
|
+
id?: never;
|
|
1445
|
+
isProviderExecuted?: never;
|
|
1446
|
+
args?: never;
|
|
1447
|
+
supportsDeferredResults?: never;
|
|
1448
|
+
};
|
|
1449
|
+
/**
|
|
1450
|
+
* Tool with user-defined input and output schemas.
|
|
1451
|
+
*/
|
|
1452
|
+
type FunctionTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = BaseFunctionTool<INPUT, OUTPUT, CONTEXT> & {
|
|
1453
|
+
type?: undefined | 'function';
|
|
1454
|
+
};
|
|
1455
|
+
/**
|
|
1456
|
+
* Tool that is defined at runtime (e.g. an MCP tool).
|
|
1457
|
+
* The types of input and output are not known at development time.
|
|
1458
|
+
*/
|
|
1459
|
+
type DynamicTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = BaseFunctionTool<INPUT, OUTPUT, CONTEXT> & {
|
|
1460
|
+
type: 'dynamic';
|
|
1461
|
+
};
|
|
1462
|
+
/**
|
|
1463
|
+
* Common properties shared by provider tools.
|
|
1464
|
+
*/
|
|
1465
|
+
type BaseProviderTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = BaseTool<INPUT, OUTPUT, CONTEXT> & {
|
|
1178
1466
|
type: 'provider';
|
|
1179
1467
|
/**
|
|
1180
1468
|
* The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
|
|
@@ -1184,6 +1472,30 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1184
1472
|
* The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
|
|
1185
1473
|
*/
|
|
1186
1474
|
args: Record<string, unknown>;
|
|
1475
|
+
description?: never;
|
|
1476
|
+
strict?: never;
|
|
1477
|
+
inputExamples?: never;
|
|
1478
|
+
};
|
|
1479
|
+
/**
|
|
1480
|
+
* Tool with provider-defined input and output schemas that is executed by the
|
|
1481
|
+
* user.
|
|
1482
|
+
*/
|
|
1483
|
+
type ProviderDefinedTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
|
|
1484
|
+
/**
|
|
1485
|
+
* Flag that indicates whether the tool is executed by the provider.
|
|
1486
|
+
*/
|
|
1487
|
+
isProviderExecuted: false;
|
|
1488
|
+
supportsDeferredResults?: never;
|
|
1489
|
+
};
|
|
1490
|
+
/**
|
|
1491
|
+
* Tool with provider-defined input and output schemas that is executed by the
|
|
1492
|
+
* provider.
|
|
1493
|
+
*/
|
|
1494
|
+
type ProviderExecutedTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = BaseProviderTool<INPUT, OUTPUT, CONTEXT> & {
|
|
1495
|
+
/**
|
|
1496
|
+
* Flag that indicates whether the tool is executed by the provider.
|
|
1497
|
+
*/
|
|
1498
|
+
isProviderExecuted: true;
|
|
1187
1499
|
/**
|
|
1188
1500
|
* Whether this provider-executed tool supports deferred results.
|
|
1189
1501
|
*
|
|
@@ -1198,79 +1510,68 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1198
1510
|
* @default false
|
|
1199
1511
|
*/
|
|
1200
1512
|
supportsDeferredResults?: boolean;
|
|
1201
|
-
}
|
|
1202
|
-
/**
|
|
1203
|
-
* Infer the input type of a tool.
|
|
1204
|
-
*/
|
|
1205
|
-
type InferToolInput<TOOL extends Tool> = TOOL extends Tool<infer INPUT, any> ? INPUT : never;
|
|
1513
|
+
};
|
|
1206
1514
|
/**
|
|
1207
|
-
*
|
|
1515
|
+
* A tool can either be user-defined or provider-defined.
|
|
1516
|
+
*
|
|
1517
|
+
* It contains the schemas and metadata needed for the language model to call
|
|
1518
|
+
* the tool and can include an execute function for tools that are executed by
|
|
1519
|
+
* the AI SDK.
|
|
1208
1520
|
*/
|
|
1209
|
-
type
|
|
1521
|
+
type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = FunctionTool<INPUT, OUTPUT, CONTEXT> | DynamicTool<INPUT, OUTPUT, CONTEXT> | ProviderDefinedTool<INPUT, OUTPUT, CONTEXT> | ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
|
|
1210
1522
|
/**
|
|
1211
|
-
*
|
|
1523
|
+
* Infer the tool type from a tool object.
|
|
1524
|
+
*
|
|
1525
|
+
* This is useful for type inference when working with tool objects.
|
|
1212
1526
|
*/
|
|
1213
|
-
declare function tool<INPUT, OUTPUT>(tool: Tool<INPUT, OUTPUT>): Tool<INPUT, OUTPUT>;
|
|
1214
|
-
declare function tool<INPUT>(tool: Tool<INPUT, never>): Tool<INPUT, never>;
|
|
1215
|
-
declare function tool<OUTPUT>(tool: Tool<never, OUTPUT>): Tool<never, OUTPUT>;
|
|
1216
|
-
declare function tool(tool: Tool<never, never>): Tool<never, never>;
|
|
1527
|
+
declare function tool<INPUT, OUTPUT, CONTEXT extends Context>(tool: Tool<INPUT, OUTPUT, CONTEXT>): Tool<INPUT, OUTPUT, CONTEXT>;
|
|
1528
|
+
declare function tool<INPUT, CONTEXT extends Context>(tool: Tool<INPUT, never, CONTEXT>): Tool<INPUT, never, CONTEXT>;
|
|
1529
|
+
declare function tool<OUTPUT, CONTEXT extends Context>(tool: Tool<never, OUTPUT, CONTEXT>): Tool<never, OUTPUT, CONTEXT>;
|
|
1530
|
+
declare function tool<CONTEXT extends Context>(tool: Tool<never, never, CONTEXT>): Tool<never, never, CONTEXT>;
|
|
1217
1531
|
/**
|
|
1218
|
-
*
|
|
1532
|
+
* Define a dynamic tool.
|
|
1219
1533
|
*/
|
|
1220
|
-
declare function dynamicTool(tool:
|
|
1221
|
-
description?: string;
|
|
1222
|
-
title?: string;
|
|
1223
|
-
providerOptions?: ProviderOptions;
|
|
1224
|
-
inputSchema: FlexibleSchema<unknown>;
|
|
1225
|
-
execute: ToolExecuteFunction<unknown, unknown>;
|
|
1226
|
-
/**
|
|
1227
|
-
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
1228
|
-
*
|
|
1229
|
-
* If not provided, the tool result will be sent as a JSON object.
|
|
1230
|
-
*/
|
|
1231
|
-
toModelOutput?: (options: {
|
|
1232
|
-
/**
|
|
1233
|
-
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
1234
|
-
*/
|
|
1235
|
-
toolCallId: string;
|
|
1236
|
-
/**
|
|
1237
|
-
* The input of the tool call.
|
|
1238
|
-
*/
|
|
1239
|
-
input: unknown;
|
|
1240
|
-
/**
|
|
1241
|
-
* The output of the tool call.
|
|
1242
|
-
*/
|
|
1243
|
-
output: unknown;
|
|
1244
|
-
}) => ToolResultOutput | PromiseLike<ToolResultOutput>;
|
|
1245
|
-
/**
|
|
1246
|
-
* Whether the tool needs approval before it can be executed.
|
|
1247
|
-
*/
|
|
1248
|
-
needsApproval?: boolean | ToolNeedsApprovalFunction<unknown>;
|
|
1249
|
-
}): Tool<unknown, unknown> & {
|
|
1250
|
-
type: 'dynamic';
|
|
1251
|
-
};
|
|
1534
|
+
declare function dynamicTool(tool: Omit<DynamicTool<unknown, unknown, Context>, 'type'>): DynamicTool<unknown, unknown, Context>;
|
|
1252
1535
|
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1536
|
+
/**
|
|
1537
|
+
* A provider-defined tool is a tool for which the provider defines the input
|
|
1538
|
+
* and output schemas, but does not execute the tool.
|
|
1539
|
+
*/
|
|
1540
|
+
type ProviderDefinedToolFactory<INPUT, ARGS extends object, CONTEXT extends Context = {}> = <OUTPUT>(options: ARGS & {
|
|
1541
|
+
execute?: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1542
|
+
needsApproval?: Tool<INPUT, OUTPUT, CONTEXT>['needsApproval'];
|
|
1543
|
+
toModelOutput?: Tool<INPUT, OUTPUT, CONTEXT>['toModelOutput'];
|
|
1544
|
+
onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
|
|
1545
|
+
onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
|
|
1546
|
+
onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
|
|
1547
|
+
}) => ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>;
|
|
1548
|
+
declare function createProviderDefinedToolFactory<INPUT, ARGS extends object, CONTEXT extends Context = {}>({ id, inputSchema, }: {
|
|
1262
1549
|
id: `${string}.${string}`;
|
|
1263
1550
|
inputSchema: FlexibleSchema<INPUT>;
|
|
1264
|
-
}):
|
|
1265
|
-
type
|
|
1266
|
-
execute?: ToolExecuteFunction<INPUT, OUTPUT>;
|
|
1267
|
-
needsApproval?: Tool<INPUT, OUTPUT>['needsApproval'];
|
|
1268
|
-
toModelOutput?: Tool<INPUT, OUTPUT>['toModelOutput'];
|
|
1269
|
-
onInputStart?: Tool<INPUT, OUTPUT>['onInputStart'];
|
|
1270
|
-
onInputDelta?: Tool<INPUT, OUTPUT>['onInputDelta'];
|
|
1271
|
-
onInputAvailable?: Tool<INPUT, OUTPUT>['onInputAvailable'];
|
|
1272
|
-
}) =>
|
|
1273
|
-
declare function
|
|
1551
|
+
}): ProviderDefinedToolFactory<INPUT, ARGS, CONTEXT>;
|
|
1552
|
+
type ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object, CONTEXT extends Context = {}> = (options: ARGS & {
|
|
1553
|
+
execute?: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1554
|
+
needsApproval?: Tool<INPUT, OUTPUT, CONTEXT>['needsApproval'];
|
|
1555
|
+
toModelOutput?: Tool<INPUT, OUTPUT, CONTEXT>['toModelOutput'];
|
|
1556
|
+
onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
|
|
1557
|
+
onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
|
|
1558
|
+
onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
|
|
1559
|
+
}) => ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>;
|
|
1560
|
+
declare function createProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object, CONTEXT extends Context = {}>({ id, inputSchema, outputSchema, }: {
|
|
1561
|
+
id: `${string}.${string}`;
|
|
1562
|
+
inputSchema: FlexibleSchema<INPUT>;
|
|
1563
|
+
outputSchema: FlexibleSchema<OUTPUT>;
|
|
1564
|
+
}): ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS, CONTEXT>;
|
|
1565
|
+
|
|
1566
|
+
/**
|
|
1567
|
+
* A provider-executed tool is a tool for which the provider executes the tool.
|
|
1568
|
+
*/
|
|
1569
|
+
type ProviderExecutedToolFactory<INPUT, OUTPUT, ARGS extends object, CONTEXT extends Context = {}> = (options: ARGS & {
|
|
1570
|
+
onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
|
|
1571
|
+
onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
|
|
1572
|
+
onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
|
|
1573
|
+
}) => ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
|
|
1574
|
+
declare function createProviderExecutedToolFactory<INPUT, OUTPUT, ARGS extends object, CONTEXT extends Context = {}>({ id, inputSchema, outputSchema, supportsDeferredResults, }: {
|
|
1274
1575
|
id: `${string}.${string}`;
|
|
1275
1576
|
inputSchema: FlexibleSchema<INPUT>;
|
|
1276
1577
|
outputSchema: FlexibleSchema<OUTPUT>;
|
|
@@ -1285,7 +1586,38 @@ declare function createProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS e
|
|
|
1285
1586
|
* @default false
|
|
1286
1587
|
*/
|
|
1287
1588
|
supportsDeferredResults?: boolean;
|
|
1288
|
-
}):
|
|
1589
|
+
}): ProviderExecutedToolFactory<INPUT, OUTPUT, ARGS, CONTEXT>;
|
|
1590
|
+
|
|
1591
|
+
/**
|
|
1592
|
+
* Default maximum download size: 2 GiB.
|
|
1593
|
+
*
|
|
1594
|
+
* `fetch().arrayBuffer()` has ~2x peak memory overhead (undici buffers the
|
|
1595
|
+
* body internally, then creates the JS ArrayBuffer), so very large downloads
|
|
1596
|
+
* risk exceeding the default V8 heap limit on 64-bit systems and terminating
|
|
1597
|
+
* the process with an out-of-memory error.
|
|
1598
|
+
*
|
|
1599
|
+
* Setting this limit converts an unrecoverable OOM crash into a catchable
|
|
1600
|
+
* `DownloadError`.
|
|
1601
|
+
*/
|
|
1602
|
+
declare const DEFAULT_MAX_DOWNLOAD_SIZE: number;
|
|
1603
|
+
/**
|
|
1604
|
+
* Reads a fetch Response body with a size limit to prevent memory exhaustion.
|
|
1605
|
+
*
|
|
1606
|
+
* Checks the Content-Length header for early rejection, then reads the body
|
|
1607
|
+
* incrementally via ReadableStream and aborts with a DownloadError when the
|
|
1608
|
+
* limit is exceeded.
|
|
1609
|
+
*
|
|
1610
|
+
* @param response - The fetch Response to read.
|
|
1611
|
+
* @param url - The URL being downloaded (used in error messages).
|
|
1612
|
+
* @param maxBytes - Maximum allowed bytes. Defaults to DEFAULT_MAX_DOWNLOAD_SIZE.
|
|
1613
|
+
* @returns A Uint8Array containing the response body.
|
|
1614
|
+
* @throws DownloadError if the response exceeds maxBytes.
|
|
1615
|
+
*/
|
|
1616
|
+
declare function readResponseWithSizeLimit({ response, url, maxBytes, }: {
|
|
1617
|
+
response: Response;
|
|
1618
|
+
url: string;
|
|
1619
|
+
maxBytes?: number;
|
|
1620
|
+
}): Promise<Uint8Array>;
|
|
1289
1621
|
|
|
1290
1622
|
/**
|
|
1291
1623
|
* Removes entries from a record where the value is null or undefined.
|
|
@@ -1294,6 +1626,21 @@ declare function createProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS e
|
|
|
1294
1626
|
*/
|
|
1295
1627
|
declare function removeUndefinedEntries<T>(record: Record<string, T | undefined>): Record<string, T>;
|
|
1296
1628
|
|
|
1629
|
+
/**
|
|
1630
|
+
* A value or a lazy provider of a value, each of which may be synchronous or asynchronous.
|
|
1631
|
+
*
|
|
1632
|
+
* @template T The resolved type after {@link resolve} runs.
|
|
1633
|
+
*
|
|
1634
|
+
* One of:
|
|
1635
|
+
* - A plain value of type {@link T}
|
|
1636
|
+
* - A {@link PromiseLike} of {@link T} (e.g. a `Promise<T>`)
|
|
1637
|
+
* - A zero-argument function that returns a plain {@link T}
|
|
1638
|
+
* - A zero-argument function that returns a {@link PromiseLike} of {@link T}
|
|
1639
|
+
*
|
|
1640
|
+
* The function form is only invoked when passed to {@link resolve}; it is not distinguished from
|
|
1641
|
+
* a {@link T} that happens to be a function—callers should wrap function values if disambiguation
|
|
1642
|
+
* is required.
|
|
1643
|
+
*/
|
|
1297
1644
|
type Resolvable<T> = MaybePromiseLike<T> | (() => MaybePromiseLike<T>);
|
|
1298
1645
|
/**
|
|
1299
1646
|
* Resolves a value that could be a raw value, a Promise, a function returning a value,
|
|
@@ -1301,6 +1648,134 @@ type Resolvable<T> = MaybePromiseLike<T> | (() => MaybePromiseLike<T>);
|
|
|
1301
1648
|
*/
|
|
1302
1649
|
declare function resolve<T>(value: Resolvable<T>): Promise<T>;
|
|
1303
1650
|
|
|
1651
|
+
/**
|
|
1652
|
+
* Resolves a file part's media type to a full `type/subtype` form required by
|
|
1653
|
+
* providers whose API demands the full IANA media type.
|
|
1654
|
+
*
|
|
1655
|
+
* - If `part.mediaType` is already a full media type (e.g. `image/png`), it is
|
|
1656
|
+
* returned as-is.
|
|
1657
|
+
* - Otherwise, when inline bytes are available (`part.data.type === 'data'`),
|
|
1658
|
+
* the subtype is sniffed from the bytes using the signature table that
|
|
1659
|
+
* corresponds to the top-level segment.
|
|
1660
|
+
* - When neither applies (e.g. top-level-only with a URL source, or bytes that
|
|
1661
|
+
* cannot be detected), an `UnsupportedFunctionalityError` is thrown.
|
|
1662
|
+
*/
|
|
1663
|
+
declare function resolveFullMediaType({ part, }: {
|
|
1664
|
+
part: LanguageModelV4FilePart;
|
|
1665
|
+
}): string;
|
|
1666
|
+
|
|
1667
|
+
/**
|
|
1668
|
+
* Resolves a provider reference to the provider-specific identifier for the
|
|
1669
|
+
* given provider. Throws `NoSuchProviderReferenceError` if the provider is not
|
|
1670
|
+
* found in the reference mapping.
|
|
1671
|
+
*/
|
|
1672
|
+
declare function resolveProviderReference({ reference, provider, }: {
|
|
1673
|
+
reference: SharedV4ProviderReference;
|
|
1674
|
+
provider: string;
|
|
1675
|
+
}): string;
|
|
1676
|
+
|
|
1677
|
+
/**
|
|
1678
|
+
* Serializes a model instance for workflow step boundaries.
|
|
1679
|
+
* Returns the `modelId` plus the JSON-serializable config properties.
|
|
1680
|
+
*
|
|
1681
|
+
* Non-serializable values are omitted. As a special case, a
|
|
1682
|
+
* function-valued `headers` property is resolved during serialization
|
|
1683
|
+
* and included if the returned value is JSON-serializable.
|
|
1684
|
+
*
|
|
1685
|
+
* Used as the body of `static [WORKFLOW_SERIALIZE]` in provider models.
|
|
1686
|
+
*
|
|
1687
|
+
* @example
|
|
1688
|
+
* ```ts
|
|
1689
|
+
* static [WORKFLOW_SERIALIZE](model: MyLanguageModel) {
|
|
1690
|
+
* return serializeModelOptions({
|
|
1691
|
+
* modelId: model.modelId,
|
|
1692
|
+
* config: model.config,
|
|
1693
|
+
* });
|
|
1694
|
+
* }
|
|
1695
|
+
* ```
|
|
1696
|
+
*/
|
|
1697
|
+
declare function serializeModelOptions<CONFIG extends {
|
|
1698
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
|
1699
|
+
}>(options: {
|
|
1700
|
+
modelId: string;
|
|
1701
|
+
config: CONFIG;
|
|
1702
|
+
}): {
|
|
1703
|
+
modelId: string;
|
|
1704
|
+
config: JSONObject;
|
|
1705
|
+
};
|
|
1706
|
+
|
|
1707
|
+
/**
|
|
1708
|
+
* Minimal interface for a streaming tool call delta from an OpenAI-compatible API.
|
|
1709
|
+
*/
|
|
1710
|
+
interface StreamingToolCallDelta {
|
|
1711
|
+
index?: number | null;
|
|
1712
|
+
id?: string | null;
|
|
1713
|
+
type?: string | null;
|
|
1714
|
+
function?: {
|
|
1715
|
+
name?: string | null;
|
|
1716
|
+
arguments?: string | null;
|
|
1717
|
+
} | null;
|
|
1718
|
+
}
|
|
1719
|
+
interface StreamingToolCallTrackerOptions<DELTA extends StreamingToolCallDelta = StreamingToolCallDelta> {
|
|
1720
|
+
/**
|
|
1721
|
+
* ID generator function for tool call IDs.
|
|
1722
|
+
* Defaults to the standard generateId.
|
|
1723
|
+
*/
|
|
1724
|
+
generateId?: () => string;
|
|
1725
|
+
/**
|
|
1726
|
+
* How to validate the `type` field on new tool call deltas.
|
|
1727
|
+
* - `'none'`: no validation (default)
|
|
1728
|
+
* - `'if-present'`: throw if type is present and not `'function'`
|
|
1729
|
+
* - `'required'`: throw if type is not exactly `'function'`
|
|
1730
|
+
*/
|
|
1731
|
+
typeValidation?: 'none' | 'if-present' | 'required';
|
|
1732
|
+
/**
|
|
1733
|
+
* Extract provider-specific metadata from a tool call delta.
|
|
1734
|
+
* Called once when a new tool call is detected.
|
|
1735
|
+
* The returned metadata is stored on the tool call and passed to
|
|
1736
|
+
* `buildToolCallProviderMetadata` when the tool call is finalized.
|
|
1737
|
+
*/
|
|
1738
|
+
extractMetadata?: (delta: DELTA) => SharedV4ProviderMetadata | undefined;
|
|
1739
|
+
/**
|
|
1740
|
+
* Build the `providerMetadata` object for a `tool-call` event.
|
|
1741
|
+
* Receives the metadata previously extracted via `extractMetadata`.
|
|
1742
|
+
* If `undefined` is returned, no `providerMetadata` is included in the event.
|
|
1743
|
+
*/
|
|
1744
|
+
buildToolCallProviderMetadata?: (metadata: SharedV4ProviderMetadata | undefined) => SharedV4ProviderMetadata | undefined;
|
|
1745
|
+
}
|
|
1746
|
+
type StreamingToolCallTrackerController = Pick<TransformStreamDefaultController<LanguageModelV4StreamPart>, 'enqueue'>;
|
|
1747
|
+
/**
|
|
1748
|
+
* Tracks streaming tool call state across multiple deltas from an
|
|
1749
|
+
* OpenAI-compatible chat completion stream. Handles argument accumulation,
|
|
1750
|
+
* emits tool-input-start/delta/end and tool-call events, and finalizes
|
|
1751
|
+
* unfinished tool calls on flush.
|
|
1752
|
+
*
|
|
1753
|
+
* Used by openai, openai-compatible, groq, deepseek, and alibaba providers.
|
|
1754
|
+
*/
|
|
1755
|
+
declare class StreamingToolCallTracker<DELTA extends StreamingToolCallDelta = StreamingToolCallDelta> {
|
|
1756
|
+
private toolCalls;
|
|
1757
|
+
private readonly controller;
|
|
1758
|
+
private readonly _generateId;
|
|
1759
|
+
private readonly typeValidation;
|
|
1760
|
+
private readonly extractMetadata?;
|
|
1761
|
+
private readonly buildToolCallProviderMetadata?;
|
|
1762
|
+
constructor(controller: StreamingToolCallTrackerController, options?: StreamingToolCallTrackerOptions<DELTA>);
|
|
1763
|
+
/**
|
|
1764
|
+
* Process a tool call delta from a streaming response chunk.
|
|
1765
|
+
* Emits tool-input-start, tool-input-delta, tool-input-end, and tool-call
|
|
1766
|
+
* events as appropriate.
|
|
1767
|
+
*/
|
|
1768
|
+
processDelta(toolCallDelta: DELTA): void;
|
|
1769
|
+
/**
|
|
1770
|
+
* Finalize any unfinished tool calls. Should be called during the stream's
|
|
1771
|
+
* flush handler to ensure all tool calls are properly completed.
|
|
1772
|
+
*/
|
|
1773
|
+
flush(): void;
|
|
1774
|
+
private processNewToolCall;
|
|
1775
|
+
private processExistingToolCall;
|
|
1776
|
+
private finishToolCall;
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1304
1779
|
/**
|
|
1305
1780
|
* Strips file extension segments from a filename.
|
|
1306
1781
|
*
|
|
@@ -1379,18 +1854,74 @@ declare function withUserAgentSuffix(headers: HeadersInit | Record<string, strin
|
|
|
1379
1854
|
|
|
1380
1855
|
declare function withoutTrailingSlash(url: string | undefined): string | undefined;
|
|
1381
1856
|
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1857
|
+
/**
|
|
1858
|
+
* A tool that is guaranteed to expose an execute function.
|
|
1859
|
+
*/
|
|
1860
|
+
type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
|
|
1861
|
+
execute: NonNullable<TOOL['execute']>;
|
|
1862
|
+
};
|
|
1863
|
+
/**
|
|
1864
|
+
* Checks whether a tool exposes an execute function.
|
|
1865
|
+
*/
|
|
1866
|
+
declare function isExecutableTool<TOOL extends Tool>(tool: TOOL | undefined): tool is ExecutableTool<TOOL>;
|
|
1867
|
+
|
|
1868
|
+
/**
|
|
1869
|
+
* Infer the context type of a tool.
|
|
1870
|
+
*/
|
|
1871
|
+
type InferToolContext<TOOL extends Tool> = TOOL extends Tool<any, any, infer CONTEXT> ? HasRequiredKey<CONTEXT> extends true ? CONTEXT : never : never;
|
|
1872
|
+
|
|
1873
|
+
/**
|
|
1874
|
+
* Infer the input type of a tool.
|
|
1875
|
+
*/
|
|
1876
|
+
type InferToolInput<TOOL extends Tool<any, any, any>> = TOOL extends Tool<infer INPUT, any, any> ? INPUT : never;
|
|
1877
|
+
|
|
1878
|
+
/**
|
|
1879
|
+
* Infer the output type of a tool.
|
|
1880
|
+
*/
|
|
1881
|
+
type InferToolOutput<TOOL extends Tool<any, any, any>> = TOOL extends Tool<any, infer OUTPUT, any> ? OUTPUT : never;
|
|
1882
|
+
|
|
1883
|
+
/**
|
|
1884
|
+
* Executes a tool function and normalizes its results into a stream of outputs.
|
|
1885
|
+
*
|
|
1886
|
+
* - If the tool's `execute` function returns an `AsyncIterable`, each yielded value is emitted as
|
|
1887
|
+
* `{ type: "preliminary", output }`. After iteration completes, the last yielded value is emitted
|
|
1888
|
+
* again as `{ type: "final", output }`.
|
|
1889
|
+
* - If the tool returns a direct value or Promise, a single `{ type: "final", output }` is yielded.
|
|
1890
|
+
*
|
|
1891
|
+
* @param params.tool The tool whose `execute` function should be invoked.
|
|
1892
|
+
* @param params.input The input value to pass to the tool.
|
|
1893
|
+
* @param params.options Additional options for tool execution.
|
|
1894
|
+
* @yields A preliminary output for each streamed value, followed by a final output, or a single final
|
|
1895
|
+
* output for non-streaming tools.
|
|
1896
|
+
*/
|
|
1897
|
+
declare function executeTool<TOOL extends Tool>({ tool, input, options, }: {
|
|
1898
|
+
tool: ExecutableTool<TOOL>;
|
|
1899
|
+
input: InferToolInput<TOOL>;
|
|
1900
|
+
options: ToolExecutionOptions<InferToolContext<TOOL>>;
|
|
1386
1901
|
}): AsyncGenerator<{
|
|
1387
1902
|
type: 'preliminary';
|
|
1388
|
-
output:
|
|
1903
|
+
output: InferToolOutput<TOOL>;
|
|
1389
1904
|
} | {
|
|
1390
1905
|
type: 'final';
|
|
1391
|
-
output:
|
|
1906
|
+
output: InferToolOutput<TOOL>;
|
|
1392
1907
|
}>;
|
|
1393
1908
|
|
|
1909
|
+
/**
|
|
1910
|
+
* A mapping of tool names to tool definitions.
|
|
1911
|
+
*/
|
|
1912
|
+
type ToolSet = Record<string, (Tool<never, never, any> | Tool<any, any, any> | Tool<any, never, any> | Tool<never, any, any>) & Pick<Tool<any, any, any>, 'execute' | 'onInputAvailable' | 'onInputStart' | 'onInputDelta' | 'needsApproval'>>;
|
|
1913
|
+
|
|
1914
|
+
/**
|
|
1915
|
+
* Infer the context type for a tool set.
|
|
1916
|
+
*
|
|
1917
|
+
* The inferred type maps each tool name to its required context type.
|
|
1918
|
+
*
|
|
1919
|
+
* Tools without required context properties are omitted from the result.
|
|
1920
|
+
*/
|
|
1921
|
+
type InferToolSetContext<TOOLS extends ToolSet> = {
|
|
1922
|
+
[K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never ? never : K]: InferToolContext<NoInfer<TOOLS[K]>>;
|
|
1923
|
+
};
|
|
1924
|
+
|
|
1394
1925
|
/**
|
|
1395
1926
|
* Typed tool call that is returned by generateText and streamText.
|
|
1396
1927
|
* It contains the tool call ID, the tool name, and the tool arguments.
|
|
@@ -1450,9 +1981,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
|
|
|
1450
1981
|
dynamic?: boolean;
|
|
1451
1982
|
}
|
|
1452
1983
|
|
|
1453
|
-
|
|
1454
|
-
* @deprecated Use ToolExecutionOptions instead.
|
|
1455
|
-
*/
|
|
1456
|
-
type ToolCallOptions = ToolExecutionOptions;
|
|
1457
|
-
|
|
1458
|
-
export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isNonNullable, isParsableJson, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|
|
1984
|
+
export { type Arrayable, type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type DynamicTool, type ExecutableTool, type FetchFunction, type FileData, type FileDataData, type FileDataReference, type FileDataText, type FileDataUrl, type FilePart, type FlexibleSchema, type FunctionTool, type HasRequiredKey, type IdGenerator, type ImagePart, type InferSchema, type InferToolContext, type InferToolInput, type InferToolOutput, type InferToolSetContext, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderDefinedTool, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderExecutedTool, type ProviderExecutedToolFactory, type ProviderOptions, type ProviderReference, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SensitiveContext, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type ToolSet, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asArray, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertInlineFileDataToUint8Array, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createProviderExecutedToolFactory, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, detectMediaType, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, filterNullable, generateId, getFromApi, getRuntimeEnvironmentUserAgent, getTopLevelMediaType, injectJsonInstructionIntoMessages, isAbortError, isBuffer, isCustomReasoning, isExecutableTool, isFullMediaType, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveFullMediaType, resolveProviderReference, safeParseJSON, safeValidateTypes, serializeModelOptions, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|