@tscgoods/dsh-effort-discovery 0.3.1 → 0.5.2

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
@@ -17,8 +17,8 @@ reasoningEfforts: { 'off': null, minimal: minimal, low: low, medium: medium, hig
17
17
  ## 安装
18
18
 
19
19
  ```powershell
20
- # 本地开发(pnpm link,改完重启 profile 生效)
21
- dsh plugin --profile web add C:\Users\蒋老师\projects\opencode-effort-discovery\packages\dsh
20
+ # 本地开发(在仓库根目录执行;pnpm link,改完重启 profile 生效)
21
+ dsh plugin --profile web add (Join-Path (Get-Location) 'packages\dsh')
22
22
 
23
23
  # 或从 npm
24
24
  dsh plugin --profile web add @tscgoods/dsh-effort-discovery
@@ -38,7 +38,7 @@ dsh --profile web --dump-config | Select-String effort-discovery
38
38
  node packages\dsh\scripts\probe.mjs --provider ts-dpsk
39
39
  ```
40
40
 
41
- 结果写到 `~/.cache/effort-discovery/dsh.json`。插件在启动/配置更新时读这份缓存,按模型 id 匹配。
41
+ 结果写到 `~/.cache/effort-discovery/dsh.json`。启动/配置更新时,只有 provider 名、baseURL、`openai-completions` 协议和模型 id 均匹配,且实测缓存未超过 TTL 时才变换;否则保留原配置。
42
42
 
43
43
  ## 行为
44
44
 
@@ -46,7 +46,7 @@ node packages\dsh\scripts\probe.mjs --provider ts-dpsk
46
46
  | --- | --- |
47
47
  | 探测到合法档位 | `reasoningEfforts` 只保留实测档位(`none` → `off: null`),沿用原有 wire 值 |
48
48
  | 探测到只有 `none` | `reasoningEfforts: false`(当作非推理模型,菜单不再出现 Effort) |
49
- | 没探测结果 / `unknown` | 不动原配置 |
49
+ | 没探测结果 / `unknown` / 超过 TTL / 路由不匹配 | 不动原配置 |
50
50
  | `mode: "off"` | 完全不改配置,只保留 `reasoning_effort_probe` 工具 |
51
51
 
52
52
  配置项(`cordis.patch.yml` 里那一行的 `config`):
@@ -62,7 +62,7 @@ ttlMs: 21600000
62
62
 
63
63
  ## 风险与验证
64
64
 
65
- - 插件用 Cordis 的 `internal/config` waterfall 改写 `@deepseek-ai/dsh-llm-pi-ai` 这一行的 `providers`。这是 rc 版本里的内部事件,升级 dsh 后如果行为变了,请先 `--dump-config` 对照。
65
+ - 插件用 Cordis 的 `internal/config` waterfall 改写 `@deepseek-ai/dsh-llm-pi-ai` 的 `providers`,这是内部事件。0.2.3-alpha.3 的 `--dump-config` 只确认旧包存在,新代码尚需在隔离 profile 里验证宿主内实际变换。
66
66
  - 插件注册在 `llm-pi-ai` 之后(insert 追加到末尾),所以它的变换结果会参与该行的配置校验;如果未来有 bundle 插到它前面,顺序假设需要重新确认。
67
67
  - 收窄档位对已保存的会话有影响:如果会话固定了一个被移除的档位,下一步会报 `UNSUPPORTED_REASONING_EFFORT`,重新选一次档位即可。
68
- - 不想让插件改配置时,把 `mode` 设为 `off`,只用探测脚本 + 手工改 `cordis.patch.yml`。
68
+ - 不想让插件改配置时,把 `mode` 设为 `off`,只用探测脚本 + 手工改 `cordis.patch.yml`。
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { readFileSync, existsSync } from "node:fs"
1
+ import { readFileSync, existsSync, appendFileSync } from "node:fs"
2
2
  import { homedir } from "node:os"
3
3
  import { join } from "node:path"
4
4
  import z from "@deepseek-ai/schemastery"
@@ -10,16 +10,30 @@ export const Config = z.object({
10
10
  mode: z.union([z.const("transform"), z.const("off")]).default("transform"),
11
11
  /** 探测缓存有效期(毫秒) */
12
12
  ttlMs: z.number().default(6 * 60 * 60 * 1000).volatile(),
13
+ /** 把每次配置变换的输入/输出写到 ~/.cache/effort-discovery/dsh-transform.log(排障用) */
14
+ diag: z.boolean().default(false).volatile(),
13
15
  })
14
16
 
15
- const TARGET_ADAPTER = "@deepseek-ai/dsh-llm-pi-ai"
17
+ const TARGET_ADAPTER = "dsh-llm-pi-ai"
16
18
  const LEVELS = ["minimal", "low", "medium", "high", "xhigh", "max"]
19
+ const DEFAULT_TTL_MS = 6 * 60 * 60 * 1000
20
+ const normalizeURL = (value) => typeof value === "string" ? value.replace(/\/+$/, "") : ""
17
21
 
18
- function cachePath() {
19
- const dir =
22
+ function cacheDir() {
23
+ return (
20
24
  process.env.EFFORT_DISCOVERY_HOME ??
21
25
  join(process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "effort-discovery")
22
- return join(dir, "dsh.json")
26
+ )
27
+ }
28
+
29
+ function cachePath() {
30
+ return join(cacheDir(), "dsh.json")
31
+ }
32
+
33
+ function diag(line) {
34
+ try {
35
+ appendFileSync(join(cacheDir(), "dsh-transform.log"), new Date().toISOString() + " " + line + "\n")
36
+ } catch {}
23
37
  }
24
38
 
25
39
  function readCache() {
@@ -41,8 +55,8 @@ function readCache() {
41
55
  export function narrowEfforts(model, levels) {
42
56
  if (!Array.isArray(levels) || levels.length === 0) return undefined
43
57
  const current = model?.reasoningEfforts
44
- if (current === false) return false
45
-
58
+ // current === false 往往只是 YAML 里没配/手写了“不推理”,不是上游证据。
59
+ // 既然探测到了档位,就以探测结果为准重新生成 map。
46
60
  const declared = current && typeof current === "object" ? current : {}
47
61
  const next = {}
48
62
  const accepted = new Set(levels)
@@ -60,19 +74,26 @@ export function narrowEfforts(model, levels) {
60
74
  return Object.keys(next).filter((level) => level !== "off").length > 0 ? next : false
61
75
  }
62
76
 
63
- function transformProviders(raw, cache) {
77
+ export function transformProviders(raw, cache, ttlMs = DEFAULT_TTL_MS, now = Date.now()) {
64
78
  if (!raw || typeof raw !== "object" || !raw.providers || typeof raw.providers !== "object") {
65
79
  return raw
66
80
  }
81
+ if (typeof cache?.provider !== "string" || !cache.provider) return raw
67
82
  const providers = { ...raw.providers }
68
83
  let changed = false
69
84
 
70
85
  for (const [route, profile] of Object.entries(providers)) {
86
+ // A model id alone does not identify a gateway or its protocol.
87
+ if (route !== cache.provider) continue
71
88
  if (!profile || typeof profile !== "object" || !Array.isArray(profile.models)) continue
89
+ if (!cache.baseURL || !profile.baseURL || normalizeURL(cache.baseURL) !== normalizeURL(profile.baseURL)) continue
90
+ if (profile.api !== "openai-completions") continue
72
91
  const models = profile.models.map((model) => {
73
92
  if (!model || typeof model.id !== "string") return model
74
93
  const info = cache?.models?.[model.id]
75
- if (!info || !Array.isArray(info.levels)) return model
94
+ const at = info?.at ?? cache.at
95
+ if (info?.source !== "validated" || !Array.isArray(info.levels) ||
96
+ !Number.isFinite(at) || at > now || now - at >= ttlMs) return model
76
97
  const reasoningEfforts = narrowEfforts(model, info.levels)
77
98
  if (reasoningEfforts === undefined) return model
78
99
  changed = true
@@ -126,12 +147,29 @@ export default {
126
147
  "internal/config",
127
148
  (fiber, next) => {
128
149
  const raw = next()
129
- if (fiber?.entry?.options?.name !== TARGET_ADAPTER) return raw
150
+ const entryName = String(fiber?.entry?.options?.name ?? "")
151
+ if (!entryName.includes(TARGET_ADAPTER)) return raw
130
152
  const cache = readCache()
131
- if (!cache) return raw
153
+ if (!cache) {
154
+ if (config?.diag) diag(`no cache at ${cachePath()}`)
155
+ return raw
156
+ }
132
157
  try {
133
- return transformProviders(raw, cache)
134
- } catch {
158
+ const next = transformProviders(raw, cache, config?.ttlMs ?? DEFAULT_TTL_MS)
159
+ if (config?.diag) {
160
+ const before = []
161
+ for (const profile of Object.values(raw?.providers ?? {})) {
162
+ for (const model of profile?.models ?? []) before.push(`${model.id}=${JSON.stringify(model.reasoningEfforts)}`)
163
+ }
164
+ const after = []
165
+ for (const profile of Object.values(next?.providers ?? {})) {
166
+ for (const model of profile?.models ?? []) after.push(`${model.id}=${JSON.stringify(model.reasoningEfforts)}`)
167
+ }
168
+ diag(`entry=${entryName}\n before: ${before.join(" | ")}\n after: ${after.join(" | ")}`)
169
+ }
170
+ return next
171
+ } catch (error) {
172
+ if (config?.diag) diag(`transform failed: ${String(error)}`)
135
173
  return raw
136
174
  }
137
175
  },
@@ -142,4 +180,4 @@ export default {
142
180
 
143
181
  export function __diag() {
144
182
  return { cachePath: cachePath(), exists: existsSync(cachePath()) }
145
- }
183
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscgoods/dsh-effort-discovery",
3
- "version": "0.3.1",
3
+ "version": "0.5.2",
4
4
  "description": "DeepSeek Harness (dsh) bundle: probe the gateway for the reasoning-effort levels each model accepts and narrow llm-pi-ai reasoningEfforts to them.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -34,7 +34,7 @@
34
34
  "access": "public"
35
35
  },
36
36
  "engines": {
37
- "dsh": ">=0.2.2-rc.1 <0.3.0",
37
+ "dsh": ">=0.2.2-rc.1 <0.3.0 || >=0.2.3-alpha.3 <0.2.3",
38
38
  "node": ">=20"
39
39
  },
40
40
  "dsh": {
@@ -46,8 +46,8 @@
46
46
  "@deepseek-ai/schemastery": "~3.19.0"
47
47
  },
48
48
  "peerDependencies": {
49
- "@deepseek-ai/cordis": "~4.1.0",
50
- "@deepseek-ai/dsh-llm": ">=0.2.2-rc.1 <0.3.0",
51
- "@deepseek-ai/dsh-tools": ">=0.2.2-rc.1 <0.3.0"
49
+ "@deepseek-ai/cordis": "~4.1.0 || ~4.2.0-alpha.2",
50
+ "@deepseek-ai/dsh-llm": ">=0.2.2-rc.1 <0.3.0 || >=0.2.3-alpha.3 <0.2.3",
51
+ "@deepseek-ai/dsh-tools": ">=0.2.2-rc.1 <0.3.0 || >=0.2.3-alpha.3 <0.2.3"
52
52
  }
53
53
  }
package/scripts/probe.mjs CHANGED
@@ -136,6 +136,10 @@ if (!entry) {
136
136
  process.stderr.write("no provider: add one to ~/.config/effort-discovery.json\n")
137
137
  process.exit(2)
138
138
  }
139
+ if (entry.api && entry.api !== "openai") {
140
+ process.stderr.write(`provider "${providerID}" is not configured for OpenAI-compatible requests\n`)
141
+ process.exit(2)
142
+ }
139
143
 
140
144
  const models = args.models.length > 0 ? args.models : Array.isArray(entry.models) ? entry.models : []
141
145
  if (models.length === 0) {
@@ -173,4 +177,4 @@ if (args.json) {
173
177
  )
174
178
  }
175
179
  process.stdout.write(`\ncache: ${cachePath()}\n`)
176
- }
180
+ }