From de5bc2109ef852082aec0dfb77344bf903b496df Mon Sep 17 00:00:00 2001 From: liailing1026 <1815388873@qq.com> Date: Fri, 20 Mar 2026 14:24:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=99=BA=E8=83=BD=E4=BD=93=E5=BA=93user?= =?UTF-8?q?=5Fagents=E8=A1=A8=E5=AD=98=E5=82=A8=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/server.py | 59 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/backend/server.py b/backend/server.py index 8998d54..0fc6486 100644 --- a/backend/server.py +++ b/backend/server.py @@ -1643,11 +1643,15 @@ def handle_set_agents_ws(data): "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 } + # 只有 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 # 保存到数据库(仅当 save_to_db 为 true 时) @@ -1693,6 +1697,9 @@ def handle_get_agents_ws(data): 从 user_agents 数据库表读取 如果没有 user_id,会生成新的 user_id 并返回 """ + import json + import os + request_id = data.get('id') # 前端发送的数据在 data 字段中 incoming_data = data.get('data', {}) @@ -1717,6 +1724,52 @@ def handle_get_agents_ws(data): print(f"[get_agents] 用户 {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 = [] for ua in user_agents: