diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 7a309f9a0..9756fa435 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -937,14 +937,19 @@ func nativeEndpoints(e provider.Entry) []string { // drawerObjects are the image models magpie draws with, as another magpie // 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. -func drawerObjects() []map[string]any { +// with. A gateway key held to some models is told of those alone, as its +// chat list is. +func drawerObjects(r *http.Request) []map[string]any { + who, held := keyHolds(r) var out []map[string]any for _, p := range provider.All() { if !p.On() || p.DecideOnly() { continue } for _, m := range Drawers(p) { + if held && !modelAllowed(who, p, m.ID) { + continue + } name := cmp.Or(m.Name, m.ID) o := map[string]any{"id": p.ID + "/" + m.ID, "object": "model", "type": "model", "kind": "image", "created": 0, "owned_by": p.ID, "display_name": name, "magpie_label": name + " · " + p.Name, @@ -960,13 +965,18 @@ func drawerObjects() []map[string]any { // videomakerObjects are the video models magpie makes videos with, as // another magpie asks for them (provider.VideomakersHeader): "kind": "video". -func videomakerObjects() []map[string]any { +// A gateway key held to some models is told of those alone. +func videomakerObjects(r *http.Request) []map[string]any { + who, held := keyHolds(r) var out []map[string]any for _, p := range provider.All() { if !p.On() || p.DecideOnly() { continue } for _, m := range Videomakers(p) { + if held && !modelAllowed(who, p, m.ID) { + continue + } name := cmp.Or(m.Name, m.ID) out = append(out, map[string]any{"id": p.ID + "/" + m.ID, "object": "model", "type": "model", "kind": "video", "created": 0, "owned_by": p.ID, "display_name": name, "magpie_label": name + " · " + p.Name, @@ -1053,10 +1063,10 @@ func (s *Server) models(w http.ResponseWriter, r *http.Request) { } } if r.Header.Get(provider.DrawersHeader) != "" { - data = append(data, drawerObjects()...) + data = append(data, drawerObjects(r)...) } if r.Header.Get(provider.VideomakersHeader) != "" { - data = append(data, videomakerObjects()...) + data = append(data, videomakerObjects(r)...) } if r.Header.Get(provider.DecidersHeader) != "" { deciders := keyAllowed(r, provider.Deciders()) diff --git a/internal/gateway/key_models_test.go b/internal/gateway/key_models_test.go index 8baecad0a..1153b5b9d 100644 --- a/internal/gateway/key_models_test.go +++ b/internal/gateway/key_models_test.go @@ -341,3 +341,56 @@ func TestGatewayKeyModelsHoldCountTokensAndSystemOne(t *testing.T) { t.Fatalf("a key given the decider: %d %s, decided %d", w.Code, w.Body.String(), decided) } } + +// Another magpie added as a provider with a gateway key held to some +// models asks this one for its image and video models too +// (provider.DrawersHeader, VideomakersHeader). It is told of those the +// key may use alone, as it is of chat models; a key free of models, and +// this computer, are told of every one. +func TestGatewayKeyModelsHoldTheImageAndVideoLists(t *testing.T) { + grokSignedIn(t) + newGrokMedia(t) + keys, secrets := newCaller(t, "Elsewhere", "Grok", "Free") + if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"elsewhere/*"}}); err != nil { + t.Fatal(err) + } + if _, err := access.Update("models-key", access.Change{Key: keys[1].ID, Models: []string{"grok/grok-imagine-video"}}); err != nil { + t.Fatal(err) + } + 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[2]); !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 held elsewhere is told of image %v, video %v", image, video) + } + if image, video := listed(secrets[1]); len(image) != 0 || !slices.Equal(video, []string{"grok/grok-imagine-video"}) { + t.Fatalf("a key held to one video model is told of image %v, video %v", image, video) + } +} diff --git a/internal/gateway/video_custom_test.go b/internal/gateway/video_custom_test.go index baddba54c..901642aec 100644 --- a/internal/gateway/video_custom_test.go +++ b/internal/gateway/video_custom_test.go @@ -164,7 +164,7 @@ func TestCustomProviderMakesVideos(t *testing.T) { t.Fatalf("no model named: %d %s", code, raw) } // another magpie asking this one for its video models is told of it - if objs := videomakerObjects(); len(objs) != 1 || objs[0]["id"] != "studio/vid-1" { + if objs := videomakerObjects(httptest.NewRequest("GET", "/v1/models", nil)); len(objs) != 1 || objs[0]["id"] != "studio/vid-1" { t.Fatalf("videomakerObjects %v", objs) } }