ai 5.0.0-canary.3 → 5.0.0-canary.5

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.
@@ -1,244 +1,246 @@
1
- import { LanguageModelV2FinishReason, LanguageModelV2CallWarning, LanguageModelV2ProviderMetadata, LanguageModelV2 } from '@ai-sdk/provider';
2
- import { ReactNode } from 'react';
3
1
  import { z } from 'zod';
4
- import { Message } from '@ai-sdk/ui-utils';
2
+ import { ToolCall, ToolResult, Validator } from '@ai-sdk/provider-utils';
3
+ import { JSONSchema7 } from 'json-schema';
4
+ import { LanguageModelV2ProviderMetadata, LanguageModelV2Source, LanguageModelV2FunctionTool, LanguageModelV2ProviderDefinedTool, LanguageModelV2ToolChoice, LanguageModelV2Prompt } from '@ai-sdk/provider';
5
5
 
6
- type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
7
- type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
8
- type AIProviderProps<AIState = any, UIState = any, Actions = any> = {
9
- children: React.ReactNode;
10
- initialAIState?: AIState;
11
- initialUIState?: UIState;
12
- /** $ActionTypes is only added for type inference and is never used at runtime **/
13
- $ActionTypes?: Actions;
14
- };
15
- type AIProvider<AIState = any, UIState = any, Actions = any> = (props: AIProviderProps<AIState, UIState, Actions>) => Promise<React.ReactElement>;
16
- type InferAIState<T, Fallback> = T extends AIProvider<infer AIState, any, any> ? AIState : Fallback;
17
- type OnSetAIState<S> = ({ key, state, done, }: {
18
- key: string | number | symbol | undefined;
19
- state: S;
20
- done: boolean;
21
- }) => void | Promise<void>;
22
- type OnGetUIState<S> = AIAction<void, S | undefined>;
23
- type ValueOrUpdater<T> = T | ((current: T) => T);
24
- type MutableAIState<AIState> = {
25
- get: () => AIState;
26
- update: (newState: ValueOrUpdater<AIState>) => void;
27
- done: ((newState: AIState) => void) | (() => void);
6
+ /**
7
+ Tool choice for the generation. It supports the following settings:
8
+
9
+ - `auto` (default): the model can choose whether and which tools to call.
10
+ - `required`: the model must call a tool. It can choose which tool to call.
11
+ - `none`: the model must not call tools
12
+ - `{ type: 'tool', toolName: string (typed) }`: the model must call the specified tool
13
+ */
14
+ type ToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'required' | {
15
+ type: 'tool';
16
+ toolName: keyof TOOLS;
28
17
  };
29
18
 
30
19
  /**
31
- * Get the current AI state.
32
- * If `key` is provided, it will return the value of the specified key in the
33
- * AI state, if it's an object. If it's not an object, it will throw an error.
34
- *
35
- * @example const state = getAIState() // Get the entire AI state
36
- * @example const field = getAIState('key') // Get the value of the key
37
- */
38
- declare function getAIState<AI extends AIProvider = any>(): Readonly<InferAIState<AI, any>>;
39
- declare function getAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): Readonly<InferAIState<AI, any>[typeof key]>;
40
- /**
41
- * Get the mutable AI state. Note that you must call `.done()` when finishing
42
- * updating the AI state.
43
- *
44
- * @example
45
- * ```tsx
46
- * const state = getMutableAIState()
47
- * state.update({ ...state.get(), key: 'value' })
48
- * state.update((currentState) => ({ ...currentState, key: 'value' }))
49
- * state.done()
50
- * ```
51
- *
52
- * @example
53
- * ```tsx
54
- * const state = getMutableAIState()
55
- * state.done({ ...state.get(), key: 'value' }) // Done with a new state
56
- * ```
57
- */
58
- declare function getMutableAIState<AI extends AIProvider = any>(): MutableAIState<InferAIState<AI, any>>;
59
- declare function getMutableAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): MutableAIState<InferAIState<AI, any>[typeof key]>;
20
+ Additional provider-specific metadata that is returned from the provider.
21
+
22
+ This is needed to enable provider-specific functionality that can be
23
+ fully encapsulated in the provider.
24
+ */
25
+ type ProviderMetadata = LanguageModelV2ProviderMetadata;
26
+ /**
27
+ Additional provider-specific options.
60
28
 
61
- declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, onSetAIState, onGetUIState, }: {
62
- actions: Actions;
63
- initialAIState?: AIState;
64
- initialUIState?: UIState;
29
+ They are passed through to the provider from the AI SDK and enable
30
+ provider-specific functionality that can be fully encapsulated in the provider.
31
+ */
32
+ type ProviderOptions = LanguageModelV2ProviderMetadata;
33
+
34
+ /**
35
+ Represents the number of tokens used in a prompt and completion.
36
+ */
37
+ type LanguageModelUsage = {
65
38
  /**
66
- * This function is called whenever the AI state is updated by an Action.
67
- * You can use this to persist the AI state to a database, or to send it to a
68
- * logging service.
39
+ The number of tokens used in the prompt.
69
40
  */
70
- onSetAIState?: OnSetAIState<AIState>;
41
+ promptTokens: number;
71
42
  /**
72
- * This function is used to retrieve the UI state based on the AI state.
73
- * For example, to render the initial UI state based on a given AI state, or
74
- * to sync the UI state when the application is already loaded.
75
- *
76
- * If returning `undefined`, the client side UI state will not be updated.
77
- *
78
- * This function must be annotated with the `"use server"` directive.
79
- *
80
- * @example
81
- * ```tsx
82
- * onGetUIState: async () => {
83
- * 'use server';
84
- *
85
- * const currentAIState = getAIState();
86
- * const externalAIState = await loadAIStateFromDatabase();
87
- *
88
- * if (currentAIState === externalAIState) return undefined;
89
- *
90
- * // Update current AI state and return the new UI state
91
- * const state = getMutableAIState()
92
- * state.done(externalAIState)
93
- *
94
- * return <div>...</div>;
95
- * }
96
- * ```
43
+ The number of tokens used in the completion.
44
+ */
45
+ completionTokens: number;
46
+ /**
47
+ The total number of tokens used (promptTokens + completionTokens).
97
48
  */
98
- onGetUIState?: OnGetUIState<UIState>;
99
- }): AIProvider<AIState, UIState, Actions>;
49
+ totalTokens: number;
50
+ };
51
+ declare function calculateLanguageModelUsage({ promptTokens, completionTokens, }: {
52
+ promptTokens: number;
53
+ completionTokens: number;
54
+ }): LanguageModelUsage;
100
55
 
101
- type CallSettings = {
56
+ /**
57
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
58
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
59
+ Once the call is complete, the invocation is a tool result.
60
+
61
+ The step is used to track how to map an assistant UI message with many tool invocations
62
+ back to a sequence of LLM assistant/tool result message pairs.
63
+ It is optional for backwards compatibility.
64
+ */
65
+ type ToolInvocation = ({
66
+ state: 'partial-call';
67
+ step?: number;
68
+ } & ToolCall<string, any>) | ({
69
+ state: 'call';
70
+ step?: number;
71
+ } & ToolCall<string, any>) | ({
72
+ state: 'result';
73
+ step?: number;
74
+ } & ToolResult<string, any, any>);
75
+ /**
76
+ * An attachment that can be sent along with a message.
77
+ */
78
+ interface Attachment {
102
79
  /**
103
- Maximum number of tokens to generate.
80
+ * The name of the attachment, usually the file name.
104
81
  */
105
- maxTokens?: number;
82
+ name?: string;
106
83
  /**
107
- Temperature setting. This is a number between 0 (almost no randomness) and
108
- 1 (very random).
109
-
110
- It is recommended to set either `temperature` or `topP`, but not both.
111
-
112
- @default 0
84
+ * A string indicating the [media type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type).
85
+ * By default, it's extracted from the pathname's extension.
113
86
  */
114
- temperature?: number;
87
+ contentType?: string;
115
88
  /**
116
- Nucleus sampling. This is a number between 0 and 1.
117
-
118
- E.g. 0.1 would mean that only tokens with the top 10% probability mass
119
- are considered.
120
-
121
- It is recommended to set either `temperature` or `topP`, but not both.
89
+ * The URL of the attachment. It can either be a URL to a hosted file or a [Data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs).
122
90
  */
123
- topP?: number;
91
+ url: string;
92
+ }
93
+ /**
94
+ * AI SDK UI Messages. They are used in the client and to communicate between the frontend and the API routes.
95
+ */
96
+ interface Message {
124
97
  /**
125
- Only sample from the top K options for each subsequent token.
126
-
127
- Used to remove "long tail" low probability responses.
128
- Recommended for advanced use cases only. You usually only need to use temperature.
98
+ A unique identifier for the message.
129
99
  */
130
- topK?: number;
100
+ id: string;
131
101
  /**
132
- Presence penalty setting. It affects the likelihood of the model to
133
- repeat information that is already in the prompt.
134
-
135
- The presence penalty is a number between -1 (increase repetition)
136
- and 1 (maximum penalty, decrease repetition). 0 means no penalty.
102
+ The timestamp of the message.
137
103
  */
138
- presencePenalty?: number;
104
+ createdAt?: Date;
139
105
  /**
140
- Frequency penalty setting. It affects the likelihood of the model
141
- to repeatedly use the same words or phrases.
106
+ Text content of the message. Use parts when possible.
107
+ */
108
+ content: string;
109
+ /**
110
+ Reasoning for the message.
142
111
 
143
- The frequency penalty is a number between -1 (increase repetition)
144
- and 1 (maximum penalty, decrease repetition). 0 means no penalty.
112
+ @deprecated Use `parts` instead.
145
113
  */
146
- frequencyPenalty?: number;
114
+ reasoning?: string;
147
115
  /**
148
- Stop sequences.
149
- If set, the model will stop generating text when one of the stop sequences is generated.
150
- Providers may have limits on the number of stop sequences.
116
+ * Additional attachments to be sent along with the message.
151
117
  */
152
- stopSequences?: string[];
118
+ experimental_attachments?: Attachment[];
153
119
  /**
154
- The seed (integer) to use for random sampling. If set and supported
155
- by the model, calls will generate deterministic results.
120
+ The 'data' role is deprecated.
156
121
  */
157
- seed?: number;
122
+ role: 'system' | 'user' | 'assistant' | 'data';
158
123
  /**
159
- Maximum number of retries. Set to 0 to disable retries.
124
+ For data messages.
160
125
 
161
- @default 2
126
+ @deprecated Data messages will be removed.
162
127
  */
163
- maxRetries?: number;
128
+ data?: JSONValue;
164
129
  /**
165
- Abort signal.
130
+ * Additional message-specific information added on the server via StreamData
166
131
  */
167
- abortSignal?: AbortSignal;
132
+ annotations?: JSONValue[] | undefined;
168
133
  /**
169
- Additional HTTP headers to be sent with the request.
170
- Only applicable for HTTP-based providers.
134
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
135
+ that the assistant made as part of this message.
136
+
137
+ @deprecated Use `parts` instead.
171
138
  */
172
- headers?: Record<string, string | undefined>;
173
- };
174
-
175
- /**
176
- Reason why a language model finished generating a response.
177
-
178
- Can be one of the following:
179
- - `stop`: model generated stop sequence
180
- - `length`: model generated maximum number of tokens
181
- - `content-filter`: content filter violation stopped the model
182
- - `tool-calls`: model triggered tool calls
183
- - `error`: model stopped because of an error
184
- - `other`: model stopped for other reasons
185
- */
186
- type FinishReason = LanguageModelV2FinishReason;
187
- /**
188
- Warning from the model provider for this call. The call will proceed, but e.g.
189
- some settings might not be supported, which can lead to suboptimal results.
190
- */
191
- type CallWarning = LanguageModelV2CallWarning;
139
+ toolInvocations?: Array<ToolInvocation>;
140
+ /**
141
+ * The parts of the message. Use this for rendering the message in the UI.
142
+ *
143
+ * Assistant messages can have text, reasoning and tool invocation parts.
144
+ * User messages can have text parts.
145
+ */
146
+ parts?: Array<TextUIPart | ReasoningUIPart | ToolInvocationUIPart | SourceUIPart | FileUIPart | StepStartUIPart>;
147
+ }
192
148
  /**
193
- Tool choice for the generation. It supports the following settings:
194
-
195
- - `auto` (default): the model can choose whether and which tools to call.
196
- - `required`: the model must call a tool. It can choose which tool to call.
197
- - `none`: the model must not call tools
198
- - `{ type: 'tool', toolName: string (typed) }`: the model must call the specified tool
149
+ * A text part of a message.
199
150
  */
200
- type ToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'required' | {
201
- type: 'tool';
202
- toolName: keyof TOOLS;
151
+ type TextUIPart = {
152
+ type: 'text';
153
+ /**
154
+ * The text content.
155
+ */
156
+ text: string;
203
157
  };
204
-
205
158
  /**
206
- Additional provider-specific metadata that is returned from the provider.
207
-
208
- This is needed to enable provider-specific functionality that can be
209
- fully encapsulated in the provider.
159
+ * A reasoning part of a message.
210
160
  */
211
- type ProviderMetadata = LanguageModelV2ProviderMetadata;
161
+ type ReasoningUIPart = {
162
+ type: 'reasoning';
163
+ /**
164
+ * The reasoning text.
165
+ */
166
+ reasoning: string;
167
+ details: Array<{
168
+ type: 'text';
169
+ text: string;
170
+ signature?: string;
171
+ } | {
172
+ type: 'redacted';
173
+ data: string;
174
+ }>;
175
+ };
212
176
  /**
213
- Additional provider-specific options.
214
-
215
- They are passed through to the provider from the AI SDK and enable
216
- provider-specific functionality that can be fully encapsulated in the provider.
177
+ * A tool invocation part of a message.
217
178
  */
218
- type ProviderOptions = LanguageModelV2ProviderMetadata;
219
-
179
+ type ToolInvocationUIPart = {
180
+ type: 'tool-invocation';
181
+ /**
182
+ * The tool invocation.
183
+ */
184
+ toolInvocation: ToolInvocation;
185
+ };
220
186
  /**
221
- Represents the number of tokens used in a prompt and completion.
187
+ * A source part of a message.
222
188
  */
223
- type LanguageModelUsage = {
189
+ type SourceUIPart = {
190
+ type: 'source';
224
191
  /**
225
- The number of tokens used in the prompt.
192
+ * The source.
226
193
  */
227
- promptTokens: number;
194
+ source: LanguageModelV2Source;
195
+ };
196
+ /**
197
+ * A file part of a message.
198
+ */
199
+ type FileUIPart = {
200
+ type: 'file';
228
201
  /**
229
- The number of tokens used in the completion.
230
- */
231
- completionTokens: number;
202
+ * IANA media type of the file.
203
+ *
204
+ * @see https://www.iana.org/assignments/media-types/media-types.xhtml
205
+ */
206
+ mediaType: string;
232
207
  /**
233
- The total number of tokens used (promptTokens + completionTokens).
208
+ * The base64 encoded data.
234
209
  */
235
- totalTokens: number;
210
+ data: string;
211
+ };
212
+ /**
213
+ * A step boundary part of a message.
214
+ */
215
+ type StepStartUIPart = {
216
+ type: 'step-start';
236
217
  };
218
+ /**
219
+ A JSON value can be a string, number, boolean, object, array, or null.
220
+ JSON values can be serialized and deserialized by the JSON.stringify and JSON.parse methods.
221
+ */
222
+ type JSONValue = null | string | number | boolean | {
223
+ [value: string]: JSONValue;
224
+ } | Array<JSONValue>;
237
225
 
238
226
  /**
239
- Data content. Can either be a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer.
227
+ * Used to mark schemas so we can support both Zod and custom schemas.
240
228
  */
241
- type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
229
+ declare const schemaSymbol: unique symbol;
230
+ type Schema<OBJECT = unknown> = Validator<OBJECT> & {
231
+ /**
232
+ * Used to mark schemas so we can support both Zod and custom schemas.
233
+ */
234
+ [schemaSymbol]: true;
235
+ /**
236
+ * Schema type for inference.
237
+ */
238
+ _type: OBJECT;
239
+ /**
240
+ * The JSON Schema for the schema. It is passed to the providers.
241
+ */
242
+ readonly jsonSchema: JSONSchema7;
243
+ };
242
244
 
243
245
  type ToolResultContent = Array<{
244
246
  type: 'text';
@@ -246,9 +248,18 @@ type ToolResultContent = Array<{
246
248
  } | {
247
249
  type: 'image';
248
250
  data: string;
251
+ mediaType?: string;
252
+ /**
253
+ * @deprecated Use `mediaType` instead.
254
+ */
249
255
  mimeType?: string;
250
256
  }>;
251
257
 
258
+ /**
259
+ Data content. Can either be a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer.
260
+ */
261
+ type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
262
+
252
263
  /**
253
264
  Text content part of a prompt. It contains a string of text.
254
265
  */
@@ -282,7 +293,13 @@ interface ImagePart {
282
293
  */
283
294
  image: DataContent | URL;
284
295
  /**
285
- Optional mime type of the image.
296
+ Optional IANA media type of the image.
297
+
298
+ @see https://www.iana.org/assignments/media-types/media-types.xhtml
299
+ */
300
+ mediaType?: string;
301
+ /**
302
+ @deprecated Use `mediaType` instead.
286
303
  */
287
304
  mimeType?: string;
288
305
  /**
@@ -313,9 +330,15 @@ interface FilePart {
313
330
  */
314
331
  filename?: string;
315
332
  /**
316
- Mime type of the file.
333
+ IANA media type of the file.
334
+
335
+ @see https://www.iana.org/assignments/media-types/media-types.xhtml
317
336
  */
318
- mimeType: string;
337
+ mediaType: string;
338
+ /**
339
+ @deprecated Use `mediaType` instead.
340
+ */
341
+ mimeType?: string;
319
342
  /**
320
343
  Additional provider-specific metadata. They are passed through
321
344
  to the provider from the AI SDK and enable provider-specific
@@ -527,6 +550,76 @@ It can be a user message, an assistant message, or a tool message.
527
550
  */
528
551
  type CoreMessage = CoreSystemMessage | CoreUserMessage | CoreAssistantMessage | CoreToolMessage;
529
552
 
553
+ type ToolParameters = z.ZodTypeAny | Schema<any>;
554
+ type inferParameters<PARAMETERS extends ToolParameters> = PARAMETERS extends Schema<any> ? PARAMETERS['_type'] : PARAMETERS extends z.ZodTypeAny ? z.infer<PARAMETERS> : never;
555
+ interface ToolExecutionOptions {
556
+ /**
557
+ * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
558
+ */
559
+ toolCallId: string;
560
+ /**
561
+ * Messages that were sent to the language model to initiate the response that contained the tool call.
562
+ * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
563
+ */
564
+ messages: CoreMessage[];
565
+ /**
566
+ * An optional abort signal that indicates that the overall operation should be aborted.
567
+ */
568
+ abortSignal?: AbortSignal;
569
+ }
570
+ /**
571
+ A tool contains the description and the schema of the input that the tool expects.
572
+ This enables the language model to generate the input.
573
+
574
+ The tool can also contain an optional execute function for the actual execution function of the tool.
575
+ */
576
+ type Tool<PARAMETERS extends ToolParameters = any, RESULT = any> = {
577
+ /**
578
+ The schema of the input that the tool expects. The language model will use this to generate the input.
579
+ It is also used to validate the output of the language model.
580
+ Use descriptions to make the input understandable for the language model.
581
+ */
582
+ parameters: PARAMETERS;
583
+ /**
584
+ An optional description of what the tool does.
585
+ Will be used by the language model to decide whether to use the tool.
586
+ Not used for provider-defined tools.
587
+ */
588
+ description?: string;
589
+ /**
590
+ Optional conversion function that maps the tool result to multi-part tool content for LLMs.
591
+ */
592
+ experimental_toToolResultContent?: (result: RESULT) => ToolResultContent;
593
+ /**
594
+ An async function that is called with the arguments from the tool call and produces a result.
595
+ If not provided, the tool will not be executed automatically.
596
+
597
+ @args is the input of the tool call.
598
+ @options.abortSignal is a signal that can be used to abort the tool call.
599
+ */
600
+ execute?: (args: inferParameters<PARAMETERS>, options: ToolExecutionOptions) => PromiseLike<RESULT>;
601
+ } & ({
602
+ /**
603
+ Function tool.
604
+ */
605
+ type?: undefined | 'function';
606
+ } | {
607
+ /**
608
+ Provider-defined tool.
609
+ */
610
+ type: 'provider-defined';
611
+ /**
612
+ The ID of the tool. Should follow the format `<provider-name>.<tool-name>`.
613
+ */
614
+ id: `${string}.${string}`;
615
+ /**
616
+ The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
617
+ */
618
+ args: Record<string, unknown>;
619
+ });
620
+
621
+ type ToolSet = Record<string, Tool>;
622
+
530
623
  /**
531
624
  Prompt part of the AI function options.
532
625
  It contains a system message, a simple text prompt, or a list of messages.
@@ -546,203 +639,144 @@ type Prompt = {
546
639
  messages?: Array<CoreMessage> | Array<Omit<Message, 'id'>>;
547
640
  };
548
641
 
549
- type Streamable = ReactNode | Promise<ReactNode>;
550
- type Renderer<T extends Array<any>> = (...args: T) => Streamable | Generator<Streamable, Streamable, void> | AsyncGenerator<Streamable, Streamable, void>;
551
- type RenderTool<PARAMETERS extends z.ZodTypeAny = any> = {
552
- description?: string;
553
- parameters: PARAMETERS;
554
- generate?: Renderer<[
555
- z.infer<PARAMETERS>,
556
- {
557
- toolName: string;
558
- toolCallId: string;
559
- }
560
- ]>;
561
- };
562
- type RenderText = Renderer<[
563
- {
564
- /**
565
- * The full text content from the model so far.
566
- */
567
- content: string;
568
- /**
569
- * The new appended text content from the model since the last `text` call.
570
- */
571
- delta: string;
572
- /**
573
- * Whether the model is done generating text.
574
- * If `true`, the `content` will be the final output and this call will be the last.
575
- */
576
- done: boolean;
577
- }
578
- ]>;
579
- type RenderResult = {
580
- value: ReactNode;
581
- } & Awaited<ReturnType<LanguageModelV2['doStream']>>;
582
- /**
583
- * `streamUI` is a helper function to create a streamable UI from LLMs.
584
- */
585
- declare function streamUI<TOOLS extends {
586
- [name: string]: z.ZodTypeAny;
587
- } = {}>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, initial, text, experimental_providerMetadata, providerOptions, onFinish, ...settings }: CallSettings & Prompt & {
588
- /**
589
- * The language model to use.
590
- */
591
- model: LanguageModelV2;
592
- /**
593
- * The tools that the model can call. The model needs to support calling tools.
594
- */
595
- tools?: {
596
- [name in keyof TOOLS]: RenderTool<TOOLS[name]>;
597
- };
598
- /**
599
- * The tool choice strategy. Default: 'auto'.
600
- */
601
- toolChoice?: ToolChoice<TOOLS>;
602
- text?: RenderText;
603
- initial?: ReactNode;
604
- /**
605
- Additional provider-specific options. They are passed through
606
- to the provider from the AI SDK and enable provider-specific
607
- functionality that can be fully encapsulated in the provider.
608
- */
609
- providerOptions?: ProviderOptions;
642
+ type StandardizedPrompt = {
610
643
  /**
611
- @deprecated Use `providerOptions` instead.
612
- */
613
- experimental_providerMetadata?: ProviderMetadata;
644
+ * Original prompt type. This is forwarded to the providers and can be used
645
+ * to write send raw text to providers that support it.
646
+ */
647
+ type: 'prompt' | 'messages';
614
648
  /**
615
- * Callback that is called when the LLM response and the final object validation are finished.
616
- */
617
- onFinish?: (event: {
618
- /**
619
- * The reason why the generation finished.
620
- */
621
- finishReason: FinishReason;
622
- /**
623
- * The token usage of the generated response.
624
- */
625
- usage: LanguageModelUsage;
626
- /**
627
- * The final ui node that was generated.
628
- */
629
- value: ReactNode;
630
- /**
631
- * Warnings from the model provider (e.g. unsupported settings)
632
- */
633
- warnings?: CallWarning[];
634
- /**
635
- * Optional raw response data.
636
- */
637
- rawResponse?: {
638
- /**
639
- * Response headers.
640
- */
641
- headers?: Record<string, string>;
642
- };
643
- }) => Promise<void> | void;
644
- }): Promise<RenderResult>;
649
+ * System message.
650
+ */
651
+ system?: string;
652
+ /**
653
+ * Messages.
654
+ */
655
+ messages: CoreMessage[];
656
+ };
657
+ declare function standardizePrompt<TOOLS extends ToolSet>({ prompt, tools, }: {
658
+ prompt: Prompt;
659
+ tools: undefined | TOOLS;
660
+ }): StandardizedPrompt;
645
661
 
646
- type StreamableUIWrapper = {
662
+ type CallSettings = {
663
+ /**
664
+ Maximum number of tokens to generate.
665
+ */
666
+ maxTokens?: number;
647
667
  /**
648
- * The value of the streamable UI. This can be returned from a Server Action and received by the client.
668
+ Temperature setting. This is a number between 0 (almost no randomness) and
669
+ 1 (very random).
670
+
671
+ It is recommended to set either `temperature` or `topP`, but not both.
672
+
673
+ @default 0
649
674
  */
650
- readonly value: React.ReactNode;
675
+ temperature?: number;
651
676
  /**
652
- * This method updates the current UI node. It takes a new UI node and replaces the old one.
677
+ Nucleus sampling. This is a number between 0 and 1.
678
+
679
+ E.g. 0.1 would mean that only tokens with the top 10% probability mass
680
+ are considered.
681
+
682
+ It is recommended to set either `temperature` or `topP`, but not both.
653
683
  */
654
- update(value: React.ReactNode): StreamableUIWrapper;
684
+ topP?: number;
655
685
  /**
656
- * This method is used to append a new UI node to the end of the old one.
657
- * Once appended a new UI node, the previous UI node cannot be updated anymore.
658
- *
659
- * @example
660
- * ```jsx
661
- * const ui = createStreamableUI(<div>hello</div>)
662
- * ui.append(<div>world</div>)
663
- *
664
- * // The UI node will be:
665
- * // <>
666
- * // <div>hello</div>
667
- * // <div>world</div>
668
- * // </>
669
- * ```
686
+ Only sample from the top K options for each subsequent token.
687
+
688
+ Used to remove "long tail" low probability responses.
689
+ Recommended for advanced use cases only. You usually only need to use temperature.
670
690
  */
671
- append(value: React.ReactNode): StreamableUIWrapper;
691
+ topK?: number;
672
692
  /**
673
- * This method is used to signal that there is an error in the UI stream.
674
- * It will be thrown on the client side and caught by the nearest error boundary component.
693
+ Presence penalty setting. It affects the likelihood of the model to
694
+ repeat information that is already in the prompt.
695
+
696
+ The presence penalty is a number between -1 (increase repetition)
697
+ and 1 (maximum penalty, decrease repetition). 0 means no penalty.
675
698
  */
676
- error(error: any): StreamableUIWrapper;
699
+ presencePenalty?: number;
677
700
  /**
678
- * This method marks the UI node as finalized. You can either call it without any parameters or with a new UI node as the final state.
679
- * Once called, the UI node cannot be updated or appended anymore.
680
- *
681
- * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
701
+ Frequency penalty setting. It affects the likelihood of the model
702
+ to repeatedly use the same words or phrases.
703
+
704
+ The frequency penalty is a number between -1 (increase repetition)
705
+ and 1 (maximum penalty, decrease repetition). 0 means no penalty.
682
706
  */
683
- done(...args: [React.ReactNode] | []): StreamableUIWrapper;
684
- };
685
- /**
686
- * Create a piece of changeable UI that can be streamed to the client.
687
- * On the client side, it can be rendered as a normal React node.
688
- */
689
- declare function createStreamableUI(initialValue?: React.ReactNode): StreamableUIWrapper;
690
-
691
- declare const __internal_curr: unique symbol;
692
- declare const __internal_error: unique symbol;
693
- /**
694
- * StreamableValue is a value that can be streamed over the network via AI Actions.
695
- * To read the streamed values, use the `readStreamableValue` or `useStreamableValue` APIs.
696
- */
697
- type StreamableValue<T = any, E = any> = {
698
- [__internal_curr]?: T;
699
- [__internal_error]?: E;
700
- };
701
-
702
- /**
703
- * Create a wrapped, changeable value that can be streamed to the client.
704
- * On the client side, the value can be accessed via the readStreamableValue() API.
705
- */
706
- declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): StreamableValueWrapper<T, E>;
707
- type StreamableValueWrapper<T, E> = {
707
+ frequencyPenalty?: number;
708
708
  /**
709
- * The value of the streamable. This can be returned from a Server Action and
710
- * received by the client. To read the streamed values, use the
711
- * `readStreamableValue` or `useStreamableValue` APIs.
709
+ Stop sequences.
710
+ If set, the model will stop generating text when one of the stop sequences is generated.
711
+ Providers may have limits on the number of stop sequences.
712
712
  */
713
- readonly value: StreamableValue<T, E>;
713
+ stopSequences?: string[];
714
714
  /**
715
- * This method updates the current value with a new one.
715
+ The seed (integer) to use for random sampling. If set and supported
716
+ by the model, calls will generate deterministic results.
716
717
  */
717
- update(value: T): StreamableValueWrapper<T, E>;
718
+ seed?: number;
718
719
  /**
719
- * This method is used to append a delta string to the current value. It
720
- * requires the current value of the streamable to be a string.
721
- *
722
- * @example
723
- * ```jsx
724
- * const streamable = createStreamableValue('hello');
725
- * streamable.append(' world');
726
- *
727
- * // The value will be 'hello world'
728
- * ```
720
+ Maximum number of retries. Set to 0 to disable retries.
721
+
722
+ @default 2
729
723
  */
730
- append(value: T): StreamableValueWrapper<T, E>;
724
+ maxRetries?: number;
731
725
  /**
732
- * This method is used to signal that there is an error in the value stream.
733
- * It will be thrown on the client side when consumed via
734
- * `readStreamableValue` or `useStreamableValue`.
726
+ Abort signal.
735
727
  */
736
- error(error: any): StreamableValueWrapper<T, E>;
728
+ abortSignal?: AbortSignal;
737
729
  /**
738
- * This method marks the value as finalized. You can either call it without
739
- * any parameters or with a new value as the final state.
740
- * Once called, the value cannot be updated or appended anymore.
741
- *
742
- * This method is always **required** to be called, otherwise the response
743
- * will be stuck in a loading state.
730
+ Additional HTTP headers to be sent with the request.
731
+ Only applicable for HTTP-based providers.
744
732
  */
745
- done(...args: [T] | []): StreamableValueWrapper<T, E>;
733
+ headers?: Record<string, string | undefined>;
734
+ };
735
+
736
+ declare function prepareToolsAndToolChoice<TOOLS extends ToolSet>({ tools, toolChoice, activeTools, }: {
737
+ tools: TOOLS | undefined;
738
+ toolChoice: ToolChoice<TOOLS> | undefined;
739
+ activeTools: Array<keyof TOOLS> | undefined;
740
+ }): {
741
+ tools: Array<LanguageModelV2FunctionTool | LanguageModelV2ProviderDefinedTool> | undefined;
742
+ toolChoice: LanguageModelV2ToolChoice | undefined;
743
+ };
744
+
745
+ type RetryFunction = <OUTPUT>(fn: () => PromiseLike<OUTPUT>) => PromiseLike<OUTPUT>;
746
+
747
+ /**
748
+ * Validate and prepare retries.
749
+ */
750
+ declare function prepareRetries({ maxRetries, }: {
751
+ maxRetries: number | undefined;
752
+ }): {
753
+ maxRetries: number;
754
+ retry: RetryFunction;
746
755
  };
747
756
 
748
- export { createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, streamUI };
757
+ /**
758
+ * Validates call settings and sets default values.
759
+ */
760
+ declare function prepareCallSettings({ maxTokens, temperature, topP, topK, presencePenalty, frequencyPenalty, stopSequences, seed, }: Omit<CallSettings, 'abortSignal' | 'headers' | 'maxRetries'>): Omit<CallSettings, 'abortSignal' | 'headers' | 'maxRetries'>;
761
+
762
+ declare function download({ url }: {
763
+ url: URL;
764
+ }): Promise<{
765
+ data: Uint8Array;
766
+ mediaType: string | undefined;
767
+ }>;
768
+
769
+ declare function convertToLanguageModelPrompt({ prompt, modelSupportsImageUrls, modelSupportsUrl, downloadImplementation, }: {
770
+ prompt: StandardizedPrompt;
771
+ modelSupportsImageUrls: boolean | undefined;
772
+ modelSupportsUrl: undefined | ((url: URL) => boolean);
773
+ downloadImplementation?: typeof download;
774
+ }): Promise<LanguageModelV2Prompt>;
775
+
776
+ /**
777
+ * Warning time for notifying developers that a stream is hanging in dev mode
778
+ * using a console.warn.
779
+ */
780
+ declare const HANGING_STREAM_WARNING_TIME_MS: number;
781
+
782
+ export { HANGING_STREAM_WARNING_TIME_MS, calculateLanguageModelUsage, convertToLanguageModelPrompt, prepareCallSettings, prepareRetries, prepareToolsAndToolChoice, standardizePrompt };