billion-context 0.1.22 → 0.1.24

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,185 +42,103 @@ 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
- On first run `bili` **auto-creates an empty config file** and tells you where:
59
- so you don't have to invent the schema from scratch:
60
-
61
- ```
62
- [acp-config] created empty config at ~/.config/billion-context/billion-context.json — add your providers (see README Quickstart), then restart
63
- ```
64
-
65
- **Or configure via the web UI** (easier): open `http://localhost:8787/__acp/` in
66
- a browser while the proxy is running — edit providers in a form, and get
67
- copy-ready config snippets for Pi / OpenCode / Codex. The startup banner prints
68
- this URL too:
69
-
70
- ```
71
- acp-proxy listening on http://localhost:8787 — web UI: http://localhost:8787/__acp/
72
- ```
73
-
74
- This is the recommended path for first-time setup. (Prefer editing the JSON
75
- file directly — e.g. for git-managed or scripted deployments? See
76
- [Manual config file](#manual-config-file) below.)
77
-
78
- ### How routing works
79
-
80
- The proxy routes by a **provider name in the URL path** — the first path
81
- segment after the host. It strips the name and forwards the rest to that
82
- provider. Everything after the name is passed through untouched:
83
-
84
- ```
85
- client baseURL: http://localhost:8787/zhipu/api/coding/paas/v4
86
- └──────────┬──────────┘└────────┬────────┘
87
- proxy host remaining path
88
- + provider name (forwarded as-is)
89
- ```
90
-
91
- This is why Step 2 has you declare named providers, and Step 3 has you put that
92
- same name at the start of the client's base URL — it's how the proxy knows where
93
- to send each request. In the config below, `zhipu` corresponds to:
94
- ```json
95
- "zhipu": {
96
- "url": "https://open.bigmodel.cn",
97
- "models": {
98
- "glm-5.2": { "context": 1000000, "output": 131072 }
99
- }
100
- ```
101
-
102
- ### Step 2 — Configure your providers
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:
103
72
 
104
- Open the **web UI** at `http://localhost:8787/__acp/` and add your providers
105
- there (add provider → fill name + URL + per-model context → Save). This writes
106
- to `~/.config/billion-context/billion-context.json` directly. Then **restart
107
- `bili`** — the startup banner lists your routes:
108
-
109
- ```
110
- acp-proxy listening on http://localhost:8787 — routes: anthropic=https://api.anthropic.com, zhipu=https://open.bigmodel.cn
111
73
  ```
112
-
113
- That confirms the proxy picked up your config. (Prefer editing the JSON file by
114
- hand? See [Manual config file](#manual-config-file). Full schema — per-model
115
- context windows, optional fields — is in [Configuration](#configuration).)
116
-
117
- ### Step 3 — Point your client at the proxy
118
-
119
- Edit the client's own config file so it sends requests to
120
- `http://localhost:8787/<provider>/...` (the provider name from Step 2 as the
121
- first path segment). Put your **real** API key in the client's config too —
122
- the proxy passes it through untouched.
123
-
124
- #### Pi (billion-context-pi)
125
-
126
- Open `~/.pi/agent/models.json` and change your existing provider's **`baseUrl` line** to point at the proxy — leave every other field alone:
127
-
128
- ```jsonc
129
- // before:
130
- "baseUrl": "https://open.bigmodel.cn/api/coding/paas/v4",
131
- // after (swap the host for the proxy + the provider name you picked):
132
- "baseUrl": "http://localhost:8787/zhipu/api/coding/paas/v4",
74
+ client baseURL before: https://api.openai.com/v1
75
+ client baseURL after: http://localhost:8787/bili/https://api.openai.com/v1
133
76
  ```
134
77
 
135
- `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.
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.
136
82
 
137
- | `api` value | `baseUrl` should point at |
138
- |---|---|
139
- | `openai-completions` | an OpenAI-compatible endpoint (GLM/DeepSeek/OpenAI) → `…/zhipu/...` |
140
- | `anthropic-messages` | an Anthropic-compatible endpoint → `…/anthropic` |
141
-
142
- > If you use the `billion-context-pi` extension, run Pi in an isolated agent
143
- dir (`PI_CODING_AGENT_DIR=…`) so the client-side extension doesn't double-
144
- compress alongside the proxy. The `bili-test-pi` helper does this for you.
145
-
146
- #### OpenCode
147
-
148
- Open `~/.config/opencode/opencode.json` and change your existing provider's **`baseURL` line** to point at the proxy:
83
+ #### Examples by client
149
84
 
85
+ **OpenCode** — edit `~/.config/opencode/opencode.json`, change the provider's `baseURL`:
150
86
  ```jsonc
151
87
  // before:
152
88
  "baseURL": "https://open.bigmodel.cn/api/coding/paas/v4"
153
- // after:
154
- "baseURL": "http://localhost:8787/zhipu/api/coding/paas/v4"
89
+ // after (just prepend the proxy origin + /bili/):
90
+ "baseURL": "http://localhost:8787/bili/https://open.bigmodel.cn/api/coding/paas/v4"
155
91
  ```
156
92
 
157
- Everything else (`apiKey`, `models`) stays unchanged. For an Anthropic provider, change `baseURL` to `http://localhost:8787/anthropic`.
158
-
159
- #### Codex
160
-
161
- Open `~/.codex/config.toml` and change your existing provider's **`base_url` line** to point at the proxy:
162
-
93
+ **Codex** edit `~/.codex/config.toml`, change the provider's `base_url`:
163
94
  ```toml
164
95
  # before:
165
- base_url = "https://open.bigmodel.cn/api/coding/paas/v4"
96
+ base_url = "https://api.openai.com/v1"
166
97
  # after:
167
- base_url = "http://localhost:8787/zhipu/api/coding/paas/v4"
98
+ base_url = "http://localhost:8787/bili/https://api.openai.com/v1"
168
99
  ```
169
100
 
170
- Everything else (`name`, `wire_api`, `env_key`) stays unchanged.
171
-
172
- > Codex's Responses API needs an upstream that speaks the Responses protocol.
173
- > Most regional OpenAI-compatible endpoints only speak `/chat/completions`; if
174
- > yours 404s on `/responses`, use a relay that speaks Responses, or the
175
- > official OpenAI API.
176
-
177
- #### Other clients (Cursor / Aider / Continue …)
178
-
179
- Not yet supported. The proxy currently speaks the Anthropic, OpenAI
180
- chat-completions, and OpenAI Responses protocols — if your client uses a
181
- different protocol or a non-standard auth header, it won't work yet.
182
-
183
- ### Web UI
184
-
185
- Open `http://localhost:8787/__acp/` in a browser while the proxy is running. You can:
186
-
187
- - **Edit providers** in a form (add/remove providers and per-model context windows) and save — this writes to `billion-context.json` directly.
188
- - **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).
189
- - **View sessions** — live table of active sessions (requests, tokens saved, last seen), auto-refreshing.
190
-
191
- Changes to providers require a **restart** to take effect (the UI tells you this).
101
+ **Pi** edit `~/.pi/agent/models.json`, change the provider's `baseUrl`:
102
+ ```jsonc
103
+ // before:
104
+ "baseUrl": "https://api.anthropic.com"
105
+ // after:
106
+ "baseUrl": "http://localhost:8787/bili/https://api.anthropic.com"
107
+ ```
192
108
 
193
- ### Manual config file
109
+ **Other clients (Cursor / Aider / Continue …)** — wherever the upstream URL
110
+ is configured, prepend `http://localhost:8787/bili/` to it. Nothing else
111
+ changes.
194
112
 
195
- Prefer editing JSON by hand for git-managed configs, scripted deployments, or
196
- if you just don't want to use the browser? The web UI writes to the same file,
197
- so you can edit it directly with identical results.
113
+ ### Option BManual config file & context windows
198
114
 
199
115
  Open `~/.config/billion-context/billion-context.json` and edit the `providers`
200
- block. Each entry is a **name URL** mapping; the name is what you put in the
201
- client's base URL in Step 3.
116
+ block. **The key is the upstream URL** the string the client puts after
117
+ `/bili/`. The value declares per-model context windows for that URL:
202
118
 
203
119
  ```json
204
120
  {
205
121
  "providers": {
206
- "zhipu": {
207
- "url": "https://open.bigmodel.cn",
208
- "models": {
209
- "glm-5.2": { "context": 1000000, "output": 131072 }
210
- }
122
+ "https://open.bigmodel.cn/api/coding/paas/v4": {
123
+ "models": { "glm-5.2": { "context": 1000000 } }
211
124
  },
212
- "anthropic": "https://api.anthropic.com"
125
+ "https://api.anthropic.com": {}
213
126
  }
214
127
  }
215
128
  ```
216
129
 
217
- - Delete providers you don't use.
218
- - Add others (e.g. `"deepseek": "https://api.deepseek.com"`).
130
+ - A key matches when the client's embedded URL equals it or starts with it
131
+ (longest key wins). A bare host key covers every path on that host.
132
+ - An empty value `{}` means "this URL exists, no overrides" (context windows
133
+ come from models.dev / the prefix table).
134
+ - Delete entries you don't use; add others as needed.
219
135
  - The API key is **not** here — it lives in the client; the proxy passes it
220
136
  through untouched.
221
137
 
222
- After saving, **restart `bili`**. (Full schema per-model context windows,
223
- optional fields — is in [Configuration](#configuration).)
138
+ ### Option C Web UI & context windows
139
+
140
+ Open [http://localhost:8787/__bili/](http://localhost:8787/__bili/) to
141
+ configure.
224
142
 
225
143
  ### Verify
226
144
 
@@ -229,11 +147,11 @@ first real request shows compression activity in the log:
229
147
 
230
148
  ```bash
231
149
  # Health check (proxy up + where it forwards)
232
- curl -s http://localhost:8787/__acp/health
150
+ curl -s http://localhost:8787/__bili/health
233
151
  # → {"ok":true,"upstream":"https://api.anthropic.com"}
234
152
 
235
153
  # Live session stats (after a real request)
236
- curl -s http://localhost:8787/__acp/stats
154
+ curl -s http://localhost:8787/__bili/stats
237
155
  ```
238
156
 
239
157
  Then send one message from your client and watch the log
@@ -346,15 +264,13 @@ The config file is a single JSON object. Example:
346
264
  "port": 8787,
347
265
  "host": "127.0.0.1",
348
266
  "providers": {
349
- "zhipu": {
350
- "url": "https://open.bigmodel.cn",
267
+ "https://open.bigmodel.cn/api/coding/paas/v4": {
351
268
  "models": {
352
- "glm-5.2": { "context": 1000000, "output": 131072 },
353
- "glm-5.1": { "context": 200000, "output": 131072 }
269
+ "glm-5.2": { "context": 1000000 },
270
+ "glm-5.1": { "context": 200000 }
354
271
  }
355
272
  },
356
- "anthropic": "https://api.anthropic.com",
357
- "deepseek": "https://api.deepseek.com"
273
+ "https://api.deepseek.com": {}
358
274
  }
359
275
  }
360
276
  ```
@@ -365,12 +281,11 @@ The config file is a single JSON object. Example:
365
281
  |------|---------|-------------|
366
282
  | `port` | `8787` | Proxy listen port |
367
283
  | `host` | `127.0.0.1` | Proxy listen host |
368
- | `upstream` | `https://api.anthropic.com` | Default upstream when no route matches |
369
284
  | `sessionHeader` | `x-acp-session` | Header name clients may send to identify a conversation |
370
285
  | `log` | `true` | Enable request logging |
371
286
  | `debug` | `false` | Verbose logging (same as `ACP_DEBUG=1`) |
372
287
  | `passthrough` | `false` | Forward without compression (same as `ACP_PASSTHROUGH=1`) |
373
- | `providers` | *(none)* | Provider routes — see below |
288
+ | `providers` | *(none)* | Per-URL context overrides — see below |
374
289
  | `compress` | *(see defaults)* | `{ injectTool, injectNudge }` |
375
290
 
376
291
  > **Choosing a `host`** (IPv6 / containers): the default `127.0.0.1` is
@@ -382,53 +297,52 @@ The config file is a single JSON object. Example:
382
297
  > `--host 0.0.0.0` there. ⚠️ `0.0.0.0` / `::` expose the proxy on **all**
383
298
  > interfaces; ensure you're on a trusted network or behind a firewall.
384
299
 
385
- ### Providers (URL routing + per-model context)
386
-
387
- `providers` maps a route name to either a bare URL string (simple) or an
388
- object with `url` + optional per-model context window (recommended).
300
+ ### Providers (per-URL context overrides)
389
301
 
390
- **Simple form** provider name URL:
391
- ```json
392
- { "deepseek": "https://api.deepseek.com" }
393
- ```
302
+ Routing is always the `/bili/` prefix (see [Option A](#option-a--zero-config-bili-prefix)).
303
+ The `providers` block only declares **context-window overrides** keyed by
304
+ upstream URL. The key is the same string the client puts after `/bili/`:
394
305
 
395
- **Full form** — provider name → `{ url, models }`:
396
306
  ```json
397
307
  {
398
- "zhipu": {
399
- "url": "https://open.bigmodel.cn",
400
- "models": {
401
- "glm-5.2": { "context": 1000000, "output": 131072 },
402
- "glm-5.1": { "context": 200000 }
403
- }
308
+ "providers": {
309
+ "https://open.bigmodel.cn/api/coding/paas/v4": {
310
+ "models": {
311
+ "glm-5.2": { "context": 1000000 },
312
+ "glm-5.1": { "context": 200000 }
313
+ }
314
+ },
315
+ "https://api.deepseek.com": {}
404
316
  }
405
317
  }
406
318
  ```
407
319
 
408
- The same model can have a different context window behind different providers
409
- (e.g. relay wraps a model with a larger window). `context` is the **input
410
- context limit** (used by the compressor to decide when to nudge); `output` is
411
- the max output tokens. Both are optional; missing values fall back to the
412
- built-in model table, then to `modelContextLimit`.
320
+ The same model can have a different context window behind different upstreams
321
+ (e.g. a relay wraps a model with a larger window). `context` is the **input
322
+ context limit** (used by the compressor to decide when to nudge). It is
323
+ optional; missing values fall back to the [models.dev](https://models.dev)
324
+ registry, then the built-in prefix table.
413
325
 
414
326
  > **Why declare context at all?** The LLM `/models` API does **not** return
415
327
  > context windows (verified across OpenAI, Anthropic, 智谱, comfly). They are
416
328
  > document-level information. A wrong value (e.g. GLM-5.2 guessed as 128K
417
329
  > instead of 1M) causes spurious frequent compression. Declaring it per
418
- > provider + model makes the proxy match the registry the client itself uses.
330
+ > URL + model makes the proxy match the registry the client itself uses.
419
331
 
420
- **API keys are never stored in the proxy** — whatever key the agent sends is
421
- passed through untouched to the upstream.
332
+ ### URL key matching rules
422
333
 
423
- ### Provider name rules
334
+ - A request matches a key when the client's embedded URL **equals the key or
335
+ starts with it** (longest key wins).
336
+ - A shallow key like `https://open.bigmodel.cn` overrides every path on that
337
+ host; a deep key like `https://open.bigmodel.cn/api/anthropic` overrides
338
+ only that endpoint.
339
+ - Keys never cross hosts (the boundary check requires a `/` or end-of-string
340
+ after the key), so `https://x.com` does not match `https://x.com.evil`.
341
+ - Models not covered by any matching key fall back to models.dev, then the
342
+ prefix table, then `modelContextLimit`.
424
343
 
425
- - Must start with a letter, contain only letters/digits/`-`/`_`.
426
- - Reserved words (`v1`, `chat`, `completions`, `messages`, `models`, `api`)
427
- are rejected to avoid colliding with real API path segments.
428
- - The provider name can appear anywhere in the path; the longest match wins.
429
- - With **no providers** declared (e.g. you emptied the `providers` block),
430
- every request is forwarded to the default `upstream` with its full path —
431
- an edge case, not the normal flow.
344
+ **API keys are never stored in the proxy** — whatever key the agent sends is
345
+ passed through untouched to the upstream.
432
346
 
433
347
  ## How sessions work
434
348
 
@@ -466,7 +380,7 @@ pass an explicit `x-acp-session` header per conversation to avoid collisions.
466
380
 
467
381
  ## Status
468
382
 
469
- Early. Protocol handling and compression work against mock tests (141 passing). Real-model integration testing is the next milestone. Expect rough edges.
383
+ Early. Protocol handling and compression work against mock tests (146 passing). Real-model integration testing is the next milestone. Expect rough edges.
470
384
 
471
385
  See [billion-context-pi](https://github.com/ranxianglei/billion-context-pi) for the pi-extension mode (in-process, tighter integration, the reference implementation).
472
386