ai 5.0.0-canary.3 → 5.0.0-canary.4

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.
@@ -0,0 +1,592 @@
1
+ import { Schema, Message } from '@ai-sdk/ui-utils';
2
+ import { z } from 'zod';
3
+ import { LanguageModelV2ProviderMetadata, LanguageModelV2FunctionTool, LanguageModelV2ProviderDefinedTool, LanguageModelV2ToolChoice, LanguageModelV2Prompt } from '@ai-sdk/provider';
4
+
5
+ type ToolResultContent = Array<{
6
+ type: 'text';
7
+ text: string;
8
+ } | {
9
+ type: 'image';
10
+ data: string;
11
+ mediaType?: string;
12
+ /**
13
+ * @deprecated Use `mediaType` instead.
14
+ */
15
+ mimeType?: string;
16
+ }>;
17
+
18
+ /**
19
+ Tool choice for the generation. It supports the following settings:
20
+
21
+ - `auto` (default): the model can choose whether and which tools to call.
22
+ - `required`: the model must call a tool. It can choose which tool to call.
23
+ - `none`: the model must not call tools
24
+ - `{ type: 'tool', toolName: string (typed) }`: the model must call the specified tool
25
+ */
26
+ type ToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'required' | {
27
+ type: 'tool';
28
+ toolName: keyof TOOLS;
29
+ };
30
+
31
+ /**
32
+ Additional provider-specific metadata that is returned from the provider.
33
+
34
+ This is needed to enable provider-specific functionality that can be
35
+ fully encapsulated in the provider.
36
+ */
37
+ type ProviderMetadata = LanguageModelV2ProviderMetadata;
38
+ /**
39
+ Additional provider-specific options.
40
+
41
+ They are passed through to the provider from the AI SDK and enable
42
+ provider-specific functionality that can be fully encapsulated in the provider.
43
+ */
44
+ type ProviderOptions = LanguageModelV2ProviderMetadata;
45
+
46
+ /**
47
+ Represents the number of tokens used in a prompt and completion.
48
+ */
49
+ type LanguageModelUsage = {
50
+ /**
51
+ The number of tokens used in the prompt.
52
+ */
53
+ promptTokens: number;
54
+ /**
55
+ The number of tokens used in the completion.
56
+ */
57
+ completionTokens: number;
58
+ /**
59
+ The total number of tokens used (promptTokens + completionTokens).
60
+ */
61
+ totalTokens: number;
62
+ };
63
+ declare function calculateLanguageModelUsage({ promptTokens, completionTokens, }: {
64
+ promptTokens: number;
65
+ completionTokens: number;
66
+ }): LanguageModelUsage;
67
+
68
+ /**
69
+ Data content. Can either be a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer.
70
+ */
71
+ type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
72
+
73
+ /**
74
+ Text content part of a prompt. It contains a string of text.
75
+ */
76
+ interface TextPart {
77
+ type: 'text';
78
+ /**
79
+ The text content.
80
+ */
81
+ text: string;
82
+ /**
83
+ Additional provider-specific metadata. They are passed through
84
+ to the provider from the AI SDK and enable provider-specific
85
+ functionality that can be fully encapsulated in the provider.
86
+ */
87
+ providerOptions?: ProviderOptions;
88
+ /**
89
+ @deprecated Use `providerOptions` instead.
90
+ */
91
+ experimental_providerMetadata?: ProviderMetadata;
92
+ }
93
+ /**
94
+ Image content part of a prompt. It contains an image.
95
+ */
96
+ interface ImagePart {
97
+ type: 'image';
98
+ /**
99
+ Image data. Can either be:
100
+
101
+ - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
102
+ - URL: a URL that points to the image
103
+ */
104
+ image: DataContent | URL;
105
+ /**
106
+ Optional IANA media type of the image.
107
+
108
+ @see https://www.iana.org/assignments/media-types/media-types.xhtml
109
+ */
110
+ mediaType?: string;
111
+ /**
112
+ @deprecated Use `mediaType` instead.
113
+ */
114
+ mimeType?: string;
115
+ /**
116
+ Additional provider-specific metadata. They are passed through
117
+ to the provider from the AI SDK and enable provider-specific
118
+ functionality that can be fully encapsulated in the provider.
119
+ */
120
+ providerOptions?: ProviderOptions;
121
+ /**
122
+ @deprecated Use `providerOptions` instead.
123
+ */
124
+ experimental_providerMetadata?: ProviderMetadata;
125
+ }
126
+ /**
127
+ File content part of a prompt. It contains a file.
128
+ */
129
+ interface FilePart {
130
+ type: 'file';
131
+ /**
132
+ File data. Can either be:
133
+
134
+ - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
135
+ - URL: a URL that points to the image
136
+ */
137
+ data: DataContent | URL;
138
+ /**
139
+ Optional filename of the file.
140
+ */
141
+ filename?: string;
142
+ /**
143
+ IANA media type of the file.
144
+
145
+ @see https://www.iana.org/assignments/media-types/media-types.xhtml
146
+ */
147
+ mediaType: string;
148
+ /**
149
+ @deprecated Use `mediaType` instead.
150
+ */
151
+ mimeType?: string;
152
+ /**
153
+ Additional provider-specific metadata. They are passed through
154
+ to the provider from the AI SDK and enable provider-specific
155
+ functionality that can be fully encapsulated in the provider.
156
+ */
157
+ providerOptions?: ProviderOptions;
158
+ /**
159
+ @deprecated Use `providerOptions` instead.
160
+ */
161
+ experimental_providerMetadata?: ProviderMetadata;
162
+ }
163
+ /**
164
+ * Reasoning content part of a prompt. It contains a reasoning.
165
+ */
166
+ interface ReasoningPart {
167
+ type: 'reasoning';
168
+ /**
169
+ The reasoning text.
170
+ */
171
+ text: string;
172
+ /**
173
+ An optional signature for verifying that the reasoning originated from the model.
174
+ */
175
+ signature?: string;
176
+ /**
177
+ Additional provider-specific metadata. They are passed through
178
+ to the provider from the AI SDK and enable provider-specific
179
+ functionality that can be fully encapsulated in the provider.
180
+ */
181
+ providerOptions?: ProviderOptions;
182
+ /**
183
+ @deprecated Use `providerOptions` instead.
184
+ */
185
+ experimental_providerMetadata?: ProviderMetadata;
186
+ }
187
+ /**
188
+ Redacted reasoning content part of a prompt.
189
+ */
190
+ interface RedactedReasoningPart {
191
+ type: 'redacted-reasoning';
192
+ /**
193
+ Redacted reasoning data.
194
+ */
195
+ data: string;
196
+ /**
197
+ Additional provider-specific metadata. They are passed through
198
+ to the provider from the AI SDK and enable provider-specific
199
+ functionality that can be fully encapsulated in the provider.
200
+ */
201
+ providerOptions?: ProviderOptions;
202
+ /**
203
+ @deprecated Use `providerOptions` instead.
204
+ */
205
+ experimental_providerMetadata?: ProviderMetadata;
206
+ }
207
+ /**
208
+ Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
209
+ */
210
+ interface ToolCallPart {
211
+ type: 'tool-call';
212
+ /**
213
+ ID of the tool call. This ID is used to match the tool call with the tool result.
214
+ */
215
+ toolCallId: string;
216
+ /**
217
+ Name of the tool that is being called.
218
+ */
219
+ toolName: string;
220
+ /**
221
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
222
+ */
223
+ args: unknown;
224
+ /**
225
+ Additional provider-specific metadata. They are passed through
226
+ to the provider from the AI SDK and enable provider-specific
227
+ functionality that can be fully encapsulated in the provider.
228
+ */
229
+ providerOptions?: ProviderOptions;
230
+ /**
231
+ @deprecated Use `providerOptions` instead.
232
+ */
233
+ experimental_providerMetadata?: ProviderMetadata;
234
+ }
235
+ /**
236
+ Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
237
+ */
238
+ interface ToolResultPart {
239
+ type: 'tool-result';
240
+ /**
241
+ ID of the tool call that this result is associated with.
242
+ */
243
+ toolCallId: string;
244
+ /**
245
+ Name of the tool that generated this result.
246
+ */
247
+ toolName: string;
248
+ /**
249
+ Result of the tool call. This is a JSON-serializable object.
250
+ */
251
+ result: unknown;
252
+ /**
253
+ Multi-part content of the tool result. Only for tools that support multipart results.
254
+ */
255
+ experimental_content?: ToolResultContent;
256
+ /**
257
+ Optional flag if the result is an error or an error message.
258
+ */
259
+ isError?: boolean;
260
+ /**
261
+ Additional provider-specific metadata. They are passed through
262
+ to the provider from the AI SDK and enable provider-specific
263
+ functionality that can be fully encapsulated in the provider.
264
+ */
265
+ providerOptions?: ProviderOptions;
266
+ /**
267
+ @deprecated Use `providerOptions` instead.
268
+ */
269
+ experimental_providerMetadata?: ProviderMetadata;
270
+ }
271
+
272
+ /**
273
+ A system message. It can contain system information.
274
+
275
+ Note: using the "system" part of the prompt is strongly preferred
276
+ to increase the resilience against prompt injection attacks,
277
+ and because not all providers support several system messages.
278
+ */
279
+ type CoreSystemMessage = {
280
+ role: 'system';
281
+ content: string;
282
+ /**
283
+ Additional provider-specific metadata. They are passed through
284
+ to the provider from the AI SDK and enable provider-specific
285
+ functionality that can be fully encapsulated in the provider.
286
+ */
287
+ providerOptions?: ProviderOptions;
288
+ /**
289
+ @deprecated Use `providerOptions` instead.
290
+ */
291
+ experimental_providerMetadata?: ProviderMetadata;
292
+ };
293
+ /**
294
+ A user message. It can contain text or a combination of text and images.
295
+ */
296
+ type CoreUserMessage = {
297
+ role: 'user';
298
+ content: UserContent;
299
+ /**
300
+ Additional provider-specific metadata. They are passed through
301
+ to the provider from the AI SDK and enable provider-specific
302
+ functionality that can be fully encapsulated in the provider.
303
+ */
304
+ providerOptions?: ProviderOptions;
305
+ /**
306
+ @deprecated Use `providerOptions` instead.
307
+ */
308
+ experimental_providerMetadata?: ProviderMetadata;
309
+ };
310
+ /**
311
+ Content of a user message. It can be a string or an array of text and image parts.
312
+ */
313
+ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
314
+ /**
315
+ An assistant message. It can contain text, tool calls, or a combination of text and tool calls.
316
+ */
317
+ type CoreAssistantMessage = {
318
+ role: 'assistant';
319
+ content: AssistantContent;
320
+ /**
321
+ Additional provider-specific metadata. They are passed through
322
+ to the provider from the AI SDK and enable provider-specific
323
+ functionality that can be fully encapsulated in the provider.
324
+ */
325
+ providerOptions?: ProviderOptions;
326
+ /**
327
+ @deprecated Use `providerOptions` instead.
328
+ */
329
+ experimental_providerMetadata?: ProviderMetadata;
330
+ };
331
+ /**
332
+ Content of an assistant message.
333
+ It can be a string or an array of text, image, reasoning, redacted reasoning, and tool call parts.
334
+ */
335
+ type AssistantContent = string | Array<TextPart | FilePart | ReasoningPart | RedactedReasoningPart | ToolCallPart>;
336
+ /**
337
+ A tool message. It contains the result of one or more tool calls.
338
+ */
339
+ type CoreToolMessage = {
340
+ role: 'tool';
341
+ content: ToolContent;
342
+ /**
343
+ Additional provider-specific metadata. They are passed through
344
+ to the provider from the AI SDK and enable provider-specific
345
+ functionality that can be fully encapsulated in the provider.
346
+ */
347
+ providerOptions?: ProviderOptions;
348
+ /**
349
+ @deprecated Use `providerOptions` instead.
350
+ */
351
+ experimental_providerMetadata?: ProviderMetadata;
352
+ };
353
+ /**
354
+ Content of a tool message. It is an array of tool result parts.
355
+ */
356
+ type ToolContent = Array<ToolResultPart>;
357
+ /**
358
+ A message that can be used in the `messages` field of a prompt.
359
+ It can be a user message, an assistant message, or a tool message.
360
+ */
361
+ type CoreMessage = CoreSystemMessage | CoreUserMessage | CoreAssistantMessage | CoreToolMessage;
362
+
363
+ type ToolParameters = z.ZodTypeAny | Schema<any>;
364
+ type inferParameters<PARAMETERS extends ToolParameters> = PARAMETERS extends Schema<any> ? PARAMETERS['_type'] : PARAMETERS extends z.ZodTypeAny ? z.infer<PARAMETERS> : never;
365
+ interface ToolExecutionOptions {
366
+ /**
367
+ * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
368
+ */
369
+ toolCallId: string;
370
+ /**
371
+ * Messages that were sent to the language model to initiate the response that contained the tool call.
372
+ * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
373
+ */
374
+ messages: CoreMessage[];
375
+ /**
376
+ * An optional abort signal that indicates that the overall operation should be aborted.
377
+ */
378
+ abortSignal?: AbortSignal;
379
+ }
380
+ /**
381
+ A tool contains the description and the schema of the input that the tool expects.
382
+ This enables the language model to generate the input.
383
+
384
+ The tool can also contain an optional execute function for the actual execution function of the tool.
385
+ */
386
+ type Tool<PARAMETERS extends ToolParameters = any, RESULT = any> = {
387
+ /**
388
+ The schema of the input that the tool expects. The language model will use this to generate the input.
389
+ It is also used to validate the output of the language model.
390
+ Use descriptions to make the input understandable for the language model.
391
+ */
392
+ parameters: PARAMETERS;
393
+ /**
394
+ An optional description of what the tool does.
395
+ Will be used by the language model to decide whether to use the tool.
396
+ Not used for provider-defined tools.
397
+ */
398
+ description?: string;
399
+ /**
400
+ Optional conversion function that maps the tool result to multi-part tool content for LLMs.
401
+ */
402
+ experimental_toToolResultContent?: (result: RESULT) => ToolResultContent;
403
+ /**
404
+ An async function that is called with the arguments from the tool call and produces a result.
405
+ If not provided, the tool will not be executed automatically.
406
+
407
+ @args is the input of the tool call.
408
+ @options.abortSignal is a signal that can be used to abort the tool call.
409
+ */
410
+ execute?: (args: inferParameters<PARAMETERS>, options: ToolExecutionOptions) => PromiseLike<RESULT>;
411
+ } & ({
412
+ /**
413
+ Function tool.
414
+ */
415
+ type?: undefined | 'function';
416
+ } | {
417
+ /**
418
+ Provider-defined tool.
419
+ */
420
+ type: 'provider-defined';
421
+ /**
422
+ The ID of the tool. Should follow the format `<provider-name>.<tool-name>`.
423
+ */
424
+ id: `${string}.${string}`;
425
+ /**
426
+ The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
427
+ */
428
+ args: Record<string, unknown>;
429
+ });
430
+
431
+ type ToolSet = Record<string, Tool>;
432
+
433
+ /**
434
+ Prompt part of the AI function options.
435
+ It contains a system message, a simple text prompt, or a list of messages.
436
+ */
437
+ type Prompt = {
438
+ /**
439
+ System message to include in the prompt. Can be used with `prompt` or `messages`.
440
+ */
441
+ system?: string;
442
+ /**
443
+ A simple text prompt. You can either use `prompt` or `messages` but not both.
444
+ */
445
+ prompt?: string;
446
+ /**
447
+ A list of messages. You can either use `prompt` or `messages` but not both.
448
+ */
449
+ messages?: Array<CoreMessage> | Array<Omit<Message, 'id'>>;
450
+ };
451
+
452
+ type StandardizedPrompt = {
453
+ /**
454
+ * Original prompt type. This is forwarded to the providers and can be used
455
+ * to write send raw text to providers that support it.
456
+ */
457
+ type: 'prompt' | 'messages';
458
+ /**
459
+ * System message.
460
+ */
461
+ system?: string;
462
+ /**
463
+ * Messages.
464
+ */
465
+ messages: CoreMessage[];
466
+ };
467
+ declare function standardizePrompt<TOOLS extends ToolSet>({ prompt, tools, }: {
468
+ prompt: Prompt;
469
+ tools: undefined | TOOLS;
470
+ }): StandardizedPrompt;
471
+
472
+ type CallSettings = {
473
+ /**
474
+ Maximum number of tokens to generate.
475
+ */
476
+ maxTokens?: number;
477
+ /**
478
+ Temperature setting. This is a number between 0 (almost no randomness) and
479
+ 1 (very random).
480
+
481
+ It is recommended to set either `temperature` or `topP`, but not both.
482
+
483
+ @default 0
484
+ */
485
+ temperature?: number;
486
+ /**
487
+ Nucleus sampling. This is a number between 0 and 1.
488
+
489
+ E.g. 0.1 would mean that only tokens with the top 10% probability mass
490
+ are considered.
491
+
492
+ It is recommended to set either `temperature` or `topP`, but not both.
493
+ */
494
+ topP?: number;
495
+ /**
496
+ Only sample from the top K options for each subsequent token.
497
+
498
+ Used to remove "long tail" low probability responses.
499
+ Recommended for advanced use cases only. You usually only need to use temperature.
500
+ */
501
+ topK?: number;
502
+ /**
503
+ Presence penalty setting. It affects the likelihood of the model to
504
+ repeat information that is already in the prompt.
505
+
506
+ The presence penalty is a number between -1 (increase repetition)
507
+ and 1 (maximum penalty, decrease repetition). 0 means no penalty.
508
+ */
509
+ presencePenalty?: number;
510
+ /**
511
+ Frequency penalty setting. It affects the likelihood of the model
512
+ to repeatedly use the same words or phrases.
513
+
514
+ The frequency penalty is a number between -1 (increase repetition)
515
+ and 1 (maximum penalty, decrease repetition). 0 means no penalty.
516
+ */
517
+ frequencyPenalty?: number;
518
+ /**
519
+ Stop sequences.
520
+ If set, the model will stop generating text when one of the stop sequences is generated.
521
+ Providers may have limits on the number of stop sequences.
522
+ */
523
+ stopSequences?: string[];
524
+ /**
525
+ The seed (integer) to use for random sampling. If set and supported
526
+ by the model, calls will generate deterministic results.
527
+ */
528
+ seed?: number;
529
+ /**
530
+ Maximum number of retries. Set to 0 to disable retries.
531
+
532
+ @default 2
533
+ */
534
+ maxRetries?: number;
535
+ /**
536
+ Abort signal.
537
+ */
538
+ abortSignal?: AbortSignal;
539
+ /**
540
+ Additional HTTP headers to be sent with the request.
541
+ Only applicable for HTTP-based providers.
542
+ */
543
+ headers?: Record<string, string | undefined>;
544
+ };
545
+
546
+ declare function prepareToolsAndToolChoice<TOOLS extends ToolSet>({ tools, toolChoice, activeTools, }: {
547
+ tools: TOOLS | undefined;
548
+ toolChoice: ToolChoice<TOOLS> | undefined;
549
+ activeTools: Array<keyof TOOLS> | undefined;
550
+ }): {
551
+ tools: Array<LanguageModelV2FunctionTool | LanguageModelV2ProviderDefinedTool> | undefined;
552
+ toolChoice: LanguageModelV2ToolChoice | undefined;
553
+ };
554
+
555
+ type RetryFunction = <OUTPUT>(fn: () => PromiseLike<OUTPUT>) => PromiseLike<OUTPUT>;
556
+
557
+ /**
558
+ * Validate and prepare retries.
559
+ */
560
+ declare function prepareRetries({ maxRetries, }: {
561
+ maxRetries: number | undefined;
562
+ }): {
563
+ maxRetries: number;
564
+ retry: RetryFunction;
565
+ };
566
+
567
+ /**
568
+ * Validates call settings and sets default values.
569
+ */
570
+ declare function prepareCallSettings({ maxTokens, temperature, topP, topK, presencePenalty, frequencyPenalty, stopSequences, seed, }: Omit<CallSettings, 'abortSignal' | 'headers' | 'maxRetries'>): Omit<CallSettings, 'abortSignal' | 'headers' | 'maxRetries'>;
571
+
572
+ declare function download({ url }: {
573
+ url: URL;
574
+ }): Promise<{
575
+ data: Uint8Array;
576
+ mediaType: string | undefined;
577
+ }>;
578
+
579
+ declare function convertToLanguageModelPrompt({ prompt, modelSupportsImageUrls, modelSupportsUrl, downloadImplementation, }: {
580
+ prompt: StandardizedPrompt;
581
+ modelSupportsImageUrls: boolean | undefined;
582
+ modelSupportsUrl: undefined | ((url: URL) => boolean);
583
+ downloadImplementation?: typeof download;
584
+ }): Promise<LanguageModelV2Prompt>;
585
+
586
+ /**
587
+ * Warning time for notifying developers that a stream is hanging in dev mode
588
+ * using a console.warn.
589
+ */
590
+ declare const HANGING_STREAM_WARNING_TIME_MS: number;
591
+
592
+ export { HANGING_STREAM_WARNING_TIME_MS, calculateLanguageModelUsage, convertToLanguageModelPrompt, prepareCallSettings, prepareRetries, prepareToolsAndToolChoice, standardizePrompt };