diff --git a/internal/provider/account.go b/internal/provider/account.go index 92b56abc5..168a4e29d 100644 --- a/internal/provider/account.go +++ b/internal/provider/account.go @@ -847,7 +847,35 @@ func Accounts() []Provider { } // a built-in moved onto its plugin is the plugin's now (migrate.go) out = slices.DeleteFunc(out, func(p Provider) bool { return Moved(p.ID) }) - return append(out, pluginAccounts()...) + return placeMoved(out, pluginAccounts()) +} + +// builtinOrder is the built-ins' ids in the order Accounts lists them. +var builtinOrder = slices.Concat([]string{"claude", "codex", "copilot", "cursor", "grok", "devin", "kiro", "zcode", + "workbuddy", WorkBuddyAIID, CommandCodePlanID}, qoderAgents, []string{"zed", "factory", MiMoID, "gemini", "antigravity"}) + +// placeMoved adds the plugins' accounts to the built-ins': one a built-in +// was moved onto stands where the built-in stood, the others go last. +func placeMoved(out, plugins []Provider) []Provider { + at := func(id string) int { + if i := slices.Index(builtinOrder, id); i >= 0 { + return i + } + return len(builtinOrder) + } + var rest []Provider + for _, p := range plugins { + if !Moved(p.ID) || !slices.Contains(builtinOrder, p.ID) { + rest = append(rest, p) + continue + } + i := slices.IndexFunc(out, func(q Provider) bool { return !q.IsPlugin() && at(q.ID) > at(p.ID) }) + if i < 0 { + i = len(out) + } + out = slices.Insert(out, i, p) + } + return append(out, rest...) } func readJSON(path string, v any) bool { diff --git a/internal/provider/plugin_parity_test.go b/internal/provider/plugin_parity_test.go index a8c0b4c36..4ce791415 100644 --- a/internal/provider/plugin_parity_test.go +++ b/internal/provider/plugin_parity_test.go @@ -1,6 +1,7 @@ package provider import ( + "slices" "testing" "github.com/yetone/magpie/internal/catalog" @@ -74,3 +75,21 @@ func TestPluginCatalogPriceAndImages(t *testing.T) { t.Errorf("a model not said of: %v", *in) } } + +// A built-in moved onto its plugin stands where the built-in stood among +// the accounts; a plugin's own provider goes last. +func TestMovedAccountKeepsItsPlace(t *testing.T) { + _, grok := movedGrok(t) + fake := pluginProvider(plugin.Provider{ID: "fakeco"}, pluginLogin{}) + out := placeMoved([]Provider{{ID: "claude"}, {ID: "codex"}, {ID: "zed"}, {ID: "gemini"}}, []Provider{fake, grok}) + var ids []string + for _, p := range out { + ids = append(ids, p.ID) + } + if want := []string{"claude", "codex", "grok", "zed", "gemini", "fakeco"}; !slices.Equal(ids, want) { + t.Fatalf("accounts %v, want %v", ids, want) + } + if !out[2].IsPlugin() { + t.Fatal("the moved Grok isn't its plugin's") + } +}