@tscgoods/opencode-effort-discovery 0.4.8 → 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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.ts +40 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscgoods/opencode-effort-discovery",
3
- "version": "0.4.8",
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.3.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) console.log("[effort-discovery]", ...args)
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
- model.variants = toVariants(levels) as typeof model.variants
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
@@ -363,16 +394,17 @@ export default Plugin.define({
363
394
  }
364
395
 
365
396
  for (const model of models.filter((item) => item.providerID === provider.id)) {
366
- const id = cacheKey(provider.id, model.modelID)
397
+ const modelID = String((model as { modelID?: string }).modelID ?? model.id)
398
+ const id = cacheKey(provider.id, modelID)
367
399
  const previous = probes.get(id)
368
400
  const stale =
369
401
  !previous || previous.source.startsWith("http-") || Date.now() - previous.at >= ttlMs
370
402
  if (!stale) continue
371
403
 
372
- const result = await probeEffort({ baseURL, apiKey, modelID: model.modelID, headers })
404
+ const result = await probeEffort({ baseURL, apiKey, modelID, headers })
373
405
 
374
406
  if (result.source === "error") {
375
- log("probe failed", provider.id, model.modelID)
407
+ log("probe failed", provider.id, modelID)
376
408
  continue
377
409
  }
378
410
  // 限流/服务端错误:保留旧结论,下一轮再试
@@ -386,8 +418,8 @@ export default Plugin.define({
386
418
  const after = result.levels?.join(",") ?? "<none>"
387
419
  if (before !== after) changed = true
388
420
  probes.set(id, result)
389
- await ctx.storage.set(storageKey(provider.id, model.modelID), { ...result })
390
- log(provider.id, model.modelID, before, "->", after, result.source)
421
+ await ctx.storage.set(storageKey(provider.id, modelID), { ...result })
422
+ log(provider.id, modelID, before, "->", after, result.source)
391
423
  await sleep(PROBE_GAP_MS)
392
424
  }
393
425
  }