Files
magpie/internal/gateway/desktop_tier_test.go
T
yetone 03421f9f42 agent: Claude Desktop's row picks which magpie models Desktop's own model menu lists, several at once, and the menu names them by the model's own name (蓝猫 on Discord: wanted to pick a few models and switch among them in Desktop, and Desktop showed "DeepSeek V4.1 Flash (opencode-go)" where they want "DeepSeek V4.1 Flash") — Desktop already read its menu from the gateway's /v1/models for claude-desktop (desktopModels over provider.CatalogFor, i.e. every visible model minus the agent's HiddenModels, each under a claudeLooking alias), but its row offered no way to pick them: the start button stood the opus tier there as 「上次的选择」, and display_name was "Name (provider/model)" with that id again in description. Now Desktop's row (MENU_FROM_LIST in app.js) puts a model button where another agent's start pick stands: it names the first two picked models (+N, or "No models"), its title says Desktop reads the list as it starts so it must be quit and reopened after a change, and it opens the existing per-agent model list (openAgentModels, the am-pop multi-select posting {hidden} to /api/agent-models/claude-desktop), which stays open while several are ticked; the button, the line under the name ("Connected · n models in Claude Desktop's model menu") and, in the tray panel, the row's summary are refilled on each save instead of the row being redrawn; the panel's opened row has the same button under the provider, the expand link says Details, and the opened row's "In Claude Desktop" says the menu lists the models picked here and that Desktop reads it as it starts. The provider switch (default vs magpie) and the tiers are as before. /api/state's model count carries the first three shown models' names (modelCountJSON.Names). desktopNames gives each model its own name and keeps a suffix only where two picked models share one, case-insensitively: the provider id, or the whole id if that still collides; the description is gone. Models added later still show by default, as for every agent's list. zh strings added (no ja). TestClaudeDesktopPickedModels (two providers' DeepSeek V4.1 Flash read "(opencode-go)"/"(relay)" while both are listed, and after relay's, glm-5 and the routing groups are hidden Desktop lists exactly the three left, the DeepSeek plain, while another agent still lists all) and TestDesktopNames are new; the effort and tier tests map Desktop's rows by DesktopCatalogID instead of the description. internal/gui/tests/desktop-model-menu.test.cjs (window and panel, en and zh: the button's names and title, no 「上次的选择」, the list's ticks, unticking GLM-5 posts the whole hidden list and updates button, line and summary while the list stays open, ticking Kimi K3 back, no scroll, no native select) passes in webkit, as do desktop-tiers, agent-models, agent-connect, agent-start-pick, agent-layout, agent-grip, click-scroll, agent-models-all-hidden, agent-model-groups, agent-models-loading, agent-expand-steady, agent-disconnect, agent-tweak-fold, show-unset-agent, tray-cell-click, library-desktop-skills and by-agent. go vet ./internal/..., builds (darwin, GOOS=linux -tags nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried in real Claude Desktop: that its menu shows the plain names and only the picked models after a restart is from the /v1/models it is served, not seen in the app.
2026-10-04 05:01:12 +08:00

115 lines
3.9 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Claude Desktop's Code tab runs a subagent on a tier only when the
// gateway's /v1/models tags a model with it; untagged, every subagent ran on
// the chat's model (WilianWeng). A Claude model is tagged with its own tier,
// a model the user picks for a tier with that one as its default, and Claude
// Code's own id of a tier no row carries goes to the tier's pick.
func TestClaudeDesktopTiers(t *testing.T) {
f := &fake{reply: sse(
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m","type":"message","role":"assistant","model":"m1","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}`,
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)}
up := setup(t, provider.Anthropic, f)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Anthropic: up.URL,
Models: []string{"m1", "claude-sonnet-5", "m2"}}); err != nil {
t.Fatal(err)
}
desktop := Token + "-claude-desktop"
type row struct {
Tier string `json:"anthropic_family_tier"`
Default bool `json:"is_family_default"`
}
rows := func() map[string]row {
t.Helper()
req := httptest.NewRequest("GET", "/v1/models?limit=1000", nil)
req.Header.Set("x-api-key", desktop)
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, req)
var list struct {
Data []struct {
ID string `json:"id"`
row
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil {
t.Fatal(err)
}
out := map[string]row{}
for _, d := range list.Data {
out[DesktopCatalogID(d.ID)] = d.row
}
return out
}
send := func(model string) string {
t.Helper()
f.got = nil
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"`+model+`","max_tokens":32000,"stream":true,"tools":[{"name":"Bash","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`))
req.Header.Set("x-api-key", desktop)
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%s: %d %s", model, rec.Code, rec.Body)
}
return modelOf(f.got)
}
// nothing picked: the Claude model stands in for its own tier
got := rows()
if r := got["fake/claude-sonnet-5"]; r.Tier != "sonnet" || r.Default {
t.Fatalf("claude-sonnet-5 unpicked: %+v", r)
}
if r := got["fake/m1"]; r.Tier != "" {
t.Fatalf("m1 unpicked: %+v", r)
}
// m1 picked for opus and sonnet, m2 for haiku
for tier, id := range map[string]string{"opus": "fake/m1", "sonnet": "fake/m1", "haiku": "fake/m2"} {
if err := SetDesktopTier(tier, id); err != nil {
t.Fatal(err)
}
}
got = rows()
if r := got["fake/m1"]; r.Tier != "opus" || !r.Default {
t.Fatalf("m1 picked for opus and sonnet: %+v", r)
}
if r := got["fake/m2"]; r.Tier != "haiku" || !r.Default {
t.Fatalf("m2 picked for haiku: %+v", r)
}
if r := got["fake/claude-sonnet-5"]; r.Tier != "" {
t.Fatalf("claude-sonnet-5 with sonnet picked: %+v", r)
}
// Claude Code's own ids of a tier go to its pick, sonnet's too, which
// no row is tagged with; a Claude model magpie serves stays as asked
for asked, want := range map[string]string{
"claude-sonnet-4-6": "m1", "claude-opus-4-8[1m]": "m1", "claude-3-5-haiku-20241022": "m2",
"claude-haiku-4-5-20251001": "m2", "claude-sonnet-5": "claude-sonnet-5",
} {
if got := send(asked); got != want {
t.Errorf("%s: sent %q, want %q", asked, got, want)
}
}
if DesktopCatalogID(aliasFor("fake/m1")) != "fake/m1" || DesktopCatalogID("fake/m2") != "fake/m2" {
t.Fatal("DesktopCatalogID")
}
// unpicked again, the file goes
for _, tier := range DesktopTierNames {
if err := SetDesktopTier(tier, ""); err != nil {
t.Fatal(err)
}
}
if _, err := os.Stat(desktopTiersPath()); !os.IsNotExist(err) {
t.Fatalf("tiers file left: %v", err)
}
}