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.
141 lines
4.3 KiB
Go
141 lines
4.3 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, " ")
|
|
}
|
|
|
|
// CodexStatuslineCards are the accounts a Codex thread's footer shows
|
|
// (lyj1115 on X: the quota of the account in use, or the relay's balance,
|
|
// under Codex's chat box): those of the provider the thread's latest turn
|
|
// went to (model is provider/model), the one that answered last of them,
|
|
// a key's balance as well as a subscription's windows; with no such
|
|
// account, the one that answered last of all.
|
|
func CodexStatuslineCards(model string, qs []Quota) []Quota {
|
|
shown := func(q Quota) bool { return len(q.Windows) > 0 || q.Kind == "balance" && q.Balance != "" }
|
|
latest := func(keep func(Quota) bool) []Quota {
|
|
var out []Quota
|
|
for _, q := range qs {
|
|
if !shown(q) || !keep(q) {
|
|
continue
|
|
}
|
|
switch {
|
|
case len(out) == 0:
|
|
out = []Quota{q}
|
|
case q.LastServedAt != nil && (out[0].LastServedAt == nil || q.LastServedAt.After(*out[0].LastServedAt)):
|
|
out[0] = q
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
if p, _, ok := strings.Cut(model, "/"); ok && p != "" {
|
|
if out := latest(func(q Quota) bool { return q.Provider == p }); len(out) > 0 {
|
|
return out
|
|
}
|
|
}
|
|
return latest(func(q Quota) bool { return q.Last })
|
|
}
|
|
|
|
// CodexStatuslineCard is one account as Codex's footer shows it: its
|
|
// windows as StatuslineCard has them, or a key's balance.
|
|
func CodexStatuslineCard(q Quota, now time.Time) string {
|
|
if len(q.Windows) == 0 && q.Balance != "" {
|
|
name := q.Provider
|
|
if q.Alias != "" {
|
|
name += " " + q.Alias
|
|
}
|
|
return name + " " + q.Balance
|
|
}
|
|
q.Last = false // one account is shown: no star to tell it apart
|
|
return StatuslineCard(q, now)
|
|
}
|