diff --git a/server/src/persistence/__tests__/database.test.ts b/server/src/persistence/__tests__/database.test.ts index 0b3a397..d5c1499 100644 --- a/server/src/persistence/__tests__/database.test.ts +++ b/server/src/persistence/__tests__/database.test.ts @@ -53,7 +53,7 @@ describe('database', () => { openDatabase(dbPath); const version = getSchemaVersion(); expect(version).toBe(CURRENT_SCHEMA_VERSION); - expect(version).toBe(1); + expect(version).toBe(2); }); it('returns same db on multiple getDatabase calls', () => { @@ -102,8 +102,24 @@ describe('database', () => { expect(result.journal_mode).toBe('wal'); }); - it('CURRENT_SCHEMA_VERSION is 1', () => { - expect(CURRENT_SCHEMA_VERSION).toBe(1); + it('CURRENT_SCHEMA_VERSION is 2', () => { + expect(CURRENT_SCHEMA_VERSION).toBe(2); + }); + + it('creates llm_call_log table in new databases', () => { + const dbPath = createTempDb(); + openDatabase(dbPath); + const db = getDatabase(); + const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all() as { name: string }[]; + expect(tables.map(t => t.name)).toContain('llm_call_log'); + }); + + it('creates llm_tracking_enabled metadata in new databases', () => { + const dbPath = createTempDb(); + openDatabase(dbPath); + const db = getDatabase(); + const row = db.prepare("SELECT value FROM metadata WHERE key = 'llm_tracking_enabled'").get() as { value: string }; + expect(row.value).toBe('1'); }); it('closeDatabase allows reopening', () => { diff --git a/server/src/persistence/database.ts b/server/src/persistence/database.ts index e7d8615..30acdc9 100644 --- a/server/src/persistence/database.ts +++ b/server/src/persistence/database.ts @@ -1,6 +1,6 @@ import Database from 'better-sqlite3'; -export const CURRENT_SCHEMA_VERSION = 1; +export const CURRENT_SCHEMA_VERSION = 2; let db: Database.Database | null = null; @@ -102,6 +102,21 @@ CREATE TABLE IF NOT EXISTS inventions ( CREATE INDEX IF NOT EXISTS idx_memory_events_entity ON memory_events(entity_id); CREATE INDEX IF NOT EXISTS idx_narration_events_tick ON narration_events(tick); + +CREATE TABLE IF NOT EXISTS llm_call_log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tick INTEGER, + template_name TEXT NOT NULL, + input_tokens INTEGER NOT NULL, + output_tokens INTEGER NOT NULL, + cost_usd REAL NOT NULL, + model TEXT NOT NULL, + retries INTEGER NOT NULL DEFAULT 0, + failed INTEGER NOT NULL DEFAULT 0, + timestamp TEXT NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_llm_call_log_template ON llm_call_log(template_name); `; export function openDatabase(filepath: string): void { @@ -118,6 +133,7 @@ export function openDatabase(filepath: string): void { const existing = db.prepare("SELECT value FROM metadata WHERE key = 'schema_version'").get() as { value: string } | undefined; if (!existing) { db.prepare("INSERT INTO metadata (key, value) VALUES ('schema_version', ?)").run(String(CURRENT_SCHEMA_VERSION)); + db.prepare("INSERT INTO metadata (key, value) VALUES ('llm_tracking_enabled', '1')").run(); } } diff --git a/server/src/persistence/saveManager.ts b/server/src/persistence/saveManager.ts index 6e2723d..2b9b8fd 100644 --- a/server/src/persistence/saveManager.ts +++ b/server/src/persistence/saveManager.ts @@ -28,8 +28,25 @@ export class SaveManager { openDatabase(this.filepath); const version = getSchemaVersion(); if (version < CURRENT_SCHEMA_VERSION) { - // For now, just update the version number (migrations would go here) const db = getDatabase(); + if (version < 2) { + db.exec(` + CREATE TABLE IF NOT EXISTS llm_call_log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tick INTEGER, + template_name TEXT NOT NULL, + input_tokens INTEGER NOT NULL, + output_tokens INTEGER NOT NULL, + cost_usd REAL NOT NULL, + model TEXT NOT NULL, + retries INTEGER NOT NULL DEFAULT 0, + failed INTEGER NOT NULL DEFAULT 0, + timestamp TEXT NOT NULL + ); + CREATE INDEX IF NOT EXISTS idx_llm_call_log_template ON llm_call_log(template_name); + `); + db.prepare("INSERT OR IGNORE INTO metadata (key, value) VALUES ('llm_tracking_enabled', '1')").run(); + } db.prepare( "UPDATE metadata SET value = ? WHERE key = 'schema_version'", ).run(String(CURRENT_SCHEMA_VERSION));