Files
magpie/internal/provider/group_order.go
T
yetone 855e7985f2 routing: the groups are put in an order of the user's, as the Agents page's rows are, and /v1/models lists them so (#779)
The Routing page's group list followed the order the groups were made in, found ones after, so a group used every day couldn't be put first, though /v1/models lists the groups in that order and agents' pickers (Claude Desktop's 3P list among them) show it and some clients probe its first model. A group row's logos are now its handle, as an agent row's logo is (#57): dragged (dragRows, the Agents page's sorter) it moves the row, clicked, or the row right-clicked, it opens Move up and Move down (openRowMenu, with the Agents menu's arrows, now MOVE_UP/MOVE_DOWN), and Alt+↑/↓ moves it from the keyboard, which stays on it. The grip in the row's margin is drawn only while the row is under the pointer, dragged, or its handle is focused or its menu open, as on the Agents page. The list is drawn in its new order at once and put back if the save fails; a later move wins over an earlier one still saving.

The order is kept in providers.json as groupOrder (provider.SetGroupOrder, POST /api/groups/arrange), beside the providers' own order (#499), and applied in groupsIn, which every list of the groups is made from: the Routing page, Catalog and so /v1/models, magpie group ls and the TUI's routing page. A found group, which isn't stored until changed, can be put among the user's this way, which reordering the groups array couldn't do. A group not named (one made or found after) goes last; a renamed one keeps its place. Backup and sync carry the order as they do the providers' (MirrorGroupOrder).

TestGroupOrder (Groups and the catalog follow SetGroupOrder, providers.json has it, a new group last, a rename in place, bad orders refused, a synced order first) fails on the old code. group-arrange.test.cjs (WebKit, en and zh): the grip hidden with the pointer away and shown on the hovered row only, in its margin; right-click Move down with Move up off on the first; Alt+↓ and Alt+↑ with focus kept; the handle's click opens the menu, not the editor; a drag by the grip to the top; each posting every group's id with the removed one last; no click moving the page. It fails on the old code. agent-grip, click-scroll, account-arrange, usage-arrange, provider-order and the group-*/routing-* GUI tests pass in WebKit, but for group-from-model-add's 600x420 cases, which fail on main as well. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 19:27:51 +08:00

96 lines
2.4 KiB
Go

package provider
import (
"fmt"
"slices"
)
// orderedGroups is gs in the order the user put them in on the Routing page
// (#779): those order names first, as it has them, then the rest as they
// came (the user's, then the found ones). Every list of the groups is made
// from groupsIn, so the gateway's /v1/models, which agents' pickers show
// and some clients probe the first model of, follows it too.
func orderedGroups(gs []Group, order []string) []Group {
if len(order) == 0 {
return gs
}
at := map[string]int{}
for i, id := range order {
if _, dup := at[id]; !dup {
at[id] = i
}
}
rank := func(g Group) int {
if i, ok := at[g.ID]; ok {
return i
}
return len(order)
}
slices.SortStableFunc(gs, func(a, b Group) int { return rank(a) - rank(b) })
return gs
}
// SetGroupOrder saves the order the routing groups are listed in, by id.
// Every id must be a group's, once; the ones left out follow those named,
// as they are now, and the whole order is kept, so a group made or found
// later goes after them all.
func SetGroupOrder(ids []string) error {
f, err := read()
if err != nil {
return err
}
all := groupsIn(providerEntries())
seen := map[string]bool{}
for _, id := range ids {
if seen[id] {
return fmt.Errorf("%s is in the order twice", id)
}
seen[id] = true
if !slices.ContainsFunc(all, func(g Group) bool { return g.ID == id }) {
return fmt.Errorf("no group %q", id)
}
}
order := slices.Clone(ids)
for _, g := range all {
if !seen[g.ID] {
order = append(order, g.ID)
}
}
f.GroupOrder = order
return store(f)
}
// StoredGroupOrder is the order the user put the groups in, as saved; as
// with Stored, a file that can't be read is an error.
func StoredGroupOrder() ([]string, error) {
f, err := read()
return f.GroupOrder, err
}
// MirrorGroupOrder makes the groups' order the one another computer has,
// as sync brings it, the way MirrorOrder does the providers': its ids
// first, then the ones named here alone. None leaves the order here as it
// is.
func MirrorGroupOrder(order []string) error {
if len(order) == 0 {
return nil
}
f, err := read()
if err != nil {
return err
}
out := []string{}
seen := map[string]bool{}
for _, id := range slices.Concat(order, f.GroupOrder) {
if id != "" && !seen[id] {
seen[id] = true
out = append(out, id)
}
}
if slices.Equal(out, f.GroupOrder) {
return nil
}
f.GroupOrder = out
return store(f)
}