fix: pass agent._pk as id to useChat to prevent stale WebSocket instances#440
fix: pass agent._pk as id to useChat to prevent stale WebSocket instances#440threepointone merged 4 commits intocloudflare:mainfrom axuj:patch-1
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.
soft blocking so I can review
| messages: initialMessages, | ||
| transport: customTransport | ||
| transport: customTransport, | ||
| id: agent._pk |
There was a problem hiding this comment.
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.
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.
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