copilot-api-plus 1.0.56 → 1.0.58

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,620 @@
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 into OpenAI & Anthropic compatible APIs. Works with Claude Code, opencode, and more.
9
+
10
+ 将 GitHub Copilot 转换为 **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
+ - [代理配置](#-代理配置)
21
+ - [Claude Code 集成](#-claude-code-集成)
22
+ - [opencode 集成](#-opencode-集成)
23
+ - [API 端点](#-api-端点)
24
+ - [API Key 认证](#-api-key-认证)
25
+ - [技术细节](#-技术细节)
26
+ - [命令行参考](#️-命令行参考)
27
+ - [Docker 部署](#-docker-部署)
28
+ - [常见问题](#-常见问题)
29
+
30
+ ---
31
+
32
+ ## ✨ 功能特点
33
+
34
+ | 功能 | 说明 |
35
+ |------|------|
36
+ | 🤖 **双协议兼容** | 同时支持 OpenAI Chat Completions API 和 Anthropic Messages API |
37
+ | 💻 **Claude Code 集成** | 一键生成 Claude Code 启动命令 (`--claude-code`) |
38
+ | 📊 **使用量监控** | Web 仪表盘实时查看 API 使用情况 |
39
+ | 🔄 **自动认证** | Token 过期自动刷新,无需手动干预 |
40
+ | **速率限制** | 内置请求频率控制,避免触发限制 |
41
+ | 🌐 **代理支持** | 支持 HTTP/HTTPS 代理,配置持久化 |
42
+ | 🐳 **Docker 支持** | 提供完整的 Docker 部署方案 |
43
+ | 🔑 **API Key 认证** | 可选的 API Key 鉴权,保护公开部署的服务 |
44
+ | ✂️ **上下文透传** | 全量透传上下文至上游 API,由客户端(如 Claude Code)自行管理压缩 |
45
+ | 🔍 **智能模型匹配** | 自动处理模型名格式差异(日期后缀、dash/dot 版本号等) |
46
+
47
+ ---
48
+
49
+ ## 🚀 快速开始
50
+
51
+ ### 安装
52
+
53
+ ```bash
54
+ # 全局安装
55
+ npm install -g copilot-api-plus
56
+
57
+ # 或使用 npx 直接运行(推荐)
58
+ npx copilot-api-plus@latest start
59
+ ```
60
+
61
+ ### 基本用法
62
+
63
+ ```bash
64
+ # 启动服务器(默认使用 GitHub Copilot)
65
+ npx copilot-api-plus@latest start
66
+
67
+ # 与 Claude Code 配合
68
+ npx copilot-api-plus@latest start --claude-code
69
+ ```
70
+
71
+ 服务器启动后,默认监听 `http://localhost:4141`。
72
+
73
+ ---
74
+
75
+ ## 📖 详细使用指南
76
+
77
+ ### 1. GitHub Copilot 模式(默认)
78
+
79
+ 使用你的 GitHub Copilot 订阅访问 AI 模型。
80
+
81
+ #### 前置要求
82
+ - GitHub 账户
83
+ - 有效的 Copilot 订阅(Individual / Business / Enterprise)
84
+
85
+ #### 启动步骤
86
+
87
+ ```bash
88
+ npx copilot-api-plus@latest start
89
+ ```
90
+
91
+ **首次运行**会引导你完成 GitHub OAuth 认证:
92
+
93
+ 1. 终端显示设备码,例如:`XXXX-XXXX`
94
+ 2. 打开浏览器访问:https://github.com/login/device
95
+ 3. 输入设备码,点击授权
96
+ 4. 返回终端,等待认证完成
97
+
98
+ 认证成功后,Token 会保存到本地,下次启动无需重新认证。
99
+
100
+ #### 企业/商业账户
101
+
102
+ ```bash
103
+ # Business 计划
104
+ npx copilot-api-plus@latest start --account-type business
105
+
106
+ # Enterprise 计划
107
+ npx copilot-api-plus@latest start --account-type enterprise
108
+ ```
109
+
110
+ #### 可用模型
111
+
112
+ | 模型 | ID | 上下文长度 |
113
+ |------|-----|-----------|
114
+ | Claude Sonnet 4 | `claude-sonnet-4` | 200K |
115
+ | Claude Sonnet 4.5 | `claude-sonnet-4.5` | 200K |
116
+ | GPT-4.1 | `gpt-4.1` | 1M |
117
+ | o4-mini | `o4-mini` | 200K |
118
+ | Gemini 2.5 Pro | `gemini-2.5-pro` | 1M |
119
+
120
+ ---
121
+
122
+ ## 🌐 代理配置
123
+
124
+ 如果你需要通过代理访问网络,有两种配置方式:
125
+
126
+ ### 方式一:持久化配置(推荐)
127
+
128
+ 配置一次,永久生效,下次启动自动使用。
129
+
130
+ ```bash
131
+ # 交互式配置
132
+ npx copilot-api-plus@latest proxy --set
133
+
134
+ # 或直接设置
135
+ npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890
136
+
137
+ # 同时设置 HTTP 和 HTTPS 代理
138
+ npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890 --https-proxy http://127.0.0.1:7890
139
+ ```
140
+
141
+ #### 代理管理命令
142
+
143
+ ```bash
144
+ # 查看当前代理配置
145
+ npx copilot-api-plus@latest proxy
146
+
147
+ # 启用代理
148
+ npx copilot-api-plus@latest proxy --enable
149
+
150
+ # 禁用代理(保留设置)
151
+ npx copilot-api-plus@latest proxy --disable
152
+
153
+ # 清除代理配置
154
+ npx copilot-api-plus@latest proxy --clear
155
+ ```
156
+
157
+ #### 示例:配置 Clash 代理
158
+
159
+ ```bash
160
+ # Clash 默认端口 7890
161
+ npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890
162
+
163
+ # 验证配置
164
+ npx copilot-api-plus@latest proxy
165
+ # 输出:
166
+ # Current proxy configuration:
167
+ # Status: Enabled
168
+ # HTTP_PROXY: http://127.0.0.1:7890
169
+ # HTTPS_PROXY: http://127.0.0.1:7890
170
+ ```
171
+
172
+ ### 方式二:环境变量(临时)
173
+
174
+ 仅当次启动生效:
175
+
176
+ ```bash
177
+ # Linux/macOS
178
+ export HTTP_PROXY=http://127.0.0.1:7890
179
+ export HTTPS_PROXY=http://127.0.0.1:7890
180
+ npx copilot-api-plus@latest start --proxy-env
181
+
182
+ # Windows PowerShell
183
+ $env:HTTP_PROXY = "http://127.0.0.1:7890"
184
+ $env:HTTPS_PROXY = "http://127.0.0.1:7890"
185
+ npx copilot-api-plus@latest start --proxy-env
186
+
187
+ # Windows CMD
188
+ set HTTP_PROXY=http://127.0.0.1:7890
189
+ set HTTPS_PROXY=http://127.0.0.1:7890
190
+ npx copilot-api-plus@latest start --proxy-env
191
+ ```
192
+
193
+ ### 代理配置优先级
194
+
195
+ 1. `--proxy-env` 参数(从环境变量读取)
196
+ 2. 持久化配置(`proxy --set` 设置的)
197
+ 3. 无代理
198
+
199
+ ---
200
+
201
+ ## 💻 Claude Code 集成
202
+
203
+ [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) 是 Anthropic 的 AI 编程助手。
204
+
205
+ ### 自动配置(推荐)
206
+
207
+ ```bash
208
+ npx copilot-api-plus@latest start --claude-code
209
+ ```
210
+
211
+ 运行后:
212
+ 1. 选择主模型(用于代码生成)
213
+ 2. 选择快速模型(用于后台任务)
214
+ 3. 启动命令会自动复制到剪贴板
215
+ 4. **打开新终端**,粘贴并运行命令启动 Claude Code
216
+
217
+ ### 手动配置
218
+
219
+ 在项目根目录创建 `.claude/settings.json`:
220
+
221
+ ```json
222
+ {
223
+ "env": {
224
+ "ANTHROPIC_BASE_URL": "http://localhost:4141",
225
+ "ANTHROPIC_AUTH_TOKEN": "dummy",
226
+ "ANTHROPIC_MODEL": "claude-sonnet-4",
227
+ "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1",
228
+ "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1"
229
+ }
230
+ }
231
+ ```
232
+
233
+ 然后启动 copilot-api-plus 服务器后,在该项目目录运行 `claude` 命令。
234
+
235
+ ---
236
+
237
+ ## 🔧 opencode 集成
238
+
239
+ [opencode](https://github.com/sst/opencode) 是一个现代 AI 编程助手。
240
+
241
+ ### 配置步骤
242
+
243
+ 1. 在项目根目录创建 `opencode.json`:
244
+
245
+ ```json
246
+ {
247
+ "$schema": "https://opencode.ai/config.json",
248
+ "provider": {
249
+ "copilot-api-plus": {
250
+ "api": "openai-compatible",
251
+ "name": "Copilot API Plus",
252
+ "options": {
253
+ "baseURL": "http://127.0.0.1:4141/v1"
254
+ },
255
+ "models": {
256
+ "claude-sonnet-4": {
257
+ "name": "Claude Sonnet 4",
258
+ "id": "claude-sonnet-4",
259
+ "max_tokens": 64000,
260
+ "profile": "coder",
261
+ "limit": { "context": 200000 }
262
+ },
263
+ "gpt-4.1": {
264
+ "name": "GPT-4.1",
265
+ "id": "gpt-4.1",
266
+ "max_tokens": 32768,
267
+ "profile": "coder",
268
+ "limit": { "context": 1047576 }
269
+ }
270
+ }
271
+ }
272
+ }
273
+ }
274
+ ```
275
+
276
+ 2. 启动 copilot-api-plus:
277
+ ```bash
278
+ npx copilot-api-plus@latest start
279
+ ```
280
+
281
+ 3. 在同一目录运行 opencode:
282
+ ```bash
283
+ npx opencode@latest
284
+ ```
285
+
286
+ 4. 选择 `copilot-api-plus` 作为 provider
287
+
288
+ ### 快捷方式:使用环境变量
289
+
290
+ ```bash
291
+ # 设置环境变量
292
+ export OPENAI_BASE_URL=http://127.0.0.1:4141/v1
293
+ export OPENAI_API_KEY=dummy
294
+
295
+ # 运行 opencode
296
+ npx opencode@latest
297
+ ```
298
+
299
+ ---
300
+
301
+ ## 📡 API 端点
302
+
303
+ 服务器启动后,默认监听 `http://localhost:4141`。
304
+
305
+ ### OpenAI 兼容端点
306
+
307
+ | 端点 | 方法 | 说明 |
308
+ |------|------|------|
309
+ | `/v1/chat/completions` | POST | 聊天补全(支持流式) |
310
+ | `/v1/models` | GET | 模型列表 |
311
+ | `/v1/embeddings` | POST | 文本嵌入(仅 Copilot) |
312
+
313
+ ### Anthropic 兼容端点
314
+
315
+ | 端点 | 方法 | 说明 |
316
+ |------|------|------|
317
+ | `/v1/messages` | POST | 消息 API(支持流式) |
318
+ | `/v1/messages/count_tokens` | POST | Token 计数 |
319
+
320
+ ### 专用端点
321
+
322
+ GitHub Copilot 有独立的专用路由:
323
+
324
+ | 路由前缀 | 说明 |
325
+ |----------|------|
326
+ | `/copilot/v1/*` | GitHub Copilot 专用 |
327
+
328
+ ### 监控端点
329
+
330
+ | 端点 | 方法 | 说明 |
331
+ |------|------|------|
332
+ | `/usage` | GET | 使用量统计(仅 Copilot) |
333
+ | `/token` | GET | 当前 Token 信息 |
334
+
335
+ ### 调用示例
336
+
337
+ ```bash
338
+ # OpenAI 格式
339
+ curl http://localhost:4141/v1/chat/completions \
340
+ -H "Content-Type: application/json" \
341
+ -d '{
342
+ "model": "claude-sonnet-4",
343
+ "messages": [{"role": "user", "content": "Hello!"}]
344
+ }'
345
+
346
+ # Anthropic 格式
347
+ curl http://localhost:4141/v1/messages \
348
+ -H "Content-Type: application/json" \
349
+ -H "x-api-key: dummy" \
350
+ -d '{
351
+ "model": "claude-sonnet-4",
352
+ "max_tokens": 1024,
353
+ "messages": [{"role": "user", "content": "Hello!"}]
354
+ }'
355
+ ```
356
+
357
+ ---
358
+
359
+ ## ⚙️ 命令行参考
360
+
361
+ ### 命令列表
362
+
363
+ | 命令 | 说明 |
364
+ |------|------|
365
+ | `start` | 启动 API 服务器 |
366
+ | `auth` | 仅执行 GitHub 认证流程 |
367
+ | `logout` | 清除已保存的凭证 |
368
+ | `proxy` | 配置代理设置 |
369
+ | `check-usage` | 查看 Copilot 使用量 |
370
+ | `debug` | 显示调试信息 |
371
+
372
+ ### start 命令参数
373
+
374
+ | 参数 | 别名 | 默认值 | 说明 |
375
+ |------|------|--------|------|
376
+ | `--port` | `-p` | 4141 | 监听端口 |
377
+ | `--verbose` | `-v` | false | 详细日志 |
378
+ | `--account-type` | `-a` | individual | 账户类型 (individual/business/enterprise) |
379
+ | `--claude-code` | `-c` | false | 生成 Claude Code 启动命令 |
380
+ | `--rate-limit` | `-r` | - | 请求间隔(秒) |
381
+ | `--wait` | `-w` | false | 达到限制时等待而非报错 |
382
+ | `--manual` | - | false | 手动审批每个请求 |
383
+ | `--github-token` | `-g` | - | 直接提供 GitHub Token |
384
+ | `--show-token` | - | false | 显示 Token 信息 |
385
+ | `--proxy-env` | - | false | 从环境变量读取代理 |
386
+ | `--api-key` | - | - | API Key 鉴权(可多次指定) |
387
+
388
+ ### proxy 命令参数
389
+
390
+ | 参数 | 说明 |
391
+ |------|------|
392
+ | `--set` | 交互式配置代理 |
393
+ | `--enable` | 启用已保存的代理 |
394
+ | `--disable` | 禁用代理(保留设置) |
395
+ | `--clear` | 清除代理配置 |
396
+ | `--show` | 显示当前配置 |
397
+ | `--http-proxy` | HTTP 代理 URL |
398
+ | `--https-proxy` | HTTPS 代理 URL |
399
+ | `--no-proxy` | 不走代理的主机列表 |
400
+
401
+ ### logout 命令参数
402
+
403
+ | 参数 | 别名 | 说明 |
404
+ |------|------|------|
405
+ | `--github` | `-g` | 仅清除 GitHub Copilot 凭证 |
406
+ | `--all` | `-a` | 清除所有凭证 |
407
+
408
+ > **提示**:不带参数运行 `logout` 会显示交互式菜单供选择。
409
+
410
+ ---
411
+
412
+ ## 🔑 API Key 认证
413
+
414
+ 如果你将服务暴露到公网,可以启用 API Key 认证来保护接口:
415
+
416
+ ```bash
417
+ # 单个 Key
418
+ npx copilot-api-plus@latest start --api-key my-secret-key
419
+
420
+ # 多个 Key
421
+ npx copilot-api-plus@latest start --api-key key1 --api-key key2
422
+ ```
423
+
424
+ 启用后,所有请求需要携带 API Key:
425
+
426
+ ```bash
427
+ # OpenAI 格式 - 通过 Authorization header
428
+ curl http://localhost:4141/v1/chat/completions \
429
+ -H "Authorization: Bearer my-secret-key" \
430
+ -H "Content-Type: application/json" \
431
+ -d '{"model": "claude-sonnet-4", "messages": [{"role": "user", "content": "Hello"}]}'
432
+
433
+ # Anthropic 格式 - 通过 x-api-key header
434
+ curl http://localhost:4141/v1/messages \
435
+ -H "x-api-key: my-secret-key" \
436
+ -H "Content-Type: application/json" \
437
+ -d '{"model": "claude-sonnet-4", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}'
438
+ ```
439
+
440
+ 在 Claude Code 中使用时,将 `ANTHROPIC_AUTH_TOKEN` 设为你的 API Key 即可。
441
+
442
+ ---
443
+
444
+ ## 🔧 技术细节
445
+
446
+ ### 上下文管理
447
+
448
+ 代理层不做上下文截断,全量透传消息至上游 API。上下文压缩由客户端负责:
449
+
450
+ - **Claude Code**:通过 `/count_tokens` 端点获取当前 token 数,接近上限时自动触发 `/compact` 压缩
451
+ - **其他客户端**:如果上游 API 返回 400(token 超限),客户端自行处理重试
452
+
453
+ ### 智能模型名匹配
454
+
455
+ Anthropic 格式的模型名(如 `claude-opus-4-6`)和 Copilot 的模型列表 ID 可能存在格式差异。代理使用多策略精确匹配:
456
+
457
+ | 策略 | 示例 |
458
+ |------|------|
459
+ | 精确匹配 | `claude-opus-4-6` → `claude-opus-4-6` |
460
+ | 去日期后缀 | `claude-opus-4-6-20251101` → `claude-opus-4-6` |
461
+ | Dash → Dot | `claude-opus-4-5` → `claude-opus-4.5` |
462
+ | Dot → Dash | `claude-opus-4.5` → `claude-opus-4-5` |
463
+
464
+ 对于 Anthropic 端点(`/v1/messages`),还会先通过 `translateModelName` 做格式转换(包括旧格式 `claude-3-5-sonnet` `claude-sonnet-4.5` 的映射),再通过上述策略匹配。
465
+
466
+ ### 请求日志
467
+
468
+ 每次 API 请求会输出一行日志,包含模型名、状态码和耗时:
469
+
470
+ ```log
471
+ [claude-opus-4-6] 13:13:39 <-- POST /v1/messages?beta=true
472
+ [claude-opus-4-6] 13:13:59 --> POST /v1/messages?beta=true 200 20.1s
473
+ ```
474
+
475
+ ### 网络重试
476
+
477
+ 对上游 API 的请求内置了瞬时网络错误重试(TLS 断开、连接重置等):
478
+
479
+ - 最多重试 2 次(共 3 次尝试)
480
+ - 退避间隔:1s、2s
481
+ - 仅重试网络层错误,HTTP 错误码(如 400/500)不重试
482
+
483
+ ---
484
+
485
+ ## 🐳 Docker 部署
486
+
487
+ ### 快速启动
488
+
489
+ ```bash
490
+ # 使用预构建镜像
491
+ docker run -p 4141:4141 \
492
+ -v ./copilot-data:/root/.local/share/copilot-api-plus \
493
+ ghcr.io/imbuxiangnan-cyber/copilot-api-plus
494
+ ```
495
+
496
+ ### 自行构建
497
+
498
+ ```bash
499
+ # 构建镜像
500
+ docker build -t copilot-api-plus .
501
+
502
+ # 运行容器
503
+ docker run -p 4141:4141 \
504
+ -v ./copilot-data:/root/.local/share/copilot-api-plus \
505
+ copilot-api-plus
506
+ ```
507
+
508
+ ### Docker Compose
509
+
510
+ ```yaml
511
+ version: "3.8"
512
+ services:
513
+ copilot-api-plus:
514
+ build: .
515
+ ports:
516
+ - "4141:4141"
517
+ volumes:
518
+ - ./copilot-data:/root/.local/share/copilot-api-plus
519
+ environment:
520
+ - GH_TOKEN=your_github_token # 可选
521
+ restart: unless-stopped
522
+ ```
523
+
524
+ ### 使用代理
525
+
526
+ ```bash
527
+ docker run -p 4141:4141 \
528
+ -e HTTP_PROXY=http://host.docker.internal:7890 \
529
+ -e HTTPS_PROXY=http://host.docker.internal:7890 \
530
+ -v ./copilot-data:/root/.local/share/copilot-api-plus \
531
+ copilot-api-plus start --proxy-env
532
+ ```
533
+
534
+ ---
535
+
536
+ ## ❓ 常见问题
537
+
538
+ ### 数据存储位置
539
+
540
+ 所有数据存储在 `~/.local/share/copilot-api-plus/` 目录下:
541
+
542
+ | 文件 | 说明 |
543
+ |------|------|
544
+ | `github_token` | GitHub Token |
545
+ | `config.json` | 代理等配置 |
546
+
547
+ ### 切换账户
548
+
549
+ ```bash
550
+ # 交互式选择要清除的凭证
551
+ npx copilot-api-plus@latest logout
552
+
553
+ # 仅清除 GitHub Copilot 凭证
554
+ npx copilot-api-plus@latest logout --github
555
+ # 或简写
556
+ npx copilot-api-plus@latest logout -g
557
+
558
+ # 清除所有凭证
559
+ npx copilot-api-plus@latest logout --all
560
+ ```
561
+
562
+ ### 查看使用量
563
+
564
+ ```bash
565
+ # 命令行查看(仅 Copilot)
566
+ npx copilot-api-plus@latest check-usage
567
+ ```
568
+
569
+ 启动服务器后,也可以访问 Web 仪表盘:
570
+ ```
571
+ https://imbuxiangnan-cyber.github.io/copilot-api-plus?endpoint=http://localhost:4141/usage
572
+ ```
573
+
574
+ ### 调试问题
575
+
576
+ ```bash
577
+ # 显示调试信息
578
+ npx copilot-api-plus@latest debug
579
+
580
+ # JSON 格式输出
581
+ npx copilot-api-plus@latest debug --json
582
+
583
+ # 启用详细日志
584
+ npx copilot-api-plus@latest start --verbose
585
+ ```
586
+
587
+ ### 速率限制
588
+
589
+ 避免触发 GitHub 的滥用检测:
590
+
591
+ ```bash
592
+ # 设置请求间隔 30
593
+ npx copilot-api-plus@latest start --rate-limit 30
594
+
595
+ # 达到限制时等待而非报错
596
+ npx copilot-api-plus@latest start --rate-limit 30 --wait
597
+
598
+ # 手动审批每个请求
599
+ npx copilot-api-plus@latest start --manual
600
+ ```
601
+
602
+ ---
603
+
604
+ ## ⚠️ 免责声明
605
+
606
+ > [!WARNING]
607
+ > 这是 GitHub Copilot API 的逆向工程代理。**不受 GitHub 官方支持**,可能随时失效。使用风险自负。
608
+
609
+ > [!WARNING]
610
+ > **GitHub 安全提示**:过度的自动化或脚本化使用 Copilot 可能触发 GitHub 的滥用检测系统,导致 Copilot 访问被暂停。请负责任地使用。
611
+ >
612
+ > 相关政策:
613
+ > - [GitHub 可接受使用政策](https://docs.github.com/site-policy/acceptable-use-policies/github-acceptable-use-policies)
614
+ > - [GitHub Copilot 条款](https://docs.github.com/site-policy/github-terms/github-terms-for-additional-products-and-features#github-copilot)
615
+
616
+ ---
617
+
618
+ ## 📄 许可证
619
+
620
+ MIT License