|
| 1 | +# CodeGraph - Agent Instructions |
| 2 | + |
| 3 | +Knowledge graph visualization tool for codebases. Python FastAPI backend + React/TypeScript frontend + FalkorDB graph database. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +- **Backend** (`api/`): FastAPI, async-first. All routes in `api/index.py`. Graph ops in `api/graph.py`. LLM chat via GraphRAG in `api/llm.py`. |
| 8 | +- **Frontend** (`app/`): React 18 + TypeScript + Vite. Tailwind CSS + Radix UI. 3D force-graph visualization (D3/Force-Graph). |
| 9 | +- **Database**: FalkorDB (graph DB on Redis). Metadata in Redis key-value store. |
| 10 | +- **Analyzers** (`api/analyzers/`): tree-sitter (Python), multilspy (Java, C#). Base class in `analyzer.py`, orchestrator in `source_analyzer.py`. |
| 11 | + |
| 12 | +### Data flow |
| 13 | + |
| 14 | +1. User submits repo URL or folder path -> backend clones/reads and analyzes via language-specific analyzers |
| 15 | +2. Entities stored in FalkorDB (nodes: File, Class, Function; edges: DEFINES, CALLS, etc.) |
| 16 | +3. Metadata (URL, commit) stored in Redis |
| 17 | +4. Frontend fetches graph, renders interactive visualization |
| 18 | +5. User can chat (GraphRAG), explore neighbors, find paths |
| 19 | + |
| 20 | +## Directory structure |
| 21 | + |
| 22 | +```text |
| 23 | +api/ # Python backend |
| 24 | + index.py # FastAPI app, routes, auth, SPA serving |
| 25 | + graph.py # FalkorDB graph operations (sync + async) |
| 26 | + llm.py # GraphRAG + LiteLLM chat |
| 27 | + project.py # Repo cloning and analysis pipeline |
| 28 | + info.py # Redis metadata operations |
| 29 | + prompts.py # LLM prompt templates |
| 30 | + auto_complete.py # Prefix search |
| 31 | + analyzers/ # Language-specific code analyzers |
| 32 | + entities/ # Graph entity models |
| 33 | + git_utils/ # Git history graph construction |
| 34 | +app/ # React frontend (Vite) |
| 35 | + src/components/ # React components (ForceGraph, chat, code-graph, etc.) |
| 36 | + src/lib/ # Utilities |
| 37 | +tests/ # Pytest backend tests |
| 38 | + endpoints/ # API endpoint integration tests |
| 39 | +e2e/ # Playwright E2E tests |
| 40 | + seed_test_data.py # Test data seeder |
| 41 | +``` |
| 42 | + |
| 43 | +## Commands |
| 44 | + |
| 45 | +```bash |
| 46 | +make install # Install all deps (uv sync + npm install) |
| 47 | +make build-dev # Build frontend (dev mode) |
| 48 | +make build-prod # Build frontend (production) |
| 49 | +make run-dev # Build dev frontend + run API with reload |
| 50 | +make run-prod # Build prod frontend + run API |
| 51 | +make test # Run pytest suite |
| 52 | +make lint # Ruff + TypeScript type-check |
| 53 | +make lint-py # Ruff only |
| 54 | +make lint-fe # TypeScript type-check only |
| 55 | +make e2e # Run Playwright tests |
| 56 | +make clean # Remove build artifacts |
| 57 | +make docker-falkordb # Start FalkorDB container for testing |
| 58 | +make docker-stop # Stop test containers |
| 59 | +``` |
| 60 | + |
| 61 | +### Manual commands |
| 62 | + |
| 63 | +```bash |
| 64 | +# Backend |
| 65 | +uv run uvicorn api.index:app --host 127.0.0.1 --port 5000 --reload |
| 66 | +uv run python -m pytest tests/ --verbose |
| 67 | +uv run ruff check . |
| 68 | + |
| 69 | +# Frontend |
| 70 | +npm --prefix ./app run dev # Vite dev server (port 3000, proxies /api to 5000) |
| 71 | +npm --prefix ./app run build # Production build |
| 72 | +npm --prefix ./app run lint # Type-check |
| 73 | + |
| 74 | +# E2E |
| 75 | +npx playwright test |
| 76 | +``` |
| 77 | + |
| 78 | +## Conventions |
| 79 | + |
| 80 | +### Python (backend) |
| 81 | +- snake_case for functions/variables, PascalCase for classes |
| 82 | +- Async-first: route handlers and most graph operations are async, though api/graph.py includes some synchronous helpers |
| 83 | +- Auth: `public_or_auth` for read endpoints, `token_required` for mutating endpoints |
| 84 | +- Graph labels: PascalCase (File, Class, Function). Relations: SCREAMING_SNAKE_CASE (DEFINES, CALLS) |
| 85 | +- Linter: Ruff |
| 86 | + |
| 87 | +### TypeScript (frontend) |
| 88 | +- camelCase for functions/variables, PascalCase for components |
| 89 | +- Tailwind CSS for styling |
| 90 | +- Radix UI for headless components |
| 91 | +- Linter: tsc (type-check only) |
| 92 | + |
| 93 | +### General |
| 94 | +- Python >=3.12,<3.14. Node 20+. |
| 95 | +- Package managers: `uv` (Python), `npm` (frontend) |
| 96 | +- Environment variables: SCREAMING_SNAKE_CASE. See `.env.template` for reference. |
| 97 | + |
| 98 | +## Environment variables |
| 99 | + |
| 100 | +Key variables (see `.env.template` for full list): |
| 101 | + |
| 102 | +| Variable | Default | Purpose | |
| 103 | +|----------|---------|---------| |
| 104 | +| `FALKORDB_HOST` | `localhost` | Graph DB host | |
| 105 | +| `FALKORDB_PORT` | `6379` | Graph DB port | |
| 106 | +| `SECRET_TOKEN` | empty | API auth token | |
| 107 | +| `CODE_GRAPH_PUBLIC` | `0` | Skip auth on read endpoints when `1` | |
| 108 | +| `MODEL_NAME` | `gemini/gemini-flash-lite-latest` | LiteLLM model for chat | |
| 109 | +| `ALLOWED_ANALYSIS_DIR` | repo root | Root path for analyze_folder | |
| 110 | + |
| 111 | +## Testing |
| 112 | + |
| 113 | +- **Backend**: `make test` runs pytest against `tests/`. Endpoint tests in `tests/endpoints/`. |
| 114 | +- **E2E**: `make e2e` runs Playwright (Chromium + Firefox). Test data seeded via `e2e/seed_test_data.py`. |
| 115 | +- **CI**: GitHub Actions — `build.yml` (lint + build), `playwright.yml` (E2E, 2 shards), `release-image.yml` (Docker image). |
| 116 | + |
| 117 | +## API endpoints |
| 118 | + |
| 119 | +### Read (public_or_auth) |
| 120 | +- `GET /api/list_repos` — List indexed repos |
| 121 | +- `GET /api/graph_entities?repo=<name>` — Fetch graph entities |
| 122 | +- `POST /api/get_neighbors` — Neighboring nodes |
| 123 | +- `POST /api/auto_complete` — Prefix search |
| 124 | +- `POST /api/repo_info` — Repo stats/metadata |
| 125 | +- `POST /api/find_paths` — Paths between nodes |
| 126 | +- `POST /api/chat` — GraphRAG chat |
| 127 | +- `POST /api/list_commits` — Git commit history |
| 128 | + |
| 129 | +### Mutating (token_required) |
| 130 | +- `POST /api/analyze_folder` — Analyze local folder |
| 131 | +- `POST /api/analyze_repo` — Clone and analyze repo |
| 132 | +- `POST /api/switch_commit` — Switch to specific commit |
0 commit comments