mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Rip out the streaming chat code
This commit is contained in:
19 files changed
+212
-2551
No files matched your search
@@ -0,0 +1,132 @@
|
||||
import { useState, useCallback, useRef } from 'react';
|
||||
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
||||
import { generateText, Tool } from 'ai';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
|
||||
export interface AIMessage {
|
||||
id: string;
|
||||
role: 'user' | 'assistant' | 'system';
|
||||
content: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
interface UseAIChatOptions {
|
||||
projectId: string;
|
||||
projectName: string;
|
||||
tools?: Record<string, Tool>;
|
||||
systemPrompt?: string;
|
||||
onUpdateMetadata?: (title: string, description: string) => void;
|
||||
}
|
||||
|
||||
export function useAIChat({
|
||||
projectId: _projectId,
|
||||
projectName,
|
||||
tools = {},
|
||||
systemPrompt,
|
||||
onUpdateMetadata
|
||||
}: UseAIChatOptions) {
|
||||
const [messages, setMessages] = useState<AIMessage[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const abortControllerRef = useRef<AbortController | null>(null);
|
||||
const { settings, isConfigured } = useAISettings();
|
||||
|
||||
const addMessage = useCallback((message: AIMessage) => {
|
||||
setMessages(prev => [...prev, message]);
|
||||
}, []);
|
||||
|
||||
const sendMessage = useCallback(async (content: string) => {
|
||||
if (!isConfigured || isLoading) return;
|
||||
|
||||
// Add user message
|
||||
const userMessage: AIMessage = {
|
||||
id: crypto.randomUUID(),
|
||||
role: 'user',
|
||||
content,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
addMessage(userMessage);
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
// Create abort controller for this request
|
||||
abortControllerRef.current = new AbortController();
|
||||
|
||||
// Get all messages including the new user message
|
||||
const allMessages = [...messages, userMessage];
|
||||
const modelMessages = allMessages.map(msg => ({
|
||||
role: msg.role,
|
||||
content: msg.content
|
||||
}));
|
||||
|
||||
// Initialize OpenAI provider
|
||||
const openai = createOpenAICompatible({
|
||||
name: 'custom',
|
||||
baseURL: settings.baseUrl,
|
||||
apiKey: settings.apiKey,
|
||||
});
|
||||
|
||||
const provider = openai(settings.model);
|
||||
|
||||
// Update metadata
|
||||
if (onUpdateMetadata) {
|
||||
onUpdateMetadata('Shakespeare', `Working on ${projectName}...`);
|
||||
}
|
||||
|
||||
// Generate response
|
||||
const result = await generateText({
|
||||
model: provider,
|
||||
messages: modelMessages,
|
||||
tools,
|
||||
system: systemPrompt,
|
||||
abortSignal: abortControllerRef.current.signal,
|
||||
});
|
||||
|
||||
// Add assistant message
|
||||
const assistantMessage: AIMessage = {
|
||||
id: crypto.randomUUID(),
|
||||
role: 'assistant',
|
||||
content: result.text,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
addMessage(assistantMessage);
|
||||
|
||||
} catch (error) {
|
||||
console.error('AI chat error:', error);
|
||||
|
||||
// Add error message
|
||||
const errorMessage: AIMessage = {
|
||||
id: crypto.randomUUID(),
|
||||
role: 'assistant',
|
||||
content: error instanceof Error ? error.message : 'Sorry, I encountered an error. Please try again.',
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
addMessage(errorMessage);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
abortControllerRef.current = null;
|
||||
}
|
||||
}, [isConfigured, isLoading, addMessage, messages, settings.baseUrl, settings.apiKey, settings.model, tools, systemPrompt, onUpdateMetadata, projectName]);
|
||||
|
||||
const stopGeneration = useCallback(() => {
|
||||
if (abortControllerRef.current) {
|
||||
abortControllerRef.current.abort();
|
||||
}
|
||||
}, []);
|
||||
|
||||
const clearMessages = useCallback(() => {
|
||||
setMessages([]);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
messages,
|
||||
isLoading,
|
||||
sendMessage,
|
||||
stopGeneration,
|
||||
clearMessages,
|
||||
addMessage,
|
||||
isConfigured
|
||||
};
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,403 +0,0 @@
|
||||
import { useState, useCallback, useRef, useEffect } from 'react';
|
||||
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
||||
import { streamText, ModelMessage, generateId, Tool, stepCountIs } from 'ai';
|
||||
import type { TextPart, ToolCallPart, ToolResultPart } from 'ai';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
import { AIMessage, DotAI } from '@/lib/DotAI';
|
||||
|
||||
// Extend ModelMessage with additional UI state
|
||||
export type ChatMessage = ModelMessage & {
|
||||
id: string;
|
||||
isStreaming?: boolean;
|
||||
timestamp?: number;
|
||||
}
|
||||
|
||||
interface UseStreamingChatOptions {
|
||||
projectId: string;
|
||||
projectName: string;
|
||||
tools?: Record<string, Tool>;
|
||||
systemPrompt?: string;
|
||||
onStepFinish?: (stepResult: unknown) => void;
|
||||
onUpdateMetadata?: (title: string, description: string) => void;
|
||||
}
|
||||
|
||||
export function useStreamingChat({
|
||||
projectId,
|
||||
projectName,
|
||||
tools = {},
|
||||
systemPrompt,
|
||||
onStepFinish,
|
||||
onUpdateMetadata
|
||||
}: UseStreamingChatOptions) {
|
||||
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
||||
const [isStreaming, setIsStreaming] = useState(false);
|
||||
const [currentStreamingMessageId, setCurrentStreamingMessageId] = useState<string | null>(null);
|
||||
const [sessionName, setSessionName] = useState<string>(DotAI.generateSessionName());
|
||||
const [loadedProjectId, setLoadedProjectId] = useState<string | null>(null);
|
||||
const abortControllerRef = useRef<AbortController | null>(null);
|
||||
const { settings, isConfigured } = useAISettings();
|
||||
const { fs } = useFS();
|
||||
const dotAIRef = useRef<DotAI | null>(null);
|
||||
|
||||
// Initialize DotAI for this project
|
||||
useEffect(() => {
|
||||
const initializeDotAI = async () => {
|
||||
try {
|
||||
const dotAI = new DotAI(fs, `/projects/${projectId}`);
|
||||
dotAIRef.current = dotAI;
|
||||
|
||||
// Generate session name if not already set
|
||||
if (!sessionName) {
|
||||
setSessionName(DotAI.generateSessionName());
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Failed to initialize DotAI:', error);
|
||||
}
|
||||
};
|
||||
|
||||
initializeDotAI();
|
||||
}, [fs, projectId, sessionName]);
|
||||
|
||||
// Load message history when project changes
|
||||
useEffect(() => {
|
||||
const loadMessageHistory = async () => {
|
||||
// Only load if we haven't loaded this project yet or if the project has changed
|
||||
if (!dotAIRef.current || loadedProjectId === projectId) return;
|
||||
|
||||
// Clear messages when switching to a new project
|
||||
setMessages([]);
|
||||
setSessionName(DotAI.generateSessionName());
|
||||
|
||||
try {
|
||||
const historyDir = `/projects/${projectId}/.ai/history`;
|
||||
|
||||
// Check if history directory exists
|
||||
try {
|
||||
await fs.stat(historyDir);
|
||||
} catch {
|
||||
// History directory doesn't exist, no history to load
|
||||
setLoadedProjectId(projectId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all session files
|
||||
const files = await fs.readdir(historyDir);
|
||||
const sessionFiles = files.filter(file => file.endsWith('.jsonl'));
|
||||
|
||||
if (sessionFiles.length === 0) {
|
||||
setLoadedProjectId(projectId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Sort by filename (which includes timestamp) to get the latest session
|
||||
sessionFiles.sort();
|
||||
const latestSessionFile = sessionFiles[sessionFiles.length - 1];
|
||||
const latestSessionName = latestSessionFile.replace('.jsonl', '');
|
||||
|
||||
// Load messages from the latest session
|
||||
const sessionPath = `${historyDir}/${latestSessionFile}`;
|
||||
const sessionContent = await fs.readFile(sessionPath, 'utf8');
|
||||
|
||||
const loadedMessages: ChatMessage[] = [];
|
||||
const lines = sessionContent.split('\n').filter(line => line.trim());
|
||||
|
||||
for (const line of lines) {
|
||||
try {
|
||||
const messageData = JSON.parse(line);
|
||||
// Convert to ChatMessage format
|
||||
const chatMessage: ChatMessage = {
|
||||
id: generateId(),
|
||||
role: messageData.role,
|
||||
content: messageData.content,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
loadedMessages.push(chatMessage);
|
||||
} catch (error) {
|
||||
console.warn('Failed to parse message from history:', error);
|
||||
}
|
||||
}
|
||||
|
||||
if (loadedMessages.length > 0) {
|
||||
setMessages(loadedMessages);
|
||||
setSessionName(latestSessionName);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Failed to load message history:', error);
|
||||
} finally {
|
||||
setLoadedProjectId(projectId);
|
||||
}
|
||||
};
|
||||
|
||||
loadMessageHistory();
|
||||
}, [fs, projectId, loadedProjectId]);
|
||||
|
||||
// Save message to history
|
||||
const saveMessageToHistory = useCallback(async (message: AIMessage) => {
|
||||
if (!dotAIRef.current || !sessionName) {
|
||||
console.error("Unable to save message to history:", {
|
||||
dotAIRef,
|
||||
sessionName,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Convert ChatMessage to DotAI format
|
||||
const aiMessage = {
|
||||
role: message.role,
|
||||
content: message.content
|
||||
};
|
||||
|
||||
await dotAIRef.current.addToHistory(sessionName, aiMessage);
|
||||
} catch (error) {
|
||||
console.warn('Failed to save message to history:', error);
|
||||
}
|
||||
}, [sessionName]);
|
||||
|
||||
// Convert ChatMessage to ModelMessage for AI SDK
|
||||
const convertToModelMessages = useCallback((chatMessages: ChatMessage[]): ModelMessage[] => {
|
||||
return chatMessages.map(({ id, isStreaming, timestamp, ...modelMessage }) => modelMessage as ModelMessage);
|
||||
}, []);
|
||||
|
||||
const addMessage = useCallback((message: ChatMessage) => {
|
||||
setMessages(prev => {
|
||||
const messageMap = new Map(prev.map(m => [m.id, m]));
|
||||
messageMap.set(message.id, message);
|
||||
return Array.from(messageMap.values());
|
||||
});
|
||||
}, []);
|
||||
|
||||
const updateMessage = useCallback((id: string, updates: Partial<ChatMessage> | ((prev: ChatMessage) => Partial<ChatMessage>)) => {
|
||||
setMessages(prev => prev.map(msg => {
|
||||
if (msg.id === id) {
|
||||
const updatesObj = typeof updates === 'function' ? updates(msg) : updates;
|
||||
const updatedMessage = { ...msg, ...updatesObj } as ChatMessage;
|
||||
return updatedMessage;
|
||||
}
|
||||
return msg;
|
||||
}));
|
||||
}, []);
|
||||
|
||||
const sendMessage = useCallback(async (content: string) => {
|
||||
if (!isConfigured || isStreaming) return;
|
||||
|
||||
// Add user message
|
||||
const userMessage: ChatMessage = {
|
||||
id: generateId(),
|
||||
role: 'user',
|
||||
content,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
addMessage(userMessage);
|
||||
await saveMessageToHistory(userMessage);
|
||||
setIsStreaming(true);
|
||||
|
||||
// Track current assistant message for this step
|
||||
let currentAssistantMessageId: string | null = null;
|
||||
|
||||
try {
|
||||
// Create abort controller for this request
|
||||
abortControllerRef.current = new AbortController();
|
||||
|
||||
// Get all messages including the new user message
|
||||
const allMessages = [...messages, userMessage];
|
||||
const modelMessages = convertToModelMessages(allMessages);
|
||||
|
||||
// Initialize OpenAI provider
|
||||
const openai = createOpenAICompatible({
|
||||
name: 'custom',
|
||||
baseURL: settings.baseUrl,
|
||||
apiKey: settings.apiKey,
|
||||
});
|
||||
|
||||
const provider = openai(settings.model);
|
||||
|
||||
// Start streaming
|
||||
const result = streamText({
|
||||
model: provider,
|
||||
messages: modelMessages,
|
||||
tools,
|
||||
system: systemPrompt,
|
||||
abortSignal: abortControllerRef.current.signal,
|
||||
stopWhen: stepCountIs(50),
|
||||
onStepFinish: async (stepResult) => {
|
||||
console.log('Step finished:', stepResult);
|
||||
|
||||
// Update metadata if callback provided
|
||||
if (onUpdateMetadata && stepResult.toolCalls && stepResult.toolCalls.length > 0) {
|
||||
const toolName = stepResult.toolCalls[0].toolName;
|
||||
onUpdateMetadata('Shakespeare', `Working on ${projectName} - ${toolName}`);
|
||||
}
|
||||
|
||||
// Save messages to history after step is finished
|
||||
if (stepResult.finishReason === 'stop') {
|
||||
for (const message of stepResult.response.messages) {
|
||||
await saveMessageToHistory(message);
|
||||
}
|
||||
}
|
||||
|
||||
// Call custom step finish handler
|
||||
if (onStepFinish) {
|
||||
onStepFinish(stepResult);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Stream the content step by step using fullStream
|
||||
for await (const chunk of result.fullStream) {
|
||||
if (abortControllerRef.current?.signal.aborted) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (chunk.type) {
|
||||
case 'text-delta': {
|
||||
// Create new assistant message if we don't have one for this step
|
||||
if (!currentAssistantMessageId) {
|
||||
currentAssistantMessageId = generateId();
|
||||
const assistantMessage: ChatMessage = {
|
||||
id: currentAssistantMessageId,
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
isStreaming: true,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
addMessage(assistantMessage);
|
||||
setCurrentStreamingMessageId(currentAssistantMessageId);
|
||||
}
|
||||
|
||||
// Update the current message with new text
|
||||
updateMessage(currentAssistantMessageId, (prev) => {
|
||||
const currentContent = prev.content;
|
||||
if (typeof currentContent === 'string') {
|
||||
return {
|
||||
content: currentContent + chunk.text,
|
||||
isStreaming: true
|
||||
};
|
||||
} else {
|
||||
// If content is an array, find the last text part or add a new one
|
||||
const content = Array.isArray(currentContent) ?
|
||||
currentContent.filter((part): part is TextPart | ToolCallPart | ToolResultPart =>
|
||||
part.type === 'text' || part.type === 'tool-call' || part.type === 'tool-result'
|
||||
) : [];
|
||||
const lastPart = content[content.length - 1];
|
||||
if (lastPart && lastPart.type === 'text') {
|
||||
lastPart.text += chunk.text;
|
||||
} else {
|
||||
content.push({ type: 'text' as const, text: chunk.text });
|
||||
}
|
||||
return {
|
||||
content,
|
||||
isStreaming: true
|
||||
};
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'tool-call': {
|
||||
// Create new assistant message if we don't have one for this step
|
||||
if (!currentAssistantMessageId) {
|
||||
currentAssistantMessageId = generateId();
|
||||
const assistantMessage: ChatMessage = {
|
||||
id: currentAssistantMessageId,
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
isStreaming: true,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
addMessage(assistantMessage);
|
||||
setCurrentStreamingMessageId(currentAssistantMessageId);
|
||||
}
|
||||
|
||||
// Add tool call to current message
|
||||
updateMessage(currentAssistantMessageId, (prev) => {
|
||||
const currentContent = prev.content;
|
||||
const content: Array<TextPart | ToolCallPart | ToolResultPart> = Array.isArray(currentContent) ?
|
||||
currentContent.filter((part): part is TextPart | ToolCallPart | ToolResultPart =>
|
||||
part.type === 'text' || part.type === 'tool-call' || part.type === 'tool-result'
|
||||
) :
|
||||
typeof currentContent === 'string' && currentContent ? [{ type: 'text' as const, text: currentContent }] : [];
|
||||
|
||||
content.push({
|
||||
type: 'tool-call' as const,
|
||||
toolCallId: chunk.toolCallId,
|
||||
toolName: chunk.toolName,
|
||||
input: chunk.input
|
||||
});
|
||||
|
||||
return { content };
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Streaming error:', error);
|
||||
|
||||
// Handle error - create or update assistant message with error content
|
||||
if (currentAssistantMessageId) {
|
||||
updateMessage(currentAssistantMessageId, {
|
||||
content: error instanceof Error ? error.message : 'Sorry, I encountered an error. Please try again.',
|
||||
isStreaming: false
|
||||
});
|
||||
} else {
|
||||
// Create error message if no assistant message exists
|
||||
const errorMessageId = generateId();
|
||||
addMessage({
|
||||
id: errorMessageId,
|
||||
role: 'assistant',
|
||||
content: error instanceof Error ? error.message : 'Sorry, I encountered an error. Please try again.',
|
||||
isStreaming: false,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
setIsStreaming(false);
|
||||
setCurrentStreamingMessageId(null);
|
||||
abortControllerRef.current = null;
|
||||
}
|
||||
}, [isConfigured, isStreaming, addMessage, saveMessageToHistory, messages, convertToModelMessages, settings.baseUrl, settings.apiKey, settings.model, tools, systemPrompt, onUpdateMetadata, onStepFinish, projectName, updateMessage]);
|
||||
|
||||
const stopStreaming = useCallback(() => {
|
||||
if (abortControllerRef.current) {
|
||||
abortControllerRef.current.abort();
|
||||
}
|
||||
}, []);
|
||||
|
||||
const clearMessages = useCallback(() => {
|
||||
setMessages([]);
|
||||
// Start a new session when clearing messages
|
||||
setSessionName(DotAI.generateSessionName());
|
||||
// Reset loaded project ID so history can be loaded again
|
||||
setLoadedProjectId(null);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
messages,
|
||||
isStreaming,
|
||||
currentStreamingMessageId,
|
||||
sendMessage,
|
||||
stopStreaming,
|
||||
clearMessages,
|
||||
addMessage,
|
||||
updateMessage,
|
||||
isConfigured
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user