copilot-api-plus 1.0.56 → 1.0.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,843 +1,866 @@
1
- # Copilot API Plus
2
-
3
- [![npm version](https://img.shields.io/npm/v/copilot-api-plus.svg)](https://www.npmjs.com/package/copilot-api-plus)
4
- [![license](https://img.shields.io/npm/l/copilot-api-plus.svg)](https://github.com/imbuxiangnan-cyber/copilot-api-plus/blob/main/LICENSE)
5
-
6
- [English](README.en.md) | 简体中文
7
-
8
- > A proxy that converts GitHub Copilot, OpenCode Zen, and Google Antigravity into OpenAI & Anthropic compatible APIs. Works with Claude Code, opencode, and more.
9
-
10
- 将 GitHub Copilot、OpenCode Zen、Google Antigravity 等 AI 服务转换为 **OpenAI** 和 **Anthropic** 兼容 API,支持与 [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview)、[opencode](https://github.com/sst/opencode) 等工具无缝集成。
11
-
12
- ---
13
-
14
- ## 📋 目录
15
-
16
- - [功能特点](#-功能特点)
17
- - [快速开始](#-快速开始)
18
- - [详细使用指南](#-详细使用指南)
19
- - [GitHub Copilot 模式](#1-github-copilot-模式默认)
20
- - [OpenCode Zen 模式](#2-opencode-zen-模式)
21
- - [Google Antigravity 模式](#3-google-antigravity-模式)
22
- - [代理配置](#-代理配置)
23
- - [Claude Code 集成](#-claude-code-集成)
24
- - [opencode 集成](#-opencode-集成)
25
- - [API 端点](#-api-端点)
26
- - [API Key 认证](#-api-key-认证)
27
- - [技术细节](#-技术细节)
28
- - [命令行参考](#️-命令行参考)
29
- - [Docker 部署](#-docker-部署)
30
- - [常见问题](#-常见问题)
31
-
32
- ---
33
-
34
- ## ✨ 功能特点
35
-
36
- | 功能 | 说明 |
37
- |------|------|
38
- | 🔌 **多后端支持** | GitHub Copilot、OpenCode Zen、Google Antigravity 三种后端可选 |
39
- | 🤖 **双协议兼容** | 同时支持 OpenAI Chat Completions API 和 Anthropic Messages API |
40
- | 💻 **Claude Code 集成** | 一键生成 Claude Code 启动命令 (`--claude-code`) |
41
- | 📊 **使用量监控** | Web 仪表盘实时查看 API 使用情况 |
42
- | 🔄 **自动认证** | Token 过期自动刷新,无需手动干预 |
43
- | ⚡ **速率限制** | 内置请求频率控制,避免触发限制 |
44
- | 🌐 **代理支持** | 支持 HTTP/HTTPS 代理,配置持久化 |
45
- | 🐳 **Docker 支持** | 提供完整的 Docker 部署方案 |
46
- | 🔑 **API Key 认证** | 可选的 API Key 鉴权,保护公开部署的服务 |
47
- | ✂️ **上下文透传** | 全量透传上下文至上游 API,由客户端(如 Claude Code)自行管理压缩 |
48
- | 🔍 **智能模型匹配** | 自动处理模型名格式差异(日期后缀、dash/dot 版本号等) |
49
- | 🔁 **Antigravity 端点容错** | 双端点自动切换,按模型族追踪速率限制,指数退避重试 |
50
-
51
- ---
52
-
53
- ## 🚀 快速开始
54
-
55
- ### 安装
56
-
57
- ```bash
58
- # 全局安装
59
- npm install -g copilot-api-plus
60
-
61
- # 或使用 npx 直接运行(推荐)
62
- npx copilot-api-plus@latest start
63
- ```
64
-
65
- ### 基本用法
66
-
67
- ```bash
68
- # 启动服务器(默认使用 GitHub Copilot)
69
- npx copilot-api-plus@latest start
70
-
71
- # 使用 OpenCode Zen
72
- npx copilot-api-plus@latest start --zen
73
-
74
- # 使用 Google Antigravity
75
- npx copilot-api-plus@latest start --antigravity
76
-
77
- # 与 Claude Code 配合
78
- npx copilot-api-plus@latest start --claude-code
79
- ```
80
-
81
- 服务器启动后,默认监听 `http://localhost:4141`。
82
-
83
- ---
84
-
85
- ## 📖 详细使用指南
86
-
87
- ### 1. GitHub Copilot 模式(默认)
88
-
89
- 使用你的 GitHub Copilot 订阅访问 AI 模型。
90
-
91
- #### 前置要求
92
- - GitHub 账户
93
- - 有效的 Copilot 订阅(Individual / Business / Enterprise)
94
-
95
- #### 启动步骤
96
-
97
- ```bash
98
- npx copilot-api-plus@latest start
99
- ```
100
-
101
- **首次运行**会引导你完成 GitHub OAuth 认证:
102
-
103
- 1. 终端显示设备码,例如:`XXXX-XXXX`
104
- 2. 打开浏览器访问:https://github.com/login/device
105
- 3. 输入设备码,点击授权
106
- 4. 返回终端,等待认证完成
107
-
108
- 认证成功后,Token 会保存到本地,下次启动无需重新认证。
109
-
110
- #### 企业/商业账户
111
-
112
- ```bash
113
- # Business 计划
114
- npx copilot-api-plus@latest start --account-type business
115
-
116
- # Enterprise 计划
117
- npx copilot-api-plus@latest start --account-type enterprise
118
- ```
119
-
120
- #### 可用模型
121
-
122
- | 模型 | ID | 上下文长度 |
123
- |------|-----|-----------|
124
- | Claude Sonnet 4 | `claude-sonnet-4` | 200K |
125
- | Claude Sonnet 4.5 | `claude-sonnet-4.5` | 200K |
126
- | GPT-4.1 | `gpt-4.1` | 1M |
127
- | o4-mini | `o4-mini` | 200K |
128
- | Gemini 2.5 Pro | `gemini-2.5-pro` | 1M |
129
-
130
- ---
131
-
132
- ### 2. OpenCode Zen 模式
133
-
134
- 使用 [OpenCode Zen](https://opencode.ai/zen) 的多模型 API 服务,支持 GPT-5、Claude、Gemini 等顶级编程模型。
135
-
136
- #### 前置要求
137
- 1. 访问 https://opencode.ai/zen
138
- 2. 注册账号并创建 API Key
139
-
140
- #### 启动步骤
141
-
142
- **方式一:交互式设置**
143
- ```bash
144
- npx copilot-api-plus@latest start --zen
145
- ```
146
- 首次运行会提示输入 API Key,保存后下次自动使用。
147
-
148
- **方式二:直接指定 API Key**
149
- ```bash
150
- npx copilot-api-plus@latest start --zen --zen-api-key YOUR_API_KEY
151
- ```
152
-
153
- #### 可用模型
154
-
155
- | 模型 | ID | 说明 |
156
- |------|-----|------|
157
- | GPT-5.2 | `gpt-5.2` | OpenAI 最新模型 |
158
- | GPT-5.1 Codex Max | `gpt-5.1-codex-max` | 代码优化版 |
159
- | GPT-5.1 Codex | `gpt-5.1-codex` | 代码专用 |
160
- | GPT-5 Codex | `gpt-5-codex` | OpenAI Responses API |
161
- | Claude Opus 4.5 | `claude-opus-4-5` | Anthropic Claude (200K) |
162
- | Claude Sonnet 4.5 | `claude-sonnet-4-5` | Anthropic Claude (200K) |
163
- | Claude Sonnet 4 | `claude-sonnet-4` | Anthropic Claude |
164
- | Gemini 3 Pro | `gemini-3-pro` | Google Gemini |
165
- | Qwen3 Coder | `qwen3-coder` | Alibaba Qwen |
166
- | Kimi K2 | `kimi-k2` | Moonshot |
167
- | Grok Code Fast 1 | `grok-code-fast-1` | xAI |
168
-
169
- 更多模型请访问 [opencode.ai/zen](https://opencode.ai/zen)
170
-
171
- #### API 端点
172
-
173
- Zen 模式支持以下 API 端点:
174
-
175
- | 端点 | 说明 |
176
- |------|------|
177
- | `/v1/chat/completions` | OpenAI 兼容 Chat API |
178
- | `/v1/messages` | Anthropic 兼容 Messages API |
179
- | `/v1/responses` | OpenAI Responses API (GPT-5 系列) |
180
- | `/v1/models` | 获取可用模型列表 |
181
-
182
- 专用端点(无需 `--zen` 标志也可访问):
183
- - `/zen/v1/chat/completions`
184
- - `/zen/v1/messages`
185
- - `/zen/v1/responses`
186
- - `/zen/v1/models`
187
-
188
- #### 管理 API Key
189
-
190
- ```bash
191
- # 查看/更换 API Key(清除后重新启动会提示输入)
192
- npx copilot-api-plus@latest logout --zen
193
- ```
194
-
195
- ---
196
-
197
- ### 3. Google Antigravity 模式
198
-
199
- 使用 Google Antigravity API 服务,支持 Gemini 和 Claude 模型。
200
-
201
- #### 前置要求
202
- - Google 账户
203
-
204
- #### 认证方式
205
-
206
- **方式一:API Key(推荐 - 最简单)**
207
-
208
- 1. 访问 https://aistudio.google.com/apikey 获取 API Key
209
- 2. 使用环境变量启动:
210
-
211
- ```bash
212
- # Linux/macOS
213
- GEMINI_API_KEY=your_api_key npx copilot-api-plus@latest start --antigravity
214
-
215
- # Windows PowerShell
216
- $env:GEMINI_API_KEY = "your_api_key"
217
- npx copilot-api-plus@latest start --antigravity
218
-
219
- # Windows CMD
220
- set GEMINI_API_KEY=your_api_key
221
- npx copilot-api-plus@latest start --antigravity
222
- ```
223
-
224
- **方式二:OAuth 网页登录(推荐)**
225
-
226
- ```bash
227
- npx copilot-api-plus@latest start --antigravity
228
- ```
229
-
230
- 首次运行会提示选择登录方式:
231
- - **Web(推荐)**:自动打开浏览器完成 Google 登录,授权后自动捕获回调
232
- - **Manual**:手动复制回调 URL 到终端
233
-
234
- **方式三:自定义 OAuth 凭证**
235
-
236
- 如果遇到 `invalid_client` 错误,可以创建自己的 OAuth 应用:
237
-
238
- 1. 访问 https://console.cloud.google.com/apis/credentials
239
- 2. 创建 OAuth 2.0 客户端 ID(选择"桌面应用"类型)
240
- 3. 添加重定向 URI:`http://localhost:8046/callback`
241
- 4. 使用环境变量或命令行参数:
242
-
243
- ```bash
244
- # 环境变量方式
245
- ANTIGRAVITY_CLIENT_ID=your_client_id ANTIGRAVITY_CLIENT_SECRET=your_secret \
246
- npx copilot-api-plus@latest start --antigravity
247
-
248
- # 命令行参数方式
249
- npx copilot-api-plus@latest start --antigravity \
250
- --antigravity-client-id your_client_id \
251
- --antigravity-client-secret your_secret
252
- ```
253
-
254
- #### 可用模型
255
-
256
- | 模型 | ID | 说明 |
257
- |------|-----|------|
258
- | Gemini 2.5 Pro | `gemini-2.5-pro-exp-03-25` | Google Gemini |
259
- | Gemini 2.5 Pro Preview | `gemini-2.5-pro-preview-05-06` | Google Gemini |
260
- | Gemini 2.0 Flash | `gemini-2.0-flash-exp` | 快速响应 |
261
- | Gemini 2.0 Flash Thinking | `gemini-2.0-flash-thinking-exp` | 支持思考链 |
262
- | Claude Opus 4.5 | `claude-opus-4-5` | Anthropic Claude |
263
- | Claude Sonnet 4.5 | `claude-sonnet-4-5` | Anthropic Claude |
264
-
265
- #### 特性
266
- - ✅ 自动 Token 刷新
267
- - ✅ 多账户支持,自动轮换
268
- - ✅ 配额用尽自动切换账户
269
- - ✅ 支持 Thinking 模型(思考链输出)
270
-
271
- #### 多账户管理
272
-
273
- 可以添加多个 Google 账户,系统会在配额用尽时自动切换:
274
-
275
- ```bash
276
- # 添加新账户
277
- npx copilot-api-plus@latest antigravity add
278
-
279
- # 列出所有账户
280
- npx copilot-api-plus@latest antigravity list
281
-
282
- # 按索引删除账户
283
- npx copilot-api-plus@latest antigravity remove 0
284
-
285
- # 清除所有账户
286
- npx copilot-api-plus@latest antigravity clear
287
- # 或使用 logout 命令
288
- npx copilot-api-plus@latest logout --antigravity
289
- ```
290
-
291
- ---
292
-
293
- ## 🌐 代理配置
294
-
295
- 如果你需要通过代理访问网络,有两种配置方式:
296
-
297
- ### 方式一:持久化配置(推荐)
298
-
299
- 配置一次,永久生效,下次启动自动使用。
300
-
301
- ```bash
302
- # 交互式配置
303
- npx copilot-api-plus@latest proxy --set
304
-
305
- # 或直接设置
306
- npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890
307
-
308
- # 同时设置 HTTP HTTPS 代理
309
- npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890 --https-proxy http://127.0.0.1:7890
310
- ```
311
-
312
- #### 代理管理命令
313
-
314
- ```bash
315
- # 查看当前代理配置
316
- npx copilot-api-plus@latest proxy
317
-
318
- # 启用代理
319
- npx copilot-api-plus@latest proxy --enable
320
-
321
- # 禁用代理(保留设置)
322
- npx copilot-api-plus@latest proxy --disable
323
-
324
- # 清除代理配置
325
- npx copilot-api-plus@latest proxy --clear
326
- ```
327
-
328
- #### 示例:配置 Clash 代理
329
-
330
- ```bash
331
- # Clash 默认端口 7890
332
- npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890
333
-
334
- # 验证配置
335
- npx copilot-api-plus@latest proxy
336
- # 输出:
337
- # Current proxy configuration:
338
- # Status: ✅ Enabled
339
- # HTTP_PROXY: http://127.0.0.1:7890
340
- # HTTPS_PROXY: http://127.0.0.1:7890
341
- ```
342
-
343
- ### 方式二:环境变量(临时)
344
-
345
- 仅当次启动生效:
346
-
347
- ```bash
348
- # Linux/macOS
349
- export HTTP_PROXY=http://127.0.0.1:7890
350
- export HTTPS_PROXY=http://127.0.0.1:7890
351
- npx copilot-api-plus@latest start --proxy-env
352
-
353
- # Windows PowerShell
354
- $env:HTTP_PROXY = "http://127.0.0.1:7890"
355
- $env:HTTPS_PROXY = "http://127.0.0.1:7890"
356
- npx copilot-api-plus@latest start --proxy-env
357
-
358
- # Windows CMD
359
- set HTTP_PROXY=http://127.0.0.1:7890
360
- set HTTPS_PROXY=http://127.0.0.1:7890
361
- npx copilot-api-plus@latest start --proxy-env
362
- ```
363
-
364
- ### 代理配置优先级
365
-
366
- 1. `--proxy-env` 参数(从环境变量读取)
367
- 2. 持久化配置(`proxy --set` 设置的)
368
- 3. 无代理
369
-
370
- ---
371
-
372
- ## 💻 Claude Code 集成
373
-
374
- [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) 是 Anthropic 的 AI 编程助手。
375
-
376
- ### 自动配置(推荐)
377
-
378
- ```bash
379
- # 使用 GitHub Copilot 作为后端
380
- npx copilot-api-plus@latest start --claude-code
381
-
382
- # 使用 OpenCode Zen 作为后端
383
- npx copilot-api-plus@latest start --zen --claude-code
384
-
385
- # 使用 Google Antigravity 作为后端
386
- npx copilot-api-plus@latest start --antigravity --claude-code
387
- ```
388
-
389
- 运行后:
390
- 1. 选择主模型(用于代码生成)
391
- 2. 选择快速模型(用于后台任务)
392
- 3. 启动命令会自动复制到剪贴板
393
- 4. **打开新终端**,粘贴并运行命令启动 Claude Code
394
-
395
- ### 手动配置
396
-
397
- 在项目根目录创建 `.claude/settings.json`:
398
-
399
- ```json
400
- {
401
- "env": {
402
- "ANTHROPIC_BASE_URL": "http://localhost:4141",
403
- "ANTHROPIC_AUTH_TOKEN": "dummy",
404
- "ANTHROPIC_MODEL": "claude-sonnet-4",
405
- "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1",
406
- "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1"
407
- }
408
- }
409
- ```
410
-
411
- 然后启动 copilot-api-plus 服务器后,在该项目目录运行 `claude` 命令。
412
-
413
- ---
414
-
415
- ## 🔧 opencode 集成
416
-
417
- [opencode](https://github.com/sst/opencode) 是一个现代 AI 编程助手。
418
-
419
- ### 配置步骤
420
-
421
- 1. 在项目根目录创建 `opencode.json`:
422
-
423
- ```json
424
- {
425
- "$schema": "https://opencode.ai/config.json",
426
- "provider": {
427
- "copilot-api-plus": {
428
- "api": "openai-compatible",
429
- "name": "Copilot API Plus",
430
- "options": {
431
- "baseURL": "http://127.0.0.1:4141/v1"
432
- },
433
- "models": {
434
- "claude-sonnet-4": {
435
- "name": "Claude Sonnet 4",
436
- "id": "claude-sonnet-4",
437
- "max_tokens": 64000,
438
- "profile": "coder",
439
- "limit": { "context": 200000 }
440
- },
441
- "gpt-4.1": {
442
- "name": "GPT-4.1",
443
- "id": "gpt-4.1",
444
- "max_tokens": 32768,
445
- "profile": "coder",
446
- "limit": { "context": 1047576 }
447
- }
448
- }
449
- }
450
- }
451
- }
452
- ```
453
-
454
- 2. 启动 copilot-api-plus:
455
- ```bash
456
- npx copilot-api-plus@latest start
457
- ```
458
-
459
- 3. 在同一目录运行 opencode:
460
- ```bash
461
- npx opencode@latest
462
- ```
463
-
464
- 4. 选择 `copilot-api-plus` 作为 provider
465
-
466
- ### 快捷方式:使用环境变量
467
-
468
- ```bash
469
- # 设置环境变量
470
- export OPENAI_BASE_URL=http://127.0.0.1:4141/v1
471
- export OPENAI_API_KEY=dummy
472
-
473
- # 运行 opencode
474
- npx opencode@latest
475
- ```
476
-
477
- ---
478
-
479
- ## 📡 API 端点
480
-
481
- 服务器启动后,默认监听 `http://localhost:4141`。
482
-
483
- ### OpenAI 兼容端点
484
-
485
- | 端点 | 方法 | 说明 |
486
- |------|------|------|
487
- | `/v1/chat/completions` | POST | 聊天补全(支持流式) |
488
- | `/v1/models` | GET | 模型列表 |
489
- | `/v1/embeddings` | POST | 文本嵌入(仅 Copilot) |
490
-
491
- ### Anthropic 兼容端点
492
-
493
- | 端点 | 方法 | 说明 |
494
- |------|------|------|
495
- | `/v1/messages` | POST | 消息 API(支持流式) |
496
- | `/v1/messages/count_tokens` | POST | Token 计数 |
497
-
498
- ### 专用端点
499
-
500
- 各后端都有独立的专用路由,即使切换默认后端也能访问:
501
-
502
- | 路由前缀 | 说明 |
503
- |----------|------|
504
- | `/copilot/v1/*` | GitHub Copilot 专用 |
505
- | `/zen/v1/*` | OpenCode Zen 专用 |
506
- | `/antigravity/v1/*` | Google Antigravity 专用 |
507
-
508
- ### 监控端点
509
-
510
- | 端点 | 方法 | 说明 |
511
- |------|------|------|
512
- | `/usage` | GET | 使用量统计(仅 Copilot) |
513
- | `/token` | GET | 当前 Token 信息 |
514
-
515
- ### 调用示例
516
-
517
- ```bash
518
- # OpenAI 格式
519
- curl http://localhost:4141/v1/chat/completions \
520
- -H "Content-Type: application/json" \
521
- -d '{
522
- "model": "claude-sonnet-4",
523
- "messages": [{"role": "user", "content": "Hello!"}]
524
- }'
525
-
526
- # Anthropic 格式
527
- curl http://localhost:4141/v1/messages \
528
- -H "Content-Type: application/json" \
529
- -H "x-api-key: dummy" \
530
- -d '{
531
- "model": "claude-sonnet-4",
532
- "max_tokens": 1024,
533
- "messages": [{"role": "user", "content": "Hello!"}]
534
- }'
535
- ```
536
-
537
- ---
538
-
539
- ## ⚙️ 命令行参考
540
-
541
- ### 命令列表
542
-
543
- | 命令 | 说明 |
544
- |------|------|
545
- | `start` | 启动 API 服务器 |
546
- | `auth` | 仅执行 GitHub 认证流程 |
547
- | `logout` | 清除已保存的凭证 |
548
- | `proxy` | 配置代理设置 |
549
- | `antigravity` | 管理 Google Antigravity 账户 |
550
- | `check-usage` | 查看 Copilot 使用量 |
551
- | `debug` | 显示调试信息 |
552
-
553
- ### start 命令参数
554
-
555
- | 参数 | 别名 | 默认值 | 说明 |
556
- |------|------|--------|------|
557
- | `--port` | `-p` | 4141 | 监听端口 |
558
- | `--verbose` | `-v` | false | 详细日志 |
559
- | `--account-type` | `-a` | individual | 账户类型 (individual/business/enterprise) |
560
- | `--claude-code` | `-c` | false | 生成 Claude Code 启动命令 |
561
- | `--zen` | `-z` | false | 启用 OpenCode Zen 模式 |
562
- | `--zen-api-key` | - | - | Zen API Key |
563
- | `--antigravity` | - | false | 启用 Google Antigravity 模式 |
564
- | `--antigravity-client-id` | - | - | Antigravity OAuth Client ID |
565
- | `--antigravity-client-secret` | - | - | Antigravity OAuth Client Secret |
566
- | `--rate-limit` | `-r` | - | 请求间隔(秒) |
567
- | `--wait` | `-w` | false | 达到限制时等待而非报错 |
568
- | `--manual` | - | false | 手动审批每个请求 |
569
- | `--github-token` | `-g` | - | 直接提供 GitHub Token |
570
- | `--show-token` | - | false | 显示 Token 信息 |
571
- | `--proxy-env` | - | false | 从环境变量读取代理 |
572
- | `--api-key` | - | - | API Key 鉴权(可多次指定) |
573
-
574
- ### proxy 命令参数
575
-
576
- | 参数 | 说明 |
577
- |------|------|
578
- | `--set` | 交互式配置代理 |
579
- | `--enable` | 启用已保存的代理 |
580
- | `--disable` | 禁用代理(保留设置) |
581
- | `--clear` | 清除代理配置 |
582
- | `--show` | 显示当前配置 |
583
- | `--http-proxy` | HTTP 代理 URL |
584
- | `--https-proxy` | HTTPS 代理 URL |
585
- | `--no-proxy` | 不走代理的主机列表 |
586
-
587
- ### logout 命令参数
588
-
589
- | 参数 | 别名 | 说明 |
590
- |------|------|------|
591
- | `--github` | `-g` | 仅清除 GitHub Copilot 凭证 |
592
- | `--zen` | `-z` | 仅清除 Zen 凭证 |
593
- | `--antigravity` | - | 仅清除 Antigravity 凭证 |
594
- | `--all` | `-a` | 清除所有凭证 |
595
-
596
- > **提示**:不带参数运行 `logout` 会显示交互式菜单供选择。
597
-
598
- ### antigravity 命令
599
-
600
- 管理 Google Antigravity 账户的子命令:
601
-
602
- | 子命令 | 说明 |
603
- |--------|------|
604
- | `add` | 添加新的 Antigravity 账户(OAuth 登录) |
605
- | `list` | 列出所有已配置的账户及其状态 |
606
- | `remove <index>` | 按索引删除指定账户 |
607
- | `clear` | 清除所有 Antigravity 账户(需确认) |
608
-
609
- ```bash
610
- # 示例
611
- npx copilot-api-plus@latest antigravity add # 添加账户
612
- npx copilot-api-plus@latest antigravity list # 列出账户
613
- npx copilot-api-plus@latest antigravity remove 0 # 删除索引为 0 的账户
614
- npx copilot-api-plus@latest antigravity clear # 清除所有账户
615
- ```
616
-
617
- ---
618
-
619
- ## 🔑 API Key 认证
620
-
621
- 如果你将服务暴露到公网,可以启用 API Key 认证来保护接口:
622
-
623
- ```bash
624
- # 单个 Key
625
- npx copilot-api-plus@latest start --api-key my-secret-key
626
-
627
- # 多个 Key
628
- npx copilot-api-plus@latest start --api-key key1 --api-key key2
629
- ```
630
-
631
- 启用后,所有请求需要携带 API Key:
632
-
633
- ```bash
634
- # OpenAI 格式 - 通过 Authorization header
635
- curl http://localhost:4141/v1/chat/completions \
636
- -H "Authorization: Bearer my-secret-key" \
637
- -H "Content-Type: application/json" \
638
- -d '{"model": "claude-sonnet-4", "messages": [{"role": "user", "content": "Hello"}]}'
639
-
640
- # Anthropic 格式 - 通过 x-api-key header
641
- curl http://localhost:4141/v1/messages \
642
- -H "x-api-key: my-secret-key" \
643
- -H "Content-Type: application/json" \
644
- -d '{"model": "claude-sonnet-4", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}'
645
- ```
646
-
647
- 在 Claude Code 中使用时,将 `ANTHROPIC_AUTH_TOKEN` 设为你的 API Key 即可。
648
-
649
- ---
650
-
651
- ## 🔧 技术细节
652
-
653
- ### 上下文管理
654
-
655
- 代理层不做上下文截断,全量透传消息至上游 API。上下文压缩由客户端负责:
656
-
657
- - **Claude Code**:通过 `/count_tokens` 端点获取当前 token 数,接近上限时自动触发 `/compact` 压缩
658
- - **其他客户端**:如果上游 API 返回 400(token 超限),客户端自行处理重试
659
-
660
- ### 智能模型名匹配
661
-
662
- Anthropic 格式的模型名(如 `claude-opus-4-6`)和 Copilot 的模型列表 ID 可能存在格式差异。代理使用多策略精确匹配:
663
-
664
- | 策略 | 示例 |
665
- |------|------|
666
- | 精确匹配 | `claude-opus-4-6` → `claude-opus-4-6` |
667
- | 去日期后缀 | `claude-opus-4-6-20251101` → `claude-opus-4-6` |
668
- | Dash → Dot | `claude-opus-4-5` → `claude-opus-4.5` |
669
- | Dot → Dash | `claude-opus-4.5` → `claude-opus-4-5` |
670
-
671
- 对于 Anthropic 端点(`/v1/messages`),还会先通过 `translateModelName` 做格式转换(包括旧格式 `claude-3-5-sonnet` → `claude-sonnet-4.5` 的映射),再通过上述策略匹配。
672
-
673
- ### Antigravity 端点容错
674
-
675
- Google Antigravity 模式内置了可靠性保障:
676
-
677
- - **双端点自动切换**:daily sandbox 和 production 两个端点,一个失败自动切换到另一个
678
- - **按模型族速率追踪**:分别追踪 Gemini 和 Claude 模型族的速率限制状态
679
- - **指数退避重试**:429/503 等限流错误自动退避重试,短间隔走同端点,长间隔切换端点
680
-
681
- ### 请求日志
682
-
683
- 每次 API 请求会输出一行日志,包含模型名、状态码和耗时:
684
-
685
- ```log
686
- [claude-opus-4-6] 13:13:39 <-- POST /v1/messages?beta=true
687
- [claude-opus-4-6] 13:13:59 --> POST /v1/messages?beta=true 200 20.1s
688
- ```
689
-
690
- ### 网络重试
691
-
692
- 对上游 API 的请求内置了瞬时网络错误重试(TLS 断开、连接重置等):
693
-
694
- - 最多重试 2 次(共 3 次尝试)
695
- - 退避间隔:1s、2s
696
- - 仅重试网络层错误,HTTP 错误码(如 400/500)不重试
697
-
698
- ---
699
-
700
- ## 🐳 Docker 部署
701
-
702
- ### 快速启动
703
-
704
- ```bash
705
- # 使用预构建镜像
706
- docker run -p 4141:4141 \
707
- -v ./copilot-data:/root/.local/share/copilot-api-plus \
708
- ghcr.io/imbuxiangnan-cyber/copilot-api-plus
709
- ```
710
-
711
- ### 自行构建
712
-
713
- ```bash
714
- # 构建镜像
715
- docker build -t copilot-api-plus .
716
-
717
- # 运行容器
718
- docker run -p 4141:4141 \
719
- -v ./copilot-data:/root/.local/share/copilot-api-plus \
720
- copilot-api-plus
721
- ```
722
-
723
- ### Docker Compose
724
-
725
- ```yaml
726
- version: "3.8"
727
- services:
728
- copilot-api-plus:
729
- build: .
730
- ports:
731
- - "4141:4141"
732
- volumes:
733
- - ./copilot-data:/root/.local/share/copilot-api-plus
734
- environment:
735
- - GH_TOKEN=your_github_token # 可选
736
- restart: unless-stopped
737
- ```
738
-
739
- ### 使用代理
740
-
741
- ```bash
742
- docker run -p 4141:4141 \
743
- -e HTTP_PROXY=http://host.docker.internal:7890 \
744
- -e HTTPS_PROXY=http://host.docker.internal:7890 \
745
- -v ./copilot-data:/root/.local/share/copilot-api-plus \
746
- copilot-api-plus start --proxy-env
747
- ```
748
-
749
- ---
750
-
751
- ## ❓ 常见问题
752
-
753
- ### 数据存储位置
754
-
755
- 所有数据存储在 `~/.local/share/copilot-api-plus/` 目录下:
756
-
757
- | 文件 | 说明 |
758
- |------|------|
759
- | `github_token` | GitHub Token |
760
- | `zen-auth.json` | Zen API Key |
761
- | `antigravity-accounts.json` | Antigravity 账户 |
762
- | `config.json` | 代理等配置 |
763
-
764
- ### 切换账户
765
-
766
- ```bash
767
- # 交互式选择要清除的凭证
768
- npx copilot-api-plus@latest logout
769
-
770
- # 仅清除 GitHub Copilot 凭证
771
- npx copilot-api-plus@latest logout --github
772
- # 或简写
773
- npx copilot-api-plus@latest logout -g
774
-
775
- # 清除 Zen 凭证
776
- npx copilot-api-plus@latest logout --zen
777
-
778
- # 清除 Antigravity 凭证
779
- npx copilot-api-plus@latest logout --antigravity
780
-
781
- # 清除所有凭证
782
- npx copilot-api-plus@latest logout --all
783
- ```
784
-
785
- ### 查看使用量
786
-
787
- ```bash
788
- # 命令行查看(仅 Copilot)
789
- npx copilot-api-plus@latest check-usage
790
- ```
791
-
792
- 启动服务器后,也可以访问 Web 仪表盘:
793
- ```
794
- https://imbuxiangnan-cyber.github.io/copilot-api-plus?endpoint=http://localhost:4141/usage
795
- ```
796
-
797
- ### 调试问题
798
-
799
- ```bash
800
- # 显示调试信息
801
- npx copilot-api-plus@latest debug
802
-
803
- # JSON 格式输出
804
- npx copilot-api-plus@latest debug --json
805
-
806
- # 启用详细日志
807
- npx copilot-api-plus@latest start --verbose
808
- ```
809
-
810
- ### 速率限制
811
-
812
- 避免触发 GitHub 的滥用检测:
813
-
814
- ```bash
815
- # 设置请求间隔 30 秒
816
- npx copilot-api-plus@latest start --rate-limit 30
817
-
818
- # 达到限制时等待而非报错
819
- npx copilot-api-plus@latest start --rate-limit 30 --wait
820
-
821
- # 手动审批每个请求
822
- npx copilot-api-plus@latest start --manual
823
- ```
824
-
825
- ---
826
-
827
- ## ⚠️ 免责声明
828
-
829
- > [!WARNING]
830
- > 这是 GitHub Copilot API 的逆向工程代理。**不受 GitHub 官方支持**,可能随时失效。使用风险自负。
831
-
832
- > [!WARNING]
833
- > **GitHub 安全提示**:过度的自动化或脚本化使用 Copilot 可能触发 GitHub 的滥用检测系统,导致 Copilot 访问被暂停。请负责任地使用。
834
- >
835
- > 相关政策:
836
- > - [GitHub 可接受使用政策](https://docs.github.com/site-policy/acceptable-use-policies/github-acceptable-use-policies)
837
- > - [GitHub Copilot 条款](https://docs.github.com/site-policy/github-terms/github-terms-for-additional-products-and-features#github-copilot)
838
-
839
- ---
840
-
841
- ## 📄 许可证
842
-
843
- MIT License
1
+ # Copilot API Plus
2
+
3
+ [![npm version](https://img.shields.io/npm/v/copilot-api-plus.svg)](https://www.npmjs.com/package/copilot-api-plus)
4
+ [![license](https://img.shields.io/npm/l/copilot-api-plus.svg)](https://github.com/imbuxiangnan-cyber/copilot-api-plus/blob/main/LICENSE)
5
+
6
+ [English](README.en.md) | 简体中文
7
+
8
+ > A proxy that converts GitHub Copilot, OpenCode Zen, and Google Antigravity into OpenAI & Anthropic compatible APIs. Works with Claude Code, opencode, and more.
9
+
10
+ 将 GitHub Copilot、OpenCode Zen、Google Antigravity 等 AI 服务转换为 **OpenAI** 和 **Anthropic** 兼容 API,支持与 [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview)、[opencode](https://github.com/sst/opencode) 等工具无缝集成。
11
+
12
+ ---
13
+
14
+ ## 📋 目录
15
+
16
+ - [功能特点](#-功能特点)
17
+ - [快速开始](#-快速开始)
18
+ - [详细使用指南](#-详细使用指南)
19
+ - [GitHub Copilot 模式](#1-github-copilot-模式默认)
20
+ - [OpenCode Zen 模式](#2-opencode-zen-模式)
21
+ - [Google Antigravity 模式](#3-google-antigravity-模式)
22
+ - [代理配置](#-代理配置)
23
+ - [Claude Code 集成](#-claude-code-集成)
24
+ - [opencode 集成](#-opencode-集成)
25
+ - [API 端点](#-api-端点)
26
+ - [API Key 认证](#-api-key-认证)
27
+ - [技术细节](#-技术细节)
28
+ - [命令行参考](#️-命令行参考)
29
+ - [Docker 部署](#-docker-部署)
30
+ - [常见问题](#-常见问题)
31
+
32
+ ---
33
+
34
+ ## ✨ 功能特点
35
+
36
+ | 功能 | 说明 |
37
+ |------|------|
38
+ | 🔌 **多后端支持** | GitHub Copilot、OpenCode Zen、Google Antigravity 三种后端可选 |
39
+ | 🤖 **双协议兼容** | 同时支持 OpenAI Chat Completions API 和 Anthropic Messages API |
40
+ | 💻 **Claude Code 集成** | 一键生成 Claude Code 启动命令 (`--claude-code`) |
41
+ | 📊 **使用量监控** | Web 仪表盘实时查看 API 使用情况 |
42
+ | 🔄 **自动认证** | Token 过期自动刷新,无需手动干预 |
43
+ | ⚡ **速率限制** | 内置请求频率控制,避免触发限制 |
44
+ | 🌐 **代理支持** | 支持 HTTP/HTTPS 代理,配置持久化 |
45
+ | 🐳 **Docker 支持** | 提供完整的 Docker 部署方案 |
46
+ | 🔑 **API Key 认证** | 可选的 API Key 鉴权,保护公开部署的服务 |
47
+ | ✂️ **上下文透传** | 全量透传上下文至上游 API,由客户端(如 Claude Code)自行管理压缩 |
48
+ | 🔍 **智能模型匹配** | 自动处理模型名格式差异(日期后缀、dash/dot 版本号等) |
49
+ | 🔁 **Antigravity 端点容错** | 熔断器状态机、加权账号调度、后台任务自动降级、双端点自动切换 |
50
+
51
+ ---
52
+
53
+ ## 🚀 快速开始
54
+
55
+ ### 安装
56
+
57
+ ```bash
58
+ # 全局安装
59
+ npm install -g copilot-api-plus
60
+
61
+ # 或使用 npx 直接运行(推荐)
62
+ npx copilot-api-plus@latest start
63
+ ```
64
+
65
+ ### 基本用法
66
+
67
+ ```bash
68
+ # 启动服务器(默认使用 GitHub Copilot)
69
+ npx copilot-api-plus@latest start
70
+
71
+ # 使用 OpenCode Zen
72
+ npx copilot-api-plus@latest start --zen
73
+
74
+ # 使用 Google Antigravity
75
+ npx copilot-api-plus@latest start --antigravity
76
+
77
+ # 与 Claude Code 配合
78
+ npx copilot-api-plus@latest start --claude-code
79
+ ```
80
+
81
+ 服务器启动后,默认监听 `http://localhost:4141`。
82
+
83
+ ---
84
+
85
+ ## 📖 详细使用指南
86
+
87
+ ### 1. GitHub Copilot 模式(默认)
88
+
89
+ 使用你的 GitHub Copilot 订阅访问 AI 模型。
90
+
91
+ #### 前置要求
92
+ - GitHub 账户
93
+ - 有效的 Copilot 订阅(Individual / Business / Enterprise)
94
+
95
+ #### 启动步骤
96
+
97
+ ```bash
98
+ npx copilot-api-plus@latest start
99
+ ```
100
+
101
+ **首次运行**会引导你完成 GitHub OAuth 认证:
102
+
103
+ 1. 终端显示设备码,例如:`XXXX-XXXX`
104
+ 2. 打开浏览器访问:https://github.com/login/device
105
+ 3. 输入设备码,点击授权
106
+ 4. 返回终端,等待认证完成
107
+
108
+ 认证成功后,Token 会保存到本地,下次启动无需重新认证。
109
+
110
+ #### 企业/商业账户
111
+
112
+ ```bash
113
+ # Business 计划
114
+ npx copilot-api-plus@latest start --account-type business
115
+
116
+ # Enterprise 计划
117
+ npx copilot-api-plus@latest start --account-type enterprise
118
+ ```
119
+
120
+ #### 可用模型
121
+
122
+ | 模型 | ID | 上下文长度 |
123
+ |------|-----|-----------|
124
+ | Claude Sonnet 4 | `claude-sonnet-4` | 200K |
125
+ | Claude Sonnet 4.5 | `claude-sonnet-4.5` | 200K |
126
+ | GPT-4.1 | `gpt-4.1` | 1M |
127
+ | o4-mini | `o4-mini` | 200K |
128
+ | Gemini 2.5 Pro | `gemini-2.5-pro` | 1M |
129
+
130
+ ---
131
+
132
+ ### 2. OpenCode Zen 模式
133
+
134
+ 使用 [OpenCode Zen](https://opencode.ai/zen) 的多模型 API 服务,支持 GPT-5、Claude、Gemini 等顶级编程模型。
135
+
136
+ #### 前置要求
137
+ 1. 访问 https://opencode.ai/zen
138
+ 2. 注册账号并创建 API Key
139
+
140
+ #### 启动步骤
141
+
142
+ **方式一:交互式设置**
143
+ ```bash
144
+ npx copilot-api-plus@latest start --zen
145
+ ```
146
+ 首次运行会提示输入 API Key,保存后下次自动使用。
147
+
148
+ **方式二:直接指定 API Key**
149
+ ```bash
150
+ npx copilot-api-plus@latest start --zen --zen-api-key YOUR_API_KEY
151
+ ```
152
+
153
+ #### 可用模型
154
+
155
+ | 模型 | ID | 说明 |
156
+ |------|-----|------|
157
+ | GPT-5.2 | `gpt-5.2` | OpenAI 最新模型 |
158
+ | GPT-5.1 Codex Max | `gpt-5.1-codex-max` | 代码优化版 |
159
+ | GPT-5.1 Codex | `gpt-5.1-codex` | 代码专用 |
160
+ | GPT-5 Codex | `gpt-5-codex` | OpenAI Responses API |
161
+ | Claude Opus 4.5 | `claude-opus-4-5` | Anthropic Claude (200K) |
162
+ | Claude Sonnet 4.5 | `claude-sonnet-4-5` | Anthropic Claude (200K) |
163
+ | Claude Sonnet 4 | `claude-sonnet-4` | Anthropic Claude |
164
+ | Gemini 3 Pro | `gemini-3-pro` | Google Gemini |
165
+ | Qwen3 Coder | `qwen3-coder` | Alibaba Qwen |
166
+ | Kimi K2 | `kimi-k2` | Moonshot |
167
+ | Grok Code Fast 1 | `grok-code-fast-1` | xAI |
168
+
169
+ 更多模型请访问 [opencode.ai/zen](https://opencode.ai/zen)
170
+
171
+ #### API 端点
172
+
173
+ Zen 模式支持以下 API 端点:
174
+
175
+ | 端点 | 说明 |
176
+ |------|------|
177
+ | `/v1/chat/completions` | OpenAI 兼容 Chat API |
178
+ | `/v1/messages` | Anthropic 兼容 Messages API |
179
+ | `/v1/responses` | OpenAI Responses API (GPT-5 系列) |
180
+ | `/v1/models` | 获取可用模型列表 |
181
+
182
+ 专用端点(无需 `--zen` 标志也可访问):
183
+ - `/zen/v1/chat/completions`
184
+ - `/zen/v1/messages`
185
+ - `/zen/v1/responses`
186
+ - `/zen/v1/models`
187
+
188
+ #### 管理 API Key
189
+
190
+ ```bash
191
+ # 查看/更换 API Key(清除后重新启动会提示输入)
192
+ npx copilot-api-plus@latest logout --zen
193
+ ```
194
+
195
+ ---
196
+
197
+ ### 3. Google Antigravity 模式
198
+
199
+ 使用 Google Antigravity API 服务,支持 Gemini 和 Claude 模型。
200
+
201
+ #### 前置要求
202
+ - Google 账户
203
+
204
+ #### 认证方式
205
+
206
+ **方式一:API Key(推荐 - 最简单)**
207
+
208
+ 1. 访问 https://aistudio.google.com/apikey 获取 API Key
209
+ 2. 使用环境变量启动:
210
+
211
+ ```bash
212
+ # Linux/macOS
213
+ GEMINI_API_KEY=your_api_key npx copilot-api-plus@latest start --antigravity
214
+
215
+ # Windows PowerShell
216
+ $env:GEMINI_API_KEY = "your_api_key"
217
+ npx copilot-api-plus@latest start --antigravity
218
+
219
+ # Windows CMD
220
+ set GEMINI_API_KEY=your_api_key
221
+ npx copilot-api-plus@latest start --antigravity
222
+ ```
223
+
224
+ **方式二:OAuth 网页登录(推荐)**
225
+
226
+ ```bash
227
+ npx copilot-api-plus@latest start --antigravity
228
+ ```
229
+
230
+ 首次运行会提示选择登录方式:
231
+ - **Web(推荐)**:自动打开浏览器完成 Google 登录,授权后自动捕获回调
232
+ - **Manual**:手动复制回调 URL 到终端
233
+
234
+ **方式三:自定义 OAuth 凭证**
235
+
236
+ 如果遇到 `invalid_client` 错误,可以创建自己的 OAuth 应用:
237
+
238
+ 1. 访问 https://console.cloud.google.com/apis/credentials
239
+ 2. 创建 OAuth 2.0 客户端 ID(选择"桌面应用"类型)
240
+ 3. 添加重定向 URI:`http://localhost:8046/callback`
241
+ 4. 使用环境变量或命令行参数:
242
+
243
+ ```bash
244
+ # 环境变量方式
245
+ ANTIGRAVITY_CLIENT_ID=your_client_id ANTIGRAVITY_CLIENT_SECRET=your_secret \
246
+ npx copilot-api-plus@latest start --antigravity
247
+
248
+ # 命令行参数方式
249
+ npx copilot-api-plus@latest start --antigravity \
250
+ --antigravity-client-id your_client_id \
251
+ --antigravity-client-secret your_secret
252
+ ```
253
+
254
+ #### 可用模型
255
+
256
+ | 模型 | ID | 说明 |
257
+ |------|-----|------|
258
+ | Gemini 2.5 Pro | `gemini-2.5-pro-exp-03-25` | Google Gemini |
259
+ | Gemini 2.5 Pro Preview | `gemini-2.5-pro-preview-05-06` | Google Gemini |
260
+ | Gemini 2.0 Flash | `gemini-2.0-flash-exp` | 快速响应 |
261
+ | Gemini 2.0 Flash Thinking | `gemini-2.0-flash-thinking-exp` | 支持思考链 |
262
+ | Claude Opus 4.5 | `claude-opus-4-5` | Anthropic Claude |
263
+ | Claude Sonnet 4.5 | `claude-sonnet-4-5` | Anthropic Claude |
264
+
265
+ #### 特性
266
+ - ✅ 自动 Token 刷新
267
+ - ✅ 多账户支持,加权智能调度(配额健康度 60% + Token 新鲜度 20% + 可靠性 20%)
268
+ - ✅ 配额用尽自动切换账户
269
+ - ✅ 熔断器状态机(CLOSED OPEN → HALF_OPEN → CLOSED)
270
+ - ✅ 后台/Agent 请求多信号检测,可选模型降级节省配额
271
+ - ✅ 支持 Thinking 模型(思考链输出)
272
+
273
+ #### 多账户管理
274
+
275
+ 可以添加多个 Google 账户,系统会在配额用尽时自动切换:
276
+
277
+ ```bash
278
+ # 添加新账户
279
+ npx copilot-api-plus@latest antigravity add
280
+
281
+ # 列出所有账户
282
+ npx copilot-api-plus@latest antigravity list
283
+
284
+ # 按索引删除账户
285
+ npx copilot-api-plus@latest antigravity remove 0
286
+
287
+ # 清除所有账户
288
+ npx copilot-api-plus@latest antigravity clear
289
+ # 或使用 logout 命令
290
+ npx copilot-api-plus@latest logout --antigravity
291
+ ```
292
+
293
+ ---
294
+
295
+ ## 🌐 代理配置
296
+
297
+ 如果你需要通过代理访问网络,有两种配置方式:
298
+
299
+ ### 方式一:持久化配置(推荐)
300
+
301
+ 配置一次,永久生效,下次启动自动使用。
302
+
303
+ ```bash
304
+ # 交互式配置
305
+ npx copilot-api-plus@latest proxy --set
306
+
307
+ # 或直接设置
308
+ npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890
309
+
310
+ # 同时设置 HTTP 和 HTTPS 代理
311
+ npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890 --https-proxy http://127.0.0.1:7890
312
+ ```
313
+
314
+ #### 代理管理命令
315
+
316
+ ```bash
317
+ # 查看当前代理配置
318
+ npx copilot-api-plus@latest proxy
319
+
320
+ # 启用代理
321
+ npx copilot-api-plus@latest proxy --enable
322
+
323
+ # 禁用代理(保留设置)
324
+ npx copilot-api-plus@latest proxy --disable
325
+
326
+ # 清除代理配置
327
+ npx copilot-api-plus@latest proxy --clear
328
+ ```
329
+
330
+ #### 示例:配置 Clash 代理
331
+
332
+ ```bash
333
+ # Clash 默认端口 7890
334
+ npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890
335
+
336
+ # 验证配置
337
+ npx copilot-api-plus@latest proxy
338
+ # 输出:
339
+ # Current proxy configuration:
340
+ # Status: ✅ Enabled
341
+ # HTTP_PROXY: http://127.0.0.1:7890
342
+ # HTTPS_PROXY: http://127.0.0.1:7890
343
+ ```
344
+
345
+ ### 方式二:环境变量(临时)
346
+
347
+ 仅当次启动生效:
348
+
349
+ ```bash
350
+ # Linux/macOS
351
+ export HTTP_PROXY=http://127.0.0.1:7890
352
+ export HTTPS_PROXY=http://127.0.0.1:7890
353
+ npx copilot-api-plus@latest start --proxy-env
354
+
355
+ # Windows PowerShell
356
+ $env:HTTP_PROXY = "http://127.0.0.1:7890"
357
+ $env:HTTPS_PROXY = "http://127.0.0.1:7890"
358
+ npx copilot-api-plus@latest start --proxy-env
359
+
360
+ # Windows CMD
361
+ set HTTP_PROXY=http://127.0.0.1:7890
362
+ set HTTPS_PROXY=http://127.0.0.1:7890
363
+ npx copilot-api-plus@latest start --proxy-env
364
+ ```
365
+
366
+ ### 代理配置优先级
367
+
368
+ 1. `--proxy-env` 参数(从环境变量读取)
369
+ 2. 持久化配置(`proxy --set` 设置的)
370
+ 3. 无代理
371
+
372
+ ---
373
+
374
+ ## 💻 Claude Code 集成
375
+
376
+ [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) 是 Anthropic 的 AI 编程助手。
377
+
378
+ ### 自动配置(推荐)
379
+
380
+ ```bash
381
+ # 使用 GitHub Copilot 作为后端
382
+ npx copilot-api-plus@latest start --claude-code
383
+
384
+ # 使用 OpenCode Zen 作为后端
385
+ npx copilot-api-plus@latest start --zen --claude-code
386
+
387
+ # 使用 Google Antigravity 作为后端
388
+ npx copilot-api-plus@latest start --antigravity --claude-code
389
+ ```
390
+
391
+ 运行后:
392
+ 1. 选择主模型(用于代码生成)
393
+ 2. 选择快速模型(用于后台任务)
394
+ 3. 启动命令会自动复制到剪贴板
395
+ 4. **打开新终端**,粘贴并运行命令启动 Claude Code
396
+
397
+ ### 手动配置
398
+
399
+ 在项目根目录创建 `.claude/settings.json`:
400
+
401
+ ```json
402
+ {
403
+ "env": {
404
+ "ANTHROPIC_BASE_URL": "http://localhost:4141",
405
+ "ANTHROPIC_AUTH_TOKEN": "dummy",
406
+ "ANTHROPIC_MODEL": "claude-sonnet-4",
407
+ "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1",
408
+ "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1"
409
+ }
410
+ }
411
+ ```
412
+
413
+ 然后启动 copilot-api-plus 服务器后,在该项目目录运行 `claude` 命令。
414
+
415
+ ---
416
+
417
+ ## 🔧 opencode 集成
418
+
419
+ [opencode](https://github.com/sst/opencode) 是一个现代 AI 编程助手。
420
+
421
+ ### 配置步骤
422
+
423
+ 1. 在项目根目录创建 `opencode.json`:
424
+
425
+ ```json
426
+ {
427
+ "$schema": "https://opencode.ai/config.json",
428
+ "provider": {
429
+ "copilot-api-plus": {
430
+ "api": "openai-compatible",
431
+ "name": "Copilot API Plus",
432
+ "options": {
433
+ "baseURL": "http://127.0.0.1:4141/v1"
434
+ },
435
+ "models": {
436
+ "claude-sonnet-4": {
437
+ "name": "Claude Sonnet 4",
438
+ "id": "claude-sonnet-4",
439
+ "max_tokens": 64000,
440
+ "profile": "coder",
441
+ "limit": { "context": 200000 }
442
+ },
443
+ "gpt-4.1": {
444
+ "name": "GPT-4.1",
445
+ "id": "gpt-4.1",
446
+ "max_tokens": 32768,
447
+ "profile": "coder",
448
+ "limit": { "context": 1047576 }
449
+ }
450
+ }
451
+ }
452
+ }
453
+ }
454
+ ```
455
+
456
+ 2. 启动 copilot-api-plus
457
+ ```bash
458
+ npx copilot-api-plus@latest start
459
+ ```
460
+
461
+ 3. 在同一目录运行 opencode
462
+ ```bash
463
+ npx opencode@latest
464
+ ```
465
+
466
+ 4. 选择 `copilot-api-plus` 作为 provider
467
+
468
+ ### 快捷方式:使用环境变量
469
+
470
+ ```bash
471
+ # 设置环境变量
472
+ export OPENAI_BASE_URL=http://127.0.0.1:4141/v1
473
+ export OPENAI_API_KEY=dummy
474
+
475
+ # 运行 opencode
476
+ npx opencode@latest
477
+ ```
478
+
479
+ ---
480
+
481
+ ## 📡 API 端点
482
+
483
+ 服务器启动后,默认监听 `http://localhost:4141`。
484
+
485
+ ### OpenAI 兼容端点
486
+
487
+ | 端点 | 方法 | 说明 |
488
+ |------|------|------|
489
+ | `/v1/chat/completions` | POST | 聊天补全(支持流式) |
490
+ | `/v1/models` | GET | 模型列表 |
491
+ | `/v1/embeddings` | POST | 文本嵌入(仅 Copilot) |
492
+
493
+ ### Anthropic 兼容端点
494
+
495
+ | 端点 | 方法 | 说明 |
496
+ |------|------|------|
497
+ | `/v1/messages` | POST | 消息 API(支持流式) |
498
+ | `/v1/messages/count_tokens` | POST | Token 计数 |
499
+
500
+ ### 专用端点
501
+
502
+ 各后端都有独立的专用路由,即使切换默认后端也能访问:
503
+
504
+ | 路由前缀 | 说明 |
505
+ |----------|------|
506
+ | `/copilot/v1/*` | GitHub Copilot 专用 |
507
+ | `/zen/v1/*` | OpenCode Zen 专用 |
508
+ | `/antigravity/v1/*` | Google Antigravity 专用 |
509
+
510
+ ### 监控端点
511
+
512
+ | 端点 | 方法 | 说明 |
513
+ |------|------|------|
514
+ | `/usage` | GET | 使用量统计(仅 Copilot) |
515
+ | `/token` | GET | 当前 Token 信息 |
516
+
517
+ ### 调用示例
518
+
519
+ ```bash
520
+ # OpenAI 格式
521
+ curl http://localhost:4141/v1/chat/completions \
522
+ -H "Content-Type: application/json" \
523
+ -d '{
524
+ "model": "claude-sonnet-4",
525
+ "messages": [{"role": "user", "content": "Hello!"}]
526
+ }'
527
+
528
+ # Anthropic 格式
529
+ curl http://localhost:4141/v1/messages \
530
+ -H "Content-Type: application/json" \
531
+ -H "x-api-key: dummy" \
532
+ -d '{
533
+ "model": "claude-sonnet-4",
534
+ "max_tokens": 1024,
535
+ "messages": [{"role": "user", "content": "Hello!"}]
536
+ }'
537
+ ```
538
+
539
+ ---
540
+
541
+ ## ⚙️ 命令行参考
542
+
543
+ ### 命令列表
544
+
545
+ | 命令 | 说明 |
546
+ |------|------|
547
+ | `start` | 启动 API 服务器 |
548
+ | `auth` | 仅执行 GitHub 认证流程 |
549
+ | `logout` | 清除已保存的凭证 |
550
+ | `proxy` | 配置代理设置 |
551
+ | `antigravity` | 管理 Google Antigravity 账户 |
552
+ | `check-usage` | 查看 Copilot 使用量 |
553
+ | `debug` | 显示调试信息 |
554
+
555
+ ### start 命令参数
556
+
557
+ | 参数 | 别名 | 默认值 | 说明 |
558
+ |------|------|--------|------|
559
+ | `--port` | `-p` | 4141 | 监听端口 |
560
+ | `--verbose` | `-v` | false | 详细日志 |
561
+ | `--account-type` | `-a` | individual | 账户类型 (individual/business/enterprise) |
562
+ | `--claude-code` | `-c` | false | 生成 Claude Code 启动命令 |
563
+ | `--zen` | `-z` | false | 启用 OpenCode Zen 模式 |
564
+ | `--zen-api-key` | - | - | Zen API Key |
565
+ | `--antigravity` | - | false | 启用 Google Antigravity 模式 |
566
+ | `--antigravity-client-id` | - | - | Antigravity OAuth Client ID |
567
+ | `--antigravity-client-secret` | - | - | Antigravity OAuth Client Secret |
568
+ | `--rate-limit` | `-r` | - | 请求间隔(秒) |
569
+ | `--wait` | `-w` | false | 达到限制时等待而非报错 |
570
+ | `--manual` | - | false | 手动审批每个请求 |
571
+ | `--github-token` | `-g` | - | 直接提供 GitHub Token |
572
+ | `--show-token` | - | false | 显示 Token 信息 |
573
+ | `--proxy-env` | - | false | 从环境变量读取代理 |
574
+ | `--api-key` | - | - | API Key 鉴权(可多次指定) |
575
+
576
+ ### proxy 命令参数
577
+
578
+ | 参数 | 说明 |
579
+ |------|------|
580
+ | `--set` | 交互式配置代理 |
581
+ | `--enable` | 启用已保存的代理 |
582
+ | `--disable` | 禁用代理(保留设置) |
583
+ | `--clear` | 清除代理配置 |
584
+ | `--show` | 显示当前配置 |
585
+ | `--http-proxy` | HTTP 代理 URL |
586
+ | `--https-proxy` | HTTPS 代理 URL |
587
+ | `--no-proxy` | 不走代理的主机列表 |
588
+
589
+ ### logout 命令参数
590
+
591
+ | 参数 | 别名 | 说明 |
592
+ |------|------|------|
593
+ | `--github` | `-g` | 仅清除 GitHub Copilot 凭证 |
594
+ | `--zen` | `-z` | 仅清除 Zen 凭证 |
595
+ | `--antigravity` | - | 仅清除 Antigravity 凭证 |
596
+ | `--all` | `-a` | 清除所有凭证 |
597
+
598
+ > **提示**:不带参数运行 `logout` 会显示交互式菜单供选择。
599
+
600
+ ### antigravity 命令
601
+
602
+ 管理 Google Antigravity 账户的子命令:
603
+
604
+ | 子命令 | 说明 |
605
+ |--------|------|
606
+ | `add` | 添加新的 Antigravity 账户(OAuth 登录) |
607
+ | `list` | 列出所有已配置的账户及其状态 |
608
+ | `remove <index>` | 按索引删除指定账户 |
609
+ | `clear` | 清除所有 Antigravity 账户(需确认) |
610
+
611
+ ```bash
612
+ # 示例
613
+ npx copilot-api-plus@latest antigravity add # 添加账户
614
+ npx copilot-api-plus@latest antigravity list # 列出账户
615
+ npx copilot-api-plus@latest antigravity remove 0 # 删除索引为 0 的账户
616
+ npx copilot-api-plus@latest antigravity clear # 清除所有账户
617
+ ```
618
+
619
+ ---
620
+
621
+ ## 🔑 API Key 认证
622
+
623
+ 如果你将服务暴露到公网,可以启用 API Key 认证来保护接口:
624
+
625
+ ```bash
626
+ # 单个 Key
627
+ npx copilot-api-plus@latest start --api-key my-secret-key
628
+
629
+ # 多个 Key
630
+ npx copilot-api-plus@latest start --api-key key1 --api-key key2
631
+ ```
632
+
633
+ 启用后,所有请求需要携带 API Key:
634
+
635
+ ```bash
636
+ # OpenAI 格式 - 通过 Authorization header
637
+ curl http://localhost:4141/v1/chat/completions \
638
+ -H "Authorization: Bearer my-secret-key" \
639
+ -H "Content-Type: application/json" \
640
+ -d '{"model": "claude-sonnet-4", "messages": [{"role": "user", "content": "Hello"}]}'
641
+
642
+ # Anthropic 格式 - 通过 x-api-key header
643
+ curl http://localhost:4141/v1/messages \
644
+ -H "x-api-key: my-secret-key" \
645
+ -H "Content-Type: application/json" \
646
+ -d '{"model": "claude-sonnet-4", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}'
647
+ ```
648
+
649
+ 在 Claude Code 中使用时,将 `ANTHROPIC_AUTH_TOKEN` 设为你的 API Key 即可。
650
+
651
+ ---
652
+
653
+ ## 🔧 技术细节
654
+
655
+ ### 上下文管理
656
+
657
+ 代理层不做上下文截断,全量透传消息至上游 API。上下文压缩由客户端负责:
658
+
659
+ - **Claude Code**:通过 `/count_tokens` 端点获取当前 token 数,接近上限时自动触发 `/compact` 压缩
660
+ - **其他客户端**:如果上游 API 返回 400(token 超限),客户端自行处理重试
661
+
662
+ ### 智能模型名匹配
663
+
664
+ Anthropic 格式的模型名(如 `claude-opus-4-6`)和 Copilot 的模型列表 ID 可能存在格式差异。代理使用多策略精确匹配:
665
+
666
+ | 策略 | 示例 |
667
+ |------|------|
668
+ | 精确匹配 | `claude-opus-4-6` → `claude-opus-4-6` |
669
+ | 去日期后缀 | `claude-opus-4-6-20251101` → `claude-opus-4-6` |
670
+ | Dash → Dot | `claude-opus-4-5` → `claude-opus-4.5` |
671
+ | Dot Dash | `claude-opus-4.5` → `claude-opus-4-5` |
672
+
673
+ 对于 Anthropic 端点(`/v1/messages`),还会先通过 `translateModelName` 做格式转换(包括旧格式 `claude-3-5-sonnet` → `claude-sonnet-4.5` 的映射),再通过上述策略匹配。
674
+
675
+ ### Antigravity 端点容错
676
+
677
+ Google Antigravity 模式内置了多层可靠性保障:
678
+
679
+ - **熔断器状态机**:按模型族(claude/gemini/other)独立管理,3 次失败后熔断(OPEN),30 秒后半开(HALF_OPEN)试探,连续 2 次成功则恢复(CLOSED)
680
+ - **加权账号调度**:替代简单轮换,综合评分 `score = 配额健康度×0.6 + Token新鲜度×0.2 + 可靠性×0.2`,优先选用最健康的账号
681
+ - **后台任务检测与降级**:多信号加权检测(tool_calls +0.5、tool 角色 +0.4、助手密度 +0.2、长对话 +0.1),得分 ≥ 0.6 判定为 Agent 请求,可自动降级高价模型(如 claude-sonnet-4-5 → gemini-2.5-flash)。通过 `ANTIGRAVITY_BACKGROUND_DOWNGRADE=1` 环境变量启用,默认关闭
682
+ - **双端点自动切换**:daily sandbox 和 production 两个端点,一个失败自动切换到另一个
683
+ - **指数退避重试**:429/503 等限流错误自动退避重试,短间隔走同端点,长间隔切换端点
684
+
685
+ #### 后台降级环境变量
686
+
687
+ | 变量 | 默认 | 说明 |
688
+ |------|------|------|
689
+ | `ANTIGRAVITY_BACKGROUND_DOWNGRADE` | `0` (关闭) | 设为 `1` 启用 Agent/后台请求自动模型降级 |
690
+
691
+ 降级映射:
692
+
693
+ | 原始模型 | 降级后 |
694
+ |----------|--------|
695
+ | `claude-sonnet-4-5` | `gemini-2.5-flash` |
696
+ | `claude-sonnet-4-5-thinking` | `gemini-2.5-flash-thinking` |
697
+ | `claude-opus-4-5-thinking` | `claude-sonnet-4-5-thinking` |
698
+ | `gemini-2.5-pro` | `gemini-2.5-flash` |
699
+ | `gemini-3-pro-high` | `gemini-3-flash` |
700
+ | `gemini-3-pro-low` | `gemini-3-flash` |
701
+
702
+ 客户端也可通过请求头 `X-Request-Type: background` 显式标记后台请求,无需检测直接生效。
703
+
704
+ ### 请求日志
705
+
706
+ 每次 API 请求会输出一行日志,包含模型名、状态码和耗时:
707
+
708
+ ```log
709
+ [claude-opus-4-6] 13:13:39 <-- POST /v1/messages?beta=true
710
+ [claude-opus-4-6] 13:13:59 --> POST /v1/messages?beta=true 200 20.1s
711
+ ```
712
+
713
+ ### 网络重试
714
+
715
+ 对上游 API 的请求内置了瞬时网络错误重试(TLS 断开、连接重置等):
716
+
717
+ - 最多重试 2 次(共 3 次尝试)
718
+ - 退避间隔:1s、2s
719
+ - 仅重试网络层错误,HTTP 错误码(如 400/500)不重试
720
+
721
+ ---
722
+
723
+ ## 🐳 Docker 部署
724
+
725
+ ### 快速启动
726
+
727
+ ```bash
728
+ # 使用预构建镜像
729
+ docker run -p 4141:4141 \
730
+ -v ./copilot-data:/root/.local/share/copilot-api-plus \
731
+ ghcr.io/imbuxiangnan-cyber/copilot-api-plus
732
+ ```
733
+
734
+ ### 自行构建
735
+
736
+ ```bash
737
+ # 构建镜像
738
+ docker build -t copilot-api-plus .
739
+
740
+ # 运行容器
741
+ docker run -p 4141:4141 \
742
+ -v ./copilot-data:/root/.local/share/copilot-api-plus \
743
+ copilot-api-plus
744
+ ```
745
+
746
+ ### Docker Compose
747
+
748
+ ```yaml
749
+ version: "3.8"
750
+ services:
751
+ copilot-api-plus:
752
+ build: .
753
+ ports:
754
+ - "4141:4141"
755
+ volumes:
756
+ - ./copilot-data:/root/.local/share/copilot-api-plus
757
+ environment:
758
+ - GH_TOKEN=your_github_token # 可选
759
+ restart: unless-stopped
760
+ ```
761
+
762
+ ### 使用代理
763
+
764
+ ```bash
765
+ docker run -p 4141:4141 \
766
+ -e HTTP_PROXY=http://host.docker.internal:7890 \
767
+ -e HTTPS_PROXY=http://host.docker.internal:7890 \
768
+ -v ./copilot-data:/root/.local/share/copilot-api-plus \
769
+ copilot-api-plus start --proxy-env
770
+ ```
771
+
772
+ ---
773
+
774
+ ## ❓ 常见问题
775
+
776
+ ### 数据存储位置
777
+
778
+ 所有数据存储在 `~/.local/share/copilot-api-plus/` 目录下:
779
+
780
+ | 文件 | 说明 |
781
+ |------|------|
782
+ | `github_token` | GitHub Token |
783
+ | `zen-auth.json` | Zen API Key |
784
+ | `antigravity-accounts.json` | Antigravity 账户 |
785
+ | `config.json` | 代理等配置 |
786
+
787
+ ### 切换账户
788
+
789
+ ```bash
790
+ # 交互式选择要清除的凭证
791
+ npx copilot-api-plus@latest logout
792
+
793
+ # 仅清除 GitHub Copilot 凭证
794
+ npx copilot-api-plus@latest logout --github
795
+ # 或简写
796
+ npx copilot-api-plus@latest logout -g
797
+
798
+ # 清除 Zen 凭证
799
+ npx copilot-api-plus@latest logout --zen
800
+
801
+ # 清除 Antigravity 凭证
802
+ npx copilot-api-plus@latest logout --antigravity
803
+
804
+ # 清除所有凭证
805
+ npx copilot-api-plus@latest logout --all
806
+ ```
807
+
808
+ ### 查看使用量
809
+
810
+ ```bash
811
+ # 命令行查看(仅 Copilot)
812
+ npx copilot-api-plus@latest check-usage
813
+ ```
814
+
815
+ 启动服务器后,也可以访问 Web 仪表盘:
816
+ ```
817
+ https://imbuxiangnan-cyber.github.io/copilot-api-plus?endpoint=http://localhost:4141/usage
818
+ ```
819
+
820
+ ### 调试问题
821
+
822
+ ```bash
823
+ # 显示调试信息
824
+ npx copilot-api-plus@latest debug
825
+
826
+ # JSON 格式输出
827
+ npx copilot-api-plus@latest debug --json
828
+
829
+ # 启用详细日志
830
+ npx copilot-api-plus@latest start --verbose
831
+ ```
832
+
833
+ ### 速率限制
834
+
835
+ 避免触发 GitHub 的滥用检测:
836
+
837
+ ```bash
838
+ # 设置请求间隔 30 秒
839
+ npx copilot-api-plus@latest start --rate-limit 30
840
+
841
+ # 达到限制时等待而非报错
842
+ npx copilot-api-plus@latest start --rate-limit 30 --wait
843
+
844
+ # 手动审批每个请求
845
+ npx copilot-api-plus@latest start --manual
846
+ ```
847
+
848
+ ---
849
+
850
+ ## ⚠️ 免责声明
851
+
852
+ > [!WARNING]
853
+ > 这是 GitHub Copilot API 的逆向工程代理。**不受 GitHub 官方支持**,可能随时失效。使用风险自负。
854
+
855
+ > [!WARNING]
856
+ > **GitHub 安全提示**:过度的自动化或脚本化使用 Copilot 可能触发 GitHub 的滥用检测系统,导致 Copilot 访问被暂停。请负责任地使用。
857
+ >
858
+ > 相关政策:
859
+ > - [GitHub 可接受使用政策](https://docs.github.com/site-policy/acceptable-use-policies/github-acceptable-use-policies)
860
+ > - [GitHub Copilot 条款](https://docs.github.com/site-policy/github-terms/github-terms-for-additional-products-and-features#github-copilot)
861
+
862
+ ---
863
+
864
+ ## 📄 许可证
865
+
866
+ MIT License