-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
219 lines (165 loc) · 7.27 KB
/
Copy pathmain.py
File metadata and controls
219 lines (165 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
Codebase Analyzer v2 — FastAPI entry point.
Endpoints:
POST /index Submit a GitHub repo for async semantic indexing
GET /index/{job_id}/status Poll indexing job status
POST /search Semantic search over an indexed repo
POST /ask Q&A over an indexed codebase (RAG)
GET /health Health check
"""
from __future__ import annotations
from dotenv import load_dotenv
load_dotenv()
import asyncio
import hashlib
import os
import shutil
from typing import Optional
from fastapi import FastAPI, BackgroundTasks, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from fastapi.responses import FileResponse
from app.github_utils import fetch_repo_zip, unzip_file
from app.file_analyzer import get_python_files
from app.chunker import chunk_python_file, ChunkStrategy
from app.indexer import RepoIndex
from app.llm_client import answer_with_context
from app.rate_limiter import RateLimitMiddleware
from app.security import validate_repo_url, check_repo_size
app = FastAPI(
title="Codebase Analyzer",
description="Semantic code search and Q&A over GitHub repositories using FAISS + LLMs",
version="2.0.0",
)
app.add_middleware(RateLimitMiddleware)
# In-memory job store — swap for Redis in production
_jobs: dict[str, dict] = {}
# ── Request / Response models ──────────────────────────────────────────────────
class IndexRequest(BaseModel):
repo_url: str
strategy: ChunkStrategy = ChunkStrategy.FUNCTION
class SearchRequest(BaseModel):
repo_url: str
query: str
top_k: int = 5
class AskRequest(BaseModel):
repo_url: str
question: str
top_k: int = 5
# ── Helpers ────────────────────────────────────────────────────────────────────
def _repo_id(url: str) -> str:
"""Stable short ID for a repo URL."""
return hashlib.md5(url.strip().lower().encode()).hexdigest()[:12]
# ── Background indexing task ───────────────────────────────────────────────────
async def _run_indexing(repo_url: str, strategy: ChunkStrategy, job_id: str):
tmp_dir = f"/tmp/codebase_{job_id}"
try:
# Security: check repo size before downloading
_jobs[job_id].update({"status": "validating"})
repo_info = check_repo_size(repo_url)
_jobs[job_id].update({"status": "downloading", **repo_info})
zip_path = await asyncio.to_thread(
fetch_repo_zip, repo_url, download_dir=tmp_dir
)
extract_dir = os.path.join(tmp_dir, "extracted")
os.makedirs(extract_dir, exist_ok=True)
await asyncio.to_thread(unzip_file, zip_path, extract_dir)
_jobs[job_id].update({"status": "chunking"})
py_files = [f for f in get_python_files(extract_dir) if not f.endswith("__init__.py")]
if not py_files:
raise ValueError("No Python files found in repository.")
all_chunks = []
for fp in py_files:
all_chunks.extend(chunk_python_file(fp, strategy=strategy))
if not all_chunks:
raise ValueError("No code chunks produced — repository may be empty.")
_jobs[job_id].update({
"status": "indexing",
"chunks_found": len(all_chunks),
"files_found": len(py_files),
})
repo_idx = RepoIndex(job_id)
stats = await asyncio.to_thread(repo_idx.build, all_chunks)
_jobs[job_id].update({"status": "done", **stats})
except Exception as exc:
_jobs[job_id].update({"status": "failed", "error": str(exc)})
finally:
# Clean up temp files — don't leave extracted repos on disk
shutil.rmtree(tmp_dir, ignore_errors=True)
# ── API endpoints ──────────────────────────────────────────────────────────────
@app.post("/index", summary="Submit a GitHub repo for semantic indexing")
async def index_repo(req: IndexRequest, background_tasks: BackgroundTasks):
# Security: validate URL + SSRF check before anything else
try:
validate_repo_url(req.repo_url)
except ValueError as e:
raise HTTPException(status_code=422, detail=str(e))
job_id = _repo_id(req.repo_url)
# Skip re-indexing if already done
if _jobs.get(job_id, {}).get("status") == "done" and RepoIndex.exists(job_id):
return {"job_id": job_id, "status": "already_indexed"}
# Prevent duplicate concurrent jobs
if _jobs.get(job_id, {}).get("status") in ("downloading", "chunking", "indexing"):
return {"job_id": job_id, "status": "in_progress"}
_jobs[job_id] = {"status": "queued", "repo_url": req.repo_url, "strategy": req.strategy}
background_tasks.add_task(_run_indexing, req.repo_url, req.strategy, job_id)
return {"job_id": job_id, "status": "queued"}
@app.get("/index/{job_id}/status", summary="Poll indexing job status")
def index_status(job_id: str):
if job_id not in _jobs:
raise HTTPException(status_code=404, detail=f"Job '{job_id}' not found.")
return _jobs[job_id]
@app.post("/search", summary="Semantic search over an indexed repo")
def semantic_search(req: SearchRequest):
job_id = _repo_id(req.repo_url)
if not RepoIndex.exists(job_id):
raise HTTPException(
status_code=404,
detail="Repository not indexed. POST /index first and wait for status=done.",
)
idx = RepoIndex(job_id)
results = idx.search(req.query, top_k=req.top_k)
return {
"query": req.query,
"search_ms": results[0][0].get("_search_ms") if results else None,
"results": [
{
"file": r[0]["file_path"],
"name": r[0]["name"],
"type": r[0]["chunk_type"],
"lines": f"{r[0]['start_line']}–{r[0]['end_line']}",
"score": round(r[1], 4),
"preview": r[0]["content"][:400],
}
for r in results
],
}
@app.post("/ask", summary="Q&A over an indexed codebase using retrieved context")
def ask_codebase(req: AskRequest):
job_id = _repo_id(req.repo_url)
if not RepoIndex.exists(job_id):
raise HTTPException(
status_code=404,
detail="Repository not indexed. POST /index first and wait for status=done.",
)
idx = RepoIndex(job_id)
context_chunks = idx.search(req.question, top_k=req.top_k)
answer = answer_with_context(req.question, context_chunks)
return {
"question": req.question,
"answer": answer,
"sources": [
{
"file": c[0]["file_path"],
"name": c[0]["name"],
"lines": f"{c[0]['start_line']}–{c[0]['end_line']}",
}
for c in context_chunks
],
}
@app.get("/", include_in_schema=False)
def serve_frontend():
return FileResponse("static/index.html")
@app.get("/health")
def health():
return {"status": "ok", "version": "2.0.0"}