gitlab-ai-review 2.2.0 → 2.4.0

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/cli.js ADDED
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * GitLab AI Review CLI
5
+ * 支持通过 npx 直接执行代码审查
6
+ */
7
+
8
+ import { GitLabAIReview } from './index.js';
9
+
10
+ async function main() {
11
+ console.log('🚀 GitLab AI Review CLI 启动...\n');
12
+
13
+ try {
14
+ // 1. 创建 SDK 实例(自动从环境变量读取配置)
15
+ const sdk = new GitLabAIReview();
16
+
17
+ // 2. 验证配置
18
+ console.log('📋 配置信息:');
19
+ console.log(` - GitLab Host: ${sdk.config.gitlab.host}`);
20
+ console.log(` - Project ID: ${sdk.config.project.projectId}`);
21
+ console.log(` - MR IID: ${sdk.config.project.mergeRequestIid}`);
22
+ console.log(` - AI API Key: ${sdk.config.ai?.arkApiKey ? '✅ 已配置' : '❌ 未配置'}`);
23
+ console.log(` - Review Guard: ${sdk.config.ai?.guardConfig?.content ? '✅ 已加载' : '⚠️ 未找到'}`);
24
+ console.log();
25
+
26
+ // 3. 获取 MR 变更信息
27
+ console.log('📥 获取 MR 变更信息...');
28
+ const changes = await sdk.getMergeRequestChanges();
29
+ console.log(`✅ 共发现 ${changes.length} 个文件变更\n`);
30
+
31
+ // 4. 执行 AI 代码审查
32
+ if (sdk.config.ai?.arkApiKey && changes.length > 0) {
33
+ console.log('🤖 开始 AI 代码审查...\n');
34
+
35
+ const results = await sdk.reviewAndCommentOnLines();
36
+
37
+ console.log('\n📊 审查结果汇总:');
38
+ console.log(` - 审查文件数: ${results.length}`);
39
+ const successCount = results.filter(r => r.success).length;
40
+ const commentCount = results.reduce((sum, r) => sum + (r.commentCount || 0), 0);
41
+ console.log(` - 成功: ${successCount}`);
42
+ console.log(` - 添加评论数: ${commentCount}`);
43
+
44
+ if (results.some(r => !r.success)) {
45
+ console.log('\n⚠️ 部分文件审查失败,请检查上方日志');
46
+ process.exit(1);
47
+ } else {
48
+ console.log('\n✅ 代码审查完成!');
49
+ }
50
+ } else if (!sdk.config.ai?.arkApiKey) {
51
+ console.error('❌ 错误: 未配置 ARK_API_KEY 环境变量');
52
+ process.exit(1);
53
+ } else {
54
+ console.log('ℹ️ 没有文件变更,跳过审查');
55
+ }
56
+
57
+ } catch (error) {
58
+ console.error('\n❌ 执行失败:', error.message);
59
+ console.error(error.stack);
60
+ process.exit(1);
61
+ }
62
+ }
63
+
64
+ // 执行主函数
65
+ main();
66
+
package/index.js CHANGED
@@ -15,7 +15,7 @@ import * as DiffParser from './lib/diff-parser.js';
15
15
  export class GitLabAIReview {
16
16
  constructor(options = {}) {
17
17
  this.name = 'GitLab AI Review SDK';
18
- this.version = '2.2.0';
18
+ this.version = '2.3.0';
19
19
 
20
20
  // 如果传入了配置,使用手动配置;否则使用自动检测
21
21
  if (options.token || options.gitlab) {
@@ -137,47 +137,6 @@ export class GitLabAIReview {
137
137
  return this.aiClient;
138
138
  }
139
139
 
140
- /**
141
- * AI 审查特定行的变更并在该行添加评论(附带项目 prompt)
142
- * @param {Object} change - 代码变更对象
143
- * @param {Object} changeInfo - 具体的行变更信息
144
- * @returns {Promise<Object>} 评论结果
145
- */
146
- async reviewAndCommentOnLine(change, changeInfo) {
147
- const aiClient = this.getAIClient();
148
-
149
- // 获取项目配置的 prompt(来自 reviewguard.md)
150
- const projectPrompt = this.config.ai?.guardConfig?.content || '';
151
-
152
- // 构建针对特定行的审查消息(附带项目 prompt)
153
- const messages = PromptTools.buildLineReviewMessages({
154
- ...changeInfo,
155
- fileName: change.new_path || change.old_path,
156
- }, projectPrompt);
157
-
158
- // 调用 AI 审查
159
- const review = await aiClient.sendMessage(messages);
160
-
161
- // 在该行添加评论
162
- const lineInfo = {
163
- filePath: change.new_path || change.old_path,
164
- oldPath: change.old_path,
165
- newLine: changeInfo.lineNumber,
166
- };
167
-
168
- const commentResult = await this.gitlabClient.createLineComment(
169
- this.config.project.projectId,
170
- this.config.project.mergeRequestIid,
171
- `🤖 **AI 代码审查**\n\n${review.content}`,
172
- lineInfo
173
- );
174
-
175
- return {
176
- review,
177
- comment: commentResult,
178
- };
179
- }
180
-
181
140
  /**
182
141
  * AI 审查 MR 的所有有意义的变更并自动添加行级评论(按文件批量处理)
183
142
  * @param {Object} options - 选项
@@ -27,71 +27,6 @@ export function buildSystemPrompt(projectPrompt = '') {
27
27
  return prompt;
28
28
  }
29
29
 
30
- /**
31
- * 构建针对特定行变更的审查提示词(附带项目 prompt)
32
- * @param {Object} changeInfo - 变更信息
33
- * @param {string} changeInfo.content - 变更内容
34
- * @param {string} changeInfo.type - 变更类型(addition/deletion)
35
- * @param {number} changeInfo.lineNumber - 行号
36
- * @param {string} changeInfo.fileName - 文件名
37
- * @param {Object} changeInfo.context - 上下文
38
- * @returns {string} 审查提示词
39
- */
40
- export function buildLineReviewPrompt(changeInfo) {
41
- const { content, type, lineNumber, fileName, context } = changeInfo;
42
-
43
- let prompt = `请审查以下代码变更:
44
-
45
- **文件**: ${fileName}
46
- **行号**: ${lineNumber}
47
- **变更类型**: ${type === 'addition' ? '新增' : '删除'}
48
-
49
- **变更内容**:
50
- \`\`\`
51
- ${type === 'addition' ? '+' : '-'} ${content}
52
- \`\`\`
53
- `;
54
-
55
- if (context && (context.before.length > 0 || context.after.length > 0)) {
56
- prompt += '\n**上下文**:\n```\n';
57
- if (context.before.length > 0) {
58
- context.before.forEach(line => {
59
- prompt += ` ${line}\n`;
60
- });
61
- }
62
- prompt += `${type === 'addition' ? '+' : '-'} ${content}\n`;
63
- if (context.after.length > 0) {
64
- context.after.forEach(line => {
65
- prompt += ` ${line}\n`;
66
- });
67
- }
68
- prompt += '```\n';
69
- }
70
-
71
- prompt += `
72
- 请提供简洁的审查意见:
73
- 1. 如果有问题,指出具体问题
74
- 2. 如果有改进建议,提供具体的建议
75
- 3. 如果代码没有问题,简单说明"代码质量良好"
76
-
77
- 请直接给出审查意见,不要重复代码内容。`;
78
-
79
- return prompt;
80
- }
81
-
82
- /**
83
- * 构建针对特定行变更的完整消息数组(附带项目 prompt)
84
- * @param {Object} changeInfo - 变更信息
85
- * @param {string} projectPrompt - 项目配置的 prompt(来自 reviewguard.md)
86
- * @returns {Array} 消息数组
87
- */
88
- export function buildLineReviewMessages(changeInfo, projectPrompt = '') {
89
- return [
90
- { role: 'system', content: buildSystemPrompt(projectPrompt) },
91
- { role: 'user', content: buildLineReviewPrompt(changeInfo) },
92
- ];
93
- }
94
-
95
30
  /**
96
31
  * 构建整个文件所有变更行的批量审查提示词
97
32
  * @param {string} fileName - 文件名
@@ -179,8 +114,6 @@ export function buildFileReviewMessages(fileName, meaningfulChanges, projectProm
179
114
  // 导出函数
180
115
  export default {
181
116
  buildSystemPrompt,
182
- buildLineReviewPrompt,
183
- buildLineReviewMessages,
184
117
  buildFileReviewPrompt,
185
118
  buildFileReviewMessages,
186
119
  };
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "gitlab-ai-review",
3
- "version": "2.2.0",
4
- "description": "GitLab AI Review SDK - 按文件批量审查,只评论有问题的代码",
3
+ "version": "2.4.0",
4
+ "description": "GitLab AI Review SDK - 支持 SDK 调用和 CLI 执行",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
+ "bin": {
8
+ "gitlab-ai-review": "./cli.js"
9
+ },
7
10
  "scripts": {
8
11
  "test": "node test-info.js",
9
12
  "test:info": "node test-info.js"
10
13
  },
11
14
  "files": [
12
15
  "index.js",
16
+ "cli.js",
13
17
  "lib/",
14
18
  "test-info.js",
15
19
  "README.md"