# quangdungluong/codewiki architecture

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

- Interactive diagram: https://gitdiagram.com/quangdungluong/codewiki
- Repository: https://github.com/quangdungluong/codewiki

## Overview

1. **Overall shape**
   - This is a two-tier application: a **Next.js frontend** in `frontend/` and a **Python backend API** in `api/` with shared internal tooling in `utils/`.
   - The product goal is to take a GitHub repo URL or owner/repo pair, analyze the codebase, and present **generated diagrams, a wiki, and chat-based Q&A**.

2. **Frontend boundary**
   - The frontend is a **Next.js App Router** app with repo-scoped routes like `/[owner]/[repo]` and `/[owner]/[repo]/diagram`.
   - It is responsible for user interaction, route navigation, language/theme UI, and rendering outputs such as markdown, Mermaid diagrams, repo trees, and chat panels.
   - Key UI pieces include `Ask`, `Mermaid`, `WikiTreeView`, `ProcessedProjects`, and `DiagramLoading`.

3. **Frontend data flow**
   - Frontend hooks such as `useDiagram` and `useProcessedProjects` likely call the backend to fetch generated artifacts and processing status.
   - `websocketClient.ts` indicates at least part of the chat or generation flow is **streamed over WebSockets**, not just plain HTTP.
   - `parseRepoInput` and `getRepoUrl` suggest the UI accepts multiple repo input forms and normalizes them into a canonical repository identifier.

4. **Backend API layer**
   - The backend entry points are `api/main.py` and `api/api.py`, which likely expose the public HTTP routes used by the frontend.
   - `api/models.py`, `api/config.py`, and `api/language_config.py` provide request/response schemas and runtime configuration.
   - `api/stream_chat.py` suggests a dedicated streaming endpoint or protocol for incremental AI responses.

5. **Repository ingestion and analysis pipeline**
   - The `utils/` package contains the core pipeline for turning a repo into structured knowledge:
     - `repo_downloader.py` fetches source code.
     - `repository_parser.py` and `repository_structure.py` extract file hierarchy and code structure.
     - `document_pipeline.py` appears to build the document corpus used for wiki generation and retrieval.
   - This is the main boundary between raw GitHub content and generated product artifacts.

6. **AI and retrieval subsystem**
   - `api/rag.py` and utility modules like `ollama_embedder.py`, `prompts.py`, and `token_utils.py` indicate a **RAG-style pipeline** for repo-aware answering.
   - `api/services/gemini_service.py` suggests LLM generation is delegated to **Google Gemini**.
   - The architecture likely combines:
     - repo-derived documents and structure,
     - embeddings for retrieval,
     - prompt assembly,
     - LLM generation for wiki pages, summaries, diagrams, and chat answers.

7. **GitHub and repository metadata integration**
   - `api/services/github_service.py` and `utils/token_utils.py` show support for **public and private repositories**, with token-based GitHub access.
   - `api/processed_projects.py` and `frontend/components/ProcessedProjects.tsx` imply the system tracks which repositories have already been analyzed so users can revisit them without full reprocessing.

8. **Generated outputs**
   - The application produces at least three user-facing artifact types:
     - **System diagrams** via `api/generate_diagram.py` and the diagram page.
     - **Wiki content** via `api/wiki.py` and `api/wiki_cache.py`.
     - **Chat answers** via `api/stream_chat.py` and `api/rag.py`.
   - `frontend/components/Markdown.tsx` and `Mermaid.tsx` are the rendering boundary for these generated outputs.

9. **Persistence and caching**
   - `utils/localdb_manager.py` and `api/wiki_cache.py` indicate some persisted local state for generated content and/or repo metadata.
   - `utils/redis_tasks.py` strongly suggests **Redis-backed asynchronous task handling** or job coordination for long-running repo analysis.
   - `api/processed_projects.py` is likely the registry used to avoid redundant recomputation and to surface prior results in the UI.

10. **Async execution and long-running work**
   - Repo analysis and AI generation are likely too slow for synchronous request/response, so the system appears to split work into:
     - request initiation from the frontend,
     - background processing via Redis/task utilities,
     - status polling or streaming updates back to the UI.
   - This is consistent with separate loading components and WebSocket client code.

11. **External service dependencies**
   - The main external dependencies that materially shape the architecture are:
     - **GitHub API** for repository access.
     - **Gemini** for LLM generation.
     - **Ollama** or a local embedding backend for vectorization.
     - **Redis** for task coordination and possibly caching.
   - These services are not incidental; they define how repo ingestion, retrieval, and generation are executed.

12. **Local development and deployment shape**
   - `docker-compose.yaml` suggests the stack is intended to run as multiple cooperating services locally.
   - The presence of separate frontend/backend trees and shared utility modules points to a deployable split between UI and API, with stateful support services alongside them.

13. **Quality gates**
   - Tests in `tests/` cover chat and repository structure behavior, which aligns with the two most critical backend concerns: **correct understanding of codebases** and **correct answer generation**.
   - GitHub workflows and pre-commit/lint configs indicate CI is used to keep both the Python and TypeScript sides consistent.

14. **How to draw the architecture**
   - A good diagram would show:
     - User → Next.js frontend,
     - Frontend → backend API over HTTP/WebSocket,
     - Backend → GitHub, Gemini, embedding backend/Ollama, Redis, local cache/DB,
     - Backend pipeline producing diagram/wiki/chat artifacts,
     - Frontend rendering those artifacts in markdown/Mermaid/tree views.
   - The main system boundary is the **repo analysis and generation backend**; the UI is primarily a presentation and interaction layer.

Main entry points: [`api/api.py`](https://github.com/quangdungluong/codewiki/blob/master/api/api.py) (Public routes), [`api/rag.py`](https://github.com/quangdungluong/codewiki/blob/master/api/rag.py) (RAG layer), [`utils/repository_parser.py`](https://github.com/quangdungluong/codewiki/blob/master/utils/repository_parser.py) (Repo parser), [`api/generate_diagram.py`](https://github.com/quangdungluong/codewiki/blob/master/api/generate_diagram.py) (Diagram gen)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_ui_shell["App shell<br/>Next.js App Router"]
  node_repo_page["Repo view<br/>Route<br/>[page.tsx]"]
  node_diagram_page["Diagram view<br/>Route<br/>[page.tsx]"]
  node_ui_components["UI widgets<br/>React components"]
  node_state_hooks["Client hooks"]
  node_repo_input["Repo parsing<br/>URL normalization"]
  node_ws_client["WebSocket client<br/>Streaming client<br/>[websocketClient.ts]"]
end

subgraph group_backend["Backend API"]
  node_api_entry["API entry<br/>FastAPI app<br/>[main.py]"]
  node_api_routes["Public routes<br/>API modules<br/>[api.py]"]
  node_chat_stream["Chat stream<br/>Streaming endpoint<br/>[stream_chat.py]"]
  node_diagram_gen["Diagram gen<br/>Artifact builder"]
  node_wiki_service["Wiki service<br/>Artifact builder<br/>[wiki.py]"]
  node_rag_layer["RAG layer<br/>Retrieval orchestration<br/>[rag.py]"]
  node_github_service["GitHub access<br/>Integration<br/>[github_service.py]"]
  node_llm_service["Gemini service<br/>LLM integration<br/>[gemini_service.py]"]
  node_processed_projects[("Project registry<br/>State store")]
end

subgraph group_pipeline["Repo Pipeline"]
  node_repo_download["Downloader<br/>Ingestion<br/>[repo_downloader.py]"]
  node_repo_parse["Repo parser<br/>Structure extraction"]
  node_doc_pipeline["Doc pipeline<br/>Corpus builder"]
  node_structure_model["Structure model<br/>Repository model"]
  node_tasks_cache[("Tasks and cache<br/>Redis/local state<br/>[redis_tasks.py]")]
end

subgraph group_external["External Services"]
  node_redis[("Redis<br/>Queue/cache")]
  node_github_api(("GitHub<br/>External API"))
  node_gemini_api(("Gemini<br/>External LLM"))
  node_embeddings(("Embeddings<br/>Vector backend<br/>[ollama_embedder.py]"))
end

node_repo_page -->|"renders"| node_ui_components
node_diagram_page -->|"renders"| node_ui_components
node_ui_shell -->|"routes to"| node_repo_page
node_ui_shell -->|"routes to"| node_diagram_page
node_ui_components -->|"updates via"| node_ws_client
node_state_hooks -->|"fetches"| node_api_routes
node_state_hooks -->|"reads"| node_processed_projects
node_repo_input -->|"normalizes for"| node_api_routes
node_ws_client -->|"streams"| node_chat_stream
node_api_entry -->|"mounts"| node_api_routes
node_api_routes -->|"serves"| node_chat_stream
node_api_routes -->|"serves"| node_diagram_gen
node_api_routes -->|"serves"| node_wiki_service
node_api_routes -->|"serves"| node_rag_layer
node_rag_layer -->|"retrieves from"| node_github_service
node_rag_layer -->|"retrieves context from"| node_doc_pipeline
node_rag_layer -->|"generates with"| node_llm_service
node_diagram_gen -->|"analyzes"| node_repo_parse
node_wiki_service -->|"builds from"| node_doc_pipeline
node_repo_download -->|"downloads via"| node_github_service
node_repo_parse -->|"consumes"| node_repo_download
node_repo_parse -->|"produces"| node_structure_model
node_doc_pipeline -->|"derives from"| node_repo_parse
node_tasks_cache -->|"coordinates with"| node_redis
node_processed_projects -->|"persists via"| node_tasks_cache
node_rag_layer -->|"embeds with"| node_embeddings
node_diagram_gen -->|"generates with"| node_gemini_api
node_wiki_service -->|"generates with"| node_gemini_api

click node_ui_shell "https://github.com/quangdungluong/codewiki/tree/master/frontend/app"
click node_repo_page "https://github.com/quangdungluong/codewiki/blob/master/frontend/app/[owner]/[repo]/page.tsx"
click node_diagram_page "https://github.com/quangdungluong/codewiki/blob/master/frontend/app/[owner]/[repo]/diagram/page.tsx"
click node_ui_components "https://github.com/quangdungluong/codewiki/tree/master/frontend/components"
click node_state_hooks "https://github.com/quangdungluong/codewiki/tree/master/frontend/hooks"
click node_repo_input "https://github.com/quangdungluong/codewiki/tree/master/frontend/utils"
click node_ws_client "https://github.com/quangdungluong/codewiki/blob/master/frontend/utils/websocketClient.ts"
click node_api_entry "https://github.com/quangdungluong/codewiki/blob/master/api/main.py"
click node_api_routes "https://github.com/quangdungluong/codewiki/blob/master/api/api.py"
click node_chat_stream "https://github.com/quangdungluong/codewiki/blob/master/api/stream_chat.py"
click node_diagram_gen "https://github.com/quangdungluong/codewiki/blob/master/api/generate_diagram.py"
click node_wiki_service "https://github.com/quangdungluong/codewiki/blob/master/api/wiki.py"
click node_rag_layer "https://github.com/quangdungluong/codewiki/blob/master/api/rag.py"
click node_github_service "https://github.com/quangdungluong/codewiki/blob/master/api/services/github_service.py"
click node_llm_service "https://github.com/quangdungluong/codewiki/blob/master/api/services/gemini_service.py"
click node_processed_projects "https://github.com/quangdungluong/codewiki/blob/master/api/processed_projects.py"
click node_repo_download "https://github.com/quangdungluong/codewiki/blob/master/utils/repo_downloader.py"
click node_repo_parse "https://github.com/quangdungluong/codewiki/blob/master/utils/repository_parser.py"
click node_doc_pipeline "https://github.com/quangdungluong/codewiki/blob/master/utils/document_pipeline.py"
click node_structure_model "https://github.com/quangdungluong/codewiki/blob/master/utils/repository_structure.py"
click node_tasks_cache "https://github.com/quangdungluong/codewiki/blob/master/utils/redis_tasks.py"
click node_embeddings "https://github.com/quangdungluong/codewiki/blob/master/utils/ollama_embedder.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_ui_shell,node_repo_page,node_diagram_page,node_ui_components,node_state_hooks,node_repo_input,node_ws_client toneBlue
class node_api_entry,node_api_routes,node_chat_stream,node_diagram_gen,node_wiki_service,node_rag_layer,node_github_service,node_llm_service,node_processed_projects toneAmber
class node_repo_download,node_repo_parse,node_doc_pipeline,node_structure_model,node_tasks_cache toneMint
class node_redis,node_github_api,node_gemini_api,node_embeddings toneRose
```

## Components

### Frontend

Next.js UI and client-side state

- [App shell](https://github.com/quangdungluong/codewiki/tree/master/frontend/app): Next.js App Router · Top-level pages, layout, and repo-scoped routes · `frontend/app`
- [Repo view](https://github.com/quangdungluong/codewiki/blob/master/frontend/app/[owner]/[repo]/page.tsx): Route · Main repository workspace and summary view · `frontend/app/[owner]/[repo]/page.tsx`
- [Diagram view](https://github.com/quangdungluong/codewiki/blob/master/frontend/app/[owner]/[repo]/diagram/page.tsx): Route · Dedicated system diagram page · `frontend/app/[owner]/[repo]/diagram/page.tsx`
- [UI widgets](https://github.com/quangdungluong/codewiki/tree/master/frontend/components): React components · Ask panel, markdown, Mermaid, tree, and loading states · `frontend/components`
- [Client hooks](https://github.com/quangdungluong/codewiki/tree/master/frontend/hooks): Hooks · Fetches diagrams and processed project status · `frontend/hooks`
- [Repo parsing](https://github.com/quangdungluong/codewiki/tree/master/frontend/utils): URL normalization · Converts user input into canonical repo identifiers · `frontend/utils`
- [WebSocket client](https://github.com/quangdungluong/codewiki/blob/master/frontend/utils/websocketClient.ts): Streaming client · Streams chat or generation updates to the UI · `frontend/utils/websocketClient.ts`

### Backend API

Python API, generation, and retrieval services

- [API entry](https://github.com/quangdungluong/codewiki/blob/master/api/main.py): FastAPI app · HTTP entrypoints and route wiring · `api/main.py`
- [Public routes](https://github.com/quangdungluong/codewiki/blob/master/api/api.py): API modules · Primary endpoints for app features · `api/api.py`
- [Chat stream](https://github.com/quangdungluong/codewiki/blob/master/api/stream_chat.py): Streaming endpoint · Incremental chat responses and message flow · `api/stream_chat.py`
- [Diagram gen](https://github.com/quangdungluong/codewiki/blob/master/api/generate_diagram.py): Artifact builder · Produces system diagrams for repositories · `api/generate_diagram.py`
- [Wiki service](https://github.com/quangdungluong/codewiki/blob/master/api/wiki.py): Artifact builder · Generates wiki content and cacheable pages · `api/wiki.py`
- [RAG layer](https://github.com/quangdungluong/codewiki/blob/master/api/rag.py): Retrieval orchestration · Repo-aware answering and prompt assembly · `api/rag.py`
- [GitHub access](https://github.com/quangdungluong/codewiki/blob/master/api/services/github_service.py): Integration · Repository metadata and source access · `api/services/github_service.py`
- [Gemini service](https://github.com/quangdungluong/codewiki/blob/master/api/services/gemini_service.py): LLM integration · Delegates generation to Gemini · `api/services/gemini_service.py`
- [Project registry](https://github.com/quangdungluong/codewiki/blob/master/api/processed_projects.py): State store · Tracks analyzed repositories and reuse state · `api/processed_projects.py`

### Repo Pipeline

Shared ingestion and analysis utilities

- [Downloader](https://github.com/quangdungluong/codewiki/blob/master/utils/repo_downloader.py): Ingestion · Fetches repository source for analysis · `utils/repo_downloader.py`
- [Repo parser](https://github.com/quangdungluong/codewiki/blob/master/utils/repository_parser.py): Structure extraction · Builds file and code structure from source · `utils/repository_parser.py`
- [Doc pipeline](https://github.com/quangdungluong/codewiki/blob/master/utils/document_pipeline.py): Corpus builder · Creates documents used for wiki and retrieval · `utils/document_pipeline.py`
- [Structure model](https://github.com/quangdungluong/codewiki/blob/master/utils/repository_structure.py): Repository model · Represents parsed repository hierarchy · `utils/repository_structure.py`
- [Tasks and cache](https://github.com/quangdungluong/codewiki/blob/master/utils/redis_tasks.py): Redis/local state · Background jobs plus local persistence · `utils/redis_tasks.py`

### External Services

Third-party systems used by the app

- **Redis**: Queue/cache · Async coordination for long-running work
- **GitHub**: External API · Remote repository source
- **Gemini**: External LLM · Model generation backend
- [Embeddings](https://github.com/quangdungluong/codewiki/blob/master/utils/ollama_embedder.py): Vector backend · Local or Ollama-based embedding generation · `utils/ollama_embedder.py`

## Connections

- Repo view → UI widgets: renders · Repository view composes key widgets
- Diagram view → UI widgets: renders · Diagram route uses shared presentation components
- App shell → Repo view: routes to · App router hosts repo-scoped page
- App shell → Diagram view: routes to · App router hosts diagram page
- UI widgets → WebSocket client: updates via · Chat and generation UI receive streamed updates
- Client hooks → Public routes: fetches · Hooks query backend for artifacts and status
- Client hooks → Project registry: reads · Processed projects view status and history
- Repo parsing → Public routes: normalizes for · Canonical repo identifiers are sent to backend
- WebSocket client → Chat stream: streams · WebSocket client connects to incremental chat flow
- API entry → Public routes: mounts · Main application exposes API modules
- Public routes → Chat stream: serves · Public API includes chat streaming
- Public routes → Diagram gen: serves · Public API includes diagram generation
- Public routes → Wiki service: serves · Public API includes wiki artifact generation
- Public routes → RAG layer: serves · Public API exposes repo-aware Q&A
- RAG layer → GitHub access: retrieves from · RAG uses repository metadata and source access
- RAG layer → Doc pipeline: retrieves context from · Repo documents feed answer generation
- RAG layer → Gemini service: generates with · Answers and summaries are produced by Gemini
- Diagram gen → Repo parser: analyzes · Diagram generation depends on parsed structure
- Wiki service → Doc pipeline: builds from · Wiki content is derived from the document corpus
- Downloader → GitHub access: downloads via · Repository source comes from GitHub
- Repo parser → Downloader: consumes · Parser works on downloaded repository contents
- Repo parser → Structure model: produces · Parsed hierarchy becomes a reusable structure model
- Doc pipeline → Repo parser: derives from · Document corpus is built from parsed code structure
- Tasks and cache → Redis: coordinates with · Long-running jobs are coordinated through Redis
- Project registry → Tasks and cache: persists via · Registry likely backed by local or cached state
- RAG layer → Embeddings: embeds with · Retrieval uses vectorization backend
- Diagram gen → Gemini: generates with · Diagram synthesis uses LLM generation
- Wiki service → Gemini: generates with · Wiki content is generated by Gemini
