@zhuxb-clouds/ai-code-review 1.3.0 → 1.3.2
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.
- package/README.md +118 -111
- package/README.zh-CN.md +224 -0
- package/bin/cli.mjs +17 -10
- package/bin/hook.mjs +20 -57
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,240 +1,247 @@
|
|
|
1
1
|
# AI Code Reviewer & Commit Generator
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[English] | [简体中文](./README.zh-CN.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A Git Hooks integration solution built with Node.js and OpenAI-compatible APIs. It automatically performs code reviews during `git commit` and generates commit messages that follow the [Conventional Commits](https://www.conventionalcommits.org/) specification based on your code changes.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
**Supported AI Providers**: OpenAI, DeepSeek, and more.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
9
|
+
## 🚀 Key Features
|
|
10
|
+
|
|
11
|
+
* **Automated Review**: Intercepts potential bugs or sub-optimal practices before the commit is finalized.
|
|
12
|
+
* **Semantic Commits**: Automatically drafts commit messages adhering to the Conventional Commits standard.
|
|
13
|
+
* **Seamless Integration**: Powered by Git Hooks; requires no changes to your existing development workflow.
|
|
14
|
+
* **Cost Efficiency**: Supports diff size limits to prevent excessive Token usage.
|
|
15
|
+
* **One-Click Installation**: Easily installable as an npm package in any project.
|
|
16
|
+
* **Multi-Provider Support**: Compatible with OpenAI, DeepSeek, and other OpenAI-compliant APIs.
|
|
17
|
+
* **Proxy Support**: Full support for HTTP/HTTPS/SOCKS5 proxies.
|
|
16
18
|
|
|
17
19
|
---
|
|
18
20
|
|
|
19
|
-
## 🛠️
|
|
21
|
+
## 🛠️ Technical Architecture
|
|
20
22
|
|
|
21
23
|
```
|
|
22
|
-
git commit → Husky (commit-msg) → ai-review-hook → AI API (OpenAI/DeepSeek)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
git commit → Husky (prepare-commit-msg) → ai-review-hook → AI API (OpenAI/DeepSeek)
|
|
25
|
+
↓
|
|
26
|
+
✅ Pass: Auto-fill Commit Message
|
|
27
|
+
❌ Fail: Block commit & output suggestions
|
|
28
|
+
|
|
26
29
|
```
|
|
27
30
|
|
|
28
31
|
---
|
|
29
32
|
|
|
30
|
-
## 📦
|
|
33
|
+
## 📦 Quick Start
|
|
31
34
|
|
|
32
|
-
### 1.
|
|
35
|
+
### 1. Install in your project
|
|
33
36
|
|
|
34
37
|
```bash
|
|
35
|
-
#
|
|
38
|
+
# Install as a dev dependency
|
|
36
39
|
npm install ai-code-review -D
|
|
37
40
|
|
|
38
|
-
#
|
|
41
|
+
# Initialize (Automatically installs Husky and configures Git Hooks)
|
|
39
42
|
npx ai-review init
|
|
43
|
+
|
|
40
44
|
```
|
|
41
45
|
|
|
42
|
-
### 2.
|
|
46
|
+
### 2. Configure Environment Variables
|
|
43
47
|
|
|
44
|
-
|
|
48
|
+
Copy the generated `.env.example` to `.env` and enter your API Key:
|
|
45
49
|
|
|
46
50
|
```bash
|
|
47
51
|
cp .env.example .env
|
|
52
|
+
|
|
48
53
|
```
|
|
49
54
|
|
|
50
|
-
####
|
|
55
|
+
#### Using OpenAI
|
|
51
56
|
|
|
52
57
|
```bash
|
|
53
58
|
AI_PROVIDER=openai
|
|
54
59
|
OPENAI_API_KEY=sk-your-openai-key-here
|
|
55
60
|
OPENAI_MODEL=gpt-4o-mini
|
|
61
|
+
|
|
56
62
|
```
|
|
57
63
|
|
|
58
|
-
####
|
|
64
|
+
#### Using DeepSeek
|
|
59
65
|
|
|
60
66
|
```bash
|
|
61
67
|
AI_PROVIDER=deepseek
|
|
62
68
|
DEEPSEEK_API_KEY=sk-your-deepseek-key-here
|
|
63
|
-
# OPENAI_MODEL=deepseek-chat #
|
|
69
|
+
# OPENAI_MODEL=deepseek-chat # Optional, defaults to deepseek-chat
|
|
70
|
+
|
|
64
71
|
```
|
|
65
72
|
|
|
66
|
-
####
|
|
73
|
+
#### Using Proxies
|
|
67
74
|
|
|
68
75
|
```bash
|
|
69
76
|
HTTPS_PROXY=http://127.0.0.1:7890
|
|
77
|
+
|
|
70
78
|
```
|
|
71
79
|
|
|
72
|
-
> ⚠️
|
|
80
|
+
> ⚠️ **Security Note**: `.env` is automatically added to `.gitignore`. Never commit your API keys!
|
|
73
81
|
|
|
74
|
-
### 3.
|
|
82
|
+
### 3. Usage
|
|
75
83
|
|
|
76
84
|
```bash
|
|
77
|
-
#
|
|
85
|
+
# Develop and stage your changes as usual
|
|
78
86
|
git add .
|
|
79
87
|
|
|
80
|
-
#
|
|
88
|
+
# Execute commit (Recommended: omit -m to let AI generate the message)
|
|
81
89
|
git commit
|
|
82
90
|
|
|
83
|
-
# AI
|
|
84
|
-
# 🔍
|
|
85
|
-
# ✅ AI Review
|
|
86
|
-
# 📝
|
|
91
|
+
# AI will automatically review and generate the message
|
|
92
|
+
# 🔍 Running AI Code Review...
|
|
93
|
+
# ✅ AI Review Passed
|
|
94
|
+
# 📝 Generated Message: feat(auth): add JWT token validation
|
|
95
|
+
|
|
87
96
|
```
|
|
88
97
|
|
|
89
98
|
---
|
|
90
99
|
|
|
91
|
-
## 📂
|
|
100
|
+
## 📂 Post-Installation Directory Structure
|
|
92
101
|
|
|
93
102
|
```
|
|
94
103
|
your-project/
|
|
95
104
|
├── .husky/
|
|
96
|
-
│ └── commit-msg
|
|
97
|
-
├── .env # API
|
|
98
|
-
├── .env.example #
|
|
99
|
-
├── .reviewignore # AI
|
|
100
|
-
├── .reviewignore.example #
|
|
101
|
-
├── .gitignore #
|
|
102
|
-
└── package.json #
|
|
105
|
+
│ └── prepare-commit-msg # Git Hook (Auto-generated)
|
|
106
|
+
├── .env # API Keys (User-created, do not commit!)
|
|
107
|
+
├── .env.example # Configuration template (Auto-generated)
|
|
108
|
+
├── .reviewignore # AI Review ignore file (Optional)
|
|
109
|
+
├── .reviewignore.example # Ignore rules template (Auto-generated)
|
|
110
|
+
├── .gitignore # Now includes .env
|
|
111
|
+
└── package.json # Contains ai-code-review dependency
|
|
112
|
+
|
|
103
113
|
```
|
|
104
114
|
|
|
105
115
|
---
|
|
106
116
|
|
|
107
|
-
## 🚫
|
|
117
|
+
## 🚫 File Exclusion (.reviewignore)
|
|
108
118
|
|
|
109
|
-
|
|
119
|
+
Create a `.reviewignore` file to skip AI reviews for specific files. The syntax is identical to `.gitignore`:
|
|
110
120
|
|
|
111
121
|
```bash
|
|
112
|
-
#
|
|
122
|
+
# Copy the example file
|
|
113
123
|
cp .reviewignore.example .reviewignore
|
|
124
|
+
|
|
114
125
|
```
|
|
115
126
|
|
|
116
|
-
###
|
|
127
|
+
### Supported Syntax
|
|
117
128
|
|
|
118
129
|
```gitignore
|
|
119
|
-
#
|
|
120
|
-
#
|
|
130
|
+
# Comments
|
|
131
|
+
# This is a comment
|
|
121
132
|
|
|
122
|
-
#
|
|
123
|
-
package-lock.json #
|
|
124
|
-
*.min.js # *
|
|
125
|
-
dist/ #
|
|
126
|
-
**/*.snap # **
|
|
133
|
+
# Wildcards
|
|
134
|
+
package-lock.json # Match specific file
|
|
135
|
+
*.min.js # * matches any string (excluding /)
|
|
136
|
+
dist/ # Match entire directory
|
|
137
|
+
**/*.snap # ** matches any path depth
|
|
138
|
+
|
|
139
|
+
# Negation patterns
|
|
140
|
+
*.md # Ignore all markdown files
|
|
141
|
+
!README.md # Do NOT ignore README.md
|
|
127
142
|
|
|
128
|
-
# 否定模式
|
|
129
|
-
*.md # 忽略所有 markdown
|
|
130
|
-
!README.md # 但不忽略 README.md
|
|
131
143
|
```
|
|
132
144
|
|
|
133
|
-
###
|
|
145
|
+
### Common Configuration Examples
|
|
134
146
|
|
|
135
147
|
```gitignore
|
|
136
|
-
#
|
|
148
|
+
# Lock files
|
|
137
149
|
package-lock.json
|
|
138
150
|
pnpm-lock.yaml
|
|
139
151
|
yarn.lock
|
|
140
152
|
|
|
141
|
-
#
|
|
153
|
+
# Generated files
|
|
142
154
|
*.min.js
|
|
143
155
|
*.bundle.js
|
|
144
156
|
dist/
|
|
145
157
|
build/
|
|
146
158
|
|
|
147
|
-
#
|
|
159
|
+
# Docs and Assets
|
|
148
160
|
*.md
|
|
149
161
|
*.svg
|
|
150
162
|
*.png
|
|
151
163
|
|
|
152
|
-
#
|
|
164
|
+
# Test Snapshots
|
|
153
165
|
__snapshots__/
|
|
154
166
|
*.snap
|
|
167
|
+
|
|
155
168
|
```
|
|
156
169
|
|
|
157
170
|
---
|
|
158
171
|
|
|
159
|
-
## ⌨️ CLI
|
|
172
|
+
## ⌨️ CLI Commands
|
|
160
173
|
|
|
161
174
|
```bash
|
|
162
|
-
#
|
|
175
|
+
# Full Initialization (Install Husky + Config Hook)
|
|
163
176
|
npx ai-review init
|
|
164
177
|
|
|
165
|
-
#
|
|
178
|
+
# Configure Hook Only (If Husky is already installed)
|
|
166
179
|
npx ai-review setup
|
|
167
180
|
|
|
168
|
-
#
|
|
181
|
+
# Display help information
|
|
169
182
|
npx ai-review help
|
|
183
|
+
|
|
170
184
|
```
|
|
171
185
|
|
|
172
|
-
###
|
|
186
|
+
### Skipping AI Review
|
|
173
187
|
|
|
174
188
|
```bash
|
|
175
|
-
#
|
|
176
|
-
git commit
|
|
189
|
+
# Manual messages using -m will automatically skip AI generation
|
|
190
|
+
git commit -m "feat: your manual message"
|
|
191
|
+
|
|
192
|
+
# Merge, squash, and amend operations also skip the review automatically
|
|
193
|
+
git merge feature-branch
|
|
194
|
+
git commit --amend
|
|
177
195
|
|
|
178
|
-
# 带 -m 提交时仍会进行审查,但会使用你提供的消息
|
|
179
|
-
git commit -m "your message"
|
|
180
196
|
```
|
|
181
197
|
|
|
182
198
|
---
|
|
183
199
|
|
|
184
|
-
## ⚙️
|
|
200
|
+
## ⚙️ Configuration Options
|
|
185
201
|
|
|
186
|
-
|
|
202
|
+
Configure your setup via environment variables in the `.env` file:
|
|
187
203
|
|
|
188
|
-
###
|
|
204
|
+
### Base Configuration
|
|
189
205
|
|
|
190
|
-
|
|
|
191
|
-
| ------------------ | -------- |
|
|
192
|
-
| `AI_PROVIDER` | `openai` | AI
|
|
193
|
-
| `OPENAI_API_KEY` | - | OpenAI API Key
|
|
194
|
-
| `DEEPSEEK_API_KEY` | - | DeepSeek API Key
|
|
195
|
-
| `OPENAI_BASE_URL` |
|
|
196
|
-
| `OPENAI_MODEL` |
|
|
206
|
+
| Variable | Default | Description |
|
|
207
|
+
| ------------------ | -------- | --------------------------------------------------------- |
|
|
208
|
+
| `AI_PROVIDER` | `openai` | AI Provider: `openai` or `deepseek` |
|
|
209
|
+
| `OPENAI_API_KEY` | - | OpenAI API Key (Required if using OpenAI) |
|
|
210
|
+
| `DEEPSEEK_API_KEY` | - | DeepSeek API Key (Required if using DeepSeek) |
|
|
211
|
+
| `OPENAI_BASE_URL` | Auto | Custom API endpoint URL |
|
|
212
|
+
| `OPENAI_MODEL` | Auto | Model name (OpenAI: gpt-4o-mini, DeepSeek: deepseek-chat) |
|
|
197
213
|
|
|
198
|
-
###
|
|
214
|
+
### Network Configuration
|
|
199
215
|
|
|
200
|
-
|
|
|
201
|
-
| ------------- |
|
|
202
|
-
| `HTTPS_PROXY` | -
|
|
203
|
-
| `HTTP_PROXY` | -
|
|
216
|
+
| Variable | Default | Description |
|
|
217
|
+
| ------------- | ------- | ------------------------------- |
|
|
218
|
+
| `HTTPS_PROXY` | - | HTTP/HTTPS/SOCKS5 proxy address |
|
|
219
|
+
| `HTTP_PROXY` | - | Alternative proxy address |
|
|
204
220
|
|
|
205
|
-
###
|
|
221
|
+
### Behavior Configuration
|
|
206
222
|
|
|
207
|
-
|
|
|
208
|
-
| ------------------------- |
|
|
209
|
-
| `AI_REVIEW_MAX_DIFF_SIZE` | `15000`
|
|
210
|
-
| `AI_REVIEW_TIMEOUT` | `30000`
|
|
211
|
-
| `AI_REVIEW_MAX_RETRIES` | `3`
|
|
212
|
-
| `AI_REVIEW_RETRY_DELAY` | `1000`
|
|
213
|
-
| `AI_REVIEW_VERBOSE` | `false`
|
|
214
|
-
| `AI_REVIEW_SKIP_BUILD` | `false` | 跳过构建检查 |
|
|
215
|
-
| `AI_REVIEW_BUILD_COMMAND` | `npm run build` | 构建命令 |
|
|
223
|
+
| Variable | Default | Description |
|
|
224
|
+
| ------------------------- | ------- | ---------------------------------------------- |
|
|
225
|
+
| `AI_REVIEW_MAX_DIFF_SIZE` | `15000` | Max characters in diff (truncated if exceeded) |
|
|
226
|
+
| `AI_REVIEW_TIMEOUT` | `30000` | API request timeout in milliseconds |
|
|
227
|
+
| `AI_REVIEW_MAX_RETRIES` | `3` | Max retries on failure |
|
|
228
|
+
| `AI_REVIEW_RETRY_DELAY` | `1000` | Delay between retries in milliseconds |
|
|
229
|
+
| `AI_REVIEW_VERBOSE` | `false` | Enable detailed logging |
|
|
216
230
|
|
|
217
|
-
|
|
231
|
+
### Build Checks (Optional)
|
|
218
232
|
|
|
219
|
-
|
|
233
|
+
Build checks have been moved to the hook script. You can customize them by editing `.husky/prepare-commit-msg`:
|
|
220
234
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
| [aicommits](https://github.com/Nutlope/aicommits) | 开箱即用的 CLI | 不含代码审查功能 |
|
|
225
|
-
| [cz-git](https://cz-git.qbb.sh/) + AI | 交互式提交、规范完善 | 配置较复杂 |
|
|
226
|
-
| GitHub Copilot | IDE 集成、体验好 | 需要订阅、无 Hook 集成 |
|
|
227
|
-
|
|
228
|
-
---
|
|
235
|
+
```bash
|
|
236
|
+
# Open the hook file
|
|
237
|
+
vim .husky/prepare-commit-msg
|
|
229
238
|
|
|
230
|
-
|
|
239
|
+
# Uncomment the following lines to enable build checks
|
|
240
|
+
echo "🔨 Running build check..."
|
|
241
|
+
npm run build || exit 1
|
|
242
|
+
echo "✅ Build passed"
|
|
231
243
|
|
|
232
|
-
|
|
233
|
-
2. **成本控制**:
|
|
234
|
-
- 默认使用 `gpt-4o-mini`,成本约为 GPT-4 的 1/10
|
|
235
|
-
- 大改动建议分批 `git add` 提交
|
|
236
|
-
3. **安全**:`.env` 文件绝对不要提交到仓库
|
|
237
|
-
4. **容错**:脚本在 API 出错时会允许提交,不阻塞开发
|
|
244
|
+
```
|
|
238
245
|
|
|
239
246
|
---
|
|
240
247
|
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# AI Code Reviewer & Commit Generator
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [简体中文]
|
|
4
|
+
|
|
5
|
+
基于 Node.js 和 OpenAI-compatible API 的 Git Hooks 集成方案。在执行 `git commit` 时自动进行代码审查,并根据 Diff 自动生成符合 [Conventional Commits](https://www.conventionalcommits.org/) 规范的提交信息。
|
|
6
|
+
|
|
7
|
+
**支持的 AI 提供商**: OpenAI, DeepSeek
|
|
8
|
+
|
|
9
|
+
## 🚀 核心特性
|
|
10
|
+
|
|
11
|
+
- **自动化审查**:在代码提交前拦截潜在 Bug 或不规范实践
|
|
12
|
+
- **语义化提交**:自动撰写符合 Conventional Commits 规范的提交信息
|
|
13
|
+
- **无感集成**:通过 Git Hooks 实现,无需改变原有开发习惯
|
|
14
|
+
- **成本可控**:支持 Diff 大小限制,避免 Token 浪费
|
|
15
|
+
- **一键安装**:作为 npm 包安装到任何项目
|
|
16
|
+
- **多提供商支持**:支持 OpenAI、DeepSeek 等兼容 API
|
|
17
|
+
- **代理支持**:支持 HTTP/HTTPS/SOCKS5 代理
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 🛠️ 技术架构
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
git commit → Husky (prepare-commit-msg) → ai-review-hook → AI API (OpenAI/DeepSeek)
|
|
25
|
+
↓
|
|
26
|
+
✅ 通过:自动填充 Commit Message
|
|
27
|
+
❌ 失败:拦截提交并输出建议
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 📦 快速开始
|
|
33
|
+
|
|
34
|
+
### 1. 安装到你的项目
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
# 安装为开发依赖
|
|
38
|
+
npm install ai-code-review -D
|
|
39
|
+
|
|
40
|
+
# 初始化(自动安装 Husky 并配置 Git Hook)
|
|
41
|
+
npx ai-review init
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 2. 配置环境变量
|
|
45
|
+
|
|
46
|
+
复制生成的 `.env.example` 为 `.env` 并填入你的 API Key:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
cp .env.example .env
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
#### 使用 OpenAI
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
AI_PROVIDER=openai
|
|
56
|
+
OPENAI_API_KEY=sk-your-openai-key-here
|
|
57
|
+
OPENAI_MODEL=gpt-4o-mini
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### 使用 DeepSeek
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
AI_PROVIDER=deepseek
|
|
64
|
+
DEEPSEEK_API_KEY=sk-your-deepseek-key-here
|
|
65
|
+
# OPENAI_MODEL=deepseek-chat # 可选,默认 deepseek-chat
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### 使用代理
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
HTTPS_PROXY=http://127.0.0.1:7890
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
> ⚠️ **安全提示**:`.env` 已自动添加到 `.gitignore`,请勿手动提交!
|
|
75
|
+
|
|
76
|
+
### 3. 开始使用
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
# 正常开发并暂存更改
|
|
80
|
+
git add .
|
|
81
|
+
|
|
82
|
+
# 发起提交(推荐不带 -m,让 AI 生成)
|
|
83
|
+
git commit
|
|
84
|
+
|
|
85
|
+
# AI 会自动审查并生成提交信息
|
|
86
|
+
# 🔍 正在进行 AI 代码审查...
|
|
87
|
+
# ✅ AI Review 通过
|
|
88
|
+
# 📝 生成的提交信息: feat(auth): add JWT token validation
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 📂 安装后的目录结构
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
your-project/
|
|
97
|
+
├── .husky/
|
|
98
|
+
│ └── prepare-commit-msg # Git Hook(自动创建)
|
|
99
|
+
├── .env # API Key(自己创建,不要提交!)
|
|
100
|
+
├── .env.example # 配置示例(自动创建)
|
|
101
|
+
├── .reviewignore # AI 审查忽略文件(可选)
|
|
102
|
+
├── .reviewignore.example # 忽略规则示例(自动创建)
|
|
103
|
+
├── .gitignore # 已包含 .env
|
|
104
|
+
└── package.json # 包含 ai-code-review 依赖
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## 🚫 文件忽略配置 (.reviewignore)
|
|
110
|
+
|
|
111
|
+
创建 `.reviewignore` 文件来跳过某些文件的 AI 审查,语法类似 `.gitignore`:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
# 复制示例文件
|
|
115
|
+
cp .reviewignore.example .reviewignore
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 支持的语法
|
|
119
|
+
|
|
120
|
+
```gitignore
|
|
121
|
+
# 注释
|
|
122
|
+
# 这是一个注释
|
|
123
|
+
|
|
124
|
+
# 通配符
|
|
125
|
+
package-lock.json # 匹配特定文件
|
|
126
|
+
*.min.js # * 匹配任意字符(不包括 /)
|
|
127
|
+
dist/ # 匹配整个目录
|
|
128
|
+
**/*.snap # ** 匹配任意路径层级
|
|
129
|
+
|
|
130
|
+
# 否定模式
|
|
131
|
+
*.md # 忽略所有 markdown
|
|
132
|
+
!README.md # 但不忽略 README.md
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### 常见配置示例
|
|
136
|
+
|
|
137
|
+
```gitignore
|
|
138
|
+
# 锁文件
|
|
139
|
+
package-lock.json
|
|
140
|
+
pnpm-lock.yaml
|
|
141
|
+
yarn.lock
|
|
142
|
+
|
|
143
|
+
# 生成的文件
|
|
144
|
+
*.min.js
|
|
145
|
+
*.bundle.js
|
|
146
|
+
dist/
|
|
147
|
+
build/
|
|
148
|
+
|
|
149
|
+
# 文档和资源
|
|
150
|
+
*.md
|
|
151
|
+
*.svg
|
|
152
|
+
*.png
|
|
153
|
+
|
|
154
|
+
# 测试快照
|
|
155
|
+
__snapshots__/
|
|
156
|
+
*.snap
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## ⌨️ CLI 命令
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
# 完整初始化(安装 Husky + 配置 Hook)
|
|
165
|
+
npx ai-review init
|
|
166
|
+
|
|
167
|
+
# 仅配置 Hook(如果 Husky 已安装)
|
|
168
|
+
npx ai-review setup
|
|
169
|
+
|
|
170
|
+
# 显示帮助
|
|
171
|
+
npx ai-review help
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 跳过 AI Review
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
# 使用 -m 参数时自动跳过 AI 生成(直接使用你的消息)
|
|
178
|
+
git commit -m "feat: your message"
|
|
179
|
+
|
|
180
|
+
# merge/squash/amend 提交也会自动跳过
|
|
181
|
+
git merge feature-branch
|
|
182
|
+
git commit --amend
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## ⚙️ 配置选项
|
|
188
|
+
|
|
189
|
+
通过环境变量配置:
|
|
190
|
+
|
|
191
|
+
### 基础配置
|
|
192
|
+
|
|
193
|
+
| 环境变量 | 默认值 | 说明 |
|
|
194
|
+
| ------------------ | -------- | ---------------------------------------------------------------- |
|
|
195
|
+
| `AI_PROVIDER` | `openai` | AI 提供商:`openai` 或 `deepseek` |
|
|
196
|
+
| `OPENAI_API_KEY` | - | OpenAI API Key(使用 OpenAI 时必填) |
|
|
197
|
+
| `DEEPSEEK_API_KEY` | - | DeepSeek API Key(使用 DeepSeek 时必填) |
|
|
198
|
+
| `OPENAI_BASE_URL` | 自动设置 | 自定义 API 地址(可覆盖默认) |
|
|
199
|
+
| `OPENAI_MODEL` | 自动设置 | 模型名称(OpenAI 默认 gpt-4o-mini,DeepSeek 默认 deepseek-chat) |
|
|
200
|
+
|
|
201
|
+
### 网络配置
|
|
202
|
+
|
|
203
|
+
| 环境变量 | 默认值 | 说明 |
|
|
204
|
+
| ------------- | ------ | -------------------------- |
|
|
205
|
+
| `HTTPS_PROXY` | - | HTTP/HTTPS/SOCKS5 代理地址 |
|
|
206
|
+
| `HTTP_PROXY` | - | 同上,备选 |
|
|
207
|
+
|
|
208
|
+
### 行为配置
|
|
209
|
+
|
|
210
|
+
| 环境变量 | 默认值 | 说明 |
|
|
211
|
+
| ------------------------- | --------------- | -------------------------- |
|
|
212
|
+
| `AI_REVIEW_MAX_DIFF_SIZE` | `15000` | 最大 Diff 字符数,超出截断 |
|
|
213
|
+
| `AI_REVIEW_TIMEOUT` | `30000` | API 请求超时时间(毫秒) |
|
|
214
|
+
| `AI_REVIEW_MAX_RETRIES` | `3` | 失败时最大重试次数 |
|
|
215
|
+
| `AI_REVIEW_RETRY_DELAY` | `1000` | 重试间隔时间(毫秒) |
|
|
216
|
+
| `AI_REVIEW_VERBOSE` | `false` | 启用详细日志 |
|
|
217
|
+
| `AI_REVIEW_SKIP_BUILD` | `false` | 跳过构建检查 |
|
|
218
|
+
| `AI_REVIEW_BUILD_COMMAND` | `npm run build` | 构建命令 |
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## 📄 License
|
|
223
|
+
|
|
224
|
+
MIT
|
package/bin/cli.mjs
CHANGED
|
@@ -25,9 +25,18 @@ const command = process.argv[2];
|
|
|
25
25
|
const HOOK_CONTENT = `#!/bin/sh
|
|
26
26
|
|
|
27
27
|
# AI Code Review Hook
|
|
28
|
-
# 使用 commit-msg hook 以支持 --no-verify 跳过
|
|
29
28
|
# $1: 提交消息文件路径
|
|
30
|
-
|
|
29
|
+
# $2: 提交来源 (message, template, merge, squash, commit)
|
|
30
|
+
|
|
31
|
+
# ======== 可选:构建检查 ========
|
|
32
|
+
# 取消下面的注释来启用构建检查,可自行修改命令
|
|
33
|
+
# echo "🔨 正在运行构建检查..."
|
|
34
|
+
# npm run build || exit 1
|
|
35
|
+
# echo "✅ 构建通过"
|
|
36
|
+
# ================================
|
|
37
|
+
|
|
38
|
+
# AI 代码审查和 commit message 生成
|
|
39
|
+
npx ai-review-hook "$1" "$2"
|
|
31
40
|
`;
|
|
32
41
|
|
|
33
42
|
const ENV_EXAMPLE = `# AI 提供商选择 (openai / deepseek)
|
|
@@ -50,8 +59,6 @@ OPENAI_API_KEY=sk-your-openai-api-key-here
|
|
|
50
59
|
# AI_REVIEW_MAX_RETRIES=3
|
|
51
60
|
# AI_REVIEW_RETRY_DELAY=1000
|
|
52
61
|
# AI_REVIEW_VERBOSE=false
|
|
53
|
-
# AI_REVIEW_SKIP_BUILD=false
|
|
54
|
-
# AI_REVIEW_BUILD_COMMAND=npm run build
|
|
55
62
|
`;
|
|
56
63
|
|
|
57
64
|
function showHelp() {
|
|
@@ -117,17 +124,17 @@ function setupHook() {
|
|
|
117
124
|
fs.mkdirSync(huskyDir, { recursive: true });
|
|
118
125
|
}
|
|
119
126
|
|
|
120
|
-
// 创建 commit-msg hook
|
|
121
|
-
const hookPath = path.join(huskyDir, "commit-msg");
|
|
127
|
+
// 创建 prepare-commit-msg hook
|
|
128
|
+
const hookPath = path.join(huskyDir, "prepare-commit-msg");
|
|
122
129
|
fs.writeFileSync(hookPath, HOOK_CONTENT);
|
|
123
130
|
fs.chmodSync(hookPath, "755");
|
|
124
|
-
console.log("✅ 创建 Git Hook: .husky/commit-msg");
|
|
131
|
+
console.log("✅ 创建 Git Hook: .husky/prepare-commit-msg");
|
|
125
132
|
|
|
126
|
-
// 删除旧的
|
|
127
|
-
const oldHookPath = path.join(huskyDir, "
|
|
133
|
+
// 删除旧的 commit-msg hook(如果存在)
|
|
134
|
+
const oldHookPath = path.join(huskyDir, "commit-msg");
|
|
128
135
|
if (fs.existsSync(oldHookPath)) {
|
|
129
136
|
fs.unlinkSync(oldHookPath);
|
|
130
|
-
console.log("🗑️ 删除旧的 Hook: .husky/
|
|
137
|
+
console.log("🗑️ 删除旧的 Hook: .husky/commit-msg");
|
|
131
138
|
}
|
|
132
139
|
|
|
133
140
|
// 创建 .env.example
|
package/bin/hook.mjs
CHANGED
|
@@ -77,8 +77,6 @@ const CONFIG = {
|
|
|
77
77
|
model: aiConfig.model,
|
|
78
78
|
maxDiffSize: parseInt(process.env.AI_REVIEW_MAX_DIFF_SIZE) || 15000,
|
|
79
79
|
timeout: parseInt(process.env.AI_REVIEW_TIMEOUT) || 30000,
|
|
80
|
-
skipBuild: process.env.AI_REVIEW_SKIP_BUILD === "true",
|
|
81
|
-
buildCommand: process.env.AI_REVIEW_BUILD_COMMAND || "npm run build",
|
|
82
80
|
verbose: process.env.AI_REVIEW_VERBOSE === "true",
|
|
83
81
|
maxRetries: parseInt(process.env.AI_REVIEW_MAX_RETRIES) || 3,
|
|
84
82
|
retryDelay: parseInt(process.env.AI_REVIEW_RETRY_DELAY) || 1000,
|
|
@@ -259,38 +257,25 @@ function filterDiff(diff, ignorePatterns) {
|
|
|
259
257
|
}
|
|
260
258
|
|
|
261
259
|
const commitMsgFile = process.argv[2];
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
.split("\n")
|
|
282
|
-
.filter((line) => !line.startsWith("#") && line.trim());
|
|
283
|
-
return meaningfulLines.length > 0;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// 如果是 merge 提交或已有 message,跳过处理
|
|
287
|
-
if (isMergeCommit()) {
|
|
288
|
-
console.log("ℹ️ 跳过 AI Review(merge 提交)");
|
|
289
|
-
process.exit(0);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
if (hasExistingMessage()) {
|
|
293
|
-
logVerbose("检测到已有 commit message,跳过 AI 生成");
|
|
260
|
+
const commitSource = process.argv[3]; // message, template, merge, squash, commit
|
|
261
|
+
|
|
262
|
+
// 检查是否应该跳过 AI Review
|
|
263
|
+
// commitSource 说明:
|
|
264
|
+
// - "message": 使用 -m 或 -F 提供了消息
|
|
265
|
+
// - "template": 使用了模板
|
|
266
|
+
// - "merge": merge 提交
|
|
267
|
+
// - "squash": squash 提交
|
|
268
|
+
// - "commit": 使用 -c/-C/--amend
|
|
269
|
+
|
|
270
|
+
// 如果使用了 -m 提供消息,或者是 merge/squash/amend,跳过 AI 生成
|
|
271
|
+
if (["message", "merge", "squash", "commit"].includes(commitSource)) {
|
|
272
|
+
const reasons = {
|
|
273
|
+
message: "使用了 -m 参数",
|
|
274
|
+
merge: "merge 提交",
|
|
275
|
+
squash: "squash 提交",
|
|
276
|
+
commit: "amend 提交",
|
|
277
|
+
};
|
|
278
|
+
console.log(`ℹ️ 跳过 AI Review(${reasons[commitSource] || commitSource})`);
|
|
294
279
|
process.exit(0);
|
|
295
280
|
}
|
|
296
281
|
|
|
@@ -364,7 +349,6 @@ async function runAIReview() {
|
|
|
364
349
|
logVerbose(`模型: ${CONFIG.model}`);
|
|
365
350
|
logVerbose(`最大 Diff 大小: ${CONFIG.maxDiffSize}`);
|
|
366
351
|
logVerbose(`超时时间: ${CONFIG.timeout}ms`);
|
|
367
|
-
logVerbose(`跳过构建: ${CONFIG.skipBuild}`);
|
|
368
352
|
if (process.env.OPENAI_BASE_URL) {
|
|
369
353
|
logVerbose(`API Base URL: ${process.env.OPENAI_BASE_URL}`);
|
|
370
354
|
}
|
|
@@ -399,28 +383,7 @@ async function runAIReview() {
|
|
|
399
383
|
|
|
400
384
|
logVerbose(`过滤后 Diff 大小: ${(diff.length / 1000).toFixed(2)}KB`);
|
|
401
385
|
|
|
402
|
-
// 2.
|
|
403
|
-
if (!CONFIG.skipBuild) {
|
|
404
|
-
console.log(`🔨 正在运行构建检查: ${CONFIG.buildCommand}`);
|
|
405
|
-
const buildTimer = logTime("构建检查");
|
|
406
|
-
try {
|
|
407
|
-
execSync(CONFIG.buildCommand, {
|
|
408
|
-
cwd: projectRoot,
|
|
409
|
-
stdio: "inherit",
|
|
410
|
-
encoding: "utf-8",
|
|
411
|
-
});
|
|
412
|
-
console.log("✅ 构建通过");
|
|
413
|
-
logTimeEnd(buildTimer);
|
|
414
|
-
} catch (buildError) {
|
|
415
|
-
console.error("❌ 构建失败,请修复后重新提交");
|
|
416
|
-
console.error("\n使用 git commit --no-verify 可跳过检查");
|
|
417
|
-
process.exit(1);
|
|
418
|
-
}
|
|
419
|
-
} else {
|
|
420
|
-
logVerbose("跳过构建检查 (AI_REVIEW_SKIP_BUILD=true)");
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
// 3. 检查 Diff 大小
|
|
386
|
+
// 2. 检查 Diff 大小
|
|
424
387
|
if (diff.length > CONFIG.maxDiffSize) {
|
|
425
388
|
console.warn(`⚠️ Diff 过大 (${(diff.length / 1000).toFixed(1)}KB),建议分批提交`);
|
|
426
389
|
console.warn(` 当前限制: ${CONFIG.maxDiffSize / 1000}KB,超出部分将被截断`);
|