pi-provider-antigravity2
v0.3.0
Published
Pi provider extension for Antigravity / Google Cloud Code Assist
Maintainers
Readme
pi-provider-antigravity2
English | 简体中文
An Antigravity (Google Cloud Code Assist v1internal) provider extension for Pi, enabling you to use your Antigravity subscription quota directly inside Pi for Gemini, Claude, and other supported models.
[!CAUTION] Disclaimer: This is an unofficial, community-maintained provider extension intended for educational and personal research purposes. Using unofficial clients against Google Cloud Code Assist / Antigravity may violate Google's Terms of Service and can result in quota loss or account restrictions. Use at your own risk. OAuth client credentials are not bundled in this repository; see OAuth Client Credentials below before running
/login antigravityor refreshing tokens.
🧠 Native Pi Core, Optional agy Setup
This extension is strictly a Pi provider, auth, and protocol adapter:
- ⚙️ Full Pi Core Agent Loop & Built-in Tools: All conversation turns, context management, reasoning, and native tool execution (
read,bash,edit,write, etc.) are 100% executed bypiitself. - Optional
agySetup: Pi runs conversations and refreshes tokens itself, with no third-party runtime npm dependencies. On Linux, the extension can runagyduring/login antigravity,/antigravity sync-client, or/antigravity import-agyto identify its OAuth client configuration. - 🌐 Direct Cloud Code Assist HTTP/SSE: Speaks directly to
https://daily-cloudcode-pa.googleapis.com/v1internalover nativefetch, supporting streaming responses,xhighthinking levels (e.g.thinkingLevel: 100), multi-turnthoughtSignaturepreservation, and cross-family tool history compatibility.
🔐 Authentication & Credential Management
The extension includes a complete Google OAuth PKCE login and credential lifecycle (in auth.ts):
🔑 OAuth Client Credentials (Relies on agy Client)
This package does not ship built-in Google OAuth client credentials. Interactive /login antigravity and Refresh Token renewal rely on the OAuth client credentials provided by a locally installed official agy (antigravity-cli) client (because Antigravity uses restricted Google internal scopes such as aicode, cclog, experimentsandconfigs and the https://antigravity.google/oauth-callback redirect URI, which a self-created GCP OAuth client cannot authorize).
Automatic Setup via /login antigravity (Recommended):
Every time you run /login antigravity:
- The extension runs
agy acp --loginin a temporary profile with stdin closed and readsclient_idfrom the official Google login URL. It does not open a browser or complete anagylogin. - The extension reads the local binary to find the matching Client Secret and build the HTTP User-Agent. If it cannot verify a fresh pair, it reuses the saved pair, or prompts for missing values. Explicit OAuth environment overrides still take precedence.
- Authorize Pi in your browser and paste the authorization code. After the token exchange succeeds, changed values are saved to
~/.pi/agent/pi-provider-antigravity.json. A failed or cancelled exchange does not replace saved values.
Manual Extraction / Configuration (Optional):
ANTIGRAVITY_CLIENT_ID: Runagylogin once and copy theclient_id=...query parameter from the Google OAuth URL opened in your browser (note:strings "$(which agy)"lists multiple bundled Google client IDs; pick the one shown inagy's login URL):strings "$(which agy)" | grep -oE '[0-9]+-[a-z0-9]+\.apps\.googleusercontent\.com' | sort -uANTIGRAVITY_CLIENT_SECRET:agyis a Go binary whose.rodataconcatenates strings without\0terminators, soGOCSPX-[A-Za-z0-9_-]+can swallow characters from the adjacent string. Match the exact 35-character format (GOCSPX-+ 28 chars) and probeoauth2.googleapis.com/tokenwith a dummy code to find the matching secret (invalid_grantmeans the pair matches;invalid_clientmeans it belongs to another bundled client):node -e ' const fs = require("node:fs"); const txt = fs.readFileSync(process.argv[1], "latin1"); const secrets = [...new Set(txt.match(/GOCSPX-[A-Za-z0-9_-]{28}/g) || [])]; (async () => { for (const s of secrets) { const r = await fetch("https://oauth2.googleapis.com/token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ client_id: process.argv[2], client_secret: s, code: "probe", grant_type: "authorization_code", redirect_uri: "https://antigravity.google/oauth-callback", }), }); const j = await r.json(); if (j.error === "invalid_grant") { console.log(s); break; } } })(); ' "$(which agy)" "YOUR_CLIENT_ID.apps.googleusercontent.com"
How to configure them (choose either option):
- Option 1 — Persistent config file (recommended, write once in
$PI_CODING_AGENT_DIR/pi-provider-antigravity.json, default~/.pi/agent/pi-provider-antigravity.json):{ "oauthClientId": "YOUR_CLIENT_ID.apps.googleusercontent.com", "oauthClientSecret": "GOCSPX-YOUR_CLIENT_SECRET" } - Option 2 — Environment variables (takes precedence over the config file):
export ANTIGRAVITY_CLIENT_ID="YOUR_CLIENT_ID.apps.googleusercontent.com" export ANTIGRAVITY_CLIENT_SECRET="GOCSPX-YOUR_CLIENT_SECRET"
[!NOTE] If you only place a pre-minted, still-valid
accesstoken (ya29..., withoutrefresh) inauth.json, inference works without OAuth client credentials until that access token expires (~1 hour), after which it cannot auto-refresh.
- Interactive Login: Run
/login antigravityin Pi. The extension generates anS256PKCE authorization URL and exchanges the pasted authorization code for OAuth tokens. - Automatic Project ID Resolution: Queries
v1internal:loadCodeAssistfor the account'scloudaicompanionProject, falling back safely to"aicode-consumers"if unavailable. - Refresh Token Normalization: Automatically strips accidental leading hyphens (such as
-1//...→1//...) and extra whitespace from refresh tokens. - Seamless Refresh & 401 Recovery:
- Packs
{ token, projectId }into the storedaccessfield and preserves known non-fallbackprojectIdvalues across refreshes. - During streaming, HTTP
401can trigger a single token refresh and retry.
- Packs
HTTP User-Agent Override
/login antigravity reads the version and build changelist (cl) from the local agy binary on disk; this step doesn't run agy. If both are recognized, it saves the resulting HTTP userAgent under the top level of $PI_CODING_AGENT_DIR/pi-provider-antigravity.json (default ~/.pi/agent/pi-provider-antigravity.json). This also runs when OAuth client credentials are already configured; rerun login after upgrading agy to refresh the value.
If agy is missing or its build metadata is unrecognized, login leaves the existing userAgent alone. You can set it manually to a single-line ASCII string. If unset, the extension uses the built-in default for your OS and architecture.
🚨 Why Does Pi's Unmodified System Prompt Trigger HTTP 429?
Wire captures, field-ablation tests, and character-level bisection confirmed that the Antigravity backend explicitly detects and rejects Pi's default system prompt signature:
- When a request carries top-level
"requestType": "agent"andrequest.systemInstructionmatches Pi's default harness phrasing:prefix[:43]("You are a coding assistant running inside p") → HTTP 200prefix[:44]("You are a coding assistant running inside pi") → Instant HTTP 429"running inside pi, a coding agent harness"→ Instant HTTP 429
- This triggers an instant synthetic
429 RESOURCE_EXHAUSTED(with noRetry-Afterheader and noquotaResetDelay/retryDelayin the error payload), rather than a real quota exhaustion.
✂️ How This Extension Adapts System Prompts (Only When Switched to antigravity)
To bypass the synthetic 429 check while preserving Pi's coding capabilities and context as much as possible, prompt adaptation is strictly scoped to outbound Antigravity requests:
1. 🔒 Strictly Isolated Scope
- Only when Pi is switched to the
antigravityprovider do requests flow throughpi.registerProvider("antigravity", { streamSimple })→streamAntigravity→buildAntigravityRequestBody(builder.ts), adaptingrequest.systemInstructionin the outbound JSON payload. - The
session_startandbefore_agent_starthooks are read/validate only — they never mutate Pi's in-memory system prompt. - When using OpenAI, Anthropic, or any other provider, Pi's system prompt is left completely untouched.
2. 🧩 Minimal Adaptation & Context Caveats
- Rewrites the opening
preambleand strips WAF triggers: Neutralizes phrases like"running inside pi..."and removes Pi's built-in<docs>boilerplate viasystemInstructionRewritesso Antigravity's proxy WAF accepts the prompt. - All other live Pi context remains intact:
<tools>,<rules>,<skills>,<cwd>, projectAGENTS.md, andAPPEND_SYSTEM.mdare still injected live on every turn — nothing goes stale from cached static prompts. - No Pi Docs (
<docs>) onantigravity: Because Pi's built-in<docs>section is stripped from outboundantigravitysystem instructions,antigravitymodels cannot see Pi's self-documentation. If you need to ask questions about Pi itself (usage, configuration, extensions, etc.), switch to another provider. - Synthetic 429 Auto-Recovery: If an unmatched prompt variant still triggers a synthetic 429 (no backoff metadata), stream.ts and waf-recovery.ts strip top-level
requestType, apply emergency sanitization, and if needed bisect the blockedsystemInstructionline(s) (cached in-memory for the current process without altering conversation history).
[!IMPORTANT] 💡 Customize
userModifiedin~/.pi/agent/pi-provider-antigravity-prompts.jsonOn your first session start, the extension generates a concise, human-readable prompt config file at
$PI_CODING_AGENT_DIR/pi-provider-antigravity-prompts.json(default~/.pi/agent/pi-provider-antigravity-prompts.json).
- Each prompt item is flat and self-contained, with three fields:
piOriginal: Pi's original text (read-only reference; only checked/updated when the file is created or whenpiVersionchanges).extensionBuiltIn: The extension's built-in default (read-only reference, synced from default-prompts.json).userModified: The only section active at runtime (initialized to matchextensionBuiltInas an editable placeholder).- Fields you can customize under
userModified:
prompts.preamble.userModified: Customize the opening system prompt sent to Antigravity models (for example, your preferred terminal assistant persona — avoid reintroducing"running inside pi"). Live<tools>,<rules>, and<cwd>sections are still appended dynamically.prompts.systemInstructionRewrites.rules[*].userModified: Edit fallback regex replacement text (patternappears once at the rule level; setuserModifiedtonullto disable a rule).prompts.searchSystemInstruction.userModified: Customize the system instruction sent to the background Gemini model forantigravity_websearch. The model comes from the cached Antigravity catalog'swebSearchModelIds, withgemini-3.5-flash-liteas a fallback when no search ID is available.prompts.webSearchToolDescription/webSearchToolSnippet/webSearchToolGuidelinesuserModified: Customize theantigravity_websearchtool description,<tools>snippet, and<rules>guidelines exposed to the main model (restart or/reloadPi for tool registration edits; all other edits take effect on the very next turn).- Safety & Reset: When
piVersionis unchanged, extracting Pi's prompt is skipped, while file integrity is still checked on startup without overwriting youruserModifiedcustomizations. Delete any key inuserModified(or delete the whole file) and restart Pi to reset it toextensionBuiltIn.
🛠️ Installation & Usage
📦 Install / Load
[!NOTE] If you previously installed the deprecated
pi-provider-antigravitypackage, remove it first (pi remove npm:pi-provider-antigravity) to avoid conflicts — both share theantigravityprovider ID,/antigravitycommand, and~/.pi/agent/pi-provider-antigravity*.jsonconfig paths.
# Option A: Install from npm (recommended)
pi install npm:pi-provider-antigravity2
# Option B: Try for a single session without installing
pi -e npm:pi-provider-antigravity2
# Option C: Load or symlink from a local clone
pi -e /path/to/repo
# or: ln -s /path/to/repo/pi-provider-antigravity2 ~/.pi/agent/extensions/pi-provider-antigravity2Log in, fetch the model list, then select an antigravity model:
/login antigravity
/antigravity refresh
/modelOnly /antigravity models and /antigravity refresh fetch the Antigravity catalog. Startup, /model, and /scoped-models use the last saved list. For a new headless installation, seed the cache with one of those commands in an interactive session or copy pi-provider-antigravity-models.json into the agent directory. Existing entries in Pi's models-store.json still work as a fallback.
Import an existing agy login (Linux)
Run /antigravity import-agy in Pi if you are already signed into local agy with a personal Google account. The command checks the current user's Secret Service using busctl, reads the unlocked entry with secret-tool (libsecret), or checks the private fallback file at ~/.gemini/antigravity-cli/antigravity-oauth-token when no keyring entry exists. It verifies token renewal before saving. Replacing an existing Pi login requires confirmation.
The command does not change the agy login or provider config. It saves the imported login and matching OAuth client pair in Pi's owner-only auth.json; explicit OAuth environment overrides still take precedence. This unofficial import may stop working if agy changes its credential format. No sync-client is needed after a successful import. If no Antigravity models are cached, run /antigravity refresh and then select one with /model. If models are already available, go straight to /model.
🪪 Headless Login with a Refresh Token (auth.json)
If you already have a Google OAuth refresh token (for headless servers, containers, or migrated credentials), you can skip browser login by writing directly to $PI_CODING_AGENT_DIR/auth.json (default ~/.pi/agent/auth.json):
{
"antigravity": {
"type": "oauth",
"refresh": "1//03_your_refresh_token_here",
"access": "",
"expires": 0
}
}type: Must be"oauth".refresh: Your Google OAuth refresh token (1//...; accidental leading-1//...prefixes are automatically normalized).access&expires: Leave as""and0. When a request needs credentials, Pi exchangesrefreshfor anaccess_tokenand the extension resolvesprojectIdautomatically.- Restrict file permissions to owner-only:
chmod 600 ~/.pi/agent/auth.json.
Idle disconnect
After 90 seconds idle at startup or after an agent run settles (including errors and Escape), the extension aborts outstanding Antigravity requests and blocks background traffic, including quota refresh and cache warming. With an Antigravity model selected, Pi displays pi-provider-antigravity2: disconnected (AFK) above the editor. Sending a new prompt or running an /antigravity command resumes access on demand; no login is required. Other providers are unaffected.
💬 Built-in Commands & Tool
/antigravity usage: Show remaining 5h & weekly quota bars and reset timers/antigravity models: Refresh and display recommended/available models with context window and remaining quota/antigravity refresh: Refresh the same catalog without displaying the list/antigravity sync-client: Sync OAuth Client ID, Secret, and User-Agent from localagy. If the Client ID changed and Pi is already logged in, the command can update the User-Agent but keeps the existing OAuth pair; run/login antigravityto switch./antigravity import-agy: Import the current localagypersonal OAuth login on Linux without opening a browser./antigravity settings: Interactive settings dialog (persisted to~/.pi/agent/pi-provider-antigravity.json):Quota footer("off"/"smart"/"all"): Status footer quota display modeUnsigned tool calls("fail"/"skip"): Handling policy when cross-provider history loses athoughtSignature
/antigravity websearch <query>&antigravity_websearchtool: Native HTTP Google Search Grounding with inline source citations. Responses without web sources are reported as search failures.
🧪 Development & Offline Tests
All source modules use erasable TypeScript with explicit .ts imports and zero runtime external dependencies, running directly on Node v22.19+:
npm test
# Equivalent to: node --experimental-strip-types --test tests/*.test.ts📁 Project Structure
- pi-provider-antigravity2/index.ts — Pi extension entrypoint, provider, command, and tool registration
- pi-provider-antigravity2/auth.ts — OAuth PKCE flow, token refresh, and
loadCodeAssistproject resolution - pi-provider-antigravity2/protocol.ts — Cloud Code HTTP/SSE client and structured HTTP errors
- pi-provider-antigravity2/model-identity.ts & model-catalog.ts — Model family detection, catalog refresh,
xhighthinking config, and cold-start bootstrap - pi-provider-antigravity2/default-prompts.json & prompts.ts — Externalized default prompts and
pi-provider-antigravity-prompts.jsonlifecycle management - pi-provider-antigravity2/builder.ts — Outbound system instruction adaptation, tool schema cleanup, multi-turn translation, and signature handling
- pi-provider-antigravity2/sse.ts & stream.ts — Incremental SSE parsing, synthetic 429 fallback, and 401 recovery
- pi-provider-antigravity2/config.ts, settings.ts, quota-status.ts, search.ts, commands.ts — Config persistence, quota footer, web search tool, and
/antigravitycommand router
