@ai-sdk/provider-utils 5.0.0-beta.2 → 5.0.0-beta.21
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 +226 -0
- package/dist/index.d.ts +433 -174
- package/dist/index.js +322 -284
- package/dist/index.js.map +1 -1
- package/dist/test/index.js +2 -35
- package/dist/test/index.js.map +1 -1
- package/package.json +8 -10
- package/src/convert-image-model-file-to-data-uri.ts +3 -3
- package/src/create-tool-name-mapping.ts +5 -21
- package/src/get-error-message.ts +1 -15
- package/src/has-required-key.ts +6 -0
- package/src/index.ts +17 -7
- package/src/inject-json-instruction.ts +5 -5
- package/src/is-json-serializable.ts +29 -0
- package/src/is-provider-reference.ts +18 -0
- package/src/load-api-key.ts +1 -1
- package/src/load-setting.ts +1 -1
- package/src/map-reasoning-to-provider.ts +105 -0
- package/src/provider-tool-factory.ts +43 -32
- package/src/resolve-provider-reference.ts +27 -0
- package/src/resolve.ts +15 -0
- package/src/response-handler.ts +1 -1
- package/src/secure-json-parse.ts +1 -1
- package/src/serialize-model-options.ts +63 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +2 -2
- package/src/types/assistant-model-message.ts +4 -0
- package/src/types/content-part.ts +98 -14
- package/src/types/context.ts +4 -0
- package/src/types/executable-tool.ts +17 -0
- package/src/types/execute-tool.ts +28 -9
- package/src/types/index.ts +11 -9
- 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 +17 -0
- package/src/types/provider-options.ts +2 -2
- package/src/types/provider-reference.ts +10 -0
- package/src/types/tool-set.ts +22 -0
- package/src/types/tool.ts +74 -40
- package/src/types/union-to-intersection.ts +17 -0
- package/src/validate-download-url.ts +7 -2
- package/dist/index.d.mts +0 -1433
- 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/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ImageModelV4File, LanguageModelV4FunctionTool, LanguageModelV4ProviderTool, AISDKError, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV4Prompt, LanguageModelV4FilePart, SharedV4ProviderReference, LanguageModelV4CallOptions, SharedV4Warning, SharedV4ProviderOptions, JSONObject, 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
|
|
|
8
10
|
declare function combineHeaders(...headers: Array<Record<string, string | undefined> | undefined>): Record<string, string | undefined>;
|
|
@@ -16,6 +18,50 @@ declare function combineHeaders(...headers: Array<Record<string, string | undefi
|
|
|
16
18
|
*/
|
|
17
19
|
declare function convertAsyncIteratorToReadableStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
|
|
18
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Convert an ImageModelV4File to a URL or data URI string.
|
|
23
|
+
*
|
|
24
|
+
* If the file is a URL, it returns the URL as-is.
|
|
25
|
+
* If the file is base64 data, it returns a data URI with the base64 data.
|
|
26
|
+
* If the file is a Uint8Array, it converts it to base64 and returns a data URI.
|
|
27
|
+
*/
|
|
28
|
+
declare function convertImageModelFileToDataUri(file: ImageModelV4File): string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Converts an input object to FormData for multipart/form-data requests.
|
|
32
|
+
*
|
|
33
|
+
* Handles the following cases:
|
|
34
|
+
* - `null` or `undefined` values are skipped
|
|
35
|
+
* - Arrays with a single element are appended as a single value
|
|
36
|
+
* - Arrays with multiple elements are appended with `[]` suffix (e.g., `image[]`)
|
|
37
|
+
* unless `useArrayBrackets` is set to `false`
|
|
38
|
+
* - All other values are appended directly
|
|
39
|
+
*
|
|
40
|
+
* @param input - The input object to convert. Use a generic type for type validation.
|
|
41
|
+
* @param options - Optional configuration object.
|
|
42
|
+
* @param options.useArrayBrackets - Whether to add `[]` suffix for multi-element arrays.
|
|
43
|
+
* Defaults to `true`. Set to `false` for APIs that expect repeated keys without brackets.
|
|
44
|
+
* @returns A FormData object containing the input values.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* type MyInput = {
|
|
49
|
+
* model: string;
|
|
50
|
+
* prompt: string;
|
|
51
|
+
* images: Blob[];
|
|
52
|
+
* };
|
|
53
|
+
*
|
|
54
|
+
* const formData = convertToFormData<MyInput>({
|
|
55
|
+
* model: 'gpt-image-1',
|
|
56
|
+
* prompt: 'A cat',
|
|
57
|
+
* images: [blob1, blob2],
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function convertToFormData<T extends Record<string, unknown>>(input: T, options?: {
|
|
62
|
+
useArrayBrackets?: boolean;
|
|
63
|
+
}): FormData;
|
|
64
|
+
|
|
19
65
|
/**
|
|
20
66
|
* Interface for mapping between custom tool names and provider tool names.
|
|
21
67
|
*/
|
|
@@ -41,20 +87,15 @@ interface ToolNameMapping {
|
|
|
41
87
|
* @param tools - Tools that were passed to the language model.
|
|
42
88
|
* @param providerToolNames - Maps the provider tool ids to the provider tool names.
|
|
43
89
|
*/
|
|
44
|
-
declare function createToolNameMapping({ tools, providerToolNames,
|
|
90
|
+
declare function createToolNameMapping({ tools, providerToolNames, }: {
|
|
45
91
|
/**
|
|
46
92
|
* Tools that were passed to the language model.
|
|
47
93
|
*/
|
|
48
|
-
tools: Array<
|
|
94
|
+
tools: Array<LanguageModelV4FunctionTool | LanguageModelV4ProviderTool> | undefined;
|
|
49
95
|
/**
|
|
50
96
|
* Maps the provider tool ids to the provider tool names.
|
|
51
97
|
*/
|
|
52
98
|
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
99
|
}): ToolNameMapping;
|
|
59
100
|
|
|
60
101
|
/**
|
|
@@ -86,60 +127,6 @@ declare class DelayedPromise<T> {
|
|
|
86
127
|
isPending(): boolean;
|
|
87
128
|
}
|
|
88
129
|
|
|
89
|
-
/**
|
|
90
|
-
* Extracts the headers from a response object and returns them as a key-value object.
|
|
91
|
-
*
|
|
92
|
-
* @param response - The response object to extract headers from.
|
|
93
|
-
* @returns The headers as a key-value object.
|
|
94
|
-
*/
|
|
95
|
-
declare function extractResponseHeaders(response: Response): {
|
|
96
|
-
[k: string]: string;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Convert an ImageModelV3File to a URL or data URI string.
|
|
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.
|
|
105
|
-
*/
|
|
106
|
-
declare function convertImageModelFileToDataUri(file: ImageModelV3File): string;
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Converts an input object to FormData for multipart/form-data requests.
|
|
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
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
declare function convertToFormData<T extends Record<string, unknown>>(input: T, options?: {
|
|
140
|
-
useArrayBrackets?: boolean;
|
|
141
|
-
}): FormData;
|
|
142
|
-
|
|
143
130
|
/**
|
|
144
131
|
* Download a file from a URL and return it as a Blob.
|
|
145
132
|
*
|
|
@@ -173,35 +160,14 @@ declare class DownloadError extends AISDKError {
|
|
|
173
160
|
}
|
|
174
161
|
|
|
175
162
|
/**
|
|
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.
|
|
182
|
-
*
|
|
183
|
-
* Setting this limit converts an unrecoverable OOM crash into a catchable
|
|
184
|
-
* `DownloadError`.
|
|
185
|
-
*/
|
|
186
|
-
declare const DEFAULT_MAX_DOWNLOAD_SIZE: number;
|
|
187
|
-
/**
|
|
188
|
-
* Reads a fetch Response body with a size limit to prevent memory exhaustion.
|
|
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.
|
|
163
|
+
* Extracts the headers from a response object and returns them as a key-value object.
|
|
193
164
|
*
|
|
194
|
-
* @param response - The
|
|
195
|
-
* @
|
|
196
|
-
* @param maxBytes - Maximum allowed bytes. Defaults to DEFAULT_MAX_DOWNLOAD_SIZE.
|
|
197
|
-
* @returns A Uint8Array containing the response body.
|
|
198
|
-
* @throws DownloadError if the response exceeds maxBytes.
|
|
165
|
+
* @param response - The response object to extract headers from.
|
|
166
|
+
* @returns The headers as a key-value object.
|
|
199
167
|
*/
|
|
200
|
-
declare function
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
maxBytes?: number;
|
|
204
|
-
}): Promise<Uint8Array>;
|
|
168
|
+
declare function extractResponseHeaders(response: Response): {
|
|
169
|
+
[k: string]: string;
|
|
170
|
+
};
|
|
205
171
|
|
|
206
172
|
/**
|
|
207
173
|
* Fetch function type (standardizes the version of fetch used).
|
|
@@ -234,8 +200,6 @@ type IdGenerator = () => string;
|
|
|
234
200
|
*/
|
|
235
201
|
declare const generateId: IdGenerator;
|
|
236
202
|
|
|
237
|
-
declare function getErrorMessage(error: unknown | undefined): string;
|
|
238
|
-
|
|
239
203
|
/**
|
|
240
204
|
* Used to mark schemas so we can support both Zod and custom schemas.
|
|
241
205
|
*/
|
|
@@ -389,12 +353,19 @@ declare const getFromApi: <T>({ url, headers, successfulResponseHandler, failedR
|
|
|
389
353
|
|
|
390
354
|
declare function getRuntimeEnvironmentUserAgent(globalThisAny?: any): string;
|
|
391
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Checks if an object has required keys.
|
|
358
|
+
* @param OBJECT - The object to check.
|
|
359
|
+
* @returns True if the object has required keys, false otherwise.
|
|
360
|
+
*/
|
|
361
|
+
type HasRequiredKey<OBJECT> = {} extends OBJECT ? false : true;
|
|
362
|
+
|
|
392
363
|
declare function injectJsonInstructionIntoMessages({ messages, schema, schemaPrefix, schemaSuffix, }: {
|
|
393
|
-
messages:
|
|
364
|
+
messages: LanguageModelV4Prompt;
|
|
394
365
|
schema?: JSONSchema7;
|
|
395
366
|
schemaPrefix?: string;
|
|
396
367
|
schemaSuffix?: string;
|
|
397
|
-
}):
|
|
368
|
+
}): LanguageModelV4Prompt;
|
|
398
369
|
|
|
399
370
|
declare function isAbortError(error: unknown): error is Error;
|
|
400
371
|
|
|
@@ -407,6 +378,12 @@ declare function isAbortError(error: unknown): error is Error;
|
|
|
407
378
|
*/
|
|
408
379
|
declare function isNonNullable<T>(value: T | undefined | null): value is NonNullable<T>;
|
|
409
380
|
|
|
381
|
+
/**
|
|
382
|
+
* Checks whether file part data is a provider reference (a mapping of provider
|
|
383
|
+
* names to provider-specific identifiers) as opposed to raw bytes or a URL.
|
|
384
|
+
*/
|
|
385
|
+
declare function isProviderReference(data: LanguageModelV4FilePart['data']): data is SharedV4ProviderReference;
|
|
386
|
+
|
|
410
387
|
/**
|
|
411
388
|
* Checks if the given URL is supported natively by the model.
|
|
412
389
|
*
|
|
@@ -459,6 +436,41 @@ declare function loadSetting({ settingValue, environmentVariableName, settingNam
|
|
|
459
436
|
description: string;
|
|
460
437
|
}): string;
|
|
461
438
|
|
|
439
|
+
type ReasoningLevel = Exclude<LanguageModelV4CallOptions['reasoning'], 'none' | 'provider-default' | undefined>;
|
|
440
|
+
declare function isCustomReasoning(reasoning: LanguageModelV4CallOptions['reasoning']): reasoning is Exclude<LanguageModelV4CallOptions['reasoning'], 'provider-default' | undefined>;
|
|
441
|
+
/**
|
|
442
|
+
* Maps a top-level reasoning level to a provider-specific effort string using
|
|
443
|
+
* the given effort map. Pushes a compatibility warning if the reasoning level
|
|
444
|
+
* maps to a different string, or an unsupported warning if the level is not
|
|
445
|
+
* present in the map.
|
|
446
|
+
*
|
|
447
|
+
* @returns The mapped effort string, or `undefined` if the level is not
|
|
448
|
+
* supported.
|
|
449
|
+
*/
|
|
450
|
+
declare function mapReasoningToProviderEffort<T extends string>({ reasoning, effortMap, warnings, }: {
|
|
451
|
+
reasoning: ReasoningLevel;
|
|
452
|
+
effortMap: Partial<Record<ReasoningLevel, T>>;
|
|
453
|
+
warnings: SharedV4Warning[];
|
|
454
|
+
}): T | undefined;
|
|
455
|
+
/**
|
|
456
|
+
* Maps a top-level reasoning level to an absolute token budget by multiplying
|
|
457
|
+
* the model's max output tokens by a percentage from the budget percentages
|
|
458
|
+
* map. The result is clamped between `minReasoningBudget` (default 1024) and
|
|
459
|
+
* `maxReasoningBudget`. Pushes an unsupported warning if the level is not
|
|
460
|
+
* present in the budget percentages map.
|
|
461
|
+
*
|
|
462
|
+
* @returns The computed token budget, or `undefined` if the level is not
|
|
463
|
+
* supported.
|
|
464
|
+
*/
|
|
465
|
+
declare function mapReasoningToProviderBudget({ reasoning, maxOutputTokens, maxReasoningBudget, minReasoningBudget, budgetPercentages, warnings, }: {
|
|
466
|
+
reasoning: ReasoningLevel;
|
|
467
|
+
maxOutputTokens: number;
|
|
468
|
+
maxReasoningBudget: number;
|
|
469
|
+
minReasoningBudget?: number;
|
|
470
|
+
budgetPercentages?: Partial<Record<ReasoningLevel, number>>;
|
|
471
|
+
warnings: SharedV4Warning[];
|
|
472
|
+
}): number | undefined;
|
|
473
|
+
|
|
462
474
|
type MaybePromiseLike<T> = T | PromiseLike<T>;
|
|
463
475
|
|
|
464
476
|
/**
|
|
@@ -549,7 +561,16 @@ type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
|
|
|
549
561
|
* They are passed through to the provider from the AI SDK and enable
|
|
550
562
|
* provider-specific functionality that can be fully encapsulated in the provider.
|
|
551
563
|
*/
|
|
552
|
-
type ProviderOptions =
|
|
564
|
+
type ProviderOptions = SharedV4ProviderOptions;
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* A mapping of provider names to provider-specific file identifiers.
|
|
568
|
+
*
|
|
569
|
+
* Provider references allow files to be identified across different
|
|
570
|
+
* providers without re-uploading, by storing each provider's own
|
|
571
|
+
* identifier for the same logical file.
|
|
572
|
+
*/
|
|
573
|
+
type ProviderReference = SharedV4ProviderReference;
|
|
553
574
|
|
|
554
575
|
/**
|
|
555
576
|
* Text content part of a prompt. It contains a string of text.
|
|
@@ -577,8 +598,9 @@ interface ImagePart {
|
|
|
577
598
|
*
|
|
578
599
|
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
579
600
|
* - URL: a URL that points to the image
|
|
601
|
+
* - ProviderReference: a provider reference from `uploadFile`
|
|
580
602
|
*/
|
|
581
|
-
image: DataContent | URL;
|
|
603
|
+
image: DataContent | URL | ProviderReference;
|
|
582
604
|
/**
|
|
583
605
|
* Optional IANA media type of the image.
|
|
584
606
|
*
|
|
@@ -601,9 +623,10 @@ interface FilePart {
|
|
|
601
623
|
* File data. Can either be:
|
|
602
624
|
*
|
|
603
625
|
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
604
|
-
* - URL: a URL that points to the
|
|
626
|
+
* - URL: a URL that points to the file
|
|
627
|
+
* - ProviderReference: a provider reference from `uploadFile`
|
|
605
628
|
*/
|
|
606
|
-
data: DataContent | URL;
|
|
629
|
+
data: DataContent | URL | ProviderReference;
|
|
607
630
|
/**
|
|
608
631
|
* Optional filename of the file.
|
|
609
632
|
*/
|
|
@@ -637,6 +660,48 @@ interface ReasoningPart {
|
|
|
637
660
|
*/
|
|
638
661
|
providerOptions?: ProviderOptions;
|
|
639
662
|
}
|
|
663
|
+
/**
|
|
664
|
+
* Custom content part of a prompt. It contains no standardized payload beyond
|
|
665
|
+
* provider-specific options.
|
|
666
|
+
*/
|
|
667
|
+
interface CustomPart {
|
|
668
|
+
type: 'custom';
|
|
669
|
+
/**
|
|
670
|
+
* The kind of custom content, in the format `{provider}.{provider-type}`.
|
|
671
|
+
*/
|
|
672
|
+
kind: `${string}.${string}`;
|
|
673
|
+
/**
|
|
674
|
+
* Additional provider-specific metadata. They are passed through
|
|
675
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
676
|
+
* functionality that can be fully encapsulated in the provider.
|
|
677
|
+
*/
|
|
678
|
+
providerOptions?: ProviderOptions;
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Reasoning file content part of a prompt. It contains a file generated as part of reasoning.
|
|
682
|
+
*/
|
|
683
|
+
interface ReasoningFilePart {
|
|
684
|
+
type: 'reasoning-file';
|
|
685
|
+
/**
|
|
686
|
+
* File data. Can either be:
|
|
687
|
+
*
|
|
688
|
+
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
689
|
+
* - URL: a URL that points to the file
|
|
690
|
+
*/
|
|
691
|
+
data: DataContent | URL;
|
|
692
|
+
/**
|
|
693
|
+
* IANA media type of the file.
|
|
694
|
+
*
|
|
695
|
+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
696
|
+
*/
|
|
697
|
+
mediaType: string;
|
|
698
|
+
/**
|
|
699
|
+
* Additional provider-specific metadata. They are passed through
|
|
700
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
701
|
+
* functionality that can be fully encapsulated in the provider.
|
|
702
|
+
*/
|
|
703
|
+
providerOptions?: ProviderOptions;
|
|
704
|
+
}
|
|
640
705
|
/**
|
|
641
706
|
* Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
|
|
642
707
|
*/
|
|
@@ -748,13 +813,6 @@ type ToolResultOutput = {
|
|
|
748
813
|
* Provider-specific options.
|
|
749
814
|
*/
|
|
750
815
|
providerOptions?: ProviderOptions;
|
|
751
|
-
} | {
|
|
752
|
-
/**
|
|
753
|
-
* @deprecated Use image-data or file-data instead.
|
|
754
|
-
*/
|
|
755
|
-
type: 'media';
|
|
756
|
-
data: string;
|
|
757
|
-
mediaType: string;
|
|
758
816
|
} | {
|
|
759
817
|
type: 'file-data';
|
|
760
818
|
/**
|
|
@@ -780,11 +838,19 @@ type ToolResultOutput = {
|
|
|
780
838
|
* URL of the file.
|
|
781
839
|
*/
|
|
782
840
|
url: string;
|
|
841
|
+
/**
|
|
842
|
+
* IANA media type.
|
|
843
|
+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
844
|
+
*/
|
|
845
|
+
mediaType?: string;
|
|
783
846
|
/**
|
|
784
847
|
* Provider-specific options.
|
|
785
848
|
*/
|
|
786
849
|
providerOptions?: ProviderOptions;
|
|
787
850
|
} | {
|
|
851
|
+
/**
|
|
852
|
+
* @deprecated Use file-reference instead.
|
|
853
|
+
*/
|
|
788
854
|
type: 'file-id';
|
|
789
855
|
/**
|
|
790
856
|
* ID of the file.
|
|
@@ -799,9 +865,20 @@ type ToolResultOutput = {
|
|
|
799
865
|
* Provider-specific options.
|
|
800
866
|
*/
|
|
801
867
|
providerOptions?: ProviderOptions;
|
|
868
|
+
} | {
|
|
869
|
+
type: 'file-reference';
|
|
870
|
+
/**
|
|
871
|
+
* Provider-specific references for the file.
|
|
872
|
+
* The key is the provider name, e.g. 'openai' or 'anthropic'.
|
|
873
|
+
*/
|
|
874
|
+
providerReference: ProviderReference;
|
|
875
|
+
/**
|
|
876
|
+
* Provider-specific options.
|
|
877
|
+
*/
|
|
878
|
+
providerOptions?: ProviderOptions;
|
|
802
879
|
} | {
|
|
803
880
|
/**
|
|
804
|
-
*
|
|
881
|
+
* @deprecated Use file-data instead.
|
|
805
882
|
*/
|
|
806
883
|
type: 'image-data';
|
|
807
884
|
/**
|
|
@@ -819,7 +896,7 @@ type ToolResultOutput = {
|
|
|
819
896
|
providerOptions?: ProviderOptions;
|
|
820
897
|
} | {
|
|
821
898
|
/**
|
|
822
|
-
*
|
|
899
|
+
* @deprecated Use file-url instead.
|
|
823
900
|
*/
|
|
824
901
|
type: 'image-url';
|
|
825
902
|
/**
|
|
@@ -832,7 +909,7 @@ type ToolResultOutput = {
|
|
|
832
909
|
providerOptions?: ProviderOptions;
|
|
833
910
|
} | {
|
|
834
911
|
/**
|
|
835
|
-
*
|
|
912
|
+
* @deprecated Use file-reference instead.
|
|
836
913
|
*/
|
|
837
914
|
type: 'image-file-id';
|
|
838
915
|
/**
|
|
@@ -848,6 +925,20 @@ type ToolResultOutput = {
|
|
|
848
925
|
* Provider-specific options.
|
|
849
926
|
*/
|
|
850
927
|
providerOptions?: ProviderOptions;
|
|
928
|
+
} | {
|
|
929
|
+
/**
|
|
930
|
+
* @deprecated Use file-reference instead.
|
|
931
|
+
*/
|
|
932
|
+
type: 'image-file-reference';
|
|
933
|
+
/**
|
|
934
|
+
* Provider-specific references for the image file.
|
|
935
|
+
* The key is the provider name, e.g. 'openai' or 'anthropic'.
|
|
936
|
+
*/
|
|
937
|
+
providerReference: ProviderReference;
|
|
938
|
+
/**
|
|
939
|
+
* Provider-specific options.
|
|
940
|
+
*/
|
|
941
|
+
providerOptions?: ProviderOptions;
|
|
851
942
|
} | {
|
|
852
943
|
/**
|
|
853
944
|
* Custom content part. This can be used to implement
|
|
@@ -893,7 +984,7 @@ type AssistantModelMessage = {
|
|
|
893
984
|
* Content of an assistant message.
|
|
894
985
|
* It can be a string or an array of text, image, reasoning, redacted reasoning, and tool call parts.
|
|
895
986
|
*/
|
|
896
|
-
type AssistantContent = string | Array<TextPart | FilePart | ReasoningPart | ToolCallPart | ToolResultPart | ToolApprovalRequest>;
|
|
987
|
+
type AssistantContent = string | Array<TextPart | CustomPart | FilePart | ReasoningPart | ReasoningFilePart | ToolCallPart | ToolResultPart | ToolApprovalRequest>;
|
|
897
988
|
|
|
898
989
|
/**
|
|
899
990
|
* A system message. It can contain system information.
|
|
@@ -980,9 +1071,14 @@ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
|
|
|
980
1071
|
type ModelMessage = SystemModelMessage | UserModelMessage | AssistantModelMessage | ToolModelMessage;
|
|
981
1072
|
|
|
982
1073
|
/**
|
|
983
|
-
*
|
|
1074
|
+
* A context object that is passed into tool execution.
|
|
984
1075
|
*/
|
|
985
|
-
|
|
1076
|
+
type Context = Record<string, unknown>;
|
|
1077
|
+
|
|
1078
|
+
/**
|
|
1079
|
+
* Additional options that are sent into each tool execution.
|
|
1080
|
+
*/
|
|
1081
|
+
interface ToolExecutionOptions<CONTEXT extends Context | unknown | never> {
|
|
986
1082
|
/**
|
|
987
1083
|
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
988
1084
|
*/
|
|
@@ -997,7 +1093,7 @@ interface ToolExecutionOptions {
|
|
|
997
1093
|
*/
|
|
998
1094
|
abortSignal?: AbortSignal;
|
|
999
1095
|
/**
|
|
1000
|
-
* User-defined context.
|
|
1096
|
+
* User-defined runtime context.
|
|
1001
1097
|
*
|
|
1002
1098
|
* Treat the context object as immutable inside tools.
|
|
1003
1099
|
* Mutating the context object can lead to race conditions and unexpected results
|
|
@@ -1005,15 +1101,13 @@ interface ToolExecutionOptions {
|
|
|
1005
1101
|
*
|
|
1006
1102
|
* If you need to mutate the context, analyze the tool calls and results
|
|
1007
1103
|
* in `prepareStep` and update it there.
|
|
1008
|
-
*
|
|
1009
|
-
* Experimental (can break in patch releases).
|
|
1010
1104
|
*/
|
|
1011
|
-
|
|
1105
|
+
context: CONTEXT;
|
|
1012
1106
|
}
|
|
1013
1107
|
/**
|
|
1014
1108
|
* Function that is called to determine if the tool needs approval before it can be executed.
|
|
1015
1109
|
*/
|
|
1016
|
-
type ToolNeedsApprovalFunction<INPUT> = (input: INPUT, options: {
|
|
1110
|
+
type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context | unknown | never> = (input: INPUT, options: {
|
|
1017
1111
|
/**
|
|
1018
1112
|
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
1019
1113
|
*/
|
|
@@ -1024,15 +1118,26 @@ type ToolNeedsApprovalFunction<INPUT> = (input: INPUT, options: {
|
|
|
1024
1118
|
*/
|
|
1025
1119
|
messages: ModelMessage[];
|
|
1026
1120
|
/**
|
|
1027
|
-
*
|
|
1121
|
+
* User-defined runtime context.
|
|
1122
|
+
*
|
|
1123
|
+
* Treat the context object as immutable inside tools.
|
|
1124
|
+
* Mutating the context object can lead to race conditions and unexpected results
|
|
1125
|
+
* when tools are called in parallel.
|
|
1028
1126
|
*
|
|
1029
|
-
*
|
|
1127
|
+
* If you need to mutate the context, analyze the tool calls and results
|
|
1128
|
+
* in `prepareStep` and update it there.
|
|
1030
1129
|
*/
|
|
1031
|
-
|
|
1130
|
+
context: CONTEXT;
|
|
1032
1131
|
}) => boolean | PromiseLike<boolean>;
|
|
1033
|
-
|
|
1132
|
+
/**
|
|
1133
|
+
* Function that executes the tool and returns either a single result or a stream of results.
|
|
1134
|
+
*/
|
|
1135
|
+
type ToolExecuteFunction<INPUT, OUTPUT, CONTEXT extends Context | unknown | never> = (input: INPUT, options: ToolExecutionOptions<CONTEXT>) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
|
|
1034
1136
|
type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
|
|
1035
|
-
|
|
1137
|
+
/**
|
|
1138
|
+
* Helper type to determine the output properties of a tool.
|
|
1139
|
+
*/
|
|
1140
|
+
type ToolOutputProperties<INPUT, OUTPUT, CONTEXT extends Context | unknown | never> = NeverOptional<OUTPUT, {
|
|
1036
1141
|
/**
|
|
1037
1142
|
* An async function that is called with the arguments from the tool call and produces a result.
|
|
1038
1143
|
* If not provided, the tool will not be executed automatically.
|
|
@@ -1040,7 +1145,7 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<OUTPUT, {
|
|
|
1040
1145
|
* @args is the input of the tool call.
|
|
1041
1146
|
* @options.abortSignal is a signal that can be used to abort the tool call.
|
|
1042
1147
|
*/
|
|
1043
|
-
execute: ToolExecuteFunction<INPUT, OUTPUT>;
|
|
1148
|
+
execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1044
1149
|
outputSchema?: FlexibleSchema<OUTPUT>;
|
|
1045
1150
|
} | {
|
|
1046
1151
|
outputSchema: FlexibleSchema<OUTPUT>;
|
|
@@ -1052,7 +1157,7 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<OUTPUT, {
|
|
|
1052
1157
|
*
|
|
1053
1158
|
* The tool can also contain an optional execute function for the actual execution function of the tool.
|
|
1054
1159
|
*/
|
|
1055
|
-
type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any> = {
|
|
1160
|
+
type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = {
|
|
1056
1161
|
/**
|
|
1057
1162
|
* An optional description of what the tool does.
|
|
1058
1163
|
* Will be used by the language model to decide whether to use the tool.
|
|
@@ -1084,10 +1189,18 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1084
1189
|
inputExamples?: Array<{
|
|
1085
1190
|
input: NoInfer<INPUT>;
|
|
1086
1191
|
}>;
|
|
1192
|
+
/**
|
|
1193
|
+
* An optional schema describing the context that the tool expects.
|
|
1194
|
+
*
|
|
1195
|
+
* The context is passed to execute function as part of the execution options.
|
|
1196
|
+
*/
|
|
1197
|
+
contextSchema?: FlexibleSchema<CONTEXT>;
|
|
1087
1198
|
/**
|
|
1088
1199
|
* Whether the tool needs approval before it can be executed.
|
|
1089
1200
|
*/
|
|
1090
|
-
needsApproval?: boolean | ToolNeedsApprovalFunction<[
|
|
1201
|
+
needsApproval?: boolean | ToolNeedsApprovalFunction<[
|
|
1202
|
+
INPUT
|
|
1203
|
+
] extends [never] ? unknown : INPUT, NoInfer<CONTEXT>>;
|
|
1091
1204
|
/**
|
|
1092
1205
|
* Strict mode setting for the tool.
|
|
1093
1206
|
*
|
|
@@ -1100,26 +1213,28 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1100
1213
|
* Optional function that is called when the argument streaming starts.
|
|
1101
1214
|
* Only called when the tool is used in a streaming context.
|
|
1102
1215
|
*/
|
|
1103
|
-
onInputStart?: (options: ToolExecutionOptions) => void | PromiseLike<void>;
|
|
1216
|
+
onInputStart?: (options: ToolExecutionOptions<NoInfer<CONTEXT>>) => void | PromiseLike<void>;
|
|
1104
1217
|
/**
|
|
1105
1218
|
* Optional function that is called when an argument streaming delta is available.
|
|
1106
1219
|
* Only called when the tool is used in a streaming context.
|
|
1107
1220
|
*/
|
|
1108
1221
|
onInputDelta?: (options: {
|
|
1109
1222
|
inputTextDelta: string;
|
|
1110
|
-
} & ToolExecutionOptions) => void | PromiseLike<void>;
|
|
1223
|
+
} & ToolExecutionOptions<NoInfer<CONTEXT>>) => void | PromiseLike<void>;
|
|
1111
1224
|
/**
|
|
1112
1225
|
* Optional function that is called when a tool call can be started,
|
|
1113
1226
|
* even if the execute function is not provided.
|
|
1114
1227
|
*/
|
|
1115
1228
|
onInputAvailable?: (options: {
|
|
1116
1229
|
input: [INPUT] extends [never] ? unknown : INPUT;
|
|
1117
|
-
} & ToolExecutionOptions) => void | PromiseLike<void>;
|
|
1118
|
-
} & ToolOutputProperties<INPUT, OUTPUT
|
|
1230
|
+
} & ToolExecutionOptions<NoInfer<CONTEXT>>) => void | PromiseLike<void>;
|
|
1231
|
+
} & ToolOutputProperties<INPUT, OUTPUT, NoInfer<CONTEXT>> & {
|
|
1119
1232
|
/**
|
|
1120
1233
|
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
1121
1234
|
*
|
|
1122
1235
|
* If not provided, the tool result will be sent as a JSON object.
|
|
1236
|
+
*
|
|
1237
|
+
* 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).
|
|
1123
1238
|
*/
|
|
1124
1239
|
toModelOutput?: (options: {
|
|
1125
1240
|
/**
|
|
@@ -1174,21 +1289,13 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1174
1289
|
*/
|
|
1175
1290
|
supportsDeferredResults?: boolean;
|
|
1176
1291
|
});
|
|
1177
|
-
/**
|
|
1178
|
-
* Infer the input type of a tool.
|
|
1179
|
-
*/
|
|
1180
|
-
type InferToolInput<TOOL extends Tool> = TOOL extends Tool<infer INPUT, any> ? INPUT : never;
|
|
1181
|
-
/**
|
|
1182
|
-
* Infer the output type of a tool.
|
|
1183
|
-
*/
|
|
1184
|
-
type InferToolOutput<TOOL extends Tool> = TOOL extends Tool<any, infer OUTPUT> ? OUTPUT : never;
|
|
1185
1292
|
/**
|
|
1186
1293
|
* Helper function for inferring the execute args of a tool.
|
|
1187
1294
|
*/
|
|
1188
|
-
declare function tool<INPUT, OUTPUT>(tool: Tool<INPUT, OUTPUT>): Tool<INPUT, OUTPUT>;
|
|
1189
|
-
declare function tool<INPUT>(tool: Tool<INPUT, never>): Tool<INPUT, never>;
|
|
1190
|
-
declare function tool<OUTPUT>(tool: Tool<never, OUTPUT>): Tool<never, OUTPUT>;
|
|
1191
|
-
declare function tool(tool: Tool<never, never>): Tool<never, never>;
|
|
1295
|
+
declare function tool<INPUT, OUTPUT, CONTEXT extends Context>(tool: Tool<INPUT, OUTPUT, CONTEXT>): Tool<INPUT, OUTPUT, CONTEXT>;
|
|
1296
|
+
declare function tool<INPUT, CONTEXT extends Context>(tool: Tool<INPUT, never, CONTEXT>): Tool<INPUT, never, CONTEXT>;
|
|
1297
|
+
declare function tool<OUTPUT, CONTEXT extends Context>(tool: Tool<never, OUTPUT, CONTEXT>): Tool<never, OUTPUT, CONTEXT>;
|
|
1298
|
+
declare function tool<CONTEXT extends Context>(tool: Tool<never, never, CONTEXT>): Tool<never, never, CONTEXT>;
|
|
1192
1299
|
/**
|
|
1193
1300
|
* Defines a dynamic tool.
|
|
1194
1301
|
*/
|
|
@@ -1197,7 +1304,7 @@ declare function dynamicTool(tool: {
|
|
|
1197
1304
|
title?: string;
|
|
1198
1305
|
providerOptions?: ProviderOptions;
|
|
1199
1306
|
inputSchema: FlexibleSchema<unknown>;
|
|
1200
|
-
execute: ToolExecuteFunction<unknown, unknown>;
|
|
1307
|
+
execute: ToolExecuteFunction<unknown, unknown, Context>;
|
|
1201
1308
|
/**
|
|
1202
1309
|
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
1203
1310
|
*
|
|
@@ -1220,32 +1327,32 @@ declare function dynamicTool(tool: {
|
|
|
1220
1327
|
/**
|
|
1221
1328
|
* Whether the tool needs approval before it can be executed.
|
|
1222
1329
|
*/
|
|
1223
|
-
needsApproval?: boolean | ToolNeedsApprovalFunction<unknown>;
|
|
1224
|
-
}): Tool<unknown, unknown> & {
|
|
1330
|
+
needsApproval?: boolean | ToolNeedsApprovalFunction<unknown, Context>;
|
|
1331
|
+
}): Tool<unknown, unknown, Context> & {
|
|
1225
1332
|
type: 'dynamic';
|
|
1226
1333
|
};
|
|
1227
1334
|
|
|
1228
|
-
type ProviderToolFactory<INPUT, ARGS extends object> = <OUTPUT>(options: ARGS & {
|
|
1229
|
-
execute?: ToolExecuteFunction<INPUT, OUTPUT>;
|
|
1230
|
-
needsApproval?: Tool<INPUT, OUTPUT>['needsApproval'];
|
|
1231
|
-
toModelOutput?: Tool<INPUT, OUTPUT>['toModelOutput'];
|
|
1232
|
-
onInputStart?: Tool<INPUT, OUTPUT>['onInputStart'];
|
|
1233
|
-
onInputDelta?: Tool<INPUT, OUTPUT>['onInputDelta'];
|
|
1234
|
-
onInputAvailable?: Tool<INPUT, OUTPUT>['onInputAvailable'];
|
|
1235
|
-
}) => Tool<INPUT, OUTPUT>;
|
|
1236
|
-
declare function createProviderToolFactory<INPUT, ARGS extends object>({ id, inputSchema, }: {
|
|
1335
|
+
type ProviderToolFactory<INPUT, ARGS extends object, CONTEXT extends Context = {}> = <OUTPUT>(options: ARGS & {
|
|
1336
|
+
execute?: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1337
|
+
needsApproval?: Tool<INPUT, OUTPUT, CONTEXT>['needsApproval'];
|
|
1338
|
+
toModelOutput?: Tool<INPUT, OUTPUT, CONTEXT>['toModelOutput'];
|
|
1339
|
+
onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
|
|
1340
|
+
onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
|
|
1341
|
+
onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
|
|
1342
|
+
}) => Tool<INPUT, OUTPUT, CONTEXT>;
|
|
1343
|
+
declare function createProviderToolFactory<INPUT, ARGS extends object, CONTEXT extends Context = {}>({ id, inputSchema, }: {
|
|
1237
1344
|
id: `${string}.${string}`;
|
|
1238
1345
|
inputSchema: FlexibleSchema<INPUT>;
|
|
1239
|
-
}): ProviderToolFactory<INPUT, ARGS>;
|
|
1240
|
-
type ProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object> = (options: ARGS & {
|
|
1241
|
-
execute?: ToolExecuteFunction<INPUT, OUTPUT>;
|
|
1242
|
-
needsApproval?: Tool<INPUT, OUTPUT>['needsApproval'];
|
|
1243
|
-
toModelOutput?: Tool<INPUT, OUTPUT>['toModelOutput'];
|
|
1244
|
-
onInputStart?: Tool<INPUT, OUTPUT>['onInputStart'];
|
|
1245
|
-
onInputDelta?: Tool<INPUT, OUTPUT>['onInputDelta'];
|
|
1246
|
-
onInputAvailable?: Tool<INPUT, OUTPUT>['onInputAvailable'];
|
|
1247
|
-
}) => Tool<INPUT, OUTPUT>;
|
|
1248
|
-
declare function createProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object>({ id, inputSchema, outputSchema, supportsDeferredResults, }: {
|
|
1346
|
+
}): ProviderToolFactory<INPUT, ARGS, CONTEXT>;
|
|
1347
|
+
type ProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object, CONTEXT extends Context = {}> = (options: ARGS & {
|
|
1348
|
+
execute?: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1349
|
+
needsApproval?: Tool<INPUT, OUTPUT, CONTEXT>['needsApproval'];
|
|
1350
|
+
toModelOutput?: Tool<INPUT, OUTPUT, CONTEXT>['toModelOutput'];
|
|
1351
|
+
onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
|
|
1352
|
+
onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
|
|
1353
|
+
onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
|
|
1354
|
+
}) => Tool<INPUT, OUTPUT, CONTEXT>;
|
|
1355
|
+
declare function createProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object, CONTEXT extends Context = {}>({ id, inputSchema, outputSchema, supportsDeferredResults, }: {
|
|
1249
1356
|
id: `${string}.${string}`;
|
|
1250
1357
|
inputSchema: FlexibleSchema<INPUT>;
|
|
1251
1358
|
outputSchema: FlexibleSchema<OUTPUT>;
|
|
@@ -1260,7 +1367,38 @@ declare function createProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS e
|
|
|
1260
1367
|
* @default false
|
|
1261
1368
|
*/
|
|
1262
1369
|
supportsDeferredResults?: boolean;
|
|
1263
|
-
}): ProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS>;
|
|
1370
|
+
}): ProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS, CONTEXT>;
|
|
1371
|
+
|
|
1372
|
+
/**
|
|
1373
|
+
* Default maximum download size: 2 GiB.
|
|
1374
|
+
*
|
|
1375
|
+
* `fetch().arrayBuffer()` has ~2x peak memory overhead (undici buffers the
|
|
1376
|
+
* body internally, then creates the JS ArrayBuffer), so very large downloads
|
|
1377
|
+
* risk exceeding the default V8 heap limit on 64-bit systems and terminating
|
|
1378
|
+
* the process with an out-of-memory error.
|
|
1379
|
+
*
|
|
1380
|
+
* Setting this limit converts an unrecoverable OOM crash into a catchable
|
|
1381
|
+
* `DownloadError`.
|
|
1382
|
+
*/
|
|
1383
|
+
declare const DEFAULT_MAX_DOWNLOAD_SIZE: number;
|
|
1384
|
+
/**
|
|
1385
|
+
* Reads a fetch Response body with a size limit to prevent memory exhaustion.
|
|
1386
|
+
*
|
|
1387
|
+
* Checks the Content-Length header for early rejection, then reads the body
|
|
1388
|
+
* incrementally via ReadableStream and aborts with a DownloadError when the
|
|
1389
|
+
* limit is exceeded.
|
|
1390
|
+
*
|
|
1391
|
+
* @param response - The fetch Response to read.
|
|
1392
|
+
* @param url - The URL being downloaded (used in error messages).
|
|
1393
|
+
* @param maxBytes - Maximum allowed bytes. Defaults to DEFAULT_MAX_DOWNLOAD_SIZE.
|
|
1394
|
+
* @returns A Uint8Array containing the response body.
|
|
1395
|
+
* @throws DownloadError if the response exceeds maxBytes.
|
|
1396
|
+
*/
|
|
1397
|
+
declare function readResponseWithSizeLimit({ response, url, maxBytes, }: {
|
|
1398
|
+
response: Response;
|
|
1399
|
+
url: string;
|
|
1400
|
+
maxBytes?: number;
|
|
1401
|
+
}): Promise<Uint8Array>;
|
|
1264
1402
|
|
|
1265
1403
|
/**
|
|
1266
1404
|
* Removes entries from a record where the value is null or undefined.
|
|
@@ -1269,6 +1407,21 @@ declare function createProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS e
|
|
|
1269
1407
|
*/
|
|
1270
1408
|
declare function removeUndefinedEntries<T>(record: Record<string, T | undefined>): Record<string, T>;
|
|
1271
1409
|
|
|
1410
|
+
/**
|
|
1411
|
+
* A value or a lazy provider of a value, each of which may be synchronous or asynchronous.
|
|
1412
|
+
*
|
|
1413
|
+
* @template T The resolved type after {@link resolve} runs.
|
|
1414
|
+
*
|
|
1415
|
+
* One of:
|
|
1416
|
+
* - A plain value of type {@link T}
|
|
1417
|
+
* - A {@link PromiseLike} of {@link T} (e.g. a `Promise<T>`)
|
|
1418
|
+
* - A zero-argument function that returns a plain {@link T}
|
|
1419
|
+
* - A zero-argument function that returns a {@link PromiseLike} of {@link T}
|
|
1420
|
+
*
|
|
1421
|
+
* The function form is only invoked when passed to {@link resolve}; it is not distinguished from
|
|
1422
|
+
* a {@link T} that happens to be a function—callers should wrap function values if disambiguation
|
|
1423
|
+
* is required.
|
|
1424
|
+
*/
|
|
1272
1425
|
type Resolvable<T> = MaybePromiseLike<T> | (() => MaybePromiseLike<T>);
|
|
1273
1426
|
/**
|
|
1274
1427
|
* Resolves a value that could be a raw value, a Promise, a function returning a value,
|
|
@@ -1276,6 +1429,46 @@ type Resolvable<T> = MaybePromiseLike<T> | (() => MaybePromiseLike<T>);
|
|
|
1276
1429
|
*/
|
|
1277
1430
|
declare function resolve<T>(value: Resolvable<T>): Promise<T>;
|
|
1278
1431
|
|
|
1432
|
+
/**
|
|
1433
|
+
* Resolves a provider reference to the provider-specific identifier for the
|
|
1434
|
+
* given provider. Throws `NoSuchProviderReferenceError` if the provider is not
|
|
1435
|
+
* found in the reference mapping.
|
|
1436
|
+
*/
|
|
1437
|
+
declare function resolveProviderReference({ reference, provider, }: {
|
|
1438
|
+
reference: SharedV4ProviderReference;
|
|
1439
|
+
provider: string;
|
|
1440
|
+
}): string;
|
|
1441
|
+
|
|
1442
|
+
/**
|
|
1443
|
+
* Serializes a model instance for workflow step boundaries.
|
|
1444
|
+
* Returns the `modelId` plus the JSON-serializable config properties.
|
|
1445
|
+
*
|
|
1446
|
+
* Non-serializable values are omitted. As a special case, a
|
|
1447
|
+
* function-valued `headers` property is resolved during serialization
|
|
1448
|
+
* and included if the returned value is JSON-serializable.
|
|
1449
|
+
*
|
|
1450
|
+
* Used as the body of `static [WORKFLOW_SERIALIZE]` in provider models.
|
|
1451
|
+
*
|
|
1452
|
+
* @example
|
|
1453
|
+
* ```ts
|
|
1454
|
+
* static [WORKFLOW_SERIALIZE](model: MyLanguageModel) {
|
|
1455
|
+
* return serializeModelOptions({
|
|
1456
|
+
* modelId: model.modelId,
|
|
1457
|
+
* config: model.config,
|
|
1458
|
+
* });
|
|
1459
|
+
* }
|
|
1460
|
+
* ```
|
|
1461
|
+
*/
|
|
1462
|
+
declare function serializeModelOptions<CONFIG extends {
|
|
1463
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
|
1464
|
+
}>(options: {
|
|
1465
|
+
modelId: string;
|
|
1466
|
+
config: CONFIG;
|
|
1467
|
+
}): {
|
|
1468
|
+
modelId: string;
|
|
1469
|
+
config: JSONObject;
|
|
1470
|
+
};
|
|
1471
|
+
|
|
1279
1472
|
/**
|
|
1280
1473
|
* Strips file extension segments from a filename.
|
|
1281
1474
|
*
|
|
@@ -1354,18 +1547,89 @@ declare function withUserAgentSuffix(headers: HeadersInit | Record<string, strin
|
|
|
1354
1547
|
|
|
1355
1548
|
declare function withoutTrailingSlash(url: string | undefined): string | undefined;
|
|
1356
1549
|
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1550
|
+
/**
|
|
1551
|
+
* A tool that is guaranteed to expose an execute function.
|
|
1552
|
+
*/
|
|
1553
|
+
type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
|
|
1554
|
+
execute: NonNullable<TOOL['execute']>;
|
|
1555
|
+
};
|
|
1556
|
+
/**
|
|
1557
|
+
* Checks whether a tool exposes an execute function.
|
|
1558
|
+
*/
|
|
1559
|
+
declare function isExecutableTool<TOOL extends Tool>(tool: TOOL | undefined): tool is ExecutableTool<TOOL>;
|
|
1560
|
+
|
|
1561
|
+
/**
|
|
1562
|
+
* Infer the context type of a tool.
|
|
1563
|
+
*/
|
|
1564
|
+
type InferToolContext<TOOL extends Tool> = TOOL extends Tool<any, any, infer CONTEXT> ? HasRequiredKey<CONTEXT> extends true ? CONTEXT : never : never;
|
|
1565
|
+
|
|
1566
|
+
/**
|
|
1567
|
+
* Infer the input type of a tool.
|
|
1568
|
+
*/
|
|
1569
|
+
type InferToolInput<TOOL extends Tool<any, any, any>> = TOOL extends Tool<infer INPUT, any, any> ? INPUT : never;
|
|
1570
|
+
|
|
1571
|
+
/**
|
|
1572
|
+
* Infer the output type of a tool.
|
|
1573
|
+
*/
|
|
1574
|
+
type InferToolOutput<TOOL extends Tool<any, any, any>> = TOOL extends Tool<any, infer OUTPUT, any> ? OUTPUT : never;
|
|
1575
|
+
|
|
1576
|
+
/**
|
|
1577
|
+
* Executes a tool function and normalizes its results into a stream of outputs.
|
|
1578
|
+
*
|
|
1579
|
+
* - If the tool's `execute` function returns an `AsyncIterable`, each yielded value is emitted as
|
|
1580
|
+
* `{ type: "preliminary", output }`. After iteration completes, the last yielded value is emitted
|
|
1581
|
+
* again as `{ type: "final", output }`.
|
|
1582
|
+
* - If the tool returns a direct value or Promise, a single `{ type: "final", output }` is yielded.
|
|
1583
|
+
*
|
|
1584
|
+
* @param params.tool The tool whose `execute` function should be invoked.
|
|
1585
|
+
* @param params.input The input value to pass to the tool.
|
|
1586
|
+
* @param params.options Additional options for tool execution.
|
|
1587
|
+
* @yields A preliminary output for each streamed value, followed by a final output, or a single final
|
|
1588
|
+
* output for non-streaming tools.
|
|
1589
|
+
*/
|
|
1590
|
+
declare function executeTool<TOOL extends Tool>({ tool, input, options, }: {
|
|
1591
|
+
tool: ExecutableTool<TOOL>;
|
|
1592
|
+
input: InferToolInput<TOOL>;
|
|
1593
|
+
options: ToolExecutionOptions<InferToolContext<TOOL>>;
|
|
1361
1594
|
}): AsyncGenerator<{
|
|
1362
1595
|
type: 'preliminary';
|
|
1363
|
-
output:
|
|
1596
|
+
output: InferToolOutput<TOOL>;
|
|
1364
1597
|
} | {
|
|
1365
1598
|
type: 'final';
|
|
1366
|
-
output:
|
|
1599
|
+
output: InferToolOutput<TOOL>;
|
|
1367
1600
|
}>;
|
|
1368
1601
|
|
|
1602
|
+
/**
|
|
1603
|
+
* A mapping of tool names to tool definitions.
|
|
1604
|
+
*/
|
|
1605
|
+
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'>>;
|
|
1606
|
+
|
|
1607
|
+
/**
|
|
1608
|
+
* Converts a union type `U` into an intersection type.
|
|
1609
|
+
*
|
|
1610
|
+
* For example:
|
|
1611
|
+
* type A = { a: number };
|
|
1612
|
+
* type B = { b: string };
|
|
1613
|
+
* type Union = A | B;
|
|
1614
|
+
* type Intersection = UnionToIntersection<Union>;
|
|
1615
|
+
* // Intersection is: { a: number } & { b: string }
|
|
1616
|
+
*
|
|
1617
|
+
* This is useful when you have a union of object types and need a type with all possible properties.
|
|
1618
|
+
*/
|
|
1619
|
+
type UnionToIntersection<U> = (U extends unknown ? (arg: U) => void : never) extends (arg: infer I) => void ? I : never;
|
|
1620
|
+
|
|
1621
|
+
/**
|
|
1622
|
+
* Infer the context type for a tool set.
|
|
1623
|
+
*
|
|
1624
|
+
* The inferred type contains all properties required by the contexts of the
|
|
1625
|
+
* tools in the set.
|
|
1626
|
+
*
|
|
1627
|
+
* If there are incompatible properties, they will be of type `never`.
|
|
1628
|
+
*/
|
|
1629
|
+
type InferToolSetContext<TOOLS extends ToolSet> = UnionToIntersection<{
|
|
1630
|
+
[K in keyof TOOLS]: InferToolContext<NoInfer<TOOLS[K]>>;
|
|
1631
|
+
}[keyof TOOLS]>;
|
|
1632
|
+
|
|
1369
1633
|
/**
|
|
1370
1634
|
* Typed tool call that is returned by generateText and streamText.
|
|
1371
1635
|
* It contains the tool call ID, the tool name, and the tool arguments.
|
|
@@ -1425,9 +1689,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
|
|
|
1425
1689
|
dynamic?: boolean;
|
|
1426
1690
|
}
|
|
1427
1691
|
|
|
1428
|
-
|
|
1429
|
-
* @deprecated Use ToolExecutionOptions instead.
|
|
1430
|
-
*/
|
|
1431
|
-
type ToolCallOptions = ToolExecutionOptions;
|
|
1432
|
-
|
|
1433
|
-
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 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 };
|
|
1692
|
+
export { type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type ExecutableTool, type FetchFunction, type FilePart, type FlexibleSchema, 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 ProviderOptions, type ProviderReference, 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 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, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isCustomReasoning, isExecutableTool, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveProviderReference, safeParseJSON, safeValidateTypes, serializeModelOptions, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|