Files
magpie/internal/provider/bare.go
T
yetone 3126b0bacd gateway: with model ids without the provider on, a routing group the user made is listed by its own id too (小八 on Discord)
"路由组的外显模型名可以选择去掉 group/ 这个前缀吗": with Settings ›
Network and sharing › Model ids without the provider on, /v1/models
listed a user's routing group only as group/<id>, though the gateway
already takes a request for its bare id (GroupFor). BareEntries skipped
every group row. Now a group the user made adds a row by its own id
(fast beside group/fast; under Only in group/fast's place, through
OnlyBare as before), and GET /v1/models/fast and Ollama's lookup answer
it (BareEntry). Only where the id is the group's alone: not when a
provider serves a model by it (the model's bare id is listed, reaching
the group as the gateway routes it), when it is listed already, when
GroupFor of it reaches another group, for a group magpie found (its
auto/<model> row is already listed under the model's bare id), or for a
group a gateway key can't use.

Agents: unchanged. magpie still writes group/<id> into an agent's
config, as it writes provider/model under Only; the gateway takes either
id, so turning the setting on or off rewrites nothing. The Settings help
text says nothing that is now false, so i18n is unchanged.

Run (mica, mica-run, mote over 18 cores):
- go test -timeout 45m -count=1 ./internal/provider ./internal/gateway:
  ok (152s, 1059s)
- GOOS=linux go vet -tags nogui ./...: ok; GOOS=windows go vet ./...: ok
- gofmt -l on the changed Go files: nothing
- After the rebase onto origin/main: TestBare in internal/gateway again.

Broken by hand: with bare.go at its old version, TestBareIDsListUserGroups
fails "on: [... glm-5.3 only-a only-b], want [...] and then fast once".
With the found-group and already-reached checks taken out of groupBare /
BareEntries, it fails listing "auto-glm-5-3".

Not tried: a real client against a running magpie; the full ./... suite
and the GUI suite (no GUI file changed); Windows and Linux runs beyond vet.
2026-11-17 21:10:26 +08:00

202 lines
6.8 KiB
Go

package provider
import (
"strings"
"github.com/yetone/magpie/internal/settings"
)
// Model ids without the provider (settings.BareModelIDs; love1sbug on X):
// a client moved from another proxy (CLIProxyAPI) asks for gpt-5.6-sol,
// not codex/gpt-5.6-sol, and picks its model from /v1/models. The gateway
// has always taken a bare id (GroupFor, then Resolve); with the setting on,
// /v1/models lists each model by its bare id as well, after the
// provider/model ids, which stay as they were. Off by default: a list
// nobody asked to grow keeps its ids.
//
// A bare id is listed only where a request for it goes, as the gateway
// routes it: two providers' gpt-5.6-sol is one row, the routing group magpie
// found for it (its members in the Providers order, failed over between),
// or with found groups off the first provider in the Providers order. A
// bare id that is a catalog id already, or that the gateway would take to
// 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.
// BareModelIDs says whether /v1/models lists models by their bare ids too.
func BareModelIDs() bool { return settings.Shared().BareModelIDs }
// BareOnly says whether /v1/models lists a model by its bare id in place
// of the provider/model ids it stands for (settings.BareModelIDsOnly;
// qQianye on X), not beside them. See OnlyBare.
func BareOnly() bool {
s := settings.Shared()
return s.BareModelIDs && s.BareModelIDsOnly
}
// SetBareModelIDs turns settings.BareModelIDs on, beside the provider/model
// ids, or off.
func SetBareModelIDs(on bool) error { return SetBareIDs(on, false) }
// SetBareIDs sets settings.BareModelIDs to on and BareModelIDsOnly to only
// (which off clears). The list is built for each request, so the next one
// has the bare ids, or goes without them; no agent's file names a model by
// one, so none is written again.
func SetBareIDs(on, only bool) error {
only = on && only
s := settings.Load()
if s.BareModelIDs == on && s.BareModelIDsOnly == only {
return nil
}
s.BareModelIDs, s.BareModelIDsOnly = on, only
return settings.Save(s)
}
// Bare is a row a list adds for a bare id: ID, and Of, the index in the
// list of the entry a request for it reaches.
type Bare struct {
ID string
Of int
}
// BareEntries are the rows a list of shown adds for the bare ids, in the
// order the models first appear in shown, each once, then the routing
// groups the user made, by their own ids (groupBare).
func BareEntries(shown []Entry) []Bare {
defer Hold()()
ids := make(map[string]bool, len(shown))
for _, e := range shown {
ids[e.ID] = true
}
entries := providerEntries()
all := groupsIn(entries)
var out []Bare
seen := map[string]bool{}
reached := map[int]bool{}
for _, e := range shown {
m := e.Model
if e.Group != "" || m == "" || seen[m] || ids[m] {
continue
}
seen[m] = true
if i := bareTarget(shown, all, entries, m); i >= 0 {
out = append(out, Bare{ID: m, Of: i})
reached[i] = true
}
}
// then each routing group the user made that no model's bare id
// reaches, by its own id
for j, e := range shown {
if e.Group == "" || reached[j] {
continue
}
if b, ok := groupBare(shown, all, entries, ids, seen, j); ok {
seen[b.ID] = true
out = append(out, b)
}
}
return out
}
// groupBare is the Bare for the routing group the user made at shown[j]
// (小八 on Discord: group/fast is long to type into a client), by the id
// 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/<model>), nor by an id listed already
// or given to another row, one a provider serves a model by, or one
// GroupFor takes elsewhere.
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
}
return Bare{ID: id, Of: j}, true
}
// bareTarget is the index in shown of the entry a request for the bare id
// reaches: the routing group GroupFor gives it, else the provider's model
// Resolve does; -1 when shown has neither.
func bareTarget(shown []Entry, all []Group, entries []Entry, id string) int {
if !strings.Contains(id, "/") {
if gid, ok := groupForIn(all, id); ok {
for i, e := range shown {
if e.ID == gid || e.Group != "" && GroupPrefix+e.Group == gid {
return i
}
}
return -1
}
}
p, model, ok := resolveIn(entries, id)
if !ok {
return -1
}
for i, e := range shown {
if e.Group == "" && e.Provider.ID == p.ID && e.Model == model {
return i
}
}
return -1
}
// BareEntry is the entry of shown a client asking for a model by its bare
// id is told of (GET /v1/models/{id}), its ID the bare id.
func BareEntry(shown []Entry, id string) (Entry, bool) {
for _, b := range BareEntries(shown) {
if b.ID == id {
e := shown[b.Of]
e.ID = id
return e, true
}
}
return Entry{}, false
}
// OnlyBare is the list of shown with bare ids alone (BareOnly): each Bare
// of bares in the place of the first row it stands for, and the other
// rows it stands for left out. A Bare stands for the row a request for it
// reaches (a routing group found for the model, auto/glm-5.3, is that
// model's under another prefix), and for a provider's row of the same
// model when the request reaches that model: by that group, or by the
// provider first in the Providers order serving it by that id. A row it
// doesn't reach (a vendor/model id that names another provider of the
// user's, so a request for it goes there) and any other group's row stay,
// by their ids, so no model is lost from the list. Each row is its index
// in shown, and the bare id it is listed by, or "".
func OnlyBare(shown []Entry, bares []Bare) (rows []int, ids []string) {
by := make(map[string]int, len(bares))
reached := make(map[int]int, len(bares))
for i, b := range bares {
by[b.ID] = i
reached[b.Of] = i
}
placed := make([]bool, len(bares))
for j, e := range shown {
i, ok := reached[j]
if !ok && e.Group == "" {
if i, ok = by[e.Model]; ok {
to := shown[bares[i].Of]
ok = to.Group != "" || to.Model == e.Model
}
}
switch {
case !ok:
rows, ids = append(rows, j), append(ids, "")
case !placed[i]:
placed[i] = true
rows, ids = append(rows, bares[i].Of), append(ids, bares[i].ID)
}
}
return rows, ids
}