gateway: a hand-made group of embedding, rerank, speech, transcription or video models is served when its providers have picks

XYenon on Discord: a routing group of embedding models was a 404 on
/v1/embeddings. Picks are of chat models, so membersFound dropped every
member of a provider with picks as unpicked (#3911). 1d8658abc fixed this
for drawings with FindDrawingGroup. The sibling routes still used
FindGroup: retrieval (embeddings, rerank, speech, transcriptions and
translations, which all go through serveRetrieval) and /v1/videos.

provider.FindGroupAlso is FindGroup with an "also keep" test, and
FindDrawingGroup now wraps it. The gateway's mediaMembers keeps a member
that is not on its provider's chat list (offChatList), so a chat model the
user unticked stays out. mediaGroupFirst resolves the group's first such
member, because Resolve reads the agents' catalog and finds none of them.
The routing doc says the same.

Auto groups by kind (non-chat ids never reach autoGroups) are #5339 and
are not in this commit.

Run on mote:
- go test -count=1 -timeout 45m ./internal/gateway ./internal/provider:
  ok (gateway 823.6s, provider 180.0s)
- GOOS=linux and GOOS=windows go vet of the two packages: ok
- gofmt -l: clean
- No GUI file changed, so no GUI run.
- At the rebased head (on origin/main 2891ec7a7: 6 commits, balance_probe and
  others, none in group, retrieval or video code): go test -run
  'MediaGroup|VideoGroup|ImageGroup|Retrieval|Embedding|Speech|Transcri|Rerank|Group'
  ./internal/gateway ./internal/provider: ok; gofmt -l: clean

Red on the old code: with media_group.go removed and the three routes back
on FindGroup, TestMediaGroupOfProvidersWithChatPicks fails with 404 "magpie
knows no model "group/emb"" and TestVideoGroupOfProvidersWithChatPicks with
404 for "group/vid". In both, the members' providers have picks that don't
name the embedding/speech/video ids.

Broken by hand: offChatList made to return true lets the unticked chat
model in, and the test goes red ("a group of a chat model the user unticked
was asked: 400 ... chat-model is not an embedding model").

Not tried: a real vendor's embeddings or speech through a group (fakes
only), plugin-served embedding models, and the full suite (it runs after
the push).
This commit is contained in:
yetone
2026-11-05 14:57:43 +08:00
parent 2891ec7a7b
commit a2f7ed2e81
6 changed files with 182 additions and 6 deletions
File diff suppressed because one or more lines are too long
+5 -2
View File
@@ -98,7 +98,7 @@ func (s *Server) serveRetrieval(w http.ResponseWriter, r *http.Request, path, op
return
}
p, model, ok := resolveRetrievalModel(asked)
if _, _, isGroup := provider.FindGroup(asked); ok && !isGroup {
if _, _, isGroup := mediaMembers(asked); ok && !isGroup {
if msg, off := modelOff(p, model, asked); off {
call.Status, call.Error = 403, msg
writeError(w, provider.Chat, 403, msg)
@@ -116,7 +116,7 @@ func (s *Server) serveRetrieval(w http.ResponseWriter, r *http.Request, path, op
s.record(call)
return
}
g, ms, isGroup := provider.FindGroup(asked)
g, ms, isGroup := mediaMembers(asked)
if isGroup {
r = heldByGroup(r, g) // held to the group's accounts, if it is
}
@@ -323,6 +323,9 @@ func (s *Server) serveRetrieval(w http.ResponseWriter, r *http.Request, path, op
// bare id. Such models stay out of agents' chat-model lists, so the general
// resolver does not otherwise see them there.
func resolveRetrievalModel(id string) (provider.Provider, string, bool) {
if p, model, ok := mediaGroupFirst(id); ok {
return p, model, true
}
if p, model, ok := provider.Resolve(id); ok {
return p, model, true
}
+41
View File
@@ -0,0 +1,41 @@
package gateway
import (
"slices"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// mediaMembers is the routing group ref with its members ready now for
// an API that is no chat: embeddings, rerank, speech, transcriptions and
// videos. A provider's picks are of its chat models, and these models are
// in no chat list (catalog's textModel and pluginTalks leave embeddings,
// rerankers and speech models out when a list is fetched; videos are
// apart), so a member of a provider with picks was dropped as unpicked
// and a group of them was a 404 (XYenon on Discord). Such a member is
// kept, as a drawing's image models are (FindDrawingGroup, jzw690 on X).
// A chat model the user unticked (#3911) is on its provider's list and
// still stays out.
func mediaMembers(ref string) (provider.Group, []provider.Member, bool) {
return provider.FindGroupAlso(ref, offChatList)
}
// offChatList is whether p's list of models to chat with lacks model:
// one made for another API, which no pick can name.
func offChatList(p provider.Provider, model string) bool {
return !slices.ContainsFunc(provider.KeptListing(p).Available, func(m catalog.Model) bool { return m.ID == model })
}
// mediaGroupFirst is the first member of the group ref for an API that is
// no chat (mediaMembers): such a group is no model agents are offered, so
// Resolve, which reads their catalog, doesn't take it.
func mediaGroupFirst(ref string) (provider.Provider, string, bool) {
if !provider.IsGroupRef(ref) {
return provider.Provider{}, "", false
}
if _, ms, ok := mediaMembers(ref); ok && len(ms) > 0 {
return ms[0].Provider, ms[0].Model, true
}
return provider.Provider{}, "", false
}
+120
View File
@@ -0,0 +1,120 @@
package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// busyVendor answers every request 429, out of quota, and counts them.
func busyVendor(t *testing.T) (string, func() []string) {
t.Helper()
var mu sync.Mutex
var asked []string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
asked = append(asked, r.Method+" "+r.URL.Path)
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(429)
io.WriteString(w, `{"error":{"message":"quota exceeded"}}`)
}))
t.Cleanup(up.Close)
return up.URL, func() []string { mu.Lock(); defer mu.Unlock(); return append([]string(nil), asked...) }
}
// XYenon on Discord: a routing group of embedding models made by hand
// was a 404 when its providers had chat models picked, as most do. Picks
// are of chat models, and an embedding, reranker or speech model is in no
// chat list (it is left out when the list is fetched), so each member was
// dropped as unpicked (#3911's rule). The members are the group's, and
// the first one out of quota passes the request to the next — for
// embeddings, rerank, speech and transcriptions alike. The sibling of
// 1d8658abc, which kept a drawing group's image models.
func TestMediaGroupOfProvidersWithChatPicks(t *testing.T) {
fresh(t)
busy, busyAsked := busyVendor(t)
l, sp, sc := &library{}, &speaker{more: make(chan struct{})}, &scribe{}
lib, voice, ears := httptest.NewServer(l), httptest.NewServer(sp), httptest.NewServer(sc)
t.Cleanup(lib.Close)
t.Cleanup(voice.Close)
t.Cleanup(ears.Close)
// every provider has picks, and none of them is the media model
for _, p := range []provider.Provider{
{ID: "busy", Name: "Busy", Chat: busy + "/v1", Key: "k", Models: []string{"chat-model"}},
{ID: "lib", Name: "Lib", Chat: lib.URL + "/v1", Key: "key", Models: []string{"chat-model"}},
{ID: "voice", Name: "Voice", Chat: voice.URL + "/v1", Key: "key", Models: []string{"chat-model"}},
{ID: "ears", Name: "Ears", Chat: ears.URL + "/v1", Key: "key", Models: []string{"chat-model"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
for _, g := range []provider.Group{
{ID: "emb", Name: "emb", Members: []string{"busy/embed-1", "lib/embed-1"}, Routing: "order"},
{ID: "rr", Name: "rr", Members: []string{"busy/rerank-1", "lib/rerank-1"}, Routing: "order"},
{ID: "tts", Name: "tts", Members: []string{"busy/tts-1", "voice/gpt-4o-mini-tts"}, Routing: "order"},
{ID: "stt", Name: "stt", Members: []string{"busy/whisper-1", "ears/whisper-1"}, Routing: "order"},
// a chat model the user unticked stays out (#3911): a group of it alone has none
{ID: "unticked", Name: "unticked", Members: []string{"lib/chat-model-old"}, Routing: "order"},
} {
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
}
// lib's list has a chat model the user left unticked
if err := catalog.SaveLive("lib", lib.URL+"/v1", []catalog.Model{{ID: "chat-model"}, {ID: "chat-model-old"}}); err != nil {
t.Fatal(err)
}
s := New()
if code, raw := postRetrieval(t, s, "/v1/embeddings", `{"model":"group/emb","input":"hi"}`); code != 200 || !strings.Contains(raw, `"embedding":[0.1,-0.2]`) {
t.Fatalf("embeddings: %d %s", code, raw)
}
if sent := l.got("/v1/embeddings"); len(sent) != 1 || !strings.Contains(sent[0], `"model":"embed-1"`) {
t.Fatalf("lib was sent %v", sent)
}
if code, raw := postRetrieval(t, s, "/v1/rerank", `{"model":"group/rr","query":"q","documents":["a","b"]}`); code != 200 || !strings.Contains(raw, `"relevance_score":0.9`) {
t.Fatalf("rerank: %d %s", code, raw)
}
if code, _, raw := postSpeech(t, s, "/v1/audio/speech", `{"model":"group/tts","input":"hello","voice":"alloy"}`); code != 200 || string(raw) != string(mp3) {
t.Fatalf("speech: %d %.200s", code, raw)
}
body, ctype := audioFormBody(t, "model", "group/stt")
if code, _, raw := postAudio(t, s, "/v1/audio/transcriptions", ctype, body); code != 200 || !strings.Contains(raw, "Hello from the magpie.") {
t.Fatalf("transcription: %d %s", code, raw)
}
want := []string{"POST /v1/embeddings", "POST /v1/rerank", "POST /v1/audio/speech", "POST /v1/audio/transcriptions"}
if got := busyAsked(); strings.Join(got, ",") != strings.Join(want, ",") {
t.Fatalf("the busy member was asked %v, want %v", got, want)
}
if code, raw := postRetrieval(t, s, "/v1/embeddings", `{"model":"group/unticked","input":"hi"}`); code != 404 || len(l.got("/v1/embeddings")) != 1 {
t.Fatalf("a group of a chat model the user unticked was asked: %d %s", code, raw)
}
}
// A video group made by hand of two providers with chat models picked
// makes its video, the first member that serves no such model passing it
// on: its video models are no chat model, so no pick names them.
func TestVideoGroupOfProvidersWithChatPicks(t *testing.T) {
fresh(t)
a, b := newOpenAIVideos(t), newOpenAIVideos(t)
a.lacks = "sk-a"
saveFetched(t, provider.Provider{ID: "a", Name: "A", Chat: a.URL + "/v1", Key: "sk-a", Models: []string{"gpt-5.5"}})
saveFetched(t, provider.Provider{ID: "b", Name: "B", Chat: b.URL + "/v1", Key: "sk-b", Models: []string{"gpt-5.5"}})
if err := provider.SaveGroup(provider.Group{ID: "vid", Name: "vid", Members: []string{"a/vid-1", "b/vid-1"}, Routing: "order"}); err != nil {
t.Fatal(err)
}
code, obj, raw := asKey(t, New().Handler(), "", "POST", "/v1/videos", `{"model":"group/vid","prompt":"a cat on a fence"}`)
if code != 200 || obj["model"] != "b/vid-1" {
t.Fatalf("video: %d %s", code, raw)
}
if got := strings.Join(a.log(), ","); !strings.Contains(got, "POST /v1/videos sk-a") {
t.Fatalf("the first member was never asked: %v", a.log())
}
}
+5 -2
View File
@@ -1024,7 +1024,10 @@ func (s *Server) videosCreate(w http.ResponseWriter, r *http.Request) {
}
f.Model, call.Model = m, m
}
p, model, ok := provider.Resolve(f.Model)
p, model, ok := mediaGroupFirst(f.Model)
if !ok {
p, model, ok = provider.Resolve(f.Model)
}
if !ok {
p, model, ok = bareVideomaker(f.Model)
}
@@ -1036,7 +1039,7 @@ func (s *Server) videosCreate(w http.ResponseWriter, r *http.Request) {
fail(404, fmt.Sprintf("magpie knows no model %q to make videos with", f.Model))
return
}
g, ms, isGroup := groupToMake(f.Model, p, model, provider.FindGroup)
g, ms, isGroup := groupToMake(f.Model, p, model, mediaMembers)
if isGroup {
r = heldByGroup(r, g) // held to the group's accounts, if it is
}
+10 -1
View File
@@ -1034,6 +1034,15 @@ func GroupFinder() func(id string) (Group, []Member, bool) {
// beside them (jzw690 on X), so a group of image models draws on its
// members without any of them being a model agents are offered.
func FindDrawingGroup(id string, draws func(p Provider, model string) bool) (Group, []Member, bool) {
return FindGroupAlso(id, draws)
}
// FindGroupAlso is FindGroup with a member also says is the group's kept
// where its provider's picks leave it out: picks are of chat models, so a
// group of models made for another API (images, embeddings, speech,
// video) keeps those of a provider with picks only so (FindDrawingGroup,
// the gateway's media groups).
func FindGroupAlso(id string, also func(p Provider, model string) bool) (Group, []Member, bool) {
gid, ok := GroupIDOf(id)
if !ok {
return Group{}, nil, false
@@ -1041,7 +1050,7 @@ func FindDrawingGroup(id string, draws func(p Provider, model string) bool) (Gro
entries := providerEntries()
all := groupsIn(entries)
if g, ok := groupOf(all, gid); ok && !g.Disabled {
return g, membersFound(entries, entriesBy(entries), all, g, disabledNow(), draws), true
return g, membersFound(entries, entriesBy(entries), all, g, disabledNow(), also), true
}
return Group{}, nil, false
}