1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
| class SessionStore { private db: Database; private cache: Map<string, Session> = new Map(); constructor(dbPath: string) { this.db = new Database(dbPath); this.initSchema(); } private initSchema(): void { this.db.exec(` CREATE TABLE IF NOT EXISTS sessions ( id TEXT PRIMARY KEY, name TEXT, status TEXT, created_at TEXT, updated_at TEXT, context TEXT, config TEXT, metadata TEXT, stats TEXT ) `); this.db.exec(` CREATE TABLE IF NOT EXISTS messages ( id TEXT PRIMARY KEY, session_id TEXT, role TEXT, content TEXT, timestamp TEXT, tool_calls TEXT, tool_results TEXT, metadata TEXT, FOREIGN KEY (session_id) REFERENCES sessions(id) ) `); this.db.exec(` CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id, timestamp) `); } async create(session: Session): Promise<void> { await this.db.run(` INSERT INTO sessions ( id, name, status, created_at, updated_at, context, config, metadata, stats ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) `, [ session.id, session.name, session.status, session.createdAt.toISOString(), session.updatedAt.toISOString(), JSON.stringify(session.context), JSON.stringify(session.config), JSON.stringify(session.metadata), JSON.stringify(session.stats), ]); this.cache.set(session.id, session); } async get(sessionId: string): Promise<Session | null> { const cached = this.cache.get(sessionId); if (cached) { return cached; } const sessionRow = await this.db.get( 'SELECT * FROM sessions WHERE id = ?', [sessionId] ); if (!sessionRow) { return null; } const messages = await this.getMessages(sessionId); const session: Session = { id: sessionRow.id, name: sessionRow.name, status: sessionRow.status, createdAt: new Date(sessionRow.created_at), updatedAt: new Date(sessionRow.updated_at), context: JSON.parse(sessionRow.context), config: JSON.parse(sessionRow.config), metadata: JSON.parse(sessionRow.metadata), stats: JSON.parse(sessionRow.stats), messages, }; this.cache.set(sessionId, session); return session; } async update(session: Session): Promise<void> { session.updatedAt = new Date(); await this.db.run(` UPDATE sessions SET name = ?, status = ?, updated_at = ?, context = ?, config = ?, metadata = ?, stats = ? WHERE id = ? `, [ session.name, session.status, session.updatedAt.toISOString(), JSON.stringify(session.context), JSON.stringify(session.config), JSON.stringify(session.metadata), JSON.stringify(session.stats), session.id, ]); this.cache.set(session.id, session); } async addMessage(sessionId: string, message: Message): Promise<void> { await this.db.run(` INSERT INTO messages ( id, session_id, role, content, timestamp, tool_calls, tool_results, metadata ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) `, [ message.id, sessionId, message.role, message.content, message.timestamp.toISOString(), JSON.stringify(message.toolCalls), JSON.stringify(message.toolResults), JSON.stringify(message.metadata), ]); await this.db.run(` UPDATE sessions SET updated_at = ? WHERE id = ? `, [new Date().toISOString(), sessionId]); const session = this.cache.get(sessionId); if (session) { session.messages.push(message); session.stats.messageCount++; } } private async getMessages(sessionId: string): Promise<Message[]> { const rows = await this.db.all( 'SELECT * FROM messages WHERE session_id = ? ORDER BY timestamp ASC', [sessionId] ); return rows.map(row => ({ id: row.id, role: row.role, content: row.content, timestamp: new Date(row.timestamp), toolCalls: JSON.parse(row.tool_calls || '[]'), toolResults: JSON.parse(row.tool_results || '[]'), metadata: JSON.parse(row.metadata || '{}'), })); } async list(filter?: SessionFilter): Promise<SessionSummary[]> { let query = ` SELECT id, name, status, created_at, updated_at, stats FROM sessions `; const params: any[] = []; const conditions: string[] = []; if (filter?.status) { conditions.push('status = ?'); params.push(filter.status); } if (filter?.search) { conditions.push('name LIKE ?'); params.push(`%${filter.search}%`); } if (filter?.startTime) { conditions.push('created_at >= ?'); params.push(filter.startTime.toISOString()); } if (conditions.length > 0) { query += ' WHERE ' + conditions.join(' AND '); } query += ' ORDER BY updated_at DESC'; if (filter?.limit) { query += ` LIMIT ${filter.limit}`; } const rows = await this.db.all(query, params); return rows.map(row => ({ id: row.id, name: row.name, status: row.status, createdAt: new Date(row.created_at), updatedAt: new Date(row.updated_at), messageCount: JSON.parse(row.stats).messageCount, })); } async delete(sessionId: string): Promise<void> { await this.db.run('DELETE FROM messages WHERE session_id = ?', [sessionId]); await this.db.run('DELETE FROM sessions WHERE id = ?', [sessionId]); this.cache.delete(sessionId); } }
interface SessionSummary { id: string; name: string; status: string; createdAt: Date; updatedAt: Date; messageCount: number; }
interface SessionFilter { status?: string; search?: string; startTime?: Date; endTime?: Date; limit?: number; }
|