AI-powered code analyzer that finds bugs, suggests improvements, and fixes issues
Project description
Code Sage ๐งโโ๏ธ
Production-ready AI-powered code analyzer that finds bugs, security issues, and suggests improvements
Features โข Installation โข Quick Start โข Documentation โข Examples
๐ Overview
Code Sage is a production-ready static code analyzer built with Python that works immediately with no configuration. It combines traditional AST-based analysis with optional AI (GPT-4/Claude) to provide intelligent code review, security scanning, and automated fixes.
Why Code Sage?
- โ No Setup Required: Works instantly - no API keys, no configuration needed
- โ Comprehensive: Multi-language support, security scanning, optional AI in one tool
- โ Production Ready: Fully tested, type-hinted, well-documented code
- โ Privacy First: Runs 100% locally, AI is optional and only used if you enable it
- โ Developer Friendly: Beautiful CLI, rich output, easy configuration
- โ Extensible: Plugin architecture, custom rules, multiple output formats
โจ Features
Core Analysis
- ๐ Multi-Language: Python, JavaScript, TypeScript (extensible)
- ๐ Bug Detection: Mutable defaults, identity checks, type errors
- ๐ก Code Smells: Complexity, long functions, duplicate code
- ๐ Metrics: Cyclomatic complexity, maintainability index, Halstead
Security Scanning
- ๐ก๏ธ Secrets Detection: API keys, passwords with entropy analysis
- ๐ OWASP Top 10: SQL injection, XSS, command injection
- ๐ฆ Dependency CVEs: Checks npm and pip packages
- ๐ Language-Specific: Pickle, eval, dangerous functions
AI Integration (Optional)
- ๐ค GPT-4 & Claude: Context-aware analysis (optional)
- ๐ฌ Explanations: Plain English issue descriptions
- ๐ง Auto-Fix: AI-suggested code fixes
- ๐ฏ Smart: Focuses on critical issues first
- โก Works Without AI: Full analysis capabilities without API keys
Professional CLI
- ๐จ Beautiful UI: Rich console with colors and tables
- โก Fast: Parallel analysis with caching
- ๐ Reports: HTML, JSON, SARIF formats
- ๐ Git Integration: Pre-commit hooks, repo analysis
๐ Installation
From Source
git clone https://github.com/stanveer/Code-Sage.git
cd Code-Sage
pip install -r requirements.txt
pip install -e .
System Requirements
- Python 3.8+ (required)
- API Key (optional, only for AI features):
- OpenAI API key OR
- Anthropic API key
Note: Works perfectly without any API keys!
๐ Quick Start
1. Analyze a Local Project (No Setup Required)
code-sage analyze myproject/
This works immediately with no configuration - finds bugs, code smells, and security issues!
2. Analyze a GitHub Repository
code-sage github https://github.com/username/repo
Code Sage will clone and analyze any public repository automatically.
3. Generate Beautiful Reports
code-sage analyze myproject/ --output report.html --format html
4. Optional: Enable AI-Powered Insights
AI features are completely optional but provide enhanced explanations and fix suggestions.
# Set your API key (one-time setup)
export OPENAI_API_KEY="sk-..."
# Or for Claude
export ANTHROPIC_API_KEY="sk-ant-..."
# Run with AI enabled
code-sage analyze myproject/ --ai
Note: The tool works perfectly without AI - AI just adds extra explanations and suggestions!
๐ป Usage Examples
Basic Analysis
# Analyze current directory
code-sage analyze .
# Analyze specific file
code-sage analyze src/main.py
# Filter by severity (only show critical/high)
code-sage analyze . --severity high
# JSON output
code-sage analyze . --format json --output results.json
Security Scanning
# Enable security scan
code-sage analyze . --security
# Security only (faster)
code-sage analyze . --security --no-ai
AI-Powered Analysis (Optional)
AI features are completely optional! Code Sage performs full analysis without any API keys.
# Default: Works without AI (no setup needed)
code-sage analyze .
# Skip AI explicitly (faster)
code-sage analyze . --no-ai
# Enable AI with OpenAI GPT-4 (requires API key)
export OPENAI_API_KEY="sk-..."
code-sage analyze . --ai
# Or use Anthropic Claude (requires API key)
export ANTHROPIC_API_KEY="sk-ant-..."
code-sage analyze . --ai --config claude-config.yaml
Get API Keys (Optional):
- OpenAI: https://platform.openai.com/api-keys
- Anthropic: https://console.anthropic.com/account/keys
Configuration
# Create config file
code-sage init
# Use custom config
code-sage analyze . --config myconfig.yaml
# Verbose output
code-sage analyze . --verbose
๐ค AI Features (Optional)
Do I Need AI?
No! Code Sage is fully functional without AI:
- โ Finds all bugs, code smells, and security issues
- โ Calculates code metrics
- โ Generates reports
- โ Works 100% locally
What Does AI Add?
AI provides enhanced insights:
- ๐ฌ Plain English explanations of why issues matter
- ๐ง Intelligent fix suggestions with code examples
- ๐ฏ Context-aware recommendations
- ๐ Learning resources and best practices
Setting Up AI (Optional)
Option 1: Environment Variables (Easiest)
# For OpenAI (GPT-4)
export OPENAI_API_KEY="sk-proj-..."
# For Anthropic (Claude)
export ANTHROPIC_API_KEY="sk-ant-..."
# Then use with --ai flag
code-sage analyze . --ai
Option 2: Configuration File
Create .codesage.yaml:
ai:
enabled: true
provider: openai # or 'anthropic'
openai_api_key: ${OPENAI_API_KEY} # reads from environment
openai_model: gpt-4o-mini # Options: gpt-4o, gpt-4o-mini, gpt-3.5-turbo
temperature: 0.3
Then just run:
code-sage analyze . # AI automatically enabled
Getting API Keys
- OpenAI: https://platform.openai.com/api-keys (~$0.03/analysis)
- Anthropic: https://console.anthropic.com/account/keys (~$0.02/analysis)
Disable AI
# Explicitly disable (faster, free)
code-sage analyze . --no-ai
# Or in config file
ai:
enabled: false
โ๏ธ Configuration
Create .codesage.yaml in your project root:
ai:
enabled: true
provider: openai # or anthropic
openai_api_key: ${OPENAI_API_KEY}
openai_model: gpt-4o-mini # Options: gpt-4o, gpt-4o-mini, gpt-3.5-turbo
temperature: 0.3
analysis:
enabled_languages:
- python
- javascript
- typescript
min_severity: medium
max_complexity: 15
parallel_analysis: true
security:
enable_secrets_scan: true
enable_dependency_scan: true
enable_owasp_scan: true
min_entropy_threshold: 4.5
output:
format: rich
generate_html: true
show_ai_explanations: true
output_dir: ./reports
See .example.codesage.yaml for all configuration options.
๐ Example Output
$ code-sage analyze examples/
๐งโโ๏ธ Code Sage v1.0.0
Analyzing: examples/
โ Analyzing files... (2.3s)
โ Running security scan... (0.8s)
โ AI analysis... (4.2s)
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโ
โ Metric โ Value โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ Total Files โ 2 โ
โ Total Issues โ 23 โ
โ Analysis Time โ 7.3s โ
โโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโ
โโโโโโโโโโโโฌโโโโโโโโ
โ Severity โ Count โ
โโโโโโโโโโโโผโโโโโโโโค
โ CRITICAL โ 3 โ
โ HIGH โ 5 โ
โ MEDIUM โ 8 โ
โ LOW โ 7 โ
โโโโโโโโโโโโดโโโโโโโโ
Top Issues:
1. CRITICAL Hardcoded Secret: API Key
๐ examples/example.py:11
Potential hardcoded api key detected
2. HIGH SQL Injection
๐ examples/example.py:28
SQL query built with string concatenation
3. HIGH Use of eval()
๐ examples/example.py:45
Code injection risk via eval
๐๏ธ Architecture
code_sage/
โโโ core/ # Core engine and models
โ โโโ engine.py # Main analysis engine
โ โโโ models.py # Data models
โ โโโ config.py # Configuration management
โ โโโ analyzer.py # Base analyzer interface
โ โโโ aggregator.py # Issue deduplication & ranking
โ โโโ pattern_matcher.py # Pattern matching engine
โโโ analyzers/ # Language-specific analyzers
โ โโโ python_analyzer.py
โ โโโ javascript_analyzer.py
โโโ ai/ # AI integration
โ โโโ provider.py # AI provider abstraction
โ โโโ enrichment.py # Issue enrichment
โโโ security/ # Security scanning
โ โโโ scanner.py # Security vulnerability detection
โโโ cli/ # Command-line interface
โ โโโ main.py # CLI entry point
โ โโโ reporter.py # Report generation
โโโ git/ # Git integration
โ โโโ hooks.py # Git hooks management
โโโ utils/ # Utilities
โโโ file_utils.py # File operations
๐งช Development
Running Tests
# Install dev dependencies
pip install -r requirements.txt
# Run tests
pytest tests/ -v
# With coverage
pytest tests/ --cov=code_sage --cov-report=html
Run on Examples
# Analyze example files
code-sage analyze examples/
# Should find ~20+ issues
Code Quality
# Format code
black code_sage/
# Sort imports
isort code_sage/
# Type check
mypy code_sage/
# Lint
flake8 code_sage/
๐ฏ Supported Languages
| Language | Status | Features |
|---|---|---|
| Python | โ Full | AST analysis, complexity, security |
| JavaScript | โ Full | Syntax, common issues, security |
| TypeScript | โ Full | Same as JavaScript |
| Java | ๐ง Planned | - |
| Go | ๐ง Planned | - |
| Rust | ๐ง Planned | - |
๐ก๏ธ Security Features
Secrets Detection
- API keys, passwords, tokens
- Entropy analysis (Shannon entropy)
- Configurable patterns
- Support for custom secret patterns
OWASP Top 10
- SQL Injection
- Cross-Site Scripting (XSS)
- Command Injection
- Insecure Deserialization
- And more...
Dependency Scanning
- CVE database checks
- Python:
safetyintegration - JavaScript:
npm auditintegration - Outdated package detection
๐ Documentation
Command Reference
# Main commands
code-sage analyze <path> # Analyze code
code-sage github <url> # Analyze GitHub repo
code-sage init # Create config file
code-sage report <path> # Generate report
# Options
--config PATH # Custom config file
--output PATH # Output file
--format FORMAT # Output format (rich/json/sarif)
--severity LEVEL # Min severity (info/low/medium/high/critical)
--ai / --no-ai # Enable/disable AI
--security # Enable security scan
--verbose # Verbose output
--debug # Debug mode
Exit Codes
0- Success, no critical issues1- Critical issues found2- Analysis error
๐ค Contributing
Contributions are welcome! Please read our contributing guidelines.
Adding a New Language Analyzer
- Create
code_sage/analyzers/your_language_analyzer.py - Extend
BaseAnalyzerclass - Implement
analyze_file()method - Register in
engine.py
Adding Custom Rules
Create a .codesage-rules.yaml:
rules:
- id: custom-rule-1
name: "No TODO comments"
pattern: "# TODO"
severity: low
category: maintainability
languages: [python, javascript]
๐ Roadmap
- Python analyzer
- JavaScript/TypeScript analyzer
- AI integration (GPT-4, Claude)
- Security scanning
- HTML reports
- VS Code extension
- JetBrains IDE plugin
- Web dashboard
- More language support (Java, Go, Rust)
- Auto-fix implementation
- CI/CD integrations
๐ License
MIT License - see LICENSE file
๐ Acknowledgments
- Built with Click, Rich, and Radon
- AI powered by OpenAI and Anthropic
- Inspired by tools like SonarQube, CodeClimate, and Bandit
๐ Try It Now!
git clone https://github.com/stanveer/Code-Sage.git
cd Code-Sage
pip install -r requirements.txt
pip install -e .
# Analyze the examples
code-sage analyze examples/
# See what Code Sage can do! ๐งโโ๏ธ
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 code_sage_ai-1.0.0.tar.gz.
File metadata
- Download URL: code_sage_ai-1.0.0.tar.gz
- Upload date:
- Size: 47.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39a95142bc7ceb0285754eecb36bb806e784decc7a99a2bcb209ce61dd588019
|
|
| MD5 |
ee21dc4670d77155b74762597d53ec82
|
|
| BLAKE2b-256 |
f9cf86183b5384a7ec06eb8ce0fb1e0ece0f39dc10cb6098f12519c9cc3508c4
|
File details
Details for the file code_sage_ai-1.0.0-py3-none-any.whl.
File metadata
- Download URL: code_sage_ai-1.0.0-py3-none-any.whl
- Upload date:
- Size: 48.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ef517ccefd4d9855d00ae7778ab8094261be13e33c7beacb37ea147ba8efd0e
|
|
| MD5 |
966b59d22d41a32e226efa0288324e48
|
|
| BLAKE2b-256 |
51661d44089f69007471408cd7551e0887cba905b5be5f5f4b0f99a567ccc63f
|