mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Fix empty assistant message being added to history
This commit is contained in:
1 parent
26675ca1f5
commit
45d409ad3f
3 files changed
+126
-27
No files matched your search
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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<ChatMessage> | ((prev: ChatMessage) => Partial<ChatMessage>)) => {
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+14
-11
@@ -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 (
|
||||
<UnheadProvider head={head}>
|
||||
<AppProvider storageKey='test-app-config' defaultConfig={defaultConfig}>
|
||||
<FSProvider fs={fs}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<NostrLoginProvider storageKey='test-login'>
|
||||
<NostrProvider>
|
||||
<BrowserRouter>
|
||||
{children}
|
||||
</BrowserRouter>
|
||||
</NostrProvider>
|
||||
</NostrLoginProvider>
|
||||
</QueryClientProvider>
|
||||
</FSProvider>
|
||||
<AISettingsProvider>
|
||||
<FSProvider fs={fs}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<NostrLoginProvider storageKey='test-login'>
|
||||
<NostrProvider>
|
||||
<BrowserRouter>
|
||||
{children}
|
||||
</BrowserRouter>
|
||||
</NostrProvider>
|
||||
</NostrLoginProvider>
|
||||
</QueryClientProvider>
|
||||
</FSProvider>
|
||||
</AISettingsProvider>
|
||||
</AppProvider>
|
||||
</UnheadProvider>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user