/** * File-based Event Storage Implementation * * JSON-based event storage suitable for serverless environments and low-overhead persistence. * Each world/chat combination is stored in a separate JSON file for efficient access patterns. * * Features: * - JSON format for readable event storage * - One file per world/chat combination for easy cleanup * - Automatic sequence number generation per world/chat * - Support for time-based and sequence-based pagination * - Event type filtering * - Duplicate event ID handling (silently ignores duplicates) * - Write locking to prevent concurrent write corruption * - No database dependencies * - Suitable for serverless and file-based deployments * * Implementation: * - Uses JSON format for human-readable event storage * - Maintains sequence counters in memory with file-based initialization * - File structure: baseDir/worldId/events/chatId.json * - World-scoped storage: events stored within each world's directory * - Checks for duplicate IDs before insertion (matches SQLite INSERT OR IGNORE behavior) * - Write lock manager ensures atomic file operations per file path * * Cascade Delete Behavior: * - Deleting a world directory removes all events for that world * - Deleting a chat file removes all events for that chat * * Changes: * - 2026-03-10: Added optional targeted event deletion by event ID for trim/edit cleanup without wiping an entire chat history. * - 2026-02-08: Fixed write lock queueing bug that allowed concurrent writes after first waiter * - 2026-02-08: Switched event file writes to atomic temp-file + rename to avoid partial read/write races * - 2026-02-08: Added recovery for valid JSON arrays with trailing corruption bytes and auto-rewrite repair * - 2026-02-06: Added write lock manager to prevent concurrent write corruption during rapid event saves * - 2025-11-09: Fixed file paths to be world-scoped (baseDir/worldId/events/) instead of storage root * - 2025-11-03: Added duplicate event ID detection to prevent constraint violations */ import type { EventStorage, StoredEvent, GetEventsOptions } from './types.js'; /** * File storage configuration */ export interface FileEventStorageConfig { baseDir: string; } /** * File-backed event storage implementation */ export declare class FileEventStorage implements EventStorage { private baseDir; private seqCounters; constructor(config: FileEventStorageConfig); /** * Get context key for sequence counter */ private getContextKey; /** * Get the next sequence number for a world/chat context */ private getNextSeq; /** * Save a single event */ saveEvent(event: StoredEvent): Promise; /** * Save multiple events in batch * Note: This method generates sequence numbers sequentially to avoid race conditions */ saveEvents(events: StoredEvent[]): Promise; /** * Get events for a specific world and chat with filtering */ getEventsByWorldAndChat(worldId: string, chatId: string | null, options?: GetEventsOptions): Promise; /** * Delete all events for a specific world and chat */ deleteEventsByWorldAndChat(worldId: string, chatId: string | null): Promise; /** * Delete specific events by ID across all persisted chat files. */ deleteEventsByIds(ids: string[]): Promise; /** * Delete all events for a specific world (all chats) */ deleteEventsByWorld(worldId: string): Promise; /** * Get the latest sequence number for a world/chat context * Returns 0 if no events exist */ getLatestSeq(worldId: string, chatId: string | null): Promise; /** * Get events within a specific sequence range (inclusive) */ getEventRange(worldId: string, chatId: string | null, fromSeq: number, toSeq: number): Promise; /** * Compact a specific event file (remove deleted events, rewrite file) * This is useful after bulk deletions to reclaim space */ compact(worldId: string, chatId: string | null): Promise; } /** * Create a new file-backed event storage instance */ export declare function createFileEventStorage(config: FileEventStorageConfig): EventStorage;