Skip to content

Commit 9d2d722

Browse files
committed
feat(ai-commit): 优化性能并添加上下文支持
- 性能优化:批量获取 Git Diff,提升分析速度 - 上下文注入:AI Prompt 支持项目元数据和最近提交历史 - 新增功能:添加“查看 AI Prompt”命令,便于调试 - 配置调整:优化 tasks.json 中的 watch 任务命令 - 错误处理:改进 API 调用失败时的错误传递
1 parent fac0188 commit 9d2d722

6 files changed

Lines changed: 282 additions & 99 deletions

File tree

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"version": "2.0.0",
33
"tasks": [
44
{
5-
"type": "npm",
6-
"script": "watch",
5+
"type": "shell",
6+
"command": "npm run watch",
77
"group": "build",
88
"isBackground": true,
99
"problemMatcher": {

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@
279279
"title": "应用提交信息",
280280
"icon": "$(check)"
281281
},
282+
{
283+
"command": "fusi-tools.viewAiPrompt",
284+
"title": "View AI Prompt (查看发送给 AI 的 Prompt)",
285+
"icon": "$(eye)",
286+
"category": "Fusi Tools"
287+
},
282288
{
283289
"command": "fusi-tools.toggleSmartTranslate",
284290
"title": "启用/禁用 智能翻译",

src/features/aiCommit/ai.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ export class AiService {
1515

1616
/**
1717
* 执行生成策略:
18-
* 1. 发起快速请求 (V3) 生成 "Concise" (简短) + "Conventional" (规范) 风格
19-
* 2. 数据可用时立即调用 onUpdate
18+
* 1. 发起快速请求 (V3) 生成结果
19+
* 2. 注入 Project Meta 和 Recent Commits 上下文
2020
*/
21-
async generate(diff: string, onUpdate: (options: CommitOption[]) => void) {
21+
async generate(
22+
diff: string,
23+
projectMeta: string,
24+
recentCommits: string[],
25+
onUpdate: (options: CommitOption[]) => void
26+
) {
2227
const apiKey = this.config.get<string>("apiKey");
2328
const baseUrl = this.config.get<string>("baseUrl");
2429
const fastModel = this.config.get<string>("model") || "deepseek-chat";
@@ -28,21 +33,38 @@ export class AiService {
2833
}
2934

3035
try {
36+
// 准备 System Prompt 注入
37+
let systemPrompt = FAST_PROMPT;
38+
39+
// 注入 Project Meta
40+
systemPrompt = systemPrompt.replace("{{PROJECT_META}}", projectMeta || "(None)");
41+
42+
// 注入 Recent Commits
43+
const commitsStr = recentCommits.length > 0
44+
? recentCommits.map(c => `- ${c}`).join("\n")
45+
: "(None)";
46+
systemPrompt = systemPrompt.replace("{{RECENT_COMMITS}}", commitsStr);
47+
3148
const options = await this.fetchFastOptions(
3249
baseUrl!,
3350
apiKey,
3451
fastModel,
52+
systemPrompt,
3553
diff
3654
);
55+
56+
this.sortOptions(options);
3757
onUpdate(options);
3858
} catch (err) {
3959
Logger.error("快速模型 (Fast Model) 请求失败", err);
4060
console.error("快速模型失败:", err);
61+
throw err; // Re-throw to let upstream handle UI error
4162
}
4263
}
4364

4465
private sortOptions(options: CommitOption[]) {
45-
const order = ["Emoji", "Conventional", "StandardShort", "Smart"];
66+
// Define sort order
67+
const order = ["Emoji", "StandardShort", "Conventional", "Historical", "Smart"];
4668
options.sort((a, b) => {
4769
let ia = order.indexOf(a.type);
4870
let ib = order.indexOf(b.type);
@@ -56,9 +78,10 @@ export class AiService {
5678
baseUrl: string,
5779
apiKey: string,
5880
model: string,
81+
systemPrompt: string,
5982
diff: string
6083
): Promise<CommitOption[]> {
61-
return this.callApi(baseUrl, apiKey, model, FAST_PROMPT, diff);
84+
return this.callApi(baseUrl, apiKey, model, systemPrompt, diff);
6285
}
6386

6487
private async callApi(
@@ -77,10 +100,6 @@ export class AiService {
77100
stream: false,
78101
};
79102

80-
// [DEBUG] 打印完整请求参数
81-
// console.log("--- [AI Commit Request Payload] ---");
82-
// console.log(JSON.stringify(requestBody, null, 2));
83-
// console.log("-----------------------------------");
84103
Logger.info(`[AI 请求] 模型: ${model}, 字符数: ${userContent.length}`);
85104

86105
try {
@@ -105,8 +124,8 @@ export class AiService {
105124
return this.parseResponse(content);
106125
} catch (error) {
107126
Logger.error(`API 调用异常: ${model}`, error);
108-
console.error(error);
109-
return []; // Return empty on failure to not crash the whole Promise.all
127+
// Don't swallow here, let caller handle
128+
throw error;
110129
}
111130
}
112131

@@ -120,13 +139,12 @@ export class AiService {
120139
if (Array.isArray(parsed)) {
121140
return parsed;
122141
} else if (typeof parsed === "object") {
123-
// Should not happen with new prompts, but fallback
124142
return [parsed];
125143
}
126144
return [];
127145
} catch (e) {
128146
Logger.error("AI 响应解析失败", e);
129-
console.error("Parse error", content);
147+
console.error("Parse error content:", content);
130148
return [];
131149
}
132150
}

0 commit comments

Comments
 (0)