@ai-sdk/rsc 1.0.0-canary.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,300 @@
1
+ import { LanguageModelV2 } from '@ai-sdk/provider';
2
+ import { ReactNode } from 'react';
3
+ import { z } from 'zod';
4
+ import { CallSettings, Prompt, ToolChoice, ProviderOptions, ProviderMetadata, FinishReason, LanguageModelUsage, CallWarning } from 'ai';
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);
28
+ };
29
+
30
+ /**
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]>;
60
+
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;
65
+ /**
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.
69
+ */
70
+ onSetAIState?: OnSetAIState<AIState>;
71
+ /**
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
+ * ```
97
+ */
98
+ onGetUIState?: OnGetUIState<UIState>;
99
+ }): AIProvider<AIState, UIState, Actions>;
100
+
101
+ type Streamable = ReactNode | Promise<ReactNode>;
102
+ type Renderer<T extends Array<any>> = (...args: T) => Streamable | Generator<Streamable, Streamable, void> | AsyncGenerator<Streamable, Streamable, void>;
103
+ type RenderTool<PARAMETERS extends z.ZodTypeAny = any> = {
104
+ description?: string;
105
+ parameters: PARAMETERS;
106
+ generate?: Renderer<[
107
+ z.infer<PARAMETERS>,
108
+ {
109
+ toolName: string;
110
+ toolCallId: string;
111
+ }
112
+ ]>;
113
+ };
114
+ type RenderText = Renderer<[
115
+ {
116
+ /**
117
+ * The full text content from the model so far.
118
+ */
119
+ content: string;
120
+ /**
121
+ * The new appended text content from the model since the last `text` call.
122
+ */
123
+ delta: string;
124
+ /**
125
+ * Whether the model is done generating text.
126
+ * If `true`, the `content` will be the final output and this call will be the last.
127
+ */
128
+ done: boolean;
129
+ }
130
+ ]>;
131
+ type RenderResult = {
132
+ value: ReactNode;
133
+ } & Awaited<ReturnType<LanguageModelV2['doStream']>>;
134
+ /**
135
+ * `streamUI` is a helper function to create a streamable UI from LLMs.
136
+ */
137
+ declare function streamUI<TOOLS extends {
138
+ [name: string]: z.ZodTypeAny;
139
+ } = {}>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, initial, text, experimental_providerMetadata, providerOptions, onFinish, ...settings }: CallSettings & Prompt & {
140
+ /**
141
+ * The language model to use.
142
+ */
143
+ model: LanguageModelV2;
144
+ /**
145
+ * The tools that the model can call. The model needs to support calling tools.
146
+ */
147
+ tools?: {
148
+ [name in keyof TOOLS]: RenderTool<TOOLS[name]>;
149
+ };
150
+ /**
151
+ * The tool choice strategy. Default: 'auto'.
152
+ */
153
+ toolChoice?: ToolChoice<TOOLS>;
154
+ text?: RenderText;
155
+ initial?: ReactNode;
156
+ /**
157
+ Additional provider-specific options. They are passed through
158
+ to the provider from the AI SDK and enable provider-specific
159
+ functionality that can be fully encapsulated in the provider.
160
+ */
161
+ providerOptions?: ProviderOptions;
162
+ /**
163
+ @deprecated Use `providerOptions` instead.
164
+ */
165
+ experimental_providerMetadata?: ProviderMetadata;
166
+ /**
167
+ * Callback that is called when the LLM response and the final object validation are finished.
168
+ */
169
+ onFinish?: (event: {
170
+ /**
171
+ * The reason why the generation finished.
172
+ */
173
+ finishReason: FinishReason;
174
+ /**
175
+ * The token usage of the generated response.
176
+ */
177
+ usage: LanguageModelUsage;
178
+ /**
179
+ * The final ui node that was generated.
180
+ */
181
+ value: ReactNode;
182
+ /**
183
+ * Warnings from the model provider (e.g. unsupported settings)
184
+ */
185
+ warnings?: CallWarning[];
186
+ /**
187
+ * Optional raw response data.
188
+ */
189
+ rawResponse?: {
190
+ /**
191
+ * Response headers.
192
+ */
193
+ headers?: Record<string, string>;
194
+ };
195
+ }) => Promise<void> | void;
196
+ }): Promise<RenderResult>;
197
+
198
+ type StreamableUIWrapper = {
199
+ /**
200
+ * The value of the streamable UI. This can be returned from a Server Action and received by the client.
201
+ */
202
+ readonly value: React.ReactNode;
203
+ /**
204
+ * This method updates the current UI node. It takes a new UI node and replaces the old one.
205
+ */
206
+ update(value: React.ReactNode): StreamableUIWrapper;
207
+ /**
208
+ * This method is used to append a new UI node to the end of the old one.
209
+ * Once appended a new UI node, the previous UI node cannot be updated anymore.
210
+ *
211
+ * @example
212
+ * ```jsx
213
+ * const ui = createStreamableUI(<div>hello</div>)
214
+ * ui.append(<div>world</div>)
215
+ *
216
+ * // The UI node will be:
217
+ * // <>
218
+ * // <div>hello</div>
219
+ * // <div>world</div>
220
+ * // </>
221
+ * ```
222
+ */
223
+ append(value: React.ReactNode): StreamableUIWrapper;
224
+ /**
225
+ * This method is used to signal that there is an error in the UI stream.
226
+ * It will be thrown on the client side and caught by the nearest error boundary component.
227
+ */
228
+ error(error: any): StreamableUIWrapper;
229
+ /**
230
+ * 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.
231
+ * Once called, the UI node cannot be updated or appended anymore.
232
+ *
233
+ * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
234
+ */
235
+ done(...args: [React.ReactNode] | []): StreamableUIWrapper;
236
+ };
237
+ /**
238
+ * Create a piece of changeable UI that can be streamed to the client.
239
+ * On the client side, it can be rendered as a normal React node.
240
+ */
241
+ declare function createStreamableUI(initialValue?: React.ReactNode): StreamableUIWrapper;
242
+
243
+ declare const __internal_curr: unique symbol;
244
+ declare const __internal_error: unique symbol;
245
+ /**
246
+ * StreamableValue is a value that can be streamed over the network via AI Actions.
247
+ * To read the streamed values, use the `readStreamableValue` or `useStreamableValue` APIs.
248
+ */
249
+ type StreamableValue<T = any, E = any> = {
250
+ [__internal_curr]?: T;
251
+ [__internal_error]?: E;
252
+ };
253
+
254
+ /**
255
+ * Create a wrapped, changeable value that can be streamed to the client.
256
+ * On the client side, the value can be accessed via the readStreamableValue() API.
257
+ */
258
+ declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): StreamableValueWrapper<T, E>;
259
+ type StreamableValueWrapper<T, E> = {
260
+ /**
261
+ * The value of the streamable. This can be returned from a Server Action and
262
+ * received by the client. To read the streamed values, use the
263
+ * `readStreamableValue` or `useStreamableValue` APIs.
264
+ */
265
+ readonly value: StreamableValue<T, E>;
266
+ /**
267
+ * This method updates the current value with a new one.
268
+ */
269
+ update(value: T): StreamableValueWrapper<T, E>;
270
+ /**
271
+ * This method is used to append a delta string to the current value. It
272
+ * requires the current value of the streamable to be a string.
273
+ *
274
+ * @example
275
+ * ```jsx
276
+ * const streamable = createStreamableValue('hello');
277
+ * streamable.append(' world');
278
+ *
279
+ * // The value will be 'hello world'
280
+ * ```
281
+ */
282
+ append(value: T): StreamableValueWrapper<T, E>;
283
+ /**
284
+ * This method is used to signal that there is an error in the value stream.
285
+ * It will be thrown on the client side when consumed via
286
+ * `readStreamableValue` or `useStreamableValue`.
287
+ */
288
+ error(error: any): StreamableValueWrapper<T, E>;
289
+ /**
290
+ * This method marks the value as finalized. You can either call it without
291
+ * any parameters or with a new value as the final state.
292
+ * Once called, the value cannot be updated or appended anymore.
293
+ *
294
+ * This method is always **required** to be called, otherwise the response
295
+ * will be stuck in a loading state.
296
+ */
297
+ done(...args: [T] | []): StreamableValueWrapper<T, E>;
298
+ };
299
+
300
+ export { createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, streamUI };