From ad55c21021d33fdca20c6dfef5bb7f016afa5779 Mon Sep 17 00:00:00 2001 From: iruzen-dono Date: Mon, 16 Nov 2026 18:31:11 +0000 Subject: [PATCH 1/3] perf(desktop): batch thinking deltas like text deltas while streaming Thinking deltas updated the store on every event, so each one created a new messages array, rebuilt the render model in MessageList and re-rendered every tool group through a new toolResultMap. Fast thinking output did that dozens of times per second. They now buffer into the same 50ms window as text deltas (pendingThinkingBySession plus a flush timer), so one window produces one store update. Ten thinking deltas measure 10 store updates on main and 1 here. Ordering is explicit rather than incidental: - pending thinking flushes at the top of content_start for every block type, so a reply that thinks before it writes text keeps chatState 'streaming' and activeThinkingId null for the rest of the answer instead of falling back to 'thinking' at the first text delta; - it flushes before a tool result, turn end, an error, stopGeneration and disconnectSession, and in the user_message case after the duplicate check so a message from another client cannot land before the thinking; - clearMessages and a cleared session drop it, and the mirror's idle path folds pending text before pending thinking, matching flushPendingThinking. Tests: three new cases in chatStore.test.ts. One window with ten deltas produces one update; the real sequence thinking -> content_start text -> content_delta keeps the streaming state and yields thinking before text at turn end; a remote user_message lands after the thinking. One existing test advanced its timers for the buffered timing. bunx vitest run on the desktop suite and bun run lint (tsc, tsc electron, oxlint) both pass. --- desktop/src/stores/chatStore.test.ts | 96 +++++++++++++++++++++++ desktop/src/stores/chatStore.ts | 110 ++++++++++++++++++++++----- 2 files changed, 187 insertions(+), 19 deletions(-) diff --git a/desktop/src/stores/chatStore.test.ts b/desktop/src/stores/chatStore.test.ts index c466b440..91fd9a4c 100644 --- a/desktop/src/stores/chatStore.test.ts +++ b/desktop/src/stores/chatStore.test.ts @@ -2560,6 +2560,7 @@ describe('chatStore history mapping', () => { type: 'thinking', text: 'internal note', }) + vi.advanceTimersByTime(60) expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ { type: 'assistant_text', content: 'visible answer before thinking' }, @@ -2570,6 +2571,101 @@ describe('chatStore history mapping', () => { vi.useRealTimers() }) + it('batches thinking deltas into one store update per window', () => { + vi.useFakeTimers() + + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + let updates = 0 + const unsubscribe = useChatStore.subscribe(() => { + updates += 1 + }) + + for (const chunk of ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']) { + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: chunk }) + } + + expect(updates).toBe(0) + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toHaveLength(0) + + vi.advanceTimersByTime(60) + unsubscribe() + + expect(updates).toBe(1) + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'thinking', content: 'onetwothreefourfivesixseveneightnineten' }, + ]) + + vi.runOnlyPendingTimers() + vi.useRealTimers() + }) + + it('flushes thinking at content_start so the text keeps the streaming state', () => { + vi.useFakeTimers() + + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + vi.advanceTimersByTime(60) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.chatState).toBe('thinking') + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.activeThinkingId).not.toBeNull() + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_start', blockType: 'text' }) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.chatState).toBe('streaming') + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.activeThinkingId).toBeNull() + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_delta', text: 'the answer' }) + vi.advanceTimersByTime(60) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.chatState).toBe('streaming') + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.activeThinkingId).toBeNull() + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.streamingText).toBe('the answer') + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { + type: 'message_complete', + usage: { input_tokens: 1, output_tokens: 2 }, + }) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'thinking', content: 'internal note' }, + { type: 'assistant_text', content: 'the answer' }, + ]) + + vi.runOnlyPendingTimers() + vi.useRealTimers() + }) + + it('flushes thinking before a message from another client lands', () => { + vi.useFakeTimers() + + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'user_message', id: 'phone-1', content: 'from phone' }) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'thinking', content: 'internal note' }, + { type: 'user_text', content: 'from phone' }, + ]) + + vi.runOnlyPendingTimers() + vi.useRealTimers() + }) + it('flushes pending text before appending an error message', () => { vi.useFakeTimers() diff --git a/desktop/src/stores/chatStore.ts b/desktop/src/stores/chatStore.ts index f4ccadcb..73488e4b 100644 --- a/desktop/src/stores/chatStore.ts +++ b/desktop/src/stores/chatStore.ts @@ -316,6 +316,75 @@ function clearPendingDelta(sessionId: string): void { pendingDeltaBySession.delete(sessionId) } +const pendingThinkingBySession = new Map() +const thinkingFlushTimerBySession = new Map>() + +function consumePendingThinking(sessionId: string): string { + const flushTimer = thinkingFlushTimerBySession.get(sessionId) + if (flushTimer) { + clearTimeout(flushTimer) + thinkingFlushTimerBySession.delete(sessionId) + } + const text = pendingThinkingBySession.get(sessionId) ?? '' + pendingThinkingBySession.delete(sessionId) + return text +} + +function appendPendingThinking(sessionId: string, text: string): void { + pendingThinkingBySession.set( + sessionId, + `${pendingThinkingBySession.get(sessionId) ?? ''}${text}`, + ) +} + +function clearPendingThinking(sessionId: string): void { + const flushTimer = thinkingFlushTimerBySession.get(sessionId) + if (flushTimer) { + clearTimeout(flushTimer) + thinkingFlushTimerBySession.delete(sessionId) + } + pendingThinkingBySession.delete(sessionId) +} + +function mergeThinkingIntoMessages( + messages: UIMessage[], + content: string, + timestamp: number, +): { messages: UIMessage[]; activeThinkingId: string } { + const last = messages[messages.length - 1] + if (last && last.type === 'thinking') { + const updated = [...messages] + updated[updated.length - 1] = { ...last, content: last.content + content } + return { messages: updated, activeThinkingId: last.id } + } + const id = nextId() + return { + messages: appendMessage(messages, { id, type: 'thinking', content, timestamp }), + activeThinkingId: id, + } +} + +function flushPendingThinking(sessionId: string): void { + const text = consumePendingThinking(sessionId) + if (!text) return + const now = Date.now() + useChatStore.setState((s) => ({ + sessions: updateSessionIn(s.sessions, sessionId, (sess) => { + const pendingText = `${sess.streamingText}${consumePendingDelta(sessionId)}` + const base = pendingText.trim() + ? appendAssistantTextMessage(sess.messages, pendingText, now) + : sess.messages + const merged = mergeThinkingIntoMessages(base, text, now) + return { + messages: merged.messages, + chatState: 'thinking', + activeThinkingId: merged.activeThinkingId, + streamingText: '', + } + }), + })) +} + function consumePendingToolInputDelta(sessionId: string): string { const flushTimer = toolInputFlushTimerBySession.get(sessionId) if (flushTimer) { @@ -802,6 +871,7 @@ export const useChatStore = create((set, get) => ({ const text = consumePendingDelta(sessionId) set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, (sess) => ({ streamingText: sess.streamingText + text })) })) } + flushPendingThinking(sessionId) clearPendingToolInputDelta(sessionId) clearPendingTaskToolUseIds(sessionId) useCLITaskStore.getState().clearTasks(sessionId) @@ -967,6 +1037,7 @@ export const useChatStore = create((set, get) => ({ const text = consumePendingDelta(sessionId) set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, (sess) => ({ streamingText: sess.streamingText + text })) })) } + flushPendingThinking(sessionId) clearPendingToolInputDelta(sessionId) set((s) => { const session = s.sessions[sessionId] @@ -1138,6 +1209,7 @@ export const useChatStore = create((set, get) => ({ }, clearMessages: (sessionId) => { + clearPendingThinking(sessionId) clearPendingTaskToolUseIds(sessionId) clearPendingToolInputDelta(sessionId) set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, () => ({ @@ -1190,6 +1262,7 @@ export const useChatStore = create((set, get) => ({ const session = get().sessions[sessionId] if (!session) break if (session.messages.some((message) => message.type === 'user_text' && message.transcriptMessageId === msg.id)) break + flushPendingThinking(sessionId) update((s) => { const pendingText = `${s.streamingText}${consumePendingDelta(sessionId)}` const base = pendingText.trim() ? appendAssistantTextMessage(s.messages, pendingText, Date.now()) : s.messages @@ -1224,6 +1297,7 @@ export const useChatStore = create((set, get) => ({ case 'content_start': { const session = get().sessions[sessionId] if (!session) break + flushPendingThinking(sessionId) const pendingText = `${session.streamingText}${consumePendingDelta(sessionId)}` if (msg.blockType !== 'text' && pendingText.trim()) { update((s) => ({ @@ -1343,28 +1417,19 @@ export const useChatStore = create((set, get) => ({ break case 'thinking': - update((s) => { - const pendingText = `${s.streamingText}${consumePendingDelta(sessionId)}` - const base = pendingText.trim() - ? appendAssistantTextMessage(s.messages, pendingText, Date.now()) - : s.messages - const last = base[base.length - 1] - if (last && last.type === 'thinking') { - const updated = [...base] - updated[updated.length - 1] = { ...last, content: last.content + msg.text } - return { messages: updated, chatState: 'thinking', activeThinkingId: last.id, streamingText: '' } - } - const id = nextId() - return { - messages: appendMessage(base, { id, type: 'thinking', content: msg.text, timestamp: Date.now() }), - chatState: 'thinking', - activeThinkingId: id, - streamingText: '', - } - }) + if (!get().sessions[sessionId]) break + appendPendingThinking(sessionId, msg.text) + if (!thinkingFlushTimerBySession.has(sessionId)) { + const timer = setTimeout(() => { + thinkingFlushTimerBySession.delete(sessionId) + flushPendingThinking(sessionId) + }, 50) + thinkingFlushTimerBySession.set(sessionId, timer) + } break case 'tool_use_complete': { + flushPendingThinking(sessionId) clearPendingToolInputDelta(sessionId) const session = get().sessions[sessionId] const toolName = msg.toolName || session?.activeToolName || 'unknown' @@ -1450,6 +1515,7 @@ export const useChatStore = create((set, get) => ({ case 'message_complete': { const session = get().sessions[sessionId] if (!session) break + flushPendingThinking(sessionId) const wasAgentRunning = session.chatState !== 'idle' const text = `${session.streamingText}${consumePendingDelta(sessionId)}` let completionMessages = session.messages @@ -1489,6 +1555,7 @@ export const useChatStore = create((set, get) => ({ case 'error': if (SILENT_PERMISSION_ERROR_CODES.has(msg.code)) break + flushPendingThinking(sessionId) update((s) => { const pendingText = `${s.streamingText}${consumePendingDelta(sessionId)}` let newMessages = s.messages @@ -1543,6 +1610,7 @@ export const useChatStore = create((set, get) => ({ }) } if (msg.subtype === 'session_cleared') { + clearPendingThinking(sessionId) update(() => ({ messages: [], streamingText: '', @@ -1860,6 +1928,10 @@ function applyMirror(sessionId: string, next: SessionState): void { } else { streamingText = pendingText } + const pendingThinking = consumePendingThinking(sessionId) + if (pendingThinking) { + messages = mergeThinkingIntoMessages(messages, pendingThinking, now).messages + } } const previousVerb = specialStatusVerb(previous) From 3280d0eae4bf35e632c74ef3b76dfeaf986e9c1c Mon Sep 17 00:00:00 2001 From: iruzen-dono Date: Tue, 17 Nov 2026 20:10:55 +0000 Subject: [PATCH 2/3] fix(desktop): read the session after flushing pending thinking content_start and message_complete captured the session before flushPendingThinking moved pending text into a message and cleared streamingText, so the handler then rebuilt that text from the stale snapshot: [abc, thinking, abcdef] instead of [abc, thinking, def], and message_complete appended to the pre-flush messages array, overwriting the thinking block it had just written. Both handlers now flush first and read the session afterwards. The three delta buffers were three copies of the same append, consume, clear and schedule code. They now share one createDeltaBuffer helper, so each call site keeps only its own flush body and content_delta no longer manages its maps by hand. A local send flushes pending thinking the way the remote user_message path already did, so a thinking block cannot land after the user bubble. Tests: 11 new cases. The content_start case no longer lets the timer flush the thinking before content_start runs, and the stale-read sequence, the flush in message_complete, error, tool_use_complete, stopGeneration, disconnectSession and a local send, the clears in clearMessages and session_cleared, and the mirror idle merge each have a case that fails when the line it covers is removed. --- desktop/src/stores/chatStore.test.ts | 255 ++++++++++++++- desktop/src/stores/chatStore.ts | 306 ++++++++---------- .../performance/optimization-report.md | 2 +- 3 files changed, 388 insertions(+), 175 deletions(-) diff --git a/desktop/src/stores/chatStore.test.ts b/desktop/src/stores/chatStore.test.ts index 91fd9a4c..598a163f 100644 --- a/desktop/src/stores/chatStore.test.ts +++ b/desktop/src/stores/chatStore.test.ts @@ -2614,15 +2614,17 @@ describe('chatStore history mapping', () => { }) useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) - vi.advanceTimersByTime(60) - expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.chatState).toBe('thinking') - expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.activeThinkingId).not.toBeNull() + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toHaveLength(0) + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.activeThinkingId).toBeNull() useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_start', blockType: 'text' }) expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.chatState).toBe('streaming') expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.activeThinkingId).toBeNull() + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'thinking', content: 'internal note' }, + ]) useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_delta', text: 'the answer' }) vi.advanceTimersByTime(60) @@ -2666,6 +2668,253 @@ describe('chatStore history mapping', () => { vi.useRealTimers() }) + it('does not restore text a thinking flush already wrote when content_start repeats it', () => { + vi.useFakeTimers() + + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_start', blockType: 'text' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_delta', text: 'abc' }) + vi.advanceTimersByTime(60) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'hmm' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_start', blockType: 'text' }) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'assistant_text', content: 'abc' }, + { type: 'thinking', content: 'hmm' }, + ]) + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.streamingText).toBe('') + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_delta', text: 'def' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { + type: 'message_complete', + usage: { input_tokens: 1, output_tokens: 2 }, + }) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'assistant_text', content: 'abc' }, + { type: 'thinking', content: 'hmm' }, + { type: 'assistant_text', content: 'def' }, + ]) + + vi.runOnlyPendingTimers() + vi.useRealTimers() + }) + + it('does not duplicate text a thinking flush wrote when content_start opens a tool block', () => { + vi.useFakeTimers() + + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_start', blockType: 'text' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_delta', text: 'abc' }) + vi.advanceTimersByTime(60) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'hmm' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { + type: 'content_start', + blockType: 'tool_use', + toolUseId: 'tool-1', + toolName: 'Read', + }) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'assistant_text', content: 'abc' }, + { type: 'thinking', content: 'hmm' }, + { type: 'tool_use', toolName: 'Read' }, + ]) + + vi.runOnlyPendingTimers() + vi.useRealTimers() + }) + + it('keeps a flushed thinking block when message_complete follows it', () => { + vi.useFakeTimers() + + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_start', blockType: 'text' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'content_delta', text: 'abc' }) + vi.advanceTimersByTime(60) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'hmm' }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { + type: 'message_complete', + usage: { input_tokens: 1, output_tokens: 2 }, + }) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'assistant_text', content: 'abc' }, + { type: 'thinking', content: 'hmm' }, + ]) + + vi.runOnlyPendingTimers() + vi.useRealTimers() + }) + + it('flushes buffered thinking before a message from this client lands', () => { + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'idle' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + useChatStore.getState().sendMessage(TEST_SESSION_ID, 'from desktop') + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'thinking', content: 'internal note' }, + { type: 'user_text', content: 'from desktop' }, + ]) + }) + + it('flushes buffered thinking before an error message', () => { + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { + type: 'error', + message: 'provider failed', + code: 'provider_error', + }) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'thinking', content: 'internal note' }, + { type: 'error', message: 'provider failed' }, + ]) + }) + + it('flushes buffered thinking before a tool use completes', () => { + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { + type: 'tool_use_complete', + toolName: 'Read', + toolUseId: 'tool-1', + input: {}, + }) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'thinking', content: 'internal note' }, + { type: 'tool_use', toolName: 'Read' }, + ]) + }) + + it('flushes buffered thinking when the user stops generation', () => { + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + useChatStore.getState().stopGeneration(TEST_SESSION_ID) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'thinking', content: 'internal note' }, + ]) + }) + + it('drops buffered thinking when the session disconnects', () => { + vi.useFakeTimers() + + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + useChatStore.getState().disconnectSession(TEST_SESSION_ID) + useChatStore.setState({ sessions: { [TEST_SESSION_ID]: makeSession() } }) + vi.advanceTimersByTime(60) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toHaveLength(0) + + vi.runOnlyPendingTimers() + vi.useRealTimers() + }) + + it('drops buffered thinking when the messages are cleared', () => { + vi.useFakeTimers() + + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + useChatStore.getState().clearMessages(TEST_SESSION_ID) + vi.advanceTimersByTime(60) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toHaveLength(0) + + vi.runOnlyPendingTimers() + vi.useRealTimers() + }) + + it('drops buffered thinking when the server clears the session', () => { + vi.useFakeTimers() + + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { + type: 'system_notification', + subtype: 'session_cleared', + message: 'Conversation cleared', + }) + vi.advanceTimersByTime(60) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toHaveLength(0) + + vi.runOnlyPendingTimers() + vi.useRealTimers() + }) + + it('folds buffered text and thinking when a snapshot reports the turn idle', () => { + useChatStore.setState({ + sessions: { + [TEST_SESSION_ID]: makeSession({ chatState: 'streaming', streamingText: 'visible answer' }), + }, + }) + + useChatStore.getState().handleServerMessage(TEST_SESSION_ID, { type: 'thinking', text: 'internal note' }) + sendSnapshot(mirrorState()) + + expect(useChatStore.getState().sessions[TEST_SESSION_ID]?.messages).toMatchObject([ + { type: 'assistant_text', content: 'visible answer' }, + { type: 'thinking', content: 'internal note' }, + ]) + }) + it('flushes pending text before appending an error message', () => { vi.useFakeTimers() diff --git a/desktop/src/stores/chatStore.ts b/desktop/src/stores/chatStore.ts index 73488e4b..9e970d0e 100644 --- a/desktop/src/stores/chatStore.ts +++ b/desktop/src/stores/chatStore.ts @@ -284,68 +284,117 @@ function upsertToolUseMessage( return next } -const pendingDeltaBySession = new Map() -const flushTimerBySession = new Map>() -const pendingToolInputDeltaBySession = new Map() -const toolInputFlushTimerBySession = new Map>() - -function consumePendingDelta(sessionId: string): string { - const flushTimer = flushTimerBySession.get(sessionId) - if (flushTimer) { - clearTimeout(flushTimer) - flushTimerBySession.delete(sessionId) - } - const text = pendingDeltaBySession.get(sessionId) ?? '' - pendingDeltaBySession.delete(sessionId) - return text -} +const STREAM_FLUSH_MS = 50 -function appendPendingDelta(sessionId: string, text: string): void { - pendingDeltaBySession.set( - sessionId, - `${pendingDeltaBySession.get(sessionId) ?? ''}${text}`, - ) +type DeltaBuffer = { + append: (sessionId: string, text: string) => void + consume: (sessionId: string) => string + clear: (sessionId: string) => void + flush: (sessionId: string) => void + schedule: (sessionId: string) => void } -function clearPendingDelta(sessionId: string): void { - const flushTimer = flushTimerBySession.get(sessionId) - if (flushTimer) { - clearTimeout(flushTimer) - flushTimerBySession.delete(sessionId) - } - pendingDeltaBySession.delete(sessionId) -} +function createDeltaBuffer(onFlush: (sessionId: string, text: string) => void): DeltaBuffer { + const pendingBySession = new Map() + const timerBySession = new Map>() -const pendingThinkingBySession = new Map() -const thinkingFlushTimerBySession = new Map>() + const stopTimer = (sessionId: string): void => { + const timer = timerBySession.get(sessionId) + if (!timer) return + clearTimeout(timer) + timerBySession.delete(sessionId) + } -function consumePendingThinking(sessionId: string): string { - const flushTimer = thinkingFlushTimerBySession.get(sessionId) - if (flushTimer) { - clearTimeout(flushTimer) - thinkingFlushTimerBySession.delete(sessionId) + const consume = (sessionId: string): string => { + stopTimer(sessionId) + const text = pendingBySession.get(sessionId) ?? '' + pendingBySession.delete(sessionId) + return text } - const text = pendingThinkingBySession.get(sessionId) ?? '' - pendingThinkingBySession.delete(sessionId) - return text -} -function appendPendingThinking(sessionId: string, text: string): void { - pendingThinkingBySession.set( - sessionId, - `${pendingThinkingBySession.get(sessionId) ?? ''}${text}`, - ) -} + const flush = (sessionId: string): void => { + const text = consume(sessionId) + if (text) onFlush(sessionId, text) + } -function clearPendingThinking(sessionId: string): void { - const flushTimer = thinkingFlushTimerBySession.get(sessionId) - if (flushTimer) { - clearTimeout(flushTimer) - thinkingFlushTimerBySession.delete(sessionId) + return { + append: (sessionId, text) => { + pendingBySession.set(sessionId, `${pendingBySession.get(sessionId) ?? ''}${text}`) + }, + consume, + clear: (sessionId) => { + stopTimer(sessionId) + pendingBySession.delete(sessionId) + }, + flush, + schedule: (sessionId) => { + if (timerBySession.has(sessionId)) return + const timer = setTimeout(() => { + timerBySession.delete(sessionId) + flush(sessionId) + }, STREAM_FLUSH_MS) + timerBySession.set(sessionId, timer) + }, } - pendingThinkingBySession.delete(sessionId) } +const textDeltaBuffer = createDeltaBuffer((sessionId, text) => { + useChatStore.setState((s) => ({ + sessions: updateSessionIn(s.sessions, sessionId, (session) => ({ + streamingText: session.streamingText + text, + })), + })) +}) + +const toolInputDeltaBuffer = createDeltaBuffer((sessionId, text) => { + useChatStore.setState((s) => ({ + sessions: updateSessionIn(s.sessions, sessionId, (session) => { + const partialInput = session.streamingToolInput + text + const activeToolUseId = session.activeToolUseId + return { + streamingToolInput: partialInput, + ...(activeToolUseId + ? { + messages: upsertToolUseMessage(session.messages, activeToolUseId, (existing) => { + const toolName = existing?.toolName ?? session.activeToolName ?? 'unknown' + return { + id: existing?.id ?? nextId(), + type: 'tool_use', + toolName, + toolUseId: activeToolUseId, + input: buildPartialToolInputPreview(partialInput, existing?.input), + timestamp: existing?.timestamp ?? Date.now(), + parentToolUseId: existing?.parentToolUseId, + isPending: true, + partialInput, + } + }), + } + : {}), + } + }), + })) +}) + +const thinkingDeltaBuffer = createDeltaBuffer((sessionId, text) => { + const timestamp = Date.now() + useChatStore.setState((s) => ({ + sessions: updateSessionIn(s.sessions, sessionId, (session) => { + const pendingText = `${session.streamingText}${textDeltaBuffer.consume(sessionId)}` + const base = pendingText.trim() + ? appendAssistantTextMessage(session.messages, pendingText, timestamp) + : session.messages + const merged = mergeThinkingIntoMessages(base, text, timestamp) + return { + messages: merged.messages, + chatState: 'thinking', + activeThinkingId: merged.activeThinkingId, + streamingText: '', + } + }), + })) +}) + function mergeThinkingIntoMessages( messages: UIMessage[], content: string, @@ -365,51 +414,7 @@ function mergeThinkingIntoMessages( } function flushPendingThinking(sessionId: string): void { - const text = consumePendingThinking(sessionId) - if (!text) return - const now = Date.now() - useChatStore.setState((s) => ({ - sessions: updateSessionIn(s.sessions, sessionId, (sess) => { - const pendingText = `${sess.streamingText}${consumePendingDelta(sessionId)}` - const base = pendingText.trim() - ? appendAssistantTextMessage(sess.messages, pendingText, now) - : sess.messages - const merged = mergeThinkingIntoMessages(base, text, now) - return { - messages: merged.messages, - chatState: 'thinking', - activeThinkingId: merged.activeThinkingId, - streamingText: '', - } - }), - })) -} - -function consumePendingToolInputDelta(sessionId: string): string { - const flushTimer = toolInputFlushTimerBySession.get(sessionId) - if (flushTimer) { - clearTimeout(flushTimer) - toolInputFlushTimerBySession.delete(sessionId) - } - const text = pendingToolInputDeltaBySession.get(sessionId) ?? '' - pendingToolInputDeltaBySession.delete(sessionId) - return text -} - -function appendPendingToolInputDelta(sessionId: string, text: string): void { - pendingToolInputDeltaBySession.set( - sessionId, - `${pendingToolInputDeltaBySession.get(sessionId) ?? ''}${text}`, - ) -} - -function clearPendingToolInputDelta(sessionId: string): void { - const flushTimer = toolInputFlushTimerBySession.get(sessionId) - if (flushTimer) { - clearTimeout(flushTimer) - toolInputFlushTimerBySession.delete(sessionId) - } - pendingToolInputDeltaBySession.delete(sessionId) + thinkingDeltaBuffer.flush(sessionId) } function appendAssistantTextMessage( @@ -867,12 +872,12 @@ export const useChatStore = create((set, get) => ({ prewarmPending.delete(sessionId) reconnectingSessions.delete(sessionId) reloadAtTurnEnd.delete(sessionId) - if (pendingDeltaBySession.has(sessionId)) { - const text = consumePendingDelta(sessionId) - set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, (sess) => ({ streamingText: sess.streamingText + text })) })) + const bufferedText = textDeltaBuffer.consume(sessionId) + if (bufferedText) { + set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, (sess) => ({ streamingText: sess.streamingText + bufferedText })) })) } flushPendingThinking(sessionId) - clearPendingToolInputDelta(sessionId) + toolInputDeltaBuffer.clear(sessionId) clearPendingTaskToolUseIds(sessionId) useCLITaskStore.getState().clearTasks(sessionId) wsManager.disconnect(sessionId) @@ -927,9 +932,11 @@ export const useChatStore = create((set, get) => ({ const messageId = isMemberSession ? undefined : randomUUID() + flushPendingThinking(sessionId) + set((s) => { const session = s.sessions[sessionId] ?? createDefaultSessionState() - const bufferedDelta = consumePendingDelta(sessionId) + const bufferedDelta = textDeltaBuffer.consume(sessionId) const pendingAssistantText = `${session.streamingText}${bufferedDelta}` const newMessages = pendingAssistantText.trim() @@ -1033,12 +1040,12 @@ export const useChatStore = create((set, get) => ({ stopGeneration: (sessionId) => { wsManager.send(sessionId, { type: 'stop_generation' }) - if (pendingDeltaBySession.has(sessionId)) { - const text = consumePendingDelta(sessionId) - set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, (sess) => ({ streamingText: sess.streamingText + text })) })) + const bufferedText = textDeltaBuffer.consume(sessionId) + if (bufferedText) { + set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, (sess) => ({ streamingText: sess.streamingText + bufferedText })) })) } flushPendingThinking(sessionId) - clearPendingToolInputDelta(sessionId) + toolInputDeltaBuffer.clear(sessionId) set((s) => { const session = s.sessions[sessionId] if (!session) return s @@ -1209,9 +1216,9 @@ export const useChatStore = create((set, get) => ({ }, clearMessages: (sessionId) => { - clearPendingThinking(sessionId) + thinkingDeltaBuffer.clear(sessionId) clearPendingTaskToolUseIds(sessionId) - clearPendingToolInputDelta(sessionId) + toolInputDeltaBuffer.clear(sessionId) set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, () => ({ messages: [], activeGoal: null, @@ -1264,7 +1271,7 @@ export const useChatStore = create((set, get) => ({ if (session.messages.some((message) => message.type === 'user_text' && message.transcriptMessageId === msg.id)) break flushPendingThinking(sessionId) update((s) => { - const pendingText = `${s.streamingText}${consumePendingDelta(sessionId)}` + const pendingText = `${s.streamingText}${textDeltaBuffer.consume(sessionId)}` const base = pendingText.trim() ? appendAssistantTextMessage(s.messages, pendingText, Date.now()) : s.messages const incoming: UIMessage = { id: nextId(), @@ -1286,7 +1293,7 @@ export const useChatStore = create((set, get) => ({ case 'message_id': update((s) => { - const pendingText = `${s.streamingText}${consumePendingDelta(sessionId)}` + const pendingText = `${s.streamingText}${textDeltaBuffer.consume(sessionId)}` return { streamingText: '', messages: tagLatestAssistantText(s.messages, pendingText, msg.id, Date.now()), @@ -1295,10 +1302,11 @@ export const useChatStore = create((set, get) => ({ break case 'content_start': { + if (!get().sessions[sessionId]) break + flushPendingThinking(sessionId) const session = get().sessions[sessionId] if (!session) break - flushPendingThinking(sessionId) - const pendingText = `${session.streamingText}${consumePendingDelta(sessionId)}` + const pendingText = `${session.streamingText}${textDeltaBuffer.consume(sessionId)}` if (msg.blockType !== 'text' && pendingText.trim()) { update((s) => ({ messages: appendAssistantTextMessage(s.messages, pendingText, Date.now()), @@ -1313,7 +1321,7 @@ export const useChatStore = create((set, get) => ({ apiRetry: null, })) } else if (msg.blockType === 'tool_use') { - clearPendingToolInputDelta(sessionId) + toolInputDeltaBuffer.clear(sessionId) const toolUseId = msg.toolUseId ?? null const toolName = msg.toolName ?? 'unknown' update((s) => ({ @@ -1368,69 +1376,24 @@ export const useChatStore = create((set, get) => ({ case 'content_delta': if (msg.text !== undefined) { if (!get().sessions[sessionId]) break - appendPendingDelta(sessionId, msg.text) - if (!flushTimerBySession.has(sessionId)) { - const timer = setTimeout(() => { - const text = pendingDeltaBySession.get(sessionId) ?? '' - pendingDeltaBySession.delete(sessionId) - flushTimerBySession.delete(sessionId) - update((s) => ({ streamingText: s.streamingText + text })) - }, 50) - flushTimerBySession.set(sessionId, timer) - } + textDeltaBuffer.append(sessionId, msg.text) + textDeltaBuffer.schedule(sessionId) } if (msg.toolInput !== undefined) { - appendPendingToolInputDelta(sessionId, msg.toolInput) - if (!toolInputFlushTimerBySession.has(sessionId)) { - const timer = setTimeout(() => { - const text = consumePendingToolInputDelta(sessionId) - if (!text) return - update((s) => { - const partialInput = s.streamingToolInput + text - const activeToolUseId = s.activeToolUseId - return { - streamingToolInput: partialInput, - ...(activeToolUseId - ? { - messages: upsertToolUseMessage(s.messages, activeToolUseId, (existing) => { - const toolName = existing?.toolName ?? s.activeToolName ?? 'unknown' - return { - id: existing?.id ?? nextId(), - type: 'tool_use', - toolName, - toolUseId: activeToolUseId, - input: buildPartialToolInputPreview(partialInput, existing?.input), - timestamp: existing?.timestamp ?? Date.now(), - parentToolUseId: existing?.parentToolUseId, - isPending: true, - partialInput, - } - }), - } - : {}), - } - }) - }, 50) - toolInputFlushTimerBySession.set(sessionId, timer) - } + toolInputDeltaBuffer.append(sessionId, msg.toolInput) + toolInputDeltaBuffer.schedule(sessionId) } break case 'thinking': if (!get().sessions[sessionId]) break - appendPendingThinking(sessionId, msg.text) - if (!thinkingFlushTimerBySession.has(sessionId)) { - const timer = setTimeout(() => { - thinkingFlushTimerBySession.delete(sessionId) - flushPendingThinking(sessionId) - }, 50) - thinkingFlushTimerBySession.set(sessionId, timer) - } + thinkingDeltaBuffer.append(sessionId, msg.text) + thinkingDeltaBuffer.schedule(sessionId) break case 'tool_use_complete': { flushPendingThinking(sessionId) - clearPendingToolInputDelta(sessionId) + toolInputDeltaBuffer.clear(sessionId) const session = get().sessions[sessionId] const toolName = msg.toolName || session?.activeToolName || 'unknown' const toolUseId = msg.toolUseId || session?.activeToolUseId || '' @@ -1513,11 +1476,12 @@ export const useChatStore = create((set, get) => ({ } case 'message_complete': { + if (!get().sessions[sessionId]) break + flushPendingThinking(sessionId) const session = get().sessions[sessionId] if (!session) break - flushPendingThinking(sessionId) const wasAgentRunning = session.chatState !== 'idle' - const text = `${session.streamingText}${consumePendingDelta(sessionId)}` + const text = `${session.streamingText}${textDeltaBuffer.consume(sessionId)}` let completionMessages = session.messages if (text.trim()) { completionMessages = appendAssistantTextMessage(session.messages, text, Date.now()) @@ -1557,7 +1521,7 @@ export const useChatStore = create((set, get) => ({ if (SILENT_PERMISSION_ERROR_CODES.has(msg.code)) break flushPendingThinking(sessionId) update((s) => { - const pendingText = `${s.streamingText}${consumePendingDelta(sessionId)}` + const pendingText = `${s.streamingText}${textDeltaBuffer.consume(sessionId)}` let newMessages = s.messages if (pendingText.trim()) { newMessages = appendAssistantTextMessage(newMessages, pendingText, Date.now()) @@ -1610,7 +1574,7 @@ export const useChatStore = create((set, get) => ({ }) } if (msg.subtype === 'session_cleared') { - clearPendingThinking(sessionId) + thinkingDeltaBuffer.clear(sessionId) update(() => ({ messages: [], streamingText: '', @@ -1628,7 +1592,7 @@ export const useChatStore = create((set, get) => ({ backgroundAgentTasks: {}, agentTaskNotifications: {}, })) - clearPendingDelta(sessionId) + textDeltaBuffer.clear(sessionId) clearPendingTaskToolUseIds(sessionId) useCLITaskStore.getState().clearTasks(sessionId) useSessionStore.getState().updateSessionTitle(sessionId, 'New Session') @@ -1921,14 +1885,14 @@ function applyMirror(sessionId: string, next: SessionState): void { } if (!isRunning) { - const pendingText = `${streamingText}${consumePendingDelta(sessionId)}` + const pendingText = `${streamingText}${textDeltaBuffer.consume(sessionId)}` if (wasRunning && pendingText.trim()) { messages = appendAssistantTextMessage(messages, pendingText, now) streamingText = '' } else { streamingText = pendingText } - const pendingThinking = consumePendingThinking(sessionId) + const pendingThinking = thinkingDeltaBuffer.consume(sessionId) if (pendingThinking) { messages = mergeThinkingIntoMessages(messages, pendingThinking, now).messages } diff --git a/docs/features/performance/optimization-report.md b/docs/features/performance/optimization-report.md index 98bf0b44..4257ce0e 100644 --- a/docs/features/performance/optimization-report.md +++ b/docs/features/performance/optimization-report.md @@ -67,7 +67,7 @@ CLI subprocess → SDK WebSocket → Server 翻译 → Client WebSocket ### 4. chatStore 状态形状重构 -**现状**:`chatStore.ts` 2600 行巨石,`updateSessionIn` 每次创建新 sessions Record + 新 session 对象。`content_delta` 有 50ms 节流缓解,但其他消息类型(status、thinking、tool_result 等)均无节流,立即触发全量 spread。 +**现状**:`chatStore.ts` 2600 行巨石,`updateSessionIn` 每次创建新 sessions Record + 新 session 对象。`content_delta` 与 `thinking` 均有 50ms 节流缓解,但其他消息类型(status、tool_result 等)均无节流,立即触发全量 spread。 - `desktop/src/stores/chatStore.ts:732-740` — `updateSessionIn` 全量 spread - `desktop/src/stores/chatStore.ts:1212-1215` — `update()` 辅助函数包装每个消息处理 From 1c9eca956d858b75589381f9f05e7188a7b7f0b5 Mon Sep 17 00:00:00 2001 From: Diao Shengjia <104132148+GoDiao@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:12:46 +0800 Subject: [PATCH 3/3] refactor(desktop): call the thinking buffer's flush directly --- desktop/src/stores/chatStore.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/desktop/src/stores/chatStore.ts b/desktop/src/stores/chatStore.ts index 9e970d0e..53a102ba 100644 --- a/desktop/src/stores/chatStore.ts +++ b/desktop/src/stores/chatStore.ts @@ -413,10 +413,6 @@ function mergeThinkingIntoMessages( } } -function flushPendingThinking(sessionId: string): void { - thinkingDeltaBuffer.flush(sessionId) -} - function appendAssistantTextMessage( messages: UIMessage[], content: string, @@ -876,7 +872,7 @@ export const useChatStore = create((set, get) => ({ if (bufferedText) { set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, (sess) => ({ streamingText: sess.streamingText + bufferedText })) })) } - flushPendingThinking(sessionId) + thinkingDeltaBuffer.flush(sessionId) toolInputDeltaBuffer.clear(sessionId) clearPendingTaskToolUseIds(sessionId) useCLITaskStore.getState().clearTasks(sessionId) @@ -932,7 +928,7 @@ export const useChatStore = create((set, get) => ({ const messageId = isMemberSession ? undefined : randomUUID() - flushPendingThinking(sessionId) + thinkingDeltaBuffer.flush(sessionId) set((s) => { const session = s.sessions[sessionId] ?? createDefaultSessionState() @@ -1044,7 +1040,7 @@ export const useChatStore = create((set, get) => ({ if (bufferedText) { set((s) => ({ sessions: updateSessionIn(s.sessions, sessionId, (sess) => ({ streamingText: sess.streamingText + bufferedText })) })) } - flushPendingThinking(sessionId) + thinkingDeltaBuffer.flush(sessionId) toolInputDeltaBuffer.clear(sessionId) set((s) => { const session = s.sessions[sessionId] @@ -1269,7 +1265,7 @@ export const useChatStore = create((set, get) => ({ const session = get().sessions[sessionId] if (!session) break if (session.messages.some((message) => message.type === 'user_text' && message.transcriptMessageId === msg.id)) break - flushPendingThinking(sessionId) + thinkingDeltaBuffer.flush(sessionId) update((s) => { const pendingText = `${s.streamingText}${textDeltaBuffer.consume(sessionId)}` const base = pendingText.trim() ? appendAssistantTextMessage(s.messages, pendingText, Date.now()) : s.messages @@ -1303,7 +1299,7 @@ export const useChatStore = create((set, get) => ({ case 'content_start': { if (!get().sessions[sessionId]) break - flushPendingThinking(sessionId) + thinkingDeltaBuffer.flush(sessionId) const session = get().sessions[sessionId] if (!session) break const pendingText = `${session.streamingText}${textDeltaBuffer.consume(sessionId)}` @@ -1392,7 +1388,7 @@ export const useChatStore = create((set, get) => ({ break case 'tool_use_complete': { - flushPendingThinking(sessionId) + thinkingDeltaBuffer.flush(sessionId) toolInputDeltaBuffer.clear(sessionId) const session = get().sessions[sessionId] const toolName = msg.toolName || session?.activeToolName || 'unknown' @@ -1477,7 +1473,7 @@ export const useChatStore = create((set, get) => ({ case 'message_complete': { if (!get().sessions[sessionId]) break - flushPendingThinking(sessionId) + thinkingDeltaBuffer.flush(sessionId) const session = get().sessions[sessionId] if (!session) break const wasAgentRunning = session.chatState !== 'idle' @@ -1519,7 +1515,7 @@ export const useChatStore = create((set, get) => ({ case 'error': if (SILENT_PERMISSION_ERROR_CODES.has(msg.code)) break - flushPendingThinking(sessionId) + thinkingDeltaBuffer.flush(sessionId) update((s) => { const pendingText = `${s.streamingText}${textDeltaBuffer.consume(sessionId)}` let newMessages = s.messages