From c456f2e51bc2bf59233f75eeb5ed517ce9d7ba68 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期三, 18 十二月 2024 16:00:41 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/rag-gateway

---
 app/api/v2/public_api.py          |   16 ++++++++++++----
 app/service/v2/initialize_data.py |   38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/app/api/v2/public_api.py b/app/api/v2/public_api.py
index ee02589..633db26 100644
--- a/app/api/v2/public_api.py
+++ b/app/api/v2/public_api.py
@@ -1,7 +1,7 @@
 import json
 
 from fastapi import APIRouter, Depends
-
+from fastapi.responses import JSONResponse
 from Log import logger
 from app.api import Response
 from app.api.auth import login
@@ -10,7 +10,7 @@
 from app.models.public_api_model import DfToken, AppRegister
 from app.service.v2.api_token import DfTokenDao
 from app.service.v2.app_register import AppRegisterDao
-from app.service.v2.initialize_data import dialog_menu_sync, user_update_app
+from app.service.v2.initialize_data import dialog_menu_sync, create_menu_sync, user_update_app
 from app.task.sync_resources import sync_knowledge, sync_dialog, sync_agent, sync_llm, sync_resource
 
 public_api = APIRouter()
@@ -38,7 +38,6 @@
 
     try:
         for app_id, status in app_dict.items():
-
             AppRegisterDao(db).update_and_insert_app(app_id, status)
     except Exception as e:
         logger.error(e)
@@ -48,7 +47,7 @@
 
 
 @public_api.get("/sync/resource", response_model=Response)
-async def sync_resource_data(resource_type:int, db=Depends(get_db)):
+async def sync_resource_data(resource_type: int, db=Depends(get_db)):
     if resource_type == 1:
         await sync_knowledge()
     elif resource_type == 2:
@@ -74,6 +73,15 @@
     return Response(code=200, msg="success", data={})
 
 
+@public_api.get("/sync/create/dialog_menu", response_model=Response)
+async def get_dialog_menu(db=Depends(get_db)):
+    try:
+        result = await create_menu_sync(db)
+        return JSONResponse(content={"code": 200, "msg": "", "data": result})
+    except Exception as e:
+        return JSONResponse(content={"code": 500, "msg": str(e), "data": {"error": "Failed to create menu"}})
+
+
 @public_api.post("/sync/update_app", response_model=Response)
 async def sync_update_app(userid, db=Depends(get_db)):
     try:
diff --git a/app/service/v2/initialize_data.py b/app/service/v2/initialize_data.py
index 1bb860c..a778b13 100644
--- a/app/service/v2/initialize_data.py
+++ b/app/service/v2/initialize_data.py
@@ -1,5 +1,6 @@
 import json
 import time
+import os
 from Log import logger
 from app.config.const import DIFY
 from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \
@@ -36,6 +37,41 @@
         menu_obj = WebMenuModel(**menu)
         db.add(menu_obj)
     db.commit()
+
+
+async def create_menu_sync(db):
+    json_file_path = "env_conf/menu_conf.json"
+    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()
+    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 = {
+                    'id': dialog.id,
+                    'chat_id': dialog.id,
+                    'chat_type': '',
+                    'agentType': dialog.dialog_type
+                }
+                menu['dialog'].append(new_dialog_item)
+    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)
+    with open(new_file_path, 'w', encoding='utf-8') as new_file:
+        json.dump(json_data, new_file, ensure_ascii=False, indent=4)
+    return {
+        "file_name": new_file_name,
+        "json_data": json_data
+    }
 
 
 async def default_group_sync(db):
@@ -177,4 +213,4 @@
     user_id = await user_app_dao.insert_user_app_data(username, password, email, user_id, app_id, app_type)
     if not user_id:
         raise Exception("Failed to register with app")
-    print({"msg": "User registered successfully", "userFlag": user_id})
\ No newline at end of file
+    print({"msg": "User registered successfully", "userFlag": user_id})

--
Gitblit v1.8.0