Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ai/rsc: Improve getAIState and getMutableAIState types #1458

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-bugs-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

ai/rsc: improve getAIState and getMutableAIState types
10 changes: 6 additions & 4 deletions packages/core/rsc/ai-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ export function sealMutableAIState() {
* @example const state = getAIState() // Get the entire AI state
* @example const field = getAIState('key') // Get the value of the key
*/
function getAIState<AI extends AIProvider = any>(): InferAIState<AI, any>;
function getAIState<AI extends AIProvider = any>(): Readonly<
InferAIState<AI, any>
>;
function getAIState<AI extends AIProvider = any>(
key: keyof InferAIState<AI, any>,
): InferAIState<AI, any>[typeof key];
): Readonly<InferAIState<AI, any>[typeof key]>;
function getAIState<AI extends AIProvider = any>(
...args: [] | [key: keyof InferAIState<AI, any>]
) {
Expand Down Expand Up @@ -183,10 +185,10 @@ function getMutableAIState<AI extends AIProvider = any>(
)}" field from the AI state because it's not an object.`,
);
}
return store.currentState[key];
return store.currentState[key] as Readonly<AIStateWithKey>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be better ways to do this.

}

return store.currentState as AIState;
return store.currentState as Readonly<AIState>;
},
update: function update(newAIState: NewStateOrUpdater) {
doUpdate(newAIState, false);
Expand Down