Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
"""
Configuration constants for the Geminicli2api proxy server.
Centralizes all configuration to avoid duplication across modules.

- 启动时加载一次配置到内存
- 修改配置时调用 reload_config() 重新从数据库加载
"""

import os
from typing import Any, Optional
from dotenv import load_dotenv
load_dotenv()
from src.i18n import ts

# 全局配置缓存
# {ts(f"id_649")}
_config_cache: dict[str, Any] = {}
_config_initialized = False

# Client Configuration

# 需要自动封禁的错误码 (默认值,可通过环境变量或配置覆盖)
# {ts(f"id_651")} ({ts('id_650')})
AUTO_BAN_ERROR_CODES = [403]

# ====================== 环境变量映射表 ======================
# 统一维护环境变量名和配置键名的映射关系
# 格式: "环境变量名": "配置键名"
# ====================== {ts(f"id_652")} ======================
# {ts(f"id_653")}
# {ts(f"id_57")}: "{ts('id_654')}": f"{ts('id_655')}"
ENV_MAPPINGS = {
"CODE_ASSIST_ENDPOINT": "code_assist_endpoint",
"CREDENTIALS_DIR": "credentials_dir",
Expand All @@ -44,13 +44,14 @@
"API_PASSWORD": "api_password",
"PANEL_PASSWORD": "panel_password",
"PASSWORD": "password",
"I18N_LANG": "i18n_lang",
}


# ====================== 配置系统 ======================
# ====================== {ts(f"id_656")} ======================

async def init_config():
"""初始化配置缓存(启动时调用一次)"""
f"""{ts('id_657')}"""
global _config_cache, _config_initialized

if _config_initialized:
Expand All @@ -62,38 +63,38 @@ async def init_config():
_config_cache = await storage_adapter.get_all_config()
_config_initialized = True
except Exception:
# 初始化失败时使用空缓存
# {ts(f"id_658")}
_config_cache = {}
_config_initialized = True


async def reload_config():
"""重新加载配置(修改配置后调用)"""
f"""{ts('id_659')}"""
global _config_cache, _config_initialized

try:
from src.storage_adapter import get_storage_adapter
storage_adapter = await get_storage_adapter()

# 如果后端支持 reload_config_cache,调用它
# {ts(f"id_660")} reload_config_cache{ts('id_661')}
if hasattr(storage_adapter._backend, 'reload_config_cache'):
await storage_adapter._backend.reload_config_cache()

# 重新加载配置缓存
# {ts(f"id_662")}
_config_cache = await storage_adapter.get_all_config()
_config_initialized = True
except Exception:
pass


def _get_cached_config(key: str, default: Any = None) -> Any:
"""从内存缓存获取配置(同步)"""
f"""{ts('id_663')}"""
return _config_cache.get(key, default)


async def get_config_value(key: str, default: Any = None, env_var: Optional[str] = None) -> Any:
"""Get configuration value with priority: ENV > Storage > default."""
# 确保配置已初始化
# {ts(f"id_664")}
if not _config_initialized:
await init_config()

Expand Down Expand Up @@ -235,12 +236,12 @@ async def get_api_password() -> str:
Database config key: api_password
Default: Uses PASSWORD env var for compatibility, otherwise 'pwd'
"""
# 优先使用 API_PASSWORD,如果没有则使用通用 PASSWORD 保证兼容性
# {ts(f"id_667")} API_PASSWORD{ts('id_665')} PASSWORD {ts('id_666')}
api_password = await get_config_value("api_password", None, "API_PASSWORD")
if api_password is not None:
return str(api_password)

# 兼容性:使用通用密码
# {ts(f"id_668")}
return str(await get_config_value("password", "pwd", "PASSWORD"))


Expand All @@ -252,12 +253,12 @@ async def get_panel_password() -> str:
Database config key: panel_password
Default: Uses PASSWORD env var for compatibility, otherwise 'pwd'
"""
# 优先使用 PANEL_PASSWORD,如果没有则使用通用 PASSWORD 保证兼容性
# {ts(f"id_667")} PANEL_PASSWORD{ts('id_665')} PASSWORD {ts('id_666')}
panel_password = await get_config_value("panel_password", None, "PANEL_PASSWORD")
if panel_password is not None:
return str(panel_password)

# 兼容性:使用通用密码
# {ts(f"id_668")}
return str(await get_config_value("password", "pwd", "PASSWORD"))


Expand Down Expand Up @@ -302,8 +303,8 @@ async def get_compatibility_mode_enabled() -> bool:
"""
Get compatibility mode setting.

兼容性模式:启用后所有system消息全部转换成user,停用system_instructions。
该选项可能会降低模型理解能力,但是能避免流式空回的情况。
{ts(f"id_669")}system{ts('id_670')}user{ts('id_671')}system_instructions{ts('id_672')}
{ts(f"id_673")}

Environment variable: COMPATIBILITY_MODE
Database config key: compatibility_mode_enabled
Expand All @@ -320,8 +321,8 @@ async def get_return_thoughts_to_frontend() -> bool:
"""
Get return thoughts to frontend setting.

控制是否将思维链返回到前端。
启用后,思维链会在响应中返回;禁用后,思维链会在响应中被过滤掉。
{ts(f"id_674")}
{ts(f"id_675")}

Environment variable: RETURN_THOUGHTS_TO_FRONTEND
Database config key: return_thoughts_to_frontend
Expand All @@ -338,8 +339,8 @@ async def get_antigravity_stream2nostream() -> bool:
"""
Get use stream for non-stream setting.

控制antigravity非流式请求是否使用流式API并收集为完整响应。
启用后,非流式请求将在后端使用流式API,然后收集所有块后再返回完整响应。
{ts(f"id_678")}antigravity{ts('id_676')}API{ts('id_677')}
{ts(f"id_680")}API{ts('id_679')}

Environment variable: ANTIGRAVITY_STREAM2NOSTREAM
Database config key: antigravity_stream2nostream
Expand All @@ -356,7 +357,7 @@ async def get_oauth_proxy_url() -> str:
"""
Get OAuth proxy URL setting.

用于Google OAuth2认证的代理URL。
{ts(f"id_14")}Google OAuth2{ts('id_59')}URL{ts('id_672')}

Environment variable: OAUTH_PROXY_URL
Database config key: oauth_proxy_url
Expand All @@ -373,7 +374,7 @@ async def get_googleapis_proxy_url() -> str:
"""
Get Google APIs proxy URL setting.

用于Google APIs调用的代理URL。
{ts(f"id_14")}Google APIs{ts('id_60')}URL{ts('id_672')}

Environment variable: GOOGLEAPIS_PROXY_URL
Database config key: googleapis_proxy_url
Expand All @@ -390,7 +391,7 @@ async def get_resource_manager_api_url() -> str:
"""
Get Google Cloud Resource Manager API URL setting.

用于Google Cloud Resource Manager API的URL。
{ts(f"id_14")}Google Cloud Resource Manager API{ts('id_61')}URL{ts('id_672')}

Environment variable: RESOURCE_MANAGER_API_URL
Database config key: resource_manager_api_url
Expand All @@ -409,7 +410,7 @@ async def get_service_usage_api_url() -> str:
"""
Get Google Cloud Service Usage API URL setting.

用于Google Cloud Service Usage API的URL。
{ts(f"id_14")}Google Cloud Service Usage API{ts('id_61')}URL{ts('id_672')}

Environment variable: SERVICE_USAGE_API_URL
Database config key: service_usage_api_url
Expand All @@ -426,7 +427,7 @@ async def get_antigravity_api_url() -> str:
"""
Get Antigravity API URL setting.

用于Google Antigravity API的URL。
{ts(f"id_14")}Google Antigravity API{ts('id_61')}URL{ts('id_672')}

Environment variable: ANTIGRAVITY_API_URL
Database config key: antigravity_api_url
Expand All @@ -439,3 +440,7 @@ async def get_antigravity_api_url() -> str:
"ANTIGRAVITY_API_URL",
)
)

async def get_i18n_lang() -> str:
"""Get i18n language setting."""
return str(await get_config_value("i18n_lang", "zh", "I18N_LANG"))
Loading