@ai-sdk/rsc 1.0.0-canary.10
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.
- package/CHANGELOG.md +125 -0
- package/LICENSE +13 -0
- package/README.md +3 -0
- package/dist/index.d.ts +361 -0
- package/dist/index.mjs +18 -0
- package/dist/rsc-client.d.mts +1 -0
- package/dist/rsc-client.mjs +18 -0
- package/dist/rsc-client.mjs.map +1 -0
- package/dist/rsc-server.d.mts +296 -0
- package/dist/rsc-server.mjs +739 -0
- package/dist/rsc-server.mjs.map +1 -0
- package/dist/rsc-shared.d.mts +101 -0
- package/dist/rsc-shared.mjs +308 -0
- package/dist/rsc-shared.mjs.map +1 -0
- package/package.json +95 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @ai-sdk/rsc
|
|
2
|
+
|
|
3
|
+
## 1.0.0-canary.10
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [e86be6f]
|
|
8
|
+
- @ai-sdk/provider@2.0.0-canary.9
|
|
9
|
+
- ai@5.0.0-canary.12
|
|
10
|
+
- @ai-sdk/provider-utils@3.0.0-canary.10
|
|
11
|
+
|
|
12
|
+
## 1.0.0-canary.9
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies [95857aa]
|
|
17
|
+
- Updated dependencies [7ea4132]
|
|
18
|
+
- Updated dependencies [8e64e9c]
|
|
19
|
+
- @ai-sdk/provider@2.0.0-canary.8
|
|
20
|
+
- ai@5.0.0-canary.11
|
|
21
|
+
- @ai-sdk/provider-utils@3.0.0-canary.9
|
|
22
|
+
|
|
23
|
+
## 1.0.0-canary.8
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- Updated dependencies [d8aeaef]
|
|
28
|
+
- Updated dependencies [3e10408]
|
|
29
|
+
- ai@5.0.0-canary.10
|
|
30
|
+
|
|
31
|
+
## 1.0.0-canary.7
|
|
32
|
+
|
|
33
|
+
### Patch Changes
|
|
34
|
+
|
|
35
|
+
- Updated dependencies [a847c3e]
|
|
36
|
+
- Updated dependencies [5d142ab]
|
|
37
|
+
- Updated dependencies [b6b43c7]
|
|
38
|
+
- Updated dependencies [cb9c9e4]
|
|
39
|
+
- Updated dependencies [8aa9e20]
|
|
40
|
+
- Updated dependencies [3795467]
|
|
41
|
+
- Updated dependencies [b32e192]
|
|
42
|
+
- ai@5.0.0-canary.9
|
|
43
|
+
- @ai-sdk/provider-utils@3.0.0-canary.8
|
|
44
|
+
- @ai-sdk/provider@2.0.0-canary.7
|
|
45
|
+
|
|
46
|
+
## 1.0.0-canary.6
|
|
47
|
+
|
|
48
|
+
### Patch Changes
|
|
49
|
+
|
|
50
|
+
- Updated dependencies [5d1e3ba]
|
|
51
|
+
- Updated dependencies [26735b5]
|
|
52
|
+
- Updated dependencies [443d8ec]
|
|
53
|
+
- Updated dependencies [14c9410]
|
|
54
|
+
- Updated dependencies [7827a49]
|
|
55
|
+
- Updated dependencies [bd8a36c]
|
|
56
|
+
- Updated dependencies [d9c98f4]
|
|
57
|
+
- Updated dependencies [c4a2fec]
|
|
58
|
+
- Updated dependencies [b6f9f3c]
|
|
59
|
+
- Updated dependencies [0054544]
|
|
60
|
+
- Updated dependencies [9e9c809]
|
|
61
|
+
- Updated dependencies [32831c6]
|
|
62
|
+
- Updated dependencies [d0f9495]
|
|
63
|
+
- Updated dependencies [92c8e66]
|
|
64
|
+
- Updated dependencies [fd65bc6]
|
|
65
|
+
- Updated dependencies [393138b]
|
|
66
|
+
- Updated dependencies [7182d14]
|
|
67
|
+
- Updated dependencies [5bdff05]
|
|
68
|
+
- ai@5.0.0-canary.8
|
|
69
|
+
- @ai-sdk/provider@2.0.0-canary.6
|
|
70
|
+
- @ai-sdk/provider-utils@3.0.0-canary.7
|
|
71
|
+
|
|
72
|
+
## 1.0.0-canary.5
|
|
73
|
+
|
|
74
|
+
### Patch Changes
|
|
75
|
+
|
|
76
|
+
- Updated dependencies [411e483]
|
|
77
|
+
- Updated dependencies [0b78e17]
|
|
78
|
+
- Updated dependencies [79457bd]
|
|
79
|
+
- Updated dependencies [6fba4c7]
|
|
80
|
+
- Updated dependencies [3e3b9df]
|
|
81
|
+
- Updated dependencies [ad80501]
|
|
82
|
+
- Updated dependencies [1766ede]
|
|
83
|
+
- Updated dependencies [f10304b]
|
|
84
|
+
- @ai-sdk/provider@2.0.0-canary.5
|
|
85
|
+
- ai@5.0.0-canary.7
|
|
86
|
+
- @ai-sdk/provider-utils@3.0.0-canary.6
|
|
87
|
+
|
|
88
|
+
## 1.0.0-canary.4
|
|
89
|
+
|
|
90
|
+
### Patch Changes
|
|
91
|
+
|
|
92
|
+
- Updated dependencies [6f6bb89]
|
|
93
|
+
- @ai-sdk/provider@2.0.0-canary.4
|
|
94
|
+
- ai@5.0.0-canary.6
|
|
95
|
+
- @ai-sdk/provider-utils@3.0.0-canary.5
|
|
96
|
+
|
|
97
|
+
## 1.0.0-canary.3
|
|
98
|
+
|
|
99
|
+
### Patch Changes
|
|
100
|
+
|
|
101
|
+
- Updated dependencies [d1a1aa1]
|
|
102
|
+
- Updated dependencies [b71fe8d]
|
|
103
|
+
- Updated dependencies [d91b50d]
|
|
104
|
+
- @ai-sdk/provider@2.0.0-canary.3
|
|
105
|
+
- ai@5.0.0-canary.5
|
|
106
|
+
- @ai-sdk/provider-utils@3.0.0-canary.4
|
|
107
|
+
|
|
108
|
+
## 1.0.0-canary.2
|
|
109
|
+
|
|
110
|
+
### Major Changes
|
|
111
|
+
|
|
112
|
+
- e1cbf8a: chore(@ai-sdk/rsc): extract to separate package
|
|
113
|
+
|
|
114
|
+
### Patch Changes
|
|
115
|
+
|
|
116
|
+
- Updated dependencies [e1cbf8a]
|
|
117
|
+
- Updated dependencies [225f087]
|
|
118
|
+
- Updated dependencies [a166433]
|
|
119
|
+
- Updated dependencies [abf9a79]
|
|
120
|
+
- Updated dependencies [9f95b35]
|
|
121
|
+
- Updated dependencies [0a87932]
|
|
122
|
+
- Updated dependencies [6dc848c]
|
|
123
|
+
- ai@5.0.0-canary.4
|
|
124
|
+
- @ai-sdk/provider-utils@3.0.0-canary.3
|
|
125
|
+
- @ai-sdk/provider@2.0.0-canary.2
|
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023 Vercel, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { LanguageModelV2 } from '@ai-sdk/provider';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { Schema, CallSettings, Prompt, ToolChoice, ProviderOptions, 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 InferUIState<T, Fallback> = T extends AIProvider<any, infer UIState, any> ? UIState : Fallback;
|
|
18
|
+
type InferActions<T, Fallback> = T extends AIProvider<any, any, infer Actions> ? Actions : Fallback;
|
|
19
|
+
type OnSetAIState<S> = ({ key, state, done, }: {
|
|
20
|
+
key: string | number | symbol | undefined;
|
|
21
|
+
state: S;
|
|
22
|
+
done: boolean;
|
|
23
|
+
}) => void | Promise<void>;
|
|
24
|
+
type OnGetUIState<S> = AIAction<void, S | undefined>;
|
|
25
|
+
type ValueOrUpdater<T> = T | ((current: T) => T);
|
|
26
|
+
type MutableAIState<AIState> = {
|
|
27
|
+
get: () => AIState;
|
|
28
|
+
update: (newState: ValueOrUpdater<AIState>) => void;
|
|
29
|
+
done: ((newState: AIState) => void) | (() => void);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Get the current AI state.
|
|
34
|
+
* If `key` is provided, it will return the value of the specified key in the
|
|
35
|
+
* AI state, if it's an object. If it's not an object, it will throw an error.
|
|
36
|
+
*
|
|
37
|
+
* @example const state = getAIState() // Get the entire AI state
|
|
38
|
+
* @example const field = getAIState('key') // Get the value of the key
|
|
39
|
+
*/
|
|
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]>;
|
|
42
|
+
/**
|
|
43
|
+
* Get the mutable AI state. Note that you must call `.done()` when finishing
|
|
44
|
+
* updating the AI state.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* const state = getMutableAIState()
|
|
49
|
+
* state.update({ ...state.get(), key: 'value' })
|
|
50
|
+
* state.update((currentState) => ({ ...currentState, key: 'value' }))
|
|
51
|
+
* state.done()
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```tsx
|
|
56
|
+
* const state = getMutableAIState()
|
|
57
|
+
* state.done({ ...state.get(), key: 'value' }) // Done with a new state
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
declare function getMutableAIState<AI extends AIProvider = any>(): MutableAIState<InferAIState<AI, any>>;
|
|
61
|
+
declare function getMutableAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): MutableAIState<InferAIState<AI, any>[typeof key]>;
|
|
62
|
+
|
|
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;
|
|
67
|
+
/**
|
|
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.
|
|
71
|
+
*/
|
|
72
|
+
onSetAIState?: OnSetAIState<AIState>;
|
|
73
|
+
/**
|
|
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.
|
|
77
|
+
*
|
|
78
|
+
* If returning `undefined`, the client side UI state will not be updated.
|
|
79
|
+
*
|
|
80
|
+
* This function must be annotated with the `"use server"` directive.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```tsx
|
|
84
|
+
* onGetUIState: async () => {
|
|
85
|
+
* 'use server';
|
|
86
|
+
*
|
|
87
|
+
* const currentAIState = getAIState();
|
|
88
|
+
* const externalAIState = await loadAIStateFromDatabase();
|
|
89
|
+
*
|
|
90
|
+
* if (currentAIState === externalAIState) return undefined;
|
|
91
|
+
*
|
|
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
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
onGetUIState?: OnGetUIState<UIState>;
|
|
101
|
+
}): AIProvider<AIState, UIState, Actions>;
|
|
102
|
+
|
|
103
|
+
type Streamable = ReactNode | Promise<ReactNode>;
|
|
104
|
+
type Renderer<T extends Array<any>> = (...args: T) => Streamable | Generator<Streamable, Streamable, void> | AsyncGenerator<Streamable, Streamable, void>;
|
|
105
|
+
type RenderTool<PARAMETERS extends z.Schema | Schema = any> = {
|
|
106
|
+
description?: string;
|
|
107
|
+
parameters: PARAMETERS;
|
|
108
|
+
generate?: Renderer<[
|
|
109
|
+
PARAMETERS extends z.Schema ? z.infer<PARAMETERS> : PARAMETERS extends Schema<infer T> ? T : never,
|
|
110
|
+
{
|
|
111
|
+
toolName: string;
|
|
112
|
+
toolCallId: string;
|
|
113
|
+
}
|
|
114
|
+
]>;
|
|
115
|
+
};
|
|
116
|
+
type RenderText = Renderer<[
|
|
117
|
+
{
|
|
118
|
+
/**
|
|
119
|
+
* The full text content from the model so far.
|
|
120
|
+
*/
|
|
121
|
+
content: string;
|
|
122
|
+
/**
|
|
123
|
+
* The new appended text content from the model since the last `text` call.
|
|
124
|
+
*/
|
|
125
|
+
delta: string;
|
|
126
|
+
/**
|
|
127
|
+
* Whether the model is done generating text.
|
|
128
|
+
* If `true`, the `content` will be the final output and this call will be the last.
|
|
129
|
+
*/
|
|
130
|
+
done: boolean;
|
|
131
|
+
}
|
|
132
|
+
]>;
|
|
133
|
+
type RenderResult = {
|
|
134
|
+
value: ReactNode;
|
|
135
|
+
} & Awaited<ReturnType<LanguageModelV2['doStream']>>;
|
|
136
|
+
/**
|
|
137
|
+
* `streamUI` is a helper function to create a streamable UI from LLMs.
|
|
138
|
+
*/
|
|
139
|
+
declare function streamUI<TOOLS extends {
|
|
140
|
+
[name: string]: z.Schema | Schema;
|
|
141
|
+
} = {}>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, initial, text, providerOptions, onFinish, ...settings }: CallSettings & Prompt & {
|
|
142
|
+
/**
|
|
143
|
+
* The language model to use.
|
|
144
|
+
*/
|
|
145
|
+
model: LanguageModelV2;
|
|
146
|
+
/**
|
|
147
|
+
* The tools that the model can call. The model needs to support calling tools.
|
|
148
|
+
*/
|
|
149
|
+
tools?: {
|
|
150
|
+
[name in keyof TOOLS]: RenderTool<TOOLS[name]>;
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* The tool choice strategy. Default: 'auto'.
|
|
154
|
+
*/
|
|
155
|
+
toolChoice?: ToolChoice<TOOLS>;
|
|
156
|
+
text?: RenderText;
|
|
157
|
+
initial?: ReactNode;
|
|
158
|
+
/**
|
|
159
|
+
Additional provider-specific options. They are passed through
|
|
160
|
+
to the provider from the AI SDK and enable provider-specific
|
|
161
|
+
functionality that can be fully encapsulated in the provider.
|
|
162
|
+
*/
|
|
163
|
+
providerOptions?: ProviderOptions;
|
|
164
|
+
/**
|
|
165
|
+
* Callback that is called when the LLM response and the final object validation are finished.
|
|
166
|
+
*/
|
|
167
|
+
onFinish?: (event: {
|
|
168
|
+
/**
|
|
169
|
+
* The reason why the generation finished.
|
|
170
|
+
*/
|
|
171
|
+
finishReason: FinishReason;
|
|
172
|
+
/**
|
|
173
|
+
* The token usage of the generated response.
|
|
174
|
+
*/
|
|
175
|
+
usage: LanguageModelUsage;
|
|
176
|
+
/**
|
|
177
|
+
* The final ui node that was generated.
|
|
178
|
+
*/
|
|
179
|
+
value: ReactNode;
|
|
180
|
+
/**
|
|
181
|
+
* Warnings from the model provider (e.g. unsupported settings)
|
|
182
|
+
*/
|
|
183
|
+
warnings?: CallWarning[];
|
|
184
|
+
/**
|
|
185
|
+
* Optional response data.
|
|
186
|
+
*/
|
|
187
|
+
response?: {
|
|
188
|
+
/**
|
|
189
|
+
* Response headers.
|
|
190
|
+
*/
|
|
191
|
+
headers?: Record<string, string>;
|
|
192
|
+
};
|
|
193
|
+
}) => Promise<void> | void;
|
|
194
|
+
}): Promise<RenderResult>;
|
|
195
|
+
|
|
196
|
+
type StreamableUIWrapper = {
|
|
197
|
+
/**
|
|
198
|
+
* The value of the streamable UI. This can be returned from a Server Action and received by the client.
|
|
199
|
+
*/
|
|
200
|
+
readonly value: React.ReactNode;
|
|
201
|
+
/**
|
|
202
|
+
* This method updates the current UI node. It takes a new UI node and replaces the old one.
|
|
203
|
+
*/
|
|
204
|
+
update(value: React.ReactNode): StreamableUIWrapper;
|
|
205
|
+
/**
|
|
206
|
+
* This method is used to append a new UI node to the end of the old one.
|
|
207
|
+
* Once appended a new UI node, the previous UI node cannot be updated anymore.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```jsx
|
|
211
|
+
* const ui = createStreamableUI(<div>hello</div>)
|
|
212
|
+
* ui.append(<div>world</div>)
|
|
213
|
+
*
|
|
214
|
+
* // The UI node will be:
|
|
215
|
+
* // <>
|
|
216
|
+
* // <div>hello</div>
|
|
217
|
+
* // <div>world</div>
|
|
218
|
+
* // </>
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
append(value: React.ReactNode): StreamableUIWrapper;
|
|
222
|
+
/**
|
|
223
|
+
* This method is used to signal that there is an error in the UI stream.
|
|
224
|
+
* It will be thrown on the client side and caught by the nearest error boundary component.
|
|
225
|
+
*/
|
|
226
|
+
error(error: any): StreamableUIWrapper;
|
|
227
|
+
/**
|
|
228
|
+
* 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.
|
|
229
|
+
* Once called, the UI node cannot be updated or appended anymore.
|
|
230
|
+
*
|
|
231
|
+
* This method is always **required** to be called, otherwise the response will be stuck in a loading state.
|
|
232
|
+
*/
|
|
233
|
+
done(...args: [React.ReactNode] | []): StreamableUIWrapper;
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* Create a piece of changeable UI that can be streamed to the client.
|
|
237
|
+
* On the client side, it can be rendered as a normal React node.
|
|
238
|
+
*/
|
|
239
|
+
declare function createStreamableUI(initialValue?: React.ReactNode): StreamableUIWrapper;
|
|
240
|
+
|
|
241
|
+
declare const __internal_curr: unique symbol;
|
|
242
|
+
declare const __internal_error: unique symbol;
|
|
243
|
+
/**
|
|
244
|
+
* StreamableValue is a value that can be streamed over the network via AI Actions.
|
|
245
|
+
* To read the streamed values, use the `readStreamableValue` or `useStreamableValue` APIs.
|
|
246
|
+
*/
|
|
247
|
+
type StreamableValue<T = any, E = any> = {
|
|
248
|
+
[__internal_curr]?: T;
|
|
249
|
+
[__internal_error]?: E;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Create a wrapped, changeable value that can be streamed to the client.
|
|
254
|
+
* On the client side, the value can be accessed via the readStreamableValue() API.
|
|
255
|
+
*/
|
|
256
|
+
declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): StreamableValueWrapper<T, E>;
|
|
257
|
+
type StreamableValueWrapper<T, E> = {
|
|
258
|
+
/**
|
|
259
|
+
* The value of the streamable. This can be returned from a Server Action and
|
|
260
|
+
* received by the client. To read the streamed values, use the
|
|
261
|
+
* `readStreamableValue` or `useStreamableValue` APIs.
|
|
262
|
+
*/
|
|
263
|
+
readonly value: StreamableValue<T, E>;
|
|
264
|
+
/**
|
|
265
|
+
* This method updates the current value with a new one.
|
|
266
|
+
*/
|
|
267
|
+
update(value: T): StreamableValueWrapper<T, E>;
|
|
268
|
+
/**
|
|
269
|
+
* This method is used to append a delta string to the current value. It
|
|
270
|
+
* requires the current value of the streamable to be a string.
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```jsx
|
|
274
|
+
* const streamable = createStreamableValue('hello');
|
|
275
|
+
* streamable.append(' world');
|
|
276
|
+
*
|
|
277
|
+
* // The value will be 'hello world'
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
280
|
+
append(value: T): StreamableValueWrapper<T, E>;
|
|
281
|
+
/**
|
|
282
|
+
* This method is used to signal that there is an error in the value stream.
|
|
283
|
+
* It will be thrown on the client side when consumed via
|
|
284
|
+
* `readStreamableValue` or `useStreamableValue`.
|
|
285
|
+
*/
|
|
286
|
+
error(error: any): StreamableValueWrapper<T, E>;
|
|
287
|
+
/**
|
|
288
|
+
* This method marks the value as finalized. You can either call it without
|
|
289
|
+
* any parameters or with a new value as the final state.
|
|
290
|
+
* Once called, the value cannot be updated or appended anymore.
|
|
291
|
+
*
|
|
292
|
+
* This method is always **required** to be called, otherwise the response
|
|
293
|
+
* will be stuck in a loading state.
|
|
294
|
+
*/
|
|
295
|
+
done(...args: [T] | []): StreamableValueWrapper<T, E>;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* `readStreamableValue` takes a streamable value created via the `createStreamableValue().value` API,
|
|
300
|
+
* and returns an async iterator.
|
|
301
|
+
*
|
|
302
|
+
* ```js
|
|
303
|
+
* // Inside your AI action:
|
|
304
|
+
*
|
|
305
|
+
* async function action() {
|
|
306
|
+
* 'use server'
|
|
307
|
+
* const streamable = createStreamableValue();
|
|
308
|
+
*
|
|
309
|
+
* streamable.update(1);
|
|
310
|
+
* streamable.update(2);
|
|
311
|
+
* streamable.done(3);
|
|
312
|
+
* // ...
|
|
313
|
+
* return streamable.value;
|
|
314
|
+
* }
|
|
315
|
+
* ```
|
|
316
|
+
*
|
|
317
|
+
* And to read the value:
|
|
318
|
+
*
|
|
319
|
+
* ```js
|
|
320
|
+
* const streamableValue = await action()
|
|
321
|
+
* for await (const v of readStreamableValue(streamableValue)) {
|
|
322
|
+
* console.log(v)
|
|
323
|
+
* }
|
|
324
|
+
* ```
|
|
325
|
+
*
|
|
326
|
+
* This logs out 1, 2, 3 on console.
|
|
327
|
+
*/
|
|
328
|
+
declare function readStreamableValue<T = unknown>(streamableValue: StreamableValue<T>): AsyncIterable<T | undefined>;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* `useStreamableValue` is a React hook that takes a streamable value created via the `createStreamableValue().value` API,
|
|
332
|
+
* and returns the current value, error, and pending state.
|
|
333
|
+
*
|
|
334
|
+
* This is useful for consuming streamable values received from a component's props. For example:
|
|
335
|
+
*
|
|
336
|
+
* ```js
|
|
337
|
+
* function MyComponent({ streamableValue }) {
|
|
338
|
+
* const [data, error, pending] = useStreamableValue(streamableValue);
|
|
339
|
+
*
|
|
340
|
+
* if (pending) return <div>Loading...</div>;
|
|
341
|
+
* if (error) return <div>Error: {error.message}</div>;
|
|
342
|
+
*
|
|
343
|
+
* return <div>Data: {data}</div>;
|
|
344
|
+
* }
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
347
|
+
declare function useStreamableValue<T = unknown, Error = unknown>(streamableValue?: StreamableValue<T>): [data: T | undefined, error: Error | undefined, pending: boolean];
|
|
348
|
+
|
|
349
|
+
declare function useUIState<AI extends AIProvider = any>(): [InferUIState<AI, any>, (v: InferUIState<AI, any> | ((v_: InferUIState<AI, any>) => InferUIState<AI, any>)) => void];
|
|
350
|
+
declare function useAIState<AI extends AIProvider = any>(): [
|
|
351
|
+
InferAIState<AI, any>,
|
|
352
|
+
(newState: ValueOrUpdater<InferAIState<AI, any>>) => void
|
|
353
|
+
];
|
|
354
|
+
declare function useAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): [
|
|
355
|
+
InferAIState<AI, any>[typeof key],
|
|
356
|
+
(newState: ValueOrUpdater<InferAIState<AI, any>[typeof key]>) => void
|
|
357
|
+
];
|
|
358
|
+
declare function useActions<AI extends AIProvider = any>(): InferActions<AI, any>;
|
|
359
|
+
declare function useSyncUIState(): () => Promise<void>;
|
|
360
|
+
|
|
361
|
+
export { StreamableValue, createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, readStreamableValue, streamUI, useAIState, useActions, useStreamableValue, useSyncUIState, useUIState };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/types/index.ts
|
|
17
|
+
var types_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { readStreamableValue, useAIState, useActions, useStreamableValue, useSyncUIState, useUIState } from './rsc-shared.mjs';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/rsc-client.ts
|
|
2
|
+
import {
|
|
3
|
+
readStreamableValue,
|
|
4
|
+
useStreamableValue,
|
|
5
|
+
useUIState,
|
|
6
|
+
useAIState,
|
|
7
|
+
useActions,
|
|
8
|
+
useSyncUIState
|
|
9
|
+
} from "./rsc-shared.mjs";
|
|
10
|
+
export {
|
|
11
|
+
readStreamableValue,
|
|
12
|
+
useAIState,
|
|
13
|
+
useActions,
|
|
14
|
+
useStreamableValue,
|
|
15
|
+
useSyncUIState,
|
|
16
|
+
useUIState
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=rsc-client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rsc-client.ts"],"sourcesContent":["export {\n readStreamableValue,\n useStreamableValue,\n useUIState,\n useAIState,\n useActions,\n useSyncUIState,\n} from './rsc-shared.mjs';\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":[]}
|