WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@jeffcarbs
Copy link

Background

Follow-up to:

That PR fixed stale subscriptions when the chat ID changes. However, it was incomplete since it only handled the case where the new ID was passed in the options. It's also possible for you to pass in a chat instance directly which could have a new ID.

Summary

Tweak the logic there to use the chatRef.current.id in the dependency array, so no matter how the ID changes we'll refresh the subscription callbacks.

Manual Verification

Didn't do manually testing since the unit tests for this specific case (plus the one I added) seemed sufficient.

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

@vercel-ai-sdk vercel-ai-sdk bot added the ai/ui label Dec 5, 2025
expect(screen.queryByTestId('message-0')).not.toBeInTheDocument();
});

it('should handle streaming correctly when the id changes', async () => {
Copy link
Author

Choose a reason for hiding this comment

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

This test is largely copied from the test added in #7387:

it('should handle streaming correctly when id changes from undefined to defined', async () => {
const controller = new TestResponseController();
server.urls['/api/chat'].response = {
type: 'controlled-stream',
controller,
};
// First, change the ID from undefined to 'chat-123'
await userEvent.click(screen.getByTestId('change-id'));
// Then send a message
await userEvent.click(screen.getByTestId('send-message'));
await waitFor(() => {
expect(screen.getByTestId('status')).toHaveTextContent('submitted');
});
controller.write(formatChunk({ type: 'text-start', id: '0' }));
controller.write(
formatChunk({ type: 'text-delta', id: '0', delta: 'Hello' }),
);
// Verify streaming is working - text should appear immediately
await waitFor(() => {
expect(
JSON.parse(screen.getByTestId('messages').textContent ?? ''),
).toContainEqual(
expect.objectContaining({
role: 'assistant',
parts: expect.arrayContaining([
expect.objectContaining({
type: 'text',
text: 'Hello',
}),
]),
}),
);
});
controller.write(formatChunk({ type: 'text-delta', id: '0', delta: ',' }));
controller.write(
formatChunk({ type: 'text-delta', id: '0', delta: ' world' }),
);
controller.write(formatChunk({ type: 'text-delta', id: '0', delta: '.' }));
controller.write(formatChunk({ type: 'text-end', id: '0' }));
controller.close();
await waitFor(() => {
expect(
JSON.parse(screen.getByTestId('messages').textContent ?? ''),
).toContainEqual(
expect.objectContaining({
role: 'assistant',
parts: expect.arrayContaining([
expect.objectContaining({
type: 'text',
text: 'Hello, world.',
state: 'done',
}),
]),
}),
);
});
});

@jeffcarbs jeffcarbs marked this pull request as ready for review December 5, 2025 17:35
};

// First, change the ID
await userEvent.click(screen.getByTestId('do-change-chat'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this changing the id on the chat instance in the way you describe in the bug report? ie without your fix would this test fail?

Copy link
Author

@jeffcarbs jeffcarbs Dec 6, 2025

Choose a reason for hiding this comment

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

Yes, you can see here it resets the chat instance to one with a new ID:

<button
data-testid="do-change-chat"
onClick={() => {
setChat(
new Chat({
id: 'second-id',
generateId: mockId(),
}),
);
}}
/>

The test fails before the change:
Screenshot 2025-12-05 at 10 24 01 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants