From a58221e75d2e38172533e17b6590983a9b3cabb9 Mon Sep 17 00:00:00 2001
From: chengf <cgf12138@163.com>
Date: 星期六, 11 十月 2025 18:46:52 +0800
Subject: [PATCH] 修复仁智企对话保存功能问题

---
 aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java |   43 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 35 insertions(+), 8 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 f489f9b..9999eff 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
@@ -335,6 +335,7 @@
 
             @Override
             public void onResponse(Call call, Response response) {
+                int a = 1;
                 AiMessage aiMessage = new AiMessage();
                 com.agentsflex.core.llm.response.AiMessageResponse aiMessageResponse
                         = new com.agentsflex.core.llm.response.AiMessageResponse(new HistoriesPrompt(), response.message(), aiMessage);
@@ -365,16 +366,42 @@
                                 // 杩欓噷闇�瑕佹牴鎹叿浣揂PI璋冩暣璺緞
                                 JsonObject messageObj = gson.fromJson(data, JsonObject.class);
 //                                System.out.println(messageObj);
-                                if(messageObj.get("event").getAsString().equals("message_end")){
+                                if(!messageObj.has("answer")){
+                                    try {
+                                        JsonArray asJsonArray = messageObj.getAsJsonObject("metadata").getAsJsonArray("retriever_resources");
+                                        if (asJsonArray.size() > 0) {
+                                            aiMessage.setFullContent("-----------------------");
+                                            sb.append("\n"+aiMessage.getFullContent());
+                                            emitter.send(JSON.toJSONString(aiMessage));
+                                            for (int i = 0; i < asJsonArray.size(); i++) {
+                                                aiMessage.setFullContent(asJsonArray.get(i).getAsJsonObject().get("document_name").getAsString());
+                                                aiMessage.setContent(null);
+//                                                aiMessageResponse.setMessage(aiMessage);
+                                                sb.append("\n"+aiMessage.getFullContent());
+                                                emitter.send(JSON.toJSONString(aiMessage));
+                                            }
+                                        }
+                                    } catch (Exception e) {
+                                        System.out.println("meizuo");
+                                    }
                                     AiBotMessage aiBotMessage = new AiBotMessage();
                                     aiBotMessage.setBotId(botId);
                                     aiBotMessage.setSessionId(sessionId);
                                     aiBotMessage.setAccountId(new BigInteger(userId));
                                     aiBotMessage.setRole("assistant");
+                                    String content = aiBotMessage.getContent();
                                     aiBotMessage.setContent(sb.toString());
                                     aiBotMessage.setCreated(new Date());
                                     aiBotMessage.setIsExternalMsg(1);
-                                    aiBotMessageService.save(aiBotMessage);
+                                    if(a == 1){
+                                        a = 0;
+                                        aiBotMessageService.save(aiBotMessage);
+                                    }else{
+                                        QueryWrapper qw = new QueryWrapper();
+                                        qw.eq("content", content);
+                                        aiBotMessageService.remove(qw);
+                                        aiBotMessageService.save(aiBotMessage);
+                                    }
 //                                    System.out.println("end");
                                 }else{
                                     String context = messageObj.get("answer").getAsString();
@@ -390,16 +417,16 @@
                                     aiMessage.setContent(context);
                                     aiMessageResponse.setMessage(aiMessage);
                                     if (!messageObj.get("answer").getAsString().isEmpty()) {
-                                        if(!blean && aiMessage.getContent().startsWith("</details>")){
-                                            blean = true;
-                                            aiMessage.setContent(aiMessage.getContent().replaceAll("(?i)<[^>]*>", "\n\n"));
-                                        }
-                                        if(blean){
+//                                        if(!blean && aiMessage.getContent().startsWith("</details>")){
+//                                            blean = true;
+//                                            aiMessage.setContent(aiMessage.getContent().replaceAll("(?i)<[^>]*>", "\n\n"));
+//                                        }
+//                                        if(blean){
                                             sb.append(aiMessage.getContent());
 //                                            System.out.println(aiMessage);
                                             // 鍙戦�佹秷鎭墖娈电粰鍓嶇
                                             emitter.send(JSON.toJSONString(aiMessage));
-                                        }
+//                                        }
                                     }
                                 }
 

--
Gitblit v1.8.0