Files
magpie/internal/gateway/groupfast.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

83 lines
2.8 KiB
Go

package gateway
import (
"context"
"github.com/yetone/magpie/internal/provider"
)
// claudeFastBeta is the beta Claude's fast mode (speed "fast") is asked
// under.
const claudeFastBeta = "fast-mode-2026-02-01"
// withFast asks for the vendor's fast mode in the agent's own protocol, as
// a group's member sent fast is (provider.Group.Fast): priority processing
// in OpenAI's words, speed "fast" in Anthropic's. A request carried on in
// another protocol keeps it (Request.Fast), and goes out with it only
// where the vendor and model have one (buildResponses, buildChat, build,
// cursorModelID). A client's own ask for Ultrafast, faster than priority,
// is kept: sent fast, it isn't sent slower than it asked (#1784,
// zhenghaowen0967: OMP with fastPicks asked Ultrafast and went as
// priority). An account that doesn't offer it is asked for priority in
// its place, as any is (tierMate).
func withFast(proto provider.Protocol, body []byte) []byte {
switch proto {
case provider.Chat, provider.Responses:
if askedTier(body) == "ultrafast" {
return body
}
return withFields(body, map[string]any{"service_tier": "priority"})
case provider.Anthropic:
return withFields(body, map[string]any{"speed": "fast"})
}
return body
}
// fastPick sends the model an agent picked to be sent fast (#954) so:
// every account and key of it, not a fallback to another. Ultra sends it
// as Ultrafast where the account offers that (#1954).
func fastPick(cands []candidate, pl planned, providerID, model string, ultra bool) {
for i, c := range cands {
if c.p.ID == providerID && c.model == model {
cands[i].fast, cands[i].ultra = true, ultra
}
}
for _, ws := range [][]Weighed{pl.order, pl.left} {
for i, w := range ws {
if w.Provider == providerID && w.Model == model {
ws[i].Fast = true
}
}
}
}
// ownFastKey marks a try of a member or pick the user sent fast on a
// provider they added by its address (provider.OwnFast, #1615): it is asked for service_tier
// "priority" on its OpenAI API (forwardTranslated) whatever API the agent
// spoke. An agent on Chat or Responses says it in its own body as well
// (withFast), which a relayed request carries as it is.
type ownFastKey struct{}
func withOwnFast(ctx context.Context) context.Context {
return context.WithValue(ctx, ownFastKey{}, true)
}
func isOwnFast(ctx context.Context) bool {
v, _ := ctx.Value(ownFastKey{}).(bool)
return v
}
// ultraKey marks a try of a pick sent Ultrafast (#1954) from an agent on
// Claude's API: its speed "fast" goes to the ChatGPT backend as Ultrafast
// (forwardTranslated), on an account that offers it.
type ultraKey struct{}
func withUltra(ctx context.Context) context.Context {
return context.WithValue(ctx, ultraKey{}, true)
}
func isUltra(ctx context.Context) bool {
v, _ := ctx.Value(ultraKey{}).(bool)
return v
}