Files
magpie/internal/agent/rename.go
T
yetone 36b0d23fe7 providers: a provider of the user's saved on a subscription's id is moved off it, so the subscription is listed once signed in (LingYi_Stu on X: workbuddy 国内版本无法使用呢 — "凌一 已登录,但 magpie 无法列出它,请反馈此问题", magpie 0.1.384, WorkBuddy 5.6.2 signed in, the international build listed with 3 accounts beside it) — before WorkBuddy's plan was a subscription (v0.1.261), a provider added as "WorkBuddy" was saved with the id workbuddy, and All() skips an account whose id a saved provider has: the sign-in said done, logins.json kept the account, and the Providers page listed only the user's own provider, the card still saying Free · Pro. Now start-up (agent.MoveOffAccountIDs, beside RenameLegacy) moves each provider with a base URL saved on a subscription's id to the next free one (workbuddy-2) the way a rename does: routing groups, fallbacks and the agents on its models follow; the old id is not kept as one it Was, being the subscription's. Rename allows it for such a provider alone, a subscription still keeps its id, and Find takes an id before a name, so workbuddy is the subscription and not a provider called WorkBuddy. Tested with a new TestWorkBuddyUnderUsersOwnID (the sign-in done and unlisted, then both listed after the move, a fallback moved, the subscription refused a rename), and end to end in a sandbox HOME with a WorkBuddy auth file and providers.json: main lists the custom WorkBuddy alone; with the change magpie providers lists workbuddy-2 (its fallback moved) and WorkBuddy signed in as 凌一. go test -tags nogui ./... (TestUserPath failed once under load, passes on its own), go build for linux and windows
2026-09-29 18:17:14 +08:00

76 lines
2.2 KiB
Go

package agent
import (
"strings"
"github.com/yetone/magpie/internal/provider"
)
// RenameProvider gives a provider another id (provider.Rename) and moves
// the agents on one of its models to the same model by the new id, each
// spelled the way the agent spells it. It answers the agents it moved.
func RenameProvider(from, to string) ([]string, error) {
from = strings.ToLower(strings.TrimSpace(from))
to = strings.ToLower(strings.TrimSpace(to))
type move struct {
a *Agent
key, val string
}
// what the agents are on is read before: the picker's options name
// the provider by the id it has
var moves []move
if from != to {
for _, a := range Detected() {
vals := a.Values()
for _, f := range a.Fields {
v := vals[f.Key]
if v == "" {
continue
}
nv := ""
if f.Options != nil {
for _, o := range f.Options(vals) {
if o.Value == v && strings.HasPrefix(o.Ref, from+"/") {
now := to + "/" + strings.TrimPrefix(o.Ref, from+"/")
nv = strings.Replace(v, o.Ref, now, 1)
break
}
}
}
if nv == "" && strings.HasPrefix(v, magpieID+"/"+from+"/") {
nv = magpieID + "/" + to + "/" + strings.TrimPrefix(v, magpieID+"/"+from+"/")
}
if nv != "" && nv != v {
moves = append(moves, move{a, f.Key, nv})
}
}
}
}
if err := provider.Rename(from, to); err != nil {
return nil, err
}
var moved []string
for _, m := range moves {
if err := m.a.Apply(m.key, m.val); err != nil {
return moved, err
}
if len(moved) == 0 || moved[len(moved)-1] != m.a.Name {
moved = append(moved, m.a.Name)
}
}
SyncCatalog()
return moved, nil
}
// MoveOffAccountIDs moves each of the user's providers saved on a
// subscription's id (provider.OnAccountIDs) to a free one, the agents on it
// with it, so the subscription is listed once signed in: a "WorkBuddy" key
// saved as workbuddy before WorkBuddy's plan was a subscription hid it, and
// signing in to it said "signed in, but magpie can't list it". Run at
// start-up; errors are swallowed as RenameLegacy's are.
func MoveOffAccountIDs() {
for _, id := range provider.OnAccountIDs() {
_, _ = RenameProvider(id, provider.FreeID(id))
}
}