diff --git a/utils/fastapi_utils.py b/utils/fastapi_utils.py index 30290e2..ac4c5c3 100644 --- a/utils/fastapi_utils.py +++ b/utils/fastapi_utils.py @@ -606,12 +606,27 @@ async def fetch_bot_config_from_db(bot_user_id: str, user_identifier: Optional[s from agent.db_pool_manager import get_db_pool_manager from utils.settings import NEW_API_BASE_URL - # 通用智能体直接从配置文件读取 + pool = get_db_pool_manager().pool + + # 通用智能体从配置文件读取,并注入当前登录用户的 api_key if is_general_agent_id(bot_user_id): from utils.general_agent_config import get_general_agent_runtime_config - return get_general_agent_runtime_config(user_identifier) - - pool = get_db_pool_manager().pool + config = get_general_agent_runtime_config(user_identifier) + if not config.get("api_key") and user_identifier: + async with pool.connection() as conn: + async with conn.cursor() as cursor: + await cursor.execute( + "SELECT new_api_token FROM agent_user WHERE username = %s", + (user_identifier,) + ) + row = await cursor.fetchone() + if row and row[0]: + config["api_key"] = row[0] + logger.info( + f"Fetched general agent config: model={config['model']}, " + f"api_key={'*' + config['api_key'][-4:] if config.get('api_key') else 'N/A'}" + ) + return config async with pool.connection() as conn: async with conn.cursor() as cursor: