dsh-aperture 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 uy/sun
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.en.md ADDED
@@ -0,0 +1,316 @@
1
+ # dsh-aperture
2
+
3
+ Discover the models an [Aperture](https://tailscale.com/kb/1542/aperture) gateway serves, and publish them to DeepSeek Harness as `llm-pi-ai` provider routes — so they simply appear in the model selector.
4
+
5
+ The reference implementation is [he0119/vscode-aperture-for-copilot](https://github.com/he0119/vscode-aperture-for-copilot). The difference is that the extension had to implement a provider from scratch, while DSH already ships `@deepseek-ai/dsh-llm-pi-ai` — an adapter that speaks OpenAI Chat Completions and Anthropic Messages, which is exactly what Aperture exposes. So this plugin **only discovers; it converts no API format**. It translates a discovery result into `llm-pi-ai` provider profiles and lets the existing adapter do the rest.
6
+
7
+ ```yaml
8
+ # ~/.dsh/settings.yaml — written by the plugin; you do not hand-write this
9
+ llm-pi-ai:
10
+ providers:
11
+ aperture:
12
+ api: openai-completions
13
+ baseURL: https://ai.example.ts.net/v1
14
+ models:
15
+ - id: deepseek-flash
16
+ name: DeepSeek V4.1 Flash
17
+ contextWindow: 1048576
18
+ maxTokens: 384000
19
+ ```
20
+
21
+ ## Features
22
+
23
+ - **Zero-configuration discovery.** One `baseUrl` is enough. `GET {baseUrl}/v1/models` needs no key; Aperture authenticates by network identity (Tailscale).
24
+ - **Per-model protocol routing.** Each model's `supported_endpoints` decides which route it is published on, instead of publishing models that 404 on every request.
25
+ - **Capacity and capability completion.** Aperture's own fields win, [models.dev](https://models.dev) answers what Aperture left out, and a configurable conservative default answers last.
26
+ - **Reasoning mapping.** Only levels the upstream actually accepts are offered, with the DeepSeek `thinking` dialect expressed through `compat`.
27
+ - **Writes only what it owns.** Writes are path-addressed, so your hand-written providers survive untouched; an unchanged section is not rewritten; a failed discovery never wipes the published catalog.
28
+ - **A `/aperture` command** for status, model listing, forced refresh, and route removal.
29
+
30
+ ## Install
31
+
32
+ ```sh
33
+ # from git (pin the commit, so a later push cannot silently change what runs)
34
+ npx @deepseek-ai/dsh plugin --profile web add github:he0119/dsh-aperture#<commit>
35
+
36
+ # or from a local checkout
37
+ npx @deepseek-ai/dsh plugin --profile web add /path/to/dsh-aperture
38
+ ```
39
+
40
+ > **The git spec must be one pnpm recognises** (`github:owner/repo`, `git+https://…`, `git+ssh://…`).
41
+ > The scp-style `git@github.com:he0119/dsh-aperture.git` is parsed as a **local path** on Windows —
42
+ > the colon reads as a drive letter — and fails with
43
+ > `Failed to resolve dependency: The filename, directory name, or volume label syntax is incorrect. (os error 123)`.
44
+ >
45
+ > If git over HTTPS is unusable on this machine — schannel / `SEC_E_NO_CREDENTIALS` — let it fall back to SSH with
46
+ > `git config --global url."git@github.com:".insteadOf "https://github.com/"`.
47
+
48
+ Before starting, look at the composed result:
49
+
50
+ ```sh
51
+ npx @deepseek-ai/dsh --profile web --dump-config # should show a "# == dsh-aperture" layer
52
+ ```
53
+
54
+ **Restart DSH afterwards** — bundle changes are not hot-reloaded.
55
+
56
+ ### The first install has to authorize the build (or use a prebuilt artifact)
57
+
58
+ This is a TypeScript source package: a git install pulls **source**, and a `prepare` script compiles `lib/` during
59
+ install. pnpm ≥10 refuses to run a git dependency's build scripts until it is explicitly allowed, so **the first
60
+ `add` fails** — dsh then tells you the exact package key, which you write into the profile's `pnpm-workspace.yaml`:
61
+
62
+ ```yaml
63
+ # ~/.dsh/profiles/web/pnpm-workspace.yaml
64
+ allowBuilds:
65
+ dsh-aperture: true
66
+ ```
67
+
68
+ (The key takes the **map** form with `true`; a list is not accepted.) Re-run the `add` afterwards.
69
+
70
+ Read that authorization as "let this package's code run on your machine at install time, outside any agent sandbox".
71
+ Authorize only source you trust, and pin the commit (`github:he0119/dsh-aperture#<sha>`) — a git install pulls code
72
+ that can change.
73
+
74
+ If you would rather **not** authorize anything, use a prebuilt artifact, and pnpm never needs to run a script of ours:
75
+
76
+ ```sh
77
+ pnpm pack # in this repository: dsh-aperture-0.1.0.tgz, with lib/ inside
78
+ npx @deepseek-ai/dsh plugin --profile web add ./dsh-aperture-0.1.0.tgz
79
+ ```
80
+
81
+ Once published to npm, `dsh plugin add dsh-aperture` works the same way.
82
+
83
+ > pnpm may warn `Issues with peer dependencies found` during install. **That is expected**: the
84
+ > `@deepseek-ai/cordis`, `@deepseek-ai/schemastery` and `@deepseek-ai/dsh-settings` packages this plugin peers on ship
85
+ > with DSH and are resolved by DSH itself (they never enter the profile's `node_modules`), so pnpm not seeing them does
86
+ > not affect running the plugin.
87
+
88
+ `dsh plugin add` adds the package to the profile and reconciles `dsh.profile.bundles`: any dependency declaring `dsh.bundle.patch` joins the layer stack automatically. **Restart DSH afterwards** — bundle changes are not hot-reloaded.
89
+
90
+ Then point it at your instance, either in the user layer:
91
+
92
+ ```yaml
93
+ # ~/.dsh/settings.yaml (recommended)
94
+ aperture:
95
+ baseUrl: https://ai.example.ts.net
96
+ ```
97
+
98
+ …or in the profile's `cordis.patch.yml` (composition layer). The user layer wins.
99
+
100
+ After the restart the models appear in the selector under the route `Aperture`. Confirm with `/aperture` — the
101
+ command output and the log lines report in Chinese:
102
+
103
+ ```
104
+ Aperture:https://ai.example.ts.net
105
+ 最近一次刷新:配置变更 · 2026-09-22T16:31:02.184Z · 412ms · 成功
106
+ 清单:422 个条目
107
+ 端点:https://ai.example.ts.net/v1/models 列出了 16 行
108
+ 路由 aperture:11 个模型,经由 openai-completions → https://ai.example.ts.net/v1
109
+ 路由 aperture-anthropic:1 个模型,经由 anthropic-messages → https://ai.example.ts.net
110
+ 未服务:4 个模型(gemini-2.5-flash, gemini-2.5-flash-lite, gemini-2.5-pro, gemini-3.1-flash-image-preview)
111
+ 设置:向 llm-pi-ai 写入 2 个操作(aperture, aperture-anthropic)
112
+ ```
113
+
114
+ ## How it works
115
+
116
+ ```
117
+ GET {baseUrl}/v1/models
118
+ │
119
+ ├─ per model: supported_endpoints ──► route
120
+ │ /v1/chat/completions ──► route (openai-completions)
121
+ │ /v1/messages ──► anthropicRoute (anthropic-messages)
122
+ │ native generateContent only ──► not published (reported under 未服务)
123
+ │
124
+ ├─ capacity: Aperture fields ─► models.dev ─► default
125
+ ├─ reasoning: Aperture fields ─► models.dev ─► off
126
+ │
127
+ └─ llm-pi-ai providers.<route>, written to settings.yaml by revision
128
+ ```
129
+
130
+ ### Why two routes
131
+
132
+ Aperture is an endpoint-aware gateway: a given model does not accept every protocol. Measured against a live instance:
133
+
134
+ | Models | `supported_endpoints` | Result |
135
+ | --- | --- | --- |
136
+ | 11 (DeepSeek / MiMo / Grok / Qwen / LongCat …) | `/v1/chat/completions` | route `aperture` |
137
+ | `MiniMax-M3` | `/v1/messages` | route `aperture-anthropic` |
138
+ | 4 Gemini models | `/v1beta/models/{model}:generateContent` | not published |
139
+
140
+ Using the wrong endpoint fails loudly, and Aperture names the right one:
141
+
142
+ ```
143
+ 404 model "gemini-2.5-flash" is available via gemini_generate_content, not openai_chat
144
+ 404 model "MiniMax-M3" is available via anthropic_messages, not openai_chat
145
+ ```
146
+
147
+ `llm-pi-ai` speaks OpenAI-compatible and Anthropic Messages only, so those four Gemini models **cannot** be attached through it. The plugin reports them instead of publishing models that are guaranteed to fail.
148
+
149
+ ### The placeholder credential
150
+
151
+ `llm-pi-ai` — more precisely the pi-ai SDK beneath it — throws `No API key for provider` when no credential exists at all. Aperture authenticates by network identity and needs none, so the plugin writes a placeholder header:
152
+
153
+ - route `aperture` → `authorization: Bearer dsh-aperture`
154
+ - route `aperture-anthropic` → `x-api-key: dsh-aperture`
155
+
156
+ **It is not a key**; it only makes the adapter willing to send the request, and a live Aperture instance answers a garbage bearer with 200. When a real credential is needed, point `apiKeyEnv` at a credential-seam record and no placeholder is written.
157
+
158
+ ### Where each fact comes from
159
+
160
+ | Fact | Precedence |
161
+ | --- | --- |
162
+ | `contextWindow` | Aperture `context_window_tokens` / `max_input_tokens` / `limit.context` → models.dev → `defaultContextWindow` |
163
+ | `maxTokens` | Aperture `max_output_tokens` / `limit.output` → models.dev → **nothing** |
164
+ | `input` (modalities) | with `images: metadata`: Aperture capability fields → models.dev → `["text"]`; with `images: ignore`: always `["text"]` |
165
+ | `reasoning` | Aperture capability fields → models.dev → off |
166
+
167
+ An unstated `maxTokens` is deliberately **omitted**: in `llm-pi-ai` it is both the declared output capability and the per-request `max_tokens` cap, so inventing 16384 would truncate every request.
168
+
169
+ ### Reasoning levels are conservative
170
+
171
+ A level in `reasoningEfforts` is the value sent on the wire. Measured:
172
+
173
+ - DeepSeek accepts `minimal/low/medium/high/xhigh/max/none`, returns `reasoning_content` by default, and needs `thinking: {type: disabled}` to suppress it;
174
+ - MiMo (`mimo-v2.6-pro`) answers **HTTP 400** for `minimal`, `xhigh`, and `max`.
175
+
176
+ So only two levels are offered by default:
177
+
178
+ | Model | `reasoningEfforts` | `compat` |
179
+ | --- | --- | --- |
180
+ | DeepSeek family (id/name/provider contains `deepseek`) | `{off: disabled, high: high, max: max}` | `{supportsReasoningEffort: true, thinkingFormat: deepseek}` |
181
+ | every other reasoning model | `{off: null, high: high}` | `{supportsReasoningEffort: true}` |
182
+
183
+ More levels are one configuration entry away:
184
+
185
+ ```yaml
186
+ aperture:
187
+ models:
188
+ - id: deepseek-v4-pro
189
+ reasoningEfforts:
190
+ off: disabled
191
+ minimal: minimal
192
+ low: low
193
+ medium: medium
194
+ high: high
195
+ max: max
196
+ ```
197
+
198
+ The Anthropic route declares **no reasoning levels** by default: Anthropic thinking is a different switch (it takes a budget), so `reasoning_effort` would mean nothing there. Opt in explicitly with `thinking: true`.
199
+
200
+ ### When a models.dev id does not match
201
+
202
+ Gateways rename things. In practice `deepseek-flash` and `k3` have no models.dev entry (the catalog calls them `deepseek/deepseek-v4-flash` and `moonshotai/kimi-k3`), and no scoring rule can bridge that without guessing. Bridge it explicitly:
203
+
204
+ ```yaml
205
+ aperture:
206
+ modelAliases:
207
+ deepseek-flash: deepseek/deepseek-v4-flash
208
+ k3: moonshotai/kimi-k3
209
+ ```
210
+
211
+ Only then do those two pick up the catalog's reasoning flag and capacity.
212
+
213
+ ### Lifecycle and dependencies
214
+
215
+ - `settings` is a **hard dependency** (`inject = ['settings']`). Writing into settings *is* this plugin's job, so
216
+ without it there is nothing to publish into: the framework holds the plugin at `PENDING`, unloads it if the
217
+ provider is replaced, and reloads it afterwards — rather than leaving a loaded instance with nowhere to publish.
218
+ - `commands` is deliberately **not** declared. A deployment without the command service should still get discovery,
219
+ so it is attached optionally through `ctx.inject(['commands'])`.
220
+ - The refresh timer is registered through `ctx.effect`, so it is cleaned up on unload; a configuration change
221
+ re-arms it with the new interval.
222
+ - **Registering the settings section is what starts the first discovery**: `installSection` calls `setSource` and
223
+ then notifies `onChange` at attach, so the first refresh already sees the user layer — one pass, not a
224
+ composition-config pass followed by a reconciling one.
225
+ - The section is held as a **live thunk, not a snapshot**: editing the `aperture:` section of
226
+ `~/.dsh/settings.yaml` reaches the next refresh with no restart and no plugin reload.
227
+
228
+ ## Commands
229
+
230
+ | Command | Effect |
231
+ | --- | --- |
232
+ | `/aperture` | status of the last refresh (default) |
233
+ | `/aperture models` | every model, its route, and the source of each fact |
234
+ | `/aperture refresh` | discover and republish now |
235
+ | `/aperture remove` | withdraw this plugin's routes from the `llm-pi-ai` section |
236
+
237
+ ## Configuration
238
+
239
+ Every key may live in the `aperture:` section of `~/.dsh/settings.yaml` (user layer) or in the profile's `cordis.patch.yml` (composition layer).
240
+
241
+ | Key | Default | Meaning |
242
+ | --- | --- | --- |
243
+ | `baseUrl` | `''` | Aperture instance root. A trailing `/v1` is tolerated and stripped; empty leaves the plugin dormant |
244
+ | `route` | `aperture` | Route key owning OpenAI-compatible models |
245
+ | `anthropicRoute` | `aperture-anthropic` | Route key owning Anthropic Messages models |
246
+ | `displayName` | `Aperture` | Selector label |
247
+ | `anthropicDisplayName` | `Aperture (Anthropic)` | Selector label for the Anthropic route |
248
+ | `apiKeyEnv` | `''` | Credential-seam reference; non-empty suppresses the placeholder header |
249
+ | `placeholderCredential` | `dsh-aperture` | Placeholder value; `''` writes no placeholder header at all |
250
+ | `headers` | `{}` | Extra request headers; they **win over** the placeholder |
251
+ | `enabledModelIds` | `[]` | Non-empty restricts discovery to these ids (explicit `models` entries are exempt) |
252
+ | `modelAliases` | `{}` | Gateway id → models.dev id |
253
+ | `models` | `[]` | Per-model overrides and extras: `id`, `name`, `api`, `contextWindow`, `maxTokens`, `input`, `thinking`, `reasoningEfforts` |
254
+ | `modelMetadataUrl` | `https://models.dev/models.json` | Catalog URL; `''` disables enrichment |
255
+ | `defaultContextWindow` | `128000` | Context capacity when nothing sizes a model |
256
+ | `images` | `ignore` | `metadata` adopts models.dev input modalities (images) |
257
+ | `reasoning` | `auto` | `off` declares every model non-reasoning |
258
+ | `sync` | `true` | `false` discovers without writing (`/aperture` still reports) |
259
+ | `refreshIntervalMinutes` | `0` | Periodic refresh; `0` refreshes only at load and on change |
260
+ | `timeoutMs` | `20000` | Per-request timeout for the gateway and the catalog |
261
+
262
+ `models` both overrides discovered entries (only the fields you state) and adds models the gateway did not list — for instance those four Gemini models, if you know they work on an OpenAI-compatible endpoint after all:
263
+
264
+ ```yaml
265
+ aperture:
266
+ models:
267
+ - id: gemini-2.5-pro
268
+ api: openai-completions
269
+ ```
270
+
271
+ ## Write behaviour
272
+
273
+ The plugin touches exactly two keys under `llm-pi-ai.providers` (`route` and `anthropicRoute`), and:
274
+
275
+ - **writes nothing when nothing changed** — each refresh compares against the resolved section, so there is no rewrite churn and no watcher feedback loop;
276
+ - **writes with path ops** — not one field of your other providers is touched;
277
+ - **writes nothing after a failed discovery** — a transient gateway outage keeps the catalog that is already serving;
278
+ - **writes by revision** — a conflict with another writer (the Models page, another process) re-reads once and retries;
279
+ - **removes a route that lost its models** — no empty route pointing at a stale catalog is left behind.
280
+
281
+ `设置:未写入(已处于同步状态)` in `/aperture` is the normal steady state.
282
+
283
+ ## Development
284
+
285
+ ```sh
286
+ npm install # if the machine-level npm cache is not writable: npm install --cache ./.npm-cache --ignore-scripts
287
+ npm run build # tsc -> lib/
288
+ npm run typecheck # includes test/
289
+ npm test # offline unit tests (93, no node_modules needed)
290
+ npm run test:live # end-to-end: real harness stack + real gateway
291
+ npm run inspect # print the generated settings.yaml and its resolutions
292
+ ```
293
+
294
+ `npm run test:live` needs a gateway address:
295
+
296
+ ```sh
297
+ DSH_APERTURE_LIVE_URL=https://ai.example.ts.net npm run test:live
298
+ ```
299
+
300
+ It boots the real `dsh-settings-file`, `dsh-llm`, `dsh-llm-pi-ai`, and this plugin, performs a real discovery, and then asserts that the settings file was written, both routes registered, `ctx.llm.listModels()` can see the models, and capacity and reasoning levels match. It is the only test that proves the adapter actually accepts what was written.
301
+
302
+ > `lib/` is a build artifact: a git install and `npm publish` both compile it through the `prepare` script.
303
+ > Installing from a **local directory** (`link:`) does *not* run `prepare`, so run `npm run build` before
304
+ > debugging locally.
305
+
306
+ ## Known boundaries
307
+
308
+ - **No API-format conversion**, by design rather than by omission — and therefore only the two protocols `llm-pi-ai` speaks. Native Gemini endpoints cannot be attached.
309
+ - `supported_endpoints` is the only protocol signal. A gateway that reports none is treated as OpenAI-compatible.
310
+ - models.dev is best-effort enrichment: if it cannot be fetched, discovery still succeeds.
311
+ - The plugin registers no provider directory (`registerConfigurableProviders`) — `llm-pi-ai` already claims that, and a duplicate registration throws.
312
+ - **Renaming `route` / `anthropicRoute` leaves the old key behind** in `llm-pi-ai.providers`: the plugin only knows the two keys it currently owns, not the names it used before. The stale route does no harm and simply stops refreshing; delete the entry by hand to clean it up.
313
+
314
+ ## License
315
+
316
+ MIT
package/README.md ADDED
@@ -0,0 +1,303 @@
1
+ # dsh-aperture
2
+
3
+ 把 [Aperture](https://tailscale.com/kb/1542/aperture) 网关暴露的模型**自动发现**出来,写进 DeepSeek Harness 的 `llm-pi-ai` 设置里,让它们直接出现在模型选择器中。
4
+
5
+ 参考实现是 [he0119/vscode-aperture-for-copilot](https://github.com/he0119/vscode-aperture-for-copilot)。区别在于:那个扩展需要自己实现一套 provider;而 DSH 里已经有 `@deepseek-ai/dsh-llm-pi-ai`,它本来就讲 OpenAI Chat Completions 和 Anthropic Messages——正好是 Aperture 唯一暴露的两种协议。所以这个插件**只做发现,不做任何 API 格式转换**:它把探测结果翻译成 `llm-pi-ai` 的 provider profiles,剩下的交给现成的适配器。
6
+
7
+ ```yaml
8
+ # ~/.dsh/settings.yaml —— 由插件自动写入,不需要手写
9
+ llm-pi-ai:
10
+ providers:
11
+ aperture:
12
+ api: openai-completions
13
+ baseURL: https://ai.example.ts.net/v1
14
+ models:
15
+ - id: deepseek-flash
16
+ name: DeepSeek V4.1 Flash
17
+ contextWindow: 1048576
18
+ maxTokens: 384000
19
+ ```
20
+
21
+ ## 特性
22
+
23
+ - **零配置发现**:只需一个 `baseUrl`。`GET {baseUrl}/v1/models` 不需要密钥,靠网络身份(Tailscale)认证。
24
+ - **按协议自动分流**:读每个模型的 `supported_endpoints`,把它发到它真正应答的那条路由上,而不是让它每条请求都 404。
25
+ - **容量与能力补全**:Aperture 自己的字段优先,缺失时用 [models.dev](https://models.dev) 补全,再缺失时用可配置的保守默认值。
26
+ - **推理等级映射**:只暴露后端实测接受的等级,并按 DeepSeek 的 `thinking` 方言生成 `compat`。
27
+ - **只写自己拥有的键**:写入是路径寻址的,你手写的其它 provider 原样保留;内容没变就不写;探测失败绝不删空已有目录。
28
+ - **`/aperture` 命令**:查看状态、列出模型、强制刷新、撤销路由。
29
+
30
+ ## 安装
31
+
32
+ ```sh
33
+ # 从 git(建议锁 commit,后续推送就无法悄悄改变实际运行的代码)
34
+ npx @deepseek-ai/dsh plugin --profile web add github:he0119/dsh-aperture#<commit>
35
+
36
+ # 或从本地目录(开发时)
37
+ npx @deepseek-ai/dsh plugin --profile web add /path/to/dsh-aperture
38
+ ```
39
+
40
+ > **git 地址必须用 pnpm 认的写法**(`github:owner/repo`、`git+https://…`、`git+ssh://…`)。
41
+ > `git@github.com:he0119/dsh-aperture.git` 这种 scp 风格在 Windows 上会被当成**本地路径**解析——冒号被当作盘符——
42
+ > 直接报 `Failed to resolve dependency: 文件名、目录名或卷标语法不正确。 (os error 123)`。
43
+ >
44
+ > 如果本机 git 的 HTTPS 不可用、报 schannel / `SEC_E_NO_CREDENTIALS`,用
45
+ > `git config --global url."git@github.com:".insteadOf "https://github.com/"` 让它改走 SSH。
46
+
47
+ `dsh plugin add` 会把包加进 profile,并自动把本包的 bundle patch 追加到 `dsh.profile.bundles`。启动前可以先只看组合结果:
48
+
49
+ ```sh
50
+ npx @deepseek-ai/dsh --profile web --dump-config # 应出现 "# == dsh-aperture" 层
51
+ ```
52
+
53
+ **装完需要重启这次 DSH**(bundle 变化不会热加载)。
54
+
55
+ ### 第一次安装要放行构建(或者用预构建产物)
56
+
57
+ 这是个 TypeScript 源码包,git 安装拉下来的是**源码**,靠 `prepare` 脚本在安装时编译出 `lib/`。
58
+ pnpm ≥10 在得到显式允许前**拒绝运行 git 依赖的构建脚本**,所以**第一次 `add` 会失败**;dsh 会把 pnpm 打印的包键告诉你,
59
+ 把它写进 profile 的 `pnpm-workspace.yaml`:
60
+
61
+ ```yaml
62
+ # ~/.dsh/profiles/web/pnpm-workspace.yaml
63
+ allowBuilds:
64
+ dsh-aperture: true
65
+ ```
66
+
67
+ (键是**映射**形式,值为 `true`;写成列表无效。)然后重跑一次 `add` 即可。
68
+
69
+ 请把这项授权理解为「允许这个包在你机器上、于 agent 沙箱之外执行安装脚本」。只对信任的源码授权,并且锁 commit
70
+ (`github:he0119/dsh-aperture#<sha>`)——git 安装拉取的是会变的代码。
71
+
72
+ **不想授权**的话,用预构建产物,pnpm 就完全不需要运行你的任何脚本:
73
+
74
+ ```sh
75
+ pnpm pack # 在本仓库里打出 dsh-aperture-0.1.0.tgz(含 lib/)
76
+ npx @deepseek-ai/dsh plugin --profile web add ./dsh-aperture-0.1.0.tgz
77
+ ```
78
+
79
+ 发布到 npm 之后同样可以 `dsh plugin add dsh-aperture`。
80
+
81
+ > 安装时 pnpm 可能警告 `Issues with peer dependencies found`。**这是预期的**:本包依赖的
82
+ > `@deepseek-ai/cordis`、`@deepseek-ai/schemastery`、`@deepseek-ai/dsh-settings` 等都是 DSH 自带、
83
+ > 由 DSH 自己解析的包(它们不进 profile 的 `node_modules`),所以 pnpm 看不到它们并不影响运行。
84
+
85
+
86
+ 然后填上你的 Aperture 地址,二选一:
87
+
88
+ ```yaml
89
+ # ~/.dsh/settings.yaml(用户层,推荐)
90
+ aperture:
91
+ baseUrl: https://ai.example.ts.net
92
+ ```
93
+
94
+ 或者直接改 profile 里的 `cordis.patch.yml`(组合层)。用户层覆盖组合层。
95
+
96
+ 重启后模型就会出现在选择器里(路由名 `Aperture`)。用 `/aperture` 确认:
97
+
98
+ ```
99
+ Aperture:https://ai.example.ts.net
100
+ 最近一次刷新:配置变更 · 2026-09-22T16:31:02.184Z · 412ms · 成功
101
+ 清单:422 个条目
102
+ 端点:https://ai.example.ts.net/v1/models 列出了 16 行
103
+ 路由 aperture:11 个模型,经由 openai-completions → https://ai.example.ts.net/v1
104
+ 路由 aperture-anthropic:1 个模型,经由 anthropic-messages → https://ai.example.ts.net
105
+ 未服务:4 个模型(gemini-2.5-flash, gemini-2.5-flash-lite, gemini-2.5-pro, gemini-3.1-flash-image-preview)
106
+ 设置:向 llm-pi-ai 写入 2 个操作(aperture, aperture-anthropic)
107
+ ```
108
+
109
+ ## 工作原理
110
+
111
+ ```
112
+ GET {baseUrl}/v1/models
113
+ │
114
+ ├─ 每个模型:supported_endpoints ──► 分流
115
+ │ /v1/chat/completions ──► route (openai-completions)
116
+ │ /v1/messages ──► anthropicRoute (anthropic-messages)
117
+ │ 只有原生 generateContent ──► 不发布(列进「未服务」)
118
+ │
119
+ ├─ 容量:Aperture 字段 ─► models.dev ─► 默认值
120
+ ├─ 推理:Aperture 字段 ─► models.dev ─► 关闭
121
+ │
122
+ └─ 生成 llm-pi-ai 的 providers.<route>,按 revision 写入 settings.yaml
123
+ ```
124
+
125
+ ### 为什么有两条路由
126
+
127
+ Aperture 是按**端点**网关的:同一个模型不是所有协议都收。实测(`https://ai.long-antares.ts.net/`):
128
+
129
+ | 模型 | `supported_endpoints` | 结果 |
130
+ | --- | --- | --- |
131
+ | 11 个(DeepSeek / MiMo / Grok / Qwen / LongCat …) | `/v1/chat/completions` | 走 `aperture` |
132
+ | `MiniMax-M3` | `/v1/messages` | 走 `aperture-anthropic` |
133
+ | 4 个 Gemini | `/v1beta/models/{model}:generateContent` | 不发布 |
134
+
135
+ 走错端点的失败是明确的(Aperture 直接告诉你该用哪个):
136
+
137
+ ```
138
+ 404 model "gemini-2.5-flash" is available via gemini_generate_content, not openai_chat
139
+ 404 model "MiniMax-M3" is available via anthropic_messages, not openai_chat
140
+ ```
141
+
142
+ `llm-pi-ai` 只讲 OpenAI 兼容协议和 Anthropic Messages,所以 Gemini 那 4 个**无法**接入——插件会把它们列出来,而不是发布一堆必然 404 的模型。
143
+
144
+ ### 关于“占位凭据”
145
+
146
+ `llm-pi-ai`(更准确地说,它背后的 pi-ai)在没有任何密钥时会直接抛 `No API key for provider`。Aperture 靠网络身份认证,本不需要密钥,所以插件默认写入一个占位请求头:
147
+
148
+ - `aperture` 路由 → `authorization: Bearer dsh-aperture`
149
+ - `aperture-anthropic` 路由 → `x-api-key: dsh-aperture`
150
+
151
+ **这不是密钥**,只是让适配器愿意发请求。实测 Aperture 对垃圾 Bearer 照常返回 200。如果你确实需要真密钥,用 `apiKeyEnv` 指向凭据 seam 里的记录,占位头就不会写入。
152
+
153
+ ### 容量与能力的来源优先级
154
+
155
+ | 事实 | 优先级 |
156
+ | --- | --- |
157
+ | `contextWindow` | Aperture `context_window_tokens` / `max_input_tokens` / `limit.context` → models.dev → `defaultContextWindow` |
158
+ | `maxTokens` | Aperture `max_output_tokens` / `limit.output` → models.dev → **不写** |
159
+ | `input`(多模态) | `images: metadata` 时取 Aperture 能力字段 → models.dev → `["text"]`;`images: ignore` 时恒为 `["text"]` |
160
+ | `reasoning` | Aperture 能力字段 → models.dev → 关闭 |
161
+
162
+ `maxTokens` 没人声明时**故意不写**:在 `llm-pi-ai` 里它同时是「输出能力」和「每次请求的 max_tokens 上限」,凭空编一个 16384 会把每次请求都截断。
163
+
164
+ ### 推理等级是保守的
165
+
166
+ `reasoningEfforts` 的等级 = 实际发出去的参数值。实测:
167
+
168
+ - DeepSeek 接受 `minimal/low/medium/high/xhigh/max/none`,且默认就会返回 `reasoning_content`,用 `thinking: {type: disabled}` 才能关掉;
169
+ - MiMo(`mimo-v2.6-pro`)对 `minimal`/`xhigh`/`max` 直接 **HTTP 400**。
170
+
171
+ 所以默认只给两档:
172
+
173
+ | 模型 | `reasoningEfforts` | `compat` |
174
+ | --- | --- | --- |
175
+ | DeepSeek 系(id/name/provider 含 `deepseek`) | `{off: disabled, high: high, max: max}` | `{supportsReasoningEffort: true, thinkingFormat: deepseek}` |
176
+ | 其它会推理的模型 | `{off: null, high: high}` | `{supportsReasoningEffort: true}` |
177
+
178
+ 想要更多档位,用 `models` 自己加:
179
+
180
+ ```yaml
181
+ aperture:
182
+ models:
183
+ - id: deepseek-v4-pro
184
+ reasoningEfforts:
185
+ off: disabled
186
+ minimal: minimal
187
+ low: low
188
+ medium: medium
189
+ high: high
190
+ max: max
191
+ ```
192
+
193
+ Anthropic 路由**默认不给推理档位**:Anthropic 的 thinking 是另一套开关(要 budget),发 `reasoning_effort` 没有意义。要开就用 `thinking: true` 显式声明。
194
+
195
+ ### models.dev id 对不上时
196
+
197
+ 网关会改名。实测里 `deepseek-flash`、`k3` 在 models.dev 上找不到对应条目(那边叫 `deepseek/deepseek-v4-flash`、`moonshotai/kimi-k3`),靠打分是猜不出来的,于是显式映射:
198
+
199
+ ```yaml
200
+ aperture:
201
+ modelAliases:
202
+ deepseek-flash: deepseek/deepseek-v4-flash
203
+ k3: moonshotai/kimi-k3
204
+ ```
205
+
206
+ 映射之后这两个模型才会拿到 models.dev 的推理标记和容量。
207
+
208
+ ### 生命周期与依赖
209
+
210
+ - `settings` 是**硬依赖**(`inject = ['settings']`)。本插件的职责就是往设置里写,没有它发布对象都不存在,所以宁可让框架把插件挂在 `PENDING`,provider 被替换时自动卸载、恢复后重新加载,而不是留着一个无处发布的实例。
211
+ - `commands` **不声明**。没有命令服务的部署也该照常获得发现能力,所以它走 `ctx.inject(['commands'])` 可选挂载。
212
+ - 定时刷新用 `ctx.effect` 注册,卸载自动清理;配置变更会按新值重新计算间隔。
213
+ - **注册配置段本身就是第一次发现的触发器**:`installSection` 挂载时先 `setSource` 再通知 `onChange`,所以首次刷新看到的就是叠加了用户层的配置,不需要为了对齐两份配置再刷一遍。
214
+ - 配置段是**活引用**(thunk)而不是快照:改 `~/.dsh/settings.yaml` 里的 `aperture:` 段,下一次刷新立刻用新值,不必重启、也不必重载插件。
215
+
216
+ ## 命令
217
+
218
+ | 命令 | 作用 |
219
+ | --- | --- |
220
+ | `/aperture` | 上一次刷新的状态(默认) |
221
+ | `/aperture models` | 列出每个模型、它的路由,以及每条事实的来源 |
222
+ | `/aperture refresh` | 立刻重新发现并发布 |
223
+ | `/aperture remove` | 把本插件的路由从 `llm-pi-ai` 段里撤掉 |
224
+
225
+ ## 配置
226
+
227
+ 所有键都可写在 `~/.dsh/settings.yaml` 的 `aperture:` 段(用户层),或 profile 的 `cordis.patch.yml`(组合层)。
228
+
229
+ | 键 | 默认 | 说明 |
230
+ | --- | --- | --- |
231
+ | `baseUrl` | `''` | Aperture 实例地址。末尾 `/v1` 会被容忍并去掉;空值表示休眠(不探测、不写入) |
232
+ | `route` | `aperture` | OpenAI 兼容模型的路由名 |
233
+ | `anthropicRoute` | `aperture-anthropic` | Anthropic Messages 模型的路由名 |
234
+ | `displayName` | `Aperture` | 选择器里的名字 |
235
+ | `anthropicDisplayName` | `Aperture (Anthropic)` | 同上,Anthropic 路由 |
236
+ | `apiKeyEnv` | `''` | 凭据 seam 里的引用名;非空时不再写占位头 |
237
+ | `placeholderCredential` | `dsh-aperture` | 占位凭据的值;设成 `''` 就完全不写占位头 |
238
+ | `headers` | `{}` | 每条请求额外带的头,**优先于**占位头 |
239
+ | `enabledModelIds` | `[]` | 非空时只保留这些 id(`models` 里显式列出的不受限) |
240
+ | `modelAliases` | `{}` | 网关 id → models.dev id |
241
+ | `models` | `[]` | 逐模型覆盖或补充:`id`、`name`、`api`、`contextWindow`、`maxTokens`、`input`、`thinking`、`reasoningEfforts` |
242
+ | `modelMetadataUrl` | `https://models.dev/models.json` | 目录地址;设成 `''` 关闭补全 |
243
+ | `defaultContextWindow` | `128000` | 谁都没说容量时的上下文 |
244
+ | `images` | `ignore` | `metadata` = 采用 models.dev 的输入模态(图片) |
245
+ | `reasoning` | `auto` | `off` = 所有模型都当不会推理 |
246
+ | `sync` | `true` | `false` = 只探测不写设置(`/aperture` 仍可查看) |
247
+ | `refreshIntervalMinutes` | `0` | 定时刷新间隔;`0` = 只在启动和配置变化时刷新 |
248
+ | `timeoutMs` | `20000` | 网关与目录的单次请求超时 |
249
+
250
+ `models` 既能覆盖已发现的模型(只覆盖你写了的字段),也能补充网关**没**列出的模型——比如上面那 4 个 Gemini 模型,只要你知道它在 OpenAI 兼容端点上确实能用:
251
+
252
+ ```yaml
253
+ aperture:
254
+ models:
255
+ - id: gemini-2.5-pro
256
+ api: openai-completions
257
+ ```
258
+
259
+ ## 写入行为
260
+
261
+ 插件只碰 `llm-pi-ai.providers` 下的两个键(`route` / `anthropicRoute`),并且:
262
+
263
+ - **内容相同就不写**——每次刷新都对比解析后的段,避免无意义的重写和文件监听回环;
264
+ - **用路径操作写**——你的其它 provider 一个字段都不会动;
265
+ - **探测失败就不写**——网关临时不可达时保留已经生效的目录,而不是清空;
266
+ - **带 revision 写**——与其它写入者(比如模型页)冲突时重读一次再写;
267
+ - **路由没模型了就删掉**——避免留下指向旧目录的空路由。
268
+
269
+ `/aperture` 里说 `设置:未写入(已处于同步状态)` 是正常状态。
270
+
271
+ ## 开发
272
+
273
+ ```sh
274
+ npm install # 若机器级 npm 缓存不可写:npm install --cache ./.npm-cache --ignore-scripts
275
+ npm run build # tsc -> lib/
276
+ npm run typecheck # 含 test/
277
+ npm test # 纯函数单测(93 个,离线,不需要 node_modules)
278
+ npm run test:live # 端到端:真实 DSH 栈 + 真实网关
279
+ npm run inspect # 打印真实生成的 settings.yaml 与解析结果
280
+ ```
281
+
282
+ `npm run test:live` 需要网关地址:
283
+
284
+ ```sh
285
+ DSH_APERTURE_LIVE_URL=https://ai.example.ts.net npm run test:live
286
+ ```
287
+
288
+ 它会拉起真实的 `dsh-settings-file` + `dsh-llm` + `dsh-llm-pi-ai` + 本插件,跑一次真实发现,然后断言:设置文件写对了、两条路由注册了、`ctx.llm.listModels()` 能看到模型、容量与推理档位与预期一致。这是唯一能证明“写进去的东西适配器真的收”的测试。
289
+
290
+ > `lib/` 是构建产物:git 安装与 `npm publish` 都由 `prepare` 脚本现场编译。
291
+ > 而从本地目录安装(`link:`)不会跑 `prepare`,所以本地调试前先 `npm run build`。
292
+
293
+ ## 已知边界
294
+
295
+ - **不转换 API 格式**,这是设计目标而不是缺陷。也因此只支持 `llm-pi-ai` 讲得了的两种协议;Gemini 原生端点接不了。
296
+ - `supported_endpoints` 是唯一的协议依据。网关如果不报,就按 OpenAI 兼容处理。
297
+ - models.dev 是尽力而为的补全:拉不到就是拉不到,发现本身照常成功。
298
+ - 本插件不注册任何 provider 目录(`registerConfigurableProviders`)——`llm-pi-ai` 已经认领了那件事,重复注册会抛错。
299
+ - **改了 `route` / `anthropicRoute` 的路由名之后,旧键会留在 `llm-pi-ai.providers` 里**(插件只认自己当前拥有的两个键,无法知道历史上用过哪些名字)。它不会报错,只是不再刷新;要清理就手动删掉那一行。
300
+
301
+ ## License
302
+
303
+ MIT