Files
plugins/packages/agy-cli/platform.mjs
T
gitxmandgitxm 0f0c4ec68f agy-cli: Antigravity on the local agy CLI over ACP (#785)
New package `@magpie-community/opencode-agy-cli-auth` (provider `agy-cli`, `packages/agy-cli`). It serves Antigravity models through the user's installed `agy` CLI over ACP (`agy acp`) and uses agy's own Google sign-in. The plugin reads and writes no Google tokens.

The bridge is gitxm's. On top of it, after review:

- **ACP identity.** The client names itself (`opencode-agy-cli-auth` and its package version) instead of `zed`.
- **Catalog.**
  - Only what agy offers this client is listed: Gemini until a session says more, then the session's model option and `availableModels`.
  - Requests are still routed by everything `agy models` lists.
- **Prompt.** agy keeps its own system prompt by default. The minimal agent is opt-in with `AGY_BRIDGE_MINIMAL_AGENT=1`, and the README says what turning it on sends upstream.
- **Version check.** agy older than 1.4.12 is refused at the handshake. 1.4.12 is the oldest version the `nc` MCP bridge was checked on, and the README documents the bridge.
- **Auto-update.**
  - `AGY_CLI_DISABLE_AUTO_UPDATE` defaults to `true` only for the plugin's own agy processes.
  - A value the user set is passed on as written.
- **Docs.** The READMEs and the registry summary say how this differs from antigravity-acp (#87).
- **Tests.** The author's tests came over under `test/` with a fake agy (an ACP stdio server). Each suite runs in its own `bun test`, and a `mock.module` preload stands in for `node:child_process`.
- **Cleanup.** A duplicate, unreachable image branch in validateBody is gone.

Run at ae5eafb0 on mote (macOS) with HOME=$(mktemp -d):

- `bun test packages/agy-cli`: 7 pass. Per suite:
  - platform 14, compat 9, usage 20;
  - catalog 15, usage-settlement 18, repeat-batch 14;
  - smoke ALL OK (301 checks).
- `bun scripts/check.mjs`: all ok.
- Repo-wide `bun test`: 6792 pass, 0 fail (6812 tests, 492 files).

Broken by hand, and each went red:

- clientInfo set back to zed;
- the version check turned off;
- routing limited to the listed models;
- auto-update forced to true;
- the minimal agent on by default.

Not tried: a real agy, since mote has none, or a real Antigravity account.

Co-authored-by: gitxm <21175063+gitxm@users.noreply.github.com>
2026-11-07 20:14:45 +08:00

105 lines
5.2 KiB
JavaScript

import { spawn } from "node:child_process"
import path from "node:path"
export function agyCandidates(env, home, platform = process.platform) {
const windows = platform === "win32"
const paths = windows ? path.win32 : path.posix
const names = windows ? ["agy.exe", "agy.cmd", "agy.bat"] : ["agy"]
const searchPath = env.PATH || (windows ? env.Path : "") || ""
const candidates = env.AGY_PATH ? [env.AGY_PATH] : []
for (const directory of searchPath.split(windows ? ";" : ":")) {
if (directory) candidates.push(...names.map(name => paths.join(directory.replace(/^"|"$/g, ""), name)))
}
const directories = windows ? [
paths.join(env.LOCALAPPDATA || paths.join(home, "AppData", "Local"), "agy", "bin"),
paths.join(env.LOCALAPPDATA || paths.join(home, "AppData", "Local"), "Programs", "Antigravity", "bin"),
paths.join(home, ".local", "bin"),
paths.join(home, ".gemini", "bin"),
] : [paths.join(home, ".local", "bin"), paths.join(home, ".gemini", "bin"), "/usr/local/bin", "/opt/homebrew/bin"]
for (const directory of directories) candidates.push(...names.map(name => paths.join(directory, name)))
return candidates
}
// The cmd.exe escaping algorithm is adapted from node-cross-spawn:
// https://github.com/moxystudio/node-cross-spawn/blob/master/lib/util/escape.js
// Copyright (c) 2018 Made With MOXY Lda
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
const cmdMeta = /([()\][%!^"`<>&|;, *?])/g
function cmdArgument(value, doubleEscape) {
let argument = String(value)
if (/[\0\r\n]/.test(argument)) throw new Error("a Windows command argument contains a line break or NUL")
argument = argument.replace(/(?=(\\+?)?)\1"/g, '$1$1\\"')
argument = argument.replace(/(?=(\\+?)?)\1$/, "$1$1")
argument = `"${argument}"`.replace(cmdMeta, "^$1")
// npm's cmd-shim adds a second cmd expansion pass when forwarding %*.
return doubleEscape ? argument.replace(cmdMeta, "^$1") : argument
}
export function agySpawnSpec(file, args, platform = process.platform, env = process.env) {
if (platform !== "win32" || !/\.(?:cmd|bat)$/i.test(file)) return { file, args, options: {} }
if (/[\0\r\n]/.test(file)) throw new Error("a Windows command path contains a line break or NUL")
const doubleEscape = /node_modules[\\/]\.bin[\\/][^\\/]+\.cmd$/i.test(file)
const command = [path.win32.normalize(file).replace(cmdMeta, "^$1"), ...args.map(arg => cmdArgument(arg, doubleEscape))].join(" ")
return {
file: env.ComSpec || env.COMSPEC || env.comspec || "cmd.exe",
args: ["/d", "/v:off", "/s", "/c", `"${command}"`],
options: { windowsVerbatimArguments: true, windowsHide: true },
}
}
// The environment an agy child runs with. A value the user set for
// AGY_CLI_DISABLE_AUTO_UPDATE (in the parent's environment or the caller's)
// is kept as written. Only when nobody set it do the plugin's children run
// with auto-update off, so a pool of ACP processes doesn't update the binary
// under each other; the user's own `agy` runs keep updating.
export function childEnv(parent = process.env, extra = {}, platform = process.platform) {
const env = {}
const names = new Map()
for (const source of [parent, extra]) {
for (const [key, value] of Object.entries(source || {})) {
if (value === undefined) continue
// Windows treats env names case-insensitively; keep the caller's PATH.
const name = platform === "win32" ? key.toUpperCase() : key
if (names.has(name)) delete env[names.get(name)]
names.set(name, key)
env[key] = value
}
}
if (![...names.keys()].some(name => name.toUpperCase() === "AGY_CLI_DISABLE_AUTO_UPDATE")) {
env.AGY_CLI_DISABLE_AUTO_UPDATE = "true"
}
return env
}
export function spawnAgy(file, args, options = {}) {
const command = agySpawnSpec(file, args)
const env = childEnv(process.env, options?.env)
return spawn(command.file, command.args, { ...options, ...command.options, windowsHide: true, env })
}
export function windowsMcpShim(executable, server) {
const quote = value => {
if (/["\0\r\n]/.test(value)) throw new Error("invalid Windows MCP shim path")
return `"${value.replace(/%/g, "%%")}"`
}
return `@echo off\r\nsetlocal DisableDelayedExpansion\r\n${quote(executable)} ${quote(server)}\r\n`
}