What changed
- usage.Record has tier_asked, tier_sent and tier_served, and the CSV gets
them as its last three columns.
- tier_asked is the client body's service_tier (Claude's speed fast reads
"fast").
- tier_sent is the tier the try's last request actually went out at. It
is noted in forwardOnce after every rewrite (a fast pick's priority,
Ultrafast turned to priority on a plan without it, buildResponses
dropping a tier the host can't take) onto the per-attempt wireAt. So a
handed-on try's own row has its own tier.
- tier_served is only what the reply said: service_tier at the top level,
in response.service_tier, or in an Anthropic usage. It is read by the
relay sniffer and by both translated readers, and is empty when the
reply says nothing. It is never filled in from the ask.
- Codex's own relayed sign-in (codex_backend) records the same three.
- /v1/magpie/route: Route.tier (asked), and each Try has tier and
tierServed. The session route has asked_tier, tier, tier_served and each
try's tier and tier_served.
- docs/reference.md (CSV columns) and gateway-routing.md describe them.
Why
zhenghaowen0967 asked that a request whose Ultrafast was sent as priority
to an account without it show that in usage, per try, without calling
the served tier confirmed unless the reply says so.
What was run
- TestTierRecordedPerTry. With Pro out, the first try is recorded
ultrafast, the second priority, and the final record has asked=ultrafast,
sent=priority and served empty while the fake doesn't echo. With an
echoing fake, served is what it said ("ultrafast", and "default" when
none was asked). It also checks the CSV columns, and the Codex relay path
with priority.
- TestUpstreamTierShapes covers the Chat chunk, Responses
response.completed, a Responses body, an Anthropic message_start and
message_delta usage, null, an over-long tier and non-JSON, as well as
bodyTier per protocol.
- Broke it by hand:
- with forwardOnce's noteWireTier removed, TestTierRecordedPerTry goes
red (route tries' tier empty, TierSent empty);
- with the sniffer's Tier line removed, TestUpstreamTierShapes goes red,
and so does the Codex relay case of TestTierRecordedPerTry.
- go test -tags nogui ./internal/gateway ./internal/usage ./internal/cli;
ledger_test's CSV expectations gain the three empty columns.
- gofmt -l; go vet on darwin; GOOS=linux and GOOS=windows go vet -tags
nogui.
Not done
- The served-tier fixtures are built from the shapes OpenAI and Anthropic
document, not from saved replies. Whether chatgpt.com's Codex backend
echoes service_tier wasn't tried against a real account.
- Bridges that don't go through forwardOnce (the Claude subscription,
Cursor's built-in) leave tier_sent empty.
- There is no price multiplier for priority/ultrafast and no GUI column.
168 lines
6.4 KiB
Go
168 lines
6.4 KiB
Go
package gateway
|
|
|
|
// GET /v1/magpie/route: where a session's turn went, for an agent's UI to
|
|
// show while the reply is still on its way (#405). Routing decides the
|
|
// member, and a group's decision model the effort, before the vendor is
|
|
// asked; each fallback is in the trace as it happens. This tells one
|
|
// session's latest route from the trace, nothing the trace doesn't hold.
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// routeWaitMax caps how long one ask waits for the route to change.
|
|
const routeWaitMax = 60 * time.Second
|
|
|
|
// SessionRoute is a session's latest request as routing has it so far.
|
|
type SessionRoute struct {
|
|
ID int64 `json:"id"`
|
|
Time time.Time `json:"time"`
|
|
Kind string `json:"kind,omitempty"` // a side call's: a title, a subagent…
|
|
Asked string `json:"asked"` // the model as the agent asked
|
|
AskedEffort string `json:"asked_effort,omitempty"` // the reasoning it asked for
|
|
AskedTier string `json:"asked_tier,omitempty"` // the service tier it asked for
|
|
Group string `json:"group,omitempty"` // the routing group's id, when it asked one
|
|
Rule *RuleHit `json:"rule,omitempty"` // the group's rules, as the Routing page has them
|
|
Nested []NestedRule `json:"nested,omitempty"`
|
|
// Model and Effort: the try under way, or the last, as provider/model
|
|
// and the reasoning it was sent at
|
|
Model string `json:"model,omitempty"`
|
|
Effort string `json:"effort,omitempty"`
|
|
// Tier and TierServed: the service tier that try went out at, and the
|
|
// one its reply said it ran at, when it said one (#1784)
|
|
Tier string `json:"tier,omitempty"`
|
|
TierServed string `json:"tier_served,omitempty"`
|
|
Served string `json:"served,omitempty"` // the model the reply says answered
|
|
Tries []RouteTry `json:"tries"`
|
|
Done bool `json:"done"`
|
|
Status int `json:"status,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
Millis int64 `json:"ms,omitempty"`
|
|
TTFT int64 `json:"ttft_ms,omitempty"`
|
|
}
|
|
|
|
// RouteTry is one member trying the request: each failed one was fallen
|
|
// over from.
|
|
type RouteTry struct {
|
|
Model string `json:"model"` // provider/model
|
|
Effort string `json:"effort,omitempty"`
|
|
// Tier: the service tier it went out at; TierServed: the one its
|
|
// reply said it ran at
|
|
Tier string `json:"tier,omitempty"`
|
|
TierServed string `json:"tier_served,omitempty"`
|
|
Done bool `json:"done"`
|
|
Status int `json:"status,omitempty"`
|
|
Fail string `json:"fail,omitempty"` // why it failed, as the trace tells it
|
|
Millis int64 `json:"ms,omitempty"`
|
|
}
|
|
|
|
func sessionRouteOf(r Route) SessionRoute {
|
|
out := SessionRoute{ID: r.ID, Time: r.Time, Kind: r.Kind, Asked: r.Model, AskedEffort: r.Effort, AskedTier: r.Tier, Rule: r.Rule, Nested: r.Nested,
|
|
Served: r.Served, Tries: []RouteTry{}, Done: r.Done, Status: r.Status, Error: r.Error, Millis: r.Millis, TTFT: r.TTFT}
|
|
if r.Group != nil {
|
|
out.Group = r.Group.ID
|
|
}
|
|
// a try names its seat, whose provider is in the order weighed
|
|
providerOf := func(id string) string {
|
|
for _, ws := range [][]Weighed{r.Order, r.Left} {
|
|
for _, w := range ws {
|
|
if w.ID == id {
|
|
return w.Provider
|
|
}
|
|
}
|
|
}
|
|
return r.Provider
|
|
}
|
|
for _, t := range r.Tries {
|
|
model := t.Model
|
|
if p := providerOf(t.ID); p != "" && model != "" {
|
|
model = p + "/" + model
|
|
}
|
|
out.Tries = append(out.Tries, RouteTry{Model: model, Effort: t.Effort, Tier: t.Tier, TierServed: t.TierServed, Done: t.Done, Status: t.Status, Fail: t.Fail, Millis: t.Millis})
|
|
}
|
|
if n := len(out.Tries); n > 0 {
|
|
out.Model, out.Effort = out.Tries[n-1].Model, out.Tries[n-1].Effort
|
|
out.Tier, out.TierServed = out.Tries[n-1].Tier, out.Tries[n-1].TierServed
|
|
}
|
|
return out
|
|
}
|
|
|
|
// sessionRoute answers GET /v1/magpie/route?session=<id>, or with the
|
|
// session in X-Magpie-Session (or the agent's own session header): the
|
|
// session's latest route, null before it has one. With after=<seq> and
|
|
// wait=<seconds> it waits, up to a minute, for the route to change past
|
|
// seq. Like the quotas, it answers this machine, and another only with the
|
|
// shared gateway's key.
|
|
func (s *Server) sessionRoute(w http.ResponseWriter, r *http.Request) {
|
|
if !local(r) && !sharedWith(r) {
|
|
writeError(w, provider.Chat, http.StatusForbidden, "magpie's routes are told to another machine only when magpie is shared on the local network (Settings → Share on local network) and the request carries its API key (Authorization: Bearer <key> or x-api-key: <key>)")
|
|
return
|
|
}
|
|
q := r.URL.Query()
|
|
session := strings.TrimSpace(q.Get("session"))
|
|
if len(session) > 128 {
|
|
session = session[:128] // as sessionOf keeps it
|
|
}
|
|
if session == "" {
|
|
session = sessionOf(r.Header)
|
|
}
|
|
if session == "" {
|
|
writeError(w, provider.Chat, http.StatusBadRequest, "name the session: ?session=<id> or the "+SessionHeader+" header")
|
|
return
|
|
}
|
|
after, _ := strconv.ParseInt(q.Get("after"), 10, 64)
|
|
secs, _ := strconv.ParseFloat(q.Get("wait"), 64)
|
|
wait := min(time.Duration(secs*float64(time.Second)), routeWaitMax)
|
|
seq, route := s.trace.sessionLatest(r.Context(), session, after, wait)
|
|
out := map[string]any{"session": session, "seq": seq, "route": nil}
|
|
if route != nil {
|
|
out["route"] = sessionRouteOf(*route)
|
|
}
|
|
writeJSON(w, 200, out)
|
|
}
|
|
|
|
// sessionLatest is a copy of session's latest route, and its seq; with
|
|
// wait, it waits up to that long for it to change past after.
|
|
func (t *trace) sessionLatest(ctx context.Context, session string, after int64, wait time.Duration) (int64, *Route) {
|
|
deadline := time.NewTimer(max(wait, 0))
|
|
defer deadline.Stop()
|
|
for {
|
|
t.mu.Lock()
|
|
var seq int64
|
|
var found *Route
|
|
for i := len(t.routes) - 1; i >= 0; i-- {
|
|
if r := t.routes[i]; r.Session == session {
|
|
c := *r
|
|
c.Order = append([]Weighed{}, r.Order...) // [] for none: the GUI reads it as a list
|
|
c.Left = append([]Weighed(nil), r.Left...)
|
|
c.Tries = append([]Try{}, r.Tries...)
|
|
seq, found = r.Seq, &c
|
|
break
|
|
}
|
|
}
|
|
// after past the trace's count: the gateway started over since
|
|
if wait <= 0 || seq > after || t.seq < after {
|
|
t.mu.Unlock()
|
|
return seq, found
|
|
}
|
|
if t.wake == nil {
|
|
t.wake = make(chan struct{})
|
|
}
|
|
wake := t.wake
|
|
t.mu.Unlock()
|
|
select {
|
|
case <-wake:
|
|
case <-deadline.C:
|
|
return seq, found
|
|
case <-ctx.Done():
|
|
return seq, found
|
|
}
|
|
}
|
|
}
|