瀏覽代碼

Merge branch 'master' of http://192.168.5.5:10010/r/rag-gateway

zhaoqingang 1 年之前
父節點
當前提交
9191081e44
共有 4 個文件被更改,包括 971 次插入35 次删除
  1. 12 6
      app/api/excel.py
  2. 862 0
      app/config/env_conf/resource.json
  3. 91 27
      app/task/fetch_agent.py
  4. 6 2
      main.py

+ 12 - 6
app/api/excel.py

@@ -135,16 +135,22 @@ async def ws_excel(websocket: WebSocket, current_user: UserModel = Depends(get_c
             await websocket.close()
 
 
-@router.get("/download/{filename}")
-async def download_file(filename: str, background_tasks: BackgroundTasks,
-                        current_user: UserModel = Depends(get_current_user)):
+@router.get("/download/excel")
+async def download_file(background_tasks: BackgroundTasks, current_user: UserModel = Depends(get_current_user)):
     user_id = str(current_user.id)
     user_excel = user_file_path(user_id, EXCEL_FILES_PATH)
     user_source = user_file_path(user_id, SOURCE_FILES_PATH)
-    file_path = os.path.join(user_excel, filename)
 
-    if not os.path.exists(file_path):
-        return JSONResponse(status_code=404, content={"error": "文件不存在"})
+    if not os.path.exists(user_excel):
+        return JSONResponse(status_code=404, content={"error": "用户目录不存在"})
+
+    excel_files = [f for f in os.listdir(user_excel) if os.path.isfile(os.path.join(user_excel, f))]
+    excel_files.sort(key=lambda x: os.path.getmtime(os.path.join(user_excel, x)), reverse=True)
+    if not excel_files:
+        return JSONResponse(status_code=404, content={"error": "用户目录内没有文件"})
+
+    filename = excel_files[0]
+    file_path = os.path.join(user_excel, filename)
 
     def delete_files_in_directory(directory):
         for root, dirs, files in os.walk(directory, topdown=False):

+ 862 - 0
app/config/env_conf/resource.json

@@ -0,0 +1,862 @@
+[
+  {
+    "id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+    "created_at": "8/8/2024 19:21:42",
+    "updated_at": "8/8/2024 19:21:42",
+    "name": "root",
+    "url": "root",
+    "path": null,
+    "perms": "root",
+    "description": null,
+    "icon": null,
+    "seq": 0,
+    "target": null,
+    "canbdeeleted": 1,
+    "resource_type_id": "3",
+    "resource_id": null,
+    "status": "1",
+    "hidden": null,
+    "children": [
+      {
+        "id": "76e81844-c3a6-4ea0-882f-150be2987d9d",
+        "created_at": "23/12/2024 09:50:31",
+        "updated_at": "23/12/2024 09:50:31",
+        "name": "智能体详情",
+        "url": "/intelligent/detail",
+        "path": "/intelligent/detail",
+        "perms": "system:intelligent:detail",
+        "description": "",
+        "icon": "",
+        "seq": 0,
+        "target": null,
+        "canbdeeleted": null,
+        "resource_type_id": "3",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "2",
+        "hidden": 0
+      },
+
+      {
+        "id": "3750795c-404b-488b-8080-89c037b4a187",
+        "created_at": "18/12/2024 19:20:09",
+        "updated_at": "20/12/2024 11:38:54",
+        "name": "小数",
+        "url": "Xiaoshu",
+        "path": "/xiaoshu",
+        "perms": "system:menu:xiaoshu",
+        "description": "",
+        "icon": "IconRobot",
+        "seq": 97,
+        "target": null,
+        "canbdeeleted": null,
+        "resource_type_id": "0",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "1",
+        "hidden": 0
+      },
+      {
+        "id": "3ea0f0b8-5160-4005-91d6-7c6650a5934c",
+        "created_at": "20/12/2024 11:38:54",
+        "updated_at": "20/12/2024 11:38:54",
+        "name": "会话历史",
+        "url": "ConversationHistory",
+        "path": "/chat/history",
+        "perms": "system:conversation:hisroty",
+        "description": "",
+        "icon": "",
+        "seq": 0,
+        "target": null,
+        "canbdeeleted": null,
+        "resource_type_id": "3",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "1",
+        "hidden": 0
+      },
+      {
+        "id": "6469b565-908a-4771-a9e3-1cf8651b26f4",
+        "created_at": "12/9/2024 08:01:13",
+        "updated_at": "20/12/2024 11:38:54",
+        "name": "会话",
+        "url": "ConversationQA",
+        "path": "/chat/qa",
+        "perms": "system:menu:conversation",
+        "description": null,
+        "icon": "iconVoice",
+        "seq": 101,
+        "target": null,
+        "canbdeeleted": 1,
+        "resource_type_id": "0",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "1",
+        "hidden": null
+      },
+      {
+        "id": "6b3640d2-6142-4dce-b09b-ce11f9e6c89c",
+        "created_at": "20/12/2024 11:38:54",
+        "updated_at": "20/12/2024 11:38:54",
+        "name": "会话进行中",
+        "url": "ConversationQA",
+        "path": "/chat/qa",
+        "perms": "system:conversation:qa",
+        "description": "",
+        "icon": "",
+        "seq": 0,
+        "target": null,
+        "canbdeeleted": null,
+        "resource_type_id": "3",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "1",
+        "hidden": 0
+      },
+      {
+        "id": "6da7445a-5980-4380-ab1d-75021c1dd269",
+        "created_at": "18/12/2024 19:20:09",
+        "updated_at": "20/12/2024 11:38:54",
+        "name": "智能体",
+        "url": "Intelligent",
+        "path": "/intelligent",
+        "perms": "system:menu:intelligent",
+        "description": "",
+        "icon": "IconRobot",
+        "seq": 100,
+        "target": null,
+        "canbdeeleted": null,
+        "resource_type_id": "0",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "1",
+        "hidden": 0
+      },
+      {
+        "id": "9fb9456c-1000-485e-9cbb-905d951b86c1",
+        "created_at": "18/12/2024 19:20:09",
+        "updated_at": "20/12/2024 11:38:54",
+        "name": "知识库管理",
+        "url": "Knowledge",
+        "path": "/knowledge",
+        "perms": "system:menu:knowledge",
+        "description": "",
+        "icon": "IconStorage",
+        "seq": 99,
+        "target": null,
+        "canbdeeleted": null,
+        "resource_type_id": "0",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "1",
+        "hidden": 0,
+        "children": [
+          {
+            "id": "2d1c2831-79f4-4817-b79f-be4e49687bd1",
+            "created_at": "19/12/2024 10:08:40",
+            "updated_at": "24/12/2024 19:15:20",
+            "name": "知识库详情",
+            "url": "KnowledgeDetail",
+            "path": "detail",
+            "perms": "system:knowledge:detail",
+            "description": "",
+            "icon": "",
+            "seq": 0,
+            "target": null,
+            "canbdeeleted": null,
+            "resource_type_id": "3",
+            "resource_id": "9fb9456c-1000-485e-9cbb-905d951b86c1",
+            "status": "1",
+            "hidden": 0
+          },
+          {
+            "id": "242060f6-741e-423a-8634-2f7ece1f0c6d",
+            "created_at": "19/12/2024 10:08:40",
+            "updated_at": "24/12/2024 19:15:20",
+            "name": "知识库删除",
+            "url": "-",
+            "path": "-",
+            "perms": "system:knowledge:delete",
+            "description": "",
+            "icon": "",
+            "seq": 0,
+            "target": null,
+            "canbdeeleted": null,
+            "resource_type_id": "1",
+            "resource_id": "9fb9456c-1000-485e-9cbb-905d951b86c1",
+            "status": "1",
+            "hidden": 0
+          },
+          {
+            "id": "25722c9e-63fe-4c80-8c05-5fcb9fb58d01",
+            "created_at": "19/12/2024 10:08:40",
+            "updated_at": "24/12/2024 19:15:20",
+            "name": "知识库新增",
+            "url": "-",
+            "path": "-",
+            "perms": "system:knowledge:add",
+            "description": "",
+            "icon": "",
+            "seq": 0,
+            "target": null,
+            "canbdeeleted": null,
+            "resource_type_id": "1",
+            "resource_id": "9fb9456c-1000-485e-9cbb-905d951b86c1",
+            "status": "0",
+            "hidden": 0
+          }
+        ]
+      },
+      {
+        "id": "a4931c80-c890-4142-a698-87940c937b57",
+        "created_at": "20/12/2024 11:38:54",
+        "updated_at": "20/12/2024 11:38:54",
+        "name": "会话面板",
+        "url": "ConversationBoard",
+        "path": "/chat/board",
+        "perms": "system:conversation:board",
+        "description": "",
+        "icon": "",
+        "seq": 0,
+        "target": null,
+        "canbdeeleted": null,
+        "resource_type_id": "3",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "1",
+        "hidden": 0
+      },
+      {
+        "id": "online",
+        "created_at": "25/8/2015 10:34:53",
+        "updated_at": "20/12/2024 11:38:54",
+        "name": "模型管理",
+        "url": "Model",
+        "path": "/model",
+        "perms": "system:menu:model",
+        "description": "监控用户登录、注销",
+        "icon": "IconRelation",
+        "seq": 98,
+        "target": null,
+        "canbdeeleted": 1,
+        "resource_type_id": "0",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "1",
+        "hidden": null
+      },
+      {
+        "id": "xtgl",
+        "created_at": "25/8/2015 10:34:53",
+        "updated_at": "20/12/2024 11:38:54",
+        "name": "权限管理",
+        "url": "layout/LayoutPermission",
+        "path": "/permission",
+        "perms": "system:menu:permission",
+        "description": "管理系统的资源、角色、机构、用户等信息",
+        "icon": "IconLock",
+        "seq": 96,
+        "target": null,
+        "canbdeeleted": 1,
+        "resource_type_id": "0",
+        "resource_id": "1eed48e2-d510-42f2-9495-bc299bae237b",
+        "status": "1",
+        "hidden": null,
+        "children": [
+          {
+            "id": "0642e4a6-3d48-4635-ba2a-bf4e39c351ed",
+            "created_at": "28/10/2024 07:38:41",
+            "updated_at": "18/12/2024 20:05:23",
+            "name": "用户组管理",
+            "url": "PermissionGroup",
+            "path": "/permission/group",
+            "perms": "system:group",
+            "description": "管理系统用户组",
+            "icon": null,
+            "seq": 0,
+            "target": null,
+            "canbdeeleted": 1,
+            "resource_type_id": "0",
+            "resource_id": "xtgl",
+            "status": "1",
+            "hidden": null,
+            "children": [
+              {
+                "id": "0bcb1f37-f87e-4f47-ba72-a2e194884b8f",
+                "created_at": "28/10/2024 07:41:16",
+                "updated_at": "18/12/2024 15:09:28",
+                "name": "编辑用户组",
+                "url": "/base/syuser!update",
+                "path": "",
+                "perms": "system:group:edit",
+                "description": "查看用户组",
+                "icon": null,
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "0642e4a6-3d48-4635-ba2a-bf4e39c351ed",
+                "status": "1",
+                "hidden": null
+              },
+
+              {
+                "id": "2648f4bd-b5d5-4a34-8b9c-128fd970b014",
+                "created_at": "18/12/2024 15:09:28",
+                "updated_at": "18/12/2024 15:09:28",
+                "name": "用户组成员管理",
+                "url": "-",
+                "path": "",
+                "perms": "system:group:memberConfig",
+                "description": "",
+                "icon": "",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": null,
+                "resource_type_id": "1",
+                "resource_id": "0642e4a6-3d48-4635-ba2a-bf4e39c351ed",
+                "status": "1",
+                "hidden": 0
+              },
+
+              {
+                "id": "3daf1a03-c0c9-4ebd-935d-fe46561971fb",
+                "created_at": "28/10/2024 07:46:14",
+                "updated_at": "28/10/2024 07:46:41",
+                "name": "用户组列表",
+                "url": "/base/syuser!grid",
+                "path": null,
+                "perms": "system:group:list",
+                "description": null,
+                "icon": null,
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "0642e4a6-3d48-4635-ba2a-bf4e39c351ed",
+                "status": "1",
+                "hidden": null
+              },
+
+              {
+                "id": "72fe917f-70ee-4cbf-8c21-f1852f3ef037",
+                "created_at": "18/12/2024 15:09:28",
+                "updated_at": "19/12/2024 16:08:38",
+                "name": "用户组权限管理",
+                "url": "-",
+                "path": "",
+                "perms": "system:group:permissionConfig",
+                "description": "",
+                "icon": "",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": null,
+                "resource_type_id": "3",
+                "resource_id": "0642e4a6-3d48-4635-ba2a-bf4e39c351ed",
+                "status": "1",
+                "hidden": 0
+              },
+
+              {
+                "id": "5d9ab44e-488c-4dd8-8688-56650393fab1",
+                "created_at": "18/12/2024 11:35:02",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "删除用户组",
+                "url": "-",
+                "path": "",
+                "perms": "system:group:delete",
+                "description": "",
+                "icon": "",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": null,
+                "resource_type_id": "1",
+                "resource_id": "0642e4a6-3d48-4635-ba2a-bf4e39c351ed",
+                "status": "1",
+                "hidden": 0
+              },
+
+              {
+                "id": "f2b2c9be-1c39-4416-b3c7-21891fc28f84",
+                "created_at": "28/10/2024 07:56:04",
+                "updated_at": "28/10/2024 07:56:04",
+                "name": "添加用户组",
+                "url": "/base/sygroup!save",
+                "path": null,
+                "perms": "system:group:add",
+                "description": null,
+                "icon": null,
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "0642e4a6-3d48-4635-ba2a-bf4e39c351ed",
+                "status": "1",
+                "hidden": null
+              }
+            ]
+          },
+          {
+            "id": "jggl",
+            "created_at": "25/8/2015 10:34:53",
+            "updated_at": "18/12/2024 20:05:23",
+            "name": "机构管理",
+            "url": "PermissionOrganization",
+            "path": "/permission/org",
+            "perms": "system:dept",
+            "description": "管理系统中用户的机构",
+            "icon": "tree",
+            "seq": 0,
+            "target": null,
+            "canbdeeleted": 1,
+            "resource_type_id": "0",
+            "resource_id": "xtgl",
+            "status": "1",
+            "hidden": null,
+            "children": [
+              {
+                "id": "jgbj",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "25/5/2022 00:39:56",
+                "name": "编辑机构",
+                "url": "/base/syorganization!update",
+                "path": null,
+                "perms": "system:dept:edit",
+                "description": "编辑机构",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 2,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "jggl",
+                "status": "1",
+                "hidden": null
+              },
+              {
+                "id": "jglb",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "28/11/2016 14:09:52",
+                "name": "机构列表",
+                "url": "/base/syorganization!treeGrid",
+                "path": null,
+                "perms": "system:dept:list",
+                "description": "查询机构列表",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "jggl",
+                "status": "1",
+                "hidden": null
+              },
+              {
+                "id": "jgsc",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "删除机构",
+                "url": "/base/syorganization!delete",
+                "path": "",
+                "perms": "system:dept:delete",
+                "description": "删除机构",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "jggl",
+                "status": "1",
+                "hidden": null
+              },
+              {
+                "id": "jgtj",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "25/8/2015 10:34:53",
+                "name": "添加机构",
+                "url": "/base/syorganization!save",
+                "path": null,
+                "perms": "system:dept:add",
+                "description": "添加机构",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 1,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "jggl",
+                "status": "1",
+                "hidden": null
+              }
+            ]
+          },
+          {
+            "id": "jsgl",
+            "created_at": "25/8/2015 10:34:53",
+            "updated_at": "18/12/2024 20:05:23",
+            "name": "角色管理",
+            "url": "PermissionRole",
+            "path": "/permission/role",
+            "perms": "system:role",
+            "description": "管理系统中用户的角色",
+            "icon": "peoples",
+            "seq": 0,
+            "target": null,
+            "canbdeeleted": 1,
+            "resource_type_id": "0",
+            "resource_id": "xtgl",
+            "status": "1",
+            "hidden": null,
+            "children": [
+              {
+                "id": "00b61433-a5cd-46e9-a867-2e249dd310f4",
+                "created_at": "18/12/2024 11:35:02",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "角色权限配置",
+                "url": "-",
+                "path": "",
+                "perms": "system:role:permissionEdit",
+                "description": "角色权限配置",
+                "icon": "",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": null,
+                "resource_type_id": "1",
+                "resource_id": "jsgl",
+                "status": "1",
+                "hidden": 0
+              },
+              {
+                "id": "f9947112-c8e4-42cd-ba3a-2612eb373564",
+                "created_at": "18/12/2024 11:35:02",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "角色部门配置",
+                "url": "-",
+                "path": "",
+                "perms": "system:role:deptEdit",
+                "description": "角色部门配置",
+                "icon": "",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": null,
+                "resource_type_id": "1",
+                "resource_id": "jsgl",
+                "status": "1",
+                "hidden": 0
+              },
+              {
+                "id": "jsbj",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "25/8/2015 10:34:53",
+                "name": "编辑角色",
+                "url": "/base/syrole!update",
+                "path": null,
+                "perms": "system:role:edit",
+                "description": "编辑角色",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 2,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "jsgl",
+                "status": "1",
+                "hidden": null
+              },
+              {
+                "id": "jslb",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "25/8/2015 10:34:53",
+                "name": "角色列表",
+                "url": "/base/syrole!grid",
+                "path": null,
+                "perms": "system:role:list",
+                "description": "查询角色列表",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "jsgl",
+                "status": "1",
+                "hidden": null
+              },
+              {
+                "id": "jssc",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "删除角色",
+                "url": "/base/syrole!delete",
+                "path": "",
+                "perms": "system:role:delete",
+                "description": "删除角色",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "jsgl",
+                "status": "1",
+                "hidden": null
+              },
+              {
+                "id": "jstj",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "25/8/2015 10:34:53",
+                "name": "添加角色",
+                "url": "/base/syrole!save",
+                "path": null,
+                "perms": "system:role:add",
+                "description": "添加角色",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 1,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "jsgl",
+                "status": "1",
+                "hidden": null
+              }
+            ]
+          },
+
+          {
+            "id": "yhgl",
+            "created_at": "25/8/2015 10:34:53",
+            "updated_at": "18/12/2024 20:05:23",
+            "name": "用户管理",
+            "url": "PermissionAccount",
+            "path": "/permission/account",
+            "perms": "system:user",
+            "description": "管理系统中用户的用户",
+            "icon": "user",
+            "seq": 0,
+            "target": null,
+            "canbdeeleted": 1,
+            "resource_type_id": "0",
+            "resource_id": "xtgl",
+            "status": "1",
+            "hidden": null,
+            "children": [
+              {
+                "id": "07417a54-4afc-45ec-84ee-c59baf7d45b4",
+                "created_at": "18/12/2024 11:35:02",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "用户角色",
+                "url": "-",
+                "path": "",
+                "perms": "system:user:viewPermission",
+                "description": "编辑用户角色",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": null,
+                "resource_type_id": "1",
+                "resource_id": "yhgl",
+                "status": "1",
+                "hidden": 0
+              },
+
+              {
+                "id": "a06316f1-b7e4-48b8-80c8-49b1dbb79c4a",
+                "created_at": "18/12/2024 11:35:02",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "重制密码",
+                "url": "-",
+                "path": "",
+                "perms": "system:role:resetPassword",
+                "description": "",
+                "icon": "",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": null,
+                "resource_type_id": "1",
+                "resource_id": "yhgl",
+                "status": "0",
+                "hidden": 0
+              },
+
+              {
+                "id": "e368cae2-96b6-46e5-b537-946a8dc5ede6",
+                "created_at": "18/12/2024 11:35:02",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "部门配置",
+                "url": "-",
+                "path": "",
+                "perms": "system:user:deptEdit",
+                "description": "编辑用户机构",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": null,
+                "resource_type_id": "1",
+                "resource_id": "yhgl",
+                "status": "1",
+                "hidden": 0
+              },
+
+              {
+                "id": "yhbj",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "25/8/2015 10:34:53",
+                "name": "编辑用户",
+                "url": "/base/syuser!update",
+                "path": null,
+                "perms": "system:user:edit",
+                "description": "编辑用户",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 2,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "yhgl",
+                "status": "1",
+                "hidden": null
+              },
+
+              {
+                "id": "yhlb",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "25/8/2015 10:34:53",
+                "name": "用户列表",
+                "url": "/base/syuser!grid",
+                "path": null,
+                "perms": "system:user:list",
+                "description": "查询用户列表",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "yhgl",
+                "status": "1",
+                "hidden": null
+              },
+
+              {
+                "id": "yhsc",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "删除用户",
+                "url": "/base/syuser!delete",
+                "path": "",
+                "perms": "system:user:delete",
+                "description": "删除用户",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "yhgl",
+                "status": "1",
+                "hidden": null
+              },
+
+              {
+                "id": "yhtj",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "25/8/2015 10:34:53",
+                "name": "添加用户",
+                "url": "/base/syuser!save",
+                "path": null,
+                "perms": "system:user:add",
+                "description": "添加用户",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 1,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "yhgl",
+                "status": "1",
+                "hidden": null
+              }
+            ]
+          },
+          {
+            "id": "zygl",
+            "created_at": "25/8/2015 10:34:53",
+            "updated_at": "18/12/2024 20:05:23",
+            "name": "资源管理",
+            "url": "PermissionResource",
+            "path": "/permission/resource",
+            "perms": "system:menu",
+            "description": "管理系统的资源",
+            "icon": "header-zhishiku",
+            "seq": 0,
+            "target": null,
+            "canbdeeleted": 1,
+            "resource_type_id": "0",
+            "resource_id": "xtgl",
+            "status": "1",
+            "hidden": null,
+            "children": [
+              {
+                "id": "zybj",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "5/9/2024 03:14:57",
+                "name": "编辑资源",
+                "url": "/base/syresource!update",
+                "path": null,
+                "perms": "system:menu:edit",
+                "description": "编辑资源",
+                "icon": "/api/v1/llm/Image/e65d0ccc-026e-4e08-bcbd-742d7b5ee61a.png",
+                "seq": 2,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "zygl",
+                "status": "1",
+                "hidden": null
+              },
+
+              {
+                "id": "zylb",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "资源列表",
+                "url": "/base/syresource!treeGrid",
+                "path": "",
+                "perms": "system:menu:list",
+                "description": "查询资源",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "zygl",
+                "status": "1",
+                "hidden": null
+              },
+
+              {
+                "id": "zysc",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "18/12/2024 11:35:02",
+                "name": "删除资源",
+                "url": "/base/syresource!delete",
+                "path": "",
+                "perms": "system:menu:delete",
+                "description": "删除资源",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 0,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "zygl",
+                "status": "1",
+                "hidden": null
+              },
+
+              {
+                "id": "zytj",
+                "created_at": "25/8/2015 10:34:53",
+                "updated_at": "25/8/2015 10:34:53",
+                "name": "添加资源",
+                "url": "/base/syresource!save",
+                "path": null,
+                "perms": "system:menu:add",
+                "description": "添加资源",
+                "icon": "ext-icon-bullet_wrench",
+                "seq": 1,
+                "target": null,
+                "canbdeeleted": 1,
+                "resource_type_id": "1",
+                "resource_id": "zygl",
+                "status": "1",
+                "hidden": null
+              }
+            ]
+          }
+        ]
+      }
+    ]
+  }
+]

+ 91 - 27
app/task/fetch_agent.py

@@ -1,17 +1,20 @@
+import json
+import os
 from pickle import PROTO
 from typing import Dict, List, Tuple
 
 from sqlalchemy import create_engine, Column, String, Integer, Text
 from sqlalchemy.exc import IntegrityError
-from sqlalchemy.orm import sessionmaker
+from sqlalchemy.orm import sessionmaker, Session
 
 from app.config.config import settings
-from app.config.const import RAGFLOW, BISHENG, DIFY
+from app.config.const import RAGFLOW, BISHENG, DIFY, ENV_CONF_PATH
 from app.models import KnowledgeModel
 from app.models.dialog_model import DialogModel
 from app.models.user_model import UserAppModel
 from app.models.agent_model import AgentModel
 from app.models.base_model import SessionLocal, Base
+from app.models.resource_model import ResourceModel
 from app.service.v2.app_register import AppRegisterDao
 
 # 创建数据库引擎和会话工厂
@@ -207,9 +210,9 @@ def update_ids_in_local(data: List[Tuple]):
     finally:
         db.close()
 
-def get_rag_user_id(db, tenant_id, app_type):
 
-    user = db.query(UserAppModel).filter(UserAppModel.app_type==app_type, UserAppModel.app_id==tenant_id).first()
+def get_rag_user_id(db, tenant_id, app_type):
+    user = db.query(UserAppModel).filter(UserAppModel.app_type == app_type, UserAppModel.app_id == tenant_id).first()
     if user:
         return user.user_id
     return tenant_id
@@ -220,30 +223,34 @@ def get_data_from_bisheng_v2(names: List[str]) -> List[Dict]:
     try:
         if names:
             query = db.query(Flow.id, Flow.name, Flow.description, Flow.status, Flow.user_id) \
-                .filter(Flow.name.in_(names), Flow.status=="1")
+                .filter(Flow.name.in_(names), Flow.status == "1")
         else:
-            query = db.query(Flow.id, Flow.name, Flow.description, Flow.status, Flow.user_id).filter(Flow.status=="1")
+            query = db.query(Flow.id, Flow.name, Flow.description, Flow.status, Flow.user_id).filter(Flow.status == "1")
 
         results = query.all()
         # print(f"Executing query: {query}")
         # 格式化id为UUID
-        formatted_results = [{"id":row[0], "name": row[1], "description": row[2], "status": row[3], "user_id": str(row[4]), "mode": "agent-dialog"} for row in results]
+        formatted_results = [
+            {"id": row[0], "name": row[1], "description": row[2], "status": row[3], "user_id": str(row[4]),
+             "mode": "agent-dialog"} for row in results]
         return formatted_results
     finally:
         db.close()
 
+
 def get_data_from_ragflow_v2(names: List[str]) -> List[Dict]:
     db = SessionRagflow()
     try:
         if names:
             query = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id) \
-                .filter( Dialog.name.in_(names), Dialog.status == "1")
+                .filter(Dialog.name.in_(names), Dialog.status == "1")
         else:
-            query = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id).filter( Dialog.status == "1")
+            query = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id).filter(
+                Dialog.status == "1")
 
         results = query.all()
         formatted_results = [
-            {"id": row[0], "name": row[1], "description": row[2], "status": "1" if row[3]  == "1" else "2",
+            {"id": row[0], "name": row[1], "description": row[2], "status": "1" if row[3] == "1" else "2",
              "user_id": str(row[4]), "mode": "agent-dialog"} for row in results]
         return formatted_results
     finally:
@@ -255,7 +262,7 @@ def get_data_from_dify_v2(names: List[str]) -> List[Dict]:
     try:
         if names:
             query = db.query(DfApps.id, DfApps.name, DfApps.description, DfApps.status, DfApps.tenant_id, DfApps.mode) \
-                .filter( DfApps.name.in_(names))
+                .filter(DfApps.name.in_(names))
         else:
             query = db.query(DfApps.id, DfApps.name, DfApps.description, DfApps.status, DfApps.tenant_id, DfApps.mode)
 
@@ -268,11 +275,10 @@ def get_data_from_dify_v2(names: List[str]) -> List[Dict]:
         db.close()
 
 
-
-def update_ids_in_local_v2(data: List[Dict], dialog_type:str):
+def update_ids_in_local_v2(data: List[Dict], dialog_type: str):
     db = SessionLocal()
     agent_id_list = []
-    type_dict = {"1": RAGFLOW,"2": BISHENG,"4": DIFY}
+    type_dict = {"1": RAGFLOW, "2": BISHENG, "4": DIFY}
     try:
         for row in data:
             agent_id_list.append(row["id"])
@@ -284,7 +290,10 @@ def update_ids_in_local_v2(data: List[Dict], dialog_type:str):
                 existing_agent.mode = row["mode"]
                 # existing_agent.tenant_id = get_rag_user_id(db, row["user_id"], type_dict[dialog_type])
             else:
-                existing = DialogModel(id=row["id"], status=row["status"], name=row["name"], description=row["description"], tenant_id=get_rag_user_id(db, row["user_id"], type_dict[dialog_type]), dialog_type=dialog_type, mode=row["mode"])
+                existing = DialogModel(id=row["id"], status=row["status"], name=row["name"],
+                                       description=row["description"],
+                                       tenant_id=get_rag_user_id(db, row["user_id"], type_dict[dialog_type]),
+                                       dialog_type=dialog_type, mode=row["mode"])
                 db.add(existing)
         db.commit()
         for dialog in db.query(DialogModel).filter_by(dialog_type=dialog_type).all():
@@ -299,12 +308,12 @@ def update_ids_in_local_v2(data: List[Dict], dialog_type:str):
         db.close()
 
 
-
 def get_data_from_ragflow_knowledge():
     db = SessionRagflow()
     try:
 
-        results = db.query(RgKnowledge.id, RgKnowledge.name, RgKnowledge.description, RgKnowledge.status, RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission).all()
+        results = db.query(RgKnowledge.id, RgKnowledge.name, RgKnowledge.description, RgKnowledge.status,
+                           RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission).all()
         formatted_results = [
             {"id": row[0], "name": row[1], "description": row[2], "status": str(row[3]),
              "user_id": str(row[4]), "doc_num": row[5], "permission": row[6]} for row in results]
@@ -312,6 +321,7 @@ def get_data_from_ragflow_knowledge():
     finally:
         db.close()
 
+
 def sync_agents_v2():
     db = SessionLocal()
 
@@ -336,6 +346,7 @@ def sync_agents_v2():
     finally:
         db.close()
 
+
 def update_ids_in_local_knowledge(data, klg_type):
     type_dict = {"1": RAGFLOW, "2": BISHENG, "4": DIFY}
     db = SessionLocal()
@@ -348,13 +359,14 @@ def update_ids_in_local_knowledge(data, klg_type):
                 existing_agent.name = row["name"]
                 existing_agent.description = row["description"]
                 # existing_agent.tenant_id = get_rag_user_id(db, row["user_id"], type_dict[klg_type])
-                existing_agent.permission =  row["permission"]
-                existing_agent.documents =  row["doc_num"]
-                existing_agent.status =  row["status"]
+                existing_agent.permission = row["permission"]
+                existing_agent.documents = row["doc_num"]
+                existing_agent.status = row["status"]
             else:
                 existing = KnowledgeModel(id=row["id"], name=row["name"], description=row["description"],
-                                       tenant_id=get_rag_user_id(db, row["user_id"], type_dict[klg_type]),status=row["status"],
-                                       knowledge_type=1, permission=row["permission"], documents=row["doc_num"])
+                                          tenant_id=get_rag_user_id(db, row["user_id"], type_dict[klg_type]),
+                                          status=row["status"],
+                                          knowledge_type=1, permission=row["permission"], documents=row["doc_num"])
                 db.add(existing)
         db.commit()
         for dialog in db.query(KnowledgeModel).filter_by(knowledge_type=klg_type).all():
@@ -367,13 +379,16 @@ def update_ids_in_local_knowledge(data, klg_type):
     finally:
         db.close()
 
+
 def get_one_from_ragflow_knowledge(klg_id):
     db = SessionRagflow()
     try:
 
-        row = db.query(RgKnowledge.id, RgKnowledge.name, RgKnowledge.description, RgKnowledge.status, RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission).filter(RgKnowledge.id==klg_id).first()
+        row = db.query(RgKnowledge.id, RgKnowledge.name, RgKnowledge.description, RgKnowledge.status,
+                       RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission).filter(
+            RgKnowledge.id == klg_id).first()
         return {"id": row[0], "name": row[1], "description": row[2], "status": str(row[3]),
-             "user_id": str(row[4]), "doc_num": row[5], "permission": row[6]} if row else {}
+                "user_id": str(row[4]), "doc_num": row[5], "permission": row[6]} if row else {}
     finally:
         db.close()
 
@@ -405,15 +420,64 @@ def update_ragflow_user_tenant(user_id: str):
     db = SessionRagflow()
     try:
         if user_id:
-            db.query(RgUserTenant).filter( RgUserTenant.user_id==user_id, RgUserTenant.role=="invite").update({"role": "normal"})
-            db.query(RgUserTenant).filter( RgUserTenant.tenant_id==user_id, RgUserTenant.role=="invite").update({"role": "normal"})
+            db.query(RgUserTenant).filter(RgUserTenant.user_id == user_id, RgUserTenant.role == "invite").update(
+                {"role": "normal"})
+            db.query(RgUserTenant).filter(RgUserTenant.tenant_id == user_id, RgUserTenant.role == "invite").update(
+                {"role": "normal"})
         else:
-            db.query(RgUserTenant).filter(RgUserTenant.role=="invite").update({"role": "normal"})
+            db.query(RgUserTenant).filter(RgUserTenant.role == "invite").update({"role": "normal"})
         db.commit()
     finally:
         db.close()
 
 
+def import_tree(session: Session, node: dict, parent=None):
+    resource = ResourceModel(
+        id=node['id'],
+        name=node['name'],
+        url=node['url'],
+        path=node.get('path'),
+        perms=node['perms'],
+        description=node.get('description'),
+        icon=node.get('icon'),
+        seq=node['seq'],
+        target=node.get('target'),
+        canbdeeleted=node.get('canbdeeleted'),
+        resource_type_id=node['resource_type_id'],
+        resource_id=node.get('resource_id'),
+        status=node['status'],
+        hidden=node.get('hidden')
+    )
+    if parent:
+        resource.parent = parent
+    session.add(resource)
+    if 'children' in node:
+        for child in node['children']:
+            import_tree(session, child, parent=resource)
+    session.commit()
+
+
+def sync_resources_from_json():
+    db = SessionLocal()
+    try:
+        if db.query(ResourceModel).count() == 0:
+            with open(os.path.join(ENV_CONF_PATH, "resource.json"), 'r', encoding='utf-8') as file:
+                json_data = json.load(file)
+
+            db.query(ResourceModel).delete()
+            db.commit()
+
+            for node in json_data:
+                import_tree(db, node)
+            print("add resources record successfully")
+        else:
+            print("sync resources successfully")
+    except Exception as e:
+        print(f"Failed to sync resources: {str(e)}")
+    finally:
+        db.close()
+
+
 if __name__ == "__main__":
     # a = get_data_from_dify_v2([])
     # print(a)

+ 6 - 2
main.py

@@ -24,10 +24,13 @@ from app.api.group import group_router
 from app.api.role import role_router
 from app.models.base_model import init_db
 # from app.models.base_model import init_db
-from app.task.fetch_agent import sync_agents, initialize_agents, sync_agents_v2, sync_knowledge
+from app.task.fetch_agent import sync_agents, initialize_agents, sync_agents_v2, sync_knowledge, \
+    sync_resources_from_json
 from app.init_config.init_run_data import sync_default_data
 
 init_db()
+
+
 # 使用 Lifespan 事件处理程序
 @asynccontextmanager
 async def lifespan(app: FastAPI):
@@ -39,6 +42,7 @@ async def lifespan(app: FastAPI):
 
     sync_agents_v2()
     sync_knowledge()
+    sync_resources_from_json()
     yield
     # 在应用关闭时执行清理操作(如果需要)
     pass
@@ -64,7 +68,6 @@ scheduler = BackgroundScheduler()
 scheduler.add_job(sync_agents_v2, 'interval', minutes=60, id="sync_resource_data")
 scheduler.start()
 
-
 app.include_router(auth_router, prefix='/api/auth', tags=["auth"])
 app.include_router(chat_router, prefix='/api/chat', tags=["chat"])
 app.include_router(agent_router, prefix='/api/agent', tags=["agent"])
@@ -86,4 +89,5 @@ app.mount("/static", StaticFiles(directory="app/images"), name="static")
 
 if __name__ == "__main__":
     import uvicorn
+
     uvicorn.run(app, host="0.0.0.0", port=9201)