Skip to content

Latest commit

Β 

History

History
472 lines (353 loc) Β· 14.1 KB

File metadata and controls

472 lines (353 loc) Β· 14.1 KB

CCany - Go Version

δΈ­ζ–‡ | English

A Claude Code proxy server rewritten in Go, providing a complete frontend and backend interface that supports converting Claude API requests to OpenAI API calls.

Features

  • Complete Claude API Compatibility: Supports full /v1/messages endpoint
  • Multi-Provider Support: OpenAI, Azure OpenAI, local models (Ollama), and any OpenAI-compatible API
  • Intelligent Model Mapping: Configure large and small models via environment variables
  • Function Calling: Complete tool usage support and conversion
  • Streaming Response: Real-time SSE streaming support
  • Image Support: Base64 encoded image input
  • Web Management Interface: Apple-style design management panel
  • Database Support: Data persistence using ent and SQLite3
  • User Management: Complete user authentication and authorization system
  • Request Logging: Detailed API request logging and analysis
  • System Monitoring: Real-time system performance monitoring and health checks
  • Cache Optimization: Intelligent cache system for performance improvement
  • Error Handling: Comprehensive error handling and logging

Quick Start

1. Install Dependencies

# Ensure Go 1.21+ is installed
go version

# Download dependencies
go mod tidy

2. Optional Environment Variable Configuration

# Copy configuration file (optional)
cp .env.example .env

# Edit .env file to set data storage directory and security keys
# All API and service configurations are managed through the web interface

3. Run with Docker (Recommended)

Using published images:

# Using Docker Hub image with data persistence
docker run -d -p 8082:8082 \
  -v ccany_data:/app/data \
  -v ccany_logs:/app/logs \
  -e OPENAI_API_KEY=your_openai_api_key \
  czyt/ccany:latest

# Or using GitHub Container Registry image with data persistence
docker run -d -p 8082:8082 \
  -v ccany_data:/app/data \
  -v ccany_logs:/app/logs \
  -e OPENAI_API_KEY=your_openai_api_key \
  ghcr.io/ca-x/ccany:latest

Using Docker Compose (Production):

# Copy environment configuration
cp .env.example .env
# Edit .env file to set required API keys

# Use production compose (with published images)
docker-compose -f docker-compose.production.yml up -d

# Or use full compose (includes monitoring services)
docker-compose up -d

4. Local Development

# Start server
go run cmd/server/main.go

5. Initial Setup

# After starting the server, initial setup is required for first access
# Visit http://localhost:8082/setup to create admin account and configure API keys
# Or use the deployment script for automated deployment:
chmod +x scripts/deploy.sh
./scripts/deploy.sh start

6. Configure API Keys

# After logging into the web interface, configure in the management panel:
# - OpenAI API key and base URL
# - Claude API key and base URL
# - Model configuration
# - Performance parameters

7. Use Claude Code

ANTHROPIC_BASE_URL=http://localhost:8082 ANTHROPIC_AUTH_TOKEN="some-api-key" claude

8. Access Web Interface

Open your browser and visit http://localhost:8082 to view the management panel.

Configuration

Environment Variables (Optional)

System Configuration:

  • CLAUDE_PROXY_DATA_PATH - Data storage directory (default: ./data)
  • CLAUDE_PROXY_MASTER_KEY - Master key for encrypting sensitive configurations (recommended for production)
  • JWT_SECRET - JWT key for user authentication (recommended for production)

Backend Configuration (Managed through Web Interface)

API Configuration:

  • OpenAI API key and base URL
  • Claude API key and base URL
  • Azure API version (optional)

Model Configuration:

  • Large model (default: gpt-4o)
  • Small model (default: gpt-4o-mini)

Server Settings:

  • Server host (default: 0.0.0.0)
  • Server port (default: 8082)
  • Log level (default: info)

Performance Configuration:

  • Maximum token limit (default: 4096)
  • Minimum token limit (default: 100)
  • Request timeout seconds (default: 90)
  • Maximum retry attempts (default: 2)
  • Temperature parameter (default: 0.7)
  • Streaming response (default: true)

πŸ’‘ Note: All API and service configurations are now managed through the web management interface, no longer requiring environment variables. Visit the /setup page for initial setup on first run.

Model Mapping

The proxy maps Claude model requests to your configured models:

Claude Request Mapped To Environment Variable
Models containing "haiku" SMALL_MODEL Default: gpt-4o-mini
Models containing "sonnet" or "opus" BIG_MODEL Default: gpt-4o

Provider Configuration Examples

OpenAI

Configure through web interface:

  • OpenAI API Key: sk-your-openai-key
  • OpenAI Base URL: https://api.openai.com/v1
  • Large Model: gpt-4o
  • Small Model: gpt-4o-mini

Azure OpenAI

Configure through web interface:

  • OpenAI API Key: your-azure-key
  • OpenAI Base URL: https://your-resource.openai.azure.com/openai/deployments/your-deployment
  • Azure API Version: 2024-02-01
  • Large Model: gpt-4
  • Small Model: gpt-35-turbo

Local Model (Ollama)

Configure through web interface:

  • OpenAI API Key: dummy-key (required but can be dummy)
  • OpenAI Base URL: http://localhost:11434/v1
  • Large Model: llama3.1:70b
  • Small Model: llama3.1:8b

Claude Official API

Configure through web interface:

  • Claude API Key: sk-ant-your-claude-key
  • Claude Base URL: https://api.anthropic.com

Web Management Interface

Access http://localhost:8082 to use the web management interface, which includes:

  • Dashboard: View service status and statistics
  • Request Logs: View detailed records and analysis of all API requests
  • System Monitoring: Real-time system performance monitoring and health checks
  • User Management: User account management and permission control
  • Configuration Management: View and modify system configuration parameters
  • API Testing: Test connections and send test messages

The interface features Apple-style design with responsive layout and frosted glass effects. A complete authentication system ensures the security of the management interface.

Project Structure

ccany/
β”œβ”€β”€ cmd/
β”‚   └── server/
β”‚       └── main.go              # Main program entry
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ app/                     # Application configuration management
β”‚   β”œβ”€β”€ auth/                    # Authentication service
β”‚   β”œβ”€β”€ cache/                   # Cache service
β”‚   β”œβ”€β”€ claudecode/              # Claude Code compatibility services
β”‚   β”œβ”€β”€ client/                  # OpenAI client
β”‚   β”œβ”€β”€ config/                  # Configuration management
β”‚   β”œβ”€β”€ converter/               # Request/response converter
β”‚   β”œβ”€β”€ crypto/                  # Encryption service
β”‚   β”œβ”€β”€ database/                # Database management
β”‚   β”œβ”€β”€ handlers/                # HTTP handlers
β”‚   β”œβ”€β”€ logging/                 # Request logging
β”‚   β”œβ”€β”€ middleware/              # Middleware
β”‚   β”œβ”€β”€ models/                  # Data models
β”‚   └── monitoring/              # System monitoring
β”œβ”€β”€ ent/
β”‚   β”œβ”€β”€ schema/                  # Database schema definitions
β”‚   └── ...                     # Generated ORM code
β”œβ”€β”€ tests/
β”‚   └── basic_test.go           # Basic test file
β”œβ”€β”€ scripts/
β”‚   └── deploy.sh                # Deployment script
β”œβ”€β”€ web/
β”‚   β”œβ”€β”€ index.html              # Main page
β”‚   β”œβ”€β”€ setup.html              # Setup page
β”‚   └── static/
β”‚       β”œβ”€β”€ css/                # Style files
β”‚       └── js/                 # JavaScript files
β”œβ”€β”€ .env.example                # Configuration template
β”œβ”€β”€ go.mod                      # Go module file
β”œβ”€β”€ Makefile                    # Build script
└── README.md                   # This file

API Endpoints

Claude Compatible Endpoints

  • POST /v1/messages - Create message
  • POST /v1/messages/count_tokens - Count tokens

Management Endpoints

  • GET / - Web management interface
  • GET /health - Health check
  • GET /ready - Readiness check
  • GET /setup - Initialization setup interface
  • POST /setup - Submit initialization setup

Authentication Endpoints

  • POST /admin/auth/login - Admin login
  • POST /admin/auth/logout - Admin logout
  • GET /admin/auth/profile - Get user information

Management API Endpoints

  • GET /admin/users - Get user list
  • POST /admin/users - Create user
  • PUT /admin/users/:id - Update user
  • DELETE /admin/users/:id - Delete user
  • GET /admin/config - Get configuration information
  • PUT /admin/config - Update configuration
  • GET /admin/request-logs - Get request logs
  • GET /admin/request-logs/stats - Get request statistics
  • DELETE /admin/request-logs - Clear request logs

Monitoring Endpoints

  • GET /admin/monitoring/health - System health check
  • GET /admin/monitoring/metrics - System metrics
  • GET /admin/monitoring/system - System information

Development

Build

# Build executable
go build -o ccany cmd/server/main.go

# Cross-compile
GOOS=linux GOARCH=amd64 go build -o ccany-linux cmd/server/main.go
GOOS=windows GOARCH=amd64 go build -o ccany.exe cmd/server/main.go

Testing

# Run all tests
go test ./...

# Run tests for specific package
go test ./internal/converter

# Run integration tests (requires server to be running)
go test -v ./tests/

# Run benchmark tests
go test -bench=. ./tests/

# Generate test coverage report
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html

Code Formatting

# Format code
go fmt ./...

# Check code
go vet ./...

Performance

  • Concurrent Processing: High concurrency using Goroutines
  • Connection Pool: Efficient HTTP connection management
  • Streaming Support: Real-time response streaming
  • Intelligent Caching: Multi-strategy cache system (LRU, LFU, TTL)
  • Request Logging: Asynchronous logging without performance impact
  • System Monitoring: Low-overhead real-time performance monitoring
  • Database Optimization: Connection pooling and query optimization
  • Memory Management: Graceful memory usage and garbage collection
  • Configurable Timeouts: Configurable timeouts and retries
  • Intelligent Error Handling: Detailed logging

Integration with Claude Code

This proxy is designed to work seamlessly with Claude Code CLI. The enhanced version includes complete Claude Code compatibility support:

# Start the enhanced proxy using deployment script
./scripts/deploy.sh start

# Use Claude Code with the proxy
ANTHROPIC_BASE_URL=http://localhost:8082 claude

# Or set permanently
export ANTHROPIC_BASE_URL=http://localhost:8082
claude

Enhanced Claude Code Features

  • βœ… Complete SSE Event Sequence: Support for message_start, content_block_start, ping, content_block_delta, content_block_stop, message_delta, message_stop events
  • βœ… Request Cancellation Support: Client disconnect detection and graceful request cancellation
  • βœ… Claude Configuration Automation: Automatic creation of ~/.claude.json configuration file
  • βœ… Thinking Mode: Support for thinking field and intelligent model routing
  • βœ… Enhanced Tool Calls: Tool call streaming with incremental JSON parsing
  • βœ… Cache Tokens: Support for cache_read_input_tokens usage reporting
  • βœ… Smart Routing: Intelligent model selection based on complexity and token count

Deployment Options

# Basic deployment
./scripts/deploy.sh start

# Deployment with monitoring
./scripts/deploy.sh monitoring

# Deployment with Nginx
./scripts/deploy.sh nginx

# Test Claude Code compatibility
./scripts/deploy.sh test

# Show help
./scripts/deploy.sh help

Docker Deployment

Using Docker Compose

# Copy environment configuration
cp .env.example .env
# Edit .env file to configure API keys

# Basic deployment
docker-compose up -d

# Deployment with monitoring
docker-compose --profile monitoring up -d

# Deployment with Nginx
docker-compose --profile nginx up -d

# Test Claude Code compatibility
docker-compose --profile test up --build test-claude-code

Using Deployment Script

# Automated deployment (recommended)
./scripts/deploy.sh start

# Deployment with monitoring stack
./scripts/deploy.sh monitoring

# Check service status
./scripts/deploy.sh status

# View logs
./scripts/deploy.sh logs

For detailed deployment guide, please refer to: Deployment Documentation

License

MIT License

Contributing

Issues and Pull Requests are welcome!

Changelog

v1.3.0 (Enhanced - Claude Code Compatibility)

  • βœ… Complete Claude Code compatibility support
  • βœ… Enhanced SSE event sequence (message_start, content_block_start, ping, content_block_delta, content_block_stop, message_delta, message_stop)
  • βœ… Request cancellation and client disconnect detection
  • βœ… Claude configuration auto-initialization (~/.claude.json)
  • βœ… Thinking mode support and intelligent model routing
  • βœ… Enhanced tool call streaming
  • βœ… Cache token usage reporting
  • βœ… Enhanced Docker and Docker Compose configuration
  • βœ… GitHub Actions CI/CD pipeline
  • βœ… Complete deployment scripts and monitoring support
  • βœ… Enhanced Nginx configuration and performance optimization

v1.2.0

  • Complete backend management system
  • User authentication and authorization
  • Request logging and analysis
  • System monitoring and health checks
  • Intelligent cache system
  • Complete test suite
  • Enhanced documentation and deployment guide

v1.1.0

  • Database integration and ORM support
  • Configuration management system
  • Secure configuration storage
  • Database migration support

v1.0.0

  • Initial Go version release
  • Complete Claude API compatibility
  • Web management interface
  • Database support
  • Apple-style design UI