From 4eb46966002c6ca24cbb8cc8b519a05610e81649 Mon Sep 17 00:00:00 2001
From: admin <cgf12138@163.com>
Date: 星期一, 09 六月 2025 11:17:24 +0800
Subject: [PATCH] 0606

---
 aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java |   35 +++++++++--------
 aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java    |   44 +++++++++++++--------
 2 files changed, 46 insertions(+), 33 deletions(-)

diff --git a/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java b/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java
index 420a25a..1226036 100644
--- a/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java
+++ b/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java
@@ -3,10 +3,12 @@
 import com.agentsflex.core.message.AiMessage;
 import com.agentsflex.core.prompt.HistoriesPrompt;
 import com.alibaba.fastjson.JSON;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.gson.*;
 import com.mybatisflex.core.query.QueryWrapper;
 import okhttp3.*;
 import okio.BufferedSource;
+import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 import tech.aiflowy.ai.entity.AiBotMessage;
 import tech.aiflowy.ai.service.AiBotMessageService;
@@ -131,6 +133,9 @@
                                     JsonElement dataElement = jsonObject.get("data");
                                     if (dataElement != null && !dataElement.isJsonNull()) {
                                         JsonObject dataObject = dataElement.getAsJsonObject();
+                                        if (dataObject != null && dataObject.has("node_type")) {
+                                            continue;
+                                        }
                                         if (dataObject != null && dataObject.has("title")) {
                                             JsonElement titleElement = dataObject.get("title");
                                             if (titleElement != null && !titleElement.isJsonNull()) {
@@ -178,32 +183,37 @@
         return future;
     }
 
-    public String fileUpload(String userId, String filePath){
-        // 瑕佷笂浼犵殑鏂囦欢璺緞锛屾浛鎹负瀹為檯鐨勬枃浠惰矾寰�
-//        String filePath = "C:\\Users\\admin\\Desktop\\鍥藉姟闄㈡斂绛栨枃浠跺簱.xlsx";
-        // 鐢ㄦ埛鏍囪瘑锛屾浛鎹负瀹為檯鐨勭敤鎴锋爣璇嗭紝瑕佸拰鍙戦�佹秷鎭帴鍙g殑 user 淇濇寔涓�鑷�
+    public String fileUpload(String userId, MultipartFile file) {
+        // 鐢ㄦ埛鏍囪瘑锛岃鍜屽彂閫佹秷鎭帴鍙g殑 user 淇濇寔涓�鑷�
         String user = userId;
 
-        File file = new File(filePath);
-        if (!file.exists()) {
-            System.out.println("鏂囦欢涓嶅瓨鍦細" + filePath);
-            return user;
+        // 鍒ゆ柇鏂囦欢鏄惁涓虹┖
+        if (file.isEmpty()) {
+            System.out.println("涓婁紶鏂囦欢涓虹┖");
+            return "涓婁紶鏂囦欢涓虹┖";
         }
 
         OkHttpClient client = new OkHttpClient();
 
         // 鏋勫缓 multipart/form-data 璇锋眰浣�
-        RequestBody requestBody = new MultipartBody.Builder()
-                .setType(MultipartBody.FORM)
-                .addFormDataPart("file", file.getName(),
-                        RequestBody.create(MediaType.parse("application/octet-stream"), file))
-                .addFormDataPart("user", user)
-                .build();
+        MultipartBody.Builder requestBodyBuilder = null;
+        try {
+            requestBodyBuilder = new MultipartBody.Builder()
+                    .setType(MultipartBody.FORM)
+                    // 娣诲姞涓婁紶鏂囦欢锛岃繖閲岀洿鎺ョ敤 MultipartFile 鐨勫瓧鑺傛暟缁勬瀯寤鸿姹備綋
+                    .addFormDataPart("file", file.getOriginalFilename(),
+                            RequestBody.create(MediaType.parse("application/octet-stream"), file.getBytes()))
+                    .addFormDataPart("user", user);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+        RequestBody requestBody = requestBodyBuilder.build();
 
         Request request = new Request.Builder()
-                .url(apiUrl)
+                .url(apiUrl)  // 鏇挎崲涓哄疄闄呯殑鎺ュ彛鍦板潃甯搁噺
                 .post(requestBody)
-                .header("Authorization", apiKey)
+                .header("Authorization", apiKey)  // 鏇挎崲涓哄疄闄呯殑鎺堟潈瀵嗛挜甯搁噺
                 .build();
 
         try (Response response = client.newCall(request).execute()) {
@@ -216,7 +226,7 @@
             return responseBody;
         } catch (IOException e) {
             e.printStackTrace();
-            return e.getMessage();
+            return "鏂囦欢涓婁紶澶辫触锛�" + e.getMessage();
         }
     }
 
diff --git a/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java b/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java
index d5543f2..ed66287 100644
--- a/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java
+++ b/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java
@@ -28,13 +28,11 @@
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.*;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
+import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 import tech.aiflowy.ai.config.DifyStreamClient;
 import tech.aiflowy.ai.config.FileReference;
@@ -55,6 +53,7 @@
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.File;
 import java.math.BigInteger;
 import java.util.*;
 
@@ -141,13 +140,22 @@
     @Autowired
     private ObjectMapper objectMapper;
 
+//    @PostMapping("chat")
+//    public SseEmitter chat2(@JsonBody(value = "prompt", required = true) String prompt,
+//                           @JsonBody(value = "botId", required = true) BigInteger botId,
+//                           @JsonBody(value = "sessionId", required = true) String sessionId,
+//                           @JsonBody(value = "isExternalMsg") int isExternalMsg,
+//                           @JsonBody("file") String file,//涓婁紶鏂囦欢
+//                           HttpServletResponse response){
+//        File file =
+//    }
 
     @PostMapping("chat")
     public SseEmitter chat(@JsonBody(value = "prompt", required = true) String prompt,
                            @JsonBody(value = "botId", required = true) BigInteger botId,
                            @JsonBody(value = "sessionId", required = true) String sessionId,
                            @JsonBody(value = "isExternalMsg") int isExternalMsg,
-                           @JsonBody(value = "file") String file,
+                           @JsonBody(value = "file") String file,//涓婁紶鏂囦欢
                            HttpServletResponse response) {
         response.setContentType("text/event-stream");
         AiBot aiBot = service.getById(botId);
@@ -165,17 +173,12 @@
 
                 DifyStreamClient client = new DifyStreamClient(apiUrl, apiKey, aiBotMessageService);
                 DifyStreamClient uploadClient = new DifyStreamClient(aiBot.getModelAPI()+"/files/upload", apiKey, aiBotMessageService);
-                String test = uploadClient.fileUpload(SaTokenUtil.getLoginAccount().getId() + "", file);
-                System.out.println(test);
-                Gson gson = new GsonBuilder().setPrettyPrinting().create();
-                JsonObject fileJson = gson.fromJson(test, JsonObject.class);
-                String fileId = fileJson.get("id").getAsString();
+                String fileId = file;
 
                 // 2. 鏋勫缓鏂囦欢鍙傛暟瀵硅薄
                 Map<String, Object> fileParam = new HashMap<>();
                 fileParam.put("transfer_method", "local_file");
                 fileParam.put("upload_file_id", fileId);
-                String[] split = file.split("\\.");
 //                fileParam.put("type", fileJson.get("extension").getAsString()); // 渚嬪 "excel"銆�"pdf" 绛�
                 fileParam.put("type", "document"); // 渚嬪 "excel"銆�"pdf" 绛�
 
@@ -363,8 +366,8 @@
     }
 
     @PostMapping("files/upload")
-    public Result filesUpload(@JsonBody(value = "botId", required = true) BigInteger botId,
-                           String file,
+    public Result filesUpload(@RequestParam("botId") BigInteger botId,
+                              @RequestParam("file") MultipartFile file,
                            HttpServletResponse response){
         try{
             String userId = SaTokenUtil.getLoginAccount().getId() + "";
@@ -374,10 +377,10 @@
             String apiUrl = aiBot.getModelAPI(); // 鏇挎崲涓哄疄闄匒PI URL
             String apiKey = aiBot.getModelKEY(); // 鏇挎崲涓哄疄闄匒PI Key
             DifyStreamClient client = new DifyStreamClient(apiUrl, apiKey, aiBotMessageService);
-            client.fileUpload(userId,file);
-            return Result.success("鎴愬姛锛�");
+            String s = client.fileUpload(userId, file);
+            return Result.success(s);
         }catch (Exception e){
-            return Result.fail(400, String.valueOf(e));
+            return Result.fail(400,String.valueOf(e));
         }
     }
 

--
Gitblit v1.8.0