-
Notifications
You must be signed in to change notification settings - Fork 327
fix: pass agent._pk as id to useChat to prevent stale WebSocket instances #440
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
Conversation
🦋 Changeset detectedLatest commit: 43640fa The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
threepointone
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
soft blocking so I can review
| messages: initialMessages, | ||
| transport: customTransport | ||
| transport: customTransport, | ||
| id: agent._pk |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a problem with this is that _pk is an identifier for the connection, but "id" passed to useChat is for the conversation
it's actually not clear to me how this actually solves the problem of stale websocket instances that are being generated by useAgent
I'll keep looking at this, but the recommended path for now is to not use StrictMode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As explained in #465 (comment), the useChat hook of the Vercel AI SDK uses useRef to store the Chat instance. The Chat instance saved in useRef will only be updated when option.id or option.chat changes.
_pk is a random identifier generated along with the WebSocket (ws) connection. A change in _pk indicates that the WebSocket has changed. When _pk is passed as the id to useChat, it enables the reinitialization of the Chat instance, allowing the use of the new WebSocket connection.
It is unclear why StrictMode causes two WebSocket instances to be generated. However, even without Strict Mode, it is assumed that if the WebSocket connection is disconnected and reconnected, both the WebSocket instance and _pk will change—making it still necessary to have useChat update the Chat instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok understood
Pass the agent's unique identifier (
agent._pk) as theidparameter to the useChat hook. This ensures that when useAgent creates multiple WebSocket instances, useAgentChat will get the updated connection associated with the current agent instance.Fixes #439