ai-git-tools 2.0.75 → 2.0.77
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-git-tools",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.77",
|
|
4
4
|
"description": "AI-powered Git automation tools for commit messages and PR generation",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"CHANGELOG.md"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@github/copilot-sdk": "0.1
|
|
42
|
+
"@github/copilot-sdk": "^0.2.1",
|
|
43
43
|
"commander": "^12.0.0",
|
|
44
44
|
"chalk": "^5.3.0",
|
|
45
45
|
"ora": "^8.0.1",
|
|
@@ -96,16 +96,30 @@ function getAllChanges() {
|
|
|
96
96
|
async function analyzeAndGroupChanges(changes, config) {
|
|
97
97
|
console.log('🤖 正在使用 AI 分析變更並分組...\n');
|
|
98
98
|
|
|
99
|
-
//
|
|
100
|
-
const
|
|
99
|
+
// 檔案數量超過此閾值時,改為僅傳送檔名+狀態,不含 diff 內容,避免 prompt 超過模型 context window
|
|
100
|
+
const FILE_DIFF_THRESHOLD = 30;
|
|
101
|
+
const useFilenameOnly = changes.length > FILE_DIFF_THRESHOLD;
|
|
102
|
+
|
|
103
|
+
if (useFilenameOnly) {
|
|
104
|
+
console.log(`📝 檔案數量較多(${changes.length} 個),使用檔名分析模式以避免超出模型限制\n`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 每個檔案的 diff 字元預算(字元數,非行數)
|
|
108
|
+
const perFileBudget = Math.floor(config.ai.maxDiffLength / Math.max(changes.length, 1));
|
|
109
|
+
|
|
101
110
|
const changeSummary = changes
|
|
102
111
|
.map((change, index) => {
|
|
103
|
-
const diff = getFileDiff(change.filePath, change.isNew, change.isDeleted);
|
|
104
|
-
const lines = diff.split('\n');
|
|
105
|
-
const truncatedDiff = lines.slice(0, Math.min(50, maxDiffPerFile / 100)).join('\n');
|
|
106
112
|
let status = '(已修改)';
|
|
107
113
|
if (change.isNew) status = '(新檔案)';
|
|
108
114
|
if (change.isDeleted) status = '(已刪除)';
|
|
115
|
+
|
|
116
|
+
if (useFilenameOnly) {
|
|
117
|
+
return `[檔案 ${index}] ${change.filePath} ${status}`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const diff = getFileDiff(change.filePath, change.isNew, change.isDeleted);
|
|
121
|
+
const truncatedDiff =
|
|
122
|
+
diff.length > perFileBudget ? diff.substring(0, perFileBudget) + '\n...(已截斷)' : diff;
|
|
109
123
|
return `[檔案 ${index}] ${change.filePath}\n${status}\n${truncatedDiff}\n`;
|
|
110
124
|
})
|
|
111
125
|
.join('\n---\n\n');
|
|
@@ -180,13 +194,19 @@ ${changeSummary}
|
|
|
180
194
|
* 為特定群組生成 commit message
|
|
181
195
|
*/
|
|
182
196
|
async function generateCommitMessage(group, files, config) {
|
|
197
|
+
// 每個檔案最多 2000 字元,避免單一群組內大量 diff 超出限制
|
|
198
|
+
const MAX_DIFF_PER_FILE = 2000;
|
|
183
199
|
const filesList = files
|
|
184
200
|
.map((file) => {
|
|
185
201
|
const diff = getFileDiff(file.filePath, file.isNew, file.isDeleted);
|
|
202
|
+
const truncatedDiff =
|
|
203
|
+
diff.length > MAX_DIFF_PER_FILE
|
|
204
|
+
? diff.substring(0, MAX_DIFF_PER_FILE) + '\n...(已截斷)'
|
|
205
|
+
: diff;
|
|
186
206
|
let status = '修改';
|
|
187
207
|
if (file.isNew) status = '新增';
|
|
188
208
|
if (file.isDeleted) status = '刪除';
|
|
189
|
-
return `檔案: ${file.filePath} [${status}]\n${
|
|
209
|
+
return `檔案: ${file.filePath} [${status}]\n${truncatedDiff}`;
|
|
190
210
|
})
|
|
191
211
|
.join('\n\n---\n\n');
|
|
192
212
|
|
|
@@ -65,8 +65,7 @@ export class AIAnalyzer {
|
|
|
65
65
|
* 生成 PR 內容(失敗時自動重建 client 重試一次)
|
|
66
66
|
*/
|
|
67
67
|
async generatePRContent(commits, diff) {
|
|
68
|
-
const
|
|
69
|
-
const prompt = this.buildPRPrompt(commits, diff, skillsSummary);
|
|
68
|
+
const prompt = this.buildPRPrompt(commits, diff);
|
|
70
69
|
|
|
71
70
|
for (let attempt = 1; attempt <= 2; attempt++) {
|
|
72
71
|
try {
|
|
@@ -79,7 +78,10 @@ export class AIAnalyzer {
|
|
|
79
78
|
|
|
80
79
|
const responsePromise = session.sendAndWait({ prompt });
|
|
81
80
|
const timeoutPromise = new Promise((_, reject) => {
|
|
82
|
-
setTimeout(
|
|
81
|
+
setTimeout(
|
|
82
|
+
() => reject(new Error(`AI 請求超時 (${AI_TIMEOUT_MS / 1000} 秒)`)),
|
|
83
|
+
AI_TIMEOUT_MS
|
|
84
|
+
);
|
|
83
85
|
});
|
|
84
86
|
|
|
85
87
|
const response = await Promise.race([responsePromise, timeoutPromise]);
|
|
@@ -94,9 +96,15 @@ export class AIAnalyzer {
|
|
|
94
96
|
// SDK 內部 session.idle timeout(hardcoded 60s)→ 重試一次
|
|
95
97
|
const isSessionIdleTimeout =
|
|
96
98
|
error.message?.includes('session.idle') || error.message?.includes('Timeout after');
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
if (isSessionIdleTimeout) {
|
|
100
|
+
if (attempt === 1) {
|
|
101
|
+
log.warning(` AI session 超時,即將重試...\n`);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
// 兩次都超時 → 模型速度不足,給出具體建議
|
|
105
|
+
log.error(` 模型 ${this.model} 在此變更大小下回應過慢`);
|
|
106
|
+
log.info(` 建議改用更快的模型:ai-git-tools pr --model gpt-5.4\n`);
|
|
107
|
+
throw new Error(`AI 生成超時:模型 ${this.model} 回應過慢,請加 --model gpt-5.4 重試`);
|
|
100
108
|
}
|
|
101
109
|
throw error;
|
|
102
110
|
}
|
|
@@ -121,7 +129,10 @@ export class AIAnalyzer {
|
|
|
121
129
|
// 使用超時保護(150 秒)
|
|
122
130
|
const responsePromise = session.sendAndWait({ prompt });
|
|
123
131
|
const timeoutPromise = new Promise((_, reject) => {
|
|
124
|
-
setTimeout(
|
|
132
|
+
setTimeout(
|
|
133
|
+
() => reject(new Error(`AI 請求超時 (${AI_TIMEOUT_MS / 1000} 秒)`)),
|
|
134
|
+
AI_TIMEOUT_MS
|
|
135
|
+
);
|
|
125
136
|
});
|
|
126
137
|
|
|
127
138
|
const response = await Promise.race([responsePromise, timeoutPromise]);
|
|
@@ -160,12 +171,10 @@ export class AIAnalyzer {
|
|
|
160
171
|
/**
|
|
161
172
|
* 建立 PR 生成 Prompt
|
|
162
173
|
*/
|
|
163
|
-
buildPRPrompt(commits, diff
|
|
174
|
+
buildPRPrompt(commits, diff) {
|
|
164
175
|
return `你是一個專業的前端工程師,熟悉 Next.js、React 效能優化和團隊開發規範。
|
|
165
176
|
請根據以下 commit 訊息和程式碼變更,直接輸出一個清晰的 Pull Request 標題和描述。
|
|
166
177
|
|
|
167
|
-
${skillsSummary}
|
|
168
|
-
|
|
169
178
|
**輸出格式**(不要加任何引導語,直接輸出以下內容):
|
|
170
179
|
|
|
171
180
|
# [type]: [PR 標題]
|
|
@@ -190,18 +199,7 @@ ${skillsSummary}
|
|
|
190
199
|
- [ ] ⚡ 效能改進 (perf)
|
|
191
200
|
- [ ] 🔧 其他 (chore)
|
|
192
201
|
|
|
193
|
-
>
|
|
194
|
-
>
|
|
195
|
-
> **判斷準則**:
|
|
196
|
-
> - ✨ **新功能 (feat)**: 新增檔案、新增 API、新增組件、新增功能邏輯、新增配置選項
|
|
197
|
-
> - 🐛 **Bug 修復 (fix)**: 修復錯誤、修正邏輯問題
|
|
198
|
-
> - ♻️ **重構 (refactor)**: 重組程式碼結構但不改變功能
|
|
199
|
-
> - 💄 **樣式調整 (style)**: UI/CSS 調整、格式化
|
|
200
|
-
> - 📝 **文件更新 (docs)**: README、註解、文檔變更
|
|
201
|
-
> - ⚡ **效能改進 (perf)**: 優化效能
|
|
202
|
-
> - 🔧 **其他 (chore)**: 建構工具、依賴更新、配置調整
|
|
203
|
-
>
|
|
204
|
-
> **特別注意**: 如果 diff 中有「新增檔案」或「新增功能」,**務必勾選** ✨ 新功能 (feat)
|
|
202
|
+
> 根據 diff 和 commit 自動勾選(可複選),[ ] 改為 [x];有新增檔案或功能必勾 ✨ feat
|
|
205
203
|
|
|
206
204
|
## 🧪 測試方法
|
|
207
205
|
1. [具體的測試步驟 1]
|
|
@@ -230,21 +228,7 @@ ${skillsSummary}
|
|
|
230
228
|
|
|
231
229
|
---
|
|
232
230
|
|
|
233
|
-
|
|
234
|
-
1. PR 標題格式:type: 簡短描述(不超過 50 字)
|
|
235
|
-
2. type 必須符合 Conventional Commits
|
|
236
|
-
3. 變更摘要用 2-3 句話概括整體影響
|
|
237
|
-
4. 全部使用繁體中文(台灣正體)
|
|
238
|
-
5. 不要在開頭加引導語句
|
|
239
|
-
6. 直接開始輸出 # [type]: [標題]
|
|
240
|
-
7. **變更類型判斷必須準確**:
|
|
241
|
-
- 檢查 diff 中是否有 "new file mode" 或大量 "+++" 行(表示新增檔案)
|
|
242
|
-
- 檢查 commit 訊息是否包含「新增」、「add」、「feat」等關鍵字
|
|
243
|
-
- 檢查主要變更列表,如果提到「新增 xxx」就必須勾選 ✨ 新功能 (feat)
|
|
244
|
-
- 新增配置檔、新增組件、新增 API、新增功能都算 feat
|
|
245
|
-
- 一個 PR 可以同時是多種類型(如:feat + refactor + chore)
|
|
246
|
-
8. **Risk Level 判斷**:HIGH = 影響付款/登入/資料寫入核心流程;MEDIUM = 影響現有功能但有降級保護;LOW = 新增功能或純重構
|
|
247
|
-
9. **Reviewer 重點**:列出最值得仔細看的 1-3 個地方(核心演算法、架構決策、潛在邊界條件)
|
|
231
|
+
**規則**:直接輸出 # [type]: [標題],繁體中文(台灣正體),type 符合 Conventional Commits;新增檔案/功能優先 feat,可複選多種類型;Risk Level:HIGH=核心流程,MEDIUM=影響現有功能,LOW=新增或重構;Reviewer 重點列 1-3 個值得仔細看的地方。
|
|
248
232
|
|
|
249
233
|
---
|
|
250
234
|
|
|
@@ -379,7 +363,7 @@ ${diff.length > CONSTANTS.MAX_DIFF_LENGTH ? '\n... (內容過長已截斷)' : ''
|
|
|
379
363
|
let riskLevel = '低';
|
|
380
364
|
const riskReasons = [];
|
|
381
365
|
|
|
382
|
-
changedFiles.forEach(
|
|
366
|
+
changedFiles.forEach(file => {
|
|
383
367
|
const lower = file.toLowerCase();
|
|
384
368
|
if (lower.includes('/api/')) {
|
|
385
369
|
if (!impacts.includes('API 層')) impacts.push('API 層');
|
|
@@ -397,7 +381,7 @@ ${diff.length > CONSTANTS.MAX_DIFF_LENGTH ? '\n... (內容過長已截斷)' : ''
|
|
|
397
381
|
});
|
|
398
382
|
|
|
399
383
|
const warnings = [];
|
|
400
|
-
const hasTestFiles = changedFiles.some(
|
|
384
|
+
const hasTestFiles = changedFiles.some(f => f.includes('test') || f.includes('spec'));
|
|
401
385
|
if (!hasTestFiles && changedFiles.length > 3) {
|
|
402
386
|
warnings.push({
|
|
403
387
|
level: '⚠️',
|
|
@@ -435,7 +419,7 @@ ${diff.length > CONSTANTS.MAX_DIFF_LENGTH ? '\n... (內容過長已截斷)' : ''
|
|
|
435
419
|
|
|
436
420
|
if (blastRadius.riskReasons && blastRadius.riskReasons.length > 0) {
|
|
437
421
|
enhancedBody += `**風險因素**:\n`;
|
|
438
|
-
blastRadius.riskReasons.forEach(
|
|
422
|
+
blastRadius.riskReasons.forEach(reason => {
|
|
439
423
|
enhancedBody += `- ${reason}\n`;
|
|
440
424
|
});
|
|
441
425
|
enhancedBody += '\n';
|
|
@@ -443,7 +427,7 @@ ${diff.length > CONSTANTS.MAX_DIFF_LENGTH ? '\n... (內容過長已截斷)' : ''
|
|
|
443
427
|
|
|
444
428
|
if (blastRadius.externalBehaviors && blastRadius.externalBehaviors.length > 0) {
|
|
445
429
|
enhancedBody += `**對外行為變更**:\n`;
|
|
446
|
-
blastRadius.externalBehaviors.forEach(
|
|
430
|
+
blastRadius.externalBehaviors.forEach(behavior => {
|
|
447
431
|
enhancedBody += `- ${behavior}\n`;
|
|
448
432
|
});
|
|
449
433
|
enhancedBody += '\n';
|
|
@@ -452,7 +436,7 @@ ${diff.length > CONSTANTS.MAX_DIFF_LENGTH ? '\n... (內容過長已截斷)' : ''
|
|
|
452
436
|
// 添加規範警告
|
|
453
437
|
if (warnings.length > 0) {
|
|
454
438
|
enhancedBody += '\n## ⚠️ 注意事項\n\n';
|
|
455
|
-
warnings.forEach(
|
|
439
|
+
warnings.forEach(warning => {
|
|
456
440
|
enhancedBody += `${warning.level} **${warning.message}**\n`;
|
|
457
441
|
if (warning.suggestion) {
|
|
458
442
|
enhancedBody += ` - 💡 ${warning.suggestion}\n`;
|