Files
magpie/internal/cli/main.go
T
yetone f2c6831a31 gateway: a read-only gateway key reads quotas and usage and uses nothing (Steven on Discord)
Steven's ESP32 desk display polls magpie (Docker, on his LAN) every 15s
on GET /v1/magpie/quotas and /v1/magpie/usage and never sends a model
request. Only a key held to nothing was told the usage summary, so the
key kept in the device's flash had to be one that could spend every
account.

A gateway key can now be read-only (access.Key.ReadOnly):
- `magpie gateway-key add <name> --read-only`, `magpie gateway-key access
  <id> [read-only|full]` (`gateway-key --help` lists it; `list` has an
  ACCESS column), and "Make read-only" / "Give full access" in the key's
  menu in Gateway -> Gateway keys, with a "Read only" tag on its row.
- readOnlyGuard (internal/gateway/readonly.go), after callerGuard and
  before withCaller/keyLimited, judges the pattern ServeMux.Handler routes
  the request to. A read-only key is answered GET /, /v1, the quotas,
  their history and cards, the usage summary and its requests, its limit
  and the concurrency. Every other route is refused with 403
  gateway_key_read_only: every model route, count_tokens, the Responses
  WebSocket's upgrade, every MCP server, Codex's and Gemini's backends,
  /v1/models and the Ollama routes, the catch-all, the trace and route,
  debug profiles, and every write (usage ingest, quotas refresh). It is
  default deny, so a route added later is refused until it is classified.
- The key is held to nothing else, so heldKey leaves it alone: it reads the
  usage summary and every account's quotas, as a key held to nothing does.
  None of those routes holds a key's secret.
- The sharing key can't be made read-only, since magpie gives it to its own
  agents (LANSecret). Update refuses it, and the GUI item is off with why.

Older versions: a magpie from before this commit doesn't know `readOnly`
and treats such a key as a full one, so downgrading magpie widens the key.

Tests:
- TestReadOnlyKeyEveryRoute (internal/gateway/readonly_test.go) takes the
  patterns from the router itself (routes() records each HandleFunc). It
  fails when a pattern is in neither readOnlyRoutes nor its refused list.
  It sends every refused pattern, by each method it takes, from
  192.168.1.9 and from loopback, plus a WebSocket upgrade and an unclean
  chat path, and expects 403 gateway_key_read_only with the upstream never
  reached. It checks that every allowed route answers, that no body holds
  any key's secret or the upstream key, that the usage summary names the
  other key's use, and that a full key and a key made full again still
  reach the model.
- TestReadOnlyKey (access) covers add-key and access-key on and off,
  Authenticate, List, and the sharing key refused.
- TestGatewayKeyCLIReadOnly covers --read-only before and after the name
  and beside --key, access, the list column, bad values, and --help.
- gateway-key-readonly.test.cjs runs Chromium and WebKit, 1100 and 420px,
  in en/zh/zh-TW/ja/de. It covers the tag, both menu items and what they
  post, the status line, the sharing key's item off with its why, no
  scroll on click, no sideways scroll, no select, and no left stripe.
  gateway-card.test.cjs now expects the menu's 4th item.
- TestClientPathCoversHandler takes the patterns from routes() instead of
  parsing Handler's source, which moved into routes().

Broken by hand:
- readOnlyGuard passing everything: TestReadOnlyKeyEveryRoute red (103
  lines, /v1/models 200 onward).
- A new `POST /v1/new-thing` route in routes(): red, "in neither
  readOnlyRoutes nor readOnlyRefused".
- The GUI item not off for the sharing key: gateway-key-readonly red 10 of
  10 in Chromium.

Run at the pushed head (on origin/main 0911a65e7), on mote: TestReadOnly*,
TestHeldKey*, TestClientPathCoversHandler, TestGatewayKey*, TestCaller*
in access, cli and gateway ok; TestCoreScriptBudget ok; on Linux
(wsl-run, both engines) gateway-key-readonly, gateway-card, i18n-keys,
gateway-caller-keys and gateway-key-quotas 68/68.

Full Go suite on mote (macOS) at the same change on origin/main
dca3e2d71 (94630237d), -timeout 45m: 78 packages ok; internal/gateway
red only on the four TestAntigravityTurned* and
TestCodexSpentGrokFailingIsTold (red the same at origin/main, #4362);
internal/gui red only on ten AppKit window tests that hit their 30s
bar while mote's launchd throttled every app launch (#4125).

Run on mote (macOS) on origin/main dca3e2d71:
- go test ./internal/access ./internal/cli ./internal/gateway: access ok;
  cli red only on TestLibraryMCPHub's TempDir cleanup (#4140); gateway
  red only on the four TestAntigravityTurned* and
  TestCodexSpentGrokFailingIsTold, red the same at origin/main (#4362).
  TestReadOnly*, TestHeldKey*, TestClientPathCoversHandler,
  TestGatewayKeyCLIReadOnly pass.
- TestCoreScriptBudget ok (no budget raised). gofmt clean; go vet
  darwin (access, cli, gateway), GOOS=windows ./... and GOOS=linux
  -tags nogui ./... clean.
- GUI, earlier at 807048470 on mote: gateway-key-readonly,
  gateway-key-quotas, i18n-keys, gateway-card, api-key-theme,
  gateway-connect-keys and gateway-caller-keys 34/34 in both engines.
  At this tree mote's WebKit hung at launch for every test (#4125), so
  the 43 gateway-*/key-*/api-key-*/caller-*/menu-*/i18n-* files ran on
  Linux (wsl-run, both engines): 501 of 514. The reds in api-key-theme,
  gateway-key-limit, gateway-key-models, key-replace and menu-input-scroll
  are red the same at origin/main e8d42c02d (#4120, #3523, #4081, #4126,
  #3402); gateway-keys-empty's WebKit wait was red 1 in ~13 runs here and
  0 in 4 at main, filed as #4383 (this change doesn't touch its path).
- Full Go suite on Linux (wsl-run, at 807048470 with this change): reds
  only where Linux can't build the GUI (no gtk4/webkitgtk) and in
  internal/agent's install tests (#4033) and the gateway reds above,
  each red the same at origin/main dca3e2d71; plus
  TestClientPathCoversHandler, which parsed Handler's source and is now
  read from routes() (fixed here).

Not tried: a real ESP32 or Docker magpie; a remote client against a
running gateway (only httptest through lanGuard/Handler); the Windows
test runs (vet only); the whole GUI suite (mote's WebKit hung, #4125).
2026-10-30 14:33:01 +08:00

918 lines
38 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package cli is magpie's command line: every command, and the app, tray
// and TUI it opens (the root's main.go only calls Main).
package cli
import (
"context"
"errors"
"fmt"
"os"
"slices"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/claudebridge"
"github.com/yetone/magpie/internal/codexrelay"
"github.com/yetone/magpie/internal/davsync"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/imagemcp"
"github.com/yetone/magpie/internal/netproxy"
"github.com/yetone/magpie/internal/notices"
"github.com/yetone/magpie/internal/pluginrt"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/profile"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/tui"
"github.com/yetone/magpie/internal/update"
)
// version is the root main package's, set at build time
// (-ldflags "-X main.version=…") and handed over by Main.
var version = "dev"
const usage = `magpie — one place to pick every agent's model
magpie open the app: a window plus a menu bar icon
magpie tray start in the menu bar only
magpie panel open the menu bar icon's quick panel, or close it
magpie autostart [on|off] open magpie (in the menu bar) when you log in, or say whether it does
magpie tui the same thing, in the terminal (serves the gateway while open when no magpie does)
magpie web [--addr host:port] [--lan] [--no-open] [--gateway] [--tls-cert <file> --tls-key <file>]
the app's window in a browser, with the gateway (no desktop needed: WSL, a server over SSH)
a new key each run; MAGPIE_WEB_KEY (16+ letters, digits, - . _ ~) keeps one, signed in for 400 days
behind a reverse proxy, MAGPIE_WEB_URL=https://<the page there> prints the link through it
--gateway: gateway mode, no Agents, Sessions or Library (on by itself with no agents here; Settings › General turns it off)
--tls-cert/--tls-key: the page and the gateway over https:// to other computers
magpie ls list detected agents and their settings
magpie <agent> show one agent
magpie <agent> <model> set an agent's model e.g. magpie claude deepseek/deepseek-chat
magpie <agent> <field> show one field e.g. magpie codex effort
magpie <agent> <field> <value> set another field e.g. magpie codex effort high
magpie <agent> default take magpie out: the agent back on what it had before
magpie <agent> <field> default that field back to the agent's own default
magpie <command> --help what a command or an agent takes; -h or --help anywhere does nothing else
magpie save <name> snapshot every agent's settings as a profile
magpie use <name> apply a profile
magpie profiles list profiles
magpie rm <name> delete a profile
magpie backup [--no-keys] [--no-library] [file] providers, keys, settings, profiles, agent models and the library in one file, sealed with a passphrase
magpie restore [--no-agents] [--no-library] <file> put a backup in on this machine
magpie uninstall [--yes] disconnect every agent, putting back what each had, then remove magpie's service and files
magpie webdav [on <address>|set k=v…|now|off] the same, kept the same on every computer through a WebDAV folder (magpie webdav help)
magpie s3 [on s3://<bucket>[/<prefix>]|set k=v…|now|off] the same through an S3-compatible bucket: AWS, R2, B2, MinIO… (magpie s3 help)
magpie github [on github://<owner>/<repo>[/<folder>]|set k=v…|now|off] the same in an encrypted GitHub repository (magpie github help)
magpie library [sync|instructions|mcp|skill] the instructions, MCP servers and skills written into every agent (magpie library help)
magpie providers list your providers: host, key, models, who uses them
magpie presets the vendors magpie knows: add one with just a key
magpie provider add <preset> <key> e.g. magpie provider add deepseek sk-…
magpie provider add <name> k=v… a custom vendor (magpie provider for the fields)
magpie provider key|models|test|rm <id>
magpie provider fallback <id> <provider/model>… use these when it's out of quota or down
magpie import [-y] <link> add the provider a magpie://import?… link describes
magpie models [<agent>] every model agents can pick, as provider/model; an agent's, and why others aren't
magpie model name <provider/model> <name>|--reset the name a model goes by, everywhere
magpie model efforts <provider/model> <l>,<l>|--reset the reasoning levels a model offers (magpie model help)
magpie visible [<agent> <family|provider|group>,… | all | --only-picked | --show-new]
which models an agent is shown: families (magpie provider/group set <id> family=…),
or only the models ticked for it, a new one off until it is ticked
magpie search [add <api> <key>|rm <api>] Tavily, Brave, Exa, Firecrawl, You.com, DeepSeek, Doubao Search (Volcengine) or SearXNG for web search when no provider can search
magpie groups routing groups: several models agents pick as one, group/<id>
magpie group add <name> models=<m1>,<m2> [routing=smart|order|rotate|usage|pace|weight|free|fast] [stays=auto|session|turn|off]
magpie group <id> | set <id> k=v… | rm <id> show, change or remove one (magpie group help for more)
magpie accounts [agent] [--json] every subscription magpie knows, with each one's allowance used and when it resets
magpie accounts add <agent> sign in to one more Claude, ChatGPT or Google (Gemini CLI, Antigravity) subscription
magpie accounts add copilot [--host <name>.ghe.com] one more Copilot account, on github.com or an enterprise's GHE.com
magpie accounts discovery [on|off] read agents' sign-ins (default on; MAGPIE_NO_ACCOUNTS=1 disables too)
magpie accounts import <codex|claude|antigravity|factory> <file>... [--yes] bring in accounts from other tools' files (Codex's auth.json, Cockpit Tools, CLIProxyAPI, Sub2API); the tool a ChatGPT or Claude file came from is signed out of it
magpie accounts switch <agent> <email> sign the agent in to another of them
magpie accounts refresh renew the saved ChatGPT sign-ins now (the gateway does it daily)
magpie accounts checkin WorkBuddy's daily check-in (签到) for each WorkBuddy account, now (Settings can do it daily)
magpie accounts project <gemini|antigravity> <email> <project> the Google Cloud project a Google account's requests go to
magpie accounts alias <agent> <email> [<name>|--clear] a name of your own for an account, shown on its card, in the menu bar and in magpie quota
magpie accounts switch-only codex [on|off] only switch Codex's own sign-in to an account with room; its requests go straight to OpenAI, never through magpie
magpie accounts switch-only claude [on|off] only switch Claude Code's own sign-in to an account with room; magpie serves no requests on the Claude accounts
magpie claude-code [install [--yes]|remove] the Claude Code a Claude subscription runs; install downloads Anthropic's own build, checked against its manifest, for a server or container without one
magpie plugin [add <package>|rm|update|on|off|login <provider>|logout <provider>]
OpenCode provider plugins and pi packages: subscriptions signed in to, and served, through a plugin
magpie plugin move|migrate <subscription> run a built-in subscription's accounts on its community plugin
magpie serve [--web[=host:port]] [--tls-cert <file> --tls-key <file>]
run the gateway alone (the app runs it too); --web serves magpie web's page too, from the same process (127.0.0.1:3430 unless told);
with a certificate and key (PEM; MAGPIE_TLS_CERT, MAGPIE_TLS_KEY) other computers reach both over https://
magpie daemon [install|uninstall|start|stop|restart|status]
magpie serve as a service of yours, from boot or login and started again when it stops (systemd --user on Linux, launchd on the Mac)
magpie daemon install [--web] [--lan] [--addr host:port] [--no-web] [--tls-cert <file> --tls-key <file>|--no-tls]
--web: the service serves magpie web's page too, with a key kept across restarts; installed again, it keeps what it served
magpie daemon web [on|off] [--lan] [--addr host:port]
the service's page: its link, or turn it on or off
magpie healthcheck exit 0 when the gateway answers (a container's HEALTHCHECK)
magpie heap [<file>] a Go heap profile of the running magpie, with its memory numbers, for a report of it using too much memory
magpie cpu [<file>] [--seconds N] a Go CPU profile of the running magpie over 30s (5..120) while you use it, for a report of it using too much CPU
magpie goroutines [<file>] every goroutine of the running magpie, its stack and how long it waited, for a report of it stuck at something
magpie gateway-key list|add <name> [--read-only]|rotate <id>|remove <id> manage the keys clients use to call a shared gateway
magpie gateway-key limit <id> [list|off|reset [n]|remove <n>|[add] day|week|month|<N>d|<N>h --tokens N --cost USD --cache-reads] a key's own limits, and what it used
magpie gateway-key models <id> [all|<provider>/<model>|<provider>/* ...] the models a key may use, every one unless it names some
magpie gateway-key quotas <id> [on|off] whether a key held to a budget, models or accounts is told the quotas of what it may use
magpie gateway-key access <id> [read-only|full] read-only: the key reads quotas and usage (GET /, /v1/magpie/quotas, /v1/magpie/usage) and uses no model, MCP server or write
magpie mcp image the image and video generation MCP server an agent is given from the library (stdio)
magpie tools [<name>] the tools magpie's MCP servers give agents (images, vision, web search, plugins'), with their parameters
magpie tool <name> [--<param> <value>…] [<words>] [-o <file|folder>] [--json] call one through the running magpie; an image or video it makes is saved
magpie usage [today|7d|30d|all] tokens and cost per agent, model and subscription account (30d)
magpie usage --csv [--account <name>] [today|7d|30d|all] every request as CSV (or one account's): the model asked for, sent and served, tokens, cost, time, status, account
magpie usage [--csv [--account <name>]] --last <dur>|--days <n> the same for a window ending now (24h, 90m, 1.5 days), not from a midnight
magpie sessions [--model <m>] [--folder <f>] [--json] the latest Claude Code, Codex, OpenCode and Pi sessions, with what each cost
magpie sessions --days N|today|all [--model <m>] [--folder <f>] [--json]
what every session spent, day by day, with the top models and folders (7 days)
magpie sessions read [on|off all|<agent>…] which agents' session history magpie may read (none until allowed)
magpie quota [<provider>] [--json] what is left of every subscription, plan and key balance
magpie quota wait <provider|account> [--timeout <d>] [--quiet]
block until that subscription (any of its accounts) or account has allowance again
magpie quota history [<provider|account>] [--days N] [--json]
each window's readings over time, kept 45 days
magpie statusline [<provider|account>] one line for Claude Code's status line: the session's model and each account's windows
magpie sync refresh the model catalog and vendor model lists
magpie agents list every supported agent
magpie update [check] [--proxy <url>] [--mirror <prefix>]
install the newest release (check: only say if there is one); --proxy: an
http(s):// or socks5:// proxy for it; --mirror: a GitHub download mirror put
before the github.com URL (none unless given; still checked against usemagpie.ai's SHA-256)
magpie update mirror [<prefix>|off] the mirror every update, the app's own too, is downloaded through
magpie update channel [stable|every] which versions updates offer: stable (one a week, an urgent one at
once) or every version as it comes out (the default)
magpie update auto [on|off] [download|notify] [30m|1h|6h|24h]
whether the app looks for updates by itself, how often (6h), and whether
it downloads one at once or only says it is out (notify: for metered networks)
magpie licenses the third-party notices of what magpie is built with
agents: claude (cc), codex, gemini, opencode (oc), mimocode, deveco, pi, goose, cursor, zed, copilot, crush, aside
`
var (
bold = lipgloss.NewStyle().Bold(true)
muted = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#8B8F98", Dark: "#7C8290"})
faint = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#C4C7CE", Dark: "#4A4F5A"})
green = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#0F9D58", Dark: "#7EE2A8"})
)
// Main runs magpie with os.Args, at the version the build was stamped with.
func Main(v string) {
version = v
// before anything reads or writes a file: no home, or a relative one,
// would put the agents' configs and magpie's keys under the working
// folder. What needs no file still answers (magpie version in a
// container or a script without HOME): run makes magpie's folders first.
ignored, err := appdir.CheckEnv()
if err != nil {
if len(os.Args) > 1 {
switch os.Args[1] {
case "-v", "--version", "version":
fmt.Println("magpie", version)
return
case "-h", "--help", "help":
fmt.Print(usage)
return
case "licenses":
fmt.Print(notices.Text)
return
}
}
fmt.Fprintln(os.Stderr, "magpie:", err)
os.Exit(1)
}
slices.Sort(ignored)
for _, v := range ignored {
fmt.Fprintf(os.Stderr, "magpie: ignoring %s: not an absolute path (the programs magpie starts still get it)\n", v)
}
if provider.TookOpenedURL(os.Args[1:]) {
// Claude Code, signing in for magpie, handed over the page to open
return
}
endProbesOnSignal()
gateway.Version = version
netproxy.Install()
update.GUI = hasGUI
err = run(os.Args[1:])
proc.EndProbes() // a CLI still being asked something isn't left to init
sessions.Saved() // the session index kept, for the next run
if err != nil {
// a command with exit codes of its own (quota wait) says which
code := 1
var e exitError
if errors.As(err, &e) {
code = e.code
}
if msg := err.Error(); msg != "" {
fmt.Fprintln(os.Stderr, "magpie:", msg)
}
os.Exit(code)
}
}
// runTUI runs the TUI, which quits on Ctrl+C and SIGTERM itself once it
// has started; it asks CLIs first, and a signal then ends those.
func runTUI() error {
return tuiRun(ownSignals)
}
// tuiRun is tui.Run; a var so tests can stand in for it.
var tuiRun = tui.Run
// syncAgentsOnChange sets what a catalog change (a provider or group
// saved, a vendor's list fetched anew) does: the model lists agents keep
// in files of their own are brought up to date behind the save, which
// answers without waiting for every agent's files to be written (on
// Windows, half a second for each one another program holds open).
func syncAgentsOnChange() { catalog.Changed = agent.SyncCatalogSoon }
// startUp brings the files magpie and its agents keep up to what this
// magpie reads, before a command that may read them; a var so a test can
// tell it ran.
var startUp = func() {
// started by an update, the magpie it replaces goes first: the moves
// below write the files it may still be writing
update.AwaitPredecessor()
makeDirs()
settings.Migrate()
// every built-in plugin starts on (plugins.yml), on a new install too
if _, err := pluginrt.Settle(); err != nil {
fmt.Fprintf(os.Stderr, "magpie: plugins.yml: %v\n", err)
}
if _, err := pluginrt.Refresh(); err != nil {
fmt.Fprintf(os.Stderr, "magpie: plugins.yml: %v\n", err)
}
// a vendor's keys saved a provider each ("NVIDIA #001"…) are one
// provider with those keys, before anything lists them
if _, err := provider.FoldCopies(); err != nil {
fmt.Fprintf(os.Stderr, "magpie: providers.json: %v\n", err)
}
// the providers and settings read once for every agent's fields, which
// the moves below look at (a write among them reads them again)
release := provider.Hold()
agent.RenameLegacy()
agent.MoveCursorEfforts()
agent.MoveAntigravityEfforts()
agent.MoveOffAccountIDs()
release()
}
func run(args []string) error {
// a command that saved a provider or a group ends with the agents'
// files written, which its save no longer waits for; and a test's run
// leaves catalog.Changed as it found it
was := catalog.Changed
defer func() {
agent.SyncCatalogSettled()
catalog.Changed = was
}()
// what a shell's agy runs (agy_cli.go): every argument after run is
// agy's, --help too, and nothing is moved or started first
if ok, err := agyCmd(args); ok {
return err
}
// before anything is read, moved or started: --help on any command
// prints its usage and does nothing else
if helpAsked(args) {
fmt.Println(strings.TrimRight(helpFor(args), "\n"))
return nil
}
if len(args) > 0 && args[0] == "healthcheck" {
return healthcheck() // every few seconds in a container: nothing else
}
if len(args) > 0 && args[0] == "heap" {
// the running magpie's heap profile: asks it, and moves nothing here
return heapCmd(args[1:])
}
if len(args) > 0 && args[0] == "cpu" {
// the running magpie's CPU profile: asks it, and moves nothing here
return cpuCmd(args[1:])
}
if len(args) > 0 && args[0] == "goroutines" {
// the running magpie's goroutines: asks it, and moves nothing here
return goroutinesCmd(args[1:])
}
if len(args) == 2 && args[0] == agent.DryRunArg {
// an agent disconnected on a copy of its files under a temporary
// home, for the Agents page to show what disconnecting changes
// (agent.DisconnectPreview)
return agent.DryRun(args[1])
}
if len(args) > 1 && (args[0] == "accounts" || args[0] == "account") && args[1] == "discovery" {
return accountsDiscoveryCmd(args[2:])
}
// what an agent starts for each session or message talks to the
// running gateway and nothing else: the moves below list every
// provider and account, which starts the plugin host and asks claude,
// cursor-agent and devin who is signed in — five processes and a
// Claude /usage for each Claude Code session's image MCP (#3318)
if len(args) > 0 {
switch args[0] {
case "mcp":
return imagemcp.Run(args[1:])
case "claude-mcp-helper": // internal: stdio MCP subprocess spawned by Claude Code
return claudebridge.RunMCP(args[1:])
case "statusline":
return statuslineCmd(args)
case "tools":
return toolsCmd(args[1:])
case "tool":
return toolCmd(args[1:])
case codexrelay.Command: // internal: what a quitting magpie leaves for Codex's open threads
return codexrelay.Main(args[1:], gateway.CodexRelay())
case "uninstall":
// what the app starts as it quits from Settings › Uninstall
// magpie: nothing is made again before its folders go
if len(args) > 1 && args[1] == "--data-after" {
return uninstallData(args[2:])
}
}
}
startUp()
// a provider added, edited or removed, or a list fetched anew, reaches
// the model lists agents keep in files of their own, behind the save
// that changed it (agent.SyncCatalogSoon)
syncAgentsOnChange()
// a model Claude Code names that magpie doesn't serve goes to the one
// it is set to use for that tier
gateway.StandIn = agent.StandIn
// and its auto mode classifier, on a sonnet tier that follows a main
// model /model has moved, to the model it moved to
gateway.ClassifierStandIn = agent.ClassifierStandIn
// a Codex that reaches magpie for account failover alone is handed
// only its own models (#1385)
gateway.CodexOwnOnly = agent.CodexOwnOnly
// what magpie writes into an agent's TOML, YAML or dotenv config says
// so in a comment, for whoever reads it once magpie is gone
agent.MarkWritten()
// the Codex app's titles, descriptions and summaries, which it asks
// for on a model of its own, on the model of magpie's picked beside
// its ChatGPT sign-in rather than on that account (atom_Ray on X)
gateway.CodexSideModel = agent.CodexSideModel
gateway.CodexSideSet = agent.CodexSideSet
// the list the Codex app reads from a file beside the ChatGPT sign-in,
// its picker showing magpie's models only so (laddieyao on Discord),
// written again when the backend's list changes
agent.CodexDesktopList = gateway.CodexDesktopCatalog
gateway.CodexDesktopChanged = agent.SyncCatalogSoon
// the setup kept the same on every computer, by whichever serves
gateway.WhileServing = append(gateway.WhileServing, davsync.Run)
// and dsh's patch lists, which dsh reads live: a route left behind by
// something else writing the file fails every session there until
// magpie writes its own list again
gateway.WhileServing = append(gateway.WhileServing, agent.KeepDshWired)
// and Cursor Private Inference's variables, which the Mac's launchd
// forgets at a restart, for the gateway's address now
gateway.WhileServing = append(gateway.WhileServing, agent.KeepCursorLocalEnv)
// and Claude Science's daemon, which a restart of the computer ends,
// started on the gateway again while it is connected
gateway.WhileServing = append(gateway.WhileServing, agent.KeepClaudeScience)
// and the request archive, when it is on, goes to the bucket sync is to
gateway.ArchiveBucket = func() (gateway.Putter, bool) {
if b, ok := davsync.S3Bucket(); ok {
return b, true
}
return nil, false
}
if len(args) == 0 {
if hasGUI {
return runGUI(true, "")
}
gateway.Serves = "tui"
return runTUI()
}
// a magpie:// link the system handed over (Windows, Linux): the app
// opens it for the user to confirm
if strings.HasPrefix(strings.ToLower(args[0]), "magpie:") {
if !hasGUI {
return importCmd(args)
}
return runGUI(false, args[0])
}
// how this process was started, for the Routing page of another magpie on
// this computer to name the one that serves the gateway (#2606)
switch args[0] {
case "tui", "web", "serve", "tray":
gateway.Serves = args[0]
case "app", "gui", "panel", "-Embedding":
gateway.Serves = "app"
}
switch args[0] {
case "tui":
return runTUI()
case "web":
return webCmd(args[1:])
case "app", "gui":
// `magpie gui settings`: the window on that tab, as a restart to
// update from it comes back (update.RelaunchArgs)
if len(args) > 1 {
return runWindow(args[1])
}
return runGUI(true, "")
case "tray":
return runGUI(false, "")
case "-Embedding":
// Windows starting magpie for a click on one of its notifications
// (a usage alert, #368) left in the Action Center after it quit:
// the window, on the Usage page
return runWindow("usage")
case "panel":
return runPanel()
case "autostart":
return autostartCmd(args[1:])
case "daemon":
return daemonCmd(args[1:])
case "-h", "--help", "help":
fmt.Print(usage)
return nil
case "-v", "--version", "version":
fmt.Println("magpie", version)
return nil
case "licenses":
fmt.Print(notices.Text)
return nil
case "ls", "list":
// in the order the app lists them; those hidden there come last, dimmed
shown, hidden := settings.Arrange(settings.Load(), agent.Detected(), func(a *agent.Agent) string { return a.ID })
return list(append(shown, hidden...), true, len(shown))
case "agents":
return list(agent.All(), false, -1)
case "sync":
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := catalog.Sync(ctx); err != nil {
return err
}
fmt.Println(green.Render("✓"), "catalog saved to", catalog.CachePath())
refreshLive(ctx)
return nil
case "save", "use", "rm", "profiles":
return profiles(args)
case "import":
return importCmd(args[1:])
case "providers":
return providers()
case "presets":
return presets()
case "provider":
return providerCmd(args)
case "models":
return models(args[1:])
case "model":
return modelCmd(args[1:])
case "visible":
return visibleCmd(args[1:])
case "search":
return searchCmd(args[1:])
case "groups":
return groups()
case "group":
return groupCmd(args)
case "serve":
return serveCmd(args[1:])
case "gateway-key":
return gatewayKeys(args)
case "accounts", "account":
return accountsCmd(args)
case "usage":
return usageCmd(args)
case "sessions":
return sessionsCmd(args)
case "quota", "quotas":
return quotaCmd(args)
case "update":
return updateCmd(args)
case "library", "lib":
return libraryCmd(args)
case "backup":
return backupCmd(args[1:])
case "restore":
return restoreCmd(args[1:])
case "uninstall":
return uninstallCmd(args[1:])
case "webdav", "dav":
return webdavCmd(args[1:])
case "plugin", "plugins":
return pluginCmd(args)
case "s3":
return s3Cmd(args[1:])
case "github":
return githubCmd(args[1:])
case "claude-code":
return claudeCodeCmd(args[1:])
}
// the agent of a plugin that is gone (deleted on disk) is no agent
// here, but what magpie stashed of its file puts it back (#3810)
if len(args) == 2 && strings.EqualFold(args[1], "default") {
if g, ok, err := agent.RestoreGone(args[0]); ok {
return goneDefault(args[0], g, err)
}
}
a, err := agent.Find(args[0])
if err != nil {
return err
}
// no agent form takes a flag: one is never set as a model or a value
// (`magpie snow --verbose` would take magpie out of Snow, #1457)
if f := flagArg(args[1:]); f != "" {
return fmt.Errorf("unknown flag %s: nothing was changed\n\n%s", f, strings.TrimRight(agentHelp(a), "\n"))
}
if len(a.Fields) == 0 && a.Import != nil {
// `magpie cindy`: it takes magpie through its own link, confirmed there
if len(args) > 1 {
link := a.Import()
if link == "" {
// Lody takes only a gateway on this computer's loopback
return fmt.Errorf("%s takes magpie only at a loopback address, and the gateway is at %s: nothing was opened", a.Name, gateway.URL())
}
// handed over only when something here opens it, or nothing
// opens and nothing says so (sungt, 10-29)
openApp := func(u string) error { return appLink(u, func(u string) error { openInBrowser(u); return nil }) }
handOver := openApp
if a.ID == "lody" {
// Lody on Linux is run with the link (agent.OpenLody)
handOver = func(u string) error { return agent.OpenLody(u, openApp) }
}
if err := handOver(link); err != nil {
return fmt.Errorf("%w. Open %s once or install it again, then run this again", err, a.Name)
}
fmt.Println(green.Render("✓"), bold.Render(a.Name), muted.Render("opened to add magpie — confirm it there"))
fmt.Println(muted.Render(" " + link))
return nil
}
}
switch len(args) {
case 1:
return list([]*agent.Agent{a}, true, -1)
case 2:
// default is magpie's word in any case, as an agent's name and a
// field's are: magpie codex DEFAULT wrote model = "DEFAULT"
if strings.EqualFold(args[1], "default") {
if a.Wired() {
return disconnect(a)
}
return set(a, a.Fields[0].Key, "")
}
// `magpie codex model`: a field's name alone asks what it is set to,
// where it was set as the model (model = "model"). Claude Code's
// opus is a model's name as well as a tier's, and stays the model
if f := a.Field(args[1]); f != nil && !modelAlias(a, args[1]) {
return showField(a, f)
}
// `magpie codex xhigh`: a bare value that belongs to a non-model field
// (effort levels, for instance) is routed there; anything else is a model.
if f := fieldForValue(a, args[1]); f != nil {
return set(a, f.Key, args[1])
}
return set(a, a.Fields[0].Key, args[1])
case 3:
value := args[2]
if strings.EqualFold(value, "default") {
value = ""
}
return set(a, args[1], value)
}
return fmt.Errorf("too many arguments\n\n%s", usage)
}
func set(a *agent.Agent, key, value string) error {
f := a.Field(key)
if f == nil {
var keys []string
for _, f := range a.Fields {
keys = append(keys, f.Key)
}
return fmt.Errorf("%s has no field %q (fields: %s)", a.Name, key, strings.Join(keys, ", "))
}
// a field's name is no value: magpie codex subagent effort ran Codex's
// subagents on a model called effort. Claude Code's opus is a model's
// name as well as a tier's
if g := a.Field(value); value != "" && g != nil && !modelAlias(a, value) {
return fieldAsValue(a, f, g, value)
}
value, err := a.Spell(f.Key, value)
if err != nil {
return err
}
before := f.Get()
wired := a.Wired()
// a magpie model picked on an agent not connected yet connects it
// first, as the Agents page's picker does (#1900): the agent is wired
// in, and Disconnect puts back what it had before
if err := a.Pick(f.Key, value); err != nil {
return err
}
// what the config reads now, not what was asked: an agent may name the
// model under a provider of its own (OpenCode's magpie-relay/…), and a
// value it had already is said to be so
now := f.Get()
shown := now
if value == "" || now == "" {
shown = muted.Render("default")
}
if now == before {
shown += " " + muted.Render("(unchanged)")
}
fmt.Println(green.Render("✓"), bold.Render(a.Name), muted.Render(f.Label), shown)
// a value that is the agent's own, not one of magpie's, takes magpie
// out: said, as Disconnect is, with the way back (#2000: magpie claude
// sonnet put Claude Code back on its DeepSeek endpoint, which refused
// Sonnet, and only "✓ Claude Code model sonnet" was printed)
if wired && now != "" && !a.Wired() {
fmt.Println(muted.Render(fmt.Sprintf(" disconnected from magpie: %s is %s's own, sent to the endpoint it had before magpie; magpie %s <model> with one of magpie's models connects it again", now, a.Name, a.ID)))
}
if a.Notice != nil && (!f.Live || !wired && a.Wired()) {
if n := a.Notice(); n != "" {
fmt.Println(muted.Render(" ↻ " + n))
}
}
return nil
}
// modelAlias says whether a word names the agent's model, in any case,
// though a field goes by it too: Claude Code's opus, a tier's name as well
func modelAlias(a *agent.Agent, w string) bool {
return slices.ContainsFunc(a.ModelAliases, func(m string) bool { return strings.EqualFold(m, w) })
}
// fieldAsValue refuses field g's name given as field f's value, for what
// was meant: two words that are one field's label (magpie codex subagent
// effort), a switch, which is turned on (Claude Code's ultracode), or the
// field, which its name alone shows.
func fieldAsValue(a *agent.Agent, f, g *agent.Field, value string) error {
for _, name := range []string{f.Key + " " + value, f.Key + "_" + value} {
if h := a.Field(name); h != nil {
return fmt.Errorf("%q is one field of %s's: magpie %s %s shows it", h.Label, a.Name, a.ID, h.Key)
}
}
if g.Options != nil {
if o := g.Options(a.Values()); len(o) == 1 && o[0].Value == "on" {
return fmt.Errorf("%q is a switch of %s's, not a value for its %s: magpie %s %s on", value, a.Name, f.Label, a.ID, g.Key)
}
}
return fmt.Errorf("%q is a field of %s's, not a value for its %s: magpie %s %s shows it", value, a.Name, f.Label, a.ID, g.Key)
}
// showField is `magpie <agent> <field>`: what the field is set to, as set
// prints it
func showField(a *agent.Agent, f *agent.Field) error {
v := f.Get()
if v == "" {
v = muted.Render("default")
}
fmt.Println(bold.Render(a.Name), muted.Render(f.Label), v)
return nil
}
// disconnect is `magpie <agent> default` on an agent magpie is wired into:
// the Agents page's Disconnect, which puts back what the user had before
// magpie — Claude Code's own model, its endpoint — where a field's default
// leaves the agent as installed (__jingling on X: magpie claude default
// took the model they had set away with magpie's)
func disconnect(a *agent.Agent) error {
before := a.Values()
if err := a.Disconnect(); err != nil {
return err
}
now := a.Values()
for _, f := range a.Fields {
if before[f.Key] == now[f.Key] {
continue
}
shown := now[f.Key]
if shown == "" {
shown = muted.Render("default")
}
fmt.Println(green.Render("✓"), bold.Render(a.Name), muted.Render(f.Label), shown)
}
fmt.Println(muted.Render(" disconnected from magpie, back to what it had before"))
if a.Notice != nil {
if n := a.Notice(); n != "" {
fmt.Println(muted.Render(" ↻ " + n))
}
}
return nil
}
// goneDefault says what `magpie <id> default` did for the agent of a
// plugin that is gone.
func goneDefault(id string, g agent.Gone, err error) error {
for _, p := range g.Put {
fmt.Println(green.Render("✓"), bold.Render(id), muted.Render(p))
}
for _, p := range g.Off {
fmt.Println(green.Render("✓"), bold.Render(id), muted.Render(p+": magpie was out of it already"))
}
if len(g.Put) > 0 {
fmt.Println(muted.Render(" its plugin is gone; disconnected from magpie, back to what it had before"))
}
if g.PluginOff != "" {
fmt.Println(green.Render("✓"), bold.Render(g.PluginOff), muted.Render("is not on magpie: its plugin is off, and magpie put back what it had when the plugin was turned off"))
}
if err != nil {
return fmt.Errorf("%s's plugin is gone, and magpie couldn't put its config back: %w", id, err)
}
return nil
}
func fieldForValue(a *agent.Agent, v string) *agent.Field {
vals := a.Values()
// the agent's suffix after a model (omp's ":max") aside: a role on the
// same model at that level offers it as typed, and would take it. A list
// of models no picker offers: it is for the model
if a.SplitSuffix != nil {
m, _, one := a.SplitSuffix(v)
if !one {
return nil
}
v = m
}
// a model stays with the model, even where other fields offer it too
// (Claude Code's opus/sonnet/haiku/fable)
for _, o := range a.Fields[0].Options(vals) {
if o.Value == v {
return nil
}
}
for i := 1; i < len(a.Fields); i++ {
for _, o := range a.Fields[i].Options(vals) {
if o.Value == v {
return &a.Fields[i]
}
}
}
return nil
}
// list prints the agents; those from dimFrom on (when not -1) are the ones
// hidden in the app, and are dimmed.
func list(agents []*agent.Agent, detectedOnly bool, dimFrom int) error {
if len(agents) == 0 {
return fmt.Errorf("no supported agents found on this machine")
}
type row struct{ name, vals, path string }
var rows []row
nameW, valW := 0, 0
for i, a := range agents {
r := row{name: a.Name, path: tilde(a.Path)}
if !detectedOnly && !a.Detected() {
r.name = faint.Render(a.Name)
r.vals = faint.Render("not detected")
r.path = ""
} else if a.CLIMissing() {
// its folder left by an uninstall (#2173): what it holds is
// read by nothing
r.name = faint.Render(a.Name)
r.vals = faint.Render("CLI not found")
} else {
vals := a.Values()
dim := dimFrom >= 0 && i >= dimFrom
label, value := muted, lipgloss.NewStyle()
if dim {
label, value = faint, faint
}
var parts []string
for _, f := range a.Fields {
v := vals[f.Key]
if v == "" && f.Quiet {
continue
}
if v == "" {
v = faint.Render("—")
} else {
v = value.Render(v)
}
if f.Label == "model" {
parts = append(parts, v)
} else {
parts = append(parts, label.Render(f.Label)+" "+v)
}
}
r.name = bold.Render(a.Name)
if dim {
r.name = faint.Render(a.Name) + " " + faint.Render("hidden")
}
r.vals = strings.Join(parts, label.Render(" · "))
// through magpie for account failover while not connected (#1385)
if said := a.FailoverSaid(); said != "" {
r.vals += label.Render(" · " + said)
}
if a.Import != nil {
// unknown reads as not added here: the line only offers the
// command, never says magpie is missing
added := false
if a.Added != nil {
added, _ = a.Added()
}
if added {
r.vals = value.Render("magpie added")
} else {
r.vals = label.Render("magpie "+a.ID+" add") + faint.Render(" to add magpie")
}
}
}
nameW = max(nameW, lipgloss.Width(r.name))
valW = max(valW, lipgloss.Width(r.vals))
rows = append(rows, r)
}
for _, r := range rows {
fmt.Printf(" %s %s %s\n", pad(r.name, nameW), pad(r.vals, valW), faint.Render(r.path))
}
return nil
}
func tilde(p string) string {
if home, err := os.UserHomeDir(); err == nil && strings.HasPrefix(p, home) {
return "~" + p[len(home):]
}
return p
}
func profiles(args []string) error {
switch args[0] {
case "profiles":
ps, err := profile.Load()
if err != nil {
return err
}
if len(ps) == 0 {
fmt.Println(muted.Render("no profiles yet · magpie save <name>"))
return nil
}
detected := agent.Detected()
for _, n := range profile.Names(ps) {
// ✓ the one the setup is now (superhtoooth on X)
mark := " "
if profile.InUse(ps[n], detected, nil) {
mark = green.Render("✓")
}
fmt.Printf("%s %s %s\n", mark, bold.Render(n), muted.Render(profile.LongSummary(ps[n])))
}
return nil
case "save":
if len(args) < 2 {
return fmt.Errorf("usage: magpie save <name>")
}
p, err := profile.Snapshot()
if err != nil {
return err
}
if err := profile.Save(args[1], p); err != nil {
return err
}
fmt.Println(green.Render("✓"), "saved profile", bold.Render(args[1]))
if p.Library != nil {
fmt.Println(" ", muted.Render("with the "+p.Library.Summary()))
}
return nil
case "use":
if len(args) < 2 {
return fmt.Errorf("usage: magpie use <name>")
}
ps, err := profile.Load()
if err != nil {
return err
}
p, ok := ps[args[1]]
if !ok {
return fmt.Errorf("no profile named %q", args[1])
}
a, err := profile.Apply(p)
if err != nil {
return err
}
fmt.Println(green.Render("✓"), "applied", bold.Render(args[1]), muted.Render(fmt.Sprintf("(%d changed)", a.Changed)))
for _, line := range profile.Report(a) {
fmt.Println(" ", muted.Render(line))
}
return nil
case "rm":
if len(args) < 2 {
return fmt.Errorf("usage: magpie rm <name>")
}
if err := profile.Delete(args[1]); err != nil {
return err
}
fmt.Println(green.Render("✓"), "deleted profile", bold.Render(args[1]))
return nil
}
return nil
}
func pad(s string, w int) string {
if n := w - lipgloss.Width(s); n > 0 {
return s + strings.Repeat(" ", n)
}
return s
}