diff --git a/internal/agent/claude.go b/internal/agent/claude.go index e1ef09e67..f76673c06 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -732,12 +732,22 @@ func claudeIn(at place) *Agent { } } } + // each row's label is the model's name as every agent's list calls + // it, by the Provider in model names setting (provider.Labels): the + // picker took the name alone, so none of the setting's three ways + // changed /model (love1sbug on X). A description that would say the + // provider the label already says is left "via magpie". + labels := listLabels("claude") rows := []any{} for _, o := range viaMagpie("claude", "") { if slices.ContainsFunc(onTier, func(v string) bool { return claudeSameModel(v, o.Value) }) { continue } - rows = append(rows, map[string]any{"model": spell(mark1M(o.Value)), "label": cmp.Or(o.Label, o.Value), "description": o.Note}) + label, note := cmp.Or(labels[o.Ref], o.Label, o.Value), o.Note + if by, ok := strings.CutSuffix(note, " · via magpie"); ok && label != o.Label && strings.HasSuffix(label, " · "+by) { + note = "via magpie" + } + rows = append(rows, map[string]any{"model": spell(mark1M(o.Value)), "label": label, "description": note}) } if len(rows) == 0 { return dropPicker() diff --git a/internal/agent/claude_picker_group_test.go b/internal/agent/claude_picker_group_test.go index 51dbd7a56..ccafe3c0d 100644 --- a/internal/agent/claude_picker_group_test.go +++ b/internal/agent/claude_picker_group_test.go @@ -69,7 +69,7 @@ func TestClaudePickerListsRoutingGroups(t *testing.T) { t.Fatalf("picker has %d options, want the group and 30 models: %v", len(opts), listed()) } // the group, first, as the model Claude Code was set to is spelled - if g := opts[0]; g["model"] != main || g["label"] != "Auto DeepSeek" || g["description"] != "routing group · via magpie" { + if g := opts[0]; g["model"] != main || g["label"] != "Auto DeepSeek · routing group" || g["description"] != "via magpie" { t.Fatalf("first option %v, want the group as %q: %v", g, main, listed()) } for _, m := range models { diff --git a/internal/agent/claude_picker_names_test.go b/internal/agent/claude_picker_names_test.go new file mode 100644 index 000000000..dbd93e02c --- /dev/null +++ b/internal/agent/claude_picker_names_test.go @@ -0,0 +1,85 @@ +package agent + +import ( + "encoding/json" + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/provider" +) + +// love1sbug on X: switching Names in agents' lists between Off, Not on +// names I set and On changed nothing in Claude Code's /model, as its +// modelPicker labelled every model by its name alone. Each row is now +// labelled as every agent's list names the model (provider.Labels), and +// written again when the setting changes; a description that would repeat +// the provider the label says is left "via magpie". +func TestClaudePickerFollowsSuffixMode(t *testing.T) { + home := syncHome(t) + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"glm-4.6", "glm-5"}}); err != nil { + t.Fatal(err) + } + if err := provider.SetModelName("relay/glm-5", "My GLM"); err != nil { + t.Fatal(err) + } + if err := provider.SaveGroup(provider.Group{ID: "mix", Name: "Mix", Members: []string{"relay/glm-4.6", "relay/glm-5"}}); err != nil { + t.Fatal(err) + } + path := filepath.Join(home, ".claude", "settings.json") + writeFile(t, path, `{"theme":"dark"}`) + if err := claude(home).Apply("model", "relay/glm-4.6"); err != nil { + t.Fatal(err) + } + catalog.Changed = SyncCatalog + t.Cleanup(func() { catalog.Changed = nil }) + rows := func() map[string][2]string { + t.Helper() + var p struct { + Options []struct{ Model, Label, Description string } + } + raw, _ := edit.GetJSON(path, "modelPicker") + if err := json.Unmarshal([]byte(raw), &p); err != nil { + t.Fatalf("modelPicker: %v\n%s", err, readFile(path)) + } + out := map[string][2]string{} + for _, o := range p.Options { + out[o.Model] = [2]string{o.Label, o.Description} + } + return out + } + for _, c := range []struct { + mode string + glm, mine, mix string + glmNote, mixNot string + }{ + {provider.SuffixOn, "GLM-4.6 · Relay", "My GLM · Relay", "Mix · routing group", "via magpie", "via magpie"}, + {provider.SuffixOwn, "GLM-4.6 · Relay", "My GLM", "Mix", "via magpie", "routing group · via magpie"}, + {provider.SuffixOff, "GLM-4.6", "My GLM", "Mix", "Relay · via magpie", "routing group · via magpie"}, + {provider.SuffixOn, "GLM-4.6 · Relay", "My GLM · Relay", "Mix · routing group", "via magpie", "via magpie"}, + } { + if err := provider.SetSuffixMode(c.mode); err != nil { + t.Fatal(err) + } + got := rows() + want := map[string][2]string{ + "relay/glm-4.6": {c.glm, c.glmNote}, + "relay/glm-5": {c.mine, ""}, + "group/mix": {c.mix, c.mixNot}, + } + for id, w := range want { + g, ok := got[id] + if !ok { + for k := range got { + if k == id+"[1m]" { + g, ok = got[k], true + } + } + } + if !ok || g[0] != w[0] || w[1] != "" && g[1] != w[1] { + t.Fatalf("%s: %s is %q, want %q (%v)", c.mode, id, g, w, got) + } + } + } +} diff --git a/internal/agent/route.go b/internal/agent/route.go index 092b90f7b..0f5e831cd 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -71,6 +71,19 @@ func viaMagpie(agent, prefix string) []Option { return append(groups, out...) } +// listLabels are the names the agent's own list calls the catalog's models +// by, by id: provider.Labels, which follows the Provider in model names +// setting, where an Option's Label is the name alone for magpie's picker. +func listLabels(agent string) map[string]string { + shown, _ := provider.CatalogFor(agent) + labels := provider.Labels(shown) + out := make(map[string]string, len(shown)) + for i, e := range shown { + out[e.ID] = labels[i] + } + return out +} + // viaMagpieFor is viaMagpie without the agent's own account: Codex CLI going // through magpie to its own ChatGPT login would only add a hop. func viaMagpieFor(agentID, prefix string) []Option {