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>>;
@@ -29,11 +28,6 @@ type MutableAIState<AIState> = {
29
28
  update: (newState: ValueOrUpdater<AIState>) => void;
30
29
  done: ((newState: AIState) => void) | (() => void);
31
30
  };
32
- /**
33
- * StreamableValue is a value that can be streamed over the network via AI Actions.
34
- * To read the streamed values, use the `readStreamableValue` or `useStreamableValue` APIs.
35
- */
36
- type StreamableValue<T = any, E = any> = {};
37
31
 
38
32
  /**
39
33
  * Get the current AI state.
@@ -43,10 +37,10 @@ type StreamableValue<T = any, E = any> = {};
43
37
  * @example const state = getAIState() // Get the entire AI state
44
38
  * @example const field = getAIState('key') // Get the value of the key
45
39
  */
46
- declare function getAIState<AI extends AIProvider = any>(): InferAIState<AI, any>;
47
- declare function getAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): InferAIState<AI, any>[typeof key];
40
+ declare function getAIState<AI extends AIProvider = any>(): Readonly<InferAIState<AI, any>>;
41
+ declare function getAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): Readonly<InferAIState<AI, any>[typeof key]>;
48
42
  /**
49
- * Get the mutable AI state. Note that you must call `.close()` when finishing
43
+ * Get the mutable AI state. Note that you must call `.done()` when finishing
50
44
  * updating the AI state.
51
45
  *
52
46
  * @example
@@ -66,160 +60,45 @@ declare function getAIState<AI extends AIProvider = any>(key: keyof InferAIState
66
60
  declare function getMutableAIState<AI extends AIProvider = any>(): MutableAIState<InferAIState<AI, any>>;
67
61
  declare function getMutableAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): MutableAIState<InferAIState<AI, any>[typeof key]>;
68
62
 
69
- /**
70
- * Create a piece of changable UI that can be streamed to the client.
71
- * On the client side, it can be rendered as a normal React node.
72
- */
73
- declare function createStreamableUI(initialValue?: React.ReactNode): {
74
- /**
75
- * The value of the streamable UI. This can be returned from a Server Action and received by the client.
76
- */
77
- value: react_jsx_runtime.JSX.Element;
63
+ declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, onSetAIState, onGetUIState, }: {
64
+ actions: Actions;
65
+ initialAIState?: AIState;
66
+ initialUIState?: UIState;
78
67
  /**
79
- * This method updates the current UI node. It takes a new UI node and replaces the old one.
68
+ * This function is called whenever the AI state is updated by an Action.
69
+ * You can use this to persist the AI state to a database, or to send it to a
70
+ * logging service.
80
71
  */
81
- update(value: React.ReactNode): any;
72
+ onSetAIState?: OnSetAIState<AIState>;
82
73
  /**
83
- * This method is used to append a new UI node to the end of the old one.
84
- * Once appended a new UI node, the previous UI node cannot be updated anymore.
85
- *
86
- * @example
87
- * ```jsx
88
- * const ui = createStreamableUI(<div>hello</div>)
89
- * ui.append(<div>world</div>)
74
+ * This function is used to retrieve the UI state based on the AI state.
75
+ * For example, to render the initial UI state based on a given AI state, or
76
+ * to sync the UI state when the application is already loaded.
90
77
  *
91
- * // The UI node will be:
92
- * // <>
93
- * // <div>hello</div>
94
- * // <div>world</div>
95
- * // </>
96
- * ```
97
- */
98
- append(value: React.ReactNode): any;
99
- /**
100
- * This method is used to signal that there is an error in the UI stream.
101
- * It will be thrown on the client side and caught by the nearest error boundary component.
102
- */
103
- error(error: any): any;
104
- /**
105
- * 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.
106
- * Once called, the UI node cannot be updated or appended anymore.
78
+ * If returning `undefined`, the client side UI state will not be updated.
107
79
  *
108
- * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
109
- */
110
- done(...args: [] | [React.ReactNode]): any;
111
- };
112
- declare const STREAMABLE_VALUE_INTERNAL_LOCK: unique symbol;
113
- /**
114
- * Create a wrapped, changable value that can be streamed to the client.
115
- * On the client side, the value can be accessed via the readStreamableValue() API.
116
- */
117
- declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): {
118
- /**
119
- * @internal This is an internal lock to prevent the value from being
120
- * updated by the user.
121
- */
122
- [STREAMABLE_VALUE_INTERNAL_LOCK]: boolean;
123
- /**
124
- * The value of the streamable. This can be returned from a Server Action and
125
- * received by the client. To read the streamed values, use the
126
- * `readStreamableValue` or `useStreamableValue` APIs.
127
- */
128
- readonly value: StreamableValue<T, E>;
129
- /**
130
- * This method updates the current value with a new one.
131
- */
132
- update(value: T): any;
133
- /**
134
- * This method is used to append a delta string to the current value. It
135
- * requires the current value of the streamable to be a string.
80
+ * This function must be annotated with the `"use server"` directive.
136
81
  *
137
82
  * @example
138
- * ```jsx
139
- * const streamable = createStreamableValue('hello');
140
- * streamable.append(' world');
83
+ * ```tsx
84
+ * onGetUIState: async () => {
85
+ * 'use server';
141
86
  *
142
- * // The value will be 'hello world'
143
- * ```
144
- */
145
- append(value: T): any;
146
- /**
147
- * This method is used to signal that there is an error in the value stream.
148
- * It will be thrown on the client side when consumed via
149
- * `readStreamableValue` or `useStreamableValue`.
150
- */
151
- error(error: any): any;
152
- /**
153
- * This method marks the value as finalized. You can either call it without
154
- * any parameters or with a new value as the final state.
155
- * Once called, the value cannot be updated or appended anymore.
87
+ * const currentAIState = getAIState();
88
+ * const externalAIState = await loadAIStateFromDatabase();
156
89
  *
157
- * This method is always **required** to be called, otherwise the response
158
- * will be stuck in a loading state.
159
- */
160
- done(...args: [] | [T]): any;
161
- };
162
-
163
- type Streamable$1 = ReactNode | Promise<ReactNode>;
164
- type Renderer$1<T> = (props: T) => Streamable$1 | Generator<Streamable$1, Streamable$1, void> | AsyncGenerator<Streamable$1, Streamable$1, void>;
165
- /**
166
- * `render` is a helper function to create a streamable UI from some LLMs.
167
- * This API only supports OpenAI's GPT models with Function Calling and Assistants Tools,
168
- * please use `streamUI` for compatibility with other providers.
169
- *
170
- * @deprecated It's recommended to use the `streamUI` API for compatibility with AI SDK Core APIs
171
- * and future features. This API will be removed in a future release.
172
- */
173
- declare function render<TS extends {
174
- [name: string]: z.Schema;
175
- } = {}, FS extends {
176
- [name: string]: z.Schema;
177
- } = {}>(options: {
178
- /**
179
- * The model name to use. Must be OpenAI SDK compatible. Tools and Functions are only supported
180
- * GPT models (3.5/4), OpenAI Assistants, Mistral small and large, and Fireworks firefunction-v1.
90
+ * if (currentAIState === externalAIState) return undefined;
181
91
  *
182
- * @example "gpt-3.5-turbo"
183
- */
184
- model: string;
185
- /**
186
- * The provider instance to use. Currently the only provider available is OpenAI.
187
- * This needs to match the model name.
92
+ * // Update current AI state and return the new UI state
93
+ * const state = getMutableAIState()
94
+ * state.done(externalAIState)
95
+ *
96
+ * return <div>...</div>;
97
+ * }
98
+ * ```
188
99
  */
189
- provider: OpenAI;
190
- messages: Parameters<typeof OpenAI.prototype.chat.completions.create>[0]['messages'];
191
- text?: Renderer$1<{
192
- /**
193
- * The full text content from the model so far.
194
- */
195
- content: string;
196
- /**
197
- * The new appended text content from the model since the last `text` call.
198
- */
199
- delta: string;
200
- /**
201
- * Whether the model is done generating text.
202
- * If `true`, the `content` will be the final output and this call will be the last.
203
- */
204
- done: boolean;
205
- }>;
206
- tools?: {
207
- [name in keyof TS]: {
208
- description?: string;
209
- parameters: TS[name];
210
- render: Renderer$1<z.infer<TS[name]>>;
211
- };
212
- };
213
- functions?: {
214
- [name in keyof FS]: {
215
- description?: string;
216
- parameters: FS[name];
217
- render: Renderer$1<z.infer<FS[name]>>;
218
- };
219
- };
220
- initial?: ReactNode;
221
- temperature?: number;
222
- }): ReactNode;
100
+ onGetUIState?: OnGetUIState<UIState>;
101
+ }): AIProvider<AIState, UIState, Actions>;
223
102
 
224
103
  type CallSettings = {
225
104
  /**
@@ -245,13 +124,18 @@ type CallSettings = {
245
124
  */
246
125
  topP?: number;
247
126
  /**
127
+ Only sample from the top K options for each subsequent token.
128
+
129
+ Used to remove "long tail" low probability responses.
130
+ Recommended for advanced use cases only. You usually only need to use temperature.
131
+ */
132
+ topK?: number;
133
+ /**
248
134
  Presence penalty setting. It affects the likelihood of the model to
249
135
  repeat information that is already in the prompt.
250
136
 
251
137
  The presence penalty is a number between -1 (increase repetition)
252
138
  and 1 (maximum penalty, decrease repetition). 0 means no penalty.
253
-
254
- @default 0
255
139
  */
256
140
  presencePenalty?: number;
257
141
  /**
@@ -260,11 +144,15 @@ type CallSettings = {
260
144
 
261
145
  The frequency penalty is a number between -1 (increase repetition)
262
146
  and 1 (maximum penalty, decrease repetition). 0 means no penalty.
263
-
264
- @default 0
265
147
  */
266
148
  frequencyPenalty?: number;
267
149
  /**
150
+ Stop sequences.
151
+ If set, the model will stop generating text when one of the stop sequences is generated.
152
+ Providers may have limits on the number of stop sequences.
153
+ */
154
+ stopSequences?: string[];
155
+ /**
268
156
  The seed (integer) to use for random sampling. If set and supported
269
157
  by the model, calls will generate deterministic results.
270
158
  */
@@ -279,6 +167,74 @@ type CallSettings = {
279
167
  Abort signal.
280
168
  */
281
169
  abortSignal?: AbortSignal;
170
+ /**
171
+ Additional HTTP headers to be sent with the request.
172
+ Only applicable for HTTP-based providers.
173
+ */
174
+ headers?: Record<string, string | undefined>;
175
+ };
176
+
177
+ /**
178
+ Reason why a language model finished generating a response.
179
+
180
+ Can be one of the following:
181
+ - `stop`: model generated stop sequence
182
+ - `length`: model generated maximum number of tokens
183
+ - `content-filter`: content filter violation stopped the model
184
+ - `tool-calls`: model triggered tool calls
185
+ - `error`: model stopped because of an error
186
+ - `other`: model stopped for other reasons
187
+ */
188
+ type FinishReason = LanguageModelV1FinishReason;
189
+ /**
190
+ Warning from the model provider for this call. The call will proceed, but e.g.
191
+ some settings might not be supported, which can lead to suboptimal results.
192
+ */
193
+ type CallWarning = LanguageModelV1CallWarning;
194
+ /**
195
+ Tool choice for the generation. It supports the following settings:
196
+
197
+ - `auto` (default): the model can choose whether and which tools to call.
198
+ - `required`: the model must call a tool. It can choose which tool to call.
199
+ - `none`: the model must not call tools
200
+ - `{ type: 'tool', toolName: string (typed) }`: the model must call the specified tool
201
+ */
202
+ type ToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'required' | {
203
+ type: 'tool';
204
+ toolName: keyof TOOLS;
205
+ };
206
+
207
+ /**
208
+ Additional provider-specific metadata that is returned from the provider.
209
+
210
+ This is needed to enable provider-specific functionality that can be
211
+ fully encapsulated in the provider.
212
+ */
213
+ type ProviderMetadata = LanguageModelV1ProviderMetadata;
214
+ /**
215
+ Additional provider-specific options.
216
+
217
+ They are passed through to the provider from the AI SDK and enable
218
+ provider-specific functionality that can be fully encapsulated in the provider.
219
+ */
220
+ type ProviderOptions = LanguageModelV1ProviderMetadata;
221
+
222
+ /**
223
+ Represents the number of tokens used in a prompt and completion.
224
+ */
225
+ type LanguageModelUsage = {
226
+ /**
227
+ The number of tokens used in the prompt.
228
+ */
229
+ promptTokens: number;
230
+ /**
231
+ The number of tokens used in the completion.
232
+ */
233
+ completionTokens: number;
234
+ /**
235
+ The total number of tokens used (promptTokens + completionTokens).
236
+ */
237
+ totalTokens: number;
282
238
  };
283
239
 
284
240
  /**
@@ -286,6 +242,15 @@ Data content. Can either be a base64-encoded string, a Uint8Array, an ArrayBuffe
286
242
  */
287
243
  type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
288
244
 
245
+ type ToolResultContent = Array<{
246
+ type: 'text';
247
+ text: string;
248
+ } | {
249
+ type: 'image';
250
+ data: string;
251
+ mimeType?: string;
252
+ }>;
253
+
289
254
  /**
290
255
  Text content part of a prompt. It contains a string of text.
291
256
  */
@@ -295,6 +260,16 @@ interface TextPart {
295
260
  The text content.
296
261
  */
297
262
  text: string;
263
+ /**
264
+ Additional provider-specific metadata. They are passed through
265
+ to the provider from the AI SDK and enable provider-specific
266
+ functionality that can be fully encapsulated in the provider.
267
+ */
268
+ providerOptions?: ProviderOptions;
269
+ /**
270
+ @deprecated Use `providerOptions` instead.
271
+ */
272
+ experimental_providerMetadata?: ProviderMetadata;
298
273
  }
299
274
  /**
300
275
  Image content part of a prompt. It contains an image.
@@ -312,6 +287,91 @@ interface ImagePart {
312
287
  Optional mime type of the image.
313
288
  */
314
289
  mimeType?: string;
290
+ /**
291
+ Additional provider-specific metadata. They are passed through
292
+ to the provider from the AI SDK and enable provider-specific
293
+ functionality that can be fully encapsulated in the provider.
294
+ */
295
+ providerOptions?: ProviderOptions;
296
+ /**
297
+ @deprecated Use `providerOptions` instead.
298
+ */
299
+ experimental_providerMetadata?: ProviderMetadata;
300
+ }
301
+ /**
302
+ File content part of a prompt. It contains a file.
303
+ */
304
+ interface FilePart {
305
+ type: 'file';
306
+ /**
307
+ File data. Can either be:
308
+
309
+ - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
310
+ - URL: a URL that points to the image
311
+ */
312
+ data: DataContent | URL;
313
+ /**
314
+ Optional filename of the file.
315
+ */
316
+ filename?: string;
317
+ /**
318
+ Mime type of the file.
319
+ */
320
+ mimeType: string;
321
+ /**
322
+ Additional provider-specific metadata. They are passed through
323
+ to the provider from the AI SDK and enable provider-specific
324
+ functionality that can be fully encapsulated in the provider.
325
+ */
326
+ providerOptions?: ProviderOptions;
327
+ /**
328
+ @deprecated Use `providerOptions` instead.
329
+ */
330
+ experimental_providerMetadata?: ProviderMetadata;
331
+ }
332
+ /**
333
+ * Reasoning content part of a prompt. It contains a reasoning.
334
+ */
335
+ interface ReasoningPart {
336
+ type: 'reasoning';
337
+ /**
338
+ The reasoning text.
339
+ */
340
+ text: string;
341
+ /**
342
+ An optional signature for verifying that the reasoning originated from the model.
343
+ */
344
+ signature?: string;
345
+ /**
346
+ Additional provider-specific metadata. They are passed through
347
+ to the provider from the AI SDK and enable provider-specific
348
+ functionality that can be fully encapsulated in the provider.
349
+ */
350
+ providerOptions?: ProviderOptions;
351
+ /**
352
+ @deprecated Use `providerOptions` instead.
353
+ */
354
+ experimental_providerMetadata?: ProviderMetadata;
355
+ }
356
+ /**
357
+ Redacted reasoning content part of a prompt.
358
+ */
359
+ interface RedactedReasoningPart {
360
+ type: 'redacted-reasoning';
361
+ /**
362
+ Redacted reasoning data.
363
+ */
364
+ data: string;
365
+ /**
366
+ Additional provider-specific metadata. They are passed through
367
+ to the provider from the AI SDK and enable provider-specific
368
+ functionality that can be fully encapsulated in the provider.
369
+ */
370
+ providerOptions?: ProviderOptions;
371
+ /**
372
+ @deprecated Use `providerOptions` instead.
373
+ */
374
+ experimental_providerMetadata?: ProviderMetadata;
315
375
  }
316
376
  /**
317
377
  Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
@@ -330,6 +390,16 @@ interface ToolCallPart {
330
390
  Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
331
391
  */
332
392
  args: unknown;
393
+ /**
394
+ Additional provider-specific metadata. They are passed through
395
+ to the provider from the AI SDK and enable provider-specific
396
+ functionality that can be fully encapsulated in the provider.
397
+ */
398
+ providerOptions?: ProviderOptions;
399
+ /**
400
+ @deprecated Use `providerOptions` instead.
401
+ */
402
+ experimental_providerMetadata?: ProviderMetadata;
333
403
  }
334
404
  /**
335
405
  Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
@@ -349,16 +419,25 @@ interface ToolResultPart {
349
419
  */
350
420
  result: unknown;
351
421
  /**
422
+ Multi-part content of the tool result. Only for tools that support multipart results.
423
+ */
424
+ experimental_content?: ToolResultContent;
425
+ /**
352
426
  Optional flag if the result is an error or an error message.
353
427
  */
354
428
  isError?: boolean;
429
+ /**
430
+ Additional provider-specific metadata. They are passed through
431
+ to the provider from the AI SDK and enable provider-specific
432
+ functionality that can be fully encapsulated in the provider.
433
+ */
434
+ providerOptions?: ProviderOptions;
435
+ /**
436
+ @deprecated Use `providerOptions` instead.
437
+ */
438
+ experimental_providerMetadata?: ProviderMetadata;
355
439
  }
356
440
 
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
441
  /**
363
442
  A system message. It can contain system information.
364
443
 
@@ -369,6 +448,16 @@ type CoreMessage = CoreSystemMessage | CoreUserMessage | CoreAssistantMessage |
369
448
  type CoreSystemMessage = {
370
449
  role: 'system';
371
450
  content: string;
451
+ /**
452
+ Additional provider-specific metadata. They are passed through
453
+ to the provider from the AI SDK and enable provider-specific
454
+ functionality that can be fully encapsulated in the provider.
455
+ */
456
+ providerOptions?: ProviderOptions;
457
+ /**
458
+ @deprecated Use `providerOptions` instead.
459
+ */
460
+ experimental_providerMetadata?: ProviderMetadata;
372
461
  };
373
462
  /**
374
463
  A user message. It can contain text or a combination of text and images.
@@ -376,36 +465,73 @@ A user message. It can contain text or a combination of text and images.
376
465
  type CoreUserMessage = {
377
466
  role: 'user';
378
467
  content: UserContent;
468
+ /**
469
+ Additional provider-specific metadata. They are passed through
470
+ to the provider from the AI SDK and enable provider-specific
471
+ functionality that can be fully encapsulated in the provider.
472
+ */
473
+ providerOptions?: ProviderOptions;
474
+ /**
475
+ @deprecated Use `providerOptions` instead.
476
+ */
477
+ experimental_providerMetadata?: ProviderMetadata;
379
478
  };
380
479
  /**
381
480
  Content of a user message. It can be a string or an array of text and image parts.
382
481
  */
383
- type UserContent = string | Array<TextPart | ImagePart>;
482
+ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
384
483
  /**
385
484
  An assistant message. It can contain text, tool calls, or a combination of text and tool calls.
386
485
  */
387
486
  type CoreAssistantMessage = {
388
487
  role: 'assistant';
389
488
  content: AssistantContent;
489
+ /**
490
+ Additional provider-specific metadata. They are passed through
491
+ to the provider from the AI SDK and enable provider-specific
492
+ functionality that can be fully encapsulated in the provider.
493
+ */
494
+ providerOptions?: ProviderOptions;
495
+ /**
496
+ @deprecated Use `providerOptions` instead.
497
+ */
498
+ experimental_providerMetadata?: ProviderMetadata;
390
499
  };
391
500
  /**
392
- Content of an assistant message. It can be a string or an array of text and tool call parts.
501
+ Content of an assistant message.
502
+ It can be a string or an array of text, image, reasoning, redacted reasoning, and tool call parts.
393
503
  */
394
- type AssistantContent = string | Array<TextPart | ToolCallPart>;
504
+ type AssistantContent = string | Array<TextPart | FilePart | ReasoningPart | RedactedReasoningPart | ToolCallPart>;
395
505
  /**
396
506
  A tool message. It contains the result of one or more tool calls.
397
507
  */
398
508
  type CoreToolMessage = {
399
509
  role: 'tool';
400
510
  content: ToolContent;
511
+ /**
512
+ Additional provider-specific metadata. They are passed through
513
+ to the provider from the AI SDK and enable provider-specific
514
+ functionality that can be fully encapsulated in the provider.
515
+ */
516
+ providerOptions?: ProviderOptions;
517
+ /**
518
+ @deprecated Use `providerOptions` instead.
519
+ */
520
+ experimental_providerMetadata?: ProviderMetadata;
401
521
  };
402
522
  /**
403
523
  Content of a tool message. It is an array of tool result parts.
404
524
  */
405
525
  type ToolContent = Array<ToolResultPart>;
526
+ /**
527
+ A message that can be used in the `messages` field of a prompt.
528
+ It can be a user message, an assistant message, or a tool message.
529
+ */
530
+ type CoreMessage = CoreSystemMessage | CoreUserMessage | CoreAssistantMessage | CoreToolMessage;
406
531
 
407
532
  /**
408
- Prompt part of the AI function options. It contains a system message, a simple text prompt, or a list of messages.
533
+ Prompt part of the AI function options.
534
+ It contains a system message, a simple text prompt, or a list of messages.
409
535
  */
410
536
  type Prompt = {
411
537
  /**
@@ -417,9 +543,9 @@ type Prompt = {
417
543
  */
418
544
  prompt?: string;
419
545
  /**
420
- A list of messsages. You can either use `prompt` or `messages` but not both.
546
+ A list of messages. You can either use `prompt` or `messages` but not both.
421
547
  */
422
- messages?: Array<CoreMessage>;
548
+ messages?: Array<CoreMessage> | Array<Omit<Message, 'id'>>;
423
549
  };
424
550
 
425
551
  type Streamable = ReactNode | Promise<ReactNode>;
@@ -460,7 +586,7 @@ type RenderResult = {
460
586
  */
461
587
  declare function streamUI<TOOLS extends {
462
588
  [name: string]: z.ZodTypeAny;
463
- } = {}>({ model, tools, system, prompt, messages, maxRetries, abortSignal, initial, text, ...settings }: CallSettings & Prompt & {
589
+ } = {}>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, initial, text, experimental_providerMetadata, providerOptions, onFinish, ...settings }: CallSettings & Prompt & {
464
590
  /**
465
591
  * The language model to use.
466
592
  */
@@ -471,49 +597,155 @@ declare function streamUI<TOOLS extends {
471
597
  tools?: {
472
598
  [name in keyof TOOLS]: RenderTool<TOOLS[name]>;
473
599
  };
600
+ /**
601
+ * The tool choice strategy. Default: 'auto'.
602
+ */
603
+ toolChoice?: ToolChoice<TOOLS>;
474
604
  text?: RenderText;
475
605
  initial?: ReactNode;
606
+ /**
607
+ Additional provider-specific options. They are passed through
608
+ to the provider from the AI SDK and enable provider-specific
609
+ functionality that can be fully encapsulated in the provider.
610
+ */
611
+ providerOptions?: ProviderOptions;
612
+ /**
613
+ @deprecated Use `providerOptions` instead.
614
+ */
615
+ experimental_providerMetadata?: ProviderMetadata;
616
+ /**
617
+ * Callback that is called when the LLM response and the final object validation are finished.
618
+ */
619
+ onFinish?: (event: {
620
+ /**
621
+ * The reason why the generation finished.
622
+ */
623
+ finishReason: FinishReason;
624
+ /**
625
+ * The token usage of the generated response.
626
+ */
627
+ usage: LanguageModelUsage;
628
+ /**
629
+ * The final ui node that was generated.
630
+ */
631
+ value: ReactNode;
632
+ /**
633
+ * Warnings from the model provider (e.g. unsupported settings)
634
+ */
635
+ warnings?: CallWarning[];
636
+ /**
637
+ * Optional raw response data.
638
+ */
639
+ rawResponse?: {
640
+ /**
641
+ * Response headers.
642
+ */
643
+ headers?: Record<string, string>;
644
+ };
645
+ }) => Promise<void> | void;
476
646
  }): Promise<RenderResult>;
477
647
 
478
- declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, onSetAIState, onGetUIState, }: {
479
- actions: Actions;
480
- initialAIState?: AIState;
481
- initialUIState?: UIState;
648
+ type StreamableUIWrapper = {
482
649
  /**
483
- * This function is called whenever the AI state is updated by an Action.
484
- * You can use this to persist the AI state to a database, or to send it to a
485
- * logging service.
650
+ * The value of the streamable UI. This can be returned from a Server Action and received by the client.
486
651
  */
487
- onSetAIState?: OnSetAIState<AIState>;
652
+ readonly value: React.ReactNode;
488
653
  /**
489
- * This function is used to retrieve the UI state based on the AI state.
490
- * For example, to render the initial UI state based on a given AI state, or
491
- * to sync the UI state when the application is already loaded.
492
- *
493
- * If returning `undefined`, the client side UI state will not be updated.
494
- *
495
- * This function must be annotated with the `"use server"` directive.
654
+ * This method updates the current UI node. It takes a new UI node and replaces the old one.
655
+ */
656
+ update(value: React.ReactNode): StreamableUIWrapper;
657
+ /**
658
+ * This method is used to append a new UI node to the end of the old one.
659
+ * Once appended a new UI node, the previous UI node cannot be updated anymore.
496
660
  *
497
661
  * @example
498
- * ```tsx
499
- * onGetUIState: async () => {
500
- * 'use server';
662
+ * ```jsx
663
+ * const ui = createStreamableUI(<div>hello</div>)
664
+ * ui.append(<div>world</div>)
501
665
  *
502
- * const currentAIState = getAIState();
503
- * const externalAIState = await loadAIStateFromDatabase();
666
+ * // The UI node will be:
667
+ * // <>
668
+ * // <div>hello</div>
669
+ * // <div>world</div>
670
+ * // </>
671
+ * ```
672
+ */
673
+ append(value: React.ReactNode): StreamableUIWrapper;
674
+ /**
675
+ * This method is used to signal that there is an error in the UI stream.
676
+ * It will be thrown on the client side and caught by the nearest error boundary component.
677
+ */
678
+ error(error: any): StreamableUIWrapper;
679
+ /**
680
+ * 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.
681
+ * Once called, the UI node cannot be updated or appended anymore.
504
682
  *
505
- * if (currentAIState === externalAIState) return undefined;
683
+ * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
684
+ */
685
+ done(...args: [React.ReactNode] | []): StreamableUIWrapper;
686
+ };
687
+ /**
688
+ * Create a piece of changeable UI that can be streamed to the client.
689
+ * On the client side, it can be rendered as a normal React node.
690
+ */
691
+ declare function createStreamableUI(initialValue?: React.ReactNode): StreamableUIWrapper;
692
+
693
+ declare const __internal_curr: unique symbol;
694
+ declare const __internal_error: unique symbol;
695
+ /**
696
+ * StreamableValue is a value that can be streamed over the network via AI Actions.
697
+ * To read the streamed values, use the `readStreamableValue` or `useStreamableValue` APIs.
698
+ */
699
+ type StreamableValue<T = any, E = any> = {
700
+ [__internal_curr]?: T;
701
+ [__internal_error]?: E;
702
+ };
703
+
704
+ /**
705
+ * Create a wrapped, changeable value that can be streamed to the client.
706
+ * On the client side, the value can be accessed via the readStreamableValue() API.
707
+ */
708
+ declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): StreamableValueWrapper<T, E>;
709
+ type StreamableValueWrapper<T, E> = {
710
+ /**
711
+ * The value of the streamable. This can be returned from a Server Action and
712
+ * received by the client. To read the streamed values, use the
713
+ * `readStreamableValue` or `useStreamableValue` APIs.
714
+ */
715
+ readonly value: StreamableValue<T, E>;
716
+ /**
717
+ * This method updates the current value with a new one.
718
+ */
719
+ update(value: T): StreamableValueWrapper<T, E>;
720
+ /**
721
+ * This method is used to append a delta string to the current value. It
722
+ * requires the current value of the streamable to be a string.
506
723
  *
507
- * // Update current AI state and return the new UI state
508
- * const state = getMutableAIState()
509
- * state.done(externalAIState)
724
+ * @example
725
+ * ```jsx
726
+ * const streamable = createStreamableValue('hello');
727
+ * streamable.append(' world');
510
728
  *
511
- * return <div>...</div>;
512
- * }
729
+ * // The value will be 'hello world'
513
730
  * ```
514
731
  */
515
- onGetUIState?: OnGetUIState<UIState>;
516
- }): AIProvider<AIState, UIState, Actions>;
732
+ append(value: T): StreamableValueWrapper<T, E>;
733
+ /**
734
+ * This method is used to signal that there is an error in the value stream.
735
+ * It will be thrown on the client side when consumed via
736
+ * `readStreamableValue` or `useStreamableValue`.
737
+ */
738
+ error(error: any): StreamableValueWrapper<T, E>;
739
+ /**
740
+ * This method marks the value as finalized. You can either call it without
741
+ * any parameters or with a new value as the final state.
742
+ * Once called, the value cannot be updated or appended anymore.
743
+ *
744
+ * This method is always **required** to be called, otherwise the response
745
+ * will be stuck in a loading state.
746
+ */
747
+ done(...args: [T] | []): StreamableValueWrapper<T, E>;
748
+ };
517
749
 
518
750
  /**
519
751
  * `readStreamableValue` takes a streamable value created via the `createStreamableValue().value` API,
@@ -546,6 +778,7 @@ declare function createAI<AIState = any, UIState = any, Actions extends AIAction
546
778
  * This logs out 1, 2, 3 on console.
547
779
  */
548
780
  declare function readStreamableValue<T = unknown>(streamableValue: StreamableValue<T>): AsyncIterable<T | undefined>;
781
+
549
782
  /**
550
783
  * `useStreamableValue` is a React hook that takes a streamable value created via the `createStreamableValue().value` API,
551
784
  * and returns the current value, error, and pending state.
@@ -577,4 +810,4 @@ declare function useAIState<AI extends AIProvider = any>(key: keyof InferAIState
577
810
  declare function useActions<AI extends AIProvider = any>(): InferActions<AI, any>;
578
811
  declare function useSyncUIState(): () => Promise<void>;
579
812
 
580
- export { StreamableValue, createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, readStreamableValue, render, streamUI, useAIState, useActions, useStreamableValue, useSyncUIState, useUIState };
813
+ export { StreamableValue, createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, readStreamableValue, streamUI, useAIState, useActions, useStreamableValue, useSyncUIState, useUIState };