Files
magpie/internal/provider/fastpick.go
T
yetone e243937ca3 gateway: a fast pick can be sent as Ultrafast where the account offers it (#1954)
zhenghaowen0967 asked for an agent's fast pick of a ChatGPT model to go as
Codex's Ultrafast without the client asking for it. settings.UltraPicks
holds the agent's fast picks that are set to Ultrafast; each one is in
FastPicks too.

- The gateway marks those candidates `ultra` in fastPick.
  - On Chat and Responses, a try sends service_tier "ultrafast".
  - From Messages, a try carries withUltra to forwardTranslated, which
    sets Request.Ultrafast for the ChatGPT backend.
  - In every case, the #1784 check decides the account. One whose list
    doesn't offer Ultrafast goes after a mate that does (tierMate). With
    no mate left, it is sent priority.
  - That check now covers Chat as well as Responses.
- Sibling fix: chat.go dropped a Chat client's own service_tier
  "ultrafast" (Fast was false, Ultrafast unset). It now parses as
  Responses does.
- provider: SetFastTier (""/priority/ultrafast) behind SetFastPick.
  CanUltra and UltraModels say whether one of Codex's accounts lists
  Ultrafast on the model. UltraPicks follow a provider rename
  (renameModelPrefs) and survive a GUI settings save.
- GUI: an option gets ultraOk/ultra. Where ultraOk is set, the bolt in
  the picker steps usual -> Fast -> Ultrafast -> usual, as a camera's
  flash button steps its modes. Ultrafast shows two overlapped bolts, in
  the row and after the field's model. Tooltips and the status line are
  in en, zh, zh-TW, ja and de. POST /api/agent-fast takes `ultra`.
- No `:ultrafast` suffix for routing groups yet.

Tests, each broken by hand and seen red:
- TestUltraPickSentUltrafast covers omp/Responses with no tier asked
  (acct-pro:ultrafast), the Pro account out (then acct-plus:priority),
  another agent unchanged, opencode/Chat and claude/Messages picks, a
  Chat client's own ultrafast, and switching back to Fast or off.
  - Red with ultra forced false (4 cases).
  - Red without withUltra (claude went priority).
  - Red without the chat.go parse (2 cases).
- TestSetFastPick: red without UltraPicks in renameModelPrefs.
- TestSettingsSaveKeepsWhatItDoesNotSend: red without the api.go carry.
- fast-ultra.test.cjs (en zh ja de, window and 440px panel): red (9/9)
  without the Ultrafast step.

Run (vet, gofmt, GUI and full suite at base 8b84dc902; the reruns
below at a rebase onto 6b3ee5a03 and then onto 63be47717 origin/main):
- gofmt -l: clean.
- go vet -tags nogui ./... for darwin, linux and windows: clean.
- GUI, chromium + webkit: fast-ultra, fast-pick, group-member-fast,
  i18n-keys, model-pick and 14 other agent-model/picker files. 197/197
  pass.
- Full suite: set -o pipefail; go test -timeout 45m ./... (sandbox
  HOME, macOS, 3 other sessions' suites running at the same time):
  every package ok except two.
  - internal/gateway: TestMaxRPMGroupPrefersMemberWithRoom hit its fixed
    1s bar (1.126s) under load. It passes 5/5 alone (-count=5). Filed
    as #2520.
  - internal/gui: TestPanelJoinsFullScreenSpaces, red on macOS 26.2 on
    main already, filed as #2450.
- Rerun of internal/gateway and internal/library after the rebase onto
  6b3ee5a03 (both packages changed upstream), -timeout 60m, load
  average ~290: library ok. gateway hit the 60m timeout (the test
  running, TestPauseByFolder, was 13s in) and
  TestQueueRefusalIsInTheUsageLedger saw b's 1s queue timeout as a
  second 429. Filed as #2541. TestQueueRefusalIsInTheUsageLedger,
  TestPauseByFolder and TestUltraPickSentUltrafast, -count=5 at this
  head: ok.
- At the 63be47717 rebase: gofmt clean; vet darwin/linux/windows clean.
  internal/provider ok, internal/settings ok. internal/agent hit the
  45m timeout under load ~295 (in TestCursorLocalEffort, at 0s), so
  this is not a green for that package. Its -run
  "Fast|Option|Models|Picker|Cursor|Codex" subset, run through the
  shared 3-slot queue, was ok in 794s. The whole package is left to
  CI's Test run.

Not tried: a real Pro account's Ultrafast. The fake ChatGPT backend's
model lists offer it on one account only.
2026-10-16 19:14:52 +08:00

150 lines
4.5 KiB
Go

package provider
import (
"errors"
"fmt"
"slices"
"strings"
"github.com/yetone/magpie/internal/catalog"
"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.
//
// A fast pick of a ChatGPT model may be sent as Ultrafast instead (#1954,
// settings' UltraPicks): the gateway asks it of each account that offers it
// on the model, and priority of one that doesn't.
// 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)]
}
// IsUltraPick reports whether agent's requests for the entry id are sent as
// Ultrafast where the account offers it.
func IsUltraPick(agent, id string) bool {
return slices.Contains(UltraPicks(agent), id)
}
// UltraPicks are the fast picks agent's requests for are sent as Ultrafast.
func UltraPicks(agent string) []string {
return heldSettings().UltraPicks[strings.ToLower(agent)]
}
// CanUltra reports whether a fast pick of model on p can be sent as
// Ultrafast: a ChatGPT model that one of Codex's accounts lists the tier
// on (Account.Tiers). A list that doesn't say tiers offers none.
func CanUltra(p Provider, model string) bool {
return UltraOn(p, model, UltraModels())
}
// UltraOn is CanUltra with UltraModels read once, for a list of them all.
func UltraOn(p Provider, model string, ultra map[string]bool) bool {
return !p.IsPlugin() && p.Account != nil && p.Account.Agent == "codex" && CanFast(p, model) && ultra[model]
}
// UltraModels are the models one of Codex's accounts offers Ultrafast on.
func UltraModels() map[string]bool {
out := map[string]bool{}
for _, l := range Logins("codex") {
live, _, found := catalog.Live(accountModels("codex", l.User))
if !found {
continue
}
for _, m := range live {
if slices.Contains(m.Tiers, "ultrafast") {
out[m.ID] = true
}
}
}
return out
}
// 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 {
tier := ""
if fast {
tier = "priority"
}
return SetFastTier(agent, id, tier)
}
// SetFastTier sends agent's requests for the entry id at tier: "" the usual
// speed, "priority" fast, "ultrafast" Ultrafast where the account offers it
// (CanUltra), priority where it doesn't.
func SetFastTier(agent, id, tier string) error {
agent, id = strings.ToLower(strings.TrimSpace(agent)), strings.TrimSpace(id)
if agent == "" {
return errors.New("no agent")
}
switch tier {
case "", "priority", "ultrafast":
default:
return fmt.Errorf("magpie knows no tier %q", tier)
}
if tier != "" {
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)
}
if tier == "ultrafast" && !CanUltra(p, model) {
return fmt.Errorf("no account offers Ultrafast on %s", id)
}
}
s := settings.Load()
fast := setPick(&s.FastPicks, agent, id, tier != "")
ultra := setPick(&s.UltraPicks, agent, id, tier == "ultrafast")
if !fast && !ultra {
return nil
}
if err := settings.Save(s); err != nil {
return err
}
Changed() // a held settings read sees it
return nil
}
// setPick puts id in agent's list in m, or takes it out, and reports
// whether that changed the list.
func setPick(m *map[string][]string, agent, id string, on bool) bool {
was := (*m)[agent]
keep := slices.DeleteFunc(slices.Clone(was), func(x string) bool { return x == id })
if on {
keep = append(keep, id)
}
if slices.Equal(was, keep) {
return false
}
if len(keep) == 0 {
delete(*m, agent)
} else {
if *m == nil {
*m = map[string][]string{}
}
(*m)[agent] = keep
}
return true
}