Claude Code Proxy - translate Claude API requests to OpenAI-compatible API calls
moonbit_claude_proxy/
├── moon.mod.json # 模块配置 (依赖 moonbitlang/async 0.19.0)
├── moon.pkg # 根包导入配置
├── constants.mbt # API 常量定义 (角色、内容类型、SSE 事件等)
├── config.mbt # 环境变量配置管理
├── model_manager.mbt # Claude → OpenAI 模型映射
├── models.mbt # 数据模型 (Claude/OpenAI API 类型及 JSON 解析)
├── request_converter.mbt # Claude → OpenAI 请求格式转换
├── response_converter.mbt # OpenAI → Claude 响应转换 + SSE 流式处理
├── openai_client.mbt # 异步 HTTP 客户端 (OpenAI API 调用)
├── server.mbt # HTTP 服务器 (路由、端点、请求处理)
└── cmd/main/
├── moon.pkg # 可执行包配置 (native 目标)
└── main.mbt # 入口点| 环境变量 | 说明 |
|---|---|
| OPENAI_API_KEY | OpenAI API 密钥 |
| 环境变量 | 说明 | 默认值 |
|---|---|---|
| BIG_MODEL | opus 请求使用的模型 | gpt-4o |
| MIDDLE_MODEL | sonnet 请求使用的模型 | 同 BIG_MODEL |
| SMALL_MODEL | haiku 请求使用的模型 | gpt-4o-mini |
| 环境变量 | 说明 | 默认值 |
|---|---|---|
| OPENAI_BASE_URL | API 基础 URL | https://api.openai.com/v1 |
| ANTHROPIC_API_KEY | 客户端验证密钥 (可选) | 未设置 |
| HOST | 服务器主机 | 0.0.0.0 |
| PORT | 服务器端口 | 8082 |
| 环境变量 | 说明 | 默认值 |
|---|---|---|
| MAX_TOKENS_LIMIT | 最大 tokens 限制 | 4096 |
| MIN_TOKENS_LIMIT | 最小 tokens 限制 | 100 |
| REQUEST_TIMEOUT | 请求超时 (秒) | 90 |
curl -fsSL https://cli.moonbitlang.com/install.sh | bashexport OPENAI_API_KEY="sk-your-api-key"
export BIG_MODEL="gpt-4o"
export SMALL_MODEL="gpt-4o-mini"cd moonbit_claude_proxy
moon run cmd/mainANTHROPIC_BASE_URL=http://localhost:8082 claude| Claude 模型 | 映射到 | 环境变量 |
|---|---|---|
| 包含 "haiku" | SMALL_MODEL | 默认: gpt-4o-mini |
| 包含 "sonnet" | MIDDLE_MODEL | 默认: gpt-4o |
| 包含 "opus" | BIG_MODEL | 默认: gpt-4o |
export OPENAI_API_KEY="sk-your-openai-key"
export OPENAI_BASE_URL="https://api.openai.com/v1"
export BIG_MODEL="gpt-4o"
export SMALL_MODEL="gpt-4o-mini"export OPENAI_API_KEY="your-azure-key"
export OPENAI_BASE_URL="https://your-resource.openai.azure.com/openai/deployments/your-deployment"
export BIG_MODEL="gpt-4"
export SMALL_MODEL="gpt-35-turbo"export OPENAI_API_KEY="dummy-key"
export OPENAI_BASE_URL="http://localhost:11434/v1"
export BIG_MODEL="llama3.1:70b"
export SMALL_MODEL="llama3.1:8b"Claude Code 客户端
↓
[HTTP 请求]
↓
API 端点验证 (可选密钥校验)
↓
请求转换器 (Claude → OpenAI 格式)
↓
模型映射 (根据模型名称选择目标)
↓
OpenAI HTTP 客户端 (异步)
↓
OpenAI API 服务器
↓
响应转换器 (OpenAI → Claude 格式)
↓
[HTTP 响应 / SSE 流]
↓
Claude Code 客户端moon test --target native convert_test.mbtmoon test --target native integration_test.mbtmoon test --target native# 类型检查
moon check --target native
# 格式化代码
moon fmt
# 直接运行
moon run cmd/main
# 查看帮助
moon run cmd/main -- --help# Debug 构建(快速,包含调试信息)
moon build --target native
# Release 构建(优化,体积更小)
moon build --target native --release| 模式 | 路径 |
|---|---|
| Debug | _build/native/debug/build/cmd/main/main.exe |
| Release | _build/native/release/build/cmd/main/main.exe |
./_build/native/debug/build/cmd/main/main.exe注意: MoonBit 目前不支持交叉编译,Windows/Linux/macOS 需在各自平台分别编译。
pub(all) struct ClaudeMessagesRequest {
model : String
max_tokens : Int
messages : Array[ClaudeMessage]
system : Json?
stop_sequences : Array[String]
stream : Bool
temperature : Double
top_p : Double?
tools : Array[ClaudeTool]
tool_choice : Map[String, Json]?
} derive(Debug)pub(all) struct ClaudeTokenCountRequest {
model : String
messages : Array[ClaudeMessage]
system : Json?
tools : Array[ClaudeTool]
tool_choice : Map[String, Json]?
} derive(Debug)pub(all) struct Config {
openai_api_key : String
anthropic_api_key : String?
openai_base_url : String
big_model : String
middle_model : String
small_model : String
host : String
port : Int
max_tokens_limit : Int
min_tokens_limit : Int
request_timeout : Int
} derive(Debug)pub(all) struct OpenAIClient {
base_url : String
api_key : String
custom_headers : Map[String, String]
}async fn OpenAIClient::chat_completion(self : OpenAIClient, request_body : Map[String, Json]) -> Map[String, Json]async fn OpenAIClient::chat_completion_stream(self : OpenAIClient, request_body : Map[String, Json]) -> Array[String]async fn load_env(path? : String) -> Unitfn sse_message_delta(stop_reason : String, input_tokens : Int, output_tokens : Int) -> StringInstall
Download zipClaude Code Proxy - translate Claude API requests to OpenAI-compatible API calls