How gaurav-chakraborty/gitdiagram works
1. High-level shape
GitDiagram is a split-frontend / generation-backend system. The Next.js app owns the user experience, public pages, browse/catalog features, and browser-side interactions. A separate FastAPI service in backend/ can run the long-lived diagram generation pipeline in production, while the Next app also has in-repo Route Handlers for a “next” generation mode.
2. Primary user flow
The core flow is: user submits a GitHub repo URL → the system resolves repository metadata and reads the repo tree/README → an LLM turns that snapshot into an explanation and graph model → the graph is compiled into Mermaid → Mermaid is validated → the app renders an interactive diagram and stores the result for reuse. The final output is not just a picture; it includes explanation, graph structure, Mermaid source, and terminal generation status.
3. Frontend application boundary
The Next.js app under src/app is the public product surface: landing page, repo pages, browse pages, sponsor page, and API routes. The repo-specific route src/app/[username]/[repo]/page.tsx is the canonical diagram page. Client-side behavior lives in reusable components and hooks under src/components, src/hooks, and src/features, with diagram state management abstracted away from rendering.
4. Generation API boundary
There are two generation entry points:
- src/app/api/generate/* for the Next-native path
- backend/app/routers/generate.py for the FastAPI path
The frontend selects the backend explicitly through environment-driven configuration. This is important architecturally because the generation pipeline is shared conceptually, but deployed in two runtimes.
5. Server-side generation pipeline
The generation subsystem in src/server/generate and backend/app/services is the heart of the product. It pulls repository data from GitHub, formats and cleans the input, estimates cost, selects model config, calls OpenAI/OpenRouter, and constructs a graph from the repo structure. The pipeline then validates graph paths and Mermaid syntax before returning a result the UI can safely render.
6. Validation and safety gates
Diagram generation is guarded by multiple checks:
- repo-path validation against the actual file tree
- Mermaid validation before rendering
- complimentary quota gating for free usage
- terminal failure summaries when generation fails without a saved artifact
These checks prevent invalid diagrams from being surfaced and reduce repeated expensive model runs.
7. State and persistence
Persistent state is intentionally split by concern:
- Cloudflare R2 stores successful diagram artifacts
- Upstash Redis stores complimentary quota state and short-lived failure summaries
- repository page and browse-related caching lives in src/server/storage
- private repo persistence uses a separate namespace keyed off the user’s GitHub token
There is no database like Postgres in the runtime path; storage is object/kv based.
8. GitHub integration
GitHub is the main source system. The app fetches repo metadata, default branch, recursive file tree, README, and stars. Private repository access is supported via a GitHub personal access token with repo scope, handled through the app UI and server auth utilities. The architecture treats GitHub as both the source of truth for code structure and the source of social metadata used in repo pages.
9. Browse/index experience
The browse subsystem is separate from on-demand generation. src/app/browse, src/features/browse, and src/app/api/browse-index support a catalog of preexisting diagrams and hover previews. This is a read-optimized surface that reuses stored artifacts instead of regenerating diagrams on every request.
10. Rendering, export, and social assets
The diagram UI is interactive and supports copy/export flows. Mermaid rendering is encapsulated in components like mermaid-diagram.tsx, with toolbar actions for copy/download. The app also generates Open Graph/Twitter images and repo-specific social cards under src/app/[username]/[repo] and src/server/og, which means diagram data feeds not only the main page but also social sharing assets.
11. Backend implementation details
The backend/ folder is a standalone FastAPI service with its own Python app structure (routers, services, core). It mirrors the same generation responsibilities as the Next backend: GitHub fetch, graph construction, Mermaid generation, quota/cost logic, and observability. A small Bun-based Mermaid validator bridge exists here so the Python service can validate diagrams with the same Mermaid/DOMPurify approach used by the Next runtime.
12. Operational concerns
The system is deployed as a mixed stack: Vercel for the Next frontend, Railway for the long-lived FastAPI generator, Cloudflare R2 for artifacts, and Upstash Redis for lightweight state. CI is GitHub Actions, and the repo includes Docker and docker-compose support for local parity. Observability and error formatting are first-class in the codebase, which matters because generation is async, failure-prone, and externally dependent on GitHub and LLM providers.
Main entry points: route.ts Generate API · graph_service.py Backend services · mermaid-validator.ts Validation gates · graph.ts Graph builder
Components 26
Next app
Public product surface, client interactions, and in-repo generation entrypoints.
- Landing · Marketing entry and repository submission surface.
src/app/page.tsx - Repo page · Canonical repository diagram page with repo-specific route handling.
src/app/[username]/[repo]/page.tsx - Browse · Read-optimized catalog of preexisting diagrams.
src/app/browse/page.tsx - Generate API · Next-native generation and cost endpoints.
src/app/api/generate/stream/route.ts - Preview API · Diagram preview endpoint used by the UI.
src/app/api/diagram-preview/route.ts - Browse index · Serves catalog index data for browse views.
src/app/api/browse-index/route.ts - Repo client · Client-side repo page behavior, state, and interactions.
src/app/[username]/[repo]/repo-page-client.tsx - Diagram UI · Interactive Mermaid rendering, toolbar, export, and copy flows.
src/components/mermaid-diagram.tsx - Browse UI · Browse controls, hover previews, and catalog result rendering.
src/components/browse-catalog.tsx - Diagram hooks · Browser-side streaming, export, and diagram state orchestration.
src/hooks/diagram/useDiagramStream.ts - Diagram API client · Client adapter for diagram generation, SSE, and cost calls.
src/features/diagram/api.ts - GitHub URL tools · URL parsing and repo identity normalization for the user flow.
src/features/diagram/github-url.ts - Cache actions · Next server actions used to manage cached repo/page state.
src/app/_actions/cache.ts
Generation pipeline
Shared repo-to-diagram workflow and validation logic.
- GitHub fetch · Repository metadata, tree, README, and star retrieval.
src/server/generate/github.ts - Graph builder · Turns repository snapshots into structured graph output.
src/server/generate/graph.ts - Mermaid gen · Compiles graph output into Mermaid source.
src/server/generate/mermaid.ts - Validation gates · Repo-path and Mermaid validation before rendering or storage.
src/server/generate/mermaid-validator.ts - Quota & cost · Complimentary usage gating and cost estimation.
src/server/generate/complimentary-gate.ts - LLM client · Model selection and provider access for generation.
src/server/generate/openai.ts
Persistence
Artifact, cache, quota, and status storage.
- Artifact store · Persists successful diagram artifacts for reuse.
src/server/storage/r2.ts - Status store · Short-lived generation status and failure summaries.
src/server/storage/status-store.ts - Quota store · Complimentary quota tracking and gating state.
src/server/storage/quota-store.ts - Browse store · Cached browse index and stored diagram catalog state.
src/server/storage/browse-diagrams.ts
FastAPI backend
Standalone Python service for long-lived generation.
- Generate router · Python generation entrypoint mirroring the Next-native path.
backend/app/routers/generate.py - Backend services · Python-side generation, validation, and provider orchestration.
backend/app/services/graph_service.py - Mermaid bridge · Bun-based Mermaid validation bridge for the Python service.
backend/lib/mermaid-validator.ts
Connections 29
- Landing → Repo page: open repo · Users move from landing into a repository-specific diagram page.
- Repo page → Repo client: hydrate · The page hands off to client-side repo interactions and generation state.
- Repo client → GitHub URL tools: parse URL · Repository URLs are normalized before generation.
- Repo client → Diagram API client: request diagram · Client triggers generation, streaming, and cost operations.
- Repo page → Diagram UI: render diagram · Rendered results are shown through the Mermaid diagram surface.
- Diagram hooks → Diagram API client: stream updates · Hooks consume SSE and orchestrate diagram generation state.
- Browse → Browse UI: browse catalog · Browse page renders catalog controls and preview surfaces.
- Browse UI → Browse index: load index · Catalog data is fetched from the browse index route.
- Browse UI → Browse store: reuse artifacts · Browse experiences read from stored diagram catalog data.
- Generate API → GitHub fetch: fetch repo · Next-native generation starts by pulling repository data from GitHub.
- Generate API → Quota & cost: gate usage · Complimentary access and cost estimation are checked before generation.
- Generate API → LLM client: call model · The pipeline sends the cleaned snapshot to an LLM provider.
- Generate API → Graph builder: build graph · Structured repository data is converted into a graph model.
- Graph builder → Mermaid gen: compile · The graph model is transformed into Mermaid source.
- Mermaid gen → Validation gates: verify syntax · Generated Mermaid is validated before being surfaced to users.
- Graph builder → Validation gates: verify paths · Graph nodes and paths are checked against repository structure.
- Validation gates → Artifact store: persist success · Successful artifacts are stored for reuse.
- Validation gates → Status store: record status · Failures and terminal generation state are stored briefly.
- Quota & cost → Quota store: check quota · Complimentary usage state is read and updated in KV storage.
- Generate API → Artifact store: save output · Rendered diagram artifacts are written for later reuse.
- Preview API → Artifact store: preview artifact · Preview rendering reads from stored or generated artifacts.
- Cache actions → Browse store: refresh cache · Server actions update cached browse and page state.
- Generate router → Backend services: orchestrate · FastAPI router delegates to backend generation services.
- Backend services → GitHub fetch: fetch repo · Backend pipeline also pulls repository snapshots from GitHub.
- Backend services → LLM client: call model · Backend generation uses the same provider abstraction conceptually.
- Backend services → Graph builder: build graph · Python service mirrors graph construction responsibilities.
- Backend services → Mermaid bridge: validate mermaid · Backend uses the Bun bridge for Mermaid validation.
- Mermaid bridge → Validation gates: share rules · Validation behavior is aligned across runtimes.
- Diagram UI → Mermaid gen: display source · The UI renders Mermaid produced by the generation pipeline.