/** * PostgreSQL + pgvector Vector Store * * High-performance vector store using PostgreSQL with pgvector extension. * * Index strategy (per embedding dimension): * ≤ 2000 dims → HNSW on vector column (vector_cosine_ops) O(log n) * > 2000 dims → Two-phase binary quantization search: * Phase 1: ANN on bit column (bit_hamming_ops, HNSW) → candidates * Phase 2: exact cosine re-rank on vector column → top-k * * All write/delete/list operations use raw SQL to avoid the Prisma 7.7.0 + Bun ORM bug * (isObjectEnumValue is not a function). */ import { BaseVectorStore } from './base-vector-store.js'; import { IVectorCollection, VectorDocument, VectorStoreStats, ProjectInfo, SearchResult } from '@massa-th0th/shared'; import type { Pool } from 'pg'; export interface PostgresConfig { connectionString: string; poolSize?: number; indexType?: 'hnsw' | 'ivfflat'; indexParams?: { m?: number; efConstruction?: number; lists?: number; }; } export declare class PostgresVectorStore extends BaseVectorStore { private pool; private config; private initialized; private schemaDimensions; private tableName; /** True when the table has embedding_bq column + HNSW index. Set during init. */ private bqEnabled; constructor(config: PostgresConfig); private getTableName; /** * Clamp a raw pgvector cosine-similarity (`1 - (embedding <=> query)`) into * the [0,1] relevance contract exposed by SearchResult.score. pgvector * returns NULL — which parseFloats to NaN — for rows whose embedding is * missing or zero-norm, and cosine similarity can dip below 0 for * anti-correlated vectors. Both are normalized to 0 so every search result * always carries a finite, in-range score. */ private normalizeScore; /** * Convert float32 embedding to a bit-string for binary quantization. * Positive (≥ 0) → '1', Negative (< 0) → '0'. * The result is passed to PostgreSQL as `$1::bit(N)`. */ private floatsToBit; ensureInitialized(): Promise; /** * Detect chunks indexed under a different dimension than the current provider. * * When the user changes EMBEDDING_PROVIDER or the embedding model, the new * provider's dims may not match existing rows (which are stored in * vector_documents_d). Search then silently hits an empty table for * affected projects. We warn so the user knows a reindex is required. */ private detectOrphanedChunks; private createFallbackTable; private createVectorIndex; /** * Create HNSW index on the binary-quantized bit column. * bit_hamming_ops supports any dimension (no pgvector limit). */ private createBqIndex; addDocument(id: string, content: string, metadata?: Record): Promise; /** * Insert documents, embedding them in sub-batches to avoid overwhelming * the embedding backend. Each sub-batch is its own short transaction * (matches PostgresVectorStore semantics — partial progress survives an * Ollama crash mid-file, instead of rolling back the whole file). * Fails-open per-document when a whole sub-batch's embed call errors. */ addDocuments(documents: VectorDocument[]): Promise; /** Insert a pre-embedded sub-batch inside a single short transaction. */ private insertSubBatch; search(query: string, limit?: number, projectId?: string): Promise; searchByEmbedding(embedding: number[], limit?: number, projectId?: string): Promise; /** * Direct cosine similarity search (for dims ≤ 2000 with HNSW index). */ private searchDirect; /** * Two-phase binary-quantization search (for dims > 2000). * * Phase 1: ANN using Hamming distance on bit column → `candidates` rows * Phase 2: exact cosine re-rank on float vector → top `limit` rows */ private searchTwoPhase; delete(id: string): Promise; deleteByProject(projectId: string): Promise; update(id: string, content: string, metadata?: Record): Promise; getStats(projectId?: string): Promise; listProjects(): Promise; /** * Get or create a collection (for IVectorStore interface compatibility). * * Mirrors PostgresVectorStore.getCollection semantics: returns a handle bound * to this store's pool/table that scopes read/write by `name` (the projectId). * IndexManager uses this for _metadata document lookup/persistence. */ getCollection(name: string): Promise; healthCheck(): Promise; close(): Promise; /** Embedding dimensions declared by the table schema, or null if unknown. */ getSchemaDimensions(): number | null; /** True when the table has the embedding_bq column + index. */ isBqEnabled(): boolean; /** Encode a float embedding to a pgvector bit-string (binary quantization). */ toBitString(embedding: number[]): string; /** Batch-embed texts via the shared provider (exposed for the collection). */ embedBatchPublic(contents: string[]): Promise; } //# sourceMappingURL=postgres-vector-store.d.ts.map