Skip to content

Commit 9803a52

Browse files
committed
feat: 完善文档说明
1 parent f109e17 commit 9803a52

15 files changed

Lines changed: 470 additions & 100 deletions

File tree

README.md

Lines changed: 168 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,79 @@
1-
# code-gate
2-
3-
AI 助力的提交时代码 Review 工具,支持本地 Ollama 或云端 AI 服务(DeepSeek, OpenAI, Anthropic, Aliyun Qwen, Volcengine Doubao, Zhipu GLM 等),审查 `git commit``staged diff`,并以 GitHub Diff 风格在本地页面展示结果。
4-
5-
## 安装
6-
- `npm i -D code-gate`
7-
8-
## 初始化与集成
9-
- 推荐使用 `init` 命令,选择初始化方式并自动生成配置文件:
10-
- 原生 Git Hooks:`npx code-gate init -m git`
11-
- Husky:`npx code-gate init -m husky`
12-
13-
## 手动初始化(不覆盖现有钩子)
14-
- Husky:
15-
- 编辑 `.husky/pre-commit`,在原有内容后追加:
16-
```bash
17-
./node_modules/.bin/code-gate-hook
18-
```
19-
> 提示:如果希望支持全局安装模式,也可使用 `npx code-gate-hook`
20-
- 确认 `git config core.hooksPath` 输出 `.husky`
21-
- 原生 Git Hooks:
22-
- 在项目根创建 `.githooks/pre-commit`,内容:
23-
```bash
24-
#!/bin/sh
25-
./node_modules/.bin/code-gate-hook
26-
```
27-
- 设置 `core.hooksPath`
28-
- `git config core.hooksPath .githooks`
29-
30-
31-
## 命令
32-
- `npx code-gate init` 交互式初始化(可选择 git/husky,自动生成配置文件,并提示是否添加配置至 .gitignore)
33-
- `npx code-gate hook` 在 Hook 中执行交互式 Review(通常由 commit 操作自动触发,无需手动运行)
34-
35-
## 配置文件
36-
- 仅支持 `.codegate.js` 格式。
37-
- 示例:
38-
```js
1+
<div align="center">
2+
<img src="assets/logo.png" alt="Code Gate Logo" width="120" />
3+
</div>
4+
5+
[English](./README.md) | [简体中文](./README_ZH.md)
6+
7+
# Code Gate
8+
9+
**Your Lightweight Local AI Code Review Assistant**
10+
11+
Code Gate is an intelligent code review tool seamlessly integrated into your Git workflow. When you run `git commit`, it automatically analyzes staged code changes, utilizing local LLMs (Ollama) or cloud AI services to provide instant feedback on code quality, security suggestions, and optimization plans.
12+
13+
## ✨ Features
14+
15+
- **🔒 Privacy First**: Native support for Ollama local models.
16+
- **☁️ Multi-Model Support**: Seamlessly integrates with DeepSeek, OpenAI, Anthropic, Aliyun Qwen, Doubao, and more.
17+
- **🌍 Multi-Language**: Built-in support for English, Chinese (Simplified & Traditional), Japanese, Korean, German, and French.
18+
- **⚡️ High Performance**: Intelligent concurrent processing for faster reviews across multiple files.
19+
- **🛠️ Highly Customizable**: Custom prompts, file filtering rules, and review strategies.
20+
- **📊 Visual Reports**: Generates intuitive HTML review reports with clear diffs and AI suggestions.
21+
22+
## 🚀 Quick Start
23+
24+
### 1. Installation
25+
26+
Install `code-gate` as a development dependency:
27+
28+
```bash
29+
npm i -D code-gate
30+
```
31+
32+
### 2. Initialization
33+
34+
We provide a one-click initialization command to configure Git Hooks.
35+
36+
**Automatic Init (Recommended)**
37+
38+
```bash
39+
# Interactive selection for Git Hooks or Husky
40+
npx code-gate init
41+
```
42+
43+
You can also specify arguments if you prefer a specific hook manager:
44+
45+
- **Native Git Hooks**: `npx code-gate init -m git`
46+
- **Husky**: `npx code-gate init -m husky`
47+
48+
> After initialization, you can choose to add the generated config file to `.gitignore`.
49+
50+
### 3. Usage
51+
52+
Just commit your code as usual:
53+
54+
```bash
55+
git add .
56+
git commit -m "feat: new feature"
57+
```
58+
59+
Code Gate intercepts the commit:
60+
1. Analyzes code changes.
61+
<img src="assets/step1.png" width="300" />
62+
2. Starts a local server and generates a review report.
63+
3. Automatically opens the report in your browser.
64+
<img src="assets/step2.png" width="600" />
65+
4. You choose to **Confirm Commit** or **Cancel** in the terminal.
66+
<img src="assets/step3.png" width="300" />
67+
68+
---
69+
70+
## ⚙️ Configuration
71+
72+
The `code-gate.config.js` in your project root controls all behaviors.
73+
74+
### Basic Configuration
75+
76+
```javascript
3977
export default {
4078
provider: 'ollama',
4179
providerOptions: {
@@ -57,7 +95,8 @@ export default {
5795
// volcengine: { baseURL: 'https://ark.cn-beijing.volces.com/api/v3', apiKeyEnv: 'VOLCENGINE_API_KEY', model: 'doubao-pro-32k' },
5896
// zhipu: { baseURL: 'https://open.bigmodel.cn/api/paas/v4', apiKeyEnv: 'ZHIPU_API_KEY', model: 'glm-4' }
5997
},
60-
fileTypes: [],
98+
language: 'en',
99+
fileTypes: ['ts', 'tsx', 'css'],
61100
ui: {
62101
openBrowser: true,
63102
port: 5175
@@ -66,42 +105,81 @@ export default {
66105
maxDiffLines: 10000,
67106
maxFiles: 100
68107
},
69-
reviewMode: 'files',
70-
prompt: '作为资深代码审查工程师,从安全、性能、代码风格与测试覆盖角度审查本次变更,指出问题与改进建议,并给出必要的示例补丁。',
108+
prompt: 'as a senior code reviewer, please review the code changes and provide feedback on security, performance, code style, and test coverage. Highlight any issues or areas for improvement, and offer concrete suggestions with code examples if possible.',
71109
output: {
72-
dir: '.review-logs'
73-
}
74-
}
75-
```
110+
dir: '.review-logs'
111+
},
112+
}
113+
```
114+
115+
### API Key Configuration
116+
117+
Choose the appropriate configuration scheme based on your project needs. Taking deepseek as an example.
118+
For security, avoid hardcoding API Keys in the config file.
119+
120+
**Option A: Config File**
121+
122+
Set in `.code-gate.js`:
123+
124+
```javascript
125+
export default {
126+
providerOptions: {
127+
deepseek: {
128+
// ...other config
129+
apiKey: 'your-deepseek-api-key'
130+
}
131+
}
132+
}
133+
```
134+
135+
**Option B: Git Hook Injection**
136+
137+
Export temporarily in `.githooks/pre-commit` or `.husky/pre-commit`:
138+
139+
```bash
140+
#!/bin/sh
141+
export DEEPSEEK_API_KEY=[your-deepseek-api-key]
142+
./node_modules/.bin/code-gate-hook
143+
```
144+
145+
**Option C: Environment Variables (Recommended)**
76146

77-
### 参数说明
78-
| 参数 | 类型 | 默认值 | 说明 |
147+
Set in your `.env` file or system environment:
148+
149+
```bash
150+
export DEEPSEEK_API_KEY=[your-deepseek-api-key]
151+
```
152+
153+
## 📖 Configuration Details
154+
155+
| Parameter | Type | Default | Description |
79156
| :--- | :--- | :--- | :--- |
80-
| `provider` | `string` | `'ollama'` | 选择使用的 AI 审查引擎。可选值: `'ollama'`, `'deepseek'`, `'openai'`, `'anthropic'`, `'aliyun'`, `'volcengine'`, `'zhipu'`|
81-
| `providerOptions` | `object` | `{}` | 各 Provider 的具体配置集合(见下表) |
82-
| `fileTypes` | `string[]` | `[]` | 需要审查的文件类型扩展名列表(白名单)。若为空数组或未配置,则审查所有文件。 |
83-
| `exclude` | `string[]` | `['**/package-lock.json', '**/yarn.lock', '**/pnpm-lock.yaml']` | 不需要审查的文件或目录列表(黑名单),支持 glob 模式匹配(如 `node_modules/**`)。优先级高于 `fileTypes`|
84-
| `ui.openBrowser` | `boolean` | `true` | 是否自动打开浏览器预览 |
85-
| `ui.port` | `number` | `5175` | 预览服务端口 |
86-
| `limits.maxDiffLines` | `number` | `10000` | 最大 diff 行数,超出限制可能导致审查不完整或消耗过多 Token |
87-
| `limits.maxFiles` | `number` | `100` | 最大审查文件数 |
88-
| `reviewMode` | `string` | `'files'` | 审查模式:`'summary'` (仅汇总), `'files'` (仅文件详情), `'both'` (两者都有) |
89-
| `language` | `string` | `'en'` | 界面与 Prompt 语言。可选值:`'en'`, `'zh-CN'`, `'ja'`, `'ko'`, `'de'`, `'fr'` |
90-
| `prompt` | `string` | `...` | 发送给 AI 的通用系统提示词 |
91-
| `output.dir` | `string` | `'.review-logs'` | 本地生成报告和静态资源的输出目录 |
92-
93-
### providerOptions 配置
94-
每个 Provider 可配置以下字段,支持 `request` 选项控制请求超时与重试。
95-
96-
**关键参数说明:**
97-
- `baseURL`: API 基础地址(如 `https://api.deepseek.com``http://localhost:11434`
98-
- `apiKey`: API 密钥(直接在配置中指定,不推荐提交到仓库)
99-
- `apiKeyEnv`: 存储 API 密钥的环境变量名称(推荐方式,如 `DEEPSEEK_API_KEY`
100-
- `model`: 使用的模型名称(如 `deepseek-chat`, `qwen2.5-coder`
101-
- `concurrencyFiles`: 并发审查的文件数量(建议云端 API 设置 4-8,本地模型设置 1)
102-
- `request`: 高级请求配置(见下表“高级配置”)
103-
104-
| Provider | 可配置参数 |
157+
| `provider` | `string` | `'ollama'` | AI Provider. Supports `ollama`, `deepseek`, `openai`, `anthropic`, `aliyun`, `volcengine`, `zhipu`, etc. |
158+
| `providerOptions` | `object` | `{}` | Specific configurations for each Provider (see table below) |
159+
| `fileTypes` | `string[]` | `[]` | List of file extensions to review (whitelist). Reviews all files if empty or undefined. |
160+
| `exclude` | `string[]` | `['**/package-lock.json', '**/yarn.lock', '**/pnpm-lock.yaml']` | List of files or directories to ignore (blacklist), supports glob patterns (e.g., `node_modules/**`). Higher priority than `fileTypes`. |
161+
| `ui.openBrowser` | `boolean` | `true` | Auto-open browser for report preview |
162+
| `ui.port` | `number` | `5175` | Preview server port |
163+
| `limits.maxDiffLines` | `number` | `10000` | Max diff lines per review. Exceeding may cause incomplete review or excessive token usage. |
164+
| `limits.maxFiles` | `number` | `100` | Max number of files to review |
165+
| `reviewMode` | `string` | `'files'` | Review Mode: `'summary'` (summary only), `'files'` (file details only), `'both'` (both) |
166+
| `language` | `string` | `'en'` | UI & Prompt Language. Options: `'en'`, `'zh-CN'`, `'zh-TW'`, `'ja'`, `'ko'`, `'de'`, `'fr'` |
167+
| `prompt` | `string` | `...` | Universal system prompt sent to AI |
168+
| `output.dir` | `string` | `'.review-logs'` | Output directory for local reports and static assets |
169+
170+
### providerOptions Configuration
171+
172+
Each Provider supports the following fields, with `request` option for timeout and retry control.
173+
174+
**Key Parameters:**
175+
- `baseURL`: API base URL (e.g., `https://api.deepseek.com` or `http://localhost:11434`)
176+
- `apiKey`: API Key (specified directly in config, not recommended for committing)
177+
- `apiKeyEnv`: Environment variable name storing the API Key (Recommended, e.g., `DEEPSEEK_API_KEY`)
178+
- `model`: Model name to use (e.g., `deepseek-chat`, `qwen2.5-coder`)
179+
- `concurrencyFiles`: Number of concurrent file reviews (Recommended: Cloud API 4-8, Local Model 1)
180+
- `request`: Advanced request configuration (see "Advanced Configuration" below)
181+
182+
| Provider | Configurable Parameters |
105183
| :--- | :--- |
106184
| **deepseek** | `baseURL`, `apiKey`, `apiKeyEnv`, `model`, `concurrencyFiles`, `request` |
107185
| **ollama** | `baseURL`, `model`, `concurrencyFiles`, `request` |
@@ -112,28 +190,28 @@ export default {
112190
| **zhipu** | `baseURL`, `apiKey`, `apiKeyEnv`, `model`, `request` |
113191
| **azureOpenAI** | `endpoint`, `apiKey`, `apiKeyEnv`, `deployment`, `apiVersion`, `request` |
114192

115-
#### 高级配置 (request)
116-
`providerOptions.<provider>.request` 中配置,用于控制请求行为:
193+
#### Advanced Configuration (request)
117194

118-
| 参数 | 类型 | 默认值 | 说明 |
195+
Configure in `providerOptions.<provider>.request` to control request behavior:
196+
197+
| Parameter | Type | Default | Description |
119198
| :--- | :--- | :--- | :--- |
120-
| `timeout` | `number` | `undefined` | 请求超时时间(毫秒)。Ollama 默认建议设大一些(如 15000+) |
121-
| `retries` | `number` | `0` | 请求失败重试次数 |
122-
| `backoffMs` | `number` | `300` | 重试间隔时间(毫秒) |
199+
| `timeout` | `number` | `undefined` | Request timeout (ms). Recommended to set higher for Ollama (e.g., 15000+) |
200+
| `retries` | `number` | `0` | Number of retries on request failure |
201+
| `backoffMs` | `number` | `300` | Retry interval (ms) |
202+
203+
> **Note**: `concurrencyFiles` controls the number of concurrent file reviews (Default: DeepSeek=4, Ollama=1, Others=4).
123204
124-
> **注意**`concurrencyFiles` 控制并发审查的文件数(默认 DeepSeek=4, Ollama=1, 其他=4)。
205+
## ❓ FAQ
125206

126-
## 使用流程
127-
- 运行 `git commit` 时会询问是否进行 Review:
128-
- 选择否:正常提交。
129-
- 选择是:抓取 `staged diff` 调用 AI 审查,生成本地页面并打印预览 URL,再询问是否继续提交。
130-
- 非交互环境会自动跳过。
131-
- 页面顶部会显示 AI 状态(是否参与、Provider、Model、错误信息)
207+
**Q: Report shows diffs but no AI suggestions?**
208+
- Check `provider` configuration.
209+
- If using Ollama, ensure local service is running (`ollama serve`) and model is pulled (`ollama pull qwen2.5-coder`).
210+
- If using Cloud API, check API Key validity and network connection.
132211

133-
> **ps**: 如需在非交互环境强制执行,可在 `pre-commit` 中使用:`npx code-gate hook -f`
212+
**Q: How to skip review in CI/CD?**
213+
Code Gate detects non-interactive environments and skips automatically. To force skip, use `git commit --no-verify`.
134214

135-
## 故障排查
136-
- 页面只有 diff、没有 AI 审查内容:
137-
- 如果provider选择的是第三方服务商,确保环境变量或providerOptions里配置正确。
138-
- 如果provider选择的是Ollama,确保本地 Ollama 正在运行(默认 `http://localhost:11434`),并且模型已安装;例如 `ollama list` 查看,`ollama pull qwen2.5-coder`
215+
## 📄 License
139216

217+
MIT

0 commit comments

Comments
 (0)