From 257b6fa80e2647de31d360bb7caf47ffa76bde93 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 26 Sep 2026 01:35:44 +0800 Subject: [PATCH] =?UTF-8?q?group:=20a=20removed=20found=20group=20is=20sai?= =?UTF-8?q?d=20to=20be=20removed=20wherever=20it=20is=20=E2=80=94=20once?= =?UTF-8?q?=20its=20model=20was=20down=20to=20one=20provider,=20magpie=20n?= =?UTF-8?q?o=20longer=20found=20it,=20so=20`magpie=20group=20rm`=20said=20?= =?UTF-8?q?no=20group=20"auto-=E2=80=A6"=20and=20`magpie=20groups`=20didn'?= =?UTF-8?q?t=20list=20it=20though=20its=20record=20was=20still=20in=20prov?= =?UTF-8?q?iders.json;=20both=20now=20know=20it,=20restore=20takes=20the?= =?UTF-8?q?=20record=20out,=20and=20group=20help=20and=20groups=20say=20th?= =?UTF-8?q?at=20the=20{"hidden":=20true}=20record=20is=20what=20keeps=20a?= =?UTF-8?q?=20found=20group=20removed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- groups_cli.go | 43 +++++++++++++++++++++++++++++++++----- internal/provider/group.go | 13 ++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/groups_cli.go b/groups_cli.go index d0d61a22f..a881a05bf 100644 --- a/groups_cli.go +++ b/groups_cli.go @@ -25,6 +25,10 @@ const groupUsage = `usage: magpie group rm remove a group (one magpie found is hidden instead) magpie group restore bring back a group magpie found that you removed + magpie finds a group for each model two or more providers serve (auto-, never stored); + removing one stores {"id":…,"hidden":true} in providers.json, which is what keeps it removed: + take that record out of the file and the group is back + models provider/model ids as magpie models lists them; a bare model id works when one provider serves it routing smart (default) of the subscriptions with quota to spare, the one renewing soonest first order the first model until it can't answer, then the next @@ -383,6 +387,10 @@ func groupCmd(args []string) error { if err != nil { return err } + if len(g.Members) == 0 { + fmt.Println(green.Render("✓"), bold.Render(g.ID), "is no longer removed", muted.Render("· it shows once two providers serve its model")) + return nil + } fmt.Println(green.Render("✓"), bold.Render(g.Name), "is back") return showGroup(g) } @@ -448,13 +456,28 @@ func setGroup(ref string, pairs []string) (provider.Group, error) { return findGroup(g.ID) } +// removedOnly is a removed found group magpie doesn't find now (its model +// is down to one provider): only its record is left, to keep it removed. +func removedOnly(ref string) (string, bool) { + ref = strings.TrimPrefix(strings.TrimPrefix(strings.TrimSpace(ref), "magpie/"), provider.GroupPrefix) + for _, id := range provider.RemovedGroups() { + if strings.EqualFold(id, ref) { + return id, true + } + } + return "", false +} + func removeGroup(ref string) (provider.Group, error) { g, err := findGroup(ref) if err != nil { + if id, ok := removedOnly(ref); ok { + return g, fmt.Errorf("%s is removed already (and no two providers serve its model now); magpie group restore %s brings it back", id, id) + } return g, err } if g.Hidden { - return g, fmt.Errorf("%s is removed already", g.ID) + return g, fmt.Errorf("%s is removed already; magpie group restore %s brings it back", g.ID, g.ID) } return g, provider.DeleteGroup(g.ID) } @@ -462,6 +485,10 @@ func removeGroup(ref string) (provider.Group, error) { func restoreGroup(ref string) (provider.Group, error) { g, err := findGroup(ref) if err != nil { + if id, ok := removedOnly(ref); ok { + // its record goes; the group comes back once two providers serve its model + return provider.Group{ID: id, Name: id}, provider.ShowGroup(id) + } return g, err } if !g.Hidden { @@ -566,13 +593,19 @@ func groups() error { for _, r := range rows { fmt.Printf(" %s %s %s %s%s\n", pad(r.name, w[0]), pad(r.id, w[1]), pad(r.how, w[2]), r.members, r.uses) } - if len(hidden) > 0 { - var ids []string - for _, g := range hidden { - ids = append(ids, g.ID) + var ids []string + for _, g := range hidden { + ids = append(ids, g.ID) + } + for _, id := range provider.RemovedGroups() { + if !slices.Contains(ids, id) { + ids = append(ids, id) // its model is down to one provider now } + } + if len(ids) > 0 { fmt.Println() fmt.Println(" ", muted.Render("removed: "+strings.Join(ids, ", ")+" · magpie group restore brings one back")) + fmt.Println(" ", muted.Render(" (each is a {\"hidden\": true} record in providers.json that keeps it removed; deleting the record brings it back)")) } return nil } diff --git a/internal/provider/group.go b/internal/provider/group.go index ea594a806..7f831fe26 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -279,6 +279,19 @@ func DeleteGroup(id string) error { return store(f) } +// RemovedGroups are the found groups the user removed, whether or not +// magpie finds them now: each is a record in providers.json ({"id", "hidden": +// true}) that keeps it removed when two providers serve its model again. +func RemovedGroups() []string { + var out []string + for _, g := range load().Groups { + if g.Hidden { + out = append(out, g.ID) + } + } + return out +} + // ShowGroup brings back a group magpie found that the user had removed. func ShowGroup(id string) error { f := load()