Files
magpie/internal/gateway/only_picked_test.go
T
yetone 5da6c24e80 agents, gui, cli: an agent can be shown only the models picked for it, so a model added later stays out of its list until ticked (#1337, nianlee-official)
nianlee-official asked for an agent whose model list grows only by hand:
today the Agents page's per-agent list hides models one at a time, and
every new model, of a new provider or an old one, shows up in the
agent's picker and config on its own ("New models are shown").

What changed:
- settings: PickedModels, per agent. An agent named in it is shown only
  the catalog entries listed for it (after Visible); HiddenModels is not
  read for it.
- provider: ModelOff(agent) is the one test CatalogFor, the Agents page
  count and Codex's own ChatGPT models (CodexNativeHidden) use, so the
  gateway's /v1/models for that agent's key, the lists magpie writes into
  its config and Codex's /model all narrow the same way hidden models do.
  SetOnlyPicked switches it on or off keeping what is shown as it is
  (on: what is shown becomes the picks; off: the listed models not
  picked become hidden). SetPickedModels keeps a pick whose provider is
  missing for now. renameModelPrefs moves picks with a provider rename.
  A model asked for by name still resolves against the full catalog.
- GUI: the list's foot "New models are shown" note is now a switch
  "Only models I pick", with a tooltip saying what each side does. On,
  a click sends what is ticked ({shown}), not what is hidden, so a model
  that arrived after the list was opened isn't picked by accident; the
  server still reads {hidden} from an older page. The model in use is
  always kept. Hide all / Show all's armed state takes the switch's room.
  zh, zh-TW, ja and de strings; the old note string is gone.
- CLI: magpie visible <agent> --only-picked | --show-new; magpie visible
  lists such an agent as "only models picked"; magpie models <agent>
  says how many are not picked, and what to do when none is.
- /api/settings keeps PickedModels like HiddenModels.
- docs/reference.md: the per-agent list bullet.
Not touched: #1302 (bulk agent visibility) and its files.

Run:
- go test -tags nogui ./... in a temp HOME: 44 packages ok.
- New: provider TestOnlyPickedModels, gui TestAgentModelsOnlyPicked,
  gateway TestOnlyPickedModelsList (opencode's key: a new provider's and
  an old provider's new model are left out of /v1/models, cursor's key
  has them, and a chat request naming fresh/x1 reaches the upstream) and
  TestCodexModelListOnlyPicked. With ModelOff made to ignore picks, all
  four fail.
- GUI: new agent-models-only-picked.test.cjs, Chromium and WebKit, en/zh/de,
  420 and 1100 wide (12/12). With save() sending {hidden} in pick mode it
  fails. agent-model*, agent-models*, codex-model-order, cursor-local-models,
  desktop-model-menu, click-scroll, gui-ja, gui-de, gui-zh-tw: 85/85.
- gofmt -l clean; go vet -tags nogui ./... on darwin, GOOS=linux and
  GOOS=windows; go build, and linux/windows builds.
- The built CLI in a sandbox HOME: visible codex --only-picked, a model
  added after it is left out of `magpie models codex` and kept in claude's;
  --show-new keeps it out and new ones show again.
Not tried: the real window against a live gateway, or a real agent
reading the narrowed config.
2026-10-09 08:50:14 +08:00

138 lines
4.8 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// An agent shown only the models picked for it (nianlee-official, #1337):
// with its key, /v1/models leaves out a model that came after the switch,
// of a new provider or an old one, while another agent's list has it; and
// the model asked for by name still goes through.
func TestOnlyPickedModelsList(t *testing.T) {
f := &fake{t: t, reply: sse(`data: {"choices":[{"delta":{"content":"ok"}}]}`, `data: [DONE]`)}
setup(t, provider.Chat, f)
list := func(key string) map[string]bool {
t.Helper()
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/v1/models", nil)
req.Header.Set("Authorization", "Bearer "+key)
New().Handler().ServeHTTP(rec, req)
var got struct {
Data []struct{ ID string } `json:"data"`
}
json.Unmarshal(rec.Body.Bytes(), &got)
out := map[string]bool{}
for _, m := range got.Data {
out[m.ID] = true
}
return out
}
if err := provider.SetOnlyPicked("opencode", true); err != nil {
t.Fatal(err)
}
if got := list(TokenFor("opencode")); !got["fake/m1"] {
t.Fatalf("switched on, what was shown went: %v", got)
}
// a new model of the provider it had, and a new provider
up := httptest.NewServer(f)
defer up.Close()
for _, p := range []provider.Provider{
{ID: "fake", Name: "Fake", Key: "k", Chat: up.URL + "/v1", Models: []string{"m1", "m2"}},
{ID: "fresh", Name: "Fresh", Key: "k", Chat: up.URL + "/v1", Models: []string{"x1"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
if got := list(TokenFor("opencode")); !got["fake/m1"] || got["fake/m2"] || got["fresh/x1"] {
t.Errorf("only picked: %v", got)
}
if got := list(TokenFor("cursor")); !got["fake/m2"] || !got["fresh/x1"] {
t.Errorf("another agent's list: %v", got)
}
// asked for by name, one it isn't shown still answers
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"fresh/x1","stream":true,"messages":[{"role":"user","content":"hi"}]}`))
req.Header.Set("Authorization", "Bearer "+TokenFor("opencode"))
New().Handler().ServeHTTP(rec, req)
if rec.Code != 200 || !strings.Contains(string(f.got), `"x1"`) {
t.Errorf("a model not picked, by name: %d %s; upstream got %s", rec.Code, rec.Body, f.got)
}
// ticked, it is listed
if err := provider.SetPickedModels("opencode", []string{"fake/m1", "fresh/x1"}); err != nil {
t.Fatal(err)
}
if got := list(TokenFor("opencode")); !got["fresh/x1"] || got["fake/m2"] {
t.Errorf("fresh/x1 ticked: %v", got)
}
}
// Codex's own ChatGPT models follow the switch too: a model the account
// gains after it is off Codex's list until ticked.
func TestCodexModelListOnlyPicked(t *testing.T) {
codexSignedIn(t)
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-5.5"}}); err != nil {
t.Fatal(err)
}
slugs := `{"slug":"gpt-5.5","priority":1}`
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("ETag", `"v1"`)
io.WriteString(w, `{"models":[`+slugs+`]}`)
}))
defer up.Close()
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
defer func() { provider.CodexBase = was }()
list := func() []string {
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", CodexPath+"/models", nil)
req.Header.Set("Authorization", "Bearer chatgpt-token")
New().Handler().ServeHTTP(rec, req)
var got struct {
Models []map[string]any `json:"models"`
}
json.Unmarshal(rec.Body.Bytes(), &got)
var native []string
for _, m := range got.Models {
if slug, _ := m["slug"].(string); strings.HasPrefix(slug, "gpt-") {
native = append(native, slug)
}
}
return native
}
if err := provider.SetOnlyPicked("codex", true); err != nil {
t.Fatal(err)
}
if native := list(); len(native) != 1 || native[0] != "gpt-5.5" {
t.Fatalf("switched on: %v", native)
}
slugs += `,{"slug":"gpt-6","priority":2}`
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-5.5", "gpt-6"}}); err != nil {
t.Fatal(err)
}
if native := list(); len(native) != 1 || native[0] != "gpt-5.5" {
t.Errorf("a model the account gained since: %v", native)
}
// off: what is shown stays (gpt-6 still off), and one that comes now shows
if err := provider.SetOnlyPicked("codex", false); err != nil {
t.Fatal(err)
}
if native := list(); len(native) != 1 || native[0] != "gpt-5.5" {
t.Errorf("switched off: %v", native)
}
slugs += `,{"slug":"gpt-6-mini","priority":3}`
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-5.5", "gpt-6", "gpt-6-mini"}}); err != nil {
t.Fatal(err)
}
if native := list(); len(native) != 2 || native[1] != "gpt-6-mini" {
t.Errorf("switched off, a new model: %v", native)
}
}