Skip to content

Conversation

@axuj
Copy link
Contributor

@axuj axuj commented Sep 7, 2025

Pass the agent's unique identifier (agent._pk) as the id parameter 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

@changeset-bot
Copy link

changeset-bot bot commented Sep 7, 2025

🦋 Changeset detected

Latest commit: 43640fa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Sep 8, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@440

commit: 43640fa

Copy link
Contributor

@threepointone threepointone left a 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
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah ok understood

@threepointone threepointone merged commit 9ef35e2 into cloudflare:main Sep 23, 2025
3 checks passed
@threepointone threepointone mentioned this pull request Sep 23, 2025
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.

Stale WebSocket Instances Used in useAgentChat

2 participants