Commit 2f668ffc by sunqi02 Committed by 胡悦

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

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