Files
magpie/internal/provider/provider_order.go
T
yetone 7b4065b12a sync: the order the providers were put in on the Providers tab goes with them, so a computer that syncs lists and tries them as the other does (ARNO on Discord: 发现webdav同步的时候没有同步provider的顺序) — since #499 the order is kept apart from the providers, as providers.json's "order" (ids, the built-in signed-in accounts among them), and the list is sorted by it when read; the providers themselves stay in the file in the order they were added. The sync's bundle (backup.Bundle) carried the providers, icons, groups and search APIs but not that order, and the providers part was hashed without it, so arranging the list was not even a change to sync, and a computer bringing the providers in (provider.Mirror) kept its own order, or none. The bundle now has Order (omitempty, from provider.StoredOrder), hashed with the providers part only when there is one, so the hashes of a setup never arranged are as before and an older magpie's file still compares the same; take carries it with the providers, and bring applies it after Mirror with the new provider.MirrorOrder: the other computer's ids first as it has them, then the ones named here alone (an account only this computer has) in the order they have here; none, from a magpie before this or a setup never arranged, leaves the order here as it is. This goes for WebDAV and S3 alike (one path). A backup restored by hand still merges providers without changing the order. TestSyncProviderOrder (a arranges three providers, b joins and lists them in a's order and pushes nothing back, b arranging only is pushed and a follows; against an httptest WebDAV server) fails without the change ("b after joining: [one two three]"); TestMirrorOrder covers the merge. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... (temp HOME) pass.
2026-10-02 15:01:24 +08:00

97 lines
2.4 KiB
Go

package provider
import (
"fmt"
"slices"
)
// ordered is ps in the order the user put them in (#499): those order
// names first, as it has them, then the rest as they came (the order they
// were added). The order is more than the list's: the agents' pickers list
// the models in it, a bare model id goes to the first provider serving it,
// and a group magpie finds of a model more than one provider serves tries
// them in it.
func ordered(ps []Provider, order []string) []Provider {
if len(order) == 0 {
return ps
}
at := map[string]int{}
for i, id := range order {
if _, dup := at[id]; !dup {
at[id] = i
}
}
rank := func(p Provider) int {
if i, ok := at[p.ID]; ok {
return i
}
return len(order)
}
slices.SortStableFunc(ps, func(a, b Provider) int { return rank(a) - rank(b) })
return ps
}
// SetOrder saves the order the providers are listed and tried in, by id.
// Every id must be a provider's, once; the ones left out follow those
// named, as they are now, so a provider added later goes after them all.
func SetOrder(ids []string) error {
f, err := read()
if err != nil {
return err
}
all := All()
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 _, ok := find(all, id); !ok {
return fmt.Errorf("no provider %q", id)
}
}
order := slices.Clone(ids)
for _, p := range all {
if !seen[p.ID] {
order = append(order, p.ID)
}
}
f.Order = order
return store(f)
}
// StoredOrder is the order the user put the providers in, as saved; as
// with Stored, a file that can't be read is an error.
func StoredOrder() ([]string, error) {
f, err := read()
return f.Order, err
}
// MirrorOrder makes the providers' order the one another computer has, as
// sync brings it: its ids first, as it has them, then the ones named here
// alone (a signed-in account only this computer has), in the order they
// have here. None, as from a magpie before the order was synced or one
// never arranged, leaves the order here as it is.
func MirrorOrder(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.Order) {
if id != "" && !seen[id] {
seen[id] = true
out = append(out, id)
}
}
if slices.Equal(out, f.Order) {
return nil
}
f.Order = out
return store(f)
}