@ai-sdk/provider-utils 4.0.9 → 4.0.11
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 +14 -0
- package/dist/index.d.mts +170 -166
- package/dist/index.d.ts +170 -166
- package/dist/index.js +37 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +37 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/extract-response-headers.ts +5 -5
- package/src/generate-id.ts +11 -11
- package/src/handle-fetch-error.ts +33 -0
- package/src/types/assistant-model-message.ts +6 -6
- package/src/types/content-part.ts +70 -70
- package/src/types/data-content.ts +1 -1
- package/src/types/model-message.ts +2 -2
- package/src/types/provider-options.ts +4 -4
- package/src/types/system-model-message.ts +9 -9
- package/src/types/tool-call.ts +7 -7
- package/src/types/tool-model-message.ts +5 -5
- package/src/types/tool-result.ts +8 -8
- package/src/types/tool.ts +28 -28
- package/src/types/user-model-message.ts +7 -7
- package/src/validate-types.ts +11 -5
package/src/types/tool.ts
CHANGED
|
@@ -82,12 +82,12 @@ type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<
|
|
|
82
82
|
OUTPUT,
|
|
83
83
|
| {
|
|
84
84
|
/**
|
|
85
|
-
An async function that is called with the arguments from the tool call and produces a result.
|
|
86
|
-
If not provided, the tool will not be executed automatically.
|
|
87
|
-
|
|
88
|
-
@args is the input of the tool call.
|
|
89
|
-
@options.abortSignal is a signal that can be used to abort the tool call.
|
|
90
|
-
|
|
85
|
+
* An async function that is called with the arguments from the tool call and produces a result.
|
|
86
|
+
* If not provided, the tool will not be executed automatically.
|
|
87
|
+
*
|
|
88
|
+
* @args is the input of the tool call.
|
|
89
|
+
* @options.abortSignal is a signal that can be used to abort the tool call.
|
|
90
|
+
*/
|
|
91
91
|
execute: ToolExecuteFunction<INPUT, OUTPUT>;
|
|
92
92
|
|
|
93
93
|
outputSchema?: FlexibleSchema<OUTPUT>;
|
|
@@ -100,19 +100,19 @@ If not provided, the tool will not be executed automatically.
|
|
|
100
100
|
>;
|
|
101
101
|
|
|
102
102
|
/**
|
|
103
|
-
A tool contains the description and the schema of the input that the tool expects.
|
|
104
|
-
This enables the language model to generate the input.
|
|
105
|
-
|
|
106
|
-
The tool can also contain an optional execute function for the actual execution function of the tool.
|
|
103
|
+
* A tool contains the description and the schema of the input that the tool expects.
|
|
104
|
+
* This enables the language model to generate the input.
|
|
105
|
+
*
|
|
106
|
+
* The tool can also contain an optional execute function for the actual execution function of the tool.
|
|
107
107
|
*/
|
|
108
108
|
export type Tool<
|
|
109
109
|
INPUT extends JSONValue | unknown | never = any,
|
|
110
110
|
OUTPUT extends JSONValue | unknown | never = any,
|
|
111
111
|
> = {
|
|
112
112
|
/**
|
|
113
|
-
An optional description of what the tool does.
|
|
114
|
-
Will be used by the language model to decide whether to use the tool.
|
|
115
|
-
Not used for provider-defined tools.
|
|
113
|
+
* An optional description of what the tool does.
|
|
114
|
+
* Will be used by the language model to decide whether to use the tool.
|
|
115
|
+
* Not used for provider-defined tools.
|
|
116
116
|
*/
|
|
117
117
|
description?: string;
|
|
118
118
|
|
|
@@ -122,9 +122,9 @@ Not used for provider-defined tools.
|
|
|
122
122
|
title?: string;
|
|
123
123
|
|
|
124
124
|
/**
|
|
125
|
-
Additional provider-specific metadata. They are passed through
|
|
126
|
-
to the provider from the AI SDK and enable provider-specific
|
|
127
|
-
functionality that can be fully encapsulated in the provider.
|
|
125
|
+
* Additional provider-specific metadata. They are passed through
|
|
126
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
127
|
+
* functionality that can be fully encapsulated in the provider.
|
|
128
128
|
*/
|
|
129
129
|
providerOptions?: ProviderOptions;
|
|
130
130
|
|
|
@@ -211,31 +211,31 @@ functionality that can be fully encapsulated in the provider.
|
|
|
211
211
|
} & (
|
|
212
212
|
| {
|
|
213
213
|
/**
|
|
214
|
-
Tool with user-defined input and output schemas.
|
|
215
|
-
|
|
214
|
+
* Tool with user-defined input and output schemas.
|
|
215
|
+
*/
|
|
216
216
|
type?: undefined | 'function';
|
|
217
217
|
}
|
|
218
218
|
| {
|
|
219
219
|
/**
|
|
220
|
-
Tool that is defined at runtime (e.g. an MCP tool).
|
|
221
|
-
The types of input and output are not known at development time.
|
|
222
|
-
|
|
220
|
+
* Tool that is defined at runtime (e.g. an MCP tool).
|
|
221
|
+
* The types of input and output are not known at development time.
|
|
222
|
+
*/
|
|
223
223
|
type: 'dynamic';
|
|
224
224
|
}
|
|
225
225
|
| {
|
|
226
226
|
/**
|
|
227
|
-
Tool with provider-defined input and output schemas.
|
|
228
|
-
|
|
227
|
+
* Tool with provider-defined input and output schemas.
|
|
228
|
+
*/
|
|
229
229
|
type: 'provider';
|
|
230
230
|
|
|
231
231
|
/**
|
|
232
|
-
The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
|
|
233
|
-
|
|
232
|
+
* The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
|
|
233
|
+
*/
|
|
234
234
|
id: `${string}.${string}`;
|
|
235
235
|
|
|
236
236
|
/**
|
|
237
|
-
The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
|
|
238
|
-
|
|
237
|
+
* The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
|
|
238
|
+
*/
|
|
239
239
|
args: Record<string, unknown>;
|
|
240
240
|
|
|
241
241
|
/**
|
|
@@ -268,7 +268,7 @@ export type InferToolOutput<TOOL extends Tool> =
|
|
|
268
268
|
TOOL extends Tool<any, infer OUTPUT> ? OUTPUT : never;
|
|
269
269
|
|
|
270
270
|
/**
|
|
271
|
-
Helper function for inferring the execute args of a tool.
|
|
271
|
+
* Helper function for inferring the execute args of a tool.
|
|
272
272
|
*/
|
|
273
273
|
// Note: overload order is important for auto-completion
|
|
274
274
|
export function tool<INPUT, OUTPUT>(
|
|
@@ -2,21 +2,21 @@ import { FilePart, ImagePart, TextPart } from './content-part';
|
|
|
2
2
|
import { ProviderOptions } from './provider-options';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
A user message. It can contain text or a combination of text and images.
|
|
5
|
+
* A user message. It can contain text or a combination of text and images.
|
|
6
6
|
*/
|
|
7
7
|
export type UserModelMessage = {
|
|
8
8
|
role: 'user';
|
|
9
9
|
content: UserContent;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
* Additional provider-specific metadata. They are passed through
|
|
13
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
14
|
+
* functionality that can be fully encapsulated in the provider.
|
|
15
|
+
*/
|
|
16
16
|
providerOptions?: ProviderOptions;
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
* Content of a user message. It can be a string or an array of text and image parts.
|
|
21
|
+
*/
|
|
22
22
|
export type UserContent = string | Array<TextPart | ImagePart | FilePart>;
|
package/src/validate-types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TypeValidationError } from '@ai-sdk/provider';
|
|
1
|
+
import { TypeValidationContext, TypeValidationError } from '@ai-sdk/provider';
|
|
2
2
|
import { FlexibleSchema, asSchema } from './schema';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -8,19 +8,22 @@ import { FlexibleSchema, asSchema } from './schema';
|
|
|
8
8
|
* @template T - The type of the object to validate.
|
|
9
9
|
* @param {string} options.value - The object to validate.
|
|
10
10
|
* @param {Validator<T>} options.schema - The schema to use for validating the JSON.
|
|
11
|
+
* @param {TypeValidationContext} options.context - Optional context about what is being validated.
|
|
11
12
|
* @returns {Promise<T>} - The typed object.
|
|
12
13
|
*/
|
|
13
14
|
export async function validateTypes<OBJECT>({
|
|
14
15
|
value,
|
|
15
16
|
schema,
|
|
17
|
+
context,
|
|
16
18
|
}: {
|
|
17
19
|
value: unknown;
|
|
18
20
|
schema: FlexibleSchema<OBJECT>;
|
|
21
|
+
context?: TypeValidationContext;
|
|
19
22
|
}): Promise<OBJECT> {
|
|
20
|
-
const result = await safeValidateTypes({ value, schema });
|
|
23
|
+
const result = await safeValidateTypes({ value, schema, context });
|
|
21
24
|
|
|
22
25
|
if (!result.success) {
|
|
23
|
-
throw TypeValidationError.wrap({ value, cause: result.error });
|
|
26
|
+
throw TypeValidationError.wrap({ value, cause: result.error, context });
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
return result.value;
|
|
@@ -33,14 +36,17 @@ export async function validateTypes<OBJECT>({
|
|
|
33
36
|
* @template T - The type of the object to validate.
|
|
34
37
|
* @param {string} options.value - The JSON object to validate.
|
|
35
38
|
* @param {Validator<T>} options.schema - The schema to use for validating the JSON.
|
|
39
|
+
* @param {TypeValidationContext} options.context - Optional context about what is being validated.
|
|
36
40
|
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
|
37
41
|
*/
|
|
38
42
|
export async function safeValidateTypes<OBJECT>({
|
|
39
43
|
value,
|
|
40
44
|
schema,
|
|
45
|
+
context,
|
|
41
46
|
}: {
|
|
42
47
|
value: unknown;
|
|
43
48
|
schema: FlexibleSchema<OBJECT>;
|
|
49
|
+
context?: TypeValidationContext;
|
|
44
50
|
}): Promise<
|
|
45
51
|
| {
|
|
46
52
|
success: true;
|
|
@@ -68,13 +74,13 @@ export async function safeValidateTypes<OBJECT>({
|
|
|
68
74
|
|
|
69
75
|
return {
|
|
70
76
|
success: false,
|
|
71
|
-
error: TypeValidationError.wrap({ value, cause: result.error }),
|
|
77
|
+
error: TypeValidationError.wrap({ value, cause: result.error, context }),
|
|
72
78
|
rawValue: value,
|
|
73
79
|
};
|
|
74
80
|
} catch (error) {
|
|
75
81
|
return {
|
|
76
82
|
success: false,
|
|
77
|
-
error: TypeValidationError.wrap({ value, cause: error }),
|
|
83
|
+
error: TypeValidationError.wrap({ value, cause: error, context }),
|
|
78
84
|
rawValue: value,
|
|
79
85
|
};
|
|
80
86
|
}
|