Files
magpie/internal/provider/visible.go
T
yetone 273be22cac agents: Codex's /model lists its models in the order dragged on the Agents page (M3chD09, #855) — Codex lists the models by the priority magpie gives them: a ChatGPT account's own first, as the backend has them, then magpie's in the catalog's order, with no way to change it. Codex's model list (Pick on its row) now has a third view, Order: the models shown as Codex lists them, numbered, each dragged elsewhere (its grip shows on hover) or moved with Alt+↑/↓; it is saved at once (settings orderedModels, by agent and entry id), and the account's own models and magpie's go in one order. A model not in the order (one added later) goes after those that are, as it stood; the hidden stay hidden and out of the order view, hiding keeps the order, and a provider renamed renames its ids in the order too. Default order puts magpie's back. Codex gets it at once: signed in to ChatGPT, the gateway's /models gives the backend's and magpie's models priorities 1..n in that order and the list's ETag carries the order so Codex asks again; not signed in, the catalog file magpie writes for Codex (ListedFor → InOrder) is rewritten in it. Only Codex's list can be ordered; base_instructions are untouched. zh, ja, de strings. TestCodexModelListInUsersOrder (gateway: default order, an order across the account's and magpie's, the ETag changing, CatalogFor, opencode's untouched, none restoring it) and TestAgentModelOrderAPI (gui: orderable, an order read back, a new model last, hidden kept, hiding keeping it, reset, opencode refused) fail on the old code (no SetModelOrder/ModelOrder); codex-model-order.test.cjs (WebKit, en and zh: Order lists the own first and leaves the hidden out, Alt+↓ and a drag post the order, Default order posts none, nothing scrolls; a list that can't be ordered has no Order) fails on the old code (no Order). agent-models, agent-models-all-hidden, agent-models-loading, agent-model-groups, click-scroll, gui-ja and gui-de pass on WebKit. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-05 11:52:34 +08:00

225 lines
6.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package provider
import (
"errors"
"slices"
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// Which of the catalog an agent is shown. Settings' Visible names, for an
// agent, the families its lists hold: a family is the tag providers and
// groups are given (magpie provider set <id> family=relay), and a
// provider's or group's id names it alone. The gateway's model list and
// what magpie writes into the agents' files both come from CatalogFor, so
// the two never differ.
// Names are what an entry answers to in a visibility: its family, and its
// provider's id or its group's id (as group/<id> too).
func (e Entry) Names() []string {
var out []string
if e.Family != "" {
out = append(out, e.Family)
}
if e.Group != "" {
return append(out, e.Group, GroupPrefix+e.Group)
}
return append(out, e.Provider.ID)
}
// VisibleTo is what agent's lists are narrowed to, and whether they are.
func VisibleTo(agent string) ([]string, bool) {
names, ok := heldSettings().Visible[strings.ToLower(agent)]
return slices.Clone(names), ok
}
// Shows reports whether a visibility shows e.
func Shows(names []string, e Entry) bool {
for _, n := range e.Names() {
if slices.ContainsFunc(names, func(v string) bool { return strings.EqualFold(v, n) }) {
return true
}
}
return false
}
// Described is set by the gateway: whether an image sent to a model that
// can't see is described to it by one that can (Settings › Vision). Agents
// are then told every model takes images; told a model is text-only, they
// turn the user's image away before magpie is asked (Codex: "does not
// support image input").
var Described func() bool
// described is Described, false before the gateway sets it.
func described() bool { return Described != nil && Described() }
// CatalogFor is the catalog as agent is shown it, and what is kept from it
// (none when its lists aren't narrowed): its visibility's, less the models
// taken out of its lists one by one (HiddenModels).
func CatalogFor(agent string) (shown, hidden []Entry) {
listed, hidden := ListedFor(agent)
off := HiddenModels(agent)
for _, e := range listed {
if off[e.ID] {
hidden = append(hidden, e)
} else {
shown = append(shown, e)
}
}
return shown, hidden
}
// ListedFor is the catalog agent's visibility gives it, the models it may
// pick to show or not among, and what the visibility keeps from it.
func ListedFor(agent string) (listed, kept []Entry) {
all := Catalog()
names, ok := VisibleTo(agent)
if !ok {
return InOrder(agent, all), nil
}
for _, e := range all {
if Shows(names, e) {
listed = append(listed, e)
} else {
kept = append(kept, e)
}
}
return InOrder(agent, listed), kept
}
// HiddenModels are the ids of the entries taken out of agent's lists.
func HiddenModels(agent string) map[string]bool {
ids := heldSettings().HiddenModels[strings.ToLower(agent)]
out := make(map[string]bool, len(ids))
for _, id := range ids {
out[id] = true
}
return out
}
// SetHiddenModels takes these entries out of agent's lists, and puts back
// every other; none shows it every model its visibility gives it. The
// agents that keep the models in files of their own are told.
func SetHiddenModels(agent string, ids []string) error {
agent = strings.ToLower(strings.TrimSpace(agent))
if agent == "" {
return errors.New("no agent")
}
var keep []string
for _, id := range ids {
if id = strings.TrimSpace(id); id != "" && !slices.Contains(keep, id) {
keep = append(keep, id)
}
}
slices.Sort(keep)
s := settings.Load()
if slices.Equal(s.HiddenModels[agent], keep) {
return nil
}
if len(keep) == 0 {
delete(s.HiddenModels, agent)
} else {
if s.HiddenModels == nil {
s.HiddenModels = map[string][]string{}
}
s.HiddenModels[agent] = keep
}
if err := settings.Save(s); err != nil {
return err
}
catalog.Touched()
return nil
}
// Families are the families providers and groups are tagged with, sorted.
func Families() []string {
var out []string
add := func(f string) {
if f != "" && !slices.Contains(out, f) {
out = append(out, f)
}
}
for _, p := range All() {
add(p.Family)
}
for _, g := range Groups() {
add(g.Family)
}
slices.Sort(out)
return out
}
// ModelOrder is the order the user put agent's models in (OrderedModels),
// by entry id; none when they never did.
func ModelOrder(agent string) []string {
return slices.Clone(heldSettings().OrderedModels[strings.ToLower(agent)])
}
// SetModelOrder lists agent's models in this order, by entry id: the ones
// it names first, as it names them, then every other as before; none puts
// them back in magpie's own order. Only agent's lists follow it.
func SetModelOrder(agent string, ids []string) error {
agent = strings.ToLower(strings.TrimSpace(agent))
if agent == "" {
return errors.New("no agent")
}
var keep []string
for _, id := range ids {
if id = strings.TrimSpace(id); id != "" && !slices.Contains(keep, id) {
keep = append(keep, id)
}
}
s := settings.Load()
if slices.Equal(s.OrderedModels[agent], keep) {
return nil
}
if len(keep) == 0 {
delete(s.OrderedModels, agent)
} else {
if s.OrderedModels == nil {
s.OrderedModels = map[string][]string{}
}
s.OrderedModels[agent] = keep
}
if err := settings.Save(s); err != nil {
return err
}
catalog.Touched()
return nil
}
// InOrder is es in agent's order: the entries ModelOrder names first, as it
// names them, then the rest as they come — a model added since goes after.
// Codex's own, a ChatGPT account's models, lead the rest, as Codex's list
// has them before magpie's.
func InOrder(agent string, es []Entry) []Entry {
order := ModelOrder(agent)
if len(order) == 0 {
return es
}
at := make(map[string]int, len(order))
for i, id := range order {
at[id] = i
}
out := slices.Clone(es)
rank := func(e Entry) int {
if i, ok := at[e.ID]; ok {
return i
}
if strings.EqualFold(agent, "codex") && !CodexOwn(e) {
return len(order) + 1
}
return len(order)
}
slices.SortStableFunc(out, func(a, b Entry) int { return rank(a) - rank(b) })
return out
}
// CodexOwn reports whether e is one of a ChatGPT account's own models,
// which the ChatGPT backend lists to Codex by its bare slug (e.Model).
func CodexOwn(e Entry) bool {
return e.Group == "" && e.Provider.Account != nil && e.Provider.Account.Agent == "codex"
}