๐ ไธญๆๆๆกฃ | ไธญๆๅฟซ้ๆๅ โข ไธญๆๆๆฏๆๆกฃ
A high-performance AI model proxy service based on FastAPI and Svelte 5, supporting multi-provider configuration and management.
CC Switch is an enterprise-grade API proxy service that implements Anthropic-compatible API endpoints and forwards requests to backend providers supporting OpenAI-compatible interfaces (such as Qwen, ModelScope, AI Ping, Anthropic, etc.). Through a unified API interface, you can easily switch between different AI model providers without modifying client code.
backend/app/
โโโ main.py # Application entry point
โโโ config/ # Configuration management
โ โโโ settings.py # Main config (ProviderConfig, AppConfig, etc.)
โ โโโ hot_reload.py # Config hot-reload with watchdog
โโโ core/ # Core business logic
โ โโโ auth.py # JWT auth, API key validation
โ โโโ constants.py # Constants (API_VERSION, MAX_MESSAGE_LENGTH, etc.)
โ โโโ lifecycle.py # Startup/shutdown events
โ โโโ model_manager.py # Provider & model routing
โ โโโ models.py # Pydantic models (Message, MessagesRequest, etc.)
โ โโโ permissions.py # Permission definitions & checks
โโโ routes/ # API routes (unified under /routes/)
โ โโโ messages.py # /v1/messages endpoint
โ โโโ auth.py # /api/auth/* (login, register)
โ โโโ api_keys.py # /api/api_keys/* (API key management)
โ โโโ providers.py # /api/providers/* (provider management)
โ โโโ conversations.py # /api/conversations/* (chat history)
โ โโโ health.py # /api/health/* (health check)
โ โโโ stats.py # /api/stats/* (statistics)
โ โโโ config.py # /api/config/* (config management)
โ โโโ preferences.py # /api/preferences/* (user preferences)
โ โโโ event_logging.py # /api/event_logging/* (event logging)
โ โโโ admin_permissions.py # /api/admin/permissions/* (user & permission management)
โ โโโ oauth.py # /oauth/* (OAuth login)
โโโ services/ # Business logic services
โ โโโ handlers/ # Request handlers (OpenAI/Anthropic format)
โ โ โโโ base.py
โ โ โโโ openai_handler.py
โ โ โโโ anthropic_handler.py
โ โโโ message_service.py # Message processing & provider routing
โ โโโ health_service.py # Health monitoring service
โ โโโ provider_service.py # Provider management service
โ โโโ token_counter.py # Token counting & history lookup
โ โโโ config_service.py # Config service
โ โโโ oauth_service.py # OAuth service
โโโ converters/ # Format conversion (Anthropic โ OpenAI)
โ โโโ anthropic_request_convert.py # Anthropic โ OpenAI request
โ โโโ openai_response_convert.py # OpenAI โ Anthropic response
โโโ infrastructure/ # Infrastructure layer
โ โโโ clients/ # Provider API clients
โ โ โโโ openai_client.py
โ โ โโโ anthropic_client.py
โ โโโ circuit_breaker.py # Circuit breaker pattern
โ โโโ concurrency_manager.py # Concurrency control
โ โโโ retry.py # Retry with backoff
โ โโโ cache.py # In-memory/Redis cache
โ โโโ telemetry.py # OpenTelemetry integration
โโโ database/ # Data access layer (async SQLite)
โ โโโ core.py # Database connection & schema
โ โโโ users.py # User management
โ โโโ api_keys.py # API key storage
โ โโโ conversations.py # Chat conversations & messages
โ โโโ request_logs.py # Request logging
โ โโโ token_usage.py # Token usage tracking
โ โโโ health_history.py # Health history
โ โโโ config_changes.py # Config change history
โ โโโ oauth_accounts.py # OAuth account associations
โ โโโ encryption.py # Encryption utilities
โโโ utils/ # Utility functions
โ โโโ token_extractor.py # Unified token extraction (supports OpenAI/Anthropic)
โ โโโ security_utils.py # Encryption, validation, API key masking
โ โโโ color_logger.py # Colored logging
โ โโโ error_handler.py # Error response formatting
โ โโโ response.py # Response utilities
โโโ encryption_key.py # Encryption key management
frontend/src/
โโโ lib/
โ โโโ components/ # Reusable Svelte components
โ โ โโโ chat/ # Chat-related components (ChatArea, MessageBubble, etc.)
โ โ โโโ layout/ # Layout components (Header, MobileNav)
โ โ โโโ providers/ # Provider management components
โ โ โโโ settings/ # Settings components
โ โ โโโ ui/ # Base UI components (Button, Input, Card, etc.)
โ โ โโโ i18n/ # Internationalization component (Translate)
โ โ โโโ ErrorMessageModal.svelte
โ โ โโโ Pagination.svelte
โ โ โโโ ProviderForm.svelte
โ โ โโโ SettingsModal.svelte
โ โ โโโ WelcomeModal.svelte
โ โ โโโ OAuthIcon.svelte
โ โโโ services/ # API client services
โ โ โโโ api.ts # Main API client
โ โ โโโ chatService.ts # Chat service
โ โ โโโ auth.ts # Auth service
โ โ โโโ permissions.ts # Permission management service
โ โ โโโ oauthProviders.ts # OAuth provider configuration
โ โ โโโ apiKeys.ts # API Key service
โ โ โโโ apiKeyStorage.ts # Secure API Key storage
โ โ โโโ providers.ts # Provider service
โ โ โโโ health.ts # Health monitoring service
โ โ โโโ stats.ts # Statistics service
โ โ โโโ config.ts # Config service
โ โ โโโ preferences.ts # User preferences service
โ โโโ stores/ # Svelte stores (Svelte 5 $state)
โ โ โโโ auth.svelte.ts # Auth state
โ โ โโโ chatSession.ts # Chat session state
โ โ โโโ providers.ts # Provider state
โ โ โโโ health.ts # Health state
โ โ โโโ language.ts # Internationalization state
โ โ โโโ theme.ts # Theme state
โ โ โโโ toast.ts # Toast message state
โ โ โโโ config.ts # Config state
โ โโโ types/ # TypeScript type definitions
โ โ โโโ permission.ts # Permission types
โ โ โโโ apiKey.ts # API Key types
โ โ โโโ provider.ts # Provider types
โ โ โโโ health.ts # Health types
โ โ โโโ config.ts # Config types
โ โ โโโ language.ts # Language types
โ โโโ config/ # Configuration files
โ โ โโโ keyboardShortcuts.ts # Keyboard shortcuts
โ โโโ utils/ # Utility functions
โ โ โโโ gesture.ts # Gesture detection
โ โ โโโ session.ts # Session management
โ โโโ i18n/ # Internationalization resources (16 languages)
โโโ routes/ # SvelteKit pages
โ โโโ +layout.svelte # Root layout (auth & permission checks)
โ โโโ +page.svelte # Home page
โ โโโ login/ # Login page (email + OAuth)
โ โ โโโ +page.ts
โ โโโ chat/ # Chat page
โ โโโ providers/ # Provider management
โ โโโ api-keys/ # API Key management
โ โโโ health/ # Health monitoring
โ โโโ stats/ # Usage statistics
โ โโโ config/ # System configuration
โ โโโ admin/
โ โ โโโ users/ # User management
โ โ โโโ +page.svelte # User list
โ โ โโโ [id]/ # User details & permission config
โ โโโ oauth/
โ โโโ [provider]/ # OAuth callback handling
โ โโโ callback/ # OAuth callback page
โโโ app.html # HTML template
Client Request
โ
API Routes (/routes/messages.py, /routes/*.py)
โ
Message Service (message_service.py)
โ
Converters (converters/)
โ
Provider Handler (services/handlers/)
โ
Provider Client (infrastructure/clients/)
โ
Backend AI Provider (OpenAI/Anthropic format)
โ
Response Conversion
โ
Client
- ๐ฅ High-Performance Architecture - Async database + connection pool, HTTP connection pool optimization, supports 10k QPS
- ๐ก๏ธ Enterprise-Grade Security - JWT key management, encrypted data storage, strong password policies
- ๐ Multiple Authentication Methods - Email/password login + OAuth social login (GitHub, Google, Feishu, Microsoft, OIDC)
- ๐ฅ User Management - Complete user lifecycle management (create, edit, delete, enable/disable)
- ๐ก๏ธ Fine-Grained Permission Control - 9 permission points for precise access control, per-user permission configuration
- ๐ Internationalization Support - 16 languages supported (Chinese, English, Japanese, Korean, etc.)
- ๐ Modern Management Interface - Svelte 5 + TypeScript, PWA support, dark/light themes
- ๐ง Smart Management - OpenTelemetry integration, health monitoring, automatic failover, circuit breaker pattern
- ๐ Operations Monitoring - Performance statistics, token usage tracking, real-time logs
- ๐ฌ Conversation Management - Chat history, multi-conversation support, token usage statistics
- ๐ข Multi-Provider Support - Unified API interface, intelligent model mapping, provider token limits
- Python 3.9+ (recommended 3.11+)
- Node.js 18+ (recommended 20+)
- npm/pnpm/yarn (recommended pnpm)
- Docker & Docker Compose (optional, for containerized deployment)
# Clone the repository
git clone https://github.com/MichaelHuang7119/cc-switch.git
cd cc-switch
# Start all services (backend + frontend)
docker-compose up -d
# Check service status
docker-compose ps
# View logs
docker-compose logs -f frontend
docker-compose logs -f backendAfter starting the services:
- Frontend Management Interface: http://localhost:5173
- API Documentation: http://localhost:8000/docs
1. Start Backend Service
cd backend
bash start.sh # With hot reload for development mode: bash start.sh --dev2. Start Frontend Service (New Terminal)
cd frontend
# Using bash start script
bash start.sh # With hot reload for development mode: bash start.sh --dev
# Using npm/pnpm (can specify port)
pnpm install # or: npm install, install dependencies first time
pnpm dev -- --port 5173 # or: npm dev -- --port 5173- Visit the frontend management interface: http://localhost:5173
- System will automatically redirect to login page
- Use default admin credentials:
- Email:
[email protected] - Password:
admin123
- Email:
Important: Please change the password immediately after first login! Production environments require strong passwords.
Production environment please set the following environment variables for data security and extended configuration:
# Required - JWT secret key
export JWT_SECRET_KEY="your-strong-secret-key-here"
# Recommended - Encryption key (for sensitive data encryption)
export ENCRYPTION_KEY="your-fernet-encryption-key-here"
# Recommended - Admin password (at least 12 characters)
export ADMIN_PASSWORD="your-secure-password"
# Performance optimization - database connection pool
export DB_POOL_SIZE=20
export DB_POOL_TIMEOUT=30.0
# Optional - monitoring configuration
export ENABLE_TELEMETRY=true
export OTLP_ENDPOINT=http://jaeger:4318- Configure AI Providers:
Option 1: Edit backend configuration file
Edit the backend/provider.json file:
{
"providers": [
{
"name": "qwen",
"enabled": true,
"priority": 1,
"api_key": "${QWEN_API_KEY}",
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
"timeout": 60,
"max_retries": 1,
"models": {
"big": ["qwen-plus", "qwen-max"],
"middle": ["qwen-turbo"],
"small": ["qwen-plus"]
}
},
{
"name": "anthropic-direct",
"enabled": true,
"priority": 2,
"api_key": "${ANTHROPIC_API_KEY}",
"base_url": "https://api.anthropic.com",
"api_format": "anthropic",
"timeout": 60,
"max_retries": 1,
"models": {
"big": ["claude-3-opus-20240229"],
"middle": ["claude-3-sonnet-20240229"],
"small": ["claude-3-haiku-20240307"]
}
}
],
"fallback_strategy": "priority",
"circuit_breaker": {
"failure_threshold": 5,
"recovery_timeout": 60
}
}Option 2: Configure via frontend
- Create an API Key:
Method 1: Create via cURL using backend API
Creating an API Key requires admin privileges. You must obtain a JWT token first.
# Step 1: Login to get JWT token
curl -X POST http://localhost:8000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]", "password": "admin123"}'Response example:
{
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"token_type": "bearer",
"user": {
"id": 1,
"email": "[email protected]",
"name": "Administrator",
"is_admin": true
}
}# Step 2: Create API Key
curl -X POST http://localhost:8000/api/api-keys \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <your_JWT_token>" \
-d '{"name": "my-api-key", "email": "[email protected]"}'Response example:
{
"id": 1,
"api_key": "sk-abc123...", // Full API Key is only shown now, please store it safely
"key_prefix": "sk-abc1...",
"name": "my-api-key",
"email": "[email protected]",
"is_active": true
}Method 2: Create via frontend interface
- Login to the management interface
- Go to "API Key Management" page
- Click "Create API Key"
- Fill in name and email (optional)
- Copy the generated API Key (Note: Cannot view the full key after creation)
- Configure Claude Code Environment Variables:
# Backend only (assuming backend port is 8000)
export ANTHROPIC_BASE_URL=http://localhost:8000
# When both frontend and backend are running, you can also access via frontend proxy (e.g., port 5173)
export ANTHROPIC_BASE_URL=http://localhost:5173
# API Key: in development mode, can be any value; in production, use the created valid key
export ANTHROPIC_API_KEY="sk-xxxxxxxxxxxxx"
# Claude Code model configuration: haiku (small), sonnet (middle), opus (big)
# These correspond to the small, middle, big model tiers in provider.json
# For example:
# export ANTHROPIC_MODEL="sonnet"
# export ANTHROPIC_SMALL_FAST_MODEL="haiku"
# export ANTHROPIC_DEFAULT_SONNET_MODEL="sonnet"
# export ANTHROPIC_DEFAULT_OPUS_MODEL="opus"
# export ANTHROPIC_DEFAULT_HAIKU_MODEL="haiku"The system supports multiple OAuth providers for social login. Configure the corresponding environment variables to enable:
# GitHub OAuth
export GITHUB_CLIENT_ID="your-github-client-id"
export GITHUB_CLIENT_SECRET="your-github-client-secret"
# Google OAuth
export GOOGLE_CLIENT_ID="your-google-client-id"
export GOOGLE_CLIENT_SECRET="your-google-client-secret"
# Feishu OAuth (Lark)
export FEISHU_CLIENT_ID="your-feishu-client-id"
export FEISHU_CLIENT_SECRET="your-feishu-client-secret"
# Microsoft OAuth (Azure AD)
export MICROSOFT_CLIENT_ID="your-microsoft-client-id"
export MICROSOFT_CLIENT_SECRET="your-microsoft-client-secret"
export MICROSOFT_TENANT_ID="common" # or specific tenant ID
# Generic OIDC (supports Logto, Keycloak, Authentik, etc.)
export OIDC_CLIENT_ID="your-oidc-client-id"
export OIDC_CLIENT_SECRET="your-oidc-client-secret"
export OIDC_AUTHORIZATION_URL="https://your-oidc-server/oauth/authorize"
export OIDC_TOKEN_URL="https://your-oidc-server/oauth/token"After configuration, the login page will display the corresponding OAuth login buttons.
# Access backend directly (http://localhost:8000/v1/messages)
# Or via frontend proxy (http://localhost:5173/v1/messages)
curl -X POST http://localhost:8000/v1/messages \
-H "Content-Type: application/json" \
-H "X-API-Key: sk-xxxxxxxxxxxxx" \
-d '{
"model": "haiku",
"messages": [{"role": "user", "content": "Hello!"}],
"max_tokens": 100
}'# Access backend directly (http://localhost:8000/v1/messages)
# Or via frontend proxy (http://localhost:5173/v1/messages)
curl -X POST http://localhost:8000/v1/messages \
-H "Content-Type: application/json" \
-H "X-API-Key: sk-xxxxxxxxxxxxx" \
-d '{
"model": "sonnet",
"messages": [{"role": "user", "content": "Tell me a story"}],
"max_tokens": 200,
"stream": true
}'# Access backend directly (http://localhost:8000/v1/messages)
# Or via frontend proxy (http://localhost:5173/v1/messages)
curl -X POST http://localhost:8000/v1/messages \
-H "Content-Type: application/json" \
-H "X-API-Key: sk-xxxxxxxxxxxxx" \
-d '{
"model": "opus",
"messages": [{"role": "user", "content": "What'"'"'s the weather in Beijing today?"}],
"max_tokens": 200,
"tools": [{
"name": "get_weather",
"description": "Get weather information for a specified city",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
}
},
"required": ["location"]
}
}]
}'# Access backend directly (http://localhost:8000/health)
# Or via frontend proxy (http://localhost:5173/health)
curl http://localhost:8000/health# Access backend directly (http://localhost:8000/api/stats/token-usage)
# Or via frontend proxy (http://localhost:5173/api/stats/token-usage)
curl -H "Authorization: Bearer <your-jwt-token>" \
http://localhost:8000/api/stats/token-usage# Access backend directly (http://localhost:8000/api/stats/requests)
# Or via frontend proxy (http://localhost:5173/api/stats/requests)
curl -H "Authorization: Bearer <your-jwt-token>" \
http://localhost:8000/api/stats/requests- ๐ง Development Guide - Detailed development guide, architecture documentation, API reference
- ๐ Complete Technical Documentation - Comprehensive technical documentation
- ๐ Interactive API Documentation - Complete interactive API documentation
- ๐ฎ Online Demo - (Coming Soon)
- ๐ Issue Tracker - Report bugs and request features
- ๐ ไธญๆๅฟซ้ๆๅ - ไธญๆ็่ฏดๆๆๆกฃ
- ๐ ไธญๆๆๆฏๆๆกฃ - ๅฎๆดๆๆฏๆๆกฃ
- OAuth Multi-Provider Support: Added GitHub, Google, Feishu, Microsoft, OIDC OAuth login methods
- User Management System: Complete user CRUD with pagination, search, enable/disable
- Fine-Grained Permission Control: 9 permission points (chat, conversations, preferences, providers, api_keys, stats, health, config, users)
- Per-User Permission Configuration: Support configuring permissions per user, admins have all permissions
- Frontend Route Protection: Unauthorized users are automatically redirected when accessing restricted pages
- Enhanced API Key Management: Secure storage, one-time full key display, one-click copy
- 16 Language Support Added: Chinese, English, ๆฅๆฌ่ช, ํ๊ตญ์ด, Franรงais, Espaรฑol, Deutsch, ะ ัััะบะธะน, Portuguรชs, Italiano, Nederlands, ุงูุนุฑุจูุฉ, เคนเคฟเคจเฅเคฆเฅ, เนเธเธข, Tiแบฟng Viแปt, Bahasa Indonesia
- Smart Language Switching: One-click language switching in top navigation bar, automatically remembers user preferences
- Full Localization: All pages, forms, buttons, messages, Toast notifications completely translated
- Fixed Chat Timestamps: Resolved "Invalid Date" issue, supports multiple time formats
- Svelte 5 Upgrade: Fully upgraded to Svelte 5 syntax, using
$state()and$derived()features
We welcome all forms of contribution! Please read the "Contributing Guidelines" section in ๐ Development Guide for detailed information.
If this project is helpful to you, please consider supporting our development efforts!
Your support helps us:
- ๐ Continuously develop and optimize features
- ๐ Quickly fix issues
- ๐ Improve documentation and examples
- ๐ Add more language support
- โ Keep developers motivated
This project is licensed under the MIT License.
Thanks to all developers who have contributed to this project!
Documentation | Development Guide | Issue Tracker | Changelog
Made with โค๏ธ by the CCS Team




