An intelligent codebase analysis and refactoring system that helps improve code quality, maintainability, and generates insights about your projects using Google's Gemini AI.
- π Codebase Analysis: Analyzes code structure, complexity, and quality metrics
- π€ AI-Powered Insights: Uses Google Gemini AI for intelligent code analysis and suggestions
- β¨ Code Refactoring: Automated code improvements while preserving functionality
- π Multi-language Support: Python, JavaScript, TypeScript, Java, C/C++, Go, Rust, and more
- πΊοΈ Repository Mapping: Complete file tree analysis with metadata
- ποΈ Interview Questions: Generates technical interview questions based on code analysis
- π Comprehensive Reports: Detailed recommendations for codebase improvements
- β‘ Rate Limit Management: Built-in delays and retry logic for API stability
- Python (
.py) - JavaScript/TypeScript (
.js,.jsx,.ts,.tsx) - Java (
.java) - C/C++ (
.c,.cpp,.h,.hpp) - C# (
.cs) - Go (
.go) - Rust (
.rs) - PHP (
.php) - Ruby (
.rb) - Swift (
.swift) - Kotlin (
.kt) - Scala (
.scala) - R (
.r) - HTML/CSS (
.html,.css) - Shell scripts (
.sh) - SQL (
.sql) - And many more...
-
Clone the repository:
git clone <repository-url> cd code-refactoring-agent
-
Run the setup script:
python setup.py
-
Get your Gemini API key:
- Visit Google AI Studio
- Create a new API key
- Add it to your
.envfile:GEMINI_API_KEY=your_api_key_here
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
cp .env.example .env # Edit .env and add your GEMINI_API_KEY
Analyze a local codebase:
python main.py /path/to/your/projectAnalyze a GitHub repository:
python main.py https://github.com/username/repository.gitSpecify custom output directory:
python main.py /path/to/project --output-dir my_refactored_codeChoose between Gemini models based on your needs:
Test your API key and find working models:
python test_gemini_models.pyFast processing (recommended for most cases):
python main.py /path/to/project --model gemini-1.5-flash-latestHigher quality analysis and refactoring:
python main.py /path/to/project --model gemini-1.5-pro-latestAdjust delay between API calls:
python main.py /path/to/project --delay 5 # 5 seconds between callsProcess only specific phases:
python main.py /path/to/project --skip-refactoring # Analysis only
python main.py /path/to/project --skip-analysis # Refactoring onlyGet help:
python main.py --helpComplete example with all options:
python main.py https://github.com/user/repo.git \
--output-dir custom_output \
--model gemini-1.5-pro-latest \
--delay 3The tool generates several outputs in your specified directory:
refactored_codebase/
βββ π src/ # Refactored source code
β βββ π main.py # Improved Python files
β βββ π¨ utils.js # Refactored JavaScript
βββ π CODEBASE_RECOMMENDATIONS.md # Overall improvement suggestions
βββ π INTERVIEW_QUESTIONS.md # Generated interview questions
-
Analysis Phase:
- Scans your codebase for supported file types
- Analyzes each file for complexity, maintainability, and code smells
- Generates detailed quality reports using Gemini AI
-
Refactoring Phase:
- Applies AI-powered improvements to your code
- Maintains original functionality while improving readability
- Adds comments and documentation where appropriate
-
Insights Generation:
- Creates overall codebase recommendations
- Generates technical interview questions
- Provides actionable improvement suggestions
You can customize the behavior using environment variables in your .env file:
# Required
GEMINI_API_KEY=your_api_key_here
# Optional model configuration
GEMINI_MODEL=gemini-1.5-flash-latest # or gemini-1.5-pro-latest
GEMINI_TEMPERATURE=0.2 # Creativity level (0.0-1.0)
GEMINI_MAX_RETRIES=3 # API retry attempts
GEMINI_REQUEST_TIMEOUT=30 # Request timeout in seconds
# File processing limits
MAX_FILE_SIZE_FOR_REFACTORING=15000 # Max characters for refactoring
MAX_FILE_SIZE_FOR_ANALYSIS=12000 # Max characters for analysis| Model | Speed | Quality | Use Case |
|---|---|---|---|
gemini-1.5-flash-latest |
β‘ Fast | Good | General analysis, large codebases |
gemini-1.5-pro-latest |
π Slower | Excellent | Complex refactoring, detailed analysis |
gemini-pro |
β‘ Fast | Good | Legacy model, stable |
The tool automatically ignores common directories and files:
- Version control:
.git,.svn - Dependencies:
node_modules,venv,__pycache__ - Build outputs:
dist,build,target - IDE files:
.idea,.vscode
"GEMINI_API_KEY not found":
- Ensure your
.envfile exists and contains your API key - Get your API key from Google AI Studio
"Rate limit exceeded" or "Quota exceeded":
- Increase the
--delayparameter (try--delay 5or higher) - Use
--skip-analysisor--skip-refactoringto reduce API calls - Check your Gemini API quota in Google AI Studio
Model not found (404 error):
- Run
python test_gemini_models.pyto check available models - Use
gemini-1.5-flash-latestorgemini-proinstead - Your API key might not have access to newer models "Git command not found":
- Install Git: https://git-scm.com/downloads
- Ensure Git is in your system PATH
Large files being skipped:
- Files over 15KB are copied without refactoring
- Files over 12KB are truncated for analysis
- This prevents API token limit issues
-
For large codebases:
- Start with
--skip-refactoringto get analysis results quickly - Use
gemini-1.5-flash-latestfor faster processing - Run
python test_gemini_models.pyfirst to find working models - Increase
--delayto avoid rate limits
- Start with
-
For detailed analysis:
- Use
gemini-1.5-pro-latestfor better quality - Process smaller codebases or specific directories
- Use
--skip-analysisif you only need refactored code
- Use
-
Rate limit management:
- The tool automatically adds delays between requests
- Implements exponential backoff for retries
- Monitors for rate limit responses
- Gemini 1.5 Flash: Fast and efficient, generous free tier
- Gemini 1.5 Pro: More capable, limited free requests per minute
- Check current limits at: Google AI Studio
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Google Gemini AI for powerful code analysis
- Uses the latest Gemini 1.5 models for optimal performance
- Designed with rate limiting and error handling for production use
- Support for more programming languages
- Custom refactoring rules and templates
- Integration with popular IDEs
- Batch processing for multiple repositories
- Web-based interface
- Custom analysis rules
- Team collaboration features
For issues, feature requests, or questions:
- Check the Issues page
- Create a new issue with detailed information
- Include error messages and your configuration
Common support scenarios:
- Rate limiting issues β Adjust
--delayparameter - Large codebases β Use
--skip-analysisor--skip-refactoring - API errors β Check your Gemini API key and quota
- Installation issues β Run
python setup.pyagain
- v2.0.0: Migrated to Google Gemini AI with improved rate limiting
- v1.0.0: Initial release with Groq API support