agy-cli signs in and serves requests through Google's agy CLI. In magpie's
Docker image there is none, so its sign-in failed with "agy CLI not found"
and the user had to get a shell in the container and install it by hand,
where it lived outside the volume. Claude's sign-in already installs
claude when it is missing ("Installing Claude Code…"); a plugin had no way
to say it needs a CLI.
A plugin now declares its CLI as auth.cli = {name, manual, installed(),
install({signal})}. The host lists it as Provider.CLI and answers two new
calls, cliInstalled and installCli (run through the provider's proxy, and
given up when magpie gives up). pluginCLI returns that CLI when it isn't
installed, so the plugin's sign-in goes through the same "installing"
state as Claude's: the Providers sheet shows "Installing Antigravity CLI…",
then the sign-in link; a failed install names its cause and the plugin's
manual command. `magpie plugin login` does the same in a terminal. An
answer magpie can't get (host down, a timeout) counts as installed, so a
plugin is never reinstalled for a failed read.
The agy-cli side (installing agy into the data folder, which is on the
container's volume, and keeping that copy current) is in
magpie-community/plugins.
Run, at this head (rebased on 60ebaea03), on mica:
- go test ./internal/cli ./internal/plugin ./internal/provider ./internal/gui: ok
- GOOS=linux (-tags nogui) and GOOS=windows go vet of plugin, provider, cli: ok; gofmt clean
- TestPluginSignInInstallsItsCLI red on the old pluginSignIn (no CLI found,
straight to waiting), green now. TestPluginInstallsItsCLI covers the host
calls: listing, a refused install, an install given up, a good one.
Real use, magpie's Dockerfile built on xiaomi-gpu (Docker Desktop, linux
amd64), volume on /config, agy-cli loaded from its working tree:
- `magpie plugin login agy-cli 1`: "Installing it…", agy 1.5.9 downloaded
to /config/data/magpie-agy-cli/agy, then agy's Google sign-in link.
- restart: agy still there and runs.
- `magpie web` in the container, agy removed, POST /api/plugin-signin:
state installing ("Antigravity CLI"), then waiting with the link.
- agy.version set back to 1.5.0 and restarted: the plugin updated its copy
a minute later.
Not tried: finishing a Google sign-in in the container (no keyring there;
agy-cli's file sign-in is the path), a real install on macOS, Windows or a
musl Linux, and the full suite.
245 lines
8.3 KiB
Go
245 lines
8.3 KiB
Go
package provider
|
|
|
|
// Some subscriptions are used through the vendor's own CLI — Claude
|
|
// Code, Cursor's, Grok's, Devin's — and signed in with it too. One that isn't installed is
|
|
// installed with the vendor's own installer when its sign-in starts, as the
|
|
// vendor's page tells a person to, rather than leaving the sign-in to fail
|
|
// or, as Devin's did, to finish with nothing to show for it.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/claudecode"
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// agentCLI is a CLI a subscription runs through: how to find it, and the
|
|
// vendor's install one-liners for a POSIX shell and for PowerShell. native,
|
|
// when there is one, does what the shell one does without a shell, for a
|
|
// magpie with none (its Docker image had no bash, curl or wget until #2834). setup: the
|
|
// installer ends by running the CLI's own setup, which asks how to sign in;
|
|
// magpie signs in itself once the CLI is there. manual, when set, is how
|
|
// a person installs it, told when the install fails, where a CLI with no
|
|
// one-liner (Claude Code's, which magpie downloads itself) has native only.
|
|
type agentCLI struct {
|
|
Name string
|
|
find func() string
|
|
sh string
|
|
ps string
|
|
native func(ctx context.Context) error
|
|
manual string
|
|
setup bool
|
|
}
|
|
|
|
func cliFor(agent string) (agentCLI, bool) {
|
|
switch agent {
|
|
case "claude":
|
|
// Kabubu01 (X): the GUI's Claude sign-in failed with "install
|
|
// Claude Code first … or run `magpie claude-code install`". The
|
|
// sign-in now downloads it as that command does: Anthropic's own
|
|
// build, checked against its manifest, kept in magpie's cache and
|
|
// not put on PATH
|
|
return agentCLI{Name: "Claude Code", find: func() string {
|
|
c, ok := findClaudeCLI()
|
|
if ok && c.wsl != nil {
|
|
return "wsl:" + c.wsl.Distro
|
|
}
|
|
return c.path
|
|
}, native: downloadClaudeCode, manual: "magpie claude-code install"}, true
|
|
case "devin":
|
|
return agentCLI{Name: "Devin CLI", find: func() string { return DevinExecutable() },
|
|
sh: "curl -fsSL https://cli.devin.ai/install.sh | bash",
|
|
ps: "irm https://static.devin.ai/cli/setup.ps1 | iex",
|
|
setup: true}, true
|
|
case "cursor":
|
|
return agentCLI{Name: "Cursor CLI", find: func() string { return CursorExecutable() },
|
|
sh: "curl https://cursor.com/install -fsS | bash",
|
|
ps: "irm 'https://cursor.com/install?win32=true' | iex"}, true
|
|
case "grok":
|
|
return agentCLI{Name: "Grok Build", find: func() string { return GrokExecutable() },
|
|
sh: "curl -fsSL https://x.ai/cli/install.sh | bash",
|
|
ps: "irm https://x.ai/cli/install.ps1 | iex",
|
|
native: installGrokBuild}, true
|
|
}
|
|
return agentCLI{}, false
|
|
}
|
|
|
|
// missingCLI is the CLI an agent's sign-in needs and this machine lacks.
|
|
func missingCLI(agent string) (agentCLI, bool) {
|
|
c, ok := cliFor(agent)
|
|
if !ok || c.find() != "" {
|
|
return agentCLI{}, false
|
|
}
|
|
return c, true
|
|
}
|
|
|
|
// installTimeout bounds an installer: they download a few hundred MB at most.
|
|
var installTimeout = 10 * time.Minute
|
|
|
|
// runInstaller runs a vendor's installer; a var so tests can fake it.
|
|
var runInstaller = func(ctx context.Context, c agentCLI) ([]byte, error) {
|
|
if c.native != nil && (c.sh == "" && c.ps == "" || runtime.GOOS != "windows" && !shellInstallerRuns()) {
|
|
return nil, c.native(ctx)
|
|
}
|
|
var cmd *exec.Cmd
|
|
if runtime.GOOS == "windows" {
|
|
// the one-liner goes to PowerShell on stdin, as one typed into it
|
|
// does: on PowerShell's command line, Microsoft Defender took
|
|
// "irm <url> | iex" for a Trojan (Trojan:Win32/Commando.A!ml) and
|
|
// stopped it before it ran, which came back as "Access is denied"
|
|
cmd = proc.CommandContext(ctx, "powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", "-")
|
|
cmd.Stdin = strings.NewReader(c.ps + "\n")
|
|
} else {
|
|
cmd = proc.CommandContext(ctx, "bash", "-c", c.sh)
|
|
}
|
|
cmd.Dir, _ = os.UserHomeDir()
|
|
cmd.Env = netproxy.Env(nil)
|
|
if runtime.GOOS == "windows" && c.setup {
|
|
return runUntilInstalled(ctx, cmd, c)
|
|
}
|
|
// nothing more on stdin: an installer that would ask something takes
|
|
// its default
|
|
return cmd.CombinedOutput()
|
|
}
|
|
|
|
// installerTail is how long an installer that ends in its CLI's setup is
|
|
// given to end by itself once the CLI is in place; a var so tests can
|
|
// shorten it.
|
|
var installerTail = 5 * time.Second
|
|
|
|
// runUntilInstalled runs an installer that ends in its CLI's setup, on
|
|
// Windows, and ends it, with what it started, installerTail after the CLI
|
|
// is in place. Devin's runs `devin setup`, whose menu asking how to sign in
|
|
// waits for a key on the console, and no one can answer the console an
|
|
// installer gets from magpie (it has no window, and magpie keeps its
|
|
// output): the sign-in waited out installTimeout, then went on with
|
|
// `devin setup` still running.
|
|
func runUntilInstalled(ctx context.Context, cmd *exec.Cmd, c agentCLI) ([]byte, error) {
|
|
var out bytes.Buffer
|
|
cmd.Stdout, cmd.Stderr = &out, &out
|
|
t, err := proc.StartTree(cmd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ended := make(chan struct{})
|
|
go func() {
|
|
tick := time.NewTicker(time.Second)
|
|
defer tick.Stop()
|
|
var tail <-chan time.Time
|
|
for {
|
|
select {
|
|
case <-ended:
|
|
return
|
|
case <-ctx.Done():
|
|
t.Kill()
|
|
return
|
|
case <-tail:
|
|
t.Kill()
|
|
return
|
|
case <-tick.C:
|
|
if tail != nil {
|
|
continue
|
|
}
|
|
// the installer puts the CLI's folder on the user's PATH,
|
|
// which a running program reads from the registry
|
|
refreshPath()
|
|
if c.find() != "" {
|
|
tail = time.After(installerTail)
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
err = t.Wait()
|
|
close(ended)
|
|
return out.Bytes(), err
|
|
}
|
|
|
|
// downloadClaudeCode downloads the newest Claude Code into magpie's cache
|
|
// (claudecode.Install), where claudecode.Find, and so the sign-in, finds
|
|
// it; a var so tests never reach the network.
|
|
var downloadClaudeCode = func(ctx context.Context) error {
|
|
r, err := claudecode.Latest(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = claudecode.Install(ctx, r, nil)
|
|
return err
|
|
}
|
|
|
|
// shellInstallerRuns is whether a vendor's `curl … | bash` can run here:
|
|
// bash, and curl, which every one-liner (and the scripts behind them)
|
|
// downloads with. magpie's Docker image had none of them, and the install
|
|
// failed there with "exec: bash: not found". Since 7bfe3137 it has bash and
|
|
// busybox's wget but still no curl, and the one-liner failed there too.
|
|
// Since #2834 it is Debian with bash and curl, and the CLIs the one-liners
|
|
// install find the loader and libraries they link. A var so tests can say
|
|
// there is none.
|
|
var shellInstallerRuns = func() bool {
|
|
for _, p := range []string{"bash", "curl"} {
|
|
if _, err := exec.LookPath(p); err != nil {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// installCLI installs c with its vendor's installer and makes sure magpie
|
|
// finds it afterwards.
|
|
func installCLI(ctx context.Context, c agentCLI) error {
|
|
ctx, cancel := context.WithTimeout(ctx, installTimeout)
|
|
defer cancel()
|
|
out, err := runInstaller(ctx, c)
|
|
// an installer adds its directory to the user's PATH, which a running
|
|
// program never sees
|
|
refreshPath()
|
|
manual := c.sh
|
|
if runtime.GOOS == "windows" {
|
|
manual = c.ps
|
|
}
|
|
if c.manual != "" {
|
|
manual = c.manual
|
|
}
|
|
if ctx.Err() == context.Canceled {
|
|
return ctx.Err()
|
|
}
|
|
// what counts is the CLI being there: an installer may end in a step
|
|
// that wants a terminal — Devin's runs `devin setup`, whose sign-in
|
|
// gives up without one — and fail after it installed everything
|
|
if c.find() != "" {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
msg := lastLine(out)
|
|
if msg == "" {
|
|
msg = err.Error()
|
|
}
|
|
if manual == "" {
|
|
return errorf("installing %s failed (%s); install it yourself, then sign in again", c.Name, msg)
|
|
}
|
|
return errorf("installing %s failed (%s); install it yourself with `%s`, then sign in again", c.Name, msg, manual)
|
|
}
|
|
if manual == "" {
|
|
return errorf("the %s installer finished, but magpie can't find it; install it yourself, then sign in again", c.Name)
|
|
}
|
|
return errorf("the %s installer finished, but magpie can't find it; install it yourself with `%s`, then sign in again", c.Name, manual)
|
|
}
|
|
|
|
// lastLine is the last line of a command's output with something on it,
|
|
// without its colours.
|
|
func lastLine(out []byte) string {
|
|
lines := strings.Split(ansi.ReplaceAllString(string(out), ""), "\n")
|
|
for i := len(lines) - 1; i >= 0; i-- {
|
|
if l := strings.TrimSpace(lines[i]); l != "" {
|
|
return l
|
|
}
|
|
}
|
|
return ""
|
|
}
|