# kuju29/textphantomocr_overlay architecture

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

- Interactive diagram: https://gitdiagram.com/kuju29/textphantomocr_overlay
- Repository: https://github.com/kuju29/textphantomocr_overlay

## Overview

TextPhantomOCR Overlay is a browser extension backed by a Python API for translating text in manga images. The documented user flow starts with right-clicking one image or an empty page area; translation then returns an overlay. The repository also contains AI-provider routing, Lens OCR, detector-independent grouping, and image-processing stages. The README establishes the user-facing flow, while the source index confirms selected internal relationships; unsampled route and extension wiring is represented only where documented or indexed.

Main entry points: [`api/backend/application/lens_service.py`](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/application/lens_service.py) (Lens service), [`api/backend/main.py`](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/main.py) (FastAPI app), [`src/content/overlay/message-controller.js`](https://github.com/kuju29/textphantomocr_overlay/blob/main/src/content/overlay/message-controller.js) (Image actions), [`src/background/pipeline/image-routing.js`](https://github.com/kuju29/textphantomocr_overlay/blob/main/src/background/pipeline/image-routing.js) (Background pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_extension["Browser extension"]
  node_content["Image actions"]
  node_reader["Reader runtime<br/>[runtime.js]"]
  node_background["Background pipeline<br/>[image-routing.js]"]
  node_transport["API transport<br/>[server.js]"]
  node_profile["AI profiles<br/>[ai-profiles.js]"]
  node_storage[("Extension storage<br/>[storage.js]")]
end

subgraph group_api["API and jobs"]
  node_api_entry["FastAPI app<br/>[main.py]"]
  node_translate_routes["Translation routes<br/>[translate.py]"]
  node_lens_routes["Lens routes<br/>[lens_v1.py]"]
  node_jobs["Job orchestration<br/>[pipeline.py]"]
end

subgraph group_ai["AI translation"]
  node_ai_request["AI request handling<br/>[orchestration.py]"]
  node_provider["OpenRouter provider"]
  node_ai_transport["OpenRouter transport<br/>[openrouter_chat.py]"]
end

subgraph group_vision["OCR and rendering"]
  node_lens_service["Lens service<br/>[lens_service.py]"]
  node_lens_client["Lens OCR client<br/>[client.py]"]
  node_grouping["Text grouping"]
  node_partition["Graph partition<br/>[pipeline.py]"]
  node_render["Overlay rendering<br/>[translated.py]"]
end

node_user(("Reader"))
node_external_ai{{"AI provider"}}

node_user -->|"selects image"| node_content
node_user -->|"starts page translation"| node_reader
node_api_entry -->|"registers route"| node_translate_routes
node_api_entry -->|"registers route"| node_lens_routes
node_jobs -.->|"imports service"| node_lens_service
node_lens_service -->|"uses OCR client"| node_lens_client
node_lens_service -->|"uses grouping"| node_grouping
node_grouping -->|"partitions graph"| node_partition
node_ai_request -.->|"resolves provider"| node_provider
node_provider -->|"uses transport"| node_ai_transport
node_ai_transport -.->|"sends request"| node_external_ai
node_profile -->|"reads and writes"| node_storage
node_transport -.->|"sends API request"| node_external_ai
node_content -.->|"requests translation"| node_background
node_background -.->|"dispatches request"| node_transport
node_render -.->|"returns overlay"| node_reader

click node_content "https://github.com/kuju29/textphantomocr_overlay/blob/main/src/content/overlay/message-controller.js"
click node_reader "https://github.com/kuju29/textphantomocr_overlay/blob/main/src/content/reader/runtime.js"
click node_background "https://github.com/kuju29/textphantomocr_overlay/blob/main/src/background/pipeline/image-routing.js"
click node_transport "https://github.com/kuju29/textphantomocr_overlay/blob/main/src/background/ai/transports/server.js"
click node_profile "https://github.com/kuju29/textphantomocr_overlay/blob/main/src/shared/ai-profiles.js"
click node_storage "https://github.com/kuju29/textphantomocr_overlay/blob/main/src/shared/storage.js"
click node_api_entry "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/main.py"
click node_translate_routes "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/api/routes/translate.py"
click node_lens_routes "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/api/routes/lens_v1.py"
click node_jobs "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/jobs/pipeline.py"
click node_lens_service "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/application/lens_service.py"
click node_lens_client "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/lens/client.py"
click node_grouping "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/grouping/detector_free_service.py"
click node_partition "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/render/lens_graph_partition/pipeline.py"
click node_render "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/render/html/translated.py"
click node_ai_request "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/application/ai_translation/orchestration.py"
click node_provider "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/ai/providers/cloud_openrouter.py"
click node_ai_transport "https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/ai/transports/openrouter_chat.py"

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_content,node_reader,node_background,node_transport,node_profile,node_storage toneBlue
class node_api_entry,node_translate_routes,node_lens_routes,node_jobs toneAmber
class node_ai_request,node_provider,node_ai_transport toneMint
class node_lens_service,node_lens_client,node_grouping,node_partition,node_render toneRose
class node_user,node_external_ai toneIndigo
```

## Components

### Browser extension

- [Image actions](https://github.com/kuju29/textphantomocr_overlay/blob/main/src/content/overlay/message-controller.js): `src/content/overlay/message-controller.js`
- [Reader runtime](https://github.com/kuju29/textphantomocr_overlay/blob/main/src/content/reader/runtime.js): `src/content/reader/runtime.js`
- [Background pipeline](https://github.com/kuju29/textphantomocr_overlay/blob/main/src/background/pipeline/image-routing.js): `src/background/pipeline/image-routing.js`
- [API transport](https://github.com/kuju29/textphantomocr_overlay/blob/main/src/background/ai/transports/server.js): `src/background/ai/transports/server.js`
- [AI profiles](https://github.com/kuju29/textphantomocr_overlay/blob/main/src/shared/ai-profiles.js): `src/shared/ai-profiles.js`
- [Extension storage](https://github.com/kuju29/textphantomocr_overlay/blob/main/src/shared/storage.js): `src/shared/storage.js`

### API and jobs

- [FastAPI app](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/main.py): `api/backend/main.py`
- [Translation routes](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/api/routes/translate.py): `api/backend/api/routes/translate.py`
- [Lens routes](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/api/routes/lens_v1.py): `api/backend/api/routes/lens_v1.py`
- [Job orchestration](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/jobs/pipeline.py): `api/backend/jobs/pipeline.py`

### AI translation

- [AI request handling](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/application/ai_translation/orchestration.py): `api/backend/application/ai_translation/orchestration.py`
- [OpenRouter provider](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/ai/providers/cloud_openrouter.py): `api/backend/ai/providers/cloud_openrouter.py`
- [OpenRouter transport](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/ai/transports/openrouter_chat.py): `api/backend/ai/transports/openrouter_chat.py`

### OCR and rendering

- [Lens service](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/application/lens_service.py): `api/backend/application/lens_service.py`
- [Lens OCR client](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/lens/client.py): `api/backend/lens/client.py`
- [Text grouping](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/grouping/detector_free_service.py): `api/backend/grouping/detector_free_service.py`
- [Graph partition](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/render/lens_graph_partition/pipeline.py): `api/backend/render/lens_graph_partition/pipeline.py`
- [Overlay rendering](https://github.com/kuju29/textphantomocr_overlay/blob/main/api/backend/render/html/translated.py): `api/backend/render/html/translated.py`

### Other components

- **Reader**
- **AI provider**

## Connections

- Reader → Image actions: selects image (evidence: `README.md`)
- Reader → Reader runtime: starts page translation (evidence: `README.md`)
- FastAPI app → Translation routes: registers route (evidence: `api/backend/main.py`)
- FastAPI app → Lens routes: registers route (evidence: `api/backend/main.py`)
- Job orchestration → Lens service: imports service
- Lens service → Lens OCR client: uses OCR client (evidence: `api/backend/application/lens_service.py`)
- Lens service → Text grouping: uses grouping (evidence: `api/backend/application/lens_service.py`)
- Text grouping → Graph partition: partitions graph (evidence: `api/backend/grouping/detector_free_service.py`)
- AI request handling → OpenRouter provider: resolves provider
- OpenRouter provider → OpenRouter transport: uses transport (evidence: `api/backend/ai/providers/cloud_openrouter.py`)
- OpenRouter transport → AI provider: sends request
- AI profiles → Extension storage: reads and writes (evidence: `src/shared/ai-profile-storage.js`)
- API transport → AI provider: sends API request
- Image actions → Background pipeline: requests translation
- Background pipeline → API transport: dispatches request
- Overlay rendering → Reader runtime: returns overlay
