@hex4c59/pi-ext-copilot-auto 0.1.0 → 0.1.1

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/docs/PROTOCOL.md CHANGED
@@ -1,804 +1,804 @@
1
- # GitHub Copilot Auto 协议逆向记录
2
-
3
- 本文记录 `copilot-auto` 扩展所依赖的协议事实,以及得出这些结论的验证过程。
4
- GitHub 没有公开这套接口,以下内容全部来自实测与本机 VS Code 扩展源码。
5
-
6
- **用途**:GitHub 改动协议导致扩展失效时,按本文逐项复核,定位是哪一环变了。
7
-
8
- - 实测日期:2026-11-09
9
- - 参照实现:VS Code `copilot-chat` **0.72.0**(内置扩展,随 VS Code 1.144.1 分发)
10
- - 源码位置:`<VS Code 安装目录>/resources/app/extensions/copilot/dist/extension.js`
11
- - 账号标识与实际额度值已从本文样例中移除;套餐字段以 `free_educational_quota` 作为结构示例。
12
-
13
- ## 三条先读结论
14
-
15
- 在深入细节前,先记住这三条。它们能让排查少走大部分弯路:
16
-
17
- 1. **两个路由通道的令牌作用域不同,不可互换。**
18
- `/auto` 的令牌只覆盖它选中的那个模型;`/models/session` 的令牌覆盖其
19
- `available_models`。用错模型返回 `Requested model not available for session`。
20
- 2. **模型自述不可信。** 问模型“你是谁”会得到错误且不一致的答案,
21
- 不能用于判断实际运行的模型(第 10 章)。
22
- 3. **缺 `X-GitHub-Api-Version` 会返回误导性的 `model_not_supported`。**
23
- 看到这个错误先检查请求头,不要直接归因为权限问题(第 3 章)。
24
-
25
- > 2026-11-11 修订:本文早先版本声称 `POST /auto` 返回 404「是死代码」。
26
- > 该结论**错误**,原因是用了旧版本头 `2025-07-16`。`/auto` 真实存在,
27
- > 只接受 `X-GitHub-Api-Version: 2026-08-01`。详见第 2 章。
28
-
29
- ---
30
-
31
- ## 1. 核心结论
32
-
33
- Copilot 有**两条相互隔离的 Auto 路由通道**:
34
-
35
- | 通道 | 调用方式 | 令牌覆盖范围 |
36
- | --- | --- | --- |
37
- | `/auto` | `POST /auto`(版本头 `2026-08-01`) | **仅它选中的那个模型** |
38
- | `/models/session` | 先 `auto_mode` 协商(版本头 `2025-07-16`) | 响应里的 `available_models` |
39
-
40
- 两者下发同一种 `session_token`,都必须以 `Copilot-Session-Token` 头发给
41
- `/responses`。不带该头时,Auto 专属模型一律返回
42
- `400 model_not_supported`(已实测)。
43
-
44
- `gpt-5.3-codex` 在普通通道下**即使**出现在 `/models` 目录、`policy.state` 标为
45
- `enabled`、`capabilities` 齐全,请求时仍返回 `400 model_not_supported`。
46
- 只有 Auto 通道能调用。
47
-
48
- 这就是「VS Code 能用、pi 不能用」的根因:pi 内置的 `github-copilot` provider
49
- 只实现了普通通道。
50
-
51
- ---
52
-
53
- ## 2. 端点
54
-
55
- ```
56
- POST /auto Auto 路由(新),返回选中模型的完整对象
57
- POST /models/session Auto 路由(旧),返回 id 与授权清单
58
- GET /models 模型目录(能力、端点、计费、policy)
59
- POST /responses 实际对话
60
- ```
61
-
62
- 以上均在 `api.individual.githubcopilot.com`(或 `endpoints.api` 返回的主机)。
63
-
64
- 额度与套餐信息在**另一个主机**:
65
-
66
- ```
67
- GET https://api.github.com/copilot_internal/user
68
- GET https://api.github.com/copilot_internal/v2/token
69
- ```
70
-
71
- ### `/auto` 确实存在(早先结论已推翻)
72
-
73
- 早先版本记录「`POST /auto` 实测返回 404,是死代码」。**那是错的**。
74
- 实测(2026-11-11):
75
-
76
- | 版本头 | `POST /auto` |
77
- | --- | --- |
78
- | `2025-07-16` | `404 bad request: error: invalid apiVersion` |
79
- | **`2026-08-01`** | **200,返回完整路由结果** |
80
-
81
- 因此 404 错误描述的是**版本不受支持**,不是路径不存在。OpenCode v2 使用
82
- 正是 `2026-08-01`。
83
-
84
- ### `/auto` 请求体
85
-
86
- ```json
87
- { "prompt": "write quicksort in python", "has_image": true }
88
- ```
89
-
90
- `has_image` 仅在请求含图片时带上。实测**无法指定模型**:附加 `model`、
91
- `model_id` 或 `selected_model` 字段均被忽略,服务端照旧自行选择。
92
-
93
- ### `/auto` 响应结构
94
-
95
- 比 `/models/session` 多一层:`selected_model` 是**完整模型对象**,
96
- 因此无需再查目录就能拿到能力和计费:
97
-
98
- ```json
99
- {
100
- "session_token": "<redacted>",
101
- "selected_model": {
102
- "id": "gpt-5.6-luna",
103
- "name": "GPT-5.6 Luna",
104
- "version": "gpt-5.6-luna",
105
- "supported_endpoints": ["/responses", "ws:/responses"],
106
- "capabilities": {
107
- "family": "gpt-5.6-luna",
108
- "limits": { "max_context_window_tokens": 1050000, "max_output_tokens": 128000, "max_prompt_tokens": 922000 },
109
- "supports": { "tool_calls": true, "streaming": true, "vision": true, "reasoning_effort": ["none", "low", "medium", "high", "xhigh", "max"] }
110
- },
111
- "billing": { "token_prices": { "batch_size": 1000000, "default": { "input_price": 20, "output_price": 120 } } }
112
- },
113
- "expires_at": 1794472092,
114
- "multi_turn": true,
115
- "multi_turn_mode": "stateless",
116
- "hydra_scores": {},
117
- "discounted_costs": { "gpt-5.6-luna": 0.1 },
118
- "selection_reason": "..."
119
- }
120
- ```
121
-
122
- 注意 `selected_model` 对象**不含** `policy`、`model_picker_enabled`、
123
- `billing.auto_discount`;这些只在 `/models` 目录里出现。
124
- `/auto` 的 `session_token` 载荷与旧通道也不同:
125
-
126
- ```json
127
- {
128
- "auto_session_id": "<redacted>",
129
- "auto_tier": "balance",
130
- "reasoning_bucket": "low",
131
- "selected_model": "gpt-5.6-luna",
132
- "discounted_costs": { "gpt-5.6-luna": 0.1 },
133
- "sub": "<redacted>",
134
- "iat": 1794385692,
135
- "exp": 1794472092
136
- }
137
- ```
138
-
139
- 实测有效期 24 小时(`exp - iat = 86400`);旧通道是 1 小时。
140
-
141
- ### 已排除的路径
142
-
143
- | 路径 | 实测结果 |
144
- | --- | --- |
145
- | `POST /auto`(版本头 `2025-07-16`) | 404 `invalid apiVersion` |
146
- | `POST /models/session/intent` | 400 `Missing Copilot-Session-Token header` |
147
- | `POST /v1/messages` | `model_not_supported`(Anthropic 通道,与本议题无关) |
148
-
149
- ---
150
-
151
- ## 3. 请求头(最容易踩的坑)
152
-
153
- ### 必需头
154
-
155
- ```http
156
- Authorization: Bearer <copilot access token>
157
- Content-Type: application/json
158
- User-Agent: GitHubCopilotChat/0.72.0
159
- Editor-Version: vscode/1.144.1
160
- Editor-Plugin-Version: copilot-chat/0.72.0
161
- Copilot-Integration-Id: vscode-chat
162
- X-GitHub-Api-Version: 2025-07-16
163
- ```
164
-
165
- ### `X-GitHub-Api-Version` 是必需的
166
-
167
- **这是最容易被误判的一点。** 实测同一份有效 `session_token`、同一个模型:
168
-
169
- ```diff
170
- POST /responses
171
- Copilot-Session-Token: <valid>
172
- + X-GitHub-Api-Version: 2025-07-16
173
- → 200 OK
174
-
175
- POST /responses
176
- Copilot-Session-Token: <valid>
177
- - (缺少 X-GitHub-Api-Version)
178
- → 400 {"code":"model_not_supported"}
179
- ```
180
-
181
- 缺少它时返回的是 `model_not_supported`,不是「缺少版本头」,因此极易被误判成
182
- 账号没权限。
183
-
184
- 有效版本号(实测):
185
-
186
- | 版本 | `/models` | `/models/session` | `/auto` |
187
- | --- | --- | --- | --- |
188
- | `2025-04-01` | 200 | 404 Not Found | — |
189
- | `2025-05-01` | 200 | — | — |
190
- | `2025-07-16` | 200 | **200** | **404 `invalid apiVersion`** |
191
- | `2026-01-09` | 200 | 200 | — |
192
- | **`2026-08-01`** | 200 | 200 | **200** |
193
- | `2026-01-01`、`2026-11-01`、`latest` | `invalid apiVersion` | — | — |
194
-
195
- `/models/session` 要求 ≥ `2025-07-16`。证据:用 `2025-04-01` 请求
196
- `/models/session/intent` 时明确返回
197
- `auto-intent proxy requires API version 2025-07-16 or later`。
198
-
199
- `/auto` 要求 `2026-08-01`。本扩展因此对两个通道使用不同版本头:
200
- `/auto` 用 `AUTO_API_VERSION = "2026-08-01"`,其余用 `API_VERSION = "2025-07-16"`。
201
- `/responses` 对两者都接受(实测均 200)。
202
-
203
- ### 客户端身份头会被校验
204
-
205
- ```http
206
- User-Agent: GitHubCopilotChat/0.72.0
207
- Editor-Version: vscode/1.144.1
208
- Editor-Plugin-Version: copilot-chat/0.72.0
209
- Copilot-Integration-Id: vscode-chat
210
- ```
211
-
212
- 缺失或伪造的结果:
213
-
214
- | 情况 | 响应 |
215
- | --- | --- |
216
- | 完全不带 `Editor-Version` | `400 bad request: missing Editor-Version header for IDE auth` |
217
- | `Copilot-Integration-Id` 用未知值 | `400 bad request: unknown Copilot-Integration-Id` |
218
- | 用 `copilot-cli` / `vscode-agent` / `chat` | 同上,均被拒 |
219
-
220
- 已知有效值只有 `vscode-chat`(实测),以及 `copilot-chat`(源码中出现)。
221
-
222
- ### 动态头
223
-
224
- ```http
225
- X-Initiator: user | agent
226
- Openai-Intent: conversation-edits
227
- Copilot-Vision-Request: true # 仅在请求含图片时
228
- ```
229
-
230
- `X-Initiator` 判定规则(源码 `inferCopilotInitiator`):最后一条消息不是 `user`
231
- 时为 `agent`,否则 `user`。
232
-
233
- **重要**:pi-ai 只在 `model.provider === "github-copilot"` 时才注入这三个头。
234
- 自定义 provider id 必须自己补,否则请求被拒。
235
-
236
- ---
237
-
238
- ## 4. `/models/session` 请求体
239
-
240
- ### 正确形式
241
-
242
- ```json
243
- {
244
- "auto_mode": { "enabled": true },
245
- "prompt": "write quicksort in python"
246
- }
247
- ```
248
-
249
- `auto_mode` 必须是**对象**。
250
-
251
- ### 已排除的形式
252
-
253
- | 请求体 | 响应 |
254
- | --- | --- |
255
- | `{"auto_mode": true, ...}` | `400 Invalid request body` |
256
- | `{"auto_mode": "auto", ...}` | `400 Invalid request body` |
257
- | `{"auto_mode": 1, ...}` | `400 Invalid request body` |
258
- | `{"auto_mode": {"enabled": true}}`(无 prompt) | 200,可正常路由 |
259
- | `{"prompt": "..."}`(无 auto_mode) | `400 Missing auto_mode field in request` |
260
-
261
- `prompt` 可省略,但省略后路由退化为服务端默认选择。
262
-
263
- ---
264
-
265
- ### 响应结构(`free_educational_quota` 字段示例,账号、会话和额度值均为占位值)
266
-
267
- ```json
268
- {
269
- "available_models": ["mai-code-1.1-flash", "gpt-5.3-codex"],
270
- "selected_model": "mai-code-1.1-flash",
271
- "session_token": "<redacted>",
272
- "expires_at": 4600,
273
- "discounted_costs": { "gpt-5.3-codex": 0.1, "mai-code-1.1-flash": 0.1 }
274
- }
275
- ```
276
-
277
- `session_token` 是标准 JWT,载荷(仅读取,不验签):
278
-
279
- ```json
280
- {
281
- "available_models": ["mai-code-1.1-flash", "gpt-5.3-codex"],
282
- "selected_model": "mai-code-1.1-flash",
283
- "sub": "<redacted>",
284
- "iat": 1000,
285
- "exp": 4600,
286
- "discounted_costs": { "gpt-5.3-codex": 0.1, "mai-code-1.1-flash": 0.1 },
287
- "auto_session_id": "<redacted>"
288
- }
289
- ```
290
-
291
- 有效期约 1 小时(`exp - iat = 3600`)。
292
-
293
- ---
294
-
295
- ## 6. 授权边界
296
-
297
- 两个通道的令牌作用域**不同**,这是本扩展最关键的设计约束。
298
-
299
- ### `/models/session`:令牌覆盖 `available_models`
300
-
301
- `session_token` 的授权范围**严格等于** `available_models`:
302
-
303
- | 模型 | 带该 session token | 不带 |
304
- | --- | --- | --- |
305
- | `mai-code-1.1-flash` | 200 | 400 |
306
- | `gpt-5.3-codex` | 200 | 400 |
307
- | `gpt-4.1` | **400** | 400(该模型不支持 `/responses`) |
308
- | `gpt-5.6-luna` | **400** | 400 |
309
-
310
- 超出范围即使 token 有效也被拒:
311
-
312
- ```json
313
- {"error": {"message": "Requested model not available for session"}}
314
- ```
315
-
316
- ### `/auto`:令牌只覆盖它选中的那个模型
317
-
318
- 实测(2026-11-11,同一账号):
319
-
320
- | 请求 | 结果 |
321
- | --- | --- |
322
- | `/auto` 的 token + 它选中的 `gpt-5.6-luna` | 200 |
323
- | 同上 token + `mai-code-1.1-flash` | 400 `Requested model not available for session` |
324
- | 另一次 `/auto` 的 token(同样选中 luna) | 200(同模型可互换) |
325
- | 任意 token 不带 + 任一 Auto 模型 | 400 `model_not_supported` |
326
-
327
- 因此**不能**“拿一个 token 打所有模型”;换模型必须重新协商,且
328
- 要选对通道。
329
-
330
- ### `model: "auto"` 不是可发送的 model 值
331
-
332
- 必须用服务端返回的具体模型 id。注意 `Requested model not available for session`
333
- 与 `model_not_supported` **不同**——前者说明 Auto 通道认出了请求,只是该模型
334
- 不在本次令牌范围内。
335
-
336
- ### 请求体约束
337
-
338
- `max_output_tokens` 过小(实测 `8`)会返回 `400 invalid_request_body`;
339
- 同一请求改为 `16` 即 200。排查时不要据此误判为令牌问题。
340
-
341
- ---
342
-
343
- ## 7. 会话缓存策略(对齐 VS Code 与 OpenCode)
344
-
345
- VS Code 的 `AutomodeService` 缓存规则(源码核实):
346
-
347
- ```js
348
- _evictOldestSessions() // CACHE_MAX_ENTRIES = 50
349
- _isCacheEntryCompatible(n, r, o) {
350
- return n.tier === r
351
- && !this._isSessionExpired(n)
352
- && (!pze(o) || n.endpoint.supportsVision)
353
- }
354
- _routingSingler.getOrCreate(`${a}|${o}|${pze(n)}`, u) // 并发去重
355
- ```
356
-
357
- 关键点:
358
-
359
- - **缓存键是 conversation id,不是 prompt**
360
- 按 prompt 缓存会让几乎每个请求都重新协商,且同一次对话的模型会在多轮间跳转。
361
- - 最多 50 条,超出淘汰最早插入的
362
- - 同一键并发请求只协商一次
363
- - 图片请求必须复用已确认支持 vision 的会话
364
-
365
- 本扩展按此实现(`lib/sessions.ts`)。pi 的 `streamSimple` 不暴露会话 id,
366
- 因此用「首次用户消息摘要 + baseUrl」作为代理键。
367
-
368
- ### 额外的失效条件
369
-
370
- 除过期与 vision 外,本扩展还按以下条件判定缓存不可复用:
371
-
372
- - **请求模型已变**:缓存条目记录 `requestedModel`,用户从 `auto` 切到具体模型
373
- (或反之)时必定重新协商。不复用是因为两个通道的令牌作用域不同(第 6 章)。
374
- - **上下文被压缩**:对应 OpenCode 的 `compaction` hook,它把该会话的 `until`
375
- 置 0。pi 侧监听 `session_compact` 事件清空缓存。
376
- - **分支跳转(`/tree`)**:对话形态发生变化,代理键也可能不同,同样清空。
377
-
378
- ### OpenCode v2 的做法(对照)
379
-
380
- OpenCode 缓存的是 `{ model, account, token, until }`,注入令牌前先比对模型:
381
-
382
- ```js
383
- let T = m.get(q.sessionID)
384
- if (T?.token && T.model === q.model.id) q.headers["Copilot-Session-Token"] = T.token
385
- ```
386
-
387
- 也就是说:**模型不匹配时就不注入令牌**,让请求走普通通道。本扩展换成
388
- “重新协商到能覆盖该模型的通道”,因为实测多数 Auto 模型不带令牌会直接
389
- `model_not_supported`。
390
-
391
- ---
392
-
393
- ## 8. Agent 模式的自动降级
394
-
395
- 配额耗尽时 VS Code 会自动切到 Auto(源码 `switchToAutoModel`):
396
-
397
- ```js
398
- let o = (await Os.lm.selectChatModels({ id: "auto", vendor: "copilot" }))[0]
399
- ```
400
-
401
- 这是 `id: "auto"` 在源码中出现的真实场景——它是 **VS Code 的模型选择器伪 id**,
402
- 不是发给 HTTP API 的 model 值。
403
-
404
- ---
405
-
406
- ## 9. 模型目录的误导性
407
-
408
- `/models` 会把**未开通**的模型也描述得很完整:
409
-
410
- ```json
411
- {
412
- "id": "gpt-5.6-luna",
413
- "model_picker_enabled": false,
414
- "policy": { "state": "enabled" },
415
- "capabilities": { "limits": { "max_context_window_tokens": 328000 }, ... },
416
- "supported_endpoints": ["/responses", "ws:/responses"]
417
- }
418
- ```
419
-
420
- 但请求时返回 `model_not_supported`。`policy.state = "enabled"` **不代表真的
421
- 可用**,真正生效还需要匹配实验分组与套餐。
422
-
423
- **对具体模型而言,可信的授权来源是 `/models/session` 的 `available_models`**;
424
- `/auto` 能选中目录之外的模型,但没有清单可查(见第 6 章)。
425
- 本扩展据此裁剪模型列表。
426
-
427
- ### `/models` 认 GitHub refresh token
428
-
429
- 实测(2026-11-11):存储的 Copilot access token 已过期时,
430
-
431
- | 凭据 | `GET /models` |
432
- | --- | --- |
433
- | `access`(过期) | 401 `IDE token expired` |
434
- | **`refresh`**(GitHub OAuth) | **200,47 个条目** |
435
-
436
- OpenCode v2 正是用 `refresh` 拉目录。本扩展照此实现:目录优先用
437
- `refresh`,路由与对话仍用 `access`(路由端点不认 `refresh`)。
438
-
439
- ### baseURL 应取 `endpoints.api`
440
-
441
- `GET https://api.github.com/copilot_internal/user`(用 `refresh`)返回:
442
-
443
- ```json
444
- {
445
- "endpoints": {
446
- "api": "https://api.individual.githubcopilot.com",
447
- "proxy": "https://proxy.individual.githubcopilot.com",
448
- "origin-tracker": "https://origin-tracker.individual.githubcopilot.com"
449
- }
450
- }
451
- ```
452
-
453
- OpenCode 用它作为权威 baseURL(企业版/区域部署会不同),失败时才回退到
454
- 固定地址。本扩展的优先级:`endpoints.api` → access 令牌的 `proxy-ep` → 默认地址。
455
-
456
- ### 客户端版本影响目录内容
457
-
458
- 同一账号,不同 `User-Agent` 得到的条目数不同:
459
-
460
- | 客户端版本 | `/models` 条目数 |
461
- | --- | --- |
462
- | `GitHubCopilotChat/0.35.0` | 35 |
463
- | `GitHubCopilotChat/0.72.0` | 46 |
464
-
465
- 因此升级 VS Code 后应同步更新 `lib/protocol.ts` 的 `CLIENT_HEADERS`。
466
-
467
- ### `policy` 写接口可调,但不改变授权
468
-
469
- `POST /models/{id}/policy` 可以成功返回 200:
470
-
471
- ```http
472
- POST /models/gpt-5.6-luna/policy
473
- openai-intent: chat-policy
474
- x-interaction-type: chat-policy
475
-
476
- {"state":"enabled"}
477
- → 200 OK(空响应体)
478
- ```
479
-
480
- 调用后 `policy.state` 仍为 `enabled`,但 `/models/session` 的
481
- `available_models` **不变**,该模型依旧返回
482
- `Requested model not available for session`。
483
-
484
- **这个端点无助于提升权限,也未经验证可安全调用。不要用它作为排查手段**
485
- ——它是写接口,可能改变账号状态。
486
-
487
- ---
488
-
489
- ## 10. 模型自述不可信
490
-
491
- **这是本次排查中耗时最久的一个坑。** 直接问模型“你是谁”得到的答案几乎
492
- 都是错的,不能用于判断实际运行的模型。
493
-
494
- 实测(同一 `session_token`,同一问题,各问 2 次):
495
-
496
- | 实际请求的模型 | 模型自述 |
497
- | --- | --- |
498
- | `mai-code-1.1-flash` | `GPT-4o` |
499
- | `mai-code-1.1-flash` | `GPT-4o` |
500
- | `gpt-5.3-codex` | `o4-mini` |
501
- | `gpt-5.3-codex` | `ChatGPT` |
502
-
503
- 注意 `gpt-5.3-codex` 两次答案还不一致。
504
-
505
- ### 为什么会这样
506
-
507
- - 蒸馏/微调后的模型常继承训练数据里错误的自我认知;
508
- - 服务端不向模型披露自身 id;
509
- - 系统提示很少包含真实模型名。
510
-
511
- ### 正确的判断方法
512
-
513
- | 想知道 | 该看哪里 |
514
- | --- | --- |
515
- | 实际发给了哪个模型 | 请求体的 `model` 字段(或 `X-Initiator` 同级的请求记录) |
516
- | 服务端选了哪个模型 | `/models/session` 响应的 `selected_model` |
517
- | 授权范围 | 同响应的 `available_models` |
518
- | 用了多少额度 | `quota_remaining` 的变化 |
519
-
520
- **永远不要用模型的自然语言描述作为证据。** 如果用户说“UI 里看到的是 X 模型”,
521
- 先确认那是 UI 标签还是模型输出 —— 后者通常是错的。
522
-
523
- ### 曾因此得出的错误结论
524
-
525
- 本文早先版本曾推测“UI 显示 GPT-5.6 Luna 是 `model_picker_category:
526
- lightweight` 渲染的标签”。**这个推测是错的**:源码里标签取
527
- `selected_model.name ?? selected_model.id`,而服务端实际返回的
528
- `selected_model` 是纯字符串,该路径会直接失败。真实来源是模型自述。
529
-
530
- ---
531
-
532
- ## 11. 传输层
533
-
534
- `gpt-5.3-codex` 与 `mai-code-1.1-flash` 的 `supported_endpoints` 都是
535
- `["/responses", "ws:/responses"]`。
536
-
537
- 实测 WebSocket(Node 24 原生 `WebSocket`,`wss://api.individual.githubcopilot.com/responses`)
538
- 与非流式 HTTP 结果**完全一致**:授权模型 200,未授权模型 400。
539
-
540
- 因此不存在「VS Code 走 WS 所以能用」这种情况——模型选择差异与传输层无关。
541
-
542
- ---
543
-
544
- ## 12. 已排除的假设
545
-
546
- 排查过程中逐一验证并排除:
547
-
548
- | 假设 | 验证方式 | 结论 |
549
- | --- | --- | --- |
550
- | Token 过期 | 用 `copilot_internal/v2/token` 刷新新 token 重测 | 排除 |
551
- | 账号不同 | 读 VS Code `state.vscdb`,`accountId`/`sku`/`trackingId` 全一致 | 排除 |
552
- | 客户端版本旧 | 用 0.72.0 + vscode/1.144.1 头 | 排除(但目录条目数会变) |
553
- | 端点不对 | `/responses`、`/chat/completions`、`/v1/messages` | 排除 |
554
- | `/auto` 不存在 | 用 `2025-07-16` 请求得 404 | **推翻:`/auto` 存在,需 `2026-08-01`(第 2 章)** |
555
- | 长会话压缩断连 | 用 `auto-routed` 触发 `/compact` | **定位:整段历史被当成路由 prompt;见第 16 章** |
556
- | Host 不对 | `api.individual`、`api.githubcopilot`、`proxy.individual` | 排除 |
557
- | 缺版本头 | 补 `X-GitHub-Api-Version` | **这是真正的修复** |
558
- | 非流式限制 | `stream: true` + `Accept: text/event-stream` | 排除 |
559
- | WebSocket 专属 | Node 24 原生 WS 实测 | 排除 |
560
- | `model: "auto"` | 带 session token 请求 | 排除(返回 `not available for session`) |
561
- | 模型可选但 picker 隐藏 | `POST /models/{id}/policy` 启用后重测 | 排除(`available_models` 不变) |
562
- | `selected_model` 是对象 | 20+ 种参数/头组合实测 | 排除(始终为字符串) |
563
- | tier 参数影响选择 | `efficiency`/`balance`/`intelligence`/`fast` 逐个测 | 排除(结果相同) |
564
- | 客户端版本影响授权 | 0.72.0 → 1.0.0 逐个测 | 排除(`available_models` 一致) |
565
- | **UI 显示的是真实模型** | 问模型“你是谁” | **排除:模型自述错误,见第 10 章** |
566
-
567
- ---
568
-
569
- ## 13. 区域与实验分组
570
-
571
- 成功响应(`gpt-5.4-nano`)的响应头中出现:
572
-
573
- ```
574
- X-Copilot-API-Exp-Assignment-Context: <experiment-group>:<assignment-id>;permission_prompt_control:<assignment-id>;...
575
- ```
576
-
577
- 说明服务端按**实验分组**下发能力。令牌中也带地理信息:
578
-
579
- ```
580
- ip=<redacted>;asn=<redacted>;sl=<redacted>
581
- ```
582
-
583
- 令牌中包含地理信息,但具体值不纳入记录。
584
-
585
- 模型可用性可能同时受套餐、实验分组与地区影响。本文结论基于单一账号,
586
- 其他账号的 `available_models` 应以其 `/models/session` 实际返回为准。
587
-
588
- ---
589
-
590
- ## 14. 复核流程
591
-
592
- GitHub 改协议后,按以下顺序定位:
593
-
594
- ```sh
595
- # 1. 令牌是否有效
596
- curl -s https://api.github.com/copilot_internal/v2/token \
597
- -H "Authorization: Bearer <refresh token>"
598
-
599
- # 2. 目录是否可达、条目数是否变化
600
- curl -s https://api.individual.githubcopilot.com/models \
601
- -H "Authorization: Bearer <access token>" \
602
- -H "X-GitHub-Api-Version: 2025-07-16" \
603
- -H "Editor-Version: vscode/1.144.1" \
604
- -H "Copilot-Integration-Id: vscode-chat"
605
-
606
- # 3. 旧通道路由是否仍可用、授权清单是否变化
607
- curl -s -X POST https://api.individual.githubcopilot.com/models/session \
608
- -H "Authorization: Bearer <access token>" \
609
- -H "Content-Type: application/json" \
610
- -H "X-GitHub-Api-Version: 2025-07-16" \
611
- -H "Editor-Version: vscode/1.144.1" \
612
- -H "Copilot-Integration-Id: vscode-chat" \
613
- -d '{"auto_mode":{"enabled":true},"prompt":"hi"}'
614
-
615
- # 3b. 新通道路由(注意版本头必须是 2026-08-01)
616
- curl -s -X POST https://api.individual.githubcopilot.com/auto \
617
- -H "Authorization: Bearer <access token>" \
618
- -H "Content-Type: application/json" \
619
- -H "X-GitHub-Api-Version: 2026-08-01" \
620
- -H "Editor-Version: vscode/1.144.1" \
621
- -H "Copilot-Integration-Id: vscode-chat" \
622
- -d '{"prompt":"hi"}'
623
-
624
- # 4. 用上一步的 session_token 实调(模型必须是该令牌覆盖的那个)
625
- curl -s -X POST https://api.individual.githubcopilot.com/responses \
626
- -H "Authorization: Bearer <access token>" \
627
- -H "Content-Type: application/json" \
628
- -H "Copilot-Session-Token: <session_token>" \
629
- -H "X-GitHub-Api-Version: 2025-07-16" \
630
- -H "X-Initiator: user" \
631
- -d '{"model":"<selected_model>","input":[{"role":"user","content":"hi"}],"stream":false,"max_output_tokens":16}'
632
- ```
633
-
634
- 常见定位:
635
-
636
- | 现象 | 先查 |
637
- | --- | --- |
638
- | 第 2 步 401 `IDE token expired` | 改用 `refresh` 令牌;或先走第 1 步刷新(第 9 章) |
639
- | 第 3b 步 404 `invalid apiVersion` | 版本头必须是 `2026-08-01`(第 3 章) |
640
- | 第 3 步失败而第 2 步正常 | `auto_mode` 是否仍为对象形式 |
641
- | 第 4 步 `model_not_supported` 而第 3 步成功 | `X-GitHub-Api-Version` 是否被带上 |
642
- | 第 4 步 `Requested model not available for session` | 模型不属于该令牌的覆盖范围(第 6 章) |
643
- | 第 4 步 `invalid_request_body` | `max_output_tokens` 过小(实测 8 不行,16 可以) |
644
-
645
- ### 排查时的三条原则
646
-
647
- 1. **不要相信模型的自我描述。** 它说自己是哪个模型,与实际请求的 `model`
648
- 字段无关(第 10 章)。需要证据时看请求体与 `/models/session` 响应。
649
- 2. **不要用写接口做诊断。** `POST /models/{id}/policy` 能返回 200,
650
- 但不会改变授权,且可能影响账号状态。
651
- 3. **先区分「看得到」与「用得到」。** `/models` 列出 ≠ 可调用;
652
- `policy: enabled` ≠ 有权限;UI 显示 ≠ 实际模型。唯一可信的是
653
- `available_models` 加一次真实调用。
654
-
655
- ---
656
-
657
- ## 附:与 pi 内置 provider 的差异
658
-
659
- | 方面 | 内置 `github-copilot` | `copilot-auto` |
660
- | --- | --- | --- |
661
- | 通道 | 普通 | Auto |
662
- | 凭据 | 自有 OAuth,自动刷新 | **只读**复用内置凭据 |
663
- | 模型来源 | 内置目录 + `availableModelIds` | `/models` ∩ `available_models` |
664
- | 请求头 | pi-ai 自动注入 Copilot 头 | 自行补齐 |
665
- | 适用模型 | `gpt-4.1`、`gpt-4o` 等 | `gpt-5.3-codex`、`mai-code-1.1-flash` |
666
-
667
- 两者并存互补,不冲突。
668
-
669
- ---
670
-
671
- ## 15. 额度查询
672
-
673
- ### 端点与令牌
674
-
675
- 额度查询在 `https://api.github.com/copilot_internal/user`,使用
676
- **GitHub OAuth 令牌**(`auth.json` 中 `github-copilot.refresh`),
677
- 不是 Copilot 访问令牌(`access`)。
678
-
679
- ```diff
680
- GET https://api.github.com/copilot_internal/user
681
- + Authorization: Bearer <refresh token>
682
- → 200
683
-
684
- GET https://api.github.com/copilot_internal/user
685
- - Authorization: Bearer <access token>
686
- → 401 Bad credentials
687
- ```
688
-
689
- 注意 Copilot 访问令牌的格式是 `tid=...;exp=...;sku=...`,不是 JWT,
690
- 拿去请求 `api.github.com` 会被拒。两者不能混用。
691
-
692
- ### 已排除的额度端点
693
-
694
- | 路径 | 主机 | 结果 |
695
- | --- | --- | --- |
696
- | `/copilot_internal/v2/quota` | api.github.com | 404 |
697
- | `/copilot_internal/v2/entitlements` | api.github.com | 404 |
698
- | `/copilot_internal/subscription` | api.github.com | 404 |
699
- | `/copilot_internal/premium/usage` | api.github.com | 404 |
700
- | `/quota`、`/usage`、`/me` | api.individual.githubcopilot.com | 404 |
701
-
702
- 额度数据只在 `/copilot_internal/user` 的 `quota_snapshots` 里。
703
-
704
- ### 响应结构(`free_educational_quota` 字段示例,账号和额度值已脱敏)
705
-
706
- ```json
707
- {
708
- "login": "sample-user",
709
- "access_type_sku": "free_educational_quota",
710
- "copilot_plan": "individual",
711
- "assigned_date": "<date>",
712
- "quota_reset_date": "<reset-date>",
713
- "quota_snapshots": {
714
- "chat": { "unlimited": true, "percent_remaining": 100.0 },
715
- "completions": { "unlimited": true, "percent_remaining": 100.0 },
716
- "premium_interactions": {
717
- "unlimited": false,
718
- "has_quota": true,
719
- "credits_used": 8,
720
- "remaining": 95,
721
- "quota_remaining": 95.7,
722
- "entitlement": 200,
723
- "percent_remaining": 47.9,
724
- "token_based_billing": true
725
- }
726
- },
727
- "token_based_billing": true
728
- }
729
- ```
730
-
731
- ### 字段语义
732
-
733
- | 字段 | 含义 |
734
- | --- | --- |
735
- | `unlimited` | 该维度是否无上限;为 true 时其它数值字段常为 0 |
736
- | `has_quota` | 是否具备该额度 |
737
- | `credits_used` | 已消耗量(credits) |
738
- | `remaining` | 剩余量(服务端已取整) |
739
- | `entitlement` | 配额总量 |
740
- | `percent_remaining` | 剩余百分比,0-100 |
741
- | `token_based_billing` | 是否按 token 计费 |
742
-
743
- **注意 `unlimited: true` 时 `remaining` / `entitlement` 都是 0**,
744
- 不能直接相除算百分比,必须优先判断 `unlimited`。服务端在响应头里用
745
- `ent=-1` 表达同一语义。
746
-
747
- ### 只有 `free_limited_copilot` 看 `chat` 维度
748
-
749
- VS Code 决定读哪个维度(源码 `extension.js`):
750
-
751
- ```js
752
- get isFreeUser() { return this.sku === "free_limited_copilot" }
753
- // 读额度时:
754
- isFreeUser ? header("x-quota-snapshot-chat")
755
- : header("x-quota-snapshot-premium_models")
756
- ?? header("x-quota-snapshot-premium_interactions")
757
- ```
758
-
759
- | SKU | 计费维度 |
760
- | --- | --- |
761
- | `free_limited_copilot` | `chat` |
762
- | **其他全部**(含 `free_educational_quota`、`individual`、`pro`) | `premium_interactions` |
763
-
764
- **因此对 `free_educational_quota` 来说,`chat` / `completions` 的
765
- `unlimited: true` 不构成额外免费通道。所有对话(无论用哪个模型)
766
- 都从 `premium_interactions` 扣减。**
767
-
768
- 回应中 `x-quota-snapshot-*` 响应头可直接观察四个维度:
769
-
770
- ```
771
- x-quota-snapshot-chat = ent=-1&ov=0.0&ovPerm=false&rem=100.0&rst=<reset-date>
772
- x-quota-snapshot-completions = ent=-1&ov=0.0&ovPerm=false&rem=100.0&rst=<reset-date>
773
- x-quota-snapshot-premium_interactions= ent=200&ov=0.0&ovPerm=false&rem=<remaining-percent>&rst=<reset-date>
774
- ```
775
-
776
- ### 计费按 token 折算,不是按请求次数
777
-
778
- 实测(`mai-code-1.1-flash`,全系列最便宜的一款):
779
-
780
- | 输入 tokens | 输出 tokens | 扣减 premium |
781
- | --- | --- | --- |
782
- | ~30 | ~5 | **0**(未达最小计费单位) |
783
- | 10,012 | 34 | 0.2 |
784
- | 30,010 | 6,116 | 1.2 |
785
-
786
- 标定结果:**约 3 万 token ≈ 1 次 premium**。这与其 `token_prices`
787
- 一致(输入 20 USD/百万 token,输出 120 USD/百万 token)。
788
-
789
- 推论:**没有任何 Auto 模型是免费的**。`mai-code-1.1-flash` 与
790
- `gpt-5.3-codex` 的区别只是单价(后者输入价为 175 USD/百万,约为前者 9 倍)。
791
-
792
- ### 测量注意事项
793
-
794
- 1. **扣减延迟 40-50 秒**。立即查询会得到旧值,容易误判为「不扣费」。
795
- 2. **`remaining` / `credits_used` 是取整值**。0.4 这类消耗会被抹成 0,
796
- 必须读 `quota_remaining`(浮点)才能看到小额变化。
797
-
798
- ### 与 Auto 折扣的关系
799
-
800
- `/models/session` 的 `discounted_costs`(实测 `0.1`)与额度快照是两套
801
- 独立机制:前者是单次请求的模型倍率,后者是账号级的周期配额。
1
+ # GitHub Copilot Auto 协议逆向记录
2
+
3
+ 本文记录 `copilot-auto` 扩展所依赖的协议事实,以及得出这些结论的验证过程。
4
+ GitHub 没有公开这套接口,以下内容全部来自实测与本机 VS Code 扩展源码。
5
+
6
+ **用途**:GitHub 改动协议导致扩展失效时,按本文逐项复核,定位是哪一环变了。
7
+
8
+ - 实测日期:2026-11-09
9
+ - 参照实现:VS Code `copilot-chat` **0.72.0**(内置扩展,随 VS Code 1.144.1 分发)
10
+ - 源码位置:`<VS Code 安装目录>/resources/app/extensions/copilot/dist/extension.js`
11
+ - 账号标识与实际额度值已从本文样例中移除;套餐字段以 `free_educational_quota` 作为结构示例。
12
+
13
+ ## 三条先读结论
14
+
15
+ 在深入细节前,先记住这三条。它们能让排查少走大部分弯路:
16
+
17
+ 1. **两个路由通道的令牌作用域不同,不可互换。**
18
+ `/auto` 的令牌只覆盖它选中的那个模型;`/models/session` 的令牌覆盖其
19
+ `available_models`。用错模型返回 `Requested model not available for session`。
20
+ 2. **模型自述不可信。** 问模型“你是谁”会得到错误且不一致的答案,
21
+ 不能用于判断实际运行的模型(第 10 章)。
22
+ 3. **缺 `X-GitHub-Api-Version` 会返回误导性的 `model_not_supported`。**
23
+ 看到这个错误先检查请求头,不要直接归因为权限问题(第 3 章)。
24
+
25
+ > 2026-11-11 修订:本文早先版本声称 `POST /auto` 返回 404「是死代码」。
26
+ > 该结论**错误**,原因是用了旧版本头 `2025-07-16`。`/auto` 真实存在,
27
+ > 只接受 `X-GitHub-Api-Version: 2026-08-01`。详见第 2 章。
28
+
29
+ ---
30
+
31
+ ## 1. 核心结论
32
+
33
+ Copilot 有**两条相互隔离的 Auto 路由通道**:
34
+
35
+ | 通道 | 调用方式 | 令牌覆盖范围 |
36
+ | --- | --- | --- |
37
+ | `/auto` | `POST /auto`(版本头 `2026-08-01`) | **仅它选中的那个模型** |
38
+ | `/models/session` | 先 `auto_mode` 协商(版本头 `2025-07-16`) | 响应里的 `available_models` |
39
+
40
+ 两者下发同一种 `session_token`,都必须以 `Copilot-Session-Token` 头发给
41
+ `/responses`。不带该头时,Auto 专属模型一律返回
42
+ `400 model_not_supported`(已实测)。
43
+
44
+ `gpt-5.3-codex` 在普通通道下**即使**出现在 `/models` 目录、`policy.state` 标为
45
+ `enabled`、`capabilities` 齐全,请求时仍返回 `400 model_not_supported`。
46
+ 只有 Auto 通道能调用。
47
+
48
+ 这就是「VS Code 能用、pi 不能用」的根因:pi 内置的 `github-copilot` provider
49
+ 只实现了普通通道。
50
+
51
+ ---
52
+
53
+ ## 2. 端点
54
+
55
+ ```
56
+ POST /auto Auto 路由(新),返回选中模型的完整对象
57
+ POST /models/session Auto 路由(旧),返回 id 与授权清单
58
+ GET /models 模型目录(能力、端点、计费、policy)
59
+ POST /responses 实际对话
60
+ ```
61
+
62
+ 以上均在 `api.individual.githubcopilot.com`(或 `endpoints.api` 返回的主机)。
63
+
64
+ 额度与套餐信息在**另一个主机**:
65
+
66
+ ```
67
+ GET https://api.github.com/copilot_internal/user
68
+ GET https://api.github.com/copilot_internal/v2/token
69
+ ```
70
+
71
+ ### `/auto` 确实存在(早先结论已推翻)
72
+
73
+ 早先版本记录「`POST /auto` 实测返回 404,是死代码」。**那是错的**。
74
+ 实测(2026-11-11):
75
+
76
+ | 版本头 | `POST /auto` |
77
+ | --- | --- |
78
+ | `2025-07-16` | `404 bad request: error: invalid apiVersion` |
79
+ | **`2026-08-01`** | **200,返回完整路由结果** |
80
+
81
+ 因此 404 错误描述的是**版本不受支持**,不是路径不存在。OpenCode v2 使用
82
+ 正是 `2026-08-01`。
83
+
84
+ ### `/auto` 请求体
85
+
86
+ ```json
87
+ { "prompt": "write quicksort in python", "has_image": true }
88
+ ```
89
+
90
+ `has_image` 仅在请求含图片时带上。实测**无法指定模型**:附加 `model`、
91
+ `model_id` 或 `selected_model` 字段均被忽略,服务端照旧自行选择。
92
+
93
+ ### `/auto` 响应结构
94
+
95
+ 比 `/models/session` 多一层:`selected_model` 是**完整模型对象**,
96
+ 因此无需再查目录就能拿到能力和计费:
97
+
98
+ ```json
99
+ {
100
+ "session_token": "<redacted>",
101
+ "selected_model": {
102
+ "id": "gpt-5.6-luna",
103
+ "name": "GPT-5.6 Luna",
104
+ "version": "gpt-5.6-luna",
105
+ "supported_endpoints": ["/responses", "ws:/responses"],
106
+ "capabilities": {
107
+ "family": "gpt-5.6-luna",
108
+ "limits": { "max_context_window_tokens": 1050000, "max_output_tokens": 128000, "max_prompt_tokens": 922000 },
109
+ "supports": { "tool_calls": true, "streaming": true, "vision": true, "reasoning_effort": ["none", "low", "medium", "high", "xhigh", "max"] }
110
+ },
111
+ "billing": { "token_prices": { "batch_size": 1000000, "default": { "input_price": 20, "output_price": 120 } } }
112
+ },
113
+ "expires_at": 1794472092,
114
+ "multi_turn": true,
115
+ "multi_turn_mode": "stateless",
116
+ "hydra_scores": {},
117
+ "discounted_costs": { "gpt-5.6-luna": 0.1 },
118
+ "selection_reason": "..."
119
+ }
120
+ ```
121
+
122
+ 注意 `selected_model` 对象**不含** `policy`、`model_picker_enabled`、
123
+ `billing.auto_discount`;这些只在 `/models` 目录里出现。
124
+ `/auto` 的 `session_token` 载荷与旧通道也不同:
125
+
126
+ ```json
127
+ {
128
+ "auto_session_id": "<redacted>",
129
+ "auto_tier": "balance",
130
+ "reasoning_bucket": "low",
131
+ "selected_model": "gpt-5.6-luna",
132
+ "discounted_costs": { "gpt-5.6-luna": 0.1 },
133
+ "sub": "<redacted>",
134
+ "iat": 1794385692,
135
+ "exp": 1794472092
136
+ }
137
+ ```
138
+
139
+ 实测有效期 24 小时(`exp - iat = 86400`);旧通道是 1 小时。
140
+
141
+ ### 已排除的路径
142
+
143
+ | 路径 | 实测结果 |
144
+ | --- | --- |
145
+ | `POST /auto`(版本头 `2025-07-16`) | 404 `invalid apiVersion` |
146
+ | `POST /models/session/intent` | 400 `Missing Copilot-Session-Token header` |
147
+ | `POST /v1/messages` | `model_not_supported`(Anthropic 通道,与本议题无关) |
148
+
149
+ ---
150
+
151
+ ## 3. 请求头(最容易踩的坑)
152
+
153
+ ### 必需头
154
+
155
+ ```http
156
+ Authorization: Bearer <copilot access token>
157
+ Content-Type: application/json
158
+ User-Agent: GitHubCopilotChat/0.72.0
159
+ Editor-Version: vscode/1.144.1
160
+ Editor-Plugin-Version: copilot-chat/0.72.0
161
+ Copilot-Integration-Id: vscode-chat
162
+ X-GitHub-Api-Version: 2025-07-16
163
+ ```
164
+
165
+ ### `X-GitHub-Api-Version` 是必需的
166
+
167
+ **这是最容易被误判的一点。** 实测同一份有效 `session_token`、同一个模型:
168
+
169
+ ```diff
170
+ POST /responses
171
+ Copilot-Session-Token: <valid>
172
+ + X-GitHub-Api-Version: 2025-07-16
173
+ → 200 OK
174
+
175
+ POST /responses
176
+ Copilot-Session-Token: <valid>
177
+ - (缺少 X-GitHub-Api-Version)
178
+ → 400 {"code":"model_not_supported"}
179
+ ```
180
+
181
+ 缺少它时返回的是 `model_not_supported`,不是「缺少版本头」,因此极易被误判成
182
+ 账号没权限。
183
+
184
+ 有效版本号(实测):
185
+
186
+ | 版本 | `/models` | `/models/session` | `/auto` |
187
+ | --- | --- | --- | --- |
188
+ | `2025-04-01` | 200 | 404 Not Found | — |
189
+ | `2025-05-01` | 200 | — | — |
190
+ | `2025-07-16` | 200 | **200** | **404 `invalid apiVersion`** |
191
+ | `2026-01-09` | 200 | 200 | — |
192
+ | **`2026-08-01`** | 200 | 200 | **200** |
193
+ | `2026-01-01`、`2026-11-01`、`latest` | `invalid apiVersion` | — | — |
194
+
195
+ `/models/session` 要求 ≥ `2025-07-16`。证据:用 `2025-04-01` 请求
196
+ `/models/session/intent` 时明确返回
197
+ `auto-intent proxy requires API version 2025-07-16 or later`。
198
+
199
+ `/auto` 要求 `2026-08-01`。本扩展因此对两个通道使用不同版本头:
200
+ `/auto` 用 `AUTO_API_VERSION = "2026-08-01"`,其余用 `API_VERSION = "2025-07-16"`。
201
+ `/responses` 对两者都接受(实测均 200)。
202
+
203
+ ### 客户端身份头会被校验
204
+
205
+ ```http
206
+ User-Agent: GitHubCopilotChat/0.72.0
207
+ Editor-Version: vscode/1.144.1
208
+ Editor-Plugin-Version: copilot-chat/0.72.0
209
+ Copilot-Integration-Id: vscode-chat
210
+ ```
211
+
212
+ 缺失或伪造的结果:
213
+
214
+ | 情况 | 响应 |
215
+ | --- | --- |
216
+ | 完全不带 `Editor-Version` | `400 bad request: missing Editor-Version header for IDE auth` |
217
+ | `Copilot-Integration-Id` 用未知值 | `400 bad request: unknown Copilot-Integration-Id` |
218
+ | 用 `copilot-cli` / `vscode-agent` / `chat` | 同上,均被拒 |
219
+
220
+ 已知有效值只有 `vscode-chat`(实测),以及 `copilot-chat`(源码中出现)。
221
+
222
+ ### 动态头
223
+
224
+ ```http
225
+ X-Initiator: user | agent
226
+ Openai-Intent: conversation-edits
227
+ Copilot-Vision-Request: true # 仅在请求含图片时
228
+ ```
229
+
230
+ `X-Initiator` 判定规则(源码 `inferCopilotInitiator`):最后一条消息不是 `user`
231
+ 时为 `agent`,否则 `user`。
232
+
233
+ **重要**:pi-ai 只在 `model.provider === "github-copilot"` 时才注入这三个头。
234
+ 自定义 provider id 必须自己补,否则请求被拒。
235
+
236
+ ---
237
+
238
+ ## 4. `/models/session` 请求体
239
+
240
+ ### 正确形式
241
+
242
+ ```json
243
+ {
244
+ "auto_mode": { "enabled": true },
245
+ "prompt": "write quicksort in python"
246
+ }
247
+ ```
248
+
249
+ `auto_mode` 必须是**对象**。
250
+
251
+ ### 已排除的形式
252
+
253
+ | 请求体 | 响应 |
254
+ | --- | --- |
255
+ | `{"auto_mode": true, ...}` | `400 Invalid request body` |
256
+ | `{"auto_mode": "auto", ...}` | `400 Invalid request body` |
257
+ | `{"auto_mode": 1, ...}` | `400 Invalid request body` |
258
+ | `{"auto_mode": {"enabled": true}}`(无 prompt) | 200,可正常路由 |
259
+ | `{"prompt": "..."}`(无 auto_mode) | `400 Missing auto_mode field in request` |
260
+
261
+ `prompt` 可省略,但省略后路由退化为服务端默认选择。
262
+
263
+ ---
264
+
265
+ ### 响应结构(`free_educational_quota` 字段示例,账号、会话和额度值均为占位值)
266
+
267
+ ```json
268
+ {
269
+ "available_models": ["mai-code-1.1-flash", "gpt-5.3-codex"],
270
+ "selected_model": "mai-code-1.1-flash",
271
+ "session_token": "<redacted>",
272
+ "expires_at": 4600,
273
+ "discounted_costs": { "gpt-5.3-codex": 0.1, "mai-code-1.1-flash": 0.1 }
274
+ }
275
+ ```
276
+
277
+ `session_token` 是标准 JWT,载荷(仅读取,不验签):
278
+
279
+ ```json
280
+ {
281
+ "available_models": ["mai-code-1.1-flash", "gpt-5.3-codex"],
282
+ "selected_model": "mai-code-1.1-flash",
283
+ "sub": "<redacted>",
284
+ "iat": 1000,
285
+ "exp": 4600,
286
+ "discounted_costs": { "gpt-5.3-codex": 0.1, "mai-code-1.1-flash": 0.1 },
287
+ "auto_session_id": "<redacted>"
288
+ }
289
+ ```
290
+
291
+ 有效期约 1 小时(`exp - iat = 3600`)。
292
+
293
+ ---
294
+
295
+ ## 6. 授权边界
296
+
297
+ 两个通道的令牌作用域**不同**,这是本扩展最关键的设计约束。
298
+
299
+ ### `/models/session`:令牌覆盖 `available_models`
300
+
301
+ `session_token` 的授权范围**严格等于** `available_models`:
302
+
303
+ | 模型 | 带该 session token | 不带 |
304
+ | --- | --- | --- |
305
+ | `mai-code-1.1-flash` | 200 | 400 |
306
+ | `gpt-5.3-codex` | 200 | 400 |
307
+ | `gpt-4.1` | **400** | 400(该模型不支持 `/responses`) |
308
+ | `gpt-5.6-luna` | **400** | 400 |
309
+
310
+ 超出范围即使 token 有效也被拒:
311
+
312
+ ```json
313
+ {"error": {"message": "Requested model not available for session"}}
314
+ ```
315
+
316
+ ### `/auto`:令牌只覆盖它选中的那个模型
317
+
318
+ 实测(2026-11-11,同一账号):
319
+
320
+ | 请求 | 结果 |
321
+ | --- | --- |
322
+ | `/auto` 的 token + 它选中的 `gpt-5.6-luna` | 200 |
323
+ | 同上 token + `mai-code-1.1-flash` | 400 `Requested model not available for session` |
324
+ | 另一次 `/auto` 的 token(同样选中 luna) | 200(同模型可互换) |
325
+ | 任意 token 不带 + 任一 Auto 模型 | 400 `model_not_supported` |
326
+
327
+ 因此**不能**“拿一个 token 打所有模型”;换模型必须重新协商,且
328
+ 要选对通道。
329
+
330
+ ### `model: "auto"` 不是可发送的 model 值
331
+
332
+ 必须用服务端返回的具体模型 id。注意 `Requested model not available for session`
333
+ 与 `model_not_supported` **不同**——前者说明 Auto 通道认出了请求,只是该模型
334
+ 不在本次令牌范围内。
335
+
336
+ ### 请求体约束
337
+
338
+ `max_output_tokens` 过小(实测 `8`)会返回 `400 invalid_request_body`;
339
+ 同一请求改为 `16` 即 200。排查时不要据此误判为令牌问题。
340
+
341
+ ---
342
+
343
+ ## 7. 会话缓存策略(对齐 VS Code 与 OpenCode)
344
+
345
+ VS Code 的 `AutomodeService` 缓存规则(源码核实):
346
+
347
+ ```js
348
+ _evictOldestSessions() // CACHE_MAX_ENTRIES = 50
349
+ _isCacheEntryCompatible(n, r, o) {
350
+ return n.tier === r
351
+ && !this._isSessionExpired(n)
352
+ && (!pze(o) || n.endpoint.supportsVision)
353
+ }
354
+ _routingSingler.getOrCreate(`${a}|${o}|${pze(n)}`, u) // 并发去重
355
+ ```
356
+
357
+ 关键点:
358
+
359
+ - **缓存键是 conversation id,不是 prompt**
360
+ 按 prompt 缓存会让几乎每个请求都重新协商,且同一次对话的模型会在多轮间跳转。
361
+ - 最多 50 条,超出淘汰最早插入的
362
+ - 同一键并发请求只协商一次
363
+ - 图片请求必须复用已确认支持 vision 的会话
364
+
365
+ 本扩展按此实现(`lib/sessions.ts`)。pi 的 `streamSimple` 不暴露会话 id,
366
+ 因此用「首次用户消息摘要 + baseUrl」作为代理键。
367
+
368
+ ### 额外的失效条件
369
+
370
+ 除过期与 vision 外,本扩展还按以下条件判定缓存不可复用:
371
+
372
+ - **请求模型已变**:缓存条目记录 `requestedModel`,用户从 `auto` 切到具体模型
373
+ (或反之)时必定重新协商。不复用是因为两个通道的令牌作用域不同(第 6 章)。
374
+ - **上下文被压缩**:对应 OpenCode 的 `compaction` hook,它把该会话的 `until`
375
+ 置 0。pi 侧监听 `session_compact` 事件清空缓存。
376
+ - **分支跳转(`/tree`)**:对话形态发生变化,代理键也可能不同,同样清空。
377
+
378
+ ### OpenCode v2 的做法(对照)
379
+
380
+ OpenCode 缓存的是 `{ model, account, token, until }`,注入令牌前先比对模型:
381
+
382
+ ```js
383
+ let T = m.get(q.sessionID)
384
+ if (T?.token && T.model === q.model.id) q.headers["Copilot-Session-Token"] = T.token
385
+ ```
386
+
387
+ 也就是说:**模型不匹配时就不注入令牌**,让请求走普通通道。本扩展换成
388
+ “重新协商到能覆盖该模型的通道”,因为实测多数 Auto 模型不带令牌会直接
389
+ `model_not_supported`。
390
+
391
+ ---
392
+
393
+ ## 8. Agent 模式的自动降级
394
+
395
+ 配额耗尽时 VS Code 会自动切到 Auto(源码 `switchToAutoModel`):
396
+
397
+ ```js
398
+ let o = (await Os.lm.selectChatModels({ id: "auto", vendor: "copilot" }))[0]
399
+ ```
400
+
401
+ 这是 `id: "auto"` 在源码中出现的真实场景——它是 **VS Code 的模型选择器伪 id**,
402
+ 不是发给 HTTP API 的 model 值。
403
+
404
+ ---
405
+
406
+ ## 9. 模型目录的误导性
407
+
408
+ `/models` 会把**未开通**的模型也描述得很完整:
409
+
410
+ ```json
411
+ {
412
+ "id": "gpt-5.6-luna",
413
+ "model_picker_enabled": false,
414
+ "policy": { "state": "enabled" },
415
+ "capabilities": { "limits": { "max_context_window_tokens": 328000 }, ... },
416
+ "supported_endpoints": ["/responses", "ws:/responses"]
417
+ }
418
+ ```
419
+
420
+ 但请求时返回 `model_not_supported`。`policy.state = "enabled"` **不代表真的
421
+ 可用**,真正生效还需要匹配实验分组与套餐。
422
+
423
+ **对具体模型而言,可信的授权来源是 `/models/session` 的 `available_models`**;
424
+ `/auto` 能选中目录之外的模型,但没有清单可查(见第 6 章)。
425
+ 本扩展据此裁剪模型列表。
426
+
427
+ ### `/models` 认 GitHub refresh token
428
+
429
+ 实测(2026-11-11):存储的 Copilot access token 已过期时,
430
+
431
+ | 凭据 | `GET /models` |
432
+ | --- | --- |
433
+ | `access`(过期) | 401 `IDE token expired` |
434
+ | **`refresh`**(GitHub OAuth) | **200,47 个条目** |
435
+
436
+ OpenCode v2 正是用 `refresh` 拉目录。本扩展照此实现:目录优先用
437
+ `refresh`,路由与对话仍用 `access`(路由端点不认 `refresh`)。
438
+
439
+ ### baseURL 应取 `endpoints.api`
440
+
441
+ `GET https://api.github.com/copilot_internal/user`(用 `refresh`)返回:
442
+
443
+ ```json
444
+ {
445
+ "endpoints": {
446
+ "api": "https://api.individual.githubcopilot.com",
447
+ "proxy": "https://proxy.individual.githubcopilot.com",
448
+ "origin-tracker": "https://origin-tracker.individual.githubcopilot.com"
449
+ }
450
+ }
451
+ ```
452
+
453
+ OpenCode 用它作为权威 baseURL(企业版/区域部署会不同),失败时才回退到
454
+ 固定地址。本扩展的优先级:`endpoints.api` → access 令牌的 `proxy-ep` → 默认地址。
455
+
456
+ ### 客户端版本影响目录内容
457
+
458
+ 同一账号,不同 `User-Agent` 得到的条目数不同:
459
+
460
+ | 客户端版本 | `/models` 条目数 |
461
+ | --- | --- |
462
+ | `GitHubCopilotChat/0.35.0` | 35 |
463
+ | `GitHubCopilotChat/0.72.0` | 46 |
464
+
465
+ 因此升级 VS Code 后应同步更新 `lib/protocol.ts` 的 `CLIENT_HEADERS`。
466
+
467
+ ### `policy` 写接口可调,但不改变授权
468
+
469
+ `POST /models/{id}/policy` 可以成功返回 200:
470
+
471
+ ```http
472
+ POST /models/gpt-5.6-luna/policy
473
+ openai-intent: chat-policy
474
+ x-interaction-type: chat-policy
475
+
476
+ {"state":"enabled"}
477
+ → 200 OK(空响应体)
478
+ ```
479
+
480
+ 调用后 `policy.state` 仍为 `enabled`,但 `/models/session` 的
481
+ `available_models` **不变**,该模型依旧返回
482
+ `Requested model not available for session`。
483
+
484
+ **这个端点无助于提升权限,也未经验证可安全调用。不要用它作为排查手段**
485
+ ——它是写接口,可能改变账号状态。
486
+
487
+ ---
488
+
489
+ ## 10. 模型自述不可信
490
+
491
+ **这是本次排查中耗时最久的一个坑。** 直接问模型“你是谁”得到的答案几乎
492
+ 都是错的,不能用于判断实际运行的模型。
493
+
494
+ 实测(同一 `session_token`,同一问题,各问 2 次):
495
+
496
+ | 实际请求的模型 | 模型自述 |
497
+ | --- | --- |
498
+ | `mai-code-1.1-flash` | `GPT-4o` |
499
+ | `mai-code-1.1-flash` | `GPT-4o` |
500
+ | `gpt-5.3-codex` | `o4-mini` |
501
+ | `gpt-5.3-codex` | `ChatGPT` |
502
+
503
+ 注意 `gpt-5.3-codex` 两次答案还不一致。
504
+
505
+ ### 为什么会这样
506
+
507
+ - 蒸馏/微调后的模型常继承训练数据里错误的自我认知;
508
+ - 服务端不向模型披露自身 id;
509
+ - 系统提示很少包含真实模型名。
510
+
511
+ ### 正确的判断方法
512
+
513
+ | 想知道 | 该看哪里 |
514
+ | --- | --- |
515
+ | 实际发给了哪个模型 | 请求体的 `model` 字段(或 `X-Initiator` 同级的请求记录) |
516
+ | 服务端选了哪个模型 | `/models/session` 响应的 `selected_model` |
517
+ | 授权范围 | 同响应的 `available_models` |
518
+ | 用了多少额度 | `quota_remaining` 的变化 |
519
+
520
+ **永远不要用模型的自然语言描述作为证据。** 如果用户说“UI 里看到的是 X 模型”,
521
+ 先确认那是 UI 标签还是模型输出 —— 后者通常是错的。
522
+
523
+ ### 曾因此得出的错误结论
524
+
525
+ 本文早先版本曾推测“UI 显示 GPT-5.6 Luna 是 `model_picker_category:
526
+ lightweight` 渲染的标签”。**这个推测是错的**:源码里标签取
527
+ `selected_model.name ?? selected_model.id`,而服务端实际返回的
528
+ `selected_model` 是纯字符串,该路径会直接失败。真实来源是模型自述。
529
+
530
+ ---
531
+
532
+ ## 11. 传输层
533
+
534
+ `gpt-5.3-codex` 与 `mai-code-1.1-flash` 的 `supported_endpoints` 都是
535
+ `["/responses", "ws:/responses"]`。
536
+
537
+ 实测 WebSocket(Node 24 原生 `WebSocket`,`wss://api.individual.githubcopilot.com/responses`)
538
+ 与非流式 HTTP 结果**完全一致**:授权模型 200,未授权模型 400。
539
+
540
+ 因此不存在「VS Code 走 WS 所以能用」这种情况——模型选择差异与传输层无关。
541
+
542
+ ---
543
+
544
+ ## 12. 已排除的假设
545
+
546
+ 排查过程中逐一验证并排除:
547
+
548
+ | 假设 | 验证方式 | 结论 |
549
+ | --- | --- | --- |
550
+ | Token 过期 | 用 `copilot_internal/v2/token` 刷新新 token 重测 | 排除 |
551
+ | 账号不同 | 读 VS Code `state.vscdb`,`accountId`/`sku`/`trackingId` 全一致 | 排除 |
552
+ | 客户端版本旧 | 用 0.72.0 + vscode/1.144.1 头 | 排除(但目录条目数会变) |
553
+ | 端点不对 | `/responses`、`/chat/completions`、`/v1/messages` | 排除 |
554
+ | `/auto` 不存在 | 用 `2025-07-16` 请求得 404 | **推翻:`/auto` 存在,需 `2026-08-01`(第 2 章)** |
555
+ | 长会话压缩断连 | 用 `auto-routed` 触发 `/compact` | **定位:整段历史被当成路由 prompt;见第 16 章** |
556
+ | Host 不对 | `api.individual`、`api.githubcopilot`、`proxy.individual` | 排除 |
557
+ | 缺版本头 | 补 `X-GitHub-Api-Version` | **这是真正的修复** |
558
+ | 非流式限制 | `stream: true` + `Accept: text/event-stream` | 排除 |
559
+ | WebSocket 专属 | Node 24 原生 WS 实测 | 排除 |
560
+ | `model: "auto"` | 带 session token 请求 | 排除(返回 `not available for session`) |
561
+ | 模型可选但 picker 隐藏 | `POST /models/{id}/policy` 启用后重测 | 排除(`available_models` 不变) |
562
+ | `selected_model` 是对象 | 20+ 种参数/头组合实测 | 排除(始终为字符串) |
563
+ | tier 参数影响选择 | `efficiency`/`balance`/`intelligence`/`fast` 逐个测 | 排除(结果相同) |
564
+ | 客户端版本影响授权 | 0.72.0 → 1.0.0 逐个测 | 排除(`available_models` 一致) |
565
+ | **UI 显示的是真实模型** | 问模型“你是谁” | **排除:模型自述错误,见第 10 章** |
566
+
567
+ ---
568
+
569
+ ## 13. 区域与实验分组
570
+
571
+ 成功响应(`gpt-5.4-nano`)的响应头中出现:
572
+
573
+ ```
574
+ X-Copilot-API-Exp-Assignment-Context: <experiment-group>:<assignment-id>;permission_prompt_control:<assignment-id>;...
575
+ ```
576
+
577
+ 说明服务端按**实验分组**下发能力。令牌中也带地理信息:
578
+
579
+ ```
580
+ ip=<redacted>;asn=<redacted>;sl=<redacted>
581
+ ```
582
+
583
+ 令牌中包含地理信息,但具体值不纳入记录。
584
+
585
+ 模型可用性可能同时受套餐、实验分组与地区影响。本文结论基于单一账号,
586
+ 其他账号的 `available_models` 应以其 `/models/session` 实际返回为准。
587
+
588
+ ---
589
+
590
+ ## 14. 复核流程
591
+
592
+ GitHub 改协议后,按以下顺序定位:
593
+
594
+ ```sh
595
+ # 1. 令牌是否有效
596
+ curl -s https://api.github.com/copilot_internal/v2/token \
597
+ -H "Authorization: Bearer <refresh token>"
598
+
599
+ # 2. 目录是否可达、条目数是否变化
600
+ curl -s https://api.individual.githubcopilot.com/models \
601
+ -H "Authorization: Bearer <access token>" \
602
+ -H "X-GitHub-Api-Version: 2025-07-16" \
603
+ -H "Editor-Version: vscode/1.144.1" \
604
+ -H "Copilot-Integration-Id: vscode-chat"
605
+
606
+ # 3. 旧通道路由是否仍可用、授权清单是否变化
607
+ curl -s -X POST https://api.individual.githubcopilot.com/models/session \
608
+ -H "Authorization: Bearer <access token>" \
609
+ -H "Content-Type: application/json" \
610
+ -H "X-GitHub-Api-Version: 2025-07-16" \
611
+ -H "Editor-Version: vscode/1.144.1" \
612
+ -H "Copilot-Integration-Id: vscode-chat" \
613
+ -d '{"auto_mode":{"enabled":true},"prompt":"hi"}'
614
+
615
+ # 3b. 新通道路由(注意版本头必须是 2026-08-01)
616
+ curl -s -X POST https://api.individual.githubcopilot.com/auto \
617
+ -H "Authorization: Bearer <access token>" \
618
+ -H "Content-Type: application/json" \
619
+ -H "X-GitHub-Api-Version: 2026-08-01" \
620
+ -H "Editor-Version: vscode/1.144.1" \
621
+ -H "Copilot-Integration-Id: vscode-chat" \
622
+ -d '{"prompt":"hi"}'
623
+
624
+ # 4. 用上一步的 session_token 实调(模型必须是该令牌覆盖的那个)
625
+ curl -s -X POST https://api.individual.githubcopilot.com/responses \
626
+ -H "Authorization: Bearer <access token>" \
627
+ -H "Content-Type: application/json" \
628
+ -H "Copilot-Session-Token: <session_token>" \
629
+ -H "X-GitHub-Api-Version: 2025-07-16" \
630
+ -H "X-Initiator: user" \
631
+ -d '{"model":"<selected_model>","input":[{"role":"user","content":"hi"}],"stream":false,"max_output_tokens":16}'
632
+ ```
633
+
634
+ 常见定位:
635
+
636
+ | 现象 | 先查 |
637
+ | --- | --- |
638
+ | 第 2 步 401 `IDE token expired` | 改用 `refresh` 令牌;或先走第 1 步刷新(第 9 章) |
639
+ | 第 3b 步 404 `invalid apiVersion` | 版本头必须是 `2026-08-01`(第 3 章) |
640
+ | 第 3 步失败而第 2 步正常 | `auto_mode` 是否仍为对象形式 |
641
+ | 第 4 步 `model_not_supported` 而第 3 步成功 | `X-GitHub-Api-Version` 是否被带上 |
642
+ | 第 4 步 `Requested model not available for session` | 模型不属于该令牌的覆盖范围(第 6 章) |
643
+ | 第 4 步 `invalid_request_body` | `max_output_tokens` 过小(实测 8 不行,16 可以) |
644
+
645
+ ### 排查时的三条原则
646
+
647
+ 1. **不要相信模型的自我描述。** 它说自己是哪个模型,与实际请求的 `model`
648
+ 字段无关(第 10 章)。需要证据时看请求体与 `/models/session` 响应。
649
+ 2. **不要用写接口做诊断。** `POST /models/{id}/policy` 能返回 200,
650
+ 但不会改变授权,且可能影响账号状态。
651
+ 3. **先区分「看得到」与「用得到」。** `/models` 列出 ≠ 可调用;
652
+ `policy: enabled` ≠ 有权限;UI 显示 ≠ 实际模型。唯一可信的是
653
+ `available_models` 加一次真实调用。
654
+
655
+ ---
656
+
657
+ ## 附:与 pi 内置 provider 的差异
658
+
659
+ | 方面 | 内置 `github-copilot` | `copilot-auto` |
660
+ | --- | --- | --- |
661
+ | 通道 | 普通 | Auto |
662
+ | 凭据 | 自有 OAuth,自动刷新 | **只读**复用内置凭据 |
663
+ | 模型来源 | 内置目录 + `availableModelIds` | `/models` ∩ `available_models` |
664
+ | 请求头 | pi-ai 自动注入 Copilot 头 | 自行补齐 |
665
+ | 适用模型 | `gpt-4.1`、`gpt-4o` 等 | `gpt-5.3-codex`、`mai-code-1.1-flash` |
666
+
667
+ 两者并存互补,不冲突。
668
+
669
+ ---
670
+
671
+ ## 15. 额度查询
672
+
673
+ ### 端点与令牌
674
+
675
+ 额度查询在 `https://api.github.com/copilot_internal/user`,使用
676
+ **GitHub OAuth 令牌**(`auth.json` 中 `github-copilot.refresh`),
677
+ 不是 Copilot 访问令牌(`access`)。
678
+
679
+ ```diff
680
+ GET https://api.github.com/copilot_internal/user
681
+ + Authorization: Bearer <refresh token>
682
+ → 200
683
+
684
+ GET https://api.github.com/copilot_internal/user
685
+ - Authorization: Bearer <access token>
686
+ → 401 Bad credentials
687
+ ```
688
+
689
+ 注意 Copilot 访问令牌的格式是 `tid=...;exp=...;sku=...`,不是 JWT,
690
+ 拿去请求 `api.github.com` 会被拒。两者不能混用。
691
+
692
+ ### 已排除的额度端点
693
+
694
+ | 路径 | 主机 | 结果 |
695
+ | --- | --- | --- |
696
+ | `/copilot_internal/v2/quota` | api.github.com | 404 |
697
+ | `/copilot_internal/v2/entitlements` | api.github.com | 404 |
698
+ | `/copilot_internal/subscription` | api.github.com | 404 |
699
+ | `/copilot_internal/premium/usage` | api.github.com | 404 |
700
+ | `/quota`、`/usage`、`/me` | api.individual.githubcopilot.com | 404 |
701
+
702
+ 额度数据只在 `/copilot_internal/user` 的 `quota_snapshots` 里。
703
+
704
+ ### 响应结构(`free_educational_quota` 字段示例,账号和额度值已脱敏)
705
+
706
+ ```json
707
+ {
708
+ "login": "sample-user",
709
+ "access_type_sku": "free_educational_quota",
710
+ "copilot_plan": "individual",
711
+ "assigned_date": "<date>",
712
+ "quota_reset_date": "<reset-date>",
713
+ "quota_snapshots": {
714
+ "chat": { "unlimited": true, "percent_remaining": 100.0 },
715
+ "completions": { "unlimited": true, "percent_remaining": 100.0 },
716
+ "premium_interactions": {
717
+ "unlimited": false,
718
+ "has_quota": true,
719
+ "credits_used": 8,
720
+ "remaining": 95,
721
+ "quota_remaining": 95.7,
722
+ "entitlement": 200,
723
+ "percent_remaining": 47.9,
724
+ "token_based_billing": true
725
+ }
726
+ },
727
+ "token_based_billing": true
728
+ }
729
+ ```
730
+
731
+ ### 字段语义
732
+
733
+ | 字段 | 含义 |
734
+ | --- | --- |
735
+ | `unlimited` | 该维度是否无上限;为 true 时其它数值字段常为 0 |
736
+ | `has_quota` | 是否具备该额度 |
737
+ | `credits_used` | 已消耗量(credits) |
738
+ | `remaining` | 剩余量(服务端已取整) |
739
+ | `entitlement` | 配额总量 |
740
+ | `percent_remaining` | 剩余百分比,0-100 |
741
+ | `token_based_billing` | 是否按 token 计费 |
742
+
743
+ **注意 `unlimited: true` 时 `remaining` / `entitlement` 都是 0**,
744
+ 不能直接相除算百分比,必须优先判断 `unlimited`。服务端在响应头里用
745
+ `ent=-1` 表达同一语义。
746
+
747
+ ### 只有 `free_limited_copilot` 看 `chat` 维度
748
+
749
+ VS Code 决定读哪个维度(源码 `extension.js`):
750
+
751
+ ```js
752
+ get isFreeUser() { return this.sku === "free_limited_copilot" }
753
+ // 读额度时:
754
+ isFreeUser ? header("x-quota-snapshot-chat")
755
+ : header("x-quota-snapshot-premium_models")
756
+ ?? header("x-quota-snapshot-premium_interactions")
757
+ ```
758
+
759
+ | SKU | 计费维度 |
760
+ | --- | --- |
761
+ | `free_limited_copilot` | `chat` |
762
+ | **其他全部**(含 `free_educational_quota`、`individual`、`pro`) | `premium_interactions` |
763
+
764
+ **因此对 `free_educational_quota` 来说,`chat` / `completions` 的
765
+ `unlimited: true` 不构成额外免费通道。所有对话(无论用哪个模型)
766
+ 都从 `premium_interactions` 扣减。**
767
+
768
+ 回应中 `x-quota-snapshot-*` 响应头可直接观察四个维度:
769
+
770
+ ```
771
+ x-quota-snapshot-chat = ent=-1&ov=0.0&ovPerm=false&rem=100.0&rst=<reset-date>
772
+ x-quota-snapshot-completions = ent=-1&ov=0.0&ovPerm=false&rem=100.0&rst=<reset-date>
773
+ x-quota-snapshot-premium_interactions= ent=200&ov=0.0&ovPerm=false&rem=<remaining-percent>&rst=<reset-date>
774
+ ```
775
+
776
+ ### 计费按 token 折算,不是按请求次数
777
+
778
+ 实测(`mai-code-1.1-flash`,全系列最便宜的一款):
779
+
780
+ | 输入 tokens | 输出 tokens | 扣减 premium |
781
+ | --- | --- | --- |
782
+ | ~30 | ~5 | **0**(未达最小计费单位) |
783
+ | 10,012 | 34 | 0.2 |
784
+ | 30,010 | 6,116 | 1.2 |
785
+
786
+ 标定结果:**约 3 万 token ≈ 1 次 premium**。这与其 `token_prices`
787
+ 一致(输入 20 USD/百万 token,输出 120 USD/百万 token)。
788
+
789
+ 推论:**没有任何 Auto 模型是免费的**。`mai-code-1.1-flash` 与
790
+ `gpt-5.3-codex` 的区别只是单价(后者输入价为 175 USD/百万,约为前者 9 倍)。
791
+
792
+ ### 测量注意事项
793
+
794
+ 1. **扣减延迟 40-50 秒**。立即查询会得到旧值,容易误判为「不扣费」。
795
+ 2. **`remaining` / `credits_used` 是取整值**。0.4 这类消耗会被抹成 0,
796
+ 必须读 `quota_remaining`(浮点)才能看到小额变化。
797
+
798
+ ### 与 Auto 折扣的关系
799
+
800
+ `/models/session` 的 `discounted_costs`(实测 `0.1`)与额度快照是两套
801
+ 独立机制:前者是单次请求的模型倍率,后者是账号级的周期配额。
802
802
 
803
803
  ---
804
804