-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(ai-sdk/react): Fix subscribeToMessages callback dependency in useChat #10908
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@ai-sdk/react': patch | ||
| --- | ||
|
|
||
| Fix subscribeToMessages callback dependency in useChat |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2314,6 +2314,71 @@ describe('chat instance changes', () => { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| expect(screen.queryByTestId('message-0')).not.toBeInTheDocument(); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| it('should handle streaming correctly when the id changes', async () => { | ||||||||||||||||||||||||
| const controller = new TestResponseController(); | ||||||||||||||||||||||||
| server.urls['/api/chat'].response = { | ||||||||||||||||||||||||
| type: 'controlled-stream', | ||||||||||||||||||||||||
| controller, | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // First, change the ID | ||||||||||||||||||||||||
| await userEvent.click(screen.getByTestId('do-change-chat')); | ||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: ai/packages/react/src/use-chat.ui.test.tsx Lines 2252 to 2262 in fa8e472
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Then send a message | ||||||||||||||||||||||||
| await userEvent.click(screen.getByTestId('do-send')); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| 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', | ||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||
| ]), | ||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| describe('streaming with id change from undefined to defined', () => { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||

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.
This test is largely copied from the test added in #7387:
ai/packages/react/src/use-chat.ui.test.tsx
Lines 2352 to 2415 in 68d8803