david-baxter commited on
Commit
91feb7c
·
verified ·
1 Parent(s): 3434d68

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +398 -1
README.md CHANGED
@@ -5,6 +5,403 @@ colorFrom: green
5
  colorTo: blue
6
  sdk: docker
7
  pinned: false
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  colorTo: blue
6
  sdk: docker
7
  pinned: false
8
+ app_port: 8002
9
  ---
10
 
11
+ # Cursor2API
12
+
13
+ [English](README_EN.md) | 简体中文
14
+
15
+ 一个将 Cursor Web 转换为 OpenAI `chat/completions` 兼容 API 的 Go 服务。
16
+
17
+ [![Go Version](https://img.shields.io/badge/Go-1.24+-blue.svg)](https://golang.org)
18
+ [![License: PolyForm Noncommercial](https://img.shields.io/badge/License-PolyForm%20Noncommercial-orange.svg)](https://polyformproject.org/licenses/noncommercial/1.0.0/)
19
+
20
+ ## ✨ 特性
21
+
22
+ - 🔄 **API 兼容**: 兼容 OpenAI `chat/completions` 接口
23
+ - ⚡ **高性能**: 低延迟响应
24
+ - 🔐 **安全认证**: 支持 API Key 认证
25
+ - 🌐 **模型派生**: 自动暴露 `*-thinking` 模型
26
+ - 🧰 **工具调用**: 支持 `tools` / `tool_choice` / `tool_calls`
27
+ - 🛡️ **错误处理**: 完善的错误处理机制
28
+ - 📊 **健康检查**: 内置健康检查接口
29
+
30
+ ## 🖼️ 效果图
31
+
32
+ ![首页预览](docs/images/home.png)
33
+ ![调用效果预览 1](docs/images/play1.png)
34
+ ![调用效果预览 2](docs/images/play2.png)
35
+
36
+ ## 🤖 支持的模型
37
+
38
+ - **Anthropic Claude**: `claude-sonnet-4.6`
39
+ - **自动派生 thinking 模型**: `claude-sonnet-4.6-thinking`
40
+
41
+ ## 🚀 快速开始
42
+
43
+ ### 环境要求
44
+
45
+ - Go 1.24+
46
+ - Node.js 18+ (用于 JavaScript 执行)
47
+
48
+ ### 本地运行方式
49
+
50
+ #### 方法一:直接运行(推荐用于开发)
51
+
52
+ **Linux/macOS**:
53
+ ```bash
54
+ git clone https://github.com/libaxuan/cursor2api-go.git
55
+ cd cursor2api-go
56
+ chmod +x start.sh
57
+ ./start.sh
58
+ ```
59
+
60
+ **Windows**:
61
+ ```batch
62
+ # 双击运行或在 cmd 中执行
63
+ start-go.bat
64
+
65
+ # 或在 Git Bash / Windows Terminal 中
66
+ ./start-go-utf8.bat
67
+ ```
68
+
69
+ #### 方法二:手动编译运行
70
+
71
+ ```bash
72
+ # 克隆项目
73
+ git clone https://github.com/libaxuan/cursor2api-go.git
74
+ cd cursor2api-go
75
+
76
+ # 下载依赖
77
+ go mod tidy
78
+
79
+ # 编译
80
+ go build -o cursor2api-go
81
+
82
+ # 运行
83
+ ./cursor2api-go
84
+ ```
85
+
86
+ #### 方法三:使用 go run
87
+
88
+ ```bash
89
+ git clone https://github.com/libaxuan/cursor2api-go.git
90
+ cd cursor2api-go
91
+ go run main.go
92
+ ```
93
+
94
+ 服务将在 `http://localhost:8002` 启动
95
+
96
+ ## 🚀 服务器部署方式
97
+
98
+ ### Docker 部署
99
+
100
+ 1. **构建镜像**:
101
+ ```bash
102
+ # 构建镜像
103
+ docker build -t cursor2api-go .
104
+ ```
105
+
106
+ 2. **运行容器**:
107
+ ```bash
108
+ # 运行容器(推荐)
109
+ docker run -d \
110
+ --name cursor2api-go \
111
+ --restart unless-stopped \
112
+ -p 8002:8002 \
113
+ -e API_KEY=your-secret-key \
114
+ -e DEBUG=false \
115
+ cursor2api-go
116
+
117
+ # 或者使用默认配置运行
118
+ docker run -d --name cursor2api-go --restart unless-stopped -p 8002:8002 cursor2api-go
119
+ ```
120
+
121
+ ### Docker Compose 部署(推荐用于生产环境)
122
+
123
+ 1. **使用 docker-compose.yml**:
124
+ ```bash
125
+ # 启动服务
126
+ docker-compose up -d
127
+
128
+ # 停止服务
129
+ docker-compose down
130
+
131
+ # 查看日志
132
+ docker-compose logs -f
133
+ ```
134
+
135
+ 2. **自定义配置**:
136
+ 修改 `docker-compose.yml` 文件中的环境变量以满足您的需求:
137
+ - 修改 `API_KEY` 为安全的密钥
138
+ - 根据需要调整 `MODELS`、`TIMEOUT` 等配置
139
+ - 更改暴露的端口
140
+
141
+ ### 系统服务部署(Linux)
142
+
143
+ 1. **编译并移动二进制文件**:
144
+ ```bash
145
+ go build -o cursor2api-go
146
+ sudo mv cursor2api-go /usr/local/bin/
147
+ sudo chmod +x /usr/local/bin/cursor2api-go
148
+ ```
149
+
150
+ 2. **创建系统服务文件** `/etc/systemd/system/cursor2api-go.service`:
151
+ ```ini
152
+ [Unit]
153
+ Description=Cursor2API Service
154
+ After=network.target
155
+
156
+ [Service]
157
+ Type=simple
158
+ User=your-user
159
+ WorkingDirectory=/home/your-user/cursor2api-go
160
+ ExecStart=/usr/local/bin/cursor2api-go
161
+ Restart=always
162
+ Environment=API_KEY=your-secret-key
163
+ Environment=PORT=8002
164
+
165
+ [Install]
166
+ WantedBy=multi-user.target
167
+ ```
168
+
169
+ 3. **启动服务**:
170
+ ```bash
171
+ # 重载 systemd 配置
172
+ sudo systemctl daemon-reload
173
+
174
+ # 启用开机自启
175
+ sudo systemctl enable cursor2api-go
176
+
177
+ # 启动服务
178
+ sudo systemctl start cursor2api-go
179
+
180
+ # 查看状态
181
+ sudo systemctl status cursor2api-go
182
+ ```
183
+
184
+ ## 📡 API 使用
185
+
186
+ ### 获取模型列表
187
+
188
+ ```bash
189
+ curl -H "Authorization: Bearer 0000" http://localhost:8002/v1/models
190
+ ```
191
+
192
+ ### 非流式聊天
193
+
194
+ ```bash
195
+ curl -X POST http://localhost:8002/v1/chat/completions \
196
+ -H "Content-Type: application/json" \
197
+ -H "Authorization: Bearer 0000" \
198
+ -d '{
199
+ "model": "claude-sonnet-4.6",
200
+ "messages": [{"role": "user", "content": "Hello!"}],
201
+ "stream": false
202
+ }'
203
+ ```
204
+
205
+ ### 流式聊天
206
+
207
+ ```bash
208
+ curl -X POST http://localhost:8002/v1/chat/completions \
209
+ -H "Content-Type: application/json" \
210
+ -H "Authorization: Bearer 0000" \
211
+ -d '{
212
+ "model": "claude-sonnet-4.6",
213
+ "messages": [{"role": "user", "content": "Hello!"}],
214
+ "stream": true
215
+ }'
216
+ ```
217
+
218
+ ### 带工具的请求
219
+
220
+ ```bash
221
+ curl -X POST http://localhost:8002/v1/chat/completions \
222
+ -H "Content-Type: application/json" \
223
+ -H "Authorization: Bearer 0000" \
224
+ -d '{
225
+ "model": "claude-sonnet-4.6",
226
+ "messages": [{"role": "user", "content": "帮我查询北京天气"}],
227
+ "tools": [
228
+ {
229
+ "type": "function",
230
+ "function": {
231
+ "name": "get_weather",
232
+ "description": "获取实时天气",
233
+ "parameters": {
234
+ "type": "object",
235
+ "properties": {
236
+ "city": {"type": "string"}
237
+ },
238
+ "required": ["city"]
239
+ }
240
+ }
241
+ }
242
+ ]
243
+ }'
244
+ ```
245
+
246
+ ### `-thinking` 模型
247
+
248
+ ```bash
249
+ curl -X POST http://localhost:8002/v1/chat/completions \
250
+ -H "Content-Type: application/json" \
251
+ -H "Authorization: Bearer 0000" \
252
+ -d '{
253
+ "model": "claude-sonnet-4.6-thinking",
254
+ "messages": [{"role": "user", "content": "先思考再决定要不要用工具"}],
255
+ "tools": [
256
+ {
257
+ "type": "function",
258
+ "function": {
259
+ "name": "lookup",
260
+ "parameters": {
261
+ "type": "object",
262
+ "properties": {
263
+ "q": {"type": "string"}
264
+ },
265
+ "required": ["q"]
266
+ }
267
+ }
268
+ }
269
+ ],
270
+ "stream": true
271
+ }'
272
+ ```
273
+
274
+ ### 在第三方应用中使用
275
+
276
+ 在任何支持自定义 OpenAI API 的应用中(如 ChatGPT Next Web、Lobe Chat 等):
277
+
278
+ 1. **API 地址**: `http://localhost:8002`
279
+ 2. **API 密钥**: `0000`(或自定义)
280
+ 3. **模型**: 选择支持的模型之一;基础模型会自动有对应的 `-thinking` 版本
281
+
282
+ ## ⚙️ 配置说明
283
+
284
+ ### 环境变量
285
+
286
+ | 变量名 | 默认值 | 说明 |
287
+ |--------|--------|------|
288
+ | `PORT` | `8002` | 服务器端口 |
289
+ | `DEBUG` | `false` | 调试模式(启用后显示详细日志和路由信息) |
290
+ | `API_KEY` | `0000` | API 认证密钥 |
291
+ | `MODELS` | `claude-sonnet-4.6` | 基础模型列表(逗号分隔),服务会自动追加对应的 `-thinking` 公开模型 |
292
+ | `TIMEOUT` | `60` | 请求超时时间(秒) |
293
+ | `KILO_TOOL_STRICT` | `false` | Kilo Code 兼容开关:当请求提供 `tools` 且 `tool_choice=auto` 时,将其提升为“必须至少调用一次工具” |
294
+
295
+ ### 调试模式
296
+
297
+ 默认情况下,服务以简洁模式运行。如需启用详细日志:
298
+
299
+ **方式 1**: 修改 `.env` 文件
300
+ ```bash
301
+ DEBUG=true
302
+ ```
303
+
304
+ **方式 2**: 使用环境变量
305
+ ```bash
306
+ DEBUG=true ./cursor2api-go
307
+ ```
308
+
309
+ 调试模式会显示:
310
+ - 详细的 GIN 路由信息
311
+ - 每个请求的详细日志
312
+ - x-is-human token 信息
313
+ - 浏览器指纹配置
314
+
315
+ ### 故障排除
316
+
317
+ 遇到问题?查看 **[故障排除指南](TROUBLESHOOTING.md)** 了解常见问题的解决方案,包括:
318
+ - 403 Access Denied 错误
319
+ - Token 获取失败
320
+ - 连接超时
321
+ - Cloudflare 拦截
322
+
323
+ ## 🧩 与 Kilo Code / Agent 编排器的兼容性
324
+
325
+ 部分编排器会在“提供了 tools”时强制模型必须产出工具调用,否则报类似 `MODEL_NO_TOOLS_USED` 的错误。为改善这一类兼容问题:
326
+
327
+ - **建议**:在 `.env` 中启用 `KILO_TOOL_STRICT=true`
328
+ - **非流式补救**:当请求提供 `tools` 且被判定为“必须用工具”(`tool_choice=required/指定函数`,或启用 `KILO_TOOL_STRICT`)时,如果本轮没有产出 `tool_calls`,服务会自动 **重试 1 次**(仅非流式;流式不重试)
329
+
330
+
331
+ ### Windows 启动脚本说明
332
+
333
+ 项目提供两个 Windows 启动脚本:
334
+
335
+ - **`start-go.bat`** (推荐): GBK 编码,完美兼容 Windows cmd.exe
336
+ - **`start-go-utf8.bat`**: UTF-8 编码,适用于 Git Bash、PowerShell、Windows Terminal
337
+
338
+ 两个脚本功能完全相同,仅显示样式不同。如遇乱码请使用 `start-go.bat`。
339
+
340
+ ## 🧪 开发
341
+
342
+ ### 运行测试
343
+
344
+ ```bash
345
+ # 运行现有测试
346
+ go test ./...
347
+ ```
348
+
349
+ ### 构建项目
350
+
351
+ ```bash
352
+ # 构建可执行文件
353
+ go build -o cursor2api-go
354
+
355
+ # 交叉编译 (例如 Linux)
356
+ GOOS=linux GOARCH=amd64 go build -o cursor2api-go-linux
357
+ ```
358
+
359
+ ## 📁 项目结构
360
+
361
+ ```
362
+ cursor2api-go/
363
+ ├── main.go # 主程序入口 (Go 版本)
364
+ ├── config/ # 配置管理 (Go 版本)
365
+ ├── handlers/ # HTTP 处理器 (Go 版本)
366
+ ├── services/ # 业务服务层 (Go 版本)
367
+ ├── models/ # 数据模型 (Go 版本)
368
+ ├── utils/ # 工具函数 (Go 版本)
369
+ ├── middleware/ # 中间件 (Go 版本)
370
+ ├── jscode/ # JavaScript 代码 (Go 版本)
371
+ ├── static/ # 静态文件 (Go 版本)
372
+ ├── start.sh # Linux/macOS 启动脚本
373
+ ├── start-go.bat # Windows 启动脚本 (GBK)
374
+ ├── start-go-utf8.bat # Windows 启动脚本 (UTF-8)
375
+
376
+ └── README.md # 项目说明
377
+ ```
378
+
379
+ ## 🤝 贡献指南
380
+
381
+ 欢迎贡献代码!请遵循以下步骤:
382
+
383
+ 1. Fork 本仓库
384
+ 2. 创建功能分支 (`git checkout -b feature/AmazingFeature`)
385
+ 3. 提交更改 (`git commit -m 'feat: Add some AmazingFeature'`)
386
+ 4. 推送到分支 (`git push origin feature/AmazingFeature`)
387
+ 5. 开启 Pull Request
388
+
389
+ ### 代码规范
390
+
391
+ - 遵循 [Go Code Review Comments](https://github.com/golang/go/wiki/CodeReviewComments)
392
+ - 使用 `gofmt` 格式化代码
393
+ - 使用 `go vet` 检查代码
394
+ - 提交信息遵循 [Conventional Commits](https://conventionalcommits.org/) 规范
395
+
396
+ ## 📄 许可证
397
+
398
+ 本项目采用 [PolyForm Noncommercial 1.0.0](https://polyformproject.org/licenses/noncommercial/1.0.0/) 许可证。
399
+ 禁止商业用途。查看 [LICENSE](LICENSE) 文件了解详情。
400
+
401
+ ## ⚠️ 免责声明
402
+
403
+ 使用本项目时请遵守相关服务的使用条款。
404
+
405
+ ---
406
+
407
+ ⭐ 如果这个项目对您有帮助,请给我们一个 Star!