Skip to content

feat (core, react): refactor useChat to use MessageStore #5770

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

Draft
wants to merge 22 commits into
base: v5
Choose a base branch
from

Conversation

iteratetograceness
Copy link
Collaborator

@iteratetograceness iteratetograceness commented Apr 14, 2025

Open questions:

  • We technically lose the SWR caching across chat id's by relying on a ChatStore that re-initializes when chat id changes. Do we need a hybrid approach?
  • What do we mean when we say we want to support robust multi-chat support?
  • How can we better handle: partial state clean up, and step calculation?

Things to note:

  • How does this new approach translate over to Svelte/Solid/Vue?

Background

Summary

Tasks

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • If required, a patch changeset for relevant packages has been added
  • You've run pnpm prettier-fix to fix any formatting issues

Future Work

@@ -0,0 +1,237 @@
import { LanguageModelV2FinishReason } from '@ai-sdk/provider';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TODO: TESTS

@@ -8,17 +8,24 @@ import type {
UseChatOptions,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

review packages/react/src/use-chat.ui.test.tsx to see if there are additional test cases to add

@iteratetograceness iteratetograceness changed the title [WIP] useSyncExternalStore feat (core, react): refactor useChat to use MessageStore Apr 16, 2025
* Internal class for managing UIMessages
*/
export class MessagesStore {
chatId: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

what would it take to make messages store multi-chat ready? i.e. being able to switch between chats with different ids? (we have many bugs in that area)

this might mean storing additional information such as state here (and potentially renaming to ChatStore)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Would love to chat about this further next week!

Comment on lines 44 to 47
this.subscribers = new Set();
this.notify = throttleMs
? throttle(() => this.emit(), throttleMs)
: () => this.emit();
Copy link
Collaborator

Choose a reason for hiding this comment

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

how are emit, subscribers, and notify related?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

subscribers holds all the callbacks we call when we want to notify of changes! notify is a wrapper around the internal emit method that is throttled if throttleMs is passed in!

@lgrammel
Copy link
Collaborator

Direction is great - might be good to explore how this could look like as a more complete chat store, and also check if this will work with Svelte and Vue.

import { throttle } from './throttle';

interface ChatStoreSubscriber {
onChatMessagesChanged(): void;
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this be async or not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants