billion-context 0.1.23 → 0.1.25

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
@@ -42,179 +42,160 @@ This installs the `bili` command (`bili-proxy` is kept as an alias).
42
42
 
43
43
  ## Quickstart
44
44
 
45
- Three steps: **start the proxy edit the config file → point your client at it**.
45
+ Two ways to use it pick one:
46
+
47
+ - **Zero-config (simplest):** prefix your client's baseURL with the proxy
48
+ origin + `/bili/`. No config file needed — context windows are auto-detected
49
+ from the [models.dev](https://models.dev) registry. The `/bili/` prefix also
50
+ doubles as a self-detection signal: billion-context client extensions
51
+ (billion-context-pi / opencode-acp) can recognize it in their own baseUrl
52
+ and self-disable, so you never get double compression.
53
+ - **Explicit context-window overrides:** declare per-URL context windows in a
54
+ config file (or the web UI) for endpoints the registry doesn't know about,
55
+ or when you want to pin an exact value. Routing is the same `/bili/` prefix
56
+ either way — the config only changes which context window the proxy uses.
57
+
46
58
  Compression is injected automatically — you only configure routing, never
47
59
  compression itself.
48
60
 
49
- ### Step 1Start the proxy
61
+ ### Option AZero-config (`/bili/` prefix)
62
+
63
+ Start the proxy:
50
64
 
51
65
  ```bash
52
66
  bili
53
67
  ```
54
68
 
55
- It listens on `http://127.0.0.1:8787`. Keep this terminal open (or run in the
56
- background; see [Running the proxy](#running-the-proxy)).
57
-
58
- Click [http://localhost:8787/__acp/](http://localhost:8787/__acp/) to add your models:
59
- <img width="2908" height="1787" alt="image" src="https://github.com/user-attachments/assets/cacf4b64-e5c6-41f2-b270-fd2be02eab0c" />
60
-
61
- On first run `bili` **auto-creates an empty config file** and tells you where,
62
- so you don't have to invent the schema from scratch. The startup banner prints
63
- the web UI URL too:
64
-
65
- ```
66
- acp-proxy listening on http://localhost:8787 — web UI: http://localhost:8787/__acp/
67
- ```
68
-
69
- ### How routing works
70
-
71
- The proxy routes by a **provider name in the URL path** — the first path
72
- segment after the host. It strips the name and forwards the rest to that
73
- provider. Everything after the name is passed through untouched:
74
-
75
- ```
76
- client baseURL: http://localhost:8787/zhipu/api/coding/paas/v4
77
- └──────────┬──────────┘└────────┬────────┘
78
- proxy host remaining path
79
- + provider name (forwarded as-is)
80
- ```
81
-
82
- This is why Step 2 has you declare named providers, and Step 3 has you put that
83
- same name at the start of the client's base URL — it's how the proxy knows where
84
- to send each request. In the config below, `zhipu` corresponds to:
85
- ```json
86
- "zhipu": {
87
- "url": "https://open.bigmodel.cn",
88
- "models": {
89
- "glm-5.2": { "context": 1000000, "output": 131072 }
90
- }
91
- ```
92
-
93
- ### Step 2 — Configure your providers
94
-
95
- Copy the content below:
96
- <img width="2931" height="1519" alt="image" src="https://github.com/user-attachments/assets/c02278be-bc7a-4f14-8f58-0f2d83784d54" />
97
-
98
- > Prefer not to use the web UI? You can also edit the JSON file directly — see
99
- > [Manual config file](#manual-config-file) below.
100
-
101
- Then **restart `bili`** — the startup banner lists your routes:
69
+ Then just prefix your client's existing baseURL with `http://localhost:8787/bili/`.
70
+ The full upstream URL is embedded in the path, so the proxy knows where to
71
+ forward without any config:
102
72
 
103
73
  ```
104
- acp-proxy listening on http://localhost:8787 — routes: anthropic=https://api.anthropic.com, zhipu=https://open.bigmodel.cn
74
+ client baseURL before: https://api.openai.com/v1
75
+ client baseURL after: http://localhost:8787/bili/https://api.openai.com/v1
105
76
  ```
106
77
 
107
- That confirms the proxy picked up your config. (Prefer editing the JSON file by
108
- hand? See [Manual config file](#manual-config-file). Full schema — per-model
109
- context windows, optional fields is in [Configuration](#configuration).)
110
-
111
- ### Step 3 — Point your client at the proxy
112
-
113
- Edit the client's own config file so it sends requests to
114
- `http://localhost:8787/<provider>/...` (the provider name from Step 2 as the
115
- first path segment). Put your **real** API key in the client's config too —
116
- the proxy passes it through untouched.
78
+ That's it put your real API key in the client config as usual (the proxy
79
+ passes it through untouched). Context windows (gpt-5.1-codex=400K,
80
+ glm-5.2=1M, claude-opus-4=200K, …) are looked up from models.dev
81
+ automatically.
117
82
 
118
- #### Pi (billion-context-pi)
83
+ #### A. API-key clients (`/bili/` prefix)
119
84
 
120
- Open `~/.pi/agent/models.json` and change your existing provider's **`baseUrl` line** to point at the proxy leave every other field alone:
121
-
122
- ```jsonc
123
- // before:
124
- "baseUrl": "https://open.bigmodel.cn/api/coding/paas/v4",
125
- // after (swap the host for the proxy + the provider name you picked):
126
- "baseUrl": "http://localhost:8787/zhipu/api/coding/paas/v4",
127
- ```
128
-
129
- `http://localhost:8787` is the proxy, `zhipu` is the name from Step 2, and the remaining path `/api/coding/paas/v4` is forwarded as-is to Zhipu. `apiKey`, `api`, and `models` stay unchanged.
130
-
131
- | `api` value | `baseUrl` should point at |
132
- |---|---|
133
- | `openai-completions` | an OpenAI-compatible endpoint (GLM/DeepSeek/OpenAI) → `…/zhipu/...` |
134
- | `anthropic-messages` | an Anthropic-compatible endpoint → `…/anthropic` |
135
-
136
- > If you use the `billion-context-pi` extension, run Pi in an isolated agent
137
- dir (`PI_CODING_AGENT_DIR=…`) so the client-side extension doesn't double-
138
- compress alongside the proxy. The `bili-test-pi` helper does this for you.
139
-
140
- #### OpenCode
141
-
142
- Open `~/.config/opencode/opencode.json` and change your existing provider's **`baseURL` line** to point at the proxy:
85
+ Clients you configure with an **API key** (not a login) let you change the
86
+ upstream URL. Just prepend `http://localhost:8787/bili/` to it — that's the
87
+ only change.
143
88
 
89
+ **OpenCode** — edit `~/.config/opencode/opencode.json`, change the provider's `baseURL`:
144
90
  ```jsonc
145
91
  // before:
146
92
  "baseURL": "https://open.bigmodel.cn/api/coding/paas/v4"
147
- // after:
148
- "baseURL": "http://localhost:8787/zhipu/api/coding/paas/v4"
93
+ // after (just prepend the proxy origin + /bili/):
94
+ "baseURL": "http://localhost:8787/bili/https://open.bigmodel.cn/api/coding/paas/v4"
149
95
  ```
150
96
 
151
- Everything else (`apiKey`, `models`) stays unchanged. For an Anthropic provider, change `baseURL` to `http://localhost:8787/anthropic`.
152
-
153
- #### Codex
154
-
155
- Open `~/.codex/config.toml` and change your existing provider's **`base_url` line** to point at the proxy:
156
-
97
+ **Codex (API key)** edit `~/.codex/config.toml`, change the provider's `base_url`:
157
98
  ```toml
158
99
  # before:
159
- base_url = "https://open.bigmodel.cn/api/coding/paas/v4"
100
+ base_url = "https://api.openai.com/v1"
160
101
  # after:
161
- base_url = "http://localhost:8787/zhipu/api/coding/paas/v4"
102
+ base_url = "http://localhost:8787/bili/https://api.openai.com/v1"
162
103
  ```
163
104
 
164
- Everything else (`name`, `wire_api`, `env_key`) stays unchanged.
165
-
166
- > Codex's Responses API needs an upstream that speaks the Responses protocol.
167
- > Most regional OpenAI-compatible endpoints only speak `/chat/completions`; if
168
- > yours 404s on `/responses`, use a relay that speaks Responses, or the
169
- > official OpenAI API.
170
-
171
- #### Other clients (Cursor / Aider / Continue …)
172
-
173
- Not yet supported. The proxy currently speaks the Anthropic, OpenAI
174
- chat-completions, and OpenAI Responses protocols — if your client uses a
175
- different protocol or a non-standard auth header, it won't work yet.
176
-
177
- ### Web UI
105
+ **Pi** edit `~/.pi/agent/models.json`, change the provider's `baseUrl`:
106
+ ```jsonc
107
+ // before:
108
+ "baseUrl": "https://api.anthropic.com"
109
+ // after:
110
+ "baseUrl": "http://localhost:8787/bili/https://api.anthropic.com"
111
+ ```
178
112
 
179
- Open `http://localhost:8787/__acp/` in a browser while the proxy is running. You can:
113
+ **Other API-key clients (Cursor / Aider / Continue …)** wherever the
114
+ upstream URL is configured, prepend `http://localhost:8787/bili/` to it.
115
+ Nothing else changes.
180
116
 
181
- - **Edit providers** in a form (add/remove providers and per-model context windows) and save — this writes to `billion-context.json` directly.
182
- - **Generate client URLs** — pick a provider, get ready-to-copy config snippets for Pi / OpenCode / Codex (the `baseUrl`/`baseURL`/`base_url` line with the proxy origin + provider name filled in).
183
- - **View sessions** — live table of active sessions (requests, tokens saved, last seen), auto-refreshing.
117
+ #### B. Login/subscription clients (MITM transparent proxy)
184
118
 
185
- Changes to providers require a **restart** to take effect (the UI tells you this).
119
+ Clients you sign **into an account** (ChatGPT Plus/Pro, Claude, ZCode coding
120
+ plan, …) authenticate via **OAuth and hardcode the endpoint** — you can't
121
+ change the baseURL, so the `/bili/` prefix trick doesn't work. These need
122
+ **MITM transparent-proxy mode** instead.
186
123
 
187
- ### Manual config file
124
+ Supported login clients:
188
125
 
189
- Prefer editing JSON by hand for git-managed configs, scripted deployments, or
190
- if you just don't want to use the browser? The web UI writes to the same file,
191
- so you can edit it directly with identical results.
126
+ | Client | Login | Endpoint hardcoded | Status |
127
+ |---|---|---|---|
128
+ | **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) |
130
+ | **Claude Code** | Claude subscription (OAuth) | `api.anthropic.com` | ❓ untested (may not work — needs verification) |
131
+
132
+ How MITM mode works: the client only offers an **HTTP proxy** setting, so it
133
+ sends `CONNECT <host>:443`; billion-context terminates the TLS locally (with a
134
+ locally-generated root CA), injects compression into the cleartext, then
135
+ re-encrypts and forwards. The OAuth token travels in the client's
136
+ `Authorization` header, which is forwarded untouched — so the subscription
137
+ discount is preserved.
138
+
139
+ MITM is on by default and is scoped to a **whitelist** of model hosts
140
+ (`open.bigmodel.cn`, `api.anthropic.com`, `api.openai.com`, `chatgpt.com`).
141
+ All other HTTPS hosts are blind-tunnelled — billion-context never decrypts
142
+ non-model traffic.
143
+
144
+ **One-time setup (trust the root CA in the client):**
145
+
146
+ 1. Start the proxy once to generate the root CA:
147
+ ```bash
148
+ bili start
149
+ ls ~/.local/share/billion-context/ca/root-ca.pem # exists now
150
+ ```
151
+
152
+ 2. In the client's **Settings → Network / Proxy** set:
153
+ - **HTTP Proxy**: `http://127.0.0.1:8787`
154
+ - **Proxy CA certificate path**: `~/.local/share/billion-context/ca/root-ca.pem`
155
+ - (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.)
158
+
159
+ 3. Restart the client. Its model traffic now flows through billion-context
160
+ with compression injected. Send a message and check the proxy log
161
+ (`~/.local/state/billion-context/bili.log`) for
162
+ `mitm <host>:443 tunnel established`.
163
+
164
+ > The root CA is generated locally and lives only on this machine; it is
165
+ > **not** a system-wide install. Only the client you configure (via the
166
+ > CA-path setting, which it feeds to Node as `NODE_EXTRA_CA_CERTS`) trusts it,
167
+ > so no other app is affected. Deleting the CA files and restarting the proxy
168
+ > regenerates them.
169
+
170
+ ### Option B — Manual config file & context windows
192
171
 
193
172
  Open `~/.config/billion-context/billion-context.json` and edit the `providers`
194
- block. Each entry is a **name URL** mapping; the name is what you put in the
195
- client's base URL in Step 3.
173
+ block. **The key is the upstream URL** the string the client puts after
174
+ `/bili/`. The value declares per-model context windows for that URL:
196
175
 
197
176
  ```json
198
177
  {
199
178
  "providers": {
200
- "zhipu": {
201
- "url": "https://open.bigmodel.cn",
202
- "models": {
203
- "glm-5.2": { "context": 1000000, "output": 131072 }
204
- }
179
+ "https://open.bigmodel.cn/api/coding/paas/v4": {
180
+ "models": { "glm-5.2": { "context": 1000000 } }
205
181
  },
206
- "anthropic": "https://api.anthropic.com"
182
+ "https://api.anthropic.com": {}
207
183
  }
208
184
  }
209
185
  ```
210
186
 
211
- - Delete providers you don't use.
212
- - Add others (e.g. `"deepseek": "https://api.deepseek.com"`).
187
+ - A key matches when the client's embedded URL equals it or starts with it
188
+ (longest key wins). A bare host key covers every path on that host.
189
+ - An empty value `{}` means "this URL exists, no overrides" (context windows
190
+ come from models.dev / the prefix table).
191
+ - Delete entries you don't use; add others as needed.
213
192
  - The API key is **not** here — it lives in the client; the proxy passes it
214
193
  through untouched.
215
194
 
216
- After saving, **restart `bili`**. (Full schema per-model context windows,
217
- optional fields — is in [Configuration](#configuration).)
195
+ ### Option C Web UI & context windows
196
+
197
+ Open [http://localhost:8787/__bili/](http://localhost:8787/__bili/) to
198
+ configure.
218
199
 
219
200
  ### Verify
220
201
 
@@ -223,11 +204,11 @@ first real request shows compression activity in the log:
223
204
 
224
205
  ```bash
225
206
  # Health check (proxy up + where it forwards)
226
- curl -s http://localhost:8787/__acp/health
207
+ curl -s http://localhost:8787/__bili/health
227
208
  # → {"ok":true,"upstream":"https://api.anthropic.com"}
228
209
 
229
210
  # Live session stats (after a real request)
230
- curl -s http://localhost:8787/__acp/stats
211
+ curl -s http://localhost:8787/__bili/stats
231
212
  ```
232
213
 
233
214
  Then send one message from your client and watch the log
@@ -340,15 +321,13 @@ The config file is a single JSON object. Example:
340
321
  "port": 8787,
341
322
  "host": "127.0.0.1",
342
323
  "providers": {
343
- "zhipu": {
344
- "url": "https://open.bigmodel.cn",
324
+ "https://open.bigmodel.cn/api/coding/paas/v4": {
345
325
  "models": {
346
- "glm-5.2": { "context": 1000000, "output": 131072 },
347
- "glm-5.1": { "context": 200000, "output": 131072 }
326
+ "glm-5.2": { "context": 1000000 },
327
+ "glm-5.1": { "context": 200000 }
348
328
  }
349
329
  },
350
- "anthropic": "https://api.anthropic.com",
351
- "deepseek": "https://api.deepseek.com"
330
+ "https://api.deepseek.com": {}
352
331
  }
353
332
  }
354
333
  ```
@@ -359,12 +338,11 @@ The config file is a single JSON object. Example:
359
338
  |------|---------|-------------|
360
339
  | `port` | `8787` | Proxy listen port |
361
340
  | `host` | `127.0.0.1` | Proxy listen host |
362
- | `upstream` | `https://api.anthropic.com` | Default upstream when no route matches |
363
341
  | `sessionHeader` | `x-acp-session` | Header name clients may send to identify a conversation |
364
342
  | `log` | `true` | Enable request logging |
365
343
  | `debug` | `false` | Verbose logging (same as `ACP_DEBUG=1`) |
366
344
  | `passthrough` | `false` | Forward without compression (same as `ACP_PASSTHROUGH=1`) |
367
- | `providers` | *(none)* | Provider routes — see below |
345
+ | `providers` | *(none)* | Per-URL context overrides — see below |
368
346
  | `compress` | *(see defaults)* | `{ injectTool, injectNudge }` |
369
347
 
370
348
  > **Choosing a `host`** (IPv6 / containers): the default `127.0.0.1` is
@@ -376,53 +354,52 @@ The config file is a single JSON object. Example:
376
354
  > `--host 0.0.0.0` there. ⚠️ `0.0.0.0` / `::` expose the proxy on **all**
377
355
  > interfaces; ensure you're on a trusted network or behind a firewall.
378
356
 
379
- ### Providers (URL routing + per-model context)
357
+ ### Providers (per-URL context overrides)
380
358
 
381
- `providers` maps a route name to either a bare URL string (simple) or an
382
- object with `url` + optional per-model context window (recommended).
359
+ Routing is always the `/bili/` prefix (see [Option A](#option-a--zero-config-bili-prefix)).
360
+ The `providers` block only declares **context-window overrides** keyed by
361
+ upstream URL. The key is the same string the client puts after `/bili/`:
383
362
 
384
- **Simple form** — provider name → URL:
385
- ```json
386
- { "deepseek": "https://api.deepseek.com" }
387
- ```
388
-
389
- **Full form** — provider name → `{ url, models }`:
390
363
  ```json
391
364
  {
392
- "zhipu": {
393
- "url": "https://open.bigmodel.cn",
394
- "models": {
395
- "glm-5.2": { "context": 1000000, "output": 131072 },
396
- "glm-5.1": { "context": 200000 }
397
- }
365
+ "providers": {
366
+ "https://open.bigmodel.cn/api/coding/paas/v4": {
367
+ "models": {
368
+ "glm-5.2": { "context": 1000000 },
369
+ "glm-5.1": { "context": 200000 }
370
+ }
371
+ },
372
+ "https://api.deepseek.com": {}
398
373
  }
399
374
  }
400
375
  ```
401
376
 
402
- The same model can have a different context window behind different providers
403
- (e.g. relay wraps a model with a larger window). `context` is the **input
404
- context limit** (used by the compressor to decide when to nudge); `output` is
405
- the max output tokens. Both are optional; missing values fall back to the
406
- built-in model table, then to `modelContextLimit`.
377
+ The same model can have a different context window behind different upstreams
378
+ (e.g. a relay wraps a model with a larger window). `context` is the **input
379
+ context limit** (used by the compressor to decide when to nudge). It is
380
+ optional; missing values fall back to the [models.dev](https://models.dev)
381
+ registry, then the built-in prefix table.
407
382
 
408
383
  > **Why declare context at all?** The LLM `/models` API does **not** return
409
384
  > context windows (verified across OpenAI, Anthropic, 智谱, comfly). They are
410
385
  > document-level information. A wrong value (e.g. GLM-5.2 guessed as 128K
411
386
  > instead of 1M) causes spurious frequent compression. Declaring it per
412
- > provider + model makes the proxy match the registry the client itself uses.
387
+ > URL + model makes the proxy match the registry the client itself uses.
413
388
 
414
- **API keys are never stored in the proxy** — whatever key the agent sends is
415
- passed through untouched to the upstream.
389
+ ### URL key matching rules
416
390
 
417
- ### Provider name rules
391
+ - A request matches a key when the client's embedded URL **equals the key or
392
+ starts with it** (longest key wins).
393
+ - A shallow key like `https://open.bigmodel.cn` overrides every path on that
394
+ host; a deep key like `https://open.bigmodel.cn/api/anthropic` overrides
395
+ only that endpoint.
396
+ - Keys never cross hosts (the boundary check requires a `/` or end-of-string
397
+ after the key), so `https://x.com` does not match `https://x.com.evil`.
398
+ - Models not covered by any matching key fall back to models.dev, then the
399
+ prefix table, then `modelContextLimit`.
418
400
 
419
- - Must start with a letter, contain only letters/digits/`-`/`_`.
420
- - Reserved words (`v1`, `chat`, `completions`, `messages`, `models`, `api`)
421
- are rejected to avoid colliding with real API path segments.
422
- - The provider name can appear anywhere in the path; the longest match wins.
423
- - With **no providers** declared (e.g. you emptied the `providers` block),
424
- every request is forwarded to the default `upstream` with its full path —
425
- an edge case, not the normal flow.
401
+ **API keys are never stored in the proxy** — whatever key the agent sends is
402
+ passed through untouched to the upstream.
426
403
 
427
404
  ## How sessions work
428
405
 
@@ -460,7 +437,7 @@ pass an explicit `x-acp-session` header per conversation to avoid collisions.
460
437
 
461
438
  ## Status
462
439
 
463
- Early. Protocol handling and compression work against mock tests (141 passing). Real-model integration testing is the next milestone. Expect rough edges.
440
+ Early. Protocol handling and compression work against mock tests (146 passing). Real-model integration testing is the next milestone. Expect rough edges.
464
441
 
465
442
  See [billion-context-pi](https://github.com/ranxianglei/billion-context-pi) for the pi-extension mode (in-process, tighter integration, the reference implementation).
466
443