agy-cli 0.1.68: bring in the retired Antigravity plugin's accounts
A user asked on X (@tongzhenbao) to move the accounts signed in on the
retired Antigravity plugin (provider "antigravity") into agy-cli in one
click. That plugin signed in with Antigravity's own Google OAuth client
(1071006060591-tmhssin2h21lcre235vtolojh4g403ep), and agy's own
`agy acp --login` link uses the same client id, so agy can take those
refresh tokens as its own: nothing is sent anywhere agy doesn't already
send its sign-in, and no other client is spoken for.
auth.import (magpie's "Import accounts from a file…") now takes:
- the old plugin's saved oauth entry, one, a list or plugin-auth.json's
map of them (refresh "<token>|<project>", accountId = email);
- agy's own token file / keyring value (go-keyring-base64: prefix too).
A bare refresh token or another tool's export is refused, since it may be
another client's. Each account gets its own agy home with the token file
agy writes (0600), and is added only once `agy models` runs there; one agy
refuses leaves no home. An account already signed in (same email) gives
its existing key back.
Run:
- mote: packages/agy-cli `HOME=$(mktemp -d) bun test`: smoke, signin,
cloak, install all pass; test/usage.mjs red (3 of 30, different each
run) and red the same way on pristine origin/main e5cb9598 (8 and 9
of 30, two runs on mote), green on ubuntu CI; filed separately.
- the new smoke checks are red on the old index.mjs ("plugin.auth.import
is not a function").
- bun scripts/check.mjs: ok.
- real account, sandbox HOME: the owner's live old-plugin entry copied
into a sandbox .gemini/antigravity-cli/antigravity-oauth-token;
`SSH_CONNECTION=… agy models` listed the models and agy refreshed the
file in the same shape (refresh token not rotated).
Not tried: the import through a running magpie with a real account
(magpie's Providers-page Move comes in a magpie release).
This commit is contained in:
@@ -182,6 +182,12 @@ After installing, sign in from Magpie; no terminal needed.
|
||||
and agy keeps its sign-in in a file there, leaving the account in the
|
||||
system keyring as it is. Each account has its own row and its own usage,
|
||||
and is switched and rotated like any provider's accounts.
|
||||
Accounts signed in on the retired Antigravity plugin (`antigravity`) move
|
||||
over from the line Magpie shows over its Providers list (Move), or from
|
||||
agy-cli's Import accounts from a file…, which takes that plugin's sign-in or
|
||||
agy's own token file. They are Antigravity's own Google sign-ins: each gets
|
||||
an agy home of its own and is added once agy signs in with it, and the
|
||||
credential goes nowhere else.
|
||||
2. Refer to models as `agy-cli/<model>`. When agy lists separate model IDs
|
||||
for different thinking levels, they are grouped here as variants of one model,
|
||||
and each request is mapped back to the matching agy ID.
|
||||
|
||||
@@ -128,6 +128,10 @@ magpie plugin add @magpie-community/opencode-agy-cli-auth
|
||||
Google 账号」。新账号有自己的 agy 主目录(`~/.local/state/magpie-agy-bridge/accounts/`
|
||||
下),它的登录保存在那个目录里的文件中,不动系统钥匙串里原来那个账号。每个账号单独一行,
|
||||
用量分开显示,可以像其他 provider 的账号一样切换和轮换。
|
||||
用过已退役的 Antigravity 插件(`antigravity`)的话,Magpie 的 Providers 页顶上会提示把那里的
|
||||
账号移过来,点 Move 即可;也可以在 agy-cli 的 Import accounts from a file… 里选那份登录记录
|
||||
或 agy 的令牌文件。它们本就是 Antigravity 自己的 Google 登录,每个账号放进自己的 agy 主目录,
|
||||
由 agy 检查通过后才加入,凭据不会发到别处。
|
||||
2. 模型名写成 `agy-cli/<模型>`。agy 对同一模型不同思考等级给出的多个模型 ID,
|
||||
在这里会合并成一个模型的多个变体,请求时再映射回对应的 agy ID。
|
||||
3. 在客户端里选用这个模型即可。
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
// Accounts brought in from a sign-in made with Antigravity's own Google
|
||||
// OAuth client, so agy, which signs in with that same client, takes them
|
||||
// as its own: nothing is sent anywhere but where agy already sends its
|
||||
// sign-in (Google), and no other client is spoken for.
|
||||
//
|
||||
// Two kinds are taken, and only these, since each is known to come from
|
||||
// that client:
|
||||
// - the retired Antigravity plugin's (@magpie-community/opencode-
|
||||
// antigravity-auth, provider "antigravity") saved sign-in: an oauth
|
||||
// entry whose refresh is "<refresh token>|<Code Assist project>", with
|
||||
// the access token, its expiry (ms) and the email as accountId; one,
|
||||
// a list, or a map of them (magpie's plugin-auth.json);
|
||||
// - agy's own stored sign-in, the JSON of its token file
|
||||
// ({"token":{access_token, refresh_token, expiry, …}, "id_token_email"}).
|
||||
// A refresh token from anywhere else (another tool's export) may be another
|
||||
// client's, so it isn't taken.
|
||||
//
|
||||
// Each account becomes the value agy keeps in its token file
|
||||
// (.gemini/antigravity-cli/antigravity-oauth-token), the same JSON agy
|
||||
// writes: agy renews the access token itself on its first run.
|
||||
|
||||
function str(v) {
|
||||
return typeof v === "string" ? v.trim() : ""
|
||||
}
|
||||
|
||||
// fromPlugin is the account a retired Antigravity plugin's entry holds, or
|
||||
// null when the value isn't one.
|
||||
function fromPlugin(v) {
|
||||
if (!v || typeof v !== "object" || v.type !== "oauth" || typeof v.refresh !== "string") return null
|
||||
const refresh = v.refresh.split("|")[0].trim()
|
||||
if (!refresh) return null
|
||||
const expires = Number(v.expires)
|
||||
return {
|
||||
email: str(v.accountId) || str(v.metadata?.email),
|
||||
refresh,
|
||||
access: str(v.access),
|
||||
expires: Number.isFinite(expires) && expires > 0 ? expires : 0,
|
||||
}
|
||||
}
|
||||
|
||||
// fromAgy is the account agy's own stored sign-in holds, or null.
|
||||
function fromAgy(v) {
|
||||
const t = v?.token
|
||||
if (!t || typeof t !== "object" || typeof t.refresh_token !== "string" || !t.refresh_token.trim()) return null
|
||||
const at = Date.parse(t.expiry ?? "")
|
||||
return {
|
||||
email: str(v.id_token_email),
|
||||
refresh: t.refresh_token.trim(),
|
||||
access: str(t.access_token),
|
||||
expires: Number.isFinite(at) && at > 0 ? at : 0,
|
||||
}
|
||||
}
|
||||
|
||||
function decode(text) {
|
||||
let value = String(text ?? "").trim()
|
||||
if (value.startsWith("go-keyring-base64:")) value = Buffer.from(value.slice(18), "base64").toString("utf8")
|
||||
try { return JSON.parse(value) } catch { return undefined }
|
||||
}
|
||||
|
||||
// importedAccounts is every account the texts hold, one per refresh token,
|
||||
// in order: [{ email, refresh, access, expires }].
|
||||
export function importedAccounts(texts) {
|
||||
const out = []
|
||||
const seen = new Set()
|
||||
const add = (a) => {
|
||||
if (!a || seen.has(a.refresh)) return
|
||||
seen.add(a.refresh)
|
||||
out.push(a)
|
||||
}
|
||||
const walk = (v, depth) => {
|
||||
if (!v || typeof v !== "object" || depth > 2) return
|
||||
const a = fromPlugin(v) ?? fromAgy(v)
|
||||
if (a) return add(a)
|
||||
for (const x of Array.isArray(v) ? v : Object.values(v)) walk(x, depth + 1)
|
||||
}
|
||||
for (const text of Array.isArray(texts) ? texts : [texts]) walk(decode(text), 0)
|
||||
return out
|
||||
}
|
||||
|
||||
// tokenFileText is what agy keeps in its token file for the account a.
|
||||
export function tokenFileText(a) {
|
||||
return JSON.stringify({
|
||||
token: {
|
||||
access_token: a.access,
|
||||
token_type: "Bearer",
|
||||
refresh_token: a.refresh,
|
||||
// with no expiry said, the access token is taken as ended (not Go's
|
||||
// zero time, which agy reads as "never ends"): agy renews it first
|
||||
expiry: new Date(a.expires || 0).toISOString(),
|
||||
},
|
||||
auth_method: "consumer",
|
||||
...(a.email ? { id_token_email: a.email } : {}),
|
||||
})
|
||||
}
|
||||
|
||||
// IMPORT_FROM says what magpie's "Import accounts from a file…" takes.
|
||||
export const IMPORT_FROM = {
|
||||
en: "Choose or paste a sign-in from the retired Antigravity plugin, or agy's own token file (antigravity-oauth-token). They are Antigravity's own Google sign-ins, which agy takes as they are; each account is checked with agy before it is added.",
|
||||
froms: {
|
||||
zh: "选择或粘贴已退役的 Antigravity 插件的登录记录,或 agy 自己的令牌文件(antigravity-oauth-token)。它们本就是 Antigravity 自己的 Google 登录,agy 可以直接使用;每个账号先由 agy 检查再添加。",
|
||||
"zh-TW": "選擇或貼上已退役的 Antigravity 外掛的登入記錄,或 agy 自己的權杖檔(antigravity-oauth-token)。它們本就是 Antigravity 自己的 Google 登入,agy 可以直接使用;每個帳號先由 agy 檢查再加入。",
|
||||
ja: "引退した Antigravity プラグインのサインイン、または agy 自身のトークンファイル(antigravity-oauth-token)を選ぶか貼り付けてください。どちらも Antigravity 自身の Google サインインなので、agy はそのまま使えます。各アカウントは追加前に agy で確認します。",
|
||||
},
|
||||
}
|
||||
@@ -12,7 +12,8 @@ import { storeCatalogRows } from "./model-ids.mjs"
|
||||
import { backgroundBlocked, cannotSignIn, forgetStoredSignIn, storedEmail, storedSignInNow } from "./signin.mjs"
|
||||
import { sanitizePatternFields } from "./re2-pattern.mjs"
|
||||
import { startLogin } from "./login.mjs"
|
||||
import { currentHome, dropHome, homeIdOfAuth, homeOf, inAccount, newHome } from "./account.mjs"
|
||||
import { accountsDir, currentHome, dropHome, homeIdOfAuth, homeOf, inAccount, newHome, tokenFile } from "./account.mjs"
|
||||
import { IMPORT_FROM, importedAccounts, tokenFileText } from "./import.mjs"
|
||||
import { AGY_MANUAL, installAgy, keepAgyCurrent } from "./install.mjs"
|
||||
import { ensureGeminiArrayItems } from "./array-items.mjs"
|
||||
import { expandToolSchema } from "./tool-schema.mjs"
|
||||
@@ -5951,6 +5952,64 @@ async function googleSignIn(agy, account = null) {
|
||||
}
|
||||
}
|
||||
|
||||
// homeOfEmail is the id of the account home already signed in to email
|
||||
// ("" for agy's own sign-in), or null when none is.
|
||||
async function homeOfEmail(email) {
|
||||
const want = email.toLowerCase()
|
||||
if ((await storedEmail({ home: null })).toLowerCase() === want) return ""
|
||||
let ids = []
|
||||
try { ids = fs.readdirSync(accountsDir()) } catch {}
|
||||
for (const id of ids) {
|
||||
const home = homeOf(id)
|
||||
if (home && (await storedEmail({ home })).toLowerCase() === want) return id
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// importSignIns brings in the accounts the texts hold (import.mjs): each
|
||||
// one an account home of its own, whose token file is the sign-in, kept
|
||||
// once agy lists the account's models with it. An account signed in here
|
||||
// already is left as it is and named again, so it isn't added twice.
|
||||
async function importSignIns(texts) {
|
||||
const accounts = importedAccounts(texts)
|
||||
if (!accounts.length) return { type: "failed", error: "no Antigravity sign-in in it: take the retired Antigravity plugin's sign-in or agy's token file" }
|
||||
const agy = findAgy()
|
||||
if (!agy) return noAgy()
|
||||
const out = []
|
||||
for (const a of accounts) {
|
||||
const failed = (error) => ({ type: "failed", error, ...(a.email ? { accountId: a.email } : {}) })
|
||||
if (a.email) {
|
||||
const had = await homeOfEmail(a.email)
|
||||
if (had !== null) {
|
||||
out.push(await inAccount(homeOf(had), () => signedInAs(had)))
|
||||
continue
|
||||
}
|
||||
}
|
||||
let account
|
||||
try {
|
||||
account = newHome()
|
||||
const file = tokenFile(account.home)
|
||||
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 })
|
||||
fs.writeFileSync(file, tokenFileText(a), { mode: 0o600 })
|
||||
} catch (error) {
|
||||
if (account) dropHome(account.home)
|
||||
out.push(failed(`couldn't make a folder for the account: ${error.message}`))
|
||||
continue
|
||||
}
|
||||
out.push(await inAccount(account.home, async () => {
|
||||
forgetStoredSignIn()
|
||||
forgetOffered()
|
||||
const probe = await probeSignedIn(agy)
|
||||
if (!probe.ok) {
|
||||
dropHome(account.home)
|
||||
return failed(`agy couldn't sign in with it: ${probe.why}`)
|
||||
}
|
||||
return signedInAs(account.id)
|
||||
}))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// homeOfAuth is the home of the account a saved sign-in is, null for
|
||||
// agy's own.
|
||||
async function homeOfAuth(getAuth) {
|
||||
@@ -5988,6 +6047,11 @@ export const AntigravityBridgePlugin = async () => {
|
||||
},
|
||||
// Magpie's usage hook; OpenCode ignores this extension. agy owns auth.
|
||||
usage: async (getAuth) => readUsageOf(await homeOfAuth(getAuth)),
|
||||
// magpie's "Import accounts from a file…" (OpenCode ignores it): the
|
||||
// retired Antigravity plugin's sign-ins, which magpie's Move hands over
|
||||
importFrom: IMPORT_FROM.en,
|
||||
importFroms: IMPORT_FROM.froms,
|
||||
import: (texts) => importSignIns(Array.isArray(texts) ? texts : [texts]),
|
||||
methods: [
|
||||
{
|
||||
// agy's own Google sign-in (`agy acp --login`): magpie opens its
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-agy-cli-auth",
|
||||
"version": "0.1.67",
|
||||
"version": "0.1.68",
|
||||
"description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
@@ -21,6 +21,7 @@
|
||||
"model-ids.mjs",
|
||||
"signin.mjs",
|
||||
"account.mjs",
|
||||
"import.mjs",
|
||||
"README.md",
|
||||
"README.en.md",
|
||||
"LICENSE",
|
||||
|
||||
@@ -90,6 +90,13 @@ if (argv[0] === "acp" && argv.includes("--login")) {
|
||||
process.stderr.write('couldn\'t log in: token exchange failed: oauth2: "invalid_grant" "Bad Request"\n')
|
||||
process.exit(1)
|
||||
}
|
||||
// an account home whose token file Google turned away (a revoked sign-in)
|
||||
const homeToken = path.join(where.home, ".gemini", "antigravity-cli", "antigravity-oauth-token")
|
||||
if (argv.includes("models") && where.ssh && fs.existsSync(homeToken) && fs.readFileSync(homeToken, "utf8").includes("1//revoked")) {
|
||||
log({ type: "models", revoked: true })
|
||||
process.stderr.write('Error: oauth2: "invalid_grant" "Token has been expired or revoked."\n')
|
||||
process.exit(1)
|
||||
}
|
||||
if (argv.includes("models") && fs.existsSync(signedOut)) {
|
||||
log({ type: "models", update: process.env.AGY_CLI_DISABLE_AUTO_UPDATE, signedOut: true })
|
||||
process.stdout.write("Fetching available models...\n")
|
||||
@@ -838,6 +845,53 @@ const legacyModel = "agy-cli/gemini-test-low"
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// tongzhenbao on X: the retired Antigravity plugin's accounts move here
|
||||
// in one go. Its sign-ins were made with Antigravity's own Google client,
|
||||
// the one agy signs in with, so each becomes an account home whose token
|
||||
// file is that sign-in, kept once agy lists its models with it.
|
||||
const savedState = process.env.XDG_STATE_HOME
|
||||
const xdg = path.join(stateDir, "xdg-import")
|
||||
process.env.XDG_STATE_HOME = xdg
|
||||
const accounts = path.join(xdg, "magpie-agy-bridge", "accounts")
|
||||
try {
|
||||
check("accounts can be brought in from a file", typeof plugin.auth.import === "function" && /Antigravity/.test(plugin.auth.importFrom) && !!plugin.auth.importFroms?.zh)
|
||||
const old = {
|
||||
antigravity: { type: "oauth", refresh: "1//moved-a|proj-a", access: "ya29.a", expires: 1700000000000, accountId: "moved-a@example.com" },
|
||||
"antigravity#2": { type: "oauth", refresh: "1//revoked|proj-b", access: "", expires: 0, accountId: "revoked@example.com" },
|
||||
}
|
||||
const got = await plugin.auth.import([JSON.stringify(old), JSON.stringify({ type: "oauth", refresh: "1//moved-a|proj-a", accountId: "moved-a@example.com" })])
|
||||
check("each account is tried once", Array.isArray(got) && got.length === 2, JSON.stringify(got))
|
||||
const [moved, revoked] = got
|
||||
check("a moved sign-in is kept as an account home, named by its email",
|
||||
moved?.type === "success" && moved.key === `agy:${moved.metadata?.agyHome}` && moved.metadata?.email === "moved-a@example.com", JSON.stringify(moved))
|
||||
const home = path.join(accounts, moved?.metadata?.agyHome ?? "none")
|
||||
const file = path.join(home, ".gemini", "antigravity-cli", "antigravity-oauth-token")
|
||||
const kept = JSON.parse(fs.readFileSync(file, "utf8"))
|
||||
check("its token file is agy's own shape, with the plugin's refresh token and no project",
|
||||
kept.token?.refresh_token === "1//moved-a" && kept.token.access_token === "ya29.a" && kept.token.expiry === new Date(1700000000000).toISOString() && kept.auth_method === "consumer" && kept.id_token_email === "moved-a@example.com", JSON.stringify(kept))
|
||||
if (process.platform !== "win32") check("the token file is the user's alone", (fs.statSync(file).mode & 0o077) === 0)
|
||||
const probe = logs().filter(event => event.type === "models" && event.home === home)
|
||||
check("agy was asked with it in the account's home, from the file", probe.length >= 1 && probe.every(event => event.ssh === "127.0.0.1 0 127.0.0.1 0"), JSON.stringify(probe))
|
||||
check("a sign-in agy can't use is turned away and leaves no home",
|
||||
revoked?.type === "failed" && revoked.accountId === "revoked@example.com" && /invalid_grant/.test(revoked.error) && fs.readdirSync(accounts).length === 1, JSON.stringify(revoked))
|
||||
|
||||
const again = await plugin.auth.import([JSON.stringify(old.antigravity)])
|
||||
check("moved twice, an account is named again, not added twice",
|
||||
again?.[0]?.type === "success" && again[0].key === moved.key && fs.readdirSync(accounts).length === 1, JSON.stringify(again))
|
||||
|
||||
const agyOwn = JSON.stringify({ token: { access_token: "ya29.c", refresh_token: "1//own-c", expiry: "2026-01-01T00:00:00Z", token_type: "Bearer" }, auth_method: "consumer", id_token_email: "own-c@example.com" })
|
||||
const fromFile = await plugin.auth.import(["go-keyring-base64:" + Buffer.from(agyOwn).toString("base64")])
|
||||
check("agy's own stored sign-in is taken too", fromFile?.[0]?.type === "success" && fromFile[0].metadata?.email === "own-c@example.com", JSON.stringify(fromFile))
|
||||
|
||||
const other = await plugin.auth.import([JSON.stringify({ refresh_token: "1//someone-else", client_id: "x.apps.googleusercontent.com" }), "1//bare-token"])
|
||||
check("a refresh token that may be another client's isn't taken", other?.type === "failed" && /no Antigravity sign-in/.test(other.error), JSON.stringify(other))
|
||||
} finally {
|
||||
if (savedState === undefined) delete process.env.XDG_STATE_HOME
|
||||
else process.env.XDG_STATE_HOME = savedState
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// signed out: agy's own sign-in gives the link and takes the pasted code
|
||||
const signedOut = path.join(stateDir, "signed-out")
|
||||
|
||||
@@ -42,6 +42,7 @@ export const TOOL_SCHEMA_FILE = path.join(REPO_DIR, "tool-schema.mjs")
|
||||
export const TOOL_BUDGET_FILE = path.join(REPO_DIR, "tool-budget.mjs")
|
||||
export const INSTALL_FILE = path.join(REPO_DIR, "install.mjs")
|
||||
export const ACCOUNT_FILE = path.join(REPO_DIR, "account.mjs")
|
||||
export const IMPORT_FILE = path.join(REPO_DIR, "import.mjs")
|
||||
export const MESSAGES_URL = "agy://local/v1/messages"
|
||||
export const COUNT_TOKENS_URL = "agy://local/v1/messages/count_tokens"
|
||||
export const CATALOG_TTL_MS = 5 * 60 * 1000
|
||||
@@ -549,6 +550,7 @@ export async function createBridgeKit(options = {}) {
|
||||
const toolBudgetUrl = pathToFileURL(TOOL_BUDGET_FILE).href
|
||||
const installUrl = pathToFileURL(INSTALL_FILE).href
|
||||
const accountUrl = pathToFileURL(ACCOUNT_FILE).href
|
||||
const importUrl = pathToFileURL(IMPORT_FILE).href
|
||||
const linker = (specifier) => {
|
||||
const key = specifier.startsWith("node:") || specifier.startsWith("./") || specifier === RUNTIME_SPECIFIER
|
||||
? specifier
|
||||
@@ -568,6 +570,7 @@ export async function createBridgeKit(options = {}) {
|
||||
else if (key === "./tool-budget.mjs") module = sourceModule(TOOL_BUDGET_FILE, "", toolBudgetUrl)
|
||||
else if (key === "./install.mjs") module = sourceModule(INSTALL_FILE, "", installUrl)
|
||||
else if (key === "./account.mjs") module = sourceModule(ACCOUNT_FILE, "", accountUrl)
|
||||
else if (key === "./import.mjs") module = sourceModule(IMPORT_FILE, "", importUrl)
|
||||
else if (key.startsWith("node:")) module = builtinModule(key, overrides[key])
|
||||
else throw new Error(`testkit: 未登记的依赖 ${specifier}`)
|
||||
cache.set(key, module)
|
||||
|
||||
Reference in New Issue
Block a user