Skip to content

Commit ba7f003

Browse files
authored
feat(commit): enhance commit message generation with i18n support and improved processing (#530)
1 parent a99b75e commit ba7f003

File tree

7 files changed

+36
-138
lines changed

7 files changed

+36
-138
lines changed

src/api/providers/zgsm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ export class ZgsmAiHandler extends BaseProvider implements SingleCompletionHandl
538538
"system",
539539
),
540540
},
541-
timeout: 5000,
541+
timeout: 20000,
542542
}),
543543
)
544544
} catch (error) {

src/core/costrict/commit/__tests__/commitGenerator.test.ts

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -206,74 +206,6 @@ describe("CommitMessageGenerator", () => {
206206
})
207207
})
208208

209-
describe("getLocalizedPromptPrefix", () => {
210-
it("should return Chinese prompt for zh-CN", () => {
211-
const result = (generator as any).getLocalizedPromptPrefix("zh-CN")
212-
expect(result).toContain("根据以下 git 变更生成提交信息")
213-
})
214-
215-
it("should return Traditional Chinese prompt for zh-TW", () => {
216-
const result = (generator as any).getLocalizedPromptPrefix("zh-TW")
217-
expect(result).toContain("根據以下 git 變更生成提交訊息")
218-
})
219-
220-
it("should return English prompt for other languages", () => {
221-
const result = (generator as any).getLocalizedPromptPrefix("en")
222-
expect(result).toContain("Generate a commit message based on the following git changes")
223-
})
224-
})
225-
226-
describe("getLocalizedConventionalPrompt", () => {
227-
it("should return Chinese conventional prompt for zh-CN", () => {
228-
const result = (generator as any).getLocalizedConventionalPrompt("zh-CN")
229-
expect(result).toContain("约定式提交格式")
230-
})
231-
232-
it("should return Traditional Chinese conventional prompt for zh-TW", () => {
233-
const result = (generator as any).getLocalizedConventionalPrompt("zh-TW")
234-
expect(result).toContain("約定式提交格式")
235-
})
236-
237-
it("should return English conventional prompt for other languages", () => {
238-
const result = (generator as any).getLocalizedConventionalPrompt("en")
239-
expect(result).toContain("conventional commit format")
240-
})
241-
})
242-
243-
describe("getLocalizedSimplePrompt", () => {
244-
it("should return Chinese simple prompt for zh-CN", () => {
245-
const result = (generator as any).getLocalizedSimplePrompt("zh-CN")
246-
expect(result).toContain("简洁的提交信息")
247-
})
248-
249-
it("should return Traditional Chinese simple prompt for zh-TW", () => {
250-
const result = (generator as any).getLocalizedSimplePrompt("zh-TW")
251-
expect(result).toContain("簡潔的提交訊息")
252-
})
253-
254-
it("should return English simple prompt for other languages", () => {
255-
const result = (generator as any).getLocalizedSimplePrompt("en")
256-
expect(result).toContain("concise commit message")
257-
})
258-
})
259-
260-
describe("getLocalizedReturnPrompt", () => {
261-
it("should return Chinese return prompt for zh-CN", () => {
262-
const result = (generator as any).getLocalizedReturnPrompt("zh-CN")
263-
expect(result).toContain("只返回提交信息")
264-
})
265-
266-
it("should return Traditional Chinese return prompt for zh-TW", () => {
267-
const result = (generator as any).getLocalizedReturnPrompt("zh-TW")
268-
expect(result).toContain("只返回提交訊息")
269-
})
270-
271-
it("should return English return prompt for other languages", () => {
272-
const result = (generator as any).getLocalizedReturnPrompt("en")
273-
expect(result).toContain("Return only the commit message")
274-
})
275-
})
276-
277209
describe("shouldFilterFileContent", () => {
278210
it("should return true for image files", () => {
279211
const result = (generator as any).shouldFilterFileContent("image.png")

src/core/costrict/commit/commitGenerator.ts

Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import { ProviderSettings } from "@roo-code/types"
88
import type { ClineProvider } from "../../webview/ClineProvider"
99
import { t } from "../../../i18n"
1010
import { singleCompletionHandler } from "../../../utils/single-completion-handler"
11+
import { truncateOutput } from "../../../integrations/misc/extract-text"
1112

1213
const execAsync = promisify(exec)
1314

15+
const GIT_OUTPUT_CHAR_LIMIT = 30000
1416
/**
1517
* Commit message generator service
1618
*/
@@ -189,7 +191,7 @@ export class CommitMessageGenerator {
189191
// Prepare prompt for AI
190192
const systemPrompt =
191193
"You are an expert at generating concise, meaningful commit messages based on git diff information. Follow conventional commit format when appropriate."
192-
const aiMessage = await singleCompletionHandler(
194+
let aiMessage = await singleCompletionHandler(
193195
apiConfiguration!,
194196
this.buildAIPrompt(diffInfo, options),
195197
systemPrompt,
@@ -199,6 +201,12 @@ export class CommitMessageGenerator {
199201
modelId: options.commitModelId,
200202
},
201203
)
204+
205+
if (aiMessage.includes("</think>")) {
206+
// Remove the <think> tag
207+
aiMessage = (aiMessage.split("</think>")[1] || "").trim()
208+
}
209+
202210
if (!aiMessage) {
203211
throw new Error(t("commit:commit.error.aiFailed"))
204212
}
@@ -356,11 +364,10 @@ export class CommitMessageGenerator {
356364
* Build prompt for AI commit generation
357365
*/
358366
private buildAIPrompt(diffInfo: GitDiffInfo, options: CommitGenerationOptions): string {
359-
const { useConventionalCommits = true, language } = options
367+
const { useConventionalCommits = true } = options
360368
const lang = this.getCommitLanguage(options)
361-
362369
// Build language-specific prompt
363-
let prompt = this.getLocalizedPromptPrefix(lang)
370+
let prompt = t("commit:commit.prompt.prefix", { lng: lang })
364371

365372
if (diffInfo.added.length > 0) {
366373
prompt += `Added files:\n${diffInfo.added.map((f) => `- ${f}`).join("\n")}\n\n`
@@ -380,15 +387,16 @@ export class CommitMessageGenerator {
380387

381388
// Filter diff content to exclude content from files that should only show filenames
382389
const filteredDiffContent = this.filterDiffContent(diffInfo.diffContent, diffInfo)
383-
prompt += `Diff content:\n${filteredDiffContent}\n\n`
390+
391+
prompt += `Diff content:\n${truncateOutput(filteredDiffContent, undefined, GIT_OUTPUT_CHAR_LIMIT)}\n\n`
384392

385393
if (useConventionalCommits) {
386-
prompt += this.getLocalizedConventionalPrompt(lang)
394+
prompt += t("commit:commit.prompt.conventional", { lng: lang })
387395
} else {
388-
prompt += this.getLocalizedSimplePrompt(lang)
396+
prompt += t("commit:commit.prompt.simple", { lng: lang })
389397
}
390398

391-
prompt += this.getLocalizedReturnPrompt(lang)
399+
prompt += t("commit:commit.prompt.return", { lng: lang })
392400

393401
return prompt
394402
}
@@ -759,10 +767,6 @@ export class CommitMessageGenerator {
759767
private generateBody(diffInfo: GitDiffInfo): string {
760768
const lines: string[] = []
761769

762-
// const summary = `Added: ${diffInfo.added.length}, Modified: ${diffInfo.modified.length}, Deleted: ${diffInfo.deleted.length}, Renamed: ${diffInfo.renamed.length}`
763-
// lines.push(summary)
764-
// if (lines.length > 0) lines.push("")
765-
766770
if (diffInfo.added.length > 0) {
767771
lines.push(t("commit:commit.files.added"))
768772
diffInfo.added.forEach((file) => lines.push(`- ${file}`))
@@ -809,60 +813,4 @@ export class CommitMessageGenerator {
809813
// Fallback to VSCode environment language
810814
return vscode.env.language || "en"
811815
}
812-
813-
/**
814-
* Get localized prompt prefix based on language
815-
*/
816-
private getLocalizedPromptPrefix(lang: string): string {
817-
switch (lang) {
818-
case "zh-CN":
819-
return `根据以下 git 变更生成提交信息:\n\n`
820-
case "zh-TW":
821-
return `根據以下 git 變更生成提交訊息:\n\n`
822-
default:
823-
return `Generate a commit message based on the following git changes:\n\n`
824-
}
825-
}
826-
827-
/**
828-
* Get localized conventional commit prompt
829-
*/
830-
private getLocalizedConventionalPrompt(lang: string): string {
831-
switch (lang) {
832-
case "zh-CN":
833-
return `请生成遵循约定式提交格式的提交信息 (type(scope): description)。`
834-
case "zh-TW":
835-
return `請生成遵循約定式提交格式的提交訊息 (type(scope): description)。`
836-
default:
837-
return `Please generate a commit message following conventional commit format (type(scope): description).`
838-
}
839-
}
840-
841-
/**
842-
* Get localized simple commit prompt
843-
*/
844-
private getLocalizedSimplePrompt(lang: string): string {
845-
switch (lang) {
846-
case "zh-CN":
847-
return `请生成简洁的提交信息。`
848-
case "zh-TW":
849-
return `請生成簡潔的提交訊息。`
850-
default:
851-
return `Please generate a concise commit message.`
852-
}
853-
}
854-
855-
/**
856-
* Get localized return instruction
857-
*/
858-
private getLocalizedReturnPrompt(lang: string): string {
859-
switch (lang) {
860-
case "zh-CN":
861-
return ` 只返回提交信息,不要解释。`
862-
case "zh-TW":
863-
return ` 只返回提交訊息,不要解釋。`
864-
default:
865-
return ` Return only the commit message, no explanations.`
866-
}
867-
}
868816
}

src/core/costrict/commit/commitService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class CommitService {
4949
const config = vscode.workspace.getConfiguration("zgsm.commit")
5050
const useConventionalCommits = config.get<boolean>("useConventionalCommits", true)
5151
const commitModelId = config.get<string>("commitModelId", "")
52-
const maxLength = config.get<number>("maxLength", 72)
52+
const maxLength = config.get<number>("maxLength", 150)
5353
const language = config.get<string>("language", "auto")
5454

5555
// Generate commit message

src/i18n/costrict-i18n/locales/en/commit.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
"modified": "Modified files:",
3535
"deleted": "Deleted files:",
3636
"renamed": "Renamed files:"
37+
},
38+
"prompt": {
39+
"prefix": "Generate a commit message based on the following git changes:\n\n",
40+
"conventional": "Please generate a commit message following conventional commit format (type(scope): description).",
41+
"simple": "Please generate a concise commit message.",
42+
"return": " Return only the commit message, no explanations."
3743
}
3844
}
3945
}

src/i18n/costrict-i18n/locales/zh-CN/commit.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/i18n/costrict-i18n/locales/zh-TW/commit.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)