Skip to content

[draft] active chat context #7355

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 8 commits into
base: main
Choose a base branch
from

Conversation

iteratetograceness
Copy link
Collaborator

@iteratetograceness iteratetograceness commented Jul 16, 2025

Background

Minimal example in next uses window.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:

  • Resetting on / route

Summary

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 calling window.history.pushState.

Verification

Tested new chat creation, navigation across chats, and back/forward navigation in development env

Tasks

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • Formatting issues have been fixed (run 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

@lgrammel
Copy link
Collaborator

thank you! can you make this a separate example (dont want to override what is there since it is a good example in itself)?

@hoanginc144
Copy link

@iteratetograceness Not sure if it helps, but regarding new chat flow and also navigating back to /chat, I solved it using React context to set the first message before navigating to /chat/id, then after router.push I use useEffect to send the first message. I also use a hasSentFirstMessage ref to make sure it only sends the first message once. Obviously the downside is now I have to call sendMessage within a useEffect, which I do not like, but so far it has worked well for my case.

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.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy