Commit e07b3644 by sunqi02

refactor(settings): restructure AgentConfig for DB-driven config

- Delete AGENT_CODE_SYNC_MAP (no longer needed)
- Remove 'role' field from AgentConfig
- Rename prompt_template → prompt
- Add is_steward field
- Remove sync_agent_codes_from_db from loader and main

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent b9cc8894
...@@ -6,7 +6,7 @@ from typing import Dict, Optional, Tuple ...@@ -6,7 +6,7 @@ from typing import Dict, Optional, Tuple
from pydantic import ValidationError from pydantic import ValidationError
from app.config.settings import AGENT_CODE_SYNC_MAP, AgentConfig, ModelConfig, Settings from app.config.settings import AgentConfig, ModelConfig, Settings
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -38,44 +38,6 @@ class ConfigLoader: ...@@ -38,44 +38,6 @@ class ConfigLoader:
except ValidationError as exc: except ValidationError as exc:
raise ConfigLoadError(f"Agent 配置校验失败: {exc}") from exc raise ConfigLoadError(f"Agent 配置校验失败: {exc}") from exc
def sync_agent_codes_from_db(
self, agents: Dict[str, AgentConfig], db_session
) -> None:
"""从 ai_agents 表同步 agent.code,失败时保留 JSON 默认值。
根据 AGENT_CODE_SYNC_MAP 中的关键字,模糊匹配 ai_agents.name,
将匹配到的 Agent.id 更新到 AgentConfig.code。
"""
try:
from app.model.agent import Agent
for keyword, internal_key in AGENT_CODE_SYNC_MAP.items():
if internal_key not in agents:
continue
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(row.id)
logger.info(
"Agent code 已从 DB 同步: %s.code %s → %s",
internal_key, old_code, row.id,
)
else:
logger.warning(
"DB 中未找到 name 包含'%s'的记录,保留 JSON 默认值 code='%s'",
keyword, agents[internal_key].code,
)
except Exception:
logger.warning(
"从 DB 同步 agent code 失败,使用 JSON 默认值",
exc_info=True,
)
def load_skills_from_toolkit(self, toolkit) -> Dict[str, dict]: def load_skills_from_toolkit(self, toolkit) -> Dict[str, dict]:
skills = {} skills = {}
for name, skill in toolkit.skills.items(): for name, skill in toolkit.skills.items():
......
from pydantic import BaseModel from pydantic import BaseModel
from typing import Dict, List from typing import List
# ai_agents.name 关键字 → 内部 agent dict key
# 用于从 DB 同步 agent.code 值
AGENT_CODE_SYNC_MAP: Dict[str, str] = {
"管家": "main",
"会议": "meeting",
}
class ModelConfig(BaseModel): class ModelConfig(BaseModel):
...@@ -20,8 +12,8 @@ class ModelConfig(BaseModel): ...@@ -20,8 +12,8 @@ class ModelConfig(BaseModel):
class AgentConfig(BaseModel): class AgentConfig(BaseModel):
name: str name: str
code: str code: str
role: str prompt: str
prompt_template: str is_steward: bool = False
skills: List[str] = [] skills: List[str] = []
......
...@@ -64,9 +64,8 @@ async def lifespan(app: FastAPI): ...@@ -64,9 +64,8 @@ async def lifespan(app: FastAPI):
settings = config_loader.load_settings() settings = config_loader.load_settings()
db = SessionLocal() db = SessionLocal()
# 加载 Agent 配置并从 DB 同步 code 值 # 加载 Agent 配置
agents = config_loader.load_agents() agents = config_loader.load_agents()
config_loader.sync_agent_codes_from_db(agents, db)
session_service = ActiveSessionService(db=db) session_service = ActiveSessionService(db=db)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!