@tscgoods/opencode-effort-discovery 0.3.1 → 0.4.9
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 +48 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tscgoods/opencode-effort-discovery",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.9",
|
|
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
|
|
@@ -99,8 +102,10 @@ export function parseLevels(text: string): string[] | null {
|
|
|
99
102
|
.map((item) => item.trim().replace(/^[`'"\s]+|[`'"\s.]+$/g, "").toLowerCase())
|
|
100
103
|
.filter((item) => /^[a-z0-9_-]+$/.test(item))
|
|
101
104
|
|
|
102
|
-
//
|
|
103
|
-
const
|
|
105
|
+
// 只匹配逗号分隔的档位名,避免把 JSON 里的 "type":"invalid_request_error" 吞进来
|
|
106
|
+
const list = "[a-z0-9_-]+(?:\\s*,\\s*[a-z0-9_-]+)*"
|
|
107
|
+
|
|
108
|
+
const valid = new RegExp(`valid levels?:\\s*(${list})`, "i").exec(text)
|
|
104
109
|
if (valid) {
|
|
105
110
|
const levels = clean(valid[1])
|
|
106
111
|
if (levels.length > 0) return levels
|
|
@@ -116,10 +121,10 @@ export function parseLevels(text: string): string[] | null {
|
|
|
116
121
|
}
|
|
117
122
|
|
|
118
123
|
// "supported levels are low|medium|high|max"
|
|
119
|
-
const supported =
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
124
|
+
const supported = new RegExp(
|
|
125
|
+
`(?:supported|allowed|valid)\\s+(?:levels|values|efforts)[^:\\n]*[:-]\\s*(${list})`,
|
|
126
|
+
"i",
|
|
127
|
+
).exec(text)
|
|
123
128
|
if (supported) {
|
|
124
129
|
const levels = clean(supported[1])
|
|
125
130
|
if (levels.length > 0) return levels
|
|
@@ -221,8 +226,25 @@ export default Plugin.define({
|
|
|
221
226
|
? (options.overrides as Record<string, Record<string, string[]>>)
|
|
222
227
|
: {}
|
|
223
228
|
const debug = options.debug === true
|
|
229
|
+
// server 不捕获插件 stdout;debug 时同时写文件,保证可观测
|
|
230
|
+
const debugFile = join(tmpdir(), "effort-discovery-opencode.log")
|
|
224
231
|
const log = (...args: unknown[]) => {
|
|
225
|
-
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
|
+
}
|
|
226
248
|
}
|
|
227
249
|
|
|
228
250
|
const probes = new Map<string, ProbeResult>()
|
|
@@ -264,12 +286,15 @@ export default Plugin.define({
|
|
|
264
286
|
for (const model of editor.list()) {
|
|
265
287
|
const providerID = String(model.providerID)
|
|
266
288
|
if (!providerIDs.has(providerID)) continue
|
|
267
|
-
const modelID = String(model.modelID ?? model.id)
|
|
289
|
+
const modelID = String((model as { modelID?: string }).modelID ?? model.id)
|
|
268
290
|
const levels = resolveLevels(providerID, modelID)
|
|
269
291
|
if (!levels) continue
|
|
270
292
|
const current = (model.variants ?? []).map((variant) => variant.id).join(",")
|
|
271
293
|
if (current === levels.join(",")) continue
|
|
272
|
-
|
|
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
|
+
})
|
|
273
298
|
}
|
|
274
299
|
})
|
|
275
300
|
|
|
@@ -340,6 +365,14 @@ export default Plugin.define({
|
|
|
340
365
|
log("no target providers")
|
|
341
366
|
return
|
|
342
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
|
+
)
|
|
343
376
|
|
|
344
377
|
const runProbes = async () => {
|
|
345
378
|
let changed = false
|
|
@@ -361,16 +394,17 @@ export default Plugin.define({
|
|
|
361
394
|
}
|
|
362
395
|
|
|
363
396
|
for (const model of models.filter((item) => item.providerID === provider.id)) {
|
|
364
|
-
const
|
|
397
|
+
const modelID = String((model as { modelID?: string }).modelID ?? model.id)
|
|
398
|
+
const id = cacheKey(provider.id, modelID)
|
|
365
399
|
const previous = probes.get(id)
|
|
366
400
|
const stale =
|
|
367
401
|
!previous || previous.source.startsWith("http-") || Date.now() - previous.at >= ttlMs
|
|
368
402
|
if (!stale) continue
|
|
369
403
|
|
|
370
|
-
const result = await probeEffort({ baseURL, apiKey, modelID
|
|
404
|
+
const result = await probeEffort({ baseURL, apiKey, modelID, headers })
|
|
371
405
|
|
|
372
406
|
if (result.source === "error") {
|
|
373
|
-
log("probe failed", provider.id,
|
|
407
|
+
log("probe failed", provider.id, modelID)
|
|
374
408
|
continue
|
|
375
409
|
}
|
|
376
410
|
// 限流/服务端错误:保留旧结论,下一轮再试
|
|
@@ -384,8 +418,8 @@ export default Plugin.define({
|
|
|
384
418
|
const after = result.levels?.join(",") ?? "<none>"
|
|
385
419
|
if (before !== after) changed = true
|
|
386
420
|
probes.set(id, result)
|
|
387
|
-
await ctx.storage.set(storageKey(provider.id,
|
|
388
|
-
log(provider.id,
|
|
421
|
+
await ctx.storage.set(storageKey(provider.id, modelID), { ...result })
|
|
422
|
+
log(provider.id, modelID, before, "->", after, result.source)
|
|
389
423
|
await sleep(PROBE_GAP_MS)
|
|
390
424
|
}
|
|
391
425
|
}
|