Fix loading messages from .ai dir

This commit is contained in:
Alex Gleason committed 2025-08-23 01:31:22 -05:00
1 parent 28d7cd6c4a
commit 26675ca1f5
2 files changed
+14 -11

No files matched your search

+1 -5
View File
@@ -81,7 +81,6 @@ When creating new components or pages, follow the existing patterns in the codeb
currentStreamingMessageId,
sendMessage,
stopStreaming,
clearMessages,
} = useStreamingChat({
projectId,
projectName,
@@ -191,10 +190,7 @@ BASE_DOMAIN=nostrdeploy.com`);
}
};
// Clear messages when navigating between projects
useEffect(() => {
clearMessages();
}, [projectId, clearMessages]);
// Note: Message clearing and history loading is now handled in useStreamingChat hook
useEffect(() => {
if (scrollAreaRef.current && messages) {
+13 -6
View File
@@ -34,7 +34,7 @@ export function useStreamingChat({
const [isStreaming, setIsStreaming] = useState(false);
const [currentStreamingMessageId, setCurrentStreamingMessageId] = useState<string | null>(null);
const [sessionName, setSessionName] = useState<string>('');
const [isHistoryLoaded, setIsHistoryLoaded] = useState(false);
const [loadedProjectId, setLoadedProjectId] = useState<string | null>(null);
const abortControllerRef = useRef<AbortController | null>(null);
const { settings, isConfigured } = useAISettings();
const { fs } = useFS();
@@ -62,7 +62,12 @@ export function useStreamingChat({
// Load message history when project changes
useEffect(() => {
const loadMessageHistory = async () => {
if (!dotAIRef.current || isHistoryLoaded) return;
// 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('');
try {
const historyDir = `/projects/${projectId}/.ai/history`;
@@ -72,7 +77,7 @@ export function useStreamingChat({
await fs.stat(historyDir);
} catch {
// History directory doesn't exist, no history to load
setIsHistoryLoaded(true);
setLoadedProjectId(projectId);
return;
}
@@ -81,7 +86,7 @@ export function useStreamingChat({
const sessionFiles = files.filter(file => file.endsWith('.jsonl'));
if (sessionFiles.length === 0) {
setIsHistoryLoaded(true);
setLoadedProjectId(projectId);
return;
}
@@ -120,12 +125,12 @@ export function useStreamingChat({
} catch (error) {
console.warn('Failed to load message history:', error);
} finally {
setIsHistoryLoaded(true);
setLoadedProjectId(projectId);
}
};
loadMessageHistory();
}, [fs, projectId, isHistoryLoaded]);
}, [fs, projectId, loadedProjectId]);
// Save message to history
const saveMessageToHistory = useCallback(async (message: ChatMessage) => {
@@ -420,6 +425,8 @@ export function useStreamingChat({
setMessages([]);
// Start a new session when clearing messages
setSessionName(DotAI.generateSessionName());
// Reset loaded project ID so history can be loaded again
setLoadedProjectId(null);
}, []);
return {