# app/services/agent/tools/builtin/system_config_tools.py from typing import Optional, Dict, Any, List from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select, desc from app.models.user import VasUser from app.models.configuration import Configuration from app.services.configuration_service import ConfigurationService from app.services.agent.tools.registry import agent_tool from app.core.logger import logger ADMIN_CHECK_MSG = "Permission denied: This operation is restricted to system administrators (admin)" @agent_tool( name="list_system_configurations", description="查询全站全局动态环境变量与系统运行配置参数列表(包含配置项 Key、说明、更新时间)", ) async def list_system_configurations( *, db: AsyncSession, current_user: VasUser, ) -> Dict[str, Any]: """查询全站配置列表""" if current_user.role != "admin": return {"error": ADMIN_CHECK_MSG} configs = await ConfigurationService.get_all(db) items = [] for c in configs: items.append({ "id": c.id, "config_key": c.config_key, "description": c.description, "config_value": c.config_value[:100] + "..." if c.config_value and len(c.config_value) > 100 else c.config_value, "created_at": c.created_at.strftime("%Y-%m-%d %H:%M:%S") if c.created_at else None, "updated_at": c.updated_at.strftime("%Y-%m-%d %H:%M:%S") if c.updated_at else None, }) return { "count": len(items), "configurations": items, } @agent_tool( name="get_system_configuration", description="获取指定配置 Key 的完整内容与参数定义", ) async def get_system_configuration( config_key: str, *, db: AsyncSession, current_user: VasUser, ) -> Dict[str, Any]: """获取单个全局配置""" if current_user.role != "admin": return {"error": ADMIN_CHECK_MSG} clean_key = config_key.strip() try: c = await ConfigurationService.get_by_key(db, clean_key) return { "id": c.id, "config_key": c.config_key, "description": c.description, "config_value": c.config_value, "created_at": c.created_at.strftime("%Y-%m-%d %H:%M:%S") if c.created_at else None, "updated_at": c.updated_at.strftime("%Y-%m-%d %H:%M:%S") if c.updated_at else None, } except Exception as e: return {"error": f"Config key '{clean_key}' not found or error: {str(e)}"} @agent_tool( name="set_system_configuration", description="设置或修改全局动态环境变量/系统配置(支持更新已有值或创建新键值配置)", is_dangerous=True, ) async def set_system_configuration( config_key: str, config_value: str, description: Optional[str] = None, *, db: AsyncSession, current_user: VasUser, ) -> Dict[str, Any]: """更新或创建全局动态配置""" if current_user.role != "admin": return {"error": ADMIN_CHECK_MSG} clean_key = config_key.strip() if not clean_key: return {"error": "Config key cannot be empty"} stmt = select(Configuration).where(Configuration.config_key == clean_key) res = await db.execute(stmt) c = res.scalar_one_or_none() if c: c.config_value = config_value if description is not None: c.description = description await db.commit() await db.refresh(c) action = "updated" else: c = Configuration( config_key=clean_key, config_value=config_value, description=description or "", ) db.add(c) await db.commit() await db.refresh(c) action = "created" logger.info(f"⚙️ [AI_AGENT] Config {action}: key={clean_key} by admin={current_user.id}") return { "success": True, "action": action, "config_key": c.config_key, "config_value": c.config_value, "updated_at": c.updated_at.strftime("%Y-%m-%d %H:%M:%S") if c.updated_at else None, }