Skip to content

Commit c52d079

Browse files
committed
refactor(ai-commit): 移除历史提交参考并简化选项
- 从 AI 服务中移除 `recentCommits` 参数和相关处理逻辑 - 删除 Git 服务中的 `getRecentCommits` 方法 - 更新系统提示,移除历史提交参考部分 - 简化提交选项排序,移除 'Historical' 类型 - 更新相关类型定义和上下文缓存结构
1 parent ba2d7d9 commit c52d079

4 files changed

Lines changed: 11 additions & 52 deletions

File tree

src/features/aiCommit/ai.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export class AiService {
2121
async generate(
2222
diff: string,
2323
projectMeta: string,
24-
recentCommits: string[],
2524
onUpdate: (options: CommitOption[]) => void
2625
) {
2726
const apiKey = this.config.get<string>("apiKey");
@@ -39,12 +38,6 @@ export class AiService {
3938
// 注入 Project Meta
4039
systemPrompt = systemPrompt.replace("{{PROJECT_META}}", projectMeta || "(None)");
4140

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-
4841
const options = await this.fetchFastOptions(
4942
baseUrl!,
5043
apiKey,
@@ -64,7 +57,7 @@ export class AiService {
6457

6558
private sortOptions(options: CommitOption[]) {
6659
// Define sort order
67-
const order = ["Emoji", "StandardShort", "Conventional", "Historical", "Smart"];
60+
const order = ["Emoji", "StandardShort", "Conventional", "Smart"];
6861
options.sort((a, b) => {
6962
let ia = order.indexOf(a.type);
7063
let ib = order.indexOf(b.type);

src/features/aiCommit/git.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -257,31 +257,7 @@ export class GitService {
257257
return totalOutput.trim();
258258
}
259259

260-
/**
261-
* 获取最近的 N 条提交记录作为参考
262-
*/
263-
async getRecentCommits(limit: number = 5): Promise<string[]> {
264-
const repo = this.repository;
265-
if (!repo) return [];
266-
try {
267-
// %s = subject only, %B = raw body (subject + body)
268-
// Let's use %B to capture full style (including body formatting)
269-
const output = await this.execCommand(
270-
`git log -n ${limit} --pretty=format:"%B%n------------------------"`,
271-
repo.rootUri.fsPath
272-
);
273-
274-
const debugLogs = output
275-
.split("------------------------")
276-
.map(s => s.trim())
277-
.filter(s => s.length > 0);
278-
279-
return debugLogs;
280-
} catch (e) {
281-
console.warn("Failed to fetch recent commits", e);
282-
return [];
283-
}
284-
}
260+
285261

286262
/**
287263
* 旧接口兼容

src/features/aiCommit/index.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function activate(context: vscode.ExtensionContext) {
2323
// 缓存 diff 结果,供 "预处理后生成" 使用
2424
let cachedDiff: string | null = null;
2525
// 缓存上下文信息,供 "预览 Prompt" 使用
26-
let lastContext: { diff: string; projectMeta: string; recentCommits: string[] } | null = null;
26+
let lastContext: { diff: string; projectMeta: string } | null = null;
2727

2828
// 1. 注册 TreeDataProvider
2929
context.subscriptions.push(
@@ -35,13 +35,9 @@ export function activate(context: vscode.ExtensionContext) {
3535
provideTextDocumentContent(uri: vscode.Uri): string {
3636
if (!lastContext) return "No Prompt generated yet.";
3737

38-
const { diff, projectMeta, recentCommits } = lastContext;
38+
const { diff, projectMeta } = lastContext;
3939
let content = FAST_PROMPT;
4040
content = content.replace("{{PROJECT_META}}", projectMeta || "(None)");
41-
const commitsStr = recentCommits.length > 0
42-
? recentCommits.map(c => `- ${c}`).join("\n")
43-
: "(None)";
44-
content = content.replace("{{RECENT_COMMITS}}", commitsStr);
4541

4642
return `=== SYSTEM PROMPT ===\n${content}\n\n=== USER CONTENT (DIFF) ===\n${diff}`;
4743
}
@@ -82,21 +78,21 @@ export function activate(context: vscode.ExtensionContext) {
8278
},
8379
async () => {
8480
try {
85-
// A. 获取 Git 历史 (Few-shot)
86-
const recentCommits = await gitService.getRecentCommits(5);
81+
// A. (Removed) 获取 Git 历史 (Few-shot)
82+
// const recentCommits = await gitService.getRecentCommits(5);
8783

8884
// B. 获取项目元数据
8985
const projectMeta = await getProjectMeta();
9086

9187
// 保存上下文供预览
92-
lastContext = { diff, projectMeta, recentCommits };
88+
lastContext = { diff, projectMeta };
9389

9490
// 调用快速 AI 服务
9591
Logger.info(
9692
`正在调用 AI 生成 (DeepSeek V3)... Diff 长度: ${diff.length}`
9793
);
9894

99-
await aiService.generate(diff, projectMeta, recentCommits, (options) => {
95+
await aiService.generate(diff, projectMeta, (options) => {
10096
Logger.info(`收到 AI 部分结果: ${options.length} 条建议`);
10197
provider.refresh(options);
10298
});
@@ -137,9 +133,8 @@ export function activate(context: vscode.ExtensionContext) {
137133
cachedDiff = await gitService.formatSmartDiff(analysis);
138134

139135
// [New] 预加载上下文,以便用户可以在“生成前”查看 Prompt
140-
const recentCommits = await gitService.getRecentCommits(5);
141136
const projectMeta = await getProjectMeta();
142-
lastContext = { diff: cachedDiff, projectMeta, recentCommits };
137+
lastContext = { diff: cachedDiff, projectMeta };
143138

144139
// 4. 更新 UI 展示文件列表
145140
provider.updatePreProcess(analysis);

src/features/aiCommit/prompts.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
export const FAST_PROMPT = `
2-
You are a developer assistant. Analyze git diff and generate 4 commit messages:
2+
You are a developer assistant. Analyze git diff and generate 3 commit messages:
33
1. "Emoji": Concise, starts with emoji (e.g. ✨), max 50 chars.
44
2. "StandardShort": Strictly one line, Standard Conventional format (feat(scope): subject), NO emoji.
55
3. "Conventional": Standard Conventional Commits format (feat(scope): subject). **Can be multi-line** if explanation is needed.
6-
4. "Historical": Mimic the style of the user's recent commits (provided below). If recent commits are messy, try to clean them up slightly but keep the "spirit" (e.g. language, brevity).
76
87
All messages must be in Simplified Chinese (简体中文).
98
109
Project Metadata:
1110
{{PROJECT_META}}
1211
13-
Recent Commits (Reference):
14-
{{RECENT_COMMITS}}
15-
1612
Note: The input may be a "Smart Diff" or "Summary Mode":
1713
- If you see "[DELETED] file", that file was deleted.
1814
- If you see "[BINARY/ASSET] file", it's a binary/asset change.
@@ -22,7 +18,6 @@ Return strictly a JSON Array:
2218
[
2319
{"type":"Emoji","description":"Emoji 风格 (简短)","message":"✨ ..."},
2420
{"type":"StandardShort","description":"极简标准","message":"feat(scope): ..."},
25-
{"type":"Conventional","description":"Conventional 规范 (标准)","message":"feat(scope): ...\\n\\nBody..."},
26-
{"type":"Historical","description":"仿历史风格 (习惯)","message":"..."}
21+
{"type":"Conventional","description":"Conventional 规范 (标准)","message":"feat(scope): ...\\n\\nBody..."}
2722
]
2823
`;

0 commit comments

Comments
 (0)