Files
magpie/internal/agent/lists_models_test.go
T
yetone 611ce90d15 agent: OpenBot, magpie's models in its model picker (jiezhengj)
OpenBot (nightly-labs/openbot, openbot.run, v0.58.0) is a new row on the
Agents page. jiezhengj on Discord asked for it: today they type magpie's
address into OpenBot by hand and add each model one at a time.

OpenBot lists the user's own OpenAI-compatible endpoints from
openbot-custom-providers-v1.json in its userData folder:
~/Library/Application Support/OpenBot, %APPDATA%\OpenBot, or
~/.config/OpenBot. Connecting adds an entry with id "magpie" pointing at
the gateway's /v1 and holding the models picked for OpenBot, with
secret null. OpenBot seals an entry's key in safeStorage, which magpie
doesn't open, so the entry has no key. OpenBot hands it to OpenCode
keyless, and the gateway accepts it as a request from this computer.

What the entry keeps to:
- OpenBot's decoders drop the whole list over one bad entry. So the entry
  has at most 64 models, only ids OpenBot accepts as magpie/<id>, and a
  context window only inside 1,024 to 100,000,000.
- The user's other entries stay. The name, harness and secret they give
  magpie's entry in OpenBot stay too.
- A sync with nothing to change doesn't write.
- A file whose version isn't the number 1, or whose providers isn't a
  list, is one OpenBot refuses itself, so magpie leaves it alone.
- Disconnect takes out magpie's entry, and the file too when only the
  version and an empty list are left.

OpenBot reads the file at start, so while it runs, a notice says to
restart it. The notice is in every language.

OpenBot is detected by its userData folder, OpenBot.app
(app.openbot.desktop), or OpenBot.exe under %LOCALAPPDATA%\Programs or
%ProgramFiles%. Docs updated: agent-wiring.md, reference.md, and both
READMEs.

Run at the pushed head, on mote:
- gofmt -l: clean.
- go vet -tags nogui for internal/agent and internal/gui, and for ./...
  with GOOS=linux and GOOS=windows: ok.
- go test -tags nogui -count=1 -timeout 45m on internal/agent and every
  package that imports it (., backup, cli, davsync, gui, library,
  profile, stats, tui, uninstall, plugins/...): ok.
- GUI: agent-notice-lang, i18n-keys, header-relang and about-notices in
  Chromium and WebKit: 58 of 58 pass.

After rebasing on d20caffae (appdir, i18n.js and gui changed on main), these were run again: gofmt; linux and windows vet of agent, gui and appdir; go test of the same three: ok. The four GUI files plus checkin-now-account: 80 of 80 pass.

Real OpenBot 0.58.0: the arm64 zip, sha512 checked against
latest-mac.yml, notarized by Akudama GmbH. It ran on mote under a
sandbox HOME with --user-data-dir and --use-mock-keychain, on a file
that magpie's own openBotWire wrote.
- OpenBot started without setting the file aside.
- Asked over CDP, window.openbot.customProviders.list() returned magpie's
  entry with all three models and harness opencode.
- Saving another endpoint in OpenBot wrote magpie's entry back unchanged.
- The notice's pgrep pattern matched the running OpenBot.

Broken by hand, each red:
- the 64 cap
- the id filter
- the version check
- keeping the user's name, secret and harness
- writing version 1 into a new file
- the notice's Running check

One break stayed green: dropping openBotWire's own no-change check.
edit.SetJSON already skips a write that changes nothing, so the test
holds at that layer.

Not tried:
- a chat through OpenBot to the gateway. OpenBot's onboarding and its
  OpenCode download were not gone through, so no request reached magpie.
- OpenBot's MCP servers. They live in its SQLite database, which magpie
  doesn't write; magpie's /mcp/magpie/... servers are added by hand.
- Windows. Its install folder is read from electron-builder's defaults,
  not from a real install.
- Linux and the AppImage.
- A request from OpenBot is counted under OpenCode's name, since
  OpenCode sends it.
2026-11-02 05:37:32 +08:00

169 lines
6.6 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"slices"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// listsSandbox is a HOME of its own with one provider of two models.
func listsSandbox(t *testing.T) (home, cfg string) {
t.Helper()
home = t.TempDir()
cfg = filepath.Join(home, ".config")
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", cfg)
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}}); err != nil {
t.Fatal(err)
}
return home, cfg
}
// An agent whose switch puts magpie's models in a picker of its own, with
// no field of its picking among them, lists only the ones picked for it on
// the Agents page, as every other agent does: ZCode was given every model,
// with no way to hide one (#1508, NovaTrailX). ListsModels is what gives
// its row that list.
func TestEveryPickerOfMagpiesModelsCanBeNarrowed(t *testing.T) {
home, cfg := listsSandbox(t)
var bare, menus []string
for _, a := range builtins(home, cfg) {
vals := a.Values()
refs, magpie := false, false
for _, f := range a.Fields {
if f.Options == nil {
continue
}
for _, o := range f.Options(vals) {
refs = refs || o.Ref != ""
magpie = magpie || o.Value == magpieID
}
}
switch {
// Claude Desktop's tier pickers list the catalog once it is
// connected (desktopTierFields), and the sandbox has no Desktop
case refs, a.ID == "claude-desktop":
// Kiro Crew's models are its harness's: the agent it runs (Codex,
// Claude Code…), whose own row narrows them
case a.ID == "kirocrew":
case a.ListsModels:
menus = append(menus, a.ID)
case magpie:
bare = append(bare, a.ID)
}
}
if len(bare) > 0 {
t.Errorf("magpie's models are put in these agents' pickers with no way to pick which: %v", bare)
}
for _, id := range []string{"zcode", "pencil", "openpencil", "openbot", "newmax", "t3code", "workbuddy", "workbuddy-ai", copilotJBID, CursorLocalID} {
if !slices.Contains(menus, id) {
t.Errorf("%s's row has no model list to pick in (ListsModels): %v", id, menus)
}
}
}
// The models taken out of ZCode's list on the Agents page are out of both
// files it reads, config.json and provider_config.json, and come back when
// they are put back; what the user set by hand stays: their own providers,
// rules and keys, and their manual rule for a model of magpie's (#1508).
func TestZCodeListsOnlyThePicked(t *testing.T) {
home, _ := listsSandbox(t)
dir := filepath.Join(home, ".zcode", "v2")
path, rules := filepath.Join(dir, "config.json"), filepath.Join(dir, "provider_config.json")
os.MkdirAll(dir, 0o755)
os.WriteFile(path, []byte(`{"provider":{"builtin:bigmodel":{"name":"Bigmodel","kind":"anthropic","enabled":true},"mine":{"name":"Mine","kind":"openai","models":{"m1":{"name":"M1"}}}},"theme":"dark"}`), 0o644)
os.WriteFile(rules, []byte(`{"schemaVersion":1,"config":{"providerConfigRules":{"providerRules":[{"providerId":"mine","enabled":true,"config":{"personalModelIds":["m1"]}}]},"modelConfigRules":{"providerModelRules":[{"providerId":"mine","modelId":"m1","config":{"properties":{"contextWindow":1000}}}],"manualProviderModelRules":[{"providerId":"magpie","modelId":"deepseek/pro","config":{"enabled":true,"properties":{"contextWindow":4242}}}]}},"ui":{"lang":"zh"}}`), 0o600)
a := zcode(home)
if err := a.Field("provider").Set("magpie"); err != nil {
t.Fatal(err)
}
type configDoc struct {
Theme string `json:"theme"`
Provider map[string]map[string]any `json:"provider"`
}
readConfig := func() configDoc {
var c configDoc
b, _ := os.ReadFile(path)
if err := json.Unmarshal(b, &c); err != nil {
t.Fatalf("%v\n%s", err, b)
}
if c.Theme != "dark" || c.Provider["builtin:bigmodel"] == nil || c.Provider["mine"]["models"].(map[string]any)["m1"] == nil {
t.Fatalf("the user's own config went: %s", b)
}
return c
}
type rulesDoc struct {
UI map[string]any `json:"ui"`
Config struct {
ProviderConfigRules struct {
ProviderRules []map[string]any `json:"providerRules"`
} `json:"providerConfigRules"`
ModelConfigRules struct {
ProviderModelRules []map[string]any `json:"providerModelRules"`
ManualProviderModelRules []map[string]any `json:"manualProviderModelRules"`
} `json:"modelConfigRules"`
} `json:"config"`
}
readRules := func() rulesDoc {
var d rulesDoc
b, _ := os.ReadFile(rules)
if err := json.Unmarshal(b, &d); err != nil {
t.Fatalf("%v\n%s", err, b)
}
pr, mr := d.Config.ProviderConfigRules.ProviderRules, d.Config.ModelConfigRules
if d.UI["lang"] != "zh" || len(pr) != 2 || pr[0]["providerId"] != "mine" ||
!slices.ContainsFunc(mr.ProviderModelRules, func(r map[string]any) bool { return r["providerId"] == "mine" && r["modelId"] == "m1" }) ||
len(mr.ManualProviderModelRules) != 1 || mr.ManualProviderModelRules[0]["modelId"] != "deepseek/pro" {
t.Fatalf("the user's own rules went: %s", b)
}
return d
}
listed := func() (config, personal, ruled []string) {
for id := range readConfig().Provider["magpie"]["models"].(map[string]any) {
config = append(config, id)
}
d := readRules()
for _, id := range d.Config.ProviderConfigRules.ProviderRules[1]["config"].(map[string]any)["personalModelIds"].([]any) {
personal = append(personal, id.(string))
}
for _, r := range d.Config.ModelConfigRules.ProviderModelRules {
if r["providerId"] == magpieID {
ruled = append(ruled, r["modelId"].(string))
}
}
slices.Sort(config)
slices.Sort(personal)
return config, personal, ruled
}
both := []string{"deepseek/flash", "deepseek/pro"}
if c, p, _ := listed(); !slices.Equal(c, both) || !slices.Equal(p, both) {
t.Fatalf("connected: config.json %v, provider_config.json %v", c, p)
}
// taken out on the Agents page: gone from both, at the next sync
if err := provider.SetHiddenModels("zcode", []string{"deepseek/flash"}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if c, p, r := listed(); !slices.Equal(c, []string{"deepseek/pro"}) || !slices.Equal(p, []string{"deepseek/pro"}) || len(r) != 0 {
t.Fatalf("hidden: config.json %v, provider_config.json %v, rules %v", c, p, r)
}
// and back
if err := provider.SetHiddenModels("zcode", nil); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if c, p, r := listed(); !slices.Equal(c, both) || !slices.Equal(p, both) || !slices.Equal(r, []string{"deepseek/flash"}) {
t.Fatalf("shown again: config.json %v, provider_config.json %v, rules %v", c, p, r)
}
}