Skip to content

Commit c46427e

Browse files
gkorlandCopilot
andcommitted
docs: sync project docs with FastAPI implementation
Update the README, environment template, and reference docs so they match the current FastAPI/Uvicorn runtime, /api endpoints, supported analyzers, and auth/env behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 24eb501 commit c46427e

4 files changed

Lines changed: 489 additions & 105 deletions

File tree

.env.template

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,28 @@
22
FALKORDB_HOST=localhost
33
FALKORDB_PORT=6379
44

5-
# OpenAI API key for LLM features
6-
OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
5+
# Optional FalkorDB authentication
6+
FALKORDB_USERNAME=
7+
FALKORDB_PASSWORD=
78

8-
# Secret token for API authentication
9+
# Token checked by authenticated endpoints. If left empty, the current
10+
# implementation accepts requests without an Authorization header.
911
SECRET_TOKEN=<YOUR_SECRET_TOKEN>
1012

11-
# Flask server settings
12-
FLASK_RUN_HOST=0.0.0.0
13-
FLASK_RUN_PORT=5000
14-
15-
# Set to 1 to enable public access for analyze_repo/switch_commit endpoints
13+
# Set to 1 to make read-only endpoints public.
1614
CODE_GRAPH_PUBLIC=0
15+
16+
# Limit /api/analyze_folder to this directory tree. Leave commented to use
17+
# the repository root as the default allowed directory.
18+
# ALLOWED_ANALYSIS_DIR=/absolute/path/to/projects
19+
20+
# LiteLLM model used by /api/chat
21+
MODEL_NAME=gemini/gemini-flash-lite-latest
22+
23+
# Provider credential for the default Gemini model. Change this to the
24+
# appropriate provider key if you change MODEL_NAME.
25+
GEMINI_API_KEY=<YOUR_GEMINI_API_KEY>
26+
27+
# Optional Uvicorn bind settings used by start.sh / make run-*
28+
HOST=0.0.0.0
29+
PORT=5000

ARCHITECTURE_ANALYSIS.md

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
# CodeGraph FastAPI Backend - Architecture Analysis
2+
3+
## Executive Summary
4+
5+
CodeGraph currently exposes a **FastAPI** backend from `api.index:app` and serves the built React UI from the same process when `app/dist` exists.
6+
7+
The framework-specific HTTP code is concentrated in `api/index.py`. Most of the backend domain modules (`graph.py`, `project.py`, `analyzers/`, `git_utils/`, `info.py`, `llm.py`) are reusable Python components that do not depend on FastAPI.
8+
9+
## 1. Backend Layout
10+
11+
```
12+
api/
13+
├── __init__.py # Public package exports
14+
├── index.py # FastAPI app, auth dependencies, routes, SPA serving
15+
├── graph.py # FalkorDB graph access (sync + async helpers)
16+
├── llm.py # GraphRAG + LiteLLM chat integration
17+
├── info.py # Repository metadata stored in Redis/FalkorDB
18+
├── project.py # Clone/local repo analysis orchestration
19+
├── auto_complete.py # Prefix search helper
20+
├── prompts.py # Chat/Cypher prompt templates
21+
22+
├── analyzers/
23+
│ ├── analyzer.py # Abstract analyzer base class
24+
│ ├── source_analyzer.py # File scanning + analyzer dispatch
25+
│ ├── python/analyzer.py # Python analyzer
26+
│ ├── java/analyzer.py # Java analyzer
27+
│ ├── csharp/analyzer.py # C# analyzer
28+
│ └── c/analyzer.py # Present in tree, but not registered
29+
30+
├── entities/ # Entity/File wrappers and encoders
31+
├── git_utils/ # Git history graph and repo utilities
32+
└── code_coverage/ # Coverage helpers
33+
```
34+
35+
## 2. HTTP Layer (`api/index.py`)
36+
37+
### 2.1 Application and routing
38+
39+
- The backend app is `FastAPI()`.
40+
- All API routes are mounted under `/api/...`.
41+
- A catch-all route serves static files from `app/dist` and falls back to `index.html` for the React SPA.
42+
43+
### 2.2 Authentication dependencies
44+
45+
`api/index.py` defines two FastAPI dependencies:
46+
47+
- `public_or_auth`: used by read-only endpoints. If `CODE_GRAPH_PUBLIC=1`, the request is allowed without auth; otherwise it checks the `Authorization` header against `SECRET_TOKEN`.
48+
- `token_required`: used by mutating endpoints and always checks the `Authorization` header against `SECRET_TOKEN`.
49+
50+
The current `_verify_token()` helper also treats a missing `SECRET_TOKEN` as allowing requests with no `Authorization` header.
51+
52+
### 2.3 Request models
53+
54+
The API uses Pydantic request models for POST bodies, including:
55+
56+
- `RepoRequest`
57+
- `NeighborsRequest`
58+
- `AutoCompleteRequest`
59+
- `FindPathsRequest`
60+
- `ChatRequest`
61+
- `AnalyzeFolderRequest`
62+
- `AnalyzeRepoRequest`
63+
- `SwitchCommitRequest`
64+
65+
### 2.4 Endpoint inventory
66+
67+
**Read endpoints** (`public_or_auth`):
68+
69+
- `GET /api/graph_entities`
70+
- `POST /api/get_neighbors`
71+
- `POST /api/auto_complete`
72+
- `GET /api/list_repos`
73+
- `POST /api/repo_info`
74+
- `POST /api/find_paths`
75+
- `POST /api/chat`
76+
- `POST /api/list_commits`
77+
78+
**Mutating endpoints** (`token_required`):
79+
80+
- `POST /api/analyze_folder`
81+
- `POST /api/analyze_repo`
82+
- `POST /api/switch_commit`
83+
84+
### 2.5 Async behavior
85+
86+
The FastAPI handlers are `async def`, but several heavy operations are still blocking and are moved off the event loop with `asyncio.get_running_loop().run_in_executor(...)`:
87+
88+
- local folder analysis
89+
- repository clone + analysis
90+
- LLM chat work
91+
- commit switching
92+
93+
## 3. Domain Modules
94+
95+
### 3.1 `graph.py`
96+
97+
`Graph` is the core FalkorDB interface used for code-graph mutations and queries. It also exposes helpers such as:
98+
99+
- `get_sub_graph()`
100+
- `get_neighbors()`
101+
- `add_entity()`
102+
- `connect_entities()`
103+
- `find_paths()`
104+
- `stats()`
105+
- backlog helpers used during git-history processing
106+
107+
Async route handlers use `AsyncGraphQuery` and `async_get_repos()` for non-blocking access patterns.
108+
109+
### 3.2 `project.py`
110+
111+
`Project` represents either:
112+
113+
- a cloned git repository via `Project.from_git_repository(url)`, or
114+
- a local repository via `Project.from_local_repository(path)`.
115+
116+
Its two main orchestration steps are:
117+
118+
- `analyze_sources(ignore)`
119+
- `process_git_history(ignore)`
120+
121+
### 3.3 `analyzers/source_analyzer.py`
122+
123+
`SourceAnalyzer` walks the repository tree, picks a registered analyzer by file extension, and builds the code graph.
124+
125+
Registered analyzers in the current code:
126+
127+
- `.py` -> `PythonAnalyzer`
128+
- `.java` -> `JavaAnalyzer`
129+
- `.cs` -> `CSharpAnalyzer`
130+
131+
The C analyzer source exists, but `.c` and `.h` registrations are commented out.
132+
133+
### 3.4 `git_utils/`
134+
135+
Git history is modeled as a separate FalkorDB graph per repository (for example `{repo_name}_git`).
136+
137+
Key pieces:
138+
139+
- `GitGraph` / `AsyncGitGraph`
140+
- `build_commit_graph(...)`
141+
- `switch_commit(...)`
142+
- helper functions for diff classification and ignore checks
143+
144+
### 3.5 `info.py`
145+
146+
Repository metadata is stored via Redis-compatible access backed by FalkorDB connection settings. Stored fields include:
147+
148+
- `repo_url`
149+
- `commit`
150+
151+
### 3.6 `llm.py`
152+
153+
Chat requests use GraphRAG-SDK with LiteLLM:
154+
155+
- default `MODEL_NAME` is `gemini/gemini-flash-lite-latest`
156+
- the backend creates a `KnowledgeGraph` bound to the repository graph
157+
- `ask()` offloads the synchronous chat session call to a worker thread
158+
159+
## 4. Runtime and Environment
160+
161+
### 4.1 Local development
162+
163+
Typical backend dev command:
164+
165+
```bash
166+
uv run uvicorn api.index:app --host 127.0.0.1 --port 5000 --reload
167+
```
168+
169+
Typical frontend dev command:
170+
171+
```bash
172+
cd app && npm run dev
173+
```
174+
175+
`app/vite.config.ts` proxies `/api` requests to `http://127.0.0.1:5000` during frontend development.
176+
177+
### 4.2 Production/container startup
178+
179+
The checked-in production entrypoints use Uvicorn, not Flask:
180+
181+
- `make run-prod`
182+
- `start.sh`
183+
- Docker image entrypoint (`/start.sh`)
184+
185+
### 4.3 Important environment variables
186+
187+
- `FALKORDB_HOST`
188+
- `FALKORDB_PORT`
189+
- `FALKORDB_USERNAME`
190+
- `FALKORDB_PASSWORD`
191+
- `SECRET_TOKEN`
192+
- `CODE_GRAPH_PUBLIC`
193+
- `ALLOWED_ANALYSIS_DIR`
194+
- `MODEL_NAME`
195+
- provider-specific LiteLLM credential(s), such as `GEMINI_API_KEY` for the default model
196+
197+
## 5. Storage Model
198+
199+
### 5.1 Code graph
200+
201+
The main repository graph lives in FalkorDB and contains entities such as:
202+
203+
- `File`
204+
- `Class`
205+
- `Function`
206+
- `Interface`
207+
208+
Relationships include:
209+
210+
- `DEFINES`
211+
- `CALLS`
212+
- `EXTENDS`
213+
- `IMPLEMENTS`
214+
215+
### 5.2 Git graph
216+
217+
Commit history is stored in a second graph named `{repo_name}_git`, with commit metadata and parent/child edges.
218+
219+
### 5.3 Repository metadata
220+
221+
Repository URL and current commit are stored in Redis-style hashes keyed as `{repo_name}_info`.
222+
223+
## 6. Request Flows
224+
225+
### 6.1 `POST /api/analyze_repo`
226+
227+
1. FastAPI validates the request body with `AnalyzeRepoRequest`.
228+
2. `token_required` checks the `Authorization` header.
229+
3. `Project.from_git_repository()` clones the repo locally.
230+
4. `analyze_sources()` builds the code graph.
231+
5. `process_git_history()` builds the repository's git graph.
232+
6. The endpoint returns `{"status": "success"}`.
233+
234+
### 6.2 `POST /api/chat`
235+
236+
1. FastAPI validates `repo` and `msg`.
237+
2. `public_or_auth` enforces auth/public rules.
238+
3. `ask()` creates a GraphRAG chat session for the repository graph.
239+
4. LiteLLM generates Cypher and a natural-language response.
240+
5. The endpoint returns `{"status": "success", "response": ...}`.
241+
242+
## 7. Key Takeaways
243+
244+
- The backend is now FastAPI + Uvicorn, not Flask.
245+
- All public API paths are under `/api/...`.
246+
- The React app can be served by the backend from `app/dist`.
247+
- Most backend logic remains framework-agnostic and reusable.
248+
- Supported analyzers are currently Python, Java, and C#.

QUICK_REFERENCE.txt

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
╔════════════════════════════════════════════════════════════════════════════╗
2+
║ CodeGraph FastAPI Backend - QUICK REFERENCE ║
3+
╚════════════════════════════════════════════════════════════════════════════╝
4+
5+
BACKEND SNAPSHOT
6+
─────────────────────────────────────────────────────────────────────────────
7+
Backend app: api.index:app
8+
Framework: FastAPI
9+
Server: Uvicorn
10+
Frontend: React + Vite in app/
11+
Built UI serving: FastAPI serves app/dist when it exists
12+
Supported analyzers: Python, Java, C#
13+
C analyzer: Present in source tree, but currently disabled
14+
15+
KEY PATHS
16+
─────────────────────────────────────────────────────────────────────────────
17+
api/index.py FastAPI app, auth deps, API routes, SPA serving
18+
api/project.py Repo clone/local analysis orchestration
19+
api/analyzers/source_analyzer.py
20+
Registered analyzers (.py, .java, .cs)
21+
api/llm.py GraphRAG + LiteLLM chat integration
22+
api/graph.py FalkorDB graph access
23+
api/git_utils/ Git history graph + commit switching
24+
app/vite.config.ts Dev proxy from /api -> http://127.0.0.1:5000
25+
Makefile Common install/build/run/test/lint commands
26+
start.sh Container entrypoint (starts uvicorn)
27+
28+
COMMON COMMANDS
29+
─────────────────────────────────────────────────────────────────────────────
30+
Install backend deps: uv sync --all-extras
31+
Install frontend deps: npm install --prefix ./app
32+
Install e2e deps: npm install
33+
34+
Backend dev server: uv run uvicorn api.index:app --host 127.0.0.1 --port 5000 --reload
35+
Frontend dev server: cd app && npm run dev
36+
Prod-style serve: npm --prefix ./app run build && uv run uvicorn api.index:app --host 0.0.0.0 --port 5000
37+
38+
Make targets: make install | build-dev | build-prod | run-dev |
39+
run-prod | test | lint | e2e | clean
40+
41+
AUTHENTICATION RULES
42+
─────────────────────────────────────────────────────────────────────────────
43+
Header format: Authorization: Bearer <SECRET_TOKEN>
44+
(raw token string also works)
45+
Read endpoints: public_or_auth dependency
46+
Mutating endpoints: token_required dependency
47+
Public mode: CODE_GRAPH_PUBLIC=1 skips auth on read endpoints
48+
Important detail: If SECRET_TOKEN is unset, requests without an
49+
Authorization header are accepted by the current
50+
implementation
51+
52+
IMPORTANT ENV VARS
53+
─────────────────────────────────────────────────────────────────────────────
54+
FALKORDB_HOST FalkorDB hostname (default: localhost)
55+
FALKORDB_PORT FalkorDB port (default: 6379)
56+
FALKORDB_USERNAME Optional FalkorDB username
57+
FALKORDB_PASSWORD Optional FalkorDB password
58+
SECRET_TOKEN Token checked by protected endpoints
59+
CODE_GRAPH_PUBLIC Set to 1 to make read endpoints public
60+
ALLOWED_ANALYSIS_DIR Limits /api/analyze_folder to one directory tree
61+
MODEL_NAME LiteLLM model for /api/chat
62+
default: gemini/gemini-flash-lite-latest
63+
GEMINI_API_KEY Example provider credential for the default model
64+
HOST / PORT Optional bind settings for start.sh / make run-*
65+
66+
API ENDPOINTS
67+
─────────────────────────────────────────────────────────────────────────────
68+
GET /api/list_repos
69+
GET /api/graph_entities?repo=<name>
70+
POST /api/get_neighbors
71+
POST /api/auto_complete
72+
POST /api/repo_info
73+
POST /api/find_paths
74+
POST /api/chat
75+
POST /api/list_commits
76+
POST /api/analyze_folder (requires token dependency)
77+
POST /api/analyze_repo (requires token dependency)
78+
POST /api/switch_commit (requires token dependency)
79+
80+
ANALYSIS NOTES
81+
─────────────────────────────────────────────────────────────────────────────
82+
/api/analyze_folder The requested path must be inside ALLOWED_ANALYSIS_DIR
83+
/api/analyze_repo Clones the repository, analyzes sources, then builds
84+
git history
85+
/api/chat Uses GraphRAG-SDK + LiteLLM against the repo graph
86+
/api/list_commits Reads from the separate {repo_name}_git graph
87+
88+
DEVELOPMENT NOTES
89+
─────────────────────────────────────────────────────────────────────────────
90+
Vite dev URL: http://localhost:3000
91+
Backend API URL: http://127.0.0.1:5000/api/...
92+
Built app URL: http://localhost:5000
93+
Root package.json: Playwright only
94+
app/package.json: Frontend build/lint/dev scripts
95+
Tests: make test runs pytest, but some legacy analyzer/
96+
git-history tests still need maintenance on a
97+
clean checkout

0 commit comments

Comments
 (0)