baranwang on X (11-06): `claude` stays on the user's own Claude account, `claude-magpie` goes through magpie, and nothing is switched back and forth. `claude-magpie` runs `magpie claude run [args…]`. That writes a settings file of magpie's own, <config>/claude-launch/settings.json, connected as Connect connects Claude Code (the gateway, its key, the shared LAN key when sharing is on, the model, the tiers) and starts `claude --settings <file> args…`. Claude Code puts a --settings file's env and model over the user's settings.json, a claude.ai sign-in and the shell's variables (tried on 2.1.318), so ~/.claude/settings.json is never rewritten. ANTHROPIC_API_KEY and the cloud switches (Bedrock, Vertex…) are blanked in the file, so neither the user's file nor the shell can take a request past magpie. The file is the agent `claude#launch`, listed nowhere: the Agents page, Check and Notice see only the user's file. It is set again when the gateway moved or its key changed. - /model in a claude-magpie session saves to the user's settings.json (Claude Code never writes a --settings file). After the session a magpie model picked there moves into the launch file, for the next claude-magpie, and the user's file gets its model back. A Claude alias (sonnet) stays. - Claude Code's own one-time `opus` -> `opus[1m]` rewrite of the user's file (migration_opus_to_opus1m), which the launch file's 1M opus tier sets off, is undone the same way. Found in the end-to-end run below. - claude runs as a child so that can happen: on Unix a stopped child stops magpie too (^Z, fg; proc.WaitFollowingStops reaps it with WUNTRACED, as cmd.Wait can't see a stop), and claude's exit code is magpie's. On Windows magpie ignores Ctrl+C while claude has it. - The command: a launcher beside the `magpie` command. Settings › Command line (AddCLI) writes it; `magpie claude launcher [add|remove]` adds it for a command put there before. It is a `#!/bin/sh` exec of magpie's stable path, or on Windows a .cmd running "%~dp0magpie". A file by that name without "written by magpie" is never written over. - Docs: agent-wiring.md, and the start page in en/zh/ja. Run at this commit (rebased on origin/main218b38c59): - mote: gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet ./...; go build for darwin, linux (nogui), windows; go test ./... -timeout 45m: 85 packages ok; red only TestMainWindowCorners (#3949) and TestRenewLoginsKeepsClaudeReading (#5481), both red alone on origin/mainb020ae102. The run before at f22a9ae61 also had proc's TestEveryStartedCommandIsWaited red on this change's own Wait4 loop, fixed here by moving it into proc. - Windows (xiaomi-gpu): go test ./internal/update ./internal/cli: cli ok; update red only in TestPackagedByDpkg, red the same on origin/mainf47390bf3, filed as #4603. - Real Claude Code 2.1.318 in WSL (magpie-desktop), sandbox HOME, a built magpie (`serve` on its own port), a fake Anthropic upstream: - `magpie claude run -p "say hi"`: PONG, exit 0; upstream got claude-opus-4-6 with the provider's key. A user settings.json with its own ANTHROPIC_API_KEY and "model": "opus" kept both values. - Interactive in tmux under bash -i: `/model fakeco/glm-4.6` then /exit: the user's file is back on opus, the launch file on fakeco/glm-4.6. ^Z stops both magpie and claude (T), fg resumes both. - `magpie claude launcher add` wrote ~/bin/claude-magpie, and running it answered PONG. - Real Claude Code 2.1.281 on Windows: claude-magpie.cmd beside a built magpie.exe, `claude-magpie --version` printed the version through `claude --settings <launch file>`, exit 0. Broken by hand, each red: the blanks dropped; the launch dir override; the stale-address check; ClaudeLaunchAfter returning early; the opus[1m] rewrite check; WaitFollowingStops answering 0 for a signal; "$@" turned into $* in the launcher; the model mark check; AddCLI's launcher loop; --settings dropped from the args; the restore call dropped. Not tried: macOS with a real Claude Code (the Unix path ran in WSL only); Windows interactive (Ctrl+C, /model restore); a real Claude subscription or API key upstream (a fake upstream only). Known limits: a session that ends by a crash or kill skips the restore; managed settings (policy) outrank --settings; magpie has to be running (claude-magpie warns when it isn't); Claude Code itself reformats the user's settings.json when it runs its opus migration, values kept.
182 lines
5.3 KiB
Go
182 lines
5.3 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 "log", "logs":
|
|
return logUsage
|
|
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 "settings":
|
|
return settingsUsage
|
|
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)
|
|
}
|
|
if a.ID == "claude" {
|
|
fmt.Fprintf(&b, " magpie %s run [args…] start Claude Code through magpie, a plain claude left as it is (what claude-magpie runs)\n", id)
|
|
fmt.Fprintf(&b, " magpie %s launcher [add|remove] the claude-magpie command\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 ""
|
|
}
|