Chicring: Settings showed "网关端口" (gateway port), but the row showed
MAGPIE_ADDR's host:port.
What the field takes, from the code: renderPort posts to
/api/settings/port, and setPort (internal/gui/port.go) accepts only a port
from 1024 to 65535 (settings.CheckPort). The gateway listens on that port,
on 127.0.0.1, or on 0.0.0.0 while shared on the LAN. When MAGPIE_ADDR is
set, it is the whole host:port and comes first. The row then shows its
value and is disabled. So the row is the gateway's address, but only its
port is typed here; it isn't a second way to write MAGPIE_ADDR.
The row is renamed in every language: Gateway address / 网关地址 / 閘道位址 /
ゲートウェイのアドレス / Adresse des Zugangspunkts. The new hint says "Only
the port is set here, 1024 to 65535; MAGPIE_ADDR=host:port sets the whole
address instead. Agents magpie connected move with it. Now {url}". With
MAGPIE_ADDR set, the hint says "MAGPIE_ADDR={addr} sets the gateway's whole
address, host:port: unset it to set the port here". The CLI help (cpu,
healthcheck, heap, quota, statusline) and setPort's comment named
"Settings' Gateway port". They now say "the port in Settings' Gateway
address".
Test: gateway-port.test.cjs pins the new name and hints in en and zh. With
MAGPIE_ADDR set, it now also checks that the field shows 127.0.0.1:3426
and that the row's name and aria-label say the address. gui-zh-tw, gui-ja,
gui-de and i18n-keys cover the other languages. Broken by hand: on the old
app.js it is red, "not ok 1 - en expected: 'Gateway address' actual:
'Gateway port'", and zh red against '网关地址'.
What was run. The whole suites ran at dcf7eeae3, which was these two commits
on origin/main 592899467. They were rebased cleanly onto 5c557c793 and
re-checked there.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./... and
GOOS=windows go vet ./...: ok, at both heads.
- GOMODCACHE=... go test -timeout 45m ./... in a sandbox HOME: 63 ok.
internal/agent, internal/gateway and internal/library hit the 45m package
timeout while other sessions' full suites ran (load ~27-40). Their stacks
were in short tests (TestDisconnectStaysThroughSync 21s,
TestSub2APIThinkingOffRefused 0s, TestSkillHow 10s), so nothing hung.
Each passed when run alone:
"ok internal/agent 1127.439s", "ok internal/gateway 1044.808s",
"ok internal/library 1029.999s".
- The whole GUI suite, node --test --test-concurrency=2
internal/gui/tests/*.test.cjs, with playwright 1.62.0:
- BROWSER=chromium: "# tests 5862 # pass 5844 # fail 18".
- BROWSER=webkit: "# tests 5854 # pass 5835 # fail 19".
Every red file was rerun alone on a freshly fetched origin/main
(a3ebed202 / 5c557c793) and is red there too:
- library-row-remove (chromium, 4 of 8 runs on main): filed #3099.
- quota-count-wrap and quota-name-translated (webkit): #2228.
- usage-agents-hide (both engines): #2276.
- usage-fold (chromium): #2163.
- plugin-card-update (webkit, 2 of 4 runs on 5c557c793): #3079.
- s3-sync (webkit): #2448.
- usage-checkin's Qoder tab (webkit): #2549.
- tray-usages and routing-rule-remove: passed alone on both trees.
None of these files touch the provider editor or the gateway address row.
- At 5c557c793 + these commits: decide-endpoint-label, gateway-port,
system-one, remote-magpie, i18n-keys, gui-zh-tw, gui-ja, gui-de and
lan-docker in both engines: "# tests 54 # pass 54 # fail 0".
go test ./internal/gui ./internal/cli: ok.
167 lines
5.3 KiB
Go
167 lines
5.3 KiB
Go
package cli
|
|
|
|
// magpie statusline: one line for Claude Code's status line (#1997,
|
|
// kensenzhao). Claude Code runs the statusLine command on every message,
|
|
// hands it the session's JSON on stdin and shows what it prints, however
|
|
// Claude Code signs in to the gateway; the anthropic-ratelimit-* headers
|
|
// (#1257) reach it only while it is signed in to claude.ai itself, and
|
|
// tell only the one account that answered. This asks the running gateway
|
|
// for what it already holds — the session's latest route and every
|
|
// account's windows as last read (/v1/magpie/quotas?kept=1) — and asks no
|
|
// vendor itself, so it never waits on one.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
const statuslineUsage = `usage: magpie statusline [<provider|account>…]
|
|
one line for Claude Code's status line: the model the session's latest turn went
|
|
to through magpie, then each subscription and plan magpie has, as last read: its
|
|
windows' use ("5h 62% 7d 41%"), * on the one that answered last. Accounts that
|
|
answered through magpie in the last 30 days are shown, all of them when none has;
|
|
a provider or an account's email, login or name keeps only those.
|
|
Claude Code runs it on every message with the session's JSON on stdin; put it in
|
|
~/.claude/settings.json yourself (magpie doesn't write it, a status line such as
|
|
claude-hud may hold it):
|
|
"statusLine": {"type": "command", "command": "magpie statusline"}
|
|
It asks the gateway on this computer (the port in Settings' Gateway address, or MAGPIE_ADDR), never
|
|
a vendor, and prints nothing when the gateway isn't running.`
|
|
|
|
// statuslineWait bounds each ask of the gateway: Claude Code shows the line
|
|
// only once the command ends.
|
|
const statuslineWait = time.Second
|
|
|
|
// statuslineCmd: magpie statusline [<provider|account>…]
|
|
func statuslineCmd(args []string) error {
|
|
fmt.Println(statusline(os.Stdin, args[1:], "http://"+settings.GatewayAddr(), time.Now()))
|
|
return nil
|
|
}
|
|
|
|
// statusline is the line: "" when the gateway can't be asked.
|
|
func statusline(stdin io.Reader, only []string, base string, now time.Time) string {
|
|
var in struct {
|
|
SessionID string `json:"session_id"`
|
|
}
|
|
if stdin != nil {
|
|
// Claude Code's JSON is small; a terminal's stdin with nothing
|
|
// piped in isn't waited on (read only when it is a pipe or file)
|
|
if f, ok := stdin.(*os.File); !ok || stdinPiped(f) {
|
|
_ = json.NewDecoder(io.LimitReader(stdin, 1<<20)).Decode(&in)
|
|
}
|
|
}
|
|
client := &http.Client{Timeout: statuslineWait}
|
|
var quotas struct {
|
|
Data []provider.Quota `json:"data"`
|
|
}
|
|
if !getJSON(client, base+"/v1/magpie/quotas?kept=1", "as) {
|
|
return ""
|
|
}
|
|
var parts []string
|
|
if in.SessionID != "" {
|
|
var route struct {
|
|
Route *struct {
|
|
Asked string `json:"asked"`
|
|
Model string `json:"model"`
|
|
} `json:"route"`
|
|
}
|
|
if getJSON(client, base+"/v1/magpie/route?session="+url.QueryEscape(in.SessionID), &route) && route.Route != nil {
|
|
if m := route.Route.Model; m != "" {
|
|
parts = append(parts, "→ "+m)
|
|
} else if route.Route.Asked != "" {
|
|
parts = append(parts, "→ "+route.Route.Asked)
|
|
}
|
|
}
|
|
}
|
|
for _, q := range statuslineCards(quotas.Data, only) {
|
|
parts = append(parts, statuslineCard(q, now))
|
|
}
|
|
return strings.Join(parts, " | ")
|
|
}
|
|
|
|
// stdinPiped: f is a pipe or a file, not a terminal waiting for typing.
|
|
func stdinPiped(f *os.File) bool {
|
|
st, err := f.Stat()
|
|
return err == nil && st.Mode()&os.ModeCharDevice == 0
|
|
}
|
|
|
|
func getJSON(c *http.Client, u string, v any) bool {
|
|
res, err := c.Get(u)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer res.Body.Close()
|
|
return res.StatusCode == http.StatusOK && json.NewDecoder(res.Body).Decode(v) == nil
|
|
}
|
|
|
|
// statuslineCards are the subscriptions and plans with windows to show:
|
|
// those named, else those that answered through magpie lately, else all.
|
|
func statuslineCards(qs []provider.Quota, only []string) []provider.Quota {
|
|
var withWindows, served []provider.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 provider.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 provider.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.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, " ")
|
|
}
|