@@ -55,15 +55,48 @@ export function createAI<
55
55
initialAIState,
56
56
initialUIState,
57
57
58
- unstable_onSetAIState : onSetAIState ,
59
- unstable_onGetUIState : onGetUIState ,
58
+ onSetAIState,
59
+ onGetUIState,
60
60
} : {
61
61
actions : Actions ;
62
62
initialAIState ?: AIState ;
63
63
initialUIState ?: UIState ;
64
64
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 > ;
67
100
} ) {
68
101
// Wrap all actions with our HoC.
69
102
const wrappedActions : ServerWrappedActions = { } ;
0 commit comments