/** * Channel History Manager * * In-memory storage of recent messages per channel, similar to OpenClaw's * guildHistories map. Stores message history with attachments for context * injection and skill matching. * * Features: * - FIFO ring buffer with configurable limit * - Attachment references preserved * - History formatting for Claude context * - Automatic cleanup of old entries * - SQLite backup for persistence across restarts (Sprint 3 F5) */ import type { SQLiteDatabase } from '../sqlite.js'; import type { MessageAttachment } from './types.js'; /** * Single history entry */ export interface HistoryEntry { /** Message ID */ messageId: string; /** Author username */ sender: string; /** Author user ID */ userId: string; /** Message text content */ body: string; /** Timestamp */ timestamp: number; /** Attached files */ attachments?: MessageAttachment[]; /** Whether this is a bot message */ isBot?: boolean; } /** * Channel history configuration */ export interface ChannelHistoryConfig { /** Maximum messages to keep per channel (default: 20) */ limit?: number; /** Maximum age in ms before auto-cleanup (default: 10 minutes) */ maxAgeMs?: number; /** Optional SQLite database for persistence */ db?: SQLiteDatabase; /** Messages to preload from DB on startup per channel (default: 5) */ preloadLimit?: number; } export interface FormatForContextOptions { /** When provided, include only these users plus bot-authored entries. */ includeUserIds?: ReadonlySet; } /** * Channel History Manager * * Manages per-channel message history in memory with SQLite backup. */ export declare class ChannelHistory { private histories; private config; private db?; private preloadLimit; private cleanupInterval?; constructor(config?: ChannelHistoryConfig); /** * Run database migration (create table if not exists) */ private runMigration; /** * Load recent messages from SQLite on startup */ private loadFromDb; /** * Start periodic cleanup of old messages (every 1 hour) */ private startCleanupTimer; /** * Cleanup messages older than 24 hours from SQLite */ private cleanupDb; /** * Record a message to channel history (in-memory + DB) */ record(channelId: string, entry: HistoryEntry): void; /** * Get history for a channel */ getHistory(channelId: string): HistoryEntry[]; /** * Get recent history excluding the current message */ getRecentHistory(channelId: string, excludeMessageId?: string): HistoryEntry[]; /** * Get recent attachments from history (for skill matching) */ getRecentAttachments(channelId: string, userId?: string): MessageAttachment[]; /** * Format history for Claude context injection * Similar to OpenClaw's "[Chat messages since your last reply - for context]" */ formatForContext(channelId: string, excludeMessageId?: string, keepBotSender?: string, opts?: FormatForContextOptions): string; /** * Update the sender name of a specific history entry. * Safe encapsulated method that avoids direct array mutation from outside. */ updateSender(channelId: string, messageId: string, newSender: string): boolean; /** * Clear history for a channel (after bot reply, like OpenClaw) */ clear(channelId: string): void; /** * Clear attachments from history while keeping text for conversation context */ clearAttachments(channelId: string): void; /** * Clear all histories */ clearAll(): void; /** * Cleanup resources (stop timers) */ destroy(): void; /** * Get all channel IDs with history */ getChannelIds(): string[]; /** * Cleanup old entries across all channels */ cleanup(): number; } /** * Get global channel history instance * * @param config - Optional config for first initialization */ export declare function getChannelHistory(config?: ChannelHistoryConfig): ChannelHistory; /** * Set global channel history instance (for testing) */ export declare function setChannelHistory(history: ChannelHistory): void; /** * Initialize global channel history with SQLite database * * Should be called once at startup before any gateway initialization. */ export declare function initChannelHistory(db: SQLiteDatabase, config?: ChannelHistoryConfig): ChannelHistory; //# sourceMappingURL=channel-history.d.ts.map