34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Central guard for Gemini API egress.
|
|
|
|
Gemini is fallback-only in this project. The fallback is disabled by default and
|
|
must be explicitly enabled with GEMINI_FALLBACK_ENABLED=true before any code path
|
|
may initialize the SDK or call the REST API.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
|
|
_TRUE_VALUES = {"1", "true", "yes", "on"}
|
|
|
|
|
|
def is_gemini_fallback_enabled(context: str | None = None) -> bool:
|
|
"""Return whether Gemini fallback traffic is allowed for this process."""
|
|
return os.getenv("GEMINI_FALLBACK_ENABLED", "false").strip().lower() in _TRUE_VALUES
|
|
|
|
|
|
def get_gemini_api_key(context: str | None = None) -> str:
|
|
"""Return the Gemini API key only when fallback traffic is enabled."""
|
|
if not is_gemini_fallback_enabled(context):
|
|
return ""
|
|
return os.getenv("GEMINI_API_KEY", "")
|
|
|
|
|
|
def gemini_disabled_message(context: str | None = None) -> str:
|
|
"""Human-readable reason for telemetry and error paths."""
|
|
suffix = f" ({context})" if context else ""
|
|
return f"Gemini fallback disabled by GEMINI_FALLBACK_ENABLED=false{suffix}"
|