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.
339 lines
12 KiB
Go
339 lines
12 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
func TestOpenCodeDiscoversFrom(t *testing.T) {
|
|
for v, want := range map[string]bool{
|
|
"": false, "junk": false, "0.15.0": false, "1.2.0": false, "1.3.9": false,
|
|
"1.4.0": true, "1.19.6": true, "v1.19.6": true, "2.0.0": false, "2.0.4": false,
|
|
"2.0.5": true, "2.3.2": true, "2.10.0": true, "3.0.0": true,
|
|
} {
|
|
if got := openCodeDiscovers(v); got != want {
|
|
t.Errorf("openCodeDiscovers(%q) = %v, want %v", v, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// pluginOf is magpie's plugin beside the OpenCode config at path, "" with
|
|
// none.
|
|
func pluginOf(path string) string {
|
|
b, _ := os.ReadFile(openCodePluginPath(path))
|
|
return string(b)
|
|
}
|
|
|
|
// modelsUnder is the file's models under key, by id.
|
|
func modelsUnder(t *testing.T, path, key string) map[string]any {
|
|
t.Helper()
|
|
raw, ok := edit.GetJSON(path, key+".models")
|
|
if !ok {
|
|
return nil
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(raw), &m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// chawyehsu on X: OpenCode finds magpie's models itself, as
|
|
// yuhp/opencode-models-discovery does, so opencode.json has no list of
|
|
// them. For an OpenCode that loads plugins so, magpie's provider is in
|
|
// the file without models, and magpie's plugin, with the list it falls
|
|
// back on, beside it; both go when magpie's provider does.
|
|
func TestOpenCodeFindsMagpiesModels(t *testing.T) {
|
|
for _, c := range []struct{ version, key string }{{"1.19.6", "provider." + magpieID}, {"2.3.2", openCodeNativeKey}} {
|
|
t.Run(c.version, func(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
reasonCatalog(t)
|
|
openCodeAt(t, c.version)
|
|
oc := opencode(home, filepath.Join(home, ".config"))
|
|
if err := oc.Apply("model", "magpie/think/deep"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw := readFile(oc.Path)
|
|
if _, ok := edit.GetJSON(oc.Path, c.key); !ok {
|
|
t.Fatalf("no %s:\n%s", c.key, raw)
|
|
}
|
|
if ms, ok := edit.GetJSON(oc.Path, c.key+".models"); ok {
|
|
t.Fatalf("models listed in the file: %s", ms)
|
|
}
|
|
p := pluginOf(oc.Path)
|
|
if !strings.HasPrefix(p, openCodePluginMark) {
|
|
t.Fatalf("no plugin of magpie's: %q", p)
|
|
}
|
|
for _, w := range []string{`"think/deep"`, `"think/new"`, `"rsn/plan"`, gatewayV1(), `"v1":`, `"v2":`} {
|
|
if !strings.Contains(p, w) {
|
|
t.Fatalf("the plugin has no %s:\n%s", w, p)
|
|
}
|
|
}
|
|
if strings.Contains(p, "__MAGPIE__") {
|
|
t.Fatal("the plugin's list was never filled in")
|
|
}
|
|
if off := oc.Check(); off != "" {
|
|
t.Fatalf("Check: %s", off)
|
|
}
|
|
var wrote []string
|
|
if err := edit.Filtered(func(p string, b []byte) []byte { wrote = append(wrote, p); return b }, oc.Sync); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(wrote) > 0 {
|
|
t.Fatalf("a second sync wrote %v", wrote)
|
|
}
|
|
if err := oc.Apply("model", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v, ok := edit.GetJSON(oc.Path, c.key); ok {
|
|
t.Fatalf("%s left: %s", c.key, v)
|
|
}
|
|
if _, err := os.Stat(openCodePluginPath(oc.Path)); !os.IsNotExist(err) {
|
|
t.Fatalf("the plugin outlived magpie's provider: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// An OpenCode that doesn't load such a plugin (before 1.4, 2.0.0 to
|
|
// 2.0.4), or of a version magpie can't tell, has the list in the file as
|
|
// before; and one moving back to such an OpenCode gets the list back and
|
|
// magpie's plugin taken away.
|
|
func TestOpenCodeBeforePluginsKeepsTheList(t *testing.T) {
|
|
for _, v := range []string{"", "1.3.9", "2.0.4"} {
|
|
t.Run("v"+v, func(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
reasonCatalog(t)
|
|
openCodeAt(t, "2.3.2")
|
|
oc := opencode(home, filepath.Join(home, ".config"))
|
|
if err := oc.Apply("model", "magpie/think/deep"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if pluginOf(oc.Path) == "" {
|
|
t.Fatal("no plugin for 2.3.2")
|
|
}
|
|
openCodeAt(t, v)
|
|
if err := oc.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key := "provider." + magpieID
|
|
if strings.HasPrefix(v, "2.") {
|
|
key = openCodeNativeKey
|
|
}
|
|
ms := modelsUnder(t, oc.Path, key)
|
|
for _, id := range []string{"think/deep", "think/new", "rsn/plan"} {
|
|
if ms[id] == nil {
|
|
t.Fatalf("no %s under %s:\n%s", id, key, readFile(oc.Path))
|
|
}
|
|
}
|
|
if p := pluginOf(oc.Path); p != "" {
|
|
t.Fatalf("the plugin stayed for %q", v)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A config magpie wrote the list into moves: magpie's models go, and of
|
|
// them only what the user said stays (a variant of theirs, a reply limit
|
|
// they typed, with the rest of that limit), with a model magpie no
|
|
// longer lists gone, the user's other providers and options untouched. A
|
|
// model the user adds afterwards is theirs.
|
|
func TestOpenCodeListMovesToThePlugin(t *testing.T) {
|
|
for _, c := range []struct{ version, key string }{{"1.19.6", "provider." + magpieID}, {"2.3.2", openCodeNativeKey}} {
|
|
t.Run(c.version, func(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
reasonCatalog(t)
|
|
old := "1.3.9"
|
|
if strings.HasPrefix(c.version, "2.") {
|
|
old = "2.0.4"
|
|
}
|
|
openCodeAt(t, old)
|
|
oc := opencode(home, filepath.Join(home, ".config"))
|
|
if err := oc.Apply("model", "magpie/think/deep"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// the user's own: a provider, a timeout, a variant, a reply
|
|
// limit; and a model of magpie's from before
|
|
mine := map[string]any{"reasoningEffort": "low", "temperature": 0.3}
|
|
var variant any = mine
|
|
if strings.HasPrefix(c.version, "2.") {
|
|
variant = map[string]any{"id": "mine", "settings": mine}
|
|
ms := modelsUnder(t, oc.Path, c.key)
|
|
deep := ms["think/deep"].(map[string]any)
|
|
deep["variants"] = append(deep["variants"].([]any), variant)
|
|
if err := edit.SetJSON(oc.Path, edit.KV{Path: c.key + ".models", Value: ms}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else if err := edit.SetJSON(oc.Path, edit.KV{Path: c.key + ".models.think/deep.variants.mine", Value: variant}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
settings := "options"
|
|
if strings.HasPrefix(c.version, "2.") {
|
|
settings = "settings"
|
|
}
|
|
if err := edit.SetJSON(oc.Path,
|
|
edit.KV{Path: c.key + ".models.rsn/plan.limit.output", Value: 1234},
|
|
edit.KV{Path: c.key + ".models.gone/old", Value: map[string]any{"name": "old"}},
|
|
edit.KV{Path: c.key + "." + settings + ".timeout", Value: 600000},
|
|
edit.KV{Path: "provider.other", Value: map[string]any{"npm": "@ai-sdk/openai-compatible", "models": map[string]any{"x": map[string]any{}}}},
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
openCodeAt(t, c.version)
|
|
if err := oc.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw := readFile(oc.Path)
|
|
ms := modelsUnder(t, oc.Path, c.key)
|
|
if ms["gone/old"] != nil || ms["think/new"] != nil {
|
|
t.Fatalf("magpie's models stayed:\n%s", raw)
|
|
}
|
|
deep, _ := ms["think/deep"].(map[string]any)
|
|
if _, has := deep["name"]; has {
|
|
t.Fatalf("magpie's fields stayed on think/deep:\n%s", raw)
|
|
}
|
|
if strings.HasPrefix(c.version, "2.") {
|
|
vs, _ := deep["variants"].([]any)
|
|
if len(vs) != 1 || !sameJSONValue(vs[0], variant) {
|
|
t.Fatalf("think/deep's variants %v, want the user's alone:\n%s", vs, raw)
|
|
}
|
|
} else {
|
|
vs, _ := deep["variants"].(map[string]any)
|
|
if len(vs) != 1 || !sameJSONValue(vs["mine"], variant) {
|
|
t.Fatalf("think/deep's variants %v, want the user's alone:\n%s", vs, raw)
|
|
}
|
|
}
|
|
plan, _ := ms["rsn/plan"].(map[string]any)
|
|
// the limit stays whole, which OpenCode 1 needs ("Missing key
|
|
// …limit.context" for one without its context, 1.19.6)
|
|
if l, _ := plan["limit"].(map[string]any); l == nil || l["output"] != float64(1234) || l["context"] != float64(200000) {
|
|
t.Fatalf("rsn/plan's own limit.output lost, or its limit not whole:\n%s", raw)
|
|
}
|
|
if v, _ := edit.GetJSON(oc.Path, c.key+"."+settings+".timeout"); v != "600000" {
|
|
t.Fatalf("the user's timeout = %q:\n%s", v, raw)
|
|
}
|
|
if _, ok := edit.GetJSON(oc.Path, "provider.other.models.x"); !ok {
|
|
t.Fatalf("the user's provider changed:\n%s", raw)
|
|
}
|
|
if pluginOf(oc.Path) == "" {
|
|
t.Fatal("no plugin")
|
|
}
|
|
// a model the user adds now is theirs
|
|
if err := edit.SetJSON(oc.Path, edit.KV{Path: c.key + ".models.local/own", Value: map[string]any{"name": "own"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := oc.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if modelsUnder(t, oc.Path, c.key)["local/own"] == nil {
|
|
t.Fatalf("the user's model went:\n%s", readFile(oc.Path))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A file at the plugin's path magpie didn't write is the user's: never
|
|
// written over, and never removed.
|
|
func TestOpenCodeUsersOwnPluginStays(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
reasonCatalog(t)
|
|
openCodeAt(t, "1.19.6")
|
|
oc := opencode(home, filepath.Join(home, ".config"))
|
|
path := openCodePluginPath(oc.Path)
|
|
os.MkdirAll(filepath.Dir(path), 0o755)
|
|
const own = "export default {}\n"
|
|
if err := os.WriteFile(path, []byte(own), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := oc.Apply("model", "magpie/think/deep"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := pluginOf(oc.Path); got != own {
|
|
t.Fatalf("the user's file written over: %q", got)
|
|
}
|
|
if err := oc.Apply("model", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := pluginOf(oc.Path); got != own {
|
|
t.Fatalf("the user's file removed: %q", got)
|
|
}
|
|
}
|
|
|
|
// The plugin as OpenCode runs it (opencode_models.js, under node): for
|
|
// OpenCode 1 its config hook fills magpie's provider from the gateway,
|
|
// the user's fields over magpie's and a model of theirs kept; for OpenCode
|
|
// 2 its transform does, every model with variants; with the gateway down,
|
|
// both fall back on the list in the file.
|
|
func TestOpenCodePluginRuns(t *testing.T) {
|
|
node, err := exec.LookPath("node")
|
|
if err != nil {
|
|
t.Skip("no node to run the plugin")
|
|
}
|
|
home, _ := codexHome(t, "", "")
|
|
reasonCatalog(t)
|
|
src := openCodePluginFor(magpieProviderJSONAt("opencode", "opencode", "http://127.0.0.1:1"))
|
|
plugin := filepath.Join(home, "magpie-models.mjs")
|
|
if err := os.WriteFile(plugin, []byte(src), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
served, _ := json.Marshal(map[string]any{"opencode": map[string]any{"models": []any{
|
|
map[string]any{"id": "live/one", "entry": map[string]any{"name": "One", "limit": map[string]any{"context": 1000, "output": 100}, "variants": map[string]any{"high": map[string]any{"reasoningEffort": "high"}}}},
|
|
}}, "opencode2": map[string]any{"models": []any{
|
|
map[string]any{"id": "live/one", "entry": map[string]any{"name": "One", "limit": map[string]any{"context": 1000, "output": 100}, "variants": []any{}}},
|
|
}}})
|
|
run := func(mode string) map[string]any {
|
|
t.Helper()
|
|
abs, _ := filepath.Abs(filepath.Join("testdata", "opencode", "drive-plugin.mjs"))
|
|
cmd := exec.Command(node, abs, plugin, mode)
|
|
cmd.Env = append(os.Environ(), "MAGPIE_SERVED="+string(served))
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%s: %v\n%s", mode, err, out)
|
|
}
|
|
var r map[string]any
|
|
if err := json.Unmarshal(out, &r); err != nil {
|
|
t.Fatalf("%s: %v\n%s", mode, err, out)
|
|
}
|
|
return r
|
|
}
|
|
get := func(v any, path ...string) any {
|
|
for _, k := range path {
|
|
m, _ := v.(map[string]any)
|
|
v = m[k]
|
|
}
|
|
return v
|
|
}
|
|
// OpenCode 1, gateway up
|
|
r := run("v1-up")
|
|
if get(r, "live/one", "name") != "One" || get(r, "live/one", "limit", "output") != float64(4321) ||
|
|
get(r, "live/one", "limit", "context") != float64(1000) || get(r, "live/one", "variants", "high", "reasoningEffort") != "high" ||
|
|
get(r, "live/one", "variants", "mine", "temperature") != 0.5 || get(r, "local/own", "name") != "own" || r["think/deep"] != nil {
|
|
t.Fatalf("OpenCode 1, magpie up: %v", r)
|
|
}
|
|
// down: the list the file has
|
|
r = run("v1-down")
|
|
if get(r, "think/deep", "name") == nil || get(r, "rsn/plan", "name") == nil || get(r, "local/own", "name") != "own" || get(r, "live/one", "name") != nil {
|
|
t.Fatalf("OpenCode 1, magpie down: %v", r)
|
|
}
|
|
r = run("v2-up")
|
|
if get(r, "live/one", "name") != "One" || get(r, "live/one", "limit", "context") != float64(1000) || r["think/deep"] != nil {
|
|
t.Fatalf("OpenCode 2, magpie up: %v", r)
|
|
}
|
|
if vs, ok := get(r, "live/one", "variants").([]any); !ok || len(vs) != 0 {
|
|
t.Fatalf("OpenCode 2's variants: %v", r)
|
|
}
|
|
r = run("v2-down")
|
|
if get(r, "think/deep", "name") == nil || get(r, "rsn/plan", "name") == nil {
|
|
t.Fatalf("OpenCode 2, magpie down: %v", r)
|
|
}
|
|
if vs, ok := get(r, "think/deep", "variants").([]any); !ok || len(vs) == 0 {
|
|
t.Fatalf("OpenCode 2's think/deep has no variants: %v", r)
|
|
}
|
|
}
|