From f4f39e43569e2fff2ab632700d44ce50f836d2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Sun, 21 Jun 2026 17:17:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=80=9A=E7=94=A8=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E4=BD=93=E6=B3=A8=E5=85=A5=E5=BD=93=E5=89=8D=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=20api=5Fkey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通用智能体没有 owner,配置文件默认也不写 api_key。 复用普通 bot 的 owner token 逻辑:按 user_identifier 查 agent_user.new_api_token。 若配置文件里显式配置了 api_key,则优先用配置的。 Co-Authored-By: Claude Opus 4.7 --- utils/fastapi_utils.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) 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: