@tscgoods/effort-discovery-core 0.4.0 → 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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/src/cli.js +12 -5
  3. package/src/index.js +43 -17
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.5",
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
@@ -84,6 +84,20 @@ export function parseLevels(text) {
84
84
  return null
85
85
  }
86
86
 
87
+ /**
88
+ * 通用网关有时把所有枚举值都回显出来(serde 的 expected one of),
89
+ * 只有互相印证的窄子集才能作为模型级证据。
90
+ */
91
+ export function pickLevels(candidates) {
92
+ const specific = candidates.filter((levels) => levels.length > 0 && levels.length < 6)
93
+ if (specific.length === 0) return null
94
+ let result = specific[0]
95
+ for (const levels of specific.slice(1)) {
96
+ result = result.filter((level) => levels.includes(level))
97
+ }
98
+ return result.length > 0 ? result : null
99
+ }
100
+
87
101
  /**
88
102
  * 用非法值探一次,读回上游自己声明的合法档位。
89
103
  * 个别网关只对特定非法值透传上游报错,因此按 sentinels 依次尝试。
@@ -98,6 +112,7 @@ export async function probeEffort(input) {
98
112
 
99
113
  let last = { levels: null, source: "error", at: Date.now() }
100
114
  let sawOk = false
115
+ const candidates = []
101
116
 
102
117
  for (const sentinel of sentinels) {
103
118
  let response
@@ -124,13 +139,19 @@ export async function probeEffort(input) {
124
139
  if (response.ok) { sawOk = true; continue }
125
140
 
126
141
  const levels = parseLevels(text)
127
- if (levels) return { levels, source: "validated", at: Date.now() }
142
+ if (levels) {
143
+ candidates.push(levels)
144
+ continue
145
+ }
128
146
 
129
147
  last = { levels: null, source: `http-${response.status}`, at: Date.now() }
130
148
  // 限流/鉴权错误换 sentinel 也没用,直接返回
131
149
  if (response.status === 401 || response.status === 403 || response.status === 429) return last
132
150
  }
133
151
 
152
+ const levels = pickLevels(candidates)
153
+ if (levels) return { levels, source: "validated", at: Date.now() }
154
+ if (candidates.length > 0) return { levels: null, source: "no-validation", at: Date.now() }
134
155
  return sawOk ? { levels: null, source: "no-validation", at: Date.now() } : last
135
156
  }
136
157
 
@@ -264,7 +285,7 @@ export const ANTHROPIC_EFFORT_LADDER = ["max", "xhigh", "high", "medium", "low"]
264
285
  * 1) 先发一个明显非法的 effort:若网关仍返回 2xx,说明它根本不校验这个字段,
265
286
  * 结果标记为 no-validation(绝不把 2xx 当成“支持全部档位”)。
266
287
  * 2) 若非法值触发 400 且错误里列出合法值,直接解析出来(最精确)。
267
- * 3) 否则用 ladder 逐级试探。
288
+ * 3) 否则逐个试探 ladder,不推断未测试的档位。
268
289
  */
269
290
  export async function probeAnthropicEffort(input) {
270
291
  const {
@@ -276,7 +297,9 @@ export async function probeAnthropicEffort(input) {
276
297
  ladder = ANTHROPIC_EFFORT_LADDER,
277
298
  } = input
278
299
 
279
- const base = normalizeBaseURL(baseURL).replace(/\/v1$/, "")
300
+ const normalized = normalizeBaseURL(baseURL)
301
+ if (!normalized) return { levels: null, source: "error", at: Date.now() }
302
+ const base = normalized.replace(/\/v1$/, "")
280
303
  const url = `${base}/v1/messages`
281
304
  let withBeta = true
282
305
  let unknown = false
@@ -319,7 +342,12 @@ export async function probeAnthropicEffort(input) {
319
342
  }
320
343
 
321
344
  // 1) sentinel:网关若接受任意值,就是没有校验
322
- const sentinel = await send("effort-discovery-probe")
345
+ let sentinel = await send("effort-discovery-probe")
346
+ if (sentinel?.status === 400 && /effort-2025-11-24|anthropic-beta/i.test(sentinel.text) &&
347
+ !/output_config|does not support|unsupported|invalid value/i.test(sentinel.text)) {
348
+ withBeta = false
349
+ sentinel = await send("effort-discovery-probe")
350
+ }
323
351
  const fatal = classified(sentinel)
324
352
  if (fatal) return fatal
325
353
  if (sentinel) {
@@ -328,17 +356,13 @@ export async function probeAnthropicEffort(input) {
328
356
  }
329
357
  const fromError = parseLevels(sentinel.text)
330
358
  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
- }
359
+ }
360
+ if (!sentinel || ![400, 422].includes(sentinel.status) || !/effort/i.test(sentinel.text)) {
361
+ return { levels: null, source: sentinel ? `http-${sentinel.status}` : "error", at: Date.now() }
339
362
  }
340
363
 
341
- // 2) ladder:从高到低逐个试
364
+ // 2) 单档成功不能证明其他档位也成功。
365
+ const accepted = []
342
366
  for (let index = 0; index < ladder.length; index++) {
343
367
  const result = await send(ladder[index])
344
368
  const fatal = classified(result)
@@ -348,7 +372,8 @@ export async function probeAnthropicEffort(input) {
348
372
  continue
349
373
  }
350
374
  if (result.status >= 200 && result.status < 300) {
351
- return { levels: ladder.slice(index), source: "validated", at: Date.now() }
375
+ accepted.push(ladder[index])
376
+ continue
352
377
  }
353
378
 
354
379
  const mentionsBeta = /effort-2025-11-24|anthropic-beta/i.test(result.text)
@@ -362,13 +387,14 @@ export async function probeAnthropicEffort(input) {
362
387
  if (result.status === 400 || result.status === 422) {
363
388
  const listed = parseLevels(result.text)
364
389
  if (listed) return { levels: listed, source: "validated", at: Date.now() }
365
- if (!mentionsEffort && !mentionsBeta) unknown = true
390
+ if (!/effort/i.test(result.text)) unknown = true
366
391
  continue
367
392
  }
368
393
  unknown = true
369
394
  }
370
395
 
371
- return { levels: null, source: unknown ? "error" : "no-validation", at: Date.now() }
396
+ if (unknown) return { levels: null, source: "incomplete", at: Date.now() }
397
+ return { levels: accepted.length ? accepted : null, source: accepted.length ? "individual-2xx" : "no-validation", at: Date.now() }
372
398
  }
373
399
 
374
- export const __internal = { isObject }
400
+ export const __internal = { isObject }