chawyehsu on X asked for dynamic model discovery, as
yuhp/opencode-models-discovery does. magpie wrote its whole catalog into
opencode.json's magpie provider. For an OpenCode that loads plugins of
today's shape (1.4.0 and later, 2.0.5 and later; openCodeDiscovers),
the provider now has no "models". magpie writes plugins/magpie-models.js
beside the file (opencode_models.js, marked by its first line). It fills
the provider's models as OpenCode starts, from the gateway's new
/v1/models?format=opencode (opencode2 for OpenCode 2;
gateway/opencode_models.go). That endpoint gives the same entries a sync
would have listed, in magpie's order, never cached, and only the
models of OpenCode's catalog the key asking may use (keyListed).
- OpenCode 1: through its config hook, which runs before it reads
providers. The user's entries under magpie's provider are laid over
magpie's, and an id magpie doesn't list stays.
- OpenCode 2: through ctx.model.transform. The config's own entries are
laid on afterwards by OpenCode. Every entry says variants ([] for
none), so OpenCode makes up none.
- Gateway not answering (3s timeout): the plugin lists the models magpie
had when it last wrote it.
Existing users: the first sync on such an OpenCode takes magpie's list
out of the file (modelsLeft). Of each model, only what the user said
differently stays: their own variant, or a limit.output they typed. A
limit or cost they changed stays whole, since OpenCode 1.19.6 refuses
one without its context ("Missing key ...limit.context"). An id magpie
no longer lists goes that once (stash opencode-discover:<path>), as it
did on every sync before; one added after that is the user's and stays.
The user's other keys and providers are untouched. These keep the list
in the file as before: an OpenCode older than those versions, of no
known version, and the forks (mimocode, DevEco, Air). Going back to
such an OpenCode writes the list again and removes the plugin, and so
does taking magpie out. A file at the plugin's path without magpie's
first line is the user's and is never touched.
Run:
- mote-run go vet ./internal/agent ./internal/gateway, also with
GOOS=linux and GOOS=windows: clean. gofmt -l: clean.
- mote-run go test ./internal/agent -run
'OpenCode|OpenChamber|Air|DevEco|Mimo': ok (TestOpenCodePluginRuns runs
the plugin in node against a stubbed gateway, up and down, v1 and v2).
- mote-run go test ./internal/gateway -run 'TestOpenCodeModelsList|Models': ok.
- Broken by hand, each red: no key filter (TestOpenCodeModelsList),
openCodeDiscovers always false (Finds/ListMoves/DiscoversFrom), the
delta keeping whole models (ListMoves), and the plugin letting magpie's
fields win over the user's (PluginRuns).
- Three opencode_v2 tests that check the listed words now pin 2.0.4 and
1.3.9, which still list.
- Real OpenCode on mote in a sandbox HOME, opencode-ai 1.19.6 and
@opencode/cli 2.3.2. Config and plugin written by magpie's own code, a
fake /v1/models serving one extra model:
- `opencode models` lists the served model with the gateway up, and
the cached list with it down. Both versions, Bearer key sent.
- A file moved from 1.3.9/2.0.4 with a user limit.output and a user
model: `opencode models magpie --verbose` (1.19.6) and
`opencode api --standalone GET /api/model` (2.3.2) resolve magpie's
models the same as the old full-list file did. Names, limits,
variants and the user's edits are identical; 2.3.2 orders a model
the file names first.
- Full suite: mote-run go test ./... -timeout 45m -count=1 on this change over base 1bfa8d5a1: 103 packages ok, no FAIL.
- Rebased on 7300be4d0 (main touched agents.go and gateway.go): go vet
darwin/linux/windows of internal/agent and internal/gateway clean;
go test ./internal/agent ok; ./internal/gateway red only in
TestSearchQueueKeepsCodexAlive/comments and /cancel at load ~37, the
idle-bar class of #7090 (commented), 5/5 ok alone at this head.
- Not changed: the forks mimocode, DevEco and Air keep the list; filed as #7055.
Not tried: a WSL distro's OpenCode, and OpenChamber's own UI over the
plugin-filled provider.
54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
// Runs magpie's OpenCode plugin (argv[2]) as OpenCode would, mode argv[3]:
|
|
// v1-up, v1-down (OpenCode 1's config hook), v2-up, v2-down (OpenCode 2's
|
|
// model transform), with the gateway answering MAGPIE_SERVED or down.
|
|
// Prints magpie's provider's models.
|
|
import { pathToFileURL } from "node:url"
|
|
|
|
const [, , plugin, mode] = process.argv
|
|
const served = JSON.parse(process.env.MAGPIE_SERVED || "{}")
|
|
globalThis.fetch = async (url, opts) => {
|
|
if (mode.endsWith("-down")) throw new Error("connection refused")
|
|
const format = new URL(url).searchParams.get("format")
|
|
if (!opts?.headers?.authorization?.startsWith("Bearer ")) return { ok: false, status: 401 }
|
|
return { ok: true, json: async () => served[format] }
|
|
}
|
|
const mod = (await import(pathToFileURL(plugin).href)).default
|
|
|
|
if (mode.startsWith("v1")) {
|
|
const hooks = await mod.server({}, {})
|
|
const cfg = {
|
|
provider: {
|
|
magpie: {
|
|
npm: "@ai-sdk/openai-compatible",
|
|
options: {},
|
|
models: {
|
|
"live/one": { limit: { output: 4321 }, variants: { mine: { temperature: 0.5 } } },
|
|
"local/own": { name: "own" },
|
|
},
|
|
},
|
|
},
|
|
}
|
|
await hooks.config(cfg)
|
|
console.log(JSON.stringify(cfg.provider.magpie.models))
|
|
} else {
|
|
const models = {}
|
|
const editor = {
|
|
provider: { get: (id) => (id === "magpie" ? { provider: { id }, models: [] } : undefined) },
|
|
update: (p, m, fn) => {
|
|
if (p !== "magpie") return
|
|
const cur = models[m] ?? {
|
|
id: m, name: m, variants: [], cost: [],
|
|
capabilities: { tools: true, input: ["text", "image"], output: ["text"] },
|
|
limit: { context: 200000, output: 32000 },
|
|
}
|
|
fn(cur)
|
|
models[m] = cur
|
|
},
|
|
}
|
|
await mod.setup({
|
|
model: { transform: async (cb) => { cb(editor); return { dispose() {} } } },
|
|
provider: { get: async () => ({ location: "test", data: { id: "magpie", settings: {} } }) },
|
|
})
|
|
console.log(JSON.stringify(models))
|
|
}
|