import type { DocumentChunk, SessionMeta, SessionSource, DatabaseConfig, QueryResult, MessageRow, ToolCallRow, AnalyticsFilter, ToolUsageStat, MessageStat, OverviewStats, SessionAnalytics } from "./types"; /** * Resolves the default DB path cross-platform: * - Respects OPENCODE_MEMORY_DB_PATH env var * - Falls back to ~/.local/share/opencode-memory/sessions.db (works on both * macOS and Linux) */ export declare function resolveDbPath(overridePath?: string): string; export interface Database { prepare: (sql: string) => Statement; exec: (sql: string) => void; pragma: (sql: string, opts?: { simple?: boolean; }) => unknown; close: () => void; transaction: (fn: (...args: unknown[]) => T) => (...args: unknown[]) => T; } export interface Statement { run: (...args: unknown[]) => { changes: number; lastInsertRowid: number | bigint; }; get: (...args: unknown[]) => unknown; all: (...args: unknown[]) => unknown[]; } /** * Opens (or creates) a better-sqlite3 database with the sqlite-vec extension * loaded. Initialises the schema if needed. */ export declare function openDatabase(config: DatabaseConfig): Database; /** * Creates the schema tables if they don't already exist. * Exported so tests can call it with an in-memory DB. */ export declare function initSchema(db: Database, embeddingDimension?: number): void; export declare function getSessionMeta(db: Database, sessionId: string): SessionMeta | null; export declare function upsertSessionMeta(db: Database, meta: SessionMeta): void; /** * Inserts a batch of chunks + their embeddings inside a single transaction. * Chunks with duplicate chunk_ids are silently skipped (IGNORE conflict). */ export declare function insertChunks(db: Database, chunks: DocumentChunk[], embeddings: number[][]): void; /** * Batch-inserts message rows. Duplicate (session_id, id) pairs are silently * skipped via INSERT OR IGNORE (idempotent for incremental indexing). */ export declare function insertMessages(db: Database, rows: MessageRow[]): void; /** * Batch-inserts tool call rows. */ export declare function insertToolCalls(db: Database, rows: ToolCallRow[]): void; export interface SectionFilterOptions { /** Single prefix (backward compat). Matches LOWER(section) LIKE prefix%. */ sectionFilter?: string; /** Include only chunks whose section matches one of these prefixes. */ includeSections?: string[]; /** Exclude chunks whose section matches any of these prefixes. */ excludeSections?: string[]; } export declare function queryByEmbedding(db: Database, queryEmbedding: number[], topK?: number, projectFilter?: string, sourceFilter?: SessionSource, fromMs?: number, toMs?: number, sectionFilter?: string, sectionOpts?: SectionFilterOptions): QueryResult[]; /** * Full-text keyword search using FTS5 with BM25 ranking. * Falls back to empty results if the FTS table is empty. */ export declare function queryByKeyword(db: Database, queryText: string, topK?: number, projectFilter?: string, sourceFilter?: SessionSource, fromMs?: number, toMs?: number, sectionFilter?: string, sectionOpts?: SectionFilterOptions): QueryResult[]; /** * Hybrid search: runs both vector and keyword search, merges results * using Reciprocal Rank Fusion (RRF). */ export declare function queryHybrid(db: Database, queryEmbedding: number[], queryText: string, topK?: number, projectFilter?: string, sourceFilter?: SessionSource, fromMs?: number, toMs?: number, sectionFilter?: string, sectionOpts?: SectionFilterOptions): QueryResult[]; export declare function getChunksByUrl(db: Database, url: string, startIndex?: number, endIndex?: number): QueryResult[]; /** * Fetches a window of chunks around a target chunk within the same session. * Orders all session chunks by created_at + chunk_index, finds the target, * and returns `window` chunks before and after it. */ export declare function getSessionContext(db: Database, sessionId: string, chunkId: string, windowSize?: number): QueryResult[]; /** * Lists all session URLs stored in the DB (for "get_session_chunks" calls that * pass a session_id instead of a full URL). */ export declare function listSessionUrls(db: Database, sessionId: string): string[]; export interface SessionRow extends SessionMeta { chunk_count: number; } export interface SessionFilter { source?: SessionSource; fromDate?: number; toDate?: number; } /** * Returns all sessions from sessions_meta enriched with their chunk count, * ordered by updated_at DESC. */ export declare function listSessions(db: Database, filter?: SessionFilter): SessionRow[]; export interface ChunkRow { chunk_id: string; chunk_index: number; total_chunks: number; section: string; heading_hierarchy: string; content: string; url: string; } /** * Returns all chunks for a session ordered by message_order ASC, chunk_index ASC. * message_order is the 0-based position of the message within the session at * index time — stable and source-agnostic (works for OpenCode, Claude Code, * and Cursor whose message IDs are not guaranteed to sort chronologically). */ export declare function getSessionChunksOrdered(db: Database, sessionId: string): ChunkRow[]; /** * Deletes a session's chunks and metadata inside a single transaction. * Returns the number of chunks deleted. */ export declare function deleteSession(db: Database, sessionId: string): number; /** * Deletes all sessions last updated before `olderThanMs` (unix milliseconds). * Returns the number of sessions and chunks removed. */ export declare function deleteSessionsOlderThan(db: Database, olderThanMs: number): { sessions: number; chunks: number; }; /** * Returns tool usage stats: call count, error count, and distinct session count per tool. */ export declare function getToolUsageStats(db: Database, filter?: AnalyticsFilter): ToolUsageStat[]; /** * Returns message counts grouped by role. */ export declare function getMessageStats(db: Database, filter?: AnalyticsFilter): MessageStat[]; /** * Returns aggregate overview stats across all indexed structured data. */ export declare function getOverviewStats(db: Database, filter?: AnalyticsFilter): OverviewStats; /** * Returns detailed analytics for a single session. */ export declare function getSessionAnalytics(db: Database, sessionId: string): SessionAnalytics | null; //# sourceMappingURL=database.d.ts.map