Skip to content

Commit fe29fc0

Browse files
authored
Merge pull request #10 from wangkang/main
Enable users to create their own prompts
2 parents 48efab1 + 411544e commit fe29fc0

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

prompt.go

+25-23
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ func findGitRoot(dir string) (string, error) {
8585
}
8686

8787
const styleGuideFilename = "COMMITS.md"
88+
const defaultUserStyleGuide = `
89+
1. Limit the subject line to 50 characters.
90+
2. Use the imperative mood in the subject line.
91+
3. Capitalize the subject line such as "Fix Issue 886" and don't end it with a period.
92+
4. The subject line should summarize the main change concisely.
93+
5. Only include a body if absolutely necessary for complex changes.
94+
6. If a body is needed, separate it from the subject with a blank line.
95+
7. Wrap the body at 72 characters.
96+
8. In the body, explain the why, not the what (the diff shows the what).
97+
9. Use bullet points in the body only for truly distinct changes.
98+
10. Be extremely concise. Assume the reader can understand the diff.
99+
11. Never repeat information between the subject and body.
100+
12. Do not repeat commit messages from previous commits.
101+
13. Prioritize clarity and brevity over completeness.
102+
14. Adhere to the repository's commit style if it exists.
103+
`
88104

89105
// findRepoStyleGuide searches for "COMMITS.md" in the repository root of dir
90106
// and returns its contents.
@@ -101,7 +117,7 @@ func findRepoStyleGuide(dir string) (string, error) {
101117
}
102118
return "", fmt.Errorf("read style guide: %w", err)
103119
}
104-
return string(styleGuide), nil
120+
return strings.TrimSpace(string(styleGuide)), nil
105121
}
106122

107123
func findUserStyleGuide() (string, error) {
@@ -116,8 +132,7 @@ func findUserStyleGuide() (string, error) {
116132
}
117133
return "", fmt.Errorf("read user style guide: %w", err)
118134
}
119-
120-
return string(styleGuide), nil
135+
return strings.TrimSpace(string(styleGuide)), nil
121136
}
122137

123138
func BuildPrompt(
@@ -132,21 +147,7 @@ func BuildPrompt(
132147
Role: openai.ChatMessageRoleSystem,
133148
Content: strings.Join([]string{
134149
"You are a tool called `aicommit` that generates high quality commit messages for git diffs.",
135-
"Generate only the commit message, without any additional text. Follow these guidelines:",
136-
"1. Limit the subject line to 50 characters.",
137-
"2. Use the imperative mood in the subject line.",
138-
"3. Capitalize the subject line and don't end it with a period.",
139-
"4. The subject line should summarize the main change concisely.",
140-
"5. Only include a body if absolutely necessary for complex changes.",
141-
"6. If a body is needed, separate it from the subject with a blank line.",
142-
"7. Wrap the body at 72 characters.",
143-
"8. In the body, explain the why, not the what (the diff shows the what).",
144-
"9. Use bullet points in the body only for truly distinct changes.",
145-
"10. Be extremely concise. Assume the reader can understand the diff.",
146-
"11. Never repeat information between the subject and body.",
147-
"12. Do not repeat commit messages from previous commits.",
148-
"13. Prioritize clarity and brevity over completeness.",
149-
"14. Adhere to the repository's commit style if it exists.",
150+
"Generate only the commit message, without any additional text.",
150151
}, "\n"),
151152
},
152153
}
@@ -258,12 +259,13 @@ func BuildPrompt(
258259
if err != nil {
259260
return nil, fmt.Errorf("find user style guide: %w", err)
260261
}
261-
if userStyleGuide != "" {
262-
resp = append(resp, openai.ChatCompletionMessage{
263-
Role: openai.ChatMessageRoleSystem,
264-
Content: "This user has a preferred style guide:\n" + userStyleGuide,
265-
})
262+
if userStyleGuide == "" {
263+
userStyleGuide = defaultUserStyleGuide
266264
}
265+
resp = append(resp, openai.ChatCompletionMessage{
266+
Role: openai.ChatMessageRoleSystem,
267+
Content: "This user has a preferred style guide:\n" + userStyleGuide,
268+
})
267269
}
268270

269271
resp = append(resp, openai.ChatCompletionMessage{

0 commit comments

Comments
 (0)