diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 9435acc13..791161629 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -938,7 +938,7 @@ func nativeEndpoints(e provider.Entry) []string { // asks for them in its list (provider.DrawersHeader): "kind": "image", // which an agent's list never has, so it takes none for a model to chat // with. A gateway key held to some models is told of those alone, as its -// chat list is. +// chat list is: models it names that an account it may use serves (#2579). func drawerObjects(r *http.Request) []map[string]any { who, held := keyHolds(r) var out []map[string]any @@ -947,7 +947,7 @@ func drawerObjects(r *http.Request) []map[string]any { continue } for _, m := range Drawers(p) { - if held && !modelAllowed(who, p, m.ID) { + if held && (!modelAllowed(who, p, m.ID) || !accountServes(who, p, m.ID)) { continue } name := cmp.Or(m.Name, m.ID) @@ -965,7 +965,8 @@ func drawerObjects(r *http.Request) []map[string]any { // videomakerObjects are the video models magpie makes videos with, as // another magpie asks for them (provider.VideomakersHeader): "kind": "video". -// A gateway key held to some models is told of those alone. +// A gateway key held to some models or accounts is told of those alone, +// as its chat list is (#2579). func videomakerObjects(r *http.Request) []map[string]any { who, held := keyHolds(r) var out []map[string]any @@ -974,7 +975,7 @@ func videomakerObjects(r *http.Request) []map[string]any { continue } for _, m := range Videomakers(p) { - if held && !modelAllowed(who, p, m.ID) { + if held && (!modelAllowed(who, p, m.ID) || !accountServes(who, p, m.ID)) { continue } name := cmp.Or(m.Name, m.ID) diff --git a/internal/gateway/key_models_test.go b/internal/gateway/key_models_test.go index 1153b5b9d..cd40681fd 100644 --- a/internal/gateway/key_models_test.go +++ b/internal/gateway/key_models_test.go @@ -394,3 +394,55 @@ func TestGatewayKeyModelsHoldTheImageAndVideoLists(t *testing.T) { t.Fatalf("a key held to one video model is told of image %v, video %v", image, video) } } + +// A gateway key held to an account that serves only a chat model must not +// be told of that provider's image/video models in /v1/models (#2579): the +// chat catalog already applies accountServes; drawerObjects and +// videomakerObjects used to list them from modelAllowed alone. +func TestGatewayKeyAccountsHoldTheImageAndVideoLists(t *testing.T) { + grokSignedIn(t) + newGrokMedia(t) + keys, secrets := newCaller(t, "Held", "Free") + if _, err := access.Update("accounts-key", access.Change{Key: keys[0].ID, Accounts: []string{"grok/me@x.ai"}}); err != nil { + t.Fatal(err) + } + if err := provider.SetAccountModels("grok", "me@x.ai", []string{"grok-4"}); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + _ = provider.SetAccountModels("grok", "me@x.ai", nil) + }) + h := New().Handler() + listed := func(secret string) (image, video []string) { + r := httptest.NewRequest("GET", "/v1/models", nil) + if secret != "" { + r.Header.Set("Authorization", "Bearer "+secret) + } + r.Header.Set(provider.DrawersHeader, "1") + r.Header.Set(provider.VideomakersHeader, "1") + w := httptest.NewRecorder() + h.ServeHTTP(w, r) + var l struct{ Data []struct{ ID, Kind string } } + if err := json.Unmarshal(w.Body.Bytes(), &l); err != nil { + t.Fatal(w.Body.String()) + } + for _, m := range l.Data { + switch m.Kind { + case "image": + image = append(image, m.ID) + case "video": + video = append(video, m.ID) + } + } + return image, video + } + if image, video := listed(""); !slices.Contains(image, "grok/grok-imagine-image") || !slices.Contains(video, "grok/grok-imagine-video") { + t.Fatalf("this computer is told of image %v, video %v", image, video) + } + if image, video := listed(secrets[1]); !slices.Contains(image, "grok/grok-imagine-image") || !slices.Contains(video, "grok/grok-imagine-video") { + t.Fatalf("a free key is told of image %v, video %v", image, video) + } + if image, video := listed(secrets[0]); len(image) != 0 || len(video) != 0 { + t.Fatalf("a key whose account serves no image or video model is told of image %v, video %v", image, video) + } +}