Files
magpie/internal/cli/help_cli.go
T
yetone 9ae7912210 agent: plain agy starts Antigravity CLI on magpie in a new terminal
agy takes magpie's gateway, key (GEMINI_API_KEY, GOOGLE_GEMINI_BASE_URL)
and --model only from how it is started, so a user on magpie had to copy
the launch command at every start (bestony on Discord). Now:

- magpie agy run [args…] execs the real agy with the env and --model when
  agy's settings.json is on magpie's model (a --model/-model the user
  gives wins), and runs agy untouched otherwise. On Windows agy is a child
  through proc and its exit code is passed through. It refuses when "agy"
  on PATH resolves to magpie itself. magpie agy env prints what run adds
  (PowerShell's function reads it, since the desktop exe has no console).
- update.ShellFunction puts a function named agy in a marked block
  (# >>> magpie agy >>> … # <<< magpie agy <<<) of each shell profile
  cliShells finds: .zshrc (ZDOTDIR), the bash profile a terminal reads
  (.bash_profile, or .bash_login/.profile on macOS when that is missing),
  config.fish once its folder exists, and the PowerShell 7 / Windows
  PowerShell profiles where the execution policy runs them. Only the block
  is written, in place, temp-and-rename, CRLF and BOM kept; a profile that
  can't be read is not written; only files inside the home are. The
  function falls back to `command agy` when the magpie it names is gone.
- Set to a magpie model writes the block, stepping out takes it out (the
  file comes back byte for byte), and Sync at start writes it for users
  already wired.
- Notice, Launch tooltip and the connect panel say plain agy works in a
  new terminal, with the command for a terminal open now. Strings in
  i18n.js for zh, zh-TW, ja, de. docs/reference.md and
  docs/subsystems/agent-wiring.md updated.

Run at this head (base a3d4f8c5c, origin/main just fetched):
- go test -tags nogui -count=1 -timeout 45m ./internal/agent/...
  ./internal/cli/... ./internal/update/... ./internal/gui/...
  ./internal/proc/... : all ok.
- go vet ./... (darwin), GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... : clean. gofmt -l on the changed files: none.
- GUI (node --test, chromium + webkit): agy-launch, cursor-local-launch,
  agent-connect, agent-connect-no-models, agent-connect-all-hidden,
  agent-connected-unfolded, connect-address-kept, connect-snippet-shell,
  agent-notice-lang, agent-leave-magpie, agent-disconnect, copy-command,
  i18n-keys, gui-de, gui-ja, gui-zh-tw: 0 fail. Not the whole GUI suite.
- Real try, sandbox HOME under the scratchpad, nogui build, fake agy on
  PATH printing its args and the two env vars, env -i:
  magpie provider add deepseek + magpie agy deepseek/deepseek-chat wrote
  the block after the user's two .zshrc lines; `zsh -i -c 'agy -p hi'` got
  --model magpie/deepseek/deepseek-chat, GEMINI_API_KEY=magpie-agy and
  GOOGLE_GEMINI_BASE_URL=http://127.0.0.1:38712; `agy --model x` kept x;
  wiring again left .zshrc's mtime alone and added .bash_profile once it
  existed, and `bash -l -i -c 'agy -p hi'` got the same; `magpie agy env x`
  refused; magpie agy default took both blocks out, .zshrc equal to the
  original, and plain agy ran with no env and no --model.

Broken by hand, each red, then restored:
- no fnInHome check: TestShellFunction, TestShellProfiles red.
- block appended instead of replaced in place: TestShellFunction red.
- unreadable profile treated as empty: TestShellFunction red.
- blank line before the block left on removal: TestShellFunction,
  TestWithFnBlock red.
- --model always added: TestAgyRun red.
- Sync without agyShell: TestAgyShell red. Stepping out without
  agyShell(false): TestAgyShell red.
- app.js typed row off, typed tooltip off: agy-launch 4 of 10 red each.

Not tried: PowerShell on Windows for real (vet only); WSL; a user's own
`alias agy` (an alias beats the function); a terminal already open needs
the copied command; macOS bash started non-login reads .bashrc, which
isn't written; a Documents folder redirected outside the profile isn't
written.
2026-10-27 07:30:16 +08:00

174 lines
5.0 KiB
Go

package cli
import (
"fmt"
"slices"
"strconv"
"strings"
"github.com/yetone/magpie/internal/agent"
)
// helpAsked is whether args ask for help rather than for the command to
// run: -h or --help anywhere, or help as the word after the command. It
// is looked at before run does anything, since a command that took
// --help for a value acted on it: `magpie snow --help` set Snow's model
// to "--help", which took magpie out of it (#1457), and `magpie serve
// --help` started a gateway.
func helpAsked(args []string) bool {
// what magpie is started with by others (Claude Code's MCP helper, the
// Agents page's dry run, a link, Windows' -Embedding) never carries
// an exact -h, --help or help
for _, a := range args {
if a == "-h" || a == "--help" {
return true
}
}
return len(args) > 1 && args[1] == "help"
}
// helpFor is what `magpie <command> --help` prints: the command's own
// usage where it has one, its lines of magpie's usage otherwise, an
// agent's forms and fields for an agent, and the whole usage for
// anything else.
func helpFor(args []string) string {
cmd := args[0]
switch cmd {
case "-h", "--help", "help":
return usage
case "model":
if len(args) > 1 && (args[1] == "spec" || args[1] == "specs") {
return specUsage
}
return modelUsage
case "group", "groups":
if len(args) > 1 && (args[1] == "rule" || args[1] == "rules") {
return ruleUsage
}
return groupUsage
case "library", "lib":
return " " + libraryUsage
case "plugin", "plugins":
return pluginUsage
case "quota", "quotas":
return quotaUsage
case "statusline":
return statuslineUsage
case "heap":
return heapUsage
case "cpu":
return cpuUsage
case "goroutines":
return goroutinesUsage
case "sessions":
return sessionsUsage
case "search":
return searchUsage
case "tool", "tools":
return toolUsage
case "visible":
return visibleUsage
case "webdav", "dav":
return webdavUsage
case "s3":
return s3Usage
case "github":
return githubUsage
case "claude-code":
return claudeCodeUsage
case "list":
cmd = "ls"
case "account":
cmd = "accounts"
case "app", "gui":
cmd = ""
}
if cmd != "" && !strings.HasPrefix(cmd, "-") {
if lines := usageLines(cmd); lines != "" {
return lines
}
}
if a, err := agent.Find(cmd); err == nil && cmd != "" {
return agentHelp(a)
}
return usage
}
// usageLines are the lines of magpie's usage for `magpie <cmd>`, with the
// lines that carry on from them.
func usageLines(cmd string) string {
var b strings.Builder
in := false
for _, l := range strings.Split(usage, "\n") {
if rest, ok := strings.CutPrefix(l, " magpie "); ok {
in = rest == cmd || strings.HasPrefix(rest, cmd+" ")
} else if !strings.HasPrefix(l, " ") {
in = false
}
if in {
b.WriteString(l + "\n")
}
}
return b.String()
}
// agentHelp is `magpie <agent> --help`: the forms that set it, and its
// fields.
func agentHelp(a *agent.Agent) string {
var b strings.Builder
id := a.ID
fmt.Fprintf(&b, "%s\n\n", a.Name)
fmt.Fprintf(&b, " magpie %s show it\n", id)
if len(a.Fields) == 0 {
return b.String()
}
fmt.Fprintf(&b, " magpie %s <model> set its model (magpie models %s lists them)\n", id, id)
fmt.Fprintf(&b, " magpie %s <field> show one field\n", id)
fmt.Fprintf(&b, " magpie %s <field> <value> set another field\n", id)
fmt.Fprintf(&b, " magpie %s default take magpie out: back on what it had before\n", id)
fmt.Fprintf(&b, " magpie %s <field> default that field back to its own default\n", id)
if a.ID == "agy" {
fmt.Fprintf(&b, " magpie %s run [args…] start agy, on magpie while its model is magpie's (what your shell's agy runs)\n", id)
fmt.Fprintf(&b, " magpie %s env the variables and model it is started with\n", id)
}
var keys []string
for _, f := range a.Fields {
keys = append(keys, f.Key)
}
fmt.Fprintf(&b, "\nfields: %s\n", strings.Join(keys, ", "))
// Claude Code's tiers are named as its model's aliases are
var both []string
for _, f := range a.Fields {
if slices.Contains(a.ModelAliases, f.Key) {
both = append(both, f.Key)
}
}
if n := len(both); n > 0 {
names := both[0]
if n > 1 {
names = strings.Join(both[:n-1], ", ") + " and " + both[n-1]
}
fmt.Fprintf(&b, " %s alone name the %s: magpie %s %s sets its %s to %s\n", names, a.Fields[0].Label, id, both[0], a.Fields[0].Label, both[0])
// they are the agent's own models, not magpie's: setting one takes
// magpie out (#2000, read as a query of the tier)
fmt.Fprintf(&b, " they are %s's own models, not magpie's: setting one takes magpie out; magpie %s alone shows each\n", a.Name, id)
}
return b.String()
}
// flagArg is an argument to `magpie <agent> …` that is a flag, which no
// agent form takes: it is said to be one, rather than set as a model or a
// field's value. A negative number is a value.
func flagArg(args []string) string {
for _, a := range args {
if !strings.HasPrefix(a, "-") {
continue
}
if _, err := strconv.ParseFloat(a, 64); err == nil {
continue
}
return a
}
return ""
}