Skip to content

Commit

Permalink
Make onSetAIState and onGetUIState stable (#1375)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Apr 17, 2024
1 parent 160b025 commit e94fb32
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-eyes-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

feat(ai/rsc): Make `onSetAIState` and `onGetUIState` stable
41 changes: 37 additions & 4 deletions packages/core/rsc/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,48 @@ export function createAI<
initialAIState,
initialUIState,

unstable_onSetAIState: onSetAIState,
unstable_onGetUIState: onGetUIState,
onSetAIState,
onGetUIState,
}: {
actions: Actions;
initialAIState?: AIState;
initialUIState?: UIState;

unstable_onSetAIState?: OnSetAIState<AIState>;
unstable_onGetUIState?: OnGetUIState<UIState>;
/**
* This function is called whenever the AI state is updated by an Action.
* You can use this to persist the AI state to a database, or to send it to a
* logging service.
*/
onSetAIState?: OnSetAIState<AIState>;

/**
* This function is used to retrieve the UI state based on the AI state.
* For example, to render the initial UI state based on a given AI state, or
* to sync the UI state when the application is already loaded.
*
* If returning `undefined`, the client side UI state will not be updated.
*
* This function must be annotated with the `"use server"` directive.
*
* @example
* ```tsx
* onGetUIState: async () => {
* 'use server';
*
* const currentAIState = getAIState();
* const externalAIState = await loadAIStateFromDatabase();
*
* if (currentAIState === externalAIState) return undefined;
*
* // Update current AI state and return the new UI state
* const state = getMutableAIState()
* state.done(externalAIState)
*
* return <div>...</div>;
* }
* ```
*/
onGetUIState?: OnGetUIState<UIState>;
}) {
// Wrap all actions with our HoC.
const wrappedActions: ServerWrappedActions = {};
Expand Down

0 comments on commit e94fb32

Please sign in to comment.