From 36c00e400801786ee0f06a50e6046f49777ed188 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期二, 14 一月 2025 15:47:31 +0800
Subject: [PATCH] tmp

---
 app/service/v2/initialize_data.py |   66 +++++++++++++++++++++++++-------
 1 files changed, 51 insertions(+), 15 deletions(-)

diff --git a/app/service/v2/initialize_data.py b/app/service/v2/initialize_data.py
index a778b13..6491b2a 100644
--- a/app/service/v2/initialize_data.py
+++ b/app/service/v2/initialize_data.py
@@ -2,7 +2,7 @@
 import time
 import os
 from Log import logger
-from app.config.const import DIFY
+from app.config.const import DIFY, ENV_CONF_PATH
 from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \
     cipher_suite
 from app.service.auth import UserAppDao
@@ -17,7 +17,7 @@
 
 async def dialog_menu_sync(db):
     menu_list = []
-    with open("env_conf/menu_conf.json", 'r', encoding='utf-8') as file:
+    with open(os.path.join(ENV_CONF_PATH, "menu_conf.json") , 'r', encoding='utf-8') as file:
         # 鍔犺浇JSON鏁版嵁
         data = json.load(file)
         menu_list = data.get("data", [])
@@ -40,29 +40,35 @@
 
 
 async def create_menu_sync(db):
-    json_file_path = "env_conf/menu_conf.json"
+    # json_file_path = "env_conf/menu_conf.json.template"
+    json_file_path = os.path.join(ENV_CONF_PATH, "menu_conf.json.template")
     with open(json_file_path, 'r', encoding='utf-8') as file:
         json_data = json.load(file).get("data", [])
-        for menu in json_data:
-            menu['dialog'].clear()
+        # for menu in json_data:
+        #     menu['dialog'].clear()
     dialogs = db.query(DialogModel).all()
 
     dialog_dict = {}
     for dialog in dialogs:
         if dialog.name not in dialog_dict:
             dialog_dict[dialog.name] = []
-        dialog_dict[dialog.name].append(dialog)
-
-    for menu in json_data:
-        if menu['title'] in dialog_dict:
-            for dialog in dialog_dict[menu['title']]:
-                new_dialog_item = {
+        dialog_dict[dialog.name].append({
                     'id': dialog.id,
                     'chat_id': dialog.id,
                     'chat_type': '',
                     'agentType': dialog.dialog_type
-                }
-                menu['dialog'].append(new_dialog_item)
+                })
+
+    for menu in json_data:
+        # if menu['title'] in dialog_dict:
+        #     for dialog in dialog_dict[menu['title']]:
+        #         new_dialog_item = {
+        #             'id': dialog.id,
+        #             'chat_id': dialog.id,
+        #             'chat_type': '',
+        #             'agentType': dialog.dialog_type
+        #         }
+        menu['dialog']= dialog_dict.get(menu['title'], [])
     json_data = {"data": json_data}
     new_file_name = f"menu_conf.json.template"
     new_file_path = os.path.join(os.path.dirname(json_file_path), new_file_name)
@@ -102,7 +108,7 @@
 
 async def app_register_sync(db):
     app_dict = {}
-    with open("env_conf/app_register_conf.json", 'r', encoding='utf-8') as file:
+    with open(os.path.join(ENV_CONF_PATH, "app_register_conf.json"), 'r', encoding='utf-8') as file:
         # 鍔犺浇JSON鏁版嵁
         app_dict = json.load(file)
         try:
@@ -114,7 +120,7 @@
 
 async def basic_agent_sync(db):
     agent_list = []
-    with open("env_conf/default_agent_conf.json", 'r', encoding='utf-8') as file:
+    with open(os.path.join(ENV_CONF_PATH, "default_agent_conf.json"), 'r', encoding='utf-8') as file:
         # 鍔犺浇JSON鏁版嵁
         agent_dict = json.load(file)
         agent_list = agent_dict.get("basic", [])
@@ -214,3 +220,33 @@
     if not user_id:
         raise Exception("Failed to register with app")
     print({"msg": "User registered successfully", "userFlag": user_id})
+
+
+async def admin_account_sync(db):
+    agent_list = []
+    with open(os.path.join(ENV_CONF_PATH, "default_agent_conf.json"), 'r', encoding='utf-8') as file:
+        # 鍔犺浇JSON鏁版嵁
+        agent_dict = json.load(file)
+        agent_list = agent_dict.get("basic", [])
+    user = db.query(UserModel).filter_by(permission="admin").first()
+    for agent in agent_list:
+        dialog = db.query(DialogModel).filter(DialogModel.id == agent["id"]).first()
+        if dialog:
+            try:
+                dialog.name = agent["name"]
+                dialog.description = agent["description"]
+                dialog.icon = agent["icon"]
+                db.commit()
+            except Exception as e:
+                logger.error(e)
+        else:
+            try:
+                dialog = DialogModel(id=agent["id"], name=agent["name"], description=agent["description"],
+                                     icon=agent["icon"], tenant_id=user.id if user else "", dialog_type="3",
+                                     agent_id=agent["id"])
+                db.add(dialog)
+                db.commit()
+                db.refresh(dialog)
+            except Exception as e:
+                print(e)
+                db.rollback()
\ No newline at end of file

--
Gitblit v1.8.0