Commit 09368a15 by sunqi02

fix(agent): 更新从数据库同步 agent.code 的逻辑,调整关键字匹配方式

1 parent aa87ab71
......@@ -41,41 +41,33 @@ class ConfigLoader:
def sync_agent_codes_from_db(
self, agents: Dict[str, AgentConfig], db_session
) -> None:
"""从 official_site_agent_display 表同步 agent.code,失败时保留 JSON 默认值。
"""从 ai_agents 表同步 agent.code,失败时保留 JSON 默认值。
根据 AGENT_CODE_SYNC_MAP 中的映射,查询 DB 中 agent_name 对应的 agent_id,
然后更新 AgentConfig.code = str(agent_id)。
根据 AGENT_CODE_SYNC_MAP 中的关键字,模糊匹配 ai_agents.name,
将匹配到的 Agent.id 更新到 AgentConfig.code。
"""
try:
from app.model.official_site_agent_display import OfficialSiteAgentDisplay
from app.model.agent import Agent
target_names = list(AGENT_CODE_SYNC_MAP.keys())
rows = (
db_session.query(
OfficialSiteAgentDisplay.agent_name,
OfficialSiteAgentDisplay.agent_id,
)
.filter(OfficialSiteAgentDisplay.agent_name.in_(target_names))
.all()
)
name_to_id = {row.agent_name: row.agent_id for row in rows}
for db_name, internal_key in AGENT_CODE_SYNC_MAP.items():
for keyword, internal_key in AGENT_CODE_SYNC_MAP.items():
if internal_key not in agents:
continue
agent_id = name_to_id.get(db_name)
if agent_id is not None:
row = (
db_session.query(Agent.id)
.filter(Agent.name.like(f"%{keyword}%"))
.first()
)
if row is not None:
old_code = agents[internal_key].code
agents[internal_key].code = str(agent_id)
agents[internal_key].code = str(row.id)
logger.info(
"Agent code 已从 DB 同步: %s.code %s → %s",
internal_key, old_code, agent_id,
internal_key, old_code, row.id,
)
else:
logger.warning(
"DB 中未找到 agent_name='%s',保留 JSON 默认值 code='%s'",
db_name, agents[internal_key].code,
"DB 中未找到 name 包含'%s'的记录,保留 JSON 默认值 code='%s'",
keyword, agents[internal_key].code,
)
except Exception:
......
......@@ -2,11 +2,11 @@ from pydantic import BaseModel
from typing import Dict, List
# official_site_agent_display.agent_name → 内部 agent dict key
# ai_agents.name 关键字 → 内部 agent dict key
# 用于从 DB 同步 agent.code 值
AGENT_CODE_SYNC_MAP: Dict[str, str] = {
"管家 Agent": "main",
"会议agent": "meeting",
"管家": "main",
"会议": "meeting",
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!