Skip to content

Commit 7a5b8dc

Browse files
committed
project init
0 parents  commit 7a5b8dc

22 files changed

Lines changed: 1865 additions & 0 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
/dist
3+
.DS_Store
4+
.trae

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 鸡斯拉
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# code-gate
2+
3+
AI 助力的提交时代码 Review 工具,支持本地 Ollama 或 DeepSeek API,审查 `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+
- simple-git-hooks:`npx code-gate init -m simple`
13+
- 跳过配置文件生成:`npx code-gate init --no-config`
14+
- 仍支持旧方式:
15+
- 原生 Git Hooks 快速安装:`npx code-gate setup`
16+
- 该命令会创建 `.githooks/pre-commit` 并设置 `core.hooksPath`
17+
18+
## 手动初始化(不覆盖现有钩子)
19+
- Husky:
20+
- 编辑 `.husky/pre-commit`,在原有内容后追加一行:
21+
- `npx code-gate hook`
22+
- 确认 `git config core.hooksPath` 输出 `.husky`
23+
- 原生 Git Hooks:
24+
- 在项目根创建 `.githooks/pre-commit`,内容示例:
25+
- `#!/usr/bin/env sh`
26+
- `npx code-gate hook`
27+
- 设置 `core.hooksPath`
28+
- `git config core.hooksPath .githooks`
29+
- 配置文件生成:
30+
- 在项目根新建 `code-gate.config.json`(可从下文示例复制并按需调整)
31+
- 如使用 DeepSeek,请在环境变量设置 `DEEPSEEK_API_KEY`
32+
33+
## 命令
34+
- `npx code-gate init` 交互式初始化(可选择 git/husky/simple,并生成配置文件)
35+
- `npx code-gate setup` 快速安装原生 Git Hook
36+
- `npx code-gate hook` 在 Hook 中执行交互式 Review
37+
38+
## 本地开发(link 调试)
39+
-`code-gate` 仓库:
40+
- `npm install`
41+
- `npm run build:watch`
42+
- `npm link`
43+
- 在目标项目:
44+
- `npm link code-gate`
45+
- `npx code-gate init -m git`(或 `husky`/`simple`
46+
- `git add` + `git commit` 时触发审查流程
47+
48+
## 配置文件
49+
- 推荐使用 `.codegate.js`(支持注释与更灵活的写法),兼容旧的 `code-gate.config.json/.yaml``.code-gaterc.{json,yaml}`
50+
- 示例(`.codegate.js`):
51+
```js
52+
// provider: 选择使用的 AI 审查引擎,可选值: 'ollama' | 'deepseek'
53+
// review: 审查相关设置
54+
// - enabled: 是否启用审查
55+
// - provider: 覆盖顶层 provider,支持按需切换,可选值: 'ollama' | 'deepseek'
56+
// - mode: 审查展示模式,可选值: 'aggregate'(整合输出)| 'per_file'(按文件 Tab 展示)
57+
// ollama: 本地 Ollama 设置
58+
// - baseURL: Ollama 服务地址,默认 'http://localhost:11434'
59+
// - model: 模型名称,例如 'qwen3:8b'、'deepseek-r1:14b'
60+
// deepseek: DeepSeek 云端设置
61+
// - baseURL: API 地址,默认 'https://api.deepseek.com'
62+
// - apiKeyEnv: 存放密钥的环境变量名,默认 'DEEPSEEK_API_KEY'
63+
// - model: 模型名称,默认 'deepseek-chat'
64+
// fileTypes: 需要审查的文件类型扩展名列表
65+
// scope: 审查范围,可选值: 'staged' | 'allChanged' | { include?: string[], exclude?: string[] }
66+
// ui: 页面与交互设置
67+
// - openBrowser: 是否自动打开浏览器
68+
// - theme: 主题,目前仅 'github'
69+
// - port: 预览服务端口
70+
// limits: 限制项
71+
// - maxDiffLines: 最大 diff 行数
72+
// - maxFiles: 最大审查文件数
73+
// rules: 审查关注点,可选值: 'security' | 'performance' | 'style' | 'tests'
74+
// prompt: 通用提示词
75+
// output: 输出目录配置
76+
// - dir: 本地输出目录
77+
export default {
78+
provider: 'deepseek',
79+
review: { enabled: true, mode: 'aggregate' },
80+
deepseek: { baseURL: 'https://api.deepseek.com', apiKeyEnv: 'DEEPSEEK_API_KEY', model: 'deepseek-chat' },
81+
ollama: { baseURL: 'http://localhost:11434', model: 'qwen3:8b' },
82+
fileTypes: ['ts', 'tsx', 'js', 'jsx', 'json', 'md', 'py', 'go', 'rs'],
83+
scope: 'staged',
84+
ui: { openBrowser: true, theme: 'github', port: 5175 },
85+
limits: { maxDiffLines: 10000, maxFiles: 100 },
86+
rules: { focus: ['security', 'performance', 'style', 'tests'] },
87+
prompt: '作为资深代码审查工程师,从安全、性能、代码风格与测试覆盖角度审查本次变更,指出问题与改进建议,并给出必要的示例补丁。',
88+
output: { dir: '.code-gate' }
89+
}
90+
```
91+
92+
## 使用流程
93+
- 运行 `git commit` 时会询问是否进行 Review:
94+
- 选择否:正常提交。
95+
- 选择是:抓取 `staged diff` 调用 AI 审查,生成本地页面并打印预览 URL,再询问是否继续提交。
96+
- 非交互环境会自动跳过。
97+
- 如需在非交互环境强制执行,可在 `.husky/pre-commit` 中使用:`npx code-gate hook -f`
98+
- 页面顶部会显示 AI 状态(是否参与、Provider、Model、错误信息)
99+
- 支持按文件 Tab 展示:在配置中设置 `"review": { "mode": "per_file" }`,每个文件一个 Tab,Tabs 超出视野时横向滚动,每个文件顶部展示该文件的审查内容
100+
101+
## 故障排查
102+
- 页面只有 diff、没有 AI 审查内容:
103+
- DeepSeek:确保设置了环境变量 `DEEPSEEK_API_KEY`,并且 `provider``deepseek`;可在 shell 中 `export DEEPSEEK_API_KEY="your_key"`
104+
- Ollama:确保本地 Ollama 正在运行(默认 `http://localhost:11434`),并且模型已安装;例如 `ollama list` 查看,`ollama pull qwen2.5-coder`
105+
- 可在 `code-gate.config.json` 中切换 `provider`,调整 `prompt``ui.port`
106+
- 出错时页面顶部会显示原因与解决建议
107+
108+
## DeepSeek 集成
109+
- 使用 OpenAI 兼容接口 `https://api.deepseek.com`,需在环境变量设置密钥:`DEEPSEEK_API_KEY`
110+
- 参考文档:https://api-docs.deepseek.com/
111+
112+
## Ollama 集成
113+
- 通过本地 HTTP 接口调用,不内置安装;需用户自行安装与启动 Ollama。
114+
- 默认地址:`http://localhost:11434`
115+
116+
## 注意
117+
- 不会将密钥写入仓库;配置建议走环境变量。
118+
- 大 Diff 会消耗模型 token,可通过 `limits` 控制。

bin/code-gate.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env node
2+
import('../dist/cli.js')
3+
.then(async (mod) => {
4+
if (mod && typeof mod.run === 'function') {
5+
await mod.run()
6+
} else {
7+
console.error('code-gate: CLI not found in dist/cli.js')
8+
process.exit(1)
9+
}
10+
})
11+
.catch(async () => {
12+
const { createRequire } = await import('module')
13+
const require = createRequire(import.meta.url)
14+
const path = require('path')
15+
const url = require('url')
16+
const fs = require('fs')
17+
const dist = path.join(path.dirname(url.fileURLToPath(import.meta.url)), '..', 'dist', 'cli.js')
18+
if (fs.existsSync(dist)) {
19+
const mod = await import(url.pathToFileURL(dist).href)
20+
if (mod && typeof mod.run === 'function') await mod.run()
21+
else {
22+
console.error('code-gate: CLI not found in dist/cli.js')
23+
process.exit(1)
24+
}
25+
} else {
26+
console.error('code-gate is not built yet. Run `npm run build`.')
27+
process.exit(1)
28+
}
29+
})

0 commit comments

Comments
 (0)