Files
magpie/internal/gateway/groupfast.go
T
yetone 343920464f routing: Fast keeps the provider's extra-body Ultrafast; a relay's GPT model can go Ultrafast (KoiLx0 on X)
KoiLx0 on X: with Azure or another provider that takes service_tier
"ultrafast", the group's speed toggle stopped at Fast, and they asked
whether the group's Fast overwrites {"service_tier": "ultrafast"} put
in the provider's extra request body. Before this commit it did. A member
sent fast got service_tier "priority" in its body before the extra body
was merged. A non-forced extra-body field only fills in a missing value,
so "priority" went upstream. Only "!service_tier" survived.

Precedence now:
- Member not sent fast: as before. The agent's own tier is kept, and the
  extra body's service_tier fills in when the agent sends none.
- Member sent Fast: priority. If the provider's extra body asks for
  "ultrafast", it is sent ultrafast, so Fast is never sent slower than
  the user asked of the provider. A slower extra-body tier (flex) gives
  way to the toggle, which the user set on this member.
- Member sent Ultrafast: ultrafast.
- A forced "!service_tier" in the extra body still goes over all of
  these as written.
The same holds for an agent's fast pick (same member loop).

Ultrafast on a provider the user added by its address (OwnFast): the
toggle goes on to Ultrafast for a GPT model when OpenAI offers the tier
on that model. That is known when one of Codex's accounts lists it, or
when Codex's own entry in models_cache.json for the slug does
(codexcat.OwnOffers). Codex itself is already offered the tier on such a
provider from that same entry (ownTiers). Like its Fast, Ultrafast is
only asked for. A provider that turns the field away is asked again
without it. Other vendors and OpenAI's own API are unchanged. The
Ultrafast button's tooltip on such a provider says the tier is asked of
the provider (new string in zh, zh-TW, ja, de).

Tests, red on the old code:
- TestFastMemberOwnProviderExtraTier: the outgoing body for each
  combination of group speed and extra body, from Chat, Responses and
  Anthropic agents. Old code sent "priority" for "fast + extra ultrafast"
  and for "Ultrafast".
- TestCanUltraOwnProvider: old code returned false for a relay's
  gpt-6.1-sol that Codex's entry offers Ultrafast on.
- group-member-fast.test.cjs: an own provider's member steps usual,
  Fast, Ultrafast and saves ultra; one with no Ultrafast steps back to
  usual. en/zh, Chromium and WebKit.

Docs: gateway-routing.md (a group member's speed) and
providers-accounts.md (Extra body) say the precedence and the
Ultrafast rule.

Run on mote at df2652f63 (base b0d634b85), log kept whole:
- gofmt -l internal: nothing.
- go vet (darwin) provider, gateway, codexcat, gui, agent; GOOS=linux
  and GOOS=windows go vet provider, gateway, codexcat: clean.
- go test -timeout 45m -count=1 provider (176s), gateway (1017s),
  codexcat, agent (275s), settingsapi: ok. go test -tags nogui gui: ok.
- GUI on mica, Chromium and WebKit: group-member-fast, model-speed,
  routing-service-tier, i18n-keys, i18n-en-no-cjk pass. fast-ultra is
  red the same way on b0d634b85 (extra POST /api/update/using, #6201).
- Broken by hand: the new Go tests red on the old sources (above).
Rebased onto 338eae3c2 (provider/models.go, remote groups) after
4f40a1061 (sessions, agent/codex.go, app.js, i18n.js; none
of this change's files). At that head, on mica: TestLoopGuardLeavesOrdinaryRepliesAlone (reads
the two edited docs), TestFastMemberOwnProviderExtraTier,
TestGroupMemberUltrafast and TestCanUltraOwnProvider ok; at 338eae3c2
plus this, go vet and go test of provider (143s) and codexcat, and
gateway's fast, ultra, model-tier and loop-guard tests: ok.

Not tried: a real request to Azure or a relay that serves "ultrafast";
the whole GUI suite; a Linux or Windows run beyond cross-vet.
2026-11-15 19:06:45 +08:00

90 lines
3.0 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
}
// extraUltra reports whether p's extra body asks for Ultrafast itself
// (provider.ExtraTier), which a member or pick sent fast on it is sent at.
func extraUltra(p provider.Provider) bool {
tier, _ := p.ExtraTier()
return tier == "ultrafast"
}
// 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
}