@tscgoods/codex-effort-discovery 0.5.1 → 0.5.5

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "effort-discovery",
3
- "version": "0.5.0",
4
- "description": "Probe the gateway for the reasoning-effort levels a model really accepts and generate a Codex model catalog that offers exactly those.",
3
+ "version": "0.5.5",
4
+ "description": "Inspect gateway reasoning-effort evidence and prepare a Codex model catalog.",
5
5
  "author": {
6
6
  "name": "tscgoods"
7
7
  },
@@ -27,4 +27,4 @@
27
27
  "Probe my gateway for the effort levels each model accepts"
28
28
  ]
29
29
  }
30
- }
30
+ }
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @tscgoods/codex-effort-discovery
2
2
 
3
- Codex CLI 插件:探测网关真正接受的 reasoning effort 档位,然后生成/更新一份 **model catalog**,让 Codex 的档位菜单只出现这些档位。
3
+ Codex CLI 插件:根据网关错误回显或原生 catalog,生成/更新一份 **model catalog**。需自行确认探针与 Codex 使用同一路由和协议。
4
4
 
5
5
  ## 为什么需要
6
6
 
@@ -34,13 +34,13 @@ node scripts/sync-catalog.mjs --write
34
34
  # model_catalog_json = 'C:\Users\<you>\.codex\model-catalogs\efforts.json'
35
35
  ```
36
36
 
37
- 插件还提供 `/efforts` 命令和 `effort-probe` 技能,让 agent 代跑这两步。
37
+ 插件提供 `effort-probe` 技能;`commands/efforts.md` 是兼容性参考文件,当前 Codex 插件文档没有将其列为支持的插件组件,不能保证 `/efforts` 可用。
38
38
 
39
39
  ## 防覆盖:guard.mjs
40
40
 
41
41
  cc-switch 之类的工具会**重新生成自己的 catalog 文件并把 config.toml 的指针改回去**,
42
42
  导致修好的档位被覆盖。`guard.mjs` 的思路是不跟它抢指针——不论 config 指向哪份文件,
43
- 直接**就地**把实测档位修补进那份文件本身(cc-switch 的文件也修),指针改不改都无所谓:
43
+ 直接**就地**把缓存档位修补进那份文件本身(cc-switch 的文件也修)。目前只检查用户级 `config.toml`,不解析 profile 等覆盖项,使用前应确认实际路径:
44
44
 
45
45
  ```powershell
46
46
  # 查看会修什么(dry-run)
@@ -64,16 +64,16 @@ node scripts/probe.mjs; node scripts/guard.mjs --write
64
64
 
65
65
  ## 哪个证据可信
66
66
 
67
- DeepSeek 官方文档(https://api-docs.deepseek.com/zh-cn/guides/thinking_mode)明确写着 `reasoning_effort` 只接受 `low / high / max`,`medium` 和 `xhigh` 都会被映射成 `high`;Anthropic 格式是 `none/low/high/max`。
67
+ 证据按网关和协议分别看待:同名模型在不同代理、不同协议下可能有不同处理。Codex 通常使用 Responses,脚本的主动探针使用 Chat Completions,**不能默认两者等价**。
68
68
 
69
- 但网关的原生 catalog(cc-switch 生成的 `/v1/models`)给 deepseek 标的是 `low/medium/high/xhigh`——**这个 catalog 是错的,而且漏了 max**,导致 Codex 菜单里选不到 max。
69
+ 某个网关的原生 catalog 与错误回显可能不一致;这并不能单独证明另一条协议路由的 catalog 错误。
70
70
 
71
71
  因此 `probe.mjs` 的默认优先级是:
72
72
 
73
- 1. **上游错误回显**(`/v1/chat/completions` 的非法值 400,反映实际接受的档位)→ `source: validated`;
73
+ 1. **上游错误回显**(`/v1/chat/completions` 的非法值错误,反映该路由的声明)→ `source: validated`;
74
74
  2. **原生 catalog** 兜底(错误回显拿不到时)→ `source: native-catalog`。
75
75
 
76
- `none` 会被过滤掉(Codex 的 `supported_reasoning_levels` 不接受它),最终 deepseek 得到 `low/high/max`。`--no-native` 保留作"只看错误回显"的诊断开关。若网关原生 catalog 与错误回显一致,两者结果相同,无副作用。
76
+ 脚本只写已知的普通档位,忽略 `none` 和未知值;没有可写档位时保留原菜单。旧版缓存和超过 6 小时的结果需要重新探测,配置的 provider 与 URL 不匹配时也不会写入。`--no-native` 只看错误回显;自动回显优先的结果写入前必须人工确认适用于 Codex 实际路由。模型 slug 只做精确匹配,不按同名后缀跨 provider 匹配。
77
77
 
78
78
  ## 更省事的替代
79
79
 
@@ -85,10 +85,10 @@ base_url = "http://127.0.0.1:7891/v1"
85
85
  model_catalog_url = "http://127.0.0.1:7891/v1/models" # 必须与 base_url 同源同端口
86
86
  ```
87
87
 
88
- 但本例中 cc-switch 的 catalog 是错的,所以要用插件生成并写入正确的 catalog。
88
+ cc-switch 等工具可能重新生成 catalog;写入前应先核对来源。
89
89
 
90
90
  ## 注意
91
91
 
92
92
  - Codex 只在启动时读 catalog,改完需要重开会话。
93
- - 只有 `ultra` 会被 Codex 重映射;其余值(包括非法值)会原样发给上游,上游 400 会直接变成 `turn.failed`。所以 catalog 写得准很关键。
94
- - `(unknown)` 的模型不会被写进 catalog,保留原值。
93
+ - 本机 0.172.0 的内置 catalog 包含 `ultra`(自动任务委派语义);脚本不会从网关回显中自动生成该项。
94
+ - `(unknown)` 的模型不会被写进 catalog,保留原值。
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tscgoods/codex-effort-discovery",
3
- "version": "0.5.1",
4
- "description": "Codex CLI plugin: probe which reasoning-effort levels your gateway accepts, then generate a model catalog that makes Codex offer exactly those.",
3
+ "version": "0.5.5",
4
+ "description": "Codex CLI plugin: inspect gateway effort evidence and update a model catalog after verifying the route.",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "files": [
package/scripts/guard.mjs CHANGED
@@ -2,6 +2,7 @@
2
2
  import { existsSync, statSync } from "node:fs"
3
3
  import {
4
4
  cachePath,
5
+ cacheMatchesProvider,
5
6
  catalogPath,
6
7
  codexHome,
7
8
  configCatalogTarget,
@@ -12,11 +13,11 @@ import {
12
13
 
13
14
  const HELP = `用法: node guard.mjs [--write] [--watch] [--json]
14
15
 
15
- 防止 cc-switch 等工具重新生成 catalog 后档位被覆盖回错误值。
16
+ 在确认缓存适用于当前路由后,修补 cc-switch 等工具重新生成的 catalog。
16
17
 
17
18
  原理:不论 config.toml 的 model_catalog_json 指向哪份文件,直接把
18
19
  ${cachePath()} 里的实测档位【就地修补】进那份文件本身(cc-switch 的文件也修),
19
- 这样指针被改来改去都无所谓,文件内容永远是对的。同时修补 ${catalogPath()} 副本。
20
+ 同时修补 ${catalogPath()} 副本。只读取 config.toml 中的路径,不能识别所有 profile 覆盖。
20
21
 
21
22
  (无参数) dry-run,只显示会修什么
22
23
  --write 实际修补一次
@@ -55,6 +56,7 @@ function repair({ write }) {
55
56
  if (!cache?.models) {
56
57
  return { error: `no cache at ${cachePath()} — run probe.mjs first` }
57
58
  }
59
+ if (!cacheMatchesProvider(cache)) return { error: "cached provider/URL differs from current configuration — run probe.mjs again" }
58
60
  const report = []
59
61
  for (const file of targetFiles()) {
60
62
  if (!existsSync(file)) {
@@ -84,7 +86,7 @@ function print(result, write) {
84
86
  continue
85
87
  }
86
88
  if (entry.changes.length === 0) {
87
- process.stdout.write(`ok ${entry.file} (already correct)\n`)
89
+ process.stdout.write(`skip ${entry.file} (no applicable changes; cache may be stale or unmatched)\n`)
88
90
  continue
89
91
  }
90
92
  for (const change of entry.changes) {
package/scripts/lib.mjs CHANGED
@@ -45,6 +45,13 @@ export function loadConfig() {
45
45
  return config && typeof config === "object" ? config : {}
46
46
  }
47
47
 
48
+ export function cacheMatchesProvider(cache) {
49
+ const entry = loadConfig().providers?.[cache?.provider]
50
+ const clean = (value) => typeof value === "string" ? value.replace(/\/+$/, "") : ""
51
+ return Boolean(entry && (!entry.api || entry.api === "openai") && clean(cache?.baseURL) &&
52
+ clean(cache.baseURL) === clean(entry.baseURL ?? entry.url))
53
+ }
54
+
48
55
  export function resolveSecret(value) {
49
56
  if (typeof value !== "string") return undefined
50
57
  const raw = value.trim()
@@ -79,10 +86,22 @@ export function parseLevels(text) {
79
86
  return null
80
87
  }
81
88
 
89
+ /** 通用网关可能回显全枚举;只有互相印证的窄子集才算模型级证据 */
90
+ export function pickLevels(candidates) {
91
+ const specific = candidates.filter((levels) => levels.length > 0 && levels.length < 6)
92
+ if (specific.length === 0) return null
93
+ let result = specific[0]
94
+ for (const levels of specific.slice(1)) {
95
+ result = result.filter((level) => levels.includes(level))
96
+ }
97
+ return result.length > 0 ? result : null
98
+ }
99
+
82
100
  export async function probeOpenAI({ baseURL, apiKey, model, headers = {}, timeoutMs = 20_000 }) {
83
101
  const url = `${String(baseURL).replace(/\/+$/, "")}/chat/completions`
84
102
  let last = { levels: null, source: "error", at: Date.now() }
85
103
  let sawOk = false
104
+ const candidates = []
86
105
  for (const sentinel of ["effort-discovery-probe", "ultra", "maximum", "bogus"]) {
87
106
  let response
88
107
  try {
@@ -109,10 +128,16 @@ export async function probeOpenAI({ baseURL, apiKey, model, headers = {}, timeou
109
128
  const text = await response.text()
110
129
  if (response.ok) { sawOk = true; continue }
111
130
  const levels = parseLevels(text)
112
- if (levels) return { levels, source: "validated", at: Date.now() }
131
+ if (levels) {
132
+ candidates.push(levels)
133
+ continue
134
+ }
113
135
  last = { levels: null, source: `http-${response.status}`, at: Date.now() }
114
136
  if ([401, 403, 429].includes(response.status)) return last
115
137
  }
138
+ const levels = pickLevels(candidates)
139
+ if (levels) return { levels, source: "validated", at: Date.now() }
140
+ if (candidates.length > 0) return { levels: null, source: "no-validation", at: Date.now() }
116
141
  return sawOk ? { levels: null, source: "no-validation", at: Date.now() } : last
117
142
  }
118
143
 
@@ -145,30 +170,20 @@ export async function nativeCatalog({ baseURL, apiKey, headers = {}, timeoutMs =
145
170
 
146
171
  /**
147
172
  * 把探测缓存里的正确档位修补进 catalog(就地修改传入对象)。
148
- * cacheModels 的键可能是带 provider 前缀的 slug(ccs-newapi/xxx),也可能是裸模型 id;
149
- * catalog slug 先精确匹配,再按 "/" 后缀退化匹配。
173
+ * 只按 slug 精确匹配;相同后缀可能代表不同 provider 或路由。
150
174
  * 返回变更列表。
151
175
  */
152
- export function patchCatalog(catalog, cacheModels) {
176
+ export function patchCatalog(catalog, cacheModels, now = Date.now()) {
153
177
  const changes = []
154
178
  if (!catalog?.models || !cacheModels) return changes
155
179
 
156
- const lookup = (slug) => {
157
- const exact = cacheModels[slug]
158
- if (exact) return exact
159
- const bare = String(slug).split("/").pop()
160
- // 裸 id 匹配:缓存键自身是裸 id,或者缓存键的后缀等于裸 id
161
- const candidates = Object.entries(cacheModels).filter(([key]) => {
162
- const tail = key.split("/").pop()
163
- return key === bare || tail === bare
164
- })
165
- return candidates.length === 1 ? candidates[0][1] : undefined
166
- }
167
-
168
180
  for (const model of catalog.models) {
169
- const info = lookup(model.slug)
170
- if (!info?.levels?.length) continue
171
- const desired = info.levels.filter((level) => level !== "off")
181
+ const info = cacheModels[model.slug]
182
+ if (info?.evidenceVersion !== 2 || !["validated", "native-catalog"].includes(info.source) || !info?.levels?.length ||
183
+ !Number.isFinite(info.at) || info.at > now || now - info.at >= 6 * 60 * 60 * 1000) continue
184
+ const desired = [...new Set(info.levels.filter((level) => CODEX_EFFORTS.includes(level)))]
185
+ // Unknown/non-representable values must never produce an invalid or empty catalog.
186
+ if (desired.length === 0) continue
172
187
  const before = (model.supported_reasoning_levels ?? []).map((item) =>
173
188
  typeof item === "string" ? item : item.effort,
174
189
  )
@@ -199,10 +214,9 @@ export function configCatalogTarget() {
199
214
 
200
215
  /** 读取当前生效的 catalog(codex debug models 优先,其次 config.toml 的 model_catalog_json) */
201
216
  export function currentCatalog() {
202
- const config = readFileSync(join(codexHome(), "config.toml"), "utf8")
203
- const match = /^\s*model_catalog_json\s*=\s*['"](.+?)['"]/m.exec(config)
204
- const fromConfig = match ? readJson(match[1].replace(/\\\\/g, "\\")) : undefined
205
- if (fromConfig?.models) return { catalog: fromConfig, source: match[1] }
217
+ const target = configCatalogTarget()
218
+ const fromConfig = target ? readJson(target) : undefined
219
+ if (fromConfig?.models) return { catalog: fromConfig, source: target }
206
220
 
207
221
  try {
208
222
  const output = execSync("codex debug models", { encoding: "utf8", timeout: 60_000, windowsHide: true })
@@ -211,4 +225,4 @@ export function currentCatalog() {
211
225
  } catch {}
212
226
 
213
227
  return undefined
214
- }
228
+ }
package/scripts/probe.mjs CHANGED
@@ -25,8 +25,9 @@ function parse(argv) {
25
25
 
26
26
  const HELP = `用法: node probe.mjs [--provider <id>] [--model <slug> ...] [--no-native] [--json]
27
27
 
28
- 1) 先尝试网关的原生 catalog(GET {baseURL}/models 里的 supported_reasoning_levels,零成本);
29
- 2) 没有就用非法 reasoning_effort 探测(每个模型最多 4 次极小请求)。
28
+ 1) 获取网关原生 catalog(GET {baseURL}/models 里的 supported_reasoning_levels);
29
+ 2) 对每个模型探测 Chat Completions 的非法 reasoning_effort(最多 4 次极小请求);
30
+ 错误回显优先,原生 catalog 兜底。Codex 使用 Responses,结果需要按实际路由核对。
30
31
  provider 来自 ${"~/.config/effort-discovery.json"},默认取第一个 openai provider。
31
32
  结果写入 ${cachePath()}。
32
33
  `
@@ -48,6 +49,10 @@ if (!entry) {
48
49
  process.stderr.write(`no provider: add one to ~/.config/effort-discovery.json\n`)
49
50
  process.exit(2)
50
51
  }
52
+ if (entry.api && entry.api !== "openai") {
53
+ process.stderr.write(`provider "${providerID}" is not configured for OpenAI-compatible requests\n`)
54
+ process.exit(2)
55
+ }
51
56
 
52
57
  const baseURL = entry.baseURL ?? entry.url
53
58
  const apiKey = resolveSecret(entry.apiKey)
@@ -67,10 +72,11 @@ for (const model of models) {
67
72
  // 上游错误回显优先(它反映实际接受的 effort),原生 catalog 兜底
68
73
  const levels = validated
69
74
  ? probed.levels.filter((level) => CODEX_EFFORTS.includes(level))
70
- : nativeLevels ?? null
75
+ : nativeLevels ? nativeLevels.filter((level) => CODEX_EFFORTS.includes(level)) : null
71
76
 
72
77
  result.models[model] = {
73
78
  levels,
79
+ evidenceVersion: 2,
74
80
  source: validated
75
81
  ? "validated"
76
82
  : nativeLevels
@@ -100,4 +106,4 @@ if (args.json) {
100
106
  row.map((cell, index) => String(cell ?? "").padEnd(widths[index])).join(" ") + "\n",
101
107
  )
102
108
  }
103
- }
109
+ }
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { catalogPath, cachePath, currentCatalog, patchCatalog, readJson, writeJson } from "./lib.mjs"
2
+ import { catalogPath, cacheMatchesProvider, cachePath, currentCatalog, patchCatalog, readJson, writeJson } from "./lib.mjs"
3
3
 
4
4
  function parse(argv) {
5
5
  const args = {}
@@ -34,6 +34,10 @@ if (!cache?.models) {
34
34
  process.stderr.write(`no cache at ${cachePath()} — run probe.mjs first\n`)
35
35
  process.exit(2)
36
36
  }
37
+ if (!cacheMatchesProvider(cache)) {
38
+ process.stderr.write("cached provider/URL differs from current configuration — run probe.mjs again\n")
39
+ process.exit(2)
40
+ }
37
41
 
38
42
  const current = currentCatalog()
39
43
  if (!current) {
@@ -58,4 +62,4 @@ if (!args.write) {
58
62
 
59
63
  writeJson(catalogPath(), catalog)
60
64
  process.stdout.write(`written: ${catalogPath()}\n`)
61
- process.stdout.write(`现在在 config.toml 里设置:\n model_catalog_json = '${catalogPath()}'\n`)
65
+ process.stdout.write(`现在在 config.toml 里设置:\n model_catalog_json = '${catalogPath()}'\n`)
@@ -9,14 +9,14 @@ Codex 的档位列表来自 **model catalog**(`model_catalog_json`),插件
9
9
 
10
10
  ## 步骤
11
11
 
12
- 1. 探测(先试网关原生 catalog,失败再发极小请求):
12
+ 1. 获取网关原生 catalog,并用 Chat Completions 发极小请求;Codex 的 Responses 路由可能不同:
13
13
 
14
14
  ```bash
15
15
  node "${PLUGIN_ROOT}/scripts/probe.mjs" --json
16
16
  ```
17
17
 
18
18
  2. 就地修补 catalog(默认 dry-run)。guard 会修「config 当前指向的文件」+ efforts.json,
19
- 不论指针指向哪,文件内容都是对的——能抵抗 cc-switch 重新生成 catalog 造成的覆盖:
19
+ 但结果仅按精确 slug 修补,且必须人工确认路由对应关系:
20
20
 
21
21
  ```bash
22
22
  node "${PLUGIN_ROOT}/scripts/guard.mjs"
@@ -47,6 +47,7 @@ model_catalog_json = 'C:\Users\<you>\.codex\model-catalogs\efforts.json'
47
47
  ## 规则
48
48
 
49
49
  - `(unknown)` 的模型不要写 catalog,保留原值。
50
+ - 不要把 Chat Completions 或其他代理的回显自动视为 Responses 路由的确证。
50
51
  - 不要删 `off`/`minimal` 之类没探测到的档位之外的内容;只替换 `supported_reasoning_levels` 和必要的 `default_reasoning_level`。
51
52
  - catalog 是整体替换文件:务必从当前生效的 catalog 复制后再改(脚本已经这么做)。
52
- - 改动 `config.toml` 属于用户配置,必须先征得同意。
53
+ - 改动 `config.toml` 属于用户配置,必须先征得同意。