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.
242 lines
8.5 KiB
Go
242 lines
8.5 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"maps"
|
|
"slices"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
// OpenCode 2 keeps a provider under providers, in words of its own, and
|
|
// still reads OpenCode 1's provider: a provider named under both is the one
|
|
// under providers, whole, and the other only a line in its log
|
|
// (packages/core/src/config/normalize.ts, mergeMaps, at v2.1.4). For
|
|
// OpenCode 1 magpie writes its provider under provider, which both read; for
|
|
// OpenCode 2 under providers alone (ocFile). OpenChamber writes a
|
|
// provider its form saves under providers and takes it out of provider
|
|
// (packages/web/server/lib/opencode/config-v2.js, writeSectionEntry), so
|
|
// once it had saved magpie's, magpie's next sync put magpie's back under
|
|
// provider while OpenCode 2 went on reading OpenChamber's copy: a model
|
|
// added since was missing from OpenCode 2 and OpenChamber (#3883). Where
|
|
// the file keeps magpie's provider under providers, magpie writes it there
|
|
// too, in OpenCode 2's words; a file without one doesn't get one.
|
|
const openCodeNativeKey = "providers." + magpieID
|
|
|
|
// openCodeNative is magpie's provider block for an OpenCode config, in
|
|
// OpenCode 1's words (magpieProviderJSONAt's "opencode" shape), as OpenCode
|
|
// 2 reads it from provider (packages/core/src/v1/config/migrate.ts,
|
|
// migrateStandardProvider and migrateModel; checked against what 2.1.4's
|
|
// opencode debug config makes of it): npm is package "aisdk:<npm>",
|
|
// options (magpie's baseURL and apiKey) are settings, a model's modalities
|
|
// are capabilities beside tools (true, OpenCode's default), and its
|
|
// variants a list of {id, settings, body} (openCodeNativeVariant).
|
|
// reasoning, which OpenCode 2 doesn't read, goes.
|
|
func openCodeNative(block any) any {
|
|
b, _ := block.(map[string]any)
|
|
out := map[string]any{"name": b["name"]}
|
|
if npm, _ := b["npm"].(string); npm != "" {
|
|
out["package"] = "aisdk:" + npm
|
|
}
|
|
if o, ok := b["options"]; ok {
|
|
// setCacheKey is no setting of the package's but OpenCode 2's
|
|
// supportsPromptCacheKey, for the whole provider (migrate.ts,
|
|
// migrateStandardProvider; #5468)
|
|
if om, ok := o.(map[string]any); ok {
|
|
if ck, has := om["setCacheKey"]; has {
|
|
out["compatibility"] = map[string]any{"supportsPromptCacheKey": ck}
|
|
om = maps.Clone(om)
|
|
delete(om, "setCacheKey")
|
|
o = om
|
|
}
|
|
}
|
|
out["settings"] = o
|
|
}
|
|
// in the order magpie lists them (#4575): OpenCode lists a provider's
|
|
// models in the file's key order
|
|
// and none where the block has none (OpenCode's magpie plugin lists
|
|
// them: opencode_discover.go)
|
|
in, listed := b["models"].(orderedJSON)
|
|
if !listed {
|
|
return out
|
|
}
|
|
ms := orderedJSON{}
|
|
for _, p := range in {
|
|
m, _ := p.v.(map[string]any)
|
|
e := map[string]any{"name": m["name"]}
|
|
if mod, ok := m["modalities"].(map[string]any); ok {
|
|
e["capabilities"] = map[string]any{"tools": true, "input": mod["input"], "output": mod["output"]}
|
|
}
|
|
if l, ok := m["limit"]; ok {
|
|
e["limit"] = l
|
|
}
|
|
// a cost is a list of tiers in OpenCode 2, its cache prices
|
|
// under cache (migrateModel's costs)
|
|
if c, ok := m["cost"].(map[string]any); ok {
|
|
e["cost"] = []any{map[string]any{"input": c["input"], "output": c["output"],
|
|
"cache": map[string]any{"read": c["cache_read"], "write": c["cache_write"]}}}
|
|
}
|
|
if vs, ok := m["variants"].(orderedJSON); ok {
|
|
list := []any{}
|
|
for _, p := range vs {
|
|
list = append(list, openCodeNativeVariant(p.k, p.v))
|
|
}
|
|
e["variants"] = list
|
|
}
|
|
ms = append(ms, jsonPair{p.k, e})
|
|
}
|
|
out["models"] = ms
|
|
return out
|
|
}
|
|
|
|
// openCodeSettings are the options of an @ai-sdk/openai-compatible model
|
|
// that OpenCode 2 reads itself; any other a variant has goes in the
|
|
// request's body as it is (packages/core/src/v1/config/provider-options.ts,
|
|
// COMPATIBLE_KEYS).
|
|
var openCodeSettings = []string{"apiKey", "baseURL", "headers", "body", "extraBody", "timeout",
|
|
"chunkTimeout", "headerTimeout", "disabled", "reasoningEffort", "textVerbosity", "user", "strictJsonSchema"}
|
|
|
|
// openCodeNativeVariant is the variant named id, opts its options in
|
|
// OpenCode 1's words, as OpenCode 2 has it: the options it reads itself in
|
|
// settings, the rest in body, there only when there are some (a fast
|
|
// variant's service_tier, beside its extraBody).
|
|
func openCodeNativeVariant(id string, opts any) orderedJSON {
|
|
o, _ := opts.(map[string]any)
|
|
settings, body := map[string]any{}, map[string]any{}
|
|
for k, v := range o {
|
|
if slices.Contains(openCodeSettings, k) {
|
|
settings[k] = v
|
|
} else {
|
|
body[k] = v
|
|
}
|
|
}
|
|
out := orderedJSON{{"id", id}, {"settings", settings}}
|
|
if len(body) > 0 {
|
|
out = append(out, jsonPair{"body", body})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// openCodeNativeKept is openCodeKept for magpie's provider under providers:
|
|
// magpie's models are its own, written whole, and what else the block holds
|
|
// stays (theirsKept), as does a variant the user put on one of them, where
|
|
// it is or after magpie's. A variant that says what magpie's of its name
|
|
// says is magpie's (openCodeOwnVariant): one the model has no longer goes.
|
|
func openCodeNativeKept(path string, block func() any) func() any {
|
|
return theirsKept(path, openCodeNativeKey, func() any {
|
|
v := openCodeNative(block())
|
|
// a supportsPromptCacheKey the file has already is the user's
|
|
// (#5468)
|
|
if _, set := edit.GetJSON(path, openCodeNativeKey+".compatibility.supportsPromptCacheKey"); set {
|
|
v = withoutOption(v, "compatibility", "supportsPromptCacheKey")
|
|
}
|
|
raw, ok := edit.GetJSON(path, openCodeNativeKey+".models")
|
|
if !ok {
|
|
return v
|
|
}
|
|
var have map[string]struct {
|
|
Variants []json.RawMessage `json:"variants"`
|
|
}
|
|
ms, _ := v.(map[string]any)["models"].(orderedJSON)
|
|
if json.Unmarshal([]byte(raw), &have) != nil {
|
|
return v
|
|
}
|
|
idOf := func(x any) string {
|
|
var e struct {
|
|
ID string `json:"id"`
|
|
}
|
|
b, _ := json.Marshal(x)
|
|
json.Unmarshal(b, &e)
|
|
return e.ID
|
|
}
|
|
for i, p := range ms {
|
|
h, ok := have[p.k]
|
|
if !ok {
|
|
continue
|
|
}
|
|
e, _ := p.v.(map[string]any)
|
|
ours, ok := e["variants"].([]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, rv := range h.Variants {
|
|
vid, own := openCodeOwnVariant(rv)
|
|
if own || vid == "" {
|
|
continue
|
|
}
|
|
if i := slices.IndexFunc(ours, func(o any) bool { return idOf(o) == vid }); i >= 0 {
|
|
ours[i] = rv
|
|
} else {
|
|
ours = append(ours, rv)
|
|
}
|
|
}
|
|
e["variants"] = ours
|
|
ms[i].v = e
|
|
}
|
|
return v
|
|
}, "models")
|
|
}
|
|
|
|
// openCodeOwnVariant is the id of a variant under providers, and whether
|
|
// it is magpie's: it says what magpie's of that name says (openCodeVariant)
|
|
// and nothing else, its options split as OpenCode 2 splits them or all in
|
|
// settings, as OpenChamber writes a variant it carries over from provider
|
|
// (config-v2.js, toModelVariants).
|
|
func openCodeOwnVariant(raw json.RawMessage) (string, bool) {
|
|
var v map[string]json.RawMessage
|
|
if json.Unmarshal(raw, &v) != nil {
|
|
return "", false
|
|
}
|
|
var id string
|
|
json.Unmarshal(v["id"], &id)
|
|
opts := map[string]json.RawMessage{}
|
|
for k, part := range v {
|
|
switch k {
|
|
case "id":
|
|
case "settings", "body":
|
|
var m map[string]json.RawMessage
|
|
if json.Unmarshal(part, &m) != nil {
|
|
return id, false
|
|
}
|
|
for ok, ov := range m {
|
|
if _, twice := opts[ok]; twice {
|
|
return id, false
|
|
}
|
|
opts[ok] = ov
|
|
}
|
|
default:
|
|
return id, false
|
|
}
|
|
}
|
|
b, err := json.Marshal(opts)
|
|
return id, err == nil && id != "" && sameJSON(string(b), openCodeVariant(id))
|
|
}
|
|
|
|
// syncOpenCodeNative brings magpie's provider under providers in the
|
|
// OpenCode config at path up to block, magpie's provider in OpenCode 1's
|
|
// words, where the file keeps it there.
|
|
func syncOpenCodeNative(path string, block func() any) error {
|
|
return syncJSONInOrder(path, openCodeNativeKey, openCodeNativeKept(path, block))
|
|
}
|
|
|
|
// openCodeWiringOff is wiringOff for magpie's provider in the OpenCode
|
|
// config at path: under provider, and under providers where the file keeps
|
|
// it there, which OpenCode 2 reads instead; under providers alone in a file
|
|
// written for OpenCode 2 (ocFile). With neither, it is gone.
|
|
func openCodeWiringOff(name, path, v1, key string) string {
|
|
under := func(prefix string) func(string) (string, bool) {
|
|
return func(k string) (string, bool) { return edit.GetJSON(path, prefix+k) }
|
|
}
|
|
_, legacy := edit.GetJSON(path, "provider."+magpieID)
|
|
_, native := edit.GetJSON(path, openCodeNativeKey)
|
|
if legacy || !native {
|
|
if off := wiringOff(name, path, under("provider."+magpieID+".options."), "baseURL", v1, "apiKey", key); off != "" {
|
|
return off
|
|
}
|
|
}
|
|
if !native {
|
|
return ""
|
|
}
|
|
return wiringOff(name, path, under(openCodeNativeKey+".settings."), "baseURL", v1, "apiKey", key)
|
|
}
|