How hjj4woer/gitdiagram works
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: graph.ts Generate pipeline · api.ts Diagram API · graph_service.py Backend pipeline · mermaid-diagram.tsx Diagram UI
Components 19
Next.js app
User-facing app, UI, and browser-side diagram experience.
- Home page · Entry pages, repo pages, sponsor flows, and app shell.
src/app/page.tsx - Repo page · Repository landing page and diagram presentation for a selected repo.
src/app/[username]/[repo]/page.tsx - Diagram UI · Mermaid rendering, toolbar actions, export, and presentation controls.
src/components/mermaid-diagram.tsx - Stream hook · Browser-side streaming state and progress handling for generation.
src/hooks/diagram/useDiagramStream.ts - Export 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 · Client wrappers for generation APIs, SSE, cost helpers, and graph types.
src/features/diagram/api.ts - SSE helpers · Streaming event parsing and transport helpers for generation updates.
src/features/diagram/sse.ts - Graph model · 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 · Next-side orchestration for GitHub fetch, prompting, graph building, validation, and retries.
src/server/generate/graph.ts - GitHub fetch · Repo metadata, tree, README, and private repo access on the Next side.
src/server/generate/github.ts - Mermaid validation · In-process Mermaid validation for Next runtime output.
src/server/generate/mermaid-validator.ts - Storage layer · R2, Redis, cache, quota, artifact, and browse persistence services.
src/server/storage/r2.ts - API routes · 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 · Python API entrypoint and routers for production generation service.
backend/app/main.py - Backend pipeline · Mirrored generation orchestration on the FastAPI side.
backend/app/services/graph_service.py - Backend GitHub · Repository snapshot retrieval and filtering for the FastAPI service.
backend/app/services/github_service.py - Bun validator · 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 · Indexed and precomputed diagram discovery with hover previews.
src/app/browse/page.tsx - Browse data · Client/server browse catalog data and index access.
src/features/browse/catalog.ts
Connections 20
- 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.