From 9fd1804b1256d6892aaf2524de9db705fd3b208a Mon Sep 17 00:00:00 2001 From: hongnhatbkhn Date: Mon, 2 Nov 2026 14:11:00 +0700 Subject: [PATCH] probe for available model variants --- scripts/sync-models.mjs | 123 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 113 insertions(+), 10 deletions(-) diff --git a/scripts/sync-models.mjs b/scripts/sync-models.mjs index ac3e8f5..db83c42 100644 --- a/scripts/sync-models.mjs +++ b/scripts/sync-models.mjs @@ -3,8 +3,9 @@ // opencode builds `/models` from the configured model map; a custom `file://` provider is only // ever asked for `languageModel(id)`, never queried for discovery. So the catalog has to be baked // into opencode.json. This script fetches the live catalog, lets the user pick which models to -// enable with a builtins-only checkbox UI, probes the selection for vision support, and writes -// the result into the global opencode config. Deselecting a model removes it from the config. +// enable with a builtins-only checkbox UI, probes the selection for vision support and for the +// reasoning_effort tiers upstream accepts, and writes the result into the global opencode config. +// Deselecting a model removes it from the config. // // Run: node scripts/sync-models.mjs (build first: npm run build) import { mkdirSync, realpathSync, writeFileSync, renameSync } from "node:fs"; @@ -37,6 +38,67 @@ const NO_IMAGE = // so this is only a best guess for ids that look like known vision families. const VISION_HEURISTIC = /claude|gpt-5|gemini|grok|qwen.*vl|vision|(?:^|[^a-z])vl(?:[^a-z]|$)|omni|multimodal/i; +// Candidate reasoning_effort tiers probed live per model. A tier counts as supported when a +// tiny request carrying it completes; upstream rejects unsupported values for the whole +// request, which is exactly the signal. A timeout is inconclusive, not a rejection — a slow +// thinking model must not lose its tier. +const EFFORT_CANDIDATES = ["low", "medium", "high", "xhigh", "max"]; + +// Static guess when live probing cannot classify a model (unreachable or plan-gated), +// mirroring the vision-heuristic fallback: common tiers, plus DeepSeek V4's extra "max". +function fallbackEfforts(id) { + const tiers = ["low", "medium", "high"]; + if (/deepseek-v4/i.test(id)) tiers.push("max"); + return tiers; +} + +// User-curated variants are honored (and never overwritten) only when the entry carries at +// least one; an empty `variants: {}` counts as absent so probing still runs and can fill it. +function hasVariants(value) { + return !!value && typeof value === "object" && Object.keys(value).length > 0; +} + +async function probeEffort(provider, id, effort) { + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(new Error("probe timeout")), 30000); + try { + await provider.languageModel(id).doGenerate({ + prompt: [{ role: "user", content: [{ type: "text", text: "Reply with exactly: ok" }] }], + maxOutputTokens: 256, + temperature: 0, + ...(effort === undefined ? {} : { providerOptions: { [PROVIDER_ID]: { reasoningEffort: effort } } }), + abortSignal: controller.signal, + }); + return true; + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + return /probe timeout|abort/i.test(message) ? undefined : false; + } finally { + clearTimeout(timer); + } +} + +// Resolves the tiers to write as variants: the probed-and-verified tiers when at least one +// request succeeded, the static fallback when probing could not classify, or none when the +// model is reachable but rejects every candidate effort (variants stay unset so /variants +// does not offer tiers that always fail). +async function probeEfforts(provider, id) { + const verified = []; + let inconclusive = false; + for (const effort of EFFORT_CANDIDATES) { + const ok = await probeEffort(provider, id, effort); + if (ok === true) verified.push(effort); + else if (ok === undefined) inconclusive = true; + } + if (verified.length > 0) return { tiers: verified, source: "probed" }; + if (inconclusive) return { tiers: fallbackEfforts(id), source: "fallback" }; + // Every candidate hard-rejected. A no-effort baseline separates "model rejects all + // efforts" from "model is unreachable or plan-gated" (where the fallback is the best guess). + const baseline = await probeEffort(provider, id, undefined); + if (baseline === true) return { tiers: [], source: "none" }; + return { tiers: fallbackEfforts(id), source: "fallback" }; +} + async function probeVision(provider, id) { const controller = new AbortController(); const timer = setTimeout(() => controller.abort(new Error("probe timeout")), 30000); @@ -357,7 +419,7 @@ async function main() { } const selected = picked.map((index) => catalog[index]).filter(Boolean); - console.log(`Probing vision (${selected.length} models, concurrency ${CONCURRENCY})...`); + console.log(`Probing vision + reasoning efforts (${selected.length} models, concurrency ${CONCURRENCY})...`); const provider = createCommandCode({ name: PROVIDER_ID, apiKey: key, baseURL, ccVersion, maxRetries: 1 }); const probeResults = new Array(selected.length); let probeCursor = 0; @@ -369,11 +431,26 @@ async function main() { // Some models intermittently deny seeing an image on the first try. Retry an explicit // "no image" once so a flaky denial does not mislabel a vision model; inconclusive // results skip the retry. - let outcome = await probeVision(provider, model.id); - if (outcome.vision === false) outcome = await probeVision(provider, model.id); - const tag = outcome.vision === true ? "vision" : outcome.vision === false ? "no-vision" : "unknown"; - console.log(` [${index + 1}/${selected.length}] ${model.id}: ${tag} (${outcome.reason})`); - probeResults[index] = outcome; + let vision = await probeVision(provider, model.id); + if (vision.vision === false) vision = await probeVision(provider, model.id); + const tag = vision.vision === true ? "vision" : vision.vision === false ? "no-vision" : "unknown"; + console.log(` [${index + 1}/${selected.length}] ${model.id}: ${tag} (${vision.reason})`); + // Reasoning-effort probe. Skipped when the user already curated variants for this id + // (kept as-is, nothing probed or overwritten), or when the vision probe showed the + // model is not reachable at all — the effort probes would only burn failing requests + // on the same failure, so the static fallback covers it. + const userVariants = existing[model.id]?.variants; + let efforts; + if (hasVariants(userVariants)) { + efforts = { tiers: Object.keys(userVariants), source: "kept" }; + } else if (vision.vision === undefined && !/^empty\b/.test(vision.reason)) { + efforts = { tiers: fallbackEfforts(model.id), source: "fallback" }; + } else { + efforts = await probeEfforts(provider, model.id); + } + const effortTag = `${efforts.tiers.join(",") || "none"}${efforts.source === "probed" ? "" : ` (${efforts.source})`}`; + console.log(` [${index + 1}/${selected.length}] ${model.id}: efforts=${effortTag}`); + probeResults[index] = { vision, efforts }; } }); await Promise.all(probeRunners); @@ -381,9 +458,19 @@ async function main() { // Deselect = remove: the final map holds exactly the picked ids. Extra user fields on // picked ids are preserved; unpicked ids are dropped. const merged = {}; - const stats = { probed: 0, vision: 0, noVision: 0, unknown: 0, heuristic: 0 }; + const stats = { + probed: 0, + vision: 0, + noVision: 0, + unknown: 0, + heuristic: 0, + effortsProbed: 0, + effortsFallback: 0, + effortsNone: 0, + effortsKept: 0, + }; selected.forEach((model, index) => { - const outcome = probeResults[index]; + const outcome = probeResults[index]?.vision; let vision; if (outcome && outcome.vision !== undefined) { vision = outcome.vision; @@ -406,6 +493,20 @@ async function main() { }, }; merged[model.id] = { ...(existing[model.id] ?? {}), ...entry }; + // Variants are what opencode's /variants switcher (and --variant) lists; a custom + // provider cannot register them itself. Never overwrite user-curated variants. + const efforts = probeResults[index]?.efforts; + if (efforts) { + if (efforts.tiers.length > 0 && !hasVariants(merged[model.id].variants)) { + merged[model.id].variants = Object.fromEntries( + efforts.tiers.map((tier) => [tier, { reasoningEffort: tier }]), + ); + } + if (efforts.source === "probed") stats.effortsProbed++; + else if (efforts.source === "fallback") stats.effortsFallback++; + else if (efforts.source === "none") stats.effortsNone++; + else stats.effortsKept++; + } }); const removed = [...existingIds].filter((id) => !(id in merged)).length; @@ -435,6 +536,8 @@ async function main() { ` selected=${selected.length} removed=${removed}\n` + ` probed=${stats.probed} vision=${stats.vision} no-vision=${stats.noVision} ` + `unknown=${stats.unknown} heuristic-vision=${stats.heuristic}\n` + + ` efforts: probed=${stats.effortsProbed} fallback=${stats.effortsFallback} ` + + `none=${stats.effortsNone} kept=${stats.effortsKept}\n` + `Restart opencode, then run: opencode models ${PROVIDER_ID}`, ); }