From f7def4651e48093c47008031a313c88df9811c88 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 30 Sep 2025 11:51:41 +0800
Subject: [PATCH] feat(ai): 新增知识文件刷新与删除功能

---
 src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java |   73 +++++++++++++++++++++++++++++++++++-
 1 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java b/src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java
index 9811090..3ea8e89 100644
--- a/src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java
+++ b/src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java
@@ -9,12 +9,10 @@
 import java.io.FileInputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
-import java.nio.file.Paths;
 import java.security.MessageDigest;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.concurrent.TimeUnit;
 
 public class KnowledgeBaseUtil {
     public static final String       ACCESS_KEY_ID     = "LTAI5tCyQRwhZ2eimxCFKbdq";
@@ -61,6 +59,20 @@
         // 调用添加分类API接口
         AddCategoryResponse addCategoryResponse = client.addCategoryWithOptions(WORKSPACE_ID, addCategoryRequest, headers, runtime);
         return addCategoryResponse.getBody().getData().getCategoryId();
+    }
+
+
+    /**
+     * 永久删除应用数据中的指定文件
+     * @param fileId 文件 ID
+     * @throws Exception 当API调用失败或其他异常情况时抛出
+     */
+    public static void deleteFile(String fileId) throws Exception {
+        com.aliyun.bailian20231229.Client client = KnowledgeBaseUtil.createClient();
+        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
+        java.util.Map<String, String> headers = new java.util.HashMap<>();
+        client.deleteFileWithOptions(fileId, WORKSPACE_ID, headers, runtime);
+        return;
     }
 
     /**
@@ -242,7 +254,7 @@
      * @param indexId     知识库ID
      * @return 阿里云百炼服务的响应对象
      */
-    public static GetIndexJobStatusResponse getIndexJobStatus(com.aliyun.bailian20231229.Client client, String workspaceId, String jobId, String indexId) throws Exception {
+    public static GetIndexJobStatusResponse getFileJobStatus(com.aliyun.bailian20231229.Client client, String workspaceId, String jobId, String indexId) throws Exception {
         Map<String, String> headers = new HashMap<>();
         com.aliyun.bailian20231229.models.GetIndexJobStatusRequest getIndexJobStatusRequest = new com.aliyun.bailian20231229.models.GetIndexJobStatusRequest();
         getIndexJobStatusRequest.setIndexId(indexId);
@@ -381,6 +393,61 @@
         }
     }
 
+    public static int getFileJobStatus(String fileId) {
+        int state = 0;
+        // 步骤1:初始化客户端(Client)
+        System.out.println("步骤1:创建Client");
+        com.aliyun.bailian20231229.Client client = null;
+        try {
+            client = createClient();
+            // 步骤6:检查更新后的文件状态
+            System.out.println("步骤6:检查阿里云百炼中的文件状态");
+            DescribeFileResponse describeResponse = describeFile(client, WORKSPACE_ID, fileId);
+            String status = describeResponse.getBody().getData().getStatus();
+            System.out.println("当前文件状态:" + status);
+            if ("PARSE_SUCCESS".equals(status)) {
+                state = 1;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return state;
+    }
+
+    public static int getIndexKnowledgeJobStatus(String jobId, String indexId) {
+        int state = 0;
+        // 步骤1:初始化客户端(Client)
+        System.out.println("步骤1:创建Client");
+        com.aliyun.bailian20231229.Client client = null;
+        try {
+            client = createClient();
+            System.out.println("步骤8:等待追加任务完成");
+            GetIndexJobStatusResponse jobStatusResponse = getFileJobStatus(client, WORKSPACE_ID, jobId, indexId);
+            String status = jobStatusResponse.getBody().getData().getStatus();
+            System.out.println("当前索引任务状态:" + status);
+            if ("COMPLETED".equals(status)) {
+                state = 1;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return state;
+    }
+
+
+    public static void knowledgeFileDelete(String fileId, String indexId) {
+        com.aliyun.bailian20231229.Client client = null;
+        try {
+            System.out.println("步骤1:创建Client");
+            client = createClient();
+            // 步骤9:删除旧文件
+            System.out.println("步骤9:删除旧文件");
+            deleteIndexDocument(client, WORKSPACE_ID, indexId, fileId);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
     /**
      * 向一个文档类知识库追加导入已解析的文件
      *

--
Gitblit v1.9.1