183207892172 2 тижнів тому
батько
коміт
e4e210d961

+ 61 - 0
java/storlead-knowledge/storlead-knowledge-api/src/main/java/com/storlead/knowledge/api/WorkflowsController.java

@@ -0,0 +1,61 @@
+package com.storlead.knowledge.api;
+
+
+import com.storlead.framework.common.result.Result;
+import com.storlead.knowledge.config.DifyProperties;
+import com.storlead.knowledge.pojo.dto.ChatDTO;
+import com.storlead.knowledge.service.WorkflowsService;
+import com.storlead.knowledge.utils.HttpService;
+import com.storlead.knowledge.utils.JacksonHolder;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+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.servlet.mvc.method.annotation.SseEmitter;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+
+@RestController
+@RequestMapping("/workflows/")
+@Api(tags = "知识库: 流程")
+public class WorkflowsController {
+    @Resource
+    private HttpService httpService;
+
+    @Resource
+    private DifyProperties difyProperties;
+    @Resource
+    private WorkflowsService workflowsService;
+
+
+    @PostMapping("blocking")
+    @ApiOperation("执行流程")
+    // “”
+    public Result<Object> workflowsBlock(@RequestBody ChatDTO chatDTO) {
+        return workflowsService.AiWorkflows(chatDTO);
+    }
+
+    @PostMapping("streaming")
+    @ApiOperation("执行流程 流式返回")
+    public SseEmitter workflowsStreaming(@RequestBody ChatDTO chatDTO) throws IOException {
+
+        SseEmitter emitter = new SseEmitter(0L); // 0 = 不超时
+
+        String url = difyProperties.getBaseUrl() + "workflows/run";
+
+        String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(chatDTO);
+        httpService.postStream(
+                url,
+                "Bearer " +chatDTO.getAppId(),
+                body,
+                emitter,
+                null
+        );
+
+        return emitter;
+    }
+
+}

+ 46 - 0
java/storlead-knowledge/storlead-knowledge-biz/src/main/java/com/storlead/knowledge/service/impl/WorkflowsServiceImpl.java

@@ -0,0 +1,46 @@
+package com.storlead.knowledge.service.impl;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.storlead.framework.common.result.Result;
+import com.storlead.knowledge.config.DifyProperties;
+import com.storlead.knowledge.entity.AiMessageEntity;
+import com.storlead.knowledge.pojo.dto.ChatDTO;
+import com.storlead.knowledge.service.AiMessageService;
+import com.storlead.knowledge.service.ChatService;
+import com.storlead.knowledge.service.WorkflowsService;
+import com.storlead.knowledge.utils.HttpService;
+import com.storlead.knowledge.utils.JacksonHolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+/**
+ * 对话服务实现
+ */
+@Service
+public class WorkflowsServiceImpl implements WorkflowsService{
+
+    private static final Logger log = LoggerFactory.getLogger(WorkflowsServiceImpl.class);
+
+    @Resource
+    private HttpService httpService;
+
+    @Resource
+    private DifyProperties difyProperties;
+
+    @Override
+    public Result<Object> AiWorkflows(ChatDTO chatDTO) {
+        try {
+            String url = difyProperties.getBaseUrl() + "workflows/run";
+            String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(chatDTO);
+            return httpService.post(url, null, "Bearer " + chatDTO.getAppId(), body, new TypeReference<>() {});
+        }catch (Exception e){
+            return Result.error("requestAnalysis -- error",e.getMessage());
+        }
+    }
+
+
+}

+ 19 - 0
java/storlead-knowledge/storlead-knowledge-spi/src/main/java/com/storlead/knowledge/service/WorkflowsService.java

@@ -0,0 +1,19 @@
+package com.storlead.knowledge.service;
+
+import com.storlead.framework.common.result.Result;
+import com.storlead.knowledge.pojo.dto.ChatDTO;
+
+/**
+ * 流程对话服务(供其他模块通过 SPI 直接调用,不依赖 HTTP Controller)
+ */
+public interface WorkflowsService {
+
+    /**
+     * 创建聊天(blocking 模式),逻辑与 {@code ChatController#chat} 一致
+     *
+     * @param
+     * @return Dify 调用结果
+     */
+    Result<Object> AiWorkflows(ChatDTO chatDTO);
+
+}