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.
113 lines
3.6 KiB
Go
113 lines
3.6 KiB
Go
//go:build !windows
|
|
|
|
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// Typing agy just works (bestony on Discord): picking one of magpie's
|
|
// models for agy gives the user's shells a function agy that starts it
|
|
// through magpie, the next start puts it back for a user wired before,
|
|
// and stepping out takes it out, the user's own lines as they were.
|
|
func TestAgyShell(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("ZDOTDIR", "")
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mine := "alias ll='ls -l'\n"
|
|
rcs := []string{".zshrc", ".bashrc", ".bash_profile"}
|
|
for _, n := range rcs {
|
|
os.WriteFile(filepath.Join(home, n), []byte(mine), 0o644)
|
|
}
|
|
a, err := Find("agy")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if env, m := AgyStart(); env != nil || m != "" {
|
|
t.Fatalf("AgyStart before: %v %q", env, m)
|
|
}
|
|
if err := a.Apply("model", "magpie/deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
in := update.ShellFunctionIn("agy")
|
|
if len(in) == 0 {
|
|
t.Fatal("no shell's profile has agy")
|
|
}
|
|
for _, p := range in {
|
|
b, _ := os.ReadFile(p)
|
|
if s := string(b); !strings.HasPrefix(s, mine+"\n# >>> magpie agy >>>\nfunction agy {\n") || !strings.Contains(s, ` agy run "$@"; else command agy "$@"; fi`) || !strings.HasSuffix(s, "}\n# <<< magpie agy <<<\n") {
|
|
t.Fatalf("%s:\n%s", p, s)
|
|
}
|
|
}
|
|
if a.Typed() != "agy" {
|
|
t.Fatalf("typed: %q", a.Typed())
|
|
}
|
|
if n := a.Notice(); !strings.HasPrefix(n, "Type agy in a new terminal: magpie added it to ~/") || !strings.HasSuffix(n, "(in a terminal open now: "+a.Launch()+").") {
|
|
t.Fatalf("notice: %q", n)
|
|
}
|
|
env, m := AgyStart()
|
|
if m != "magpie/deepseek/pro" || strings.Join(env, " ") != "GEMINI_API_KEY="+gateway.TokenFor("agy")+" GOOGLE_GEMINI_BASE_URL="+gateway.URL() {
|
|
t.Fatalf("AgyStart: %v %q", env, m)
|
|
}
|
|
|
|
// wired before the function was given: the next start gives it
|
|
for _, p := range in {
|
|
os.WriteFile(p, []byte(mine), 0o644)
|
|
}
|
|
if a.Typed() != "" {
|
|
t.Fatal("typed with no function")
|
|
}
|
|
if err := a.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := update.ShellFunctionIn("agy"); strings.Join(got, ",") != strings.Join(in, ",") {
|
|
t.Fatalf("after Sync: %v, want %v", got, in)
|
|
}
|
|
|
|
// out: as the user had them
|
|
if err := a.Apply("model", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, n := range rcs {
|
|
if b, _ := os.ReadFile(filepath.Join(home, n)); string(b) != mine {
|
|
t.Fatalf("%s after stepping out:\n%s", n, b)
|
|
}
|
|
}
|
|
if a.Typed() != "" {
|
|
t.Fatal("typed after stepping out")
|
|
}
|
|
// and a start off magpie doesn't give it again
|
|
a.Sync()
|
|
if got := update.ShellFunctionIn("agy"); len(got) != 0 {
|
|
t.Fatalf("Sync off magpie: %v", got)
|
|
}
|
|
}
|
|
|
|
// The function in each shell runs this magpie's `agy run`, the agy on
|
|
// PATH when magpie is gone, its path quoted for that shell.
|
|
func TestAgyFunction(t *testing.T) {
|
|
prog := "/Users/o'neil/magpie"
|
|
for kind, want := range map[string]string{
|
|
"posix": `if [ -x '/Users/o'\''neil/magpie' ]; then '/Users/o'\''neil/magpie' agy run "$@"; else command agy "$@"; fi`,
|
|
"fish": `'/Users/o\'neil/magpie' agy run $argv`,
|
|
"powershell": `foreach ($l in @(& '/Users/o''neil/magpie' agy env))`,
|
|
} {
|
|
if got := agyFunction(kind, prog); !strings.Contains(got, want) {
|
|
t.Errorf("%s:\n%s\nwant it to have %s", kind, got, want)
|
|
}
|
|
}
|
|
}
|