Files
magpie/internal/cli/statusline_cli.go
T
yetone 2ab2f69132 agent: Codex's status line shows the account's quota or the relay's balance (opt-in)
lyj1115 on X asked for the current account's quota, or the relay key's
balance, under Codex's chat box, off by default. Codex's TUI draws its
footer from a status line provider from 0.171.0-alpha.12 on: a stdio
MCP server whose tool advertises a statusline entrypoint, picked in
[tui] status_line_provider behind [features] tui_extensions_statusline.
Codex reads it about once a second with the thread's id. Nothing is put
into the prompt or the model's reply.

- `magpie statusline --codex` is that server. Each read asks the local
  gateway for the thread's latest route and the kept quotas (no vendor
  read). It answers `→ provider/model` and that provider's account that
  answered last, with its windows or a key's balance. A gateway that is
  down gives a null tree, never an error, because an error stops Codex's
  reads. The tool's name and words name neither magpie nor a vendor,
  since the tool is listed to the model.
- A status line square on Codex's row (`magpie codex statusline on`)
  writes the flag, [mcp_servers.quota_line] (this magpie by full path,
  args statusline --codex, env_vars = ["MAGPIE_ADDR"]) and the
  provider. Every write is read back, and the file is restored on a
  mismatch. Off and Disconnect take out only magpie's keys, and the flag
  only when the stash says magpie wrote it.
- Nothing of the user's is written over. A provider of their own, a
  quota_line of theirs, the flag turned off or an unreadable file keeps
  the square away and refuses on.
- The switch is offered only when a Codex CLI here is 0.171.0-alpha.12
  or newer (0.170.0 reads the keys and draws nothing), and only on this
  computer, not in a WSL distro.
- In the GUI, the status line square's title takes the note of its
  field's on option, which Claude Code's field already sent with the
  same words TOGGLES kept, so Codex's says its own words in en, zh,
  zh-TW, ja and de. TOGGLES' copy of Claude's note went, and app.js
  stays under its budget (30 bytes of room) without a raise.

The Codex desktop app has no such footer. cockpit-tools reaches it
through a debug port, and magpie doesn't.

Run (WSL magpie-desktop, since mote's load was over 18):
- go vet -tags nogui ./...; GOOS=windows go vet of agent, cli, provider
  and gui; gofmt -l clean.
- go test -tags nogui of ./internal/cli and ./internal/provider: ok.
- ./internal/agent -run "CodexStatus|Statusline|StatusLine|TestCodex|
  TestWSLCodex": ok.
- ./internal/gui -run "CoreScript|Budget|I18n|Keys" and node --test of
  codex-statusline, claude-statusline and i18n-keys (both engines, every
  language, window and panel), with vet (and GOOS=linux and windows
  vet) of agent, cli, provider and gui, on mote at the rebased head
  (on 1bd1a08a3): all ok, 64 of 64 GUI cases.

Broken by hand:
- free() reduced to `readable`: TestCodexStatusLineLeavesTheUsers is red
  on their provider, their server and flag off.
- The field not appended: TestCodexStatusLineSwitch is red ("no
  statusline field").
- The first build checked `at.base == nil` after codexIn had set base.
  Every agent test went red, which is how that bug was found.

Real Codex, in a sandbox HOME on WSL with a fake gateway:
- 0.171.0-alpha.13, with `magpie codex statusline on` writing config.toml,
  draws `→ relay/glm-5  relay $4.20` under the prompt, and the server is
  read about once a second.
- Without env_vars, Codex starts the server without MAGPIE_ADDR: nothing
  was asked and nothing was drawn. That is why env_vars is written.
- On a second time leaves the file byte for byte. Off brings it back to
  what it was, apart from Codex's own NUX counter.
- 0.170.0 lists quota_line from the same file and runs fine.

Not tried: macOS and Windows Codex; a real gateway with real accounts
(the server was fed through MAGPIE_ADDR); whether an open Codex session
picks the footer up without a restart.

Full suite: run after push.
2026-11-19 00:44:40 +08:00

113 lines
4.4 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 (
"cmp"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
const statuslineUsage = `usage: magpie statusline [<provider|account>…]
magpie statusline --codex
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. Turn it on
with the status line square on Claude Code's row in the Agents page (or
magpie claude statusline on), which writes ~/.claude/settings.json's statusLine
unless a status line of your own (claude-hud, a script) is there; or add it yourself:
"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.
--codex is Codex's footer (Codex 0.171 on): an MCP server Codex starts, showing the
model the thread's latest turn went to and that account's windows or balance. Turn it
on with the status line square on Codex's row in the Agents page (or
magpie codex statusline on), which writes ~/.codex/config.toml's [mcp_servers.quota_line],
[features] tui_extensions_statusline and [tui] status_line_provider.`
// 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 {
if len(args) > 1 && strings.HasPrefix(args[1], "-") {
if args[1] == "--codex" && len(args) == 2 {
// Codex's footer: an MCP server Codex starts (statusline_codex.go)
return newCodexStatus(gateway.ClientURL()).serve(os.Stdin, os.Stdout)
}
return fmt.Errorf("magpie statusline takes providers or accounts, or --codex alone, not %q\n%s", args[1], statuslineUsage)
}
fmt.Println(statusline(os.Stdin, args[1:], gateway.ClientURL(), 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", &quotas) {
return ""
}
model := ""
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 {
model = cmp.Or(route.Route.Model, route.Route.Asked)
}
}
return provider.StatuslineText(model, quotas.Data, only, now)
}
// 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
}