46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
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": "",
|
|
}
|
|
|
|
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
|