Files
magpie/internal/gui/agent_models.go
T
yetone 2533ccdaf9 gui: a model with a fast mode is sent fast, or not, from its row in the agent's picker (#954, Gyarados4157) — Fast was only a routing group member's (Group.Fast), so a GPT on a ChatGPT account picked straight in Codex or OpenCode could only go fast through a group of one. A model provider.CanFast says has a fast mode (a ChatGPT account's GPT, an OpenAI key's Responses GPT/o-series, Cursor's -fast, Claude Opus on an Anthropic key) now has a bolt beside its star in the agent's picker and in the same picker opened from a provider's row; a click posts /api/agent-fast and switches it for that agent (settings FastPicks, by agent, "<provider>/<model>", moved by a provider rename and kept by the Settings page's save) without changing the pick or closing the picker; a refusal puts it back. The field shows a bolt after a model sent fast. The gateway asks for the vendor's fast mode (service_tier "priority" / speed "fast", as for a group member) on that agent's requests for that entry, every account and key of it, not its fallbacks, another agent's requests or the same model at another provider; the Routing trace marks the try Fast. A routing group is never switched: its members say it. zh/ja/de added. fast-pick (window and 440px panel, en/zh/ja/de, Chromium and WebKit; the bolt only on models with a fast mode, the post, the refusal, the provider row's picker) passes and fails without the fix; TestFastPick (gateway, Chat/Responses/Anthropic in, fails with the gateway hunk off), TestSetFastPick, TestFastPickOptions and the settings-keep test (fails without the carry) pass with -race; i18n-keys, gui-ja, gui-de, favorite-star, agent-models-picker, claude-picker-fold and click-scroll pass; go test ./internal/gui/ ./internal/provider/ ./internal/agent/ ./internal/gateway/ ./internal/settings/ pass.
2026-10-06 11:15:52 +08:00

259 lines
7.9 KiB
Go

package gui
import (
"cmp"
"encoding/json"
"errors"
"net/http"
"strings"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/provider"
)
// Which of the catalog an agent's lists show, picked one model at a time
// on the Agents page (settings.HiddenModels): the line under an agent's
// name counts them, and opens the list to pick in.
// modelCountJSON is how many models an agent's lists show, of those its
// visibility gives it.
type modelCountJSON struct {
Shown int `json:"shown"`
Listed int `json:"listed"`
// By is the models shown by whose they are, a connected agent's
// expanded row's chips (OpenAI 6 · OpenRouter 24)
By []modelGroupJSON `json:"by,omitempty"`
// Names are the first few models shown by name, for a row that says
// which it lists rather than how many (Claude Desktop's)
Names []string `json:"names,omitempty"`
}
// countNames is how many models' names modelCount gives.
const countNames = 3
// modelGroupJSON is how many of the models shown one provider (or the
// routing groups) gives.
type modelGroupJSON struct {
Name string `json:"name"`
Icon string `json:"icon,omitempty"`
Icons []string `json:"icons,omitempty"`
N int `json:"n"`
}
// agentModelJSON is a model an agent may be shown, as the list draws it.
type agentModelJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Group string `json:"group"` // its provider's name, or "Routing groups"
Icon string `json:"icon,omitempty"`
Icons []string `json:"icons,omitempty"`
// Logo is its maker's: known by the model's family, or its provider's
// when that is the maker; "" when neither says, and for a group, which
// has Icons
Logo string `json:"logo,omitempty"`
Context int `json:"context,omitempty"`
Hidden bool `json:"hidden,omitempty"`
// InUse: the agent is set to it, so it can't be taken out
InUse bool `json:"inUse,omitempty"`
// Own: one of Codex's own, a ChatGPT account's, which Codex lists
// ahead of magpie's until the user puts them in an order (#855)
Own bool `json:"own,omitempty"`
}
// takesCatalog reports whether an agent picks among magpie's catalog: some
// option of a field of its is a catalog entry.
func takesCatalog(fields []fieldJSON) bool {
for _, f := range fields {
for _, o := range f.Options {
if o.Ref != "" {
return true
}
}
}
return false
}
// agentFields are an agent's pickers as the Agents page draws them.
func agentFields(a *agent.Agent, vals map[string]string) []fieldJSON {
out := []fieldJSON{}
for _, f := range a.Fields {
opts := f.Options(vals)
if opts == nil {
opts = []agent.Option{}
}
out = append(out, fieldJSON{Key: f.Key, Label: f.Label, Value: vals[f.Key], Options: opts})
}
return out
}
// agentModelCount is the line under an agent's name, nil for an agent that
// doesn't pick among the catalog nor has its menu from the gateway's list
// (agent.ListsModels). Its pickers list only the models shown, so
// with every one taken out they have no catalog entry left, yet the line is
// the one way to put them back (#356): it stays while any is hidden.
func agentModelCount(a *agent.Agent, fields []fieldJSON) *modelCountJSON {
id := a.ListsFor()
if takesCatalog(fields) || a.ListsModels {
return modelCount(id)
}
if len(provider.HiddenModels(id)) == 0 {
return nil
}
if c := modelCount(id); c.Shown < c.Listed {
return c
}
return nil
}
func modelCount(id string) *modelCountJSON {
listed, _ := provider.ListedFor(id)
off := provider.HiddenModels(id)
c := &modelCountJSON{Listed: len(listed)}
at := map[string]int{}
for _, e := range listed {
if off[e.ID] {
continue
}
c.Shown++
if len(c.Names) < countNames {
c.Names = append(c.Names, cmp.Or(e.Name, e.Model, e.ID))
}
g := modelGroupJSON{Name: e.Provider.Name, Icon: e.Provider.Icon}
if e.Group != "" {
g = modelGroupJSON{Name: agent.RoutingGroups, Icons: e.Icons}
}
i, ok := at[g.Name]
if !ok {
i = len(c.By)
at[g.Name] = i
c.By = append(c.By, g)
}
c.By[i].N++
}
return c
}
// usedBy reports whether one of the agent's values is the entry: its id,
// with a prefix the agent's files give it (magpie/…), or, for the agent's
// own account, the vendor's model id alone.
func usedBy(a *agent.Agent, vals map[string]string, e provider.Entry) bool {
for _, v := range vals {
if v == "" {
continue
}
if v == e.ID || strings.HasSuffix(v, "/"+e.ID) {
return true
}
if acc := e.Provider.Account; e.Group == "" && acc != nil && acc.Agent == a.ListsFor() && v == e.Model {
return true
}
}
return false
}
func agentModelList(a *agent.Agent) []agentModelJSON {
id := a.ListsFor()
listed, _ := provider.ListedFor(id)
off := provider.HiddenModels(id)
vals := a.Values()
out := []agentModelJSON{}
for _, e := range listed {
m := agentModelJSON{ID: e.ID, Name: e.Name, Group: e.Provider.Name, Icon: e.Provider.Icon, Context: e.Context,
Hidden: off[e.ID], InUse: usedBy(a, vals, e), Own: id == "codex" && provider.CodexOwn(e)}
if m.Name == "" {
m.Name = e.Model
}
m.Logo = agent.ModelIcon(e.Model)
if e.Group != "" {
// a group shows its providers' icons, as it does everywhere else
m.Group, m.Icons, m.Logo = agent.RoutingGroups, e.Icons, ""
} else if m.Logo == "" {
if p := provider.Preset(e.Provider.Preset); p != nil && p.Kind == provider.KindVendor {
m.Logo = e.Provider.Icon
}
}
out = append(out, m)
}
return out
}
// orderable reports whether the agent's model list is put in the order the
// user drags it into: Codex's, whose /model lists the models by the
// priority magpie gives them (#855).
func orderable(id string) bool { return id == "codex" }
func agentModelsAPI(mux *http.ServeMux) {
// a model the agent picks sent in its vendor's fast mode, or not (#954):
// for is the option's fastFor, the agent whose requests it goes on
mux.HandleFunc("POST /api/agent-fast", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
For, Ref string
Fast bool
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
if err := provider.SetFastPick(in.For, in.Ref, in.Fast); err != nil {
fail(rw, err)
return
}
writeJSON(rw, map[string]any{"fast": provider.IsFastPick(in.For, in.Ref)})
})
mux.HandleFunc("GET /api/agent-models/{id}", func(rw http.ResponseWriter, r *http.Request) {
a, err := agent.Find(r.PathValue("id"))
if err != nil {
fail(rw, err)
return
}
writeJSON(rw, map[string]any{"models": agentModelList(a), "orderable": orderable(a.ListsFor()), "ordered": len(provider.ModelOrder(a.ListsFor())) > 0})
})
// hidden is every entry to take out of the agent's lists; the others
// are shown, and one the agent is set to is kept in whatever is asked
//
// order, instead, is the order the agent's list puts them in (#855):
// the ones named first, as named; none puts back magpie's own
mux.HandleFunc("POST /api/agent-models/{id}", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
Hidden []string
Order *[]string
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
a, err := agent.Find(r.PathValue("id"))
if err != nil {
fail(rw, err)
return
}
if in.Order != nil {
if !orderable(a.ListsFor()) {
fail(rw, errors.New(a.Name+"'s model list can't be put in an order"))
return
}
if err := provider.SetModelOrder(a.ListsFor(), *in.Order); err != nil {
fail(rw, err)
return
}
writeJSON(rw, map[string]any{"models": agentModelList(a), "ordered": len(provider.ModelOrder(a.ListsFor())) > 0})
return
}
used := map[string]bool{}
for _, m := range agentModelList(a) {
used[m.ID] = m.InUse
}
var hidden []string
for _, id := range in.Hidden {
if !used[id] {
hidden = append(hidden, id)
}
}
if err := provider.SetHiddenModels(a.ListsFor(), hidden); err != nil {
fail(rw, err)
return
}
writeJSON(rw, map[string]any{"models": agentModelList(a), "count": modelCount(a.ListsFor())})
})
}