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.
137 lines
4.3 KiB
Go
137 lines
4.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"slices"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/claudecode"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// `magpie claude run [args…]` is claude-magpie (baranwang on X): Claude
|
|
// Code through magpie, while a plain `claude` stays on the user's own
|
|
// account. claude starts with --settings and magpie's launch file
|
|
// (agent/claude_launch.go), so ~/.claude/settings.json isn't switched;
|
|
// every argument is claude's, --help too. `magpie claude launcher` puts
|
|
// the claude-magpie command beside the magpie one on the PATH.
|
|
|
|
const claudeRunUsage = `magpie claude run [args…] start Claude Code through magpie
|
|
magpie claude launcher [add|remove]
|
|
the claude-magpie command, which runs this
|
|
|
|
claude starts with --settings and a settings file magpie keeps for it, wired
|
|
to magpie's gateway, key and models; your own ~/.claude/settings.json and a
|
|
plain claude stay as they are. Every argument after run is claude's.
|
|
`
|
|
|
|
// claudeLauncherName is the command a terminal runs for claude-magpie.
|
|
const claudeLauncherName = "claude-magpie"
|
|
|
|
// claudeLaunchCmd is `magpie claude run|launcher …`; ok false for
|
|
// another form.
|
|
func claudeLaunchCmd(args []string) (ok bool, err error) {
|
|
if len(args) < 2 || args[0] != "claude" && args[0] != "claude-code" && args[0] != "cc" {
|
|
return false, nil
|
|
}
|
|
switch args[1] {
|
|
case "run":
|
|
return true, claudeRun(args[2:])
|
|
case "launcher":
|
|
rest := args[2:]
|
|
if len(rest) > 0 && slices.Contains([]string{"-h", "--help", "help"}, rest[0]) {
|
|
fmt.Print(claudeRunUsage)
|
|
return true, nil
|
|
}
|
|
return true, claudeLauncher(rest)
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// claudeRunExec runs claude (claude_run_*.go) and waits for it; tests set it.
|
|
var claudeRunExec = claudeRunOS
|
|
|
|
// claudeFind is the Claude Code to start; tests set it.
|
|
var claudeFind = claudecode.Find
|
|
|
|
// claudeGatewayUp says whether magpie's gateway answers; tests set it.
|
|
var claudeGatewayUp = gateway.Running
|
|
|
|
// claudeLetGo hands claude the terminal (its Ctrl+C, its exit) and ends
|
|
// what magpie started (probes, plugin hosts), so nothing of magpie's runs
|
|
// beside a session for its hours; tests set it, since a host ended stays
|
|
// ended in the process.
|
|
var claudeLetGo = func() {
|
|
leaveSignals()
|
|
proc.EndProbes()
|
|
plugin.EndHosts()
|
|
}
|
|
|
|
func claudeRun(args []string) error {
|
|
bin := claudeFind()
|
|
if bin == "" {
|
|
return errors.New("Claude Code isn't installed: install it from https://claude.com/claude-code, " + claudecode.InstallHint)
|
|
}
|
|
if self, err := os.Executable(); err == nil && sameProgram(self, bin) {
|
|
return errors.New("the claude on your PATH is magpie itself; install Claude Code")
|
|
}
|
|
home, _ := os.UserHomeDir()
|
|
plugin.ServePages(false)
|
|
startUp()
|
|
path, err := agent.ClaudeLaunch(home)
|
|
if err != nil {
|
|
return fmt.Errorf("writing claude-magpie's settings: %w", err)
|
|
}
|
|
if !claudeGatewayUp() {
|
|
fmt.Fprintln(os.Stderr, "magpie: the gateway isn't running, so Claude Code's requests will fail: open magpie, or run `magpie serve`")
|
|
}
|
|
mark := agent.ClaudeLaunchBefore(home)
|
|
claudeLetGo()
|
|
runErr := claudeRunExec(bin, append([]string{"--settings", path}, args...), os.Environ())
|
|
if err := agent.ClaudeLaunchAfter(home, mark); err != nil {
|
|
fmt.Fprintln(os.Stderr, "magpie: putting your settings.json's model back:", err)
|
|
}
|
|
return runErr
|
|
}
|
|
|
|
func claudeLauncher(args []string) error {
|
|
what := "status"
|
|
if len(args) > 0 {
|
|
what = args[0]
|
|
}
|
|
if len(args) > 1 {
|
|
return fmt.Errorf("magpie claude launcher takes one word, not %q", args[1])
|
|
}
|
|
switch what {
|
|
case "status":
|
|
if p := update.Launcher(claudeLauncherName); p != "" {
|
|
fmt.Println(p)
|
|
return nil
|
|
}
|
|
fmt.Println("claude-magpie isn't on your PATH: run `magpie claude launcher add`")
|
|
return nil
|
|
case "add":
|
|
p, err := update.AddLauncher(claudeLauncherName, "claude", "run")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println("claude-magpie:", p)
|
|
return nil
|
|
case "remove":
|
|
p, err := update.RemoveLauncher(claudeLauncherName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if p != "" {
|
|
fmt.Println("removed", p)
|
|
}
|
|
return nil
|
|
}
|
|
return fmt.Errorf("magpie claude launcher: add, remove or nothing, not %q", what)
|
|
}
|