| import { ChatSession, Message } from '@/types/chat' | |
| const STORAGE_KEYS = { | |
| sessions: 'edge-llm-sessions', | |
| currentSession: 'edge-llm-current-session' | |
| } | |
| function generateId(): string { | |
| return Math.random().toString(36).substring(2) + Date.now().toString(36) | |
| } | |
| function generateMessageId(): string { | |
| return generateId() | |
| } | |
| class ChatStorageManager { | |
| getAllSessions(): ChatSession[] { | |
| const stored = localStorage.getItem(STORAGE_KEYS.sessions) | |
| return stored ? JSON.parse(stored) : [] | |
| } | |
| getSession(sessionId: string): ChatSession | null { | |
| const sessions = this.getAllSessions() | |
| return sessions.find(s => s.id === sessionId) || null | |
| } | |
| createSession(title?: string, model?: string, systemPrompt?: string): ChatSession { | |
| const newSession: ChatSession = { | |
| id: generateId(), | |
| title: title || `New Chat ${new Date().toLocaleString()}`, | |
| messages: [], | |
| model, | |
| systemPrompt, | |
| createdAt: Date.now(), | |
| updatedAt: Date.now() | |
| } | |
| const sessions = this.getAllSessions() | |
| sessions.unshift(newSession) | |
| localStorage.setItem(STORAGE_KEYS.sessions, JSON.stringify(sessions)) | |
| return newSession | |
| } | |
| updateSession(sessionId: string, updates: Partial<ChatSession>): void { | |
| const sessions = this.getAllSessions() | |
| const sessionIndex = sessions.findIndex(s => s.id === sessionId) | |
| if (sessionIndex !== -1) { | |
| sessions[sessionIndex] = { | |
| ...sessions[sessionIndex], | |
| ...updates, | |
| updatedAt: Date.now() | |
| } | |
| localStorage.setItem(STORAGE_KEYS.sessions, JSON.stringify(sessions)) | |
| } | |
| } | |
| deleteSession(sessionId: string): void { | |
| const sessions = this.getAllSessions() | |
| const filtered = sessions.filter(s => s.id !== sessionId) | |
| localStorage.setItem(STORAGE_KEYS.sessions, JSON.stringify(filtered)) | |
| if (this.getCurrentSessionId() === sessionId) { | |
| localStorage.removeItem(STORAGE_KEYS.currentSession) | |
| } | |
| } | |
| addMessageToSession(sessionId: string, message: Omit<Message, 'id' | 'timestamp'>): void { | |
| const sessions = this.getAllSessions() | |
| const sessionIndex = sessions.findIndex(s => s.id === sessionId) | |
| if (sessionIndex !== -1) { | |
| const newMessage: Message = { | |
| ...message, | |
| id: generateMessageId(), | |
| timestamp: Date.now() | |
| } | |
| sessions[sessionIndex].messages.push(newMessage) | |
| sessions[sessionIndex].updatedAt = Date.now() | |
| localStorage.setItem(STORAGE_KEYS.sessions, JSON.stringify(sessions)) | |
| } | |
| } | |
| getCurrentSessionId(): string | null { | |
| return localStorage.getItem(STORAGE_KEYS.currentSession) | |
| } | |
| getCurrentSession(): ChatSession | null { | |
| const currentId = this.getCurrentSessionId() | |
| return currentId ? this.getSession(currentId) : null | |
| } | |
| setCurrentSession(sessionId: string): void { | |
| localStorage.setItem(STORAGE_KEYS.currentSession, sessionId) | |
| } | |
| clear(): void { | |
| localStorage.removeItem(STORAGE_KEYS.sessions) | |
| localStorage.removeItem(STORAGE_KEYS.currentSession) | |
| } | |
| } | |
| export const chatStorage = new ChatStorageManager() | |