gitlab-ai-review 2.1.0 → 2.3.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/index.js +6 -46
- package/lib/prompt-tools.js +6 -71
- package/package.json +1 -1
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.
|
|
18
|
+
this.version = '2.3.0';
|
|
19
19
|
|
|
20
20
|
// 如果传入了配置,使用手动配置;否则使用自动检测
|
|
21
21
|
if (options.token || options.gitlab) {
|
|
@@ -137,61 +137,21 @@ 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 - 选项
|
|
184
|
-
* @param {number} options.maxFiles -
|
|
143
|
+
* @param {number} options.maxFiles - 最大审查文件数量(默认不限制)
|
|
185
144
|
* @returns {Promise<Array>} 评论结果数组
|
|
186
145
|
*/
|
|
187
146
|
async reviewAndCommentOnLines(options = {}) {
|
|
188
|
-
const { maxFiles =
|
|
147
|
+
const { maxFiles = Infinity } = options;
|
|
189
148
|
const changes = await this.getMergeRequestChanges();
|
|
190
149
|
const results = [];
|
|
191
150
|
|
|
192
|
-
|
|
151
|
+
const filesToReview = maxFiles === Infinity ? changes.length : Math.min(maxFiles, changes.length);
|
|
152
|
+
console.log(`共 ${changes.length} 个文件需要审查${maxFiles === Infinity ? '(不限制数量)' : `(最多审查 ${maxFiles} 个)`}`);
|
|
193
153
|
|
|
194
|
-
for (const change of changes.slice(0,
|
|
154
|
+
for (const change of changes.slice(0, filesToReview)) {
|
|
195
155
|
const fileName = change.new_path || change.old_path;
|
|
196
156
|
|
|
197
157
|
try {
|
package/lib/prompt-tools.js
CHANGED
|
@@ -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 - 文件名
|
|
@@ -136,7 +71,8 @@ export function buildFileReviewPrompt(fileName, meaningfulChanges) {
|
|
|
136
71
|
|
|
137
72
|
1. 仔细审查每一行变更,判断是否存在问题
|
|
138
73
|
2. **只对有问题的行提出审查意见**,没有问题的行不需要评论
|
|
139
|
-
3.
|
|
74
|
+
3. **必须使用中文**返回审查意见
|
|
75
|
+
4. 返回 JSON 格式的结果,格式如下:
|
|
140
76
|
|
|
141
77
|
\`\`\`json
|
|
142
78
|
{
|
|
@@ -144,7 +80,7 @@ export function buildFileReviewPrompt(fileName, meaningfulChanges) {
|
|
|
144
80
|
{
|
|
145
81
|
"lineNumber": 15,
|
|
146
82
|
"hasIssue": true,
|
|
147
|
-
"comment": "
|
|
83
|
+
"comment": "这里用中文描述具体的问题和改进建议"
|
|
148
84
|
},
|
|
149
85
|
{
|
|
150
86
|
"lineNumber": 20,
|
|
@@ -154,8 +90,9 @@ export function buildFileReviewPrompt(fileName, meaningfulChanges) {
|
|
|
154
90
|
}
|
|
155
91
|
\`\`\`
|
|
156
92
|
|
|
157
|
-
|
|
158
|
-
|
|
93
|
+
5. 如果所有代码都没有问题,返回空的 reviews 数组:\`{"reviews": []}\`
|
|
94
|
+
6. 只返回 JSON,不要包含其他文字说明
|
|
95
|
+
7. comment 字段必须使用中文,要简洁明了`;
|
|
159
96
|
|
|
160
97
|
return prompt;
|
|
161
98
|
}
|
|
@@ -177,8 +114,6 @@ export function buildFileReviewMessages(fileName, meaningfulChanges, projectProm
|
|
|
177
114
|
// 导出函数
|
|
178
115
|
export default {
|
|
179
116
|
buildSystemPrompt,
|
|
180
|
-
buildLineReviewPrompt,
|
|
181
|
-
buildLineReviewMessages,
|
|
182
117
|
buildFileReviewPrompt,
|
|
183
118
|
buildFileReviewMessages,
|
|
184
119
|
};
|