AI Powered Commit Message Normalization Tool
Project description
CMAI - AI-Powered Commit Message Normalizer
CMAI is an intelligent command-line tool that leverages AI to transform informal or colloquial commit messages into standardized, professional Git commit messages. It analyzes your staged changes and uses advanced language models to generate clear, concise, and conventional commit messages.
๐ Features
- AI-Powered Normalization: Converts informal commit descriptions into professional, standardized commit messages
- Git Integration: Automatically analyzes staged changes to provide context-aware suggestions
- Multiple AI Provider Support: Currently supports Bailian (Qwen) models with extensible architecture for additional providers
- Configurable: Customizable prompt templates and model settings
- Token Usage Tracking: Monitor AI token consumption for cost management
- Comprehensive Logging: Detailed logging for debugging and monitoring
๐ Quick Start
Installation
Install CMAI using pip:
pip install cmai
Or install from source:
git clone https://github.com/yumuzhihan/cmai.git
cd cmai
pip install -e .
Basic Usage
- Stage your changes in Git:
git add .
- Use CMAI to generate a normalized commit message:
cmai "fix some bugs in user authentication"
- The tool will output a standardized commit message like:
Commit message: Fix authentication bugs in user login module
Tokens used: 45
๐ง Configuration
CMAI uses a configuration file located at ~/.config/cmai/settings.env. The configuration file will be automatically created on first run.
Environment Variables
Create or edit ~/.config/cmai/settings.env:
# AI Provider Configuration
PROVIDER=openai
API_BASE=https://dashscope.aliyuncs.com/compatible-mode/v1
API_KEY=your_api_key_here
MODEL=qwen-turbo-latest
# Logging Configuration
LOG_LEVEL=INFO
LOG_FILE_PATH=/path/to/logfile.log
# Prompt Template (optional customization)
PROMPT_TEMPLATE=Please generate a standardized commit message based on the user description: {user_input}. The changes include: {diff_content}. Respond only with the normalized commit message in English.
API Key Setup
For Bailian (Qwen) models, you can set your API key in several ways:
- Environment variable (recommended):
export DASHSCOPE_API_KEY=your_api_key_here
-
Configuration file: Add
API_KEY=your_api_key_hereto~/.config/cmai/settings.env -
Custom config file: Use the
--configoption to specify a different configuration file
๐ Usage Examples
Command Examples
# Simple commit message normalization
cmai "updated readme file"
# Output: Update README documentation
Using Custom Configuration
# Use a specific configuration file
cmai "refactored auth system" --config /path/to/custom/config.env
Specifying Repository Path
# Analyze changes in a specific repository
cmai "fixed login bug" --repo /path/to/your/repo
Full Command Options
cmai [OPTIONS] MESSAGE
Arguments:
MESSAGE The informal commit message to be normalized [required]
Options:
-c, --config TEXT Path to configuration file
-r, --repo TEXT Git repository path
--help Show this message and exit
๐๏ธ Architecture
CMAI follows a modular architecture with the following components:
Core Components
cmai.main: Entry point and CLI interface using Clickcmai.core.normalizer: Core logic for commit message normalizationcmai.core.get_logger: Logging factory and configurationcmai.config.settings: Configuration management using Pydantic
Provider System
cmai.providers.base: Abstract base class for AI providerscmai.providers.bailian_provider: Bailian (Qwen) AI implementation- Extensible design allows for easy addition of new AI providers
Utilities
cmai.utils.git_staged_analyzer: Git repository analysis and diff extraction
Data Models
class AIResponse(BaseModel):
content: str # The normalized commit message
model: str # AI model used
provider: str # AI provider name
tokens_used: Optional[int] # Token consumption
๐ Extending CMAI
Adding New AI Providers
To add support for a new AI provider, create a class that inherits from BaseAIClient:
from cmai.providers.base import BaseAIClient, AIResponse
class CustomProvider(BaseAIClient):
async def normalize_commit(self, prompt: str, **kwargs) -> AIResponse:
# Implement your provider logic here
pass
def validate_config(self) -> bool:
# Implement configuration validation
pass
Custom Prompt Templates
You can customize the prompt template by modifying the PROMPT_TEMPLATE setting:
PROMPT_TEMPLATE=Custom prompt: {user_input}. Context: {diff_content}. Generate a commit message.
Available placeholders:
{user_input}: The user's informal commit message{diff_content}: Git diff information from staged changes
๐ ๏ธ Development
Setting Up Development Environment
- Clone the repository:
git clone https://github.com/yumuzhihan/cmai.git
cd cmai
- Create a virtual environment:
uv venv
# Activate virtual environment
# On Unix/macOS:
source .venv/bin/activate
# On Windows:
# .venv\Scripts\activate
- Install development dependencies:
# Install all dependencies including dev dependencies
uv sync --dev
# Or if you prefer to install only production dependencies:
uv sync
Project Structure
cmai/
โโโ cmai/
โ โโโ __init__.py
โ โโโ main.py # CLI entry point
โ โโโ config/
โ โ โโโ __init__.py
โ โ โโโ settings.py # Configuration management
โ โโโ core/
โ โ โโโ __init__.py
โ โ โโโ get_logger.py # Logging utilities
โ โ โโโ normalizer.py # Core normalization logic
โ โโโ providers/
โ โ โโโ __init__.py
โ โ โโโ base.py # Abstract provider interface
โ โ โโโ bailian_provider.py # Bailian implementation
โ โโโ utils/
โ โโโ __init__.py
โ โโโ git_staged_analyzer.py # Git utilities
โโโ tests/ # Test suite
โโโ docs/ # Documentation
โโโ scripts/ # Utility scripts
โโโ pyproject.toml # Project configuration
โโโ LICENSE # AGPL-3.0 License
โโโ README.md # This file
Running Tests
python -m pytest tests/
๐ Requirements
- Python 3.12 or higher
- Git (for repository analysis)
- Internet connection (for AI provider APIs)
Dependencies
click>=8.2.1- Command-line interfaceopenai>=1.91.0- OpenAI-compatible API clientpydantic>=2.11.7- Data validation and settingspydantic-settings>=2.10.0- Settings management
๐ค Contributing
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Contribution Guidelines
- Follow PEP 8 coding standards
- Add type hints to all functions
- Write comprehensive tests
- Update documentation for new features
- Ensure backward compatibility
๐ License
This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). See the LICENSE file for details.
๐ Acknowledgments
- Built with Click for the CLI interface
- Uses Pydantic for configuration and data validation
- Integrates with Bailian API for AI functionality
- Inspired by conventional commit standards
๐ Support
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information
- Review the documentation and configuration guide
Note: This tool requires API access to AI language models. Please ensure you have appropriate API keys and understand the associated costs before using CMAI in production environments.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cmai-0.1.1.tar.gz.
File metadata
- Download URL: cmai-0.1.1.tar.gz
- Upload date:
- Size: 25.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d896aafa569616f1acc85b302740823cbb638785bab47eccaa9311b7acca3534
|
|
| MD5 |
0becd447d7bdbf54b5e1330f6ab417d6
|
|
| BLAKE2b-256 |
543b9be26b77822d8cf62d7103f226c3aa70e2dbbc96c4692dfa02cfbf88ebce
|
File details
Details for the file cmai-0.1.1-py3-none-any.whl.
File metadata
- Download URL: cmai-0.1.1-py3-none-any.whl
- Upload date:
- Size: 24.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0fea0c6ae6130a3ae0f6a9f73bfc96c81d8959c2bc4f0aac3db10ce3bdfe4c9
|
|
| MD5 |
63a48baf2eb73484f2e14393a15db276
|
|
| BLAKE2b-256 |
8a960517a8e14ba0b8925719d2e9b951b58fc1c13959d7b14f68a968f64f8bf0
|