From a5a9ac7c2a9fd6026b0d87e1b0eff763eb4512d0 Mon Sep 17 00:00:00 2001
From: admin <cgf12138@163.com>
Date: 星期二, 10 六月 2025 18:25:20 +0800
Subject: [PATCH] 0610

---
 aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java |  123 +++++++++++++++++++++++++++++++++++++----
 1 files changed, 111 insertions(+), 12 deletions(-)

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 ed66287..2bfc6aa 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
@@ -155,6 +155,7 @@
                            @JsonBody(value = "botId", required = true) BigInteger botId,
                            @JsonBody(value = "sessionId", required = true) String sessionId,
                            @JsonBody(value = "isExternalMsg") int isExternalMsg,
+                           @JsonBody(value = "files") List<String> files,//涓婁紶鏂囦欢
                            @JsonBody(value = "file") String file,//涓婁紶鏂囦欢
                            HttpServletResponse response) {
         response.setContentType("text/event-stream");
@@ -166,27 +167,125 @@
         String systemPrompt = llmOptions != null ? (String) llmOptions.get("systemPrompt") : null;
 
         if (StringUtil.hasText(aiBot.getModelAPI())){
-            if (aiBot.getBotTypeId() == 2) {
+            if (aiBot.getBotTypeId() == 2 || aiBot.getBotTypeId() == 3) {
 
                 String apiUrl = aiBot.getModelAPI()+"/workflows/run"; // 鏇挎崲涓哄疄闄匒PI URL
                 String apiKey = aiBot.getModelKEY(); // 鏇挎崲涓哄疄闄匒PI Key
 
                 DifyStreamClient client = new DifyStreamClient(apiUrl, apiKey, aiBotMessageService);
-                DifyStreamClient uploadClient = new DifyStreamClient(aiBot.getModelAPI()+"/files/upload", apiKey, aiBotMessageService);
-                String fileId = file;
+//                DifyStreamClient uploadClient = new DifyStreamClient(aiBot.getModelAPI()+"/files/upload", apiKey, aiBotMessageService);
 
-                // 2. 鏋勫缓鏂囦欢鍙傛暟瀵硅薄
-                Map<String, Object> fileParam = new HashMap<>();
-                fileParam.put("transfer_method", "local_file");
-                fileParam.put("upload_file_id", fileId);
-//                fileParam.put("type", fileJson.get("extension").getAsString()); // 渚嬪 "excel"銆�"pdf" 绛�
-                fileParam.put("type", "document"); // 渚嬪 "excel"銆�"pdf" 绛�
 
-                // 3. 缁勮 inputs 鍙傛暟
+
+
                 Map<String, Object> inputs = new HashMap<>();
-                inputs.put("w", fileParam); // 娣诲姞鏂囦欢鍙傛暟锛寁ariableName 濡� "document"
+                if(aiBot.getBotTypeId() == 3){
+                    // 2. 鏋勫缓鏂囦欢鍙傛暟瀵硅薄
 
+                    HttpHeaders headers = new HttpHeaders();
+                    headers.set("Authorization", apiKey);
 
+                    HttpEntity<Object> requestEntity = new HttpEntity<>(null, headers);
+
+                    ResponseEntity<String> parameters = restTemplate.exchange(
+                            aiBot.getModelAPI()+"/parameters",
+                            HttpMethod.GET,
+                            requestEntity, // 璇锋眰浣擄紙GET 鏃犺姹備綋锛�
+                            String.class // 鍝嶅簲缁撴灉绫诲瀷
+                    );
+
+                    org.json.JSONObject jsonObj = new org.json.JSONObject(parameters.getBody());
+                    // 鍏堣幏鍙� user_input_form 鏁扮粍
+                    org.json.JSONArray userInputFormArr = jsonObj.getJSONArray("user_input_form");
+                    String variable = null;
+                    String type = "";
+                    // 閬嶅巻鏁扮粍锛屾壘鍒� file - list 缁撴瀯
+                    for (int i = 0; i < userInputFormArr.length(); i++) {
+                        org.json.JSONObject formItem = userInputFormArr.getJSONObject(i);
+                        if (formItem.has("file-list")) {
+                            org.json.JSONObject fileListObj = formItem.getJSONObject("file-list");
+                            // 鎻愬彇 variable 鍙傛暟
+                            variable = fileListObj.getString("variable");
+                            // 鎻愬彇 allowed_file_types 鍙傛暟锛堟暟缁勫舰寮忥級
+                            org.json.JSONArray allowedFileTypesArr = fileListObj.getJSONArray("allowed_file_types");
+                            System.out.println("variable: " + variable);
+                            System.out.println("allowed_file_types: " + allowedFileTypesArr.toString());
+                            type = allowedFileTypesArr.toString().replace("[\"", "").replace("\"]", "");
+                            break; // 杩欓噷鍋囪鍙湁涓�涓鍚堢殑 file - list锛屾壘鍒板氨閫�鍑哄惊鐜紝鍙牴鎹疄闄呮儏鍐佃皟鏁�
+                        }
+                    }
+
+                    List fileList = new ArrayList<>();
+                    Map<String, Object> fileParam = new HashMap<>();
+                    for (String fileId : files) {
+                        fileParam.put("transfer_method", "local_file");
+                        fileParam.put("upload_file_id", fileId);
+                        fileParam.put("type", type); // 渚嬪 "excel"銆�"pdf" 绛�
+                        fileList.add(fileParam);
+                    }
+                    // 3. 缁勮 inputs 鍙傛暟
+                    inputs.put(variable, fileList); // 娣诲姞鏂囦欢鍙傛暟锛寁ariableName 濡� "document"
+                }
+                else {
+
+                    HttpHeaders headers = new HttpHeaders();
+                    headers.set("Authorization", apiKey);
+
+                    Map<String, Object> fileParam = new HashMap<>();
+                    fileParam.put("transfer_method", "local_file");
+                    fileParam.put("upload_file_id", file);
+                    HttpEntity<Object> requestEntity = new HttpEntity<>(null, headers);
+
+                    ResponseEntity<String> parameters = restTemplate.exchange(
+                            aiBot.getModelAPI()+"/parameters",
+                            HttpMethod.GET,
+                            requestEntity, // 璇锋眰浣擄紙GET 鏃犺姹備綋锛�
+                            String.class // 鍝嶅簲缁撴灉绫诲瀷
+                    );
+//                    System.out.println("========================\n"+parameters.getBody()+"\n=====================");
+                    String variable = null;
+                    String type = "";
+                    try {
+                        // 瑙f瀽 JSON 瀛楃涓�
+                        org.json.JSONObject configJson = new org.json.JSONObject(parameters.getBody());
+
+                        // 鑾峰彇 user_input_form 鏁扮粍
+                        org.json.JSONArray userInputFormArray = configJson.getJSONArray("user_input_form");
+
+                        // 閬嶅巻 user_input_form 鏁扮粍涓殑姣忎釜鍏冪礌
+                        for (int i = 0; i < userInputFormArray.length(); i++) {
+                            org.json.JSONObject formElement = userInputFormArray.getJSONObject(i);
+
+                            // 妫�鏌ユ槸鍚﹀寘鍚� "file" 瀵硅薄
+                            if (formElement.has("file")) {
+                                org.json.JSONObject fileObject = formElement.getJSONObject("file");
+
+                                // 鎻愬彇 variable
+                                variable = fileObject.getString("variable");
+
+                                // 鎻愬彇 allowed_file_types 鏁扮粍
+                                org.json.JSONArray allowedFileTypesArray = fileObject.getJSONArray("allowed_file_types");
+
+                                // 鎵撳嵃缁撴灉
+                                System.out.println("Variable: " + variable);
+                                System.out.print("Document Types: ");
+
+                                for (int j = 0; j < allowedFileTypesArray.length(); j++) {
+                                    type = type + allowedFileTypesArray.getString(j);
+                                    System.out.print(allowedFileTypesArray.getString(j) + " ");
+                                }
+
+                                System.out.println();
+                            }
+                        }
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                    fileParam.put("type", type); // 渚嬪 "excel"銆�"pdf" 绛�
+                    // 3. 缁勮 inputs 鍙傛暟
+                    inputs.put(variable, fileParam); // 娣诲姞鏂囦欢鍙傛暟锛寁ariableName 濡� "document"
+
+                }
 
                 AiBotMessageMemory memory = new AiBotMessageMemory(botId, SaTokenUtil.getLoginAccount().getId(),
                         sessionId, isExternalMsg, aiBotMessageService, aiBotConversationMessageMapper,
@@ -273,7 +372,7 @@
             } catch (Exception e) {
                 emitter.completeWithError(e);
             }
-            System.out.println(emitter.toString());
+//            System.out.println(emitter.toString());
             return emitter;
         }
         else{

--
Gitblit v1.8.0