AI Code Review Agent:用多 Agent 架构做自动化代码审查
提交一段代码,AI 自动从架构、安全、性能、风格四个维度审查,输出结构化报告和评分。本文详解多 Agent 编排 + FastAPI + Streamlit 的完整实现。
一、为什么做这个项目
代码审查是所有开发团队的刚需,但人工审查耗时费力。能不能让 AI 自动做这件事?
这个项目就是答案:用户粘贴代码 → 4 个 AI 审查 Worker 并行执行 → 生成结构化审查报告。
二、架构
markdown
用户粘贴代码
│
▼
Supervisor(编排器)
│
┌────┼────┬────┐
▼ ▼ ▼ ▼
架构 安全 性能 风格
Worker Worker Worker Worker
│ │ │ │
└────┼────┼────┘
▼
▼
Report Writer
│
▼
评分 + 问题列表 + 建议
每个 Worker 只做一件事:
| Worker | 审查维度 | 典型发现 |
|---|---|---|
| Architecture | 架构设计 | 职责不清晰、过度耦合 |
| Security | 安全漏洞 | 硬编码密码、命令注入、SQL 注入 |
| Performance | 性能优化 | 重复计算、不必要的 I/O |
| Style | 代码风格 | 命名规范、未使用导入 |
三、核心设计
1. Supervisor-Worker 模式
Supervisor 负责编排,不是再调一次 LLM,而是把 4 个 Worker 的结果合并:
python
def run_review(code: str, filename: str) -> ReviewReport:
# 结构分析
stats = analyze_structure(code, "python")
# 4 个 Worker 依次执行
all_issues = []
for worker in [check_architecture, check_security,
check_performance, check_style]:
raw = worker(code, filename)
all_issues.extend(raw)
# 计算评分
score = calculate_score(all_issues)
return ReviewReport(issues=all_issues, score=score, ...)
2. 结构化输出
用 Pydantic 定义数据结构,确保 LLM 的输出是可预测、可验证的:
python
class ReviewIssue(BaseModel):
severity: Literal["critical", "major", "minor", "info"]
category: Literal["architecture", "security", "performance", "style"]
title: str
description: str
suggestion: str
class ReviewReport(BaseModel):
score: int # 0-100
issues: List[ReviewIssue]
strengths: List[str]
suggestions: List[str]
3. 严重级别归一化
不同 LLM 返回的严重级别名称不同(HIGH / high / CRITICAL / ERROR),需要做一层映射:
python
def _normalize_severity(s: str) -> str:
mapping = {
"critical": "critical", "CRITICAL": "critical",
"high": "major", "HIGH": "major",
"medium": "minor", "MEDIUM": "minor",
"low": "info", "LOW": "info",
}
return mapping.get(s.strip(), "info")
4. 评分算法
python
def _calculate_score(issues: list) -> int:
score = 100
for i in issues:
if i.severity == "critical": score -= 15
elif i.severity == "major": score -= 8
elif i.severity == "minor": score -= 3
return max(score, 0)
四、效果演示
输入一段有问题的代码:
python
password = "admin123"
import os
os.system("cat /etc/passwd")
exec("print('hello')")
输出:
ini
评分: 55/100
发现 4 个问题
🔴 [CRITICAL] 硬编码密码
描述: 代码中直接硬编码了密码
建议: 使用环境变量替代
🔴 [CRITICAL] 命令注入
描述: os.system 执行系统命令
建议: 使用 subprocess.run 并避免 shell=True
🟡 [MAJOR] 使用 exec 执行代码
描述: exec 执行不可信输入
建议: 移除 exec 调用
五、项目结构
bash
code-review-agent/
├── app/
│ ├── api.py # FastAPI 接口
│ ├── ui.py # Streamlit 前端
│ ├── models.py # Pydantic 数据结构
│ ├── workers.py # 4 个审查 Worker(LangChain + DeepSeek)
│ ├── supervisor.py # 编排器 + 评分
│ └── tools/
│ └── code_analyzer.py # 代码结构分析
├── data/samples/
│ └── bad_code.py # 测试用例
└── requirements.txt
六、可以改进的方向
| 方向 | 技术 | 难度 |
|---|---|---|
| 支持 GitHub PR 链接 | 拉取 PR diff 审查 | ⭐⭐⭐ |
| 流式输出 | SSE 实时推送结果 | ⭐⭐ |
| 更多语言支持 | Go / Rust / Java 规则 | ⭐⭐ |
| 自定义规则 | 团队规范配置文件 | ⭐⭐⭐ |
| 历史记录 | 每次审查存入数据库 | ⭐ |
| CI/CD 集成 | GitHub Action 插件 | ⭐⭐⭐ |
七、项目链接
GitHub: github.com/cuzz123/cod...
完整项目组合:
- 客服 Agent(RAG):github.com/cuzz123/cus...
- 多 Agent 调研助手:github.com/cuzz123/res...
- RAG 知识库问答系统:github.com/cuzz123/doc...
- MCP API Gateway:github.com/cuzz123/mcp...