The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(desktop): typing no longer re-renders the transcript's store subscribers per keystroke
Tekniume03c9c668bb1
diff --git a/apps/desktop/src/app/chat/composer/composer-store-writer.test.ts b/apps/desktop/src/app/chat/composer/composer-store-writer.test.tsnew file mode 100644index 00000000000..24f211b7a2e--- /dev/null+++ b/apps/desktop/src/app/chat/composer/composer-store-writer.test.ts@@ -0,0 +1,42 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++import { createComposerStoreWriter } from './composer-store-writer'++describe('composer store writer — keystrokes reach the assistant-ui store without a write per character', () => {+ beforeEach(() => {+ vi.useFakeTimers()+ })++ afterEach(() => {+ vi.useRealTimers()+ })++ it('writes an edge flip at once and coalesces the rest of a burst into one trailing write', () => {+ const write = vi.fn()+ const writer = createComposerStoreWriter(write, 100)++ writer.typing('h') // empty -> non-empty flips the send-button edge+ writer.typing('he')+ writer.typing('hel')+ writer.typing('hell')++ expect(write.mock.calls).toEqual([['h']])++ vi.advanceTimersByTime(100)++ expect(write.mock.calls).toEqual([['h'], ['hell']])+ expect(writer.pending()).toBe(false)+ })++ it('never lets a pending keystroke land after a later immediate write (send clears the draft)', () => {+ const write = vi.fn()+ const writer = createComposerStoreWriter(write, 100)++ writer.typing('h')+ writer.typing('hello')+ writer.now('')+ vi.advanceTimersByTime(500)++ expect(write.mock.calls).toEqual([['h'], ['']])+ })+})diff --git a/apps/desktop/src/app/chat/composer/composer-store-writer.ts b/apps/desktop/src/app/chat/composer/composer-store-writer.tsnew file mode 100644index 00000000000..fe718747a95--- /dev/null+++ b/apps/desktop/src/app/chat/composer/composer-store-writer.ts@@ -0,0 +1,101 @@+import { SLASH_COMMAND_RE } from '@hermes/shared'++/**+ * Every fact the app derives from the assistant-ui composer text through+ * `useAuiState` (send-button state, the `?` help sigil, steer vs slash, the+ * empty placeholder, hard-newline layout). A keystroke that leaves this+ * signature unchanged is invisible to every one of those selectors, so its+ * store write can wait. Add a field here when a new `s.composer.text`+ * selector appears.+ */+export function composerTextEdges(text: string): string {+ const trimmed = text.trim()++ return [+ text.length === 0,+ trimmed.length > 0,+ text === '?',+ trimmed.length > 0 && !SLASH_COMMAND_RE.test(trimmed),+ text.trimEnd().includes('\n')+ ].join('|')+}++// Typing pause after which the trailing text lands in the store (draft+// persistence and the runtime mirror read it from there).+export const TYPING_STORE_IDLE_MS = 180++export interface ComposerStoreWriter {+ /** Write now, superseding any pending typed text. */+ now: (text: string) => void+ /** Per-keystroke path: write only when an edge flips, else after a typing pause. */+ typing: (text: string) => void+ /** Land pending typed text immediately (before a read that needs the store current). */+ flush: () => void+ /** Drop pending typed text without writing it (unmount). */+ cancel: () => void+ /** True while typed text the store has not seen is waiting. */+ pending: () => boolean+ /** The store changed under us (runtime subscription): adopt it as the written baseline. */+ observed: (text: string) => void+}++/**+ * Each assistant-ui composer write re-renders the thread's client resources+ * and re-checks every `useAuiState` selector of every mounted message, so its+ * cost grows with the transcript (live: 19 ms of main-thread time per+ * keystroke once a long session's history is mounted, vs 0.5 ms on a short+ * one). Keystrokes go through `typing`, which coalesces a burst into one write.+ */+export function createComposerStoreWriter(+ write: (text: string) => void,+ idleMs = TYPING_STORE_IDLE_MS+): ComposerStoreWriter {+ let written = ''+ let pendingText: string | null = null+ let timer: ReturnType<typeof setTimeout> | undefined++ const clear = () => {+ if (timer !== undefined) {+ clearTimeout(timer)+ timer = undefined+ }++ pendingText = null+ }++ const now = (text: string) => {+ clear()+ written = text+ write(text)+ }++ const flush = () => {+ if (pendingText !== null) {+ now(pendingText)+ }+ }++ const typing = (text: string) => {+ if (composerTextEdges(text) !== composerTextEdges(written)) {+ now(text)++ return+ }++ pendingText = text++ if (timer !== undefined) {+ clearTimeout(timer)+ }++ timer = setTimeout(flush, idleMs)+ }++ const observed = (text: string) => {+ if (pendingText === null) {+ written = text+ }+ }++ return { cancel: clear, flush, now, observed, pending: () => pendingText !== null, typing }+}diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.tsindex 7550ceddc02..84c26184595 100644--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts@@ -29,6 +29,7 @@ import { isBrowsingHistory } from '@/store/composer-input-history'import { $composerPopout } from '@/store/composer-popout'import { migrateQueuedPrompts } from '@/store/composer-queue'+import { type ComposerStoreWriter, createComposerStoreWriter } from '../composer-store-writer'import {cloneAttachments,DRAFT_PERSIST_DEBOUNCE_MS,@@ -109,16 +110,24 @@ export function useComposerDraft({// assistant-ui's composer mutators throw when the core isn't bound yet (a// startup/thread-swap window); the DOM + draftRef hold the text and the// subscription reconciles once it binds, so swallow the premature write.- const setComposerText = useCallback(- (value: string) => {- try {- aui.composer().setText(value)- } catch {- // Composer core not bound yet — DOM/draftRef carry the text.- }- },- [aui]- )+ const auiRef = useRef(aui)+ auiRef.current = aui+ const storeWriterRef = useRef<ComposerStoreWriter | null>(null)++ storeWriterRef.current ??= createComposerStoreWriter(value => {+ try {+ auiRef.current.composer().setText(value)+ } catch {+ // Composer core not bound yet — DOM/draftRef carry the text.+ }+ })++ const storeWriter = storeWriterRef.current++ const setComposerText = useCallback((value: string) => storeWriter.now(value), [storeWriter])+++ useEffect(() => () => storeWriter.cancel(), [storeWriter])const editorRef = useRef<HTMLDivElement | null>(null)const draftRef = useRef('')@@ -426,29 +435,36 @@ export function useComposerDraft({// Read the editor's current plain text into draftRef + composer state. This// closes the "queued rAF flush hasn't run yet" window so scope-swap/pagehide- // persistence captures the latest keystrokes.- const syncDraftFromEditor = useCallback(() => {- const editor = editorRef.current-- if (!editor) {- return draftRef.current- }+ // persistence captures the latest keystrokes. `typing` marks ChatBar's+ // coalesced per-keystroke flush, whose store write may wait for a typing+ // pause so the transcript's store subscribers do not re-render on every+ // character (see composer-store-writer.ts); every other caller lands the+ // text now, pending keystrokes included.+ const syncDraftFromEditor = useCallback(+ (typing = false, editor: HTMLDivElement | null = editorRef.current) => {+ if (!editor) {+ return draftRef.current+ }- // Same normalize-then-sanitize the rAF flush does. An emptied editor still- // holds the placeholder <br> that keeps the contenteditable from collapsing- // to a sliver, and that serializes as "\n" — so an editor the user just- // cleared would otherwise stash a one-newline draft and come back non-empty.- normalizeComposerEditorDom(editor)+ // Same normalize-then-sanitize the rAF flush does. An emptied editor still+ // holds the placeholder <br> that keeps the contenteditable from collapsing+ // to a sliver, and that serializes as "\n" — so an editor the user just+ // cleared would otherwise stash a one-newline draft and come back non-empty.+ normalizeComposerEditorDom(editor)- const text = sanitizeComposerInput(composerPlainText(editor))+ const text = sanitizeComposerInput(composerPlainText(editor))- if (text !== draftRef.current) {- draftRef.current = text- setComposerText(text)- }+ if (text !== draftRef.current) {+ draftRef.current = text+ storeWriter[typing ? 'typing' : 'now'](text)+ } else if (!typing) {+ storeWriter.flush()+ }- return text- }, [setComposerText])+ return text+ },+ [storeWriter]+ )// Imperative draft sync — the spine of the "work only when work is to be// performed" model. Subscribing to the composer runtime directly (not@@ -462,7 +478,15 @@ export function useComposerDraft({// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)useEffect(() => {const sync = () => {+ // Typed text the store has not seen yet is newer than the store: mirroring+ // it now would roll draftRef (and an unfocused editor) back. The pending+ // write lands shortly and runs this again.+ if (storeWriter.pending()) {+ return+ }+const text = composerRuntime.getState().text+ storeWriter.observed(text)draftRef.current = textconst editor = editorRef.current@@ -507,7 +531,7 @@ export function useComposerDraft({unsubscribe()window.clearTimeout(draftPersistTimerRef.current)}- }, [composerRuntime, queueEditRef])+ }, [composerRuntime, queueEditRef, storeWriter])const insertText = (text: string) => {const base = draftRef.currentdiff --git a/apps/desktop/src/app/chat/composer/index.tsx b/apps/desktop/src/app/chat/composer/index.tsxindex d174c640138..13e4102c416 100644--- a/apps/desktop/src/app/chat/composer/index.tsx+++ b/apps/desktop/src/app/chat/composer/index.tsx@@ -94,11 +94,9 @@ import { renderComposerQueueSlot, useComposerQueueScopeKey } from './queue-slot'import { RestoredDraftNotice } from './restored-draft-notice'import {beginComposerComposition,- composerPlainText,deleteChipBeforeCaret,deleteSelectionInEditor,insertComposerContentsAtCaret,- normalizeComposerEditorDom,RICH_INPUT_SLOT} from './rich-editor'import { useComposerScope, useComposerSurfaceId } from './scope'@@ -495,20 +493,13 @@ export function ChatBar({// flushes to one per paint is lossless.const flushRafRef = useRef<number | undefined>(undefined)- const flushEditorToDraft = (editor: HTMLDivElement) => {+ const flushEditorToDraft = (editor: HTMLDivElement, typing = false) => {if (flushRafRef.current !== undefined) {window.cancelAnimationFrame(flushRafRef.current)flushRafRef.current = undefined}- normalizeComposerEditorDom(editor)-- const nextDraft = sanitizeComposerInput(composerPlainText(editor))-- if (nextDraft !== draftRef.current) {- draftRef.current = nextDraft- setComposerText(nextDraft)- }+ syncDraftFromEditor(typing, editor)window.setTimeout(refreshTrigger, 0)}@@ -523,7 +514,7 @@ export function ChatBar({flushRafRef.current = window.requestAnimationFrame(() => {flushRafRef.current = undefined- flushEditorToDraft(editor)+ flushEditorToDraft(editor, true)})}