# cursor2api-go **Repository Path**: mefaso/cursor2api-go ## Basic Information - **Project Name**: cursor2api-go - **Description**: cursor2api-go 自用 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: my - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-18 - **Last Updated**: 2026-03-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Cursor2API [English](README_EN.md) | 简体中文 一个将 Cursor Web 转换为 OpenAI `chat/completions` 兼容 API 的 Go 服务。 [![Go Version](https://img.shields.io/badge/Go-1.24+-blue.svg)](https://golang.org) [![License: PolyForm Noncommercial](https://img.shields.io/badge/License-PolyForm%20Noncommercial-orange.svg)](https://polyformproject.org/licenses/noncommercial/1.0.0/) ## ✨ 特性 - 🔄 **API 兼容**: 兼容 OpenAI `chat/completions` 接口 - ⚡ **高性能**: 低延迟响应 - 🔐 **安全认证**: 支持 API Key 认证 - 🌐 **模型派生**: 自动暴露 `*-thinking` 模型 - 🧰 **工具调用**: 支持 `tools` / `tool_choice` / `tool_calls` - 🛡️ **错误处理**: 完善的错误处理机制 - 📊 **健康检查**: 内置健康检查接口 ## 🖼️ 效果图 ![首页预览](docs/images/home.png) ![调用效果预览 1](docs/images/play1.png) ![调用效果预览 2](docs/images/play2.png) ## 🤖 支持的模型 - **Anthropic Claude**: `claude-sonnet-4.6` - **自动派生 thinking 模型**: `claude-sonnet-4.6-thinking` ## 🚀 快速开始 ### 环境要求 - Go 1.24+ - Node.js 18+ (用于 JavaScript 执行) ### 本地运行方式 #### 方法一:直接运行(推荐用于开发) **Linux/macOS**: ```bash git clone https://github.com/libaxuan/cursor2api-go.git cd cursor2api-go chmod +x start.sh ./start.sh ``` **Windows**: ```batch # 双击运行或在 cmd 中执行 start-go.bat # 或在 Git Bash / Windows Terminal 中 ./start-go-utf8.bat ``` #### 方法二:手动编译运行 ```bash # 克隆项目 git clone https://github.com/libaxuan/cursor2api-go.git cd cursor2api-go # 下载依赖 go mod tidy # 编译 go build -o cursor2api-go # 运行 ./cursor2api-go ``` #### 方法三:使用 go run ```bash git clone https://github.com/libaxuan/cursor2api-go.git cd cursor2api-go go run main.go ``` 服务将在 `http://localhost:8002` 启动 ## 🚀 服务器部署方式 ### Docker 部署 1. **构建镜像**: ```bash # 构建镜像 docker build -t cursor2api-go . ``` 2. **运行容器**: ```bash # 运行容器(推荐) docker run -d \ --name cursor2api-go \ --restart unless-stopped \ -p 8002:8002 \ -e API_KEY=your-secret-key \ -e DEBUG=false \ cursor2api-go # 或者使用默认配置运行 docker run -d --name cursor2api-go --restart unless-stopped -p 8002:8002 cursor2api-go ``` ### Docker Compose 部署(推荐用于生产环境) 1. **使用 docker-compose.yml**: ```bash # 启动服务 docker-compose up -d # 停止服务 docker-compose down # 查看日志 docker-compose logs -f ``` 2. **自定义配置**: 修改 `docker-compose.yml` 文件中的环境变量以满足您的需求: - 修改 `API_KEY` 为安全的密钥 - 根据需要调整 `MODELS`、`TIMEOUT` 等配置 - 更改暴露的端口 ### 系统服务部署(Linux) 1. **编译并移动二进制文件**: ```bash go build -o cursor2api-go sudo mv cursor2api-go /usr/local/bin/ sudo chmod +x /usr/local/bin/cursor2api-go ``` 2. **创建系统服务文件** `/etc/systemd/system/cursor2api-go.service`: ```ini [Unit] Description=Cursor2API Service After=network.target [Service] Type=simple User=your-user WorkingDirectory=/home/your-user/cursor2api-go ExecStart=/usr/local/bin/cursor2api-go Restart=always Environment=API_KEY=your-secret-key Environment=PORT=8002 [Install] WantedBy=multi-user.target ``` 3. **启动服务**: ```bash # 重载 systemd 配置 sudo systemctl daemon-reload # 启用开机自启 sudo systemctl enable cursor2api-go # 启动服务 sudo systemctl start cursor2api-go # 查看状态 sudo systemctl status cursor2api-go ``` ## 📡 API 使用 ### 获取模型列表 ```bash curl -H "Authorization: Bearer 0000" http://localhost:8002/v1/models ``` ### 非流式聊天 ```bash curl -X POST http://localhost:8002/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer 0000" \ -d '{ "model": "claude-sonnet-4.6", "messages": [{"role": "user", "content": "Hello!"}], "stream": false }' ``` ### 流式聊天 ```bash curl -X POST http://localhost:8002/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer 0000" \ -d '{ "model": "claude-sonnet-4.6", "messages": [{"role": "user", "content": "Hello!"}], "stream": true }' ``` ### 带工具的请求 ```bash curl -X POST http://localhost:8002/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer 0000" \ -d '{ "model": "claude-sonnet-4.6", "messages": [{"role": "user", "content": "帮我查询北京天气"}], "tools": [ { "type": "function", "function": { "name": "get_weather", "description": "获取实时天气", "parameters": { "type": "object", "properties": { "city": {"type": "string"} }, "required": ["city"] } } } ] }' ``` ### `-thinking` 模型 ```bash curl -X POST http://localhost:8002/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer 0000" \ -d '{ "model": "claude-sonnet-4.6-thinking", "messages": [{"role": "user", "content": "先思考再决定要不要用工具"}], "tools": [ { "type": "function", "function": { "name": "lookup", "parameters": { "type": "object", "properties": { "q": {"type": "string"} }, "required": ["q"] } } } ], "stream": true }' ``` ### 在第三方应用中使用 在任何支持自定义 OpenAI API 的应用中(如 ChatGPT Next Web、Lobe Chat 等): 1. **API 地址**: `http://localhost:8002` 2. **API 密钥**: `0000`(或自定义) 3. **模型**: 选择支持的模型之一;基础模型会自动有对应的 `-thinking` 版本 ## ⚙️ 配置说明 ### 环境变量 | 变量名 | 默认值 | 说明 | |--------|--------|------| | `PORT` | `8002` | 服务器端口 | | `DEBUG` | `false` | 调试模式(启用后显示详细日志和路由信息) | | `API_KEY` | `0000` | API 认证密钥 | | `MODELS` | `claude-sonnet-4.6` | 基础模型列表(逗号分隔),服务会自动追加对应的 `-thinking` 公开模型 | | `TIMEOUT` | `60` | 请求超时时间(秒) | | `KILO_TOOL_STRICT` | `false` | Kilo Code 兼容开关:当请求提供 `tools` 且 `tool_choice=auto` 时,将其提升为“必须至少调用一次工具” | | `STRICT_TOOL_MATCH` | `false` | 工具信号严格匹配模式。默认 `false` (宽松模式):解析任意 `<>` 信号,解决模型幻觉信号问题。设为 `true` 则仅解析与系统注入一致的信号 | | `FAIL_ON_UNPARSED_TOOLCALL` | `false` | 未解析工具阻断:当为 `true` 时,若响应内容中残留 `<