# sathiyasenpai/vectortrace architecture

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

- Interactive diagram: https://gitdiagram.com/sathiyasenpai/vectortrace
- Repository: https://github.com/sathiyasenpai/vectortrace

## Overview

#### Purpose and Runtime Shape
VectorTrace is a Manifest V3 Chrome extension for locally defining, executing, and repairing webpage extraction schemas. WXT builds separate extension contexts: MV3 service worker, injected content script, React popup/options pages, and an offscreen document for WASM inference. Primary entry points are `src/entrypoints/background.ts`, `src/entrypoints/content.ts`, and `src/entrypoints/offscreen/index.ts`.

#### User-Facing Control Surfaces
The toolbar popup drives schema creation, field selection, extraction, result review, exports, and healing actions. Its stateful workflows are separated into hooks for schema, extraction, change detection, and auto-healing: `src/entrypoints/popup/App.tsx`, `src/popup/hooks/useExtraction.ts`, `src/popup/hooks/useAutoHeal.ts`. The options UI manages persistent preferences, including healing behavior: `src/entrypoints/options/App.tsx`, `src/shared/settings.ts`.

#### Page Capture and Extraction Boundary
The content script is the only layer operating directly on target-page DOMs. It presents visual picking, derives robust CSS/XPath selectors, and extracts/normalizes visible text. Popup requests cross into the content context through messaging; selectors are evaluated CSS-first with XPath fallback. Core paths: `src/content/element-picker.ts`, `src/content/selector-generator.ts`, `src/content/text-extractor.ts`.

#### Background Orchestration and Messaging
The service worker is the control-plane router between popup, content script, storage, and the offscreen model host. Shared message contracts isolate extension contexts and avoid direct DOM/model access from UI code. Relevant boundaries are `src/entrypoints/background.ts`, `src/shared/messaging.ts`, and `src/popup/utils/messaging.ts`.

#### Change Detection and Semantic Healing
Extraction compares current selector output with captured expectations to distinguish valid extraction, broken selectors, hidden/empty content, and content drift. On failure, the system enumerates candidate visible text elements, generates embeddings, cosine-ranks candidates against the saved field vector, and can apply a confidence-qualified replacement selector. Similarity logic and healing history are concentrated in `src/background/similarity.ts`, `src/popup/hooks/useChangeDetection.ts`, and `src/shared/heal-tracker.ts`.

#### Local ML Execution
Embedding inference uses Hugging Face Transformers/ONNX Runtime Web with the 384-dimensional `all-MiniLM-L6-v2` model. Because MV3 service workers cannot host the needed WASM/DOM runtime, background orchestration delegates inference to the offscreen document, which self-warms on creation. The model pipeline is `src/background/embedding-pipeline.ts`; runtime host is `src/entrypoints/offscreen/index.ts`; `plugins/vite-plugin-onnx-bundle.ts` packages ONNX/WASM assets.

#### Persistence and External Dependencies
All operational data remains browser-local: schemas and settings use `chrome.storage.local`; embeddings use IndexedDB to accommodate larger vectors; heal records preserve selector migrations. No backend, API, or telemetry service is part of the current architecture. Persistence abstractions are `src/shared/chrome-storage.ts`, `src/shared/idb-store.ts`, and `src/shared/types.ts`. Required browser infrastructure includes `storage`, `unlimitedStorage`, `offscreen`, scripting/tab access, and `<all_urls>` host permissions.

Main entry points: [`src/entrypoints/background.ts`](https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/background.ts) (MV3 Service Worker), [`src/entrypoints/popup/App.tsx`](https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/popup/App.tsx) (Toolbar Popup), [`src/entrypoints/content.ts`](https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/content.ts) (Content Script), [`src/popup/hooks/useExtraction.ts`](https://github.com/sathiyasenpai/vectortrace/blob/main/src/popup/hooks/useExtraction.ts) (Extraction Workflow)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Extension UI"]
  node_popup["Toolbar Popup<br/>React UI<br/>[App.tsx]"]
  node_extraction_hook["Extraction Workflow<br/>popup hook<br/>[useExtraction.ts]"]
  node_healing_hooks["Detection &amp; Healing<br/>popup hooks"]
  node_auto_heal["Auto-Heal Actions<br/>popup hook<br/>[useAutoHeal.ts]"]
  node_options["Options UI<br/>React UI<br/>[App.tsx]"]
end

subgraph group_page["Target Page Boundary"]
  node_content["Content Script<br/>page runtime<br/>[content.ts]"]
  node_picker["Visual Element Picker<br/>DOM interaction<br/>[element-picker.ts]"]
  node_selectors["Selector Generation<br/>selector engine"]
  node_text_extractor["Text Extraction<br/>DOM extraction<br/>[text-extractor.ts]"]
end

subgraph group_control["Control Plane"]
  node_background{{"MV3 Service Worker<br/>control-plane runtime<br/>[background.ts]"}}
  node_message_contracts["Message Contracts<br/>shared protocol<br/>[messaging.ts]"]
end

subgraph group_ml["Local ML"]
  node_embedding_pipeline["Embedding Pipeline<br/>inference orchestration"]
  node_offscreen["Offscreen Model Host<br/>WASM/DOM runtime<br/>[index.ts]"]
  node_similarity["Candidate Ranking<br/>similarity engine<br/>[similarity.ts]"]
end

subgraph group_data["Local Persistence"]
  node_chrome_storage[("Schemas &amp; Settings<br/>chrome.storage.local<br/>[chrome-storage.ts]")]
  node_embedding_store[("Embedding Store<br/>IndexedDB<br/>[idb-store.ts]")]
  node_heal_tracker[("Heal History<br/>local records<br/>[heal-tracker.ts]")]
  node_settings["Healing Preferences<br/>shared settings<br/>[settings.ts]"]
end

node_popup -->|"uses"| node_extraction_hook
node_popup -->|"uses"| node_healing_hooks
node_popup -->|"uses"| node_auto_heal
node_options -->|"edits"| node_settings
node_extraction_hook -->|"requests extraction"| node_background
node_healing_hooks -->|"requests detection"| node_background
node_auto_heal -->|"requests repair"| node_background
node_background -->|"uses"| node_message_contracts
node_background -->|"routes page commands"| node_content
node_content -->|"presents"| node_picker
node_picker -->|"selected element"| node_selectors
node_content -->|"extracts through"| node_text_extractor
node_text_extractor -->|"evaluates"| node_selectors
node_background -->|"delegates embeddings"| node_embedding_pipeline
node_embedding_pipeline -->|"runs inference in"| node_offscreen
node_background -->|"ranks repair candidates"| node_similarity
node_similarity -->|"reads saved vectors"| node_embedding_store
node_background -->|"reads and writes"| node_chrome_storage
node_background -->|"persists embeddings"| node_embedding_store
node_background -->|"records migrations"| node_heal_tracker
node_settings -->|"stored in"| node_chrome_storage

click node_popup "https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/popup/App.tsx"
click node_extraction_hook "https://github.com/sathiyasenpai/vectortrace/blob/main/src/popup/hooks/useExtraction.ts"
click node_healing_hooks "https://github.com/sathiyasenpai/vectortrace/blob/main/src/popup/hooks/useChangeDetection.ts"
click node_auto_heal "https://github.com/sathiyasenpai/vectortrace/blob/main/src/popup/hooks/useAutoHeal.ts"
click node_options "https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/options/App.tsx"
click node_content "https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/content.ts"
click node_picker "https://github.com/sathiyasenpai/vectortrace/blob/main/src/content/element-picker.ts"
click node_selectors "https://github.com/sathiyasenpai/vectortrace/blob/main/src/content/selector-generator.ts"
click node_text_extractor "https://github.com/sathiyasenpai/vectortrace/blob/main/src/content/text-extractor.ts"
click node_background "https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/background.ts"
click node_message_contracts "https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/messaging.ts"
click node_embedding_pipeline "https://github.com/sathiyasenpai/vectortrace/blob/main/src/background/embedding-pipeline.ts"
click node_offscreen "https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/offscreen/index.ts"
click node_similarity "https://github.com/sathiyasenpai/vectortrace/blob/main/src/background/similarity.ts"
click node_chrome_storage "https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/chrome-storage.ts"
click node_embedding_store "https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/idb-store.ts"
click node_heal_tracker "https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/heal-tracker.ts"
click node_settings "https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/settings.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_popup,node_extraction_hook,node_healing_hooks,node_auto_heal,node_options toneBlue
class node_content,node_picker,node_selectors,node_text_extractor toneAmber
class node_background,node_message_contracts toneMint
class node_embedding_pipeline,node_offscreen,node_similarity toneRose
class node_chrome_storage,node_embedding_store,node_heal_tracker,node_settings toneIndigo
```

## Components

### Extension UI

- [Toolbar Popup](https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/popup/App.tsx): React UI · Drives schema, extraction, review, export, and healing workflows. · `src/entrypoints/popup/App.tsx`
- [Extraction Workflow](https://github.com/sathiyasenpai/vectortrace/blob/main/src/popup/hooks/useExtraction.ts): popup hook · `src/popup/hooks/useExtraction.ts`
- [Detection & Healing](https://github.com/sathiyasenpai/vectortrace/blob/main/src/popup/hooks/useChangeDetection.ts): popup hooks · Compares results and initiates confidence-qualified repairs. · `src/popup/hooks/useChangeDetection.ts`
- [Auto-Heal Actions](https://github.com/sathiyasenpai/vectortrace/blob/main/src/popup/hooks/useAutoHeal.ts): popup hook · `src/popup/hooks/useAutoHeal.ts`
- [Options UI](https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/options/App.tsx): React UI · Manages persistent preferences including healing behavior. · `src/entrypoints/options/App.tsx`

### Target Page Boundary

- [Content Script](https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/content.ts): page runtime · Only extension context that directly accesses target-page DOMs. · `src/entrypoints/content.ts`
- [Visual Element Picker](https://github.com/sathiyasenpai/vectortrace/blob/main/src/content/element-picker.ts): DOM interaction · `src/content/element-picker.ts`
- [Selector Generation](https://github.com/sathiyasenpai/vectortrace/blob/main/src/content/selector-generator.ts): selector engine · Generates robust CSS/XPath selectors. · `src/content/selector-generator.ts`
- [Text Extraction](https://github.com/sathiyasenpai/vectortrace/blob/main/src/content/text-extractor.ts): DOM extraction · Evaluates CSS first with XPath fallback and normalizes visible text. · `src/content/text-extractor.ts`

### Control Plane

- [MV3 Service Worker](https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/background.ts): control-plane runtime · Routes requests among UI, page, storage, and offscreen inference. · `src/entrypoints/background.ts`
- [Message Contracts](https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/messaging.ts): shared protocol · Isolates extension contexts from direct DOM and model access. · `src/shared/messaging.ts`

### Local ML

- [Embedding Pipeline](https://github.com/sathiyasenpai/vectortrace/blob/main/src/background/embedding-pipeline.ts): inference orchestration · Produces 384-dimensional all-MiniLM-L6-v2 embeddings. · `src/background/embedding-pipeline.ts`
- [Offscreen Model Host](https://github.com/sathiyasenpai/vectortrace/blob/main/src/entrypoints/offscreen/index.ts): WASM/DOM runtime · Self-warms and hosts Transformers/ONNX inference outside the service worker. · `src/entrypoints/offscreen/index.ts`
- [Candidate Ranking](https://github.com/sathiyasenpai/vectortrace/blob/main/src/background/similarity.ts): similarity engine · Cosine-ranks visible-text candidates against saved field vectors. · `src/background/similarity.ts`

### Local Persistence

- [Schemas & Settings](https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/chrome-storage.ts): chrome.storage.local · `src/shared/chrome-storage.ts`
- [Embedding Store](https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/idb-store.ts): IndexedDB · Stores larger saved field vectors locally. · `src/shared/idb-store.ts`
- [Heal History](https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/heal-tracker.ts): local records · Preserves selector migrations. · `src/shared/heal-tracker.ts`
- [Healing Preferences](https://github.com/sathiyasenpai/vectortrace/blob/main/src/shared/settings.ts): shared settings · `src/shared/settings.ts`

## Connections

- Toolbar Popup → Extraction Workflow: uses
- Toolbar Popup → Detection & Healing: uses
- Toolbar Popup → Auto-Heal Actions: uses
- Options UI → Healing Preferences: edits
- Extraction Workflow → MV3 Service Worker: requests extraction
- Detection & Healing → MV3 Service Worker: requests detection
- Auto-Heal Actions → MV3 Service Worker: requests repair
- MV3 Service Worker → Message Contracts: uses
- MV3 Service Worker → Content Script: routes page commands
- Content Script → Visual Element Picker: presents
- Visual Element Picker → Selector Generation: selected element
- Content Script → Text Extraction: extracts through
- Text Extraction → Selector Generation: evaluates
- MV3 Service Worker → Embedding Pipeline: delegates embeddings
- Embedding Pipeline → Offscreen Model Host: runs inference in
- MV3 Service Worker → Candidate Ranking: ranks repair candidates
- Candidate Ranking → Embedding Store: reads saved vectors
- MV3 Service Worker → Schemas & Settings: reads and writes
- MV3 Service Worker → Embedding Store: persists embeddings
- MV3 Service Worker → Heal History: records migrations
- Healing Preferences → Schemas & Settings: stored in
