@ai-sdk/provider-utils 5.0.0-beta.4 → 5.0.0-beta.49
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 +373 -11
- package/dist/index.d.ts +1460 -553
- package/dist/index.js +1044 -361
- package/dist/index.js.map +1 -1
- package/dist/test/index.d.ts +2 -1
- package/dist/test/index.js +18 -37
- package/dist/test/index.js.map +1 -1
- package/package.json +16 -16
- package/src/add-additional-properties-to-json-schema.ts +1 -1
- package/src/as-array.ts +12 -0
- package/src/cancel-response-body.ts +19 -0
- package/src/convert-image-model-file-to-data-uri.ts +1 -1
- package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
- package/src/create-tool-name-mapping.ts +1 -1
- package/src/detect-media-type.ts +312 -0
- package/src/download-blob.ts +8 -9
- package/src/extract-lines.ts +31 -0
- package/src/fetch-with-validated-redirects.ts +87 -0
- package/src/filter-nullable.ts +11 -0
- package/src/get-error-message.ts +1 -15
- package/src/get-from-api.ts +2 -2
- package/src/has-required-key.ts +6 -0
- package/src/index.ts +47 -12
- package/src/inject-json-instruction.ts +1 -1
- package/src/is-browser-runtime.ts +13 -0
- package/src/is-buffer.ts +9 -0
- package/src/is-json-serializable.ts +29 -0
- package/src/is-provider-reference.ts +21 -0
- package/src/is-same-origin.ts +19 -0
- package/src/is-url-supported.ts +17 -2
- package/src/load-api-key.ts +1 -1
- package/src/load-setting.ts +1 -1
- package/src/map-reasoning-to-provider.ts +108 -0
- package/src/maybe-promise-like.ts +3 -0
- package/src/parse-json-event-stream.ts +3 -3
- package/src/parse-json.ts +3 -3
- package/src/parse-provider-options.ts +1 -1
- package/src/post-to-api.ts +4 -4
- package/src/provider-defined-tool-factory.ts +129 -0
- package/src/provider-executed-tool-factory.ts +69 -0
- package/src/read-response-with-size-limit.ts +4 -0
- package/src/resolve-full-media-type.ts +49 -0
- package/src/resolve-provider-reference.ts +26 -0
- package/src/resolve.ts +16 -1
- package/src/response-handler.ts +3 -3
- package/src/schema.ts +11 -4
- package/src/secure-json-parse.ts +1 -1
- package/src/serialize-model-options.ts +63 -0
- package/src/streaming-tool-call-tracker.ts +241 -0
- package/src/test/convert-response-stream-to-array.ts +1 -1
- package/src/test/is-node-version.ts +22 -1
- package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
- package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +4 -4
- package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +5 -5
- package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
- package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
- package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
- package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +10 -8
- package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +9 -10
- package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
- package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
- package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
- package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
- package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
- package/src/types/assistant-model-message.ts +5 -3
- package/src/types/content-part.ts +158 -19
- package/src/types/context.ts +4 -0
- package/src/types/executable-tool.ts +17 -0
- package/src/types/execute-tool.ts +29 -9
- package/src/types/file-data.ts +48 -0
- package/src/types/index.ts +29 -11
- package/src/types/infer-tool-context.ts +41 -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 +44 -0
- package/src/types/model-message.ts +4 -4
- package/src/types/never-optional.ts +7 -0
- package/src/types/provider-options.ts +1 -1
- package/src/types/provider-reference.ts +10 -0
- package/src/types/sandbox.ts +217 -0
- package/src/types/system-model-message.ts +1 -1
- package/src/types/tool-approval-request.ts +13 -0
- package/src/types/tool-execute-function.ts +56 -0
- package/src/types/tool-model-message.ts +3 -3
- package/src/types/tool-needs-approval-function.ts +39 -0
- package/src/types/tool-set.ts +22 -0
- package/src/types/tool.ts +278 -225
- package/src/types/user-model-message.ts +2 -2
- package/src/validate-download-url.ts +120 -33
- package/src/validate-types.ts +5 -3
- package/dist/index.d.mts +0 -1455
- package/dist/index.mjs +0 -2754
- 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/src/provider-tool-factory.ts +0 -125
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { JSONValue } from '@ai-sdk/provider';
|
|
2
|
-
import { DataContent } from './data-content';
|
|
3
|
-
import {
|
|
1
|
+
import type { JSONValue } from '@ai-sdk/provider';
|
|
2
|
+
import type { DataContent } from './data-content';
|
|
3
|
+
import type { FileData, FileDataData, FileDataUrl } from './file-data';
|
|
4
|
+
import type { ProviderOptions } from './provider-options';
|
|
5
|
+
import type { ProviderReference } from './provider-reference';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Text content part of a prompt. It contains a string of text.
|
|
@@ -23,6 +25,9 @@ export interface TextPart {
|
|
|
23
25
|
|
|
24
26
|
/**
|
|
25
27
|
* Image content part of a prompt. It contains an image.
|
|
28
|
+
*
|
|
29
|
+
* @deprecated Use `FilePart` with `mediaType: 'image'` instead:
|
|
30
|
+
* `{ type: 'file', mediaType: 'image', data: { type: 'data', data } }`.
|
|
26
31
|
*/
|
|
27
32
|
export interface ImagePart {
|
|
28
33
|
type: 'image';
|
|
@@ -32,8 +37,9 @@ export interface ImagePart {
|
|
|
32
37
|
*
|
|
33
38
|
* - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
|
34
39
|
* - URL: a URL that points to the image
|
|
40
|
+
* - ProviderReference: a provider reference from `uploadFile`
|
|
35
41
|
*/
|
|
36
|
-
image: DataContent | URL;
|
|
42
|
+
image: DataContent | URL | ProviderReference;
|
|
37
43
|
|
|
38
44
|
/**
|
|
39
45
|
* Optional IANA media type of the image.
|
|
@@ -57,12 +63,16 @@ export interface FilePart {
|
|
|
57
63
|
type: 'file';
|
|
58
64
|
|
|
59
65
|
/**
|
|
60
|
-
* File data.
|
|
66
|
+
* File data. Either a tagged shape or a bare shorthand:
|
|
61
67
|
*
|
|
62
|
-
* -
|
|
63
|
-
*
|
|
68
|
+
* - `{ type: 'data', data }` or bare `DataContent`: raw bytes
|
|
69
|
+
* (base64 string, Uint8Array, ArrayBuffer, Buffer)
|
|
70
|
+
* - `{ type: 'url', url }` or bare `URL`: a URL that points to the file
|
|
71
|
+
* - `{ type: 'reference', reference }` or bare `ProviderReference`:
|
|
72
|
+
* a provider reference from `uploadFile`
|
|
73
|
+
* - `{ type: 'text', text }`: inline text content (tagged only)
|
|
64
74
|
*/
|
|
65
|
-
data: DataContent | URL;
|
|
75
|
+
data: FileData | DataContent | URL | ProviderReference;
|
|
66
76
|
|
|
67
77
|
/**
|
|
68
78
|
* Optional filename of the file.
|
|
@@ -70,7 +80,14 @@ export interface FilePart {
|
|
|
70
80
|
filename?: string;
|
|
71
81
|
|
|
72
82
|
/**
|
|
73
|
-
* IANA media type
|
|
83
|
+
* Either a full IANA media type (`type/subtype`, e.g. `image/png`) or just
|
|
84
|
+
* the top-level IANA segment (e.g. `image`, `audio`, `video`, `text`).
|
|
85
|
+
*
|
|
86
|
+
* `*`-subtype wildcards (e.g. `image/*`) are normalized as equivalent to the
|
|
87
|
+
* top-level segment alone (e.g. `image`). Providers can use the helpers in
|
|
88
|
+
* `@ai-sdk/provider-utils` (`isFullMediaType`, `getTopLevelMediaType`,
|
|
89
|
+
* `detectMediaType`) to resolve the field according to their API
|
|
90
|
+
* requirements.
|
|
74
91
|
*
|
|
75
92
|
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
76
93
|
*/
|
|
@@ -103,6 +120,26 @@ export interface ReasoningPart {
|
|
|
103
120
|
providerOptions?: ProviderOptions;
|
|
104
121
|
}
|
|
105
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Custom content part of a prompt. It contains no standardized payload beyond
|
|
125
|
+
* provider-specific options.
|
|
126
|
+
*/
|
|
127
|
+
export interface CustomPart {
|
|
128
|
+
type: 'custom';
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The kind of custom content, in the format `{provider}.{provider-type}`.
|
|
132
|
+
*/
|
|
133
|
+
kind: `${string}.${string}`;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Additional provider-specific metadata. They are passed through
|
|
137
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
138
|
+
* functionality that can be fully encapsulated in the provider.
|
|
139
|
+
*/
|
|
140
|
+
providerOptions?: ProviderOptions;
|
|
141
|
+
}
|
|
142
|
+
|
|
106
143
|
/**
|
|
107
144
|
* Reasoning file content part of a prompt. It contains a file generated as part of reasoning.
|
|
108
145
|
*/
|
|
@@ -110,12 +147,21 @@ export interface ReasoningFilePart {
|
|
|
110
147
|
type: 'reasoning-file';
|
|
111
148
|
|
|
112
149
|
/**
|
|
113
|
-
*
|
|
150
|
+
* Reasoning file data.
|
|
114
151
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
152
|
+
* Reasoning files originate from a model's reasoning output and are always
|
|
153
|
+
* raw bytes or a fetchable URL. Unlike `FilePart.data`, the `reference` and
|
|
154
|
+
* `text` shapes are not supported here: provider references describe files
|
|
155
|
+
* uploaded by the user (not produced as model output), and reasoning text is
|
|
156
|
+
* carried by `ReasoningPart` rather than as a file.
|
|
157
|
+
*
|
|
158
|
+
* Either a tagged shape or a bare shorthand:
|
|
159
|
+
*
|
|
160
|
+
* - `{ type: 'data', data }` or bare `DataContent`: raw bytes
|
|
161
|
+
* (base64 string, Uint8Array, ArrayBuffer, Buffer)
|
|
162
|
+
* - `{ type: 'url', url }` or bare `URL`: a URL that points to the file
|
|
117
163
|
*/
|
|
118
|
-
data: DataContent | URL;
|
|
164
|
+
data: FileDataData | FileDataUrl | DataContent | URL;
|
|
119
165
|
|
|
120
166
|
/**
|
|
121
167
|
* IANA media type of the file.
|
|
@@ -271,14 +317,50 @@ export type ToolResultOutput =
|
|
|
271
317
|
providerOptions?: ProviderOptions;
|
|
272
318
|
}
|
|
273
319
|
| {
|
|
320
|
+
type: 'file';
|
|
321
|
+
|
|
274
322
|
/**
|
|
275
|
-
*
|
|
323
|
+
* File data as a tagged discriminated union:
|
|
324
|
+
*
|
|
325
|
+
* - `{ type: 'data', data }`: raw bytes
|
|
326
|
+
* (base64 string, Uint8Array, ArrayBuffer, Buffer)
|
|
327
|
+
* - `{ type: 'url', url }`: a URL that points to the file
|
|
328
|
+
* - `{ type: 'reference', reference }`: a provider reference
|
|
329
|
+
* from `uploadFile`
|
|
330
|
+
* - `{ type: 'text', text }`: inline text content (e.g. an inline
|
|
331
|
+
* text document)
|
|
332
|
+
*/
|
|
333
|
+
data: FileData;
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Either a full IANA media type (`type/subtype`, e.g. `image/png`) or just
|
|
337
|
+
* the top-level IANA segment (e.g. `image`, `audio`, `video`, `text`).
|
|
338
|
+
*
|
|
339
|
+
* `*`-subtype wildcards (e.g. `image/*`) are normalized as equivalent to the
|
|
340
|
+
* top-level segment alone (e.g. `image`). Providers can use the helpers in
|
|
341
|
+
* `@ai-sdk/provider-utils` (`isFullMediaType`, `getTopLevelMediaType`,
|
|
342
|
+
* `detectMediaType`) to resolve the field according to their API
|
|
343
|
+
* requirements.
|
|
344
|
+
*
|
|
345
|
+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
276
346
|
*/
|
|
277
|
-
type: 'media';
|
|
278
|
-
data: string;
|
|
279
347
|
mediaType: string;
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Optional filename of the file.
|
|
351
|
+
*/
|
|
352
|
+
filename?: string;
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Provider-specific options.
|
|
356
|
+
*/
|
|
357
|
+
providerOptions?: ProviderOptions;
|
|
280
358
|
}
|
|
281
359
|
| {
|
|
360
|
+
/**
|
|
361
|
+
* @deprecated Use 'file' with mediaType + tagged data instead:
|
|
362
|
+
* `{ type: 'file', mediaType, data: { type: 'data', data } }`.
|
|
363
|
+
*/
|
|
282
364
|
type: 'file-data';
|
|
283
365
|
|
|
284
366
|
/**
|
|
@@ -303,6 +385,10 @@ export type ToolResultOutput =
|
|
|
303
385
|
providerOptions?: ProviderOptions;
|
|
304
386
|
}
|
|
305
387
|
| {
|
|
388
|
+
/**
|
|
389
|
+
* @deprecated Use 'file' with mediaType and tagged data instead:
|
|
390
|
+
* `{ type: 'file', mediaType, data: { type: 'url', url: new URL(url) } }`.
|
|
391
|
+
*/
|
|
306
392
|
type: 'file-url';
|
|
307
393
|
|
|
308
394
|
/**
|
|
@@ -310,12 +396,22 @@ export type ToolResultOutput =
|
|
|
310
396
|
*/
|
|
311
397
|
url: string;
|
|
312
398
|
|
|
399
|
+
/**
|
|
400
|
+
* IANA media type.
|
|
401
|
+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
|
402
|
+
*/
|
|
403
|
+
mediaType?: string;
|
|
404
|
+
|
|
313
405
|
/**
|
|
314
406
|
* Provider-specific options.
|
|
315
407
|
*/
|
|
316
408
|
providerOptions?: ProviderOptions;
|
|
317
409
|
}
|
|
318
410
|
| {
|
|
411
|
+
/**
|
|
412
|
+
* @deprecated Use 'file' with tagged data instead:
|
|
413
|
+
* `{ type: 'file', mediaType, data: { type: 'reference', reference } }`.
|
|
414
|
+
*/
|
|
319
415
|
type: 'file-id';
|
|
320
416
|
|
|
321
417
|
/**
|
|
@@ -335,7 +431,27 @@ export type ToolResultOutput =
|
|
|
335
431
|
}
|
|
336
432
|
| {
|
|
337
433
|
/**
|
|
338
|
-
*
|
|
434
|
+
* @deprecated Use 'file' with tagged data instead:
|
|
435
|
+
* `{ type: 'file', mediaType, data: { type: 'reference', reference } }`.
|
|
436
|
+
*/
|
|
437
|
+
type: 'file-reference';
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Provider-specific references for the file.
|
|
441
|
+
* The key is the provider name, e.g. 'openai' or 'anthropic'.
|
|
442
|
+
*/
|
|
443
|
+
providerReference: ProviderReference;
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Provider-specific options.
|
|
447
|
+
*/
|
|
448
|
+
providerOptions?: ProviderOptions;
|
|
449
|
+
}
|
|
450
|
+
| {
|
|
451
|
+
/**
|
|
452
|
+
* @deprecated Use 'file' with mediaType (e.g. 'image' or a specific
|
|
453
|
+
* `image/*` subtype) and tagged data instead:
|
|
454
|
+
* `{ type: 'file', mediaType: 'image', data: { type: 'data', data } }`.
|
|
339
455
|
*/
|
|
340
456
|
type: 'image-data';
|
|
341
457
|
|
|
@@ -357,7 +473,9 @@ export type ToolResultOutput =
|
|
|
357
473
|
}
|
|
358
474
|
| {
|
|
359
475
|
/**
|
|
360
|
-
*
|
|
476
|
+
* @deprecated Use 'file' with `mediaType: 'image'` (or a specific
|
|
477
|
+
* `image/*` subtype) and tagged data instead:
|
|
478
|
+
* `{ type: 'file', mediaType: 'image', data: { type: 'url', url: new URL(url) } }`.
|
|
361
479
|
*/
|
|
362
480
|
type: 'image-url';
|
|
363
481
|
|
|
@@ -373,7 +491,9 @@ export type ToolResultOutput =
|
|
|
373
491
|
}
|
|
374
492
|
| {
|
|
375
493
|
/**
|
|
376
|
-
*
|
|
494
|
+
* @deprecated Use 'file' with `mediaType: 'image'` (or a specific
|
|
495
|
+
* `image/*` subtype) and tagged data instead:
|
|
496
|
+
* `{ type: 'file', mediaType: 'image', data: { type: 'reference', reference } }`.
|
|
377
497
|
*/
|
|
378
498
|
type: 'image-file-id';
|
|
379
499
|
|
|
@@ -387,6 +507,25 @@ export type ToolResultOutput =
|
|
|
387
507
|
*/
|
|
388
508
|
fileId: string | Record<string, string>;
|
|
389
509
|
|
|
510
|
+
/**
|
|
511
|
+
* Provider-specific options.
|
|
512
|
+
*/
|
|
513
|
+
providerOptions?: ProviderOptions;
|
|
514
|
+
}
|
|
515
|
+
| {
|
|
516
|
+
/**
|
|
517
|
+
* @deprecated Use 'file' with `mediaType: 'image'` (or a specific
|
|
518
|
+
* `image/*` subtype) and tagged data instead:
|
|
519
|
+
* `{ type: 'file', mediaType: 'image', data: { type: 'reference', reference } }`.
|
|
520
|
+
*/
|
|
521
|
+
type: 'image-file-reference';
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Provider-specific references for the image file.
|
|
525
|
+
* The key is the provider name, e.g. 'openai' or 'anthropic'.
|
|
526
|
+
*/
|
|
527
|
+
providerReference: ProviderReference;
|
|
528
|
+
|
|
390
529
|
/**
|
|
391
530
|
* Provider-specific options.
|
|
392
531
|
*/
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Tool } from './tool';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A tool that is guaranteed to expose an execute function.
|
|
5
|
+
*/
|
|
6
|
+
export type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
|
|
7
|
+
execute: NonNullable<TOOL['execute']>;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Checks whether a tool exposes an execute function.
|
|
12
|
+
*/
|
|
13
|
+
export function isExecutableTool<TOOL extends Tool>(
|
|
14
|
+
tool: TOOL | undefined,
|
|
15
|
+
): tool is ExecutableTool<TOOL> {
|
|
16
|
+
return tool != null && typeof tool.execute === 'function';
|
|
17
|
+
}
|
|
@@ -1,21 +1,41 @@
|
|
|
1
1
|
import { isAsyncIterable } from '../is-async-iterable';
|
|
2
|
-
import {
|
|
2
|
+
import type { ExecutableTool } from './executable-tool';
|
|
3
|
+
import type { InferToolContext } from './infer-tool-context';
|
|
4
|
+
import type { InferToolInput } from './infer-tool-input';
|
|
5
|
+
import type { InferToolOutput } from './infer-tool-output';
|
|
6
|
+
import type { Tool } from './tool';
|
|
7
|
+
import type { ToolExecutionOptions } from './tool-execute-function';
|
|
3
8
|
|
|
4
|
-
|
|
5
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Executes a tool function and normalizes its results into a stream of outputs.
|
|
11
|
+
*
|
|
12
|
+
* - If the tool's `execute` function returns an `AsyncIterable`, each yielded value is emitted as
|
|
13
|
+
* `{ type: "preliminary", output }`. After iteration completes, the last yielded value is emitted
|
|
14
|
+
* again as `{ type: "final", output }`.
|
|
15
|
+
* - If the tool returns a direct value or Promise, a single `{ type: "final", output }` is yielded.
|
|
16
|
+
*
|
|
17
|
+
* @param params.tool The tool whose `execute` function should be invoked.
|
|
18
|
+
* @param params.input The input value to pass to the tool.
|
|
19
|
+
* @param params.options Additional options for tool execution.
|
|
20
|
+
* @yields A preliminary output for each streamed value, followed by a final output, or a single final
|
|
21
|
+
* output for non-streaming tools.
|
|
22
|
+
*/
|
|
23
|
+
export async function* executeTool<TOOL extends Tool>({
|
|
24
|
+
tool,
|
|
6
25
|
input,
|
|
7
26
|
options,
|
|
8
27
|
}: {
|
|
9
|
-
|
|
10
|
-
input:
|
|
11
|
-
options: ToolExecutionOptions
|
|
28
|
+
tool: ExecutableTool<TOOL>;
|
|
29
|
+
input: InferToolInput<TOOL>;
|
|
30
|
+
options: ToolExecutionOptions<InferToolContext<TOOL>>;
|
|
12
31
|
}): AsyncGenerator<
|
|
13
|
-
|
|
32
|
+
| { type: 'preliminary'; output: InferToolOutput<TOOL> }
|
|
33
|
+
| { type: 'final'; output: InferToolOutput<TOOL> }
|
|
14
34
|
> {
|
|
15
|
-
const result = execute(input, options);
|
|
35
|
+
const result = tool.execute(input, options);
|
|
16
36
|
|
|
17
37
|
if (isAsyncIterable(result)) {
|
|
18
|
-
let lastOutput:
|
|
38
|
+
let lastOutput: InferToolOutput<TOOL> | undefined;
|
|
19
39
|
for await (const output of result) {
|
|
20
40
|
lastOutput = output;
|
|
21
41
|
yield { type: 'preliminary', output };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SharedV4FileDataReference,
|
|
3
|
+
SharedV4FileDataText,
|
|
4
|
+
SharedV4FileDataUrl,
|
|
5
|
+
} from '@ai-sdk/provider';
|
|
6
|
+
import type { DataContent } from './data-content';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* File data variant containing raw bytes (`Uint8Array`, `ArrayBuffer`, or
|
|
10
|
+
* `Buffer`) or a base64-encoded string.
|
|
11
|
+
*
|
|
12
|
+
* This is slightly more permissive than `SharedV4FileDataData`.
|
|
13
|
+
*/
|
|
14
|
+
export interface FileDataData {
|
|
15
|
+
type: 'data';
|
|
16
|
+
data: DataContent;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* File data variant containing a URL that points to the file.
|
|
21
|
+
*/
|
|
22
|
+
export type FileDataUrl = SharedV4FileDataUrl;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* File data variant containing a provider reference (`{ [provider]: id }`).
|
|
26
|
+
*/
|
|
27
|
+
export type FileDataReference = SharedV4FileDataReference;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* File data variant containing inline text content (e.g. an inline text
|
|
31
|
+
* document).
|
|
32
|
+
*/
|
|
33
|
+
export type FileDataText = SharedV4FileDataText;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* File data as a tagged discriminated union:
|
|
37
|
+
*
|
|
38
|
+
* - `{ type: 'data', data }`: raw bytes (`Uint8Array`, `ArrayBuffer`, or
|
|
39
|
+
* `Buffer`) or a base64-encoded string.
|
|
40
|
+
* - `{ type: 'url', url }`: a URL that points to the file.
|
|
41
|
+
* - `{ type: 'reference', reference }`: a provider reference (`{ [provider]: id }`).
|
|
42
|
+
* - `{ type: 'text', text }`: inline text content (e.g. an inline text document).
|
|
43
|
+
*/
|
|
44
|
+
export type FileData =
|
|
45
|
+
| FileDataData
|
|
46
|
+
| FileDataUrl
|
|
47
|
+
| FileDataReference
|
|
48
|
+
| FileDataText;
|
package/src/types/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type {
|
|
|
3
3
|
AssistantModelMessage,
|
|
4
4
|
} from './assistant-model-message';
|
|
5
5
|
export type {
|
|
6
|
+
CustomPart,
|
|
6
7
|
FilePart,
|
|
7
8
|
ImagePart,
|
|
8
9
|
ReasoningFilePart,
|
|
@@ -12,30 +13,47 @@ export type {
|
|
|
12
13
|
ToolResultOutput,
|
|
13
14
|
ToolResultPart,
|
|
14
15
|
} from './content-part';
|
|
16
|
+
export type { Context } from './context';
|
|
15
17
|
export type { DataContent } from './data-content';
|
|
18
|
+
export { isExecutableTool, type ExecutableTool } from './executable-tool';
|
|
16
19
|
export { executeTool } from './execute-tool';
|
|
20
|
+
export type {
|
|
21
|
+
FileData,
|
|
22
|
+
FileDataData,
|
|
23
|
+
FileDataReference,
|
|
24
|
+
FileDataText,
|
|
25
|
+
FileDataUrl,
|
|
26
|
+
} from './file-data';
|
|
27
|
+
export type { InferToolContext } from './infer-tool-context';
|
|
28
|
+
export type { InferToolInput } from './infer-tool-input';
|
|
29
|
+
export type { InferToolOutput } from './infer-tool-output';
|
|
30
|
+
export type { InferToolSetContext } from './infer-tool-set-context';
|
|
17
31
|
export type { ModelMessage } from './model-message';
|
|
18
32
|
export type { ProviderOptions } from './provider-options';
|
|
33
|
+
export type { ProviderReference } from './provider-reference';
|
|
34
|
+
export type {
|
|
35
|
+
SandboxSession as Experimental_SandboxSession,
|
|
36
|
+
SandboxProcess as Experimental_SandboxProcess,
|
|
37
|
+
} from './sandbox';
|
|
19
38
|
export type { SystemModelMessage } from './system-model-message';
|
|
20
39
|
export {
|
|
21
40
|
dynamicTool,
|
|
22
41
|
tool,
|
|
23
|
-
type
|
|
24
|
-
type
|
|
42
|
+
type DynamicTool,
|
|
43
|
+
type FunctionTool,
|
|
44
|
+
type ProviderDefinedTool,
|
|
45
|
+
type ProviderExecutedTool,
|
|
25
46
|
type Tool,
|
|
26
|
-
type ToolExecutionOptions,
|
|
27
|
-
type ToolExecuteFunction,
|
|
28
|
-
type ToolNeedsApprovalFunction,
|
|
29
47
|
} from './tool';
|
|
30
48
|
export type { ToolApprovalRequest } from './tool-approval-request';
|
|
31
49
|
export type { ToolApprovalResponse } from './tool-approval-response';
|
|
32
50
|
export type { ToolCall } from './tool-call';
|
|
51
|
+
export type {
|
|
52
|
+
ToolExecuteFunction,
|
|
53
|
+
ToolExecutionOptions,
|
|
54
|
+
} from './tool-execute-function';
|
|
33
55
|
export type { ToolContent, ToolModelMessage } from './tool-model-message';
|
|
56
|
+
export type { ToolNeedsApprovalFunction } from './tool-needs-approval-function';
|
|
34
57
|
export type { ToolResult } from './tool-result';
|
|
58
|
+
export type { ToolSet } from './tool-set';
|
|
35
59
|
export type { UserContent, UserModelMessage } from './user-model-message';
|
|
36
|
-
import type { ToolExecutionOptions } from './tool';
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @deprecated Use ToolExecutionOptions instead.
|
|
40
|
-
*/
|
|
41
|
-
export type ToolCallOptions = ToolExecutionOptions;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Context } from './context';
|
|
2
|
+
import type { Tool } from './tool';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Detects the `any` type so untyped tools can be treated as having no explicit
|
|
6
|
+
* context type.
|
|
7
|
+
*/
|
|
8
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Detects exact empty object contexts, including `{}` combined with
|
|
12
|
+
* `undefined`, which do not provide tool-specific context properties.
|
|
13
|
+
*/
|
|
14
|
+
type IsEmptyObject<T> = keyof NonNullable<T> extends never ? true : false;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Detects context types that come from omitted or broad context declarations
|
|
18
|
+
* rather than a concrete tool context schema.
|
|
19
|
+
*/
|
|
20
|
+
type IsUntypedContext<CONTEXT> =
|
|
21
|
+
IsAny<CONTEXT> extends true
|
|
22
|
+
? true
|
|
23
|
+
: unknown extends CONTEXT
|
|
24
|
+
? true
|
|
25
|
+
: IsEmptyObject<CONTEXT> extends true
|
|
26
|
+
? true
|
|
27
|
+
: string extends keyof CONTEXT
|
|
28
|
+
? CONTEXT extends Context
|
|
29
|
+
? true
|
|
30
|
+
: false
|
|
31
|
+
: false;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Infer the context type of a tool.
|
|
35
|
+
*/
|
|
36
|
+
export type InferToolContext<TOOL extends Tool> =
|
|
37
|
+
TOOL extends Tool<any, any, infer CONTEXT>
|
|
38
|
+
? IsUntypedContext<CONTEXT> extends true
|
|
39
|
+
? never
|
|
40
|
+
: CONTEXT
|
|
41
|
+
: never;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { InferToolContext } from './infer-tool-context';
|
|
2
|
+
import type { ToolSet } from './tool-set';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Builds the required portion of the tool context map for tools whose context
|
|
6
|
+
* type does not include `undefined`.
|
|
7
|
+
*/
|
|
8
|
+
type RequiredToolSetContext<TOOLS extends ToolSet> = {
|
|
9
|
+
[K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never
|
|
10
|
+
? never
|
|
11
|
+
: undefined extends InferToolContext<NoInfer<TOOLS[K]>>
|
|
12
|
+
? never
|
|
13
|
+
: K]: InferToolContext<NoInfer<TOOLS[K]>>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Builds the optional portion of the tool context map for tools whose context
|
|
18
|
+
* object itself may be `undefined`.
|
|
19
|
+
*/
|
|
20
|
+
type OptionalToolSetContext<TOOLS extends ToolSet> = {
|
|
21
|
+
[K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never
|
|
22
|
+
? never
|
|
23
|
+
: undefined extends InferToolContext<NoInfer<TOOLS[K]>>
|
|
24
|
+
? K
|
|
25
|
+
: never]?: InferToolContext<NoInfer<TOOLS[K]>>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Flattens intersected mapped types so type equality assertions and editor
|
|
30
|
+
* hovers show the resulting object shape.
|
|
31
|
+
*/
|
|
32
|
+
type Normalize<OBJECT> = { [KEY in keyof OBJECT]: OBJECT[KEY] };
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Infer the context type for a tool set.
|
|
36
|
+
*
|
|
37
|
+
* The inferred type maps each contextual tool name to its context type.
|
|
38
|
+
*
|
|
39
|
+
* Tools without concrete context are omitted. Tool contexts that include
|
|
40
|
+
* `undefined` are represented as optional properties.
|
|
41
|
+
*/
|
|
42
|
+
export type InferToolSetContext<TOOLS extends ToolSet> = Normalize<
|
|
43
|
+
RequiredToolSetContext<TOOLS> & OptionalToolSetContext<TOOLS>
|
|
44
|
+
>;
|
|
@@ -1,7 +1,7 @@
|
|
|
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';
|
|
1
|
+
import type { AssistantModelMessage } from './assistant-model-message';
|
|
2
|
+
import type { SystemModelMessage } from './system-model-message';
|
|
3
|
+
import type { ToolModelMessage } from './tool-model-message';
|
|
4
|
+
import type { UserModelMessage } from './user-model-message';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* A message that can be used in the `messages` field of a prompt.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { SharedV4ProviderReference } from '@ai-sdk/provider';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A mapping of provider names to provider-specific file identifiers.
|
|
5
|
+
*
|
|
6
|
+
* Provider references allow files to be identified across different
|
|
7
|
+
* providers without re-uploading, by storing each provider's own
|
|
8
|
+
* identifier for the same logical file.
|
|
9
|
+
*/
|
|
10
|
+
export type ProviderReference = SharedV4ProviderReference;
|