-
Notifications
You must be signed in to change notification settings - Fork 352
perf(desktop): reduce composer typing work #1236
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
Merged
MagicLizi
merged 2 commits into
makecindy:main
from
hushaowu-rh:perf/desktop-composer-typing
Aug 1, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
55 changes: 55 additions & 0 deletions
55
apps/desktop/src/renderer/components/new-chat/__tests__/composerFrameScheduler.test.ts
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,55 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { createComposerFrameScheduler } from '../composerFrameScheduler'; | ||
|
|
||
| describe('createComposerFrameScheduler', () => { | ||
| it('keeps only the latest pending frame', () => { | ||
| let nextHandle = 1; | ||
| const callbacks = new Map<number, FrameRequestCallback>(); | ||
| const cancelled: number[] = []; | ||
| let runs = 0; | ||
| const scheduler = createComposerFrameScheduler( | ||
| () => { | ||
| runs += 1; | ||
| }, | ||
| { | ||
| requestFrame: (callback) => { | ||
| const handle = nextHandle++; | ||
| callbacks.set(handle, callback); | ||
| return handle; | ||
| }, | ||
| cancelFrame: (handle) => { | ||
| cancelled.push(handle); | ||
| callbacks.delete(handle); | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| scheduler.schedule(); | ||
| scheduler.schedule(); | ||
| expect(cancelled).toEqual([1]); | ||
| expect(callbacks.size).toBe(1); | ||
| callbacks.get(2)?.(16); | ||
| expect(runs).toBe(1); | ||
| }); | ||
|
|
||
| it('cancels pending work on teardown', () => { | ||
| let callback: FrameRequestCallback | null = null; | ||
| let cancelled = false; | ||
| const scheduler = createComposerFrameScheduler(() => undefined, { | ||
| requestFrame: (next) => { | ||
| callback = next; | ||
| return 1; | ||
| }, | ||
| cancelFrame: () => { | ||
| cancelled = true; | ||
| callback = null; | ||
| }, | ||
| }); | ||
|
|
||
| scheduler.schedule(); | ||
| scheduler.cancel(); | ||
| expect(cancelled).toBe(true); | ||
| expect(callback).toBeNull(); | ||
| }); | ||
| }); |
38 changes: 38 additions & 0 deletions
38
apps/desktop/src/renderer/components/new-chat/__tests__/composerHistoryProjection.test.ts
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,38 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { deriveStableComposerHistory } from '../composerHistoryProjection'; | ||
|
|
||
| describe('deriveStableComposerHistory', () => { | ||
| it('keeps the same array while only assistant streaming content changes', () => { | ||
| const previous = deriveStableComposerHistory( | ||
| [ | ||
| { role: 'user', content: 'first' }, | ||
| { role: 'assistant', content: 'a' }, | ||
| ], | ||
| [], | ||
| ); | ||
| const next = deriveStableComposerHistory( | ||
| [ | ||
| { role: 'user', content: 'first' }, | ||
| { role: 'assistant', content: 'a growing response' }, | ||
| ], | ||
| previous, | ||
| ); | ||
|
|
||
| expect(next).toBe(previous); | ||
| }); | ||
|
|
||
| it('returns a new newest-first projection when user rows change', () => { | ||
| const previous = deriveStableComposerHistory([{ role: 'user', content: 'first' }], []); | ||
| const next = deriveStableComposerHistory( | ||
| [ | ||
| { role: 'user', content: 'first' }, | ||
| { role: 'user', content: 'second', quotesEncoded: true }, | ||
| ], | ||
| previous, | ||
| ); | ||
|
|
||
| expect(next).not.toBe(previous); | ||
| expect(next).toEqual([{ content: 'second', quotesEncoded: true }, { content: 'first' }]); | ||
| }); | ||
| }); |
75 changes: 75 additions & 0 deletions
75
apps/desktop/src/renderer/components/new-chat/__tests__/composerRenderGate.test.ts
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,75 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { | ||
| __composerRenderGateDefaultsForTest, | ||
| composerRenderSnapshot, | ||
| shouldRefreshComposerRender, | ||
| } from '../composerRenderGate'; | ||
|
|
||
| describe('composer render gate', () => { | ||
| it('skips ordinary text updates when trigger and send state are stable', () => { | ||
| const none = composerRenderSnapshot({ kind: 'none' }, true); | ||
| expect(shouldRefreshComposerRender(none, composerRenderSnapshot({ kind: 'none' }, true))).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
|
|
||
| it('refreshes when text becomes sendable or empty', () => { | ||
| expect( | ||
| shouldRefreshComposerRender( | ||
| composerRenderSnapshot({ kind: 'none' }, false), | ||
| composerRenderSnapshot({ kind: 'none' }, true), | ||
| ), | ||
| ).toBe(true); | ||
| expect( | ||
| shouldRefreshComposerRender( | ||
| composerRenderSnapshot({ kind: 'none' }, true), | ||
| composerRenderSnapshot({ kind: 'none' }, false), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('refreshes when a palette trigger changes', () => { | ||
| const before = composerRenderSnapshot({ kind: 'slash', sigil: '/', query: '', from: 1 }, true); | ||
| expect( | ||
| shouldRefreshComposerRender( | ||
| before, | ||
| composerRenderSnapshot({ kind: 'slash', sigil: '/', query: 'he', from: 1 }, true), | ||
| ), | ||
| ).toBe(true); | ||
| expect( | ||
| shouldRefreshComposerRender( | ||
| before, | ||
| composerRenderSnapshot({ kind: 'slash', sigil: '$', query: '', from: 1 }, true), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('refreshes when the trigger kind itself changes', () => { | ||
| // 'none' -> a palette trigger opening. | ||
| expect( | ||
| shouldRefreshComposerRender( | ||
| composerRenderSnapshot({ kind: 'none' }, true), | ||
| composerRenderSnapshot({ kind: 'slash', sigil: '/', query: '', from: 1 }, true), | ||
| ), | ||
| ).toBe(true); | ||
| // A palette trigger closing back to 'none'. | ||
| expect( | ||
| shouldRefreshComposerRender( | ||
| composerRenderSnapshot({ kind: 'slash', sigil: '/', query: '', from: 1 }, true), | ||
| composerRenderSnapshot({ kind: 'none' }, true), | ||
| ), | ||
| ).toBe(true); | ||
| // One palette trigger kind swapping directly for the other. | ||
| expect( | ||
| shouldRefreshComposerRender( | ||
| composerRenderSnapshot({ kind: 'slash', sigil: '/', query: '', from: 1 }, true), | ||
| composerRenderSnapshot({ kind: 'at', query: '', from: 1 }, true), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('keeps the ordinary update default explicit', () => { | ||
| expect(__composerRenderGateDefaultsForTest.ordinaryTextUpdatesRefresh).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.