import type { VectorSearchResult } from "./types.ts"; export interface VectorStoreOptions { /** Directory where the embedding model is cached (created on first model load). */ modelCacheDir?: string; /** * Loader for the @huggingface/transformers module. Defaults to a native * dynamic import; tests inject a fake module to stay offline. * @internal */ loadTransformers?: () => Promise; /** * Run the embedding pipeline in a worker thread (see embedding-worker.ts). * Default: macOS under Node, unless `loadTransformers` is injected. * @internal */ useWorker?: boolean; /** Module the worker loads instead of @huggingface/transformers (tests). @internal */ transformersEntry?: string; } /** * Self-contained vector store using sql.js (SQLite via WebAssembly) and * @huggingface/transformers for local embeddings. * * Zero configuration — works out of the box on any platform. * No native compilation required. * * Persistence: the DB lives in memory (sql.js) and is written to disk as a * whole image. Several phi processes may share the same vectors.db, so every * write is a read-modify-write under an inter-process lock (same mechanism as * the ontology graph): if another process changed the file since we last read * it, the image is reloaded and our pending mutations are replayed on top of * it before writing, so neither side's documents are lost. Writes can be * grouped with batch() to export the image once instead of once per document. */ export declare class VectorStore { private db; private SQL; private pipeline; private dbPath; private initialized; private initPromise; private modelPromise; /** mtime+size of vectors.db when we last read or wrote it; null when absent. */ private diskSignature; /** Mutations applied in memory but not yet written to disk (replayed after a reload). */ private pending; private batchDepth; private readonly modelCacheDir; private readonly loadTransformers; private readonly useWorker; private readonly transformersEntry; constructor(dbPath: string, options?: VectorStoreOptions); /** * Initialize the vector store: sets up the SQLite database. * The embedding model is loaded lazily on first embed operation. * Safe to call multiple times — only initializes once. */ init(): Promise; private _init; private static ensureSchema; /** Cheap change detector for the on-disk image (null when the file is absent). */ private readDiskSignature; /** * (Re)load the in-memory DB from the on-disk image. Returns false when no * usable image exists (absent, or corrupt: e.g. a previous crash left a * truncated file); a fresh DB is used then, so the store self-heals rather * than staying permanently dead behind a swallowed catch upstream. */ private loadFromDisk; /** * Pick up writes made by another process since we last read or wrote the * file. Only done when nothing is pending: pending mutations are replayed * on the fresh image by commit() instead. */ private refreshFromDisk; /** Apply a mutation now (in memory) and write it to disk unless a batch is open. */ private mutate; /** * Write pending mutations: under the file lock, reload the on-disk image if * another process changed it, replay our mutations on top, then write. */ private commit; /** * Group the writes made inside fn into a single disk write (one export of * the whole DB image instead of one per document). Nested calls are merged. */ batch(fn: () => Promise): Promise; /** * Load the embedding model. Called lazily on first embed operation. * Downloads the model on first use (~23MB for all-MiniLM-L6-v2). */ private loadEmbeddingModel; /** * Ensure the embedding model is loaded before use. */ private ensurePipeline; /** * Persist the in-memory SQLite database to disk. Callers hold the file lock. */ private persist; /** * Embed text into a Float32Array vector (384 dimensions). */ private embed; /** * Chunk text into overlapping segments. * * Strategy: * 1. Split by paragraphs (double newline) * 2. If a paragraph exceeds 500 chars, split by sentences * 3. Each chunk gets a 100-char overlap with the previous chunk */ private chunkText; /** * Serialize a Float32Array to a Buffer for BLOB storage. */ private serializeEmbedding; /** * Deserialize a BLOB (Uint8Array) back to Float32Array. */ private deserializeEmbedding; /** * Compute cosine similarity between two vectors. * Both vectors should be normalized (which they are from the model), * so this is equivalent to the dot product. */ private cosineSimilarity; /** Stored chunk texts for a file, in chunk order. */ private storedChunks; /** * Add a document to the vector store. * Chunks the content, embeds each chunk, and stores in the DB. * Replaces any existing chunks for this file. A document whose chunks are * already stored unchanged is skipped (no re-embedding, no disk write), so * re-indexing all notes at startup only embeds new or modified notes. * Resolves to true when the document was (re)indexed. */ addDocument(file: string, content: string): Promise; /** * Search the vector store for content similar to the query. * Returns top-k results sorted by cosine similarity (descending). * * For performance with large DBs (>10K chunks), embeddings are loaded * as Float32Array and compared using optimized JS computation. */ search(query: string, limit?: number): Promise; /** * Remove all chunks for a given file. */ removeDocument(file: string): Promise; /** Distinct file names that have chunks in the given DB. */ private static indexedFiles; /** * Remove every document whose source no longer exists (exists(file) is * false), e.g. a note deleted from disk: otherwise search keeps returning * hits pointing at files that are gone. The predicate is re-evaluated when * the mutation is replayed under the file lock on a reloaded image, so a * file re-created meanwhile by another process is not dropped. Nothing is * written when nothing is stale. Resolves to the number of files removed. */ removeMissing(exists: (file: string) => boolean): Promise; /** * Full reindex from a file map (filename → content). * Clears all existing data and re-indexes everything. */ reindex(files: Map): Promise; /** * Get statistics about the vector store. */ getStats(): { documentCount: number; chunkCount: number; lastUpdate: string; }; /** * Close the database connection, writing any pending (batched) changes. * Nothing is written when nothing changed: blindly re-exporting the image * would overwrite documents added meanwhile by another process. */ close(): void; } //# sourceMappingURL=vector-store.d.ts.map