-
Notifications
You must be signed in to change notification settings - Fork 52.9k
fix(desktop): keep message component types stable across Thread re-renders #44884
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
Closed
travelreader8-stack
wants to merge
1
commit into
NousResearch:main
from
travelreader8-stack:fix/thread-stable-message-components
+152
−3
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
apps/desktop/src/components/assistant-ui/thread-remount.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { AssistantRuntimeProvider, type ThreadMessage, useExternalStoreRuntime } from '@assistant-ui/react' | ||
| import { act, render, screen, waitFor } from '@testing-library/react' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { Thread } from './thread' | ||
|
|
||
| class NoopResizeObserver { | ||
| observe() {} | ||
|
|
||
| unobserve() {} | ||
|
|
||
| disconnect() {} | ||
| } | ||
|
|
||
| vi.stubGlobal('ResizeObserver', NoopResizeObserver) | ||
| vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => | ||
| window.setTimeout(() => callback(performance.now()), 0) | ||
| ) | ||
| vi.stubGlobal('cancelAnimationFrame', (id: number) => window.clearTimeout(id)) | ||
|
|
||
| Element.prototype.scrollTo = function scrollTo() {} | ||
|
|
||
| Element.prototype.animate = function animate() { | ||
| return { | ||
| cancel: () => {}, | ||
| finished: Promise.resolve() | ||
| } as unknown as Animation | ||
| } | ||
|
|
||
| // jsdom returns 0 for offset*; the virtualizer reads those to size its | ||
| // viewport. Fall through to client* or a sane default so virtualized | ||
| // items render (same stub as streaming.test.tsx). | ||
| function stubOffsetDimension( | ||
| prop: 'offsetHeight' | 'offsetWidth', | ||
| clientProp: 'clientHeight' | 'clientWidth', | ||
| fallback: number | ||
| ) { | ||
| const previous = Object.getOwnPropertyDescriptor(HTMLElement.prototype, prop) | ||
|
|
||
| Object.defineProperty(HTMLElement.prototype, prop, { | ||
| configurable: true, | ||
| get() { | ||
| return previous?.get?.call(this) || (this as HTMLElement)[clientProp] || fallback | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| stubOffsetDimension('offsetWidth', 'clientWidth', 800) | ||
| stubOffsetDimension('offsetHeight', 'clientHeight', 600) | ||
|
|
||
| const createdAt = new Date('2026-05-01T00:00:00.000Z') | ||
|
|
||
| const MESSAGES: ThreadMessage[] = [ | ||
| { | ||
| id: 'user-1', | ||
| role: 'user', | ||
| content: [{ type: 'text', text: 'hello from the user' }], | ||
| attachments: [], | ||
| createdAt, | ||
| metadata: { custom: {} } | ||
| } as ThreadMessage, | ||
| { | ||
| id: 'assistant-1', | ||
| role: 'assistant', | ||
| content: [{ type: 'text', text: 'stable assistant reply' }], | ||
| status: { type: 'complete', reason: 'stop' }, | ||
| createdAt, | ||
| metadata: { | ||
| unstable_state: null, | ||
| unstable_annotations: [], | ||
| unstable_data: [], | ||
| steps: [], | ||
| custom: {} | ||
| } | ||
| } as ThreadMessage | ||
| ] | ||
|
|
||
| function Harness({ | ||
| onBranchInNewChat, | ||
| onCancel | ||
| }: { | ||
| onBranchInNewChat: (messageId: string) => void | ||
| onCancel: () => void | ||
| }) { | ||
| const runtime = useExternalStoreRuntime<ThreadMessage>({ | ||
| messages: MESSAGES, | ||
| isRunning: false, | ||
| onNew: async () => {} | ||
| }) | ||
|
|
||
| return ( | ||
| <AssistantRuntimeProvider runtime={runtime}> | ||
| <Thread onBranchInNewChat={onBranchInNewChat} onCancel={onCancel} /> | ||
| </AssistantRuntimeProvider> | ||
| ) | ||
| } | ||
|
|
||
| describe('thread message mount stability', () => { | ||
| // Regression: the desktop controller re-renders every 15s (status | ||
| // snapshot poll) and used to pass freshly-created callbacks down to | ||
| // <Thread/>. Those callbacks were deps of the `messageComponents` | ||
| // useMemo, so new component *types* were created each poll and React | ||
| // unmounted/remounted every visible message — shiki re-highlighted | ||
| // code blocks and the whole thread visibly jumped. | ||
| it('keeps message DOM nodes mounted when callback props get new identities', async () => { | ||
| const { rerender } = render(<Harness onBranchInNewChat={() => {}} onCancel={() => {}} />) | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText('stable assistant reply')).toBeTruthy() | ||
| expect(screen.getByText('hello from the user')).toBeTruthy() | ||
| }) | ||
|
|
||
| const assistantBefore = screen.getByText('stable assistant reply') | ||
| const userBefore = screen.getByText('hello from the user') | ||
|
|
||
| // Same data, new callback identities — exactly what a parent | ||
| // re-render driven by an unrelated state update produces. | ||
| await act(async () => { | ||
| rerender(<Harness onBranchInNewChat={() => {}} onCancel={() => {}} />) | ||
| }) | ||
|
|
||
| expect(screen.getByText('stable assistant reply')).toBe(assistantBefore) | ||
| expect(screen.getByText('hello from the user')).toBe(userBefore) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 memo moved to
apps/desktop/src/components/assistant-ui/thread/index.tsxin current main (commit7ff6908a5). Please port the fix there and include its neweronDismissErrorandonRestoreToMessagecallback dependencies; otherwise either can still recreate the message component types.