ai-chatbot icon indicating copy to clipboard operation
ai-chatbot copied to clipboard

Fix Chat Page Refreshing Before Saving Chat

Open Saran33 opened this issue 1 year ago • 2 comments

  • FIxes: https://github.com/vercel/ai-chatbot/issues/302
  • Resolve race condition when aiState.done is called before onSetAIState
  • 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 onSetAIState callback is removed and instead an aiStateDone wrapper around aiState.done is called in place of aiState.done. This wrapper function waits until the chat is saved to the db before marking the aiState updates as done.
  • In chat.tsx, the useEffect hook 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 Purchase component status now defaults to requires_action, instead of expired, with a useEffect hook added to check and update the purchase status based on the relevant tool call message and any related system messages.
  • The toolCallId is passed as a prop to Purchase. This is used to find the relevant tool call in the aiState.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 createdAt property is added to the Message interface. If the tool call message was created more than 30 seconds ago, the checkout is marked as expired. While the status remains in a requires_action state, an interval is set to check every 5 seconds whether the status should be expired.

Saran33 avatar Aug 13 '24 19:08 Saran33

@Saran33 is attempting to deploy a commit to the Uncurated Tests Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Aug 13 '24 19:08 vercel[bot]

Thanks @Saran33 - I used the code from this PR and it looks like it does solve the issue!

preshetin avatar Aug 22 '24 09:08 preshetin