@tscgoods/effort-discovery-core 0.3.1 → 0.4.0
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/package.json +1 -1
- package/src/index.js +57 -22
package/package.json
CHANGED
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
|
|
263
|
-
*
|
|
264
|
-
*
|
|
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 {
|
|
@@ -278,8 +281,7 @@ export async function probeAnthropicEffort(input) {
|
|
|
278
281
|
let withBeta = true
|
|
279
282
|
let unknown = false
|
|
280
283
|
|
|
281
|
-
|
|
282
|
-
const level = ladder[index]
|
|
284
|
+
const send = async (level) => {
|
|
283
285
|
const headers = {
|
|
284
286
|
"Content-Type": "application/json",
|
|
285
287
|
"anthropic-version": "2023-06-01",
|
|
@@ -290,10 +292,8 @@ export async function probeAnthropicEffort(input) {
|
|
|
290
292
|
headers.Authorization = `Bearer ${apiKey}`
|
|
291
293
|
headers["x-api-key"] = apiKey
|
|
292
294
|
}
|
|
293
|
-
|
|
294
|
-
let response
|
|
295
295
|
try {
|
|
296
|
-
response = await fetch(url, {
|
|
296
|
+
const response = await fetch(url, {
|
|
297
297
|
method: "POST",
|
|
298
298
|
headers,
|
|
299
299
|
body: JSON.stringify({
|
|
@@ -304,32 +304,67 @@ export async function probeAnthropicEffort(input) {
|
|
|
304
304
|
}),
|
|
305
305
|
signal: AbortSignal.timeout(timeoutMs),
|
|
306
306
|
})
|
|
307
|
+
return { status: response.status, text: await response.text() }
|
|
307
308
|
} catch {
|
|
309
|
+
return undefined
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const classified = (result) => {
|
|
314
|
+
if (!result) return undefined
|
|
315
|
+
if ([401, 403, 429].includes(result.status)) {
|
|
316
|
+
return { levels: null, source: `http-${result.status}`, at: Date.now() }
|
|
317
|
+
}
|
|
318
|
+
return undefined
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// 1) sentinel:网关若接受任意值,就是没有校验
|
|
322
|
+
const sentinel = await send("effort-discovery-probe")
|
|
323
|
+
const fatal = classified(sentinel)
|
|
324
|
+
if (fatal) return fatal
|
|
325
|
+
if (sentinel) {
|
|
326
|
+
if (sentinel.status >= 200 && sentinel.status < 300) {
|
|
327
|
+
return { levels: null, source: "no-validation", at: Date.now() }
|
|
328
|
+
}
|
|
329
|
+
const fromError = parseLevels(sentinel.text)
|
|
330
|
+
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
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// 2) ladder:从高到低逐个试
|
|
342
|
+
for (let index = 0; index < ladder.length; index++) {
|
|
343
|
+
const result = await send(ladder[index])
|
|
344
|
+
const fatal = classified(result)
|
|
345
|
+
if (fatal) return fatal
|
|
346
|
+
if (!result) {
|
|
308
347
|
unknown = true
|
|
309
348
|
continue
|
|
310
349
|
}
|
|
350
|
+
if (result.status >= 200 && result.status < 300) {
|
|
351
|
+
return { levels: ladder.slice(index), source: "validated", at: Date.now() }
|
|
352
|
+
}
|
|
311
353
|
|
|
312
|
-
|
|
313
|
-
|
|
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)
|
|
354
|
+
const mentionsBeta = /effort-2025-11-24|anthropic-beta/i.test(result.text)
|
|
355
|
+
const mentionsEffort = /output_config|does not support|unsupported|invalid value/i.test(result.text)
|
|
317
356
|
|
|
318
|
-
if (
|
|
357
|
+
if (result.status === 400 && mentionsBeta && !mentionsEffort && withBeta) {
|
|
319
358
|
withBeta = false
|
|
320
|
-
index--
|
|
359
|
+
index--
|
|
321
360
|
continue
|
|
322
361
|
}
|
|
323
|
-
|
|
324
|
-
|
|
362
|
+
if (result.status === 400 || result.status === 422) {
|
|
363
|
+
const listed = parseLevels(result.text)
|
|
364
|
+
if (listed) return { levels: listed, source: "validated", at: Date.now() }
|
|
325
365
|
if (!mentionsEffort && !mentionsBeta) unknown = true
|
|
326
366
|
continue
|
|
327
367
|
}
|
|
328
|
-
|
|
329
|
-
if (response.status === 401 || response.status === 403 || response.status === 429) {
|
|
330
|
-
return { levels: null, source: `http-${response.status}`, at: Date.now() }
|
|
331
|
-
}
|
|
332
|
-
|
|
333
368
|
unknown = true
|
|
334
369
|
}
|
|
335
370
|
|