@ai-sdk/provider-utils 4.0.5 → 4.0.6
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 +8 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +4 -2
- package/src/__snapshots__/schema.test.ts.snap +346 -0
- package/src/add-additional-properties-to-json-schema.test.ts +289 -0
- package/src/add-additional-properties-to-json-schema.ts +53 -0
- package/src/combine-headers.ts +11 -0
- package/src/convert-async-iterator-to-readable-stream.test.ts +78 -0
- package/src/convert-async-iterator-to-readable-stream.ts +47 -0
- package/src/convert-image-model-file-to-data-uri.test.ts +85 -0
- package/src/convert-image-model-file-to-data-uri.ts +19 -0
- package/src/convert-to-form-data.test.ts +167 -0
- package/src/convert-to-form-data.ts +61 -0
- package/src/create-tool-name-mapping.test.ts +163 -0
- package/src/create-tool-name-mapping.ts +66 -0
- package/src/delay.test.ts +212 -0
- package/src/delay.ts +47 -0
- package/src/delayed-promise.test.ts +132 -0
- package/src/delayed-promise.ts +61 -0
- package/src/download-blob.test.ts +145 -0
- package/src/download-blob.ts +31 -0
- package/src/download-error.ts +39 -0
- package/src/extract-response-headers.ts +9 -0
- package/src/fetch-function.ts +4 -0
- package/src/generate-id.test.ts +31 -0
- package/src/generate-id.ts +57 -0
- package/src/get-error-message.ts +15 -0
- package/src/get-from-api.test.ts +199 -0
- package/src/get-from-api.ts +97 -0
- package/src/get-runtime-environment-user-agent.test.ts +47 -0
- package/src/get-runtime-environment-user-agent.ts +24 -0
- package/src/handle-fetch-error.ts +39 -0
- package/src/index.ts +67 -0
- package/src/inject-json-instruction.test.ts +404 -0
- package/src/inject-json-instruction.ts +63 -0
- package/src/is-abort-error.ts +8 -0
- package/src/is-async-iterable.ts +3 -0
- package/src/is-non-nullable.ts +12 -0
- package/src/is-url-supported.test.ts +282 -0
- package/src/is-url-supported.ts +40 -0
- package/src/load-api-key.ts +45 -0
- package/src/load-optional-setting.ts +30 -0
- package/src/load-setting.ts +62 -0
- package/src/maybe-promise-like.ts +3 -0
- package/src/media-type-to-extension.test.ts +26 -0
- package/src/media-type-to-extension.ts +22 -0
- package/src/normalize-headers.test.ts +64 -0
- package/src/normalize-headers.ts +38 -0
- package/src/parse-json-event-stream.ts +33 -0
- package/src/parse-json.test.ts +191 -0
- package/src/parse-json.ts +122 -0
- package/src/parse-provider-options.ts +32 -0
- package/src/post-to-api.ts +166 -0
- package/src/provider-tool-factory.ts +125 -0
- package/src/remove-undefined-entries.test.ts +57 -0
- package/src/remove-undefined-entries.ts +12 -0
- package/src/resolve.test.ts +125 -0
- package/src/resolve.ts +17 -0
- package/src/response-handler.test.ts +89 -0
- package/src/response-handler.ts +187 -0
- package/src/schema.test-d.ts +11 -0
- package/src/schema.test.ts +502 -0
- package/src/schema.ts +267 -0
- package/src/secure-json-parse.test.ts +59 -0
- package/src/secure-json-parse.ts +92 -0
- package/src/test/convert-array-to-async-iterable.ts +9 -0
- package/src/test/convert-array-to-readable-stream.ts +15 -0
- package/src/test/convert-async-iterable-to-array.ts +9 -0
- package/src/test/convert-readable-stream-to-array.ts +14 -0
- package/src/test/convert-response-stream-to-array.ts +9 -0
- package/src/test/index.ts +7 -0
- package/src/test/is-node-version.ts +4 -0
- package/src/test/mock-id.ts +8 -0
- package/src/to-json-schema/zod3-to-json-schema/LICENSE +16 -0
- package/src/to-json-schema/zod3-to-json-schema/README.md +24 -0
- package/src/to-json-schema/zod3-to-json-schema/get-relative-path.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/index.ts +1 -0
- package/src/to-json-schema/zod3-to-json-schema/options.ts +98 -0
- package/src/to-json-schema/zod3-to-json-schema/parse-def.test.ts +224 -0
- package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +109 -0
- package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +57 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/any.ts +5 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/array.test.ts +98 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +38 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.test.ts +51 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +44 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/boolean.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/branded.test.ts +16 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/catch.test.ts +15 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/date.test.ts +97 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +64 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/default.test.ts +54 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +14 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/effects.test.ts +41 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +14 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +13 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.test.ts +92 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +52 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +29 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/map.test.ts +48 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +47 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.test.ts +102 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +31 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +9 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/null.ts +9 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.test.ts +67 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +42 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/number.test.ts +65 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +44 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/object.test.ts +149 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +88 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/optional.test.ts +147 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +23 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/pipe.test.ts +35 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +29 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/promise.test.ts +15 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +11 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.test.ts +20 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.test.ts +108 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +71 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/set.test.ts +20 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +35 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/string.test.ts +438 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +426 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.test.ts +33 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +61 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +11 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/union.test.ts +226 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +144 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/refs.test.ts +919 -0
- package/src/to-json-schema/zod3-to-json-schema/refs.ts +39 -0
- package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +115 -0
- package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.test.ts +862 -0
- package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +93 -0
- package/src/types/assistant-model-message.ts +39 -0
- package/src/types/content-part.ts +379 -0
- package/src/types/data-content.ts +4 -0
- package/src/types/execute-tool.ts +27 -0
- package/src/types/index.ts +40 -0
- package/src/types/model-message.ts +14 -0
- package/src/types/provider-options.ts +9 -0
- package/src/types/system-model-message.ts +20 -0
- package/src/types/tool-approval-request.ts +16 -0
- package/src/types/tool-approval-response.ts +27 -0
- package/src/types/tool-call.ts +31 -0
- package/src/types/tool-model-message.ts +23 -0
- package/src/types/tool-result.ts +35 -0
- package/src/types/tool.test-d.ts +193 -0
- package/src/types/tool.ts +324 -0
- package/src/types/user-model-message.ts +22 -0
- package/src/uint8-utils.ts +26 -0
- package/src/validate-types.test.ts +105 -0
- package/src/validate-types.ts +81 -0
- package/src/version.ts +6 -0
- package/src/with-user-agent-suffix.test.ts +84 -0
- package/src/with-user-agent-suffix.ts +27 -0
- package/src/without-trailing-slash.ts +3 -0
- package/LICENSE +0 -13
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ZodSchema } from 'zod/v3';
|
|
2
|
+
import { Options } from './options';
|
|
3
|
+
import { parseDef } from './parse-def';
|
|
4
|
+
import { JsonSchema7Type } from './parse-types';
|
|
5
|
+
import { getRefs } from './refs';
|
|
6
|
+
import { parseAnyDef } from './parsers/any';
|
|
7
|
+
|
|
8
|
+
const zod3ToJsonSchema = (
|
|
9
|
+
schema: ZodSchema<any>,
|
|
10
|
+
options?: Partial<Options> | string,
|
|
11
|
+
): JsonSchema7Type & {
|
|
12
|
+
$schema?: string;
|
|
13
|
+
definitions?: {
|
|
14
|
+
[key: string]: JsonSchema7Type;
|
|
15
|
+
};
|
|
16
|
+
} => {
|
|
17
|
+
const refs = getRefs(options);
|
|
18
|
+
|
|
19
|
+
let definitions =
|
|
20
|
+
typeof options === 'object' && options.definitions
|
|
21
|
+
? Object.entries(options.definitions).reduce(
|
|
22
|
+
(acc: { [key: string]: JsonSchema7Type }, [name, schema]) => ({
|
|
23
|
+
...acc,
|
|
24
|
+
[name]:
|
|
25
|
+
parseDef(
|
|
26
|
+
schema._def,
|
|
27
|
+
{
|
|
28
|
+
...refs,
|
|
29
|
+
currentPath: [...refs.basePath, refs.definitionPath, name],
|
|
30
|
+
},
|
|
31
|
+
true,
|
|
32
|
+
) ?? parseAnyDef(),
|
|
33
|
+
}),
|
|
34
|
+
{},
|
|
35
|
+
)
|
|
36
|
+
: undefined;
|
|
37
|
+
|
|
38
|
+
const name =
|
|
39
|
+
typeof options === 'string'
|
|
40
|
+
? options
|
|
41
|
+
: options?.nameStrategy === 'title'
|
|
42
|
+
? undefined
|
|
43
|
+
: options?.name;
|
|
44
|
+
|
|
45
|
+
const main =
|
|
46
|
+
parseDef(
|
|
47
|
+
schema._def,
|
|
48
|
+
name === undefined
|
|
49
|
+
? refs
|
|
50
|
+
: {
|
|
51
|
+
...refs,
|
|
52
|
+
currentPath: [...refs.basePath, refs.definitionPath, name],
|
|
53
|
+
},
|
|
54
|
+
false,
|
|
55
|
+
) ?? (parseAnyDef() as JsonSchema7Type);
|
|
56
|
+
|
|
57
|
+
const title =
|
|
58
|
+
typeof options === 'object' &&
|
|
59
|
+
options.name !== undefined &&
|
|
60
|
+
options.nameStrategy === 'title'
|
|
61
|
+
? options.name
|
|
62
|
+
: undefined;
|
|
63
|
+
|
|
64
|
+
if (title !== undefined) {
|
|
65
|
+
main.title = title;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const combined: ReturnType<typeof zod3ToJsonSchema> =
|
|
69
|
+
name === undefined
|
|
70
|
+
? definitions
|
|
71
|
+
? {
|
|
72
|
+
...main,
|
|
73
|
+
[refs.definitionPath]: definitions,
|
|
74
|
+
}
|
|
75
|
+
: main
|
|
76
|
+
: {
|
|
77
|
+
$ref: [
|
|
78
|
+
...(refs.$refStrategy === 'relative' ? [] : refs.basePath),
|
|
79
|
+
refs.definitionPath,
|
|
80
|
+
name,
|
|
81
|
+
].join('/'),
|
|
82
|
+
[refs.definitionPath]: {
|
|
83
|
+
...definitions,
|
|
84
|
+
[name]: main,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
combined.$schema = 'http://json-schema.org/draft-07/schema#';
|
|
89
|
+
|
|
90
|
+
return combined;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export { zod3ToJsonSchema };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FilePart,
|
|
3
|
+
ReasoningPart,
|
|
4
|
+
TextPart,
|
|
5
|
+
ToolCallPart,
|
|
6
|
+
ToolResultPart,
|
|
7
|
+
} from './content-part';
|
|
8
|
+
import { ProviderOptions } from './provider-options';
|
|
9
|
+
import { ToolApprovalRequest } from './tool-approval-request';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
An assistant message. It can contain text, tool calls, or a combination of text and tool calls.
|
|
13
|
+
*/
|
|
14
|
+
export type AssistantModelMessage = {
|
|
15
|
+
role: 'assistant';
|
|
16
|
+
content: AssistantContent;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
Additional provider-specific metadata. They are passed through
|
|
20
|
+
to the provider from the AI SDK and enable provider-specific
|
|
21
|
+
functionality that can be fully encapsulated in the provider.
|
|
22
|
+
*/
|
|
23
|
+
providerOptions?: ProviderOptions;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
Content of an assistant message.
|
|
28
|
+
It can be a string or an array of text, image, reasoning, redacted reasoning, and tool call parts.
|
|
29
|
+
*/
|
|
30
|
+
export type AssistantContent =
|
|
31
|
+
| string
|
|
32
|
+
| Array<
|
|
33
|
+
| TextPart
|
|
34
|
+
| FilePart
|
|
35
|
+
| ReasoningPart
|
|
36
|
+
| ToolCallPart
|
|
37
|
+
| ToolResultPart
|
|
38
|
+
| ToolApprovalRequest
|
|
39
|
+
>;
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { JSONValue } from '@ai-sdk/provider';
|
|
2
|
+
import { DataContent } from './data-content';
|
|
3
|
+
import { ProviderOptions } from './provider-options';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Text content part of a prompt. It contains a string of text.
|
|
7
|
+
*/
|
|
8
|
+
export interface TextPart {
|
|
9
|
+
type: 'text';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
The text content.
|
|
13
|
+
*/
|
|
14
|
+
text: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
Additional provider-specific metadata. They are passed through
|
|
18
|
+
to the provider from the AI SDK and enable provider-specific
|
|
19
|
+
functionality that can be fully encapsulated in the provider.
|
|
20
|
+
*/
|
|
21
|
+
providerOptions?: ProviderOptions;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
Image content part of a prompt. It contains an image.
|
|
26
|
+
*/
|
|
27
|
+
export interface ImagePart {
|
|
28
|
+
type: 'image';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
Image data. Can either be:
|
|
32
|
+
|
|
33
|
+
- data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
34
|
+
- URL: a URL that points to the image
|
|
35
|
+
*/
|
|
36
|
+
image: DataContent | URL;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
Optional IANA media type of the image.
|
|
40
|
+
|
|
41
|
+
@see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
42
|
+
*/
|
|
43
|
+
mediaType?: string;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
Additional provider-specific metadata. They are passed through
|
|
47
|
+
to the provider from the AI SDK and enable provider-specific
|
|
48
|
+
functionality that can be fully encapsulated in the provider.
|
|
49
|
+
*/
|
|
50
|
+
providerOptions?: ProviderOptions;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
File content part of a prompt. It contains a file.
|
|
55
|
+
*/
|
|
56
|
+
export interface FilePart {
|
|
57
|
+
type: 'file';
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
File data. Can either be:
|
|
61
|
+
|
|
62
|
+
- data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
63
|
+
- URL: a URL that points to the image
|
|
64
|
+
*/
|
|
65
|
+
data: DataContent | URL;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
Optional filename of the file.
|
|
69
|
+
*/
|
|
70
|
+
filename?: string;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
IANA media type of the file.
|
|
74
|
+
|
|
75
|
+
@see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
76
|
+
*/
|
|
77
|
+
mediaType: string;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
Additional provider-specific metadata. They are passed through
|
|
81
|
+
to the provider from the AI SDK and enable provider-specific
|
|
82
|
+
functionality that can be fully encapsulated in the provider.
|
|
83
|
+
*/
|
|
84
|
+
providerOptions?: ProviderOptions;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Reasoning content part of a prompt. It contains a reasoning.
|
|
89
|
+
*/
|
|
90
|
+
export interface ReasoningPart {
|
|
91
|
+
type: 'reasoning';
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
The reasoning text.
|
|
95
|
+
*/
|
|
96
|
+
text: string;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
Additional provider-specific metadata. They are passed through
|
|
100
|
+
to the provider from the AI SDK and enable provider-specific
|
|
101
|
+
functionality that can be fully encapsulated in the provider.
|
|
102
|
+
*/
|
|
103
|
+
providerOptions?: ProviderOptions;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
|
|
108
|
+
*/
|
|
109
|
+
export interface ToolCallPart {
|
|
110
|
+
type: 'tool-call';
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
ID of the tool call. This ID is used to match the tool call with the tool result.
|
|
114
|
+
*/
|
|
115
|
+
toolCallId: string;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
Name of the tool that is being called.
|
|
119
|
+
*/
|
|
120
|
+
toolName: string;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
|
124
|
+
*/
|
|
125
|
+
input: unknown;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
Additional provider-specific metadata. They are passed through
|
|
129
|
+
to the provider from the AI SDK and enable provider-specific
|
|
130
|
+
functionality that can be fully encapsulated in the provider.
|
|
131
|
+
*/
|
|
132
|
+
providerOptions?: ProviderOptions;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
Whether the tool call was executed by the provider.
|
|
136
|
+
*/
|
|
137
|
+
providerExecuted?: boolean;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
|
|
142
|
+
*/
|
|
143
|
+
export interface ToolResultPart {
|
|
144
|
+
type: 'tool-result';
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
ID of the tool call that this result is associated with.
|
|
148
|
+
*/
|
|
149
|
+
toolCallId: string;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
Name of the tool that generated this result.
|
|
153
|
+
*/
|
|
154
|
+
toolName: string;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
Result of the tool call. This is a JSON-serializable object.
|
|
158
|
+
*/
|
|
159
|
+
output: ToolResultOutput;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
Additional provider-specific metadata. They are passed through
|
|
163
|
+
to the provider from the AI SDK and enable provider-specific
|
|
164
|
+
functionality that can be fully encapsulated in the provider.
|
|
165
|
+
*/
|
|
166
|
+
providerOptions?: ProviderOptions;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Output of a tool result.
|
|
171
|
+
*/
|
|
172
|
+
export type ToolResultOutput =
|
|
173
|
+
| {
|
|
174
|
+
/**
|
|
175
|
+
* Text tool output that should be directly sent to the API.
|
|
176
|
+
*/
|
|
177
|
+
type: 'text';
|
|
178
|
+
value: string;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Provider-specific options.
|
|
182
|
+
*/
|
|
183
|
+
providerOptions?: ProviderOptions;
|
|
184
|
+
}
|
|
185
|
+
| {
|
|
186
|
+
type: 'json';
|
|
187
|
+
value: JSONValue;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Provider-specific options.
|
|
191
|
+
*/
|
|
192
|
+
providerOptions?: ProviderOptions;
|
|
193
|
+
}
|
|
194
|
+
| {
|
|
195
|
+
/**
|
|
196
|
+
* Type when the user has denied the execution of the tool call.
|
|
197
|
+
*/
|
|
198
|
+
type: 'execution-denied';
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Optional reason for the execution denial.
|
|
202
|
+
*/
|
|
203
|
+
reason?: string;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Provider-specific options.
|
|
207
|
+
*/
|
|
208
|
+
providerOptions?: ProviderOptions;
|
|
209
|
+
}
|
|
210
|
+
| {
|
|
211
|
+
type: 'error-text';
|
|
212
|
+
value: string;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Provider-specific options.
|
|
216
|
+
*/
|
|
217
|
+
providerOptions?: ProviderOptions;
|
|
218
|
+
}
|
|
219
|
+
| {
|
|
220
|
+
type: 'error-json';
|
|
221
|
+
value: JSONValue;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Provider-specific options.
|
|
225
|
+
*/
|
|
226
|
+
providerOptions?: ProviderOptions;
|
|
227
|
+
}
|
|
228
|
+
| {
|
|
229
|
+
type: 'content';
|
|
230
|
+
value: Array<
|
|
231
|
+
| {
|
|
232
|
+
type: 'text';
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
Text content.
|
|
236
|
+
*/
|
|
237
|
+
text: string;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Provider-specific options.
|
|
241
|
+
*/
|
|
242
|
+
providerOptions?: ProviderOptions;
|
|
243
|
+
}
|
|
244
|
+
| {
|
|
245
|
+
/**
|
|
246
|
+
* @deprecated Use image-data or file-data instead.
|
|
247
|
+
*/
|
|
248
|
+
type: 'media';
|
|
249
|
+
data: string;
|
|
250
|
+
mediaType: string;
|
|
251
|
+
}
|
|
252
|
+
| {
|
|
253
|
+
type: 'file-data';
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
Base-64 encoded media data.
|
|
257
|
+
*/
|
|
258
|
+
data: string;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
IANA media type.
|
|
262
|
+
@see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
263
|
+
*/
|
|
264
|
+
mediaType: string;
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Optional filename of the file.
|
|
268
|
+
*/
|
|
269
|
+
filename?: string;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Provider-specific options.
|
|
273
|
+
*/
|
|
274
|
+
providerOptions?: ProviderOptions;
|
|
275
|
+
}
|
|
276
|
+
| {
|
|
277
|
+
type: 'file-url';
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* URL of the file.
|
|
281
|
+
*/
|
|
282
|
+
url: string;
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Provider-specific options.
|
|
286
|
+
*/
|
|
287
|
+
providerOptions?: ProviderOptions;
|
|
288
|
+
}
|
|
289
|
+
| {
|
|
290
|
+
type: 'file-id';
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* ID of the file.
|
|
294
|
+
*
|
|
295
|
+
* If you use multiple providers, you need to
|
|
296
|
+
* specify the provider specific ids using
|
|
297
|
+
* the Record option. The key is the provider
|
|
298
|
+
* name, e.g. 'openai' or 'anthropic'.
|
|
299
|
+
*/
|
|
300
|
+
fileId: string | Record<string, string>;
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Provider-specific options.
|
|
304
|
+
*/
|
|
305
|
+
providerOptions?: ProviderOptions;
|
|
306
|
+
}
|
|
307
|
+
| {
|
|
308
|
+
/**
|
|
309
|
+
* Images that are referenced using base64 encoded data.
|
|
310
|
+
*/
|
|
311
|
+
type: 'image-data';
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
Base-64 encoded image data.
|
|
315
|
+
*/
|
|
316
|
+
data: string;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
IANA media type.
|
|
320
|
+
@see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
321
|
+
*/
|
|
322
|
+
mediaType: string;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Provider-specific options.
|
|
326
|
+
*/
|
|
327
|
+
providerOptions?: ProviderOptions;
|
|
328
|
+
}
|
|
329
|
+
| {
|
|
330
|
+
/**
|
|
331
|
+
* Images that are referenced using a URL.
|
|
332
|
+
*/
|
|
333
|
+
type: 'image-url';
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* URL of the image.
|
|
337
|
+
*/
|
|
338
|
+
url: string;
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Provider-specific options.
|
|
342
|
+
*/
|
|
343
|
+
providerOptions?: ProviderOptions;
|
|
344
|
+
}
|
|
345
|
+
| {
|
|
346
|
+
/**
|
|
347
|
+
* Images that are referenced using a provider file id.
|
|
348
|
+
*/
|
|
349
|
+
type: 'image-file-id';
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Image that is referenced using a provider file id.
|
|
353
|
+
*
|
|
354
|
+
* If you use multiple providers, you need to
|
|
355
|
+
* specify the provider specific ids using
|
|
356
|
+
* the Record option. The key is the provider
|
|
357
|
+
* name, e.g. 'openai' or 'anthropic'.
|
|
358
|
+
*/
|
|
359
|
+
fileId: string | Record<string, string>;
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Provider-specific options.
|
|
363
|
+
*/
|
|
364
|
+
providerOptions?: ProviderOptions;
|
|
365
|
+
}
|
|
366
|
+
| {
|
|
367
|
+
/**
|
|
368
|
+
* Custom content part. This can be used to implement
|
|
369
|
+
* provider-specific content parts.
|
|
370
|
+
*/
|
|
371
|
+
type: 'custom';
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Provider-specific options.
|
|
375
|
+
*/
|
|
376
|
+
providerOptions?: ProviderOptions;
|
|
377
|
+
}
|
|
378
|
+
>;
|
|
379
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { isAsyncIterable } from '../is-async-iterable';
|
|
2
|
+
import { ToolExecutionOptions, ToolExecuteFunction } from './tool';
|
|
3
|
+
|
|
4
|
+
export async function* executeTool<INPUT, OUTPUT>({
|
|
5
|
+
execute,
|
|
6
|
+
input,
|
|
7
|
+
options,
|
|
8
|
+
}: {
|
|
9
|
+
execute: ToolExecuteFunction<INPUT, OUTPUT>;
|
|
10
|
+
input: INPUT;
|
|
11
|
+
options: ToolExecutionOptions;
|
|
12
|
+
}): AsyncGenerator<
|
|
13
|
+
{ type: 'preliminary'; output: OUTPUT } | { type: 'final'; output: OUTPUT }
|
|
14
|
+
> {
|
|
15
|
+
const result = execute(input, options);
|
|
16
|
+
|
|
17
|
+
if (isAsyncIterable(result)) {
|
|
18
|
+
let lastOutput: OUTPUT | undefined;
|
|
19
|
+
for await (const output of result) {
|
|
20
|
+
lastOutput = output;
|
|
21
|
+
yield { type: 'preliminary', output };
|
|
22
|
+
}
|
|
23
|
+
yield { type: 'final', output: lastOutput! };
|
|
24
|
+
} else {
|
|
25
|
+
yield { type: 'final', output: await result };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
AssistantContent,
|
|
3
|
+
AssistantModelMessage,
|
|
4
|
+
} from './assistant-model-message';
|
|
5
|
+
export type {
|
|
6
|
+
FilePart,
|
|
7
|
+
ImagePart,
|
|
8
|
+
ReasoningPart,
|
|
9
|
+
TextPart,
|
|
10
|
+
ToolCallPart,
|
|
11
|
+
ToolResultOutput,
|
|
12
|
+
ToolResultPart,
|
|
13
|
+
} from './content-part';
|
|
14
|
+
export type { DataContent } from './data-content';
|
|
15
|
+
export { executeTool } from './execute-tool';
|
|
16
|
+
export type { ModelMessage } from './model-message';
|
|
17
|
+
export type { ProviderOptions } from './provider-options';
|
|
18
|
+
export type { SystemModelMessage } from './system-model-message';
|
|
19
|
+
export {
|
|
20
|
+
dynamicTool,
|
|
21
|
+
tool,
|
|
22
|
+
type InferToolInput,
|
|
23
|
+
type InferToolOutput,
|
|
24
|
+
type Tool,
|
|
25
|
+
type ToolExecutionOptions,
|
|
26
|
+
type ToolExecuteFunction,
|
|
27
|
+
type ToolNeedsApprovalFunction,
|
|
28
|
+
} from './tool';
|
|
29
|
+
export type { ToolApprovalRequest } from './tool-approval-request';
|
|
30
|
+
export type { ToolApprovalResponse } from './tool-approval-response';
|
|
31
|
+
export type { ToolCall } from './tool-call';
|
|
32
|
+
export type { ToolContent, ToolModelMessage } from './tool-model-message';
|
|
33
|
+
export type { ToolResult } from './tool-result';
|
|
34
|
+
export type { UserContent, UserModelMessage } from './user-model-message';
|
|
35
|
+
import type { ToolExecutionOptions } from './tool';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @deprecated Use ToolExecutionOptions instead.
|
|
39
|
+
*/
|
|
40
|
+
export type ToolCallOptions = ToolExecutionOptions;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AssistantModelMessage } from './assistant-model-message';
|
|
2
|
+
import { SystemModelMessage } from './system-model-message';
|
|
3
|
+
import { ToolModelMessage } from './tool-model-message';
|
|
4
|
+
import { UserModelMessage } from './user-model-message';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
A message that can be used in the `messages` field of a prompt.
|
|
8
|
+
It can be a user message, an assistant message, or a tool message.
|
|
9
|
+
*/
|
|
10
|
+
export type ModelMessage =
|
|
11
|
+
| SystemModelMessage
|
|
12
|
+
| UserModelMessage
|
|
13
|
+
| AssistantModelMessage
|
|
14
|
+
| ToolModelMessage;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SharedV3ProviderOptions } from '@ai-sdk/provider';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Additional provider-specific options.
|
|
5
|
+
|
|
6
|
+
They are passed through to the provider from the AI SDK and enable
|
|
7
|
+
provider-specific functionality that can be fully encapsulated in the provider.
|
|
8
|
+
*/
|
|
9
|
+
export type ProviderOptions = SharedV3ProviderOptions;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ProviderOptions } from './provider-options';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
A system message. It can contain system information.
|
|
5
|
+
|
|
6
|
+
Note: using the "system" part of the prompt is strongly preferred
|
|
7
|
+
to increase the resilience against prompt injection attacks,
|
|
8
|
+
and because not all providers support several system messages.
|
|
9
|
+
*/
|
|
10
|
+
export type SystemModelMessage = {
|
|
11
|
+
role: 'system';
|
|
12
|
+
content: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
Additional provider-specific metadata. They are passed through
|
|
16
|
+
to the provider from the AI SDK and enable provider-specific
|
|
17
|
+
functionality that can be fully encapsulated in the provider.
|
|
18
|
+
*/
|
|
19
|
+
providerOptions?: ProviderOptions;
|
|
20
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool approval request prompt part.
|
|
3
|
+
*/
|
|
4
|
+
export type ToolApprovalRequest = {
|
|
5
|
+
type: 'tool-approval-request';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ID of the tool approval.
|
|
9
|
+
*/
|
|
10
|
+
approvalId: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* ID of the tool call that the approval request is for.
|
|
14
|
+
*/
|
|
15
|
+
toolCallId: string;
|
|
16
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool approval response prompt part.
|
|
3
|
+
*/
|
|
4
|
+
export type ToolApprovalResponse = {
|
|
5
|
+
type: 'tool-approval-response';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ID of the tool approval.
|
|
9
|
+
*/
|
|
10
|
+
approvalId: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Flag indicating whether the approval was granted or denied.
|
|
14
|
+
*/
|
|
15
|
+
approved: boolean;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Optional reason for the approval or denial.
|
|
19
|
+
*/
|
|
20
|
+
reason?: string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Flag indicating whether the tool call is provider-executed.
|
|
24
|
+
* Only provider-executed tool approval responses should be sent to the model.
|
|
25
|
+
*/
|
|
26
|
+
providerExecuted?: boolean;
|
|
27
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Typed tool call that is returned by generateText and streamText.
|
|
3
|
+
It contains the tool call ID, the tool name, and the tool arguments.
|
|
4
|
+
*/
|
|
5
|
+
export interface ToolCall<NAME extends string, INPUT> {
|
|
6
|
+
/**
|
|
7
|
+
ID of the tool call. This ID is used to match the tool call with the tool result.
|
|
8
|
+
*/
|
|
9
|
+
toolCallId: string;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
Name of the tool that is being called.
|
|
13
|
+
*/
|
|
14
|
+
toolName: NAME;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
|
18
|
+
*/
|
|
19
|
+
input: INPUT;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Whether the tool call will be executed by the provider.
|
|
23
|
+
* If this flag is not set or is false, the tool call will be executed by the client.
|
|
24
|
+
*/
|
|
25
|
+
providerExecuted?: boolean;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Whether the tool is dynamic.
|
|
29
|
+
*/
|
|
30
|
+
dynamic?: boolean;
|
|
31
|
+
}
|