Files
magpie/internal/provider/rename.go
T
yetone a999770531 plugins: a plugin's subscription does what a built-in's does, in seven places it didn't (the user: 要保证插件订阅 provider 跟内置的订阅 provider所有逻辑都要一模一样) — an installed plugin's provider id is kept for it signed in or not, as a built-in's is: a provider of the user's own isn't saved on it, renamed onto it or imported as it (freeID gives id-2). A plugin's account whose plan lacks the model is tried after the ones that list it, as a Codex Free account behind a Plus, by the list the plugin told for it. Cursor's plugin has fast mode where the built-in Cursor has it: a model with a -fast one is saved :fast in a group and sent service_tier "priority", one without is refused. Removing a plugin's provider hides it as a built-in's is, its accounts kept and shown again from Hidden or by signing in, rather than signing every account out ("stays signed in" said so already). magpie accounts lists, adds, switches and forgets a plugin's accounts by its provider's id or name. Grok's plugin beside the built-in Grok (grok-plugin) searches by itself as Grok does. A plugin's plan is named as its built-in's ("Cursor Pro", "SuperGrok"), and a plugin's resets are shown but have no Use button or Auto-use, which spent Codex's reset of the same user. TestPluginIDReservedSignedOut, TestPluginAccountLackingTheModelGoesLast, TestCursorPluginGroupFast, TestAccountsOfAPlugin, TestGrokPluginSearchesItself, plugin-plan and codex-auto-reset fail without the change; TestPluginSignIn and TestPluginAccounts now expect a removed plugin hidden. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass (TestClaudeSignedOut failed once under load, passes alone); the gateway, provider and plugin suites and TestAccountsOfAPlugin pass on Windows, the plugin tests on Linux.
2026-10-01 20:26:55 +08:00

165 lines
5.0 KiB
Go

package provider
import (
"errors"
"fmt"
"os"
"slices"
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// Rename gives a provider another id, the one its models are picked by
// (<id>/<model>). What magpie keeps that names it by id follows: the
// routing groups it is in and their rules and classifiers, the other
// providers' fallbacks, and which agents are shown it. The old id stays
// with it (Was), so an agent still running on a config that names it the
// old way reaches it, and its usage so far is counted with it; the agents'
// own files are the agent package's to rewrite (agent.RenameProvider).
// A signed-in account keeps the id of its agent.
func Rename(from, to string) error {
from = strings.ToLower(strings.TrimSpace(from))
to = strings.ToLower(strings.TrimSpace(to))
if to == "" || to != Slug(to) {
return fmt.Errorf("a provider's id must be lowercase letters, digits and dashes, not %q", to)
}
if to == from {
return nil
}
f, err := read()
if err != nil {
return err
}
i := slices.IndexFunc(f.Providers, func(p Provider) bool { return p.ID == from })
// one of the user's own saved on a subscription's id before that was one
// (a "WorkBuddy" key before v0.1.261) can be moved off it; the
// subscription itself can't
custom := i >= 0 && hasEndpoint(f.Providers[i])
_, signedIn := find(Accounts(), from)
sub := subscriptionID(from)
if (signedIn || sub) && !custom {
return fmt.Errorf("%s is a subscription: its id is its agent's", from)
}
switch {
case to == "magpie":
return errors.New(`"magpie" is what agents call the gateway itself; pick another id`)
case to == strings.TrimSuffix(GroupPrefix, "/"):
return errors.New(`"group" starts the ids of routing groups; pick another id`)
case subscriptionID(to):
return fmt.Errorf("%q is the id of the %s subscription; pick another", to, to)
}
if i < 0 {
return fmt.Errorf("no provider %q", from)
}
if slices.ContainsFunc(f.Providers, func(p Provider) bool { return p.ID == to }) {
return fmt.Errorf("there is a provider %q already", to)
}
// an id another provider once had is this one's now
for j := range f.Providers {
f.Providers[j].Was = slices.DeleteFunc(f.Providers[j].Was, func(w string) bool { return w == to })
}
p := &f.Providers[i]
p.ID = to
// the subscription's id is the subscription's: an old ref to it isn't
// taken for this one
if !sub && !slices.Contains(p.Was, from) {
p.Was = append(p.Was, from)
}
for j := range f.Providers {
for k, m := range f.Providers[j].Fallback {
f.Providers[j].Fallback[k] = renamedRef(m, from, to)
}
}
for j := range f.Groups {
g := &f.Groups[j]
for k, m := range g.Members {
g.Members[k] = renamedRef(m, from, to)
}
for k, r := range g.Rules {
g.Rules[k].Use = renamedRef(r.Use, from, to)
}
for k, m := range g.Fast {
g.Fast[k] = renamedRef(m, from, to)
}
g.Classifier = renamedRef(g.Classifier, from, to)
g.Pick = renamedRef(g.Pick, from, to)
}
// the vendor's list last fetched goes with it
os.Rename(catalog.LivePath(from), catalog.LivePath(to))
if err := store(f); err != nil {
return err
}
s := settings.Load()
changed := renameModelPrefs(&s, from, to)
for a, names := range s.Visible {
for k, n := range names {
if strings.EqualFold(n, from) {
s.Visible[a][k], changed = to, true
}
}
}
if changed {
return settings.Save(s)
}
return nil
}
// renamedRef is a model id ("provider/model", "magpie/provider/model")
// with the provider from named to instead.
func renamedRef(ref, from, to string) string {
pre, rest := "", ref
if r, ok := strings.CutPrefix(ref, "magpie/"); ok {
pre, rest = "magpie/", r
}
if m, ok := strings.CutPrefix(rest, from+"/"); ok {
return pre + to + "/" + m
}
return ref
}
// RenamedRef is ref with the provider it names by an id the provider had
// named by the id it has now.
func RenamedRef(ref string) string {
for old, now := range Renamed() {
if r := renamedRef(ref, old, now); r != ref {
return r
}
}
return ref
}
// Renamed maps the ids providers had to the ones they have now.
func Renamed() map[string]string {
out := map[string]string{}
for _, p := range load().Providers {
for _, w := range p.Was {
out[w] = p.ID
}
}
return out
}
// OnAccountIDs are the user's own providers saved on a subscription's id,
// which hide that subscription once it is signed in (All lists the one
// saved): made before the id was a subscription's, as a WorkBuddy key was
// before WorkBuddy's plan was one. MoveOffAccountIDs moves them.
func OnAccountIDs() []string {
var out []string
for _, p := range load().Providers {
if slices.Contains(accountIDs, p.ID) && hasEndpoint(p) {
out = append(out, p.ID)
}
}
return out
}
// FreeID is id, or id-2, id-3… whichever no provider or subscription has.
func FreeID(id string) string { return freeID(id) }
// hasEndpoint: a provider of the user's, not a subscription's model picks.
func hasEndpoint(p Provider) bool {
return p.Chat != "" || p.Responses != "" || p.Anthropic != "" || p.Decide != ""
}