diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 279ffb9c4..fc1e60d1c 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -3013,12 +3013,17 @@ the row of the entry that id reaches, with its label, under the bare id. not listed twice. - A routing group the user made is listed by its own id too, `fast` beside `group/fast` (`groupBare`; 小八 on Discord), after the models' bare ids, - and under Only in `group/fast`'s place. Only where that id is the - group's alone: not when a provider serves a model by it (that model's - bare id is listed, reaching the group as `GroupFor` routes it), when it - is listed already, when `GroupFor` of it reaches another group, or for a - group magpie found (its `auto/` row is listed under the model's - bare id already). A group a model's bare id already reaches adds nothing. + and under Only in `group/fast`'s place. Only where a request for that + id reaches the group: not when it is listed already, when `GroupFor` of + it reaches another group, or for a group magpie found (its + `auto/` row is listed under the model's bare id already). A + group a model's bare id already reaches adds nothing: a group named for + a model a provider serves, `glm-5.2`, is listed through that model's + row while the row is shown, and by its own row when it isn't (the + group lists its members alone, a provider kept for routing groups, the + agent's hidden models), since `GroupFor` takes `glm-5.2` to the group + before `Resolve` either way. Before, such a group stayed `group/glm-5.2` + under Only (小八 on Discord, v0.1.4377). - The target must be in the list shown: a gateway key held to some models sees only the bare ids that reach one of its models, so a key held to `b/*` doesn't see `glm-5.3` that would go to `a`. diff --git a/internal/gateway/bare_group_ids_test.go b/internal/gateway/bare_group_ids_test.go index df08cbc4d..9a5f029be 100644 --- a/internal/gateway/bare_group_ids_test.go +++ b/internal/gateway/bare_group_ids_test.go @@ -123,3 +123,74 @@ func TestBareIDsListUserGroups(t *testing.T) { t.Fatalf("group/fast under Only: %d by %q", code, by) } } + +// 小八 on Discord, v0.1.4377: with Only on, group/glm-5.2 stayed while +// other groups lost the prefix. Each such group is named for a model a +// provider serves, and its members are listed through the group alone +// (Group.Unlists), so no provider's glm-5.2 row is shown to carry the +// bare id. A request for glm-5.2 reaches the group all the same (GroupFor +// before Resolve), so the group is listed as glm-5.2, in its place. +func TestBareIDsListGroupNamedForAModel(t *testing.T) { + fresh(t) + for _, p := range []provider.Provider{ + {ID: "a", Name: "A", Key: "k", Models: []string{"glm-5.2", "other-a"}}, + {ID: "b", Name: "B", Key: "k", Models: []string{"glm-5.2"}}, + } { + f := &fake{t: t, ctype: "application/json", reply: `{"id":"from-` + p.ID + `","choices":[{"index":0,"message":{"role":"assistant","content":"hi"},"finish_reason":"stop"}]}`} + up := httptest.NewServer(f) + t.Cleanup(up.Close) + p.Chat = up.URL + "/v1" + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + } + // b first: a request that went by Resolve instead would reach a + g := provider.Group{ID: "glm-5.2", Name: "GLM 5.2", Members: []string{"b/glm-5.2", "a/glm-5.2"}, Routing: provider.Ordered, Unlists: true} + if err := provider.SaveGroup(g); err != nil { + t.Fatal(err) + } + get := func() []string { return listedIDs(t, httptest.NewRequest("GET", "/v1/models", nil)) } + count := func(ids []string, id string) int { + n := 0 + for _, x := range ids { + if x == id { + n++ + } + } + return n + } + off := get() + if !slices.Contains(off, "group/glm-5.2") || slices.Contains(off, "a/glm-5.2") || slices.Contains(off, "b/glm-5.2") { + t.Fatalf("off: %v, want group/glm-5.2 and no member listed", off) + } + + if err := provider.SetBareIDs(true, false); err != nil { + t.Fatal(err) + } + if on := get(); count(on, "glm-5.2") != 1 { + t.Fatalf("on: %v, want glm-5.2 once", on) + } + + if err := provider.SetBareIDs(true, true); err != nil { + t.Fatal(err) + } + only := get() + if slices.Contains(only, "group/glm-5.2") { + t.Fatalf("Only: %v still lists group/glm-5.2", only) + } + if i, j := slices.Index(off, "group/glm-5.2"), slices.Index(only, "glm-5.2"); i != j || count(only, "glm-5.2") != 1 { + t.Fatalf("Only: glm-5.2 at %d of %v, want once where group/glm-5.2 was, %d of %v", j, only, i, off) + } + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models/glm-5.2", nil)) + if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"id":"glm-5.2"`) { + t.Fatalf("GET /v1/models/glm-5.2: %d %s", rec.Code, rec.Body.String()) + } + for _, m := range []string{"glm-5.2", "group/glm-5.2"} { + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"`+m+`","messages":[{"role":"user","content":"hi"}]}`))) + if by := rec.Header().Get("X-Magpie-Provider"); rec.Code != 200 || by != "b" { + t.Fatalf("%s: %d by %q, want the group's first member, b: %s", m, rec.Code, by, rec.Body.String()) + } + } +} diff --git a/internal/provider/bare.go b/internal/provider/bare.go index 82e45c7ad..958e69e20 100644 --- a/internal/provider/bare.go +++ b/internal/provider/bare.go @@ -22,7 +22,8 @@ import ( // a model the list doesn't show (a gateway key held to other models, an // id like anthropic/claude-x that names a provider of the user's), isn't // listed by it. A routing group the user made is listed by its own id as -// well (fast for group/fast), where nothing else goes by that id. +// well (fast for group/fast, glm-5.2 for group/glm-5.2), where a +// request for that id reaches the group. // BareModelIDs says whether /v1/models lists models by their bare ids too. func BareModelIDs() bool { return settings.Shared().BareModelIDs } @@ -104,19 +105,21 @@ func BareEntries(shown []Entry) []Bare { // a request for it reaches it by: its own id, fast. Only where that is // unambiguous: not for a group magpie found (listed under its model's // bare id already, or by its auto/), nor by an id listed already -// or given to another row, one a provider serves a model by, or one -// GroupFor takes elsewhere. +// or given to another row, or one GroupFor takes elsewhere. +// +// A group named for a model a provider serves (glm-5.2) is listed so +// too: a request for glm-5.2 reaches the group, GroupFor before Resolve, +// whether or not the provider's glm-5.2 row is shown. When it is, that +// row's bare id reaches the group first (BareEntries' reached); when it +// isn't (the group lists its members alone, Group.Unlists; a provider +// kept for routing groups; the agent's hidden models), the group's own +// row carries the id, or Only left group/glm-5.2 (小八 on Discord). func groupBare(shown []Entry, all []Group, entries []Entry, ids, seen map[string]bool, j int) (Bare, bool) { e := shown[j] id := e.Group if !e.Named || id == "" || e.ID != GroupPrefix+id || strings.Contains(id, "/") || ids[id] || seen[id] { return Bare{}, false } - for _, x := range entries { - if x.Model == id || x.ID == id { - return Bare{}, false - } - } if i := bareTarget(shown, all, entries, id); i < 0 || shown[i].Group != id { return Bare{}, false }