/** * SQLite Database Module v3 * * Handles persistent storage of sessions, prompts, and tool calls. * Uses Bun's built-in SQLite for high-performance lookups. * * Database is stored globally at ~/.agentblame/agentblame.db * Each session is namespaced by repo identifier. */ import { Database } from "bun:sqlite"; import type { AiAgent } from "./types"; export interface DbSession { id: string; repo: string; agent: AiAgent; model: string | null; conversationId: string | null; createdAt: string; firstCommitSha: string | null; firstCommitAt: string | null; } export interface DbPrompt { id: number; sessionId: string; content: string | null; contentHash: string; timestamp: string; } export interface DbToolCall { id: number; sessionId: string; toolName: string; filePath: string | null; timestamp: string; } /** * Get the global agentblame directory (~/.agentblame/) */ export declare function getGlobalAgentBlameDir(): string; /** * Get the global database path (~/.agentblame/agentblame.db) */ export declare function getGlobalDbPath(): string; /** * Get the global logs directory (~/.agentblame/logs/) */ export declare function getGlobalLogsDir(): string; /** * Ensure the global agentblame directory structure exists */ export declare function ensureGlobalAgentBlameDir(): void; /** * Generate a repo identifier from the repo root path * Uses the git remote URL if available, otherwise the path */ export declare function getRepoIdentifier(repoRoot: string): string; /** * Set the database path directly. * For most cases, use initGlobalDatabase() instead. */ export declare function setDatabasePath(dbPath: string): void; /** * Initialize the global database * This is the primary way to initialize the database. * Throws if DB doesn't exist - user must run 'setup' first. */ export declare function initGlobalDatabase(): void; /** * Get the database file path * Throws if DB not initialized - user must run 'setup' first. */ export declare function getDbPath(): string; /** * Initialize and return the database connection */ export declare function getDatabase(): Database; /** * Close the database connection */ export declare function closeDatabase(): void; /** * Initialize database */ export declare function initDatabase(): void; /** * Reset database (drop and recreate tables) */ export declare function resetDatabase(): void; /** * Generate a stable session ID from agent and conversation ID */ export declare function generateSessionId(agent: AiAgent, conversationId: string): string; export interface UpsertSessionParams { id: string; repo: string; agent: AiAgent; model?: string | null; conversationId?: string | null; } /** * Upsert a session */ export declare function upsertSession(params: UpsertSessionParams): void; /** * Get a session by ID */ export declare function getSession(sessionId: string): DbSession | null; /** * Update session with first commit info */ export declare function updateSessionFirstCommit(sessionId: string, commitSha: string): void; /** * Get recent sessions */ export declare function getRecentSessions(limit?: number): DbSession[]; export interface InsertPromptParams { sessionId: string; content: string | null; contentHash: string; timestamp?: string; } /** * Insert a new prompt */ export declare function insertPrompt(params: InsertPromptParams): number; /** * Generate a hash for prompt content (for deduplication) */ export declare function hashPromptContent(content: string): string; /** * Get prompts for a session */ export declare function getPromptsForSession(sessionId: string): DbPrompt[]; /** * Get the most recent prompt for a session */ export declare function getLatestPromptForSession(sessionId: string): DbPrompt | null; /** * Get all prompts for a session concatenated into one string * Useful for displaying the full conversation context in CLI */ export declare function getConcatenatedPromptsForSession(sessionId: string): string | null; /** * Get all prompts for a session with their associated tool call summaries * Tool calls are grouped by the prompt that triggered them (based on timestamps) * Used for git notes and analytics */ export declare function getPromptsWithToolCounts(sessionId: string): Array<{ id: number; timestamp: string; content: string | null; tools?: Record; duration?: number; }> | null; /** * Check if a prompt already exists (by hash) */ export declare function promptExists(sessionId: string, contentHash: string): boolean; export interface InsertToolCallParams { sessionId: string; toolName: string; filePath?: string | null; timestamp?: string; } /** * Insert a new tool call */ export declare function insertToolCall(params: InsertToolCallParams): number; /** * Get tool calls for a session */ export declare function getToolCallsForSession(sessionId: string): DbToolCall[]; /** * Get unique tool names used in a session */ export declare function getToolNamesForSession(sessionId: string): string[]; /** * Get tool call counts for a session * Returns a map of tool_name -> count */ export declare function getToolCountsForSession(sessionId: string): Record; /** * Get session duration in seconds (last tool call - session start) * Returns null if no tool calls */ export declare function getSessionDuration(sessionId: string): number | null; /** * Clean up old entries (sessions without commits older than maxAgeDays) */ export declare function cleanupOldEntries(maxAgeDays?: number): { removed: number; kept: number; }; /** * Get stats for status display */ export declare function getStats(): { sessions: number; prompts: number; toolCalls: number; }; /** * Get stats for a specific repo */ export declare function getStatsForRepo(repo: string): { sessions: number; prompts: number; toolCalls: number; }; /** * Register a repo as enabled */ export declare function enableRepo(repo: string): void; /** * Unregister a repo */ export declare function disableRepo(repo: string): void; /** * Check if a repo is enabled */ export declare function isRepoEnabled(repo: string): boolean; /** * Get all enabled repos */ export declare function getEnabledRepos(): Array<{ repo: string; enabledAt: string; }>; /** * Wipe all data and recreate fresh database * Used by 'agentblame clean' */ export declare function wipeAndRecreateDatabase(): void;