ai-git-tools 2.0.45 → 2.0.46
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
package/src/core/ai-client.js
CHANGED
|
@@ -7,39 +7,52 @@ import { CopilotClient } from '@github/copilot-sdk';
|
|
|
7
7
|
|
|
8
8
|
export class AIClient {
|
|
9
9
|
/**
|
|
10
|
-
* 發送 prompt
|
|
10
|
+
* 發送 prompt 並等待回應(帶重試機制)
|
|
11
|
+
*
|
|
12
|
+
* @param {string} prompt
|
|
13
|
+
* @param {string} model
|
|
14
|
+
* @param {number} maxRetries
|
|
15
|
+
* @param {number} timeout - 毫秒,會直接傳給 SDK 的 sendAndWait
|
|
11
16
|
*/
|
|
12
|
-
static async sendAndWait(prompt, model = '
|
|
17
|
+
static async sendAndWait(prompt, model = 'claude-sonnet-4.5', maxRetries = 3, timeout = 60000) {
|
|
13
18
|
let lastError = null;
|
|
14
19
|
|
|
15
20
|
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
16
21
|
const client = new CopilotClient();
|
|
22
|
+
let session = null;
|
|
17
23
|
try {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
setTimeout(() => reject(new Error(`AI 請求超時 (${timeout}ms)`)), timeout);
|
|
24
|
+
session = await client.createSession({ model });
|
|
25
|
+
|
|
26
|
+
// 收集 session.error 事件(讓 lastError 包含細節)
|
|
27
|
+
session.on('session.error', (event) => {
|
|
28
|
+
lastError = new Error(event.data?.message || JSON.stringify(event.data));
|
|
24
29
|
});
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
// 把 timeout 直接傳給 SDK,讓 SDK 管理逾時
|
|
32
|
+
const response = await session.sendAndWait({ prompt }, timeout);
|
|
33
|
+
|
|
34
|
+
// response 為 undefined 表示 session 結束但沒有助理回訊(通常是錯誤)
|
|
35
|
+
if (!response) {
|
|
36
|
+
throw new Error(`AI 未回傳任何訊息(model: ${model})`);
|
|
37
|
+
}
|
|
27
38
|
|
|
28
39
|
const content = response?.data?.content || '';
|
|
40
|
+
if (!content) {
|
|
41
|
+
throw new Error('AI 回傳空內容');
|
|
42
|
+
}
|
|
29
43
|
return content.trim();
|
|
44
|
+
|
|
30
45
|
} catch (error) {
|
|
31
46
|
lastError = error;
|
|
32
47
|
if (attempt < maxRetries) {
|
|
33
|
-
console.log(`⚠️ AI 請求失敗,重試第 ${attempt}/${maxRetries}
|
|
48
|
+
console.log(`⚠️ AI 請求失敗,重試第 ${attempt}/${maxRetries} 次... (${error.message})`);
|
|
34
49
|
await new Promise((resolve) => setTimeout(resolve, 1000 * attempt));
|
|
35
50
|
}
|
|
36
51
|
} finally {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
await client.stop();
|
|
40
|
-
} catch (e) {
|
|
41
|
-
// 忽略關閉錯誤
|
|
52
|
+
if (session) {
|
|
53
|
+
try { await session.destroy(); } catch (_e) { /* 忽略 */ }
|
|
42
54
|
}
|
|
55
|
+
try { await client.stop(); } catch (_e) { /* 忽略 */ }
|
|
43
56
|
}
|
|
44
57
|
}
|
|
45
58
|
|
|
@@ -36,7 +36,7 @@ export class CodeGenerator {
|
|
|
36
36
|
|
|
37
37
|
let response;
|
|
38
38
|
try {
|
|
39
|
-
// 代碼生成需要較長時間,使用 180 秒 timeout
|
|
39
|
+
// 代碼生成需要較長時間,使用 180 秒 timeout(直接傳給 SDK)
|
|
40
40
|
response = await AIClient.sendAndWait(
|
|
41
41
|
prompt,
|
|
42
42
|
this.model,
|
|
@@ -44,9 +44,11 @@ export class CodeGenerator {
|
|
|
44
44
|
180_000
|
|
45
45
|
);
|
|
46
46
|
} catch (error) {
|
|
47
|
-
|
|
48
|
-
console.warn(
|
|
49
|
-
|
|
47
|
+
console.warn(` ❌ AI 調用最終失敗:${error.message}`);
|
|
48
|
+
console.warn(' 💡 排查建議:');
|
|
49
|
+
console.warn(` 1. 確認 model「${this.model}」已在你的 Copilot 方案中啟用`);
|
|
50
|
+
console.warn(' 2. 嘗試改用 gpt-4.1:在 .ai-git-config 的 autodev.aiModel 改為 "gpt-4.1"');
|
|
51
|
+
console.warn(' 3. 確認 gh auth status 有登入 GitHub');
|
|
50
52
|
return [];
|
|
51
53
|
}
|
|
52
54
|
|