翻译代理
翻译代理是 Claudex 的核心。它位于 Claude Code 和 AI 提供商之间,透明地在 Anthropic Messages API 和 OpenAI Chat Completions API(或 Responses API)之间进行转换。
Claude Code → Anthropic Messages API request │ └── Claudex Proxy (127.0.0.1:13456) │ ├── DirectAnthropic provider → forward with headers │ ├── OpenAICompatible provider │ ├── Translate request: Anthropic → OpenAI Chat Completions │ ├── Apply query_params, strip_params, custom_headers │ ├── Forward to provider │ └── Translate response: OpenAI → Anthropic │ └── OpenAIResponses provider ├── Translate request: Anthropic → OpenAI Responses API ├── Forward to provider └── Translate response: Responses → Anthropic提供商适配器
Section titled “提供商适配器”Claudex 使用 ProviderAdapter trait 处理不同提供商 API 之间的差异。已实现三种适配器:
| 适配器 | 翻译方式 | 使用者 |
|---|---|---|
| DirectAnthropic | 无(直接透传) | Anthropic、MiniMax、Vertex AI |
| ChatCompletions | 完整的 Anthropic 与 OpenAI 双向翻译 | Grok、OpenAI、DeepSeek、Kimi、GLM、OpenRouter、Groq、Mistral、Together AI、Perplexity、Cerebras、Azure OpenAI、GitHub Copilot、GitLab Duo、Ollama、vLLM、LM Studio |
| Responses | Anthropic 与 OpenAI Responses API 翻译 | ChatGPT/Codex 订阅 |
请求翻译(Anthropic → OpenAI)
Section titled “请求翻译(Anthropic → OpenAI)”| Anthropic | OpenAI |
|---|---|
system 字段 | messages 数组中的 system message |
messages[].content 块(text、image、tool_use、tool_result) | messages[].content + tool_calls |
tools 数组(JSON Schema,input_schema) | tools 数组(function 格式,parameters) |
tool_choice(auto、any、{name}) | tool_choice(auto、required、{function: {name}}) |
max_tokens | max_tokens(如果 profile 设置了 max_tokens 则取上限) |
temperature、top_p | 直接映射(如果 strip_params 匹配则剥离) |
响应翻译(OpenAI → Anthropic)
Section titled “响应翻译(OpenAI → Anthropic)”| OpenAI | Anthropic |
|---|---|
choices[0].message.content | content 块(type: text) |
choices[0].message.tool_calls | content 块(type: tool_use) |
finish_reason: stop | stop_reason: end_turn |
finish_reason: tool_calls | stop_reason: tool_use |
usage.prompt_tokens / completion_tokens | usage.input_tokens / output_tokens |
工具名称兼容
Section titled “工具名称兼容”Claude Code 可能生成超过 64 字符的工具名称(例如 mcp__server-name__very-long-tool-name-that-exceeds-the-limit)。OpenAI 及许多提供商强制执行 64 字符限制。
Claudex 自动:
- 在发出的请求中截断超过 64 字符的名称
- 构建截断名到原始名的映射表
- 在提供商响应中恢复原始名称
这一往返过程完全透明。
Claudex 完全支持 SSE(Server-Sent Events)流式传输,将 OpenAI 流块实时翻译为 Anthropic 流事件:
| OpenAI SSE | Anthropic SSE |
|---|---|
| 首个 chunk | message_start + content_block_start |
choices[0].delta.content | content_block_delta(text_delta) |
choices[0].delta.tool_calls | content_block_delta(input_json_delta) |
finish_reason 出现 | content_block_stop + message_delta + message_stop |
流式翻译器维护一个状态机,正确处理 tool call 的累积和内容块边界。
Azure OpenAI 支持
Section titled “Azure OpenAI 支持”Azure OpenAI 使用不同的认证和 URL 方案:
- 认证:使用
api-key请求头取代Authorization: Bearer - URL 格式:
https://{resource}.openai.azure.com/openai/deployments/{deployment} - API 版本:通过
query_params必填
Claudex 通过检查 base_url 是否包含 openai.azure.com 自动检测 Azure 并相应调整认证方式。
支持的提供商
Section titled “支持的提供商”| 提供商 | 类型 | Base URL |
|---|---|---|
| Anthropic | DirectAnthropic | https://api.anthropic.com |
| MiniMax | DirectAnthropic | https://api.minimax.io/anthropic |
| Google Vertex AI | DirectAnthropic | https://REGION-aiplatform.googleapis.com/v1/projects/... |
| OpenRouter | OpenAICompatible | https://openrouter.ai/api/v1 |
| Grok (xAI) | OpenAICompatible | https://api.x.ai/v1 |
| OpenAI | OpenAICompatible | https://api.openai.com/v1 |
| DeepSeek | OpenAICompatible | https://api.deepseek.com |
| Kimi/Moonshot | OpenAICompatible | https://api.moonshot.ai/v1 |
| GLM (智谱) | OpenAICompatible | https://api.z.ai/api/paas/v4 |
| Groq | OpenAICompatible | https://api.groq.com/openai/v1 |
| Mistral AI | OpenAICompatible | https://api.mistral.ai/v1 |
| Together AI | OpenAICompatible | https://api.together.xyz/v1 |
| Perplexity | OpenAICompatible | https://api.perplexity.ai |
| Cerebras | OpenAICompatible | https://api.cerebras.ai/v1 |
| Azure OpenAI | OpenAICompatible | https://{resource}.openai.azure.com/... |
| GitHub Copilot | OpenAICompatible | https://api.githubcopilot.com |
| GitLab Duo | OpenAICompatible | https://gitlab.com/api/v4/ai/llm/proxy |
| Ollama | OpenAICompatible | http://localhost:11434/v1 |
| vLLM | OpenAICompatible | http://localhost:8000/v1 |
| LM Studio | OpenAICompatible | http://localhost:1234/v1 |
| ChatGPT/Codex 订阅 | OpenAIResponses | https://chatgpt.com/backend-api/codex |
Models 端点
Section titled “Models 端点”代理暴露 /v1/models 端点,列出所有已启用的 profile。每个条目包含自定义字段:
x-claudex-profile:profile 名称x-claudex-provider:提供商类型(anthropic、openai-compatible、openai-responses)
Claude Code 查询此端点以发现可用模型。
# 以守护进程方式启动代理claudex proxy start -d
# 查看代理状态claudex proxy status
# 停止代理守护进程claudex proxy stop
# 使用自定义端口claudex proxy start -p 8080运行 claudex run <profile> 时,如果代理未运行,会自动在后台启动。