steven_4422 (Discord) runs magpie in Docker and Claude Code on the host.
The status line square writes the host's settings.json, which the
container can't reach, with a command naming a binary inside the container.
GET or POST /v1/magpie/statusline answers the line `magpie statusline`
prints, as text/plain, so the host needs only curl:
curl -fsS -m 1 -H 'Authorization: Bearer <key>' --data-binary @- http://<host>:3425/v1/magpie/statusline || true
Claude Code's stdin JSON (session_id) adds the model, as /v1/magpie/route
does; ?session= and ?only= work too. The renderer moved to
provider.StatuslineText, shared by the CLI and the endpoint. A caller that
isn't this computer needs a gateway key (lanGuard, unchanged; the handler
also refuses a non-local caller without one). A read-only key is allowed
(GET and POST in readOnlyRoutes) and gets the windows without the model; a
key held to budget, models or accounts sees only its own (scopeQuotas).
corsGuard and origin checks are untouched.
magpie web shows the command in Settings -> About (en, zh, zh-TW, ja, de).
Docs: reference.md, agent-wiring.md, gateway-routing.md, docker pages en/zh/ja.
Run at this head (rebased on d8b3749ef), on mote:
- go build, gofmt -l, GOOS=windows go vet and GOOS=linux go vet -tags nogui
on gateway, cli, provider: clean.
- go test: internal/gateway ok (a first full run failed once, 888s, name not
kept; the rerun had no failure), TestStatusline*, TestReadOnlyKeyEveryRoute,
TestStatuslineEndpointIsTheCLIsLine ok. internal/agent ok earlier, at the
pre-rebase base.
- TestCoreScriptBudget ok after shortening the new strings (no raise).
- GUI: statusline-remote (22: 2 engines x 5 languages x 1100/400, plus the
app without the row), public-url-default, i18n-keys, claude-statusline: all pass.
Broken by hand: model made wrong -> TestStatuslineEndpoint red; held-key
scope removed -> red; separator changed in the shared renderer ->
TestStatuslineEndpoint and cli TestStatuslineTellsRouteAndEveryAccount red;
the row's web check inverted -> the GUI test 0 of 22. Removing the
handler's own key check stays green because lanGuard refuses first (the
401 asserts hold through it).
Reds not from this change: cli TestEveryRouteHasACommandOrIsNamed
(GET /api/context/reply, from main), gui TestMainWindowCorners (red on
origin/main on mote), provider TestMoveToPlugin and TestMoveCutShort (red in
the full run, green alone).
Not tried: Docker or any container (not on mote or WSL); the real binary
from another computer over the LAN with a real Claude Code; the whole GUI
suite (running, 1182 files, finishes after the push); Windows and Linux
test runs.
96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package provider
|
|
|
|
// The line Claude Code's status line shows (#1997, kensenzhao): the model a
|
|
// session's latest turn went to, then each subscription and plan with the
|
|
// windows magpie last read. `magpie statusline` and the gateway's
|
|
// GET /v1/magpie/statusline (for a Claude Code on another computer than
|
|
// the gateway, steven_4422 running magpie in Docker) both print it from
|
|
// here, so the two never differ.
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// StatuslineText is the whole line: "→ model" when the session has one,
|
|
// then the cards of qs that only (or the lately served) keeps.
|
|
func StatuslineText(model string, qs []Quota, only []string, now time.Time) string {
|
|
var parts []string
|
|
if model != "" {
|
|
parts = append(parts, "→ "+model)
|
|
}
|
|
for _, q := range StatuslineCards(qs, only) {
|
|
parts = append(parts, StatuslineCard(q, now))
|
|
}
|
|
return strings.Join(parts, " | ")
|
|
}
|
|
|
|
// StatuslineCards are the subscriptions and plans with windows to show:
|
|
// those named, else those that answered through magpie lately, else all.
|
|
func StatuslineCards(qs []Quota, only []string) []Quota {
|
|
var withWindows, served []Quota
|
|
for _, q := range qs {
|
|
if q.Kind == "balance" || len(q.Windows) == 0 {
|
|
continue
|
|
}
|
|
if len(only) > 0 && !StatuslineMatches(q, only) {
|
|
continue
|
|
}
|
|
withWindows = append(withWindows, q)
|
|
if q.LastServedAt != nil {
|
|
served = append(served, q)
|
|
}
|
|
}
|
|
if len(only) == 0 && len(served) > 0 {
|
|
return served
|
|
}
|
|
return withWindows
|
|
}
|
|
|
|
func StatuslineMatches(q Quota, only []string) bool {
|
|
for _, o := range only {
|
|
for _, v := range []string{q.Provider, q.Name, q.User, q.Alias, q.Provider + "/" + q.User} {
|
|
if v != "" && strings.EqualFold(v, o) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// StatuslineCard is one account: "claude a@x.com* 5h 62% 7d 41%".
|
|
func StatuslineCard(q Quota, now time.Time) string {
|
|
name := q.Provider
|
|
switch {
|
|
case q.Alias != "":
|
|
name += " " + q.Alias
|
|
case q.User != "":
|
|
name += " " + q.User
|
|
}
|
|
if q.Last {
|
|
name += "*"
|
|
}
|
|
short := strings.NewReplacer(" hours", "h", " hour", "h", " days", "d", " day", "d", " · ", " ")
|
|
cells := []string{name}
|
|
for _, w := range q.Windows {
|
|
n := short.Replace(w.Name)
|
|
switch {
|
|
case w.Unlimited:
|
|
cells = append(cells, n+" ∞")
|
|
case w.Balance != nil:
|
|
// a wallet's figure in its unit, never a share (#3950)
|
|
cells = append(cells, n+" "+BalanceText(*w.Balance, w.Unit))
|
|
case w.Text:
|
|
// a row of text alone (a plugin's balance) has no share
|
|
cells = append(cells, n+" "+w.Display)
|
|
case w.ResetsAt != nil && !w.ResetsAt.After(now):
|
|
// its window has started again since it was read
|
|
cells = append(cells, n+" 0%")
|
|
default:
|
|
cells = append(cells, fmt.Sprintf("%s %.0f%%", n, w.Used))
|
|
}
|
|
}
|
|
return strings.Join(cells, " ")
|
|
}
|