feat:智能体库user_agents表存储修改

This commit is contained in:
liailing1026
2026-03-20 14:24:16 +08:00
parent 932629f224
commit de5bc2109e

View File

@@ -1643,11 +1643,15 @@ def handle_set_agents_ws(data):
"profile": item["Profile"], "profile": item["Profile"],
"Icon": item.get("Icon", ""), "Icon": item.get("Icon", ""),
"Classification": item.get("Classification", ""), "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 "useCustomAPI": False
} }
# 只有 agent.json 里有配置时才写入数据库,否则动态读取 config.yaml
if item.get("apiUrl"):
agent_config["apiUrl"] = item["apiUrl"]
if item.get("apiKey"):
agent_config["apiKey"] = item["apiKey"]
if item.get("apiModel"):
agent_config["apiModel"] = item["apiModel"]
AgentProfile_Dict[name] = agent_config AgentProfile_Dict[name] = agent_config
# 保存到数据库(仅当 save_to_db 为 true 时) # 保存到数据库(仅当 save_to_db 为 true 时)
@@ -1693,6 +1697,9 @@ def handle_get_agents_ws(data):
从 user_agents 数据库表读取 从 user_agents 数据库表读取
如果没有 user_id会生成新的 user_id 并返回 如果没有 user_id会生成新的 user_id 并返回
""" """
import json
import os
request_id = data.get('id') request_id = data.get('id')
# 前端发送的数据在 data 字段中 # 前端发送的数据在 data 字段中
incoming_data = data.get('data', {}) incoming_data = data.get('data', {})
@@ -1717,6 +1724,52 @@ def handle_get_agents_ws(data):
print(f"[get_agents] 用户 {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') user_agents = UserAgentCRUD.get_by_user_id(db=db, user_id='default_user')
# 如果 default_user 也没有配置,则自动从 agent.json 读取并写入数据库
if not user_agents:
print(f"[get_agents] default_user 无配置,尝试从 agent.json 初始化")
# 尝试读取 agent.json 文件
possible_paths = [
os.path.join(os.getcwd(), "..", "frontend", "public", "agent.json"),
os.path.join(os.getcwd(), "frontend", "public", "agent.json"),
os.path.join(os.getcwd(), "public", "agent.json"),
]
default_agents = None
for path in possible_paths:
if os.path.exists(path):
try:
with open(path, "r", encoding="utf-8") as f:
default_agents = json.load(f)
print(f"[get_agents] 从 {path} 读取到默认智能体配置")
break
except Exception as e:
print(f"[get_agents] 读取 {path} 失败: {e}")
if default_agents and isinstance(default_agents, list):
# 写入数据库作为 default_user
for agent in default_agents:
agent_config = {
"profile": agent.get("Profile", ""),
"Icon": agent.get("Icon", ""),
"Classification": agent.get("Classification", ""),
"useCustomAPI": False
}
# 只有 agent.json 里有配置时才写入数据库,否则动态读取 config.yaml
if agent.get("apiUrl"):
agent_config["apiUrl"] = agent["apiUrl"]
if agent.get("apiKey"):
agent_config["apiKey"] = agent["apiKey"]
if agent.get("apiModel"):
agent_config["apiModel"] = agent["apiModel"]
UserAgentCRUD.upsert(
db=db,
user_id="default_user",
agent_name=agent.get("Name", ""),
agent_config=agent_config,
)
print(f"[get_agents] 成功写入 {len(default_agents)} 个默认智能体到数据库")
# 重新查询
user_agents = UserAgentCRUD.get_by_user_id(db=db, user_id='default_user')
# 转换为前端期望的格式 # 转换为前端期望的格式
agents = [] agents = []
for ua in user_agents: for ua in user_agents: