opencode-google-antigravity-auth

repository·main·Indexed 18 days ago

https://github.com/shekohex/opencode-google-antigravity-auth

An OAuth plugin for the Opencode CLI (v0.2.15) that enables authentication with Google Antigravity (Cloud Code) accounts. It allows users to access Gemini and Claude models using existing Google quotas, featuring multi-account load balancing to bypass rate limits, a google_search tool for real-time web retrieval, and family-independent caching for managing reasoning blocks when switching between model families.

Tokens
13.1K
Snippets
47
Records
61
Agent score
64%

What's inside opencode-google-antigravity-auth

  1. Configure Multi-Account Load Balancing

    main

    The plugin can automatically rotate between multiple Google accounts to bypass rate limits (429 errors) or server errors (5xx).

    Setup: During the opencode auth login process, you will be prompted to add additional accounts. You can configure up to 10 accounts.

    Storage: Account metadata is stored in $XDG_DATA_HOME/opencode/antigravity-accounts.json (e.g., ~/.local/share/opencode/antigravity-accounts.json).

    Behavior:

    • Sticky Selection: The plugin uses the same account for all requests until an error occurs.
    • Automatic Rotation: Upon hitting a rate limit or server error, it switches to the next configured account.
    • Smart Recovery: Accounts are automatically re-enabled once the rate limit timeout expires.
    • Monitoring: The plugin logs switches to the console (e.g., [INFO] Using account 1/3 (user@gmail.com)) and shows toast notifications.
  2. How cross-model thinking blocks are managed

    main

    The plugin allows switching between Claude and Gemini models within the same conversation while managing 'thinking' (reasoning) blocks via family-independent caching.

    The Mechanism

    Because thinking block signatures are provider-specific, they cannot be shared across families. The plugin uses:

    1. Family Isolation: Signatures are cached per model family (claude or gemini).
    2. Request Restoration: When switching back to a previously used family, thinking blocks are restored from that family's cache.
    3. Foreign Removal: When switching to a new family, thinking blocks from the previous family that are not in the current family's cache are removed to prevent signature validation errors.

    Behavior Summary

    TransitionThinking Preserved
    Claude $\rightarrow$ Claude✅ All Claude thinking
    Gemini $\rightarrow$ Gemini✅ All Gemini thinking
    Claude $\rightarrow$ Gemini❌ Claude thinking removed
    Gemini $\rightarrow$ Claude❌ Gemini thinking removed

    Note: Only the thinking blocks are removed; the actual conversation text is preserved during transitions.

  3. Test the Antigravity plugin installation

    main

    Follow these steps to verify your installation:

    1. Install the plugin locally or via the registry.
    2. Run opencode auth login and select Antigravity.
    3. Complete the browser authentication flow.
    4. Run a model request using the opencode run command.
    5. Verify that the response succeeds without prompting for an API key.
    opencode run -m google/gemini-2.5-flash -p "hello"
    # or
    opencode run -m google/gemini-3-pro-high -p "solve this"
  4. Install the Antigravity OAuth Plugin for Opencode

    main

    To use Antigravity-backed Gemini models with your existing quota in the Opencode CLI, add the plugin to your Opencode configuration file.

    1. Update your Opencode config (e.g., ~/.config/opencode/config.json) to include the plugin:

    2. Run the authentication command: opencode auth login

    3. When prompted by the provider list, select OAuth with Google (Antigravity).

    Note on Authentication: The plugin uses a local callback listener at http://localhost:36742/oauth-callback. If this port is unavailable or you are in a headless environment, the CLI will automatically switch to a manual copy/paste flow.

    {
      "$schema": "https://opencode.ai/config.json",
      "plugin": ["opencode-google-antigravity-auth"]
    }
  5. Configure Opencode with Antigravity and Google Models

    main

    To use the Antigravity plugin, add opencode-google-antigravity-auth to your plugin array in opencode.json. You must also define the google provider and its associated models (e.g., Gemini 3 Pro, Gemini 3 Flash, Claude Sonnet 4.5) within the provider block to specify capabilities like reasoning, limits, costs, and modalities.

    Note that the modalities configuration is required to enable specific input types like images.

    {
      "$schema": "https://opencode.ai/config.json",
      "plugin": ["opencode-google-antigravity-auth"],
      "provider": {
        "google": {
          "npm": "@ai-sdk/google",
          "models": {
            "gemini-3-pro-preview": {
              "id": "gemini-3-pro-preview",
              "name": "Gemini 3 Pro",
              "release_date": "2025-11-18",
              "reasoning": true,
              "limit": { "context": 1000000, "output": 64000 },
              "cost": { "input": 2, "output": 12, "cache_read": 0.2 },
              "modalities": {
                "input": ["text", "image", "video", "audio", "pdf"],
                "output": ["text"]
              },
              "variants": {
                "low": { "options": { "thinkingConfig": { "thinkingLevel": "low", "includeThoughts": true } } },
                "medium": { "options": { "thinkingConfig": { "thinkingLevel": "medium", "includeThoughts": true } } },
                "high": { "options": { "thinkingConfig": { "thinkingLevel": "high", "includeThoughts": true } } }
              }
            }
          }
        }
      }
    }
  6. Enable image support for Antigravity models

    main

    To prevent the error "this model does not support image input", you must explicitly declare image support in your opencode.json model definitions by adding "image" to the modalities.input array.

    {
      "provider": {
        "google": {
          "models": {
            "gemini-3-pro-preview": {
              "modalities": {
                "input": ["text", "image"],
                "output": ["text"]
              }
            }
          }
        }
      }
    }
  7. How AccountManager handles account rotation and rate limits

    main

    The AccountManager uses a smart selection strategy to pick the best account for a specific ModelFamily via getCurrentOrNextForFamily(family):

    1. Check Current: It first checks if the currently active account is available (not rate-limited) for the requested family.
    2. Tier Check: If the current account is not paid, it checks if any paid accounts are available and not rate-limited. If a better tier is available, it will rotate.
    3. Rotation: If the current account is rate-limited or a better tier is available, it calls getNextForFamily(family), which:
      • Filters for accounts not currently rate-limited for that family.
      • Prioritizes paid accounts if any are available.
      • Uses a round-robin approach (currentIndex % pool.length) to select the next account from the pool.
    4. Rate Limit Marking: When an API call returns a rate limit error, you must call markRateLimited(account, retryAfterMs, family) to inform the manager to skip this account until the specified time.
  8. Debug Antigravity requests

    main

    You can debug Antigravity requests using OpenCode's built-in logging. Use the --log-level DEBUG flag to view detailed logs in the terminal or to write them to log files.

    Log files are stored in ~/.local/share/opencode/logs/ (or $XDG_DATA_HOME/opencode/logs/).

    opencode --log-level DEBUG --print-logs
  9. Use the google_search tool

    main

    The plugin provides a google_search tool that allows models to perform web searches and URL analysis. This tool works by making separate API calls to Gemini with native search tools enabled, allowing it to be used alongside other custom tools (like bash or read) without triggering Gemini API limitations.

    Capabilities:

    • Web Search: Real-time information retrieval.
    • URL Analysis: Fetching and analyzing specific web content.
    • Source Citations: Grounded responses with links.
    • Thinking Mode: Deep analysis with configurable budgets.

    Supported Models:

    • All Gemini models (2.5 Flash, 3 Pro, etc.)
    • Claude models (via Antigravity proxy)

    Example Prompts:

  10. ManagedAccount interface

    main

    The ManagedAccount object represents an individual OAuth identity within the AccountManager.

    FieldTypeDescription
    indexnumberThe position of the account in the managed list
    partsRefreshPartsThe core OAuth credentials (refreshToken, projectId, managedProjectId)
    accessstring (optional)The current access token
    expiresnumber (optional)The expiration timestamp of the access token
    rateLimitResetTimesRateLimitStateA map of ModelFamily to timestamp when the rate limit resets
    lastUsednumberTimestamp of the last successful use
    emailstring (optional)The account email address
    tierAccountTier (optional)The account tier (e.g., paid)
    lastSwitchReason`
  11. Configure Gemini Thinking options

    main

    The plugin forwards thinkingConfig options to Gemini models. You can configure thinkingLevel for Gemini 3 models and thinkingBudget for Gemini 2.5 models within your Opencode configuration.

    Available Keys:

    • thinkingLevel: `
  12. Handle Gemini tool name compatibility

    main

    The Gemini API requires tool names to follow the pattern ^[a-zA-Z_][a-zA-Z0-9_-]*$ (cannot start with a digit). While the plugin automatically sanitizes these by prepending t_, you can manually disable problematic tools in your config if you encounter errors.

    {
      "provider": {
        "google": {
          "models": {
            "gemini-3-pro-preview": {
              "tools": {
                "21st-dev-magic_*": false
              }
            }
          }
        }
      }
    }