refactor(db): unify SQLite table schema management and initialization (#310)
Centralized all 11 Hermes SQLite table definitions and initialization logic into a single schemas.ts file to eliminate duplication and improve maintainability. Changes: - **NEW**: packages/server/src/db/hermes/schemas.ts - Centralized schema definitions for all 11 tables - Unified initAllHermesTables() function with migration logic - Includes usage table PRIMARY KEY migration (session_id → id) - **Refactored**: packages/server/src/db/hermes/init.ts - Simplified from async to sync (all operations are synchronous) - Single responsibility: delegate to schemas.ts - **Refactored**: packages/server/src/db/hermes/session-store.ts - Removed schema definitions (now in schemas.ts) - Removed initSessionStore() function - Imports table constants from schemas.ts - **Refactored**: packages/server/src/db/hermes/usage-store.ts - Removed initUsageStore() function and migration logic - Migration moved to schemas.ts for consistency - Only handles CRUD operations now - **Refactored**: packages/server/src/db/hermes/compression-snapshot.ts - Removed initCompressionSnapshotStore() function - Fixed duplicate getCompressionSnapshot definition - Imports table constant from schemas.ts - **Refactored**: packages/server/src/services/hermes/group-chat/index.ts - Removed ensureTable() calls (now in schemas.ts) - Only handles index creation now - Imports table constants from schemas.ts - **Updated**: packages/server/src/index.ts - Removed await from initAllStores() call (now sync) Benefits: - 🎯 Single responsibility: schemas.ts manages all tables, stores only do CRUD - 📋 Centralized maintenance: all table definitions in one place - 🔄 No duplication: each table created exactly once with proper migrations - 🚀 Clean architecture: clear separation between initialization and operations Tables managed (11 total): 1. session_usage (usage statistics) 2. sessions (session metadata) 3. messages (message content) 4. chat_compression_snapshots (compression snapshots) 5. gc_rooms (group chat rooms) 6. gc_messages (group chat messages) 7. gc_room_agents (room agents) 8. gc_context_snapshots (group chat snapshots) 9. gc_room_members (room members) 10. gc_pending_session_deletes (pending session deletes) 11. gc_session_profiles (session profiles) Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
7053212d48
commit
6511033ed8
@@ -2,7 +2,8 @@
|
||||
* Self-built session database — completely replaces Hermes CLI dependency.
|
||||
* Uses the same ensureTable/getDb pattern as usage-store.ts.
|
||||
*/
|
||||
import { isSqliteAvailable, ensureTable, getDb } from '../index'
|
||||
import { isSqliteAvailable, getDb } from '../index'
|
||||
import { SESSIONS_TABLE, MESSAGES_TABLE } from './schemas'
|
||||
|
||||
// Re-export types for compatibility with sessions-db.ts consumers
|
||||
export interface HermesSessionRow {
|
||||
@@ -57,65 +58,8 @@ export interface HermesSessionDetailRow extends HermesSessionRow {
|
||||
thread_session_count: number
|
||||
}
|
||||
|
||||
// --- Schema ---
|
||||
|
||||
const SESSIONS_TABLE = 'sessions'
|
||||
|
||||
const SESSIONS_SCHEMA: Record<string, string> = {
|
||||
id: 'TEXT PRIMARY KEY',
|
||||
profile: 'TEXT NOT NULL DEFAULT \'default\'',
|
||||
source: 'TEXT NOT NULL DEFAULT \'api_server\'',
|
||||
user_id: 'TEXT',
|
||||
model: 'TEXT NOT NULL DEFAULT \'\'',
|
||||
title: 'TEXT',
|
||||
started_at: 'INTEGER NOT NULL',
|
||||
ended_at: 'INTEGER',
|
||||
end_reason: 'TEXT',
|
||||
message_count: 'INTEGER NOT NULL DEFAULT 0',
|
||||
tool_call_count: 'INTEGER NOT NULL DEFAULT 0',
|
||||
input_tokens: 'INTEGER NOT NULL DEFAULT 0',
|
||||
output_tokens: 'INTEGER NOT NULL DEFAULT 0',
|
||||
cache_read_tokens: 'INTEGER NOT NULL DEFAULT 0',
|
||||
cache_write_tokens: 'INTEGER NOT NULL DEFAULT 0',
|
||||
reasoning_tokens: 'INTEGER NOT NULL DEFAULT 0',
|
||||
billing_provider: 'TEXT',
|
||||
estimated_cost_usd: 'REAL NOT NULL DEFAULT 0',
|
||||
actual_cost_usd: 'REAL',
|
||||
cost_status: 'TEXT NOT NULL DEFAULT \'\'',
|
||||
preview: 'TEXT NOT NULL DEFAULT \'\'',
|
||||
last_active: 'INTEGER NOT NULL',
|
||||
}
|
||||
|
||||
const MESSAGES_TABLE = 'messages'
|
||||
|
||||
const MESSAGES_SCHEMA: Record<string, string> = {
|
||||
id: 'INTEGER PRIMARY KEY AUTOINCREMENT',
|
||||
session_id: 'TEXT NOT NULL',
|
||||
role: 'TEXT NOT NULL',
|
||||
content: 'TEXT NOT NULL DEFAULT \'\'',
|
||||
tool_call_id: 'TEXT',
|
||||
tool_calls: 'TEXT',
|
||||
tool_name: 'TEXT',
|
||||
timestamp: 'INTEGER NOT NULL',
|
||||
token_count: 'INTEGER',
|
||||
finish_reason: 'TEXT',
|
||||
reasoning: 'TEXT',
|
||||
reasoning_details: 'TEXT',
|
||||
reasoning_content: 'TEXT',
|
||||
codex_reasoning_items: 'TEXT',
|
||||
}
|
||||
|
||||
const MESSAGES_INDEX = 'CREATE INDEX IF NOT EXISTS idx_messages_session_id ON messages(session_id)'
|
||||
|
||||
// --- Init ---
|
||||
|
||||
export function initSessionStore(): void {
|
||||
if (!isSqliteAvailable()) return
|
||||
ensureTable(SESSIONS_TABLE, SESSIONS_SCHEMA)
|
||||
ensureTable(MESSAGES_TABLE, MESSAGES_SCHEMA)
|
||||
const db = getDb()!
|
||||
db.exec(MESSAGES_INDEX)
|
||||
}
|
||||
// Note: Table schemas and initialization are now centralized in schemas.ts
|
||||
// Tables are created automatically on bootstrap via initAllHermesTables()
|
||||
|
||||
// --- Helpers ---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user