From 2216e7a855a5bbd22b5a8bf47daaf663fd814796 Mon Sep 17 00:00:00 2001 From: Siddharth Ganesan Date: Tue, 3 Feb 2026 16:40:22 -0800 Subject: [PATCH] Fix abort --- apps/sim/stores/panel/copilot/store.ts | 39 ++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/apps/sim/stores/panel/copilot/store.ts b/apps/sim/stores/panel/copilot/store.ts index e9f33f1c6..9e8d3b137 100644 --- a/apps/sim/stores/panel/copilot/store.ts +++ b/apps/sim/stores/panel/copilot/store.ts @@ -2034,6 +2034,38 @@ function stopStreamingUpdates() { streamingUpdateQueue.clear() } +/** Flush pending streaming updates immediately (apply them to state before clearing) */ +function flushStreamingUpdates(set: any) { + if (streamingUpdateRAF !== null) { + cancelAnimationFrame(streamingUpdateRAF) + streamingUpdateRAF = null + } + if (streamingUpdateQueue.size === 0) return + + const updates = new Map(streamingUpdateQueue) + streamingUpdateQueue.clear() + + set((state: CopilotStore) => { + if (updates.size === 0) return state + return { + messages: state.messages.map((msg) => { + const update = updates.get(msg.id) + if (update) { + return { + ...msg, + content: '', + contentBlocks: + update.contentBlocks.length > 0 + ? createOptimizedContentBlocks(update.contentBlocks) + : [], + } + } + return msg + }), + } + }) +} + function createOptimizedContentBlocks(contentBlocks: any[]): any[] { const result: any[] = new Array(contentBlocks.length) for (let i = 0; i < contentBlocks.length; i++) { @@ -2983,8 +3015,11 @@ export const useCopilotStore = create()( set({ isAborting: true, suppressAbortContinueOption: suppressContinueOption }) try { abortController.abort() - stopStreamingUpdates() - const lastMessage = messages[messages.length - 1] + // Flush pending streaming updates to preserve content before stopping + flushStreamingUpdates(set) + // Re-read messages after flush to get the latest content + const { messages: updatedMessages } = get() + const lastMessage = updatedMessages[updatedMessages.length - 1] if (lastMessage && lastMessage.role === 'assistant') { const textContent = lastMessage.contentBlocks