# hjj4woer/gitdiagram architecture

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

- Interactive diagram: https://gitdiagram.com/hjj4woer/gitdiagram
- Repository: https://github.com/hjj4woer/gitdiagram

## Overview

1. **This is a split-stack app, not a single monolith.**  
   The user-facing product is a Next.js app in `src/`, while the long-lived diagram generation backend can run either as:
   - **Next.js Route Handlers** in the same repo, or
   - a separate **FastAPI service** under `backend/` for production parity.  
   The active path is selected by environment, so architecture diagrams should show both backends as interchangeable generation tiers.

2. **The frontend is responsible for UX, repo selection, and rendering.**  
   Key UI entry points live in `src/app/`:
   - homepage and repo pages
   - browse catalog
   - sponsor/private repo flows
   - API routes for generation, preview, health, and browse index  
   Most UI state is in client components and hooks under `src/components/` and `src/hooks/`, especially the diagram streaming/export hooks.

3. **The diagram feature is split into a client-facing feature layer and server-only generation logic.**  
   `src/features/diagram/` contains shared client-side types, SSE helpers, API wrappers, cost helpers, and GitHub URL parsing.  
   `src/server/generate/` contains the actual generation pipeline: fetching GitHub data, prompting the model, building graphs, validating Mermaid, pricing/cost estimation, and session/audit handling.  
   This separation is important: the browser never directly talks to GitHub or the model; it talks to the app’s generation API.

4. **The core input is a GitHub repository snapshot.**  
   Both backends assemble the same conceptual payload:
   - repo metadata and default branch
   - recursive file tree
   - README content
   - filtered repository structure with noisy folders/assets removed  
   GitHub access is implemented in `src/server/generate/github.ts` on the Next side and `backend/app/services/github_service.py` on the FastAPI side. Private repositories are supported via user-supplied GitHub token handling in the app.

5. **Generation is a streamed, multi-step pipeline.**  
   The app does not just ask the model for a diagram in one shot. The README and file tree are fed into a pipeline that:
   - produces a plain-English architecture explanation
   - turns that explanation + file tree into a structured graph of systems/nodes/edges
   - validates graph paths against the real repository tree
   - converts the graph to Mermaid
   - validates the Mermaid output
   - retries with feedback if validation fails  
   Streaming is exposed through SSE-style hooks and routes, so the UI can show progress while the backend works.

6. **The model layer is provider-abstracted.**  
   Model/provider selection is centralized in server-side config:
   - OpenAI is the default server-side provider
   - OpenRouter and Atlas Cloud are supported alternatives
   - the backend model name is environment-driven  
   The app also distinguishes between server-side generation and browser-supplied keys, which affects cost and quota handling.

7. **Mermaid validation is a notable cross-runtime boundary.**  
   Mermaid output is validated before display in both backends, but the implementation differs:
   - Next backend validates in-process in `src/server/generate/mermaid-validator.ts`
   - FastAPI backend calls a Bun-based validator wrapper in `backend/scripts/validate_mermaid.mjs` backed by `backend/lib/mermaid-validator.ts`  
   This is a key architecture detail because the Railway backend intentionally mixes Python for API orchestration with Bun/Node for Mermaid validation.

8. **Persistence is externalized and split by concern.**  
   There is no database. State is stored in:
   - **Cloudflare R2** for successful diagram artifacts and repo-page/cache-like persisted outputs
   - **Upstash Redis** for complimentary quota state and terminal failure summaries
   - repo-specific/private-token namespaces for private repo persistence  
   The storage layer lives under `src/server/storage/` on the Next side, with analogous backend services in `backend/app/services/`.

9. **The app has a browse/catalog path separate from live generation.**  
   `src/app/browse/` and `src/features/browse/` serve precomputed or indexed diagrams for discovery.  
   That path reads from stored browse diagram data and supports hover previews, so it is mostly a read-only subsystem compared to the live generation pipeline.

10. **Export and presentation are first-class UI concerns.**  
    Generated diagrams are displayed with Mermaid rendering plus toolbar actions for:
    - copy Mermaid
    - download/export image
    - open source links from graph nodes  
    The graph includes real repo paths so components can deep-link back to GitHub files/directories, which is a core user experience boundary between visualization and source navigation.

11. **Complimentary quota and cost estimation are enforced server-side.**  
    The generation pipeline includes a complimentary-gate and pricing/cost-estimate layer.  
    These guardrails live in both implementations (`src/server/generate/complimentary-gate.ts`, `pricing.ts`, `cost-estimate.ts`, and backend equivalents), meaning the system controls free usage before invoking expensive model work.

12. **Deployment is intentionally split across platforms.**  
    The intended production setup is:
    - **Vercel** for the Next.js frontend
    - **Railway** for the FastAPI backend
    - **Cloudflare R2** and **Upstash Redis** as managed state services  
    The repo also includes Docker, CI, and docs for running the backend locally, but the main architectural boundary is still frontend on Vercel vs generation service on Railway.

13. **The repository includes a mirrored backend implementation for parity and portability.**  
    `backend/` contains its own FastAPI app, routers, service classes, tests, and a Docker setup.  
    Its structure mirrors the Next-side server code closely enough that the same generation concepts exist in both places, which makes the backend a portability layer rather than a different product.

14. **Testing is organized around these boundaries.**  
    There are unit/integration tests on both sides:
    - frontend feature and hook tests under `src/**`
    - backend service/router tests under `backend/tests/`  
    This reinforces that the main architecture seams are URL parsing, streaming generation, graph/mermaid validation, GitHub fetching, and storage/quota behavior.

Main entry points: [`src/server/generate/graph.ts`](https://github.com/hjj4woer/gitdiagram/blob/main/src/server/generate/graph.ts) (Generate pipeline), [`src/features/diagram/api.ts`](https://github.com/hjj4woer/gitdiagram/blob/main/src/features/diagram/api.ts) (Diagram API), [`backend/app/services/graph_service.py`](https://github.com/hjj4woer/gitdiagram/blob/main/backend/app/services/graph_service.py) (Backend pipeline), [`src/components/mermaid-diagram.tsx`](https://github.com/hjj4woer/gitdiagram/blob/main/src/components/mermaid-diagram.tsx) (Diagram UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Next.js app"]
  node_app_home["Home page<br/>UI route<br/>[page.tsx]"]
  node_app_repo["Repo page<br/>UI route<br/>[page.tsx]"]
  node_components_diagram["Diagram UI<br/>client components"]
  node_hooks_stream["Stream hook<br/>client hook"]
  node_hooks_export["Export hook<br/>client hook"]
end

subgraph group_diagram_shared["Diagram feature layer"]
  node_feature_api["Diagram API<br/>client feature<br/>[api.ts]"]
  node_feature_sse["SSE helpers<br/>client feature<br/>[sse.ts]"]
  node_feature_graph["Graph model<br/>client feature<br/>[graph.ts]"]
end

subgraph group_next_server["Next server"]
  node_next_generate["Generate pipeline<br/>server pipeline<br/>[graph.ts]"]
  node_next_github["GitHub fetch<br/>server service<br/>[github.ts]"]
  node_next_mermaid["Mermaid validation<br/>server service"]
  node_next_storage[("Storage layer<br/>server storage<br/>[r2.ts]")]
  node_next_routes["API routes<br/>route handlers<br/>[route.ts]"]
end

subgraph group_backend["FastAPI backend"]
  node_backend_api["FastAPI app<br/>api service<br/>[main.py]"]
  node_backend_generate["Backend pipeline<br/>service pipeline<br/>[graph_service.py]"]
  node_backend_github["Backend GitHub<br/>[github_service.py]"]
  node_backend_mermaid["Bun validator<br/>validation bridge"]
end

subgraph group_browse["Browse/catalog"]
  node_browse_catalog["Browse catalog<br/>read-only feature<br/>[page.tsx]"]
  node_browse_data["Browse data<br/>catalog data<br/>[catalog.ts]"]
end

node_app_home -->|"navigate"| node_app_repo
node_app_repo -->|"renders"| node_components_diagram
node_components_diagram -->|"uses"| node_hooks_stream
node_components_diagram -->|"exports"| node_hooks_export
node_hooks_stream -->|"calls"| node_feature_api
node_hooks_export -->|"uses"| node_feature_graph
node_feature_api -->|"streams"| node_feature_sse
node_feature_api -->|"requests"| node_next_routes
node_feature_api -.->|"switches to"| node_backend_api
node_next_routes -->|"orchestrates"| node_next_generate
node_next_generate -->|"fetches"| node_next_github
node_next_generate -->|"validates"| node_next_mermaid
node_next_generate -->|"persists"| node_next_storage
node_next_generate -->|"emits"| node_feature_graph
node_next_routes -->|"serves"| node_browse_data
node_browse_catalog -->|"reads"| node_browse_data
node_backend_api -->|"routes to"| node_backend_generate
node_backend_generate -->|"fetches"| node_backend_github
node_backend_generate -->|"validates"| node_backend_mermaid
node_backend_generate -.->|"shares stores"| node_next_storage

click node_app_home "https://github.com/hjj4woer/gitdiagram/blob/main/src/app/page.tsx"
click node_app_repo "https://github.com/hjj4woer/gitdiagram/blob/main/src/app/[username]/[repo]/page.tsx"
click node_components_diagram "https://github.com/hjj4woer/gitdiagram/blob/main/src/components/mermaid-diagram.tsx"
click node_hooks_stream "https://github.com/hjj4woer/gitdiagram/blob/main/src/hooks/diagram/useDiagramStream.ts"
click node_hooks_export "https://github.com/hjj4woer/gitdiagram/blob/main/src/hooks/diagram/useDiagramExport.ts"
click node_feature_api "https://github.com/hjj4woer/gitdiagram/blob/main/src/features/diagram/api.ts"
click node_feature_sse "https://github.com/hjj4woer/gitdiagram/blob/main/src/features/diagram/sse.ts"
click node_feature_graph "https://github.com/hjj4woer/gitdiagram/blob/main/src/features/diagram/graph.ts"
click node_next_generate "https://github.com/hjj4woer/gitdiagram/blob/main/src/server/generate/graph.ts"
click node_next_github "https://github.com/hjj4woer/gitdiagram/blob/main/src/server/generate/github.ts"
click node_next_mermaid "https://github.com/hjj4woer/gitdiagram/blob/main/src/server/generate/mermaid-validator.ts"
click node_next_storage "https://github.com/hjj4woer/gitdiagram/blob/main/src/server/storage/r2.ts"
click node_next_routes "https://github.com/hjj4woer/gitdiagram/blob/main/src/app/api/generate/stream/route.ts"
click node_browse_catalog "https://github.com/hjj4woer/gitdiagram/blob/main/src/app/browse/page.tsx"
click node_browse_data "https://github.com/hjj4woer/gitdiagram/blob/main/src/features/browse/catalog.ts"
click node_backend_api "https://github.com/hjj4woer/gitdiagram/blob/main/backend/app/main.py"
click node_backend_generate "https://github.com/hjj4woer/gitdiagram/blob/main/backend/app/services/graph_service.py"
click node_backend_github "https://github.com/hjj4woer/gitdiagram/blob/main/backend/app/services/github_service.py"
click node_backend_mermaid "https://github.com/hjj4woer/gitdiagram/blob/main/backend/scripts/validate_mermaid.mjs"

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_app_home,node_app_repo,node_components_diagram,node_hooks_stream,node_hooks_export toneBlue
class node_feature_api,node_feature_sse,node_feature_graph toneAmber
class node_next_generate,node_next_github,node_next_mermaid,node_next_storage,node_next_routes toneMint
class node_backend_api,node_backend_generate,node_backend_github,node_backend_mermaid toneRose
class node_browse_catalog,node_browse_data toneIndigo
```

## Components

### Next.js app

User-facing app, UI, and browser-side diagram experience.

- [Home page](https://github.com/hjj4woer/gitdiagram/blob/main/src/app/page.tsx): UI route · Entry pages, repo pages, sponsor flows, and app shell. · `src/app/page.tsx`
- [Repo page](https://github.com/hjj4woer/gitdiagram/blob/main/src/app/[username]/[repo]/page.tsx): UI route · Repository landing page and diagram presentation for a selected repo. · `src/app/[username]/[repo]/page.tsx`
- [Diagram UI](https://github.com/hjj4woer/gitdiagram/blob/main/src/components/mermaid-diagram.tsx): client components · Mermaid rendering, toolbar actions, export, and presentation controls. · `src/components/mermaid-diagram.tsx`
- [Stream hook](https://github.com/hjj4woer/gitdiagram/blob/main/src/hooks/diagram/useDiagramStream.ts): client hook · Browser-side streaming state and progress handling for generation. · `src/hooks/diagram/useDiagramStream.ts`
- [Export hook](https://github.com/hjj4woer/gitdiagram/blob/main/src/hooks/diagram/useDiagramExport.ts): client hook · Client export and download behavior for generated diagrams. · `src/hooks/diagram/useDiagramExport.ts`

### Diagram feature layer

Shared client-side diagram types, API wrappers, SSE helpers, and export logic.

- [Diagram API](https://github.com/hjj4woer/gitdiagram/blob/main/src/features/diagram/api.ts): client feature · Client wrappers for generation APIs, SSE, cost helpers, and graph types. · `src/features/diagram/api.ts`
- [SSE helpers](https://github.com/hjj4woer/gitdiagram/blob/main/src/features/diagram/sse.ts): client feature · Streaming event parsing and transport helpers for generation updates. · `src/features/diagram/sse.ts`
- [Graph model](https://github.com/hjj4woer/gitdiagram/blob/main/src/features/diagram/graph.ts): client feature · Shared diagram graph types and browser-facing graph utilities. · `src/features/diagram/graph.ts`

### Next server

Server-side generation pipeline, storage, and app routes in the Next.js runtime.

- [Generate pipeline](https://github.com/hjj4woer/gitdiagram/blob/main/src/server/generate/graph.ts): server pipeline · Next-side orchestration for GitHub fetch, prompting, graph building, validation, and retries. · `src/server/generate/graph.ts`
- [GitHub fetch](https://github.com/hjj4woer/gitdiagram/blob/main/src/server/generate/github.ts): server service · Repo metadata, tree, README, and private repo access on the Next side. · `src/server/generate/github.ts`
- [Mermaid validation](https://github.com/hjj4woer/gitdiagram/blob/main/src/server/generate/mermaid-validator.ts): server service · In-process Mermaid validation for Next runtime output. · `src/server/generate/mermaid-validator.ts`
- [Storage layer](https://github.com/hjj4woer/gitdiagram/blob/main/src/server/storage/r2.ts): server storage · R2, Redis, cache, quota, artifact, and browse persistence services. · `src/server/storage/r2.ts`
- [API routes](https://github.com/hjj4woer/gitdiagram/blob/main/src/app/api/generate/stream/route.ts): route handlers · Stream, cost, preview, health, and browse-index endpoints. · `src/app/api/generate/stream/route.ts`

### FastAPI backend

Parallel generation service for production parity and Railway deployment.

- [FastAPI app](https://github.com/hjj4woer/gitdiagram/blob/main/backend/app/main.py): api service · Python API entrypoint and routers for production generation service. · `backend/app/main.py`
- [Backend pipeline](https://github.com/hjj4woer/gitdiagram/blob/main/backend/app/services/graph_service.py): service pipeline · Mirrored generation orchestration on the FastAPI side. · `backend/app/services/graph_service.py`
- [Backend GitHub](https://github.com/hjj4woer/gitdiagram/blob/main/backend/app/services/github_service.py): service · Repository snapshot retrieval and filtering for the FastAPI service. · `backend/app/services/github_service.py`
- [Bun validator](https://github.com/hjj4woer/gitdiagram/blob/main/backend/scripts/validate_mermaid.mjs): validation bridge · Bun-based Mermaid validation wrapper used by the Python backend. · `backend/scripts/validate_mermaid.mjs`

### Browse/catalog

Read-only discovery path for precomputed or indexed diagrams.

- [Browse catalog](https://github.com/hjj4woer/gitdiagram/blob/main/src/app/browse/page.tsx): read-only feature · Indexed and precomputed diagram discovery with hover previews. · `src/app/browse/page.tsx`
- [Browse data](https://github.com/hjj4woer/gitdiagram/blob/main/src/features/browse/catalog.ts): catalog data · Client/server browse catalog data and index access. · `src/features/browse/catalog.ts`

## Connections

- Home page → Repo page: navigate · Users move from landing pages into repository views.
- Repo page → Diagram UI: renders · Repo pages display generated diagrams and actions.
- Diagram UI → Stream hook: uses · Diagram UI consumes streamed generation state.
- Diagram UI → Export hook: exports · Toolbar actions download or copy diagram output.
- Stream hook → Diagram API: calls · Browser hooks call generation APIs through the feature layer.
- Export hook → Graph model: uses · Export relies on shared graph/diagram representations.
- Diagram API → SSE helpers: streams · Client API wrappers parse SSE-style progress events.
- Diagram API → API routes: requests · Client generation calls hit Next route handlers in the default runtime.
- Diagram API → FastAPI app: switches to · Environment can route generation to the standalone backend service.
- API routes → Generate pipeline: orchestrates · Route handlers delegate to the server generation pipeline.
- Generate pipeline → GitHub fetch: fetches · Pipeline builds repository snapshots from GitHub.
- Generate pipeline → Mermaid validation: validates · Generated Mermaid is checked before being returned.
- Generate pipeline → Storage layer: persists · Artifacts, quota state, and browse data are stored externally.
- Generate pipeline → Graph model: emits · Pipeline produces structured graph data for the UI.
- API routes → Browse data: serves · Browse index routes expose precomputed catalog data.
- Browse catalog → Browse data: reads · Discovery pages read stored browse diagrams and previews.
- FastAPI app → Backend pipeline: routes to · FastAPI routers hand off to mirrored generation services.
- Backend pipeline → Backend GitHub: fetches · Backend service builds the same repository snapshot inputs.
- Backend pipeline → Bun validator: validates · Python backend uses the Bun bridge for Mermaid validation.
- Backend pipeline → Storage layer: shares stores · Both runtimes depend on the same external storage concerns.
