Files
magpie/internal/provider/modelprefs.go
T
yetone 3df75514b9 agents: each agent's own model list — under an agent's name on the Agents page, "Showing 5 / 32 models" (or "All 41 models") opens a list to take models out of that agent's picker one by one and put them back (@ChongkaiX on X: 在 magpie 里选哪些模型出现在 Codex 的模型列表里) — until now an agent's list could only be narrowed by whole providers or families (Visible). settings.HiddenModels holds, by agent, the catalog entries taken out, after Visible: a deny list, so a new model is shown. provider.CatalogFor drops them, so the gateway's /models, the agents' files and pickers and Codex's catalog all follow in one place, and catalog.Touched rewrites the agents' files; Codex's own ChatGPT models are dropped from the backend's list too (CodexNativeHidden), and the list's ETag and models_cache tag carry them (CodexListTag) so Codex asks again. The model an agent is set to is marked 在用 and never taken out, on the server as well. The list groups routing groups first, then providers with their logos and on/total, a long group folded; search, 全部/已显示, a group's 全部显示/全部隐藏 on hover, 恢复全部显示; a click on a row applies at once, Esc or a click elsewhere closes it, and nothing scrolls. A renamed provider's hidden models follow it. Tested with agent-models.test.cjs in Chromium and WebKit, English and Chinese; TestHiddenModels (provider), TestCodexModelListHidesOnAgentsPage (gateway) and TestAgentModelsAPI (gui) in sandbox HOMEs. click-scroll, panel-fold, old-webkit, model-pick, menu-scroll, add-button pass; GOOS=linux go vet, linux and windows builds, go test -tags nogui ./... pass. Not tried in the real Wails window or against a real Codex.
2026-09-30 12:44:59 +08:00

316 lines
9.6 KiB
Go

package provider
import (
"errors"
"fmt"
"slices"
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// A model's name is the vendor's, or models.dev's, and agents are shown it
// with the provider after it (Claude Opus 5.5 · Claude Code). The user can
// give one of a provider's models a name of their own instead (Opus): it is
// kept in settings' ModelNames by "<provider id>/<model id>", apart from the
// vendor's list, so a Refresh, a restart or an upgrade leaves it, and the
// same model another provider serves keeps its own. Only the name changes:
// the model's id, and where requests for it go, stay as they were. The
// provider still goes after it (Opus · Claude Code), same as the vendor's
// own name, so a picker full of renamed models can still be told apart by
// vendor; it's left off only when the name given already says it.
// The user can also keep only some of the reasoning levels a model has
// (low, medium and high of its six): settings' ModelEfforts, kept the same
// way. The lists magpie hands out — the gateway's, and those it writes into
// the agents' files — offer only those, in the vendor's order; a request
// for another still reaches the vendor as it did. A model whose levels
// aren't known (one models.dev doesn't list, a custom provider's) can be
// given some of Levels the same way, which it is then taken to have.
// ModelName is the name the user gave a provider's model, if any.
func ModelName(pid, model string) (string, bool) {
return modelNameIn(settings.Load().ModelNames, pid, model)
}
func modelNameIn(names map[string]string, pid, model string) (string, bool) {
n, ok := names[pid+"/"+model]
return n, ok && n != ""
}
// splitRef is "provider/model" as the provider it names, by its id now,
// and the model.
func splitRef(ref string) (*Provider, string, error) {
r := strings.TrimPrefix(strings.TrimSpace(ref), "magpie/")
if strings.HasPrefix(r, GroupPrefix) {
return nil, "", errors.New("that is a routing group, not a provider's model")
}
pid, model, ok := strings.Cut(r, "/")
if !ok || pid == "" || model == "" {
return nil, "", fmt.Errorf("name a model as provider/model, not %q", ref)
}
p, err := Find(pid)
if err != nil {
return nil, "", err
}
return p, model, nil
}
// SetModelName names a provider's model, spelled "provider/model"; an empty
// name gives it back its own. The agents that keep the models in files of
// their own are told (catalog.Touched), as for any other change of the
// catalog.
func SetModelName(ref, name string) error {
p, model, err := splitRef(ref)
if err != nil {
return err
}
name = strings.Join(strings.Fields(name), " ")
if len([]rune(name)) > 80 {
return errors.New("a model's name is at most 80 characters")
}
if name != "" && !p.serves(model) {
return fmt.Errorf("%s has no model %s (magpie provider %s lists them)", p.ID, model, p.ID)
}
s := settings.Load()
key := p.ID + "/" + model
if s.ModelNames[key] == name {
return nil
}
if name == "" {
delete(s.ModelNames, key)
} else {
if s.ModelNames == nil {
s.ModelNames = map[string]string{}
}
s.ModelNames[key] = name
}
if err := settings.Save(s); err != nil {
return err
}
catalog.Touched()
return nil
}
// Levels are the reasoning levels a model whose own aren't known can be
// given.
var Levels = []string{"none", "minimal", "low", "medium", "high", "xhigh", "max"}
// SetModelEfforts keeps only these of a provider's model's reasoning
// levels in the lists magpie hands out; none, or all it has, offers them
// all again. They must be levels the model has — or, for a model whose
// levels aren't known, any of Levels, which it is given; none takes them
// away.
func SetModelEfforts(ref string, efforts []string) error {
p, model, err := splitRef(ref)
if err != nil {
return err
}
all := p.Known(model)
var keep []string
for _, e := range efforts {
e = strings.ToLower(strings.TrimSpace(e))
if e == "" {
continue
}
if len(all) == 0 && !slices.Contains(Levels, e) {
return fmt.Errorf("%q is not a reasoning level (they are %s)", e, strings.Join(Levels, ", "))
}
if len(all) > 0 && !slices.Contains(all, e) {
return fmt.Errorf("%s/%s has no reasoning level %q (it has %s)", p.ID, model, e, strings.Join(all, ", "))
}
if !slices.Contains(keep, e) {
keep = append(keep, e)
}
}
keep = effortsKept(all, keep) // in the vendor's order
if len(keep) == len(all) {
keep = nil
}
s := settings.Load()
key := p.ID + "/" + model
if slices.Equal(s.ModelEfforts[key], keep) {
return nil
}
if len(keep) == 0 {
delete(s.ModelEfforts, key)
} else {
if s.ModelEfforts == nil {
s.ModelEfforts = map[string][]string{}
}
s.ModelEfforts[key] = keep
}
if err := settings.Save(s); err != nil {
return err
}
catalog.Touched()
return nil
}
// ModelEfforts are the reasoning levels the user kept of the provider's
// models, by model id.
func (p Provider) ModelEfforts() map[string][]string {
out := map[string][]string{}
for k, es := range settings.Load().ModelEfforts {
if m, ok := strings.CutPrefix(k, p.ID+"/"); ok && len(es) > 0 {
out[m] = es
}
}
return out
}
// effortsKept is all without the levels the user didn't keep, in its own
// order; all of them when none of those kept is among them any more (the
// vendor's list changed). A model with none known has those the user gave
// it.
func effortsKept(all, kept []string) []string {
if len(kept) == 0 {
return all
}
if len(all) == 0 {
return slices.DeleteFunc(slices.Clone(Levels), func(e string) bool { return !slices.Contains(kept, e) })
}
out := slices.DeleteFunc(slices.Clone(all), func(e string) bool { return !slices.Contains(kept, e) })
if len(out) == 0 {
return all
}
return out
}
// SetModelImage says whether a provider's model takes images, in place
// of what its vendor's list says. nil gives that answer back.
func SetModelImage(ref string, images *bool) error {
p, model, err := splitRef(ref)
if err != nil {
return err
}
if images != nil && !p.serves(model) {
return fmt.Errorf("%s has no model %s (magpie provider %s lists them)", p.ID, model, p.ID)
}
if images != nil {
if vendor, known := vendorSees(p, model); known && *images == vendor {
images = nil
}
}
s := settings.Load()
key := p.ID + "/" + model
if images == nil {
if _, ok := s.ModelImages[key]; !ok {
return nil
}
delete(s.ModelImages, key)
} else {
if cur, ok := s.ModelImages[key]; ok && cur == *images {
return nil
}
if s.ModelImages == nil {
s.ModelImages = map[string]bool{}
}
s.ModelImages[key] = *images
}
if err := settings.Save(s); err != nil {
return err
}
catalog.Touched()
return nil
}
// ImageOverride is the user's answer for whether pid's model takes images.
func ImageOverride(pid, model string) (bool, bool) {
v, ok := settings.Load().ModelImages[pid+"/"+model]
return v, ok
}
// ApplyImage is what magpie tells of model: the user's answer when they
// gave one, else images as the vendor's list has it (known is that list's
// explicit answer, nil when it didn't say).
func ApplyImage(pid, model string, images bool, known *bool) (bool, *bool) {
if v, ok := ImageOverride(pid, model); ok {
return v, &v
}
return images, known
}
// vendorSees is whether p's list says model takes images, and whether it
// said so at all.
func vendorSees(p *Provider, model string) (bool, bool) {
for _, m := range p.Available() {
if m.ID != model {
continue
}
if m.ImageInput != nil {
return *m.ImageInput, true
}
return m.Images || catalog.SeesImages(m.ID), false
}
return false, false
}
// renameModelPrefs moves the names, levels and image answers given to a
// provider's models to the id it has now.
func renameModelPrefs(s *settings.Settings, from, to string) bool {
named := renameKeys(s.ModelNames, from, to)
imaged := renameKeys(s.ModelImages, from, to)
hidden := false
for _, ids := range s.HiddenModels {
for i, id := range ids {
if rest, ok := strings.CutPrefix(id, from+"/"); ok {
ids[i], hidden = to+"/"+rest, true
}
}
}
return renameKeys(s.ModelEfforts, from, to) || named || imaged || hidden
}
func renameKeys[V any](m map[string]V, from, to string) bool {
moved := map[string]V{}
for k, v := range m {
if rest, ok := strings.CutPrefix(k, from+"/"); ok {
delete(m, k)
moved[to+"/"+rest] = v
}
}
for k, v := range moved {
m[k] = v
}
return len(moved) > 0
}
// Label is how an agent's list names the entry: its name (the user's own,
// when they gave one, else the vendor's) with its provider's after it, or
// "routing group" for a group's — dropped only when a name of the user's
// own already carries the provider's, so it isn't said twice.
func (e Entry) Label() string {
by := e.Provider.Name
if e.Group != "" {
by = "routing group"
}
if e.Default != "" && strings.Contains(strings.ToLower(e.Name), strings.ToLower(by)) {
return e.Name
}
return e.Name + " · " + by
}
// ModelNames are the names the user gave the provider's models, by model id.
func (p Provider) ModelNames() map[string]string {
out := map[string]string{}
for k, n := range settings.Load().ModelNames {
if m, ok := strings.CutPrefix(k, p.ID+"/"); ok && n != "" {
out[m] = n
}
}
return out
}
// EffortsOf is the reasoning levels one of a provider's listed models has,
// before any the user left out.
func EffortsOf(m catalog.Model) []string { return effortsOf(m) }
// serves reports whether model is one of the provider's, listed or exposed:
// a name given to a model it doesn't have would never be shown.
func (p Provider) serves(model string) bool {
has := func(m catalog.Model) bool { return m.ID == model }
return slices.ContainsFunc(p.Available(), has) || slices.ContainsFunc(p.Exposed(), has)
}