mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
local storage fix
This commit is contained in:
1 parent
e59dc1b199
commit
93a1d8c620
3 files changed
+113
-240
No files matched your search
@@ -1,26 +0,0 @@
|
|||||||
import { useEffect } from 'react';
|
|
||||||
import { useSessionManager } from '@/hooks/useSessionManager';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hook to clean up sessions when projects are deleted or components unmount
|
|
||||||
*/
|
|
||||||
export function useProjectSessionCleanup(projectId: string) {
|
|
||||||
const sessionManager = useSessionManager();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
// Cleanup function that runs when the project component unmounts
|
|
||||||
// or when the projectId changes
|
|
||||||
return () => {
|
|
||||||
// Note: We don't auto-cleanup sessions when navigating away from a project
|
|
||||||
// since we want sessions to persist in the background. Sessions are only
|
|
||||||
// cleaned up when explicitly deleted or when the project is deleted.
|
|
||||||
};
|
|
||||||
}, [projectId, sessionManager]);
|
|
||||||
|
|
||||||
// Return a cleanup function that can be called when a project is deleted
|
|
||||||
const cleanupProjectSessions = async () => {
|
|
||||||
await sessionManager.deleteProjectSessions(projectId);
|
|
||||||
};
|
|
||||||
|
|
||||||
return { cleanupProjectSessions };
|
|
||||||
}
|
|
||||||
+111
-209
@@ -5,14 +5,17 @@ import { parseProviderModel } from './parseProviderModel';
|
|||||||
import type { Tool } from './tools/Tool';
|
import type { Tool } from './tools/Tool';
|
||||||
import { join } from '@std/path';
|
import { join } from '@std/path';
|
||||||
|
|
||||||
// Simple debounce utility for async functions
|
// Simple debounce utility
|
||||||
function debounce<T extends (...args: unknown[]) => Promise<void>>(func: T, wait: number): T {
|
const debounce = <T extends (...args: unknown[]) => void>(
|
||||||
|
func: T,
|
||||||
|
wait: number
|
||||||
|
): ((...args: Parameters<T>) => void) => {
|
||||||
let timeout: NodeJS.Timeout;
|
let timeout: NodeJS.Timeout;
|
||||||
return ((...args: Parameters<T>) => {
|
return (...args: Parameters<T>) => {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
timeout = setTimeout(() => func(...args), wait);
|
timeout = setTimeout(() => func(...args), wait);
|
||||||
}) as T;
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
|
export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
|
||||||
|
|
||||||
@@ -51,7 +54,7 @@ export interface SessionManagerEvents {
|
|||||||
loadingChanged: (sessionId: string, isLoading: boolean) => void;
|
loadingChanged: (sessionId: string, isLoading: boolean) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventListener<T extends unknown[]> = (...args: T) => void;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global session manager that handles multiple AI chat sessions running concurrently.
|
* Global session manager that handles multiple AI chat sessions running concurrently.
|
||||||
@@ -60,16 +63,14 @@ type EventListener<T extends unknown[]> = (...args: T) => void;
|
|||||||
export class SessionManager {
|
export class SessionManager {
|
||||||
private sessions = new Map<string, SessionState>();
|
private sessions = new Map<string, SessionState>();
|
||||||
private sessionConfigs = new Map<string, SessionConfig>();
|
private sessionConfigs = new Map<string, SessionConfig>();
|
||||||
private listeners = new Map<keyof SessionManagerEvents, Set<EventListener<unknown[]>>>();
|
private listeners: Partial<Record<keyof SessionManagerEvents, Set<(...args: unknown[]) => void>>> = {};
|
||||||
private fs: JSRuntimeFS;
|
private fs: JSRuntimeFS;
|
||||||
private aiSettings: {
|
private aiSettings: { providers: Record<string, { baseURL: string; apiKey?: string }> };
|
||||||
providers: Record<string, { baseURL: string; apiKey?: string }>;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Performance and memory management
|
// Performance constants
|
||||||
private readonly MAX_SESSIONS = 50;
|
private readonly MAX_SESSIONS = 50;
|
||||||
private readonly MAX_SESSION_AGE = 7 * 24 * 60 * 60 * 1000; // 7 days
|
private readonly MAX_SESSION_AGE = 7 * 24 * 60 * 60 * 1000; // 7 days
|
||||||
private debouncedPersist = debounce(this.persistSessions.bind(this), 1000);
|
private debouncedPersist = debounce(() => this.persistSessions(), 1000);
|
||||||
private dotAI: DotAI;
|
private dotAI: DotAI;
|
||||||
|
|
||||||
constructor(fs: JSRuntimeFS, aiSettings: { providers: Record<string, { baseURL: string; apiKey?: string }> }) {
|
constructor(fs: JSRuntimeFS, aiSettings: { providers: Record<string, { baseURL: string; apiKey?: string }> }) {
|
||||||
@@ -97,11 +98,10 @@ export class SessionManager {
|
|||||||
lastActivity: new Date(),
|
lastActivity: new Date(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Try to load existing history for this project
|
// Try to load existing history
|
||||||
try {
|
try {
|
||||||
const dotAI = new DotAI(this.fs, `/projects/${config.projectId}`);
|
const dotAI = new DotAI(this.fs, `/projects/${config.projectId}`);
|
||||||
const lastSession = await dotAI.readLastSessionHistory();
|
const lastSession = await dotAI.readLastSessionHistory();
|
||||||
|
|
||||||
if (lastSession) {
|
if (lastSession) {
|
||||||
sessionState.messages = lastSession.messages;
|
sessionState.messages = lastSession.messages;
|
||||||
sessionState.sessionName = lastSession.sessionName;
|
sessionState.sessionName = lastSession.sessionName;
|
||||||
@@ -114,7 +114,6 @@ export class SessionManager {
|
|||||||
this.sessionConfigs.set(config.id, config);
|
this.sessionConfigs.set(config.id, config);
|
||||||
|
|
||||||
this.persistSessions();
|
this.persistSessions();
|
||||||
|
|
||||||
this.emit('sessionCreated', config.id);
|
this.emit('sessionCreated', config.id);
|
||||||
this.emit('sessionUpdated', config.id, sessionState);
|
this.emit('sessionUpdated', config.id, sessionState);
|
||||||
|
|
||||||
@@ -149,15 +148,12 @@ export class SessionManager {
|
|||||||
*/
|
*/
|
||||||
async deleteSession(sessionId: string): Promise<void> {
|
async deleteSession(sessionId: string): Promise<void> {
|
||||||
const session = this.sessions.get(sessionId);
|
const session = this.sessions.get(sessionId);
|
||||||
if (session?.abortController) {
|
session?.abortController?.abort();
|
||||||
session.abortController.abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.sessions.delete(sessionId);
|
this.sessions.delete(sessionId);
|
||||||
this.sessionConfigs.delete(sessionId);
|
this.sessionConfigs.delete(sessionId);
|
||||||
|
|
||||||
await this.persistSessions();
|
await this.persistSessions();
|
||||||
|
|
||||||
this.emit('sessionDeleted', sessionId);
|
this.emit('sessionDeleted', sessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,8 +161,9 @@ export class SessionManager {
|
|||||||
* Delete all sessions for a project
|
* Delete all sessions for a project
|
||||||
*/
|
*/
|
||||||
async deleteProjectSessions(projectId: string): Promise<void> {
|
async deleteProjectSessions(projectId: string): Promise<void> {
|
||||||
const projectSessions = this.getProjectSessions(projectId);
|
await Promise.all(
|
||||||
await Promise.all(projectSessions.map(session => this.deleteSession(session.id)));
|
this.getProjectSessions(projectId).map(session => this.deleteSession(session.id))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -179,17 +176,12 @@ export class SessionManager {
|
|||||||
session.messages.push(message);
|
session.messages.push(message);
|
||||||
session.lastActivity = new Date();
|
session.lastActivity = new Date();
|
||||||
|
|
||||||
// Save to history
|
|
||||||
await this.saveSessionHistory(sessionId);
|
await this.saveSessionHistory(sessionId);
|
||||||
|
|
||||||
// Use debounced persistence for performance
|
|
||||||
this.debouncedPersist();
|
this.debouncedPersist();
|
||||||
|
this.cleanupOldSessions();
|
||||||
|
|
||||||
this.emit('messageAdded', sessionId, message);
|
this.emit('messageAdded', sessionId, message);
|
||||||
this.emit('sessionUpdated', sessionId, session);
|
this.emit('sessionUpdated', sessionId, session);
|
||||||
|
|
||||||
// Clean up old sessions periodically
|
|
||||||
this.cleanupOldSessions();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -201,15 +193,7 @@ export class SessionManager {
|
|||||||
|
|
||||||
if (!session || !config || session.isLoading) return;
|
if (!session || !config || session.isLoading) return;
|
||||||
|
|
||||||
// Add user message
|
await this.addMessage(sessionId, { role: 'user', content });
|
||||||
const userMessage: AIMessage = {
|
|
||||||
role: 'user',
|
|
||||||
content
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.addMessage(sessionId, userMessage);
|
|
||||||
|
|
||||||
// Start AI generation
|
|
||||||
await this.startGeneration(sessionId, providerModel);
|
await this.startGeneration(sessionId, providerModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,33 +293,20 @@ export class SessionManager {
|
|||||||
if (delta?.tool_calls) {
|
if (delta?.tool_calls) {
|
||||||
for (const toolCallDelta of delta.tool_calls) {
|
for (const toolCallDelta of delta.tool_calls) {
|
||||||
const index = toolCallDelta.index;
|
const index = toolCallDelta.index;
|
||||||
if (index !== undefined) {
|
if (index === undefined) continue;
|
||||||
// Initialize tool call if it doesn't exist
|
|
||||||
if (!accumulatedToolCalls[index]) {
|
|
||||||
accumulatedToolCalls[index] = {
|
|
||||||
id: toolCallDelta.id || '',
|
|
||||||
type: 'function',
|
|
||||||
function: {
|
|
||||||
name: '',
|
|
||||||
arguments: ''
|
|
||||||
}
|
|
||||||
} as OpenAI.Chat.Completions.ChatCompletionMessageFunctionToolCall;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update tool call properties
|
accumulatedToolCalls[index] ??= {
|
||||||
if (toolCallDelta.id) {
|
id: '',
|
||||||
accumulatedToolCalls[index].id = toolCallDelta.id;
|
type: 'function',
|
||||||
}
|
function: { name: '', arguments: '' }
|
||||||
if (toolCallDelta.function?.name && accumulatedToolCalls[index].type === 'function') {
|
};
|
||||||
(accumulatedToolCalls[index] as OpenAI.Chat.Completions.ChatCompletionMessageFunctionToolCall).function.name = toolCallDelta.function.name;
|
|
||||||
}
|
const toolCall = accumulatedToolCalls[index] as OpenAI.Chat.Completions.ChatCompletionMessageFunctionToolCall;
|
||||||
if (toolCallDelta.function?.arguments && accumulatedToolCalls[index].type === 'function') {
|
if (toolCallDelta.id) toolCall.id = toolCallDelta.id;
|
||||||
(accumulatedToolCalls[index] as OpenAI.Chat.Completions.ChatCompletionMessageFunctionToolCall).function.arguments += toolCallDelta.function.arguments;
|
if (toolCallDelta.function?.name) toolCall.function.name = toolCallDelta.function.name;
|
||||||
}
|
if (toolCallDelta.function?.arguments) toolCall.function.arguments += toolCallDelta.function.arguments;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update streaming message with tool calls
|
|
||||||
if (session.streamingMessage) {
|
if (session.streamingMessage) {
|
||||||
session.streamingMessage.tool_calls = accumulatedToolCalls.length > 0 ? accumulatedToolCalls : undefined;
|
session.streamingMessage.tool_calls = accumulatedToolCalls.length > 0 ? accumulatedToolCalls : undefined;
|
||||||
this.emit('streamingUpdate', sessionId, session.streamingMessage.content, session.streamingMessage.tool_calls);
|
this.emit('streamingUpdate', sessionId, session.streamingMessage.content, session.streamingMessage.tool_calls);
|
||||||
@@ -366,7 +337,7 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle tool calls
|
// Handle tool calls
|
||||||
if (accumulatedToolCalls && accumulatedToolCalls.length > 0) {
|
if (accumulatedToolCalls?.length) {
|
||||||
for (const toolCall of accumulatedToolCalls) {
|
for (const toolCall of accumulatedToolCalls) {
|
||||||
if (toolCall.type !== 'function') continue;
|
if (toolCall.type !== 'function') continue;
|
||||||
|
|
||||||
@@ -375,54 +346,17 @@ export class SessionManager {
|
|||||||
const tool = config.customTools[toolName];
|
const tool = config.customTools[toolName];
|
||||||
|
|
||||||
if (!tool) {
|
if (!tool) {
|
||||||
const toolNotFoundMessage: AIMessage = {
|
await this.addToolMessage(sessionId, conversationMessages, functionToolCall.id, `Tool ${toolName} not found`);
|
||||||
role: 'tool',
|
|
||||||
content: `Tool ${toolName} not found`,
|
|
||||||
tool_call_id: functionToolCall.id
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.addMessage(sessionId, toolNotFoundMessage);
|
|
||||||
conversationMessages.push(toolNotFoundMessage);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let toolArgs: unknown;
|
|
||||||
try {
|
|
||||||
const json = JSON.parse(functionToolCall.function.arguments);
|
|
||||||
toolArgs = tool.inputSchema.parse(json);
|
|
||||||
} catch (parseError) {
|
|
||||||
const toolErrorMessage: AIMessage = {
|
|
||||||
role: 'tool',
|
|
||||||
content: `Error parsing arguments for tool ${toolName}: ${parseError instanceof Error ? parseError.message : 'Unknown error'}`,
|
|
||||||
tool_call_id: functionToolCall.id
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.addMessage(sessionId, toolErrorMessage);
|
|
||||||
conversationMessages.push(toolErrorMessage);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Execute the tool
|
const toolArgs = tool.inputSchema.parse(JSON.parse(functionToolCall.function.arguments));
|
||||||
const content = await tool.execute(toolArgs);
|
const content = await tool.execute(toolArgs);
|
||||||
|
await this.addToolMessage(sessionId, conversationMessages, functionToolCall.id, content);
|
||||||
const toolResultMessage: AIMessage = {
|
} catch (error) {
|
||||||
role: 'tool',
|
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
|
||||||
content,
|
await this.addToolMessage(sessionId, conversationMessages, functionToolCall.id, `Error with tool ${toolName}: ${errorMsg}`);
|
||||||
tool_call_id: functionToolCall.id
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.addMessage(sessionId, toolResultMessage);
|
|
||||||
conversationMessages.push(toolResultMessage);
|
|
||||||
} catch (toolError) {
|
|
||||||
const toolErrorMessage: AIMessage = {
|
|
||||||
role: 'tool',
|
|
||||||
content: `Error executing tool ${toolName}: ${toolError instanceof Error ? toolError.message : 'Unknown error'}`,
|
|
||||||
tool_call_id: functionToolCall.id
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.addMessage(sessionId, toolErrorMessage);
|
|
||||||
conversationMessages.push(toolErrorMessage);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -431,32 +365,23 @@ export class SessionManager {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('AI generation error:', error);
|
console.error('AI generation error:', error);
|
||||||
|
|
||||||
// Handle different types of errors appropriately
|
// Handle different error types
|
||||||
let errorMessage: string;
|
if (error?.name === 'AbortError') return; // User cancelled
|
||||||
if (error && typeof error === 'object' && 'name' in error) {
|
|
||||||
if (error.name === 'AbortError') {
|
let errorMessage = 'Sorry, I encountered an unexpected error. Please try again.';
|
||||||
// User cancelled the request - don't add error message
|
const errorMsg = error?.message || '';
|
||||||
return;
|
|
||||||
} else if (error.name === 'TypeError' && error.message.includes('fetch')) {
|
if (error?.name === 'TypeError' && errorMsg.includes('fetch')) {
|
||||||
errorMessage = 'Network error: Unable to connect to AI service. Please check your internet connection and AI settings.';
|
errorMessage = 'Network error: Unable to connect to AI service. Please check your internet connection and AI settings.';
|
||||||
} else if (error.message?.includes('API key')) {
|
} else if (errorMsg.includes('API key')) {
|
||||||
errorMessage = 'Authentication error: Please check your API key in AI settings.';
|
errorMessage = 'Authentication error: Please check your API key in AI settings.';
|
||||||
} else if (error.message?.includes('rate limit')) {
|
} else if (errorMsg.includes('rate limit')) {
|
||||||
errorMessage = 'Rate limit exceeded. Please wait a moment before trying again.';
|
errorMessage = 'Rate limit exceeded. Please wait a moment before trying again.';
|
||||||
} else {
|
} else if (errorMsg) {
|
||||||
errorMessage = `AI service error: ${error.message || 'Unknown error occurred'}`;
|
errorMessage = `AI service error: ${errorMsg}`;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
errorMessage = 'Sorry, I encountered an unexpected error. Please try again.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add user-friendly error message
|
await this.addMessage(sessionId, { role: 'assistant', content: errorMessage });
|
||||||
const errorAIMessage: AIMessage = {
|
|
||||||
role: 'assistant',
|
|
||||||
content: errorMessage
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.addMessage(sessionId, errorAIMessage);
|
|
||||||
} finally {
|
} finally {
|
||||||
session.isLoading = false;
|
session.isLoading = false;
|
||||||
session.streamingMessage = undefined;
|
session.streamingMessage = undefined;
|
||||||
@@ -474,10 +399,7 @@ export class SessionManager {
|
|||||||
const session = this.sessions.get(sessionId);
|
const session = this.sessions.get(sessionId);
|
||||||
if (!session) return;
|
if (!session) return;
|
||||||
|
|
||||||
if (session.abortController) {
|
session.abortController?.abort();
|
||||||
session.abortController.abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
session.isLoading = false;
|
session.isLoading = false;
|
||||||
session.streamingMessage = undefined;
|
session.streamingMessage = undefined;
|
||||||
session.abortController = undefined;
|
session.abortController = undefined;
|
||||||
@@ -491,27 +413,20 @@ export class SessionManager {
|
|||||||
*/
|
*/
|
||||||
private cleanupOldSessions(): void {
|
private cleanupOldSessions(): void {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const sessionIdsToDelete: string[] = [];
|
|
||||||
|
|
||||||
|
// Delete old sessions
|
||||||
for (const [id, session] of this.sessions) {
|
for (const [id, session] of this.sessions) {
|
||||||
if (now - session.lastActivity.getTime() > this.MAX_SESSION_AGE) {
|
if (now - session.lastActivity.getTime() > this.MAX_SESSION_AGE) {
|
||||||
sessionIdsToDelete.push(id);
|
this.deleteSession(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete old sessions
|
// Delete oldest sessions if we have too many
|
||||||
sessionIdsToDelete.forEach(id => this.deleteSession(id));
|
|
||||||
|
|
||||||
// If we still have too many sessions, delete the oldest ones
|
|
||||||
if (this.sessions.size > this.MAX_SESSIONS) {
|
if (this.sessions.size > this.MAX_SESSIONS) {
|
||||||
const sortedSessions = Array.from(this.sessions.entries())
|
Array.from(this.sessions.entries())
|
||||||
.sort(([, a], [, b]) => a.lastActivity.getTime() - b.lastActivity.getTime());
|
.sort(([, a], [, b]) => a.lastActivity.getTime() - b.lastActivity.getTime())
|
||||||
|
|
||||||
const sessionsToDelete = sortedSessions
|
|
||||||
.slice(0, this.sessions.size - this.MAX_SESSIONS)
|
.slice(0, this.sessions.size - this.MAX_SESSIONS)
|
||||||
.map(([id]) => id);
|
.forEach(([id]) => this.deleteSession(id));
|
||||||
|
|
||||||
sessionsToDelete.forEach(id => this.deleteSession(id));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -522,20 +437,31 @@ export class SessionManager {
|
|||||||
const session = this.sessions.get(sessionId);
|
const session = this.sessions.get(sessionId);
|
||||||
if (!session) return;
|
if (!session) return;
|
||||||
|
|
||||||
// Stop any ongoing generation
|
|
||||||
this.stopGeneration(sessionId);
|
this.stopGeneration(sessionId);
|
||||||
|
|
||||||
// Clear messages and start fresh
|
|
||||||
session.messages = [];
|
session.messages = [];
|
||||||
session.streamingMessage = undefined;
|
session.streamingMessage = undefined;
|
||||||
session.sessionName = DotAI.generateSessionName();
|
session.sessionName = DotAI.generateSessionName();
|
||||||
session.lastActivity = new Date();
|
session.lastActivity = new Date();
|
||||||
|
|
||||||
this.debouncedPersist();
|
this.debouncedPersist();
|
||||||
|
|
||||||
this.emit('sessionUpdated', sessionId, session);
|
this.emit('sessionUpdated', sessionId, session);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper to add tool messages and update conversation
|
||||||
|
*/
|
||||||
|
private async addToolMessage(sessionId: string, conversationMessages: AIMessage[], toolCallId: string, content: string): Promise<void> {
|
||||||
|
const toolMessage: AIMessage = {
|
||||||
|
role: 'tool',
|
||||||
|
content,
|
||||||
|
tool_call_id: toolCallId
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.addMessage(sessionId, toolMessage);
|
||||||
|
conversationMessages.push(toolMessage);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save session history to file
|
* Save session history to file
|
||||||
*/
|
*/
|
||||||
@@ -552,33 +478,25 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event listener management
|
* Simplified event system
|
||||||
*/
|
*/
|
||||||
on<K extends keyof SessionManagerEvents>(event: K, listener: SessionManagerEvents[K]): void {
|
on<K extends keyof SessionManagerEvents>(event: K, listener: SessionManagerEvents[K]): void {
|
||||||
if (!this.listeners.has(event)) {
|
if (!this.listeners[event]) this.listeners[event] = new Set();
|
||||||
this.listeners.set(event, new Set());
|
this.listeners[event]!.add(listener as (...args: unknown[]) => void);
|
||||||
}
|
|
||||||
this.listeners.get(event)!.add(listener as EventListener<unknown[]>);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
off<K extends keyof SessionManagerEvents>(event: K, listener: SessionManagerEvents[K]): void {
|
off<K extends keyof SessionManagerEvents>(event: K, listener: SessionManagerEvents[K]): void {
|
||||||
const eventListeners = this.listeners.get(event);
|
this.listeners[event]?.delete(listener as (...args: unknown[]) => void);
|
||||||
if (eventListeners) {
|
|
||||||
eventListeners.delete(listener as EventListener<unknown[]>);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
emit<K extends keyof SessionManagerEvents>(event: K, ...args: Parameters<SessionManagerEvents[K]>): void {
|
emit<K extends keyof SessionManagerEvents>(event: K, ...args: Parameters<SessionManagerEvents[K]>): void {
|
||||||
const eventListeners = this.listeners.get(event);
|
this.listeners[event]?.forEach(listener => {
|
||||||
if (eventListeners) {
|
try {
|
||||||
eventListeners.forEach(listener => {
|
listener(...args);
|
||||||
try {
|
} catch (error) {
|
||||||
(listener as (...args: Parameters<SessionManagerEvents[K]>) => void)(...args);
|
console.error('Event listener error:', error);
|
||||||
} catch (error) {
|
}
|
||||||
console.error('Error in session manager event listener:', error);
|
});
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -586,19 +504,13 @@ export class SessionManager {
|
|||||||
*/
|
*/
|
||||||
private async persistSessions(): Promise<void> {
|
private async persistSessions(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
// Check if fs has required methods
|
if (!this.dotAI.fs?.writeFile) return;
|
||||||
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 = {
|
const sessionData = {
|
||||||
sessions: Array.from(this.sessions.entries()).map(([id, session]) => ({
|
sessions: Array.from(this.sessions.entries()).map(([id, session]) => ({
|
||||||
id,
|
id,
|
||||||
session: {
|
session: {
|
||||||
...session,
|
...session,
|
||||||
// Don't persist runtime state
|
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
streamingMessage: undefined,
|
streamingMessage: undefined,
|
||||||
abortController: undefined
|
abortController: undefined
|
||||||
@@ -608,14 +520,20 @@ export class SessionManager {
|
|||||||
timestamp: new Date().toISOString()
|
timestamp: new Date().toISOString()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Save session data to DotAI
|
// Ensure .ai directory exists before writing
|
||||||
const sessionsJson = JSON.stringify(sessionData, null, 2);
|
const aiDir = join(this.dotAI.workingDir, '.ai');
|
||||||
|
try {
|
||||||
|
await this.dotAI.fs.mkdir(aiDir, { recursive: true });
|
||||||
|
} catch {
|
||||||
|
// Directory might already exist, continue
|
||||||
|
}
|
||||||
|
|
||||||
await this.dotAI.fs.writeFile(
|
await this.dotAI.fs.writeFile(
|
||||||
join(this.dotAI.workingDir, '.ai', 'sessions.json'),
|
join(aiDir, 'sessions.json'),
|
||||||
sessionsJson
|
JSON.stringify(sessionData)
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('Failed to persist sessions to DotAI:', error);
|
console.warn('Failed to persist sessions:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -624,11 +542,7 @@ export class SessionManager {
|
|||||||
*/
|
*/
|
||||||
private async loadPersistedSessions(): Promise<void> {
|
private async loadPersistedSessions(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
// Check if fs has the required methods
|
if (!this.dotAI.fs?.readFile) return;
|
||||||
if (!this.dotAI.fs || typeof this.dotAI.fs.readFile !== 'function') {
|
|
||||||
console.warn('Filesystem does not support readFile, skipping session persistence');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const sessionsPath = join(this.dotAI.workingDir, '.ai', 'sessions.json');
|
const sessionsPath = join(this.dotAI.workingDir, '.ai', 'sessions.json');
|
||||||
|
|
||||||
@@ -637,29 +551,22 @@ export class SessionManager {
|
|||||||
const sessionData = JSON.parse(sessionsJson);
|
const sessionData = JSON.parse(sessionsJson);
|
||||||
|
|
||||||
// Restore sessions
|
// Restore sessions
|
||||||
if (sessionData.sessions) {
|
sessionData.sessions?.forEach(({ id, session }: { id: string; session: Omit<SessionState, 'lastActivity'> & { lastActivity: string } }) => {
|
||||||
sessionData.sessions.forEach(({ id, session }: { id: string; session: Omit<SessionState, 'lastActivity'> & { lastActivity: string } }) => {
|
this.sessions.set(id, {
|
||||||
// Convert lastActivity back to Date object
|
...session,
|
||||||
const restoredSession: SessionState = {
|
lastActivity: new Date(session.lastActivity)
|
||||||
...session,
|
|
||||||
lastActivity: new Date(session.lastActivity)
|
|
||||||
};
|
|
||||||
this.sessions.set(id, restoredSession);
|
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
// Restore configs
|
// Restore configs
|
||||||
if (sessionData.configs) {
|
sessionData.configs?.forEach(([id, config]: [string, SessionConfig]) => {
|
||||||
sessionData.configs.forEach(([id, config]: [string, SessionConfig]) => {
|
this.sessionConfigs.set(id, config);
|
||||||
this.sessionConfigs.set(id, config);
|
});
|
||||||
});
|
} catch {
|
||||||
}
|
// File doesn't exist or is invalid - start fresh
|
||||||
} 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) {
|
} catch (error) {
|
||||||
console.warn('Failed to load persisted sessions from DotAI:', error);
|
console.warn('Failed to load sessions:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -667,17 +574,12 @@ export class SessionManager {
|
|||||||
* Cleanup - stop all sessions and clear data
|
* Cleanup - stop all sessions and clear data
|
||||||
*/
|
*/
|
||||||
async cleanup(): Promise<void> {
|
async cleanup(): Promise<void> {
|
||||||
// Save sessions before cleanup
|
|
||||||
await this.persistSessions();
|
await this.persistSessions();
|
||||||
|
|
||||||
// Stop all active sessions
|
this.sessions.forEach((_, sessionId) => this.stopGeneration(sessionId));
|
||||||
this.sessions.forEach((session, sessionId) => {
|
|
||||||
this.stopGeneration(sessionId);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Clear all data
|
|
||||||
this.sessions.clear();
|
this.sessions.clear();
|
||||||
this.sessionConfigs.clear();
|
this.sessionConfigs.clear();
|
||||||
this.listeners.clear();
|
this.listeners = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@ import { useKeepAlive } from '@/hooks/useKeepAlive';
|
|||||||
import { GitStatusIndicator } from '@/components/GitStatusIndicator';
|
import { GitStatusIndicator } from '@/components/GitStatusIndicator';
|
||||||
import { StarButton } from '@/components/StarButton';
|
import { StarButton } from '@/components/StarButton';
|
||||||
|
|
||||||
import { useProjectSessionCleanup } from '@/hooks/useProjectSessionCleanup';
|
|
||||||
import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';
|
import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';
|
||||||
import { bytesToHex } from 'nostr-tools/utils';
|
import { bytesToHex } from 'nostr-tools/utils';
|
||||||
import { buildProject } from "@/lib/build";
|
import { buildProject } from "@/lib/build";
|
||||||
@@ -63,7 +63,7 @@ export function ProjectView() {
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
const { cleanupProjectSessions } = useProjectSessionCleanup(project?.id || '');
|
|
||||||
|
|
||||||
const loadProject = useCallback(async () => {
|
const loadProject = useCallback(async () => {
|
||||||
if (!projectId) return;
|
if (!projectId) return;
|
||||||
@@ -194,9 +194,6 @@ BASE_DOMAIN=nostrdeploy.com`);
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleProjectDeleted = async () => {
|
const handleProjectDeleted = async () => {
|
||||||
if (project) {
|
|
||||||
await cleanupProjectSessions();
|
|
||||||
}
|
|
||||||
setProject(null);
|
setProject(null);
|
||||||
navigate('/');
|
navigate('/');
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user