# anujjha27/lattice architecture

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

- Interactive diagram: https://gitdiagram.com/anujjha27/lattice
- Repository: https://github.com/anujjha27/lattice

## Overview

#### Purpose and deployment

Lattice is a hosted personal learning platform: it maintains a user knowledge graph, ingests and retrieves sources, generates learning artifacts, and tracks review progress. It is a split application: Next.js web on Vercel, FastAPI plus worker on Render, and Supabase for PostgreSQL/pgvector, Auth, and private Storage.

Key runtime entry points are `apps/api/app/main.py` for the API, `apps/api/app/jobs/runner.py` for asynchronous work, and `apps/web/app/page.tsx` / `apps/web/app/layout.tsx` for the web application.

#### Web client and session boundary

The Next.js App Router frontend provides graph, library/discovery, pathway, review, and profile experiences. It uses Supabase browser/server clients for magic-link and Google OAuth; the callback route establishes the client session. The web calls the FastAPI service through a centralized API client and feature hooks.

Grounding paths:
- `apps/web/app/auth/callback/route.ts`
- `apps/web/lib/api.ts`
- `apps/web/lib/supabase/client.ts`
- `apps/web/hooks/useBrain.ts`

#### API composition, authentication, and persistence

FastAPI assembles domain routers and cross-cutting middleware. Protected requests carry Supabase JWTs; API-side auth verifies them and applies the production email allowlist. SQLAlchemy async models map the persistent learning domain, while Alembic owns schema evolution against Supabase Postgres.

Grounding paths:
- `apps/api/app/main.py`
- `apps/api/app/api.py`
- `apps/api/app/core/auth.py`
- `apps/api/app/db/session.py`
- `apps/api/alembic/versions/0001_baseline.py`

#### Knowledge graph and learner state

The central domain is a per-user concept graph with concepts, learning state, sources, and relationships. Graph construction and validation sit separately from API presentation. Brain endpoints expose this graph, while pathways turn graph state into sequenced learning plans. Reviews and quizzes write back learner progress/mastery signals.

Grounding paths:
- `apps/api/app/domain/graph.py`
- `apps/api/app/db/models/concept.py`
- `apps/api/app/db/models/learning.py`
- `apps/api/app/modules/brain/service.py`
- `apps/api/app/modules/pathways/generator.py`

#### Source ingestion and retrieval pipeline

Users submit URLs, notes, and potentially uploaded documents. The source subsystem classifies, extracts, deduplicates, ranks, chunks, and embeds content. Persisted source/chunk vectors support grounded lesson generation and retrieval; pgvector HNSW resides in Supabase. Discovery supplements user sources through web and academic providers.

Grounding paths:
- `apps/api/app/modules/sources/routes.py`
- `apps/api/app/modules/sources/extraction.py`
- `apps/api/app/modules/sources/chunking.py`
- `apps/api/app/modules/retrieval/routes.py`
- `apps/api/app/providers/embedding.py`

#### AI generation and external-provider boundary

Provider abstractions isolate model and search vendors from learning workflows. Gemini implements the primary LLM interface; Tavily, arXiv, and OpenAlex support discovery. Lessons use source context and citations; generated pathways, quizzes, recommendations, and the AI guide are exposed as module APIs rather than directly from the web client.

Grounding paths:
- `apps/api/app/providers/llm.py`
- `apps/api/app/providers/gemini.py`
- `apps/api/app/providers/factory.py`
- `apps/api/app/modules/lessons/generator.py`
- `apps/api/app/providers/search.py`

#### Background jobs and visual/profile assets

Long-running ingestion and portrait refresh work is persisted as jobs and processed by a Postgres-backed worker, separating API request latency from external calls and generation. Portraits combine learner analytics with visual assets; opt-in photos and uploads use a private Supabase Storage bucket. Visual-source ranking, rights handling, caching, and storage access form a separate asset boundary.

Grounding paths:
- `apps/api/app/jobs/queue.py`
- `apps/api/app/jobs/handlers.py`
- `apps/api/app/db/models/job.py`
- `apps/api/app/modules/portrait/service.py`
- `apps/api/app/modules/visual_sources/service.py`
- `apps/api/app/providers/storage.py`

Main entry points: [`apps/api/app/main.py`](https://github.com/anujjha27/lattice/blob/main/apps/api/app/main.py) (FastAPI application), [`apps/api/app/modules/sources/extraction.py`](https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/sources/extraction.py) (Source ingestion), [`apps/api/app/db/session.py`](https://github.com/anujjha27/lattice/blob/main/apps/api/app/db/session.py) (Postgres and pgvector), [`apps/api/app/modules/pathways/generator.py`](https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/pathways/generator.py) (Pathway generator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web client"]
  node_web_app["Next.js experiences<br/>App Router<br/>[page.tsx]"]
  node_web_session["Session callback<br/>OAuth callback<br/>[route.ts]"]
  node_web_supabase["Supabase clients<br/>browser/server auth<br/>[client.ts]"]
  node_web_api["API client and hooks<br/>frontend data layer<br/>[api.ts]"]
end

subgraph group_api["API and domain"]
  node_api_main["FastAPI application<br/>API composition<br/>[main.py]"]
  node_api_auth["JWT authorization<br/>auth boundary<br/>[auth.py]"]
  node_brain["Brain graph service<br/>domain module<br/>[service.py]"]
  node_concept_graph["Knowledge graph<br/>domain model<br/>[graph.py]"]
  node_pathways["Pathway generator<br/>learning planner<br/>[generator.py]"]
  node_lesson_generation["Lesson generation<br/>grounded generation<br/>[generator.py]"]
end

subgraph group_pipeline["Async content pipeline"]
  node_job_queue["Persistent job queue<br/>async jobs<br/>[queue.py]"]
  node_worker["Job handlers<br/>worker processing<br/>[handlers.py]"]
  node_source_ingestion["Source ingestion<br/>content pipeline<br/>[extraction.py]"]
  node_retrieval["Retrieval API<br/>semantic retrieval<br/>[routes.py]"]
  node_portrait_assets["Portrait and visual assets<br/>asset workflow<br/>[service.py]"]
end

subgraph group_providers["External providers"]
  node_llm_provider["LLM provider abstraction<br/>model boundary<br/>[llm.py]"]
  node_search_provider["Discovery providers<br/>search boundary<br/>[search.py]"]
  node_embedding_provider["Embedding provider<br/>vectorization boundary<br/>[embedding.py]"]
end

subgraph group_supabase["Supabase platform"]
  node_supabase_auth["Supabase Auth<br/>identity service"]
  node_postgres[("Postgres and pgvector<br/>persistent domain store<br/>[session.py]")]
  node_storage["Private Storage<br/>object storage<br/>[storage.py]"]
end

node_web_app -->|"sign-in callback"| node_web_session
node_web_session -->|"establishes session"| node_web_supabase
node_web_supabase -->|"OAuth and magic links"| node_supabase_auth
node_web_app -->|"uses"| node_web_api
node_web_api -->|"authenticated requests"| node_api_main
node_api_main -->|"protects routes"| node_api_auth
node_api_auth -->|"verifies JWT"| node_supabase_auth
node_api_main -->|"brain endpoints"| node_brain
node_brain -->|"reads and validates"| node_concept_graph
node_concept_graph -->|"persists learner graph"| node_postgres
node_pathways -->|"plans from graph state"| node_concept_graph
node_api_main -->|"pathway APIs"| node_pathways
node_api_main -->|"enqueues long work"| node_job_queue
node_job_queue -->|"stored in"| node_postgres
node_job_queue -->|"dispatches"| node_worker
node_worker -->|"runs"| node_source_ingestion
node_source_ingestion -->|"embeds chunks"| node_embedding_provider
node_source_ingestion -->|"stores sources and vectors"| node_postgres
node_retrieval -->|"vector search"| node_postgres
node_lesson_generation -->|"grounds on context"| node_retrieval
node_lesson_generation -->|"generates artifacts"| node_llm_provider
node_pathways -->|"generates plans"| node_llm_provider
node_source_ingestion -->|"discovers sources"| node_search_provider
node_worker -->|"refreshes portraits"| node_portrait_assets
node_portrait_assets -->|"reads and writes assets"| node_storage

click node_web_app "https://github.com/anujjha27/lattice/blob/main/apps/web/app/page.tsx"
click node_web_session "https://github.com/anujjha27/lattice/blob/main/apps/web/app/auth/callback/route.ts"
click node_web_supabase "https://github.com/anujjha27/lattice/blob/main/apps/web/lib/supabase/client.ts"
click node_web_api "https://github.com/anujjha27/lattice/blob/main/apps/web/lib/api.ts"
click node_api_main "https://github.com/anujjha27/lattice/blob/main/apps/api/app/main.py"
click node_api_auth "https://github.com/anujjha27/lattice/blob/main/apps/api/app/core/auth.py"
click node_brain "https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/brain/service.py"
click node_concept_graph "https://github.com/anujjha27/lattice/blob/main/apps/api/app/domain/graph.py"
click node_pathways "https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/pathways/generator.py"
click node_lesson_generation "https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/lessons/generator.py"
click node_job_queue "https://github.com/anujjha27/lattice/blob/main/apps/api/app/jobs/queue.py"
click node_worker "https://github.com/anujjha27/lattice/blob/main/apps/api/app/jobs/handlers.py"
click node_source_ingestion "https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/sources/extraction.py"
click node_retrieval "https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/retrieval/routes.py"
click node_portrait_assets "https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/portrait/service.py"
click node_llm_provider "https://github.com/anujjha27/lattice/blob/main/apps/api/app/providers/llm.py"
click node_search_provider "https://github.com/anujjha27/lattice/blob/main/apps/api/app/providers/search.py"
click node_embedding_provider "https://github.com/anujjha27/lattice/blob/main/apps/api/app/providers/embedding.py"
click node_postgres "https://github.com/anujjha27/lattice/blob/main/apps/api/app/db/session.py"
click node_storage "https://github.com/anujjha27/lattice/blob/main/apps/api/app/providers/storage.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_app,node_web_session,node_web_supabase,node_web_api toneBlue
class node_api_main,node_api_auth,node_brain,node_concept_graph,node_pathways,node_lesson_generation toneAmber
class node_job_queue,node_worker,node_source_ingestion,node_retrieval,node_portrait_assets toneMint
class node_llm_provider,node_search_provider,node_embedding_provider toneRose
class node_supabase_auth,node_postgres,node_storage toneIndigo
```

## Components

### Web client

Next.js on Vercel.

- [Next.js experiences](https://github.com/anujjha27/lattice/blob/main/apps/web/app/page.tsx): App Router · Graph, library, pathway, review, and profile UI. · `apps/web/app/page.tsx`
- [Session callback](https://github.com/anujjha27/lattice/blob/main/apps/web/app/auth/callback/route.ts): OAuth callback · Establishes magic-link or Google client session. · `apps/web/app/auth/callback/route.ts`
- [Supabase clients](https://github.com/anujjha27/lattice/blob/main/apps/web/lib/supabase/client.ts): browser/server auth · `apps/web/lib/supabase/client.ts`
- [API client and hooks](https://github.com/anujjha27/lattice/blob/main/apps/web/lib/api.ts): frontend data layer · `apps/web/lib/api.ts`

### API and domain

FastAPI on Render.

- [FastAPI application](https://github.com/anujjha27/lattice/blob/main/apps/api/app/main.py): API composition · Assembles routers and middleware. · `apps/api/app/main.py`
- [JWT authorization](https://github.com/anujjha27/lattice/blob/main/apps/api/app/core/auth.py): auth boundary · Verifies Supabase JWTs and enforces the email allowlist. · `apps/api/app/core/auth.py`
- [Brain graph service](https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/brain/service.py): domain module · Exposes each learner's concept graph. · `apps/api/app/modules/brain/service.py`
- [Knowledge graph](https://github.com/anujjha27/lattice/blob/main/apps/api/app/domain/graph.py): domain model · Concepts, relationships, sources, and learner state. · `apps/api/app/domain/graph.py`
- [Pathway generator](https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/pathways/generator.py): learning planner · Sequences graph state into learning plans. · `apps/api/app/modules/pathways/generator.py`
- [Lesson generation](https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/lessons/generator.py): grounded generation · Creates cited learning artifacts from retrieved context. · `apps/api/app/modules/lessons/generator.py`

### Async content pipeline

Postgres-backed worker processes long-running work.

- [Persistent job queue](https://github.com/anujjha27/lattice/blob/main/apps/api/app/jobs/queue.py): async jobs · Stores ingestion and portrait-refresh work. · `apps/api/app/jobs/queue.py`
- [Job handlers](https://github.com/anujjha27/lattice/blob/main/apps/api/app/jobs/handlers.py): worker processing · Runs external calls and generation outside requests. · `apps/api/app/jobs/handlers.py`
- [Source ingestion](https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/sources/extraction.py): content pipeline · Classifies, extracts, deduplicates, ranks, and chunks submitted content. · `apps/api/app/modules/sources/extraction.py`
- [Retrieval API](https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/retrieval/routes.py): semantic retrieval · Queries persisted source and chunk vectors. · `apps/api/app/modules/retrieval/routes.py`
- [Portrait and visual assets](https://github.com/anujjha27/lattice/blob/main/apps/api/app/modules/portrait/service.py): asset workflow · Combines learner analytics, visual ranking, rights handling, and cached assets. · `apps/api/app/modules/portrait/service.py`

### External providers

- [LLM provider abstraction](https://github.com/anujjha27/lattice/blob/main/apps/api/app/providers/llm.py): model boundary · Gemini is the primary implementation. · `apps/api/app/providers/llm.py`
- [Discovery providers](https://github.com/anujjha27/lattice/blob/main/apps/api/app/providers/search.py): search boundary · Tavily, arXiv, and OpenAlex supplement user sources. · `apps/api/app/providers/search.py`
- [Embedding provider](https://github.com/anujjha27/lattice/blob/main/apps/api/app/providers/embedding.py): vectorization boundary · `apps/api/app/providers/embedding.py`

### Supabase platform

Postgres with pgvector, Auth, and private Storage.

- **Supabase Auth**: identity service
- [Postgres and pgvector](https://github.com/anujjha27/lattice/blob/main/apps/api/app/db/session.py): persistent domain store · Stores learning domain, jobs, source chunks, and HNSW vectors. · `apps/api/app/db/session.py`
- [Private Storage](https://github.com/anujjha27/lattice/blob/main/apps/api/app/providers/storage.py): object storage · Holds opt-in photos and uploads. · `apps/api/app/providers/storage.py`

## Connections

- Next.js experiences → Session callback: sign-in callback
- Session callback → Supabase clients: establishes session
- Supabase clients → Supabase Auth: OAuth and magic links
- Next.js experiences → API client and hooks: uses
- API client and hooks → FastAPI application: authenticated requests
- FastAPI application → JWT authorization: protects routes
- JWT authorization → Supabase Auth: verifies JWT
- FastAPI application → Brain graph service: brain endpoints
- Brain graph service → Knowledge graph: reads and validates
- Knowledge graph → Postgres and pgvector: persists learner graph
- Pathway generator → Knowledge graph: plans from graph state
- FastAPI application → Pathway generator: pathway APIs
- FastAPI application → Persistent job queue: enqueues long work
- Persistent job queue → Postgres and pgvector: stored in
- Persistent job queue → Job handlers: dispatches
- Job handlers → Source ingestion: runs
- Source ingestion → Embedding provider: embeds chunks
- Source ingestion → Postgres and pgvector: stores sources and vectors
- Retrieval API → Postgres and pgvector: vector search
- Lesson generation → Retrieval API: grounds on context
- Lesson generation → LLM provider abstraction: generates artifacts
- Pathway generator → LLM provider abstraction: generates plans
- Source ingestion → Discovery providers: discovers sources
- Job handlers → Portrait and visual assets: refreshes portraits
- Portrait and visual assets → Private Storage: reads and writes assets
