# kylehton/labl architecture

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

- Interactive diagram: https://gitdiagram.com/kylehton/labl
- Repository: https://github.com/kylehton/labl

## Overview

Labl manages Gmail labeling: users authenticate, create labels, seed them with example emails, then fetch inbox messages for semantic matching and direct labels or suggestions. The backend separates API orchestration, Gmail access, embedding/matching, and persistence; label-record history supports review workflows. The frontend exposes landing and dashboard experiences. Some routes and UI details are represented from README capabilities and the source index; links are limited to documented or excerpted relationships.

Main entry points: [`server/ml/pipeline.py`](https://github.com/kylehton/labl/blob/main/server/ml/pipeline.py) (Labeling pipeline), [`server/tasks/labeling.py`](https://github.com/kylehton/labl/blob/main/server/tasks/labeling.py) (Inbox labeling task), [`server/app/repositories/users.py`](https://github.com/kylehton/labl/blob/main/server/app/repositories/users.py) (User repository), [`server/app/api/gmail.py`](https://github.com/kylehton/labl/blob/main/server/app/api/gmail.py) (Gmail routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Web client"]
  node_web["Next.js app<br/>[layout.tsx]"]
  node_auth_ui["Auth context<br/>[AuthContext.tsx]"]
  node_landing["Landing page<br/>[page.tsx]"]
  node_dashboard["Inbox dashboard<br/>[page.tsx]"]
end

subgraph group_api["API workflows"]
  node_auth_api["OAuth routes<br/>[auth.py]"]
  node_user_api["User routes<br/>[user.py]"]
  node_gmail_api["Gmail routes<br/>[gmail.py]"]
  node_records_api["Label history routes<br/>[label_records.py]"]
  node_app_entry["FastAPI entry<br/>[main.py]"]
  node_gmail_client["Gmail client<br/>[gmail.py]"]
end

subgraph group_ml["Labeling engine"]
  node_pipeline["Labeling pipeline<br/>[pipeline.py]"]
  node_embeddings["Text embeddings<br/>[embeddings.py]"]
  node_centroid["Centroid matching<br/>[centroid.py]"]
  node_similarity["Similarity scoring<br/>[similarity.py]"]
  node_preset_labels["Preset labels<br/>[preset_labels.py]"]
end

subgraph group_storage["Persistence and sessions"]
  node_users_repo["User repository<br/>[users.py]"]
  node_label_repo["Label history store<br/>[label_records.py]"]
  node_presets_repo["Preset repository<br/>[presets.py]"]
  node_label_model["Label model<br/>[label.py]"]
  node_db["MongoDB access<br/>[db.py]"]
  node_session["Session service<br/>[session.py]"]
  node_session_auth["Session guard<br/>[session_auth.py]"]
  node_crypto["Token encryption<br/>[crypto.py]"]
end

subgraph group_jobs["Background processing"]
  node_celery["Celery setup<br/>[celery_app.py]"]
  node_label_task["Inbox labeling task<br/>[labeling.py]"]
end

node_user(("User"))
node_mongo[("MongoDB")]
node_gmail["Gmail service"]

node_user -->|"visits"| node_landing
node_user -->|"uses"| node_dashboard
node_web -->|"provides"| node_auth_ui
node_dashboard -->|"uses"| node_auth_ui
node_landing -->|"uses"| node_auth_ui
node_auth_ui -->|"checks session"| node_auth_api
node_auth_ui -->|"starts login"| node_auth_api
node_user -->|"authenticates"| node_auth_api
node_auth_api -->|"creates session"| node_session
node_session -->|"upserts user"| node_users_repo
node_session -->|"encrypts token"| node_crypto
node_session -->|"stores session"| node_db
node_session_auth -->|"loads session"| node_session
node_app_entry -->|"registers routes"| node_auth_api
node_app_entry -->|"registers routes"| node_user_api
node_app_entry -->|"registers routes"| node_gmail_api
node_app_entry -->|"registers routes"| node_records_api
node_user_api -->|"reads and updates"| node_users_repo
node_user_api -->|"seeds labels"| node_pipeline
node_gmail_api -->|"fetches mail"| node_gmail_client
node_gmail_api -->|"enqueues labeling"| node_label_task
node_gmail_api -->|"loads user labels"| node_users_repo
node_label_task -->|"labels messages"| node_pipeline
node_label_task -->|"fetches and applies"| node_gmail_client
node_label_task -->|"updates sync state"| node_users_repo
node_label_task -->|"records outcomes"| node_label_repo
node_gmail_api -->|"seeds and scores"| node_pipeline
node_pipeline -->|"embeds text"| node_embeddings
node_pipeline -->|"computes centroids"| node_centroid
node_pipeline -->|"scores matches"| node_similarity
node_pipeline -->|"uses labels"| node_label_model
node_users_repo -->|"loads defaults"| node_presets_repo
node_presets_repo -->|"seeds preset data"| node_preset_labels
node_presets_repo -->|"stores presets"| node_db
node_users_repo -->|"stores user data"| node_db
node_label_repo -->|"stores history"| node_db
node_records_api -->|"reads and updates"| node_label_repo
node_db -->|"reads and writes"| node_mongo
node_gmail_client -->|"calls Gmail API"| node_gmail
node_label_task -->|"runs as task"| node_celery

click node_web "https://github.com/kylehton/labl/blob/main/client/app/layout.tsx"
click node_auth_ui "https://github.com/kylehton/labl/blob/main/client/app/context/AuthContext.tsx"
click node_landing "https://github.com/kylehton/labl/blob/main/client/app/landing-page/page.tsx"
click node_dashboard "https://github.com/kylehton/labl/blob/main/client/app/dashboard/page.tsx"
click node_auth_api "https://github.com/kylehton/labl/blob/main/server/app/api/auth.py"
click node_user_api "https://github.com/kylehton/labl/blob/main/server/app/api/user.py"
click node_gmail_api "https://github.com/kylehton/labl/blob/main/server/app/api/gmail.py"
click node_records_api "https://github.com/kylehton/labl/blob/main/server/app/api/label_records.py"
click node_app_entry "https://github.com/kylehton/labl/blob/main/server/app/main.py"
click node_gmail_client "https://github.com/kylehton/labl/blob/main/server/config/gmail.py"
click node_pipeline "https://github.com/kylehton/labl/blob/main/server/ml/pipeline.py"
click node_embeddings "https://github.com/kylehton/labl/blob/main/server/ml/embeddings.py"
click node_centroid "https://github.com/kylehton/labl/blob/main/server/ml/centroid.py"
click node_similarity "https://github.com/kylehton/labl/blob/main/server/ml/similarity.py"
click node_preset_labels "https://github.com/kylehton/labl/blob/main/server/ml/preset_labels.py"
click node_users_repo "https://github.com/kylehton/labl/blob/main/server/app/repositories/users.py"
click node_label_repo "https://github.com/kylehton/labl/blob/main/server/app/repositories/label_records.py"
click node_presets_repo "https://github.com/kylehton/labl/blob/main/server/app/repositories/presets.py"
click node_label_model "https://github.com/kylehton/labl/blob/main/server/app/models/label.py"
click node_db "https://github.com/kylehton/labl/blob/main/server/config/db.py"
click node_session "https://github.com/kylehton/labl/blob/main/server/config/session.py"
click node_session_auth "https://github.com/kylehton/labl/blob/main/server/dependencies/session_auth.py"
click node_crypto "https://github.com/kylehton/labl/blob/main/server/config/crypto.py"
click node_celery "https://github.com/kylehton/labl/blob/main/server/config/celery_app.py"
click node_label_task "https://github.com/kylehton/labl/blob/main/server/tasks/labeling.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_web,node_auth_ui,node_landing,node_dashboard,node_user toneBlue
class node_auth_api,node_user_api,node_gmail_api,node_records_api,node_app_entry,node_gmail_client,node_mongo toneAmber
class node_pipeline,node_embeddings,node_centroid,node_similarity,node_preset_labels toneMint
class node_users_repo,node_label_repo,node_presets_repo,node_label_model,node_db,node_session,node_session_auth,node_crypto toneRose
class node_celery,node_label_task,node_gmail toneIndigo
```

## Components

### Web client

- [Next.js app](https://github.com/kylehton/labl/blob/main/client/app/layout.tsx): `client/app/layout.tsx`
- [Auth context](https://github.com/kylehton/labl/blob/main/client/app/context/AuthContext.tsx): `client/app/context/AuthContext.tsx`
- [Landing page](https://github.com/kylehton/labl/blob/main/client/app/landing-page/page.tsx): `client/app/landing-page/page.tsx`
- [Inbox dashboard](https://github.com/kylehton/labl/blob/main/client/app/dashboard/page.tsx): `client/app/dashboard/page.tsx`

### API workflows

- [OAuth routes](https://github.com/kylehton/labl/blob/main/server/app/api/auth.py): `server/app/api/auth.py`
- [User routes](https://github.com/kylehton/labl/blob/main/server/app/api/user.py): `server/app/api/user.py`
- [Gmail routes](https://github.com/kylehton/labl/blob/main/server/app/api/gmail.py): `server/app/api/gmail.py`
- [Label history routes](https://github.com/kylehton/labl/blob/main/server/app/api/label_records.py): `server/app/api/label_records.py`
- [FastAPI entry](https://github.com/kylehton/labl/blob/main/server/app/main.py): `server/app/main.py`
- [Gmail client](https://github.com/kylehton/labl/blob/main/server/config/gmail.py): `server/config/gmail.py`

### Labeling engine

- [Labeling pipeline](https://github.com/kylehton/labl/blob/main/server/ml/pipeline.py): `server/ml/pipeline.py`
- [Text embeddings](https://github.com/kylehton/labl/blob/main/server/ml/embeddings.py): `server/ml/embeddings.py`
- [Centroid matching](https://github.com/kylehton/labl/blob/main/server/ml/centroid.py): `server/ml/centroid.py`
- [Similarity scoring](https://github.com/kylehton/labl/blob/main/server/ml/similarity.py): `server/ml/similarity.py`
- [Preset labels](https://github.com/kylehton/labl/blob/main/server/ml/preset_labels.py): `server/ml/preset_labels.py`

### Persistence and sessions

- [User repository](https://github.com/kylehton/labl/blob/main/server/app/repositories/users.py): `server/app/repositories/users.py`
- [Label history store](https://github.com/kylehton/labl/blob/main/server/app/repositories/label_records.py): `server/app/repositories/label_records.py`
- [Preset repository](https://github.com/kylehton/labl/blob/main/server/app/repositories/presets.py): `server/app/repositories/presets.py`
- [Label model](https://github.com/kylehton/labl/blob/main/server/app/models/label.py): `server/app/models/label.py`
- [MongoDB access](https://github.com/kylehton/labl/blob/main/server/config/db.py): `server/config/db.py`
- [Session service](https://github.com/kylehton/labl/blob/main/server/config/session.py): `server/config/session.py`
- [Session guard](https://github.com/kylehton/labl/blob/main/server/dependencies/session_auth.py): `server/dependencies/session_auth.py`
- [Token encryption](https://github.com/kylehton/labl/blob/main/server/config/crypto.py): `server/config/crypto.py`

### Background processing

- [Celery setup](https://github.com/kylehton/labl/blob/main/server/config/celery_app.py): `server/config/celery_app.py`
- [Inbox labeling task](https://github.com/kylehton/labl/blob/main/server/tasks/labeling.py): `server/tasks/labeling.py`

### Other components

- **User**
- **MongoDB**
- **Gmail service**

## Connections

- User → Landing page: visits
- User → Inbox dashboard: uses
- Next.js app → Auth context: provides (evidence: `client/app/layout.tsx`)
- Inbox dashboard → Auth context: uses (evidence: `client/app/dashboard/page.tsx`)
- Landing page → Auth context: uses (evidence: `client/app/landing-page/page.tsx`)
- Auth context → OAuth routes: checks session (evidence: `client/app/context/AuthContext.tsx`)
- Auth context → OAuth routes: starts login (evidence: `client/app/context/AuthContext.tsx`)
- User → OAuth routes: authenticates (evidence: `README.md`)
- OAuth routes → Session service: creates session (evidence: `server/app/api/auth.py`)
- Session service → User repository: upserts user (evidence: `server/config/session.py`)
- Session service → Token encryption: encrypts token (evidence: `server/config/session.py`)
- Session service → MongoDB access: stores session (evidence: `server/config/session.py`)
- Session guard → Session service: loads session (evidence: `server/dependencies/session_auth.py`)
- FastAPI entry → OAuth routes: registers routes (evidence: `server/app/main.py`)
- FastAPI entry → User routes: registers routes (evidence: `server/app/main.py`)
- FastAPI entry → Gmail routes: registers routes (evidence: `server/app/main.py`)
- FastAPI entry → Label history routes: registers routes (evidence: `server/app/main.py`)
- User routes → User repository: reads and updates (evidence: `server/app/api/user.py`)
- User routes → Labeling pipeline: seeds labels (evidence: `server/app/api/user.py`)
- Gmail routes → Gmail client: fetches mail (evidence: `server/app/api/gmail.py`)
- Gmail routes → Inbox labeling task: enqueues labeling (evidence: `server/tasks/labeling.py`)
- Gmail routes → User repository: loads user labels (evidence: `server/app/api/gmail.py`)
- Inbox labeling task → Labeling pipeline: labels messages (evidence: `server/tasks/labeling.py`)
- Inbox labeling task → Gmail client: fetches and applies (evidence: `server/tasks/labeling.py`)
- Inbox labeling task → User repository: updates sync state (evidence: `server/tasks/labeling.py`)
- Inbox labeling task → Label history store: records outcomes (evidence: `server/tasks/labeling.py`)
- Gmail routes → Labeling pipeline: seeds and scores (evidence: `server/app/api/gmail.py`)
- Labeling pipeline → Text embeddings: embeds text (evidence: `server/ml/pipeline.py`)
- Labeling pipeline → Centroid matching: computes centroids (evidence: `server/ml/pipeline.py`)
- Labeling pipeline → Similarity scoring: scores matches (evidence: `server/ml/pipeline.py`)
- Labeling pipeline → Label model: uses labels (evidence: `server/ml/pipeline.py`)
- User repository → Preset repository: loads defaults (evidence: `server/app/repositories/users.py`)
- Preset repository → Preset labels: seeds preset data (evidence: `server/app/repositories/presets.py`)
- Preset repository → MongoDB access: stores presets (evidence: `server/app/repositories/presets.py`)
- User repository → MongoDB access: stores user data (evidence: `server/app/repositories/users.py`)
- Label history store → MongoDB access: stores history (evidence: `server/app/repositories/label_records.py`)
- Label history routes → Label history store: reads and updates (evidence: `server/app/api/label_records.py`)
- MongoDB access → MongoDB: reads and writes (evidence: `server/config/db.py`)
- Gmail client → Gmail service: calls Gmail API (evidence: `README.md`)
- Inbox labeling task → Celery setup: runs as task (evidence: `server/tasks/labeling.py`)
