ai 0.0.0-85f9a635-20240518005312 → 0.0.0-9477ebb9-20250403064906

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.
Files changed (70) hide show
  1. package/CHANGELOG.md +3521 -0
  2. package/README.md +112 -22
  3. package/dist/index.d.mts +3697 -1642
  4. package/dist/index.d.ts +3697 -1642
  5. package/dist/index.js +7201 -2942
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +7249 -2963
  8. package/dist/index.mjs.map +1 -1
  9. package/mcp-stdio/create-child-process.test.ts +92 -0
  10. package/mcp-stdio/create-child-process.ts +21 -0
  11. package/mcp-stdio/dist/index.d.mts +169 -0
  12. package/mcp-stdio/dist/index.d.ts +169 -0
  13. package/mcp-stdio/dist/index.js +352 -0
  14. package/mcp-stdio/dist/index.js.map +1 -0
  15. package/mcp-stdio/dist/index.mjs +337 -0
  16. package/mcp-stdio/dist/index.mjs.map +1 -0
  17. package/mcp-stdio/get-environment.ts +43 -0
  18. package/mcp-stdio/index.ts +4 -0
  19. package/mcp-stdio/mcp-stdio-transport.test.ts +262 -0
  20. package/mcp-stdio/mcp-stdio-transport.ts +157 -0
  21. package/package.json +46 -103
  22. package/react/dist/index.d.mts +10 -557
  23. package/react/dist/index.d.ts +10 -574
  24. package/react/dist/index.js +6 -1397
  25. package/react/dist/index.js.map +1 -1
  26. package/react/dist/index.mjs +10 -1384
  27. package/react/dist/index.mjs.map +1 -1
  28. package/rsc/dist/index.d.ts +432 -199
  29. package/rsc/dist/rsc-server.d.mts +431 -199
  30. package/rsc/dist/rsc-server.mjs +1599 -1357
  31. package/rsc/dist/rsc-server.mjs.map +1 -1
  32. package/rsc/dist/rsc-shared.d.mts +30 -23
  33. package/rsc/dist/rsc-shared.mjs +70 -108
  34. package/rsc/dist/rsc-shared.mjs.map +1 -1
  35. package/test/dist/index.d.mts +65 -0
  36. package/test/dist/index.d.ts +65 -0
  37. package/test/dist/index.js +121 -0
  38. package/test/dist/index.js.map +1 -0
  39. package/test/dist/index.mjs +94 -0
  40. package/test/dist/index.mjs.map +1 -0
  41. package/prompts/dist/index.d.mts +0 -324
  42. package/prompts/dist/index.d.ts +0 -324
  43. package/prompts/dist/index.js +0 -178
  44. package/prompts/dist/index.js.map +0 -1
  45. package/prompts/dist/index.mjs +0 -146
  46. package/prompts/dist/index.mjs.map +0 -1
  47. package/react/dist/index.server.d.mts +0 -17
  48. package/react/dist/index.server.d.ts +0 -17
  49. package/react/dist/index.server.js +0 -50
  50. package/react/dist/index.server.js.map +0 -1
  51. package/react/dist/index.server.mjs +0 -23
  52. package/react/dist/index.server.mjs.map +0 -1
  53. package/solid/dist/index.d.mts +0 -408
  54. package/solid/dist/index.d.ts +0 -408
  55. package/solid/dist/index.js +0 -1072
  56. package/solid/dist/index.js.map +0 -1
  57. package/solid/dist/index.mjs +0 -1044
  58. package/solid/dist/index.mjs.map +0 -1
  59. package/svelte/dist/index.d.mts +0 -484
  60. package/svelte/dist/index.d.ts +0 -484
  61. package/svelte/dist/index.js +0 -1778
  62. package/svelte/dist/index.js.map +0 -1
  63. package/svelte/dist/index.mjs +0 -1749
  64. package/svelte/dist/index.mjs.map +0 -1
  65. package/vue/dist/index.d.mts +0 -402
  66. package/vue/dist/index.d.ts +0 -402
  67. package/vue/dist/index.js +0 -1072
  68. package/vue/dist/index.js.map +0 -1
  69. package/vue/dist/index.mjs +0 -1034
  70. package/vue/dist/index.mjs.map +0 -1
@@ -1,8 +1,7 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
1
+ import { LanguageModelV1FinishReason, LanguageModelV1CallWarning, LanguageModelV1ProviderMetadata, LanguageModelV1 } from '@ai-sdk/provider';
2
2
  import { ReactNode } from 'react';
3
- import OpenAI from 'openai';
4
3
  import { z } from 'zod';
5
- import { LanguageModelV1 } from '@ai-sdk/provider';
4
+ import { Message } from '@ai-sdk/ui-utils';
6
5
 
7
6
  type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
8
7
  type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
@@ -27,11 +26,6 @@ type MutableAIState<AIState> = {
27
26
  update: (newState: ValueOrUpdater<AIState>) => void;
28
27
  done: ((newState: AIState) => void) | (() => void);
29
28
  };
30
- /**
31
- * StreamableValue is a value that can be streamed over the network via AI Actions.
32
- * To read the streamed values, use the `readStreamableValue` or `useStreamableValue` APIs.
33
- */
34
- type StreamableValue<T = any, E = any> = {};
35
29
 
36
30
  /**
37
31
  * Get the current AI state.
@@ -41,10 +35,10 @@ type StreamableValue<T = any, E = any> = {};
41
35
  * @example const state = getAIState() // Get the entire AI state
42
36
  * @example const field = getAIState('key') // Get the value of the key
43
37
  */
44
- declare function getAIState<AI extends AIProvider = any>(): InferAIState<AI, any>;
45
- declare function getAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): InferAIState<AI, any>[typeof key];
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]>;
46
40
  /**
47
- * Get the mutable AI state. Note that you must call `.close()` when finishing
41
+ * Get the mutable AI state. Note that you must call `.done()` when finishing
48
42
  * updating the AI state.
49
43
  *
50
44
  * @example
@@ -64,160 +58,45 @@ declare function getAIState<AI extends AIProvider = any>(key: keyof InferAIState
64
58
  declare function getMutableAIState<AI extends AIProvider = any>(): MutableAIState<InferAIState<AI, any>>;
65
59
  declare function getMutableAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): MutableAIState<InferAIState<AI, any>[typeof key]>;
66
60
 
67
- /**
68
- * Create a piece of changable UI that can be streamed to the client.
69
- * On the client side, it can be rendered as a normal React node.
70
- */
71
- declare function createStreamableUI(initialValue?: React.ReactNode): {
72
- /**
73
- * The value of the streamable UI. This can be returned from a Server Action and received by the client.
74
- */
75
- value: react_jsx_runtime.JSX.Element;
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;
76
65
  /**
77
- * This method updates the current UI node. It takes a new UI node and replaces the old one.
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.
78
69
  */
79
- update(value: React.ReactNode): any;
70
+ onSetAIState?: OnSetAIState<AIState>;
80
71
  /**
81
- * This method is used to append a new UI node to the end of the old one.
82
- * Once appended a new UI node, the previous UI node cannot be updated anymore.
83
- *
84
- * @example
85
- * ```jsx
86
- * const ui = createStreamableUI(<div>hello</div>)
87
- * ui.append(<div>world</div>)
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.
88
75
  *
89
- * // The UI node will be:
90
- * // <>
91
- * // <div>hello</div>
92
- * // <div>world</div>
93
- * // </>
94
- * ```
95
- */
96
- append(value: React.ReactNode): any;
97
- /**
98
- * This method is used to signal that there is an error in the UI stream.
99
- * It will be thrown on the client side and caught by the nearest error boundary component.
100
- */
101
- error(error: any): any;
102
- /**
103
- * 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.
104
- * Once called, the UI node cannot be updated or appended anymore.
76
+ * If returning `undefined`, the client side UI state will not be updated.
105
77
  *
106
- * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
107
- */
108
- done(...args: [] | [React.ReactNode]): any;
109
- };
110
- declare const STREAMABLE_VALUE_INTERNAL_LOCK: unique symbol;
111
- /**
112
- * Create a wrapped, changable value that can be streamed to the client.
113
- * On the client side, the value can be accessed via the readStreamableValue() API.
114
- */
115
- declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): {
116
- /**
117
- * @internal This is an internal lock to prevent the value from being
118
- * updated by the user.
119
- */
120
- [STREAMABLE_VALUE_INTERNAL_LOCK]: boolean;
121
- /**
122
- * The value of the streamable. This can be returned from a Server Action and
123
- * received by the client. To read the streamed values, use the
124
- * `readStreamableValue` or `useStreamableValue` APIs.
125
- */
126
- readonly value: StreamableValue<T, E>;
127
- /**
128
- * This method updates the current value with a new one.
129
- */
130
- update(value: T): any;
131
- /**
132
- * This method is used to append a delta string to the current value. It
133
- * requires the current value of the streamable to be a string.
78
+ * This function must be annotated with the `"use server"` directive.
134
79
  *
135
80
  * @example
136
- * ```jsx
137
- * const streamable = createStreamableValue('hello');
138
- * streamable.append(' world');
81
+ * ```tsx
82
+ * onGetUIState: async () => {
83
+ * 'use server';
139
84
  *
140
- * // The value will be 'hello world'
141
- * ```
142
- */
143
- append(value: T): any;
144
- /**
145
- * This method is used to signal that there is an error in the value stream.
146
- * It will be thrown on the client side when consumed via
147
- * `readStreamableValue` or `useStreamableValue`.
148
- */
149
- error(error: any): any;
150
- /**
151
- * This method marks the value as finalized. You can either call it without
152
- * any parameters or with a new value as the final state.
153
- * Once called, the value cannot be updated or appended anymore.
85
+ * const currentAIState = getAIState();
86
+ * const externalAIState = await loadAIStateFromDatabase();
154
87
  *
155
- * This method is always **required** to be called, otherwise the response
156
- * will be stuck in a loading state.
157
- */
158
- done(...args: [] | [T]): any;
159
- };
160
-
161
- type Streamable$1 = ReactNode | Promise<ReactNode>;
162
- type Renderer$1<T> = (props: T) => Streamable$1 | Generator<Streamable$1, Streamable$1, void> | AsyncGenerator<Streamable$1, Streamable$1, void>;
163
- /**
164
- * `render` is a helper function to create a streamable UI from some LLMs.
165
- * This API only supports OpenAI's GPT models with Function Calling and Assistants Tools,
166
- * please use `streamUI` for compatibility with other providers.
167
- *
168
- * @deprecated It's recommended to use the `streamUI` API for compatibility with AI SDK Core APIs
169
- * and future features. This API will be removed in a future release.
170
- */
171
- declare function render<TS extends {
172
- [name: string]: z.Schema;
173
- } = {}, FS extends {
174
- [name: string]: z.Schema;
175
- } = {}>(options: {
176
- /**
177
- * The model name to use. Must be OpenAI SDK compatible. Tools and Functions are only supported
178
- * GPT models (3.5/4), OpenAI Assistants, Mistral small and large, and Fireworks firefunction-v1.
88
+ * if (currentAIState === externalAIState) return undefined;
179
89
  *
180
- * @example "gpt-3.5-turbo"
181
- */
182
- model: string;
183
- /**
184
- * The provider instance to use. Currently the only provider available is OpenAI.
185
- * This needs to match the model name.
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
+ * ```
186
97
  */
187
- provider: OpenAI;
188
- messages: Parameters<typeof OpenAI.prototype.chat.completions.create>[0]['messages'];
189
- text?: Renderer$1<{
190
- /**
191
- * The full text content from the model so far.
192
- */
193
- content: string;
194
- /**
195
- * The new appended text content from the model since the last `text` call.
196
- */
197
- delta: string;
198
- /**
199
- * Whether the model is done generating text.
200
- * If `true`, the `content` will be the final output and this call will be the last.
201
- */
202
- done: boolean;
203
- }>;
204
- tools?: {
205
- [name in keyof TS]: {
206
- description?: string;
207
- parameters: TS[name];
208
- render: Renderer$1<z.infer<TS[name]>>;
209
- };
210
- };
211
- functions?: {
212
- [name in keyof FS]: {
213
- description?: string;
214
- parameters: FS[name];
215
- render: Renderer$1<z.infer<FS[name]>>;
216
- };
217
- };
218
- initial?: ReactNode;
219
- temperature?: number;
220
- }): ReactNode;
98
+ onGetUIState?: OnGetUIState<UIState>;
99
+ }): AIProvider<AIState, UIState, Actions>;
221
100
 
222
101
  type CallSettings = {
223
102
  /**
@@ -243,13 +122,18 @@ type CallSettings = {
243
122
  */
244
123
  topP?: number;
245
124
  /**
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.
129
+ */
130
+ topK?: number;
131
+ /**
246
132
  Presence penalty setting. It affects the likelihood of the model to
247
133
  repeat information that is already in the prompt.
248
134
 
249
135
  The presence penalty is a number between -1 (increase repetition)
250
136
  and 1 (maximum penalty, decrease repetition). 0 means no penalty.
251
-
252
- @default 0
253
137
  */
254
138
  presencePenalty?: number;
255
139
  /**
@@ -258,11 +142,15 @@ type CallSettings = {
258
142
 
259
143
  The frequency penalty is a number between -1 (increase repetition)
260
144
  and 1 (maximum penalty, decrease repetition). 0 means no penalty.
261
-
262
- @default 0
263
145
  */
264
146
  frequencyPenalty?: number;
265
147
  /**
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.
151
+ */
152
+ stopSequences?: string[];
153
+ /**
266
154
  The seed (integer) to use for random sampling. If set and supported
267
155
  by the model, calls will generate deterministic results.
268
156
  */
@@ -277,6 +165,74 @@ type CallSettings = {
277
165
  Abort signal.
278
166
  */
279
167
  abortSignal?: AbortSignal;
168
+ /**
169
+ Additional HTTP headers to be sent with the request.
170
+ Only applicable for HTTP-based providers.
171
+ */
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 = LanguageModelV1FinishReason;
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 = LanguageModelV1CallWarning;
192
+ /**
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
199
+ */
200
+ type ToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'required' | {
201
+ type: 'tool';
202
+ toolName: keyof TOOLS;
203
+ };
204
+
205
+ /**
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.
210
+ */
211
+ type ProviderMetadata = LanguageModelV1ProviderMetadata;
212
+ /**
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.
217
+ */
218
+ type ProviderOptions = LanguageModelV1ProviderMetadata;
219
+
220
+ /**
221
+ Represents the number of tokens used in a prompt and completion.
222
+ */
223
+ type LanguageModelUsage = {
224
+ /**
225
+ The number of tokens used in the prompt.
226
+ */
227
+ promptTokens: number;
228
+ /**
229
+ The number of tokens used in the completion.
230
+ */
231
+ completionTokens: number;
232
+ /**
233
+ The total number of tokens used (promptTokens + completionTokens).
234
+ */
235
+ totalTokens: number;
280
236
  };
281
237
 
282
238
  /**
@@ -284,6 +240,15 @@ Data content. Can either be a base64-encoded string, a Uint8Array, an ArrayBuffe
284
240
  */
285
241
  type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
286
242
 
243
+ type ToolResultContent = Array<{
244
+ type: 'text';
245
+ text: string;
246
+ } | {
247
+ type: 'image';
248
+ data: string;
249
+ mimeType?: string;
250
+ }>;
251
+
287
252
  /**
288
253
  Text content part of a prompt. It contains a string of text.
289
254
  */
@@ -293,6 +258,16 @@ interface TextPart {
293
258
  The text content.
294
259
  */
295
260
  text: string;
261
+ /**
262
+ Additional provider-specific metadata. They are passed through
263
+ to the provider from the AI SDK and enable provider-specific
264
+ functionality that can be fully encapsulated in the provider.
265
+ */
266
+ providerOptions?: ProviderOptions;
267
+ /**
268
+ @deprecated Use `providerOptions` instead.
269
+ */
270
+ experimental_providerMetadata?: ProviderMetadata;
296
271
  }
297
272
  /**
298
273
  Image content part of a prompt. It contains an image.
@@ -310,6 +285,91 @@ interface ImagePart {
310
285
  Optional mime type of the image.
311
286
  */
312
287
  mimeType?: string;
288
+ /**
289
+ Additional provider-specific metadata. They are passed through
290
+ to the provider from the AI SDK and enable provider-specific
291
+ functionality that can be fully encapsulated in the provider.
292
+ */
293
+ providerOptions?: ProviderOptions;
294
+ /**
295
+ @deprecated Use `providerOptions` instead.
296
+ */
297
+ experimental_providerMetadata?: ProviderMetadata;
298
+ }
299
+ /**
300
+ File content part of a prompt. It contains a file.
301
+ */
302
+ interface FilePart {
303
+ type: 'file';
304
+ /**
305
+ File data. Can either be:
306
+
307
+ - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
308
+ - URL: a URL that points to the image
309
+ */
310
+ data: DataContent | URL;
311
+ /**
312
+ Optional filename of the file.
313
+ */
314
+ filename?: string;
315
+ /**
316
+ Mime type of the file.
317
+ */
318
+ mimeType: string;
319
+ /**
320
+ Additional provider-specific metadata. They are passed through
321
+ to the provider from the AI SDK and enable provider-specific
322
+ functionality that can be fully encapsulated in the provider.
323
+ */
324
+ providerOptions?: ProviderOptions;
325
+ /**
326
+ @deprecated Use `providerOptions` instead.
327
+ */
328
+ experimental_providerMetadata?: ProviderMetadata;
329
+ }
330
+ /**
331
+ * Reasoning content part of a prompt. It contains a reasoning.
332
+ */
333
+ interface ReasoningPart {
334
+ type: 'reasoning';
335
+ /**
336
+ The reasoning text.
337
+ */
338
+ text: string;
339
+ /**
340
+ An optional signature for verifying that the reasoning originated from the model.
341
+ */
342
+ signature?: string;
343
+ /**
344
+ Additional provider-specific metadata. They are passed through
345
+ to the provider from the AI SDK and enable provider-specific
346
+ functionality that can be fully encapsulated in the provider.
347
+ */
348
+ providerOptions?: ProviderOptions;
349
+ /**
350
+ @deprecated Use `providerOptions` instead.
351
+ */
352
+ experimental_providerMetadata?: ProviderMetadata;
353
+ }
354
+ /**
355
+ Redacted reasoning content part of a prompt.
356
+ */
357
+ interface RedactedReasoningPart {
358
+ type: 'redacted-reasoning';
359
+ /**
360
+ Redacted reasoning data.
361
+ */
362
+ data: string;
363
+ /**
364
+ Additional provider-specific metadata. They are passed through
365
+ to the provider from the AI SDK and enable provider-specific
366
+ functionality that can be fully encapsulated in the provider.
367
+ */
368
+ providerOptions?: ProviderOptions;
369
+ /**
370
+ @deprecated Use `providerOptions` instead.
371
+ */
372
+ experimental_providerMetadata?: ProviderMetadata;
313
373
  }
314
374
  /**
315
375
  Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
@@ -328,6 +388,16 @@ interface ToolCallPart {
328
388
  Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
329
389
  */
330
390
  args: unknown;
391
+ /**
392
+ Additional provider-specific metadata. They are passed through
393
+ to the provider from the AI SDK and enable provider-specific
394
+ functionality that can be fully encapsulated in the provider.
395
+ */
396
+ providerOptions?: ProviderOptions;
397
+ /**
398
+ @deprecated Use `providerOptions` instead.
399
+ */
400
+ experimental_providerMetadata?: ProviderMetadata;
331
401
  }
332
402
  /**
333
403
  Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
@@ -347,16 +417,25 @@ interface ToolResultPart {
347
417
  */
348
418
  result: unknown;
349
419
  /**
420
+ Multi-part content of the tool result. Only for tools that support multipart results.
421
+ */
422
+ experimental_content?: ToolResultContent;
423
+ /**
350
424
  Optional flag if the result is an error or an error message.
351
425
  */
352
426
  isError?: boolean;
427
+ /**
428
+ Additional provider-specific metadata. They are passed through
429
+ to the provider from the AI SDK and enable provider-specific
430
+ functionality that can be fully encapsulated in the provider.
431
+ */
432
+ providerOptions?: ProviderOptions;
433
+ /**
434
+ @deprecated Use `providerOptions` instead.
435
+ */
436
+ experimental_providerMetadata?: ProviderMetadata;
353
437
  }
354
438
 
355
- /**
356
- A message that can be used in the `messages` field of a prompt.
357
- It can be a user message, an assistant message, or a tool message.
358
- */
359
- type CoreMessage = CoreSystemMessage | CoreUserMessage | CoreAssistantMessage | CoreToolMessage;
360
439
  /**
361
440
  A system message. It can contain system information.
362
441
 
@@ -367,6 +446,16 @@ type CoreMessage = CoreSystemMessage | CoreUserMessage | CoreAssistantMessage |
367
446
  type CoreSystemMessage = {
368
447
  role: 'system';
369
448
  content: string;
449
+ /**
450
+ Additional provider-specific metadata. They are passed through
451
+ to the provider from the AI SDK and enable provider-specific
452
+ functionality that can be fully encapsulated in the provider.
453
+ */
454
+ providerOptions?: ProviderOptions;
455
+ /**
456
+ @deprecated Use `providerOptions` instead.
457
+ */
458
+ experimental_providerMetadata?: ProviderMetadata;
370
459
  };
371
460
  /**
372
461
  A user message. It can contain text or a combination of text and images.
@@ -374,36 +463,73 @@ A user message. It can contain text or a combination of text and images.
374
463
  type CoreUserMessage = {
375
464
  role: 'user';
376
465
  content: UserContent;
466
+ /**
467
+ Additional provider-specific metadata. They are passed through
468
+ to the provider from the AI SDK and enable provider-specific
469
+ functionality that can be fully encapsulated in the provider.
470
+ */
471
+ providerOptions?: ProviderOptions;
472
+ /**
473
+ @deprecated Use `providerOptions` instead.
474
+ */
475
+ experimental_providerMetadata?: ProviderMetadata;
377
476
  };
378
477
  /**
379
478
  Content of a user message. It can be a string or an array of text and image parts.
380
479
  */
381
- type UserContent = string | Array<TextPart | ImagePart>;
480
+ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
382
481
  /**
383
482
  An assistant message. It can contain text, tool calls, or a combination of text and tool calls.
384
483
  */
385
484
  type CoreAssistantMessage = {
386
485
  role: 'assistant';
387
486
  content: AssistantContent;
487
+ /**
488
+ Additional provider-specific metadata. They are passed through
489
+ to the provider from the AI SDK and enable provider-specific
490
+ functionality that can be fully encapsulated in the provider.
491
+ */
492
+ providerOptions?: ProviderOptions;
493
+ /**
494
+ @deprecated Use `providerOptions` instead.
495
+ */
496
+ experimental_providerMetadata?: ProviderMetadata;
388
497
  };
389
498
  /**
390
- Content of an assistant message. It can be a string or an array of text and tool call parts.
499
+ Content of an assistant message.
500
+ It can be a string or an array of text, image, reasoning, redacted reasoning, and tool call parts.
391
501
  */
392
- type AssistantContent = string | Array<TextPart | ToolCallPart>;
502
+ type AssistantContent = string | Array<TextPart | FilePart | ReasoningPart | RedactedReasoningPart | ToolCallPart>;
393
503
  /**
394
504
  A tool message. It contains the result of one or more tool calls.
395
505
  */
396
506
  type CoreToolMessage = {
397
507
  role: 'tool';
398
508
  content: ToolContent;
509
+ /**
510
+ Additional provider-specific metadata. They are passed through
511
+ to the provider from the AI SDK and enable provider-specific
512
+ functionality that can be fully encapsulated in the provider.
513
+ */
514
+ providerOptions?: ProviderOptions;
515
+ /**
516
+ @deprecated Use `providerOptions` instead.
517
+ */
518
+ experimental_providerMetadata?: ProviderMetadata;
399
519
  };
400
520
  /**
401
521
  Content of a tool message. It is an array of tool result parts.
402
522
  */
403
523
  type ToolContent = Array<ToolResultPart>;
524
+ /**
525
+ A message that can be used in the `messages` field of a prompt.
526
+ It can be a user message, an assistant message, or a tool message.
527
+ */
528
+ type CoreMessage = CoreSystemMessage | CoreUserMessage | CoreAssistantMessage | CoreToolMessage;
404
529
 
405
530
  /**
406
- Prompt part of the AI function options. It contains a system message, a simple text prompt, or a list of messages.
531
+ Prompt part of the AI function options.
532
+ It contains a system message, a simple text prompt, or a list of messages.
407
533
  */
408
534
  type Prompt = {
409
535
  /**
@@ -415,9 +541,9 @@ type Prompt = {
415
541
  */
416
542
  prompt?: string;
417
543
  /**
418
- A list of messsages. You can either use `prompt` or `messages` but not both.
544
+ A list of messages. You can either use `prompt` or `messages` but not both.
419
545
  */
420
- messages?: Array<CoreMessage>;
546
+ messages?: Array<CoreMessage> | Array<Omit<Message, 'id'>>;
421
547
  };
422
548
 
423
549
  type Streamable = ReactNode | Promise<ReactNode>;
@@ -458,7 +584,7 @@ type RenderResult = {
458
584
  */
459
585
  declare function streamUI<TOOLS extends {
460
586
  [name: string]: z.ZodTypeAny;
461
- } = {}>({ model, tools, system, prompt, messages, maxRetries, abortSignal, initial, text, ...settings }: CallSettings & Prompt & {
587
+ } = {}>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, initial, text, experimental_providerMetadata, providerOptions, onFinish, ...settings }: CallSettings & Prompt & {
462
588
  /**
463
589
  * The language model to use.
464
590
  */
@@ -469,48 +595,154 @@ declare function streamUI<TOOLS extends {
469
595
  tools?: {
470
596
  [name in keyof TOOLS]: RenderTool<TOOLS[name]>;
471
597
  };
598
+ /**
599
+ * The tool choice strategy. Default: 'auto'.
600
+ */
601
+ toolChoice?: ToolChoice<TOOLS>;
472
602
  text?: RenderText;
473
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;
610
+ /**
611
+ @deprecated Use `providerOptions` instead.
612
+ */
613
+ experimental_providerMetadata?: ProviderMetadata;
614
+ /**
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;
474
644
  }): Promise<RenderResult>;
475
645
 
476
- declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, onSetAIState, onGetUIState, }: {
477
- actions: Actions;
478
- initialAIState?: AIState;
479
- initialUIState?: UIState;
646
+ type StreamableUIWrapper = {
480
647
  /**
481
- * This function is called whenever the AI state is updated by an Action.
482
- * You can use this to persist the AI state to a database, or to send it to a
483
- * logging service.
648
+ * The value of the streamable UI. This can be returned from a Server Action and received by the client.
484
649
  */
485
- onSetAIState?: OnSetAIState<AIState>;
650
+ readonly value: React.ReactNode;
486
651
  /**
487
- * This function is used to retrieve the UI state based on the AI state.
488
- * For example, to render the initial UI state based on a given AI state, or
489
- * to sync the UI state when the application is already loaded.
490
- *
491
- * If returning `undefined`, the client side UI state will not be updated.
492
- *
493
- * This function must be annotated with the `"use server"` directive.
652
+ * This method updates the current UI node. It takes a new UI node and replaces the old one.
653
+ */
654
+ update(value: React.ReactNode): StreamableUIWrapper;
655
+ /**
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.
494
658
  *
495
659
  * @example
496
- * ```tsx
497
- * onGetUIState: async () => {
498
- * 'use server';
660
+ * ```jsx
661
+ * const ui = createStreamableUI(<div>hello</div>)
662
+ * ui.append(<div>world</div>)
499
663
  *
500
- * const currentAIState = getAIState();
501
- * const externalAIState = await loadAIStateFromDatabase();
664
+ * // The UI node will be:
665
+ * // <>
666
+ * // <div>hello</div>
667
+ * // <div>world</div>
668
+ * // </>
669
+ * ```
670
+ */
671
+ append(value: React.ReactNode): StreamableUIWrapper;
672
+ /**
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.
675
+ */
676
+ error(error: any): StreamableUIWrapper;
677
+ /**
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.
502
680
  *
503
- * if (currentAIState === externalAIState) return undefined;
681
+ * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
682
+ */
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> = {
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.
712
+ */
713
+ readonly value: StreamableValue<T, E>;
714
+ /**
715
+ * This method updates the current value with a new one.
716
+ */
717
+ update(value: T): StreamableValueWrapper<T, E>;
718
+ /**
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.
504
721
  *
505
- * // Update current AI state and return the new UI state
506
- * const state = getMutableAIState()
507
- * state.done(externalAIState)
722
+ * @example
723
+ * ```jsx
724
+ * const streamable = createStreamableValue('hello');
725
+ * streamable.append(' world');
508
726
  *
509
- * return <div>...</div>;
510
- * }
727
+ * // The value will be 'hello world'
511
728
  * ```
512
729
  */
513
- onGetUIState?: OnGetUIState<UIState>;
514
- }): AIProvider<AIState, UIState, Actions>;
730
+ append(value: T): StreamableValueWrapper<T, E>;
731
+ /**
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`.
735
+ */
736
+ error(error: any): StreamableValueWrapper<T, E>;
737
+ /**
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.
744
+ */
745
+ done(...args: [T] | []): StreamableValueWrapper<T, E>;
746
+ };
515
747
 
516
- export { createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, render, streamUI };
748
+ export { createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, streamUI };