Skip to main content

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

Python 3.8+ License: MIT Code Quality

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):

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

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: safety integration
  • JavaScript: npm audit integration
  • 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 issues
  • 1 - Critical issues found
  • 2 - Analysis error

๐Ÿค Contributing

Contributions are welcome! Please read our contributing guidelines.

Adding a New Language Analyzer

  1. Create code_sage/analyzers/your_language_analyzer.py
  2. Extend BaseAnalyzer class
  3. Implement analyze_file() method
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

code_sage_ai-1.0.0.tar.gz (47.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

code_sage_ai-1.0.0-py3-none-any.whl (48.7 kB view details)

Uploaded Python 3

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

Hashes for code_sage_ai-1.0.0.tar.gz
Algorithm Hash digest
SHA256 39a95142bc7ceb0285754eecb36bb806e784decc7a99a2bcb209ce61dd588019
MD5 ee21dc4670d77155b74762597d53ec82
BLAKE2b-256 f9cf86183b5384a7ec06eb8ce0fb1e0ece0f39dc10cb6098f12519c9cc3508c4

See more details on using hashes here.

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

Hashes for code_sage_ai-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ef517ccefd4d9855d00ae7778ab8094261be13e33c7beacb37ea147ba8efd0e
MD5 966b59d22d41a32e226efa0288324e48
BLAKE2b-256 51661d44089f69007471408cd7551e0887cba905b5be5f5f4b0f99a567ccc63f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page