Skip to content
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

Fix Chat Page Refreshing Before Saving Chat #399

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Saran33
Copy link

@Saran33 Saran33 commented Aug 13, 2024

  • FIxes: Page refresh firing before saveChat #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 Page refresh firing before saveChat #302 (comment) 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.

Copy link

vercel bot commented Aug 13, 2024

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

A member of the Team first needs to authorize it.

@Saran33 Saran33 changed the title Fix Chat Page Refreshing Before Saving Chat Aug 13, 2024
@@ -39,3 +40,9 @@ export interface User extends Record<string, any> {
password: string
salt: string
}

export type MutableAIState<AIState> = {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not exported from the AI SDK so the type was copied to the codebase.

@preshetin
Copy link

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

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.

Page refresh firing before saveChat
2 participants