zrd vor 3 Monaten
Ursprung
Commit
0ca8241052

+ 11 - 4
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/app/aipromptmanagement/AppAiController.java

@@ -5,10 +5,7 @@ import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
 import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
-import cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo.WorkflowDifyFilesReqVO;
-import cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo.WorkflowDifyReqVO;
-import cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo.WorkflowRunFileReqVO;
-import cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo.WorkflowRunSaveReqVO;
+import cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo.*;
 import cn.iocoder.yudao.module.infra.service.aipromptmanagement.WorkflowRunService;
 import cn.iocoder.yudao.module.infra.service.aipromptmanagement.utils.DifyFilesUtils;
 import cn.iocoder.yudao.module.infra.service.aipromptmanagement.utils.FileResp;
@@ -167,6 +164,16 @@ public class AppAiController {
         return success(workflowRunService.getDifyResul(inputs, createReqVO.getType(), apiKey));
     }
     
+    @PostMapping("/chat-messages")
+    @Operation(summary = "通过ai问答")
+    public CommonResult<String> chatMessages(@RequestBody MessageDifyaiReqVO createReqVO) {
+        Map<String, Object> inputs = new HashMap<>();
+        String apiKey = DictFrameworkUtils.parseDictDataValue("ai_key", "多轮对话");
+        inputs.put("type", createReqVO.getType());
+        
+        return success(workflowRunService.getDifyResul(inputs, createReqVO.getType(), apiKey));
+    }
+    
     /**
      * 上传新文件
      *

+ 21 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/app/aipromptmanagement/vo/MessageDifyaiReqVO.java

@@ -0,0 +1,21 @@
+package cn.iocoder.yudao.module.infra.controller.app.aipromptmanagement.vo;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+@Schema(description = "管理后台 - 工作流运行记录新增/修改 Request VO")
+@Data
+public class MessageDifyaiReqVO {
+    
+    
+    /**
+     * 事件类型
+     */
+    private String type;
+    /**
+     * 内容
+     */
+    private String query;
+    
+    
+}

+ 2 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/aipromptmanagement/WorkflowRunService.java

@@ -40,4 +40,6 @@ public interface WorkflowRunService {
     String getDifyResul(Map<String, Object> inputs, String eventType, String aiKey);
     
     String getDifyResul(Map<String, Object> inputs, String eventType, String aiKey, Long userId);
+    
+    String getDifyMessage(Map<String, Object> inputs, String eventType, String aiKey, Long userId);
 }

+ 64 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/aipromptmanagement/WorkflowRunServiceImpl.java

@@ -245,4 +245,68 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
         DifyResponse response = getDifyResponse(inputs, userId.toString(), aiKey);
         return response.getData().getOutputs().getText();
     }
+    
+    @Override
+    public String getDifyMessage(Map<String, Object> inputs, String eventType, String aiKey, Long userId) {
+        return "";
+    }
+    
+    /**
+     * 获取dify响应
+     *
+     * @param inputs 输入
+     * @param user   用户
+     * @return {@link DifyResponse }
+     */
+    public DifyResponse getDifyMessageResponse(Map<String, Object> inputs, String user, String apiKey) {
+        DifyResponse difyResponse = new DifyResponse();
+        // 接口地址
+        String url = baseUrl + "/v1/chat-messages";
+        // 替换为你的实际 API Key
+        
+        
+        // 创建一个 ObjectMapper 实例,用于处理 JSON 转换
+        
+        
+        // 构建请求体
+        JSONObject requestBody = new JSONObject();
+        requestBody.put("inputs", inputs);
+        requestBody.put("response_mode", "streaming");
+        requestBody.put("user", user);
+//        requestBody.put("conversation_id", );
+        requestBody.put("query", user);
+        
+        // 发送 POST 请求
+        HttpResponse response = HttpRequest.post(url)
+                .header("Authorization", "Bearer " + apiKey)
+                .header("Content-Type", "application/json")
+                .body(requestBody.toString())
+                .execute();
+        
+        
+        // 处理响应
+        if (response.isOk()) {
+            // Hutool工具示例
+            String decodedBody = Convert.unicodeToStr(response.body());
+            
+            // 使用 \n\n 分割字符串
+            String[] parts = decodedBody.split("\n\n");
+            
+            // 取最后一条内容
+            String lastPart = parts[parts.length - 1];
+            
+            System.out.println("请求成功,响应内容:");
+            System.out.println(lastPart);
+            // 去除"data: "前缀
+            String jsonStr = lastPart.substring(6);
+            
+            // 转换为DifyResponse实体
+            difyResponse = JSON.parseObject(jsonStr, DifyResponse.class);
+            System.out.println(difyResponse.getData().getOutputs().getText());
+        } else {
+            System.out.println("请求失败,状态码:" + response.getStatus());
+            System.out.println("错误信息:" + response.body());
+        }
+        return difyResponse;
+    }
 }

+ 18 - 0
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuConversationController.java

@@ -59,6 +59,24 @@ public class AppKeFuConversationController {
         return success(result);
     }
     
+    @GetMapping("/getByRelID")
+    @Operation(summary = "根据客服会话")
+    @Parameter(name = "relID", description = "编号", required = true, example = "1024")
+    public CommonResult<KeFuConversationRespVO> getByRelID(@RequestParam("relID") Long relID) {
+        KeFuConversationDO conversation = conversationService.getConversationByRelID(relID);
+        if (conversation == null) {
+            return success(null);
+        }
+        
+        // 拼接数据
+        KeFuConversationRespVO result = BeanUtils.toBean(conversation, KeFuConversationRespVO.class);
+        MemberUserRespDTO memberUser = memberUserApi.getUser(conversation.getUserId());
+        if (memberUser != null) {
+            result.setUserAvatar(memberUser.getAvatar()).setUserNickname(memberUser.getNickname());
+        }
+        return success(result);
+    }
+    
     @GetMapping("/init")
     @Operation(summary = "获得客服会话")
     public CommonResult<String> init() {

+ 2 - 0
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationService.java

@@ -21,6 +21,8 @@ public interface KeFuConversationService {
      * @return 客服会话
      */
     KeFuConversationDO getConversation(Long id);
+    
+    KeFuConversationDO getConversationByRelID(Long id);
 
     /**
      * 【管理员】删除客服会话

+ 24 - 1
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationServiceImpl.java

@@ -37,7 +37,30 @@ public class KeFuConversationServiceImpl implements KeFuConversationService {
     public KeFuConversationDO getConversation(Long id) {
         return conversationMapper.selectById(id);
     }
-
+    
+    @Override
+    public KeFuConversationDO getConversationByRelID(Long relId) {
+        KeFuConversationDO conversation = conversationMapper.selectOne(
+                KeFuConversationDO::getUserId, SecurityFrameworkUtils.getLoginUserId(),
+                KeFuConversationDO::getRelUserId, relId);
+        
+        // 没有历史会话,则初始化一个新会话
+        if (conversation == null) {
+            conversation = conversationMapper.selectOne(
+                    KeFuConversationDO::getRelUserId, SecurityFrameworkUtils.getLoginUserId(),
+                    KeFuConversationDO::getUserId, relId);
+            if (conversation == null) {
+                conversation =
+                        new KeFuConversationDO().setUserId(SecurityFrameworkUtils.getLoginUserId()).setLastMessageTime(LocalDateTime.now())
+                                .setLastMessageContent(StrUtil.EMPTY).setLastMessageContentType(KeFuMessageContentTypeEnum.TEXT.getType())
+                                .setAdminPinned(Boolean.FALSE).setUserDeleted(Boolean.FALSE).setAdminDeleted(Boolean.FALSE)
+                                .setAdminUnreadMessageCount(0);
+                conversation.setRelUserId(relId);
+                conversationMapper.insert(conversation);
+            }
+        }
+        return conversation;
+    }
     @Override
     public void deleteKefuConversation(Long id) {
         // 校验存在