dsh-plugin-subscription-oauth 0.0.0-stage → 0.2.0

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.
Files changed (82) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +413 -2
  3. package/cordis.patch.yml +13 -0
  4. package/lib/auth/device-flow.d.ts +112 -0
  5. package/lib/auth/device-flow.js +319 -0
  6. package/lib/auth/device-id.d.ts +31 -0
  7. package/lib/auth/device-id.js +58 -0
  8. package/lib/auth/flow.d.ts +33 -0
  9. package/lib/auth/flow.js +12 -0
  10. package/lib/auth/glm-flow.d.ts +99 -0
  11. package/lib/auth/glm-flow.js +194 -0
  12. package/lib/auth/headers.d.ts +61 -0
  13. package/lib/auth/headers.js +106 -0
  14. package/lib/auth/pkce-flow.d.ts +78 -0
  15. package/lib/auth/pkce-flow.js +205 -0
  16. package/lib/auth/provider-controller.d.ts +241 -0
  17. package/lib/auth/provider-controller.js +502 -0
  18. package/lib/auth/registry.d.ts +91 -0
  19. package/lib/auth/registry.js +149 -0
  20. package/lib/auth/rpc.d.ts +31 -0
  21. package/lib/auth/rpc.js +200 -0
  22. package/lib/auth/store.d.ts +99 -0
  23. package/lib/auth/store.js +213 -0
  24. package/lib/auth/tokens.d.ts +76 -0
  25. package/lib/auth/tokens.js +103 -0
  26. package/lib/client/SubscriptionsSection.d.ts +34 -0
  27. package/lib/client/SubscriptionsSection.js +742 -0
  28. package/lib/client/index.d.ts +36 -0
  29. package/lib/client/index.js +47 -0
  30. package/lib/client/locales.d.ts +187 -0
  31. package/lib/client/locales.js +185 -0
  32. package/lib/client/styles.d.ts +18 -0
  33. package/lib/client/styles.js +257 -0
  34. package/lib/client/subscriptions-rpc.d.ts +24 -0
  35. package/lib/client/subscriptions-rpc.js +44 -0
  36. package/lib/client.js +2078 -0
  37. package/lib/client.js.map +1 -0
  38. package/lib/constants.d.ts +79 -0
  39. package/lib/constants.js +91 -0
  40. package/lib/errors.d.ts +28 -0
  41. package/lib/errors.js +34 -0
  42. package/lib/http.d.ts +24 -0
  43. package/lib/http.js +98 -0
  44. package/lib/index.d.ts +57 -0
  45. package/lib/index.js +185 -0
  46. package/lib/protocol.d.ts +316 -0
  47. package/lib/protocol.js +29 -0
  48. package/lib/providers/antigravity-catalog.d.ts +53 -0
  49. package/lib/providers/antigravity-catalog.js +269 -0
  50. package/lib/providers/antigravity.d.ts +331 -0
  51. package/lib/providers/antigravity.js +821 -0
  52. package/lib/providers/auto.d.ts +151 -0
  53. package/lib/providers/auto.js +351 -0
  54. package/lib/providers/catalog.d.ts +69 -0
  55. package/lib/providers/catalog.js +78 -0
  56. package/lib/providers/common.d.ts +51 -0
  57. package/lib/providers/common.js +169 -0
  58. package/lib/providers/glm-provider.d.ts +41 -0
  59. package/lib/providers/glm-provider.js +165 -0
  60. package/lib/providers/glm.d.ts +159 -0
  61. package/lib/providers/glm.js +445 -0
  62. package/lib/providers/grok.d.ts +224 -0
  63. package/lib/providers/grok.js +700 -0
  64. package/lib/providers/kimi-provider.d.ts +45 -0
  65. package/lib/providers/kimi-provider.js +148 -0
  66. package/lib/providers/kimi.d.ts +151 -0
  67. package/lib/providers/kimi.js +400 -0
  68. package/lib/providers/responses-stream.d.ts +58 -0
  69. package/lib/providers/responses-stream.js +115 -0
  70. package/lib/providers/usage.d.ts +65 -0
  71. package/lib/providers/usage.js +309 -0
  72. package/lib/settings.d.ts +122 -0
  73. package/lib/settings.js +333 -0
  74. package/lib/translate/antigravity-schema.d.ts +24 -0
  75. package/lib/translate/antigravity-schema.js +174 -0
  76. package/lib/translate/antigravity.d.ts +184 -0
  77. package/lib/translate/antigravity.js +419 -0
  78. package/lib/translate/responses.d.ts +156 -0
  79. package/lib/translate/responses.js +395 -0
  80. package/lib/translate/sse.d.ts +27 -0
  81. package/lib/translate/sse.js +82 -0
  82. package/package.json +126 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 EricFL1998
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,414 @@
1
- # Temporary Holding Version
1
+ # dsh-plugin-subscription-oauth
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![Listed on dsh-plugin.org](https://dsh-plugin.org/badges/listed.svg)](https://dsh-plugin.org/plugins/EricFL1998/dsh-subscription-oauth)
4
+ [![npm](https://img.shields.io/npm/v/dsh-plugin-subscription-oauth.svg)](https://www.npmjs.com/package/dsh-plugin-subscription-oauth)
5
+ [![Licence: MIT](https://img.shields.io/badge/licence-MIT-blue.svg)](LICENSE)
6
+
7
+ **Use the AI subscriptions you already pay for as LLM providers in DeepSeek
8
+ Harness — no API key.** Sign in once per provider from **Settings →
9
+ Subscriptions** (订阅) and that account's models join the session model picker,
10
+ with its plan quota shown next to the connection.
11
+
12
+ ```sh
13
+ dsh plugin --profile web add dsh-plugin-subscription-oauth
14
+ ```
15
+
16
+ The same package installs straight from the repository, which needs no registry:
17
+
18
+ ```sh
19
+ dsh plugin --profile web add github:EricFL1998/dsh-subscription-oauth
20
+ ```
21
+
22
+ | | |
23
+ | --- | --- |
24
+ | **Capability** | 模型与推理 / Model & inference — it registers LLM provider routes |
25
+ | **Licence** | [MIT](LICENSE) |
26
+ | **Built for** | DeepSeek Harness `0.2.0-rc.2` (`@deepseek-ai/dsh-llm`), Node ≥ 20 |
27
+ | **Platforms** | Windows, macOS and Linux; nothing platform-specific beyond Node itself |
28
+ | **Accounts** | your own subscriptions only — credentials stay on your machine |
29
+ | **Network** | the providers' own hosts and the OAuth endpoints listed below; no telemetry, no third-party relay |
30
+
31
+ | Route | Provider | Login | Wire |
32
+ | --- | --- | --- | --- |
33
+ | `kimi` | Kimi Code | OAuth **device flow** (RFC 8628) | OpenAI **Responses** — `https://api.kimi.com/coding/v1/responses` |
34
+ | `grok` | Grok (X Premium) | OIDC **authorization code + PKCE** on a loopback callback | OpenAI **Responses** — `https://api.x.ai/v1/responses` |
35
+ | `antigravity` | Google Antigravity | Google **authorization code + PKCE** (`/oauth-callback`) plus Cloud AI Companion project discovery | Cloud Code Assist `v1internal` — `streamGenerateContent?alt=sse` |
36
+ | `glm` | GLM (BigModel) | ZCode's **browser sign-in with a polled flow** (China region) | OpenAI **Responses** — `https://open.bigmodel.cn/api/coding/paas/v4/responses` |
37
+ | `auto` | Auto | none — it holds no account of its own | delegates to whichever route owns the model it picks |
38
+
39
+ The Kimi device flow is the one `opencodex` uses; the Grok flow is the one its
40
+ CLI uses (`auth.x.ai` discovery, client `b1a00492-…`, port `56121`, scope
41
+ `grok-cli:access`); the Antigravity flow is Google's desktop OAuth client
42
+ (`accounts.google.com`, port `51121`, scopes including `cclog` and
43
+ `experimentsandconfigs`), with the account's project discovered through
44
+ `loadCodeAssist`.
45
+
46
+ **GLM signs in the way ZCode does**, not with an API key: the plugin asks
47
+ `zcode.z.ai` for a login flow, opens `bigmodel.cn/login` in the browser, and polls
48
+ the flow until the subscription's tokens come back. Nothing has to receive a
49
+ `zcode://oauth/callback` redirect and no app secret is involved, because the
50
+ callback is completed server-side and the flow is authenticated by a secret the
51
+ plugin generates itself. Allowances are read from BigModel's own
52
+ `/api/monitor/usage/quota/limit`, which meters a rolling five-hour window and a
53
+ weekly one.
54
+
55
+ ## What it registers
56
+
57
+ | Surface | What appears |
58
+ | --- | --- |
59
+ | `ctx.llm` | the `kimi`, `grok`, `antigravity`, `glm` and `auto` provider routes (models, reasoning efforts, streaming) |
60
+ | Settings | a **Subscriptions** section: one card per provider — sign in, sign out, default account, token refresh, plan quota, model catalog, manage dialog |
61
+ | Model picker | each provider's own models, plus **`auto` as its own route** spanning them — Kimi ships `kimi-for-coding`, `k3`, `k3[1m]`, `k3-256k`; Grok seeds `grok-4.6`, `grok-4.5` and adds whatever its live catalog reports; Antigravity lists the agent-callable subset of what its endpoint reports (`fetchAvailableModels`), filtering out tabs, commit-message and transcription surfaces; GLM ships the `glm-5.3` family (`glm-5.3`, `glm-5.3-flash`, `glm-5.2`, `glm-5.1`, `glm-5`) and adds whatever its coding host lists |
62
+
63
+ Credentials live at `~/.dsh/plugins/kimi-oauth/auth.json` (mode `0600`,
64
+ written atomically, one section per provider). The directory keeps the name this
65
+ plugin had when it served Kimi Code alone: it holds **live logins**, so it is left
66
+ in place rather than renamed, and every profile that mounted the earlier version
67
+ finds its accounts exactly where it left them. This machine's Kimi device id
68
+ lives beside it in `kimi-device-id`; it is stable across restarts, because the
69
+ service correlates usage to it.
70
+
71
+ ### Token lifetimes, and why the card may say "expired"
72
+
73
+ Kimi Code access tokens live **15 minutes**; the refresh token lives **30 days**.
74
+ Grok access tokens are similar. Every request refreshes automatically when the
75
+ access token is within five minutes of lapsing, and the Settings card refreshes
76
+ eagerly while you are looking at it — so what it shows is the live expiry, and
77
+ "token expired" appears only when a refresh genuinely failed and the account
78
+ needs a new sign-in. Access-token expiry is normal and does not mean the login
79
+ is gone.
80
+
81
+ ## Settings page
82
+
83
+ | Every route folded | A subscription card open | A card with its catalog |
84
+ | --- | --- | --- |
85
+ | ![Folded provider list](docs/images/providers-folded.png) | ![GLM card with plan quota](docs/images/glm-card-open.png) | ![Kimi card with its catalog](docs/images/kimi-card-open.png) |
86
+
87
+ The page leads with **`auto`**, the way to reach every other route at once; the
88
+ routes it stands in for follow in the order they were configured. Each route is one
89
+ **folded row** — status dot, name, route key, a one-line summary of the connection,
90
+ and an **Edit** button — so the section reads as a list of providers and opens one
91
+ on demand. **Every visit starts folded** — an open card is a gesture within one
92
+ visit, not a stored setting — so re-opening Settings always shows the same compact
93
+ list. Inside an open card are the
94
+ account rows (★ sets the default account, `退出登录`/`Sign out` removes one), the
95
+ model catalog, and the plan quota. Controls follow the host's own settings-form
96
+ vocabulary — a 6 px radius (`--dsw-radius-sm`), a transparent fill, a hairline
97
+ border, and the theme's hover/active/focus tokens — declared in one scoped
98
+ stylesheet, so the page keeps that look instead of falling back to the browser's
99
+ default button chrome.
100
+
101
+ The **Models** heading is its own disclosure, **folded on every visit** too: click it to open the
102
+ catalog. **Each model has its own switch** (a toggle,
103
+ not a checkbox), which decides whether the session model picker offers it. The
104
+ choice is stored on the host (`~/.dsh/plugins/kimi-oauth/preferences.json`) as a
105
+ hidden list, so it survives restarts and every session sees it; a model added by a
106
+ later catalog refresh appears automatically rather than arriving hidden. A hidden
107
+ model is listed dimmed and stays **resolvable** — an existing session that already
108
+ selected it keeps working — so hiding is a picker-visibility choice, not a
109
+ revocation. `· 已隐藏 N 个` in the header counts the hidden ones.
110
+
111
+ **Add model** publishes an id this build has never heard of, so a model released
112
+ after the plugin was built is usable at once. The id is checked for shape, not
113
+ against a list — the endpoint is the only authority on what it serves — and it is
114
+ stored per route, published with conservative metadata, and listed with a `×` so a
115
+ typo can be removed again. **Drag a row** by its ⠿ grip (or press ↑ / ↓ while the
116
+ grip is focused) to re-order the list; that order is what the picker follows for
117
+ that route.
118
+
119
+ **`auto` is a provider of its own** (route `auto`), not a model inside each one: it
120
+ stands in for the enabled models of *every* provider the harness serves in one list,
121
+ each row badged with the provider that owns it. That includes the routes this plugin
122
+ registers **and** the ones it does not — the built-in DeepSeek providers and any
123
+ provider you added yourself — because it enumerates the harness roster
124
+ (`ctx.llm.listProviders()` / `listModels()`) and dispatches through it rather than
125
+ looking only at its own routes. It leads both the Settings section and the model
126
+ picker, because it is the one entry that reaches every route. It has no account, so
127
+ its card offers no connect or manage — only the list. A switch takes a model out of
128
+ Auto **without** hiding it from the picker, drag re-orders what Auto tries first, and
129
+ the arrangement is stored separately (`auto.order` / `auto.off`). Switch every model
130
+ off and Auto is simply not offered in the picker.
131
+
132
+ A provider with no signed-in account contributes no rows: `listModels` answers with
133
+ nothing until a route has a session, so signing out removes that route's models from
134
+ the picker and from Auto at the same time, and its card hides the catalog instead of
135
+ offering models that cannot be called.
136
+
137
+ Auto falls through to the next model when the provider refuses the preferred one
138
+ (renamed or gone, rate-limited, out of allowance, or a transport failure). A
139
+ concrete model id never falls back — it dispatches to exactly that model. Once a
140
+ stream has produced any output the turn belongs to that model, so a later failure
141
+ is reported instead of being papered over by a second model's text. Every fallback
142
+ is logged (`model X unavailable … falling back to Y`).
143
+
144
+ **Plan and usage** shows the subscription tier and every allowance the provider
145
+ states, with a meter and a reset time: Kimi's rolling 5-hour window, weekly window
146
+ and included credit (`GET /coding/v1/usages`); Grok's weekly included credit
147
+ (`cli-chat-proxy.grok.com/v1/billing?format=credits`, plus the plan tier from the
148
+ token's own `tier` claim); and Antigravity's **four** buckets — Gemini and Claude,
149
+ each rolling and weekly — read from `v1internal:retrieveUserQuotaSummary`, falling
150
+ back to the per-model `quotaInfo` of `fetchAvailableModels` (worst-spent model per
151
+ family, classified by id, display name and tier, so an `Opus` row counts as
152
+ Claude). A provider that states nothing says so instead of inventing a figure.
153
+
154
+ **Manage** opens a dialog with per-account token refresh, the credential-file
155
+ path, and the resolved endpoints.
156
+
157
+ While a device authorization is pending the card shows the code, its expiry, and
158
+ the buttons to open or copy it, and the status dot turns amber. A loopback
159
+ provider (Grok) instead shows a sign-in link and waits for the browser redirect.
160
+
161
+ ## Install
162
+
163
+ ```sh
164
+ dsh plugin --profile web add dsh-plugin-subscription-oauth
165
+ ```
166
+
167
+ `web` is the profile that serves the Settings page; use `desktop` (or any other
168
+ profile name) if that is the one you run.
169
+
170
+ The CLI forwards its arguments to pnpm, so a pinned version, a git URL and a local
171
+ path are all spelled the same way:
172
+
173
+ ```sh
174
+ dsh plugin --profile web add dsh-plugin-subscription-oauth@0.2.0
175
+ dsh plugin --profile web add github:EricFL1998/dsh-subscription-oauth
176
+ dsh plugin --profile web add /path/to/dsh-plugin-subscription-oauth
177
+ ```
178
+
179
+ Restart `dsh web` (or the desktop app) afterwards so the new bundle loads. A local
180
+ directory is installed as a `link:`, so afterwards `npm run build` in the checkout
181
+ is enough to publish a change; restart again to pick it up.
182
+
183
+ The host packages (`@deepseek-ai/dsh-llm` and the rest) are **optional** peers: the
184
+ harness provides them at runtime, so installing this plugin does not fetch a second
185
+ copy of the host it is running inside.
186
+
187
+ ### Releasing (maintainers)
188
+
189
+ `prepublishOnly` rebuilds `lib/` — the TypeScript output plus the browser bundle — so
190
+ a published tarball can never carry a stale client:
191
+
192
+ ```sh
193
+ npm version patch # or minor/major
194
+ npm publish
195
+ ```
196
+
197
+ For a headless composition that only needs the model route (no Settings page):
198
+
199
+ ```sh
200
+ dsh --profile headless --patch /path/to/dsh-plugin-subscription-oauth/overlay.yml "your task"
201
+ ```
202
+
203
+ where `overlay.yml` is a one-entry patch naming
204
+ `/path/to/dsh-plugin-subscription-oauth/lib/index.js`. Log in once from a web profile
205
+ first — the token file is shared.
206
+
207
+ ## Hiding a built-in provider from the picker
208
+
209
+ A plugin cannot unregister another bundle's route (`ctx.llm` has no unregister
210
+ API), so hiding a built-in provider is a profile-patch change. To hide the
211
+ **DeepSeek API-key** route and its models while keeping the **DeepSeek account**
212
+ (sign-in) route:
213
+
214
+ ```yaml
215
+ # $DSH_HOME/profiles/<profile>/cordis.patch.yml
216
+ - id: llm-deepseek
217
+ name: "@deepseek-ai/dsh-llm-deepseek-api-key"
218
+ config:
219
+ models: []
220
+ ```
221
+
222
+ `models` replaces the adapter's default catalog, so `deepseek-official` reports
223
+ no models and the picker hides the empty group (`Empty groups are hidden`); the
224
+ route stays registered, so a session that already saved one of its model ids
225
+ keeps working. Leave `llm-deepseek-account` alone to keep the account route.
226
+ Restart the profile (or let HMR reload) for the change to apply.
227
+
228
+ ## Use
229
+
230
+ 1. Open **Settings → Subscriptions** (订阅) and press **Connect** on the card you want.
231
+ 2. Finish the sign-in the card shows: Kimi Code and (when answered) a device code
232
+ you approve in the browser; Grok and Antigravity open a sign-in page and wait for
233
+ the redirect. The section updates by itself while an attempt is pending.
234
+ 3. Open the model picker in any session and choose a model under that provider —
235
+ or leave it on **Auto** to let the enabled models be tried in order.
236
+
237
+ Not signed in? The provider stays out of the picker, and a request fails with
238
+ `INVALID_CREDENTIAL` pointing at the Settings page. Nothing else breaks.
239
+
240
+ Multiple accounts are supported: connect again to add another. The ★ default
241
+ account serves requests; use **Use by default** to switch.
242
+
243
+ ## Config
244
+
245
+ ```yaml
246
+ - id: llm-subscription-oauth
247
+ name: dsh-plugin-subscription-oauth
248
+ config:
249
+ providers: [kimi, grok, antigravity] # routes to register; all are default
250
+ enabled: true
251
+ discovery: true # consult each endpoint's live model list
252
+ streamIdleTimeoutMs: 300000 # abandon a silent stream after this
253
+ defaultModel: kimi-for-coding # model the picker preselects on the Kimi route
254
+ extraModels: [] # Kimi ids to publish without shipped metadata
255
+ # oauthHost: https://auth.kimi.com
256
+ # apiBase: https://api.kimi.com/coding/v1
257
+ ```
258
+
259
+ Only the Kimi route takes `oauthHost`/`apiBase`/`defaultModel`/`extraModels`: the
260
+ Grok and Antigravity endpoints are fixed by their own clients, and their model
261
+ lists come from the live catalog. Omitting a route from `providers` simply leaves
262
+ it unregistered.
263
+
264
+ `oauthHost` and `apiBase` can also come from the environment
265
+ (`KIMI_CODE_OAUTH_HOST` / `KIMI_OAUTH_HOST`, `KIMI_CODE_API_BASE` /
266
+ `KIMI_API_BASE`); an explicit config value wins. `apiBase` accepts either the
267
+ bare base or the full `.../responses` URL.
268
+
269
+ ## How it works
270
+
271
+ The package is a standard DeepSeek Harness plugin: `lib/index.js` exports
272
+ `apply(ctx)`, which registers one `ctx.llm` adapter per route and mounts the
273
+ Settings section through `ctx.slots`. `lib/client.js` is the browser half — the
274
+ section itself, loaded by the harness bundle loader. Nothing runs outside the
275
+ harness process, and uninstalling the bundle removes every route it added.
276
+
277
+ **Login** is the RFC 8628 device authorization grant against
278
+ `auth.kimi.com`. The plugin requests a device code, shows it, and polls the
279
+ token endpoint with `authorization_pending` / `slow_down` / `access_denied` /
280
+ `expired_token` handling until the browser approves. Tokens are stored per
281
+ account, keyed by the `user_id` (or `sub`) claim inside Kimi's JWTs.
282
+
283
+ **Grok** signs in with the flow its own CLI uses: OIDC discovery at
284
+ `auth.x.ai`, an authorization-code exchange with PKCE on a loopback callback
285
+ (`127.0.0.1:56121/callback`), and a refresh grant that keeps the client identity.
286
+ **Antigravity** is Google's desktop OAuth client on `127.0.0.1:51121`, whose
287
+ scopes include `cloud-platform`; login is only complete once `loadCodeAssist`
288
+ reports the account's Cloud AI Companion project, because every later
289
+ `v1internal` call is scoped to it.
290
+
291
+ **Requests** carry three identity layers:
292
+
293
+ * the bearer access token, refreshed ahead of expiry (5-minute preempt) with one
294
+ in-flight refresh shared per account;
295
+ * Kimi's `X-Msh-*` client identity (`KimiCLI/<version>`, platform, device id) —
296
+ the service admits the request by this family;
297
+ * the harness attribution product. Because Kimi admits a request by the CLI
298
+ product token while the harness requires its own attribution on every provider
299
+ request, the two are joined into one RFC 9110 `User-Agent` value rather than
300
+ sent as two competing fields.
301
+
302
+ **The wire** is OpenAI Responses: `instructions` + `input` items,
303
+ `function_call` / `function_call_output` kept adjacent (the endpoint requires
304
+ it), `stream: true`, `store: false`, `prompt_cache_key` from the session id for
305
+ cache affinity, and `reasoning.effort` mapped from the harness's effort id.
306
+ Stored reasoning is never replayed as text — the endpoint keeps a subscription
307
+ account's thinking encrypted server-side, so each turn stands on its own visible
308
+ content.
309
+
310
+ Kimi locks temperature and sampling penalties on subscription coding models, so a
311
+ caller-supplied `temperature` is dropped rather than rejected upstream.
312
+
313
+ ## Troubleshooting
314
+
315
+ | Symptom | Cause |
316
+ | --- | --- |
317
+ | `<provider> login expired; sign in again…` | the refresh token was rejected; connect again |
318
+ | No models in the picker | not signed in, or the route was not re-announced — check **Settings → Subscriptions** |
319
+ | No models at all for Antigravity | the account's `fetchAvailableModels` returned nothing; the route ships no curated list, so press **Refresh** after signing in |
320
+ | `model not found` | the id is not served by your plan; use **Refresh** |
321
+ | The code card never clears | the attempt is still pending or expired; press **Cancel** and connect again |
322
+ | `model discovery failed … using the built-in catalog` | the live list was unreachable; the shipped catalog still works (Kimi and Grok ship one, Antigravity does not) |
323
+ | Rows cannot be dragged | the layout is switched off in Settings, or the drag started on the checkbox — grab the ⠿ grip |
324
+
325
+ ## Security notes
326
+
327
+ ### What it stores, and where
328
+
329
+ | Path | Contents | Mode |
330
+ | --- | --- | --- |
331
+ | `~/.dsh/plugins/kimi-oauth/auth.json` | one section per provider: access token, refresh token, expiry, account id, plan | `0600`, written atomically |
332
+ | `~/.dsh/plugins/kimi-oauth/preferences.json` | model visibility, your ordering, and Auto's chain — no secrets | `0600` |
333
+ | `~/.dsh/plugins/kimi-oauth/kimi-device-id` | the stable device id Kimi's API expects | `0600` |
334
+
335
+ The directory keeps the name this plugin had when it served Kimi Code alone: it
336
+ holds **live logins**, so renaming it would sign everyone out. Nothing is stored
337
+ outside `~/.dsh`, and nothing is uploaded anywhere except to the providers
338
+ themselves.
339
+
340
+ ### What it talks to
341
+
342
+ | Route | Hosts contacted | Why |
343
+ | --- | --- | --- |
344
+ | `kimi` | `auth.kimi.com`, `api.kimi.com` | device-flow sign-in, then the coding API |
345
+ | `grok` | `auth.x.ai`, `api.x.ai`, `cli-chat-proxy.grok.com` | OIDC sign-in, the Responses API, the plan's credit figure |
346
+ | `antigravity` | `accounts.google.com`, `oauth2.googleapis.com`, `cloudcode-pa.googleapis.com` | Google sign-in, the account's project, then Cloud Code Assist (including its quota accounting) |
347
+ | `glm` | `bigmodel.cn`, `zcode.z.ai`, `open.bigmodel.cn` | browser sign-in brokered by ZCode, then the coding-plan API and its allowance |
348
+
349
+ There is no analytics, no update ping and no relay through anyone else's server:
350
+ every request goes to the host that owns the account. No request body, prompt or
351
+ token is ever logged.
352
+
353
+ ### Boundaries worth knowing
354
+
355
+ * The token file is written with mode `0600`; only the account id, email, host,
356
+ and device id are ever shown in the UI — never a token.
357
+ * The Kimi and GLM flows need no listener on this machine at all: Kimi polls, and
358
+ GLM's browser callback is completed server-side by the provider. Grok and
359
+ Antigravity hold one loopback listener **only** while a login attempt is pending,
360
+ on `127.0.0.1:56121` and `127.0.0.1:51121`, and it is closed as soon as the
361
+ attempt settles or lapses. A loopback destination is never proxied.
362
+ * Outbound requests honour `HTTPS_PROXY` / `HTTP_PROXY` / `ALL_PROXY` and
363
+ `NO_PROXY`.
364
+ * Signing in authorizes this machine for that account — the provider's own consent
365
+ screen is the consent step, and it is never completed on your behalf.
366
+ * A plugin cannot unregister another bundle's provider (`ctx.llm` has no unregister
367
+ API), so this plugin never hides anything you had before; see
368
+ [Hiding a built-in provider from the picker](#hiding-a-built-in-provider-from-the-picker)
369
+ if that is what you want.
370
+ * **Known limits**: OpenAI-compatible API keys are not supported by design (this is
371
+ a subscription plugin); GLM's flow has no refresh grant, so a lapsed sign-in asks
372
+ you to sign in again rather than renewing silently; Antigravity ships no curated
373
+ model list and depends on its endpoint answering.
374
+
375
+ ## Develop
376
+
377
+ ```sh
378
+ npm install # development baseline: published DSH 0.2.0-rc.2 packages
379
+ npm run build # tsc (lib/) + tsdown (lib/client.js browser bundle)
380
+ npm test # node --test over the compiled unit specs
381
+ npm run typecheck # tsc --noEmit
382
+ ```
383
+
384
+ `tools/probe-kimi-oauth.mjs` performs a live check that Kimi accepts the shipped
385
+ client id and identity headers; it starts a device authorization and immediately
386
+ abandons it, so no account is touched (`npm run build` first).
387
+
388
+ ### Reviewing the settings UI without a host
389
+
390
+ `tools/preview-entry.tsx` mounts the real section with a stubbed RPC caller, so
391
+ a design change can be screenshotted on its own. Query flags drive the states:
392
+ `?dark=1`, `?grok=1`, `?ag=1`, `?busy=1`, `?hide=1`, `?collapse=1`, `?manage=1`.
393
+
394
+ ```sh
395
+ npm run preview # bundles probe/preview.js
396
+ # then open probe/preview.html in a browser, or screenshot it headlessly:
397
+ msedge --headless=new --user-data-dir=%TEMP%\shot --window-size=620,1900 \
398
+ --screenshot=shot.png "file:///<checkout>/probe/preview.html?dark=1&grok=1&ag=1"
399
+ ```
400
+
401
+ The harness deliberately styles no `button` of its own: the shell gives its
402
+ controls chrome through a CSS class plugin markup does not carry, so a preview
403
+ that decorated bare buttons would hide exactly the regression this page had.
404
+
405
+ Query flags: `dark=1` (dark theme), `compact=1` (model list collapsed),
406
+ `busy=1` (pending device authorization), `manage=1` (manage dialog open).
407
+ `probe/` holds only generated preview output and is gitignored; the committed
408
+ screenshots live in `docs/images/`.
409
+
410
+ After `npm run build`, restart `dsh web` to pick up the change.
411
+
412
+ ## License
413
+
414
+ MIT
@@ -0,0 +1,13 @@
1
+ # Bundle self-activation patch.
2
+ #
3
+ # When this package is installed into a profile (`dsh plugin --profile web add
4
+ # dsh-plugin-subscription-oauth`, or a local checkout / git URL), this patch mounts the
5
+ # plugin's node half. The client half (Settings -> Subscriptions) is discovered
6
+ # automatically from the `dsh.client` declaration in package.json.
7
+ #
8
+ # Installing this patch is also what decides whether a login persists: the
9
+ # token file below the DSH home is shared between profiles, so a login made in
10
+ # one window is visible in another once both mount this plugin.
11
+ - insert:
12
+ - id: llm-subscription-oauth
13
+ name: dsh-plugin-subscription-oauth
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Kimi Code's OAuth 2.0 device authorization grant (RFC 8628).
3
+ *
4
+ * Kimi Code has no loopback redirect: authorization happens on
5
+ * `www.kimi.com`, and this side polls `auth.kimi.com` for the token. The token
6
+ * endpoint answers with a JWT whose payload carries the account identity, so
7
+ * the flow settles the account id from the tokens themselves.
8
+ *
9
+ * @module dsh-plugin-subscription-oauth/auth/device-flow
10
+ */
11
+ /** Token endpoint payload normalised into an absolute expiry. */
12
+ export interface KimiTokenSet {
13
+ readonly accessToken: string;
14
+ readonly refreshToken?: string;
15
+ /** Epoch milliseconds after which the access token must not be sent. */
16
+ readonly expiresAt: number;
17
+ }
18
+ /** How to reach Kimi's device and token endpoints for one flow. */
19
+ export interface DeviceFlowSpec {
20
+ /** OAuth host, e.g. `https://auth.kimi.com` (no trailing slash). */
21
+ readonly host: string;
22
+ /** This machine's persisted device id, sent on every request. */
23
+ readonly deviceId: string;
24
+ /** Injected transport; defaults to the global `fetch`. */
25
+ readonly fetchFn?: typeof fetch;
26
+ }
27
+ /** One pending device authorization the browser can display and cancel. */
28
+ export interface DeviceAttempt {
29
+ /** URL the user opens (the `verification_uri_complete` form when offered). */
30
+ readonly verificationUrl: string;
31
+ /** Code the user would type when the complete URL is not usable. */
32
+ readonly userCode: string;
33
+ /** Epoch milliseconds after which the code is refused and the attempt lapses. */
34
+ readonly expiresAt: number;
35
+ /** Resolves with the tokens once the user approves; rejects on failure/cancel. */
36
+ waitToken(): Promise<KimiTokenSet>;
37
+ /** Abandon the attempt; any in-flight poll settles as a cancellation. */
38
+ cancel(): void;
39
+ }
40
+ /** Error every abandoned attempt settles with. */
41
+ export declare const LOGIN_CANCELLED_MESSAGE = "login cancelled";
42
+ /**
43
+ * Turn a token-endpoint payload into a token set with an absolute expiry.
44
+ *
45
+ * `expires_in` must be a finite non-negative number: a missing or nonsensical
46
+ * lifetime would otherwise mint a credential the harness treats as valid
47
+ * forever.
48
+ *
49
+ * The stored instant is the token's REAL expiry, with no safety margin baked in.
50
+ * Kimi Code's access tokens live only 15 minutes, so subtracting the refresh
51
+ * margin here would report a third of that life as already gone — and the card
52
+ * that shows this field would claim "expired" minutes after a successful login.
53
+ * The margin belongs to the refresh trigger, not to the recorded fact.
54
+ *
55
+ * @param payload - decoded token-endpoint JSON.
56
+ * @param previousRefreshToken - refresh token to retain when the response omits one.
57
+ * @returns the normalised token set.
58
+ * @throws {Error} `Kimi token response missing required fields` when unusable.
59
+ */
60
+ export declare function parseTokenPayload(payload: unknown, previousRefreshToken?: string): KimiTokenSet;
61
+ /**
62
+ * Decode one JWT's payload claims.
63
+ * @param token - the JWT, or undefined.
64
+ * @returns the claims object, or undefined for an opaque or malformed token.
65
+ */
66
+ export declare function decodeJwtClaims(token: string | undefined): Record<string, unknown> | undefined;
67
+ /**
68
+ * Recover the account identity from Kimi's tokens.
69
+ *
70
+ * The access token is authoritative and the refresh token the fallback; a
71
+ * `user_id` claim outranks `sub` in either token because the CLI's sessions
72
+ * are keyed by user id while `sub` is the OAuth subject.
73
+ *
74
+ * @param accessToken - the issued access token.
75
+ * @param refreshToken - the issued refresh token, when the response carried one.
76
+ * @returns the account id and lowercased email, each when the tokens disclose it.
77
+ */
78
+ export declare function identityFromTokens(accessToken: string | undefined, refreshToken?: string): {
79
+ accountId?: string;
80
+ email?: string;
81
+ };
82
+ /**
83
+ * Owns at most one in-flight device authorization per provider route.
84
+ *
85
+ * Only one attempt can settle into the login: a second `start()` supersedes
86
+ * the first, whose poll is aborted so it cannot overwrite the newer session.
87
+ */
88
+ export declare class DeviceFlowManager {
89
+ private readonly attempts;
90
+ /** Whether an authorization is currently being polled for this route. */
91
+ isBusy(provider: string): boolean;
92
+ /** The in-flight attempt for a route, when one exists. */
93
+ pending(provider: string): DeviceAttempt | undefined;
94
+ /**
95
+ * Begin a device authorization.
96
+ * @param provider - provider route key owning this attempt.
97
+ * @param spec - endpoint host, device id, and transport.
98
+ * @returns the pending attempt, whose `verificationUrl`/`userCode` the caller shows.
99
+ */
100
+ start(provider: string, spec: DeviceFlowSpec): Promise<DeviceAttempt>;
101
+ /** Drop the attempt record, but never one that a newer `start()` replaced. */
102
+ private settle;
103
+ private requestDeviceCode;
104
+ private poll;
105
+ }
106
+ /**
107
+ * Exchange a refresh token for a fresh access token.
108
+ * @param spec - endpoint host, device id, and transport.
109
+ * @param refreshToken - the refresh token to redeem.
110
+ * @returns the refreshed token set (retaining the previous refresh token when omitted).
111
+ */
112
+ export declare function refreshKimiToken(spec: DeviceFlowSpec, refreshToken: string): Promise<KimiTokenSet>;