-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Page refresh firing before saveChat #302
Comments
having the same issue atm, were you able to fix it? |
Hey @amxv yes, I was able to resolve it. Since I'm using gemini, I had to follow the AI SDK 3 example "next-ai-rsc". But essentially it should be the same thing. Changes I made:
I now call saveChat inside of unstable_onSetAIState like this
I then call await saveChat(state) when isFinal is true and set saved to true.
And then the saveChat() is this
Let me know if that fixes it for you too or if you run into any issues with it please! |
@Godrules500 could you please raise a PR with the fix? Fixes such as this one are super helpful for keeping the template updated. |
Experiencing the same issue. |
@athrael-soju I will try to get to it soon! I also found another solution to another issue if it helps y'all. on chat.tsx, this is the problem area for me.
I updated mine to
then on actions.tsx, I added this.
don't forget to add it where submitUserMessage is added. Then update chat.tsx to |
I think you maybe sharing code from the ai-rsc template here. Is this an issue with this template? I tried reproducing this issue and I'm not seeing it. |
Hi, any news on this issue ? |
Made the changes as said here, but still facing this issue. Anyone else? My current code:
|
same for me. |
I'd wait a bit for the next version of the template to come out, as this one still has some breaking issues. They've already closed down several PRs with fixes, which hopefully points to a release soon. |
I solved this by doing these changes to "/" and "/new" . In "/" route we will check for existing chats and if present we will push to "/chat/[id]", If no chats then push to "/new".
And in "/new"
|
any fix to this? |
TL;DR: Do not call aiState.done multiple times for the same action What I figured out, is the following:
If you call aiState.done multiple times, only the first diff will be received by the client. In my case, the AIState was not correctly in sync and the refresh occurred too fast (as subsequent updates were missed) |
I don't remember what all changes I've made up to this point, but the change I just made has made a huge difference and solved 3 issues.
It seems that the AIState gets out of sync, and these 2 changes to chat.tsx seems to force aiState to get back in sync. in chat.tsx I have this.
I also had to move saveChat out of onSetAIState. I've changed the naming and extracted it to its own method, but this forced the code to wait for the db to update before marking the state as done.
Please let me know if this fixes your issue! |
@Godrules500 nice solution, thank you🚀 I incorporated some of those changes into a PR here #399 |
This doesn't appear to be an issue on the demo site though so it may have already been resolved by the Vercel team but the changes not applied to this repo. I said I'd add the changes here anyway in any case it's useful to anyone. |
I have a situation every now and then when I create a new chat that, and the saveChat takes a little bit of time, the chat.tsx router.refresh() fires too soon. How can I ensure that it waits for saveChat to finish?
This is the code that is firing before the saveChat finishes. So the assistant responds, and the saveChat finishes after the aiState.done is called.
useEffect(() => { const messagesLength = aiState.messages?.length if (messagesLength === 2) { router.refresh() } }, [aiState.messages, router])
The text was updated successfully, but these errors were encountered: