# sharma-open-source/browservec architecture

> Architecture diagram of the GitHub repository sharma-open-source/browservec, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sharma-open-source/browservec
- Repository: https://github.com/sharma-open-source/browservec

## Overview

#### 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: [`src/index.ts`](https://github.com/sharma-open-source/browservec/blob/main/src/index.ts) (BrowserVec API), [`src/store/store.ts`](https://github.com/sharma-open-source/browservec/blob/main/src/store/store.ts) (Canonical store), [`src/engine/wgsl/distance.ts`](https://github.com/sharma-open-source/browservec/blob/main/src/engine/wgsl/distance.ts) (WGSL kernels), [`src/persist/format.ts`](https://github.com/sharma-open-source/browservec/blob/main/src/persist/format.ts) (Snapshot format)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public API"]
  node_api["BrowserVec API<br/>package entry<br/>[index.ts]"]
  node_types["Public types<br/>TypeScript types<br/>[types.ts]"]
end

subgraph group_storage["Store &amp; Persistence"]
  node_store[("Canonical store<br/>record store<br/>[store.ts]")]
  node_filter["Metadata filters<br/>filter engine<br/>[filter.ts]"]
  node_snapshot_format["Snapshot format<br/>versioned serialization<br/>[format.ts]"]
  node_storage_backends[("Browser storage<br/>persistence backends<br/>[backend.ts]")]
  node_snapshot_crypto["Snapshot crypto<br/>Web Crypto<br/>[crypto.ts]"]
end

subgraph group_compute["Compute Backends"]
  node_gpu_device{{"WebGPU device<br/>GPU runtime<br/>[device.ts]"}}
  node_gpu_buffers["GPU buffers<br/>buffer manager<br/>[buffers.ts]"]
  node_wgsl["WGSL kernels<br/>GPU kernels<br/>[distance.ts]"]
  node_cpu_fallback["CPU/WASM fallback<br/>fallback runtime<br/>[cpu.ts]"]
  node_simd["SIMD kernels<br/>WASM SIMD<br/>[simd.ts]"]
end

subgraph group_indexes["Search Indexes"]
  node_flat["Flat index<br/>exact search<br/>[flat.ts]"]
  node_gpu_topk["GPU top-k<br/>candidate reduction<br/>[gpuTopk.ts]"]
  node_ivf["IVF index<br/>partitioned ANN<br/>[ivf.ts]"]
  node_ivf_training["IVF training &amp; tuning<br/>k-means tooling<br/>[kmeansTrainer.ts]"]
  node_hnsw["HNSW index<br/>graph ANN<br/>[hnsw.ts]"]
  node_hnsw_graph["HNSW graph state<br/>graph storage<br/>[hnswGraph.ts]"]
  node_hnsw_gpu["GPU beam search<br/>optional accelerator<br/>[hnswGpu.ts]"]
end

subgraph group_ingest["Quantization &amp; Embeddings"]
  node_quant["TurboQuant codec<br/>vector quantization<br/>[codec.ts]"]
  node_quant_worker["Quantization worker<br/>Web Worker<br/>[quantize.worker.ts]"]
  node_embeddings["Text embedders<br/>embedding providers<br/>[hashing.ts]"]
end

node_api -->|"exports"| node_types
node_api -->|"manages records"| node_store
node_api -->|"applies filters"| node_filter
node_api -->|"selects primary backend"| node_gpu_device
node_api -.->|"falls back"| node_cpu_fallback
node_api -->|"selects index"| node_flat
node_api -->|"selects index"| node_ivf
node_api -->|"selects index"| node_hnsw
node_api -->|"restores and saves"| node_snapshot_format
node_embeddings -->|"text vectors"| node_api
node_store -->|"supplies numeric vectors"| node_flat
node_store -->|"supplies numeric vectors"| node_ivf
node_store -->|"supplies records"| node_hnsw
node_store -->|"ingest vectors"| node_quant
node_gpu_device -->|"allocates"| node_gpu_buffers
node_gpu_buffers -->|"binds data"| node_wgsl
node_cpu_fallback -->|"uses"| node_simd
node_flat -->|"reduces candidates"| node_gpu_topk
node_gpu_topk -->|"executes on GPU"| node_wgsl
node_ivf -->|"builds clusters"| node_ivf_training
node_ivf -->|"assigns and searches clusters"| node_wgsl
node_hnsw -->|"maintains"| node_hnsw_graph
node_hnsw -.->|"optionally accelerates"| node_hnsw_gpu
node_hnsw_gpu -->|"runs beam traversal"| node_wgsl
node_quant -.->|"offloads encoding"| node_quant_worker
node_quant -->|"specialized distance kernels"| node_wgsl
node_snapshot_format -->|"reads and writes"| node_storage_backends
node_snapshot_format -.->|"encrypts snapshots"| node_snapshot_crypto
node_snapshot_format -->|"restores graph directly"| node_hnsw_graph

click node_api "https://github.com/sharma-open-source/browservec/blob/main/src/index.ts"
click node_types "https://github.com/sharma-open-source/browservec/blob/main/src/types.ts"
click node_store "https://github.com/sharma-open-source/browservec/blob/main/src/store/store.ts"
click node_filter "https://github.com/sharma-open-source/browservec/blob/main/src/store/filter.ts"
click node_gpu_device "https://github.com/sharma-open-source/browservec/blob/main/src/engine/device.ts"
click node_gpu_buffers "https://github.com/sharma-open-source/browservec/blob/main/src/engine/buffers.ts"
click node_wgsl "https://github.com/sharma-open-source/browservec/blob/main/src/engine/wgsl/distance.ts"
click node_cpu_fallback "https://github.com/sharma-open-source/browservec/blob/main/src/fallback/cpu.ts"
click node_simd "https://github.com/sharma-open-source/browservec/blob/main/src/fallback/simd.ts"
click node_flat "https://github.com/sharma-open-source/browservec/blob/main/src/index/flat.ts"
click node_gpu_topk "https://github.com/sharma-open-source/browservec/blob/main/src/index/gpuTopk.ts"
click node_ivf "https://github.com/sharma-open-source/browservec/blob/main/src/index/ivf.ts"
click node_ivf_training "https://github.com/sharma-open-source/browservec/blob/main/src/index/kmeansTrainer.ts"
click node_hnsw "https://github.com/sharma-open-source/browservec/blob/main/src/index/hnsw.ts"
click node_hnsw_graph "https://github.com/sharma-open-source/browservec/blob/main/src/index/hnswGraph.ts"
click node_hnsw_gpu "https://github.com/sharma-open-source/browservec/blob/main/src/index/hnswGpu.ts"
click node_quant "https://github.com/sharma-open-source/browservec/blob/main/src/quant/codec.ts"
click node_quant_worker "https://github.com/sharma-open-source/browservec/blob/main/src/quant/quantize.worker.ts"
click node_embeddings "https://github.com/sharma-open-source/browservec/blob/main/src/embed/hashing.ts"
click node_snapshot_format "https://github.com/sharma-open-source/browservec/blob/main/src/persist/format.ts"
click node_storage_backends "https://github.com/sharma-open-source/browservec/blob/main/src/persist/backend.ts"
click node_snapshot_crypto "https://github.com/sharma-open-source/browservec/blob/main/src/persist/crypto.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_types toneBlue
class node_store,node_filter,node_snapshot_format,node_storage_backends,node_snapshot_crypto toneAmber
class node_gpu_device,node_gpu_buffers,node_wgsl,node_cpu_fallback,node_simd toneMint
class node_flat,node_gpu_topk,node_ivf,node_ivf_training,node_hnsw,node_hnsw_graph,node_hnsw_gpu toneRose
class node_quant,node_quant_worker,node_embeddings toneIndigo
```

## Components

### Public API

- [BrowserVec API](https://github.com/sharma-open-source/browservec/blob/main/src/index.ts): package entry · Coordinates lifecycle, backend selection, and queries. · `src/index.ts`
- [Public types](https://github.com/sharma-open-source/browservec/blob/main/src/types.ts): TypeScript types · `src/types.ts`

### Store & Persistence

- [Canonical store](https://github.com/sharma-open-source/browservec/blob/main/src/store/store.ts): record store · Owns vectors, metadata, and tombstones. · `src/store/store.ts`
- [Metadata filters](https://github.com/sharma-open-source/browservec/blob/main/src/store/filter.ts): filter engine · `src/store/filter.ts`
- [Snapshot format](https://github.com/sharma-open-source/browservec/blob/main/src/persist/format.ts): versioned serialization · `src/persist/format.ts`
- [Browser storage](https://github.com/sharma-open-source/browservec/blob/main/src/persist/backend.ts): persistence backends · Persists snapshots to OPFS or IndexedDB. · `src/persist/backend.ts`
- [Snapshot crypto](https://github.com/sharma-open-source/browservec/blob/main/src/persist/crypto.ts): Web Crypto · AES-256-GCM encryption with PBKDF2. · `src/persist/crypto.ts`

### Compute Backends

- [WebGPU device](https://github.com/sharma-open-source/browservec/blob/main/src/engine/device.ts): GPU runtime · Initializes devices and manages GPU execution. · `src/engine/device.ts`
- [GPU buffers](https://github.com/sharma-open-source/browservec/blob/main/src/engine/buffers.ts): buffer manager · Allocates and chunks GPU data. · `src/engine/buffers.ts`
- [WGSL kernels](https://github.com/sharma-open-source/browservec/blob/main/src/engine/wgsl/distance.ts): GPU kernels · Distance, assignment, masks, top-k, and traversal kernels. · `src/engine/wgsl/distance.ts`
- [CPU/WASM fallback](https://github.com/sharma-open-source/browservec/blob/main/src/fallback/cpu.ts): fallback runtime · Selects scalar or WASM-SIMD execution when WebGPU is unavailable. · `src/fallback/cpu.ts`
- [SIMD kernels](https://github.com/sharma-open-source/browservec/blob/main/src/fallback/simd.ts): WASM SIMD · `src/fallback/simd.ts`

### Search Indexes

- [Flat index](https://github.com/sharma-open-source/browservec/blob/main/src/index/flat.ts): exact search · `src/index/flat.ts`
- [GPU top-k](https://github.com/sharma-open-source/browservec/blob/main/src/index/gpuTopk.ts): candidate reduction · `src/index/gpuTopk.ts`
- [IVF index](https://github.com/sharma-open-source/browservec/blob/main/src/index/ivf.ts): partitioned ANN · Searches selected vector clusters. · `src/index/ivf.ts`
- [IVF training & tuning](https://github.com/sharma-open-source/browservec/blob/main/src/index/kmeansTrainer.ts): k-means tooling · Builds partitions, quantizes them, and derives probe counts. · `src/index/kmeansTrainer.ts`
- [HNSW index](https://github.com/sharma-open-source/browservec/blob/main/src/index/hnsw.ts): graph ANN · Incremental CPU-native approximate search. · `src/index/hnsw.ts`
- [HNSW graph state](https://github.com/sharma-open-source/browservec/blob/main/src/index/hnswGraph.ts): graph storage · Persistence-ready ANN graph state. · `src/index/hnswGraph.ts`
- [GPU beam search](https://github.com/sharma-open-source/browservec/blob/main/src/index/hnswGpu.ts): optional accelerator · `src/index/hnswGpu.ts`

### Quantization & Embeddings

- [TurboQuant codec](https://github.com/sharma-open-source/browservec/blob/main/src/quant/codec.ts): vector quantization · Rotates and encodes vectors as int8, int4, or 1-bit. · `src/quant/codec.ts`
- [Quantization worker](https://github.com/sharma-open-source/browservec/blob/main/src/quant/quantize.worker.ts): Web Worker · Moves quantized ingest off the UI thread. · `src/quant/quantize.worker.ts`
- [Text embedders](https://github.com/sharma-open-source/browservec/blob/main/src/embed/hashing.ts): embedding providers · Uses hashing embeddings or optional Transformers.js models. · `src/embed/hashing.ts`

## Connections

- 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
