steven_4422 (Discord, #feedback): POST /v1/magpie/statusline answered 403
gateway_key_held to a key held to Claude's models, while GET
/v1/magpie/limit told the same key {"limited": false}. They asked for the
line scoped to what the key may use, /limit to agree, and a JSON line for
claude-hud's external usage snapshot.
The scoped view already exists (#2216): the status line goes through
scopeQuotas like the quotas routes, so a held key its owner turns Quotas
on for (Gateway -> Gateway keys, or `magpie gateway-key quotas <id> on`)
is told the accounts it may use of each provider whose every model it may
use, and nothing else. Its 403 never said so. Decision: the switch stays
the gate for every held key, budget, models or accounts alike. What holds
a key says nothing of whether its holder is the owner or a friend (#1981
was a friend's budget key seeing every account's email), and only the
owner knows that, so a model- or account-held key is not told quotas by
default either. The every-model rule of #2216 stays.
- scopeQuotas' refusal (heldQuotasOff) names the switch and what it then
tells. heldRefused, for the routes the switch doesn't open
(concurrency, trace, a card outside the scope, writeCards' told-none),
is unchanged.
- GET /v1/magpie/limit tells a held key heldTo (budget, models,
accounts), models, quotas and, with it on, quotaProviders (the
providers newQuotaScope tells it). No account is named. `limited`
keeps its meaning (the budget) for remote magpies that read it.
- ?format=json on the status line: {"text", "model", "accounts"}, the
cards the line shows in /v1/magpie/quotas?kept=1's shape (windows'
used, resetsAt), through the same scope.
- Docs: reference.md, integrating.md, gateway-routing.md; site docker
and remote pages in en, zh, ja.
Run at this head (4a9cfb52e + this), on mote:
- gofmt -l clean; go vet, GOOS=windows go vet, GOOS=linux go vet -tags
nogui on internal/gateway: clean.
- go test -run 'TestStatusline|TestHeldKey|TestRemoteMagpieShowsHeldKey|KeyLimit'
./internal/gateway: ok.
- TestStatuslineHeldKeyScope (new): this computer and a key held to
nothing see alpha One, alpha Two and beta; a key held to alpha/* sees
alpha's two; one held to alpha's Two sees Two alone; one held to
alpha/m1 sees nothing; the same through ?format=json; Quotas off is
403 gateway_key_held naming the switch, as text and JSON; /limit's
heldTo, quotas and quotaProviders for each, and no account id.
Broken by hand, each red: the scope dropped from the handler (every
held key saw every card, text and JSON); /limit's held block skipped;
scopeQuotas back on heldRefused.
Not run before the push: the full suite (mote had three full suites of
other sessions running at load ~70; it runs after, per the rules). Not
tried: a real Claude Code or claude-hud on another computer, a real
Claude account behind a held key, Windows and Linux test runs.
109 lines
4.1 KiB
Go
109 lines
4.1 KiB
Go
package gateway
|
|
|
|
// GET or POST /v1/magpie/statusline: the text `magpie statusline` prints
|
|
// for Claude Code's status line, for a Claude Code on another computer
|
|
// than the gateway (steven_4422 on Discord runs magpie in Docker and
|
|
// Claude Code on the Linux host: the container can't write the host's
|
|
// settings.json, and a command naming a binary inside it runs nowhere
|
|
// else). The host needs only curl:
|
|
//
|
|
// curl -fsS -m 1 -H 'Authorization: Bearer <key>' --data-binary @- http://<host>:3425/v1/magpie/statusline || true
|
|
//
|
|
// Claude Code puts its JSON on the command's stdin, and curl sends it as
|
|
// the body, whose session_id picks the "→ model" part (?session= does as
|
|
// well, for a GET). ?only=claude,a@example.com keeps those accounts, as
|
|
// `magpie statusline claude a@example.com` does. The line is made by
|
|
// provider.StatuslineText, the CLI's own, from the same kept windows
|
|
// (/v1/magpie/quotas?kept=1: no vendor asked) and the session's route.
|
|
//
|
|
// It answers like the quotas and the route it is made of: this computer,
|
|
// and another only with an enabled gateway key (lanGuard asks for it), a
|
|
// key held to a budget, models or accounts is told none of it, or only
|
|
// what it may use when its owner lets it be told its quotas (scopeQuotas:
|
|
// a key held to Claude's models is told Claude's accounts it may use, and
|
|
// no other provider's), and the route of its own requests alone. A
|
|
// read-only key may ask it, and is told the accounts' use without the
|
|
// model (it may not ask for a route).
|
|
//
|
|
// ?format=json answers the same as JSON, for a status line that draws its
|
|
// own (claude-hud's external usage snapshot, steven_4422): "text" the line,
|
|
// "model" the session's, and "accounts" the cards the line shows, each in
|
|
// /v1/magpie/quotas?kept=1's shape (a window that has started again since
|
|
// it was read is used 0 there too).
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// StatuslinePath is where the status line is told.
|
|
const StatuslinePath = "/v1/magpie/statusline"
|
|
|
|
func (s *Server) statusline(w http.ResponseWriter, r *http.Request) {
|
|
if !local(r) && !sharedWith(r) {
|
|
writeError(w, provider.Chat, http.StatusForbidden, "magpie's status line is told to another machine only when magpie is shared on the local network (Settings → Network and sharing → Share on local network) and the request carries its API key (Authorization: Bearer <key> or x-api-key: <key>)")
|
|
return
|
|
}
|
|
scope, ok := scopeQuotas(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
q := r.URL.Query()
|
|
session := strings.TrimSpace(q.Get("session"))
|
|
if r.Method == http.MethodPost {
|
|
// Claude Code's JSON, as the command's stdin; small, and one that
|
|
// isn't JSON only leaves the session out
|
|
var in struct {
|
|
SessionID string `json:"session_id"`
|
|
}
|
|
if json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&in) == nil && session == "" {
|
|
session = strings.TrimSpace(in.SessionID)
|
|
}
|
|
}
|
|
if len(session) > 128 {
|
|
session = session[:128] // as sessionOf keeps it
|
|
}
|
|
model := ""
|
|
who := access.Caller(r.Context())
|
|
// a read-only key is refused /v1/magpie/route (another key's routes), so
|
|
// it is told no model here either
|
|
if session != "" && !who.ReadOnly {
|
|
only := ""
|
|
if heldKey(who) {
|
|
only = who.KeyID
|
|
}
|
|
if _, route := s.trace.sessionLatest(r.Context(), session, only, 0, 0); route != nil {
|
|
sr := sessionRouteOf(*route)
|
|
model = sr.Model
|
|
if model == "" {
|
|
model = sr.Asked
|
|
}
|
|
}
|
|
}
|
|
var names []string
|
|
for _, v := range q["only"] {
|
|
for _, n := range strings.Split(v, ",") {
|
|
if n = strings.TrimSpace(n); n != "" {
|
|
names = append(names, n)
|
|
}
|
|
}
|
|
}
|
|
now := time.Now()
|
|
qs := scope.quotas(provider.KeptQuotaReport(now))
|
|
line := provider.StatuslineText(model, qs, names, now)
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
if q.Get("format") == "json" {
|
|
writeJSON(w, http.StatusOK, map[string]any{"text": line, "model": model, "accounts": append([]provider.Quota{}, provider.StatuslineCards(qs, names)...)})
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = io.WriteString(w, line+"\n")
|
|
}
|