feat:智能体配置加载数据库默认配置
This commit is contained in:
@@ -1604,53 +1604,68 @@ def handle_agent_select_modify_delete_aspect_ws(data):
|
||||
def handle_set_agents_ws(data):
|
||||
"""
|
||||
WebSocket版本:设置智能体
|
||||
保存到 user_agents 数据库表
|
||||
保存到 user_agents 数据库表(可选)
|
||||
"""
|
||||
request_id = data.get('id')
|
||||
incoming_data = data.get('data', {})
|
||||
|
||||
global AgentBoard, AgentProfile_Dict, yaml_data
|
||||
|
||||
# 支持新格式:{ agents: [...], save_to_db: true/false }
|
||||
# 兼容旧格式:直接是数组 [...]
|
||||
if isinstance(incoming_data, dict):
|
||||
agents_list = incoming_data.get('agents', incoming_data)
|
||||
save_to_db = incoming_data.get('save_to_db', False)
|
||||
else:
|
||||
agents_list = incoming_data
|
||||
save_to_db = False
|
||||
|
||||
try:
|
||||
AgentBoard = incoming_data
|
||||
# 无论是否保存到数据库,都需要设置全局变量
|
||||
AgentBoard = agents_list
|
||||
AgentProfile_Dict = {}
|
||||
|
||||
# 保存到数据库
|
||||
saved_agents = []
|
||||
with get_db_context() as db:
|
||||
for item in AgentBoard:
|
||||
name = item["Name"]
|
||||
if all(item.get(field) for field in ["apiUrl", "apiKey", "apiModel"]):
|
||||
agent_config = {
|
||||
"profile": item["Profile"],
|
||||
"Icon": item.get("Icon", ""),
|
||||
"Classification": item.get("Classification", ""),
|
||||
"apiUrl": item["apiUrl"],
|
||||
"apiKey": item["apiKey"],
|
||||
"apiModel": item["apiModel"],
|
||||
"useCustomAPI": True
|
||||
}
|
||||
else:
|
||||
agent_config = {
|
||||
"profile": item["Profile"],
|
||||
"Icon": item.get("Icon", ""),
|
||||
"Classification": item.get("Classification", ""),
|
||||
"apiUrl": yaml_data.get("OPENAI_API_BASE"),
|
||||
"apiKey": yaml_data.get("OPENAI_API_KEY"),
|
||||
"apiModel": yaml_data.get("OPENAI_API_MODEL"),
|
||||
"useCustomAPI": False
|
||||
}
|
||||
AgentProfile_Dict[name] = agent_config
|
||||
# 处理每个智能体配置
|
||||
for item in agents_list:
|
||||
name = item["Name"]
|
||||
if all(item.get(field) for field in ["apiUrl", "apiKey", "apiModel"]):
|
||||
agent_config = {
|
||||
"profile": item["Profile"],
|
||||
"Icon": item.get("Icon", ""),
|
||||
"Classification": item.get("Classification", ""),
|
||||
"apiUrl": item["apiUrl"],
|
||||
"apiKey": item["apiKey"],
|
||||
"apiModel": item["apiModel"],
|
||||
"useCustomAPI": True
|
||||
}
|
||||
else:
|
||||
agent_config = {
|
||||
"profile": item["Profile"],
|
||||
"Icon": item.get("Icon", ""),
|
||||
"Classification": item.get("Classification", ""),
|
||||
"apiUrl": yaml_data.get("OPENAI_API_BASE"),
|
||||
"apiKey": yaml_data.get("OPENAI_API_KEY"),
|
||||
"apiModel": yaml_data.get("OPENAI_API_MODEL"),
|
||||
"useCustomAPI": False
|
||||
}
|
||||
AgentProfile_Dict[name] = agent_config
|
||||
|
||||
# 保存到数据库(使用 upsert,相同 user_id + agent_name 则更新,否则创建)
|
||||
user_id = item.get("user_id", "default_user")
|
||||
agent = UserAgentCRUD.upsert(
|
||||
db=db,
|
||||
user_id=user_id,
|
||||
agent_name=name,
|
||||
agent_config=agent_config,
|
||||
)
|
||||
saved_agents.append(agent.to_dict())
|
||||
# 保存到数据库(仅当 save_to_db 为 true 时)
|
||||
saved_agents = []
|
||||
if save_to_db:
|
||||
with get_db_context() as db:
|
||||
for item in agents_list:
|
||||
name = item["Name"]
|
||||
agent_config = AgentProfile_Dict[name]
|
||||
# 保存到数据库(使用 upsert,相同 user_id + agent_name 则更新,否则创建)
|
||||
user_id = item.get("user_id", "default_user")
|
||||
agent = UserAgentCRUD.upsert(
|
||||
db=db,
|
||||
user_id=user_id,
|
||||
agent_name=name,
|
||||
agent_config=agent_config,
|
||||
)
|
||||
saved_agents.append(agent.to_dict())
|
||||
|
||||
# 返回结果
|
||||
emit('response', {
|
||||
@@ -1697,6 +1712,11 @@ def handle_get_agents_ws(data):
|
||||
with get_db_context() as db:
|
||||
user_agents = UserAgentCRUD.get_by_user_id(db=db, user_id=user_id)
|
||||
|
||||
# 如果用户没有配置,回退到 default_user 的默认配置
|
||||
if not user_agents and user_id != 'default_user':
|
||||
print(f"[get_agents] 用户 {user_id} 无配置,回退到 default_user")
|
||||
user_agents = UserAgentCRUD.get_by_user_id(db=db, user_id='default_user')
|
||||
|
||||
# 转换为前端期望的格式
|
||||
agents = []
|
||||
for ua in user_agents:
|
||||
|
||||
Reference in New Issue
Block a user