@tscgoods/opencode-effort-discovery 0.4.8 → 0.4.10
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 +2 -2
- package/src/index.ts +58 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tscgoods/opencode-effort-discovery",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.10",
|
|
4
4
|
"description": "OpenCode plugin: show exactly the reasoning effort levels your provider actually accepts.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,6 +31,6 @@
|
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@opencode/plugin": "^2.
|
|
34
|
+
"@opencode/plugin": "^2.4.1"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Plugin } from "@opencode/plugin"
|
|
2
2
|
import { execSync } from "node:child_process"
|
|
3
|
+
import { appendFileSync, mkdirSync } from "node:fs"
|
|
4
|
+
import { tmpdir } from "node:os"
|
|
5
|
+
import { join } from "node:path"
|
|
3
6
|
|
|
4
7
|
/**
|
|
5
8
|
* effort-discovery
|
|
@@ -223,8 +226,25 @@ export default Plugin.define({
|
|
|
223
226
|
? (options.overrides as Record<string, Record<string, string[]>>)
|
|
224
227
|
: {}
|
|
225
228
|
const debug = options.debug === true
|
|
229
|
+
// server 不捕获插件 stdout;debug 时同时写文件,保证可观测
|
|
230
|
+
const debugFile = join(tmpdir(), "effort-discovery-opencode.log")
|
|
226
231
|
const log = (...args: unknown[]) => {
|
|
227
|
-
if (debug)
|
|
232
|
+
if (!debug) return
|
|
233
|
+
const line = `[effort-discovery] ${args
|
|
234
|
+
.map((item) => {
|
|
235
|
+
if (typeof item === "string") return item
|
|
236
|
+
try {
|
|
237
|
+
return JSON.stringify(item)
|
|
238
|
+
} catch {
|
|
239
|
+
return String(item)
|
|
240
|
+
}
|
|
241
|
+
})
|
|
242
|
+
.join(" ")}`
|
|
243
|
+
try {
|
|
244
|
+
appendFileSync(debugFile, `${new Date().toISOString()} ${line}\n`)
|
|
245
|
+
} catch {
|
|
246
|
+
mkdirSync(tmpdir(), { recursive: true })
|
|
247
|
+
}
|
|
228
248
|
}
|
|
229
249
|
|
|
230
250
|
const probes = new Map<string, ProbeResult>()
|
|
@@ -266,12 +286,15 @@ export default Plugin.define({
|
|
|
266
286
|
for (const model of editor.list()) {
|
|
267
287
|
const providerID = String(model.providerID)
|
|
268
288
|
if (!providerIDs.has(providerID)) continue
|
|
269
|
-
const modelID = String(model.modelID ?? model.id)
|
|
289
|
+
const modelID = String((model as { modelID?: string }).modelID ?? model.id)
|
|
270
290
|
const levels = resolveLevels(providerID, modelID)
|
|
271
291
|
if (!levels) continue
|
|
272
292
|
const current = (model.variants ?? []).map((variant) => variant.id).join(",")
|
|
273
293
|
if (current === levels.join(",")) continue
|
|
274
|
-
|
|
294
|
+
log("transform", providerID, modelID, current || "<empty>", "->", levels.join(","))
|
|
295
|
+
editor.update(providerID, model.id, (draft) => {
|
|
296
|
+
draft.variants = toVariants(levels) as typeof draft.variants
|
|
297
|
+
})
|
|
275
298
|
}
|
|
276
299
|
})
|
|
277
300
|
|
|
@@ -342,6 +365,14 @@ export default Plugin.define({
|
|
|
342
365
|
log("no target providers")
|
|
343
366
|
return
|
|
344
367
|
}
|
|
368
|
+
log(
|
|
369
|
+
"targets",
|
|
370
|
+
providers.map((item) => item.id).join(","),
|
|
371
|
+
"models",
|
|
372
|
+
models
|
|
373
|
+
.map((item) => `${item.providerID}/${String((item as { modelID?: string }).modelID ?? item.id)}`)
|
|
374
|
+
.join(","),
|
|
375
|
+
)
|
|
345
376
|
|
|
346
377
|
const runProbes = async () => {
|
|
347
378
|
let changed = false
|
|
@@ -361,18 +392,34 @@ export default Plugin.define({
|
|
|
361
392
|
log("skip", provider.id, "no baseURL")
|
|
362
393
|
continue
|
|
363
394
|
}
|
|
395
|
+
// 官方 zen 端点的模型在 models.dev 里有目录元数据,且端点对探测一律 400/429,不打扰
|
|
396
|
+
try {
|
|
397
|
+
const host = new URL(baseURL).hostname
|
|
398
|
+
if (host === "opencode.ai" || host.endsWith(".opencode.ai")) {
|
|
399
|
+
log("skip", provider.id, "zen endpoint uses catalog metadata")
|
|
400
|
+
continue
|
|
401
|
+
}
|
|
402
|
+
} catch {
|
|
403
|
+
// 非法 URL 照常尝试
|
|
404
|
+
}
|
|
364
405
|
|
|
365
406
|
for (const model of models.filter((item) => item.providerID === provider.id)) {
|
|
366
|
-
const
|
|
407
|
+
const modelID = String((model as { modelID?: string }).modelID ?? model.id)
|
|
408
|
+
const id = cacheKey(provider.id, modelID)
|
|
367
409
|
const previous = probes.get(id)
|
|
410
|
+
// error/429/5xx 视为可重试;http-400/404 等确定性答复在 TTL 内缓存,避免探测风暴
|
|
411
|
+
const retryable =
|
|
412
|
+
previous?.source === "error" ||
|
|
413
|
+
previous?.source === "http-429" ||
|
|
414
|
+
(previous?.source.startsWith("http-5") ?? false)
|
|
368
415
|
const stale =
|
|
369
|
-
!previous ||
|
|
416
|
+
!previous || retryable || Date.now() - previous.at >= ttlMs
|
|
370
417
|
if (!stale) continue
|
|
371
418
|
|
|
372
|
-
const result = await probeEffort({ baseURL, apiKey, modelID
|
|
419
|
+
const result = await probeEffort({ baseURL, apiKey, modelID, headers })
|
|
373
420
|
|
|
374
421
|
if (result.source === "error") {
|
|
375
|
-
log("probe failed", provider.id,
|
|
422
|
+
log("probe failed", provider.id, modelID)
|
|
376
423
|
continue
|
|
377
424
|
}
|
|
378
425
|
// 限流/服务端错误:保留旧结论,下一轮再试
|
|
@@ -386,26 +433,23 @@ export default Plugin.define({
|
|
|
386
433
|
const after = result.levels?.join(",") ?? "<none>"
|
|
387
434
|
if (before !== after) changed = true
|
|
388
435
|
probes.set(id, result)
|
|
389
|
-
await ctx.storage.set(storageKey(provider.id,
|
|
390
|
-
log(provider.id,
|
|
436
|
+
await ctx.storage.set(storageKey(provider.id, modelID), { ...result })
|
|
437
|
+
log(provider.id, modelID, before, "->", after, result.source)
|
|
391
438
|
await sleep(PROBE_GAP_MS)
|
|
392
439
|
}
|
|
393
440
|
}
|
|
394
441
|
return { changed, limited }
|
|
395
442
|
}
|
|
396
443
|
|
|
397
|
-
//
|
|
398
|
-
let changed = false
|
|
444
|
+
// 遇到限流就退避重试,直到探测稳定或放弃;每轮结束立刻 reload,不等全部轮次
|
|
399
445
|
for (let round = 0; round < 5; round++) {
|
|
400
446
|
const result = await runProbes()
|
|
401
|
-
if (result.changed)
|
|
447
|
+
if (result.changed || (round === 0 && probes.size > 0)) await ctx.model.reload()
|
|
402
448
|
if (!result.limited) break
|
|
403
449
|
const waitMs = 60_000 * (round + 1)
|
|
404
450
|
log("backing off", waitMs)
|
|
405
451
|
await sleep(waitMs)
|
|
406
452
|
}
|
|
407
|
-
|
|
408
|
-
if (changed || probes.size > 0) await ctx.model.reload()
|
|
409
453
|
}
|
|
410
454
|
|
|
411
455
|
void bootstrap().catch((error) => log("bootstrap failed", error))
|