Files
magpie/internal/provider/fastpick.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

75 lines
2.1 KiB
Go

package provider
import (
"errors"
"fmt"
"slices"
"strings"
"github.com/yetone/magpie/internal/settings"
)
// An agent's pick of a model may be sent in its vendor's fast mode (#954),
// as a routing group's member may (Group.Fast): switched by the model in
// the agent's picker, kept by agent (settings' FastPicks), and asked for by
// the gateway on every request the agent sends that model. A group takes
// none: its members are sent as it says.
// IsFastPick reports whether agent's requests for the entry id
// ("<provider>/<model>") are sent fast.
func IsFastPick(agent, id string) bool {
return slices.Contains(FastPicks(agent), id)
}
// FastPicks are the entries agent's requests for are sent fast, read once
// for a list of them all.
func FastPicks(agent string) []string {
return heldSettings().FastPicks[strings.ToLower(agent)]
}
// SetFastPick sends agent's requests for the entry id fast, or not. Only a
// model with a fast mode magpie can ask for (CanFast) is switched on.
func SetFastPick(agent, id string, fast bool) error {
agent, id = strings.ToLower(strings.TrimSpace(agent)), strings.TrimSpace(id)
if agent == "" {
return errors.New("no agent")
}
if fast {
if strings.HasPrefix(id, GroupPrefix) {
return fmt.Errorf("%s is a routing group: its members are sent fast as it says, on the Routing page", id)
}
p, model, ok := Resolve(id)
if !ok {
return fmt.Errorf("magpie knows no model %q", id)
}
if p.ID+"/"+model != id {
return fmt.Errorf("%s is not a model as <provider>/<model>", id)
}
if !CanFast(p, model) {
return fmt.Errorf("%s has no fast mode magpie can ask for", id)
}
}
s := settings.Load()
was := s.FastPicks[agent]
keep := slices.DeleteFunc(slices.Clone(was), func(x string) bool { return x == id })
if fast {
keep = append(keep, id)
}
if slices.Equal(was, keep) {
return nil
}
if len(keep) == 0 {
delete(s.FastPicks, agent)
} else {
if s.FastPicks == nil {
s.FastPicks = map[string][]string{}
}
s.FastPicks[agent] = keep
}
if err := settings.Save(s); err != nil {
return err
}
Changed() // a held settings read sees it
return nil
}