English | 简体中文
SonnetForAPI 是一个按计划书重写的 Go 代理服务,用来把 Cursor 文档页聊天接口包装成两套对外协议:
- OpenAI
POST /v1/chat/completions - Anthropic
POST /v1/messages
它的定位是一个新的双协议桥接层:统一配置、统一鉴权、统一 Cursor 上游客户端,同时保留 *-thinking 模型暴露方式和工具调用桥接能力。
- 支持
claude-sonnet-4.6 - 自动暴露
claude-sonnet-4.6-thinking - 支持 Bearer Token 与
x-api-key - 支持 OpenAI 流式输出
- 支持 Anthropic SSE 事件输出
- 支持 tool bridge 与 thinking 事件透传
- 支持
stream_options.include_usage - 支持
config.yaml热重载 - 支持非流式拒答重试
- 支持早期上下文压缩
- 支持可选图像预处理(vision)
- Go 1.24+
- Node.js 18+
Node.js 仅用于需要执行 x-is-human JS 兜底逻辑时的运行时支持。
cp config.yaml.example config.yaml如果你更习惯环境变量方式,也可以从 .env.example 复制一份 .env 再按需覆盖。
默认配置会监听 http://localhost:3010,默认密钥为:
sk-sonnetforapi
Linux / macOS:
chmod +x start.sh
./start.shWindows:
start-go.bat或者直接:
go mod tidy
go run .curl http://localhost:3010/v1/chat/completions \
-H "Authorization: Bearer sk-sonnetforapi" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4.6-thinking",
"stream": true,
"stream_options": { "include_usage": true },
"messages": [
{ "role": "user", "content": "Summarize this service." }
]
}'curl http://localhost:3010/v1/messages \
-H "x-api-key: sk-sonnetforapi" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4.6",
"max_tokens": 1024,
"stream": true,
"messages": [
{ "role": "user", "content": "Explain the route layout." }
]
}'curl http://localhost:3010/v1/models \
-H "Authorization: Bearer sk-sonnetforapi"docker build -t sonnetforapi .
docker run --rm -p 3010:3010 sonnetforapi或:
docker-compose up -d主要配置位于 config.yaml:
api_keys: 允许访问的密钥列表cursor.model: Cursor 上游模型,默认anthropic/claude-sonnet-4.6x_is_human.enabled: 是否启用 JS token 兜底streaming.keepalive_seconds: SSE 心跳间隔thinking.enabled: 是否允许 thinking 模型注入
同时也支持环境变量覆盖:
API_KEY/API_KEYSPORTCURSOR_MODELTIMEOUTMAX_HISTORY_MESSAGESMAX_AUTO_CONTINUEPROXYSTREAM_KEEPALIVE_SECONDSSTREAM_IDLE_TIMEOUTSTREAMER_WARMUP_CHARSSTREAMER_GUARD_CHARSX_IS_HUMAN_ENABLEDREJECTION_ENABLEDREJECTION_MAX_RETRIESCOMPRESSION_ENABLEDCOMPRESSION_LEVELCOMPRESSION_KEEP_RECENTCOMPRESSION_EARLY_MSG_MAX_CHARSVISION_ENABLEDVISION_MODEVISION_BASE_URLVISION_API_KEYVISION_MODELTOOLS_SCHEMA_MODETOOLS_DESCRIPTION_MAX_LENGTHTOOLS_INCLUDE_ONLYTOOLS_EXCLUDE
首页文档页已经重写为白底、浅蓝、浅黄、浅粉的轻淡主题。
go test ./...