Files
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

401 lines
11 KiB
JavaScript

import fs from "node:fs"
import net from "node:net"
const PROTOCOL_VERSION = "2025-11-25"
const SERVER_INFO = { name: "opencode-agy-cli-auth", version: "0.1.0" }
// The bridge swaps tool catalogs in place and pushes list_changed over a watch channel.
const CAPABILITIES = { tools: { listChanged: true } }
const MAX_MESSAGE_BYTES = 16 * 1024 * 1024
const CONTROL_PORT = Number(process.env.AGY_BRIDGE_CONTROL_PORT)
const CONTROL_TOKEN = process.env.AGY_BRIDGE_TOKEN
const CONTROL_CONFIGURED = Number.isInteger(CONTROL_PORT)
&& CONTROL_PORT >= 1
&& CONTROL_PORT <= 65535
&& typeof CONTROL_TOKEN === "string"
&& CONTROL_TOKEN.length > 0
const activeControls = new Set()
const pendingCalls = new Map()
let inputChunks = []
let inputLength = 0
let discardingInput = false
let inputClosed = false
function isObject(value) {
return value !== null && typeof value === "object" && !Array.isArray(value)
}
function isValidId(value) {
return value === null
|| typeof value === "string"
|| (typeof value === "number" && Number.isFinite(value))
}
function internalError(message = "Internal error") {
return { code: -32603, message }
}
function closeAllControls() {
for (const request of [...activeControls]) request.cancel()
}
function closeInput() {
if (inputClosed) return
inputClosed = true
inputChunks = []
inputLength = 0
closeAllControls()
watchSocket?.destroy()
}
function handleStdoutError(error) {
closeInput()
process.exit(error?.code === "EPIPE" ? 0 : 1)
}
// Diagnostics only: AGY_BRIDGE_MCP_TRACE names a file that receives the MCP traffic.
const TRACE_PATH = process.env.AGY_BRIDGE_MCP_TRACE
function trace(direction, message) {
if (!TRACE_PATH) return
try {
const summary = message?.method === "tools/list" || message?.result?.tools
? { ...message, result: message.result?.tools ? { tools: message.result.tools.map(tool => tool.name) } : message.result }
: message
fs.appendFileSync(TRACE_PATH, JSON.stringify({ at: Date.now(), pid: process.pid, direction, message: summary }) + "\n", { mode: 0o600 })
} catch {}
}
function send(message) {
if (inputClosed) return
trace("out", message)
try {
process.stdout.write(JSON.stringify(message) + "\n")
} catch (error) {
handleStdoutError(error)
}
}
function sendResult(id, result) {
send({ jsonrpc: "2.0", id, result })
}
function sendError(id, error) {
send({ jsonrpc: "2.0", id, error })
}
function configurationError() {
return internalError("Bridge control channel is not configured")
}
function controlRequest(payload) {
const request = {
socket: null,
cancelled: false,
settled: false,
cancel: () => {},
promise: null,
}
request.promise = new Promise(resolve => {
const failure = () => ({ error: internalError("Bridge control channel failure") })
const finish = outcome => {
if (request.settled) return
request.settled = true
activeControls.delete(request)
request.socket?.destroy()
resolve(outcome)
}
request.cancel = () => {
if (request.settled) return
request.cancelled = true
finish(failure())
}
activeControls.add(request)
let socket
try {
socket = net.createConnection({ host: "127.0.0.1", port: CONTROL_PORT })
} catch {
finish(failure())
return
}
request.socket = socket
socket.setEncoding("utf8")
let buffer = ""
socket.on("connect", () => {
try {
socket.write(JSON.stringify(payload) + "\n")
} catch {
finish(failure())
}
})
socket.on("data", chunk => {
if (request.settled) return
buffer += chunk
const newline = buffer.indexOf("\n")
if (newline < 0) return
let line = buffer.slice(0, newline)
if (line.endsWith("\r")) line = line.slice(0, -1)
let response
try {
response = JSON.parse(line)
} catch {
finish(failure())
return
}
if (!isObject(response)) {
finish(failure())
return
}
if (Object.hasOwn(response, "error")) {
const error = response.error
if (!isObject(error) || typeof error.code !== "number" || typeof error.message !== "string") {
finish(failure())
return
}
finish({ error })
return
}
if (!Object.hasOwn(response, "result")) {
finish(failure())
return
}
finish({ result: response.result })
})
socket.on("error", () => finish(failure()))
socket.on("close", () => finish(failure()))
})
return request
}
let watchSocket = null
// One long-lived control connection; each line announces a changed tool catalog.
function startWatch() {
if (!CONTROL_CONFIGURED || watchSocket || inputClosed) return
let socket
try {
socket = net.createConnection({ host: "127.0.0.1", port: CONTROL_PORT })
} catch {
return
}
watchSocket = socket
socket.setEncoding("utf8")
let buffer = ""
socket.on("connect", () => {
try { socket.write(JSON.stringify({ token: CONTROL_TOKEN, kind: "watch" }) + "\n") } catch {}
})
socket.on("data", chunk => {
buffer += chunk
if (buffer.length > MAX_MESSAGE_BYTES) {
socket.destroy()
return
}
let newline
while ((newline = buffer.indexOf("\n")) >= 0) {
const line = buffer.slice(0, newline)
buffer = buffer.slice(newline + 1)
let event
try { event = JSON.parse(line) } catch { continue }
if (isObject(event) && event.event === "tools_changed") {
send({ jsonrpc: "2.0", method: "notifications/tools/list_changed" })
}
}
})
socket.on("error", () => {})
socket.on("close", () => {
if (watchSocket === socket) watchSocket = null
})
}
function cancelCall(params) {
if (!isObject(params) || !Object.hasOwn(params, "requestId")) return
const request = pendingCalls.get(params.requestId)
if (!request) return
pendingCalls.delete(params.requestId)
request.cancel()
}
async function listTools(message, hasId) {
if (!CONTROL_CONFIGURED) {
if (hasId) sendError(message.id, configurationError())
return
}
const request = controlRequest({ token: CONTROL_TOKEN, kind: "list" })
const outcome = await request.promise
if (inputClosed || !hasId) return
if (outcome.error) sendError(message.id, outcome.error)
else sendResult(message.id, outcome.result)
}
async function callTool(message, hasId) {
const params = message.params
if (!isObject(params)
|| typeof params.name !== "string"
|| params.name.length === 0
|| (params.arguments !== undefined && !isObject(params.arguments))) {
if (hasId) sendError(message.id, { code: -32602, message: "Invalid params" })
return
}
if (!CONTROL_CONFIGURED) {
if (hasId) sendError(message.id, configurationError())
return
}
if (hasId && pendingCalls.has(message.id)) {
sendError(message.id, { code: -32600, message: "Duplicate request id" })
return
}
const request = controlRequest({
token: CONTROL_TOKEN,
kind: "call",
name: params.name,
arguments: params.arguments === undefined ? {} : params.arguments,
meta: params._meta || {},
})
if (hasId) pendingCalls.set(message.id, request)
try {
const outcome = await request.promise
if (inputClosed || request.cancelled || !hasId) return
if (outcome.error) sendError(message.id, outcome.error)
else sendResult(message.id, outcome.result)
} finally {
if (hasId && pendingCalls.get(message.id) === request) pendingCalls.delete(message.id)
}
}
async function handleMessage(message) {
const hasId = Object.hasOwn(message, "id")
switch (message.method) {
case "server/discover":
// Answer as a pre-2026 server. Under 2026-07-28 agy expects notifications
// through subscriptions/listen; the initialize flow delivers
// notifications/tools/list_changed directly, which agy 1.4 honours.
if (hasId) sendError(message.id, { code: -32601, message: "Method not found" })
return
case "initialize": {
if (!hasId) return
const requestedVersion = isObject(message.params) && typeof message.params.protocolVersion === "string"
? message.params.protocolVersion
: PROTOCOL_VERSION
sendResult(message.id, {
protocolVersion: requestedVersion,
capabilities: CAPABILITIES,
serverInfo: SERVER_INFO,
})
return
}
case "notifications/initialized":
startWatch()
return
case "notifications/cancelled":
cancelCall(message.params)
return
case "ping":
if (hasId) sendResult(message.id, {})
return
case "tools/list":
startWatch()
await listTools(message, hasId)
return
case "tools/call":
await callTool(message, hasId)
return
default:
if (hasId) sendError(message.id, { code: -32601, message: "Method not found" })
}
}
function dispatch(message) {
handleMessage(message).catch(() => {
if (Object.hasOwn(message, "id")) sendError(message.id, internalError())
})
}
function handleLine(line) {
if (line.length > 0 && line[line.length - 1] === 13) line = line.subarray(0, -1)
if (line.length === 0) return
const text = line.toString("utf8")
if (text.trim().length === 0) return
let message
try {
message = JSON.parse(text)
} catch {
sendError(null, { code: -32700, message: "Parse error" })
return
}
if (!isObject(message)
|| message.jsonrpc !== "2.0"
|| typeof message.method !== "string"
|| (Object.hasOwn(message, "id") && !isValidId(message.id))) {
sendError(null, { code: -32600, message: "Invalid Request" })
return
}
trace("in", message)
dispatch(message)
}
function handleInput(chunk) {
let start = 0
while (start < chunk.length) {
const newline = chunk.indexOf(10, start)
const end = newline < 0 ? chunk.length : newline
if (!discardingInput && end > start) {
const piece = chunk.subarray(start, end)
if (inputLength + piece.length > MAX_MESSAGE_BYTES) {
inputChunks = []
inputLength = 0
discardingInput = true
sendError(null, { code: -32600, message: "Invalid Request: message exceeds 16 MiB" })
} else {
inputChunks.push(piece)
inputLength += piece.length
}
}
if (newline < 0) return
if (!discardingInput) {
const line = inputChunks.length === 1
? inputChunks[0]
: Buffer.concat(inputChunks, inputLength)
inputChunks = []
inputLength = 0
handleLine(line)
} else {
discardingInput = false
}
start = newline + 1
}
}
process.stdin.on("data", handleInput)
process.stdin.on("end", closeInput)
process.stdin.on("close", closeInput)
process.stdin.on("error", closeInput)
process.stdout.on("error", handleStdoutError)