How sharma-open-source/browservec works
Purpose and Public Entry Point
BrowserVec is a browser-only vector store for client-side embedding retrieval, with WebGPU acceleration and CPU/WASM fallback. The package API is exported from src/index.ts; public types and creation/query options are centralized in src/types.ts.
Store Lifecycle and API Orchestration
BrowserVec coordinates creation, ingest, querying, updates/deletes, compaction, device selection, and persistence restoration. The canonical vector/metadata/tombstone store and metadata filtering live in src/store/store.ts and src/store/filter.ts. This layer is the boundary between public records and index-specific numeric layouts.
Execution Backends
WebGPU is the primary compute runtime: device initialization, buffer allocation/chunking, and profiling are in src/engine/device.ts, src/engine/buffers.ts, and src/engine/profile.ts. GPU kernels in src/engine/wgsl/ implement distance, assignment, masks, top-k, and graph traversal. When WebGPU is absent, src/fallback/cpu.ts selects scalar or WASM-SIMD execution using src/fallback/simd.ts and src/fallback/kernel.wat.
Search Index Families
Flat exact search is implemented by src/index/flat.ts, with GPU candidate reduction in src/index/gpuTopk.ts. IVF partitions vectors through k-means and searches selected clusters (src/index/ivf.ts, src/index/kmeansTrainer.ts, src/index/ivfquant.ts); tuning can derive probe count from recall targets in src/index/ivfTune.ts. HNSW provides incremental CPU-native graph ANN search and persistence-ready graph state (src/index/hnsw.ts, src/index/hnswGraph.ts), with optional batched GPU beam search in src/index/hnswGpu.ts.
Quantized Ingest and Search
TurboQuant encodes vectors as int8, int4, or 1-bit after randomized rotation, while retaining fp32 vectors for exact reranking. Codec and rotation primitives are in src/quant/codec.ts, src/quant/rotator.ts, and src/quant/encode.ts; worker-based quantization is in src/quant/quantize.worker.ts. Quantized distance/assignment kernels are specialized in src/engine/wgsl/distanceQ8.ts, src/engine/wgsl/distanceQ4.ts, and src/engine/wgsl/distanceQ1.ts.
Async Work and Embeddings
Compute-heavy ingest/index build steps can move off the UI thread: HNSW and k-means worker entry points are src/index/hnsw.worker.ts and src/index/kmeans.worker.ts. Text APIs obtain vectors through the dependency-free hashing embedder in src/embed/hashing.ts or optional Transformers.js integration in src/embed/transformers.ts. Workers and optional model loading are external-runtime boundaries.
Persistence and Security
Snapshots serialize store/index state through versioned format and backend abstractions in src/persist/format.ts and src/persist/backend.ts. Browser storage targets are OPFS (src/persist/opfs.ts) or IndexedDB (src/persist/indexeddb.ts); snapshots may be encrypted with Web Crypto AES-256-GCM and PBKDF2 in src/persist/crypto.ts. Persistence is intentionally below index selection so restored HNSW graphs avoid rebuilds.
Main entry points: index.ts BrowserVec API · store.ts Canonical store · distance.ts WGSL kernels · format.ts Snapshot format
Components 22
Public API
- BrowserVec API · Coordinates lifecycle, backend selection, and queries.
src/index.ts - Public types
src/types.ts
Store & Persistence
- Canonical store · Owns vectors, metadata, and tombstones.
src/store/store.ts - Metadata filters
src/store/filter.ts - Snapshot format
src/persist/format.ts - Browser storage · Persists snapshots to OPFS or IndexedDB.
src/persist/backend.ts - Snapshot crypto · AES-256-GCM encryption with PBKDF2.
src/persist/crypto.ts
Compute Backends
- WebGPU device · Initializes devices and manages GPU execution.
src/engine/device.ts - GPU buffers · Allocates and chunks GPU data.
src/engine/buffers.ts - WGSL kernels · Distance, assignment, masks, top-k, and traversal kernels.
src/engine/wgsl/distance.ts - CPU/WASM fallback · Selects scalar or WASM-SIMD execution when WebGPU is unavailable.
src/fallback/cpu.ts - SIMD kernels
src/fallback/simd.ts
Search Indexes
- Flat index
src/index/flat.ts - GPU top-k
src/index/gpuTopk.ts - IVF index · Searches selected vector clusters.
src/index/ivf.ts - IVF training & tuning · Builds partitions, quantizes them, and derives probe counts.
src/index/kmeansTrainer.ts - HNSW index · Incremental CPU-native approximate search.
src/index/hnsw.ts - HNSW graph state · Persistence-ready ANN graph state.
src/index/hnswGraph.ts - GPU beam search
src/index/hnswGpu.ts
Quantization & Embeddings
- TurboQuant codec · Rotates and encodes vectors as int8, int4, or 1-bit.
src/quant/codec.ts - Quantization worker · Moves quantized ingest off the UI thread.
src/quant/quantize.worker.ts - Text embedders · Uses hashing embeddings or optional Transformers.js models.
src/embed/hashing.ts
Connections 29
- BrowserVec API → Public types: exports
- BrowserVec API → Canonical store: manages records
- BrowserVec API → Metadata filters: applies filters
- BrowserVec API → WebGPU device: selects primary backend
- BrowserVec API → CPU/WASM fallback: falls back
- BrowserVec API → Flat index: selects index
- BrowserVec API → IVF index: selects index
- BrowserVec API → HNSW index: selects index
- BrowserVec API → Snapshot format: restores and saves
- Text embedders → BrowserVec API: text vectors
- Canonical store → Flat index: supplies numeric vectors
- Canonical store → IVF index: supplies numeric vectors
- Canonical store → HNSW index: supplies records
- Canonical store → TurboQuant codec: ingest vectors
- WebGPU device → GPU buffers: allocates
- GPU buffers → WGSL kernels: binds data
- CPU/WASM fallback → SIMD kernels: uses
- Flat index → GPU top-k: reduces candidates
- GPU top-k → WGSL kernels: executes on GPU
- IVF index → IVF training & tuning: builds clusters
- IVF index → WGSL kernels: assigns and searches clusters
- HNSW index → HNSW graph state: maintains
- HNSW index → GPU beam search: optionally accelerates
- GPU beam search → WGSL kernels: runs beam traversal
- TurboQuant codec → Quantization worker: offloads encoding
- TurboQuant codec → WGSL kernels: specialized distance kernels
- Snapshot format → Browser storage: reads and writes
- Snapshot format → Snapshot crypto: encrypts snapshots
- Snapshot format → HNSW graph state: restores graph directly