Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

provider: a plugin provider offers the models of every account switched on, not only the first's (#2554, willywang; LoKi)

yetone72109ea0eb32
diff --git a/internal/provider/plugin_served_models_test.go b/internal/provider/plugin_served_models_test.go
new file mode 100644
index 000000000..5d739d179
--- /dev/null
+++ b/internal/provider/plugin_served_models_test.go
@@ -0,0 +1,87 @@
+package provider
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/plugin"
+ "github.com/yetone/magpie/internal/settings"
+)
+
+// A plugin provider offers the models any account switched on serves,
+// not the first account's alone: a model only a later account lists is
+// in the provider's catalog, and an account switched off adds nothing
+// (#2554, willywang; LoKi saw it again on 0.1.1320). Each account keeps
+// its own list for routing.
+func TestPluginProviderOffersEveryOnAccountsModels(t *testing.T) {
+ claudeHome(t)
+ t.Setenv("MAGPIE_BUN", "")
+ spec := "@example/opencode-acme-auth"
+ write := func(name string, v any) {
+ t.Helper()
+ b, _ := json.Marshal(v)
+ os.MkdirAll(settings.Dir(), 0o700)
+ if err := os.WriteFile(filepath.Join(settings.Dir(), name), b, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ }
+ write("plugins.json", map[string]any{"plugins": []map[string]any{{"spec": spec}}})
+ write("plugin-auth.json", map[string]map[string]any{
+ "acme": {"type": "api", "key": "sk-acme-0001"},
+ "acme#2": {"type": "api", "key": "sk-acme-0002"},
+ "acme#3": {"type": "api", "key": "sk-acme-0003"},
+ })
+ plugin.UseCached([]plugin.Provider{{ID: "acme", Spec: spec, Name: "Acme",
+ Methods: []plugin.Method{{Type: "api", Label: "API key"}},
+ Accounts: []plugin.Account{
+ {Key: "acme", Type: "api", Models: []string{"a"}},
+ {Key: "acme#2", Type: "api", Models: []string{"a", "b"}},
+ {Key: "acme#3", Type: "api", Models: []string{"c"}},
+ },
+ Models: listedModels(t, `[{"id": "a", "name": "A"}, {"id": "b", "name": "B"}, {"id": "c", "name": "C"}]`),
+ }})
+ t.Cleanup(func() { plugin.UseCached(nil) })
+ saveLogins(t,
+ savedLogin{Agent: "plugin:acme", User: "one", Home: "acme", On: true},
+ savedLogin{Agent: "plugin:acme", User: "two", Home: "acme#2", On: true},
+ savedLogin{Agent: "plugin:acme", User: "three", Home: "acme#3"},
+ )
+ ids := func(p Provider) string {
+ var out []string
+ for _, m := range p.Available() {
+ out = append(out, m.ID)
+ }
+ return strings.Join(out, " ")
+ }
+ ps := pluginAccounts()
+ i := slices.IndexFunc(ps, func(p Provider) bool { return p.ID == "acme" })
+ if i < 0 {
+ t.Fatalf("no acme provider in %v", ps)
+ }
+ if got := ids(ps[i]); got != "a b" {
+ t.Fatalf("the provider offers %q, want %q", got, "a b")
+ }
+ if !ps[i].Account.Lists("a") || ps[i].Account.Lists("b") {
+ t.Fatal("the first account routes past its own list")
+ }
+ per := map[string]string{}
+ for _, l := range pluginLogins(mustCached(t, "acme")) {
+ per[l.acct.Key] = ids(pluginProvider(mustCached(t, "acme"), l))
+ }
+ if per["acme"] != "a" || per["acme#2"] != "a b" || per["acme#3"] != "c" {
+ t.Fatalf("each account's own models: %v", per)
+ }
+}
+
+func mustCached(t *testing.T, id string) plugin.Provider {
+ t.Helper()
+ pp, ok := PluginOf(id)
+ if !ok {
+ t.Fatalf("no plugin %s", id)
+ }
+ return pp
+}
diff --git a/internal/provider/plugins.go b/internal/provider/plugins.go
index 96dc33ca8..4f0fb9c2b 100644
--- a/internal/provider/plugins.go
+++ b/internal/provider/plugins.go
@@ -115,7 +115,15 @@ func pluginAccounts() []Provider {
continue // shown once the move is through (migrate.go)
}
if ls := pluginLogins(pp); len(ls) > 0 {
- out = append(out, pluginProvider(pp, ls[0]))
+ // the provider offers what any account switched on serves, as
+ // the requests reach each through its own list (#2554)
+ keys := []string{ls[0].acct.Key}
+ for _, l := range ls[1:] {
+ if l.On {
+ keys = append(keys, l.acct.Key)
+ }
+ }
+ out = append(out, pluginProviderServing(pp, ls[0], keys))
}
}
return out
@@ -317,6 +325,24 @@ func (p Provider) ListError() string {
return PluginListError(pp, p.Account.pluginKey)
}
+// pluginServedCatalog is what the accounts at keys serve between them,
+// in the catalog's order: an account the plugin told no list of, or
+// doesn't know, serves all of it.
+func pluginServedCatalog(pp plugin.Provider, keys []string) []catalog.Model {
+ all := pluginCatalog(pp)
+ listed := map[string]bool{}
+ for _, k := range keys {
+ i := slices.IndexFunc(pp.Accounts, func(a plugin.Account) bool { return a.Key == k })
+ if i < 0 || pp.Accounts[i].Models == nil {
+ return all
+ }
+ for _, m := range pp.Accounts[i].Models {
+ listed[m] = true
+ }
+ }
+ return slices.DeleteFunc(all, func(m catalog.Model) bool { return !listed[m.ID] })
+}
+
// pluginLists is whether a plugin's account serves model, as the plugin
// last told its list; one it told none of serves all of the provider's.
func (a *Account) pluginLists(model string) bool {
@@ -335,7 +361,19 @@ func (a *Account) pluginLists(model string) bool {
// pluginProvider is the provider as one of its accounts, l as the
// accounts list has it.
func pluginProvider(pp plugin.Provider, l pluginLogin) Provider {
+ return pluginProviderServing(pp, l, nil)
+}
+
+// pluginProviderServing is pluginProvider offering the models the
+// accounts at keys serve between them; nil keys, l's own.
+func pluginProviderServing(pp plugin.Provider, l pluginLogin, keys []string) Provider {
acct, user := l.acct, l.User
+ serves := func(cur plugin.Provider) []catalog.Model {
+ if keys == nil {
+ return pluginAccountCatalog(cur, acct.Key)
+ }
+ return pluginServedCatalog(cur, keys)
+ }
id := PluginID(pp.ID)
name := pp.Name
if name == "" {
@@ -357,9 +395,9 @@ func pluginProvider(pp plugin.Provider, l pluginLogin) Provider {
}
a.models = func() []catalog.Model {
if cur, ok := PluginOf(id); ok {
- return pluginAccountCatalog(cur, acct.Key)
+ return serves(cur)
}
- return pluginAccountCatalog(pp, acct.Key)
+ return serves(pp)
}
a.fetch = func(ctx context.Context) ([]catalog.Model, error) {
ps, err := plugin.Providers(ctx)
@@ -374,7 +412,7 @@ func pluginProvider(pp plugin.Provider, l pluginLogin) Provider {
if e := PluginListError(cur, acct.Key); e != "" {
return nil, fmt.Errorf("%s couldn't list its models: %s", name, e)
}
- return catalog.Chat(pluginAccountCatalog(cur, acct.Key)), nil
+ return catalog.Chat(serves(cur)), nil
}
}
return nil, fmt.Errorf("%s's plugin no longer lists it", name)