Files
magpie/internal/agent/opencode_variants_test.go
T
yetone 8323b0ae87 agent: OpenCode and OpenChamber list a magpie model's reasoning levels weakest first, not alphabetically (#713, Hu9956 on v0.1.780: 思考 offered 默认, High, Low, Max, Medium, Ultra, Xhigh, where other providers in the same OpenCode go by strength and MiniMax Code had magpie's in order) — openCodeVariants returned a map[string]any, which encoding/json writes with its keys sorted, so each model's variants object in opencode.json came out high, low, max, medium, ultra, xhigh, and OpenCode builds its variant list from the object in key order (checked with OpenCode 1.18.34: opencode models --verbose under a sandbox HOME/XDG dirs keeps a config's variants in the file's order, alphabetical or by strength; it doesn't sort them itself). openCodeVariants now returns an orderedJSON (a key/value list whose MarshalJSON writes keys as given) of the model's efforts put weakest first by gateway.ByStrength, the gateway's effortRank order (none, minimal, low, medium, high, xhigh, max, ultra; a level it doesn't know after them, stably), so a level appended after max (ultra) lands last too; edit.SetJSON keeps the custom marshaller's order when it indents the block. An opencode.json an older magpie wrote would have stayed alphabetical, as Sync's sameJSON compares decoded values and took it for the same block: OpenCode's (and its WSL and mimocode forms', through openCodeLike) and OpenChamber's Sync now use syncJSONInOrder, which also rewrites the block when its tokens, keys included, don't read in the same order as magpie's (sameOrder), and leaves one in order alone; the other agents' syncs are as before. TestOpenCodeVariants now checks the marshalled bytes (weakest first, an out-of-order list sorted, none as {}), and TestOpenCodeVariantsInOrder writes opencode.json under a temp HOME and checks the raw file lists low, medium, high, xhigh, max, ultra, that a block in the old alphabetical order is put in order by Sync and that a second Sync leaves the file byte for byte; both fail on the old code, and the Sync half fails with plain syncJSON. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried in OpenCode's or OpenChamber's own picker with the reporter's setup; OpenCode reads its config at start, so one already open shows the new order after it is reopened.
2026-10-04 03:19:37 +08:00

309 lines
11 KiB
Go

package agent
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"reflect"
"slices"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
func TestOpenCodeVariants(t *testing.T) {
for _, c := range []struct {
efforts []string
want string
}{
{[]string{"none", "high", "max"}, `{"none":{"reasoningEffort":"none"},"high":{"reasoningEffort":"high"},"max":{"reasoningEffort":"max"}}`},
{[]string{"low", "medium", "high", "xhigh"}, `{"low":{"reasoningEffort":"low"},"medium":{"reasoningEffort":"medium"},"high":{"reasoningEffort":"high"},"xhigh":{"reasoningEffort":"xhigh"}}`},
// weakest first whatever order the model lists them in (#713)
{[]string{"max", "ultra", "low", "xhigh", "medium", "high"}, `{"low":{"reasoningEffort":"low"},"medium":{"reasoningEffort":"medium"},"high":{"reasoningEffort":"high"},"xhigh":{"reasoningEffort":"xhigh"},"max":{"reasoningEffort":"max"},"ultra":{"reasoningEffort":"ultra"}}`},
// none at all: an empty set, so OpenCode 2 doesn't make low, medium
// and high of its own
{nil, `{}`},
} {
b, err := json.Marshal(openCodeVariants(c.efforts))
if err != nil || string(b) != c.want {
t.Errorf("%v: got %s (%v), want %s", c.efforts, b, err, c.want)
}
}
}
// variantOrder is the variants of the one model in an opencode.json, in the
// order the file's bytes list them.
func variantOrder(t *testing.T, b []byte) []string {
t.Helper()
i := bytes.Index(b, []byte(`"variants"`))
if i < 0 {
t.Fatalf("no variants: %s", b)
}
rest := b[i:]
at := map[string]int{}
var got []string
for _, e := range []string{"none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra"} {
if j := bytes.Index(rest, []byte(`"`+e+`":`)); j >= 0 {
at[e] = j
got = append(got, e)
}
}
slices.SortFunc(got, func(x, y string) int { return at[x] - at[y] })
return got
}
// TestOpenCodeVariantsInOrder: opencode.json lists a model's variants weakest
// first, as OpenCode and OpenChamber show them in the file's order; they
// were alphabetical — high, low, max, medium, ultra, xhigh (#713). A file an
// older magpie wrote so is put in order by the next sync, and one in order
// is left alone.
func TestOpenCodeVariantsInOrder(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
if err := provider.Save(provider.Provider{
ID: "think", Name: "Think", Chat: "https://example.test/v1", Key: "key", Models: []string{"deep"},
}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("think", "https://example.test/v1", []catalog.Model{
{ID: "deep", Efforts: []string{"low", "medium", "high", "xhigh", "max", "ultra"}},
}); err != nil {
t.Fatal(err)
}
want := []string{"low", "medium", "high", "xhigh", "max", "ultra"}
path := filepath.Join(home, ".config", "opencode", "opencode.json")
writeFile(t, path, "{\n \"theme\": \"dark\"\n}\n")
oc := opencode(home, filepath.Join(home, ".config"))
if err := oc.Field("model").Set("magpie/think/deep"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(path)
if got := variantOrder(t, b); !slices.Equal(got, want) {
t.Fatalf("written: %v, want %v\n%s", got, want, b)
}
// as an older magpie wrote it: the same block from a map, keys sorted
var block map[string]any
if err := json.Unmarshal([]byte(mustJSON(t, magpieProviderJSON("opencode"))), &block); err != nil {
t.Fatal(err)
}
if err := edit.SetJSON(path, edit.KV{Path: "provider.magpie", Value: block}); err != nil {
t.Fatal(err)
}
b, _ = os.ReadFile(path)
if got := variantOrder(t, b); !slices.Equal(got, []string{"high", "low", "max", "medium", "ultra", "xhigh"}) {
t.Fatalf("old block: %v\n%s", got, b)
}
if err := oc.Sync(); err != nil {
t.Fatal(err)
}
b, _ = os.ReadFile(path)
if got := variantOrder(t, b); !slices.Equal(got, want) {
t.Fatalf("synced: %v, want %v\n%s", got, want, b)
}
if err := oc.Sync(); err != nil {
t.Fatal(err)
}
if b2, _ := os.ReadFile(path); !bytes.Equal(b, b2) {
t.Fatalf("rewritten though in order:\n%s\n%s", b, b2)
}
}
func mustJSON(t *testing.T, v any) string {
t.Helper()
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
return string(b)
}
// TestOpenCodeWritesVariants: each model of magpie's in opencode.json names
// the reasoning levels it has as variants — OpenCode 2 otherwise offers low,
// medium and high for every one — and the rest of the entry is as before.
func TestOpenCodeWritesVariants(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
if err := provider.Save(provider.Provider{
ID: "think", Name: "Think", Chat: "https://example.test/v1", Key: "key",
Models: []string{"deep", "wide", "plain"},
}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("think", "https://example.test/v1", []catalog.Model{
{ID: "deep", Efforts: []string{"none", "high", "max"}, Context: 1000000, Output: 64000},
{ID: "wide", Efforts: []string{"low", "medium", "high", "xhigh", "max"}},
{ID: "plain"},
}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".config", "opencode", "opencode.json")
writeFile(t, path, `{"theme":"dark"}`)
if err := opencode(home, filepath.Join(home, ".config")).Field("model").Set("magpie/think/deep"); err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var cfg struct {
Theme string `json:"theme"`
Model string `json:"model"`
Provider map[string]struct {
NPM string `json:"npm"`
Models map[string]map[string]any `json:"models"`
} `json:"provider"`
}
if err := json.Unmarshal(b, &cfg); err != nil {
t.Fatal(err)
}
if cfg.Theme != "dark" || cfg.Model != "magpie/think/deep" {
t.Fatalf("config: %s", b)
}
p := cfg.Provider["magpie"]
if p.NPM != "@ai-sdk/openai-compatible" {
t.Fatalf("npm %q", p.NPM)
}
levels := func(id string) []string {
t.Helper()
m, ok := p.Models[id]
if !ok {
t.Fatalf("%s missing: %s", id, b)
}
vs, ok := m["variants"].(map[string]any)
if !ok {
t.Fatalf("%s has no variants: %v", id, m)
}
var out []string
for _, e := range []string{"none", "minimal", "low", "medium", "high", "xhigh", "max"} {
if v, ok := vs[e]; ok {
if !reflect.DeepEqual(v, map[string]any{"reasoningEffort": e}) {
t.Fatalf("%s %s: %v", id, e, v)
}
out = append(out, e)
}
}
if len(out) != len(vs) {
t.Fatalf("%s: unexpected variants %v", id, vs)
}
return out
}
if got := levels("think/deep"); !reflect.DeepEqual(got, []string{"none", "high", "max"}) {
t.Fatalf("deep: %v", got)
}
if got := levels("think/wide"); !reflect.DeepEqual(got, []string{"low", "medium", "high", "xhigh", "max"}) {
t.Fatalf("wide: %v", got)
}
if got := levels("think/plain"); got != nil {
t.Fatalf("plain: %v", got)
}
if lim, _ := p.Models["think/deep"]["limit"].(map[string]any); lim["context"] != float64(1000000) {
t.Fatalf("deep limit: %v", p.Models["think/deep"])
}
}
// Xiaomi's mimo-v2.6-flash, which models.dev lists under Xiaomi with a
// thinking switch alone, gets no variants in opencode.json — under Xiaomi's
// preset or a relay of the user's alike — and no thinking levels in Pi:
// OpenCode offered it the resellers' levels up to max, which Xiaomi turns
// away with a 400 (#214). A model no maker lists keeps the resellers'.
func TestOpenCodeNoResellerLevelsForSwitchModel(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{
"xiaomi": {"models": {"mimo-v2.6-flash": {"id":"mimo-v2.6-flash","reasoning":true,"reasoning_options":[{"type":"toggle"}]}}},
"llmgateway": {"models": {
"deepinfra/mimo-v2.6-flash": {"id":"deepinfra/mimo-v2.6-flash","reasoning_options":[{"type":"effort","values":["none","minimal","low","medium","high","xhigh","max"]}]},
"xiaomi/mimo-v2.6-flash": {"id":"xiaomi/mimo-v2.6-flash","reasoning_options":[{"type":"effort","values":["none","minimal","low","medium","high","xhigh","max"]}]},
"open-model": {"id":"open-model","reasoning_options":[{"type":"effort","values":["low","high","max"]}]}}}
}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
xiaomi, err := provider.FromPreset("xiaomi")
if err != nil {
t.Fatal(err)
}
xiaomi.Key, xiaomi.Models = "k", []string{"mimo-v2.6-flash"}
if err := provider.Save(xiaomi); err != nil {
t.Fatal(err)
}
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: "https://relay.test/v1", Key: "k",
Models: []string{"mimo-v2.6-flash", "open-model", "mystery-model"}}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".config", "opencode", "opencode.json")
writeFile(t, path, `{}`)
if err := opencode(home, filepath.Join(home, ".config")).Field("model").Set("magpie/xiaomi/mimo-v2.6-flash"); err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var cfg struct {
Provider map[string]struct {
Models map[string]struct {
Variants map[string]any `json:"variants"`
} `json:"models"`
} `json:"provider"`
}
if err := json.Unmarshal(b, &cfg); err != nil {
t.Fatal(err)
}
ms := cfg.Provider["magpie"].Models
for id, want := range map[string]int{"xiaomi/mimo-v2.6-flash": 0, "relay/mimo-v2.6-flash": 0, "relay/open-model": 3} {
m, ok := ms[id]
if !ok || m.Variants == nil || len(m.Variants) != want {
t.Errorf("%s: variants %v (listed %v), want %d", id, m.Variants, ok, want)
}
}
pi, _ := json.Marshal(magpieProviderJSON("pi"))
var piCfg struct {
Models []struct {
ID string `json:"id"`
Reasoning bool `json:"reasoning"`
Levels map[string]any `json:"thinkingLevelMap"`
} `json:"models"`
}
json.Unmarshal(pi, &piCfg)
seen := 0
for _, m := range piCfg.Models {
switch m.ID {
case "xiaomi/mimo-v2.6-flash", "relay/mimo-v2.6-flash":
seen++
if m.Reasoning || m.Levels != nil {
t.Errorf("pi %s: reasoning %v, levels %v", m.ID, m.Reasoning, m.Levels)
}
case "relay/open-model":
// the full map, levels the model lacks null so Pi hides them (#243)
seen++
want := map[string]any{"off": nil, "minimal": nil, "low": "low", "medium": nil, "high": "high", "xhigh": nil, "max": "max"}
if !m.Reasoning || !reflect.DeepEqual(m.Levels, want) {
t.Errorf("pi %s: reasoning %v, levels %v", m.ID, m.Reasoning, m.Levels)
}
case "relay/mystery-model":
// levels magpie doesn't know: Pi's own, as before
seen++
if m.Reasoning || m.Levels != nil {
t.Errorf("pi %s: reasoning %v, levels %v", m.ID, m.Reasoning, m.Levels)
}
}
}
if seen != 4 {
t.Errorf("pi models: %s", pi)
}
}