Files
magpie/internal/gateway/groupfast.go
T
yetone 52f138448a gateway: a model switched fast keeps the client's own Ultrafast ask (#1784, zhenghaowen0967)
What changed: withFast, which sends a candidate in its vendor's fast mode
(a fastPick in the agent's picker, or a group member set Fast), wrote
service_tier "priority" over whatever the client sent. A client asking
for "ultrafast" was sent priority. Now a Chat or Responses body that
already asks for "ultrafast" is left as it is. The existing per-account
step after it still applies: an account whose model list doesn't offer
ultrafast is tried after one that does, and is asked for priority.

Why: zhenghaowen0967 reported, on 0.1.1219 with OMP, that gpt-6.1-sol was
switched fast for omp (fastPicks.omp) and its ultrafast requests showed
no sign of going as ultrafast. With the fast pick on, the request was
rewritten to priority before the ultrafast check ran, so it went to the
first account (Plus) as priority instead of to the Pro account that
offers ultrafast.

Test: TestFastPickKeepsTheClientsUltrafast (tier_accounts_test.go).
- omp's fast-picked codex/gpt-6-luna, asked on /v1/responses at
  chatgpt.com (redirected to the fake ChatGPT backend), goes to acct-pro
  as ultrafast.
- With Pro out, it goes acct-pro:ultrafast then acct-plus:priority.
- With no tier asked, it goes acct-plus:priority as before.
- A Chat body keeps "ultrafast" too.

What was run:
- With the fix taken out, the test fails the way the report describes:
  "ultrafast tried acct-plus:priority".
- go test -tags nogui -count=1 -run 'Fast|Tier|Ultrafast|Priority'
  ./internal/gateway passes.
- The full gateway package, gofmt and vet are run with the next commit
  (see its body).
2026-10-13 14:22:52 +08:00

68 lines
2.3 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.
func fastPick(cands []candidate, pl planned, providerID, model string) {
for i, c := range cands {
if c.p.ID == providerID && c.model == model {
cands[i].fast = true
}
}
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
}