Skip to content

Latest commit

 

History

History
321 lines (242 loc) · 11 KB

File metadata and controls

321 lines (242 loc) · 11 KB

🤖 Codestral CLI

A modern GitHub Copilot-style terminal interface for Codestral - Mistral's code generation AI. Real-time streaming responses with beautiful terminal UI and powerful developer tools.

License: MIT Python 3.8+ Codestral

✨ Features

  • 💬 Interactive Chat - GitHub Copilot-like terminal experience
  • Real-time Streaming - See responses as they generate, token by token
  • 🎨 Rich Terminal UI - Beautiful formatting with live updates and markdown support
  • 🤖 AI Function Calling - Native Mistral function calling with automatic tool selection
  • 🛠️ Smart Developer Tools - AI-powered code review, debugging, analysis with 11+ tools
  • 🚀 Async Architecture - Non-blocking, responsive interface
  • 🔧 Easy Setup - One-command installation with automatic SSL handling

🚀 Quick Start

Installation

git clone https://github.com/momominds/codestral-cli.git
cd codestral-cli
./install.sh

Setup API Key

Get your API key from Mistral Console:

export MISTRAL_API_KEY="your-api-key"

Usage

# Interactive mode
codestral-cli

# Single prompt  
codestral-cli "Write a Python function to sort a list"

# With options
codestral-cli "Explain async Python" --max-tokens 500

💻 Interactive Experience

$ codestral-cli

🚀 Codestral CLI (Function Calling Mode)
Model: codestral-latest • Tools: 11 available • AI Selection: ON

👤 You: Can you review my main.py file for security issues?

🤖 Codestral: I'll analyze your file for security concerns...
[🔧 AI automatically calls: review(file_path="main.py", focus="security")]

✅ Security Review Complete:
╭─ Code Review Results ─────────────────────────────────────────╮
│ • No SQL injection vulnerabilities detected                   │
│ • Input validation properly implemented                       │  
│ • Authentication mechanisms secure                            │
│ • Recommendation: Consider adding rate limiting               │
╰───────────────────────────────── Tokens: 120 • Time: 2.1s ──╯

👤 You: Debug this error: ImportError: No module named 'requests'

🤖 Codestral: Let me help debug that import error...
[🔧 AI automatically calls: debug(error_message="ImportError: No module named 'requests'")]

✅ Debug Analysis:
╭─ Error Diagnosis & Solution ──────────────────────────────────╮
│ Issue: Missing dependency - requests module not installed     │
│ Solution: pip install requests                                │
│ Prevention: Add requests to requirements.txt                  │
╰───────────────────────────────── Tokens: 85 • Time: 1.5s ───╯

👤 You: What's the weather like?

🤖 Codestral: I don't have access to weather data, but I can help with coding tasks!
Try asking me to review code, debug errors, or explain commands.

👤 You: exit
👋 Goodbye!

🤖 AI-Powered Function Calling

Codestral CLI uses Mistral AI's native function calling to intelligently select and execute the right tools for your requests. Just speak naturally - the AI automatically chooses what to do!

🎯 How It Works

  1. Natural Language: "Review my code for bugs"
  2. AI Selection: Codestral automatically calls review(file="code.py", focus="bugs")
  3. Execution: Tool runs and returns formatted results
  4. Response: See actionable insights immediately

🛠️ Available Tools (AI Auto-Selected)

Tool Purpose Example Trigger
analyze Code structure & complexity analysis "Analyze main.py complexity"
review Detailed code review & feedback "Review this file for security issues"
test Generate comprehensive unit tests "Generate tests for utils.py"
docs Create documentation "Document this function"
debug Analyze & fix errors "Debug: ImportError: No module named 'x'"
explain Explain shell commands "Explain 'docker run -it ubuntu'"
suggest Suggest commands for tasks "How do I deploy to AWS?"
commit Generate commit messages "Help me commit these changes"
search Find code patterns "Find all database queries"
git Git workflow help "Help with git merge conflicts"

💡 Function Calling vs Manual Commands

# Old way (still works)
/review main.py --focus security

# New AI way (preferred) 
"Can you review main.py for security issues?"
# → AI automatically calls review(file_path="main.py", focus="security")

⚙️ Configuration

Environment Variables

export MISTRAL_API_KEY="your-key"              # Required
export MISTRAL_MODEL="codestral-latest"        # Optional
export MISTRAL_MAX_TOKENS=2000                 # Optional

Config File (~/.codestral/config.json)

{
  "api_key": "your-key",
  "model": "codestral-latest", 
  "max_tokens": 2000,
  "temperature": 0.1
}

🔧 Command Line Options

codestral-cli [prompt] [options]

Arguments:
  prompt                    Prompt to send (if not provided, enters interactive mode)

Options:
  --max-tokens INT         Maximum tokens in response
  --version               Show version information
  --help                  Show help message

🆘 Troubleshooting

SSL Certificate Issues

If you get SSL certificate errors:

# Automatic fix
python3 fix_ssl.py

# Manual fix
pip3 install --upgrade certifi

# Temporary bypass (not recommended for production)
PYTHONHTTPSVERIFY=0 codestral-cli "test prompt"

Common Issues

  • API Key: Make sure MISTRAL_API_KEY is set correctly
  • Dependencies: Run pip3 install -r requirements.txt
  • Permissions: Run chmod +x install.sh main.py fix_ssl.py

📚 Documentation

Complete documentation available in the docs/ directory:

🎯 Key Differences from Traditional CLIs

Traditional CLI Codestral CLI
Block and wait ⚡ Stream live responses
Plain text output 🎨 Rich markdown formatting
Single request/response 💬 Conversational context
No developer tools 🛠️ Built-in code tools
Basic error handling 🔧 Comprehensive troubleshooting

🏗️ Architecture

  • Async Streaming: Real-time token-by-token responses using aiohttp
  • Rich Terminal UI: Beautiful formatting with rich library
  • Modular Tools: Extensible developer tool system
  • Smart Error Handling: SSL troubleshooting and helpful error messages
  • Clean Configuration: Multiple config methods with priority system

🤝 Contributing

We welcome contributions! Please see:

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Mistral AI for the excellent Codestral model
  • Rich for beautiful terminal UI
  • The Python async ecosystem (aiohttp, asyncio)
  • The open-source community for inspiration and feedback

🔗 Links


Built with ❤️ for developers who love beautiful, responsive terminal experiences.

Use with any OpenAI-compatible tool

export OPENAI_API_BASE="http://localhost:8080/v1"


## 🛠️ Built-in Tools

The CLI includes powerful developer tools accessible via `/command`:

- `/explain <command>` - Explain shell commands
- `/suggest <task>` - Suggest commands for tasks  
- `/review <file>` - Code review and feedback
- `/analyze <file>` - Analyze code structure
- `/commit` - Generate git commit messages
- `/debug <error>` - Help debug errors
- `/test <file>` - Generate unit tests
- `/docs <code>` - Generate documentation

## 📁 Project Structure

codestral-cli/ ├── main.py # CLI entry point ├── codestral_cli/ # Main package │ ├── core/ # Core functionality │ │ ├── client.py # API client │ │ ├── config.py # Configuration │ │ └── cli.py # CLI interface │ ├── tools/ # Developer tools │ ├── ui/ # User interface │ └── utils/ # Utilities ├── proxy/ # OpenAI-compatible proxy │ ├── server.py # Async server │ └── simple_server.py # Simple HTTP server ├── examples/ # Usage examples └── tests/ # Test suite


## ⚙️ Configuration

### Environment Variables
- `MISTRAL_API_KEY` - Your Codestral API key (required)
- `MISTRAL_MODEL` - Model to use (default: codestral-latest)
- `MISTRAL_BASE_URL` - API base URL (default: https://codestral.mistral.ai/v1)

### Config File
Create `~/.codestral/config.json`:
```json
{
  "model": "codestral-latest",
  "max_tokens": 2000,
  "temperature": 0.1,
  "show_loader": true
}

🔗 Integration Examples

With VS Code

# Start proxy
python3 proxy/server.py &

# Configure VS Code to use local endpoint
# Settings → OpenAI: Base URL → http://localhost:8080/v1

With curl

curl -X POST http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "user", "content": "Write a Python hello world"}]}'

🤝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

📝 License

This project is licensed under the MIT License - see LICENSE for details.

🙏 Acknowledgments

  • Mistral AI for the excellent Codestral model
  • OpenAI for the API standard enabling easy integration
  • The open-source community for inspiration and tools

Built for developers, by developers ❤️