[draft] active chat context #7355
Draft
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.
Background
Minimal example in
next
useswindow.history.pushState
w/ a Next.js router cache invalidation which was polluting the navigation history + causing a reload after navigation. Also, by migrating away from SWR, we lost the benefit of having all hooks w/ the same id share the same state which was provided to us via the SWR global cache.Unsolved:
/
routeSummary
A new minimal example added using a global state store (React Context, but could also use Zustand or Jotai) to illustrate solution for sharing active chat state across useChat instances. Can be tweaked to store a full chat registry instead of just the active chat.
This example also relies on
router.push
to avoid callingwindow.history.pushState
.Verification
Tested new chat creation, navigation across chats, and back/forward navigation in development env
Tasks
pnpm changeset
in the project root)pnpm prettier-fix
in the project root)Future Work
There is a known bug with this implementation that should be addressed as a follow up:
After starting a new chat by sending the first message from
/
, we route to/chat/[chatId]
. After the assistant response streams in, when we navigate back to/
and then return to/chat/[chatId]
, the messages are stale.Related Issues