Files
ewoooc/services/gemini_guard.py
OoO a46396ca7f
All checks were successful
CD Pipeline / deploy (push) Successful in 1m6s
[V10.350] 關閉 Gemini 預設備援出站
2026-05-20 20:10:21 +08:00

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}"