diff --git a/internal/gateway/desktop.go b/internal/gateway/desktop.go index 4d15ebf49..47727fd3c 100644 --- a/internal/gateway/desktop.go +++ b/internal/gateway/desktop.go @@ -139,14 +139,15 @@ func claudeLooking(e provider.Entry) string { // Claude Code (claudeLooking). func DesktopID(e provider.Entry) string { return claudeLooking(e) } -// desktopModels is /v1/models as Claude Desktop is shown it: every model by -// an id it keeps (claudeLooking), named so the picker tells them apart — -// it shows the name, not the id, and folds rows of one name into one entry. +// desktopModels is /v1/models as Claude Desktop is shown it: every model +// picked for it (provider.CatalogFor, the Agents page's model list) by an id +// it keeps (claudeLooking), named by its own name alone (蓝猫: "DeepSeek +// V4.1 Flash", not with its provider's beside it). Desktop shows the name, +// not the id, and its description after it, so none is given; it folds rows +// of one name into one entry, so two models of one name keep their +// provider's after it (desktopNames). func desktopModels(entries []provider.Entry) []map[string]any { - names := map[string]int{} - for _, e := range entries { - names[desktopName(e)]++ - } + names := desktopNames(entries) // the tier each model stands in for: the user's pick (a model picked // for two is tagged with the first, desktopTierTurn sends the other), // else a Claude model's own @@ -158,16 +159,10 @@ func desktopModels(entries []provider.Entry) []map[string]any { } } data := make([]map[string]any, 0, len(entries)) - for _, e := range entries { + for i, e := range entries { m := modelObject(e) - name := desktopName(e) - if names[name] > 1 && name != e.ID { - name += " (" + e.ID + ")" - } - m["display_name"] = name - if m["id"] = claudeLooking(e); m["id"] != e.ID { - m["description"] = e.ID + " in magpie" - } + m["display_name"] = names[i] + m["id"] = claudeLooking(e) if t := tierOf[e.ID]; t != "" { m["anthropic_family_tier"], m["is_family_default"] = t, true } else if t := claudeTier(e.ID); t != "" && picked[t] == "" { @@ -272,6 +267,41 @@ func desktopName(e provider.Entry) string { return e.ID } +// desktopNames are the names Claude Desktop is shown entries by, in order: +// each its own name, but for two or more of one name (case aside), which +// get their provider's id after it — "DeepSeek V4.1 Flash (opencode-go)" — +// or, where that still leaves two alike (two accounts of one provider), the +// whole catalog id. +func desktopNames(entries []provider.Entry) []string { + count := func(names []string) map[string]int { + n := map[string]int{} + for _, s := range names { + n[strings.ToLower(s)]++ + } + return n + } + out := make([]string, len(entries)) + for i, e := range entries { + out[i] = desktopName(e) + } + same := count(out) + by := make([]string, len(entries)) + for i, e := range entries { + by[i] = out[i] + if same[strings.ToLower(out[i])] > 1 && out[i] != e.ID { + whose, _, _ := strings.Cut(e.ID, "/") + by[i] += " (" + whose + ")" + } + } + still := count(by) + for i, e := range entries { + if by[i] != out[i] && still[strings.ToLower(by[i])] > 1 { + by[i] = out[i] + " (" + e.ID + ")" + } + } + return by +} + // aliased is the catalog id an alias Claude Desktop was given stands for: // anthropic/magpie- (as it was listed before too), // mythos-magpie- or magpie-.anthropic., with diff --git a/internal/gateway/desktop_effort_test.go b/internal/gateway/desktop_effort_test.go index 484203c5f..ec9ff9464 100644 --- a/internal/gateway/desktop_effort_test.go +++ b/internal/gateway/desktop_effort_test.go @@ -96,8 +96,7 @@ func TestClaudeDesktopEffortIDs(t *testing.T) { New().Handler().ServeHTTP(rec, req) var list struct { Data []struct { - ID string `json:"id"` - Description string `json:"description"` + ID string `json:"id"` } `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil { @@ -105,7 +104,7 @@ func TestClaudeDesktopEffortIDs(t *testing.T) { } got := map[string]string{} for _, m := range list.Data { - got[strings.TrimSuffix(m.Description, " in magpie")] = m.ID + got[DesktopCatalogID(m.ID)] = m.ID } for id, want := range map[string]string{ "fake/m1": desktopEffortAlias + aliasNumber("fake/m1"), diff --git a/internal/gateway/desktop_models_test.go b/internal/gateway/desktop_models_test.go new file mode 100644 index 000000000..aea59d909 --- /dev/null +++ b/internal/gateway/desktop_models_test.go @@ -0,0 +1,111 @@ +package gateway + +import ( + "encoding/json" + "net/http/httptest" + "slices" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// Claude Desktop's model menu lists the models picked for it on the Agents +// page (蓝猫): /v1/models, asked with its key, holds those and no other, each +// named by its own name (no provider's beside it, no description after it), +// and two of one name keep their provider's after it so they stay apart. +func TestClaudeDesktopPickedModels(t *testing.T) { + setup(t, provider.Anthropic, &fake{}) + for _, p := range []provider.Provider{ + {ID: "opencode-go", Name: "OpenCode Go", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"deepseek-v4.1-flash", "glm-5"}}, + {ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"deepseek-v4.1-flash"}}, + {ID: "grok-2", Name: "Grok", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"grok-4.6"}}, + } { + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + } + for ref, name := range map[string]string{"opencode-go/deepseek-v4.1-flash": "DeepSeek V4.1 Flash", "relay/deepseek-v4.1-flash": "DeepSeek V4.1 Flash", "grok-2/grok-4.6": "grok-4.6"} { + if err := provider.SetModelName(ref, name); err != nil { + t.Fatal(err) + } + } + list := func() map[string]desktopRow { + t.Helper() + req := httptest.NewRequest("GET", "/v1/models?limit=1000", nil) + req.Header.Set("x-api-key", TokenFor("claude-desktop")) + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, req) + var out struct { + Data []desktopRow `json:"data"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatal(err) + } + got := map[string]desktopRow{} + for _, d := range out.Data { + if d.Description != "" { + t.Errorf("%s has a description: %q", d.DisplayName, d.Description) + } + got[DesktopCatalogID(d.ID)] = d + } + return got + } + keys := func(m map[string]desktopRow) []string { + var out []string + for k := range m { + out = append(out, k) + } + slices.Sort(out) + return out + } + + // both DeepSeeks shown: each with its provider's after it + all := list() + if all["opencode-go/deepseek-v4.1-flash"].DisplayName != "DeepSeek V4.1 Flash (opencode-go)" || + all["relay/deepseek-v4.1-flash"].DisplayName != "DeepSeek V4.1 Flash (relay)" || all["grok-2/grok-4.6"].DisplayName != "grok-4.6" { + t.Fatalf("both shown: %v", all) + } + + // three picked, the other DeepSeek, the routing group magpie found for + // the two and GLM left out: only those three, the DeepSeek by its name + // alone + hide := []string{"relay/deepseek-v4.1-flash", "opencode-go/glm-5"} + for id := range all { + if strings.HasPrefix(id, provider.GroupPrefix) { + hide = append(hide, id) + } + } + if err := provider.SetHiddenModels("claude-desktop", hide); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { provider.SetHiddenModels("claude-desktop", nil) }) + got := list() + if want := []string{"fake/m1", "grok-2/grok-4.6", "opencode-go/deepseek-v4.1-flash"}; !slices.Equal(keys(got), want) { + t.Fatalf("Desktop lists %v, want %v", keys(got), want) + } + if n := got["opencode-go/deepseek-v4.1-flash"].DisplayName; n != "DeepSeek V4.1 Flash" { + t.Fatalf("named %q, want the name alone", n) + } + // another agent's list is its own + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil)) + var other struct{ Data []struct{ ID string } } + json.Unmarshal(rec.Body.Bytes(), &other) + if len(other.Data) != len(all) { + t.Fatalf("another agent's list: %s", rec.Body) + } +} + +// desktopNames: the name alone, a provider's id after two of one name, the +// whole id where that still leaves two alike. +func TestDesktopNames(t *testing.T) { + es := []provider.Entry{ + {ID: "a/x", Name: "X"}, {ID: "b/x", Name: "x"}, {ID: "c/y", Name: "Y"}, + {ID: "d/z", Name: "Z"}, {ID: "d/z2", Name: "Z"}, {ID: "e/w"}, + } + want := []string{"X (a)", "x (b)", "Y", "Z (d/z)", "Z (d/z2)", "e/w"} + if got := desktopNames(es); !slices.Equal(got, want) { + t.Fatalf("desktopNames = %q, want %q", got, want) + } +} diff --git a/internal/gateway/desktop_test.go b/internal/gateway/desktop_test.go index 0a0898a5f..c6d7af7dd 100644 --- a/internal/gateway/desktop_test.go +++ b/internal/gateway/desktop_test.go @@ -83,13 +83,13 @@ func TestClaudeDesktopAliases(t *testing.T) { rec := httptest.NewRecorder() New().Handler().ServeHTTP(rec, req) var list struct { - Data []struct{ ID, DisplayName, Description string } `json:"data"` + Data []desktopRow `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil { t.Fatal(err) } alias := aliasFor("fake/m1") - var row *struct{ ID, DisplayName, Description string } + var row *desktopRow for i := range list.Data { if list.Data[i].ID == alias { row = &list.Data[i] @@ -98,7 +98,7 @@ func TestClaudeDesktopAliases(t *testing.T) { t.Errorf("Desktop would drop %q", list.Data[i].ID) } } - if row == nil || row.Description != "fake/m1 in magpie" { + if row == nil || row.DisplayName != "m1" || row.Description != "" { t.Fatalf("Claude Desktop by its User-Agent: %s", rec.Body) } for _, asked := range []string{alias, alias + "[1m]"} { @@ -137,3 +137,9 @@ func TestClaudeDesktopKnown(t *testing.T) { } } } + +type desktopRow struct { + ID string `json:"id"` + DisplayName string `json:"display_name"` + Description string `json:"description"` +} diff --git a/internal/gateway/desktop_tier_test.go b/internal/gateway/desktop_tier_test.go index d266ab001..20637f7cd 100644 --- a/internal/gateway/desktop_tier_test.go +++ b/internal/gateway/desktop_tier_test.go @@ -37,8 +37,7 @@ func TestClaudeDesktopTiers(t *testing.T) { New().Handler().ServeHTTP(rec, req) var list struct { Data []struct { - ID string `json:"id"` - Description string `json:"description"` + ID string `json:"id"` row } `json:"data"` } @@ -47,11 +46,7 @@ func TestClaudeDesktopTiers(t *testing.T) { } out := map[string]row{} for _, d := range list.Data { - id := strings.TrimSuffix(d.Description, " in magpie") - if d.Description == "" { - id = d.ID - } - out[id] = d.row + out[DesktopCatalogID(d.ID)] = d.row } return out } diff --git a/internal/gui/agent_models.go b/internal/gui/agent_models.go index 5287dcb8c..71787237a 100644 --- a/internal/gui/agent_models.go +++ b/internal/gui/agent_models.go @@ -1,6 +1,7 @@ package gui import ( + "cmp" "encoding/json" "net/http" "strings" @@ -21,8 +22,14 @@ type modelCountJSON struct { // By is the models shown by whose they are, a connected agent's // expanded row's chips (OpenAI 6 · OpenRouter 24) By []modelGroupJSON `json:"by,omitempty"` + // Names are the first few models shown by name, for a row that says + // which it lists rather than how many (Claude Desktop's) + Names []string `json:"names,omitempty"` } +// countNames is how many models' names modelCount gives. +const countNames = 3 + // modelGroupJSON is how many of the models shown one provider (or the // routing groups) gives. type modelGroupJSON struct { @@ -102,6 +109,9 @@ func modelCount(id string) *modelCountJSON { continue } c.Shown++ + if len(c.Names) < countNames { + c.Names = append(c.Names, cmp.Or(e.Name, e.Model, e.ID)) + } g := modelGroupJSON{Name: e.Provider.Name, Icon: e.Provider.Icon} if e.Group != "" { g = modelGroupJSON{Name: agent.RoutingGroups, Icons: e.Icons} diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index dcb6651c6..dcf63e431 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -1119,7 +1119,7 @@ body.panel { --row-spring: cubic-bezier(.25, .3, .1, 1); } .ag-open .field { display: flex; justify-content: flex-start; width: 100%; height: 30px; gap: 8px; padding: 0 10px; border-radius: 8px; background: var(--card); font-size: 12.5px; box-shadow: inset 0 0 0 1px var(--pill-line); } body.panel .ag-open .field:hover { background: var(--card); box-shadow: inset 0 0 0 1px var(--pill-line), var(--pill-shadow); } body.panel .ag-open .field.open { box-shadow: inset 0 0 0 1px var(--accent); } -.ag-open .field .k { order: 0; width: 44px; font-size: 11px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } +.ag-open .field .k { order: 0; width: 44px; text-align: left; font-size: 11px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .ag-open .field .v { order: 1; flex: 1 1 auto; font-weight: 400; text-align: left; } .ag-open .field > .ic, .ag-open .field > .ic-stack { order: 2; flex: none; } .row.agent .ag-open .field .ic { width: 14px; height: 14px; } diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 16d64f179..f06c6ec86 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -337,7 +337,7 @@ function renderAgents() { }); v.append(mi); } - v.append(el("span", "vt", main ? (opt?.label || main.value || t("default")) : "")); + v.append(el("span", "vt", menuFromList(a) ? menuSaid(a.models) : main ? (opt?.label || main.value || t("default")) : "")); sum.append(v); // how much effort as three bars, in a column of its own down the list: // none lit for the default, off, none or auto, or for an agent that has @@ -360,6 +360,13 @@ function renderAgents() { if (f && !b.querySelector(":scope > .k")) b.prepend(el("span", "k", t(f.label))); body.append(b); } + // Claude Desktop's: which models its own menu lists, under its + // provider + if (menuFromList(a)) { + const m = menuButton(a, "main"); + m.prepend(el("span", "k", t("Models"))); + body.append(m); + } if (extras.childNodes.length) body.append(extras); row.classList.toggle("open", panelOpenAgent === a.id); row.setAttribute("aria-expanded", String(panelOpenAgent === a.id)); @@ -765,6 +772,7 @@ const PICKS_IN = { codex: "/model", claude: "/model", opencode: "/models", pi: " function connectSaid(a) { if (!a.wired) return t("Not connected · {agent} uses its own settings", { agent: a.name }); + if (menuFromList(a)) return t("Connected · {n} models in {agent}'s model menu", { n: a.models.shown, agent: a.name }); const at = PICKS_IN[a.id]; return at ? t("Connected · pick magpie's models with {cmd} in {agent}", { cmd: at, agent: a.name }) : t("Connected · magpie's models are in {agent}'s model list", { agent: a.name }); } @@ -816,6 +824,49 @@ const NATIVE_ONLY = { // agents that pick no model once started: the one they start on stays in // the row const NO_PICKER = new Set(["gemini", "hermes", "agy", "muse"]); +// agents whose own model menu is the list picked here: no one model to +// start on, but which of magpie's it offers (Claude Desktop reads +// /v1/models as it starts, and the user switches among them in it) +const MENU_FROM_LIST = new Set(["claude-desktop"]); +const menuFromList = (a) => MENU_FROM_LIST.has(a.id) && a.wired && !!a.models; + +// menuSaid: the models such an agent lists, by name while they are few +function menuSaid(c) { + if (!c.shown) return t("No models"); + const names = c.names || []; + if (!names.length) return t("{n} models", { n: c.shown }); + return names.slice(0, 2).join(", ") + (c.shown > 2 ? " +" + (c.shown - 2) : ""); +} + +// menuButton: where the model picker stands in another agent's row, the +// models its own menu lists, several picked at once (openAgentModels) +function menuButton(a, cls = "ag-start") { + const b = el("button", "field ag-menu " + cls); + b.type = "button"; + b.dataset.key = "models"; + fillMenuButton(b, a); + b.onclick = (ev) => openAgentModels(a, b, ev); + // drawn again while its list is open: the list stays, held to it + if (agentModels?.a.id === a.id) { + b.classList.add("open"); + agentModels.anchor = b; + } + return b; +} +function fillMenuButton(b, a) { + const c = a.models; + const k = b.querySelector(":scope > .k"); + b.replaceChildren(...(k ? [k] : [])); + b.append(icon("magpie")); + b.append(el("span", "v" + (c.shown ? "" : " empty"), menuSaid(c))); + const ch = el("span", "chev"); + ch.append(svg(CHEV, 11, 1.7)); + b.append(ch); + const pick = t("Pick which models {agent} lists", { agent: a.name }); + b.title = pick + "\n" + t("{agent} reads its model list as it starts: quit and reopen it after a change", { agent: a.name }); + b.setAttribute("aria-label", pick + ": " + menuSaid(c)); +} + // what turning one on costs, said in its opened row const CONNECT_COST = { gemini: "Gemini CLI's requests all go through magpie; its Google sign-in is turned off first and comes back when you disconnect", @@ -896,7 +947,8 @@ function connectLine(a, kind) { return line; } const at = PICKS_IN[a.id]; - if (a.id === "claude") say(t("Connected · switch the opus · sonnet · haiku tiers in /model")); + if (menuFromList(a)) say(connectSaid(a)); + else if (a.id === "claude") say(t("Connected · switch the opus · sonnet · haiku tiers in /model")); else if (NO_PICKER.has(a.id)) say(t("Connected · starts on the model picked here")); else if (at && a.models) say(t("Connected · {n} models in {agent}'s {cmd}", { n: a.models.shown, agent: a.name, cmd: at })); else say(connectSaid(a)); return line; @@ -908,7 +960,7 @@ function expandLink(a) { const b = el("button", "ag-link" + (open ? " open" : "")); b.type = "button"; b.setAttribute("aria-expanded", String(open)); - b.append(el("span", "", open ? t("Collapse") : a.id === "claude" ? t("Tiers") : a.models ? t("Models {n}", { n: a.models.shown }) : t("Details"))); + b.append(el("span", "", open ? t("Collapse") : a.id === "claude" ? t("Tiers") : a.models && !MENU_FROM_LIST.has(a.id) ? t("Models {n}", { n: a.models.shown }) : t("Details"))); if (!open) { const c = el("span", "chev"); c.append(svg(CHEV_R, 10, 1.6)); @@ -981,7 +1033,7 @@ function connectRow(a, row, who, { fields, extras, fieldBtn, sw, kind }) { who.append(connectLine(a, kind)); // the model it starts on, one click away beside the switch (the owner: // both ways at once): picked here, it is connected first if it wasn't - const start = kind === "ok" ? startField(a) : null; + const start = kind === "ok" && !MENU_FROM_LIST.has(a.id) ? startField(a) : null; if (start) fields.querySelector(`:scope > [data-key="${CSS.escape(start.key)}"]`)?.remove(); if (kind === "ok") { if (a.wired) row.append(expandLink(a)); @@ -990,6 +1042,8 @@ function connectRow(a, row, who, { fields, extras, fieldBtn, sw, kind }) { const launch = a.wired && fields.querySelector(".field.launch"); if (launch) row.append(launch); if (start) row.append(startButton(a, start, fieldBtn)); + // Claude Desktop's: which models its own menu lists + else if (menuFromList(a)) row.append(menuButton(a)); row.append(sw); } else if (kind === "empty") { const add = el("button", "ag-add", t("Add a provider")); @@ -1087,6 +1141,8 @@ function connectPanel(a, { fields, fieldBtn }) { parts.push(w); } kv(t("In {agent}", { agent: a.name }), ...parts); + } else if (menuFromList(a)) { + kv(t("In {agent}", { agent: a.name }), line(t("{agent}'s model menu lists the models picked here; switch among them there. It reads the list as it starts: quit and reopen it after a change.", { agent: a.name }))); } // Claude Code's tiers, each with the /model that picks it if (a.id === "claude") { @@ -1865,11 +1921,18 @@ async function openAgentModels(a, anchor, ev) { if (!g) by.push(g = m.group === ROUTING_GROUPS ? { name: m.group, icons: m.icons, n: 0 } : { name: m.group, icon: m.icon, n: 0 }); g.n++; } - const count = { shown: models.length - hidden.length, listed: models.length, by }; + const names = models.filter((m) => !m.hidden).slice(0, 3).map((m) => m.name); + const count = { shown: models.length - hidden.length, listed: models.length, by, names }; me.count = a.models = count; - // the line under the name counts them; an opened row is drawn again + // the line under the name counts them, a menu's button names them; an + // opened row is drawn again if (me.anchor.classList.contains("ag-models")) fillModelsEntry(me.anchor, a); - else renderAgents(); + else if (me.anchor.classList.contains("ag-menu")) { + fillMenuButton(me.anchor, a); + const row = me.anchor.closest(".row.agent"); + for (const said of row?.querySelectorAll(".ag-st.on .ag-st-t, .ag-conn-said") || []) said.textContent = connectSaid(a); + row?.querySelector(".ag-sum .vt")?.replaceChildren(menuSaid(count)); + } else renderAgents(); me.saving = me.saving.then(() => api("agent-models/" + encodeURIComponent(a.id), { hidden })) .catch((e) => status(e.message, "err")); }; diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 5fe9d68de..15ccb20ed 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -93,6 +93,10 @@ const I18N = { "All {n} models": "全部 {n} 个模型", "Showing {shown} / {listed} models": "显示 {shown} / {listed} 个模型", "Pick which models {agent} lists": "选择 {agent} 列出哪些模型", + "No models": "没有模型", + "Connected · {n} models in {agent}'s model menu": "已接入 · {agent} 的模型菜单里有 {n} 个模型", + "{agent} reads its model list as it starts: quit and reopen it after a change": "{agent} 启动时读取模型列表:改动后退出并重新打开它", + "{agent}'s model menu lists the models picked here; switch among them there. It reads the list as it starts: quit and reopen it after a change.": "{agent} 的模型菜单只列出这里选中的模型,在其中切换。它启动时读取列表:改动后退出并重新打开它。", "{agent}'s model list": "{agent} 的模型列表", "Models turned off don't show in {agent}'s model picker; other agents can still use them.": "关掉的模型不会出现在 {agent} 的模型列表里,别的 Agent 照常能用。", "Search models": "搜索模型", diff --git a/internal/gui/tests/desktop-model-menu.test.cjs b/internal/gui/tests/desktop-model-menu.test.cjs new file mode 100644 index 000000000..277bce376 --- /dev/null +++ b/internal/gui/tests/desktop-model-menu.test.cjs @@ -0,0 +1,150 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// Claude Desktop's own model menu lists magpie's /v1/models, which it reads +// as it starts, and the user switches among them there (蓝猫 on Discord): +// its row picks which models that menu lists, several at once, where +// another agent's row picks the one it starts on. In the window the button +// beside its switch names them and opens the model list; in the tray panel +// the opened row has it under the provider, and the row's words name them. +// Unticking one is posted at once and the row says so; the opus tier no +// longer stands in that place as "Its last pick". No native