-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathapi.py
More file actions
284 lines (226 loc) · 9.32 KB
/
api.py
File metadata and controls
284 lines (226 loc) · 9.32 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
Architecture Review Agent - FastAPI Backend
=============================================
Exposes the smart_parse → analyze_risks → generate_excalidraw_elements →
export_png → build_review_report pipeline as REST endpoints.
Serves the React frontend static files in production.
"""
import asyncio
import io
import json
import logging
import os
import re
import uuid
from pathlib import Path
from typing import Optional
from dotenv import load_dotenv
load_dotenv(override=True)
from fastapi import FastAPI, File, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from tools import (
smart_parse,
infer_architecture_llm,
analyze_risks,
generate_excalidraw_elements,
build_component_map,
save_excalidraw_file,
export_png,
build_review_report,
)
# ---------------------------------------------------------------------------
# App
# ---------------------------------------------------------------------------
logger = logging.getLogger("arch-review.api")
# Maximum input size (characters) to prevent abuse
MAX_INPUT_SIZE = 500_000 # ~500 KB of text
app = FastAPI(
title="Architecture Review Agent API",
description="AI Architecture Reviewer & Diagram Generator",
version="1.0.0",
)
# CORS: restrict to localhost during development.
# In production, set ALLOWED_ORIGINS env var to your domain(s), comma-separated.
_allowed_origins = os.getenv("ALLOWED_ORIGINS", "http://localhost:5173,http://localhost:8000").split(",")
app.add_middleware(
CORSMiddleware,
allow_origins=_allowed_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Output directory for generated files
OUTPUT_DIR = Path(__file__).parent / "output"
OUTPUT_DIR.mkdir(exist_ok=True)
# ---------------------------------------------------------------------------
# Request / Response Models
# ---------------------------------------------------------------------------
class ReviewRequest(BaseModel):
content: str
force_infer: bool = False
class InferRequest(BaseModel):
content: str
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _validate_run_id(run_id: str) -> str:
"""Sanitise run_id to prevent path traversal - must be a short hex string."""
if not re.fullmatch(r"[0-9a-fA-F]{1,16}", run_id):
raise HTTPException(status_code=400, detail="Invalid run_id format")
return run_id
def _run_pipeline(parsed: dict) -> dict:
"""Run risk analysis → diagram → component map → report."""
# Risk analysis
if parsed.get("llm_risks"):
llm_risks = parsed["llm_risks"]
risks: dict = {"critical": [], "high": [], "medium": [], "low": []}
for r in llm_risks:
risks[r.get("severity", "medium")].append(r)
risks["summary"] = {
"total": len(llm_risks),
"critical": len(risks["critical"]),
"high": len(risks["high"]),
"medium": len(risks["medium"]),
"low": len(risks["low"]),
}
else:
risks = analyze_risks(parsed["components"], parsed["connections"])
# Component map
comp_map = build_component_map(parsed["components"], parsed["connections"])
# Diagram generation
file_elems = generate_excalidraw_elements(
parsed["components"], parsed["connections"]
)
# Save files with unique ID to avoid collisions
run_id = uuid.uuid4().hex[:8]
excalidraw_path = str(OUTPUT_DIR / f"architecture_{run_id}.excalidraw")
png_path = str(OUTPUT_DIR / f"architecture_{run_id}.png")
saved = save_excalidraw_file(file_elems["elements_json"], excalidraw_path)
png_saved = export_png(
parsed["components"], parsed["connections"], png_path
)
# Read excalidraw file
with open(saved, "r", encoding="utf-8") as f:
excalidraw_file = json.load(f)
diagram_info = {
"element_count": file_elems["element_count"],
"local_file": saved,
"png_file": png_saved,
"excalidraw_file": excalidraw_file,
"run_id": run_id,
}
report = build_review_report(parsed, risks, comp_map, diagram_info)
return report
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@app.get("/api/health")
async def health():
return {"status": "ok", "service": "Architecture Review Agent"}
@app.post("/api/review")
async def review_architecture(req: ReviewRequest):
"""Full architecture review pipeline."""
if not req.content.strip():
raise HTTPException(status_code=400, detail="Content is required")
if len(req.content) > MAX_INPUT_SIZE:
raise HTTPException(status_code=413, detail=f"Input too large (max {MAX_INPUT_SIZE:,} characters)")
try:
if req.force_infer:
parsed = await infer_architecture_llm(req.content)
if parsed.get("error"):
# Fallback to rule-based
from tools import parse_architecture
parsed = parse_architecture(req.content)
else:
parsed = await smart_parse(req.content)
if not parsed.get("components"):
raise HTTPException(
status_code=422,
detail="No components could be extracted from the input",
)
report = _run_pipeline(parsed)
return JSONResponse(content=report)
except HTTPException:
raise
except Exception as exc:
logger.exception("Review pipeline failed")
raise HTTPException(status_code=500, detail="Internal server error - check server logs for details")
@app.post("/api/review/upload")
async def review_upload(file: UploadFile = File(...), force_infer: bool = False):
"""Upload a file for architecture review."""
content = (await file.read()).decode("utf-8", errors="replace")
if not content.strip():
raise HTTPException(status_code=400, detail="Uploaded file is empty")
if len(content) > MAX_INPUT_SIZE:
raise HTTPException(status_code=413, detail=f"File too large (max {MAX_INPUT_SIZE:,} characters)")
try:
if force_infer:
parsed = await infer_architecture_llm(content)
if parsed.get("error"):
from tools import parse_architecture
parsed = parse_architecture(content)
else:
parsed = await smart_parse(content)
if not parsed.get("components"):
raise HTTPException(
status_code=422,
detail="No components could be extracted from the uploaded file",
)
report = _run_pipeline(parsed)
return JSONResponse(content=report)
except HTTPException:
raise
except Exception as exc:
logger.exception("Upload pipeline failed")
raise HTTPException(status_code=500, detail="Internal server error - check server logs for details")
@app.post("/api/infer")
async def infer_architecture(req: InferRequest):
"""LLM inference only - extract architecture from unstructured text."""
if not req.content.strip():
raise HTTPException(status_code=400, detail="Content is required")
if len(req.content) > MAX_INPUT_SIZE:
raise HTTPException(status_code=413, detail=f"Input too large (max {MAX_INPUT_SIZE:,} characters)")
try:
result = await infer_architecture_llm(req.content)
return JSONResponse(content=result)
except Exception as exc:
logger.exception("Inference failed")
raise HTTPException(status_code=500, detail="Internal server error - check server logs for details")
@app.get("/api/download/png/{run_id}")
async def download_png(run_id: str):
"""Download generated PNG diagram."""
run_id = _validate_run_id(run_id)
path = OUTPUT_DIR / f"architecture_{run_id}.png"
if not path.exists():
raise HTTPException(status_code=404, detail="PNG not found")
return FileResponse(
str(path), media_type="image/png", filename="architecture.png"
)
@app.get("/api/download/excalidraw/{run_id}")
async def download_excalidraw(run_id: str):
"""Download generated Excalidraw file."""
run_id = _validate_run_id(run_id)
path = OUTPUT_DIR / f"architecture_{run_id}.excalidraw"
if not path.exists():
raise HTTPException(status_code=404, detail="Excalidraw file not found")
return FileResponse(
str(path),
media_type="application/json",
filename="architecture.excalidraw",
)
# ---------------------------------------------------------------------------
# Serve React frontend in production
# ---------------------------------------------------------------------------
_FRONTEND_DIR = Path(__file__).parent / "frontend" / "dist"
if _FRONTEND_DIR.exists():
app.mount("/", StaticFiles(directory=str(_FRONTEND_DIR), html=True), name="frontend")
# ---------------------------------------------------------------------------
# Dev entry point
# ---------------------------------------------------------------------------
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "8000"))
uvicorn.run("api:app", host="0.0.0.0", port=port, reload=True)