prisha-singla-dev/multi-agent-code-review

GitHub: prisha-singla-dev/multi-agent-code-review

基于 LangGraph 与 Gemini 的多智能体 AI 代码审查系统,从安全、性能、逻辑、风格四个维度自动化分析代码并生成评分与修复建议。

Stars: 2 | Forks: 1

# 🤖 CodeSentinel — 多智能体 AI 代码审查系统 🔗 **在线演示:** [multi-agent-code-review-iota.vercel.app](https://multi-agent-code-review-iota.vercel.app) 📡 **API:** [codesentinel-backend-cqfi.onrender.com](https://codesentinel-backend-cqfi.onrender.com/docs) ![Python](https://img.shields.io/badge/Python-3.10+-blue?logo=python) ![FastAPI](https://img.shields.io/badge/FastAPI-0.115-green?logo=fastapi) ![LangGraph](https://img.shields.io/badge/LangGraph-StateGraph-orange) ![React](https://img.shields.io/badge/React_18-Vite-61DAFB?logo=react) ![Gemini](https://img.shields.io/badge/Gemini-2.5_Flash-4285F4?logo=google) ![License](https://img.shields.io/badge/license-MIT-green) ## ✨ 功能说明 CodeSentinel 会将你的代码通过 4 个独立的 AI 智能体进行审查,每个智能体专注于不同的审查维度。随后,**高级工程师综合器** 会将所有发现合并为最终的通过/拒绝结论,并给出满分 100 分的总体评分。 | 智能体 | 关注领域 | |-------|------------| | 🔒 **SecurityAgent** | SQL 注入、XSS、硬编码密钥、OWASP Top 10、shell 注入 | | ⚡ **PerformanceAgent** | N+1 查询、O(n²) 复杂度、内存泄漏、低效循环 | | 🧠 **LogicAgent** | 边缘情况、空值处理、差一错误、静默失败、除以零 | | ✨ **StyleAgent** | PEP8、命名规范、违反 DRY 原则、缺失类型提示、docstring | ## 🏗️ 架构 ``` GitHub PR URL / Pasted Code │ ▼ FastAPI Backend │ ▼ LangGraph Orchestrator (Sequential — rate-limit safe) │ ┌──────┼──────┬──────┐ ▼ ▼ ▼ ▼ Security Perf Logic Style Agent Agent Agent Agent └──────┼──────┴──────┘ │ ▼ Synthesizer (Senior Engineer LLM) │ ┌─────┴─────┐ ▼ ▼ React UI GitHub PR Comment (live) (auto-posted) ``` ## 🚀 功能特点 - **4 个专门的 AI 智能体** — 每个专注于一个审查维度,并具备各自的错误恢复机制 - **LangGraph StateGraph 编排** — 顺序 pipeline,任何智能体失败时均有安全回退机制 - **7 个模型的 LLM 回退链** — Gemini 2.5 Flash → Gemini 2.0 Flash → 5 个 OpenRouter 免费模型,确保服务永不中断 - **GitHub webhook 集成** — 在 open / synchronize / reopen 时自动审查每个 PR - **自动化 PR 评论** — 直接在 PR 上发布带有严重性级别的结构化 Markdown 审查 - **React 前端** — 粘贴代码或输入 GitHub PR URL 即可进行即时审查 - **严重性级别** — CRITICAL / HIGH / MEDIUM / LOW / INFO,并附带具体行号引用 - **HMAC-SHA256 webhook 验证** — 以加密方式验证每个 GitHub payload - **演示模式** — 无需消耗 API 配额即可获得即时模拟响应,方便进行演示 - **零成本** — 完全部署在免费层级 (Render + Vercel) ## 🛠️ 技术栈 | 层级 | 技术 | |-------|-----------| | Backend | Python 3.10, FastAPI, Uvicorn | | 编排 | LangGraph (StateGraph), LangChain Core | | AI — 主力 | Google Gemini 2.5 Flash, Gemini 2.0 Flash | | AI — 回退 | OpenRouter: qwen3-coder, llama-3.3-70b, gpt-oss-120b (均为免费) | | Frontend | React 18, Vite | | Webhook 安全 | HMAC-SHA256 签名验证 | | HTTP Client | httpx (async) | | 部署 | Render (backend), Vercel (frontend) | | 开发隧道 | ngrok (本地 webhook 测试) | ## ⚡ 快速开始 ### 前置条件 - Python 3.10+ - Node.js 18+ - Google Gemini API 密钥 — 可在 [aistudio.google.com](https://aistudio.google.com) 免费获取 ### 1. 克隆并安装 ``` git clone https://github.com/prisha-singla-dev/multi-agent-code-review.git cd multi-agent-code-review python -m venv venv # Windows venv\Scripts\activate # Mac/Linux source venv/bin/activate pip install -r requirements.txt ``` ### 2. 配置环境 ``` cp .env.example .env ``` 编辑 `.env`: ``` # AI provider(主要) GEMINI_API_KEY=your_gemini_key_here # AI provider(备选 — 在 openrouter.ai 免费使用) OPENROUTER_API_KEY=your_openrouter_key_here # GitHub 集成 GITHUB_TOKEN=ghp_your_token_here GITHUB_WEBHOOK_SECRET=your_webhook_secret_here # 设置为 true 以进行无需任何 API 调用的即时演示 DEMO_MODE=false ``` ### 3. 启动后端 ``` uvicorn backend.main:app --reload --port 8000 ``` ### 4. 启动前端 ``` cd frontend npm install npm run dev ``` 打开 [http://localhost:3000](http://localhost:3000) ## 🔧 GitHub Webhook 设置 要以评论形式进行自动 PR 审查: ### 本地开发 (ngrok) ``` # Terminal 1 — backend uvicorn backend.main:app --reload --port 8000 # Terminal 2 — 暴露到互联网 ngrok http 8000 ``` 在 GitHub 上注册:**Repo → Settings → Webhooks → Add webhook** - Payload URL: `https://YOUR-NGROK-URL.ngrok-free.app/webhook/github` - Content type: `application/json` - Secret: 你的 `GITHUB_WEBHOOK_SECRET` - 事件:仅限 **Pull requests** ### 生产环境 (如果已部署,则此步骤已完成) Webhook URL: `https://codesentinel-backend-cqfi.onrender.com/webhook/github` ## 🧪 测试 ``` # 完整 webhook 测试套件(服务器必须正在运行) python test_webhook.py # 在已部署的 PR 上手动触发真实 review python trigger_review.py # 调试:检查来自运行中 backend 的原始 API 响应结构 python debug_response.py ``` ## 📁 项目结构 ``` multi-agent-code-review/ ├── backend/ │ ├── main.py # FastAPI app, CORS, routes │ ├── webhook.py # GitHub webhook receiver + PR comment formatter │ ├── agents/ │ │ ├── security_agent.py # OWASP, injections, hardcoded secrets │ │ ├── performance_agent.py # Complexity, N+1, memory │ │ ├── logic_agent.py # Edge cases, null handling, silent failures │ │ └── style_agent.py # PEP8, naming, DRY, type hints │ ├── orchestrator/ │ │ └── graph.py # LangGraph StateGraph pipeline + synthesizer │ ├── models/ │ │ └── schemas.py # Pydantic models (Issue, AgentReview, ReviewResponse) │ └── utils/ │ ├── llm.py # 7-model fallback chain (Gemini + OpenRouter) │ ├── github.py # GitHub PR diff fetcher │ └── mock_review.py # Demo mode realistic mock data ├── frontend/ │ ├── src/ │ │ ├── App.jsx # Main app shell, routing, state │ │ └── components/ │ │ ├── CodeInput.jsx # Code editor + PR URL input │ │ └── ReviewResults.jsx # Agent cards, score rings, issue display │ ├── vercel.json # SPA routing config │ └── vite.config.js # Dev proxy + build config ├── test_webhook.py # 6-test webhook suite ├── trigger_review.py # Manual PR trigger against live backend ├── debug_response.py # Raw API response inspector ├── test_bad_code.py # Intentionally buggy code for demos ├── render.yaml # Render deployment config ├── .env.example # Environment variable template ├── requirements.txt # Pinned Python dependencies ├── DEPLOYMENT.md # Full Render + Vercel deployment guide └── README.md ``` ## 🔑 API 参考 | 方法 | Endpoint | 描述 | |--------|----------|-------------| | `GET` | `/` | 服务状态 | | `GET` | `/health` | 健康检查 + 配置信息 | | `POST` | `/review` | 提交代码或 PR URL 进行多智能体审查 | | `POST` | `/webhook/github` | GitHub webhook 接收器 (HMAC 验证) | | `POST` | `/webhook/trigger` | 用于测试的手动审查触发器 | 交互式文档 (在线):[codesentinel-backend-cqfi.onrender.com/docs](https://codesentinel-backend-cqfi.onrender.com/docs) ## 🎯 审查输出示例 ``` 🔒 Security Agent — Score: 10/100 🔴 [CRITICAL] (line 12) Hardcoded API key found in source code 💡 Use os.getenv('API_KEY') or a secrets manager 🔴 [CRITICAL] (line 20) SQL injection via unsanitized user input 💡 Use parameterized queries: cursor.execute('SELECT * FROM users WHERE id = %s', (id,)) ⚡ Performance Agent — Score: 60/100 🟠 [HIGH] (line 44) O(n²) complexity in find_duplicates() 💡 Use a set for O(n) duplicate detection 🧠 Logic Agent — Score: 70/100 🟠 [HIGH] (line 58) Division by zero not handled 💡 Add: if b == 0: raise ValueError("b cannot be zero") ✨ Style Agent — Score: 80/100 🟡 [MEDIUM] (line 3) Function name 'x' is not descriptive 💡 Rename to reflect purpose (e.g., 'calculate_total') 📋 Final Recommendation This code is not ready to merge. The most critical concerns are the hardcoded API key and SQL injection vulnerability which pose immediate security risks. Resolve all CRITICAL issues before resubmitting. Overall Score: 55/100 | Total Issues: 8 ``` ## 🌐 部署 完全部署在免费层级 — 无需信用卡。 | 服务 | 提供商 | URL | |---------|----------|-----| | 前端 | Vercel | [multi-agent-code-review-iota.vercel.app](https://multi-agent-code-review-iota.vercel.app) | | 后端 | Render | [codesentinel-backend-cqfi.onrender.com](https://codesentinel-backend-cqfi.onrender.com) | 请参阅 [DEPLOYMENT.md](./DEPLOYMENT.md) 获取完整的逐步部署指南。 ## 📄 许可证 MIT — 可免费使用、fork 和二次开发。 ## 🙋 作者 **Prisha Singla** — [GitHub](https://github.com/prisha-singla-dev) · [LinkedIn](https://linkedin.com/in/prisha-singla) 本项目作为一个作品集构建,旨在展示多智能体 AI 系统设计、LangGraph 编排、生产级可靠性工程以及全栈部署能力。
标签:AV绕过, FastAPI, Gemini, LangGraph, React, Syscalls, 人工智能, 代码审查, 多智能体, 用户模式Hook绕过, 逆向工具