useChat external store on ChatClient (approach 3/3: @tanstack/store) - #7
Draft
tombeckenham wants to merge 1 commit into
Draft
tombeckenham wants to merge 1 commit into
tombeckenham wants to merge 1 commit into
Conversation
Alternative to the hand-rolled subscribe/getSnapshot: hold ChatClient's reactive state in a @tanstack/store Store and consume it from React with @tanstack/react-store's useStore. ai-client: - Add @tanstack/store dependency. - Add ChatClientSnapshot (exported) and a `readonly store: Store<ChatClientSnapshot>` on ChatClient, seeded in the constructor. - Each setX mutator (loading/status/subscription/connection/session/error) and the messages handler calls syncStore(), which projects current state into the Store. The onXChange callbacks stay intact for existing consumers; the Store is additive. ai-react: - Add @tanstack/react-store dependency. - useChat reads client.store via useStore. A selector (useStore(client.store, s => s.messages)) could scope per-field re-renders; kept whole here to match the public API. vs the subscribe/getSnapshot branch: less hand-written plumbing and free selector support, at the cost of two new @tanstack/store deps. All 376 ai-client + 128 ai-react tests pass (typecheck requires ai-client to be built first, as ai-react resolves it via dist types). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
🚀 Changeset Version Preview6 package(s) bumped directly, 5 bumped as dependents. 🟥 Major bumps
🟨 Minor bumps
🟩 Patch bumps
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Back
ChatClient's state with@tanstack/storeSame goal as approach 2 (#6) — one external-store primitive on
ChatClient, shared across frameworks — but using@tanstack/storeinstead of a hand-rolledsubscribe/getSnapshot.ai-client@tanstack/storedependency.ChatClientSnapshotand addreadonly store: Store<ChatClientSnapshot>toChatClient, seeded in the constructor.setXmutator and the messages handler callssyncStore(), which projects current state into the Store. TheonXChangecallbacks stay intact for existing consumers — the Store is additive.ai-react@tanstack/react-storedependency.client.storeviauseStore:Trade-off vs approach 2 (hand-rolled)
Pros: much less hand-written plumbing, and free selector support (
useStore(store, selector)) to scope re-renders per field — useful for token-by-token streaming. It's also the "most TanStack" option.Cons: two new dependencies (
@tanstack/storeinai-client,@tanstack/react-storeinai-react).Verification
tsc, ESLint (0 errors), all 376ai-client+ 128ai-reacttests pass. (Typecheck requires buildingai-clientfirst, sinceai-reactresolves it viadisttypes.)🤖 Generated with Claude Code