Files
Hermes-ui/packages/server/src/db/hermes/compression-snapshot.ts
T
ekko 6511033ed8 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>
2026-04-29 20:22:07 +08:00

41 lines
1.6 KiB
TypeScript

/**
* SQLite-backed compression snapshot store for 1:1 chat sessions.
*
* Stores the latest compression summary and the index of the last
* compressed message, so incremental compression can pick up where
* the previous one left off.
*/
import { isSqliteAvailable, getDb } from '../index'
import { COMPRESSION_SNAPSHOT_TABLE as TABLE } from './schemas'
export function getCompressionSnapshot(sessionId: string): { summary: string; lastMessageIndex: number; messageCountAtTime: number } | null {
if (!isSqliteAvailable()) return null
return getDb()!.prepare(
`SELECT summary, last_message_index AS lastMessageIndex, message_count_at_time AS messageCountAtTime FROM ${TABLE} WHERE session_id = ?`,
).get(sessionId) as any ?? null
}
export function saveCompressionSnapshot(
sessionId: string,
summary: string,
lastMessageIndex: number,
messageCountAtTime: number,
): void {
if (!isSqliteAvailable()) return
getDb()!.prepare(
`INSERT INTO ${TABLE} (session_id, summary, last_message_index, message_count_at_time, updated_at)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(session_id) DO UPDATE SET
summary = excluded.summary,
last_message_index = excluded.last_message_index,
message_count_at_time = excluded.message_count_at_time,
updated_at = excluded.updated_at`,
).run(sessionId, summary, lastMessageIndex, messageCountAtTime, Date.now())
}
export function deleteCompressionSnapshot(sessionId: string): void {
if (!isSqliteAvailable()) return
getDb()!.prepare(`DELETE FROM ${TABLE} WHERE session_id = ?`).run(sessionId)
}