Skip to content

Commit e94fb32

Browse files
authoredApr 17, 2024··
Make onSetAIState and onGetUIState stable (#1375)
1 parent 160b025 commit e94fb32

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed
 

‎.changeset/short-eyes-hang.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
feat(ai/rsc): Make `onSetAIState` and `onGetUIState` stable

‎packages/core/rsc/provider.tsx

+37-4
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,48 @@ export function createAI<
5555
initialAIState,
5656
initialUIState,
5757

58-
unstable_onSetAIState: onSetAIState,
59-
unstable_onGetUIState: onGetUIState,
58+
onSetAIState,
59+
onGetUIState,
6060
}: {
6161
actions: Actions;
6262
initialAIState?: AIState;
6363
initialUIState?: UIState;
6464

65-
unstable_onSetAIState?: OnSetAIState<AIState>;
66-
unstable_onGetUIState?: OnGetUIState<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+
/**
73+
* This function is used to retrieve the UI state based on the AI state.
74+
* For example, to render the initial UI state based on a given AI state, or
75+
* to sync the UI state when the application is already loaded.
76+
*
77+
* If returning `undefined`, the client side UI state will not be updated.
78+
*
79+
* This function must be annotated with the `"use server"` directive.
80+
*
81+
* @example
82+
* ```tsx
83+
* onGetUIState: async () => {
84+
* 'use server';
85+
*
86+
* const currentAIState = getAIState();
87+
* const externalAIState = await loadAIStateFromDatabase();
88+
*
89+
* if (currentAIState === externalAIState) return undefined;
90+
*
91+
* // Update current AI state and return the new UI state
92+
* const state = getMutableAIState()
93+
* state.done(externalAIState)
94+
*
95+
* return <div>...</div>;
96+
* }
97+
* ```
98+
*/
99+
onGetUIState?: OnGetUIState<UIState>;
67100
}) {
68101
// Wrap all actions with our HoC.
69102
const wrappedActions: ServerWrappedActions = {};

0 commit comments

Comments
 (0)
Please sign in to comment.