@tscgoods/claude-code-effort-discovery 0.4.0 → 0.4.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
@@ -29,6 +29,62 @@ Claude Code 2.1.x 里 effort 是 `low / medium / high / xhigh / max`(客户端
29
29
 
30
30
  没配置也能用:插件会退回读 `~/.claude/settings.json` 的 `env.ANTHROPIC_BASE_URL` / `ANTHROPIC_AUTH_TOKEN` 和 `modelPicker.options` 里的模型别名。
31
31
 
32
+ ## 网关别名映射(重要)
33
+
34
+ Claude 的网关别名(如 `ccs-claude-newapi--deepseek-v4.1-flash-1017[1m]`)通常**不校验** `output_config.effort`——传 `bogus` 也返回 200。这时插件只能诚实报 `no-validation`,不会凭空写档位。
35
+
36
+ 如果你在**另一个 OpenAI 兼容网关**上已经探测到同一模型的真实档位,可以在共享配置里建立别名映射,让会话启动时注入真相:
37
+
38
+ ```jsonc
39
+ // ~/.config/effort-discovery.json
40
+ {
41
+ "aliases": {
42
+ "ccs-claude-newapi--deepseek-v4.1-flash-1017[1m]": "deepseek-v4.1-flash-1017"
43
+ }
44
+ }
45
+ ```
46
+
47
+ 插件会读 `~/.cache/effort-discovery/` 下所有缓存(`cache.json` / `dsh.json` / `claude-code.json`),按别名找到实测档位,在 SessionStart 注入:
48
+
49
+ ```
50
+ ⚠ 不要选 medium / xhigh:Claude 的选择器仍会显示它们,但网关实测不支持/会忽略。
51
+ ```
52
+
53
+ **注意**:Claude Code 只有 `maxEffortLevel`(上限封顶)一个杠杆,没法隐藏中间档位;这个方案是"注入提醒",不改选择器。
54
+
55
+ ## 档位菜单的硬限制与取舍(v2.1.330)
56
+
57
+ Claude Code 的档位枚举固定为 `low / medium / high / xhigh / max`,选择器只返回它的**前缀**,再 optionally 用布尔开关删掉 `xhigh` / `max`。**没有配置项能删掉中间的 `medium`**;`*_SUPPORTED_CAPABILITIES` 环境变量在 `ANTHROPIC_BASE_URL` 网关场景下被客户端硬 gate 掉(实测无效)。因此 `{low, high, max}` 这种非连续集合在 Claude Code 里无法表达。
58
+
59
+ 三种可实现的折中:
60
+
61
+ | 方案 | 效果 | 代价 |
62
+ |---|---|---|
63
+ | **本插件的注入提示**(默认推荐) | 菜单不变,但每次会话明确告知实测档位与禁用项 | 需要你/模型遵守提示 |
64
+ | `modelPicker[].behavesAs: "claude-opus-4-6"` | 去掉 `xhigh`,剩 `low/medium/high/max` | `medium` 仍在;会连带继承该已知模型的其它能力/默认值 |
65
+ | `CLAUDE_CODE_EFFORT_LEVEL=auto` | 所有请求都不带 `output_config.effort`,绝不会发错值 | 完全失去档位控制 |
66
+
67
+ `behavesAs` 示例(只需改你自己的 `~/.claude/settings.json`):
68
+
69
+ ```json
70
+ {
71
+ "modelPicker": {
72
+ "replaceBuiltInOptions": true,
73
+ "options": [
74
+ {
75
+ "model": "ccs-claude-newapi--deepseek-v4.1-flash-1017[1m]",
76
+ "label": "deepseek-v4.1-flash-1017",
77
+ "behavesAs": "claude-opus-4-6"
78
+ }
79
+ ]
80
+ }
81
+ }
82
+ ```
83
+
84
+ 等价写法:`"modelOverrides": { "claude-opus-4-6": "ccs-claude-newapi--deepseek-v4.1-flash-1017[1m]" }`,然后用 `--model claude-opus-4-6` 启动。`modelOverrides` / `behavesAs` 确实验证过会导入已知模型的能力表(`xhigh` 会被降级成 `high`),但没有任何已知模型提供 `{low, high, max}`。
85
+
86
+ > 插件不会自动改 `settings.json` 或加 `behavesAs`——这属于你会话配置的取舍,需要你显式决定。
87
+
32
88
  ## 安装
33
89
 
34
90
  开发期(就地加载,改完 `/reload-plugins` 生效):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscgoods/claude-code-effort-discovery",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Claude Code plugin: discover which reasoning-effort levels your gateway model accepts, report them, and cap /effort to them.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/scripts/lib.mjs CHANGED
@@ -29,6 +29,57 @@ export function claudeSettingsPath() {
29
29
  return join(process.env.CLAUDE_CONFIG_DIR ?? join(homedir(), ".claude"), "settings.json")
30
30
  }
31
31
 
32
+ export const CLAUDE_LEVELS = ["low", "medium", "high", "xhigh", "max"]
33
+
34
+ /** 网关别名 → 真实模型 id 的映射(在 ~/.config/effort-discovery.json 的 aliases 里) */
35
+ export function loadAliases() {
36
+ const config = loadConfig()
37
+ const aliases = config.aliases
38
+ return aliases && typeof aliases === "object" && !Array.isArray(aliases) ? aliases : {}
39
+ }
40
+
41
+ /** 在所有缓存里查找某个模型的实测档位;优先有 levels 的证据 */
42
+ export function findProbe(modelID, aliases = {}) {
43
+ const canonical = aliases[modelID] ?? modelID
44
+ const dir = cacheDir()
45
+ const candidates = []
46
+
47
+ for (const file of ["cache.json", "dsh.json", "claude-code.json"]) {
48
+ const json = readJson(join(dir, file))
49
+ if (!json || typeof json === "object" && (json.owner || json.byProvider)) continue
50
+ const entries = json.models ?? json
51
+ if (!entries || typeof entries !== "object") continue
52
+ for (const [key, value] of Object.entries(entries)) {
53
+ if (!value || typeof value !== "object" || !("levels" in value)) continue
54
+ const match =
55
+ key === modelID ||
56
+ key === canonical ||
57
+ key.endsWith(`/${modelID}`) ||
58
+ key.endsWith(`/${canonical}`)
59
+ if (match) {
60
+ candidates.push({
61
+ from: key,
62
+ file,
63
+ source: value.source ?? "unknown",
64
+ levels: value.levels ?? null,
65
+ at: value.at ?? 0,
66
+ })
67
+ }
68
+ }
69
+ }
70
+
71
+ candidates.sort(
72
+ (a, b) => (b.levels?.length ?? 0) - (a.levels?.length ?? 0) || b.at - a.at,
73
+ )
74
+ return candidates[0]
75
+ }
76
+
77
+ /** 给出"实测支持"之外的 Claude 档位(即不该选的) */
78
+ export function unsupportedClaudeLevels(levels) {
79
+ if (!Array.isArray(levels) || levels.length === 0) return []
80
+ return CLAUDE_LEVELS.filter((level) => !levels.includes(level))
81
+ }
82
+
32
83
  export function readJson(path) {
33
84
  try {
34
85
  return JSON.parse(readFileSync(path, "utf8"))
package/scripts/probe.mjs CHANGED
@@ -1,7 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
+ CLAUDE_LEVELS,
3
4
  cachePath,
4
5
  claudeGateway,
6
+ findProbe,
7
+ loadAliases,
5
8
  loadConfig,
6
9
  normalizeBaseURL,
7
10
  probeAnthropic,
@@ -64,6 +67,7 @@ if (models.length === 0) {
64
67
  }
65
68
 
66
69
  const result = { at: Date.now(), provider: providerID, baseURL, models: {} }
70
+ const aliases = loadAliases()
67
71
  for (const model of models) {
68
72
  const probed = await probeAnthropic({
69
73
  baseURL,
@@ -71,7 +75,17 @@ for (const model of models) {
71
75
  model,
72
76
  headers: typeof entry?.headers === "object" && entry.headers ? entry.headers : {},
73
77
  })
74
- result.models[model] = probed
78
+ const authoritative = findProbe(model, aliases)
79
+ result.models[model] = {
80
+ ...probed,
81
+ alias: aliases[model] ?? null,
82
+ authoritativeLevels: authoritative?.levels ?? null,
83
+ authoritativeFrom: authoritative?.from ?? null,
84
+ unsupportedInClaude:
85
+ probed.levels?.length > 0
86
+ ? CLAUDE_LEVELS.filter((level) => !probed.levels.includes(level))
87
+ : null,
88
+ }
75
89
  await new Promise((resolve) => setTimeout(resolve, 200))
76
90
  }
77
91
  writeJson(cachePath(), result)
@@ -1,47 +1,72 @@
1
1
  #!/usr/bin/env node
2
2
  import { readFileSync } from "node:fs"
3
- import { cachePath, readJson } from "./lib.mjs"
3
+ import {
4
+ CLAUDE_LEVELS,
5
+ cachePath,
6
+ findProbe,
7
+ loadAliases,
8
+ readJson,
9
+ unsupportedClaudeLevels,
10
+ } from "./lib.mjs"
4
11
 
5
- // SessionStart / PostModelSwitch hook: 只读缓存,不联网、不改文件
12
+ // SessionStart / PostModelSwitch hook:只读缓存,不联网、不改配置
6
13
  let input = {}
7
14
  try {
8
15
  const raw = readFileSync(0, "utf8")
9
16
  if (raw.trim().length > 0) input = JSON.parse(raw)
10
17
  } catch {}
11
18
 
12
- const cache = readJson(cachePath())
13
- if (!cache || !cache.models) process.exit(0)
14
-
15
- const currentModel =
16
- input?.model?.id ?? input?.to_model ?? input?.model_id ?? input?.requested_model ?? undefined
19
+ const aliases = loadAliases()
20
+ const modelName = input?.model?.id ?? input?.to_model ?? input?.model_id ?? input?.requested_model ?? ""
21
+ const probe = modelName ? findProbe(modelName, aliases) : undefined
17
22
 
18
- const entries = Object.entries(cache.models).filter(([, info]) => Array.isArray(info.levels))
19
- if (entries.length === 0) process.exit(0)
23
+ const lines = []
20
24
 
21
- const describe = ([model, info]) => `${model}: ${info.levels.join(" / ")} (${info.source})`
22
- const current = currentModel
23
- ? entries.find(([model]) => model === currentModel || currentModel.includes(model))
24
- : undefined
25
+ if (probe?.levels?.length) {
26
+ const supported = probe.levels.filter((level) => CLAUDE_LEVELS.includes(level))
27
+ const unsupported = unsupportedClaudeLevels(probe.levels)
28
+ const canonical = aliases[modelName]
29
+ lines.push(
30
+ `${modelName} 的网关实测档位: ${probe.levels.join(" / ")}(证据: ${probe.source}, ${probe.from})`,
31
+ )
32
+ if (canonical) lines.push(`网关别名映射: ${modelName} → ${canonical}`)
33
+ if (unsupported.length > 0) {
34
+ lines.push(
35
+ `⚠ 不要选 ${unsupported.join(" / ")}:Claude 的选择器仍会显示它们,但网关实测不支持/会忽略。`,
36
+ )
37
+ } else {
38
+ lines.push("实测档位覆盖 Claude 的全部选项,可以放心选择。")
39
+ }
40
+ } else {
41
+ // 没有证据:明确说明,而不是编造
42
+ lines.push(
43
+ `${modelName || "当前模型"}: 网关未给出可信的 effort 声明(no-validation),插件不改写档位。`,
44
+ )
45
+ if (probe) lines.push(`最近一次探测: ${probe.source}(${probe.from})`)
46
+ if (modelName && !aliases[modelName]) {
47
+ lines.push("提示: 可在 ~/.config/effort-discovery.json 的 aliases 里把该别名映射到真实模型 id。")
48
+ }
49
+ }
25
50
 
26
- const lines = current
27
- ? [`当前模型支持: ${describe(current)}`, `其他模型: ${entries.filter(([m]) => m !== current[0]).map(describe).join("; ") || "无"}`]
28
- : entries.map(describe)
29
-
30
- const summary = lines.join("\n").slice(0, 9000)
31
- const age = Math.round((Date.now() - (cache.at ?? Date.now())) / 60000)
51
+ const cache = readJson(cachePath())
52
+ if (cache?.models) {
53
+ const others = Object.entries(cache.models)
54
+ .filter(([model]) => model !== modelName)
55
+ .map(([model, info]) => `${model}: ${(info.levels ?? []).join(" ") || "unknown"}`)
56
+ if (others.length > 0) lines.push(`其他模型缓存: ${others.join("; ")}`)
57
+ }
32
58
 
59
+ const age = probe?.at ? Math.round((Date.now() - probe.at) / 60000) : null
33
60
  const message =
34
- `effort-discovery: ${current ? `${current[0]} 支持 ${(current[1].levels ?? []).join(", ")}` : "已缓存模型档位"}` +
35
- `(${age} 分钟前探测;/effort-discovery:probe-effort 可刷新)`
61
+ `effort-discovery${age !== null ? `(${age} 分钟前探测)` : ""}:\n${lines.join("\n")}\n` +
62
+ `刷新请运行 /effort-discovery:probe-effort。`
36
63
 
37
64
  process.stdout.write(
38
65
  JSON.stringify({
39
- systemMessage: message,
66
+ systemMessage: message.slice(0, 1000),
40
67
  hookSpecificOutput: {
41
68
  hookEventName: input?.hook_event_name ?? "SessionStart",
42
- additionalContext:
43
- `网关的 reasoning effort 探测结果(缓存 ${age} 分钟前):\n${summary}\n` +
44
- `选择档位时只使用列出的级别;未列出的级别会被网关拒绝或忽略。`,
69
+ additionalContext: `网关 effort 实测结果:\n${lines.join("\n")}`,
45
70
  },
46
71
  }) + "\n",
47
72
  )