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.
109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// `magpie agy run [args…]` is what the function agy magpie puts in the
|
|
// user's shells runs (agent/agy.go): the agy on PATH, given magpie's
|
|
// gateway, key and model while settings.json's model is one of magpie's,
|
|
// and as it is otherwise. Every argument is agy's, --help too. `magpie agy
|
|
// env` prints those variables and the model, one NAME=value a line and
|
|
// nothing off magpie, for PowerShell's function.
|
|
|
|
const agyRunUsage = `magpie agy run [args…] start agy, on magpie while its model is one of magpie's
|
|
|
|
The agy on your PATH starts with GEMINI_API_KEY, GOOGLE_GEMINI_BASE_URL and
|
|
--model <its model> (not when you give --model yourself); every argument is
|
|
agy's. Off magpie's models it starts as it is. The function agy magpie puts
|
|
in your shell's profile runs this.
|
|
|
|
magpie agy env the variables and model it starts agy with, a NAME=value
|
|
a line; nothing while agy isn't on magpie
|
|
`
|
|
|
|
// agyCmd is `magpie agy run|env …`; ok false for another form.
|
|
func agyCmd(args []string) (ok bool, err error) {
|
|
if len(args) < 2 || args[0] != "agy" && args[0] != "antigravity-cli" {
|
|
return false, nil
|
|
}
|
|
switch args[1] {
|
|
case "run":
|
|
return true, agyRun(args[2:])
|
|
case "env":
|
|
if len(args) > 2 {
|
|
if slices.Contains([]string{"-h", "--help", "help"}, args[2]) {
|
|
fmt.Print(agyRunUsage)
|
|
return true, nil
|
|
}
|
|
return true, fmt.Errorf("magpie agy env takes nothing after it, not %q", args[2])
|
|
}
|
|
env, model := agent.AgyStart()
|
|
if model == "" {
|
|
return true, nil
|
|
}
|
|
for _, kv := range env {
|
|
fmt.Println(kv)
|
|
}
|
|
fmt.Println("model=" + model)
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// agyExec starts the program bin as agy with args and env, in place of
|
|
// magpie where the system can (agy_exec_*.go); tests set it.
|
|
var agyExec = agyExecOS
|
|
|
|
// agyLook is the agy on PATH; tests set it.
|
|
var agyLook = func() (string, error) { return proc.Look("agy") }
|
|
|
|
func agyRun(args []string) error {
|
|
bin, err := agyLook()
|
|
if err != nil {
|
|
return errors.New("agy isn't installed, or isn't on your PATH")
|
|
}
|
|
if self, err := os.Executable(); err == nil && sameProgram(self, bin) {
|
|
return errors.New("the agy on your PATH is magpie itself; install Antigravity CLI")
|
|
}
|
|
env := os.Environ()
|
|
start, model := agent.AgyStart()
|
|
if model != "" {
|
|
env = slices.DeleteFunc(env, func(kv string) bool {
|
|
k, _, _ := strings.Cut(kv, "=")
|
|
return strings.EqualFold(k, "GEMINI_API_KEY") || strings.EqualFold(k, "GOOGLE_GEMINI_BASE_URL")
|
|
})
|
|
env = append(env, start...)
|
|
if !slices.ContainsFunc(args, modelFlag) {
|
|
args = append([]string{"--model", model}, args...)
|
|
}
|
|
}
|
|
return agyExec(bin, args, env)
|
|
}
|
|
|
|
// modelFlag says a is agy's --model, which it takes as -model too.
|
|
func modelFlag(a string) bool {
|
|
a, _, _ = strings.Cut(a, "=")
|
|
return a == "--model" || a == "-model"
|
|
}
|
|
|
|
func sameProgram(a, b string) bool {
|
|
if ra, err := filepath.EvalSymlinks(a); err == nil {
|
|
a = ra
|
|
}
|
|
if rb, err := filepath.EvalSymlinks(b); err == nil {
|
|
b = rb
|
|
}
|
|
sa, err1 := os.Stat(a)
|
|
sb, err2 := os.Stat(b)
|
|
return err1 == nil && err2 == nil && os.SameFile(sa, sb)
|
|
}
|