@tscgoods/dsh-effort-discovery 0.3.2 → 0.5.4

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,15 +38,16 @@ 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`。同一网关可能对不同的非法值返回不同列表(模型自己的 `none/low/high/max`,或网关通用的全档位列表);探针会收集多次响应,取更窄且能交叉验证的子集。启动/配置更新时,只有 provider 名、baseURL、`openai-completions` 协议和模型 id 均匹配,且实测缓存未超过 TTL 时才变换;否则保留原配置。
42
42
 
43
43
  ## 行为
44
44
 
45
45
  | 情况 | 结果 |
46
46
  | --- | --- |
47
- | 探测到合法档位 | `reasoningEfforts` 只保留实测档位(`none` → `off: null`),沿用原有 wire 值 |
47
+ | 探测到具体档位(少于 6 个) | `reasoningEfforts` 只保留实测档位(`none` → `off: null`),沿用原有 wire 值 |
48
+ | 网关回显全档位/过宽列表 | 视为不可靠,不扩菜单,保留原配置 |
48
49
  | 探测到只有 `none` | `reasoningEfforts: false`(当作非推理模型,菜单不再出现 Effort) |
49
- | 没探测结果 / `unknown` | 不动原配置 |
50
+ | 没探测结果 / `unknown` / 超过 TTL / 路由不匹配 | 不动原配置 |
50
51
  | `mode: "off"` | 完全不改配置,只保留 `reasoning_effort_probe` 工具 |
51
52
 
52
53
  配置项(`cordis.patch.yml` 里那一行的 `config`):
@@ -62,7 +63,7 @@ ttlMs: 21600000
62
63
 
63
64
  ## 风险与验证
64
65
 
65
- - 插件用 Cordis 的 `internal/config` waterfall 改写 `@deepseek-ai/dsh-llm-pi-ai` 这一行的 `providers`。这是 rc 版本里的内部事件,升级 dsh 后如果行为变了,请先 `--dump-config` 对照。
66
+ - 插件用 Cordis 的 `internal/config` waterfall 改写 `@deepseek-ai/dsh-llm-pi-ai` 的 `providers`,这是内部事件。0.2.3-alpha.3 的 `--dump-config` 只确认旧包存在,新代码尚需在隔离 profile 里验证宿主内实际变换。
66
67
  - 插件注册在 `llm-pi-ai` 之后(insert 追加到末尾),所以它的变换结果会参与该行的配置校验;如果未来有 bundle 插到它前面,顺序假设需要重新确认。
67
68
  - 收窄档位对已保存的会话有影响:如果会话固定了一个被移除的档位,下一步会报 `UNSUPPORTED_REASONING_EFFORT`,重新选一次档位即可。
68
- - 不想让插件改配置时,把 `mode` 设为 `off`,只用探测脚本 + 手工改 `cordis.patch.yml`。
69
+ - 不想让插件改配置时,把 `mode` 设为 `off`,只用探测脚本 + 手工改 `cordis.patch.yml`。
package/lib/index.js CHANGED
@@ -16,6 +16,8 @@ export const Config = z.object({
16
16
 
17
17
  const TARGET_ADAPTER = "dsh-llm-pi-ai"
18
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(/\/+$/, "") : ""
19
21
 
20
22
  function cacheDir() {
21
23
  return (
@@ -52,6 +54,8 @@ function readCache() {
52
54
  */
53
55
  export function narrowEfforts(model, levels) {
54
56
  if (!Array.isArray(levels) || levels.length === 0) return undefined
57
+ // 通用网关常把所有档位都列为“合法”;过宽的列表不足以作为菜单依据。
58
+ if (levels.length >= 6) return undefined
55
59
  const current = model?.reasoningEfforts
56
60
  // current === false 往往只是 YAML 里没配/手写了“不推理”,不是上游证据。
57
61
  // 既然探测到了档位,就以探测结果为准重新生成 map。
@@ -72,19 +76,26 @@ export function narrowEfforts(model, levels) {
72
76
  return Object.keys(next).filter((level) => level !== "off").length > 0 ? next : false
73
77
  }
74
78
 
75
- function transformProviders(raw, cache) {
79
+ export function transformProviders(raw, cache, ttlMs = DEFAULT_TTL_MS, now = Date.now()) {
76
80
  if (!raw || typeof raw !== "object" || !raw.providers || typeof raw.providers !== "object") {
77
81
  return raw
78
82
  }
83
+ if (typeof cache?.provider !== "string" || !cache.provider) return raw
79
84
  const providers = { ...raw.providers }
80
85
  let changed = false
81
86
 
82
87
  for (const [route, profile] of Object.entries(providers)) {
88
+ // A model id alone does not identify a gateway or its protocol.
89
+ if (route !== cache.provider) continue
83
90
  if (!profile || typeof profile !== "object" || !Array.isArray(profile.models)) continue
91
+ if (!cache.baseURL || !profile.baseURL || normalizeURL(cache.baseURL) !== normalizeURL(profile.baseURL)) continue
92
+ if (profile.api !== "openai-completions") continue
84
93
  const models = profile.models.map((model) => {
85
94
  if (!model || typeof model.id !== "string") return model
86
95
  const info = cache?.models?.[model.id]
87
- if (!info || !Array.isArray(info.levels)) return model
96
+ const at = info?.at ?? cache.at
97
+ if (info?.source !== "validated" || !Array.isArray(info.levels) ||
98
+ !Number.isFinite(at) || at > now || now - at >= ttlMs) return model
88
99
  const reasoningEfforts = narrowEfforts(model, info.levels)
89
100
  if (reasoningEfforts === undefined) return model
90
101
  changed = true
@@ -146,7 +157,7 @@ export default {
146
157
  return raw
147
158
  }
148
159
  try {
149
- const next = transformProviders(raw, cache)
160
+ const next = transformProviders(raw, cache, config?.ttlMs ?? DEFAULT_TTL_MS)
150
161
  if (config?.diag) {
151
162
  const before = []
152
163
  for (const profile of Object.values(raw?.providers ?? {})) {
@@ -171,4 +182,4 @@ export default {
171
182
 
172
183
  export function __diag() {
173
184
  return { cachePath: cachePath(), exists: existsSync(cachePath()) }
174
- }
185
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscgoods/dsh-effort-discovery",
3
- "version": "0.3.2",
3
+ "version": "0.5.4",
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
@@ -60,10 +60,22 @@ function parseLevels(text) {
60
60
  return null
61
61
  }
62
62
 
63
+ /** 通用网关会回显“所有已知档位”;只取更窄的、可交叉验证的子集 */
64
+ function pickLevels(candidates) {
65
+ const specific = candidates.filter((levels) => levels.length > 0 && levels.length < 6)
66
+ if (specific.length === 0) return null
67
+ let result = specific[0]
68
+ for (const levels of specific.slice(1)) {
69
+ result = result.filter((level) => levels.includes(level))
70
+ }
71
+ return result.length > 0 ? result : null
72
+ }
73
+
63
74
  async function probe({ baseURL, apiKey, model, headers = {} }) {
64
75
  const url = `${String(baseURL).replace(/\/+$/, "")}/chat/completions`
65
76
  let last = { levels: null, source: "error", at: Date.now() }
66
77
  let sawOk = false
78
+ const candidates = []
67
79
  for (const sentinel of ["effort-discovery-probe", "ultra", "maximum", "bogus"]) {
68
80
  let response
69
81
  try {
@@ -93,10 +105,16 @@ async function probe({ baseURL, apiKey, model, headers = {} }) {
93
105
  continue
94
106
  }
95
107
  const levels = parseLevels(text)
96
- if (levels) return { levels, source: "validated", at: Date.now() }
108
+ if (levels) {
109
+ candidates.push(levels)
110
+ continue
111
+ }
97
112
  last = { levels: null, source: `http-${response.status}`, at: Date.now() }
98
113
  if ([401, 403, 429].includes(response.status)) return last
99
114
  }
115
+ const levels = pickLevels(candidates)
116
+ if (levels) return { levels, source: "validated", at: Date.now() }
117
+ if (candidates.length > 0) return { levels: null, source: "no-validation", at: Date.now() }
100
118
  return sawOk ? { levels: null, source: "no-validation", at: Date.now() } : last
101
119
  }
102
120
 
@@ -136,6 +154,10 @@ if (!entry) {
136
154
  process.stderr.write("no provider: add one to ~/.config/effort-discovery.json\n")
137
155
  process.exit(2)
138
156
  }
157
+ if (entry.api && entry.api !== "openai") {
158
+ process.stderr.write(`provider "${providerID}" is not configured for OpenAI-compatible requests\n`)
159
+ process.exit(2)
160
+ }
139
161
 
140
162
  const models = args.models.length > 0 ? args.models : Array.isArray(entry.models) ? entry.models : []
141
163
  if (models.length === 0) {
@@ -173,4 +195,4 @@ if (args.json) {
173
195
  )
174
196
  }
175
197
  process.stdout.write(`\ncache: ${cachePath()}\n`)
176
- }
198
+ }