diff --git a/src/components/SessionManagerProvider.tsx b/src/components/SessionManagerProvider.tsx index 990def33..8c45c33f 100644 --- a/src/components/SessionManagerProvider.tsx +++ b/src/components/SessionManagerProvider.tsx @@ -22,7 +22,10 @@ export function SessionManagerProvider({ children }: SessionManagerProviderProps // Cleanup on unmount useEffect(() => { return () => { - sessionManager.cleanup(); + // Fire and forget cleanup - we don't need to await in useEffect cleanup + sessionManager.cleanup().catch(error => { + console.warn('Failed to cleanup session manager:', error); + }); }; }, [sessionManager]); diff --git a/src/hooks/useProjectSessionCleanup.ts b/src/hooks/useProjectSessionCleanup.ts index 9713c4a1..2a91c238 100644 --- a/src/hooks/useProjectSessionCleanup.ts +++ b/src/hooks/useProjectSessionCleanup.ts @@ -18,8 +18,8 @@ export function useProjectSessionCleanup(projectId: string) { }, [projectId, sessionManager]); // Return a cleanup function that can be called when a project is deleted - const cleanupProjectSessions = () => { - sessionManager.deleteProjectSessions(projectId); + const cleanupProjectSessions = async () => { + await sessionManager.deleteProjectSessions(projectId); }; return { cleanupProjectSessions }; diff --git a/src/lib/DotAI.ts b/src/lib/DotAI.ts index 2f8580fa..b8bb4917 100644 --- a/src/lib/DotAI.ts +++ b/src/lib/DotAI.ts @@ -4,9 +4,9 @@ import type { JSRuntimeFS } from "./JSRuntime"; /** AI message history manager and .ai directory utilities */ export class DotAI { - private readonly fs: JSRuntimeFS; - private readonly historyDir: string; - private readonly workingDir: string; + readonly fs: JSRuntimeFS; + readonly historyDir: string; + readonly workingDir: string; constructor(fs: JSRuntimeFS, workingDir: string = "/") { this.fs = fs; diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 90679cb1..6ad340ea 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -3,9 +3,10 @@ import type { JSRuntimeFS } from './JSRuntime'; import { DotAI } from './DotAI'; import { parseProviderModel } from './parseProviderModel'; import type { Tool } from './tools/Tool'; +import { join } from '@std/path'; -// Simple debounce utility -function debounce void>(func: T, wait: number): T { +// Simple debounce utility for async functions +function debounce Promise>(func: T, wait: number): T { let timeout: NodeJS.Timeout; return ((...args: Parameters) => { clearTimeout(timeout); @@ -69,13 +70,17 @@ export class SessionManager { private readonly MAX_SESSIONS = 50; private readonly MAX_SESSION_AGE = 7 * 24 * 60 * 60 * 1000; // 7 days private debouncedPersist = debounce(this.persistSessions.bind(this), 1000); + private dotAI: DotAI; constructor(fs: JSRuntimeFS, aiSettings: { providers: Record }) { this.fs = fs; this.aiSettings = aiSettings; + this.dotAI = new DotAI(fs, '/'); // Load persisted sessions on initialization - this.loadPersistedSessions(); + this.loadPersistedSessions().catch(error => { + console.warn('Failed to load sessions during initialization:', error); + }); } /** @@ -142,7 +147,7 @@ export class SessionManager { /** * Delete a session */ - deleteSession(sessionId: string): void { + async deleteSession(sessionId: string): Promise { const session = this.sessions.get(sessionId); if (session?.abortController) { session.abortController.abort(); @@ -151,7 +156,7 @@ export class SessionManager { this.sessions.delete(sessionId); this.sessionConfigs.delete(sessionId); - this.persistSessions(); + await this.persistSessions(); this.emit('sessionDeleted', sessionId); } @@ -159,11 +164,9 @@ export class SessionManager { /** * Delete all sessions for a project */ - deleteProjectSessions(projectId: string): void { + async deleteProjectSessions(projectId: string): Promise { const projectSessions = this.getProjectSessions(projectId); - projectSessions.forEach(session => { - this.deleteSession(session.id); - }); + await Promise.all(projectSessions.map(session => this.deleteSession(session.id))); } /** @@ -579,10 +582,17 @@ export class SessionManager { } /** - * Save session states to localStorage for persistence + * Save session states using DotAI for persistence */ - private persistSessions(): void { + private async persistSessions(): Promise { try { + // Check if fs has required methods + if (!this.dotAI.fs || typeof this.dotAI.fs.writeFile !== 'function') { + console.warn('Filesystem does not support writeFile, skipping session persistence'); + return; + } + + // Create session data structure const sessionData = { sessions: Array.from(this.sessions.entries()).map(([id, session]) => ({ id, @@ -594,54 +604,71 @@ export class SessionManager { abortController: undefined } })), - configs: Array.from(this.sessionConfigs.entries()) + configs: Array.from(this.sessionConfigs.entries()), + timestamp: new Date().toISOString() }; - localStorage.setItem('shakespeare:sessions', JSON.stringify(sessionData)); + // Save session data to DotAI + const sessionsJson = JSON.stringify(sessionData, null, 2); + await this.dotAI.fs.writeFile( + join(this.dotAI.workingDir, '.ai', 'sessions.json'), + sessionsJson + ); } catch (error) { - console.warn('Failed to persist sessions:', error); + console.warn('Failed to persist sessions to DotAI:', error); } } /** - * Load persisted sessions from localStorage + * Load persisted sessions using DotAI */ - private loadPersistedSessions(): void { + private async loadPersistedSessions(): Promise { try { - const data = localStorage.getItem('shakespeare:sessions'); - if (!data) return; - - const sessionData = JSON.parse(data); - - // Restore sessions - if (sessionData.sessions) { - sessionData.sessions.forEach(({ id, session }: { id: string; session: Omit & { lastActivity: string } }) => { - // Convert lastActivity back to Date object - const restoredSession: SessionState = { - ...session, - lastActivity: new Date(session.lastActivity) - }; - this.sessions.set(id, restoredSession); - }); + // Check if fs has the required methods + if (!this.dotAI.fs || typeof this.dotAI.fs.readFile !== 'function') { + console.warn('Filesystem does not support readFile, skipping session persistence'); + return; } - // Restore configs - if (sessionData.configs) { - sessionData.configs.forEach(([id, config]: [string, SessionConfig]) => { - this.sessionConfigs.set(id, config); - }); + const sessionsPath = join(this.dotAI.workingDir, '.ai', 'sessions.json'); + + try { + const sessionsJson = await this.dotAI.fs.readFile(sessionsPath, 'utf8'); + const sessionData = JSON.parse(sessionsJson); + + // Restore sessions + if (sessionData.sessions) { + sessionData.sessions.forEach(({ id, session }: { id: string; session: Omit & { lastActivity: string } }) => { + // Convert lastActivity back to Date object + const restoredSession: SessionState = { + ...session, + lastActivity: new Date(session.lastActivity) + }; + this.sessions.set(id, restoredSession); + }); + } + + // Restore configs + if (sessionData.configs) { + sessionData.configs.forEach(([id, config]: [string, SessionConfig]) => { + this.sessionConfigs.set(id, config); + }); + } + } catch (error) { + // File doesn't exist or is invalid - that's okay, we start fresh + console.warn('No existing sessions found or invalid session data:', error); } } catch (error) { - console.warn('Failed to load persisted sessions:', error); + console.warn('Failed to load persisted sessions from DotAI:', error); } } /** * Cleanup - stop all sessions and clear data */ - cleanup(): void { + async cleanup(): Promise { // Save sessions before cleanup - this.persistSessions(); + await this.persistSessions(); // Stop all active sessions this.sessions.forEach((session, sessionId) => { diff --git a/src/pages/ProjectView.tsx b/src/pages/ProjectView.tsx index 3ff48021..7c1d9e84 100644 --- a/src/pages/ProjectView.tsx +++ b/src/pages/ProjectView.tsx @@ -193,9 +193,9 @@ BASE_DOMAIN=nostrdeploy.com`); // when isAILoading becomes true after user interaction }; - const handleProjectDeleted = () => { + const handleProjectDeleted = async () => { if (project) { - cleanupProjectSessions(); + await cleanupProjectSessions(); } setProject(null); navigate('/');