💅 style(devtools/commitgen/generator.py): update system and user prompts
Updates the system prompt to provide more detailed instructions on writing conventional commit messages. The user prompt is also updated to request code changes for generating the commit message. This change: - Improves the user experience by providing clearer instructions - Encourages the use of the correct emoji prefix for each commit type - Ensures that the generated commit messages match the conventional format
This commit is contained in:
1 file changed
+80
-101
+80
-101
@@ -18,123 +18,101 @@ class CommitGenerator(AIService):
|
|||||||
self, diff: str, temperature: Optional[float] = None
|
self, diff: str, temperature: Optional[float] = None
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Generate a commit message from a diff."""
|
"""Generate a commit message from a diff."""
|
||||||
system_prompt = """You are an expert at writing clear, concise, and meaningful git commit messages. Follow these rules strictly:
|
system_prompt = """You are an expert Git assistant trained to write clean, conventional commit messages with optional bodies.
|
||||||
|
|
||||||
2. Format: type(scope): description
|
Follow this format:
|
||||||
- type: One of feat, fix, docs, style, refactor, test, chore
|
|
||||||
- scope: Optional, describes the section of the codebase affected
|
|
||||||
- description: Clear, concise explanation of the change
|
|
||||||
|
|
||||||
3. Types and their emojis:
|
<type>(<scope>): <short summary>
|
||||||
- feat: ✨ (new feature or enhancement)
|
[blank line]
|
||||||
- fix: 🐛 (bug fix)
|
<detailed body explaining the change>
|
||||||
- docs: 📚 (documentation changes)
|
|
||||||
- style: 💅 (formatting, missing semi colons, etc; no code change)
|
|
||||||
- refactor: ♻️ (code restructuring, no functional changes)
|
|
||||||
- test: ✅ (adding or modifying tests)
|
|
||||||
- chore: 🔧 (maintenance tasks, dependencies, etc)
|
|
||||||
|
|
||||||
4. Guidelines:
|
Where:
|
||||||
- Write the ENTIRE message on a single line
|
- type: One of feat, fix, docs, style, refactor, test, chore
|
||||||
- Keep the message concise and to the point
|
- scope: Optional but recommended (e.g. auth, api, db)
|
||||||
- Use imperative mood ("add" not "added")
|
- summary: A short, one-line description of the change
|
||||||
- Don't end with a period
|
- body: (Optional) Add context, rationale, and details about the change
|
||||||
- Focus on the "why" not the "what"
|
|
||||||
- Be specific about the change
|
|
||||||
- Use present tense
|
|
||||||
- Start with a verb
|
|
||||||
- Don't include pull request references in the main message
|
|
||||||
- Keep the message clean and focused on the change itself
|
|
||||||
- When analyzing multiple files, focus on the overall change and its impact
|
|
||||||
- DO NOT include any explanatory text or "Based on the changes" phrases
|
|
||||||
- DO NOT include file paths in the message
|
|
||||||
- DO NOT include any text about what can be written
|
|
||||||
|
|
||||||
5. Examples:
|
Each type must have an emoji:
|
||||||
- ✨ feat(auth): add OAuth3 authentication
|
- ✨ feat: New feature
|
||||||
- 🐛 fix(api): handle null response from server
|
- 🐛 fix: Bug fix
|
||||||
- 📚 docs(readme): update installation instructions
|
- 📚 docs: Documentation
|
||||||
- 💅 style(ui): format button component
|
- 💅 style: Formatting (no code logic)
|
||||||
- ♻️ refactor(db): optimize database queries
|
- ♻️ refactor: Code restructure without behavior change
|
||||||
- ✅ test(api): add unit tests for user endpoints
|
- ✅ test: Tests added or modified
|
||||||
- 🔧 chore(deps): update dependencies
|
- 🔧 chore: Maintenance, configs, build scripts, etc.
|
||||||
|
|
||||||
Write ONLY the commit message in the conventional format. Do not include any explanatory text, file paths, or other content."""
|
Rules:
|
||||||
|
- Subject must be a single line, max ~72 characters
|
||||||
|
- Use imperative mood: “add”, not “added”
|
||||||
|
- Don’t include file paths, issue numbers, or PR references
|
||||||
|
- Use body only if extra context is helpful (start with a verb or full sentence)
|
||||||
|
- Don’t include explanations about what can be written
|
||||||
|
- DO NOT include “commit message” or meta-language
|
||||||
|
|
||||||
user_prompt = f"""Write a conventional commit message for these changes:
|
Examples:
|
||||||
|
✨ feat(auth): add OAuth2 login support
|
||||||
|
|
||||||
|
Adds full OAuth2 login flow using access and refresh tokens.
|
||||||
|
This improves external authentication and reduces local user management.
|
||||||
|
"""
|
||||||
|
|
||||||
|
user_prompt = f"""Generate a conventional commit message for the following code changes:
|
||||||
|
|
||||||
{diff}
|
{diff}
|
||||||
|
|
||||||
Focus on:
|
- Include a single-line subject
|
||||||
2. What type of change this is (feat, fix, etc.)
|
- Add a body section if useful, separated by a blank line
|
||||||
3. What part of the codebase is affected (scope)
|
- Output must match the conventional commit format
|
||||||
4. What the change accomplishes (description)
|
- Use the correct emoji prefix
|
||||||
5. If multiple files are changed, identify the common theme or purpose
|
"""
|
||||||
|
|
||||||
Write ONLY the commit message in the conventional format. Do not include any explanatory text."""
|
|
||||||
|
|
||||||
message = self.generate_completion(
|
message = self.generate_completion(
|
||||||
system_prompt, user_prompt, temperature=temperature
|
system_prompt, user_prompt, temperature=temperature
|
||||||
)
|
)
|
||||||
|
|
||||||
# Clean up the response to ensure it's a valid commit message
|
# Clean and extract subject and optional body
|
||||||
lines = [line.strip() for line in message.split("\n") if line.strip()]
|
lines = [line.rstrip() for line in message.splitlines()]
|
||||||
|
subject = ""
|
||||||
|
body_lines = []
|
||||||
|
|
||||||
# Find the first line that looks like a commit message
|
for i, line in enumerate(lines):
|
||||||
for line in lines:
|
if not subject and line and ":" in line:
|
||||||
# Skip lines that are explanations or contain file paths
|
subject = line.strip()
|
||||||
if any(
|
|
||||||
skip in line.lower()
|
|
||||||
for skip in [
|
|
||||||
"based on",
|
|
||||||
"changes in",
|
|
||||||
"can be written",
|
|
||||||
"commit message",
|
|
||||||
"following",
|
|
||||||
"these changes",
|
|
||||||
"the changes",
|
|
||||||
]
|
|
||||||
):
|
|
||||||
continue
|
continue
|
||||||
|
if subject:
|
||||||
|
body_lines = lines[i:]
|
||||||
|
break
|
||||||
|
|
||||||
# Skip lines that are just file paths
|
if not subject:
|
||||||
if line.startswith("`") or line.startswith("/") or ":" in line:
|
subject = "🔧 chore: update code"
|
||||||
continue
|
|
||||||
|
|
||||||
message = line
|
# Ensure emoji in the subject
|
||||||
break
|
|
||||||
else:
|
|
||||||
# If no valid message found, use the first non-empty line
|
|
||||||
message = lines[0] if lines else "chore: update code"
|
|
||||||
|
|
||||||
# Remove any pull request references from the main message
|
|
||||||
message = (
|
|
||||||
message.split("(")[1].strip()
|
|
||||||
if "(" in message and "#" in message
|
|
||||||
else message
|
|
||||||
)
|
|
||||||
|
|
||||||
# Ensure the message starts with an emoji
|
|
||||||
if not any(
|
if not any(
|
||||||
emoji in message for emoji in ["✨", "🐛", "📚", "💅", "♻️", "✅", "🔧"]
|
emoji in subject for emoji in ["✨", "🐛", "📚", "💅", "♻️", "✅", "🔧"]
|
||||||
):
|
):
|
||||||
# Add appropriate emoji based on the commit type
|
if subject.startswith("feat"):
|
||||||
if message.startswith("feat"):
|
subject = "✨ " + subject
|
||||||
message = "✨ " + message
|
elif subject.startswith("fix"):
|
||||||
elif message.startswith("fix"):
|
subject = "🐛 " + subject
|
||||||
message = "🐛 " + message
|
elif subject.startswith("docs"):
|
||||||
elif message.startswith("docs"):
|
subject = "📚 " + subject
|
||||||
message = "📚 " + message
|
elif subject.startswith("style"):
|
||||||
elif message.startswith("style"):
|
subject = "💅 " + subject
|
||||||
message = "💅 " + message
|
elif subject.startswith("refactor"):
|
||||||
elif message.startswith("refactor"):
|
subject = "♻️ " + subject
|
||||||
message = "♻️ " + message
|
elif subject.startswith("test"):
|
||||||
elif message.startswith("test"):
|
subject = "✅ " + subject
|
||||||
message = "✅ " + message
|
elif subject.startswith("chore"):
|
||||||
elif message.startswith("chore"):
|
subject = "🔧 " + subject
|
||||||
message = "🔧 " + message
|
|
||||||
else:
|
else:
|
||||||
message = "🔧 " + message # Default to chore
|
subject = "🔧 " + subject
|
||||||
|
|
||||||
|
# Final formatted message
|
||||||
|
message = subject
|
||||||
|
if body_lines:
|
||||||
|
body = "\n".join(line.strip() for line in body_lines if line.strip())
|
||||||
|
if body:
|
||||||
|
message += "\n\n" + body
|
||||||
|
|
||||||
return message
|
return message
|
||||||
|
|
||||||
@@ -171,11 +149,12 @@ Write ONLY the commit message in the conventional format. Do not include any exp
|
|||||||
) -> str:
|
) -> str:
|
||||||
"""Generate a changelog from a list of commits."""
|
"""Generate a changelog from a list of commits."""
|
||||||
system_prompt = """You are a helpful AI that generates changelogs.
|
system_prompt = """You are a helpful AI that generates changelogs.
|
||||||
Follow these rules:
|
Follow these rules:
|
||||||
2. Group changes by type (Added, Changed, Fixed, etc.)
|
- Group changes by type (Added, Changed, Fixed, etc.)
|
||||||
3. Keep descriptions concise but informative
|
- Keep descriptions concise but informative
|
||||||
4. Use past tense
|
- Use past tense
|
||||||
5. Start each entry with a verb"""
|
- Start each entry with a verb
|
||||||
|
"""
|
||||||
|
|
||||||
user_prompt = (
|
user_prompt = (
|
||||||
f"Generate a changelog for version {version} with these commits:\n\n"
|
f"Generate a changelog for version {version} with these commits:\n\n"
|
||||||
|
|||||||
Reference in new issue
Block a user