billion-context 0.1.28 → 0.1.30

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
@@ -114,7 +114,31 @@ base_url = "http://localhost:8787/bili/https://api.openai.com/v1"
114
114
  upstream URL is configured, prepend `http://localhost:8787/bili/` to it.
115
115
  Nothing else changes.
116
116
 
117
- #### B. Login/subscription clients (MITM transparent proxy)
117
+ #### B. Codex Route (API key or ChatGPT subscription)
118
+
119
+ Codex can be connected without changing its active `model_provider` and
120
+ without trusting a MITM certificate. Start bili, open
121
+ [http://localhost:8787/__bili/](http://localhost:8787/__bili/), then enable
122
+ **Codex Route** on the Routing page. For scripted startup, enable it before
123
+ starting the owning server:
124
+
125
+ ```bash
126
+ bili codex enable --port 8787
127
+ bili start --port 8787
128
+ ```
129
+
130
+ billion-context resolves only the active Codex provider, records its real
131
+ `base_url`, and temporarily replaces that one field with
132
+ `http://127.0.0.1:8787/codex`. The provider id never changes, so existing Codex
133
+ history stays in the same bucket. Disabling the route or gracefully stopping
134
+ the owning bili process restores the original field; edits made by the user
135
+ while routing was active are preserved. A stale takeover is safely recovered
136
+ the next time bili starts.
137
+
138
+ The Routing page also offers a manual, backup-first repair for sessions created
139
+ by older bili test provider ids. Normal routing never migrates Codex history.
140
+
141
+ #### C. Other login/subscription clients (MITM transparent proxy)
118
142
 
119
143
  Clients you sign **into an account** (ChatGPT Plus/Pro, Claude, ZCode coding
120
144
  plan, …) authenticate via **OAuth and hardcode the endpoint** — you can't
@@ -126,7 +150,7 @@ Supported login clients:
126
150
  | Client | Login | Endpoint hardcoded | Status |
127
151
  |---|---|---|---|
128
152
  | **ZCode** | bigmodel coding plan (OAuth) | `open.bigmodel.cn` (builtin provider) | ✅ tested |
129
- | **Codex** | ChatGPT account (OAuth) | `chatgpt.com/backend-api` | untested (may not work needs verification) |
153
+ | **Codex** | ChatGPT account (OAuth) | `chatgpt.com/backend-api` | use Codex Route above; MITM is fallback only |
130
154
  | **Claude Code** | Claude subscription (OAuth) | `api.anthropic.com` | ❓ untested (may not work — needs verification) |
131
155
 
132
156
  How MITM mode works: the client only offers an **HTTP proxy** setting, so it
@@ -153,8 +177,8 @@ non-model traffic.
153
177
  - **HTTP Proxy**: `http://127.0.0.1:8787`
154
178
  - **Proxy CA certificate path**: `~/.local/share/billion-context/ca/root-ca.pem`
155
179
  - (optional) **No-proxy list**: `localhost,127.0.0.1`
156
- - (For ZCode specifically: **Settings → Network**. For Codex/Claude Code:
157
- set the `HTTPS_PROXY` env var and `NODE_EXTRA_CA_CERTS` to the CA path.)
180
+ - (For ZCode specifically: **Settings → Network**. For Claude Code, set the
181
+ `HTTPS_PROXY` env var and `NODE_EXTRA_CA_CERTS` to the CA path.)
158
182
 
159
183
  3. Restart the client. Its model traffic now flows through billion-context
160
184
  with compression injected. Send a message and check the proxy log
@@ -435,17 +459,23 @@ If the proxy's own outbound connections to a model provider are blocked
435
459
  ```
436
460
 
437
461
  Rules:
438
- - **Global `proxy`** (top level) applies to every provider's outbound.
439
- - **Per-URL `proxy`** overrides the global for that host.
462
+ - **Per-URL `proxy`** has the highest priority for its matching provider URL.
463
+ - Remaining priority is `BILI_UPSTREAM_PROXY` Web UI manual proxy → top-level
464
+ `proxy` → `HTTPS_PROXY` / `HTTP_PROXY` / `ALL_PROXY` → Windows system proxy
465
+ → direct.
440
466
  - Empty string `""` means **explicitly direct** (override-and-disable).
441
- - Neither set = direct connect.
442
- - Only HTTP proxies (`http://host:port`). SOCKS5 is not supported yet.
467
+ - Auto mode honors `NO_PROXY` and the Windows proxy bypass list for
468
+ environment/system fallbacks. A proxy pointing back to bili's own local port
469
+ is ignored or rejected to prevent a loop.
470
+ - HTTP and HTTPS proxy origins are supported. SOCKS5 is not supported yet.
443
471
  - Both outbound paths are covered: `/bili/` path-mode (fetch) AND MITM CONNECT
444
472
  tunnels (the proxy's connection to the real upstream goes through the HTTP
445
473
  CONNECT proxy).
446
474
 
447
- Env override: `BILI_UPSTREAM_PROXY=http://127.0.0.1:20172` (same as global
448
- `proxy`; config file wins over env if both set).
475
+ Env override: `BILI_UPSTREAM_PROXY=http://127.0.0.1:20172` (higher priority than
476
+ the config file). On Windows, common Clash/Mihomo static system proxies are
477
+ discovered automatically; the Web UI shows the effective source and any PAC
478
+ URL detected in Internet Settings.
449
479
 
450
480
  **MITM vs `/bili/` — distinguishing the key scheme.** A login client
451
481
  (ZCode via MITM) and an API-key client can both hit the same host
package/README.zh-CN.md CHANGED
@@ -96,7 +96,27 @@ base_url = "http://localhost:8787/bili/https://api.openai.com/v1"
96
96
 
97
97
  **其他 API-key 客户端(Cursor / Aider / Continue ……)** —— 只要配置了上游 URL,前面加 `http://localhost:8787/bili/` 就行,其他都不用改。
98
98
 
99
- #### B. 登录/订阅客户端(MITM 透明代理)
99
+ #### B. Codex 路由(API key 或 ChatGPT 订阅)
100
+
101
+ Codex 不需要改 `model_provider`,也不需要信任 MITM 证书。启动 bili,打开
102
+ [http://localhost:8787/__bili/](http://localhost:8787/__bili/),在“路由”页启用
103
+ **Codex 路由**。脚本化启动时,先启用路由,再启动拥有它的服务:
104
+
105
+ ```bash
106
+ bili codex enable --port 8787
107
+ bili start --port 8787
108
+ ```
109
+
110
+ billion-context 只解析当前激活的 Codex provider,记录它的真实 `base_url`,
111
+ 并临时只把这个字段替换成 `http://127.0.0.1:8787/codex`。provider id 始终
112
+ 不变,所以现有 Codex 历史仍在同一个分桶。关闭路由或正常停止拥有该路由的
113
+ bili 进程时会恢复原字段;路由期间用户自己做的新修改会被保留。异常退出留下
114
+ 的接管状态会在下次启动 bili 时安全恢复。
115
+
116
+ “路由”页还提供旧版 bili 测试 provider 会话的手动修复:先预览、先备份、再
117
+ 修复。正常路由绝不会自动迁移 Codex 历史。
118
+
119
+ #### C. 其他登录/订阅客户端(MITM 透明代理)
100
120
 
101
121
  需要 **登录账号**的客户端(ChatGPT Plus/Pro、Claude、ZCode coding plan
102
122
  ……)通过 **OAuth 认证,且硬编码了端点**——你改不了 baseURL,所以 `/bili/`
@@ -107,7 +127,7 @@ base_url = "http://localhost:8787/bili/https://api.openai.com/v1"
107
127
  | 客户端 | 登录方式 | 硬编码端点 | 状态 |
108
128
  |---|---|---|---|
109
129
  | **ZCode** | 智谱 coding plan(OAuth) | `open.bigmodel.cn`(内置 provider) | ✅ 已测试 |
110
- | **Codex** | ChatGPT 账号(OAuth) | `chatgpt.com/backend-api` | 未测试(可能不支持,需验证) |
130
+ | **Codex** | ChatGPT 账号(OAuth) | `chatgpt.com/backend-api` | 优先使用上面的 Codex 路由;MITM 仅作 fallback |
111
131
  | **Claude Code** | Claude 订阅(OAuth) | `api.anthropic.com` | ❓ 未测试(可能不支持,需验证) |
112
132
 
113
133
  MITM 模式原理:这类客户端只提供 **HTTP 代理**设置,所以它发送
@@ -130,8 +150,8 @@ MITM 默认开启,且只对一份 **白名单**中的模型域名(`open.bigmodel
130
150
  - **HTTP 代理**:`http://127.0.0.1:8787`
131
151
  - **代理 CA 证书路径**:`~/.local/share/billion-context/ca/root-ca.pem`
132
152
  - (可选)**不走代理列表**:`localhost,127.0.0.1`
133
- - (ZCode 具体位置:**设置 → 网络**。Codex/Claude Code:设 `HTTPS_PROXY`
134
- 环境变量 + `NODE_EXTRA_CA_CERTS` 指向 CA 路径。)
153
+ - (ZCode 具体位置:**设置 → 网络**。Claude Code:设 `HTTPS_PROXY` 环境变量
154
+ + `NODE_EXTRA_CA_CERTS` 指向 CA 路径。)
135
155
 
136
156
  3. 重启客户端。它的模型流量现在会经过 billion-context 并注入压缩。发一条消息,看 proxy 日志(`~/.local/state/billion-context/bili.log`)应出现
137
157
  `mitm <域名>:443 tunnel established`。
@@ -368,14 +388,18 @@ key 就是客户端写在 `/bili/` 后面的那个字符串:
368
388
  ```
369
389
 
370
390
  规则:
371
- - **全局 `proxy`**(顶层)对所有提供商的出站生效。
372
- - **按 URL `proxy`** 覆盖该域名的全局设置。
391
+ - **按 URL 的 `proxy`** 对匹配的 provider URL 优先级最高。
392
+ - 其余优先级为:`BILI_UPSTREAM_PROXY` Web UI 手动代理 → 顶层 `proxy`
393
+ `HTTPS_PROXY` / `HTTP_PROXY` / `ALL_PROXY` → Windows 系统代理 → 直连。
373
394
  - 空字符串 `""` 表示**明确直连**(覆盖并禁用)。
374
- - 都不配 = 直连。
375
- - 只支持 HTTP 代理(`http://host:port`)。SOCKS5 暂不支持。
395
+ - 自动模式会让环境/系统 fallback 遵守 `NO_PROXY` 与 Windows 绕过列表。
396
+ 指回 bili 自己本地端口的代理会被跳过或拒绝,防止自环。
397
+ - 支持 HTTP 和 HTTPS 代理 origin。SOCKS5 暂不支持。
376
398
  - 两条出站路径都覆盖:`/bili/` 路径模式(fetch)和 MITM CONNECT 隧道(代理连接真实上游的链路走 HTTP CONNECT 代理)。
377
399
 
378
- 环境变量覆盖:`BILI_UPSTREAM_PROXY=http://127.0.0.1:20172`(等同全局 `proxy`;两者都设时配置文件优先)。
400
+ 环境变量覆盖:`BILI_UPSTREAM_PROXY=http://127.0.0.1:20172`(优先于配置文件)。
401
+ Windows 下会自动发现常见 Clash/Mihomo 静态系统代理;Web UI 会显示实际来源,
402
+ 以及 Internet Settings 中检测到的 PAC URL。
379
403
 
380
404
  **MITM 与 `/bili/` —— 用 scheme 区分。** 登录客户端(ZCode 走 MITM)和 API-key 客户端可能连同一个域名(`open.bigmodel.cn`)。为了让它们的配置能区分,MITM 流量在查找键里用 `mitm://` scheme,`/bili/` 流量用真实的 `https://`:
381
405