claude: claude-magpie runs Claude Code through magpie, a plain claude left on the user's account
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.
This commit is contained in:
@@ -34,6 +34,7 @@ something else shows up as drift.
|
||||
| Agents a plugin adds | A plugin's `magpie.agent` module (or a `*.agent.js` file) says where an agent magpie has no setup of its own keeps its model (`config`, `format`, `model`, `prefix`), and its `connect` returns the other keys to write. One with no `model` is connect only (#2374): its one field is `provider`, magpie or nothing, as an app whose one setting is magpie as its provider; `connect` is called without a model, must return keys, and is called again on a catalog change (`Sync`); `ListsModels` is set, so its list is the models picked for it. One whose agent keeps the model a new session starts on in a file apart from its provider list says so with `startup` (`config`, `format`, `model`, and `keys` written beside the model as they are) and has no `model`: PiG keeps its providers in `models.json` and `defaultProvider`/`defaultModel` in `settings.json` (str_ on Discord: its row only listed models, where Pi's picks the one it starts on). Its field is `model` again; picking one of magpie's writes `connect`'s keys into `config` and the model and `startup.keys` into `startup.config`, each file stashed under its own `plugin:<id>:<path>` (the startup one marked by its first string key, so a model picked in the agent among magpie's, with `defaultProvider` still `magpie`, reads as magpie's). A model of the agent's own picked in it while `connect`'s keys are still in `config` is `Beside`: connected, not drift, and Disconnect leaves that pick and takes the rest out. A magpie without `startup` reads the same module as connect only (`TestPluggedStartupModel`, `TestPluggedStartupOwnPickKept`, `TestPluggedStartupRestoreGone`). `agentplug` compiles each in moejs (via `jsmod`, as gateway middleware is), reloads it when plugins.json or the file changes, and keeps why one didn't load. `plugged` makes each an `Agent` with `Plugin` set, listed after magpie's own; an id one of magpie's own agents has is refused (`PluginErrors`). `connect` is given each model as `magpieModels` builds it, with `/v1/models`' `native_endpoints` (`provider.NativeEndpoints`, carried as `catalog.Model.Endpoints`) and `pricing` (`catalog.OpenRouterPricing` of the same `EntryPricer` price), each left out when not known ([`plugged_connect_facts_test.go`](../../internal/agent/plugged_connect_facts_test.go)). | [`agentplug.go`](../../internal/agentplug/agentplug.go), [`plugged.go`](../../internal/agent/plugged.go) |
|
||||
| Environment-only agents | Agents that read the gateway only from the environment get a `Launch` command. Antigravity CLI (agy) also gets a function `agy` in each shell's profile while it is on one of magpie's models, so typing `agy` in a new terminal starts it on magpie (bestony on Discord). The function runs `magpie agy run`, which starts the agy on PATH with `GEMINI_API_KEY`, `GOOGLE_GEMINI_BASE_URL` and `--model` (no `--model` when the user gives one) while settings.json's model is still magpie's, and unchanged otherwise. PowerShell's asks `magpie agy env` for them, since the Windows app is a GUI program PowerShell doesn't wait for. `Typed` says the function is there; the row then says plain `agy` works in a new terminal, and the `Launch` command is for a terminal open before. agy has no settings-only route: 1.4.7 refuses to start without `GEMINI_API_KEY` in the environment even with a key on the custom model. Cursor Private Inference instead gets user-level variables (`cursorLocalOn`, `KeepCursorLocalEnv`), and magpie writes none of Cursor's own files. It is found by its product.json in the apps folders, in the folder Windows' list of installed programs names (`cursorLocalInstalls`, any folder picked in its installer), or as its Linux AppImage (`Cursor_Private_Inference-…AppImage`) in the folders downloads are kept in (#1254). Its effort field is kept in magpie (`provider.AgentEffort`) and asked by the gateway, its levels those of the models it lists (`pickedEfforts`, #1003). Antigravity's app has the same field while it is on magpie: its picker lists the gateway's models by id with no level of its own, so one of Antigravity's families of levels went at its default (MDX-Tom, #4838). | [`cursorlocal.go`](../../internal/agent/cursorlocal.go) |
|
||||
| Claude Science | Its daemon (`claude-science`) reads the API's address only from its environment's `ANTHROPIC_BASE_URL` and has no setting for it. Connect restarts the daemon with that variable set to the gateway (`scienceOn`: `stop`, then `serve --detached --no-browser` on the port `status` said it was on) and notes the daemon in `claude-science.json` (url, pid, port); Disconnect restarts it without the variable, one in magpie's own environment taken out too. No variable is set for the user and no file of the app's is written. Setting it again while magpie's daemon runs on this gateway does nothing (`Pick` sets the field after `connect`). `KeepClaudeScience` starts it again on the gateway, on its port, when it isn't running (a restart of the computer), each minute while magpie serves; it never stops one running. `Check` reads the daemon's `operon.lock`, not `status` (seconds for a 130 MB Bun program), and calls a daemon with another pid drift: the app's window, opened while none ran, starts its own without the variable. It is found in the Mac app's bundle (`Claude Science.app/Contents/Resources/bin`), `%LOCALAPPDATA%\Programs\ClaudeScience` on Windows, `~/.local/bin`, else on PATH. The app still needs its own Claude sign-in; the gateway never passes it on (see gateway-routing). | [`claudescience.go`](../../internal/agent/claudescience.go), [`science.go`](../../internal/gateway/science.go) |
|
||||
| Claude Code beside its own account | `claude-magpie` (baranwang on X) starts Claude Code through magpie while a plain `claude` stays on the user's own account, nothing switched back and forth. `magpie claude run [args…]` writes `<config>/claude-launch/settings.json` as `claude#launch`, an agent `claudeIn` makes at that folder (`place.dir`) and lists nowhere, so the Agents page, Check and Notice see only the user's settings.json: connected the first time as Connect would (`ConnectHow`), set again when the gateway's address or key moved (`claudeLaunchStale`, the LAN key included), synced after that, with `ANTHROPIC_API_KEY` and the cloud switches (`agentenv.ClaudeProviders`) blanked. It then runs `claude --settings <file> args…` as a child (not exec, so it can tidy after): Claude Code puts a --settings file's env and model over the user's settings.json, a claude.ai sign-in and the shell (tried on 2.1.318). `/model` saves to the user's settings.json, never the --settings file, so `ClaudeLaunchAfter` moves a magpie model picked there into the launch file and gives the user's file its model back while magpie isn't wired into it; it also undoes Claude Code's own one-time `opus` → `opus[1m]` rewrite (`migration_opus_to_opus1m`), which the launch file's 1M opus tier sets off. A Claude alias picked (`sonnet`) stays. On Unix a stopped child stops magpie too (^Z, fg), and its exit code is magpie's; on Windows magpie ignores Ctrl+C while claude runs. The command `claude-magpie` is a launcher beside the `magpie` command (`update.Launchers`): a `#!/bin/sh` `exec` of `Program()`, or on Windows a `.cmd` running `"%~dp0magpie"`. `AddCLI` writes it, and `magpie claude launcher add` puts it there for a command added before; a file by that name without `written by magpie` is never written over. StandIn falls back to the launch file's tiers. | [`claude_launch.go`](../../internal/agent/claude_launch.go), [`claude_launch_cli.go`](../../internal/cli/claude_launch_cli.go), [`claude_run_unix.go`](../../internal/cli/claude_run_unix.go), [`launcher.go`](../../internal/update/launcher.go) |
|
||||
|
||||
## Runtime path
|
||||
|
||||
@@ -177,6 +178,7 @@ something else shows up as drift.
|
||||
go test -tags nogui ./internal/agent -run 'TestVSCode|TestZooCode'
|
||||
go test -tags nogui ./internal/agent -run '^TestStepCode'
|
||||
go test -tags nogui ./internal/agent ./internal/update ./internal/cli -run 'TestAgy|TestShellFunction|TestWithFnBlock|TestShellProfiles'
|
||||
go test -tags nogui ./internal/agent ./internal/update ./internal/cli -run 'TestClaudeLaunch|TestClaudeRun|TestLauncher|TestAddLauncher'
|
||||
node --test internal/gui/tests/agy-launch.test.cjs
|
||||
go test -tags nogui ./internal/agent -run 'TestPiModelsAskedOnTheirNativeAPI|TestPiGoogle'
|
||||
go test -tags nogui ./internal/agent -run '^TestMothX'
|
||||
|
||||
@@ -2852,7 +2852,12 @@ func StandIn(agent, model string) string {
|
||||
// a Codex in a WSL distro is known by the same client
|
||||
return wslCodexStandIn()
|
||||
}
|
||||
if m := claudeStandIn(filepath.Join(here(home).claudeDir(), "settings.json"), model); m != "" || runtime.GOOS != "windows" {
|
||||
if m := claudeStandIn(filepath.Join(here(home).claudeDir(), "settings.json"), model); m != "" {
|
||||
return m
|
||||
}
|
||||
// a claude-magpie session, its settings.json not on magpie, is on
|
||||
// the launch file's models (claude_launch.go)
|
||||
if m := claudeLaunchStandIn(model); m != "" || runtime.GOOS != "windows" {
|
||||
return m
|
||||
}
|
||||
// a Claude Code in WSL is known by the same User-Agent
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/yetone/magpie/internal/agentenv"
|
||||
"github.com/yetone/magpie/internal/appdir"
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// claude-magpie (baranwang on X): Claude Code started through magpie while
|
||||
// a plain `claude` stays on the user's own account, with nothing switched
|
||||
// back and forth. magpie keeps a settings file of its own,
|
||||
// <config>/claude-launch/settings.json, wired as it would wire Claude
|
||||
// Code's (its gateway, key, models and tiers), and `magpie claude run`
|
||||
// starts claude with --settings and that file. Claude Code puts a
|
||||
// --settings file over the user's settings.json, its env key by key over
|
||||
// the user's env and the shell's (tried with Claude Code 2.1.318: a
|
||||
// claude.ai sign-in, the user's own base URL and token, a shell
|
||||
// ANTHROPIC_BASE_URL all lost to it), so ~/.claude/settings.json is never
|
||||
// written. What could still take a request past magpie, an
|
||||
// ANTHROPIC_API_KEY or a cloud switch from the user's file or the shell,
|
||||
// is blanked in the file (claudeLaunchBlanks).
|
||||
|
||||
// ClaudeLaunchID is the launch file's agent id: claude's lists (ListsFor
|
||||
// cuts at #), its stash keys under its own name.
|
||||
const ClaudeLaunchID = "claude#launch"
|
||||
|
||||
// claudeLaunchDir is the folder of the launch file; a var so tests can
|
||||
// put it in their own folder.
|
||||
var claudeLaunchDir = func() string { return filepath.Join(appdir.Config(), "claude-launch") }
|
||||
|
||||
// ClaudeLaunchPath is the settings file `magpie claude run` hands claude.
|
||||
func ClaudeLaunchPath() string { return filepath.Join(claudeLaunchDir(), "settings.json") }
|
||||
|
||||
// claudeLaunch is the launch file as an agent: Claude Code at this home,
|
||||
// its settings.json the launch file. It is listed nowhere, so the Agents
|
||||
// page, Check and Notice see only the user's own settings.json.
|
||||
func claudeLaunch(home string) *Agent {
|
||||
a := claudeIn(place{home: home, id: ClaudeLaunchID, dir: claudeLaunchDir()})
|
||||
a.ID = ClaudeLaunchID
|
||||
a.Name = "claude-magpie"
|
||||
a.Aliases = nil
|
||||
return a
|
||||
}
|
||||
|
||||
// claudeLaunchBlanks are the variables the launch file sets to "", each
|
||||
// over the user's settings.json and the shell: an API key would go to
|
||||
// magpie beside its own key (x-api-key), and a cloud switch takes Claude
|
||||
// Code to Bedrock, Vertex and the like, past magpie.
|
||||
func claudeLaunchBlanks() []string {
|
||||
return append([]string{"ANTHROPIC_API_KEY"}, agentenv.ClaudeProviders...)
|
||||
}
|
||||
|
||||
// ClaudeLaunch writes the launch file for a start: connected the first
|
||||
// time as Connect connects Claude Code (the model it would pick, the
|
||||
// tiers following it), brought up to the catalog after that (Sync), the
|
||||
// gateway's address and key as they are now, and the blanks. It answers
|
||||
// the file's path.
|
||||
func ClaudeLaunch(home string) (string, error) {
|
||||
a := claudeLaunch(home)
|
||||
path := ClaudeLaunchPath()
|
||||
switch {
|
||||
case !a.Wired():
|
||||
if _, err := a.ConnectHow(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
case claudeLaunchStale(a, path):
|
||||
// the gateway moved (another port, MAGPIE_ADDR) or wants another
|
||||
// key (LAN sharing) since: set again at the address and key of
|
||||
// now, as Rewire does. The Agents page reads a Claude Code on an
|
||||
// older address as wired still; the launch file is set at once
|
||||
if _, errs := setAgain([]*Agent{a}); len(errs) > 0 {
|
||||
return "", errs[0]
|
||||
}
|
||||
default:
|
||||
if err := a.Sync(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
var kvs []edit.KV
|
||||
for _, k := range claudeLaunchBlanks() {
|
||||
if v, has := edit.GetJSON(path, "env."+k); !has || v != "" {
|
||||
kvs = append(kvs, edit.KV{Path: "env." + k, Value: ""})
|
||||
}
|
||||
}
|
||||
if len(kvs) > 0 {
|
||||
if err := edit.SetJSON(path, kvs...); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// claudeLaunchStale says the launch file names another address or key
|
||||
// than the gateway's of now, or the Agents page's check finds it unwired.
|
||||
func claudeLaunchStale(a *Agent, path string) bool {
|
||||
at := here("")
|
||||
u, _ := edit.GetJSON(path, "env.ANTHROPIC_BASE_URL")
|
||||
k, _ := edit.GetJSON(path, "env.ANTHROPIC_AUTH_TOKEN")
|
||||
if u != at.gw() || k != at.gwKey() {
|
||||
return true
|
||||
}
|
||||
d := a.Drift()
|
||||
return d != nil && d.Kind == "unwired"
|
||||
}
|
||||
|
||||
// ClaudeModelMark is what the user's settings.json says of its model
|
||||
// before a claude-magpie session, for ClaudeLaunchAfter.
|
||||
type ClaudeModelMark struct {
|
||||
model string
|
||||
has bool
|
||||
}
|
||||
|
||||
// ClaudeLaunchBefore marks the user's model before a session.
|
||||
func ClaudeLaunchBefore(home string) ClaudeModelMark {
|
||||
m, has := edit.GetJSON(claudeUserSettings(home), "model")
|
||||
return ClaudeModelMark{m, has}
|
||||
}
|
||||
|
||||
func claudeUserSettings(home string) string {
|
||||
return filepath.Join(here(home).claudeDir(), "settings.json")
|
||||
}
|
||||
|
||||
// ClaudeLaunchAfter is what a claude-magpie session's /model leaves:
|
||||
// Claude Code saves a pick there to the user's settings.json, never to a
|
||||
// --settings file (tried with 2.1.318). A model of magpie's picked goes
|
||||
// to the launch file, for claude-magpie's next session, and the user's
|
||||
// file gets its model back while magpie isn't wired into it: a plain
|
||||
// `claude` would ask Anthropic for a model only magpie serves. A Claude
|
||||
// model of its own (sonnet) is a pick plain claude can run, and stays.
|
||||
func ClaudeLaunchAfter(home string, was ClaudeModelMark) error {
|
||||
user := claudeUserSettings(home)
|
||||
now, has := edit.GetJSON(user, "model")
|
||||
if now == was.model && has == was.has {
|
||||
return nil
|
||||
}
|
||||
if claudeRouted(here(home), user) {
|
||||
read := provider.ClaudeRead(now)
|
||||
if m, _ := tierAt(read); isMagpie(strings.TrimSuffix(m, "[1m]")) {
|
||||
return claudeLaunch(home).Apply("model", read)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !claudeLaunchRewrote(was, now) {
|
||||
read := provider.ClaudeRead(now)
|
||||
m, _ := tierAt(read)
|
||||
if !isMagpie(strings.TrimSuffix(m, "[1m]")) {
|
||||
return nil
|
||||
}
|
||||
if err := claudeLaunch(home).Apply("model", read); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if was.has {
|
||||
return edit.SetJSON(user, edit.KV{Path: "model", Value: was.model})
|
||||
}
|
||||
return edit.DelJSON(user, "model")
|
||||
}
|
||||
|
||||
// claudeLaunchRewrote says now is Claude Code's own rewrite of the user's
|
||||
// model at a claude-magpie start, not a pick: with the launch file's opus
|
||||
// tier on 1M, Claude Code 2.1.318 turns a user's "opus" into "opus[1m]"
|
||||
// in their settings.json once (migration_opus_to_opus1m, which asks
|
||||
// whether the opus it would run is opted in to 1M). Plain claude, on the
|
||||
// user's own account, would then ask for 1M context it may not have.
|
||||
func claudeLaunchRewrote(was ClaudeModelMark, now string) bool {
|
||||
return was.has && !strings.HasSuffix(was.model, "[1m]") && now == was.model+"[1m]"
|
||||
}
|
||||
|
||||
// claudeLaunchStandIn is StandIn for a claude-magpie session: the launch
|
||||
// file's main model and tiers, while it is on this gateway.
|
||||
func claudeLaunchStandIn(model string) string {
|
||||
return claudeStandIn(ClaudeLaunchPath(), model)
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/access"
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/gateway"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// claudeLaunchHome is a sandbox HOME with a relay's models, the user's own
|
||||
// Claude Code settings.json, and the launch file in a folder of the
|
||||
// test's.
|
||||
func claudeLaunchHome(t *testing.T, user string) (home, userPath, launch string) {
|
||||
t.Helper()
|
||||
home, userPath = claudeDottedHome(t, provider.Provider{ID: "relay", Name: "Relay", Anthropic: "https://example.test", Key: "k",
|
||||
Models: []string{"claude-opus-4-6", "claude-sonnet-4-5", "claude-haiku-4-5", "glm-4.6"}}, user)
|
||||
dir := filepath.Join(t.TempDir(), "claude-launch")
|
||||
was := claudeLaunchDir
|
||||
claudeLaunchDir = func() string { return dir }
|
||||
t.Cleanup(func() { claudeLaunchDir = was })
|
||||
return home, userPath, filepath.Join(dir, "settings.json")
|
||||
}
|
||||
|
||||
// claude-magpie (baranwang on X): `claude` on the user's own account,
|
||||
// `claude-magpie` through magpie, nothing switched back and forth. The
|
||||
// launch file carries what a connect writes into settings.json (the
|
||||
// gateway, its key, a model of magpie's and the tiers), and blanks what
|
||||
// would take a request past magpie; the user's settings.json is left byte
|
||||
// for byte, and the Agents page's Claude Code (Wired, Check) still reads
|
||||
// that file alone.
|
||||
func TestClaudeLaunchFileIsWiredAndTheUsersIsNot(t *testing.T) {
|
||||
user := `{"model": "opus", "env": {"ANTHROPIC_API_KEY": "sk-ant-api03-USER", "CLAUDE_CODE_USE_BEDROCK": "1"}}`
|
||||
home, userPath, launch := claudeLaunchHome(t, user)
|
||||
|
||||
path, err := ClaudeLaunch(home)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if path != launch {
|
||||
t.Fatalf("launch file at %s, want %s", path, launch)
|
||||
}
|
||||
env := func(k string) (string, bool) { return edit.GetJSON(launch, "env."+k) }
|
||||
if v, _ := env("ANTHROPIC_BASE_URL"); v != gateway.URL() {
|
||||
t.Fatalf("launch file's base URL %q, want %q:\n%s", v, gateway.URL(), readFile(launch))
|
||||
}
|
||||
if v, _ := env("ANTHROPIC_AUTH_TOKEN"); v != gateway.Token {
|
||||
t.Fatalf("launch file's key %q, want %q", v, gateway.Token)
|
||||
}
|
||||
m, _ := edit.GetJSON(launch, "model")
|
||||
if !isMagpie(strings.TrimSuffix(m, "[1m]")) {
|
||||
t.Fatalf("launch file's model %q isn't one of magpie's:\n%s", m, readFile(launch))
|
||||
}
|
||||
if v, _ := env("ANTHROPIC_DEFAULT_HAIKU_MODEL"); !isMagpie(v) {
|
||||
t.Fatalf("launch file's haiku tier %q isn't one of magpie's:\n%s", v, readFile(launch))
|
||||
}
|
||||
for _, k := range claudeLaunchBlanks() {
|
||||
if v, has := env(k); !has || v != "" {
|
||||
t.Errorf("launch file's %s is %q (set %v), want blank over the user's", k, v, has)
|
||||
}
|
||||
}
|
||||
if got := readFile(userPath); got != user {
|
||||
t.Fatalf("the user's settings.json was written:\n%s", got)
|
||||
}
|
||||
c := claude(home)
|
||||
if c.Wired() {
|
||||
t.Fatal("the Agents page's Claude Code reads as wired from the launch file")
|
||||
}
|
||||
if d := c.Drift(); d != nil {
|
||||
t.Fatalf("the user's own Claude Code drifts after a launch: %+v", *d)
|
||||
}
|
||||
for _, a := range All() {
|
||||
if a.ID == ClaudeLaunchID {
|
||||
t.Fatal("the launch file is listed among the agents")
|
||||
}
|
||||
}
|
||||
// a stand-in for a Claude model a claude-magpie session names, from
|
||||
// the launch file while the user's own file is off magpie
|
||||
if s := StandIn("claude", "claude-opus-4-6-20260101"); s == "" {
|
||||
t.Error("no stand-in for a claude-magpie session's model")
|
||||
}
|
||||
|
||||
// a second start keeps the model the file has
|
||||
if err := claudeLaunch(home).Apply("model", "relay/glm-4.6"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ClaudeLaunch(home); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := claudeLaunch(home).Field("model").Get(); got != "relay/glm-4.6" {
|
||||
t.Fatalf("a second start set the model to %q, want the one picked", got)
|
||||
}
|
||||
if got := readFile(userPath); got != user {
|
||||
t.Fatalf("the user's settings.json was written on a second start:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
// While magpie is shared at an address beyond loopback, the gateway takes
|
||||
// its sharing key alone: the launch file carries that, and is set again
|
||||
// when the gateway moves.
|
||||
func TestClaudeLaunchFileTakesTheSharedKey(t *testing.T) {
|
||||
home, _, launch := claudeLaunchHome(t, `{}`)
|
||||
if _, err := ClaudeLaunch(home); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("MAGPIE_ADDR", "172.23.80.1:3425")
|
||||
if err := access.ConfigureLAN(true, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { access.ConfigureLAN(false, false) })
|
||||
lan := access.LANSecret()
|
||||
if _, err := ClaudeLaunch(home); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v, _ := edit.GetJSON(launch, "env.ANTHROPIC_BASE_URL"); v != "http://172.23.80.1:3425" {
|
||||
t.Fatalf("launch file's base URL %q after the gateway moved:\n%s", v, readFile(launch))
|
||||
}
|
||||
if v, _ := edit.GetJSON(launch, "env.ANTHROPIC_AUTH_TOKEN"); v != lan {
|
||||
t.Fatalf("launch file's key %q, want the LAN sharing key", v)
|
||||
}
|
||||
}
|
||||
|
||||
// Claude Code saves a /model pick to the user's settings.json, never to a
|
||||
// --settings file (2.1.318). A magpie model picked in a claude-magpie
|
||||
// session goes to the launch file, and the user's file gets its model
|
||||
// back, so a plain claude isn't left asking Anthropic for it; a Claude
|
||||
// model of its own stays where Claude Code put it.
|
||||
func TestClaudeLaunchAfterTakesBackAMagpiePick(t *testing.T) {
|
||||
home, userPath, launch := claudeLaunchHome(t, `{"model": "opus", "theme": "dark"}`)
|
||||
if _, err := ClaudeLaunch(home); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mark := ClaudeLaunchBefore(home)
|
||||
// what /model leaves: the launch file's spelling of a magpie model
|
||||
if err := edit.SetJSON(userPath, edit.KV{Path: "model", Value: "relay/glm-4.6"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ClaudeLaunchAfter(home, mark); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v, _ := edit.GetJSON(userPath, "model"); v != "opus" {
|
||||
t.Fatalf("the user's model is %q after the session, want opus back:\n%s", v, readFile(userPath))
|
||||
}
|
||||
if v, _ := edit.GetJSON(userPath, "theme"); v != "dark" {
|
||||
t.Fatalf("the user's theme is %q", v)
|
||||
}
|
||||
if got := claudeLaunch(home).Field("model").Get(); got != "relay/glm-4.6" {
|
||||
t.Fatalf("the launch file's model is %q, want the pick:\n%s", got, readFile(launch))
|
||||
}
|
||||
|
||||
// Claude Code's own opus -> opus[1m] at a start (with the launch
|
||||
// file's opus tier on 1M) is taken back too, and the launch file keeps
|
||||
// its pick
|
||||
if err := edit.SetJSON(userPath, edit.KV{Path: "model", Value: "opus"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mark = ClaudeLaunchBefore(home)
|
||||
if err := edit.SetJSON(userPath, edit.KV{Path: "model", Value: "opus[1m]"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ClaudeLaunchAfter(home, mark); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v, _ := edit.GetJSON(userPath, "model"); v != "opus" {
|
||||
t.Fatalf("the user's model is %q after Claude Code's rewrite, want opus back", v)
|
||||
}
|
||||
if got := claudeLaunch(home).Field("model").Get(); got != "relay/glm-4.6" {
|
||||
t.Fatalf("the launch file's model is %q after the rewrite, want the pick kept", got)
|
||||
}
|
||||
|
||||
// no model before: none after
|
||||
if err := edit.DelJSON(userPath, "model"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mark = ClaudeLaunchBefore(home)
|
||||
if err := edit.SetJSON(userPath, edit.KV{Path: "model", Value: "relay/claude-sonnet-4-5"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ClaudeLaunchAfter(home, mark); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v, has := edit.GetJSON(userPath, "model"); has {
|
||||
t.Fatalf("the user's file has model %q, which it hadn't before", v)
|
||||
}
|
||||
|
||||
// a Claude model of its own stays
|
||||
mark = ClaudeLaunchBefore(home)
|
||||
if err := edit.SetJSON(userPath, edit.KV{Path: "model", Value: "sonnet"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ClaudeLaunchAfter(home, mark); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v, _ := edit.GetJSON(userPath, "model"); v != "sonnet" {
|
||||
t.Fatalf("the user's own pick is %q, want sonnet left", v)
|
||||
}
|
||||
if _, err := os.Stat(launch); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,9 @@ type place struct {
|
||||
// chmod takes the group's and others' bits off a file under home, where
|
||||
// magpie's own mode doesn't hold (a distro's: see ownerOnly); nil: here
|
||||
chmod func(string) error
|
||||
// dir is the agent's config folder when magpie keeps one of its own
|
||||
// for it (claude-magpie's launch file, claude_launch.go); "": its own
|
||||
dir string
|
||||
}
|
||||
|
||||
func here(home string) place { return place{home: home} }
|
||||
@@ -117,6 +120,9 @@ func (p place) codexHome() string {
|
||||
// this machine (appdir.ClaudeDir), ~/.claude in a distro, whose variables
|
||||
// magpie can't read.
|
||||
func (p place) claudeDir() string {
|
||||
if p.dir != "" {
|
||||
return p.dir
|
||||
}
|
||||
if p.spell != nil {
|
||||
return p.join(".claude")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/agent"
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/gateway"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// claude-magpie (baranwang on X): `magpie claude run` starts the user's
|
||||
// claude with --settings and magpie's launch file, every argument
|
||||
// claude's, --help too; the user's ~/.claude/settings.json is left as it
|
||||
// was, a magpie model a session's /model saved there is taken back to the
|
||||
// launch file, and claude's exit code is magpie's.
|
||||
func TestClaudeRun(t *testing.T) {
|
||||
home := helpHome(t)
|
||||
t.Setenv("CLAUDE_CONFIG_DIR", "")
|
||||
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Anthropic: "https://example.test", Key: "k",
|
||||
Models: []string{"claude-opus-4-6", "claude-haiku-4-5", "glm-4.6"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user := filepath.Join(home, ".claude", "settings.json")
|
||||
own := `{"model": "opus"}`
|
||||
os.MkdirAll(filepath.Dir(user), 0o755)
|
||||
os.WriteFile(user, []byte(own), 0o644)
|
||||
|
||||
var bin string
|
||||
var args []string
|
||||
pick := ""
|
||||
exit := error(nil)
|
||||
oldExec, oldFind, oldUp, oldStart, oldGo := claudeRunExec, claudeFind, claudeGatewayUp, startUp, claudeLetGo
|
||||
t.Cleanup(func() {
|
||||
claudeRunExec, claudeFind, claudeGatewayUp, startUp, claudeLetGo = oldExec, oldFind, oldUp, oldStart, oldGo
|
||||
})
|
||||
let := 0
|
||||
claudeLetGo = func() { let++ }
|
||||
real := filepath.Join(home, "bin", "claude")
|
||||
claudeFind = func() string { return real }
|
||||
claudeGatewayUp = func() bool { return true }
|
||||
started := 0
|
||||
startUp = func() { started++ }
|
||||
claudeRunExec = func(b string, a, _ []string) error {
|
||||
bin, args = b, a
|
||||
if pick != "" { // what /model leaves in the user's file
|
||||
edit.SetJSON(user, edit.KV{Path: "model", Value: pick})
|
||||
}
|
||||
return exit
|
||||
}
|
||||
|
||||
launch := agent.ClaudeLaunchPath()
|
||||
for _, in := range [][]string{nil, {"--help"}, {"-p", "hi", "--model", "sonnet"}} {
|
||||
if _, err := runOut(t, append([]string{"claude", "run"}, in...)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := append([]string{"--settings", launch}, in...)
|
||||
if bin != real || !slices.Equal(args, want) {
|
||||
t.Fatalf("claude run %v: %s %v, want %v", in, bin, args, want)
|
||||
}
|
||||
}
|
||||
if let == 0 {
|
||||
t.Fatal("magpie's probes and plugin hosts were left running beside claude")
|
||||
}
|
||||
if started == 0 {
|
||||
t.Fatal("claude run started before magpie's start-up")
|
||||
}
|
||||
if v, _ := edit.GetJSON(launch, "env.ANTHROPIC_BASE_URL"); v != gateway.URL() {
|
||||
t.Fatalf("launch file's base URL %q", v)
|
||||
}
|
||||
if b, _ := os.ReadFile(user); string(b) != own {
|
||||
t.Fatalf("the user's settings.json was written:\n%s", b)
|
||||
}
|
||||
|
||||
pick = "relay/glm-4.6"
|
||||
if _, err := runOut(t, "cc", "run"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v, _ := edit.GetJSON(user, "model"); v != "opus" {
|
||||
t.Fatalf("the user's model after a /model pick of magpie's: %q", v)
|
||||
}
|
||||
if v, _ := edit.GetJSON(launch, "model"); v != "relay/glm-4.6" {
|
||||
t.Fatalf("the launch file's model after the pick: %q", v)
|
||||
}
|
||||
|
||||
pick = ""
|
||||
exit = exitError{code: 3}
|
||||
var ee exitError
|
||||
if _, err := runOut(t, "claude", "run"); !errors.As(err, &ee) || ee.code != 3 {
|
||||
t.Fatalf("claude's exit 3 is %v", err)
|
||||
}
|
||||
|
||||
out, err := runOut(t, "claude", "--help")
|
||||
if err != nil || !strings.Contains(out, "magpie claude run [args…]") || !strings.Contains(out, "fields:") {
|
||||
t.Fatalf("magpie claude --help: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
claudeFind = func() string { return "" }
|
||||
if _, err := runOut(t, "claude", "run"); err == nil {
|
||||
t.Fatal("no error without Claude Code")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
//go:build !windows
|
||||
|
||||
package cli
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/yetone/magpie/internal/proc"
|
||||
)
|
||||
|
||||
// claudeRunOS starts claude and waits for it, to see what its session
|
||||
// left in the user's settings.json (agent.ClaudeLaunchAfter). It runs in
|
||||
// magpie's process group, so the terminal's Ctrl+C and Ctrl+\ reach it
|
||||
// as they would reach claude started alone, and magpie only waits: a
|
||||
// stop (Ctrl+Z) stops magpie beside it, so the shell has the terminal
|
||||
// back, and fg continues both. A SIGTERM or SIGHUP sent to magpie alone
|
||||
// is passed on. Its exit code is magpie's.
|
||||
func claudeRunOS(bin string, args, env []string) error {
|
||||
cmd := proc.Command(bin, args...)
|
||||
cmd.Env = env
|
||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
||||
// caught, not ignored: an ignored signal stays ignored in claude
|
||||
sigs := make(chan os.Signal, 8)
|
||||
signal.Notify(sigs, os.Interrupt, syscall.SIGQUIT, syscall.SIGTSTP, syscall.SIGTERM, syscall.SIGHUP)
|
||||
defer signal.Stop(sigs)
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
for s := range sigs {
|
||||
if s == syscall.SIGTERM || s == syscall.SIGHUP {
|
||||
_ = cmd.Process.Signal(s)
|
||||
}
|
||||
}
|
||||
}()
|
||||
code, err := proc.WaitFollowingStops(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if code != 0 {
|
||||
return exitError{code: code}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
|
||||
"github.com/yetone/magpie/internal/proc"
|
||||
)
|
||||
|
||||
// claudeRunOS starts claude and waits for it, its exit code magpie's, as
|
||||
// agyExecOS does. Ctrl+C is claude's to handle, so magpie lets it pass.
|
||||
func claudeRunOS(bin string, args, env []string) error {
|
||||
cmd := proc.Command(bin, args...)
|
||||
cmd.Env = env
|
||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
||||
signal.Ignore(os.Interrupt)
|
||||
defer signal.Reset(os.Interrupt)
|
||||
err := cmd.Run()
|
||||
var ee *exec.ExitError
|
||||
if errors.As(err, &ee) {
|
||||
return exitError{code: ee.ExitCode()}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -135,6 +135,10 @@ func agentHelp(a *agent.Agent) string {
|
||||
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)
|
||||
|
||||
@@ -317,6 +317,11 @@ func run(args []string) error {
|
||||
if ok, err := agyCmd(args); ok {
|
||||
return err
|
||||
}
|
||||
// claude-magpie (claude_launch_cli.go): every argument after run is
|
||||
// claude's, --help too
|
||||
if ok, err := claudeLaunchCmd(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) {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//go:build !windows
|
||||
|
||||
package proc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// WaitFollowingStops waits for a started command in the foreground and
|
||||
// reaps it, as cmd.Wait would, but follows its stops: when the terminal
|
||||
// stops it (Ctrl+Z), magpie stops itself too, so the shell has the
|
||||
// terminal back, and fg continues both. cmd.Wait can't see a stop. It
|
||||
// answers the command's exit code, 128 plus the signal's number for one
|
||||
// a signal ended. (`magpie claude run`: claude in front, magpie waiting.)
|
||||
func WaitFollowingStops(cmd *exec.Cmd) (int, error) {
|
||||
pid := cmd.Process.Pid
|
||||
for {
|
||||
var ws syscall.WaitStatus
|
||||
if _, err := syscall.Wait4(pid, &ws, syscall.WUNTRACED, nil); err != nil {
|
||||
if err == syscall.EINTR {
|
||||
continue
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
switch {
|
||||
case ws.Stopped():
|
||||
_ = syscall.Kill(os.Getpid(), syscall.SIGSTOP)
|
||||
case ws.Exited():
|
||||
return ws.ExitStatus(), nil
|
||||
case ws.Signaled():
|
||||
return 128 + int(ws.Signal()), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
//go:build !windows
|
||||
|
||||
package proc
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestWaitFollowingStopsAnswersTheExit: the exit code as it is, and 128
|
||||
// plus the signal's number for a command a signal ended, as a shell says.
|
||||
func TestWaitFollowingStopsAnswersTheExit(t *testing.T) {
|
||||
for script, want := range map[string]int{"exit 0": 0, "exit 3": 3, "kill -TERM $$": 143} {
|
||||
cmd := Command("/bin/sh", "-c", script)
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := WaitFollowingStops(cmd)
|
||||
if err != nil || got != want {
|
||||
t.Errorf("%s: %d %v, want %d", script, got, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,19 @@ func ReadCLI() *CLI {
|
||||
// AddCLI puts this app's magpie on the PATH: of every shell whose PATH has
|
||||
// the folder ReadCLI names (shell ""), or of the one shell named, by
|
||||
// linking it in ~/.local/bin and adding that folder to the shell's profile.
|
||||
// The launchers magpie keeps (Launchers: claude-magpie) go beside it, so
|
||||
// they are there with nothing more asked.
|
||||
func AddCLI(shell string) (*CLI, error) {
|
||||
v, err := addCLI(shell)
|
||||
if err == nil && v != nil && v.Ours && v.Command != "" {
|
||||
for name, args := range Launchers {
|
||||
_ = addLauncherBeside(v, name, args...) // one of the user's own by that name stays
|
||||
}
|
||||
}
|
||||
return v, err
|
||||
}
|
||||
|
||||
func addCLI(shell string) (*CLI, error) {
|
||||
cliMu.Lock()
|
||||
defer cliMu.Unlock()
|
||||
exe, err := cliExe()
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package update
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A launcher is a command of its own a terminal runs, which runs magpie
|
||||
// with arguments: claude-magpie runs `magpie claude run` (baranwang on X:
|
||||
// `claude` on the user's own Claude account, `claude-magpie` through
|
||||
// magpie). It goes where the terminal finds the magpie command, put there
|
||||
// first as Settings › Command line puts it (AddCLI): beside its link in
|
||||
// ~/.local/bin and the like, a shell script naming this program by the
|
||||
// path that stays put (Program); on Windows a .cmd in the app's folder,
|
||||
// which runs the magpie there as a terminal's `magpie` would (magpie.com
|
||||
// before magpie.exe, PATHEXT's order), so the terminal waits for it.
|
||||
|
||||
// launcherMark is in every launcher magpie writes, so one of the user's
|
||||
// own by the same name is never taken for magpie's.
|
||||
const launcherMark = "written by magpie"
|
||||
|
||||
// Launchers are the launchers magpie puts beside its command, each name
|
||||
// with the arguments it runs magpie with.
|
||||
var Launchers = map[string][]string{"claude-magpie": {"claude", "run"}}
|
||||
|
||||
// LauncherFile is the file name of the launcher name on goos.
|
||||
func LauncherFile(goos, name string) string {
|
||||
if goos == "windows" {
|
||||
return name + ".cmd"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// launcherText is the launcher on goos that runs prog (on Windows, the
|
||||
// magpie command beside it) with args and every argument it is given.
|
||||
func launcherText(goos, prog string, args ...string) string {
|
||||
if goos == "windows" {
|
||||
var b strings.Builder
|
||||
b.WriteString("@echo off\r\nrem " + launcherMark + ": runs magpie " + strings.Join(args, " ") + "\r\n\"%~dp0magpie\"")
|
||||
for _, a := range args {
|
||||
b.WriteString(" " + strings.ReplaceAll(a, "%", "%%"))
|
||||
}
|
||||
b.WriteString(" %*\r\n")
|
||||
return b.String()
|
||||
}
|
||||
q := func(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" }
|
||||
var b strings.Builder
|
||||
b.WriteString("#!/bin/sh\n# " + launcherMark + ": runs magpie " + strings.Join(args, " ") + "\nexec " + q(prog))
|
||||
for _, a := range args {
|
||||
b.WriteString(" " + q(a))
|
||||
}
|
||||
b.WriteString(" \"$@\"\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// Launcher is where the launcher name is, "" when magpie hasn't put one
|
||||
// on the PATH: in the folder the magpie command is found in.
|
||||
func Launcher(name string) string {
|
||||
v := ReadCLI()
|
||||
if !v.Ours || v.Command == "" {
|
||||
return ""
|
||||
}
|
||||
p := filepath.Join(filepath.Dir(v.Command), LauncherFile(runtime.GOOS, name))
|
||||
if !ownLauncher(p) {
|
||||
return ""
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// ownLauncher says the file at p is a launcher magpie wrote.
|
||||
func ownLauncher(p string) bool {
|
||||
b, err := os.ReadFile(p)
|
||||
return err == nil && strings.Contains(string(b), launcherMark)
|
||||
}
|
||||
|
||||
// AddLauncher writes the launcher name, which runs magpie with args,
|
||||
// beside the magpie command a terminal runs, putting that there first
|
||||
// when it isn't (AddCLI). A file of the user's own by that name is left
|
||||
// as it is. It answers the launcher's path.
|
||||
func AddLauncher(name string, args ...string) (string, error) {
|
||||
v := ReadCLI()
|
||||
if !v.Ours {
|
||||
var err error
|
||||
if v, err = addCLI(""); err != nil {
|
||||
return "", fmt.Errorf("the magpie command isn't on your PATH, and putting it there failed: %w", err)
|
||||
}
|
||||
}
|
||||
if !v.Ours || v.Command == "" {
|
||||
return "", errors.New("the magpie command isn't on your PATH: add it in Settings › Command line, then run this again")
|
||||
}
|
||||
p := filepath.Join(filepath.Dir(v.Command), LauncherFile(runtime.GOOS, name))
|
||||
return p, addLauncherBeside(v, name, args...)
|
||||
}
|
||||
|
||||
// addLauncherBeside writes the launcher name beside v's magpie command.
|
||||
func addLauncherBeside(v *CLI, name string, args ...string) error {
|
||||
prog, err := cliExe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := filepath.Join(filepath.Dir(v.Command), LauncherFile(runtime.GOOS, name))
|
||||
return writeLauncher(p, launcherText(runtime.GOOS, prog, args...))
|
||||
}
|
||||
|
||||
// writeLauncher writes text to p, unless a file there is the user's own.
|
||||
func writeLauncher(p, text string) error {
|
||||
if b, err := os.ReadFile(p); err == nil {
|
||||
if string(b) == text {
|
||||
return nil
|
||||
}
|
||||
if !strings.Contains(string(b), launcherMark) {
|
||||
return fmt.Errorf("%s is there already, and isn't magpie's: nothing was written", p)
|
||||
}
|
||||
}
|
||||
tmp := p + ".magpie-new"
|
||||
if err := os.WriteFile(tmp, []byte(text), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(tmp, p); err != nil {
|
||||
os.Remove(tmp)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveLauncher takes the launcher name out, where magpie wrote it.
|
||||
func RemoveLauncher(name string) (string, error) {
|
||||
p := Launcher(name)
|
||||
if p == "" {
|
||||
return "", nil
|
||||
}
|
||||
return p, os.Remove(p)
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
//go:build !windows
|
||||
|
||||
package update
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/testenv"
|
||||
)
|
||||
|
||||
// `magpie claude launcher add` puts the magpie command on PATH when it
|
||||
// isn't (as Settings › Command line does), then claude-magpie beside it;
|
||||
// remove takes out only the launcher. Everything is in a temporary home.
|
||||
func TestAddLauncherBesideTheCommand(t *testing.T) {
|
||||
h := t.TempDir()
|
||||
t.Setenv("HOME", h)
|
||||
t.Setenv("ZDOTDIR", "")
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
||||
exe := filepath.Join(h, "Applications", "magpie.app", "Contents", "MacOS", "magpie")
|
||||
bin := filepath.Join(h, ".local", "bin")
|
||||
for _, d := range []string{filepath.Dir(exe), bin} {
|
||||
if err := os.MkdirAll(d, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
testenv.Program(t, exe, "#!/bin/sh\n")
|
||||
oldExe, oldShells, oldPath, oldStuck := cliExe, cliShells, cliShellPath, cliStuck
|
||||
t.Cleanup(func() { cliExe, cliShells, cliShellPath, cliStuck = oldExe, oldShells, oldPath, oldStuck })
|
||||
cliExe = func() (string, error) { return exe, nil }
|
||||
cliShells = func() []cliShell { return []cliShell{{Name: "zsh", Path: "/bin/zsh", Default: true}} }
|
||||
cliShellPath = func(string) []string { return []string{bin, "/usr/bin"} }
|
||||
cliStuck = func() string { return "" }
|
||||
|
||||
if p := Launcher("claude-magpie"); p != "" {
|
||||
t.Fatalf("a launcher before any was added: %s", p)
|
||||
}
|
||||
p, err := AddLauncher("claude-magpie", "claude", "run")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p != filepath.Join(bin, "claude-magpie") {
|
||||
t.Fatalf("launcher at %s, want in %s", p, bin)
|
||||
}
|
||||
if l, err := os.Readlink(filepath.Join(bin, "magpie")); err != nil || l != exe {
|
||||
t.Fatalf("the magpie command wasn't put on PATH first: %q, %v", l, err)
|
||||
}
|
||||
b, _ := os.ReadFile(p)
|
||||
if string(b) != launcherText("darwin", exe, "claude", "run") {
|
||||
t.Fatalf("launcher:\n%s", b)
|
||||
}
|
||||
if fi, _ := os.Stat(p); fi.Mode()&0o111 == 0 {
|
||||
t.Fatalf("launcher isn't executable: %v", fi.Mode())
|
||||
}
|
||||
if Launcher("claude-magpie") != p {
|
||||
t.Fatal("the launcher added isn't found")
|
||||
}
|
||||
if got, err := RemoveLauncher("claude-magpie"); err != nil || got != p {
|
||||
t.Fatalf("remove: %q, %v", got, err)
|
||||
}
|
||||
if _, err := os.Stat(p); !os.IsNotExist(err) {
|
||||
t.Fatal("the launcher is still there")
|
||||
}
|
||||
if _, err := os.Lstat(filepath.Join(bin, "magpie")); err != nil {
|
||||
t.Fatal("removing the launcher took the magpie command out")
|
||||
}
|
||||
|
||||
// Settings › Command line puts claude-magpie beside magpie, with
|
||||
// nothing more asked
|
||||
os.Remove(filepath.Join(bin, "magpie"))
|
||||
if _, err := AddCLI(""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Launcher("claude-magpie") != p {
|
||||
t.Fatal("adding the magpie command didn't put claude-magpie beside it")
|
||||
}
|
||||
// one of the user's own by that name stays theirs
|
||||
os.WriteFile(p, []byte("#!/bin/sh\necho mine\n"), 0o755)
|
||||
if _, err := AddCLI(""); err != nil {
|
||||
t.Fatalf("the user's own claude-magpie failed the command's add: %v", err)
|
||||
}
|
||||
if b, _ := os.ReadFile(p); string(b) != "#!/bin/sh\necho mine\n" {
|
||||
t.Fatalf("the user's own claude-magpie was written over:\n%s", b)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package update
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/proc"
|
||||
)
|
||||
|
||||
// claude-magpie (baranwang on X) is a command of its own beside magpie's:
|
||||
// on Windows a .cmd that runs the magpie beside it (magpie.com, the
|
||||
// console one, before magpie.exe), elsewhere a shell script that runs
|
||||
// this program by its path. Each passes every argument on as it is.
|
||||
func TestLauncherText(t *testing.T) {
|
||||
win := launcherText("windows", `C:\ignored\magpie.exe`, "claude", "run")
|
||||
if win != "@echo off\r\nrem written by magpie: runs magpie claude run\r\n\"%~dp0magpie\" claude run %*\r\n" {
|
||||
t.Fatalf("Windows launcher:\n%q", win)
|
||||
}
|
||||
if LauncherFile("windows", "claude-magpie") != "claude-magpie.cmd" || LauncherFile("linux", "claude-magpie") != "claude-magpie" {
|
||||
t.Fatal("launcher file names")
|
||||
}
|
||||
sh := launcherText("darwin", "/Users/o'neil/Applications/magpie.app/Contents/MacOS/magpie", "claude", "run")
|
||||
want := "#!/bin/sh\n# written by magpie: runs magpie claude run\nexec '/Users/o'\\''neil/Applications/magpie.app/Contents/MacOS/magpie' 'claude' 'run' \"$@\"\n"
|
||||
if sh != want {
|
||||
t.Fatalf("shell launcher:\n%s\nwant:\n%s", sh, want)
|
||||
}
|
||||
if !strings.Contains(win, launcherMark) || !strings.Contains(sh, launcherMark) {
|
||||
t.Fatal("a launcher without magpie's mark")
|
||||
}
|
||||
}
|
||||
|
||||
// The shell launcher, run, hands the program every argument as it is:
|
||||
// spaces, quotes, $ and --help included.
|
||||
func TestLauncherRunsWithEveryArgument(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("a shell script")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
prog := filepath.Join(dir, "it's magpie")
|
||||
out := filepath.Join(dir, "args")
|
||||
if err := os.WriteFile(prog, []byte("#!/bin/sh\nfor a in \"$@\"; do printf '%s\\n' \"$a\"; done > '"+out+"'\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
l := filepath.Join(dir, "claude-magpie")
|
||||
if err := writeLauncher(l, launcherText(runtime.GOOS, prog, "claude", "run")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b, err := proc.Command(l, "-p", "a b", `"q"`, "$HOME", "--help").CombinedOutput(); err != nil {
|
||||
t.Fatalf("%v: %s", err, b)
|
||||
}
|
||||
b, _ := os.ReadFile(out)
|
||||
if got := string(b); got != "claude\nrun\n-p\na b\n\"q\"\n$HOME\n--help\n" {
|
||||
t.Fatalf("the program got:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A file of the user's own by the launcher's name is left as it is; one
|
||||
// magpie wrote is written again.
|
||||
func TestLauncherLeavesTheUsersOwnFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
p := filepath.Join(dir, "claude-magpie")
|
||||
own := "#!/bin/sh\necho mine\n"
|
||||
if err := os.WriteFile(p, []byte(own), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writeLauncher(p, launcherText("linux", "/opt/magpie", "claude", "run")); err == nil {
|
||||
t.Fatal("the user's own claude-magpie was written over")
|
||||
}
|
||||
if b, _ := os.ReadFile(p); string(b) != own {
|
||||
t.Fatalf("the user's file is now:\n%s", b)
|
||||
}
|
||||
if ownLauncher(p) {
|
||||
t.Fatal("the user's file reads as magpie's")
|
||||
}
|
||||
if err := os.WriteFile(p, []byte(launcherText("linux", "/old/magpie", "claude", "run")), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
text := launcherText("linux", "/new/magpie", "claude", "run")
|
||||
if err := writeLauncher(p, text); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b, _ := os.ReadFile(p); string(b) != text {
|
||||
t.Fatalf("magpie's own launcher wasn't brought up to date:\n%s", b)
|
||||
}
|
||||
}
|
||||
@@ -161,6 +161,7 @@
|
||||
</table></div>
|
||||
<p><b>実行中のセッションは、開始時のモデルを使い続けます。</b> エージェントは起動時に設定を読むので、変更は次のセッションから反映されます。Codex は起動時にモデル一覧も作るため、切り替えたら Codex アプリと開いている <code>codex</code> セッションを再起動してください。</p>
|
||||
<div class="callout"><b>Paseo やエディタ拡張などのフロントエンド</b>で Claude Code や Codex を代わりに動かすものは、同じ CLI を起動し、同じ <code>~/.claude</code> または <code>~/.codex</code> の設定を読みます。magpie で一度モデルを設定すれば、それらにも次のセッションから反映されます。</div>
|
||||
<div class="callout"><b><code>claude-magpie</code>:Claude Code を magpie 経由で、<code>claude</code> はそのまま。</b>ふだんの <code>claude</code> は自分の Claude アカウントのまま、必要なときだけ magpie を通したいなら、Claude Code は接続せずに <code>claude-magpie</code> で起動します。同じ <code>claude</code> を、そのセッションだけ magpie の設定(ゲートウェイ、モデルと各ティア)を自分の設定の上に重ねて起動するので、<code>~/.claude/settings.json</code> は書き換えられません。引数はすべて Claude Code に渡ります:<code>claude-magpie --resume</code>、<code>claude-magpie -p "…"</code>。<span class="ui">設定 › コマンドライン</span> で <code>magpie</code> コマンドを追加すると、その隣に置かれます。すでに追加済みなら <code>magpie claude launcher add</code> を一度実行してください。<code>magpie claude run</code> でも同じです。その中で <code>/model</code> で選んだモデルは次の <code>claude-magpie</code> に引き継がれ、自分の <code>claude</code> は元のモデルのままです。magpie が起動している必要があります。</div>
|
||||
|
||||
<h2 id="routing"><a class="anchor" href="#routing">3 · 任意:ルーティンググループ</a></h2>
|
||||
<p>プロバイダごとにアカウントが 1 つなら、ここは読み飛ばして構いません。ChatGPT アカウントが 2 つある、あるいは同じモデルをサブスクリプションとキーの両方で使えるなど、複数あるときに意味を持ちます。</p>
|
||||
@@ -270,6 +271,7 @@ magpie claude group/opus-anywhere <span class="c"># これを使う</span></p
|
||||
<tr><td><code>magpie providers</code></td><td>プロバイダ一覧:ホスト、キー、モデル、使っているエージェント</td></tr>
|
||||
<tr><td><code>magpie models</code></td><td>エージェントが選べるすべてのモデル(<code>provider/model</code> 形式)</td></tr>
|
||||
<tr><td><code>magpie claude codex/gpt-5.5</code></td><td>エージェントのモデルを設定</td></tr>
|
||||
<tr><td><code>claude-magpie</code></td><td>このセッションだけ Claude Code を magpie 経由で起動、ふだんの <code>claude</code> は自分のアカウントのまま(<code>magpie claude run</code>、<code>magpie claude launcher add</code> で PATH に追加)</td></tr>
|
||||
<tr><td><code>magpie codex effort high</code></td><td>他の項目を設定</td></tr>
|
||||
<tr><td><code>magpie codex default</code></td><td>エージェント自身のデフォルトに戻し、magpie の設定を取り除く</td></tr>
|
||||
<tr><td><code>magpie save work</code> · <code>use work</code></td><td>プロファイルを保存・適用</td></tr>
|
||||
|
||||
@@ -161,6 +161,7 @@
|
||||
</table></div>
|
||||
<p><b>A running session keeps the model it started with.</b> Agents read their config when they start, so the change applies to the next session. Codex builds its model list at start-up too: restart the Codex app and open <code>codex</code> sessions after a switch.</p>
|
||||
<div class="callout"><b>Paseo, editor extensions and other front-ends</b> that run Claude Code or Codex for you start the same CLI, which reads the same <code>~/.claude</code> or <code>~/.codex</code> config. Set the model once in magpie and they get it too, from their next session.</div>
|
||||
<div class="callout"><b><code>claude-magpie</code>: Claude Code through magpie, <code>claude</code> left as it is.</b> To keep a plain <code>claude</code> on your own Claude account and start magpie's Claude Code only when you ask for it, leave Claude Code unconnected and run <code>claude-magpie</code> instead. It starts the same <code>claude</code> with magpie's settings over yours for that session (the gateway, the model and its tiers), so <code>~/.claude/settings.json</code> is never rewritten. Every argument goes to Claude Code: <code>claude-magpie --resume</code>, <code>claude-magpie -p "…"</code>. magpie puts it beside the <code>magpie</code> command when you add that in <span class="ui">Settings › Command line</span>; with the command there already, run <code>magpie claude launcher add</code> once. <code>magpie claude run</code> does the same. A model you pick with <code>/model</code> in it is kept for the next <code>claude-magpie</code>, and your own <code>claude</code> keeps its model. magpie has to be running.</div>
|
||||
|
||||
<h2 id="routing"><a class="anchor" href="#routing">3 · Optional: routing groups</a></h2>
|
||||
<p>With one account per provider you can skip this. It matters once you have several: two ChatGPT accounts, or the same model from a subscription and from a key.</p>
|
||||
@@ -270,6 +271,7 @@ magpie claude group/opus-anywhere <span class="c"># use it</span></pre>
|
||||
<tr><td><code>magpie providers</code></td><td>Your providers: host, key, models, who uses them</td></tr>
|
||||
<tr><td><code>magpie models</code></td><td>Every model agents can pick, as <code>provider/model</code></td></tr>
|
||||
<tr><td><code>magpie claude codex/gpt-5.5</code></td><td>Set an agent’s model</td></tr>
|
||||
<tr><td><code>claude-magpie</code></td><td>Claude Code through magpie for one session, a plain <code>claude</code> left on your account (<code>magpie claude run</code>; <code>magpie claude launcher add</code> puts it on PATH)</td></tr>
|
||||
<tr><td><code>magpie codex effort high</code></td><td>Set another field</td></tr>
|
||||
<tr><td><code>magpie codex default</code></td><td>Back to the agent’s own default, magpie’s wiring removed</td></tr>
|
||||
<tr><td><code>magpie save work</code> · <code>use work</code></td><td>Save and apply a profile</td></tr>
|
||||
|
||||
@@ -161,6 +161,7 @@
|
||||
</table></div>
|
||||
<p><b>已在运行的会话保持它启动时的模型。</b>Agent 启动时读取配置,所以改动从下一个会话生效。Codex 也在启动时生成模型列表:切换后请重启 Codex 应用和已打开的 <code>codex</code> 会话。</p>
|
||||
<div class="callout"><b>Paseo、编辑器插件等</b>替你运行 Claude Code 或 Codex 的前端,启动的是同一个 CLI,读的也是同一份 <code>~/.claude</code> 或 <code>~/.codex</code> 配置。在 magpie 里设一次模型,它们从下一个会话起也会用上。</div>
|
||||
<div class="callout"><b><code>claude-magpie</code>:经 magpie 运行 Claude Code,<code>claude</code> 保持原样。</b>想让平常的 <code>claude</code> 继续用你自己的 Claude 账号、只在需要时才走 magpie,就不要连接 Claude Code,改用 <code>claude-magpie</code> 启动。它启动的是同一个 <code>claude</code>,只在这次会话里把 magpie 的设置(网关、模型和各档位)叠在你的设置之上,<code>~/.claude/settings.json</code> 不会被改写。所有参数都交给 Claude Code:<code>claude-magpie --resume</code>、<code>claude-magpie -p "…"</code>。在 <span class="ui">设置 › 命令行</span> 添加 <code>magpie</code> 命令时,magpie 会把它放在旁边;已经添加过的,运行一次 <code>magpie claude launcher add</code> 即可。<code>magpie claude run</code> 效果相同。在其中用 <code>/model</code> 选的模型会留给下一次 <code>claude-magpie</code>,你自己的 <code>claude</code> 保留原来的模型。magpie 需要在运行。</div>
|
||||
|
||||
<h2 id="routing"><a class="anchor" href="#routing">3 · 可选:路由组</a></h2>
|
||||
<p>每个供应商只有一个账号时可以跳过这一步。等你有了多个——比如两个 ChatGPT 账号,或者同一个模型既有订阅又有密钥——再来设置。</p>
|
||||
@@ -270,6 +271,7 @@ magpie claude group/opus-anywhere <span class="c"># 使用它</span></pre>
|
||||
<tr><td><code>magpie providers</code></td><td>你的供应商:地址、密钥、模型、谁在用</td></tr>
|
||||
<tr><td><code>magpie models</code></td><td>Agent 能选的所有模型,写作 <code>provider/model</code></td></tr>
|
||||
<tr><td><code>magpie claude codex/gpt-5.5</code></td><td>设置 Agent 的模型</td></tr>
|
||||
<tr><td><code>claude-magpie</code></td><td>这一次会话经 magpie 运行 Claude Code,平常的 <code>claude</code> 仍用你的账号(<code>magpie claude run</code>;<code>magpie claude launcher add</code> 放到 PATH 上)</td></tr>
|
||||
<tr><td><code>magpie codex effort high</code></td><td>设置其他字段</td></tr>
|
||||
<tr><td><code>magpie codex default</code></td><td>恢复 Agent 自己的默认值,移除 magpie 的接入</td></tr>
|
||||
<tr><td><code>magpie save work</code> · <code>use work</code></td><td>保存、应用方案</td></tr>
|
||||
|
||||
Reference in New Issue
Block a user