@tscgoods/effort-discovery-core 0.3.1 → 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 +66 -26
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscgoods/effort-discovery-core",
3
- "version": "0.3.1",
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
@@ -259,9 +259,12 @@ export const ANTHROPIC_EFFORT_BETA = "effort-2025-11-24"
259
259
  export const ANTHROPIC_EFFORT_LADDER = ["max", "xhigh", "high", "medium", "low"]
260
260
 
261
261
  /**
262
- * Anthropic Messages 协议(Claude Code / agy 走的协议)的档位探测:
263
- * 用 output_config.effort 逐级试探,遇到 400 说明该级别不被接受。
264
- * @returns {Promise<{levels: string[] | null, source: string, at: number}>}
262
+ * Anthropic Messages 协议的档位探测。
263
+ *
264
+ * 1) 先发一个明显非法的 effort:若网关仍返回 2xx,说明它根本不校验这个字段,
265
+ * 结果标记为 no-validation(绝不把 2xx 当成“支持全部档位”)。
266
+ * 2) 若非法值触发 400 且错误里列出合法值,直接解析出来(最精确)。
267
+ * 3) 否则逐个试探 ladder,不推断未测试的档位。
265
268
  */
266
269
  export async function probeAnthropicEffort(input) {
267
270
  const {
@@ -273,13 +276,14 @@ export async function probeAnthropicEffort(input) {
273
276
  ladder = ANTHROPIC_EFFORT_LADDER,
274
277
  } = input
275
278
 
276
- 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$/, "")
277
282
  const url = `${base}/v1/messages`
278
283
  let withBeta = true
279
284
  let unknown = false
280
285
 
281
- for (let index = 0; index < ladder.length; index++) {
282
- const level = ladder[index]
286
+ const send = async (level) => {
283
287
  const headers = {
284
288
  "Content-Type": "application/json",
285
289
  "anthropic-version": "2023-06-01",
@@ -290,10 +294,8 @@ export async function probeAnthropicEffort(input) {
290
294
  headers.Authorization = `Bearer ${apiKey}`
291
295
  headers["x-api-key"] = apiKey
292
296
  }
293
-
294
- let response
295
297
  try {
296
- response = await fetch(url, {
298
+ const response = await fetch(url, {
297
299
  method: "POST",
298
300
  headers,
299
301
  body: JSON.stringify({
@@ -304,36 +306,74 @@ export async function probeAnthropicEffort(input) {
304
306
  }),
305
307
  signal: AbortSignal.timeout(timeoutMs),
306
308
  })
309
+ return { status: response.status, text: await response.text() }
307
310
  } catch {
308
- unknown = true
309
- continue
311
+ return undefined
310
312
  }
313
+ }
311
314
 
312
- if (response.ok) return { levels: ladder.slice(index), source: "validated", at: Date.now() }
315
+ const classified = (result) => {
316
+ if (!result) return undefined
317
+ if ([401, 403, 429].includes(result.status)) {
318
+ return { levels: null, source: `http-${result.status}`, at: Date.now() }
319
+ }
320
+ return undefined
321
+ }
313
322
 
314
- const text = await response.text()
315
- const mentionsBeta = /effort-2025-11-24|anthropic-beta/i.test(text)
316
- const mentionsEffort = /output_config|effort|does not support/i.test(text)
323
+ // 1) sentinel:网关若接受任意值,就是没有校验
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
+ }
330
+ const fatal = classified(sentinel)
331
+ if (fatal) return fatal
332
+ if (sentinel) {
333
+ if (sentinel.status >= 200 && sentinel.status < 300) {
334
+ return { levels: null, source: "no-validation", at: Date.now() }
335
+ }
336
+ const fromError = parseLevels(sentinel.text)
337
+ if (fromError) return { levels: fromError, source: "validated", at: Date.now() }
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() }
341
+ }
317
342
 
318
- if (response.status === 400 && mentionsBeta && !mentionsEffort && withBeta) {
319
- withBeta = false
320
- index-- // 用同一档位重试,去掉 beta 头
343
+ // 2) 单档成功不能证明其他档位也成功。
344
+ const accepted = []
345
+ for (let index = 0; index < ladder.length; index++) {
346
+ const result = await send(ladder[index])
347
+ const fatal = classified(result)
348
+ if (fatal) return fatal
349
+ if (!result) {
350
+ unknown = true
321
351
  continue
322
352
  }
323
-
324
- if (response.status === 400 || response.status === 422) {
325
- if (!mentionsEffort && !mentionsBeta) unknown = true
353
+ if (result.status >= 200 && result.status < 300) {
354
+ accepted.push(ladder[index])
326
355
  continue
327
356
  }
328
357
 
329
- if (response.status === 401 || response.status === 403 || response.status === 429) {
330
- return { levels: null, source: `http-${response.status}`, at: Date.now() }
331
- }
358
+ const mentionsBeta = /effort-2025-11-24|anthropic-beta/i.test(result.text)
359
+ const mentionsEffort = /output_config|does not support|unsupported|invalid value/i.test(result.text)
332
360
 
361
+ if (result.status === 400 && mentionsBeta && !mentionsEffort && withBeta) {
362
+ withBeta = false
363
+ index--
364
+ continue
365
+ }
366
+ if (result.status === 400 || result.status === 422) {
367
+ const listed = parseLevels(result.text)
368
+ if (listed) return { levels: listed, source: "validated", at: Date.now() }
369
+ if (!/effort/i.test(result.text)) unknown = true
370
+ continue
371
+ }
333
372
  unknown = true
334
373
  }
335
374
 
336
- 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() }
337
377
  }
338
378
 
339
- export const __internal = { isObject }
379
+ export const __internal = { isObject }