@ai-sdk/provider-utils 5.0.0-beta.1 → 5.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +142 -0
- package/dist/index.d.mts +276 -76
- package/dist/index.d.ts +276 -76
- package/dist/index.js +114 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +99 -16
- package/dist/index.mjs.map +1 -1
- package/dist/test/index.js +3 -3
- package/package.json +2 -4
- package/src/convert-image-model-file-to-data-uri.ts +3 -3
- package/src/create-tool-name-mapping.ts +5 -21
- package/src/index.ts +7 -0
- package/src/inject-json-instruction.ts +5 -5
- package/src/is-provider-reference.ts +18 -0
- 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/types/assistant-model-message.ts +4 -0
- package/src/types/content-part.ts +90 -4
- 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 +7 -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 +57 -35
- package/src/types/union-to-intersection.ts +17 -0
- package/src/validate-download-url.ts +7 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4FunctionTool, LanguageModelV4ProviderTool, ImageModelV4File, AISDKError, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV4Prompt, LanguageModelV4FilePart, SharedV4ProviderReference, LanguageModelV4CallOptions, SharedV4Warning, SharedV4ProviderOptions, 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';
|
|
@@ -41,20 +41,15 @@ interface ToolNameMapping {
|
|
|
41
41
|
* @param tools - Tools that were passed to the language model.
|
|
42
42
|
* @param providerToolNames - Maps the provider tool ids to the provider tool names.
|
|
43
43
|
*/
|
|
44
|
-
declare function createToolNameMapping({ tools, providerToolNames,
|
|
44
|
+
declare function createToolNameMapping({ tools, providerToolNames, }: {
|
|
45
45
|
/**
|
|
46
46
|
* Tools that were passed to the language model.
|
|
47
47
|
*/
|
|
48
|
-
tools: Array<
|
|
48
|
+
tools: Array<LanguageModelV4FunctionTool | LanguageModelV4ProviderTool> | undefined;
|
|
49
49
|
/**
|
|
50
50
|
* Maps the provider tool ids to the provider tool names.
|
|
51
51
|
*/
|
|
52
52
|
providerToolNames: Record<`${string}.${string}`, string>;
|
|
53
|
-
/**
|
|
54
|
-
* Optional resolver for provider tool names that cannot be represented as
|
|
55
|
-
* static id -> name mappings (e.g. dynamic provider names).
|
|
56
|
-
*/
|
|
57
|
-
resolveProviderToolName?: (tool: LanguageModelV3ProviderTool) => string | undefined;
|
|
58
53
|
}): ToolNameMapping;
|
|
59
54
|
|
|
60
55
|
/**
|
|
@@ -97,13 +92,13 @@ declare function extractResponseHeaders(response: Response): {
|
|
|
97
92
|
};
|
|
98
93
|
|
|
99
94
|
/**
|
|
100
|
-
* Convert an
|
|
95
|
+
* Convert an ImageModelV4File to a URL or data URI string.
|
|
101
96
|
*
|
|
102
97
|
* If the file is a URL, it returns the URL as-is.
|
|
103
98
|
* If the file is base64 data, it returns a data URI with the base64 data.
|
|
104
99
|
* If the file is a Uint8Array, it converts it to base64 and returns a data URI.
|
|
105
100
|
*/
|
|
106
|
-
declare function convertImageModelFileToDataUri(file:
|
|
101
|
+
declare function convertImageModelFileToDataUri(file: ImageModelV4File): string;
|
|
107
102
|
|
|
108
103
|
/**
|
|
109
104
|
* Converts an input object to FormData for multipart/form-data requests.
|
|
@@ -390,11 +385,11 @@ declare const getFromApi: <T>({ url, headers, successfulResponseHandler, failedR
|
|
|
390
385
|
declare function getRuntimeEnvironmentUserAgent(globalThisAny?: any): string;
|
|
391
386
|
|
|
392
387
|
declare function injectJsonInstructionIntoMessages({ messages, schema, schemaPrefix, schemaSuffix, }: {
|
|
393
|
-
messages:
|
|
388
|
+
messages: LanguageModelV4Prompt;
|
|
394
389
|
schema?: JSONSchema7;
|
|
395
390
|
schemaPrefix?: string;
|
|
396
391
|
schemaSuffix?: string;
|
|
397
|
-
}):
|
|
392
|
+
}): LanguageModelV4Prompt;
|
|
398
393
|
|
|
399
394
|
declare function isAbortError(error: unknown): error is Error;
|
|
400
395
|
|
|
@@ -407,6 +402,12 @@ declare function isAbortError(error: unknown): error is Error;
|
|
|
407
402
|
*/
|
|
408
403
|
declare function isNonNullable<T>(value: T | undefined | null): value is NonNullable<T>;
|
|
409
404
|
|
|
405
|
+
/**
|
|
406
|
+
* Checks whether file part data is a provider reference (a mapping of provider
|
|
407
|
+
* names to provider-specific identifiers) as opposed to raw bytes or a URL.
|
|
408
|
+
*/
|
|
409
|
+
declare function isProviderReference(data: LanguageModelV4FilePart['data']): data is SharedV4ProviderReference;
|
|
410
|
+
|
|
410
411
|
/**
|
|
411
412
|
* Checks if the given URL is supported natively by the model.
|
|
412
413
|
*
|
|
@@ -431,6 +432,41 @@ declare function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterNa
|
|
|
431
432
|
description: string;
|
|
432
433
|
}): string;
|
|
433
434
|
|
|
435
|
+
type ReasoningLevel = Exclude<LanguageModelV4CallOptions['reasoning'], 'none' | 'provider-default' | undefined>;
|
|
436
|
+
declare function isCustomReasoning(reasoning: LanguageModelV4CallOptions['reasoning']): reasoning is Exclude<LanguageModelV4CallOptions['reasoning'], 'provider-default' | undefined>;
|
|
437
|
+
/**
|
|
438
|
+
* Maps a top-level reasoning level to a provider-specific effort string using
|
|
439
|
+
* the given effort map. Pushes a compatibility warning if the reasoning level
|
|
440
|
+
* maps to a different string, or an unsupported warning if the level is not
|
|
441
|
+
* present in the map.
|
|
442
|
+
*
|
|
443
|
+
* @returns The mapped effort string, or `undefined` if the level is not
|
|
444
|
+
* supported.
|
|
445
|
+
*/
|
|
446
|
+
declare function mapReasoningToProviderEffort<T extends string>({ reasoning, effortMap, warnings, }: {
|
|
447
|
+
reasoning: ReasoningLevel;
|
|
448
|
+
effortMap: Partial<Record<ReasoningLevel, T>>;
|
|
449
|
+
warnings: SharedV4Warning[];
|
|
450
|
+
}): T | undefined;
|
|
451
|
+
/**
|
|
452
|
+
* Maps a top-level reasoning level to an absolute token budget by multiplying
|
|
453
|
+
* the model's max output tokens by a percentage from the budget percentages
|
|
454
|
+
* map. The result is clamped between `minReasoningBudget` (default 1024) and
|
|
455
|
+
* `maxReasoningBudget`. Pushes an unsupported warning if the level is not
|
|
456
|
+
* present in the budget percentages map.
|
|
457
|
+
*
|
|
458
|
+
* @returns The computed token budget, or `undefined` if the level is not
|
|
459
|
+
* supported.
|
|
460
|
+
*/
|
|
461
|
+
declare function mapReasoningToProviderBudget({ reasoning, maxOutputTokens, maxReasoningBudget, minReasoningBudget, budgetPercentages, warnings, }: {
|
|
462
|
+
reasoning: ReasoningLevel;
|
|
463
|
+
maxOutputTokens: number;
|
|
464
|
+
maxReasoningBudget: number;
|
|
465
|
+
minReasoningBudget?: number;
|
|
466
|
+
budgetPercentages?: Partial<Record<ReasoningLevel, number>>;
|
|
467
|
+
warnings: SharedV4Warning[];
|
|
468
|
+
}): number | undefined;
|
|
469
|
+
|
|
434
470
|
/**
|
|
435
471
|
* Loads an optional `string` setting from the environment or a parameter.
|
|
436
472
|
*
|
|
@@ -549,7 +585,16 @@ type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
|
|
|
549
585
|
* They are passed through to the provider from the AI SDK and enable
|
|
550
586
|
* provider-specific functionality that can be fully encapsulated in the provider.
|
|
551
587
|
*/
|
|
552
|
-
type ProviderOptions =
|
|
588
|
+
type ProviderOptions = SharedV4ProviderOptions;
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* A mapping of provider names to provider-specific file identifiers.
|
|
592
|
+
*
|
|
593
|
+
* Provider references allow files to be identified across different
|
|
594
|
+
* providers without re-uploading, by storing each provider's own
|
|
595
|
+
* identifier for the same logical file.
|
|
596
|
+
*/
|
|
597
|
+
type ProviderReference = SharedV4ProviderReference;
|
|
553
598
|
|
|
554
599
|
/**
|
|
555
600
|
* Text content part of a prompt. It contains a string of text.
|
|
@@ -577,8 +622,9 @@ interface ImagePart {
|
|
|
577
622
|
*
|
|
578
623
|
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
579
624
|
* - URL: a URL that points to the image
|
|
625
|
+
* - ProviderReference: a provider reference from `uploadFile`
|
|
580
626
|
*/
|
|
581
|
-
image: DataContent | URL;
|
|
627
|
+
image: DataContent | URL | ProviderReference;
|
|
582
628
|
/**
|
|
583
629
|
* Optional IANA media type of the image.
|
|
584
630
|
*
|
|
@@ -601,9 +647,10 @@ interface FilePart {
|
|
|
601
647
|
* File data. Can either be:
|
|
602
648
|
*
|
|
603
649
|
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
604
|
-
* - URL: a URL that points to the
|
|
650
|
+
* - URL: a URL that points to the file
|
|
651
|
+
* - ProviderReference: a provider reference from `uploadFile`
|
|
605
652
|
*/
|
|
606
|
-
data: DataContent | URL;
|
|
653
|
+
data: DataContent | URL | ProviderReference;
|
|
607
654
|
/**
|
|
608
655
|
* Optional filename of the file.
|
|
609
656
|
*/
|
|
@@ -637,6 +684,48 @@ interface ReasoningPart {
|
|
|
637
684
|
*/
|
|
638
685
|
providerOptions?: ProviderOptions;
|
|
639
686
|
}
|
|
687
|
+
/**
|
|
688
|
+
* Custom content part of a prompt. It contains no standardized payload beyond
|
|
689
|
+
* provider-specific options.
|
|
690
|
+
*/
|
|
691
|
+
interface CustomPart {
|
|
692
|
+
type: 'custom';
|
|
693
|
+
/**
|
|
694
|
+
* The kind of custom content, in the format `{provider}.{provider-type}`.
|
|
695
|
+
*/
|
|
696
|
+
kind: `${string}.${string}`;
|
|
697
|
+
/**
|
|
698
|
+
* Additional provider-specific metadata. They are passed through
|
|
699
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
700
|
+
* functionality that can be fully encapsulated in the provider.
|
|
701
|
+
*/
|
|
702
|
+
providerOptions?: ProviderOptions;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Reasoning file content part of a prompt. It contains a file generated as part of reasoning.
|
|
706
|
+
*/
|
|
707
|
+
interface ReasoningFilePart {
|
|
708
|
+
type: 'reasoning-file';
|
|
709
|
+
/**
|
|
710
|
+
* File data. Can either be:
|
|
711
|
+
*
|
|
712
|
+
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
713
|
+
* - URL: a URL that points to the file
|
|
714
|
+
*/
|
|
715
|
+
data: DataContent | URL;
|
|
716
|
+
/**
|
|
717
|
+
* IANA media type of the file.
|
|
718
|
+
*
|
|
719
|
+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
720
|
+
*/
|
|
721
|
+
mediaType: string;
|
|
722
|
+
/**
|
|
723
|
+
* Additional provider-specific metadata. They are passed through
|
|
724
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
725
|
+
* functionality that can be fully encapsulated in the provider.
|
|
726
|
+
*/
|
|
727
|
+
providerOptions?: ProviderOptions;
|
|
728
|
+
}
|
|
640
729
|
/**
|
|
641
730
|
* Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
|
|
642
731
|
*/
|
|
@@ -785,6 +874,9 @@ type ToolResultOutput = {
|
|
|
785
874
|
*/
|
|
786
875
|
providerOptions?: ProviderOptions;
|
|
787
876
|
} | {
|
|
877
|
+
/**
|
|
878
|
+
* @deprecated Use file-reference instead.
|
|
879
|
+
*/
|
|
788
880
|
type: 'file-id';
|
|
789
881
|
/**
|
|
790
882
|
* ID of the file.
|
|
@@ -799,6 +891,17 @@ type ToolResultOutput = {
|
|
|
799
891
|
* Provider-specific options.
|
|
800
892
|
*/
|
|
801
893
|
providerOptions?: ProviderOptions;
|
|
894
|
+
} | {
|
|
895
|
+
type: 'file-reference';
|
|
896
|
+
/**
|
|
897
|
+
* Provider-specific references for the file.
|
|
898
|
+
* The key is the provider name, e.g. 'openai' or 'anthropic'.
|
|
899
|
+
*/
|
|
900
|
+
providerReference: ProviderReference;
|
|
901
|
+
/**
|
|
902
|
+
* Provider-specific options.
|
|
903
|
+
*/
|
|
904
|
+
providerOptions?: ProviderOptions;
|
|
802
905
|
} | {
|
|
803
906
|
/**
|
|
804
907
|
* Images that are referenced using base64 encoded data.
|
|
@@ -832,7 +935,7 @@ type ToolResultOutput = {
|
|
|
832
935
|
providerOptions?: ProviderOptions;
|
|
833
936
|
} | {
|
|
834
937
|
/**
|
|
835
|
-
*
|
|
938
|
+
* @deprecated Use image-file-reference instead.
|
|
836
939
|
*/
|
|
837
940
|
type: 'image-file-id';
|
|
838
941
|
/**
|
|
@@ -848,6 +951,20 @@ type ToolResultOutput = {
|
|
|
848
951
|
* Provider-specific options.
|
|
849
952
|
*/
|
|
850
953
|
providerOptions?: ProviderOptions;
|
|
954
|
+
} | {
|
|
955
|
+
/**
|
|
956
|
+
* Images that are referenced using a provider reference.
|
|
957
|
+
*/
|
|
958
|
+
type: 'image-file-reference';
|
|
959
|
+
/**
|
|
960
|
+
* Provider-specific references for the image file.
|
|
961
|
+
* The key is the provider name, e.g. 'openai' or 'anthropic'.
|
|
962
|
+
*/
|
|
963
|
+
providerReference: ProviderReference;
|
|
964
|
+
/**
|
|
965
|
+
* Provider-specific options.
|
|
966
|
+
*/
|
|
967
|
+
providerOptions?: ProviderOptions;
|
|
851
968
|
} | {
|
|
852
969
|
/**
|
|
853
970
|
* Custom content part. This can be used to implement
|
|
@@ -893,7 +1010,7 @@ type AssistantModelMessage = {
|
|
|
893
1010
|
* Content of an assistant message.
|
|
894
1011
|
* It can be a string or an array of text, image, reasoning, redacted reasoning, and tool call parts.
|
|
895
1012
|
*/
|
|
896
|
-
type AssistantContent = string | Array<TextPart | FilePart | ReasoningPart | ToolCallPart | ToolResultPart | ToolApprovalRequest>;
|
|
1013
|
+
type AssistantContent = string | Array<TextPart | CustomPart | FilePart | ReasoningPart | ReasoningFilePart | ToolCallPart | ToolResultPart | ToolApprovalRequest>;
|
|
897
1014
|
|
|
898
1015
|
/**
|
|
899
1016
|
* A system message. It can contain system information.
|
|
@@ -980,9 +1097,14 @@ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
|
|
|
980
1097
|
type ModelMessage = SystemModelMessage | UserModelMessage | AssistantModelMessage | ToolModelMessage;
|
|
981
1098
|
|
|
982
1099
|
/**
|
|
983
|
-
*
|
|
1100
|
+
* A context object that is passed into tool execution.
|
|
984
1101
|
*/
|
|
985
|
-
|
|
1102
|
+
type Context = Record<string, unknown>;
|
|
1103
|
+
|
|
1104
|
+
/**
|
|
1105
|
+
* Additional options that are sent into each tool execution.
|
|
1106
|
+
*/
|
|
1107
|
+
interface ToolExecutionOptions<CONTEXT extends Context> {
|
|
986
1108
|
/**
|
|
987
1109
|
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
988
1110
|
*/
|
|
@@ -1008,12 +1130,12 @@ interface ToolExecutionOptions {
|
|
|
1008
1130
|
*
|
|
1009
1131
|
* Experimental (can break in patch releases).
|
|
1010
1132
|
*/
|
|
1011
|
-
experimental_context
|
|
1133
|
+
experimental_context: CONTEXT;
|
|
1012
1134
|
}
|
|
1013
1135
|
/**
|
|
1014
1136
|
* Function that is called to determine if the tool needs approval before it can be executed.
|
|
1015
1137
|
*/
|
|
1016
|
-
type ToolNeedsApprovalFunction<INPUT> = (input: INPUT, options: {
|
|
1138
|
+
type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context> = (input: INPUT, options: {
|
|
1017
1139
|
/**
|
|
1018
1140
|
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
|
1019
1141
|
*/
|
|
@@ -1028,11 +1150,17 @@ type ToolNeedsApprovalFunction<INPUT> = (input: INPUT, options: {
|
|
|
1028
1150
|
*
|
|
1029
1151
|
* Experimental (can break in patch releases).
|
|
1030
1152
|
*/
|
|
1031
|
-
experimental_context
|
|
1153
|
+
experimental_context: CONTEXT;
|
|
1032
1154
|
}) => boolean | PromiseLike<boolean>;
|
|
1033
|
-
|
|
1155
|
+
/**
|
|
1156
|
+
* Function that executes the tool and returns either a single result or a stream of results.
|
|
1157
|
+
*/
|
|
1158
|
+
type ToolExecuteFunction<INPUT, OUTPUT, CONTEXT extends Context> = (input: INPUT, options: ToolExecutionOptions<CONTEXT>) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
|
|
1034
1159
|
type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
|
|
1035
|
-
|
|
1160
|
+
/**
|
|
1161
|
+
* Helper type to determine the output properties of a tool.
|
|
1162
|
+
*/
|
|
1163
|
+
type ToolOutputProperties<INPUT, OUTPUT, CONTEXT extends Context> = NeverOptional<OUTPUT, {
|
|
1036
1164
|
/**
|
|
1037
1165
|
* An async function that is called with the arguments from the tool call and produces a result.
|
|
1038
1166
|
* If not provided, the tool will not be executed automatically.
|
|
@@ -1040,7 +1168,7 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<OUTPUT, {
|
|
|
1040
1168
|
* @args is the input of the tool call.
|
|
1041
1169
|
* @options.abortSignal is a signal that can be used to abort the tool call.
|
|
1042
1170
|
*/
|
|
1043
|
-
execute: ToolExecuteFunction<INPUT, OUTPUT>;
|
|
1171
|
+
execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1044
1172
|
outputSchema?: FlexibleSchema<OUTPUT>;
|
|
1045
1173
|
} | {
|
|
1046
1174
|
outputSchema: FlexibleSchema<OUTPUT>;
|
|
@@ -1052,7 +1180,7 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<OUTPUT, {
|
|
|
1052
1180
|
*
|
|
1053
1181
|
* The tool can also contain an optional execute function for the actual execution function of the tool.
|
|
1054
1182
|
*/
|
|
1055
|
-
type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any> = {
|
|
1183
|
+
type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context = Context> = {
|
|
1056
1184
|
/**
|
|
1057
1185
|
* An optional description of what the tool does.
|
|
1058
1186
|
* Will be used by the language model to decide whether to use the tool.
|
|
@@ -1084,10 +1212,18 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1084
1212
|
inputExamples?: Array<{
|
|
1085
1213
|
input: NoInfer<INPUT>;
|
|
1086
1214
|
}>;
|
|
1215
|
+
/**
|
|
1216
|
+
* An optional schema describing the context that the tool expects.
|
|
1217
|
+
*
|
|
1218
|
+
* The context is passed to execute function as part of the execution options.
|
|
1219
|
+
*/
|
|
1220
|
+
contextSchema?: FlexibleSchema<CONTEXT>;
|
|
1087
1221
|
/**
|
|
1088
1222
|
* Whether the tool needs approval before it can be executed.
|
|
1089
1223
|
*/
|
|
1090
|
-
needsApproval?: boolean | ToolNeedsApprovalFunction<[
|
|
1224
|
+
needsApproval?: boolean | ToolNeedsApprovalFunction<[
|
|
1225
|
+
INPUT
|
|
1226
|
+
] extends [never] ? unknown : INPUT, NoInfer<CONTEXT>>;
|
|
1091
1227
|
/**
|
|
1092
1228
|
* Strict mode setting for the tool.
|
|
1093
1229
|
*
|
|
@@ -1100,26 +1236,28 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1100
1236
|
* Optional function that is called when the argument streaming starts.
|
|
1101
1237
|
* Only called when the tool is used in a streaming context.
|
|
1102
1238
|
*/
|
|
1103
|
-
onInputStart?: (options: ToolExecutionOptions) => void | PromiseLike<void>;
|
|
1239
|
+
onInputStart?: (options: ToolExecutionOptions<NoInfer<CONTEXT>>) => void | PromiseLike<void>;
|
|
1104
1240
|
/**
|
|
1105
1241
|
* Optional function that is called when an argument streaming delta is available.
|
|
1106
1242
|
* Only called when the tool is used in a streaming context.
|
|
1107
1243
|
*/
|
|
1108
1244
|
onInputDelta?: (options: {
|
|
1109
1245
|
inputTextDelta: string;
|
|
1110
|
-
} & ToolExecutionOptions) => void | PromiseLike<void>;
|
|
1246
|
+
} & ToolExecutionOptions<NoInfer<CONTEXT>>) => void | PromiseLike<void>;
|
|
1111
1247
|
/**
|
|
1112
1248
|
* Optional function that is called when a tool call can be started,
|
|
1113
1249
|
* even if the execute function is not provided.
|
|
1114
1250
|
*/
|
|
1115
1251
|
onInputAvailable?: (options: {
|
|
1116
1252
|
input: [INPUT] extends [never] ? unknown : INPUT;
|
|
1117
|
-
} & ToolExecutionOptions) => void | PromiseLike<void>;
|
|
1118
|
-
} & ToolOutputProperties<INPUT, OUTPUT
|
|
1253
|
+
} & ToolExecutionOptions<NoInfer<CONTEXT>>) => void | PromiseLike<void>;
|
|
1254
|
+
} & ToolOutputProperties<INPUT, OUTPUT, NoInfer<CONTEXT>> & {
|
|
1119
1255
|
/**
|
|
1120
1256
|
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
1121
1257
|
*
|
|
1122
1258
|
* If not provided, the tool result will be sent as a JSON object.
|
|
1259
|
+
*
|
|
1260
|
+
* 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
1261
|
*/
|
|
1124
1262
|
toModelOutput?: (options: {
|
|
1125
1263
|
/**
|
|
@@ -1174,21 +1312,13 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1174
1312
|
*/
|
|
1175
1313
|
supportsDeferredResults?: boolean;
|
|
1176
1314
|
});
|
|
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
1315
|
/**
|
|
1186
1316
|
* Helper function for inferring the execute args of a tool.
|
|
1187
1317
|
*/
|
|
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>;
|
|
1318
|
+
declare function tool<INPUT, OUTPUT, CONTEXT extends Context>(tool: Tool<INPUT, OUTPUT, CONTEXT>): Tool<INPUT, OUTPUT, CONTEXT>;
|
|
1319
|
+
declare function tool<INPUT, CONTEXT extends Context>(tool: Tool<INPUT, never, CONTEXT>): Tool<INPUT, never, CONTEXT>;
|
|
1320
|
+
declare function tool<OUTPUT, CONTEXT extends Context>(tool: Tool<never, OUTPUT, CONTEXT>): Tool<never, OUTPUT, CONTEXT>;
|
|
1321
|
+
declare function tool<CONTEXT extends Context>(tool: Tool<never, never, CONTEXT>): Tool<never, never, CONTEXT>;
|
|
1192
1322
|
/**
|
|
1193
1323
|
* Defines a dynamic tool.
|
|
1194
1324
|
*/
|
|
@@ -1197,7 +1327,7 @@ declare function dynamicTool(tool: {
|
|
|
1197
1327
|
title?: string;
|
|
1198
1328
|
providerOptions?: ProviderOptions;
|
|
1199
1329
|
inputSchema: FlexibleSchema<unknown>;
|
|
1200
|
-
execute: ToolExecuteFunction<unknown, unknown>;
|
|
1330
|
+
execute: ToolExecuteFunction<unknown, unknown, Context>;
|
|
1201
1331
|
/**
|
|
1202
1332
|
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
1203
1333
|
*
|
|
@@ -1220,32 +1350,32 @@ declare function dynamicTool(tool: {
|
|
|
1220
1350
|
/**
|
|
1221
1351
|
* Whether the tool needs approval before it can be executed.
|
|
1222
1352
|
*/
|
|
1223
|
-
needsApproval?: boolean | ToolNeedsApprovalFunction<unknown>;
|
|
1224
|
-
}): Tool<unknown, unknown> & {
|
|
1353
|
+
needsApproval?: boolean | ToolNeedsApprovalFunction<unknown, Context>;
|
|
1354
|
+
}): Tool<unknown, unknown, Context> & {
|
|
1225
1355
|
type: 'dynamic';
|
|
1226
1356
|
};
|
|
1227
1357
|
|
|
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, }: {
|
|
1358
|
+
type ProviderToolFactory<INPUT, ARGS extends object, CONTEXT extends Context = {}> = <OUTPUT>(options: ARGS & {
|
|
1359
|
+
execute?: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1360
|
+
needsApproval?: Tool<INPUT, OUTPUT, CONTEXT>['needsApproval'];
|
|
1361
|
+
toModelOutput?: Tool<INPUT, OUTPUT, CONTEXT>['toModelOutput'];
|
|
1362
|
+
onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
|
|
1363
|
+
onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
|
|
1364
|
+
onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
|
|
1365
|
+
}) => Tool<INPUT, OUTPUT, CONTEXT>;
|
|
1366
|
+
declare function createProviderToolFactory<INPUT, ARGS extends object, CONTEXT extends Context = {}>({ id, inputSchema, }: {
|
|
1237
1367
|
id: `${string}.${string}`;
|
|
1238
1368
|
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, }: {
|
|
1369
|
+
}): ProviderToolFactory<INPUT, ARGS, CONTEXT>;
|
|
1370
|
+
type ProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object, CONTEXT extends Context = {}> = (options: ARGS & {
|
|
1371
|
+
execute?: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1372
|
+
needsApproval?: Tool<INPUT, OUTPUT, CONTEXT>['needsApproval'];
|
|
1373
|
+
toModelOutput?: Tool<INPUT, OUTPUT, CONTEXT>['toModelOutput'];
|
|
1374
|
+
onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
|
|
1375
|
+
onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
|
|
1376
|
+
onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
|
|
1377
|
+
}) => Tool<INPUT, OUTPUT, CONTEXT>;
|
|
1378
|
+
declare function createProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object, CONTEXT extends Context = {}>({ id, inputSchema, outputSchema, supportsDeferredResults, }: {
|
|
1249
1379
|
id: `${string}.${string}`;
|
|
1250
1380
|
inputSchema: FlexibleSchema<INPUT>;
|
|
1251
1381
|
outputSchema: FlexibleSchema<OUTPUT>;
|
|
@@ -1260,7 +1390,7 @@ declare function createProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS e
|
|
|
1260
1390
|
* @default false
|
|
1261
1391
|
*/
|
|
1262
1392
|
supportsDeferredResults?: boolean;
|
|
1263
|
-
}): ProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS>;
|
|
1393
|
+
}): ProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS, CONTEXT>;
|
|
1264
1394
|
|
|
1265
1395
|
/**
|
|
1266
1396
|
* Removes entries from a record where the value is null or undefined.
|
|
@@ -1269,6 +1399,16 @@ declare function createProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS e
|
|
|
1269
1399
|
*/
|
|
1270
1400
|
declare function removeUndefinedEntries<T>(record: Record<string, T | undefined>): Record<string, T>;
|
|
1271
1401
|
|
|
1402
|
+
/**
|
|
1403
|
+
* Resolves a provider reference to the provider-specific identifier for the
|
|
1404
|
+
* given provider. Throws `NoSuchProviderReferenceError` if the provider is not
|
|
1405
|
+
* found in the reference mapping.
|
|
1406
|
+
*/
|
|
1407
|
+
declare function resolveProviderReference({ reference, provider, }: {
|
|
1408
|
+
reference: SharedV4ProviderReference;
|
|
1409
|
+
provider: string;
|
|
1410
|
+
}): string;
|
|
1411
|
+
|
|
1272
1412
|
type Resolvable<T> = MaybePromiseLike<T> | (() => MaybePromiseLike<T>);
|
|
1273
1413
|
/**
|
|
1274
1414
|
* Resolves a value that could be a raw value, a Promise, a function returning a value,
|
|
@@ -1354,10 +1494,29 @@ declare function withUserAgentSuffix(headers: HeadersInit | Record<string, strin
|
|
|
1354
1494
|
|
|
1355
1495
|
declare function withoutTrailingSlash(url: string | undefined): string | undefined;
|
|
1356
1496
|
|
|
1357
|
-
|
|
1358
|
-
|
|
1497
|
+
/**
|
|
1498
|
+
* Executes a tool function, supporting both synchronous and streaming/asynchronous results.
|
|
1499
|
+
*
|
|
1500
|
+
* This generator yields intermediate ("preliminary") outputs as they're produced, allowing callers
|
|
1501
|
+
* to stream partial tool results before completion. When execution is finished, it yields a final output,
|
|
1502
|
+
* ensuring all consumers receive a conclusive result.
|
|
1503
|
+
*
|
|
1504
|
+
* - If the tool's `execute` function returns an `AsyncIterable`, all intermediate values are yielded
|
|
1505
|
+
* as `{ type: "preliminary", output }` except the last, which is yielded as `{ type: "final", output }`.
|
|
1506
|
+
* - If the tool returns a direct value or Promise, a single `{ type: "final", output }` is yielded.
|
|
1507
|
+
*
|
|
1508
|
+
* @template INPUT Input type for the tool execution.
|
|
1509
|
+
* @template OUTPUT Output type for the tool execution.
|
|
1510
|
+
* @template CONTEXT Context object extension for execution (extends Context).
|
|
1511
|
+
* @param params.execute The tool execute function.
|
|
1512
|
+
* @param params.input Input value to pass to the execute function.
|
|
1513
|
+
* @param params.options Additional options for tool execution.
|
|
1514
|
+
* @yields An object containing either a preliminary or final output from the tool.
|
|
1515
|
+
*/
|
|
1516
|
+
declare function executeTool<INPUT, OUTPUT, CONTEXT extends Context>({ execute, input, options, }: {
|
|
1517
|
+
execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
|
|
1359
1518
|
input: INPUT;
|
|
1360
|
-
options: ToolExecutionOptions
|
|
1519
|
+
options: ToolExecutionOptions<NoInfer<CONTEXT>>;
|
|
1361
1520
|
}): AsyncGenerator<{
|
|
1362
1521
|
type: 'preliminary';
|
|
1363
1522
|
output: OUTPUT;
|
|
@@ -1366,6 +1525,52 @@ declare function executeTool<INPUT, OUTPUT>({ execute, input, options, }: {
|
|
|
1366
1525
|
output: OUTPUT;
|
|
1367
1526
|
}>;
|
|
1368
1527
|
|
|
1528
|
+
/**
|
|
1529
|
+
* Infer the context type of a tool.
|
|
1530
|
+
*/
|
|
1531
|
+
type InferToolContext<TOOL extends Tool<any, any, any>> = TOOL extends Tool<any, any, infer CONTEXT> ? CONTEXT : never;
|
|
1532
|
+
|
|
1533
|
+
/**
|
|
1534
|
+
* Infer the input type of a tool.
|
|
1535
|
+
*/
|
|
1536
|
+
type InferToolInput<TOOL extends Tool<any, any, any>> = TOOL extends Tool<infer INPUT, any, any> ? INPUT : never;
|
|
1537
|
+
|
|
1538
|
+
/**
|
|
1539
|
+
* Infer the output type of a tool.
|
|
1540
|
+
*/
|
|
1541
|
+
type InferToolOutput<TOOL extends Tool<any, any, any>> = TOOL extends Tool<any, infer OUTPUT, any> ? OUTPUT : never;
|
|
1542
|
+
|
|
1543
|
+
/**
|
|
1544
|
+
* A mapping of tool names to tool definitions.
|
|
1545
|
+
*/
|
|
1546
|
+
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'>>;
|
|
1547
|
+
|
|
1548
|
+
/**
|
|
1549
|
+
* Converts a union type `U` into an intersection type.
|
|
1550
|
+
*
|
|
1551
|
+
* For example:
|
|
1552
|
+
* type A = { a: number };
|
|
1553
|
+
* type B = { b: string };
|
|
1554
|
+
* type Union = A | B;
|
|
1555
|
+
* type Intersection = UnionToIntersection<Union>;
|
|
1556
|
+
* // Intersection is: { a: number } & { b: string }
|
|
1557
|
+
*
|
|
1558
|
+
* This is useful when you have a union of object types and need a type with all possible properties.
|
|
1559
|
+
*/
|
|
1560
|
+
type UnionToIntersection<U> = (U extends unknown ? (arg: U) => void : never) extends (arg: infer I) => void ? I : never;
|
|
1561
|
+
|
|
1562
|
+
/**
|
|
1563
|
+
* Infer the context type for a tool set.
|
|
1564
|
+
*
|
|
1565
|
+
* The inferred type contains all properties required by the contexts of the
|
|
1566
|
+
* tools in the set.
|
|
1567
|
+
*
|
|
1568
|
+
* If there are incompatible properties, they will be of type `never`.
|
|
1569
|
+
*/
|
|
1570
|
+
type InferToolSetContext<TOOLS extends ToolSet> = UnionToIntersection<{
|
|
1571
|
+
[K in keyof TOOLS]: InferToolContext<NoInfer<TOOLS[K]>>;
|
|
1572
|
+
}[keyof TOOLS]>;
|
|
1573
|
+
|
|
1369
1574
|
/**
|
|
1370
1575
|
* Typed tool call that is returned by generateText and streamText.
|
|
1371
1576
|
* It contains the tool call ID, the tool name, and the tool arguments.
|
|
@@ -1425,9 +1630,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
|
|
|
1425
1630
|
dynamic?: boolean;
|
|
1426
1631
|
}
|
|
1427
1632
|
|
|
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 };
|
|
1633
|
+
export { type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, 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, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|