💅 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
+77
-98
@@ -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>(<scope>): <short summary>
|
||||||
|
[blank line]
|
||||||
|
<detailed body explaining the change>
|
||||||
|
|
||||||
|
Where:
|
||||||
- type: One of feat, fix, docs, style, refactor, test, chore
|
- type: One of feat, fix, docs, style, refactor, test, chore
|
||||||
- scope: Optional, describes the section of the codebase affected
|
- scope: Optional but recommended (e.g. auth, api, db)
|
||||||
- description: Clear, concise explanation of the change
|
- summary: A short, one-line description of the change
|
||||||
|
- body: (Optional) Add context, rationale, and details about the change
|
||||||
|
|
||||||
3. Types and their emojis:
|
Each type must have an emoji:
|
||||||
- feat: ✨ (new feature or enhancement)
|
- ✨ feat: New feature
|
||||||
- fix: 🐛 (bug fix)
|
- 🐛 fix: Bug fix
|
||||||
- docs: 📚 (documentation changes)
|
- 📚 docs: Documentation
|
||||||
- style: 💅 (formatting, missing semi colons, etc; no code change)
|
- 💅 style: Formatting (no code logic)
|
||||||
- refactor: ♻️ (code restructuring, no functional changes)
|
- ♻️ refactor: Code restructure without behavior change
|
||||||
- test: ✅ (adding or modifying tests)
|
- ✅ test: Tests added or modified
|
||||||
- chore: 🔧 (maintenance tasks, dependencies, etc)
|
- 🔧 chore: Maintenance, configs, build scripts, etc.
|
||||||
|
|
||||||
4. Guidelines:
|
Rules:
|
||||||
- Write the ENTIRE message on a single line
|
- Subject must be a single line, max ~72 characters
|
||||||
- Keep the message concise and to the point
|
- Use imperative mood: “add”, not “added”
|
||||||
- Use imperative mood ("add" not "added")
|
- Don’t include file paths, issue numbers, or PR references
|
||||||
- Don't end with a period
|
- Use body only if extra context is helpful (start with a verb or full sentence)
|
||||||
- Focus on the "why" not the "what"
|
- Don’t include explanations about what can be written
|
||||||
- Be specific about the change
|
- DO NOT include “commit message” or meta-language
|
||||||
- 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:
|
Examples:
|
||||||
- ✨ feat(auth): add OAuth3 authentication
|
✨ feat(auth): add OAuth2 login support
|
||||||
- 🐛 fix(api): handle null response from server
|
|
||||||
- 📚 docs(readme): update installation instructions
|
|
||||||
- 💅 style(ui): format button component
|
|
||||||
- ♻️ refactor(db): optimize database queries
|
|
||||||
- ✅ test(api): add unit tests for user endpoints
|
|
||||||
- 🔧 chore(deps): update dependencies
|
|
||||||
|
|
||||||
Write ONLY the commit message in the conventional format. Do not include any explanatory text, file paths, or other content."""
|
Adds full OAuth2 login flow using access and refresh tokens.
|
||||||
|
This improves external authentication and reduces local user management.
|
||||||
|
"""
|
||||||
|
|
||||||
user_prompt = f"""Write a conventional commit message for these changes:
|
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:
|
||||||
# Skip lines that are just file paths
|
body_lines = lines[i:]
|
||||||
if line.startswith("`") or line.startswith("/") or ":" in line:
|
|
||||||
continue
|
|
||||||
|
|
||||||
message = line
|
|
||||||
break
|
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
|
if not subject:
|
||||||
message = (
|
subject = "🔧 chore: update code"
|
||||||
message.split("(")[1].strip()
|
|
||||||
if "(" in message and "#" in message
|
|
||||||
else message
|
|
||||||
)
|
|
||||||
|
|
||||||
# Ensure the message starts with an emoji
|
# Ensure emoji in the subject
|
||||||
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
|
||||||
|
|
||||||
@@ -172,10 +150,11 @@ Write ONLY the commit message in the conventional format. Do not include any exp
|
|||||||
"""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