XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
UTF-8
"""
Antigravity Provider for gpt4free (v2)

Provides access to Google's Antigravity API (Code Assist) supporting:
- Gemini 2.5 & Gemini 3 (Pro/Flash) with thinkingBudget and thinkingLevel
- Gemini 3.1 / 3.5 / 3.6 / 3.7 / 3.8 Flash & Pro variants
- Claude (Sonnet 4.5 / 4.6, Opus 4.5 / 4.6) via Antigravity proxy
- Image generation models (gemini-3.1-flash-image)

Uses OAuth2 authentication with Antigravity-specific credentials.
Supports endpoint fallback chain for reliability.
Includes interactive OAuth login flow with PKCE support.
"""

import os
import sys
import json
import base64
import time
import secrets
import hashlib
import asyncio
import uuid
import re
import webbrowser
import threading
from pathlib import Path
from typing import Any, AsyncGenerator, Dict, List, Optional, Union, Tuple
from urllib.parse import urlencode, parse_qs, urlparse
from http.server import HTTPServer, BaseHTTPRequestHandler

import aiohttp
from aiohttp import ClientSession, ClientTimeout

from ...typing import AsyncResult, Messages, MediaListType
from ...errors import MissingAuthError, RateLimitError
from ...requests.raise_for_status import raise_for_status
from ...image.copy_images import save_response_media
from ...image import to_bytes, is_data_an_media
from ...providers.response import Usage, ImageResponse, ToolCalls, Reasoning
from ...providers.asyncio import get_running_loop
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin
from ..helper import get_connector, get_system_prompt, format_media_prompt
from ... import debug


# JSON Schema keywords not supported by the Gemini API
_UNSUPPORTED_SCHEMA_KEYS = {
    "patternProperties",
    "$schema",
    "$id",
    "$defs",
    "definitions",
    "if",
    "then",
    "else",
    "not",
    "allOf",
    "anyOf",
    "oneOf",
    "default",
    "examples",
    "readOnly",
    "writeOnly",
    "contentEncoding",
    "contentMediaType",
    "additionalProperties",
    "enumDescriptions",
    "$comment",
}


def _sanitize_schema(schema: dict) -> dict:
    """Recursively remove JSON Schema keywords unsupported by the Gemini API."""
    if not isinstance(schema, dict):
        return schema
    result = {}
    for k, v in schema.items():
        if k in _UNSUPPORTED_SCHEMA_KEYS:
            continue
        if isinstance(v, dict):
            result[k] = _sanitize_schema(v)
        elif isinstance(v, list):
            result[k] = [_sanitize_schema(i) if isinstance(i, dict) else i for i in v]
        else:
            result[k] = v
    return result


def get_antigravity_oauth_creds_path() -> Path:
    """Get the default path for Antigravity OAuth credentials."""
    return Path.home() / ".antigravity" / "oauth_creds.json"


# --- Constants ---
CREDENTIALS_DIR = ".antigravity"
CREDENTIALS_FILE = "oauth_creds.json"

# Base URLs
ANTIGRAVITY_BASE_URL_DAILY = "https://daily-cloudcode-pa.googleapis.com"
ANTIGRAVITY_BASE_URL_PROD = "https://cloudcode-pa.googleapis.com"
ANTIGRAVITY_API_VERSION = "v1internal"

BASE_URLS = [
    f"{ANTIGRAVITY_BASE_URL_DAILY}/{ANTIGRAVITY_API_VERSION}",
    f"{ANTIGRAVITY_BASE_URL_PROD}/{ANTIGRAVITY_API_VERSION}",
]

PRODUCTION_URL = f"{ANTIGRAVITY_BASE_URL_PROD}/{ANTIGRAVITY_API_VERSION}"

OAUTH_CLIENT_ID = (
    "1071006060591-" + "tmhssin2h21lcre235vtolojh4g403ep" + ".apps.googleusercontent.com"
)
OAUTH_CLIENT_SECRET = "GOCSPX-" + "K58FWR486LdLJ1m" + "LB8sXC4z6qDAf"
DEFAULT_USER_AGENT = "antigravity/2.8.1 darwin/arm64"
REFRESH_SKEW = 3000  # 3000 seconds (50 minutes) advance refresh

ANTIGRAVITY_SYSTEM_PROMPT = (
    "You are Antigravity, a powerful agentic AI coding assistant designed by the Google Deepmind team "
    "working on Advanced Agentic Coding.You are pair programming with a USER to solve their coding task. "
    "The task may require creating a new codebase, modifying or debugging an existing codebase, or simply "
    "answering a question.**Absolute paths only****Proactiveness**"
)

DEFAULT_THINKING_MIN = 1024
DEFAULT_THINKING_MAX = 100000
ANTIGRAVITY_EMPTY_TEXT_PLACEHOLDER = "."

ANTIGRAVITY_STREAM_FIRST_BYTE_TIMEOUT_MS = 180000
ANTIGRAVITY_STREAM_IDLE_TIMEOUT_MS = 300000

ANTIGRAVITY_RAW_FALLBACK_MAX_LINES = 20000
ANTIGRAVITY_ERROR_BODY_MAX_BYTES = 1024 * 1024

ANTIGRAVITY_MODELS = [
    "claude-opus-4-6-thinking",
    "claude-sonnet-4-6",
    "gemini-3-flash",
    "gemini-3-pro-high",
    "gemini-3-pro-low",
    "gemini-3.1-flash-image",
    "gemini-pro-agent",
    "gemini-3.1-pro-high",
    "gemini-3.1-pro-low",
    "gpt-oss-120b-medium",
    "gemini-3.1-flash-lite",
    "gemini-3.5-flash-low",
    "gemini-3.5-flash-high",
    "gemini-3.6-flash",
    "gemini-3.6-flash-low",
    "gemini-3.6-flash-high",
    "gemini-3.7-flash",
    "gemini-3.7-flash-low",
    "gemini-3.7-flash-high",
    "gemini-3.8-flash",
    "gemini-3.8-flash-low",
    "gemini-3.8-flash-high",
]

ANTIGRAVITY_CLIENT_TO_UPSTREAM_MODEL = {
    "gemini-3.1-pro-high": "gemini-pro-agent",
    "gemini-3.1-pro-preview": "gemini-pro-agent",
    "gemini-3.5-flash-high": "gemini-3.5-flash-low",
    "gemini-3.6-flash": "gemini-3.6-flash-low",
    "gemini-3.7-flash": "gemini-3.7-flash-low",
    "gemini-3.8-flash": "gemini-3.8-flash-low",
}

ANTIGRAVITY_UPSTREAM_TO_CLIENT_MODELS = {
    "gemini-pro-agent": ["gemini-3.1-pro-high", "gemini-3.1-pro-preview"],
    "gemini-3.6-flash-low": ["gemini-3.6-flash", "gemini-3.6-flash-low"],
    "gemini-3.7-flash-low": ["gemini-3.7-flash", "gemini-3.7-flash-low"],
    "gemini-3.8-flash-low": ["gemini-3.8-flash", "gemini-3.8-flash-low"],
}

ANTIGRAVITY_CLIENT_MODEL_THINKING_LEVEL = {
    "gemini-pro-agent": "high",
    "gemini-3.1-pro-high": "high",
    "gemini-3.1-pro-preview": "high",
    "gemini-3-pro-high": "high",
    "gemini-3-pro-preview": "high",
    "gemini-3.5-flash-high": "high",
    "gemini-3.6-flash-high": "high",
    "gemini-3.7-flash-high": "high",
    "gemini-3.8-flash-high": "high",
    "gemini-3.1-pro-low": "low",
    "gemini-3-pro-low": "low",
    "gemini-3.5-flash-low": "low",
    "gemini-3.6-flash-low": "low",
    "gemini-3.7-flash-low": "low",
    "gemini-3.8-flash-low": "low",
}

ANTIGRAVITY_MODEL_METADATA = {
    "claude-opus-4-6-thinking": {
        "maxOutputTokens": 64000,
        "thinking": {"min": 1024, "max": 64000, "zeroAllowed": True, "dynamicAllowed": True},
    },
    "claude-sonnet-4-6": {
        "maxOutputTokens": 64000,
        "thinking": {"min": 1024, "max": 64000, "zeroAllowed": True, "dynamicAllowed": True},
    },
    "gemini-3-flash": {
        "maxOutputTokens": 65536,
        "thinking": {
            "min": 128,
            "max": 32768,
            "dynamicAllowed": True,
            "levels": ["minimal", "low", "medium", "high"],
        },
    },
    "gemini-3-pro-high": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 128, "max": 32768, "dynamicAllowed": True, "levels": ["low", "high"]},
    },
    "gemini-3-pro-low": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 128, "max": 32768, "dynamicAllowed": True, "levels": ["low", "high"]},
    },
    "gemini-3.1-flash-image": {
        "thinking": {"min": 128, "max": 32768, "dynamicAllowed": True, "levels": ["minimal", "high"]},
    },
    "gemini-pro-agent": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
    "gemini-3.1-pro-high": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
    "gemini-3.1-pro-low": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
    "gpt-oss-120b-medium": {
        "maxOutputTokens": 32768,
    },
    "gemini-3.1-flash-lite": {
        "maxOutputTokens": 65535,
        "thinking": {
            "min": 1,
            "max": 65535,
            "zeroAllowed": True,
            "dynamicAllowed": True,
            "levels": ["minimal", "low", "medium", "high"],
        },
    },
    "gemini-3.5-flash-low": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
    "gemini-3.6-flash-low": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
    "gemini-3.6-flash-high": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
    "gemini-3.7-flash-low": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
    "gemini-3.7-flash-high": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
    "gemini-3.8-flash-low": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
    "gemini-3.8-flash-high": {
        "maxOutputTokens": 65535,
        "thinking": {"min": 1, "max": 65535, "dynamicAllowed": True, "levels": ["low", "medium", "high"]},
    },
}

ANTIGRAVITY_HEADERS = {
    "User-Agent": DEFAULT_USER_AGENT,
    "X-Goog-Api-Client": "google-cloud-sdk vscode_cloudshelleditor/0.1",
    "Client-Metadata": '{"ideType":"ANTIGRAVITY","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}',
}

ANTIGRAVITY_AUTH_HEADERS = {
    "User-Agent": "google-api-nodejs-client/10.3.0",
    "X-Goog-Api-Client": "gl-node/22.18.0",
    "Client-Metadata": '{"ideType":"ANTIGRAVITY","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}',
}

ANTIGRAVITY_REDIRECT_URI = "http://localhost:51121/oauthcallback"
ANTIGRAVITY_SCOPES = [
    "https://www.googleapis.com/auth/cloud-platform",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/userinfo.profile",
    "https://www.googleapis.com/auth/cclog",
    "https://www.googleapis.com/auth/experimentsandconfigs",
]
CALLBACK_PORT = 51121
OAUTH_CALLBACK_PORT = CALLBACK_PORT
OAUTH_CALLBACK_PATH = "/oauthcallback"

TOOL_ID_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"


def normalize_antigravity_model_id(model_name: str) -> str:
    if not model_name or not isinstance(model_name, str):
        return ""
    normalized = model_name.strip()
    if normalized.startswith("models/"):
        normalized = normalized[len("models/") :]
    return normalized


def strip_model_suffix(model_name: str) -> str:
    normalized = normalize_antigravity_model_id(model_name)
    match = re.match(r"^(.+?)\([^()]+\)$", normalized)
    return match.group(1).strip() if match else normalized


def resolve_antigravity_upstream_model(model_name: str) -> str:
    base_model = strip_model_suffix(model_name)
    if not base_model:
        return ""
    if base_model.startswith("gemini-claude-"):
        return base_model.replace("gemini-claude-", "claude-")
    return ANTIGRAVITY_CLIENT_TO_UPSTREAM_MODEL.get(base_model, base_model)


def expand_antigravity_client_models(upstream_model: str) -> List[str]:
    base_model = strip_model_suffix(upstream_model)
    if not base_model:
        return []
    out = []

    def push(m):
        if m and m not in out:
            out.append(m)

    if base_model.startswith("claude-"):
        push(f"gemini-{base_model}")
        return out

    exposed_alias = False
    for alias in ANTIGRAVITY_UPSTREAM_TO_CLIENT_MODELS.get(base_model, []):
        if alias in ANTIGRAVITY_MODELS:
            push(alias)
            exposed_alias = True

    if base_model in ANTIGRAVITY_MODELS or (
        not exposed_alias and base_model in ANTIGRAVITY_MODEL_METADATA
    ):
        push(base_model)

    return out


def get_antigravity_model_metadata(model_name: str) -> Optional[dict]:
    upstream_model = resolve_antigravity_upstream_model(model_name)
    return ANTIGRAVITY_MODEL_METADATA.get(
        upstream_model
    ) or ANTIGRAVITY_MODEL_METADATA.get(strip_model_suffix(model_name))


def is_known_antigravity_model(model_name: str) -> bool:
    base_model = strip_model_suffix(model_name)
    if not base_model:
        return False
    return (
        base_model in ANTIGRAVITY_MODELS
        or get_antigravity_model_metadata(base_model) is not None
    )


def antigravity_model_uses_thinking_levels(model_name: str) -> bool:
    metadata = get_antigravity_model_metadata(model_name)
    levels = metadata.get("thinking", {}).get("levels") if metadata else None
    return isinstance(levels, list) and len(levels) > 0


def antigravity_model_requires_stream_for_non_stream(model_name: str) -> bool:
    name = str(model_name or "").lower()
    return (
        "claude" in name
        or "gemini-3-pro" in name
        or "gemini-3.1-flash-image" in name
    )


def normalize_antigravity_text_part(part: dict) -> None:
    if not isinstance(part, dict) or "text" not in part:
        return
    if not isinstance(part["text"], str):
        part["text"] = "" if part["text"] is None else str(part["text"])
    if len(part["text"].strip()) == 0:
        part["text"] = ANTIGRAVITY_EMPTY_TEXT_PLACEHOLDER


def normalize_antigravity_text_parts(parts: list) -> None:
    if isinstance(parts, list):
        for p in parts:
            normalize_antigravity_text_part(p)


def get_antigravity_client_model_thinking_level(model_name: str) -> str:
    base_model = strip_model_suffix(model_name)
    return ANTIGRAVITY_CLIENT_MODEL_THINKING_LEVEL.get(base_model, "")


def apply_antigravity_thinking_level_config(
    thinking_config: dict, level: str
) -> dict:
    thinking_config["thinkingLevel"] = level
    thinking_config["includeThoughts"] = True
    thinking_config.pop("thinkingBudget", None)
    thinking_config.pop("thinking_budget", None)
    return thinking_config


def apply_antigravity_client_model_thinking_level(
    payload: dict, client_model_name: str
) -> dict:
    level = get_antigravity_client_model_thinking_level(client_model_name)
    if not level or not payload.get("request"):
        return payload
    gen_cfg = payload["request"].setdefault("generationConfig", {})
    th_cfg = gen_cfg.setdefault("thinkingConfig", {})
    apply_antigravity_thinking_level_config(th_cfg, level)
    return payload


def apply_antigravity_client_model_thinking_level_to_request(
    request_body: dict, client_model_name: str
) -> dict:
    level = get_antigravity_client_model_thinking_level(client_model_name)
    if not level or not request_body:
        return request_body
    gen_cfg = request_body.setdefault("generationConfig", {})
    th_cfg = gen_cfg.setdefault("thinkingConfig", {})
    apply_antigravity_thinking_level_config(th_cfg, level)
    return request_body


def is_claude(model_name: str) -> bool:
    return bool(model_name and "claude" in model_name.lower())


def is_image_model(model_name: str) -> bool:
    return bool(model_name and "image" in model_name.lower())


def model_supports_thinking(model_name: str) -> bool:
    if not model_name:
        return False
    metadata = get_antigravity_model_metadata(model_name)
    if metadata and "thinking" in metadata:
        return True
    name = model_name.lower()
    return (
        "gemini-3" in name
        or name.startswith("gemini-2.5-")
        or "-thinking" in name
    )


def generate_request_id() -> str:
    return f"agent-{uuid.uuid4()}"


def generate_image_gen_request_id() -> str:
    return f"image_gen/{int(time.time()*1000)}/{uuid.uuid4()}/12"


def generate_session_id() -> str:
    n = secrets.randbelow(9000)
    return f"-{n}"


def generate_stable_session_id(payload: dict) -> str:
    try:
        contents = payload.get("request", {}).get("contents")
        if isinstance(contents, list):
            for content in contents:
                if (
                    isinstance(content, dict)
                    and content.get("role") == "user"
                    and isinstance(content.get("parts"), list)
                ):
                    text = (
                        content["parts"][0].get("text")
                        if content["parts"]
                        else None
                    )
                    if text:
                        digest = hashlib.sha256(text.encode("utf-8")).digest()
                        n = (
                            int.from_bytes(digest[:8], byteorder="big")
                            & 0x7FFFFFFFFFFFFFFF
                        )
                        return f"-{n}"
    except Exception:
        pass
    return generate_session_id()


def generate_project_id() -> str:
    adjectives = ["useful", "bright", "swift", "calm", "bold"]
    nouns = ["fuze", "wave", "spark", "flow", "core"]
    adj = secrets.choice(adjectives)
    noun = secrets.choice(nouns)
    random_part = secrets.token_hex(3)[:5]
    return f"{adj}-{noun}-{random_part}"


def normalize_thinking_budget(model_name: str, budget: int) -> int:
    if budget == -1:
        return -1
    thinking = get_antigravity_model_metadata(model_name)
    thinking_cfg = thinking.get("thinking", {}) if thinking else {}
    min_b = thinking_cfg.get("min", DEFAULT_THINKING_MIN)
    max_b = thinking_cfg.get("max", DEFAULT_THINKING_MAX)
    if budget < min_b:
        return min_b
    if budget > max_b:
        return max_b
    return budget


def normalize_antigravity_thinking(
    model_name: str, payload: dict, is_claude_model: bool
) -> dict:
    if not model_supports_thinking(model_name):
        if payload.get("request", {}).get("generationConfig", {}).get(
            "thinkingConfig"
        ):
            payload["request"]["generationConfig"].pop("thinkingConfig", None)
        return payload

    thinking_config = (
        payload.get("request", {})
        .get("generationConfig", {})
        .get("thinkingConfig")
    )
    if not thinking_config:
        return payload

    thinking_level = thinking_config.get("thinkingLevel")
    budget = thinking_config.get("thinkingBudget")
    thinking_requested = (thinking_level is not None) or (
        budget is not None and budget != 0
    )

    if thinking_requested and "includeThoughts" not in thinking_config:
        thinking_config["includeThoughts"] = True

    if budget is None:
        return payload

    normalized_budget = normalize_thinking_budget(model_name, budget)

    gen_cfg = payload["request"].get("generationConfig", {})
    max_tokens = gen_cfg.get("maxOutputTokens") or gen_cfg.get(
        "max_output_tokens"
    )
    if max_tokens and max_tokens > 0 and normalized_budget >= max_tokens:
        normalized_budget = max(0, max_tokens - 1)

    if is_claude_model:
        min_budget = DEFAULT_THINKING_MIN
        if 0 <= normalized_budget < min_budget and normalized_budget != -1:
            payload["request"]["generationConfig"].pop("thinkingConfig", None)
            return payload

    payload["request"]["generationConfig"]["thinkingConfig"][
        "thinkingBudget"
    ] = normalized_budget
    return payload


def generate_synthetic_tool_id() -> str:
    bytes_data = secrets.token_bytes(26)
    s = "".join(TOOL_ID_ALPHABET[b % 62] for b in bytes_data)
    return f"toolu_vrtx_{s}"


def ensure_tool_call_ids(contents: list) -> None:
    if not isinstance(contents, list):
        return
    pending_by_name = {}
    for content in contents:
        if not isinstance(content, dict) or not isinstance(
            content.get("parts"), list
        ):
            continue
        for part in content["parts"]:
            if not isinstance(part, dict):
                continue
            if "functionCall" in part:
                fc = part["functionCall"]
                if not fc.get("id"):
                    fc["id"] = generate_synthetic_tool_id()
                fc_name = fc.get("name")
                if fc_name:
                    pending_by_name.setdefault(fc_name, []).append(fc["id"])
            elif "functionResponse" in part:
                fr = part["functionResponse"]
                fr_name = fr.get("name")
                if fr_name:
                    q = pending_by_name.get(fr_name, [])
                    pending_id = q.pop(0) if q else None
                    if not fr.get("id") and pending_id:
                        fr["id"] = pending_id


def normalize_antigravity_tool_config(
    request_obj: dict, is_claude_model: bool
) -> None:
    tool_cfg = request_obj.get("toolConfig")
    if not isinstance(tool_cfg, dict):
        return
    fc_cfg = tool_cfg.get("functionCallingConfig")
    if not isinstance(fc_cfg, dict):
        return
    mode = str(fc_cfg.get("mode", "")).upper()
    if mode:
        fc_cfg["mode"] = mode
        if mode == "ANY" and "allowedFunctionNames" not in fc_cfg:
            names = []
            tools = request_obj.get("tools", [])
            if isinstance(tools, list):
                for t in tools:
                    if isinstance(t, dict) and isinstance(
                        t.get("functionDeclarations"), list
                    ):
                        for fd in t["functionDeclarations"]:
                            if isinstance(fd, dict) and fd.get("name"):
                                names.append(fd["name"])
            if names:
                fc_cfg["allowedFunctionNames"] = names


def gemini_to_antigravity(
    model_name: str, payload: dict, project_id: str
) -> dict:
    template = json.loads(json.dumps(payload))
    ensure_tool_call_ids(template.get("request", {}).get("contents"))

    is_claude_model = is_claude(model_name)
    is_img_model = is_image_model(model_name)

    template["model"] = model_name
    template["userAgent"] = "antigravity"
    template["requestType"] = "image_gen" if is_img_model else "agent"

    if project_id:
        template["project"] = project_id
    else:
        template.pop("project", None)

    if is_img_model:
        template["requestId"] = generate_image_gen_request_id()
    else:
        template["requestId"] = generate_request_id()
        if "request" not in template:
            template["request"] = {}
        template["request"]["sessionId"] = generate_stable_session_id(template)

    if "request" not in template:
        template["request"] = {}

    template["request"].pop("safetySettings", None)

    if "tool_config" in template and "toolConfig" not in template:
        template["toolConfig"] = template.pop("tool_config")
    else:
        template.pop("tool_config", None)

    if "toolConfig" in template:
        if "toolConfig" not in template["request"]:
            template["request"]["toolConfig"] = template["toolConfig"]
        template.pop("toolConfig", None)

    normalize_antigravity_tool_config(template["request"], is_claude_model)

    gen_cfg = template["request"].get("generationConfig", {})
    max_tokens = gen_cfg.get("maxOutputTokens")
    metadata = get_antigravity_model_metadata(model_name)
    model_max_tokens = metadata.get("maxOutputTokens") if metadata else None

    if (
        isinstance(max_tokens, int)
        and model_max_tokens
        and max_tokens > model_max_tokens
    ):
        template["request"]["generationConfig"][
            "maxOutputTokens"
        ] = model_max_tokens

    if not is_claude_model and "maxOutputTokens" in gen_cfg:
        gen_cfg.pop("maxOutputTokens", None)

    tools = template["request"].get("tools")
    if isinstance(tools, list):
        for tool in tools:
            if isinstance(tool, dict) and isinstance(
                tool.get("functionDeclarations"), list
            ):
                for func_decl in tool["functionDeclarations"]:
                    if "parametersJsonSchema" in func_decl:
                        func_decl["parameters"] = _sanitize_schema(
                            func_decl["parametersJsonSchema"]
                        )
                        func_decl.pop("parametersJsonSchema", None)
                    elif "parameters" in func_decl:
                        func_decl["parameters"] = _sanitize_schema(
                            func_decl["parameters"]
                        )

    if gen_cfg.get("responseJsonSchema"):
        gen_cfg["responseJsonSchema"] = _sanitize_schema(
            gen_cfg["responseJsonSchema"]
        )
    if gen_cfg.get("responseSchema"):
        gen_cfg["responseSchema"] = _sanitize_schema(gen_cfg["responseSchema"])

    if not antigravity_model_uses_thinking_levels(model_name):
        th_cfg = gen_cfg.get("thinkingConfig")
        if isinstance(th_cfg, dict) and "thinkingLevel" in th_cfg:
            th_cfg.pop("thinkingLevel", None)
            th_cfg["thinkingBudget"] = -1

    if is_img_model:
        gen_cfg = template["request"].setdefault("generationConfig", {})
        img_cfg = gen_cfg.setdefault("imageConfig", {})
        img_cfg["imageSize"] = "4K"
        th_cfg = gen_cfg.setdefault("thinkingConfig", {})
        th_cfg["includeThoughts"] = False

    template = normalize_antigravity_thinking(
        model_name, template, is_claude_model
    )
    return template


def ensure_roles_in_contents(request_body: dict, model_name: str) -> dict:
    request_body.pop("model", None)
    if "system_instruction" in request_body:
        request_body["systemInstruction"] = request_body.pop("system_instruction")

    original_system_prompt = request_body.get("systemInstruction")
    original_system_prompt_text = ""

    if original_system_prompt:
        if isinstance(original_system_prompt, str):
            original_system_prompt_text = original_system_prompt
        elif isinstance(original_system_prompt, dict):
            parts = original_system_prompt.get("parts")
            if isinstance(parts, list):
                text_parts = []
                for part in parts:
                    if isinstance(part, str):
                        text_parts.append(part)
                    elif isinstance(part, dict) and isinstance(
                        part.get("text"), str
                    ):
                        text_parts.append(part["text"])
                original_system_prompt_text = "\n".join(
                    t for t in text_parts if t
                )
            elif isinstance(original_system_prompt.get("text"), str):
                original_system_prompt_text = original_system_prompt["text"]

    name = (model_name or "").lower()
    is_gemini3 = "gemini-3" in name
    use_antigravity = is_gemini3 or "claude" in name

    if use_antigravity:
        parts = [
            {"text": ANTIGRAVITY_SYSTEM_PROMPT},
            {
                "text": f"Please ignore following [ignore]{ANTIGRAVITY_SYSTEM_PROMPT}[/ignore]"
            },
        ]
        if original_system_prompt_text:
            parts.append({"text": original_system_prompt_text})
        request_body["systemInstruction"] = {"role": "user", "parts": parts}
    elif original_system_prompt_text:
        request_body["systemInstruction"] = {
            "role": "user",
            "parts": [{"text": original_system_prompt_text}],
        }
    else:
        request_body.pop("systemInstruction", None)

    contents = request_body.get("contents")
    if isinstance(contents, list):
        for content in contents:
            if isinstance(content, dict):
                if not content.get("role"):
                    content["role"] = "user"
                if use_antigravity:
                    normalize_antigravity_text_parts(content.get("parts"))

    return request_body


def filter_sse_usage_metadata(line: str) -> str:
    if not line or not isinstance(line, str):
        return line
    if not line.startswith("data: "):
        return line
    try:
        data = json.loads(line[6:])
        finish_reason = None
        resp = data.get("response")
        if isinstance(resp, dict):
            candidates = resp.get("candidates")
            if isinstance(candidates, list) and candidates:
                finish_reason = candidates[0].get("finishReason")
        elif isinstance(data.get("candidates"), list) and data["candidates"]:
            finish_reason = data["candidates"][0].get("finishReason")

        if not finish_reason:
            if isinstance(data.get("response"), dict):
                data["response"].pop("usageMetadata", None)
            data.pop("usageMetadata", None)
            return "data: " + json.dumps(data)
    except Exception:
        pass
    return line


def convert_stream_to_non_stream(stream_text: str) -> dict:
    lines = stream_text.split("\n")
    response_template = ""
    trace_id = ""
    finish_reason = ""
    model_version = ""
    response_id = ""
    role = ""
    usage_raw = None
    parts = []

    pending_kind = ""
    pending_text = ""
    pending_thought_sig = ""

    def flush_pending():
        nonlocal pending_kind, pending_text, pending_thought_sig
        if not pending_kind:
            return
        text = pending_text
        if pending_kind == "text":
            if text.strip():
                parts.append({"text": text})
        elif pending_kind == "thought":
            if text.strip() or pending_thought_sig:
                part = {"thought": True, "text": text}
                if pending_thought_sig:
                    part["thoughtSignature"] = pending_thought_sig
                parts.append(part)
        pending_kind = ""
        pending_text = ""
        pending_thought_sig = ""

    def normalize_part(part):
        m = dict(part)
        sig = part.get("thoughtSignature") or part.get("thought_signature")
        if sig:
            m["thoughtSignature"] = sig
            m.pop("thought_signature", None)
        if "inline_data" in m:
            m["inlineData"] = m.pop("inline_data")
        return m

    for line in lines:
        trimmed = line.strip()
        if not trimmed:
            continue
        try:
            data = json.loads(trimmed)
        except Exception:
            continue

        response_node = data.get("response")
        if not response_node:
            if "candidates" in data:
                response_node = data
            else:
                continue
        response_template = json.dumps(response_node)

        if data.get("traceId"):
            trace_id = data["traceId"]

        candidates = response_node.get("candidates", [])
        if candidates and isinstance(candidates[0], dict):
            c0 = candidates[0]
            if c0.get("content", {}).get("role"):
                role = c0["content"]["role"]
            if c0.get("finishReason"):
                finish_reason = c0["finishReason"]

        if response_node.get("modelVersion"):
            model_version = response_node["modelVersion"]
        if response_node.get("responseId"):
            response_id = response_node["responseId"]
        if response_node.get("usageMetadata"):
            usage_raw = response_node["usageMetadata"]
        elif data.get("usageMetadata"):
            usage_raw = data["usageMetadata"]

        parts_array = (
            candidates[0].get("content", {}).get("parts")
            if candidates and isinstance(candidates[0], dict)
            else None
        )
        if isinstance(parts_array, list):
            for part in parts_array:
                if not isinstance(part, dict):
                    continue
                has_fc = "functionCall" in part
                has_inline = "inlineData" in part or "inline_data" in part
                sig = (
                    part.get("thoughtSignature")
                    or part.get("thought_signature")
                    or ""
                )
                text = part.get("text", "")
                thought = part.get("thought", False)

                if has_fc or has_inline:
                    flush_pending()
                    parts.append(normalize_part(part))
                    continue

                if thought or "text" in part:
                    kind = "thought" if thought else "text"
                    if pending_kind and pending_kind != kind:
                        flush_pending()
                    pending_kind = kind
                    pending_text += text
                    if kind == "thought" and sig:
                        pending_thought_sig = sig
                    continue

                flush_pending()
                parts.append(normalize_part(part))

    flush_pending()

    if not response_template:
        response_template = '{"candidates":[{"content":{"role":"model","parts":[]}}]}'

    result = json.loads(response_template)
    if "candidates" not in result or not result["candidates"]:
        result["candidates"] = [{"content": {"role": "model", "parts": []}}]
    c0 = result["candidates"][0]
    if "content" not in c0 or not isinstance(c0["content"], dict):
        c0["content"] = {"role": "model", "parts": []}
    c0["content"]["parts"] = parts

    if role:
        c0["content"]["role"] = role
    if finish_reason:
        c0["finishReason"] = finish_reason
    if model_version:
        result["modelVersion"] = model_version
    if response_id:
        result["responseId"] = response_id
    if usage_raw:
        result["usageMetadata"] = usage_raw
    elif "usageMetadata" not in result:
        result["usageMetadata"] = {
            "promptTokenCount": 0,
            "candidatesTokenCount": 0,
            "totalTokenCount": 0,
        }

    return {
        "response": result,
        "traceId": trace_id or "",
    }


def to_gemini_api_response(antigravity_response: dict) -> Optional[dict]:
    if not antigravity_response:
        return None
    compliant_response = {
        "candidates": antigravity_response.get("candidates", [])
    }
    if "usageMetadata" in antigravity_response:
        compliant_response["usageMetadata"] = antigravity_response[
            "usageMetadata"
        ]
    if "promptFeedback" in antigravity_response:
        compliant_response["promptFeedback"] = antigravity_response[
            "promptFeedback"
        ]
    if "automaticFunctionCallingHistory" in antigravity_response:
        compliant_response["automaticFunctionCallingHistory"] = (
            antigravity_response["automaticFunctionCallingHistory"]
        )
    return compliant_response


def build_antigravity_payload(
    model: str,
    request_body: dict,
    project_id: str,
    available_models: Optional[list] = None,
) -> Tuple[dict, str, str]:
    selected_model = normalize_antigravity_model_id(model)
    avail = available_models or ANTIGRAVITY_MODELS
    if selected_model not in avail and not is_known_antigravity_model(
        selected_model
    ):
        selected_model = "gemini-3-flash"
        request_body["model"] = selected_model

    actual_model_name = resolve_antigravity_upstream_model(selected_model)

    apply_antigravity_client_model_thinking_level_to_request(
        request_body, selected_model
    )
    processed_request_body = ensure_roles_in_contents(
        json.loads(json.dumps(request_body)), selected_model
    )
    payload = apply_antigravity_client_model_thinking_level(
        gemini_to_antigravity(
            actual_model_name, {"request": processed_request_body}, project_id
        ),
        selected_model,
    )
    request_body["model"] = actual_model_name
    return payload, selected_model, actual_model_name


# --- PKCE / OAuth Callback Server ---
class OAuthCallbackHandler(BaseHTTPRequestHandler):
    """HTTP request handler for OAuth callback."""

    callback_result: Optional[Dict[str, str]] = None
    callback_error: Optional[str] = None

    def log_message(self, format, *args):
        pass

    def do_GET(self):
        parsed = urlparse(self.path)

        if parsed.path != OAUTH_CALLBACK_PATH:
            self.send_error(404, "Not Found")
            return

        params = parse_qs(parsed.query)
        code = params.get("code", [None])[0]
        state = params.get("state", [None])[0]
        error = params.get("error", [None])[0]

        if error:
            OAuthCallbackHandler.callback_error = error
            self._send_error_response(error)
        elif code and state:
            OAuthCallbackHandler.callback_result = {
                "code": code,
                "state": state,
            }
            self._send_success_response()
        else:
            OAuthCallbackHandler.callback_error = (
                "Missing code or state parameter"
            )
            self._send_error_response("Missing parameters")

    def _send_success_response(self):
        html = """<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Authentication Successful</title>
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; 
               display: flex; justify-content: center; align-items: center; height: 100vh; 
               margin: 0; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
        .container { background: white; padding: 3rem; border-radius: 1rem; 
                     box-shadow: 0 20px 60px rgba(0,0,0,0.3); text-align: center; max-width: 400px; }
        h1 { color: #10B981; margin-bottom: 1rem; }
        p { color: #6B7280; line-height: 1.6; }
        .icon { font-size: 4rem; margin-bottom: 1rem; }
    </style>
</head>
<body>
    <div class="container">
        <div class="icon">✅</div>
        <h1>Authentication Successful!</h1>
        <p>You have successfully authenticated with Google.<br>You can close this window and return to your terminal.</p>
    </div>
</body>
</html>"""
        self.send_response(200)
        self.send_header("Content-Type", "text/html; charset=utf-8")
        self.send_header("Content-Length", str(len(html.encode())))
        self.end_headers()
        self.wfile.write(html.encode())

    def _send_error_response(self, error: str):
        html = f"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Authentication Failed</title>
    <style>
        body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; 
               display: flex; justify-content: center; align-items: center; height: 100vh; 
               margin: 0; background: #FEE2E2; }}
        .container {{ background: white; padding: 3rem; border-radius: 1rem; 
                     box-shadow: 0 10px 40px rgba(0,0,0,0.1); text-align: center; }}
        h1 {{ color: #EF4444; }}
        p {{ color: #6B7280; }}
    </style>
</head>
<body>
    <div class="container">
        <h1>❌ Authentication Failed</h1>
        <p>Error: {error}</p>
        <p>Please try again.</p>
    </div>
</body>
</html>"""
        self.send_response(400)
        self.send_header("Content-Type", "text/html; charset=utf-8")
        self.send_header("Content-Length", str(len(html.encode())))
        self.end_headers()
        self.wfile.write(html.encode())


class OAuthCallbackServer:
    """Local HTTP server to capture OAuth callback."""

    def __init__(self, port: int = CALLBACK_PORT, timeout: float = 300.0):
        self.port = port
        self.timeout = timeout
        self.server: Optional[HTTPServer] = None
        self._thread: Optional[threading.Thread] = None
        self._stop_flag = False

    def start(self) -> bool:
        try:
            OAuthCallbackHandler.callback_result = None
            OAuthCallbackHandler.callback_error = None
            self._stop_flag = False

            self.server = HTTPServer(("localhost", self.port), OAuthCallbackHandler)
            self.server.timeout = 0.5

            self._thread = threading.Thread(target=self._serve, daemon=True)
            self._thread.start()
            return True
        except OSError as e:
            debug.log(f"Failed to start OAuth callback server: {e}")
            return False

    def _serve(self):
        start_time = time.time()
        while not self._stop_flag and self.server:
            if time.time() - start_time > self.timeout:
                break
            if (
                OAuthCallbackHandler.callback_result
                or OAuthCallbackHandler.callback_error
            ):
                time.sleep(0.3)
                break
            try:
                self.server.handle_request()
            except Exception:
                break

    def wait_for_callback(self) -> Optional[Dict[str, str]]:
        start_time = time.time()
        while time.time() - start_time < self.timeout:
            if (
                OAuthCallbackHandler.callback_result
                or OAuthCallbackHandler.callback_error
            ):
                break
            time.sleep(0.1)

        self._stop_flag = True
        if self._thread:
            self._thread.join(timeout=2.0)

        if OAuthCallbackHandler.callback_error:
            raise RuntimeError(
                f"OAuth error: {OAuthCallbackHandler.callback_error}"
            )

        return OAuthCallbackHandler.callback_result

    def stop(self):
        self._stop_flag = True
        if self.server:
            try:
                self.server.server_close()
            except Exception:
                pass
            self.server = None


def generate_pkce_pair() -> Tuple[str, str]:
    verifier = secrets.token_urlsafe(32)
    digest = hashlib.sha256(verifier.encode("ascii")).digest()
    challenge = base64.urlsafe_b64encode(digest).rstrip(b"=").decode("ascii")
    return verifier, challenge


def encode_oauth_state(verifier: str, project_id: str = "") -> str:
    payload = {"verifier": verifier, "projectId": project_id}
    return base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")


def decode_oauth_state(state: str) -> Dict[str, str]:
    padded = state + "=" * (4 - len(state) % 4) if len(state) % 4 else state
    normalized = padded.replace("-", "+").replace("_", "/")
    try:
        decoded = base64.b64decode(normalized).decode("utf-8")
        parsed = json.loads(decoded)
        return {
            "verifier": parsed.get("verifier", ""),
            "projectId": parsed.get("projectId", ""),
        }
    except Exception:
        return {"verifier": "", "projectId": ""}


class AntigravityAuthManager(AuthFileMixin):
    """
    Handles OAuth2 authentication for Google's Antigravity API.
    Uses Antigravity-specific OAuth credentials and supports endpoint fallback.
    """

    parent = "Antigravity"
    OAUTH_REFRESH_URL = "https://oauth2.googleapis.com/token"
    OAUTH_CLIENT_ID = os.environ.get("ANTIGRAVITY_CLIENT_ID", OAUTH_CLIENT_ID)
    OAUTH_CLIENT_SECRET = os.environ.get(
        "ANTIGRAVITY_CLIENT_SECRET", OAUTH_CLIENT_SECRET
    )
    TOKEN_BUFFER_TIME = REFRESH_SKEW
    KV_TOKEN_KEY = "antigravity_oauth_token_cache"

    def __init__(self, env: Dict[str, Any]):
        self.env = env
        self._access_token: Optional[str] = None
        self._expiry: Optional[float] = None
        self._token_cache = {}
        self._working_base_url: Optional[str] = None
        self._project_id: Optional[str] = None

    async def initialize_auth(self) -> None:
        cached = await self._get_cached_token()
        now = time.time()
        if cached:
            expires_at = cached["expiry_date"] / 1000
            if expires_at - now > self.TOKEN_BUFFER_TIME:
                self._access_token = cached["access_token"]
                self._expiry = expires_at
                return

        path = AntigravityAuthManager.get_cache_file()
        if not path.exists():
            path = get_antigravity_oauth_creds_path()

        if path.exists():
            try:
                with path.open("r") as f:
                    creds = json.load(f)
            except Exception as e:
                raise RuntimeError(
                    f"Failed to read OAuth credentials from {path}: {e}"
                )
        else:
            if "ANTIGRAVITY_SERVICE_ACCOUNT" not in self.env:
                raise RuntimeError(
                    "ANTIGRAVITY_SERVICE_ACCOUNT environment variable not set. "
                    f"Please set it or create credentials at {get_antigravity_oauth_creds_path()}"
                )
            creds = json.loads(self.env["ANTIGRAVITY_SERVICE_ACCOUNT"])

        if creds.get("project_id"):
            self._project_id = creds["project_id"]

        refresh_token = creds.get("refresh_token")
        access_token = creds.get("access_token")
        expiry_date = creds.get("expiry_date")

        if access_token and expiry_date:
            expires_at = expiry_date / 1000
            if expires_at - now > self.TOKEN_BUFFER_TIME:
                self._access_token = access_token
                self._expiry = expires_at
                await self._cache_token(access_token, expiry_date)
                return

        if not refresh_token:
            raise RuntimeError("No refresh token found in credentials.")

        await self._refresh_and_cache_token(refresh_token)

    async def _refresh_and_cache_token(self, refresh_token: str) -> None:
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        data = {
            "client_id": self.OAUTH_CLIENT_ID,
            "client_secret": self.OAUTH_CLIENT_SECRET,
            "refresh_token": refresh_token,
            "grant_type": "refresh_token",
        }

        async with aiohttp.ClientSession() as session:
            async with session.post(
                self.OAUTH_REFRESH_URL, data=data, headers=headers
            ) as resp:
                if resp.status != 200:
                    text = await resp.text()
                    raise RuntimeError(f"Token refresh failed: {text}")
                resp_data = await resp.json()
                access_token = resp_data.get("access_token")
                expires_in = resp_data.get("expires_in", 3600)

                if not access_token:
                    raise RuntimeError("No access_token in refresh response.")

                self._access_token = access_token
                self._expiry = time.time() + expires_in
                expiry_date_ms = int(self._expiry * 1000)
                await self._cache_token(access_token, expiry_date_ms)

    async def _cache_token(self, access_token: str, expiry_date: int) -> None:
        token_data = {
            "access_token": access_token,
            "expiry_date": expiry_date,
            "cached_at": int(time.time() * 1000),
        }
        self._token_cache[self.KV_TOKEN_KEY] = token_data

    async def _get_cached_token(self) -> Optional[Dict[str, Any]]:
        cached = self._token_cache.get(self.KV_TOKEN_KEY)
        if cached:
            expires_at = cached["expiry_date"] / 1000
            if expires_at - time.time() > self.TOKEN_BUFFER_TIME:
                return cached
        return None

    async def clear_token_cache(self) -> None:
        self._access_token = None
        self._expiry = None
        self._token_cache.pop(self.KV_TOKEN_KEY, None)

    def get_access_token(self) -> Optional[str]:
        if (
            self._access_token is not None
            and self._expiry is not None
            and self._expiry - time.time() > self.TOKEN_BUFFER_TIME
        ):
            return self._access_token
        return None

    def get_project_id(self) -> Optional[str]:
        return self._project_id

    def get_working_base_url(self) -> str:
        """Get the cached working base URL or default to first in list."""
        return self._working_base_url or BASE_URLS[0]

    async def call_endpoint(
        self,
        method: str,
        body: Dict[str, Any],
        is_retry: bool = False,
        use_auth_headers: bool = False,
    ) -> Any:
        if not self.get_access_token():
            await self.initialize_auth()

        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.get_access_token()}",
            **(
                ANTIGRAVITY_AUTH_HEADERS
                if use_auth_headers
                else ANTIGRAVITY_HEADERS
            ),
        }

        urls_to_try = []
        if self._working_base_url:
            urls_to_try.append(self._working_base_url)
        urls_to_try.extend([url for url in BASE_URLS if url != self._working_base_url])

        last_error = None
        for base_url in urls_to_try:
            url = f"{base_url}:{method}"
            try:
                async with aiohttp.ClientSession() as session:
                    async with session.post(
                        url, headers=headers, json=body, timeout=30
                    ) as resp:
                        if resp.status == 401 and not is_retry:
                            await self.clear_token_cache()
                            await self.initialize_auth()
                            return await self.call_endpoint(
                                method,
                                body,
                                is_retry=True,
                                use_auth_headers=use_auth_headers,
                            )
                        elif resp.ok:
                            self._working_base_url = base_url
                            return await resp.json()
                        else:
                            last_error = f"HTTP {resp.status}: {await resp.text()}"
                            debug.log(
                                f"Antigravity endpoint {base_url} returned {resp.status}"
                            )
            except Exception as e:
                last_error = str(e)
                debug.log(f"Antigravity endpoint {base_url} failed: {e}")
                continue

        raise RuntimeError(
            f"All Antigravity endpoints failed. Last error: {last_error}"
        )

    @classmethod
    def build_authorization_url(cls, project_id: str = "") -> Tuple[str, str, str]:
        verifier, challenge = generate_pkce_pair()
        state = encode_oauth_state(verifier, project_id)

        params = {
            "client_id": cls.OAUTH_CLIENT_ID,
            "response_type": "code",
            "redirect_uri": ANTIGRAVITY_REDIRECT_URI,
            "scope": " ".join(ANTIGRAVITY_SCOPES),
            "code_challenge": challenge,
            "code_challenge_method": "S256",
            "state": state,
            "access_type": "offline",
            "prompt": "consent",
        }

        url = f"https://accounts.google.com/o/oauth2/v2/auth?{urlencode(params)}"
        return url, verifier, state

    @classmethod
    async def exchange_code_for_tokens(
        cls,
        code: str,
        state: str,
    ) -> Dict[str, Any]:
        decoded_state = decode_oauth_state(state)
        verifier = decoded_state.get("verifier", "")
        project_id = decoded_state.get("projectId", "")

        if not verifier:
            raise RuntimeError("Missing PKCE verifier in state parameter")

        start_time = time.time()

        async with aiohttp.ClientSession() as session:
            token_data = {
                "client_id": cls.OAUTH_CLIENT_ID,
                "client_secret": cls.OAUTH_CLIENT_SECRET,
                "code": code,
                "grant_type": "authorization_code",
                "redirect_uri": ANTIGRAVITY_REDIRECT_URI,
                "code_verifier": verifier,
            }

            async with session.post(
                "https://oauth2.googleapis.com/token",
                data=token_data,
                headers={
                    "Content-Type": "application/x-www-form-urlencoded",
                    "User-Agent": "google-api-nodejs-client/10.3.0",
                },
            ) as resp:
                if not resp.ok:
                    error_text = await resp.text()
                    raise RuntimeError(f"Token exchange failed: {error_text}")

                token_response = await resp.json()

            access_token = token_response.get("access_token")
            refresh_token = token_response.get("refresh_token")
            expires_in = token_response.get("expires_in", 3600)

            if not access_token or not refresh_token:
                raise RuntimeError("Missing tokens in response")

            email = None
            async with session.get(
                "https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
                headers={"Authorization": f"Bearer {access_token}"},
            ) as resp:
                if resp.ok:
                    user_info = await resp.json()
                    email = user_info.get("email")

            effective_project_id = project_id
            if not effective_project_id:
                effective_project_id = await cls._fetch_project_id(
                    session, access_token
                )

        expires_at = int((start_time + expires_in) * 1000)

        return {
            "access_token": access_token,
            "refresh_token": refresh_token,
            "expiry_date": expires_at,
            "email": email,
            "project_id": effective_project_id,
        }

    @classmethod
    async def _fetch_project_id(
        cls, session: aiohttp.ClientSession, access_token: str
    ) -> str:
        headers = {
            "Authorization": f"Bearer {access_token}",
            "Content-Type": "application/json",
            **ANTIGRAVITY_AUTH_HEADERS,
        }

        load_request = {
            "metadata": {
                "ideType": "ANTIGRAVITY",
                "platform": "PLATFORM_UNSPECIFIED",
                "pluginType": "GEMINI",
            }
        }

        timeout = aiohttp.ClientTimeout(total=10)
        for base_url in BASE_URLS:
            try:
                url = f"{base_url}:loadCodeAssist"
                async with session.post(
                    url, headers=headers, json=load_request, timeout=timeout
                ) as resp:
                    if resp.ok:
                        data = await resp.json()
                        project = data.get("cloudaicompanionProject")
                        if isinstance(project, dict):
                            project = project.get("id")
                        if project:
                            return project
            except asyncio.TimeoutError:
                debug.log(f"Project discovery timed out at {base_url}")
                continue
            except Exception as e:
                debug.log(f"Project discovery failed at {base_url}: {e}")
                continue

        attempts = int(os.environ.get("ANTIGRAVITY_ONBOARD_ATTEMPTS", "10"))
        delay_seconds = float(os.environ.get("ANTIGRAVITY_ONBOARD_DELAY_S", "5"))
        tier_id = os.environ.get("ANTIGRAVITY_TIER_ID", "free-tier")
        configured_project = os.environ.get("ANTIGRAVITY_PROJECT_ID", "")

        if tier_id:
            onboard_request_body = {
                "tier_id": tier_id,
                "metadata": {
                    "ide_type": "ANTIGRAVITY",
                    "ide_version": "2.8.1",
                    "ide_name": "antigravity",
                },
            }
            if configured_project:
                onboard_request_body["metadata"][
                    "cloudaicompanionProject"
                ] = configured_project

            for base_url in BASE_URLS:
                for attempt in range(attempts):
                    try:
                        url = f"{base_url}:onboardUser"
                        onboard_headers = {
                            "Authorization": f"Bearer {access_token}",
                            "Content-Type": "application/json",
                            **ANTIGRAVITY_HEADERS,
                        }
                        async with session.post(
                            url,
                            headers=onboard_headers,
                            json=onboard_request_body,
                            timeout=timeout,
                        ) as resp:
                            if not resp.ok:
                                if resp.status == 403:
                                    raise MissingAuthError(
                                        "Account not eligible for Antigravity Code Assist."
                                    )
                                break

                            payload = await resp.json()
                            response_obj = payload.get("response") or {}
                            managed = response_obj.get("cloudaicompanionProject")
                            managed_id = (
                                managed.get("id")
                                if isinstance(managed, dict)
                                else None
                            )

                            done = bool(payload.get("done", False))
                            if done and managed_id:
                                return managed_id
                            if done and configured_project:
                                return configured_project
                    except MissingAuthError:
                        raise
                    except Exception as e:
                        debug.log(
                            f"Failed to onboard managed project at {base_url}: {e}"
                        )
                        break

                    await asyncio.sleep(delay_seconds)

        return generate_project_id()

    @classmethod
    async def interactive_login(
        cls,
        project_id: str = "",
        no_browser: bool = False,
        timeout: float = 300.0,
    ) -> Dict[str, Any]:
        auth_url, verifier, state = cls.build_authorization_url(project_id)

        print("\n" + "=" * 60)
        print("Antigravity OAuth Login")
        print("=" * 60)

        callback_server = OAuthCallbackServer(timeout=timeout)
        server_started = callback_server.start()

        if server_started and not no_browser:
            print(f"\nOpening browser for authentication...")
            print(f"If browser doesn't open, visit this URL:\n")
            print(f"{auth_url}\n")
            try:
                webbrowser.open(auth_url)
            except Exception as e:
                print(f"Could not open browser automatically: {e}")
                print("Please open the URL above manually.\n")
        else:
            if not server_started:
                print(
                    f"\nCould not start local callback server on port {CALLBACK_PORT}."
                )
            print(f"\nPlease open this URL in your browser:\n")
            print(f"{auth_url}\n")

        if server_started:
            print("Waiting for authentication callback...")
            try:
                callback_result = callback_server.wait_for_callback()
                if not callback_result:
                    raise RuntimeError("OAuth callback timed out")

                code = callback_result.get("code")
                callback_state = callback_result.get("state")

                if not code:
                    raise RuntimeError("No authorization code received")

                print("\n✓ Authorization code received. Exchanging for tokens...")
                tokens = await cls.exchange_code_for_tokens(
                    code, callback_state or state
                )

                print(f"✓ Authentication successful!")
                if tokens.get("email"):
                    print(f"  Logged in as: {tokens['email']}")
                if tokens.get("project_id"):
                    print(f"  Project ID: {tokens['project_id']}")

                return tokens
            finally:
                callback_server.stop()
        else:
            print(
                "\nAfter completing authentication, you'll be redirected to a localhost URL."
            )
            print("Copy and paste the full redirect URL or just the code below:\n")
            user_input = input("Paste redirect URL or code: ").strip()

            if not user_input:
                raise RuntimeError("No input provided")

            if user_input.startswith("http"):
                parsed = urlparse(user_input)
                params = parse_qs(parsed.query)
                code = params.get("code", [None])[0]
                callback_state = params.get("state", [state])[0]
            else:
                code = user_input
                callback_state = state

            if not code:
                raise RuntimeError("Could not extract authorization code")

            print("\nExchanging code for tokens...")
            tokens = await cls.exchange_code_for_tokens(code, callback_state)

            print(f"✓ Authentication successful!")
            if tokens.get("email"):
                print(f"  Logged in as: {tokens['email']}")

            return tokens

    @classmethod
    async def login_and_save(
        cls,
        project_id: str = "",
        no_browser: bool = False,
        credentials_path: Optional[Path] = None,
    ) -> "AntigravityAuthManager":
        tokens = await cls.interactive_login(
            project_id=project_id, no_browser=no_browser
        )

        creds = {
            "access_token": tokens["access_token"],
            "refresh_token": tokens["refresh_token"],
            "expiry_date": tokens["expiry_date"],
            "email": tokens.get("email"),
            "project_id": tokens.get("project_id"),
            "client_id": cls.OAUTH_CLIENT_ID,
            "client_secret": cls.OAUTH_CLIENT_SECRET,
        }

        path = credentials_path or cls.get_cache_file()
        path.parent.mkdir(parents=True, exist_ok=True)

        with path.open("w") as f:
            json.dump(creds, f, indent=2)

        try:
            path.chmod(0o600)
        except Exception:
            pass

        print(f"\n✓ Credentials saved to: {path}")
        print("=" * 60 + "\n")

        auth_manager = cls(env=os.environ)
        auth_manager._access_token = tokens["access_token"]
        auth_manager._expiry = tokens["expiry_date"] / 1000
        return auth_manager


class AntigravityProvider:
    """
    Internal provider class for Antigravity API communication.
    Handles payload formatting, project discovery, and streaming content generation.
    """

    url = "https://cloud.google.com/code-assist"

    def __init__(self, env: dict, auth_manager: AntigravityAuthManager):
        self.env = env
        self.auth_manager = auth_manager
        self._project_id: Optional[str] = None
        self.available_models: List[str] = []

    async def discover_project_id(self) -> str:
        if self.env.get("ANTIGRAVITY_PROJECT_ID"):
            return self.env["ANTIGRAVITY_PROJECT_ID"]
        if self._project_id:
            return self._project_id

        auth_project_id = self.auth_manager.get_project_id()
        if auth_project_id:
            self._project_id = auth_project_id
            return auth_project_id

        try:
            access_token = self.auth_manager.get_access_token()
            if not access_token:
                raise RuntimeError(
                    "No valid access token available for project discovery"
                )

            async with aiohttp.ClientSession() as session:
                project = await self.auth_manager._fetch_project_id(
                    session=session, access_token=access_token
                )
            if project:
                self._project_id = project
                return project
        except MissingAuthError:
            raise
        except Exception as e:
            debug.error(f"Failed to discover project ID: {e}")

        fallback_id = generate_project_id()
        self._project_id = fallback_id
        return fallback_id

    @staticmethod
    def _messages_to_gemini_format(
        messages: list, media: MediaListType
    ) -> List[Dict[str, Any]]:
        format_messages = []
        for msg in messages:
            role = "model" if msg["role"] == "assistant" else "user"

            if msg["role"] == "tool":
                tool_result = msg.get("content", "")
                func_response_part = {
                    "functionResponse": {
                        "name": msg.get("tool_call_id", "unknown_function"),
                        "response": {
                            "result": (
                                tool_result
                                if isinstance(tool_result, str)
                                else json.dumps(tool_result)
                            )
                        },
                    }
                }
                if (
                    format_messages
                    and format_messages[-1]["role"] == "user"
                    and any(
                        "functionResponse" in p for p in format_messages[-1]["parts"]
                    )
                ):
                    format_messages[-1]["parts"].append(func_response_part)
                else:
                    format_messages.append(
                        {"role": "user", "parts": [func_response_part]}
                    )
                continue

            elif msg["role"] == "assistant" and msg.get("tool_calls"):
                parts = []
                content = msg.get("content")
                if isinstance(content, str) and content.strip():
                    parts.append({"text": content})
                for tool_call in msg["tool_calls"]:
                    if tool_call.get("type") == "function":
                        func_call = {
                            "name": tool_call["function"]["name"],
                            "args": json.loads(tool_call["function"]["arguments"]),
                        }
                        if tool_call.get("id"):
                            func_call["id"] = tool_call["id"]
                        thought_sig = (
                            tool_call.get("extra_content", {})
                            .get("google", {})
                            .get(
                                "thought_signature", "skip_thought_signature_validator"
                            )
                        )
                        parts.append(
                            {"functionCall": func_call, "thoughtSignature": thought_sig}
                        )

            elif isinstance(msg["content"], str):
                parts = [{"text": msg["content"]}]

            elif isinstance(msg["content"], list):
                parts = []
                for content in msg["content"]:
                    ctype = content.get("type")
                    if ctype == "text":
                        parts.append({"text": content["text"]})
                    elif ctype == "image_url":
                        image_url = content.get("image_url", {}).get("url")
                        if not image_url:
                            continue
                        if image_url.startswith("data:"):
                            prefix, b64data = image_url.split(",", 1)
                            mime_type = prefix.split(":")[1].split(";")[0]
                            parts.append(
                                {"inlineData": {"mimeType": mime_type, "data": b64data}}
                            )
                        else:
                            parts.append(
                                {
                                    "fileData": {
                                        "mimeType": "image/jpeg",
                                        "fileUri": image_url,
                                    }
                                }
                            )
            elif msg.get("content") is not None:
                parts = [{"text": str(msg["content"])}]
            else:
                parts = []

            format_messages.append({"role": role, "parts": parts})

        if media:
            if not format_messages:
                format_messages.append({"role": "user", "parts": []})
            for media_data, filename in media:
                if isinstance(media_data, str):
                    if not filename:
                        filename = media_data
                    extension = filename.split(".")[-1].replace("jpg", "jpeg")
                    format_messages[-1]["parts"].append(
                        {
                            "fileData": {
                                "mimeType": f"image/{extension}",
                                "fileUri": media_data,
                            }
                        }
                    )
                else:
                    media_bytes = to_bytes(media_data)
                    format_messages[-1]["parts"].append(
                        {
                            "inlineData": {
                                "mimeType": is_data_an_media(media_bytes, filename),
                                "data": base64.b64encode(media_bytes).decode(),
                            }
                        }
                    )
        return format_messages

    async def stream_content(
        self,
        model: str,
        messages: Messages,
        *,
        proxy: Optional[str] = None,
        thinking_budget: Optional[int] = None,
        tools: Optional[List[dict]] = None,
        tool_choice: Optional[str] = None,
        max_tokens: Optional[int] = None,
        temperature: Optional[float] = None,
        top_p: Optional[float] = None,
        stop: Optional[Union[str, List[str]]] = None,
        presence_penalty: Optional[float] = None,
        frequency_penalty: Optional[float] = None,
        seed: Optional[int] = None,
        response_format: Optional[Dict[str, Any]] = None,
        **kwargs,
    ) -> AsyncGenerator:
        if model in Antigravity.model_aliases:
            model = Antigravity.model_aliases[model]

        await self.auth_manager.initialize_auth()
        project_id = await self.discover_project_id()

        contents = self._messages_to_gemini_format(
            [m for m in messages if m["role"] not in ["developer", "system"]],
            media=kwargs.get("media", None),
        )
        system_prompt = get_system_prompt(messages)
        request_data = {}
        if system_prompt:
            request_data["system_instruction"] = {"parts": [{"text": system_prompt}]}

        gemini_tools = None
        function_declarations = []
        if tools:
            for tool in tools:
                if tool.get("type") == "function" and "function" in tool:
                    func = tool["function"]
                    function_declarations.append(
                        {
                            "name": func.get("name"),
                            "description": func.get("description", ""),
                            "parameters": _sanitize_schema(func.get("parameters", {})),
                        }
                    )
            if function_declarations:
                gemini_tools = [{"functionDeclarations": function_declarations}]

        generation_config = {
            "maxOutputTokens": max_tokens,
            "temperature": temperature,
            "topP": top_p,
            "stop": stop,
            "presencePenalty": presence_penalty,
            "frequencyPenalty": frequency_penalty,
            "seed": seed,
        }

        if response_format is not None and response_format.get("type") == "json_object":
            generation_config["responseMimeType"] = "application/json"

        if thinking_budget is not None:
            generation_config["thinkingConfig"] = {
                "thinkingBudget": thinking_budget,
                "includeThoughts": True,
            }

        req_body = {
            "contents": contents,
            "generationConfig": generation_config,
            "tools": gemini_tools,
            **request_data,
        }

        if tool_choice and gemini_tools:
            mode = tool_choice.upper()
            function_calling_config = {"mode": mode}
            if mode == "ANY":
                function_calling_config["allowedFunctionNames"] = [
                    fd["name"] for fd in function_declarations
                ]
            req_body["toolConfig"] = {
                "functionCallingConfig": function_calling_config
            }

        def clean_none(d):
            if isinstance(d, dict):
                return {k: clean_none(v) for k, v in d.items() if v is not None}
            if isinstance(d, list):
                return [clean_none(x) for x in d if x is not None]
            return d

        req_body = clean_none(req_body)

        payload, selected_model, actual_model_name = build_antigravity_payload(
            model, req_body, project_id, self.available_models
        )

        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.auth_manager.get_access_token()}",
            "Accept": "text/event-stream",
            **ANTIGRAVITY_HEADERS,
        }

        base_url = self.auth_manager.get_working_base_url()
        url = f"{base_url}:streamGenerateContent?alt=sse"

        async def parse_sse_stream(
            stream: aiohttp.StreamReader,
        ) -> AsyncGenerator[Dict[str, Any], None]:
            buffer = ""
            raw_lines = []
            yield_count = 0
            async for chunk_bytes in stream.iter_any():
                chunk = chunk_bytes.decode("utf-8", errors="replace")
                buffer += chunk
                lines = buffer.split("\n")
                buffer = lines.pop()

                for line in lines:
                    trimmed = line.strip()
                    if yield_count == 0 and trimmed:
                        if len(raw_lines) < ANTIGRAVITY_RAW_FALLBACK_MAX_LINES:
                            raw_lines.append(trimmed)

                    if trimmed.startswith("data: "):
                        processed_line = filter_sse_usage_metadata(trimmed)
                        json_str = processed_line[6:]
                        try:
                            data = json.loads(json_str)
                            yield_count += 1
                            yield data
                        except Exception as e:
                            debug.error(f"Error parsing SSE JSON: {e}")
                    elif trimmed == "" and raw_lines and yield_count > 0:
                        pass

            if buffer.strip():
                trimmed = buffer.strip()
                if trimmed.startswith("data: "):
                    processed_line = filter_sse_usage_metadata(trimmed)
                    json_str = processed_line[6:]
                    try:
                        yield json.loads(json_str)
                        yield_count += 1
                    except Exception as e:
                        debug.error(f"Error parsing final SSE JSON: {e}")

            if yield_count == 0 and raw_lines:
                raw_body = "\n".join(raw_lines)
                try:
                    parsed = json.loads(raw_body)
                    items = parsed if isinstance(parsed, list) else [parsed]
                    for item in items:
                        if isinstance(item, dict):
                            yield item
                except Exception as e:
                    debug.error(f"Failed to parse raw fallback JSON: {e}")

        timeout = ClientTimeout(total=None)
        connector = get_connector(None, proxy)

        async with ClientSession(
            headers=headers, timeout=timeout, connector=connector
        ) as session:
            async with session.post(url, json=payload) as resp:
                if not resp.ok:
                    if resp.status == 503:
                        try:
                            body = await resp.json(content_type=None)
                            retry_delay = int(
                                max(
                                    [
                                        float(d.get("retryDelay", 0))
                                        for d in body.get("error", {}).get(
                                            "details", []
                                        )
                                    ]
                                )
                            )
                        except Exception:
                            retry_delay = 30
                        debug.log(
                            f"Received 503 error, retrying after {retry_delay}s"
                        )
                        if retry_delay <= 120:
                            await asyncio.sleep(retry_delay)
                            resp = await session.post(url, json=payload)
                await raise_for_status(resp)

                usage_metadata = {}
                openai_tool_calls = []
                tool_calls_index = 0
                async for json_data in parse_sse_stream(resp.content):
                    candidates = json_data.get("response", {}).get(
                        "candidates", []
                    ) or json_data.get("candidates", [])
                    usage_metadata = (
                        json_data.get("response", {}).get("usageMetadata")
                        or json_data.get("usageMetadata")
                        or usage_metadata
                    )

                    if not candidates:
                        continue

                    candidate = candidates[0]
                    content = candidate.get("content", {})
                    parts = content.get("parts", [])
                    tool_calls = []

                    for part in parts:
                        if part.get("thought") is True and "text" in part:
                            yield Reasoning(part["text"])

                        elif "functionCall" in part:
                            tool_calls.append(part)

                        elif "text" in part:
                            yield part["text"]

                        elif "inlineData" in part:
                            async for media_chunk in save_response_media(
                                part["inlineData"], format_media_prompt(messages)
                            ):
                                yield media_chunk

                        elif "fileData" in part:
                            file_data = part["fileData"]
                            yield ImageResponse(file_data.get("fileUri"))

                    if tool_calls:
                        for i, part in enumerate(tool_calls):
                            tc = part["functionCall"]
                            tool_call_obj = {
                                "index": tool_calls_index,
                                "id": tc.get(
                                    "id",
                                    f"call_{i}_{tc.get('name', 'unknown')}",
                                ),
                                "type": "function",
                                "function": {
                                    "name": tc.get("name"),
                                    "arguments": json.dumps(tc.get("args", {})),
                                },
                            }
                            if "thoughtSignature" in part:
                                tool_call_obj["extra_content"] = {
                                    "google": {
                                        "thought_signature": part[
                                            "thoughtSignature"
                                        ]
                                    }
                                }
                            openai_tool_calls.append(tool_call_obj)
                            tool_calls_index += 1

                if openai_tool_calls:
                    yield ToolCalls(openai_tool_calls)

                if usage_metadata:
                    yield Usage(**usage_metadata)


class Antigravity(AsyncGeneratorProvider, ProviderModelMixin):
    """
    Antigravity Provider for gpt4free (v2).

    Provides access to Google's Antigravity API (Code Assist) supporting:
    - Gemini 2.5 & Gemini 3 Pro/Flash models
    - Gemini 3.1 / 3.5 / 3.6 / 3.7 / 3.8 Flash & Pro variants
    - Claude Sonnet 4.5/4.6 & Opus 4.5/4.6 via Antigravity proxy
    - Image generation models (gemini-3.1-flash-image)

    Requires OAuth2 credentials. Set ANTIGRAVITY_SERVICE_ACCOUNT environment
    variable or create credentials at ~/.antigravity/oauth_creds.json
    """

    label = "Google Antigravity"
    url = "https://antigravity.google"
    screenshot_url = "https://antigravity.google"
    login_url = "https://cloud.google.com/code-assist"

    default_model = "gemini-3-flash"
    fallback_models = ANTIGRAVITY_MODELS

    model_aliases = {
        "claude-sonnet-4.5": "claude-sonnet-4-5",
        "claude-opus-4.5": "claude-opus-4-5",
        "claude-sonnet-4.6": "claude-sonnet-4-6",
        "claude-opus-4.6": "claude-opus-4-6-thinking",
    }

    working = True
    supports_message_history = True
    supports_system_message = True
    supports_native_tools = True
    needs_auth = True
    active_by_default = True

    auth_manager: AntigravityAuthManager = None

    @classmethod
    def get_models(cls, **kwargs) -> List[str]:
        if not cls.models and cls.has_credentials():
            try:
                get_running_loop(check_nested=True)
                cls.models = asyncio.run(cls._fetch_models())
            except Exception as e:
                debug.log(f"Failed to fetch dynamic models: {e}")

        if cls.live == 0:
            if cls.auth_manager is None:
                cls.auth_manager = AntigravityAuthManager(env=os.environ)
            if cls.auth_manager.get_access_token() is not None:
                cls.live += 1

        return cls.models if cls.models else cls.fallback_models

    @classmethod
    async def _fetch_models(cls) -> List[str]:
        if cls.auth_manager is None:
            cls.auth_manager = AntigravityAuthManager(env=os.environ)

        await cls.auth_manager.initialize_auth()

        try:
            response = await cls.auth_manager.call_endpoint(
                method="fetchAvailableModels",
                body={"project": cls.auth_manager.get_project_id()},
            )
            models_dict = response.get("models", {})
            if isinstance(models_dict, dict):
                raw_models = [
                    key
                    for key, value in models_dict.items()
                    if not value.get("isInternal", False) and not key.startswith("tab_")
                ]
                expanded_models = []
                for m in raw_models:
                    for expanded in expand_antigravity_client_models(m):
                        if expanded not in expanded_models:
                            expanded_models.append(expanded)
                return expanded_models if expanded_models else ANTIGRAVITY_MODELS
            return ANTIGRAVITY_MODELS
        except Exception as e:
            debug.log(f"Failed to fetch models: {e}")
            return ANTIGRAVITY_MODELS

    @classmethod
    async def get_quota(cls, api_key: Optional[str] = None) -> dict:
        if cls.auth_manager is None:
            cls.auth_manager = AntigravityAuthManager(env=os.environ)
        await cls.auth_manager.initialize_auth()

        access_token = cls.auth_manager.get_access_token()
        project_id = cls.auth_manager.get_project_id()
        if not access_token or not project_id:
            raise MissingAuthError("Cannot fetch usage without valid authentication")

        return await cls.auth_manager.call_endpoint(
            method="fetchAvailableModels",
            body={"project": cls.auth_manager.get_project_id()},
        )

    @classmethod
    async def create_async_generator(
        cls,
        model: str,
        messages: Messages,
        stream: bool = False,
        media: MediaListType = None,
        tools: Optional[list] = None,
        **kwargs,
    ) -> AsyncResult:
        if cls.auth_manager is None:
            cls.auth_manager = AntigravityAuthManager(env=os.environ)

        if model in cls.model_aliases:
            model = cls.model_aliases[model]

        provider = AntigravityProvider(env=os.environ, auth_manager=cls.auth_manager)
        async for chunk in provider.stream_content(
            model=model,
            messages=messages,
            stream=stream,
            media=media,
            tools=tools,
            **kwargs,
        ):
            yield chunk

    @classmethod
    async def login(
        cls,
        project_id: str = "",
        no_browser: bool = False,
        credentials_path: Optional[Path] = None,
    ) -> "AntigravityAuthManager":
        auth_manager = await AntigravityAuthManager.login_and_save(
            project_id=project_id,
            no_browser=no_browser,
            credentials_path=credentials_path,
        )
        cls.auth_manager = auth_manager
        return auth_manager

    @classmethod
    def has_credentials(cls) -> bool:
        cache_path = AntigravityAuthManager.get_cache_file()
        if cache_path.exists():
            return True
        default_path = get_antigravity_oauth_creds_path()
        if default_path.exists():
            return True
        if "ANTIGRAVITY_SERVICE_ACCOUNT" in os.environ:
            return True
        return False

    @classmethod
    def get_credentials_path(cls) -> Path:
        cache_path = AntigravityAuthManager.get_cache_file()
        if cache_path.exists():
            return cache_path
        default_path = get_antigravity_oauth_creds_path()
        if default_path.exists():
            return default_path
        return cache_path


async def main(args: Optional[List[str]] = None):
    import argparse

    parser = argparse.ArgumentParser(
        description="Antigravity OAuth Authentication for gpt4free (v2)",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  %(prog)s login                    # Interactive login with browser
  %(prog)s login --no-browser       # Manual login (paste URL)
  %(prog)s login --project-id ID    # Login with specific project
  %(prog)s status                   # Check authentication status
  %(prog)s logout                   # Remove saved credentials
""",
    )

    subparsers = parser.add_subparsers(dest="command", help="Commands")
    login_parser = subparsers.add_parser("login", help="Authenticate with Google")
    login_parser.add_argument(
        "--project-id",
        "-p",
        default="",
        help="Google Cloud project ID (optional, auto-discovered if not set)",
    )
    login_parser.add_argument(
        "--no-browser",
        "-n",
        action="store_true",
        help="Don't auto-open browser, print URL instead",
    )

    subparsers.add_parser("status", help="Check authentication status")
    subparsers.add_parser("logout", help="Remove saved credentials")

    args = parser.parse_args(args)

    if args.command == "login":
        try:
            await Antigravity.login(
                project_id=args.project_id,
                no_browser=args.no_browser,
            )
        except KeyboardInterrupt:
            print("\n\nLogin cancelled.")
            sys.exit(1)
        except Exception as e:
            print(f"\n❌ Login failed: {e}")
            sys.exit(1)

    elif args.command == "status":
        print("\nAntigravity Authentication Status")
        print("=" * 40)

        if Antigravity.has_credentials():
            creds_path = Antigravity.get_credentials_path()
            print(f"✓ Credentials found at: {creds_path}")
            try:
                with creds_path.open() as f:
                    creds = json.load(f)

                if creds.get("email"):
                    print(f"  Email: {creds['email']}")
                if creds.get("project_id"):
                    print(f"  Project: {creds['project_id']}")

                expiry = creds.get("expiry_date")
                if expiry:
                    expiry_time = time.strftime(
                        "%Y-%m-%d %H:%M:%S", time.localtime(expiry / 1000)
                    )
                    if expiry / 1000 > time.time():
                        print(f"  Token expires: {expiry_time}")
                    else:
                        print(f"  Token expired: {expiry_time} (will auto-refresh)")
            except Exception as e:
                print(f"  (Could not read credential details: {e})")
        else:
            print("✗ No credentials found")
            print("\nRun 'antigravity login' to authenticate.")
        print()

    elif args.command == "logout":
        print("\nAntigravity Logout")
        print("=" * 40)
        removed = False

        cache_path = AntigravityAuthManager.get_cache_file()
        if cache_path.exists():
            cache_path.unlink()
            print(f"✓ Removed: {cache_path}")
            removed = True

        default_path = get_antigravity_oauth_creds_path()
        if default_path.exists():
            default_path.unlink()
            print(f"✓ Removed: {default_path}")
            removed = True

        if removed:
            print("\n✓ Credentials removed successfully.")
        else:
            print("No credentials found to remove.")
        print()
    else:
        parser.print_help()


def cli_main(args: Optional[List[str]] = None):
    asyncio.run(main(args))


if __name__ == "__main__":
    cli_main()