fix: 通用智能体注入当前登录用户的 api_key

通用智能体没有 owner,配置文件默认也不写 api_key。
复用普通 bot 的 owner token 逻辑:按 user_identifier 查 agent_user.new_api_token。
若配置文件里显式配置了 api_key,则优先用配置的。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
朱潮 2026-06-21 17:17:29 +08:00
parent cf5cbe06b9
commit f4f39e4356

View File

@ -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: