soloshine-ai-code-review 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- soloshine_ai_code_review-0.1.0/PKG-INFO +758 -0
- soloshine_ai_code_review-0.1.0/README.md +726 -0
- soloshine_ai_code_review-0.1.0/pyproject.toml +52 -0
- soloshine_ai_code_review-0.1.0/setup.cfg +4 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/__init__.py +2 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/cli.py +980 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/cli_inbox.py +225 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/collector/__init__.py +0 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/collector/diff.py +137 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/collector/filter.py +104 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/config.py +428 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/context/__init__.py +0 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/context/auto.py +284 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/dashboard.py +296 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/hooks/__init__.py +0 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/hooks/installer.py +138 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/inbox.py +214 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/llm/__init__.py +0 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/llm/base.py +18 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/llm/ollama.py +85 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/llm/openai.py +142 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/memory/__init__.py +0 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/memory/store.py +537 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/output/__init__.py +0 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/output/notify.py +122 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/output/terminal.py +197 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/prompt/__init__.py +0 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/prompt/builder.py +146 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/reviewer/__init__.py +0 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/reviewer/async_reviewer.py +215 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/reviewer/parser.py +435 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/reviewer/screener.py +134 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/rules/__init__.py +0 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/rules/formatter.py +301 -0
- soloshine_ai_code_review-0.1.0/src/ai_review/rules/loader.py +240 -0
- soloshine_ai_code_review-0.1.0/src/soloshine_ai_code_review.egg-info/PKG-INFO +758 -0
- soloshine_ai_code_review-0.1.0/src/soloshine_ai_code_review.egg-info/SOURCES.txt +39 -0
- soloshine_ai_code_review-0.1.0/src/soloshine_ai_code_review.egg-info/dependency_links.txt +1 -0
- soloshine_ai_code_review-0.1.0/src/soloshine_ai_code_review.egg-info/entry_points.txt +2 -0
- soloshine_ai_code_review-0.1.0/src/soloshine_ai_code_review.egg-info/requires.txt +8 -0
- soloshine_ai_code_review-0.1.0/src/soloshine_ai_code_review.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,758 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: soloshine-ai-code-review
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: AI-powered code review tool with pre-commit hooks, custom LLM support, and context-aware analysis
|
|
5
|
+
Author: SoloShine
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/SoloShine/ai-code-review
|
|
8
|
+
Project-URL: Documentation, https://soloshine.github.io/ai-code-review/
|
|
9
|
+
Project-URL: Repository, https://github.com/SoloShine/ai-code-review
|
|
10
|
+
Project-URL: Issues, https://github.com/SoloShine/ai-code-review/issues
|
|
11
|
+
Keywords: code-review,ai,llm,pre-commit,git-hooks,ollama,openai
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
22
|
+
Classifier: Topic :: Software Development :: Testing
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
Requires-Dist: typer>=0.9.0
|
|
26
|
+
Requires-Dist: httpx>=0.25.0
|
|
27
|
+
Requires-Dist: pyyaml>=6.0
|
|
28
|
+
Requires-Dist: rich>=13.0.0
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
31
|
+
Requires-Dist: pytest-mock>=3.10; extra == "dev"
|
|
32
|
+
|
|
33
|
+
# AI Code Review 使用手册
|
|
34
|
+
|
|
35
|
+
> 版本 0.1.0 · AI 驱动的代码审查工具,支持 Git Hook 集成、自定义规则、LLM 审查、通知中心
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 目录
|
|
40
|
+
|
|
41
|
+
- [概述](#概述)
|
|
42
|
+
- [安装](#安装)
|
|
43
|
+
- [快速开始](#快速开始)
|
|
44
|
+
- [配置](#配置)
|
|
45
|
+
- [命令参考](#命令参考)
|
|
46
|
+
- [规则系统](#规则系统)
|
|
47
|
+
- [通知中心](#通知中心)
|
|
48
|
+
- [抑制系统](#抑制系统)
|
|
49
|
+
- [项目结构](#项目结构)
|
|
50
|
+
- [注意事项](#注意事项)
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 概述
|
|
55
|
+
|
|
56
|
+
AI Code Review 是一个本地运行的代码审查工具,通过接入 LLM(大语言模型)对代码变更进行智能审查。核心能力:
|
|
57
|
+
|
|
58
|
+
| 能力 | 说明 |
|
|
59
|
+
|------|------|
|
|
60
|
+
| **Pre-commit 拦截** | 提交前快速筛查,发现严重问题直接阻止提交 |
|
|
61
|
+
| **Post-commit 深度审查** | 提交后异步执行完整审查,生成详细报告 |
|
|
62
|
+
| **自定义规则** | YAML 格式规则文件,按文件类型自动匹配 |
|
|
63
|
+
| **记忆系统** | 跨次审查跟踪重复问题,计算风险分数 |
|
|
64
|
+
| **通知中心** | 终端 Inbox + HTML 仪表盘 + 系统弹窗三通道提醒 |
|
|
65
|
+
| **抑制管理** | 对已知且暂不处理的问题标记抑制,停止弹窗干扰 |
|
|
66
|
+
|
|
67
|
+
支持的 LLM 后端:
|
|
68
|
+
|
|
69
|
+
- **Ollama** — 本地部署,无需 API Key
|
|
70
|
+
- **OpenAI Compatible** — 任何兼容 OpenAI 接口的服务(智谱 GLM、DeepSeek、通义千问等)
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## 安装
|
|
75
|
+
|
|
76
|
+
### 环境要求
|
|
77
|
+
|
|
78
|
+
- Python >= 3.10
|
|
79
|
+
- Git
|
|
80
|
+
- LLM 后端(Ollama 或 OpenAI 兼容 API)
|
|
81
|
+
|
|
82
|
+
### 安装步骤
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# 克隆项目
|
|
86
|
+
git clone <repo-url> ai-code-review
|
|
87
|
+
cd ai-code-review
|
|
88
|
+
|
|
89
|
+
# 安装(开发模式,方便后续更新)
|
|
90
|
+
pip install -e .
|
|
91
|
+
|
|
92
|
+
# 验证安装
|
|
93
|
+
ai-review --help
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 依赖说明
|
|
97
|
+
|
|
98
|
+
核心依赖(自动安装):
|
|
99
|
+
|
|
100
|
+
| 包 | 用途 |
|
|
101
|
+
|----|------|
|
|
102
|
+
| `typer` | CLI 框架 |
|
|
103
|
+
| `httpx` | LLM API 调用 |
|
|
104
|
+
| `pyyaml` | 配置和规则文件解析 |
|
|
105
|
+
| `rich` | 终端美化输出 |
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## 快速开始
|
|
110
|
+
|
|
111
|
+
### 1. 在项目中初始化
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
cd your-project
|
|
115
|
+
ai-review init
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
该命令会:
|
|
119
|
+
- 创建 `.ai-review.yaml` 配置文件
|
|
120
|
+
- 创建 `.ai-review/` 目录结构(含 `rules/` 子目录)
|
|
121
|
+
- 安装 Git pre-commit 和 post-commit 钩子
|
|
122
|
+
|
|
123
|
+
### 2. 配置 LLM 后端
|
|
124
|
+
|
|
125
|
+
编辑 `.ai-review.yaml`,选择后端:
|
|
126
|
+
|
|
127
|
+
**使用智谱 GLM(推荐国内用户):**
|
|
128
|
+
|
|
129
|
+
```yaml
|
|
130
|
+
mode: "balanced"
|
|
131
|
+
llm:
|
|
132
|
+
backend: "openai_compatible"
|
|
133
|
+
openai_compatible:
|
|
134
|
+
base_url: "https://open.bigmodel.cn/api/coding/paas/v4"
|
|
135
|
+
api_key_env: "ZHIPU_API_KEY"
|
|
136
|
+
model: "glm-5-turbo"
|
|
137
|
+
timeout: 60
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
然后设置环境变量:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
# Linux/macOS
|
|
144
|
+
export ZHIPU_API_KEY="your-api-key"
|
|
145
|
+
|
|
146
|
+
# Windows PowerShell
|
|
147
|
+
[Environment]::SetEnvironmentVariable("ZHIPU_API_KEY", "your-api-key", "User")
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**使用 Ollama(本地部署):**
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
# 先启动 Ollama 服务
|
|
154
|
+
ollama serve
|
|
155
|
+
ollama pull qwen2.5-coder:7b
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
```yaml
|
|
159
|
+
mode: "balanced"
|
|
160
|
+
llm:
|
|
161
|
+
backend: "ollama"
|
|
162
|
+
ollama:
|
|
163
|
+
base_url: "http://localhost:11434"
|
|
164
|
+
model: "qwen2.5-coder:7b"
|
|
165
|
+
timeout: 60
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### 3. 添加审查规则
|
|
169
|
+
|
|
170
|
+
在 `.ai-review/rules/` 下创建 YAML 规则文件,例如 `frontend-rules.yaml`:
|
|
171
|
+
|
|
172
|
+
```yaml
|
|
173
|
+
name: "Frontend Code Rules"
|
|
174
|
+
description: "Vue/TypeScript frontend review rules"
|
|
175
|
+
rules:
|
|
176
|
+
- id: "no-console-log"
|
|
177
|
+
title: "禁止提交 console.log 调试语句"
|
|
178
|
+
severity: "warning"
|
|
179
|
+
enabled: true
|
|
180
|
+
applies_to:
|
|
181
|
+
extensions: [".ts", ".js", ".vue"]
|
|
182
|
+
description: "console.log 调试语句不应提交到代码库,应使用项目中的 logger 工具替代。"
|
|
183
|
+
|
|
184
|
+
- id: "no-any-type"
|
|
185
|
+
title: "禁止使用 any 类型"
|
|
186
|
+
severity: "warning"
|
|
187
|
+
enabled: true
|
|
188
|
+
applies_to:
|
|
189
|
+
extensions: [".ts", ".tsx"]
|
|
190
|
+
description: "TypeScript 中使用 any 会导致类型检查失效,应使用具体类型或泛型替代。"
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### 4. 提交代码触发审查
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
# 正常提交代码
|
|
197
|
+
git add .
|
|
198
|
+
git commit -m "feat: add user component"
|
|
199
|
+
|
|
200
|
+
# pre-commit hook 会自动运行快速筛查
|
|
201
|
+
# 如果发现 ERROR 级别问题,提交会被阻止
|
|
202
|
+
# 如果通过,post-commit hook 会异步执行完整审查
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### 5. 查看审查结果
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
# 查看未读报告
|
|
209
|
+
ai-review inbox
|
|
210
|
+
|
|
211
|
+
# 查看某份报告详情
|
|
212
|
+
ai-review inbox show <report-id>
|
|
213
|
+
|
|
214
|
+
# 打开 HTML 仪表盘
|
|
215
|
+
ai-review inbox dashboard
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## 配置
|
|
221
|
+
|
|
222
|
+
配置文件为项目根目录的 `.ai-review.yaml`。
|
|
223
|
+
|
|
224
|
+
### 完整配置项
|
|
225
|
+
|
|
226
|
+
```yaml
|
|
227
|
+
# ==================== 基础设置 ====================
|
|
228
|
+
|
|
229
|
+
# 运行模式: "balanced"(平衡,推荐) | "strict"(严格)
|
|
230
|
+
mode: "balanced"
|
|
231
|
+
|
|
232
|
+
# ==================== LLM 后端 ====================
|
|
233
|
+
|
|
234
|
+
llm:
|
|
235
|
+
# 选择后端: "ollama" | "openai_compatible"
|
|
236
|
+
backend: "openai_compatible"
|
|
237
|
+
|
|
238
|
+
# Ollama 后端配置
|
|
239
|
+
ollama:
|
|
240
|
+
base_url: "http://localhost:11434"
|
|
241
|
+
model: "qwen2.5-coder:7b"
|
|
242
|
+
timeout: 60
|
|
243
|
+
|
|
244
|
+
# OpenAI 兼容后端配置
|
|
245
|
+
openai_compatible:
|
|
246
|
+
base_url: "https://open.bigmodel.cn/api/coding/paas/v4"
|
|
247
|
+
api_key_env: "ZHIPU_API_KEY" # API Key 环境变量名
|
|
248
|
+
model: "glm-5-turbo"
|
|
249
|
+
timeout: 60
|
|
250
|
+
|
|
251
|
+
# ==================== Git Hook ====================
|
|
252
|
+
|
|
253
|
+
hooks:
|
|
254
|
+
pre_commit:
|
|
255
|
+
timeout: 30 # 快速筛查超时(秒)
|
|
256
|
+
block_on: ["CRITICAL", "ERROR"] # 遇到这些级别的问题阻止提交
|
|
257
|
+
rules_filter:
|
|
258
|
+
severities: ["error"] # pre-commit 只检查 error 级别规则
|
|
259
|
+
bypass_env_vars: [] # 设置这些环境变量可跳过审查
|
|
260
|
+
|
|
261
|
+
post_commit:
|
|
262
|
+
enabled: true # 启用 post-commit 异步审查
|
|
263
|
+
|
|
264
|
+
# ==================== 记忆系统 ====================
|
|
265
|
+
|
|
266
|
+
memory:
|
|
267
|
+
enabled: true # 启用记忆系统
|
|
268
|
+
risk_threshold: 5 # 风险分数阈值(0-10)
|
|
269
|
+
# reminder: 提醒策略(内部使用)
|
|
270
|
+
|
|
271
|
+
# ==================== 仪表盘 ====================
|
|
272
|
+
|
|
273
|
+
dashboard:
|
|
274
|
+
auto_open: true # 异步审查完成后自动打开浏览器
|
|
275
|
+
|
|
276
|
+
# ==================== 排除规则 ====================
|
|
277
|
+
|
|
278
|
+
exclude:
|
|
279
|
+
- "*.lock"
|
|
280
|
+
- "*.min.js"
|
|
281
|
+
- "*.min.css"
|
|
282
|
+
- "*.png"
|
|
283
|
+
- "*.jpg"
|
|
284
|
+
- "package-lock.json"
|
|
285
|
+
- "node_modules/"
|
|
286
|
+
- "dist/"
|
|
287
|
+
- "__pycache__/"
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### 模式说明
|
|
291
|
+
|
|
292
|
+
| 模式 | Pre-commit | Post-commit | Memory | 说明 |
|
|
293
|
+
|------|-----------|-------------|--------|------|
|
|
294
|
+
| **balanced** | 仅筛查 ERROR/CRITICAL,30s 超时 | 启用 | 启用 | 推荐日常使用 |
|
|
295
|
+
| **strict** | 筛查所有级别(含 WARNING),60s 超时 | 禁用 | 禁用 | 严格模式,适合关键分支 |
|
|
296
|
+
|
|
297
|
+
---
|
|
298
|
+
|
|
299
|
+
## 命令参考
|
|
300
|
+
|
|
301
|
+
### `ai-review init`
|
|
302
|
+
|
|
303
|
+
初始化项目,创建配置文件和安装 Git 钩子。
|
|
304
|
+
|
|
305
|
+
```bash
|
|
306
|
+
ai-review init [--mode balanced] [--backend ollama] [--model qwen2.5-coder:7b]
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### `ai-review check`
|
|
310
|
+
|
|
311
|
+
执行代码审查,支持三种模式。
|
|
312
|
+
|
|
313
|
+
**Pre-commit 模式**(由 Git Hook 自动调用):
|
|
314
|
+
|
|
315
|
+
```bash
|
|
316
|
+
ai-review check --pre-commit [--no-block]
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
**异步 Post-commit 模式**(由 Git Hook 自动调用):
|
|
320
|
+
|
|
321
|
+
```bash
|
|
322
|
+
ai-review check --async --commit <sha> [--open-dashboard]
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
**手动审查模式**:
|
|
326
|
+
|
|
327
|
+
```bash
|
|
328
|
+
# 审查当前暂存区
|
|
329
|
+
ai-review check
|
|
330
|
+
|
|
331
|
+
# 审查指定文件
|
|
332
|
+
ai-review check --path src/app.py
|
|
333
|
+
|
|
334
|
+
# 与分支对比审查
|
|
335
|
+
ai-review check --base main
|
|
336
|
+
|
|
337
|
+
# 详细输出
|
|
338
|
+
ai-review check --verbose
|
|
339
|
+
|
|
340
|
+
# 调试模式(显示 prompt 和 LLM 原始响应)
|
|
341
|
+
ai-review check --debug --verbose
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
**选项说明:**
|
|
345
|
+
|
|
346
|
+
| 选项 | 说明 |
|
|
347
|
+
|------|------|
|
|
348
|
+
| `--pre-commit` | 以 pre-commit hook 模式运行(快速筛查) |
|
|
349
|
+
| `--async` | 异步 post-commit 审查模式 |
|
|
350
|
+
| `--commit <sha>` | 指定审查的 commit(异步模式必需) |
|
|
351
|
+
| `--base <branch>` | 与指定分支对比审查 |
|
|
352
|
+
| `--path <file>` | 审查指定文件 |
|
|
353
|
+
| `--no-block` | 仅报告,不阻止提交 |
|
|
354
|
+
| `--verbose`, `-v` | 显示详细输出 |
|
|
355
|
+
| `--debug` | 显示调试信息(prompt、LLM 原始响应) |
|
|
356
|
+
| `--open-dashboard` | 审查完成后打开 HTML 仪表盘 |
|
|
357
|
+
|
|
358
|
+
### `ai-review inbox`
|
|
359
|
+
|
|
360
|
+
管理审查报告通知中心。
|
|
361
|
+
|
|
362
|
+
```bash
|
|
363
|
+
# 列出所有报告(默认命令)
|
|
364
|
+
ai-review inbox
|
|
365
|
+
ai-review inbox --unread # 仅显示未读
|
|
366
|
+
ai-review inbox --status WARNING # 按状态筛选
|
|
367
|
+
ai-review inbox --limit 10 # 限制数量
|
|
368
|
+
ai-review inbox --all # 包含已归档
|
|
369
|
+
|
|
370
|
+
# 查看报告详情(自动标记已读)
|
|
371
|
+
ai-review inbox show <report-id>
|
|
372
|
+
ai-review inbox show <report-id> --md # 在浏览器中打开 Markdown 报告
|
|
373
|
+
|
|
374
|
+
# 标记已读/未读
|
|
375
|
+
ai-review inbox read <report-id>
|
|
376
|
+
ai-review inbox unread <report-id>
|
|
377
|
+
ai-review inbox mark-all-read
|
|
378
|
+
|
|
379
|
+
# 生成并打开 HTML 仪表盘
|
|
380
|
+
ai-review inbox dashboard
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### `ai-review suppress`
|
|
384
|
+
|
|
385
|
+
管理规则抑制(对特定文件跳过特定规则的弹窗通知)。
|
|
386
|
+
|
|
387
|
+
```bash
|
|
388
|
+
# 查看已抑制的规则
|
|
389
|
+
ai-review suppress --list
|
|
390
|
+
|
|
391
|
+
# 抑制某个文件的特定规则
|
|
392
|
+
ai-review suppress src/utils/helper.ts no-console-log --reason "调试阶段暂保留"
|
|
393
|
+
|
|
394
|
+
# 抑制某个文件的所有规则
|
|
395
|
+
ai-review suppress src/legacy/old.ts --all --reason "遗留代码,暂不处理"
|
|
396
|
+
|
|
397
|
+
# 取消抑制
|
|
398
|
+
ai-review suppress src/utils/helper.ts no-console-log --cancel
|
|
399
|
+
ai-review suppress src/legacy/old.ts --all --cancel
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
> **注意**:抑制不会阻止审查本身,报告仍会包含这些问题,只是不再弹窗通知。
|
|
403
|
+
|
|
404
|
+
### `ai-review status`
|
|
405
|
+
|
|
406
|
+
查看高风险文件和待处理警告。
|
|
407
|
+
|
|
408
|
+
```bash
|
|
409
|
+
ai-review status
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### `ai-review config-show`
|
|
413
|
+
|
|
414
|
+
显示当前生效的完整配置。
|
|
415
|
+
|
|
416
|
+
```bash
|
|
417
|
+
ai-review config-show
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
---
|
|
421
|
+
|
|
422
|
+
## 规则系统
|
|
423
|
+
|
|
424
|
+
### 规则文件格式
|
|
425
|
+
|
|
426
|
+
在 `.ai-review/rules/` 目录下创建 YAML 文件,支持多个规则文件:
|
|
427
|
+
|
|
428
|
+
```yaml
|
|
429
|
+
name: "规则集名称"
|
|
430
|
+
description: "规则集描述"
|
|
431
|
+
rules:
|
|
432
|
+
- id: "rule-id" # 唯一标识符
|
|
433
|
+
title: "规则标题" # 简短描述
|
|
434
|
+
severity: "error" # 级别: error | warning | info
|
|
435
|
+
enabled: true # 是否启用
|
|
436
|
+
applies_to:
|
|
437
|
+
extensions: [".py", ".js"] # 适用的文件扩展名
|
|
438
|
+
description: "详细描述规则内容,LLM 会参考此描述进行审查。"
|
|
439
|
+
|
|
440
|
+
# 更多规则...
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
### 规则字段说明
|
|
444
|
+
|
|
445
|
+
| 字段 | 必填 | 说明 |
|
|
446
|
+
|------|------|------|
|
|
447
|
+
| `id` | 是 | 规则唯一标识,用于抑制管理和报告引用 |
|
|
448
|
+
| `title` | 是 | 规则简短标题 |
|
|
449
|
+
| `severity` | 是 | `error`(阻止提交)/ `warning`(仅警告)/ `info`(提示) |
|
|
450
|
+
| `enabled` | 否 | 默认 `true`,设为 `false` 禁用 |
|
|
451
|
+
| `applies_to.extensions` | 否 | 适用的文件扩展名列表 |
|
|
452
|
+
| `applies_to.paths` | 否 | 适用的文件路径 glob 模式 |
|
|
453
|
+
| `description` | 是 | 规则详细描述,LLM 参考此内容审查 |
|
|
454
|
+
|
|
455
|
+
### Severity 与 Pre-commit 行为
|
|
456
|
+
|
|
457
|
+
| Severity | Pre-commit 行为 | Post-commit 行为 |
|
|
458
|
+
|----------|----------------|-----------------|
|
|
459
|
+
| `error` | 匹配到即阻止提交 | 报告为 ERROR 级别 |
|
|
460
|
+
| `warning` | 不阻止提交 | 报告为 WARNING 级别 |
|
|
461
|
+
| `info` | 不参与筛查 | 报告为 INFO 级别 |
|
|
462
|
+
|
|
463
|
+
> 默认配置下,`pre_commit.rules_filter.severities` 为 `["error"]`,即只有 `severity: "error"` 的规则参与 pre-commit 筛查。如果需要更严格的筛查,可改为 `["error", "warning"]`。
|
|
464
|
+
|
|
465
|
+
### 规则示例
|
|
466
|
+
|
|
467
|
+
**前端 TypeScript/Vue 规则:**
|
|
468
|
+
|
|
469
|
+
```yaml
|
|
470
|
+
name: "Vue Frontend Rules"
|
|
471
|
+
rules:
|
|
472
|
+
- id: "no-settimeout"
|
|
473
|
+
title: "禁止使用 setTimeout"
|
|
474
|
+
severity: "error"
|
|
475
|
+
applies_to:
|
|
476
|
+
extensions: [".ts", ".js", ".vue"]
|
|
477
|
+
description: "禁止使用 setTimeout 处理时序性问题,应使用 Promise/nextTick 等可靠机制。"
|
|
478
|
+
|
|
479
|
+
- id: "promise-catch"
|
|
480
|
+
title: "Promise 必须处理错误"
|
|
481
|
+
severity: "error"
|
|
482
|
+
applies_to:
|
|
483
|
+
extensions: [".ts", ".js", ".vue"]
|
|
484
|
+
description: "所有 Promise 链必须包含 .catch() 或使用 try/catch 包裹 async/await。"
|
|
485
|
+
|
|
486
|
+
- id: "no-console-log"
|
|
487
|
+
title: "禁止提交 console.log"
|
|
488
|
+
severity: "warning"
|
|
489
|
+
applies_to:
|
|
490
|
+
extensions: [".ts", ".js", ".vue"]
|
|
491
|
+
description: "console.log 调试语句不应提交到代码库,应使用项目中的 logger 工具替代。"
|
|
492
|
+
|
|
493
|
+
- id: "no-any-type"
|
|
494
|
+
title: "禁止使用 any 类型"
|
|
495
|
+
severity: "warning"
|
|
496
|
+
applies_to:
|
|
497
|
+
extensions: [".ts", ".tsx"]
|
|
498
|
+
description: "TypeScript 中使用 any 会导致类型检查失效,应使用具体类型或泛型替代。"
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
**后端 Python 规则:**
|
|
502
|
+
|
|
503
|
+
```yaml
|
|
504
|
+
name: "Python Backend Rules"
|
|
505
|
+
rules:
|
|
506
|
+
- id: "no-eval"
|
|
507
|
+
title: "禁止使用 eval"
|
|
508
|
+
severity: "error"
|
|
509
|
+
applies_to:
|
|
510
|
+
extensions: [".py"]
|
|
511
|
+
description: "禁止使用 eval,存在严重的代码注入安全风险。"
|
|
512
|
+
|
|
513
|
+
- id: "sql-injection"
|
|
514
|
+
title: "防止 SQL 注入"
|
|
515
|
+
severity: "error"
|
|
516
|
+
applies_to:
|
|
517
|
+
extensions: [".py"]
|
|
518
|
+
description: "禁止拼接 SQL 语句,必须使用参数化查询。"
|
|
519
|
+
|
|
520
|
+
- id: "no-hardcoded-secrets"
|
|
521
|
+
title: "禁止硬编码密钥"
|
|
522
|
+
severity: "error"
|
|
523
|
+
applies_to:
|
|
524
|
+
extensions: [".py"]
|
|
525
|
+
description: "禁止在代码中硬编码 API Key、密码等敏感信息,应使用环境变量。"
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
---
|
|
529
|
+
|
|
530
|
+
## 通知中心
|
|
531
|
+
|
|
532
|
+
通知中心提供三种互补的报告查看方式:
|
|
533
|
+
|
|
534
|
+
### 1. 系统弹窗(即时提醒)
|
|
535
|
+
|
|
536
|
+
每次审查完成后自动弹出 Windows/macOS/Linux 系统通知。适合快速获知审查结果。
|
|
537
|
+
|
|
538
|
+
- Windows:优先使用 BurntToast,回退到 BalloonTip
|
|
539
|
+
- 弹窗内容包含 issue 摘要
|
|
540
|
+
- 受抑制规则控制,被抑制的 issue 不触发弹窗
|
|
541
|
+
|
|
542
|
+
### 2. 终端 Inbox(日常浏览)
|
|
543
|
+
|
|
544
|
+
```bash
|
|
545
|
+
ai-review inbox # 列出报告
|
|
546
|
+
ai-review inbox --unread # 仅看未读
|
|
547
|
+
ai-review inbox show <report-id> # 查看详情(自动标记已读)
|
|
548
|
+
ai-review inbox mark-all-read # 全部标记已读
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
在终端直接浏览、搜索、阅读报告。适合开发者日常工作流。
|
|
552
|
+
|
|
553
|
+
### 3. HTML 仪表盘(全景视图)
|
|
554
|
+
|
|
555
|
+
```bash
|
|
556
|
+
ai-review inbox dashboard # 生成并打开仪表盘
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
自动生成 `.ai-review/reports/index.html`,在浏览器中打开。功能:
|
|
560
|
+
|
|
561
|
+
- 所有报告按时间倒序排列,状态分色标记
|
|
562
|
+
- 点击展开查看每个 issue 的详情和代码建议(diff 风格)
|
|
563
|
+
- 顶部搜索栏实时过滤
|
|
564
|
+
- 未读标记(蓝点)
|
|
565
|
+
- 深色主题,适配开发者审美
|
|
566
|
+
|
|
567
|
+
> 异步审查完成后,如果 `dashboard.auto_open` 为 `true`(默认),会自动在浏览器中打开仪表盘。
|
|
568
|
+
|
|
569
|
+
---
|
|
570
|
+
|
|
571
|
+
## 抑制系统
|
|
572
|
+
|
|
573
|
+
对于已知但暂不打算处理的问题,可以抑制其弹窗通知。
|
|
574
|
+
|
|
575
|
+
### 工作机制
|
|
576
|
+
|
|
577
|
+
抑制**不会**影响审查本身:
|
|
578
|
+
- 审查照常运行
|
|
579
|
+
- 报告照常生成(包含被抑制的问题)
|
|
580
|
+
- **仅跳过弹窗通知**
|
|
581
|
+
|
|
582
|
+
### 使用方法
|
|
583
|
+
|
|
584
|
+
```bash
|
|
585
|
+
# 抑制某个文件的某条规则
|
|
586
|
+
ai-review suppress src/legacy/old.ts no-eval --reason "遗留代码,计划 Q3 重构"
|
|
587
|
+
|
|
588
|
+
# 抑制某个文件的所有规则
|
|
589
|
+
ai-review suppress src/legacy/old.ts --all --reason "遗留模块,不做审查"
|
|
590
|
+
|
|
591
|
+
# 查看已抑制项
|
|
592
|
+
ai-review suppress --list
|
|
593
|
+
|
|
594
|
+
# 取消抑制
|
|
595
|
+
ai-review suppress src/legacy/old.ts no-eval --cancel
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
### 匹配规则
|
|
599
|
+
|
|
600
|
+
抑制匹配支持三级策略,解决 LLM 输出格式与规则 ID 不一致的问题:
|
|
601
|
+
|
|
602
|
+
1. **精确匹配** — 规则 ID 完全一致(忽略大小写和下划线/连字符差异)
|
|
603
|
+
2. **子串匹配** — 抑制的 ID 包含在 LLM 输出的 rule_id 中
|
|
604
|
+
3. **关键词匹配** — 从抑制 ID 中提取关键词(如 `no-console-log` → `console`, `log`),全部出现在 issue 消息中则匹配
|
|
605
|
+
|
|
606
|
+
---
|
|
607
|
+
|
|
608
|
+
## 项目结构
|
|
609
|
+
|
|
610
|
+
```
|
|
611
|
+
your-project/
|
|
612
|
+
├── .ai-review.yaml # 项目配置文件
|
|
613
|
+
└── .ai-review/
|
|
614
|
+
├── memory.json # 记忆数据(风险分数、抑制记录、inbox 状态)
|
|
615
|
+
├── rules/ # 自定义审查规则
|
|
616
|
+
│ ├── frontend.yaml
|
|
617
|
+
│ ├── backend.yaml
|
|
618
|
+
│ └── css-rules.yaml
|
|
619
|
+
└── reports/ # 审查报告(自动生成)
|
|
620
|
+
├── index.html # HTML 仪表盘
|
|
621
|
+
├── review_<tag>_<ts>.json
|
|
622
|
+
└── review_<tag>_<ts>.md
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
### 源码结构
|
|
626
|
+
|
|
627
|
+
```
|
|
628
|
+
src/ai_review/
|
|
629
|
+
├── cli.py # 主 CLI 入口(check/init/suppress/status)
|
|
630
|
+
├── cli_inbox.py # Inbox 子命令(inbox list/show/read/dashboard)
|
|
631
|
+
├── config.py # 配置系统(分层合并、模式默认值)
|
|
632
|
+
├── dashboard.py # HTML 仪表盘生成器
|
|
633
|
+
├── inbox.py # Inbox 管理器(报告注册、已读状态)
|
|
634
|
+
├── memory/
|
|
635
|
+
│ └── store.py # 记忆系统(风险分数、抑制、衰减)
|
|
636
|
+
├── llm/
|
|
637
|
+
│ ├── base.py # LLM 提供者抽象接口
|
|
638
|
+
│ ├── ollama.py # Ollama 本地后端
|
|
639
|
+
│ └── openai.py # OpenAI 兼容后端
|
|
640
|
+
├── prompt/
|
|
641
|
+
│ └── builder.py # Prompt 构建(fast/full 两种模式)
|
|
642
|
+
├── reviewer/
|
|
643
|
+
│ ├── screener.py # Pre-commit 快速筛查器
|
|
644
|
+
│ └── parser.py # LLM 输出解析(JSON 优先 + 文本回退)
|
|
645
|
+
├── rules/
|
|
646
|
+
│ ├── loader.py # 规则加载和文件匹配
|
|
647
|
+
│ └── formatter.py # 规则格式化(prompt/JSON/HTML)
|
|
648
|
+
├── hooks/
|
|
649
|
+
│ └── installer.py # Git Hook 安装/卸载
|
|
650
|
+
└── output/
|
|
651
|
+
├── notify.py # 系统通知(跨平台,三级回退)
|
|
652
|
+
└── terminal.py # Rich 终端格式化输出
|
|
653
|
+
```
|
|
654
|
+
|
|
655
|
+
---
|
|
656
|
+
|
|
657
|
+
## 注意事项
|
|
658
|
+
|
|
659
|
+
### 环境变量
|
|
660
|
+
|
|
661
|
+
| 变量 | 用途 | 示例 |
|
|
662
|
+
|------|------|------|
|
|
663
|
+
| `ZHIPU_API_KEY` | 智谱 API Key | `export ZHIPU_API_KEY="xxx"` |
|
|
664
|
+
| `OPENAI_API_KEY` | OpenAI API Key | 默认变量名,可通过 `api_key_env` 自定义 |
|
|
665
|
+
| `AI_REVIEW_MODE` | 覆盖运行模式 | `export AI_REVIEW_MODE="strict"` |
|
|
666
|
+
|
|
667
|
+
> Windows 用户设置环境变量后需**重启终端**才能生效,或使用:
|
|
668
|
+
> ```powershell
|
|
669
|
+
> [Environment]::SetEnvironmentVariable("ZHIPU_API_KEY", "your-key", "User")
|
|
670
|
+
> ```
|
|
671
|
+
|
|
672
|
+
### 编码问题
|
|
673
|
+
|
|
674
|
+
工具内部已处理 Windows GBK 编码问题(emoji 输出、子进程中文),无需额外配置。
|
|
675
|
+
|
|
676
|
+
### 性能
|
|
677
|
+
|
|
678
|
+
| 场景 | 典型耗时 | 说明 |
|
|
679
|
+
|------|---------|------|
|
|
680
|
+
| Pre-commit 筛查 | 5-15 秒 | 仅发送 diff + error 级别规则,prompt 较短 |
|
|
681
|
+
| Post-commit 全量审查 | 10-30 秒 | 包含所有规则,返回详细报告和代码建议 |
|
|
682
|
+
| 手动审查 | 10-30 秒 | 同全量审查 |
|
|
683
|
+
|
|
684
|
+
> Pre-commit 超时默认 30 秒,超时自动放行(不阻止提交)。可在配置中调整 `hooks.pre_commit.timeout`。
|
|
685
|
+
|
|
686
|
+
### LLM 故障处理
|
|
687
|
+
|
|
688
|
+
- Pre-commit:LLM 不可用时**自动放行**,不阻止提交
|
|
689
|
+
- Post-commit:LLM 不可用时**静默跳过**,不影响工作流
|
|
690
|
+
- 解析失败:LLM 返回无法解析的内容时**自动放行**
|
|
691
|
+
|
|
692
|
+
### 报告管理
|
|
693
|
+
|
|
694
|
+
- 报告存储在 `.ai-review/reports/` 下,每份报告包含 `.json`(机器可读)和 `.md`(人类可读)
|
|
695
|
+
- 建议将 `.ai-review/reports/` 加入 `.gitignore`,报告为本地开发辅助数据
|
|
696
|
+
- HTML 仪表盘每次审查后自动重新生成,始终反映最新状态
|
|
697
|
+
|
|
698
|
+
### 记忆系统衰减
|
|
699
|
+
|
|
700
|
+
- 警告记录 30 天后自动衰减清理
|
|
701
|
+
- 风险分数基于警告出现频率计算(0-10 分)
|
|
702
|
+
- 提醒策略随重复次数递减:完整 → 简短 → 最小 → 静默
|
|
703
|
+
|
|
704
|
+
### Windows 通知
|
|
705
|
+
|
|
706
|
+
系统弹窗依赖 Windows 通知服务:
|
|
707
|
+
|
|
708
|
+
- 推荐安装 BurntToast PowerShell 模块获得最佳体验:`Install-Module -Name BurntToast -Force -Scope CurrentUser`
|
|
709
|
+
- 如通知被系统设置屏蔽,工具会自动回退到终端输出
|
|
710
|
+
- 通知中心(inbox + dashboard)不依赖系统通知服务,始终可用
|
|
711
|
+
|
|
712
|
+
### 与 CI/CD 的关系
|
|
713
|
+
|
|
714
|
+
本工具定位为**本地开发辅助**,在开发者提交代码时提供即时反馈,不替代 CI/CD 中的代码审查流程。建议将 `.ai-review/` 目录加入 `.gitignore`:
|
|
715
|
+
|
|
716
|
+
```gitignore
|
|
717
|
+
# AI Code Review 本地数据
|
|
718
|
+
.ai-review/memory.json
|
|
719
|
+
.ai-review/reports/
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
### 常见问题
|
|
723
|
+
|
|
724
|
+
**Q: 提交时没有触发审查?**
|
|
725
|
+
|
|
726
|
+
检查 Git Hook 是否安装:
|
|
727
|
+
|
|
728
|
+
```bash
|
|
729
|
+
ls .git/hooks/pre-commit
|
|
730
|
+
ls .git/hooks/post-commit
|
|
731
|
+
```
|
|
732
|
+
|
|
733
|
+
如果不存在,运行 `ai-review init` 重新安装。
|
|
734
|
+
|
|
735
|
+
**Q: 审查超时了?**
|
|
736
|
+
|
|
737
|
+
Pre-commit 默认超时 30 秒。如果 LLM 响应较慢,可在 `.ai-review.yaml` 中增加超时:
|
|
738
|
+
|
|
739
|
+
```yaml
|
|
740
|
+
hooks:
|
|
741
|
+
pre_commit:
|
|
742
|
+
timeout: 60
|
|
743
|
+
```
|
|
744
|
+
|
|
745
|
+
**Q: 如何临时跳过审查?**
|
|
746
|
+
|
|
747
|
+
方法一:设置环境变量(如果配置了 `bypass_env_vars`)
|
|
748
|
+
方法二:使用 `--no-verify` 跳过 Git Hook(`git commit --no-verify`)
|
|
749
|
+
|
|
750
|
+
> 不推荐长期使用 `--no-verify`,应通过抑制系统处理已知问题。
|
|
751
|
+
|
|
752
|
+
**Q: 报告中 rule_id 是中文,抑制不生效?**
|
|
753
|
+
|
|
754
|
+
抑制系统支持模糊匹配,会从英文规则 ID 中提取关键词与 issue 消息内容匹配。如果仍不生效,检查 `ai-review suppress --list` 中存储的文件路径是否与报告中的路径一致。
|
|
755
|
+
|
|
756
|
+
**Q: 支持 GitLab / Bitbucket 吗?**
|
|
757
|
+
|
|
758
|
+
工具通过 Git 命令获取 diff 信息,与代码托管平台无关,任何 Git 仓库均可使用。
|