ai-chatbot
ai-chatbot copied to clipboard
Fix Chat Page Refreshing Before Saving Chat
- FIxes: https://github.com/vercel/ai-chatbot/issues/302
- Resolve race condition when
aiState.doneis called beforeonSetAIState - This PR prevents the UI from refreshing before the db is updated.
- As per the approach proposed by @Godrules500 in https://github.com/vercel/ai-chatbot/issues/302#issuecomment-2233485144 the
onSetAIStatecallback is removed and instead anaiStateDonewrapper aroundaiState.doneis called in place ofaiState.done. This wrapper function waits until the chat is saved to the db before marking the aiState updates as done. - In
chat.tsx, theuseEffecthook for refreshing the router contains an additional check to refresh the router if the chat contains 3 messages and the last message is a tool call. The existing check only checked for 2 messages, meaning the router was not refreshed if the first user message in a chat resulted in a tool call response. - The
Purchasecomponentstatusnow defaults torequires_action, instead ofexpired, with auseEffecthook added to check and update the purchase status based on the relevant tool call message and any related system messages. - The
toolCallIdis passed as a prop toPurchase. This is used to find the relevant tool call in theaiState.messages. - If the message immediately following the tool call message is a system message containing the string
purchased, then the purchase is marked as completed. - An optional
createdAtproperty is added to theMessageinterface. If the tool call message was created more than 30 seconds ago, the checkout is marked as expired. While the status remains in arequires_actionstate, an interval is set to check every 5 seconds whether the status should beexpired.
@Saran33 is attempting to deploy a commit to the Uncurated Tests Team on Vercel.
A member of the Team first needs to authorize it.
Thanks @Saran33 - I used the code from this PR and it looks like it does solve the issue!