@tscgoods/effort-discovery-core 0.4.0 → 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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/src/cli.js +12 -5
  3. package/src/index.js +21 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscgoods/effort-discovery-core",
3
- "version": "0.4.0",
3
+ "version": "0.5.2",
4
4
  "description": "Core probe/catalog/cache logic shared by every effort-discovery host plugin.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/cli.js CHANGED
@@ -93,6 +93,12 @@ function runProbe(provider, modelID) {
93
93
  })
94
94
  }
95
95
 
96
+ function cacheMatches(result, provider) {
97
+ return result?.evidenceVersion === 2 && result.api === provider.api &&
98
+ result.baseURL === provider.baseURL && Number.isFinite(result.at) &&
99
+ result.at <= Date.now() && Date.now() - result.at < DEFAULT_TTL_MS
100
+ }
101
+
96
102
  function printTable(rows) {
97
103
  const headers = ["provider", "model", "levels", "source"]
98
104
  const table = [headers, ...rows]
@@ -175,12 +181,13 @@ async function main() {
175
181
 
176
182
  for (const modelID of models) {
177
183
  const cached = await cache.get(provider.id, modelID)
178
- const fresh = cached && Date.now() - cached.at < DEFAULT_TTL_MS && command === "get"
184
+ const fresh = cacheMatches(cached, provider) && command === "get"
179
185
  let result = fresh ? cached : undefined
180
186
 
181
187
  if (!result) {
182
188
  const probed = await runProbe(provider, modelID)
183
- result = probed.source === "error" && cached ? cached : probed
189
+ // Old cache entries may contain ladder levels inferred by earlier versions.
190
+ result = { ...probed, evidenceVersion: 2, api: provider.api, baseURL: provider.baseURL }
184
191
  if (result.source !== "error") await cache.set(provider.id, modelID, result)
185
192
  }
186
193
 
@@ -213,10 +220,10 @@ async function main() {
213
220
  const level = String(args.level).toLowerCase()
214
221
 
215
222
  const cached = await cache.get(provider.id, modelID)
216
- let result = cached && Date.now() - cached.at < DEFAULT_TTL_MS ? cached : undefined
223
+ let result = cacheMatches(cached, provider) ? cached : undefined
217
224
  if (!result) {
218
225
  const probed = await runProbe(provider, modelID)
219
- result = probed.source === "error" && cached ? cached : probed
226
+ result = { ...probed, evidenceVersion: 2, api: provider.api, baseURL: provider.baseURL }
220
227
  if (result.source !== "error") await cache.set(provider.id, modelID, result)
221
228
  }
222
229
 
@@ -250,4 +257,4 @@ main().then(
250
257
  process.stderr.write(String(error?.stack ?? error) + "\n")
251
258
  process.exit(1)
252
259
  },
253
- )
260
+ )
package/src/index.js CHANGED
@@ -264,7 +264,7 @@ export const ANTHROPIC_EFFORT_LADDER = ["max", "xhigh", "high", "medium", "low"]
264
264
  * 1) 先发一个明显非法的 effort:若网关仍返回 2xx,说明它根本不校验这个字段,
265
265
  * 结果标记为 no-validation(绝不把 2xx 当成“支持全部档位”)。
266
266
  * 2) 若非法值触发 400 且错误里列出合法值,直接解析出来(最精确)。
267
- * 3) 否则用 ladder 逐级试探。
267
+ * 3) 否则逐个试探 ladder,不推断未测试的档位。
268
268
  */
269
269
  export async function probeAnthropicEffort(input) {
270
270
  const {
@@ -276,7 +276,9 @@ export async function probeAnthropicEffort(input) {
276
276
  ladder = ANTHROPIC_EFFORT_LADDER,
277
277
  } = input
278
278
 
279
- const base = normalizeBaseURL(baseURL).replace(/\/v1$/, "")
279
+ const normalized = normalizeBaseURL(baseURL)
280
+ if (!normalized) return { levels: null, source: "error", at: Date.now() }
281
+ const base = normalized.replace(/\/v1$/, "")
280
282
  const url = `${base}/v1/messages`
281
283
  let withBeta = true
282
284
  let unknown = false
@@ -319,7 +321,12 @@ export async function probeAnthropicEffort(input) {
319
321
  }
320
322
 
321
323
  // 1) sentinel:网关若接受任意值,就是没有校验
322
- const sentinel = await send("effort-discovery-probe")
324
+ let sentinel = await send("effort-discovery-probe")
325
+ if (sentinel?.status === 400 && /effort-2025-11-24|anthropic-beta/i.test(sentinel.text) &&
326
+ !/output_config|does not support|unsupported|invalid value/i.test(sentinel.text)) {
327
+ withBeta = false
328
+ sentinel = await send("effort-discovery-probe")
329
+ }
323
330
  const fatal = classified(sentinel)
324
331
  if (fatal) return fatal
325
332
  if (sentinel) {
@@ -328,17 +335,13 @@ export async function probeAnthropicEffort(input) {
328
335
  }
329
336
  const fromError = parseLevels(sentinel.text)
330
337
  if (fromError) return { levels: fromError, source: "validated", at: Date.now() }
331
- // 只是 beta 头不被接受时,后续去掉它重试
332
- if (
333
- sentinel.status === 400 &&
334
- /effort-2025-11-24|anthropic-beta/i.test(sentinel.text) &&
335
- !/output_config|does not support|unsupported|invalid value/i.test(sentinel.text)
336
- ) {
337
- withBeta = false
338
- }
338
+ }
339
+ if (!sentinel || ![400, 422].includes(sentinel.status) || !/effort/i.test(sentinel.text)) {
340
+ return { levels: null, source: sentinel ? `http-${sentinel.status}` : "error", at: Date.now() }
339
341
  }
340
342
 
341
- // 2) ladder:从高到低逐个试
343
+ // 2) 单档成功不能证明其他档位也成功。
344
+ const accepted = []
342
345
  for (let index = 0; index < ladder.length; index++) {
343
346
  const result = await send(ladder[index])
344
347
  const fatal = classified(result)
@@ -348,7 +351,8 @@ export async function probeAnthropicEffort(input) {
348
351
  continue
349
352
  }
350
353
  if (result.status >= 200 && result.status < 300) {
351
- return { levels: ladder.slice(index), source: "validated", at: Date.now() }
354
+ accepted.push(ladder[index])
355
+ continue
352
356
  }
353
357
 
354
358
  const mentionsBeta = /effort-2025-11-24|anthropic-beta/i.test(result.text)
@@ -362,13 +366,14 @@ export async function probeAnthropicEffort(input) {
362
366
  if (result.status === 400 || result.status === 422) {
363
367
  const listed = parseLevels(result.text)
364
368
  if (listed) return { levels: listed, source: "validated", at: Date.now() }
365
- if (!mentionsEffort && !mentionsBeta) unknown = true
369
+ if (!/effort/i.test(result.text)) unknown = true
366
370
  continue
367
371
  }
368
372
  unknown = true
369
373
  }
370
374
 
371
- return { levels: null, source: unknown ? "error" : "no-validation", at: Date.now() }
375
+ if (unknown) return { levels: null, source: "incomplete", at: Date.now() }
376
+ return { levels: accepted.length ? accepted : null, source: accepted.length ? "individual-2xx" : "no-validation", at: Date.now() }
372
377
  }
373
378
 
374
- export const __internal = { isObject }
379
+ export const __internal = { isObject }