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