feat: add SQLite session persistence

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
William Valentin
2026-02-03 00:27:12 -08:00
parent bb16732562
commit 10c848ca2a
3 changed files with 123 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
import Database from 'better-sqlite3';
import type { Message } from '../models/types.js';
export class SessionStore {
private db: Database.Database;
constructor(dbPath: string) {
this.db = new Database(dbPath);
this.init();
}
private init(): void {
this.db.exec(`
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL,
created_at INTEGER NOT NULL DEFAULT (unixepoch())
);
CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id);
`);
}
addMessage(sessionId: string, message: Message): void {
const stmt = this.db.prepare(
'INSERT INTO messages (session_id, role, content) VALUES (?, ?, ?)'
);
stmt.run(sessionId, message.role, message.content);
}
getMessages(sessionId: string): Message[] {
const stmt = this.db.prepare(
'SELECT role, content FROM messages WHERE session_id = ? ORDER BY id ASC'
);
const rows = stmt.all(sessionId) as Array<{ role: string; content: string }>;
return rows.map(row => ({
role: row.role as 'user' | 'assistant',
content: row.content,
}));
}
clearSession(sessionId: string): void {
const stmt = this.db.prepare('DELETE FROM messages WHERE session_id = ?');
stmt.run(sessionId);
}
listSessions(): string[] {
const stmt = this.db.prepare('SELECT DISTINCT session_id FROM messages');
const rows = stmt.all() as Array<{ session_id: string }>;
return rows.map(row => row.session_id);
}
close(): void {
this.db.close();
}
}