# vastnext/lexilayer-translator architecture

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

- Interactive diagram: https://gitdiagram.com/vastnext/lexilayer-translator
- Repository: https://github.com/vastnext/lexilayer-translator

## Overview

LexiLayer is a Chrome browser translation extension. A reader configures an engine and language in the popup, starts a page translation, and the content script scans and schedules text, sends batches to the background service worker, then renders returned translations in the page. The options page manages AI engines, expert prompts, preferences, and vocabulary. Separate branches support selection and input translation, video subtitles, local caching, and Anki synchronization. The README documents these capabilities; several subsystem connections are shown as unverified where sampled code did not expose their wiring.

Main entry points: [`src/background/index.ts`](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/index.ts) (Message and task coordinator), [`src/content/index.ts`](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/index.ts) (Page controller), [`src/background/provider-registry.ts`](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/provider-registry.ts) (Provider registry), [`src/options/VocabularySection.tsx`](https://github.com/vastnext/lexilayer-translator/blob/main/src/options/VocabularySection.tsx) (Vocabulary manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_controls["Extension controls"]
  node_popup["Translation controls<br/>[PopupApp.tsx]"]
  node_settings["Engine settings<br/>[OptionsApp.tsx]"]
  node_options_api["Options API<br/>[api.ts]"]
  node_config["Shared configuration<br/>[config.ts]"]
  node_experts["Expert prompts<br/>[experts.ts]"]
end

subgraph group_page["Page translation"]
  node_content_entry["Content entry<br/>[main.ts]"]
  node_page_controller["Page controller<br/>[index.ts]"]
  node_scanner["DOM scanner<br/>[dom-scanner.ts]"]
  node_site_rules["Site rules<br/>[catalog.ts]"]
  node_scheduler["Visible scheduler<br/>[scheduler.ts]"]
  node_paragraphs["Paragraph state<br/>[paragraph-store.ts]"]
  node_renderers["Page renderers<br/>[dom-renderer.ts]"]
  node_selection["Selection translation"]
end

subgraph group_engine["Translation services"]
  node_background["Message and task coordinator<br/>[index.ts]"]
  node_ai_coordinator["AI coordinator<br/>[ai-coordinator.ts]"]
  node_provider_registry["Provider registry"]
  node_google["Google client"]
  node_bing["Bing client"]
  node_openai["OpenAI client<br/>[openai-client.ts]"]
  node_cache[("Translation cache<br/>[cache.ts]")]
end

subgraph group_vocabulary["Vocabulary"]
  node_vocabulary_ui["Vocabulary manager"]
  node_vocabulary_storage[("Vocabulary storage")]
  node_anki["Anki synchronization<br/>[anki-client.ts]"]
  node_shared_vocabulary["Vocabulary model<br/>[vocabulary.ts]"]
end

subgraph group_video["Video subtitles"]
  node_youtube["YouTube subtitles<br/>[session.ts]"]
  node_youtube_renderer["Subtitle renderer<br/>[renderer.ts]"]
  node_bilibili["Bilibili subtitles<br/>[index.ts]"]
end

node_reader(("Reader"))
node_external_services(("Translation providers"))
node_anki_service(("AnkiConnect"))

node_reader -->|"chooses translation"| node_popup
node_popup -.->|"commands page"| node_content_entry
node_reader -->|"configures"| node_settings
node_settings -->|"updates settings"| node_options_api
node_options_api -->|"uses settings model"| node_config
node_content_entry -->|"assembles"| node_page_controller
node_page_controller -->|"scans text"| node_scanner
node_page_controller -->|"loads site rule"| node_site_rules
node_page_controller -->|"schedules batches"| node_scheduler
node_page_controller -->|"tracks paragraphs"| node_paragraphs
node_page_controller -->|"renders results"| node_renderers
node_scheduler -->|"runs worker"| node_page_controller
node_page_controller -.->|"requests translation"| node_background
node_background -->|"coordinates AI"| node_ai_coordinator
node_background -->|"creates provider"| node_provider_registry
node_provider_registry -.->|"selects client"| node_google
node_provider_registry -.->|"selects client"| node_bing
node_provider_registry -.->|"selects client"| node_openai
node_google -.->|"sends translation"| node_external_services
node_bing -.->|"sends translation"| node_external_services
node_openai -.->|"sends translation"| node_external_services
node_background -->|"reads and writes"| node_cache
node_background -->|"loads preferences"| node_config
node_ai_coordinator -.->|"uses expert prompts"| node_experts
node_reader -->|"selects text"| node_selection
node_selection -.->|"requests translation"| node_background
node_reader -->|"manages saved words"| node_vocabulary_ui
node_vocabulary_ui -->|"validates entries"| node_shared_vocabulary
node_background -->|"stores entries"| node_vocabulary_storage
node_vocabulary_ui -->|"starts sync"| node_anki
node_anki -.->|"sends notes"| node_anki_service
node_youtube -.->|"requests translation"| node_background
node_youtube -.->|"renders subtitles"| node_youtube_renderer
node_bilibili -.->|"requests translation"| node_background
node_bilibili -.->|"renders subtitles"| node_youtube_renderer

click node_popup "https://github.com/vastnext/lexilayer-translator/blob/main/src/popup/PopupApp.tsx"
click node_settings "https://github.com/vastnext/lexilayer-translator/blob/main/src/options/OptionsApp.tsx"
click node_options_api "https://github.com/vastnext/lexilayer-translator/blob/main/src/options/api.ts"
click node_content_entry "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/main.ts"
click node_page_controller "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/index.ts"
click node_scanner "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/dom-scanner.ts"
click node_site_rules "https://github.com/vastnext/lexilayer-translator/blob/main/src/rules/catalog.ts"
click node_scheduler "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/scheduler.ts"
click node_paragraphs "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/paragraph-store.ts"
click node_renderers "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/dom-renderer.ts"
click node_selection "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/selection-controller.ts"
click node_background "https://github.com/vastnext/lexilayer-translator/blob/main/src/background/index.ts"
click node_ai_coordinator "https://github.com/vastnext/lexilayer-translator/blob/main/src/background/ai-coordinator.ts"
click node_provider_registry "https://github.com/vastnext/lexilayer-translator/blob/main/src/background/provider-registry.ts"
click node_google "https://github.com/vastnext/lexilayer-translator/blob/main/src/background/google-translate-client.ts"
click node_bing "https://github.com/vastnext/lexilayer-translator/blob/main/src/background/bing-translate-client.ts"
click node_openai "https://github.com/vastnext/lexilayer-translator/blob/main/src/background/openai-client.ts"
click node_cache "https://github.com/vastnext/lexilayer-translator/blob/main/src/background/cache.ts"
click node_config "https://github.com/vastnext/lexilayer-translator/blob/main/src/shared/config.ts"
click node_experts "https://github.com/vastnext/lexilayer-translator/blob/main/src/shared/experts.ts"
click node_vocabulary_ui "https://github.com/vastnext/lexilayer-translator/blob/main/src/options/VocabularySection.tsx"
click node_vocabulary_storage "https://github.com/vastnext/lexilayer-translator/blob/main/src/background/vocabulary-storage.ts"
click node_anki "https://github.com/vastnext/lexilayer-translator/blob/main/src/background/anki-client.ts"
click node_youtube "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/youtube/session.ts"
click node_youtube_renderer "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/subtitles/renderer.ts"
click node_bilibili "https://github.com/vastnext/lexilayer-translator/blob/main/src/content/bilibili/index.ts"
click node_shared_vocabulary "https://github.com/vastnext/lexilayer-translator/blob/main/src/shared/vocabulary.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_settings,node_options_api,node_config,node_experts toneBlue
class node_content_entry,node_page_controller,node_scanner,node_site_rules,node_scheduler,node_paragraphs,node_renderers,node_selection toneAmber
class node_background,node_ai_coordinator,node_provider_registry,node_google,node_bing,node_openai,node_cache toneMint
class node_vocabulary_ui,node_vocabulary_storage,node_anki,node_shared_vocabulary toneRose
class node_youtube,node_youtube_renderer,node_bilibili,node_reader,node_external_services,node_anki_service toneIndigo
```

## Components

### Extension controls

- [Translation controls](https://github.com/vastnext/lexilayer-translator/blob/main/src/popup/PopupApp.tsx): `src/popup/PopupApp.tsx`
- [Engine settings](https://github.com/vastnext/lexilayer-translator/blob/main/src/options/OptionsApp.tsx): `src/options/OptionsApp.tsx`
- [Options API](https://github.com/vastnext/lexilayer-translator/blob/main/src/options/api.ts): `src/options/api.ts`
- [Shared configuration](https://github.com/vastnext/lexilayer-translator/blob/main/src/shared/config.ts): `src/shared/config.ts`
- [Expert prompts](https://github.com/vastnext/lexilayer-translator/blob/main/src/shared/experts.ts): `src/shared/experts.ts`

### Page translation

- [Content entry](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/main.ts): `src/content/main.ts`
- [Page controller](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/index.ts): `src/content/index.ts`
- [DOM scanner](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/dom-scanner.ts): `src/content/dom-scanner.ts`
- [Site rules](https://github.com/vastnext/lexilayer-translator/blob/main/src/rules/catalog.ts): `src/rules/catalog.ts`
- [Visible scheduler](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/scheduler.ts): `src/content/scheduler.ts`
- [Paragraph state](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/paragraph-store.ts): `src/content/paragraph-store.ts`
- [Page renderers](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/dom-renderer.ts): `src/content/dom-renderer.ts`
- [Selection translation](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/selection-controller.ts): `src/content/selection-controller.ts`

### Translation services

- [Message and task coordinator](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/index.ts): `src/background/index.ts`
- [AI coordinator](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/ai-coordinator.ts): `src/background/ai-coordinator.ts`
- [Provider registry](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/provider-registry.ts): `src/background/provider-registry.ts`
- [Google client](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/google-translate-client.ts): `src/background/google-translate-client.ts`
- [Bing client](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/bing-translate-client.ts): `src/background/bing-translate-client.ts`
- [OpenAI client](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/openai-client.ts): `src/background/openai-client.ts`
- [Translation cache](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/cache.ts): `src/background/cache.ts`

### Vocabulary

- [Vocabulary manager](https://github.com/vastnext/lexilayer-translator/blob/main/src/options/VocabularySection.tsx): `src/options/VocabularySection.tsx`
- [Vocabulary storage](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/vocabulary-storage.ts): `src/background/vocabulary-storage.ts`
- [Anki synchronization](https://github.com/vastnext/lexilayer-translator/blob/main/src/background/anki-client.ts): `src/background/anki-client.ts`
- [Vocabulary model](https://github.com/vastnext/lexilayer-translator/blob/main/src/shared/vocabulary.ts): `src/shared/vocabulary.ts`

### Video subtitles

- [YouTube subtitles](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/youtube/session.ts): `src/content/youtube/session.ts`
- [Subtitle renderer](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/subtitles/renderer.ts): `src/content/subtitles/renderer.ts`
- [Bilibili subtitles](https://github.com/vastnext/lexilayer-translator/blob/main/src/content/bilibili/index.ts): `src/content/bilibili/index.ts`

### Other components

- **Reader**
- **Translation providers**
- **AnkiConnect**

## Connections

- Reader → Translation controls: chooses translation (evidence: `README.md`)
- Translation controls → Content entry: commands page
- Reader → Engine settings: configures (evidence: `README.md`)
- Engine settings → Options API: updates settings (evidence: `src/options/OptionsApp.tsx`)
- Options API → Shared configuration: uses settings model (evidence: `src/options/api.ts`)
- Content entry → Page controller: assembles (evidence: `src/content/main.ts`)
- Page controller → DOM scanner: scans text (evidence: `src/content/main.ts`)
- Page controller → Site rules: loads site rule
- Page controller → Visible scheduler: schedules batches (evidence: `src/content/main.ts`)
- Page controller → Paragraph state: tracks paragraphs (evidence: `src/content/index.ts`)
- Page controller → Page renderers: renders results (evidence: `src/content/main.ts`)
- Visible scheduler → Page controller: runs worker (evidence: `src/content/main.ts`)
- Page controller → Message and task coordinator: requests translation
- Message and task coordinator → AI coordinator: coordinates AI (evidence: `src/background/index.ts`)
- Message and task coordinator → Provider registry: creates provider (evidence: `src/background/index.ts`)
- Provider registry → Google client: selects client
- Provider registry → Bing client: selects client
- Provider registry → OpenAI client: selects client
- Google client → Translation providers: sends translation
- Bing client → Translation providers: sends translation
- OpenAI client → Translation providers: sends translation
- Message and task coordinator → Translation cache: reads and writes (evidence: `src/background/index.ts`)
- Message and task coordinator → Shared configuration: loads preferences (evidence: `src/background/index.ts`)
- AI coordinator → Expert prompts: uses expert prompts
- Reader → Selection translation: selects text (evidence: `README.md`)
- Selection translation → Message and task coordinator: requests translation
- Reader → Vocabulary manager: manages saved words (evidence: `README.md`)
- Vocabulary manager → Vocabulary model: validates entries (evidence: `src/options/VocabularySection.tsx`)
- Message and task coordinator → Vocabulary storage: stores entries (evidence: `src/background/index.ts`)
- Vocabulary manager → Anki synchronization: starts sync (evidence: `src/options/VocabularySection.tsx`)
- Anki synchronization → AnkiConnect: sends notes
- YouTube subtitles → Message and task coordinator: requests translation
- YouTube subtitles → Subtitle renderer: renders subtitles
- Bilibili subtitles → Message and task coordinator: requests translation
- Bilibili subtitles → Subtitle renderer: renders subtitles
