@ai-sdk/provider-utils 3.0.0-alpha.9 → 3.0.0-beta.2
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 +79 -0
- package/dist/index.d.mts +465 -100
- package/dist/index.d.ts +465 -100
- package/dist/index.js +154 -176
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +130 -151
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
import {
|
2
|
-
import
|
1
|
+
import { JSONValue, JSONParseError, TypeValidationError, APICallError, JSONSchema7, SharedV2ProviderOptions, LanguageModelV2ToolResultOutput, LanguageModelV2ToolResultPart } from '@ai-sdk/provider';
|
2
|
+
import * as z4 from 'zod/v4';
|
3
|
+
import { ZodType, z } from 'zod/v4';
|
3
4
|
import * as z3 from 'zod/v3';
|
4
|
-
import * as z4 from 'zod/v4/core';
|
5
5
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
6
6
|
export * from '@standard-schema/spec';
|
7
|
+
export { EventSourceMessage, EventSourceParserStream } from 'eventsource-parser/stream';
|
7
8
|
|
8
9
|
declare function combineHeaders(...headers: Array<Record<string, string | undefined> | undefined>): Record<string, string | undefined>;
|
9
10
|
|
@@ -23,14 +24,6 @@ declare function convertAsyncIteratorToReadableStream<T>(iterator: AsyncIterator
|
|
23
24
|
*/
|
24
25
|
declare function delay(delayInMs?: number | null): Promise<void>;
|
25
26
|
|
26
|
-
type EventSourceChunk = {
|
27
|
-
event: string | undefined;
|
28
|
-
data: string;
|
29
|
-
id?: string;
|
30
|
-
retry?: number;
|
31
|
-
};
|
32
|
-
declare function createEventSourceParserStream(): TransformStream<string, EventSourceChunk>;
|
33
|
-
|
34
27
|
/**
|
35
28
|
Extracts the headers from a response object and returns them as a key-value object.
|
36
29
|
|
@@ -103,44 +96,8 @@ type Validator<OBJECT = unknown> = {
|
|
103
96
|
*/
|
104
97
|
declare function validator<OBJECT>(validate?: undefined | ((value: unknown) => ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>)): Validator<OBJECT>;
|
105
98
|
declare function isValidator(value: unknown): value is Validator;
|
106
|
-
declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<OBJECT>): Validator<OBJECT>;
|
107
|
-
declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<OBJECT>): Validator<OBJECT>;
|
108
|
-
|
109
|
-
/**
|
110
|
-
* Used to mark schemas so we can support both Zod and custom schemas.
|
111
|
-
*/
|
112
|
-
declare const schemaSymbol: unique symbol;
|
113
|
-
type Schema<OBJECT = unknown> = Validator<OBJECT> & {
|
114
|
-
/**
|
115
|
-
* Used to mark schemas so we can support both Zod and custom schemas.
|
116
|
-
*/
|
117
|
-
[schemaSymbol]: true;
|
118
|
-
/**
|
119
|
-
* Schema type for inference.
|
120
|
-
*/
|
121
|
-
_type: OBJECT;
|
122
|
-
/**
|
123
|
-
* The JSON Schema for the schema. It is passed to the providers.
|
124
|
-
*/
|
125
|
-
readonly jsonSchema: JSONSchema7;
|
126
|
-
};
|
127
|
-
type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.$ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
|
128
|
-
/**
|
129
|
-
* Create a schema using a JSON Schema.
|
130
|
-
*
|
131
|
-
* @param jsonSchema The JSON Schema for the schema.
|
132
|
-
* @param options.validate Optional. A validation function for the schema.
|
133
|
-
*/
|
134
|
-
declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
|
135
|
-
validate?: (value: unknown) => {
|
136
|
-
success: true;
|
137
|
-
value: OBJECT;
|
138
|
-
} | {
|
139
|
-
success: false;
|
140
|
-
error: Error;
|
141
|
-
};
|
142
|
-
}): Schema<OBJECT>;
|
143
|
-
declare function asSchema<OBJECT>(schema: z4.$ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
|
99
|
+
declare function asValidator<OBJECT>(value: Validator<OBJECT> | StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
|
100
|
+
declare function standardSchemaValidator<OBJECT>(standardSchema: StandardSchemaV1<unknown, OBJECT>): Validator<OBJECT>;
|
144
101
|
|
145
102
|
/**
|
146
103
|
* Parses a JSON string into an unknown object.
|
@@ -160,10 +117,10 @@ declare function parseJSON(options: {
|
|
160
117
|
* @param {Validator<T>} schema - The schema to use for parsing the JSON.
|
161
118
|
* @returns {Promise<T>} - The parsed object.
|
162
119
|
*/
|
163
|
-
declare function parseJSON<
|
120
|
+
declare function parseJSON<T>(options: {
|
164
121
|
text: string;
|
165
|
-
schema:
|
166
|
-
}): Promise<
|
122
|
+
schema: z4.ZodType<T> | z3.Schema<T> | Validator<T>;
|
123
|
+
}): Promise<T>;
|
167
124
|
type ParseResult<T> = {
|
168
125
|
success: true;
|
169
126
|
value: T;
|
@@ -191,10 +148,10 @@ declare function safeParseJSON(options: {
|
|
191
148
|
* @param {Validator<T>} schema - The schema to use for parsing the JSON.
|
192
149
|
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
193
150
|
*/
|
194
|
-
declare function safeParseJSON<
|
151
|
+
declare function safeParseJSON<T>(options: {
|
195
152
|
text: string;
|
196
|
-
schema:
|
197
|
-
}): Promise<ParseResult<
|
153
|
+
schema: z4.ZodType<T> | z3.Schema<T> | Validator<T>;
|
154
|
+
}): Promise<ParseResult<T>>;
|
198
155
|
declare function isParsableJson(input: string): boolean;
|
199
156
|
|
200
157
|
type ResponseHandler<RETURN_TYPE> = (options: {
|
@@ -207,13 +164,13 @@ type ResponseHandler<RETURN_TYPE> = (options: {
|
|
207
164
|
responseHeaders?: Record<string, string>;
|
208
165
|
}>;
|
209
166
|
declare const createJsonErrorResponseHandler: <T>({ errorSchema, errorToMessage, isRetryable, }: {
|
210
|
-
errorSchema:
|
167
|
+
errorSchema: ZodType<T>;
|
211
168
|
errorToMessage: (error: T) => string;
|
212
169
|
isRetryable?: (response: Response, error?: T) => boolean;
|
213
170
|
}) => ResponseHandler<APICallError>;
|
214
|
-
declare const createEventSourceResponseHandler: <T>(chunkSchema:
|
215
|
-
declare const createJsonStreamResponseHandler: <T>(chunkSchema:
|
216
|
-
declare const createJsonResponseHandler: <T>(responseSchema:
|
171
|
+
declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
|
172
|
+
declare const createJsonStreamResponseHandler: <T>(chunkSchema: ZodType<T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
|
173
|
+
declare const createJsonResponseHandler: <T>(responseSchema: ZodType<T>) => ResponseHandler<T>;
|
217
174
|
declare const createBinaryResponseHandler: () => ResponseHandler<Uint8Array>;
|
218
175
|
declare const createStatusCodeErrorResponseHandler: () => ResponseHandler<APICallError>;
|
219
176
|
|
@@ -289,13 +246,13 @@ declare function loadSetting({ settingValue, environmentVariableName, settingNam
|
|
289
246
|
*/
|
290
247
|
declare function parseJsonEventStream<T>({ stream, schema, }: {
|
291
248
|
stream: ReadableStream<Uint8Array>;
|
292
|
-
schema:
|
249
|
+
schema: ZodType<T>;
|
293
250
|
}): ReadableStream<ParseResult<T>>;
|
294
251
|
|
295
252
|
declare function parseProviderOptions<T>({ provider, providerOptions, schema, }: {
|
296
253
|
provider: string;
|
297
254
|
providerOptions: Record<string, unknown> | undefined;
|
298
|
-
schema: z.
|
255
|
+
schema: z.ZodType<T, T>;
|
299
256
|
}): Promise<T | undefined>;
|
300
257
|
|
301
258
|
declare const postJsonToApi: <T>({ url, headers, body, failedResponseHandler, successfulResponseHandler, abortSignal, fetch, }: {
|
@@ -342,24 +299,146 @@ declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, fa
|
|
342
299
|
}>;
|
343
300
|
|
344
301
|
/**
|
345
|
-
*
|
346
|
-
* @param record - The input object whose entries may be null or undefined.
|
347
|
-
* @returns A new object containing only entries with non-null and non-undefined values.
|
302
|
+
* Used to mark schemas so we can support both Zod and custom schemas.
|
348
303
|
*/
|
349
|
-
declare
|
304
|
+
declare const schemaSymbol: unique symbol;
|
305
|
+
type Schema<OBJECT = unknown> = Validator<OBJECT> & {
|
306
|
+
/**
|
307
|
+
* Used to mark schemas so we can support both Zod and custom schemas.
|
308
|
+
*/
|
309
|
+
[schemaSymbol]: true;
|
310
|
+
/**
|
311
|
+
* Schema type for inference.
|
312
|
+
*/
|
313
|
+
_type: OBJECT;
|
314
|
+
/**
|
315
|
+
* The JSON Schema for the schema. It is passed to the providers.
|
316
|
+
*/
|
317
|
+
readonly jsonSchema: JSONSchema7;
|
318
|
+
};
|
319
|
+
type FlexibleSchema<T> = z4.ZodType<T> | z3.Schema<T> | Schema<T>;
|
320
|
+
type InferSchema<SCHEMA> = SCHEMA extends z3.Schema ? z3.infer<SCHEMA> : SCHEMA extends z4.ZodType ? z4.infer<SCHEMA> : SCHEMA extends Schema<infer T> ? T : never;
|
321
|
+
/**
|
322
|
+
* Create a schema using a JSON Schema.
|
323
|
+
*
|
324
|
+
* @param jsonSchema The JSON Schema for the schema.
|
325
|
+
* @param options.validate Optional. A validation function for the schema.
|
326
|
+
*/
|
327
|
+
declare function jsonSchema<OBJECT = unknown>(jsonSchema: JSONSchema7, { validate, }?: {
|
328
|
+
validate?: (value: unknown) => {
|
329
|
+
success: true;
|
330
|
+
value: OBJECT;
|
331
|
+
} | {
|
332
|
+
success: false;
|
333
|
+
error: Error;
|
334
|
+
};
|
335
|
+
}): Schema<OBJECT>;
|
336
|
+
declare function asSchema<OBJECT>(schema: z4.ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any> | Schema<OBJECT> | undefined): Schema<OBJECT>;
|
350
337
|
|
351
|
-
type Resolvable<T> = T | Promise<T> | (() => T) | (() => Promise<T>);
|
352
338
|
/**
|
353
|
-
|
354
|
-
|
339
|
+
Additional provider-specific options.
|
340
|
+
|
341
|
+
They are passed through to the provider from the AI SDK and enable
|
342
|
+
provider-specific functionality that can be fully encapsulated in the provider.
|
355
343
|
*/
|
356
|
-
|
344
|
+
type ProviderOptions = SharedV2ProviderOptions;
|
357
345
|
|
358
346
|
/**
|
359
|
-
|
360
|
-
|
347
|
+
Data content. Can either be a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer.
|
348
|
+
*/
|
349
|
+
type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
|
350
|
+
|
351
|
+
/**
|
352
|
+
Text content part of a prompt. It contains a string of text.
|
353
|
+
*/
|
354
|
+
interface TextPart {
|
355
|
+
type: 'text';
|
356
|
+
/**
|
357
|
+
The text content.
|
358
|
+
*/
|
359
|
+
text: string;
|
360
|
+
/**
|
361
|
+
Additional provider-specific metadata. They are passed through
|
362
|
+
to the provider from the AI SDK and enable provider-specific
|
363
|
+
functionality that can be fully encapsulated in the provider.
|
364
|
+
*/
|
365
|
+
providerOptions?: ProviderOptions;
|
366
|
+
}
|
367
|
+
/**
|
368
|
+
Image content part of a prompt. It contains an image.
|
369
|
+
*/
|
370
|
+
interface ImagePart {
|
371
|
+
type: 'image';
|
372
|
+
/**
|
373
|
+
Image data. Can either be:
|
374
|
+
|
375
|
+
- data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
376
|
+
- URL: a URL that points to the image
|
377
|
+
*/
|
378
|
+
image: DataContent | URL;
|
379
|
+
/**
|
380
|
+
Optional IANA media type of the image.
|
381
|
+
|
382
|
+
@see https://www.iana.org/assignments/media-types/media-types.xhtml
|
383
|
+
*/
|
384
|
+
mediaType?: string;
|
385
|
+
/**
|
386
|
+
Additional provider-specific metadata. They are passed through
|
387
|
+
to the provider from the AI SDK and enable provider-specific
|
388
|
+
functionality that can be fully encapsulated in the provider.
|
389
|
+
*/
|
390
|
+
providerOptions?: ProviderOptions;
|
391
|
+
}
|
392
|
+
/**
|
393
|
+
File content part of a prompt. It contains a file.
|
394
|
+
*/
|
395
|
+
interface FilePart {
|
396
|
+
type: 'file';
|
397
|
+
/**
|
398
|
+
File data. Can either be:
|
399
|
+
|
400
|
+
- data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
|
401
|
+
- URL: a URL that points to the image
|
402
|
+
*/
|
403
|
+
data: DataContent | URL;
|
404
|
+
/**
|
405
|
+
Optional filename of the file.
|
406
|
+
*/
|
407
|
+
filename?: string;
|
408
|
+
/**
|
409
|
+
IANA media type of the file.
|
410
|
+
|
411
|
+
@see https://www.iana.org/assignments/media-types/media-types.xhtml
|
412
|
+
*/
|
413
|
+
mediaType: string;
|
414
|
+
/**
|
415
|
+
Additional provider-specific metadata. They are passed through
|
416
|
+
to the provider from the AI SDK and enable provider-specific
|
417
|
+
functionality that can be fully encapsulated in the provider.
|
418
|
+
*/
|
419
|
+
providerOptions?: ProviderOptions;
|
420
|
+
}
|
421
|
+
/**
|
422
|
+
* Reasoning content part of a prompt. It contains a reasoning.
|
423
|
+
*/
|
424
|
+
interface ReasoningPart {
|
425
|
+
type: 'reasoning';
|
426
|
+
/**
|
427
|
+
The reasoning text.
|
428
|
+
*/
|
429
|
+
text: string;
|
430
|
+
/**
|
431
|
+
Additional provider-specific metadata. They are passed through
|
432
|
+
to the provider from the AI SDK and enable provider-specific
|
433
|
+
functionality that can be fully encapsulated in the provider.
|
434
|
+
*/
|
435
|
+
providerOptions?: ProviderOptions;
|
436
|
+
}
|
437
|
+
/**
|
438
|
+
Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
|
361
439
|
*/
|
362
|
-
interface
|
440
|
+
interface ToolCallPart {
|
441
|
+
type: 'tool-call';
|
363
442
|
/**
|
364
443
|
ID of the tool call. This ID is used to match the tool call with the tool result.
|
365
444
|
*/
|
@@ -367,47 +446,286 @@ interface ToolCall<NAME extends string, ARGS> {
|
|
367
446
|
/**
|
368
447
|
Name of the tool that is being called.
|
369
448
|
*/
|
370
|
-
toolName:
|
449
|
+
toolName: string;
|
371
450
|
/**
|
372
451
|
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
373
452
|
*/
|
374
|
-
|
453
|
+
input: unknown;
|
454
|
+
/**
|
455
|
+
Additional provider-specific metadata. They are passed through
|
456
|
+
to the provider from the AI SDK and enable provider-specific
|
457
|
+
functionality that can be fully encapsulated in the provider.
|
458
|
+
*/
|
459
|
+
providerOptions?: ProviderOptions;
|
460
|
+
/**
|
461
|
+
Whether the tool call was executed by the provider.
|
462
|
+
*/
|
463
|
+
providerExecuted?: boolean;
|
375
464
|
}
|
376
|
-
|
377
465
|
/**
|
378
|
-
|
379
|
-
It contains the tool call ID, the tool name, the tool arguments, and the tool result.
|
466
|
+
Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
|
380
467
|
*/
|
381
|
-
interface
|
468
|
+
interface ToolResultPart {
|
469
|
+
type: 'tool-result';
|
382
470
|
/**
|
383
|
-
ID of the tool call
|
384
|
-
|
471
|
+
ID of the tool call that this result is associated with.
|
472
|
+
*/
|
385
473
|
toolCallId: string;
|
386
474
|
/**
|
387
|
-
Name of the tool that
|
475
|
+
Name of the tool that generated this result.
|
476
|
+
*/
|
477
|
+
toolName: string;
|
478
|
+
/**
|
479
|
+
Result of the tool call. This is a JSON-serializable object.
|
388
480
|
*/
|
389
|
-
|
481
|
+
output: LanguageModelV2ToolResultOutput;
|
390
482
|
/**
|
391
|
-
|
483
|
+
Additional provider-specific metadata. They are passed through
|
484
|
+
to the provider from the AI SDK and enable provider-specific
|
485
|
+
functionality that can be fully encapsulated in the provider.
|
486
|
+
*/
|
487
|
+
providerOptions?: ProviderOptions;
|
488
|
+
}
|
489
|
+
|
490
|
+
/**
|
491
|
+
An assistant message. It can contain text, tool calls, or a combination of text and tool calls.
|
492
|
+
*/
|
493
|
+
type AssistantModelMessage = {
|
494
|
+
role: 'assistant';
|
495
|
+
content: AssistantContent;
|
496
|
+
/**
|
497
|
+
Additional provider-specific metadata. They are passed through
|
498
|
+
to the provider from the AI SDK and enable provider-specific
|
499
|
+
functionality that can be fully encapsulated in the provider.
|
500
|
+
*/
|
501
|
+
providerOptions?: ProviderOptions;
|
502
|
+
};
|
503
|
+
/**
|
504
|
+
Content of an assistant message.
|
505
|
+
It can be a string or an array of text, image, reasoning, redacted reasoning, and tool call parts.
|
506
|
+
*/
|
507
|
+
type AssistantContent = string | Array<TextPart | FilePart | ReasoningPart | ToolCallPart | ToolResultPart>;
|
508
|
+
|
509
|
+
/**
|
510
|
+
A system message. It can contain system information.
|
511
|
+
|
512
|
+
Note: using the "system" part of the prompt is strongly preferred
|
513
|
+
to increase the resilience against prompt injection attacks,
|
514
|
+
and because not all providers support several system messages.
|
515
|
+
*/
|
516
|
+
type SystemModelMessage = {
|
517
|
+
role: 'system';
|
518
|
+
content: string;
|
519
|
+
/**
|
520
|
+
Additional provider-specific metadata. They are passed through
|
521
|
+
to the provider from the AI SDK and enable provider-specific
|
522
|
+
functionality that can be fully encapsulated in the provider.
|
392
523
|
*/
|
393
|
-
|
524
|
+
providerOptions?: ProviderOptions;
|
525
|
+
};
|
526
|
+
|
527
|
+
/**
|
528
|
+
A tool message. It contains the result of one or more tool calls.
|
529
|
+
*/
|
530
|
+
type ToolModelMessage = {
|
531
|
+
role: 'tool';
|
532
|
+
content: ToolContent;
|
394
533
|
/**
|
395
|
-
|
534
|
+
Additional provider-specific metadata. They are passed through
|
535
|
+
to the provider from the AI SDK and enable provider-specific
|
536
|
+
functionality that can be fully encapsulated in the provider.
|
537
|
+
*/
|
538
|
+
providerOptions?: ProviderOptions;
|
539
|
+
};
|
540
|
+
/**
|
541
|
+
Content of a tool message. It is an array of tool result parts.
|
542
|
+
*/
|
543
|
+
type ToolContent = Array<ToolResultPart>;
|
544
|
+
|
545
|
+
/**
|
546
|
+
A user message. It can contain text or a combination of text and images.
|
547
|
+
*/
|
548
|
+
type UserModelMessage = {
|
549
|
+
role: 'user';
|
550
|
+
content: UserContent;
|
551
|
+
/**
|
552
|
+
Additional provider-specific metadata. They are passed through
|
553
|
+
to the provider from the AI SDK and enable provider-specific
|
554
|
+
functionality that can be fully encapsulated in the provider.
|
396
555
|
*/
|
397
|
-
|
556
|
+
providerOptions?: ProviderOptions;
|
557
|
+
};
|
558
|
+
/**
|
559
|
+
Content of a user message. It can be a string or an array of text and image parts.
|
560
|
+
*/
|
561
|
+
type UserContent = string | Array<TextPart | ImagePart | FilePart>;
|
562
|
+
|
563
|
+
/**
|
564
|
+
A message that can be used in the `messages` field of a prompt.
|
565
|
+
It can be a user message, an assistant message, or a tool message.
|
566
|
+
*/
|
567
|
+
type ModelMessage = SystemModelMessage | UserModelMessage | AssistantModelMessage | ToolModelMessage;
|
568
|
+
|
569
|
+
interface ToolCallOptions {
|
570
|
+
/**
|
571
|
+
* The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
|
572
|
+
*/
|
573
|
+
toolCallId: string;
|
574
|
+
/**
|
575
|
+
* Messages that were sent to the language model to initiate the response that contained the tool call.
|
576
|
+
* The messages **do not** include the system prompt nor the assistant response that contained the tool call.
|
577
|
+
*/
|
578
|
+
messages: ModelMessage[];
|
579
|
+
/**
|
580
|
+
* An optional abort signal that indicates that the overall operation should be aborted.
|
581
|
+
*/
|
582
|
+
abortSignal?: AbortSignal;
|
398
583
|
}
|
399
|
-
type
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
584
|
+
type ToolExecuteFunction<INPUT, OUTPUT> = (input: INPUT, options: ToolCallOptions) => PromiseLike<OUTPUT> | OUTPUT;
|
585
|
+
type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
|
586
|
+
/**
|
587
|
+
A tool contains the description and the schema of the input that the tool expects.
|
588
|
+
This enables the language model to generate the input.
|
589
|
+
|
590
|
+
The tool can also contain an optional execute function for the actual execution function of the tool.
|
591
|
+
*/
|
592
|
+
type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any> = {
|
406
593
|
/**
|
407
|
-
|
594
|
+
An optional description of what the tool does.
|
595
|
+
Will be used by the language model to decide whether to use the tool.
|
596
|
+
Not used for provider-defined tools.
|
408
597
|
*/
|
409
|
-
|
410
|
-
|
598
|
+
description?: string;
|
599
|
+
/**
|
600
|
+
Additional provider-specific metadata. They are passed through
|
601
|
+
to the provider from the AI SDK and enable provider-specific
|
602
|
+
functionality that can be fully encapsulated in the provider.
|
603
|
+
*/
|
604
|
+
providerOptions?: ProviderOptions;
|
605
|
+
} & NeverOptional<INPUT, {
|
606
|
+
/**
|
607
|
+
The schema of the input that the tool expects. The language model will use this to generate the input.
|
608
|
+
It is also used to validate the output of the language model.
|
609
|
+
Use descriptions to make the input understandable for the language model.
|
610
|
+
*/
|
611
|
+
inputSchema: FlexibleSchema<INPUT>;
|
612
|
+
/**
|
613
|
+
* Optional function that is called when the argument streaming starts.
|
614
|
+
* Only called when the tool is used in a streaming context.
|
615
|
+
*/
|
616
|
+
onInputStart?: (options: ToolCallOptions) => void | PromiseLike<void>;
|
617
|
+
/**
|
618
|
+
* Optional function that is called when an argument streaming delta is available.
|
619
|
+
* Only called when the tool is used in a streaming context.
|
620
|
+
*/
|
621
|
+
onInputDelta?: (options: {
|
622
|
+
inputTextDelta: string;
|
623
|
+
} & ToolCallOptions) => void | PromiseLike<void>;
|
624
|
+
/**
|
625
|
+
* Optional function that is called when a tool call can be started,
|
626
|
+
* even if the execute function is not provided.
|
627
|
+
*/
|
628
|
+
onInputAvailable?: (options: {
|
629
|
+
input: [INPUT] extends [never] ? undefined : INPUT;
|
630
|
+
} & ToolCallOptions) => void | PromiseLike<void>;
|
631
|
+
}> & NeverOptional<OUTPUT, {
|
632
|
+
/**
|
633
|
+
Optional conversion function that maps the tool result to an output that can be used by the language model.
|
634
|
+
|
635
|
+
If not provided, the tool result will be sent as a JSON object.
|
636
|
+
*/
|
637
|
+
toModelOutput?: (output: OUTPUT) => LanguageModelV2ToolResultPart['output'];
|
638
|
+
} & ({
|
639
|
+
/**
|
640
|
+
An async function that is called with the arguments from the tool call and produces a result.
|
641
|
+
If not provided, the tool will not be executed automatically.
|
642
|
+
|
643
|
+
@args is the input of the tool call.
|
644
|
+
@options.abortSignal is a signal that can be used to abort the tool call.
|
645
|
+
*/
|
646
|
+
execute: ToolExecuteFunction<INPUT, OUTPUT>;
|
647
|
+
outputSchema?: FlexibleSchema<OUTPUT>;
|
648
|
+
} | {
|
649
|
+
outputSchema: FlexibleSchema<OUTPUT>;
|
650
|
+
execute?: never;
|
651
|
+
})> & ({
|
652
|
+
/**
|
653
|
+
Function tool.
|
654
|
+
*/
|
655
|
+
type?: undefined | 'function';
|
656
|
+
} | {
|
657
|
+
/**
|
658
|
+
Provider-defined tool.
|
659
|
+
*/
|
660
|
+
type: 'provider-defined';
|
661
|
+
/**
|
662
|
+
The ID of the tool. Should follow the format `<provider-name>.<unique-tool-name>`.
|
663
|
+
*/
|
664
|
+
id: `${string}.${string}`;
|
665
|
+
/**
|
666
|
+
The name of the tool that the user must use in the tool set.
|
667
|
+
*/
|
668
|
+
name: string;
|
669
|
+
/**
|
670
|
+
The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
|
671
|
+
*/
|
672
|
+
args: Record<string, unknown>;
|
673
|
+
});
|
674
|
+
/**
|
675
|
+
* Infer the input type of a tool.
|
676
|
+
*/
|
677
|
+
type InferToolInput<TOOL extends Tool> = TOOL extends Tool<infer INPUT, any> ? INPUT : never;
|
678
|
+
/**
|
679
|
+
* Infer the output type of a tool.
|
680
|
+
*/
|
681
|
+
type InferToolOutput<TOOL extends Tool> = TOOL extends Tool<any, infer OUTPUT> ? OUTPUT : never;
|
682
|
+
/**
|
683
|
+
Helper function for inferring the execute args of a tool.
|
684
|
+
*/
|
685
|
+
declare function tool<INPUT, OUTPUT>(tool: Tool<INPUT, OUTPUT>): Tool<INPUT, OUTPUT>;
|
686
|
+
declare function tool<INPUT>(tool: Tool<INPUT, never>): Tool<INPUT, never>;
|
687
|
+
declare function tool<OUTPUT>(tool: Tool<never, OUTPUT>): Tool<never, OUTPUT>;
|
688
|
+
declare function tool(tool: Tool<never, never>): Tool<never, never>;
|
689
|
+
|
690
|
+
type ProviderDefinedToolFactory<INPUT, ARGS extends object> = <OUTPUT>(options: ARGS & {
|
691
|
+
execute?: ToolExecuteFunction<INPUT, OUTPUT>;
|
692
|
+
toModelOutput?: Tool<INPUT, OUTPUT>['toModelOutput'];
|
693
|
+
onInputStart?: Tool<INPUT, OUTPUT>['onInputStart'];
|
694
|
+
onInputDelta?: Tool<INPUT, OUTPUT>['onInputDelta'];
|
695
|
+
onInputAvailable?: Tool<INPUT, OUTPUT>['onInputAvailable'];
|
696
|
+
}) => Tool<INPUT, OUTPUT>;
|
697
|
+
declare function createProviderDefinedToolFactory<INPUT, ARGS extends object>({ id, name, inputSchema, }: {
|
698
|
+
id: `${string}.${string}`;
|
699
|
+
name: string;
|
700
|
+
inputSchema: FlexibleSchema<INPUT>;
|
701
|
+
}): ProviderDefinedToolFactory<INPUT, ARGS>;
|
702
|
+
type ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object> = (options: ARGS & {
|
703
|
+
execute?: ToolExecuteFunction<INPUT, OUTPUT>;
|
704
|
+
toModelOutput?: Tool<INPUT, OUTPUT>['toModelOutput'];
|
705
|
+
onInputStart?: Tool<INPUT, OUTPUT>['onInputStart'];
|
706
|
+
onInputDelta?: Tool<INPUT, OUTPUT>['onInputDelta'];
|
707
|
+
onInputAvailable?: Tool<INPUT, OUTPUT>['onInputAvailable'];
|
708
|
+
}) => Tool<INPUT, OUTPUT>;
|
709
|
+
declare function createProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object>({ id, name, inputSchema, outputSchema, }: {
|
710
|
+
id: `${string}.${string}`;
|
711
|
+
name: string;
|
712
|
+
inputSchema: FlexibleSchema<INPUT>;
|
713
|
+
outputSchema: FlexibleSchema<OUTPUT>;
|
714
|
+
}): ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS>;
|
715
|
+
|
716
|
+
/**
|
717
|
+
* Removes entries from a record where the value is null or undefined.
|
718
|
+
* @param record - The input object whose entries may be null or undefined.
|
719
|
+
* @returns A new object containing only entries with non-null and non-undefined values.
|
720
|
+
*/
|
721
|
+
declare function removeUndefinedEntries<T>(record: Record<string, T | undefined>): Record<string, T>;
|
722
|
+
|
723
|
+
type Resolvable<T> = T | Promise<T> | (() => T) | (() => Promise<T>);
|
724
|
+
/**
|
725
|
+
* Resolves a value that could be a raw value, a Promise, a function returning a value,
|
726
|
+
* or a function returning a Promise.
|
727
|
+
*/
|
728
|
+
declare function resolve<T>(value: Resolvable<T>): Promise<T>;
|
411
729
|
|
412
730
|
declare function convertBase64ToUint8Array(base64String: string): Uint8Array<ArrayBuffer>;
|
413
731
|
declare function convertUint8ArrayToBase64(array: Uint8Array): string;
|
@@ -424,7 +742,7 @@ declare function convertToBase64(value: string | Uint8Array): string;
|
|
424
742
|
*/
|
425
743
|
declare function validateTypes<OBJECT>({ value, schema, }: {
|
426
744
|
value: unknown;
|
427
|
-
schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
|
745
|
+
schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
|
428
746
|
}): Promise<OBJECT>;
|
429
747
|
/**
|
430
748
|
* Safely validates the types of an unknown object using a schema and
|
@@ -437,7 +755,7 @@ declare function validateTypes<OBJECT>({ value, schema, }: {
|
|
437
755
|
*/
|
438
756
|
declare function safeValidateTypes<OBJECT>({ value, schema, }: {
|
439
757
|
value: unknown;
|
440
|
-
schema: StandardSchemaV1<OBJECT> | Validator<OBJECT>;
|
758
|
+
schema: StandardSchemaV1<unknown, OBJECT> | Validator<OBJECT>;
|
441
759
|
}): Promise<{
|
442
760
|
success: true;
|
443
761
|
value: OBJECT;
|
@@ -450,7 +768,7 @@ declare function safeValidateTypes<OBJECT>({ value, schema, }: {
|
|
450
768
|
|
451
769
|
declare function withoutTrailingSlash(url: string | undefined): string | undefined;
|
452
770
|
|
453
|
-
declare function zodSchema<OBJECT>(zodSchema: z4
|
771
|
+
declare function zodSchema<OBJECT>(zodSchema: z4.ZodType<OBJECT, any> | z3.Schema<OBJECT, z3.ZodTypeDef, any>, options?: {
|
454
772
|
/**
|
455
773
|
* Enables support for references in the schema.
|
456
774
|
* This is required for recursive schemas, e.g. with `z.lazy`.
|
@@ -460,4 +778,51 @@ declare function zodSchema<OBJECT>(zodSchema: z4.$ZodType<OBJECT, any> | z3.Sche
|
|
460
778
|
useReferences?: boolean;
|
461
779
|
}): Schema<OBJECT>;
|
462
780
|
|
463
|
-
|
781
|
+
/**
|
782
|
+
Typed tool call that is returned by generateText and streamText.
|
783
|
+
It contains the tool call ID, the tool name, and the tool arguments.
|
784
|
+
*/
|
785
|
+
interface ToolCall<NAME extends string, INPUT> {
|
786
|
+
/**
|
787
|
+
ID of the tool call. This ID is used to match the tool call with the tool result.
|
788
|
+
*/
|
789
|
+
toolCallId: string;
|
790
|
+
/**
|
791
|
+
Name of the tool that is being called.
|
792
|
+
*/
|
793
|
+
toolName: NAME;
|
794
|
+
/**
|
795
|
+
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
796
|
+
*/
|
797
|
+
input: INPUT;
|
798
|
+
/**
|
799
|
+
* Whether the tool call will be executed by the provider.
|
800
|
+
* If this flag is not set or is false, the tool call will be executed by the client.
|
801
|
+
*/
|
802
|
+
providerExecuted?: boolean;
|
803
|
+
}
|
804
|
+
|
805
|
+
/**
|
806
|
+
Typed tool result that is returned by `generateText` and `streamText`.
|
807
|
+
It contains the tool call ID, the tool name, the tool arguments, and the tool result.
|
808
|
+
*/
|
809
|
+
interface ToolResult<NAME extends string, INPUT, OUTPUT> {
|
810
|
+
/**
|
811
|
+
ID of the tool call. This ID is used to match the tool call with the tool result.
|
812
|
+
*/
|
813
|
+
toolCallId: string;
|
814
|
+
/**
|
815
|
+
Name of the tool that was called.
|
816
|
+
*/
|
817
|
+
toolName: NAME;
|
818
|
+
/**
|
819
|
+
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
820
|
+
*/
|
821
|
+
input: INPUT;
|
822
|
+
/**
|
823
|
+
Result of the tool call. This is the result of the tool's execution.
|
824
|
+
*/
|
825
|
+
output: OUTPUT;
|
826
|
+
}
|
827
|
+
|
828
|
+
export { type AssistantContent, type AssistantModelMessage, type DataContent, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, type ValidationResult, type Validator, asSchema, asValidator, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, extractResponseHeaders, generateId, getErrorMessage, getFromApi, isAbortError, isParsableJson, isUrlSupported, isValidator, jsonSchema, loadApiKey, loadOptionalSetting, loadSetting, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateTypes, validator, validatorSymbol, withoutTrailingSlash, zodSchema };
|