Files
opencode2api/README.md
T

220 lines
8.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# opencode2api
`opencode2api` 是一个使用 Go 编写的 OpenCode Zen / Zen Go 协议代理。它对外提供标准 OpenAI 与 Anthropic API,并自动添加 OpenCode 客户端请求头。
主要功能:
- 支持 OpenAI Chat Completions、Responses 和 Models API
- 支持 Anthropic Messages API
- 支持普通响应和 SSE 流式响应
- 支持文本、图片、thinking/reasoning、工具定义、工具调用和工具结果转换
- 分离配置 Zen key 池与 Zen Go key 池
- 模型同时存在于两个上游时按 `prefer` 配置优先使用 Go 或 Zen(默认 Go)
- 支持直连、HTTP、HTTPS、SOCKS5 和 SOCKS5H 代理
- 将 key 自动均衡绑定到代理,保持连接亲和性
- 使用稳定会话哈希保持同一会话的 key/proxy 亲和性,并在节点故障时自动回退
- 代理失败后自动迁移绑定,key 失败后进行短时冷却
- 根据真实上游流量识别代理故障,并每 15 分钟通过 Cloudflare trace 并行复查异常代理
- 为不同会话生成不同的 OpenCode 会话 ID,并支持 `x-opencode-session`、`x-session-id` 和 `conversation-id` 显式指定会话
## API 路径
| 方法 | 路径 | 协议 |
| --- | --- | --- |
| `GET` | `/v1/models` | OpenAI 模型列表 |
| `POST` | `/v1/chat/completions` | OpenAI Chat Completions |
| `POST` | `/v1/responses` | OpenAI Responses |
| `POST` | `/v1/messages` | Anthropic Messages |
| `GET` | `/healthz` | 健康检查 |
`/healthz` 无需 API key,返回服务版本以及模型目录、Zen/Go key 和代理池的汇总状态,不会暴露 key 或代理地址。模型目录尚未完成首次刷新、已经过期、没有可暴露模型或没有健康代理时返回 HTTP `503`;其余情况返回 `200`。
模型目录的过期阈值为 `models.refresh_seconds` 的两倍,且不低于 60 秒。刚启动时短暂返回 `503 starting` 属于正常现象,模型列表首次刷新成功后会变为 `200 ok`。
## 编译
需要 Go 1.24 或更高版本。
```bash
go build -o opencode2api ./
```
## Docker Compose 部署
服务器安装 Docker 与 Docker Compose 后,可以在克隆项目后直接启动:
```bash
git clone https://github.com/jasonxu114514/opencode2api.git
cd opencode2api
docker compose up -d
```
```bash
cp config.example.json config.json
# 编辑 config.json 中的 server_keys、zen_keys 或 go_keys
docker compose restart
```
```bash
curl http://127.0.0.1:8080/healthz
docker compose logs -f
```
如需修改宿主机端口:
```bash
OPENCODE2API_PORT=18080 docker compose up -d
```
## 配置
复制示例配置:
```bash
cp config.example.json config.json
```
然后编辑 `config.json`:
```json
{
"listen": "127.0.0.1:8080",
"server_keys": ["change-this-local-key"],
"zen_keys": ["sk-your-zen-key"],
"go_keys": [],
"prefer": "go",
"proxies": ["direct"],
"upstream": {
"zen": "https://opencode.ai/zen",
"go": "https://opencode.ai/zen/go"
},
"retry": {
"max_attempts": 3,
"timeout_seconds": 300
},
"models": {
"refresh_seconds": 300,
"protocols": {}
},
"performance": {
"max_idle_conns": 2048,
"max_idle_conns_per_host": 256,
"max_conns_per_host": 0,
"idle_conn_timeout_seconds": 120,
"connect_timeout_seconds": 5,
"failure_cooldown_seconds": 15
},
"logging": {
"level": "info"
}
}
```
### 基础字段
| 字段 | 含义 |
| --- | --- |
| `listen` | 本地监听地址。默认建议使用 `127.0.0.1:8080`,避免服务直接暴露到公网。 |
| `server_keys` | 调用本代理时使用的本地 API key 列表。它们只用于本地鉴权,不会发送给 OpenCode。 |
| `zen_keys` | OpenCode Zen API key 池。允许配置多个 key。 |
| `go_keys` | OpenCode Zen Go API key 池。没有 Go key 时可以使用空数组。 |
| `prefer` | 模型同时存在于 Zen 与 Go 时优先使用的上游,值为 `go` 或 `zen`,默认 `go`。仅存在于某一池时不受影响。 |
| `proxies` | 上游代理列表。支持 `direct`、`http://`、`https://`、`socks5://` 和 `socks5h://`。URL 可以包含代理用户名和密码。 |
`server_keys` 至少需要一个值;`zen_keys` 和 `go_keys` 至少有一个池不能为空。
### key 与代理分配规则
只需要直连时使用:
```json
"proxies": ["direct"]
```
SOCKS5 代理示例:
```json
"proxies": ["socks5://127.0.0.1:1080"]
```
多个代理示例:
```json
"proxies": [
"http://user:password@127.0.0.1:7890",
"socks5://127.0.0.1:1080"
]
```
### `upstream`
| 字段 | 含义 |
| --- | --- |
| `upstream.zen` | Zen 上游根地址,通常保持为 `https://opencode.ai/zen`。 |
| `upstream.go` | Zen Go 上游根地址,通常保持为 `https://opencode.ai/zen/go`。 |
### `retry`
| 字段 | 含义 |
| --- | --- |
| `retry.max_attempts` | 单个请求的最大尝试次数,包含第一次请求。网络错误、认证失败、限流和 5xx 会切换节点;其他 4xx 属于确定性的请求错误,会直接返回而不轮换 key。 |
| `retry.timeout_seconds` | 单个客户端请求的总超时时间,同时用于限制上游响应头等待时间。 |
流式响应一旦已经向客户端输出数据,就不会切换节点重新生成,避免拼接两个不同的响应。
### `models`
| 字段 | 含义 |
| --- | --- |
| `models.refresh_seconds` | 重新读取 Zen 和 Go 模型列表的间隔秒数。两个列表会并发刷新。 |
| `models.protocols` | 手动指定模型的原生协议。值只能是 `chat`、`responses` 或 `anthropic`。通常保持为空。 |
模型协议覆盖示例:
```json
"protocols": {
"custom-model": "chat"
}
```
模型同时存在于 Zen 与 Go 时按 `prefer` 配置选择:值为 `go` 时优先 Go,值为 `zen` 时优先 Zen(默认 `go`)。仅存在于某一池时才使用该池的 key。
### Thinking 工具历史兼容
所有请求都会经过同一个上游请求准备流程,同协议转发和跨协议转换不再使用两套分支。通过 Chat Completions 或 Anthropic Messages API 调用 DeepSeek、Kimi/Moonshot 或 MiMo 模型时,代理会按上游的目标协议规范化 assistant 工具历史:Chat 补全缺失或空的 `reasoning_content`;Anthropic 保留有效 thinking 文本、为缺失或空的 thinking 补充兼容占位内容、将 `redacted_thinking` 转为普通 thinking,并移除这些兼容端点不接受的 `signature`。显式启用 reasoning/thinking 的别名模型也会启用该处理,普通非 reasoning 请求不会被修改。
### `performance`
| 字段 | 含义 |
| --- | --- |
| `performance.max_idle_conns` | 所有上游连接池允许保留的最大空闲连接数。 |
| `performance.max_idle_conns_per_host` | 每个上游主机允许保留的最大空闲连接数。 |
| `performance.max_conns_per_host` | 每个主机的最大并发连接数。`0` 表示不设置上限。 |
| `performance.idle_conn_timeout_seconds` | 空闲连接在连接池中保留的时间。 |
| `performance.connect_timeout_seconds` | 与上游或代理建立 TCP 连接的超时时间。 |
| `performance.failure_cooldown_seconds` | 连接失败、认证失败、限流或 5xx 后节点的基础冷却时间。连续失败会指数增加冷却时间。 |
### `logging`
| 字段 | 含义 |
| --- | --- |
| `logging.level` | 日志级别,支持 `info` 和 `debug`。生产环境建议使用 `info`。 |
日志不会输出完整上游 key、本地 key、Authorization、`x-api-key` 或请求消息正文。
## 会话 ID
代理会为上游添加 OpenCode 使用的 `User-Agent`、`x-opencode-client`、`x-opencode-session`、`x-opencode-request` 和 `x-opencode-project` 请求头。
- 每个请求使用不同的 `x-opencode-request`,同一次请求的重试保持不变。
- 优先使用客户端提供的 `x-opencode-session`、`x-session-id`、`conversation-id`、`conversation_id` 或 `metadata.session_id` 生成会话 ID。
- 没有显式会话标识时,使用第一条用户消息生成稳定会话 ID,使同一段多轮对话保持一致。
- 如果两个独立会话的第一条消息完全相同,建议由客户端发送不同的 `x-session-id`,以确保两个会话严格分离。
## 致谢
感谢 [LINUX DO](https://linux.do) 社区一直以来的支持。