tongzhenbao on X wanted several accounts with their usage, as the old built-in had. agy keeps one sign-in where it looks for it (the system keyring's single "gemini"/"antigravity" entry), so the plugin was single: true and its second sign-in way replaced the account. Now the first account is agy's own, unchanged, and "Add another Google account" signs another one in to an agy whose home is the account's own (XDG_STATE_HOME/magpie-agy-bridge/accounts/<id>). Every agy run for that account (sign-in, models, usage, requests) gets HOME (USERPROFILE on Windows) there and SSH_CONNECTION set, which agy 1.5 reads as an SSH session and so keeps the sign-in in .gemini/antigravity-cli/ antigravity-oauth-token under that home rather than the keyring. The account's home is saved in its metadata (agyHome); the loader, usage and models run its work in an AsyncLocalStorage scope that childEnv and the stored sign-in read follow. Its conversations are keyed apart, and it gets no --gemini-dir (its token is under its own .gemini). single: true is gone, so each account has its own row and usage and rotates like any other. Checked by hand before writing it, with real agy and planted (expired) tokens in sandbox homes: agy read the file token from the sandbox HOME on macOS (mote) with SSH_CONNECTION set, on Windows (xiaomi-gpu) with USERPROFILE set, and in WSL. The live keyring was never touched. Run: - packages/agy-cli `HOME=$(mktemp -d) bun test` on mote: 35 pass, 0 fail. - bun scripts/check.mjs on mote: ok. - test/smoke.mjs on Windows (win-run): ALL OK (369 checks). - Broken by hand: the new smoke checks against the old index.mjs, signin.mjs and platform.mjs: 6 FAILURES (the method, single, the home, SSH_CONNECTION, the saved account, the failed home's cleanup). Not tried: a real Google sign-in of a second account, so agy writing the token file under SSH detection is known from file-mode users (plugins#1165) rather than seen here. Signing an added account in again makes a new home and leaves the old one (~800K). Every account shares one model catalog.
82 lines
3.1 KiB
JavaScript
82 lines
3.1 KiB
JavaScript
import { AsyncLocalStorage } from "node:async_hooks"
|
|
import { randomBytes } from "node:crypto"
|
|
import fs from "node:fs"
|
|
import os from "node:os"
|
|
import path from "node:path"
|
|
|
|
// More than one Google account. agy keeps one sign-in where it looks for
|
|
// it: the system keyring (one entry, "gemini"/"antigravity"), or, where it
|
|
// uses none, the file .gemini/antigravity-cli/antigravity-oauth-token under
|
|
// its home folder. So the first account is agy's own, as it always was,
|
|
// and every other account gets a home folder of its own: its agy runs
|
|
// (sign-in, models, usage, requests) are started with HOME (USERPROFILE on
|
|
// Windows) there, and with SSH_CONNECTION set, which agy reads as "a
|
|
// session over SSH" and so keeps the sign-in in that file rather than in
|
|
// the keyring every account would share. agy 1.5 does so on macOS, Windows
|
|
// and Linux (WSL and no-D-Bus Linux use the file already).
|
|
//
|
|
// The account's home is in its saved sign-in (metadata.agyHome, a folder
|
|
// name under accountsDir), and the work done for it runs in accountScope,
|
|
// which childEnv (platform.mjs) and the stored sign-in's read (signin.mjs)
|
|
// follow.
|
|
|
|
export const accountScope = new AsyncLocalStorage()
|
|
|
|
const HOME_ID = /^[a-z0-9]{8,32}$/
|
|
|
|
// accountsDir is where the other accounts' homes are kept.
|
|
export function accountsDir(env = process.env) {
|
|
const base = env.XDG_STATE_HOME || path.join(os.homedir(), ".local", "state")
|
|
return path.join(base, "magpie-agy-bridge", "accounts")
|
|
}
|
|
|
|
// homeOf is the folder of the account home id names, or null when id
|
|
// isn't one (never a path out of accountsDir).
|
|
export function homeOf(id, env = process.env) {
|
|
return typeof id === "string" && HOME_ID.test(id) ? path.join(accountsDir(env), id) : null
|
|
}
|
|
|
|
// homeIdOfAuth is the home a saved sign-in names, "" for agy's own.
|
|
export function homeIdOfAuth(auth) {
|
|
const id = auth?.metadata?.agyHome
|
|
return typeof id === "string" && HOME_ID.test(id) ? id : ""
|
|
}
|
|
|
|
// currentHome is the home of the account the current work is for, null
|
|
// for agy's own.
|
|
export function currentHome() {
|
|
return accountScope.getStore()?.home ?? null
|
|
}
|
|
|
|
// inAccount runs fn for the account at home (null: agy's own).
|
|
export function inAccount(home, fn) {
|
|
return accountScope.run({ home: home || null }, fn)
|
|
}
|
|
|
|
export function tokenFile(home) {
|
|
return path.join(home, ".gemini", "antigravity-cli", "antigravity-oauth-token")
|
|
}
|
|
|
|
// accountEnv is what an agy for the account at home is started with.
|
|
export function accountEnv(home, platform = process.platform) {
|
|
return {
|
|
HOME: home,
|
|
...(platform === "win32" ? { USERPROFILE: home } : {}),
|
|
// agy keeps its sign-in in tokenFile(home), not the keyring
|
|
SSH_CONNECTION: "127.0.0.1 0 127.0.0.1 0",
|
|
}
|
|
}
|
|
|
|
// newHome makes a home for an account being added: { id, home }.
|
|
export function newHome(env = process.env) {
|
|
const id = randomBytes(8).toString("hex")
|
|
const home = homeOf(id, env)
|
|
fs.mkdirSync(home, { recursive: true, mode: 0o700 })
|
|
return { id, home }
|
|
}
|
|
|
|
export function dropHome(home) {
|
|
if (!home) return
|
|
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
}
|