import logging import aiosqlite from config import settings as default_settings from database import get_active_db_path log = logging.getLogger(__name__) _cache: dict | None = None def invalidate_cache(): global _cache _cache = None async def get_ai_settings() -> dict: global _cache if _cache is not None: return _cache # ai_base_url 保留默认值(阿里云兼容 OpenAI 格式地址),其余字段必须由用户在设置页配置 result = { "ai_base_url": default_settings.ai_base_url, "ai_api_key": "", "ai_model": "", "summary_model": "", "vision_model": "", "voice_model": "", "topic_analysis_prompt": "", # 语音/视觉/报告生成独立网关与密钥;留空则由调用方回退到 ai_base_url / ai_api_key "voice_base_url": "", "voice_api_key": "", "vision_base_url": "", "vision_api_key": "", "summary_base_url": "", "summary_api_key": "", } try: path = get_active_db_path() async with aiosqlite.connect(path) as db: db.row_factory = aiosqlite.Row async with db.execute("SELECT key, value FROM app_settings") as cur: rows = await cur.fetchall() for row in rows: if row["key"] in result and row["value"]: result[row["key"]] = row["value"] except Exception as e: log.warning(f"Failed to read runtime settings: {e}") _cache = result return result