dsh-thinking-levels 4.4.17 → 4.4.18

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.4.18
4
+
5
+ - **修复 issue #10(同时覆盖 #9 的形态):与第三方 adapter 包装器同开时 `resolveModel` 互递归爆栈 / 改写报错。**
6
+ 旧实现把 `adapter.resolveModel = wrapper` 直接写在第三方拥有的 adapter 对象上,遇到两种真实插件即失效:
7
+ - `dsh-providers-hub` 用**只有 `get` 陷阱**的 Proxy 包装 adapter,其 `resolveModel` 包装器虽缓存但**每次调用重读** `target.resolveModel`。我们的赋值穿透 Proxy 写到裸 adapter,于是 hub 包装器调到我们的包装器、我们的包装器又调回它捕获的 hub 包装器 → 无限互递归(`RangeError: Maximum call stack size exceeded`,issue #10)。
8
+ - `dsh-vision-router` 用**只读** Proxy 包装(`getOwnPropertyDescriptor` 一律报 `writable: false`),赋值在严格模式下抛 `TypeError: Cannot redefine property: resolveModel`(issue #9)。
9
+ 现改为**替换 `llm.adapters` 的注册表条目**(宿主每次调用都即时 `adapters.get(provider)`,无跨调用缓存,故替换对宿主透明),不再向第三方对象写入任何属性。新门面是普通对象而非 Proxy,八个宿主调用成员(`providerInfo` / `providerRetryPolicy` / `imageRequestPricing` / `imageRequestTarget` / `listModels` / `resolveModel` / `prepareCall` / `stream`)全部透传且 `this` 绑定为原 adapter;同一 adapter 服务多个 provider 时共用一份门面(宿主 `forAdapter` 按身份比较决定 replay state 是否保留)。
10
+ #4 的幂等语义保留(同一 inner adapter 只包一次),Off/On 折叠、`low` 门控、非推理模型不动等行为逐字不变。新增 `tests/adapter-facade.spec.ts`(9 例)覆盖两种 Proxy 形态、透传完整性、`this` 绑定、多 provider 身份一致、重跑不套娃、第三方换新实例后重新包装。
11
+
3
12
  ## 4.4.16
4
13
 
5
14
  - i18n: add Portuguese (pt-BR) locale — runtime dictionary, package metadata and README/INSTALL/CHANGELOG mirrors.
package/dsh.plugin.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "dsh-external/dsh-thinking-levels",
3
- "version": "4.4.17",
3
+ "version": "4.4.18",
4
4
  "main": "./lib/index.js",
5
5
  "description": "thinking-level (reasoning_effort) control — auto-adjust per tool round, with optional manual lock and per-tool timing telemetry.",
6
6
  "engines": {
package/lib/index.d.ts CHANGED
@@ -218,16 +218,34 @@ declare module '@deepseek-ai/cordis' {
218
218
  export declare function readVolatile<T>(value: T | Volatile<T> | undefined, fallback: T): T;
219
219
  /** Minimal face of the llm service's adapter registrations (typed locally to avoid a host-package value import). */
220
220
  interface LlmRegistration {
221
- adapter: {
222
- resolveModel: (provider: string, model: string, signal?: unknown) => Promise<{
223
- reasoning?: {
224
- efforts?: {
225
- id: string;
226
- name: string;
227
- }[];
228
- };
229
- }>;
230
- };
221
+ adapter: AdapterLike;
222
+ }
223
+ /**
224
+ * The adapter members the host actually calls (`@deepseek-ai/dsh-llm`:
225
+ * `providerInfo` / `providerRetryPolicy` / `imageRequestPricing` /
226
+ * `imageRequestTarget` / `listModels` / `resolveModel` / `prepareCall` /
227
+ * `stream`, per the `LlmAdapter` declaration in dsh-tool-cordis' API catalog).
228
+ *
229
+ * The facade must forward ALL of them: dropping one silently breaks that
230
+ * provider's feature (e.g. a missing `imageRequestTarget` fails image
231
+ * requests). `resolveModel` is the only member we rewrite.
232
+ */
233
+ interface AdapterLike {
234
+ providerInfo?: (provider: string) => unknown;
235
+ providerRetryPolicy?: (provider: string) => unknown;
236
+ imageRequestPricing?: (provider: string, model: string) => unknown;
237
+ imageRequestTarget?: (provider: string, model: string, image: unknown) => unknown;
238
+ listModels?: (provider: string) => Promise<readonly unknown[]>;
239
+ resolveModel: (provider: string, model: string, signal?: unknown) => Promise<{
240
+ reasoning?: {
241
+ efforts?: {
242
+ id: string;
243
+ name: string;
244
+ }[];
245
+ };
246
+ }>;
247
+ prepareCall?: (provider: string, model: string, signal?: unknown) => Promise<unknown>;
248
+ stream?: (options: unknown) => unknown;
231
249
  }
232
250
  /**
233
251
  * Advertise `auto` (and, for configurer-confirmed rc.6-era models, `low`) in
package/lib/index.js CHANGED
@@ -116,14 +116,91 @@ const AUTO_EFFORT = { id: 'auto', name: 'Auto' };
116
116
  */
117
117
  const LOW_EFFORT = { id: 'low', name: 'Low' };
118
118
  /**
119
- * Adapters whose `resolveModel` is already wrapped — the per-adapter
119
+ * Marks a TL-built facade, so a re-run of the advertisement can tell "this
120
+ * registration already carries my facade" from "this is a third party's
121
+ * wrapper". Not enumerable: it must never leak into a `for...in` /
122
+ * `Object.keys` sweep a host or plugin performs on the adapter.
123
+ */
124
+ const TL_FACADE = Symbol('dsh-thinking-levels/adapter-facade');
125
+ /** Every member name the facade must forward; `resolveModel` is handled separately. */
126
+ const FORWARDED_ADAPTER_MEMBERS = [
127
+ 'providerInfo',
128
+ 'providerRetryPolicy',
129
+ 'imageRequestPricing',
130
+ 'imageRequestTarget',
131
+ 'listModels',
132
+ 'prepareCall',
133
+ 'stream',
134
+ ];
135
+ /** Facade per inner adapter: one identity per adapter, however many providers it serves. */
136
+ const facades = new WeakMap();
137
+ /**
138
+ * Adapters whose `resolveModel` is already advertised — the per-adapter
120
139
  * idempotence issue #4 proves was missing: re-running the advertisement on
121
140
  * every `llm/adapters-updated` stacked a new wrapper per pass, and every
122
141
  * stacked layer re-ran the post-processing on each resolveModel call.
123
- * Keyed on the adapter object itself, so a genuinely new adapter is wrapped
124
- * once and retired ones need no cleanup.
142
+ *
143
+ * Keyed on the **inner** adapter (the one a third party registered), not on
144
+ * the facade we put into the registry — see {@link advertiseModelCapability}.
125
145
  */
126
146
  const advertisedAdapters = new WeakSet();
147
+ /**
148
+ * Build a forwarding facade over `inner` that rewrites `resolveModel`'s
149
+ * result. Issue #10 (and its sibling #9) is why this exists at all.
150
+ *
151
+ * ## Why a facade instead of assigning onto `adapter.resolveModel`
152
+ *
153
+ * The old implementation did `adapter.resolveModel = wrapper`, which is an
154
+ * in-place mutation of an object a THIRD PARTY owns. Two real plugins turn
155
+ * that into a failure:
156
+ *
157
+ * - `dsh-providers-hub` wraps the adapter in a Proxy whose `get` trap returns
158
+ * a CACHED `resolveModel` wrapper that nevertheless re-reads
159
+ * `target.resolveModel` on every call, and which has **no `set` trap**. Our
160
+ * assignment wrote through to the bare adapter, so the hub wrapper called
161
+ * back into ours, which called the hub wrapper it had captured: unbounded
162
+ * mutual recursion (`RangeError: Maximum call stack size exceeded`, issue
163
+ * #10).
164
+ * - `dsh-vision-router` wraps the adapter in a read-only Proxy whose
165
+ * `getOwnPropertyDescriptor` reports `writable: false`, so the assignment
166
+ * throws `TypeError: Cannot redefine property: resolveModel` (issue #9).
167
+ *
168
+ * A facade writes nothing to the third party's object, so neither shape can
169
+ * fire. We instead replace the **registry entry** — and the host reads
170
+ * `adapters.get(provider)` fresh on every call (`dsh-llm` has no cached
171
+ * adapter reference across calls), so the swap is transparent to it.
172
+ *
173
+ * ## Why a plain object and not a Proxy
174
+ *
175
+ * The Proxy behaviours above are exactly the trap semantics we want to stay
176
+ * away from. A plain object also keeps `this` honest: every forwarded member
177
+ * is bound to `inner`, never to the facade (a facade-bound `this` would make
178
+ * an adapter's internal `this.stream` call loop back through the facade).
179
+ * @param inner - the adapter a third party registered; never mutated.
180
+ * @param postProcess - the capability advertisement applied to each result.
181
+ * @returns the facade to place in the registry entry.
182
+ */
183
+ function adapterFacade(inner, postProcess) {
184
+ const facade = {
185
+ // Rewritten: resolve through the inner adapter, then advertise on the result.
186
+ async resolveModel(provider, model, signal) {
187
+ const info = await inner.resolveModel.call(inner, provider, model, signal);
188
+ return postProcess(info, provider, model);
189
+ },
190
+ };
191
+ // Forwarded: `this` MUST be `inner`, and the function identity must be stable
192
+ // (downstream code caches by function identity).
193
+ const forwarded = facade;
194
+ const source = inner;
195
+ for (const member of FORWARDED_ADAPTER_MEMBERS) {
196
+ const value = source[member];
197
+ if (typeof value !== 'function')
198
+ continue;
199
+ forwarded[member] = value.bind(inner);
200
+ }
201
+ Object.defineProperty(facade, TL_FACADE, { value: true, enumerable: false });
202
+ return facade;
203
+ }
127
204
  /**
128
205
  * Advertise `auto` (and, for configurer-confirmed rc.6-era models, `low`) in
129
206
  * the model directory: wrap every registered adapter's `resolveModel` so the
@@ -145,18 +222,36 @@ const advertisedAdapters = new WeakSet();
145
222
  * `installLegacySection`)
146
223
  */
147
224
  export function advertiseModelCapability(llm, overrideFor, piAiFor) {
148
- for (const registration of llm?.adapters?.values() ?? []) {
149
- const adapter = registration.adapter;
225
+ const adapters = llm?.adapters;
226
+ if (adapters === undefined)
227
+ return;
228
+ // Snapshot the entries first: the loop REPLACES map entries, and a live
229
+ // iterator would then visit the replacements.
230
+ for (const [route, registration] of [...adapters]) {
231
+ const inner = registration.adapter;
232
+ // A registration already carrying our facade needs nothing (this is what a
233
+ // re-run sees). Checked before the WeakSet because the WeakSet is keyed on
234
+ // the INNER adapter, which the registry entry no longer points at.
235
+ if (inner[TL_FACADE] === true)
236
+ continue;
150
237
  // Issue #4: the re-run on every `llm/adapters-updated` must NOT stack
151
238
  // wrappers — each stacked layer re-ran the post-processing (and a full
152
- // describeSettings) on every resolveModel call. This WeakSet is the
153
- // idempotence the JSDoc has always promised.
154
- if (advertisedAdapters.has(adapter))
239
+ // describeSettings) on every resolveModel call. Keyed on the inner adapter
240
+ // so a third party's re-registration of the same instance is still
241
+ // recognised, while our own facade is not mistaken for a fresh adapter.
242
+ //
243
+ // NOTE: a cached facade must still be INSTALLED on this route — one adapter
244
+ // commonly serves several providers, and the first route's pass would
245
+ // otherwise leave the others pointing at the bare adapter (no `auto`).
246
+ const cachedFacade = facades.get(inner);
247
+ if (cachedFacade !== undefined) {
248
+ adapters.set(route, { ...registration, adapter: cachedFacade });
249
+ continue;
250
+ }
251
+ if (advertisedAdapters.has(inner))
155
252
  continue;
156
- advertisedAdapters.add(adapter);
157
- const original = adapter.resolveModel.bind(adapter);
158
- adapter.resolveModel = async (provider, model, signal) => {
159
- const info = await original(provider, model, signal);
253
+ advertisedAdapters.add(inner);
254
+ const postProcess = (info, provider, model) => {
160
255
  const reasoning = info.reasoning;
161
256
  if (reasoning === undefined)
162
257
  return info;
@@ -191,6 +286,21 @@ export function advertiseModelCapability(llm, overrideFor, piAiFor) {
191
286
  info.reasoning = { ...reasoning, efforts };
192
287
  return info;
193
288
  };
289
+ // Issue #10 / #9: replace the REGISTRY ENTRY rather than mutating the
290
+ // adapter a third party owns. See `adapterFacade` for why an in-place
291
+ // `adapter.resolveModel = ...` deadlocks against hub's write-through Proxy
292
+ // and throws against vision-router's read-only one.
293
+ //
294
+ // One facade per INNER adapter, shared across every route it serves: the
295
+ // host compares `adapters.get(provider).adapter` by identity to decide
296
+ // whether a message's replay state survives (`dsh-llm`'s `forAdapter`), so
297
+ // per-route facades would make the same adapter look like two different
298
+ // ones. The provider is passed per call, so one facade serves them all.
299
+ const existing = facades.get(inner);
300
+ const facade = existing ?? adapterFacade(inner, postProcess);
301
+ if (existing === undefined)
302
+ facades.set(inner, facade);
303
+ adapters.set(route, { ...registration, adapter: facade });
194
304
  }
195
305
  }
196
306
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-thinking-levels",
3
- "version": "4.4.17",
3
+ "version": "4.4.18",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org",
6
6
  "access": "public",