From 45d409ad3ff3547535c1fcd1dd70674203beaaa7 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 23 Aug 2025 13:01:28 -0500 Subject: [PATCH] Fix empty assistant message being added to history --- src/hooks/useStreamingChat.test.ts | 101 +++++++++++++++++++++++++++++ src/hooks/useStreamingChat.ts | 27 ++++---- src/test/TestApp.tsx | 25 +++---- 3 files changed, 126 insertions(+), 27 deletions(-) create mode 100644 src/hooks/useStreamingChat.test.ts diff --git a/src/hooks/useStreamingChat.test.ts b/src/hooks/useStreamingChat.test.ts new file mode 100644 index 00000000..a4b69a0e --- /dev/null +++ b/src/hooks/useStreamingChat.test.ts @@ -0,0 +1,101 @@ +import { describe, it, expect } from 'vitest'; + +// Test the streaming message history saving behavior +describe('useStreamingChat history saving fix', () => { + it('should only save completed messages to history', () => { + // Test the logic for when messages should be saved to history + const testCases = [ + { + message: { + role: 'user' as const, + content: 'Hello AI', + isStreaming: false, + }, + shouldSave: true, + description: 'completed user message' + }, + { + message: { + role: 'assistant' as const, + content: 'Hello user', + isStreaming: false, + }, + shouldSave: true, + description: 'completed assistant message' + }, + { + message: { + role: 'assistant' as const, + content: '', + isStreaming: true, + }, + shouldSave: false, + description: 'streaming assistant message (empty)' + }, + { + message: { + role: 'assistant' as const, + content: 'Partial response...', + isStreaming: true, + }, + shouldSave: false, + description: 'streaming assistant message (with content)' + }, + ]; + + testCases.forEach(({ message, shouldSave }) => { + // This is the logic from the addMessage function + const willSave = !message.isStreaming; + expect(willSave).toBe(shouldSave); + }); + }); + + it('should save messages when streaming stops', () => { + // Test the logic for when streaming messages get saved + const testCases = [ + { + previousMessage: { + role: 'assistant' as const, + content: 'Hello world', + isStreaming: true, + }, + update: { + isStreaming: false, + }, + shouldSave: true, + description: 'streaming stops' + }, + { + previousMessage: { + role: 'assistant' as const, + content: 'Hello', + isStreaming: true, + }, + update: { + content: 'Hello world', + isStreaming: true, + }, + shouldSave: false, + description: 'content update but still streaming' + }, + { + previousMessage: { + role: 'assistant' as const, + content: 'Hello', + isStreaming: false, + }, + update: { + content: 'Hello world', + }, + shouldSave: false, + description: 'update to already completed message' + }, + ]; + + testCases.forEach(({ previousMessage, update, shouldSave }) => { + // This is the logic from the updateMessage function + const willSave = update.isStreaming === false && previousMessage.isStreaming === true; + expect(willSave).toBe(shouldSave); + }); + }); +}); \ No newline at end of file diff --git a/src/hooks/useStreamingChat.ts b/src/hooks/useStreamingChat.ts index 48042d6b..cb812f2b 100644 --- a/src/hooks/useStreamingChat.ts +++ b/src/hooks/useStreamingChat.ts @@ -161,8 +161,11 @@ export function useStreamingChat({ return Array.from(messageMap.values()); }); - // Save to history (async, don't block UI) - saveMessageToHistory(message); + // Only save to history if the message is not currently streaming + // Streaming messages will be saved when they're completed + if (!message.isStreaming) { + saveMessageToHistory(message); + } }, [saveMessageToHistory]); const updateMessage = useCallback((id: string, updates: Partial | ((prev: ChatMessage) => Partial)) => { @@ -171,7 +174,7 @@ export function useStreamingChat({ const updatesObj = typeof updates === 'function' ? updates(msg) : updates; const updatedMessage = { ...msg, ...updatesObj } as ChatMessage; - // Save to history when message is finalized (streaming stops) + // Save to history only when message stops streaming (is completed) if (updatesObj.isStreaming === false && msg.isStreaming === true) { saveMessageToHistory(updatedMessage); } @@ -227,15 +230,6 @@ export function useStreamingChat({ onStepFinish: (stepResult) => { console.log('Step finished:', stepResult); - // Finalize current assistant message if it exists - if (currentAssistantMessageId) { - updateMessage(currentAssistantMessageId, { - isStreaming: false - }); - } - - // Tool calls and results are now handled in the streaming chunks - // Update metadata if callback provided if (onUpdateMetadata && stepResult.toolCalls && stepResult.toolCalls.length > 0) { const toolName = stepResult.toolCalls[0].toolName; @@ -246,9 +240,6 @@ export function useStreamingChat({ if (onStepFinish) { onStepFinish(stepResult); } - - // Reset current assistant message ID for next step - currentAssistantMessageId = null; } }); @@ -363,13 +354,17 @@ export function useStreamingChat({ break; } - case 'finish': { + case 'finish-step': { // Finalize the last assistant message if (currentAssistantMessageId) { updateMessage(currentAssistantMessageId, { isStreaming: false }); } + + // Reset current assistant message ID for next step + currentAssistantMessageId = null; + break; } } diff --git a/src/test/TestApp.tsx b/src/test/TestApp.tsx index 5cf3354c..04304358 100644 --- a/src/test/TestApp.tsx +++ b/src/test/TestApp.tsx @@ -8,6 +8,7 @@ import { AppProvider } from '@/components/AppProvider'; import { AppConfig } from '@/contexts/AppContext'; import { FSProvider } from '@/components/FSProvider'; import { LightningFSAdapter } from '@/lib/LightningFSAdapter'; +import { AISettingsProvider } from '@/components/AISettingsProvider'; interface TestAppProps { children: React.ReactNode; @@ -35,17 +36,19 @@ export function TestApp({ children }: TestAppProps) { return ( - - - - - - {children} - - - - - + + + + + + + {children} + + + + + + );