AI Code Review Agent:用多 Agent 架构做自动化代码审查

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...

完整项目组合:

相关推荐
knight_9___2 小时前
LLM工具调用面试篇5
人工智能·python·深度学习·面试·职场和发展·llm·agent
qcx233 小时前
【AI Agent通识九课】01 · Agent 和 ChatGPT 到底差在哪?
人工智能·ai·chatgpt·agent
维元码簿4 小时前
Claude Code 深度拆解:多 Agent 协作 4 — Swarm 团队、Coordinator 指挥官与远程 Agent
ai·agent·claude code·ai coding
冬奇Lab5 小时前
一天一个开源项目(第91篇):RuFlo - Github趋势榜第一,让 AI 像蜂群一样协同作战的多智能体编排引擎
开源·agent·ai编程
HuaCode5 小时前
如何通过华为CodeArts智能体帮我快速自动安装hermes
ai·agent·codearts·hermes
运维开发王义杰14 小时前
DevOps Agent的自我进化:AI如何积累成功经验并形成程序性记忆?
agent
RxGc18 小时前
微软AI Agent框架深度测评:Microsoft Agent Framework 1.0 vs OpenClaw/Claude企业级能力对比
人工智能·agent
HIT_Weston20 小时前
68、【Agent】【OpenCode】用户对话提示词(任务执行流程)
人工智能·agent·opencode
越来越不懂~21 小时前
Med-Copilot-7B CPU 本地部署全过程总结
copilot·agent