Files
plugins/packages/opendesign/refresh-models.mjs
T
yetone 32bf0e68f9 opendesign 0.1.10: a model keeps the seed's input kinds and limits the live catalog leaves out (#829)
modelsOf() built each signed-in model from amr-link's catalog entry alone
(facts(m)). When the entry for deepseek-v4.1-flash-eco didn't list image,
the seed's input: ["image"] was lost, magpie listed it text-only and
described the image with another vision model (claude-haiku-5.5), while
amr-link takes the image itself (the reporter's direct POST). Limits and
the cost/capability words had the same loss: an entry without them fell to
200K/32K and no word, not the seed's.

withSeed() now fills what the entry leaves out from models.mjs. Input is
the seed's kinds and the catalog's together, also when the catalog lists
text only: the seed's kinds are what the catalog itself listed at the last
refresh, and a kind wrongly dropped is silent (the image is swapped for a
description on another account's allowance), while a kind wrongly kept is
the vendor's visible error. A model or kind the seed doesn't declare is the
catalog's alone. refresh-models.mjs does the same merge and prints each
kind it kept, so removing one is an edit to models.mjs.

Siblings: astudio, cline and sensenova read input_modalities too. cline
leaves an absent list unsaid, sensenova already unions its curated list
with the live one, astudio has no seed kinds to lose. No change there.

Run on mote:
- bun test packages/opendesign: 29 pass.
- Broken by hand: the old index.mjs and refresh-models.mjs fail both new
  tests (image false; no "kept image" line, eco written without input);
  only the context fallback reverted fails the modelsOf test.
- bun scripts/check.mjs opendesign: ok.
- HOME=$(mktemp -d) bun test (whole repo): 6766 pass, 20 skip, 1 fail:
  reasonix-desktop "normal host exit flushes a disconnect and leaves an
  active connection configured" (1162ms), untouched here; it passes 3 of 3
  run alone. Its output was lost to a tail; not filed.

Not tried: a signed-in opendesign account (we have none), so whether the
live entry lists ["text"] or omits the modalities is not seen; the fix
covers both. refresh-models.mjs against the real catalog.

Reported-by: exposir
2026-11-07 17:46:50 +08:00

121 lines
5.4 KiB
JavaScript

// Regenerates models.mjs — the models the provider offers before anyone has
// signed in — from amr-link's live catalog, which is the account's own list
// (it is what the plugin reads for a signed-in account too).
//
// bun refresh-models.mjs # key from magpie's sign-in
// OPENDESIGN_KEY=sk-amr-… bun refresh-models.mjs
//
// The key is the account's runtime key: magpie keeps it in plugin-auth.json
// after `magpie plugin login opendesign`, and it is never printed or written
// anywhere but the Authorization header. A name already in models.mjs is
// kept — the catalog carries ids, not names.
//
// magpie runs a plugin host per command, so `magpie plugin login` has to have
// finished before this reads the key it saved.
import { existsSync, readFileSync, writeFileSync } from "node:fs"
import { homedir } from "node:os"
import { dirname, join } from "node:path"
import { fileURLToPath, pathToFileURL } from "node:url"
// the seed this writes, beside the script: the package root here, and the
// package root when npm installs it
const HERE = dirname(fileURLToPath(import.meta.url))
const TARGET = join(HERE, "models.mjs")
const API = "https://amr-link.open-design.ai/v1"
if (!existsSync(TARGET)) {
console.error("no models.mjs beside this script")
process.exit(1)
}
const magpieKey = () => {
const dir = process.env.XDG_CONFIG_HOME || join(homedir(), ".config")
try {
const auth = JSON.parse(readFileSync(join(dir, "magpie", "plugin-auth.json"), "utf8")).opendesign
return auth?.access || auth?.key || ""
} catch {
return ""
}
}
const key = (process.env.OPENDESIGN_KEY || "").trim() || magpieKey()
if (!key) {
console.error("No key: sign in with `magpie plugin login opendesign`, or set OPENDESIGN_KEY=sk-amr-…")
process.exit(1)
}
const res = await fetch(`${API}/models`, { headers: { authorization: `Bearer ${key}` } })
if (!res.ok) {
console.error(`amr-link answered ${res.status}: ${(await res.text()).slice(0, 200)}`)
process.exit(1)
}
const body = await res.json()
const catalog = (body?.data ?? []).filter((m) => typeof m?.id === "string" && m.id.trim())
if (!catalog.length) {
console.error("amr-link listed no models")
process.exit(1)
}
// names already chosen are kept, and so are the measured reasoning levels:
// the catalog has ids only, and says nothing of efforts
let before = {}
try {
;({ MODELS: before } = await import(pathToFileURL(TARGET).href))
} catch {}
// input is the catalog's kinds and the seed's together, as the plugin serves
// them (withSeed in index.mjs, #829): a catalog entry that leaves image out
// doesn't take it from a model the seed declares it for. A kind kept that way
// is printed, so removing it is a choice made in models.mjs, not a refresh's.
const kinds = (l) => (Array.isArray(l) ? l.filter((x) => typeof x === "string" && x !== "text") : [])
const input = (id, m) => {
const said = kinds(m?.architecture?.input_modalities)
const kept = kinds(before[id]?.input).filter((x) => !said.includes(x))
if (kept.length) console.log(`${id}: kept ${kept.join(", ")} from models.mjs; the catalog doesn't list it`)
return [...new Set([...kinds(before[id]?.input), ...said])]
}
const positive = (n) => Number.isFinite(n) && n > 0
const lines = []
for (const m of catalog) {
const id = m.id.trim()
const meta = m.metadata ?? {}
const facts = []
// what the catalog leaves out is the seed's, as the plugin serves it
const was = before[id] ?? {}
const context = positive(Number(meta.context_limit)) ? Number(meta.context_limit) : Number(was.context)
const output = positive(Number(meta.context_budget)) ? Number(meta.context_budget) : Number(was.output)
if (positive(context)) facts.push(`context: ${context}`)
if (positive(output)) facts.push(`output: ${output}`)
const extra = input(id, m)
if (extra.length) facts.push(`input: [${extra.map((x) => JSON.stringify(x)).join(", ")}]`)
const cost = typeof meta.cost === "string" && meta.cost.trim() ? meta.cost : was.cost
const capability = typeof meta.capability === "string" && meta.capability.trim() ? meta.capability : was.capability
if (typeof cost === "string") facts.push(`cost: ${JSON.stringify(cost)}`)
if (typeof capability === "string") facts.push(`capability: ${JSON.stringify(capability)}`)
const efforts = before[id]?.efforts
if (Array.isArray(efforts)) facts.push(`efforts: [${efforts.map((x) => JSON.stringify(x)).join(", ")}]`)
const name = before[id]?.name ?? id
lines.push(` ${JSON.stringify(id)}: { name: ${JSON.stringify(name)}${facts.length ? ", " + facts.join(", ") : ""} },`)
}
const header = `// The models an account is offered before anyone has signed in — the seed
// magpie and OpenCode read for the provider. Regenerate with
// \`bun refresh-models.mjs\` (beside this seed): it
// reads amr-link's catalog at the signed-in account's key, which is the same
// list a signed-in account is given again on every listing.
//
// name: what the model is called; context/output: its token limits; input:
// the non-text modalities it takes; cost and capability: the vendor's own
// words, which the plugin turns into magpie's thinking flag (the rate is
// rates.mjs's, the plan's own deduction multiplier); efforts: the reasoning
// levels amr-link was seen to take and think at, measured, not read (#137),
// so a refresh keeps them.
export const MODELS = {
${lines.join("\n")}
}
`
writeFileSync(TARGET, header)
console.log(`${TARGET}: ${lines.length} models`)