diff --git a/internal/gateway/count_tokens_test.go b/internal/gateway/count_tokens_test.go index d2a355672..a2ca3e277 100644 --- a/internal/gateway/count_tokens_test.go +++ b/internal/gateway/count_tokens_test.go @@ -176,7 +176,10 @@ func TestCountTokensKeyFailover(t *testing.T) { if err := provider.Save(p); err != nil { t.Fatal(err) } - if err := catalog.SaveLive(p.ID, up.URL, []catalog.Model{{ID: "m1", Keys: []string{provider.KeyID("k-first"), provider.KeyID("k-chat"), provider.KeyID("k-next")}}}); err != nil { + // k-unlisted's own list answered, without m1 (a key no list + // marks is unknown and serves, #1598) + if err := catalog.SaveLive(p.ID, up.URL, []catalog.Model{{ID: "m1", Keys: []string{provider.KeyID("k-first"), provider.KeyID("k-chat"), provider.KeyID("k-next")}}, + {ID: "m2", Keys: []string{provider.KeyID("k-unlisted")}}}); err != nil { t.Fatal(err) } if err := settings.Save(settings.Settings{RedactPersonal: true}); err != nil { diff --git a/internal/provider/models.go b/internal/provider/models.go index 8df9f8a98..fc122b530 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -760,17 +760,26 @@ func anyImageInput(a, b *bool) *bool { // Serves reports whether key k can be asked for model: false only when the // vendor's lists say another of the provider's keys sees it and k doesn't. +// A key no list marks — its own wasn't answered, or it was added since — +// is unknown, and serves: a failed list is not an empty one (#1598, the +// first of five keys left out of every request). func (p Provider) Serves(k KeyAccount, model string) bool { live, _, ok := p.live() if !ok { return true } + id := keyID(k.Key) + var keys []string + found, listed := false, false for _, m := range live { if m.ID == model { - return len(m.Keys) == 0 || slices.Contains(m.Keys, keyID(k.Key)) + keys, found = m.Keys, true + } + if !listed && slices.Contains(m.Keys, id) { + listed = true } } - return true + return !found || len(keys) == 0 || !listed || slices.Contains(keys, id) } // Exposed lists the models magpie offers to agents for this provider: the diff --git a/internal/provider/models_perkey_test.go b/internal/provider/models_perkey_test.go new file mode 100644 index 000000000..0eacb6f08 --- /dev/null +++ b/internal/provider/models_perkey_test.go @@ -0,0 +1,59 @@ +package provider + +import ( + "context" + "net/http" + "net/http/httptest" + "path/filepath" + "testing" +) + +// #1598 (wuwei0727): five keys, the first in key/keyName and four in +// keys. The vendor's list didn't answer the first key, the other four +// listed every model, so the first was taken to see none of them and was +// left out of every request as unlisted. A key whose list magpie doesn't +// have is unknown, not "sees nothing": it serves until a list says it +// doesn't. A key whose list did answer still serves only that list. +func TestKeyWithNoListStillServes(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + switch r.Header.Get("Authorization") { + case "Bearer sk-xx1": + http.Error(rw, `{"error":{"message":"rate limited"}}`, http.StatusTooManyRequests) + case "Bearer sk-xx5": + _, _ = rw.Write([]byte(`{"data":[{"id":"gpt-5.5"}]}`)) + default: + _, _ = rw.Write([]byte(`{"data":[{"id":"gpt-5.5"},{"id":"claude-opus-4-7"}]}`)) + } + })) + defer srv.Close() + // the reporter's shape: the first key outside keys, the rest in it + p := Provider{ID: "xx1", Name: "tokenone-xx1", Chat: srv.URL + "/v1", Key: "sk-xx1", KeyName: "xx1", + Keys: []KeyAccount{{Name: "xx2", Key: "sk-xx2"}, {Name: "xx3", Key: "sk-xx3"}, {Name: "xx4", Key: "sk-xx4"}, {Name: "xx5", Key: "sk-xx5"}}} + if _, err := p.fetch(context.Background()); err != nil { + t.Fatal(err) + } + first := p.first() + if first.Key != "sk-xx1" || first.Name != "xx1" { + t.Fatalf("first key %+v", first) + } + for _, m := range []string{"gpt-5.5", "claude-opus-4-7"} { + if !p.Serves(first, m) { + t.Errorf("the first key, whose list didn't answer, is left out of %s", m) + } + } + if p.Serves(p.Keys[3], "claude-opus-4-7") { + t.Error("a key whose list answered without the model serves it") + } + if !p.Serves(p.Keys[3], "gpt-5.5") || !p.Serves(p.Keys[0], "claude-opus-4-7") { + t.Error("a key serves what its own list has") + } + // the keys are left as the user saved them + if p.Key != "sk-xx1" || p.KeyName != "xx1" || len(p.Keys) != 4 { + t.Errorf("keys changed: %q %q %+v", p.Key, p.KeyName, p.Keys) + } +}