Files
yetone 9907c187b2 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).
2026-11-16 01:29:08 +08:00

105 lines
4.8 KiB
JavaScript

// 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 で確認します。",
},
}