@tscgoods/effort-discovery-core 0.3.0 → 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.
Files changed (3) hide show
  1. package/package.json +39 -39
  2. package/src/cli.js +0 -0
  3. package/src/index.js +57 -22
package/package.json CHANGED
@@ -1,39 +1,39 @@
1
- {
2
- "name": "@tscgoods/effort-discovery-core",
3
- "version": "0.3.0",
4
- "description": "Core probe/catalog/cache logic shared by every effort-discovery host plugin.",
5
- "type": "module",
6
- "license": "MIT",
7
- "exports": {
8
- ".": {
9
- "types": "./src/index.d.ts",
10
- "default": "./src/index.js"
11
- }
12
- },
13
- "bin": {
14
- "effort-discovery": "./src/cli.js"
15
- },
16
- "files": [
17
- "src",
18
- "README.md"
19
- ],
20
- "engines": {
21
- "node": ">=20"
22
- },
23
- "keywords": [
24
- "reasoning-effort",
25
- "opencode",
26
- "claude-code",
27
- "codex",
28
- "deepseek-harness",
29
- "antigravity"
30
- ],
31
- "repository": {
32
- "type": "git",
33
- "url": "git+https://github.com/lxgy1024/effort-discovery.git",
34
- "directory": "packages/core"
35
- },
36
- "publishConfig": {
37
- "access": "public"
38
- }
39
- }
1
+ {
2
+ "name": "@tscgoods/effort-discovery-core",
3
+ "version": "0.4.0",
4
+ "description": "Core probe/catalog/cache logic shared by every effort-discovery host plugin.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./src/index.d.ts",
10
+ "default": "./src/index.js"
11
+ }
12
+ },
13
+ "bin": {
14
+ "effort-discovery": "src/cli.js"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "README.md"
19
+ ],
20
+ "engines": {
21
+ "node": ">=20"
22
+ },
23
+ "keywords": [
24
+ "reasoning-effort",
25
+ "opencode",
26
+ "claude-code",
27
+ "codex",
28
+ "deepseek-harness",
29
+ "antigravity"
30
+ ],
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/lxgy1024/effort-discovery.git",
34
+ "directory": "packages/core"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ }
39
+ }
package/src/cli.js CHANGED
File without changes
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 {
@@ -278,8 +281,7 @@ export async function probeAnthropicEffort(input) {
278
281
  let withBeta = true
279
282
  let unknown = false
280
283
 
281
- for (let index = 0; index < ladder.length; index++) {
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
- if (response.ok) return { levels: ladder.slice(index), source: "validated", at: Date.now() }
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 (response.status === 400 && mentionsBeta && !mentionsEffort && withBeta) {
357
+ if (result.status === 400 && mentionsBeta && !mentionsEffort && withBeta) {
319
358
  withBeta = false
320
- index-- // 用同一档位重试,去掉 beta 头
359
+ index--
321
360
  continue
322
361
  }
323
-
324
- if (response.status === 400 || response.status === 422) {
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