/** * Conversation Storage Layer - CRUD operations for all conversation-related data. * * This class provides the data access layer for the cccmemory system. * It handles storing and retrieving conversations, messages, tool uses, decisions, * mistakes, requirements, and git commits. * * All store operations use transactions for atomicity and performance. * All JSON fields are automatically serialized/deserialized. * * @example * ```typescript * const storage = new ConversationStorage(sqliteManager); * await storage.storeConversations(conversations); * const conv = storage.getConversation('conv-123'); * const timeline = storage.getFileTimeline('src/index.ts'); * ``` */ import type { SQLiteManager } from "./SQLiteManager.js"; import type { Conversation, Message, ToolUse, ToolResult, FileEdit, ThinkingBlock } from "../parsers/ConversationParser.js"; import type { Decision } from "../parsers/DecisionExtractor.js"; import type { Mistake } from "../parsers/MistakeExtractor.js"; import type { GitCommit } from "../parsers/GitIntegrator.js"; import type { Requirement, Validation } from "../parsers/RequirementsExtractor.js"; import type { Methodology } from "../parsers/MethodologyExtractor.js"; import type { ResearchFinding } from "../parsers/ResearchExtractor.js"; import type { SolutionPattern } from "../parsers/SolutionPatternExtractor.js"; import { type QueryCacheConfig, type CacheStats } from "../cache/QueryCache.js"; /** * Data access layer for conversation memory storage. * * Provides CRUD operations for all conversation-related entities using SQLite. * Supports optional caching for frequently accessed queries. */ export declare class ConversationStorage { private db; private cache; private projectIdCache; /** * Create a new ConversationStorage instance. * * @param db - SQLiteManager instance for database access */ constructor(db: SQLiteManager); /** * Enable query result caching. * * Caching improves performance for frequently accessed queries by storing * results in memory. Cache is automatically invalidated when data changes. * * @param config - Cache configuration (maxSize and ttlMs) * * @example * ```typescript * storage.enableCache({ maxSize: 100, ttlMs: 300000 }); * ``` */ enableCache(config: QueryCacheConfig): void; /** * Disable query result caching. * * Clears all cached data and stops caching new queries. */ disableCache(): void; /** * Check if caching is enabled. * * @returns True if caching is enabled */ isCacheEnabled(): boolean; /** * Clear all cached query results. * * Clears the cache but keeps caching enabled. */ clearCache(): void; /** * Get cache statistics. * * Returns performance metrics including hits, misses, hit rate, and evictions. * * @returns Cache statistics or null if caching is disabled * * @example * ```typescript * const stats = storage.getCacheStats(); * if (stats) { * console.error(`Hit rate: ${(stats.hitRate * 100).toFixed(1)}%`); * } * ``` */ getCacheStats(): CacheStats | null; getProjectId(projectPath: string): number; private ensureProjectId; /** * Store conversations in the database. * * Uses UPSERT (INSERT ON CONFLICT UPDATE) to handle both new and updated conversations. * All operations are performed in a single transaction for atomicity. * * @param conversations - Array of conversation objects to store * @returns Promise that resolves when all conversations are stored * * @example * ```typescript * await storage.storeConversations([ * { * id: 'conv-123', * project_path: '/path/to/project', * first_message_at: Date.now(), * last_message_at: Date.now(), * message_count: 42, * git_branch: 'main', * claude_version: '3.5', * metadata: {}, * created_at: Date.now(), * updated_at: Date.now() * } * ]); * ``` */ storeConversations(conversations: Conversation[]): Promise>; /** * Retrieve a single conversation by ID. * * @param id - Conversation ID to retrieve * @returns Conversation object if found, null otherwise * * @example * ```typescript * const conv = storage.getConversation('conv-123'); * if (conv) { * console.error(`${conv.message_count} messages on ${conv.git_branch}`); * } * ``` */ getConversation(id: string, projectPath?: string): Conversation | null; /** * Store messages in the database. * * Stores all messages from conversations including content, metadata, and relationships. * Uses UPSERT (INSERT ON CONFLICT UPDATE) for idempotent storage. * * @param messages - Array of message objects to store * @param skipFtsRebuild - Skip FTS rebuild (for batch operations, call rebuildAllFts() at end) * @returns Promise that resolves when all messages are stored * * @example * ```typescript * await storage.storeMessages([ * { * id: 'msg-123', * conversation_id: 'conv-123', * message_type: 'text', * role: 'user', * content: 'Hello', * timestamp: Date.now(), * is_sidechain: false, * metadata: {} * } * ]); * ``` */ storeMessages(messages: Message[], options: { skipFtsRebuild?: boolean; conversationIdMap: Map; }): Promise>; /** * Rebuild the messages FTS index. * Required for FTS5 external content tables after inserting data. * Call this after batch operations that used skipFtsRebuild=true. */ rebuildMessagesFts(): void; /** * Store tool use records in the database. * * Records all tool invocations from assistant messages. * * @param toolUses - Array of tool use objects * @returns Promise that resolves when stored */ storeToolUses(toolUses: ToolUse[], messageIdMap: Map): Promise>; /** * Store tool execution results in the database. * * Records the output/results from tool invocations. * * @param toolResults - Array of tool result objects * @returns Promise that resolves when stored */ storeToolResults(toolResults: ToolResult[], messageIdMap: Map, toolUseIdMap: Map): Promise; /** * Store file edit records in the database. * * Records all file modifications made during conversations. * * @param fileEdits - Array of file edit objects * @returns Promise that resolves when stored */ storeFileEdits(fileEdits: FileEdit[], conversationIdMap: Map, messageIdMap: Map): Promise; /** * Retrieve all edits for a specific file. * * @param filePath - Path to the file * @returns Array of file edits, ordered by timestamp (most recent first) */ getFileEdits(filePath: string): FileEdit[]; /** * Store thinking blocks in the database. * * Thinking blocks contain Claude's internal reasoning. They can be large and * are optionally indexed based on the includeThinking flag. * * @param blocks - Array of thinking block objects * @returns Promise that resolves when stored */ storeThinkingBlocks(blocks: ThinkingBlock[], messageIdMap: Map): Promise; /** * Store extracted decisions in the database. * * Decisions include architectural choices, technical decisions, and their rationale. * * @param decisions - Array of decision objects * @param skipFtsRebuild - Skip FTS rebuild (for batch operations, call rebuildAllFts() at end) * @returns Promise that resolves when stored */ storeDecisions(decisions: Decision[], options: { skipFtsRebuild?: boolean; conversationIdMap: Map; messageIdMap: Map; }): Promise>; /** * Rebuild the decisions FTS index. * Required for FTS5 external content tables after inserting data. * Call this after batch operations that used skipFtsRebuild=true. */ rebuildDecisionsFts(): void; /** * Rebuild all FTS indexes. * Call this once after batch operations that used skipFtsRebuild=true. */ rebuildAllFts(): void; /** * Retrieve all decisions related to a specific file. * * @param filePath - Path to the file * @returns Array of decisions that reference this file * @internal */ getDecisionsForFile(filePath: string): Decision[]; /** * Store git commit records linked to conversations. * * Links git commits to the conversations where they were made or discussed. * * @param commits - Array of git commit objects * @returns Promise that resolves when stored */ storeGitCommits(commits: GitCommit[], projectId: number, conversationIdMap: Map, messageIdMap: Map): Promise; getCommitsForFile(filePath: string): GitCommit[]; /** * Store extracted mistakes in the database. * * Mistakes include errors, bugs, and wrong approaches that were later corrected. * * @param mistakes - Array of mistake objects * @returns Promise that resolves when stored */ storeMistakes(mistakes: Mistake[], conversationIdMap: Map, messageIdMap: Map): Promise>; /** * Store extracted requirements in the database. * * Requirements include dependencies, constraints, and specifications for components. * * @param requirements - Array of requirement objects * @returns Promise that resolves when stored */ storeRequirements(requirements: Requirement[], conversationIdMap: Map, messageIdMap: Map): Promise; /** * Store validation records in the database. * * Validations capture test results and performance data from conversations. * * @param validations - Array of validation objects * @returns Promise that resolves when stored */ storeValidations(validations: Validation[], conversationIdMap: Map): Promise; /** * Get the complete timeline of changes to a file. * * Combines file edits, git commits, and related decisions into a single timeline. * This is a key method used by tools like checkBeforeModify and getFileEvolution. * * @param filePath - Path to the file * @returns Object containing: * - `file_path`: The file path queried * - `edits`: All file edit records * - `commits`: All git commits affecting this file * - `decisions`: All decisions related to this file * * @example * ```typescript * const timeline = storage.getFileTimeline('src/index.ts'); * console.error(`${timeline.edits.length} edits`); * console.error(`${timeline.commits.length} commits`); * console.error(`${timeline.decisions.length} decisions`); * ``` */ getFileTimeline(filePath: string): { file_path: string; edits: FileEdit[]; commits: GitCommit[]; decisions: Decision[]; }; /** * Get statistics about the indexed conversation data. * * Returns counts of all major entity types stored in the database. * Used for displaying indexing results and system health checks. * * @returns Object containing counts for: * - `conversations`: Total conversations indexed * - `messages`: Total messages stored * - `decisions`: Total decisions extracted * - `mistakes`: Total mistakes documented * - `git_commits`: Total git commits linked * * @example * ```typescript * const stats = storage.getStats(); * console.error(`Indexed ${stats.conversations.count} conversations`); * console.error(`Extracted ${stats.decisions.count} decisions`); * console.error(`Linked ${stats.git_commits.count} commits`); * ``` */ getStats(): { conversations: { count: number; }; messages: { count: number; }; decisions: { count: number; }; mistakes: { count: number; }; git_commits: { count: number; }; }; getStatsForProject(projectPath: string, sourceType: "claude-code" | "codex"): { conversations: { count: number; }; messages: { count: number; }; decisions: { count: number; }; mistakes: { count: number; }; git_commits: { count: number; }; }; /** * Store extracted methodologies in the database. * * Methodologies track how AI solved problems (approach, steps, tools). * * @param methodologies - Array of methodology objects * @param conversationIdMap - Map of external to internal conversation IDs * @param messageIdMap - Map of external to internal message IDs * @returns Promise with map of external to internal methodology IDs */ storeMethodologies(methodologies: Methodology[], conversationIdMap: Map, messageIdMap: Map): Promise>; /** * Store extracted research findings in the database. * * Research findings track discoveries made during exploration/research. * * @param findings - Array of research finding objects * @param conversationIdMap - Map of external to internal conversation IDs * @param messageIdMap - Map of external to internal message IDs * @returns Promise with map of external to internal finding IDs */ storeResearchFindings(findings: ResearchFinding[], conversationIdMap: Map, messageIdMap: Map): Promise>; /** * Store extracted solution patterns in the database. * * Solution patterns track reusable solutions for common problems. * * @param patterns - Array of solution pattern objects * @param conversationIdMap - Map of external to internal conversation IDs * @param messageIdMap - Map of external to internal message IDs * @returns Promise with map of external to internal pattern IDs */ storeSolutionPatterns(patterns: SolutionPattern[], conversationIdMap: Map, messageIdMap: Map): Promise>; } //# sourceMappingURL=ConversationStorage.d.ts.map