copilot-api-plus 1.0.55 → 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.en.md ADDED
@@ -0,0 +1,862 @@
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.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
+ ---
11
+
12
+ ## 📋 Table of Contents
13
+
14
+ - [Features](#-features)
15
+ - [Quick Start](#-quick-start)
16
+ - [Usage Guide](#-usage-guide)
17
+ - [GitHub Copilot Mode](#1-github-copilot-mode-default)
18
+ - [OpenCode Zen Mode](#2-opencode-zen-mode)
19
+ - [Google Antigravity Mode](#3-google-antigravity-mode)
20
+ - [Proxy Configuration](#-proxy-configuration)
21
+ - [Claude Code Integration](#-claude-code-integration)
22
+ - [opencode Integration](#-opencode-integration)
23
+ - [API Endpoints](#-api-endpoints)
24
+ - [API Key Authentication](#-api-key-authentication)
25
+ - [Technical Details](#-technical-details)
26
+ - [CLI Reference](#️-cli-reference)
27
+ - [Docker Deployment](#-docker-deployment)
28
+ - [FAQ](#-faq)
29
+
30
+ ---
31
+
32
+ ## ✨ Features
33
+
34
+ | Feature | Description |
35
+ |---------|-------------|
36
+ | 🔌 **Multiple Backends** | Choose from GitHub Copilot, OpenCode Zen, or Google Antigravity |
37
+ | 🤖 **Dual Protocol** | Supports both OpenAI Chat Completions API and Anthropic Messages API |
38
+ | 💻 **Claude Code Integration** | One-command Claude Code setup (`--claude-code`) |
39
+ | 📊 **Usage Monitoring** | Real-time API usage dashboard |
40
+ | 🔄 **Auto Authentication** | Automatic token refresh, no manual intervention needed |
41
+ | ⚡ **Rate Limiting** | Built-in request rate control to avoid hitting limits |
42
+ | 🌐 **Proxy Support** | HTTP/HTTPS proxy with persistent configuration |
43
+ | 🐳 **Docker Support** | Full Docker deployment solution |
44
+ | 🔑 **API Key Auth** | Optional API key authentication for public deployments |
45
+ | ✂️ **Context Passthrough** | Full context passthrough to upstream API; clients (e.g. Claude Code) manage compression |
46
+ | 🔍 **Smart Model Matching** | Handles model name format differences (date suffixes, dash/dot versions, etc.) |
47
+ | 🔁 **Antigravity Failover** | Circuit breaker state machine, weighted account dispatch, background task auto-downgrade, dual-endpoint auto-switching |
48
+
49
+ ---
50
+
51
+ ## 🚀 Quick Start
52
+
53
+ ### Installation
54
+
55
+ ```bash
56
+ # Global install
57
+ npm install -g copilot-api-plus
58
+
59
+ # Or run directly with npx (recommended)
60
+ npx copilot-api-plus@latest start
61
+ ```
62
+
63
+ ### Basic Usage
64
+
65
+ ```bash
66
+ # Start server (defaults to GitHub Copilot)
67
+ npx copilot-api-plus@latest start
68
+
69
+ # Use OpenCode Zen
70
+ npx copilot-api-plus@latest start --zen
71
+
72
+ # Use Google Antigravity
73
+ npx copilot-api-plus@latest start --antigravity
74
+
75
+ # Use with Claude Code
76
+ npx copilot-api-plus@latest start --claude-code
77
+ ```
78
+
79
+ The server listens on `http://localhost:4141` by default.
80
+
81
+ ---
82
+
83
+ ## 📖 Usage Guide
84
+
85
+ ### 1. GitHub Copilot Mode (Default)
86
+
87
+ Access AI models using your GitHub Copilot subscription.
88
+
89
+ #### Prerequisites
90
+ - GitHub account
91
+ - Active Copilot subscription (Individual / Business / Enterprise)
92
+
93
+ #### Getting Started
94
+
95
+ ```bash
96
+ npx copilot-api-plus@latest start
97
+ ```
98
+
99
+ **First run** will guide you through GitHub OAuth authentication:
100
+
101
+ 1. A device code appears in the terminal, e.g.: `XXXX-XXXX`
102
+ 2. Open your browser and visit: https://github.com/login/device
103
+ 3. Enter the device code and authorize
104
+ 4. Return to the terminal and wait for authentication to complete
105
+
106
+ Once authenticated, the token is saved locally. No re-authentication needed on subsequent runs.
107
+
108
+ #### Business / Enterprise Accounts
109
+
110
+ ```bash
111
+ # Business plan
112
+ npx copilot-api-plus@latest start --account-type business
113
+
114
+ # Enterprise plan
115
+ npx copilot-api-plus@latest start --account-type enterprise
116
+ ```
117
+
118
+ #### Available Models
119
+
120
+ | Model | ID | Context Length |
121
+ |-------|-----|---------------|
122
+ | Claude Sonnet 4 | `claude-sonnet-4` | 200K |
123
+ | Claude Sonnet 4.5 | `claude-sonnet-4.5` | 200K |
124
+ | GPT-4.1 | `gpt-4.1` | 1M |
125
+ | o4-mini | `o4-mini` | 200K |
126
+ | Gemini 2.5 Pro | `gemini-2.5-pro` | 1M |
127
+
128
+ ---
129
+
130
+ ### 2. OpenCode Zen Mode
131
+
132
+ Use [OpenCode Zen](https://opencode.ai/zen)'s multi-model API service, supporting GPT-5, Claude, Gemini, and other top coding models.
133
+
134
+ #### Prerequisites
135
+ 1. Visit https://opencode.ai/zen
136
+ 2. Register and create an API Key
137
+
138
+ #### Getting Started
139
+
140
+ **Option 1: Interactive setup**
141
+ ```bash
142
+ npx copilot-api-plus@latest start --zen
143
+ ```
144
+ First run will prompt for an API Key, which is saved for future use.
145
+
146
+ **Option 2: Provide API Key directly**
147
+ ```bash
148
+ npx copilot-api-plus@latest start --zen --zen-api-key YOUR_API_KEY
149
+ ```
150
+
151
+ #### Available Models
152
+
153
+ | Model | ID | Description |
154
+ |-------|-----|-------------|
155
+ | GPT-5.2 | `gpt-5.2` | OpenAI latest |
156
+ | GPT-5.1 Codex Max | `gpt-5.1-codex-max` | Code-optimized |
157
+ | GPT-5.1 Codex | `gpt-5.1-codex` | Code-focused |
158
+ | GPT-5 Codex | `gpt-5-codex` | OpenAI Responses API |
159
+ | Claude Opus 4.5 | `claude-opus-4-5` | Anthropic Claude (200K) |
160
+ | Claude Sonnet 4.5 | `claude-sonnet-4-5` | Anthropic Claude (200K) |
161
+ | Claude Sonnet 4 | `claude-sonnet-4` | Anthropic Claude |
162
+ | Gemini 3 Pro | `gemini-3-pro` | Google Gemini |
163
+ | Qwen3 Coder | `qwen3-coder` | Alibaba Qwen |
164
+ | Kimi K2 | `kimi-k2` | Moonshot |
165
+ | Grok Code Fast 1 | `grok-code-fast-1` | xAI |
166
+
167
+ More models at [opencode.ai/zen](https://opencode.ai/zen)
168
+
169
+ #### API Endpoints
170
+
171
+ Zen mode supports the following endpoints:
172
+
173
+ | Endpoint | Description |
174
+ |----------|-------------|
175
+ | `/v1/chat/completions` | OpenAI-compatible Chat API |
176
+ | `/v1/messages` | Anthropic-compatible Messages API |
177
+ | `/v1/responses` | OpenAI Responses API (GPT-5 series) |
178
+ | `/v1/models` | List available models |
179
+
180
+ Dedicated endpoints (accessible without `--zen` flag):
181
+ - `/zen/v1/chat/completions`
182
+ - `/zen/v1/messages`
183
+ - `/zen/v1/responses`
184
+ - `/zen/v1/models`
185
+
186
+ #### Manage API Key
187
+
188
+ ```bash
189
+ # View/change API Key (clearing it will prompt for a new one on next start)
190
+ npx copilot-api-plus@latest logout --zen
191
+ ```
192
+
193
+ ---
194
+
195
+ ### 3. Google Antigravity Mode
196
+
197
+ Use Google Antigravity API service, supporting Gemini and Claude models.
198
+
199
+ #### Prerequisites
200
+ - Google account
201
+
202
+ #### Authentication Methods
203
+
204
+ **Option 1: API Key (Recommended - Simplest)**
205
+
206
+ 1. Get an API Key at https://aistudio.google.com/apikey
207
+ 2. Start with environment variable:
208
+
209
+ ```bash
210
+ # Linux/macOS
211
+ GEMINI_API_KEY=your_api_key npx copilot-api-plus@latest start --antigravity
212
+
213
+ # Windows PowerShell
214
+ $env:GEMINI_API_KEY = "your_api_key"
215
+ npx copilot-api-plus@latest start --antigravity
216
+
217
+ # Windows CMD
218
+ set GEMINI_API_KEY=your_api_key
219
+ npx copilot-api-plus@latest start --antigravity
220
+ ```
221
+
222
+ **Option 2: OAuth Web Login (Recommended)**
223
+
224
+ ```bash
225
+ npx copilot-api-plus@latest start --antigravity
226
+ ```
227
+
228
+ First run will prompt you to choose a login method:
229
+ - **Web (Recommended)**: Opens browser for Google login, automatically captures the callback
230
+ - **Manual**: Manually copy the callback URL to the terminal
231
+
232
+ **Option 3: Custom OAuth Credentials**
233
+
234
+ If you encounter an `invalid_client` error, create your own OAuth app:
235
+
236
+ 1. Visit https://console.cloud.google.com/apis/credentials
237
+ 2. Create an OAuth 2.0 Client ID (select "Desktop application" type)
238
+ 3. Add redirect URI: `http://localhost:8046/callback`
239
+ 4. Use environment variables or CLI arguments:
240
+
241
+ ```bash
242
+ # Environment variables
243
+ ANTIGRAVITY_CLIENT_ID=your_client_id ANTIGRAVITY_CLIENT_SECRET=your_secret \
244
+ npx copilot-api-plus@latest start --antigravity
245
+
246
+ # CLI arguments
247
+ npx copilot-api-plus@latest start --antigravity \
248
+ --antigravity-client-id your_client_id \
249
+ --antigravity-client-secret your_secret
250
+ ```
251
+
252
+ #### Available Models
253
+
254
+ | Model | ID | Description |
255
+ |-------|-----|-------------|
256
+ | Gemini 2.5 Pro | `gemini-2.5-pro-exp-03-25` | Google Gemini |
257
+ | Gemini 2.5 Pro Preview | `gemini-2.5-pro-preview-05-06` | Google Gemini |
258
+ | Gemini 2.0 Flash | `gemini-2.0-flash-exp` | Fast responses |
259
+ | Gemini 2.0 Flash Thinking | `gemini-2.0-flash-thinking-exp` | Chain-of-thought |
260
+ | Claude Opus 4.5 | `claude-opus-4-5` | Anthropic Claude |
261
+ | Claude Sonnet 4.5 | `claude-sonnet-4-5` | Anthropic Claude |
262
+
263
+ #### Features
264
+ - ✅ Automatic token refresh
265
+ - ✅ Multi-account support with weighted smart dispatch (quota health 60% + token freshness 20% + reliability 20%)
266
+ - ✅ Auto-switch on quota exhaustion
267
+ - ✅ Circuit breaker state machine (CLOSED → OPEN → HALF_OPEN → CLOSED)
268
+ - ✅ Multi-signal agent/background request detection with optional model downgrade to save quota
269
+ - ✅ Thinking model support (chain-of-thought output)
270
+
271
+ #### Multi-Account Management
272
+
273
+ Add multiple Google accounts; the system auto-switches when quota is exhausted:
274
+
275
+ ```bash
276
+ # Add new account
277
+ npx copilot-api-plus@latest antigravity add
278
+
279
+ # List all accounts
280
+ npx copilot-api-plus@latest antigravity list
281
+
282
+ # Remove account by index
283
+ npx copilot-api-plus@latest antigravity remove 0
284
+
285
+ # Clear all accounts
286
+ npx copilot-api-plus@latest antigravity clear
287
+ # Or use logout
288
+ npx copilot-api-plus@latest logout --antigravity
289
+ ```
290
+
291
+ ---
292
+
293
+ ## 🌐 Proxy Configuration
294
+
295
+ Two ways to configure a proxy:
296
+
297
+ ### Option 1: Persistent Configuration (Recommended)
298
+
299
+ Configure once, automatically used on every startup.
300
+
301
+ ```bash
302
+ # Interactive setup
303
+ npx copilot-api-plus@latest proxy --set
304
+
305
+ # Or set directly
306
+ npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890
307
+
308
+ # Set both HTTP and HTTPS proxy
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
+ #### Proxy Management Commands
313
+
314
+ ```bash
315
+ # View current proxy settings
316
+ npx copilot-api-plus@latest proxy
317
+
318
+ # Enable proxy
319
+ npx copilot-api-plus@latest proxy --enable
320
+
321
+ # Disable proxy (keeps settings)
322
+ npx copilot-api-plus@latest proxy --disable
323
+
324
+ # Clear proxy settings
325
+ npx copilot-api-plus@latest proxy --clear
326
+ ```
327
+
328
+ #### Example: Configure Clash Proxy
329
+
330
+ ```bash
331
+ # Clash default port 7890
332
+ npx copilot-api-plus@latest proxy --http-proxy http://127.0.0.1:7890
333
+
334
+ # Verify configuration
335
+ npx copilot-api-plus@latest proxy
336
+ # Output:
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
+ ### Option 2: Environment Variables (Temporary)
344
+
345
+ Only effective for the current session:
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
+ ### Proxy Priority
365
+
366
+ 1. `--proxy-env` flag (reads from environment variables)
367
+ 2. Persistent configuration (set via `proxy --set`)
368
+ 3. No proxy
369
+
370
+ ---
371
+
372
+ ## 💻 Claude Code Integration
373
+
374
+ [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) is Anthropic's AI coding assistant.
375
+
376
+ ### Auto Configuration (Recommended)
377
+
378
+ ```bash
379
+ # Using GitHub Copilot as backend
380
+ npx copilot-api-plus@latest start --claude-code
381
+
382
+ # Using OpenCode Zen as backend
383
+ npx copilot-api-plus@latest start --zen --claude-code
384
+
385
+ # Using Google Antigravity as backend
386
+ npx copilot-api-plus@latest start --antigravity --claude-code
387
+ ```
388
+
389
+ After running:
390
+ 1. Select main model (for code generation)
391
+ 2. Select fast model (for background tasks)
392
+ 3. The launch command is automatically copied to clipboard
393
+ 4. **Open a new terminal**, paste and run to start Claude Code
394
+
395
+ ### Manual Configuration
396
+
397
+ Create `.claude/settings.json` in your project root:
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
+ Then start the copilot-api-plus server and run `claude` in that project directory.
412
+
413
+ ---
414
+
415
+ ## 🔧 opencode Integration
416
+
417
+ [opencode](https://github.com/sst/opencode) is a modern AI coding assistant.
418
+
419
+ ### Setup
420
+
421
+ 1. Create `opencode.json` in your project root:
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. Start copilot-api-plus:
455
+ ```bash
456
+ npx copilot-api-plus@latest start
457
+ ```
458
+
459
+ 3. Run opencode in the same directory:
460
+ ```bash
461
+ npx opencode@latest
462
+ ```
463
+
464
+ 4. Select `copilot-api-plus` as the provider
465
+
466
+ ### Shortcut: Environment Variables
467
+
468
+ ```bash
469
+ # Set environment variables
470
+ export OPENAI_BASE_URL=http://127.0.0.1:4141/v1
471
+ export OPENAI_API_KEY=dummy
472
+
473
+ # Run opencode
474
+ npx opencode@latest
475
+ ```
476
+
477
+ ---
478
+
479
+ ## 📡 API Endpoints
480
+
481
+ The server listens on `http://localhost:4141` by default.
482
+
483
+ ### OpenAI-Compatible Endpoints
484
+
485
+ | Endpoint | Method | Description |
486
+ |----------|--------|-------------|
487
+ | `/v1/chat/completions` | POST | Chat completions (streaming supported) |
488
+ | `/v1/models` | GET | Model list |
489
+ | `/v1/embeddings` | POST | Text embeddings (Copilot only) |
490
+
491
+ ### Anthropic-Compatible Endpoints
492
+
493
+ | Endpoint | Method | Description |
494
+ |----------|--------|-------------|
495
+ | `/v1/messages` | POST | Messages API (streaming supported) |
496
+ | `/v1/messages/count_tokens` | POST | Token counting |
497
+
498
+ ### Dedicated Endpoints
499
+
500
+ Each backend has its own dedicated routes, accessible regardless of the default backend:
501
+
502
+ | Route Prefix | Description |
503
+ |--------------|-------------|
504
+ | `/copilot/v1/*` | GitHub Copilot |
505
+ | `/zen/v1/*` | OpenCode Zen |
506
+ | `/antigravity/v1/*` | Google Antigravity |
507
+
508
+ ### Monitoring Endpoints
509
+
510
+ | Endpoint | Method | Description |
511
+ |----------|--------|-------------|
512
+ | `/usage` | GET | Usage statistics (Copilot only) |
513
+ | `/token` | GET | Current token info |
514
+
515
+ ### Examples
516
+
517
+ ```bash
518
+ # OpenAI format
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 format
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
+ ## 🔑 API Key Authentication
540
+
541
+ To protect your service when exposed publicly, enable API key authentication:
542
+
543
+ ```bash
544
+ # Single key
545
+ npx copilot-api-plus@latest start --api-key my-secret-key
546
+
547
+ # Multiple keys
548
+ npx copilot-api-plus@latest start --api-key key1 --api-key key2
549
+ ```
550
+
551
+ Once enabled, all requests must include an API key:
552
+
553
+ ```bash
554
+ # OpenAI format - via Authorization header
555
+ curl http://localhost:4141/v1/chat/completions \
556
+ -H "Authorization: Bearer my-secret-key" \
557
+ -H "Content-Type: application/json" \
558
+ -d '{"model": "claude-sonnet-4", "messages": [{"role": "user", "content": "Hello"}]}'
559
+
560
+ # Anthropic format - via x-api-key header
561
+ curl http://localhost:4141/v1/messages \
562
+ -H "x-api-key: my-secret-key" \
563
+ -H "Content-Type: application/json" \
564
+ -d '{"model": "claude-sonnet-4", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}'
565
+ ```
566
+
567
+ When using with Claude Code, set `ANTHROPIC_AUTH_TOKEN` to your API key.
568
+
569
+ ---
570
+
571
+ ## 🔧 Technical Details
572
+
573
+ ### Context Management
574
+
575
+ The proxy does not truncate context. All messages are passed through to the upstream API as-is. Context compression is handled by the client:
576
+
577
+ - **Claude Code**: Uses `/count_tokens` to get the current token count, and automatically triggers `/compact` when approaching the limit
578
+ - **Other clients**: If the upstream API returns 400 (token limit exceeded), the client handles retry logic
579
+
580
+ ### Smart Model Name Matching
581
+
582
+ Anthropic-format model names (e.g. `claude-opus-4-6`) may differ from Copilot's model list IDs. The proxy uses multi-strategy matching:
583
+
584
+ | Strategy | Example |
585
+ |----------|---------|
586
+ | Exact match | `claude-opus-4-6` → `claude-opus-4-6` |
587
+ | Strip date suffix | `claude-opus-4-6-20251101` → `claude-opus-4-6` |
588
+ | Dash → Dot | `claude-opus-4-5` → `claude-opus-4.5` |
589
+ | Dot → Dash | `claude-opus-4.5` → `claude-opus-4-5` |
590
+
591
+ For Anthropic endpoints (`/v1/messages`), `translateModelName` also handles legacy format conversion (e.g. `claude-3-5-sonnet` → `claude-sonnet-4.5`) before applying the above strategies.
592
+
593
+ ### Antigravity Endpoint Failover
594
+
595
+ Google Antigravity mode has multi-layered reliability features:
596
+
597
+ - **Circuit breaker state machine**: Independent per model family (claude/gemini/other). Opens after 3 failures, half-opens after 30s for probing, closes after 2 consecutive successes
598
+ - **Weighted account dispatch**: Replaces simple round-robin. Scoring formula: `score = quotaHealth×0.6 + tokenFreshness×0.2 + reliability×0.2`, always selects the healthiest account
599
+ - **Background task detection & downgrade**: Multi-signal weighted detection (tool_calls +0.5, tool role +0.4, assistant density +0.2, long conversation +0.1). Score ≥ 0.6 identifies agent requests, which can be auto-downgraded to cheaper models (e.g. claude-sonnet-4-5 → gemini-2.5-flash). Enable via `ANTIGRAVITY_BACKGROUND_DOWNGRADE=1` env var (off by default)
600
+ - **Dual-endpoint auto-switching**: Daily sandbox and production endpoints; automatically switches on failure
601
+ - **Exponential backoff retry**: Auto-retry on 429/503 errors; short intervals stay on the same endpoint, longer intervals switch endpoints
602
+
603
+ #### Background Downgrade Environment Variable
604
+
605
+ | Variable | Default | Description |
606
+ |----------|---------|-------------|
607
+ | `ANTIGRAVITY_BACKGROUND_DOWNGRADE` | `0` (off) | Set to `1` to enable automatic model downgrade for agent/background requests |
608
+
609
+ Downgrade mapping:
610
+
611
+ | Original Model | Downgraded To |
612
+ |----------------|---------------|
613
+ | `claude-sonnet-4-5` | `gemini-2.5-flash` |
614
+ | `claude-sonnet-4-5-thinking` | `gemini-2.5-flash-thinking` |
615
+ | `claude-opus-4-5-thinking` | `claude-sonnet-4-5-thinking` |
616
+ | `gemini-2.5-pro` | `gemini-2.5-flash` |
617
+ | `gemini-3-pro-high` | `gemini-3-flash` |
618
+ | `gemini-3-pro-low` | `gemini-3-flash` |
619
+
620
+ Clients can also explicitly mark background requests via the `X-Request-Type: background` header, bypassing detection entirely.
621
+
622
+ ### Request Logging
623
+
624
+ Each API request outputs a log line with model name, status code, and duration:
625
+
626
+ ```log
627
+ [claude-opus-4-6] 13:13:39 <-- POST /v1/messages?beta=true
628
+ [claude-opus-4-6] 13:13:59 --> POST /v1/messages?beta=true 200 20.1s
629
+ ```
630
+
631
+ ### Network Retry
632
+
633
+ Built-in retry for transient network errors (TLS disconnect, connection reset, etc.):
634
+
635
+ - Up to 2 retries (3 total attempts)
636
+ - Backoff intervals: 1s, 2s
637
+ - Only retries network-layer errors; HTTP error codes (e.g. 400/500) are not retried
638
+
639
+ ---
640
+
641
+ ## ⚙️ CLI Reference
642
+
643
+ ### Commands
644
+
645
+ | Command | Description |
646
+ |---------|-------------|
647
+ | `start` | Start the API server |
648
+ | `auth` | Run GitHub authentication only |
649
+ | `logout` | Clear saved credentials |
650
+ | `proxy` | Configure proxy settings |
651
+ | `antigravity` | Manage Google Antigravity accounts |
652
+ | `check-usage` | View Copilot usage |
653
+ | `debug` | Show debug information |
654
+
655
+ ### start Options
656
+
657
+ | Option | Alias | Default | Description |
658
+ |--------|-------|---------|-------------|
659
+ | `--port` | `-p` | 4141 | Listen port |
660
+ | `--verbose` | `-v` | false | Verbose logging |
661
+ | `--account-type` | `-a` | individual | Account type (individual/business/enterprise) |
662
+ | `--claude-code` | `-c` | false | Generate Claude Code launch command |
663
+ | `--zen` | `-z` | false | Enable OpenCode Zen mode |
664
+ | `--zen-api-key` | - | - | Zen API Key |
665
+ | `--antigravity` | - | false | Enable Google Antigravity mode |
666
+ | `--antigravity-client-id` | - | - | Antigravity OAuth Client ID |
667
+ | `--antigravity-client-secret` | - | - | Antigravity OAuth Client Secret |
668
+ | `--rate-limit` | `-r` | - | Request interval (seconds) |
669
+ | `--wait` | `-w` | false | Wait instead of error when rate limited |
670
+ | `--manual` | - | false | Manually approve each request |
671
+ | `--github-token` | `-g` | - | Provide GitHub Token directly |
672
+ | `--show-token` | - | false | Show token info |
673
+ | `--proxy-env` | - | false | Read proxy from environment variables |
674
+ | `--api-key` | - | - | API key auth (can be specified multiple times) |
675
+
676
+ ### proxy Options
677
+
678
+ | Option | Description |
679
+ |--------|-------------|
680
+ | `--set` | Interactive proxy setup |
681
+ | `--enable` | Enable saved proxy |
682
+ | `--disable` | Disable proxy (keeps settings) |
683
+ | `--clear` | Clear proxy settings |
684
+ | `--show` | Show current settings |
685
+ | `--http-proxy` | HTTP proxy URL |
686
+ | `--https-proxy` | HTTPS proxy URL |
687
+ | `--no-proxy` | Hosts to bypass proxy |
688
+
689
+ ### logout Options
690
+
691
+ | Option | Alias | Description |
692
+ |--------|-------|-------------|
693
+ | `--github` | `-g` | Clear GitHub Copilot credentials only |
694
+ | `--zen` | `-z` | Clear Zen credentials only |
695
+ | `--antigravity` | - | Clear Antigravity credentials only |
696
+ | `--all` | `-a` | Clear all credentials |
697
+
698
+ > **Tip**: Running `logout` without arguments shows an interactive menu.
699
+
700
+ ### antigravity Subcommands
701
+
702
+ | Subcommand | Description |
703
+ |------------|-------------|
704
+ | `add` | Add a new Antigravity account (OAuth login) |
705
+ | `list` | List all configured accounts and their status |
706
+ | `remove <index>` | Remove account by index |
707
+ | `clear` | Clear all Antigravity accounts (requires confirmation) |
708
+
709
+ ```bash
710
+ # Examples
711
+ npx copilot-api-plus@latest antigravity add # Add account
712
+ npx copilot-api-plus@latest antigravity list # List accounts
713
+ npx copilot-api-plus@latest antigravity remove 0 # Remove account at index 0
714
+ npx copilot-api-plus@latest antigravity clear # Clear all accounts
715
+ ```
716
+
717
+ ---
718
+
719
+ ## 🐳 Docker Deployment
720
+
721
+ ### Quick Start
722
+
723
+ ```bash
724
+ # Using pre-built image
725
+ docker run -p 4141:4141 \
726
+ -v ./copilot-data:/root/.local/share/copilot-api-plus \
727
+ ghcr.io/imbuxiangnan-cyber/copilot-api-plus
728
+ ```
729
+
730
+ ### Build from Source
731
+
732
+ ```bash
733
+ # Build image
734
+ docker build -t copilot-api-plus .
735
+
736
+ # Run container
737
+ docker run -p 4141:4141 \
738
+ -v ./copilot-data:/root/.local/share/copilot-api-plus \
739
+ copilot-api-plus
740
+ ```
741
+
742
+ ### Docker Compose
743
+
744
+ ```yaml
745
+ version: "3.8"
746
+ services:
747
+ copilot-api-plus:
748
+ build: .
749
+ ports:
750
+ - "4141:4141"
751
+ volumes:
752
+ - ./copilot-data:/root/.local/share/copilot-api-plus
753
+ environment:
754
+ - GH_TOKEN=your_github_token # Optional
755
+ restart: unless-stopped
756
+ ```
757
+
758
+ ### Using a Proxy
759
+
760
+ ```bash
761
+ docker run -p 4141:4141 \
762
+ -e HTTP_PROXY=http://host.docker.internal:7890 \
763
+ -e HTTPS_PROXY=http://host.docker.internal:7890 \
764
+ -v ./copilot-data:/root/.local/share/copilot-api-plus \
765
+ copilot-api-plus start --proxy-env
766
+ ```
767
+
768
+ ---
769
+
770
+ ## ❓ FAQ
771
+
772
+ ### Data Storage Location
773
+
774
+ All data is stored in `~/.local/share/copilot-api-plus/`:
775
+
776
+ | File | Description |
777
+ |------|-------------|
778
+ | `github_token` | GitHub Token |
779
+ | `zen-auth.json` | Zen API Key |
780
+ | `antigravity-accounts.json` | Antigravity accounts |
781
+ | `config.json` | Proxy and other settings |
782
+
783
+ ### Switching Accounts
784
+
785
+ ```bash
786
+ # Interactive credential selection
787
+ npx copilot-api-plus@latest logout
788
+
789
+ # Clear GitHub Copilot credentials only
790
+ npx copilot-api-plus@latest logout --github
791
+ # Or shorthand
792
+ npx copilot-api-plus@latest logout -g
793
+
794
+ # Clear Zen credentials
795
+ npx copilot-api-plus@latest logout --zen
796
+
797
+ # Clear Antigravity credentials
798
+ npx copilot-api-plus@latest logout --antigravity
799
+
800
+ # Clear all credentials
801
+ npx copilot-api-plus@latest logout --all
802
+ ```
803
+
804
+ ### View Usage
805
+
806
+ ```bash
807
+ # CLI (Copilot only)
808
+ npx copilot-api-plus@latest check-usage
809
+ ```
810
+
811
+ After starting the server, you can also access the web dashboard:
812
+ ```
813
+ https://imbuxiangnan-cyber.github.io/copilot-api-plus?endpoint=http://localhost:4141/usage
814
+ ```
815
+
816
+ ### Debugging
817
+
818
+ ```bash
819
+ # Show debug info
820
+ npx copilot-api-plus@latest debug
821
+
822
+ # JSON output
823
+ npx copilot-api-plus@latest debug --json
824
+
825
+ # Enable verbose logging
826
+ npx copilot-api-plus@latest start --verbose
827
+ ```
828
+
829
+ ### Rate Limiting
830
+
831
+ To avoid triggering GitHub's abuse detection:
832
+
833
+ ```bash
834
+ # Set 30-second request interval
835
+ npx copilot-api-plus@latest start --rate-limit 30
836
+
837
+ # Wait instead of error when rate limited
838
+ npx copilot-api-plus@latest start --rate-limit 30 --wait
839
+
840
+ # Manually approve each request
841
+ npx copilot-api-plus@latest start --manual
842
+ ```
843
+
844
+ ---
845
+
846
+ ## ⚠️ Disclaimer
847
+
848
+ > [!WARNING]
849
+ > This is a reverse-engineered proxy for the GitHub Copilot API. **Not officially supported by GitHub** and may stop working at any time. Use at your own risk.
850
+
851
+ > [!WARNING]
852
+ > **GitHub Safety Notice**: Excessive automated or scripted use of Copilot may trigger GitHub's abuse detection systems, resulting in Copilot access suspension. Please use responsibly.
853
+ >
854
+ > Related policies:
855
+ > - [GitHub Acceptable Use Policies](https://docs.github.com/site-policy/acceptable-use-policies/github-acceptable-use-policies)
856
+ > - [GitHub Copilot Terms](https://docs.github.com/site-policy/github-terms/github-terms-for-additional-products-and-features#github-copilot)
857
+
858
+ ---
859
+
860
+ ## 📄 License
861
+
862
+ MIT License