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