feat: 流程编排init

This commit is contained in:
lihao05
2025-10-16 21:38:00 +08:00
parent f906645708
commit 77ddd169c7
114 changed files with 6313 additions and 17 deletions

View File

@@ -22,6 +22,7 @@
<module>ruoyi-system</module>
<module>ruoyi-generator</module>
<module>ruoyi-wechat</module>
<module>ruoyi-workflow</module>
</modules>
<properties>

View File

@@ -1,5 +1,6 @@
package org.ruoyi.chat.service.chat;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import org.ruoyi.common.chat.request.ChatRequest;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@@ -13,9 +14,21 @@ public interface IChatService {
/**
* 客户端发送消息到服务端
*
* @param chatRequest 请求对象
*/
SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter);
SseEmitter chat(ChatRequest chatRequest, SseEmitter emitter);
/**
* 工作流场景:支持 langchain4j 的 StreamingChatResponseHandler
*
* @param chatRequest ruoyi-ai 的请求对象
* @param handler langchain4j 的流式响应处理器
*/
default void chat(ChatRequest chatRequest, StreamingChatResponseHandler handler) {
throw new UnsupportedOperationException("此服务暂不支持工作流场景");
}
/**
* 获取此服务支持的模型类别
*/

View File

@@ -1,6 +1,10 @@
package org.ruoyi.chat.service.chat.impl;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
@@ -16,6 +20,9 @@ import org.ruoyi.service.IChatModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.ArrayList;
import java.util.List;
/**
* deepseek
*/
@@ -70,6 +77,52 @@ public class DeepSeekChatImpl implements IChatService {
return emitter;
}
/**
* 工作流场景:支持 langchain4j handler
*/
@Override
public void chat(ChatRequest request, StreamingChatResponseHandler handler) {
log.info("workflow chat, model: {}", request.getModel());
ChatModelVo chatModelVo = chatModelService.selectModelByName(request.getModel());
StreamingChatModel chatModel = OpenAiStreamingChatModel.builder()
.baseUrl(chatModelVo.getApiHost())
.apiKey(chatModelVo.getApiKey())
.modelName(chatModelVo.getModelName())
.logRequests(true)
.logResponses(true)
.temperature(0.8)
.build();
try {
// 将 ruoyi-ai 的 ChatRequest 转换为 langchain4j 的格式
dev.langchain4j.model.chat.request.ChatRequest langchainRequest = convertToLangchainRequest(request);
chatModel.chat(langchainRequest, handler);
} catch (Exception e) {
log.error("workflow deepseek请求失败{}", e.getMessage(), e);
throw new RuntimeException("DeepSeek workflow chat failed: " + e.getMessage(), e);
}
}
/**
* 转换请求格式
*/
private dev.langchain4j.model.chat.request.ChatRequest convertToLangchainRequest(ChatRequest request) {
List<ChatMessage> messages = new ArrayList<>();
for (org.ruoyi.common.chat.entity.chat.Message msg : request.getMessages()) {
// 简单转换,您可以根据实际需求调整
if ("user".equals(msg.getRole())) {
messages.add(UserMessage.from(msg.getContent().toString()));
} else if ("system".equals(msg.getRole())) {
messages.add(SystemMessage.from(msg.getContent().toString()));
} else if ("assistant".equals(msg.getRole())) {
messages.add(AiMessage.from(msg.getContent().toString()));
}
}
return dev.langchain4j.model.chat.request.ChatRequest.builder().messages(messages).build();
}
@Override
public String getCategory() {
return ChatModeType.DEEPSEEK.getCode();

View File

@@ -1,6 +1,10 @@
package org.ruoyi.chat.service.chat.impl;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
@@ -8,13 +12,16 @@ import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.ruoyi.chat.enums.ChatModeType;
import org.ruoyi.chat.service.chat.IChatService;
import org.ruoyi.chat.support.ChatServiceHelper;
import org.ruoyi.common.chat.request.ChatRequest;
import org.ruoyi.domain.vo.ChatModelVo;
import org.ruoyi.service.IChatModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import org.ruoyi.chat.support.ChatServiceHelper;
import java.util.ArrayList;
import java.util.List;
/**
@@ -22,7 +29,7 @@ import org.ruoyi.chat.support.ChatServiceHelper;
*/
@Service
@Slf4j
public class QianWenAiChatServiceImpl implements IChatService {
public class QianWenAiChatServiceImpl implements IChatService {
@Autowired
private IChatModelService chatModelService;
@@ -37,7 +44,6 @@ public class QianWenAiChatServiceImpl implements IChatService {
.build();
// 发送流式消息
try {
model.chat(chatRequest.getPrompt(), new StreamingChatResponseHandler() {
@@ -70,11 +76,53 @@ public class QianWenAiChatServiceImpl implements IChatService {
}
/**
* 工作流场景:支持 langchain4j handler
*/
@Override
public void chat(ChatRequest request, StreamingChatResponseHandler handler) {
log.info("workflow chat, model: {}", request.getModel());
ChatModelVo chatModelVo = chatModelService.selectModelByName(request.getModel());
StreamingChatModel model = QwenStreamingChatModel.builder()
.apiKey(chatModelVo.getApiKey())
.modelName(chatModelVo.getModelName())
.build();
try {
// 将 ruoyi-ai 的 ChatRequest 转换为 langchain4j 的格式
dev.langchain4j.model.chat.request.ChatRequest langchainRequest = convertToLangchainRequest(request);
model.chat(langchainRequest, handler);
} catch (Exception e) {
log.error("workflow 千问请求失败:{}", e.getMessage(), e);
throw new RuntimeException("QianWen workflow chat failed: " + e.getMessage(), e);
}
}
/**
* 转换请求格式
*/
private dev.langchain4j.model.chat.request.ChatRequest convertToLangchainRequest(ChatRequest request) {
List<ChatMessage> messages = new ArrayList<>();
for (org.ruoyi.common.chat.entity.chat.Message msg : request.getMessages()) {
if ("user".equals(msg.getRole())) {
messages.add(UserMessage.from(msg.getContent().toString()));
} else if ("system".equals(msg.getRole())) {
messages.add(SystemMessage.from(msg.getContent().toString()));
} else if ("assistant".equals(msg.getRole())) {
messages.add(AiMessage.from(msg.getContent().toString()));
}
}
return dev.langchain4j.model.chat.request.ChatRequest.builder()
.messages(messages)
.build();
}
@Override
public String getCategory() {
return ChatModeType.QIANWEN.getCode();
}
}

View File

@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.ruoyi</groupId>
<artifactId>ruoyi-modules</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>ruoyi-workflow</artifactId>
<description>
工作流模块
</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.ruoyi</groupId>
<artifactId>ruoyi-workflow-api</artifactId>
</dependency>
<dependency>
<groupId>org.ruoyi</groupId>
<artifactId>ruoyi-system-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>com.talanlabs</groupId>
<artifactId>avatar-generator</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.talanlabs</groupId>
<artifactId>avatar-generator-cat</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,115 @@
package org.ruoyi.workflow.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.workflow.base.ThreadContext;
import org.ruoyi.workflow.dto.workflow.*;
import org.ruoyi.workflow.entity.WorkflowComponent;
import org.ruoyi.workflow.service.WorkflowComponentService;
import org.ruoyi.workflow.service.WorkflowService;
import org.ruoyi.workflow.workflow.WorkflowStarter;
import org.ruoyi.workflow.workflow.node.switcher.OperatorEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/workflow")
@Validated
public class WorkflowController {
@Resource
private WorkflowStarter workflowStarter;
@Resource
private WorkflowService workflowService;
@Resource
private WorkflowComponentService workflowComponentService;
@PostMapping("/add")
public R<WorkflowResp> add(@RequestBody @Validated WfAddReq addReq) {
return R.ok(workflowService.add(addReq.getTitle(), addReq.getRemark(), addReq.getIsPublic()));
}
@PostMapping("/set-public/{wfUuid}")
public R setPublic(@PathVariable String wfUuid, @RequestParam(defaultValue = "true") Boolean isPublic) {
workflowService.setPublic(wfUuid, isPublic);
return R.ok();
}
@PostMapping("/update")
public R<WorkflowResp> update(@RequestBody @Validated WorkflowUpdateReq req) {
return R.ok(workflowService.update(req));
}
@PostMapping("/del/{uuid}")
public R delete(@PathVariable String uuid) {
workflowService.softDelete(uuid);
return R.ok();
}
@PostMapping("/enable/{uuid}")
public R enable(@PathVariable String uuid, @RequestParam Boolean enable) {
workflowService.enable(uuid, enable);
return R.ok();
}
@PostMapping("/base-info/update")
public R<WorkflowResp> updateBaseInfo(@RequestBody @Validated WfBaseInfoUpdateReq req) {
return R.ok(workflowService.updateBaseInfo(req.getUuid(), req.getTitle(), req.getRemark(), req.getIsPublic()));
}
@Operation(summary = "流式响应")
@PostMapping(value = "/run", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter sseAsk(@RequestBody WorkflowRunReq runReq) {
return workflowStarter.streaming(ThreadContext.getCurrentUser(), runReq.getUuid(), runReq.getInputs());
}
@GetMapping("/mine/search")
public R<Page<WorkflowResp>> searchMine(@RequestParam(defaultValue = "") String keyword,
@RequestParam(required = false) Boolean isPublic,
@NotNull @Min(1) Integer currentPage,
@NotNull @Min(10) Integer pageSize) {
return R.ok(workflowService.search(keyword, isPublic, null, currentPage, pageSize));
}
/**
* 搜索公开工作流
*
* @param keyword 搜索关键词
* @param currentPage 当前页数
* @param pageSize 每页数量
* @return 工作流列表
*/
@GetMapping("/public/search")
public R<Page<WorkflowResp>> searchPublic(@RequestParam(defaultValue = "") String keyword,
@NotNull @Min(1) Integer currentPage,
@NotNull @Min(10) Integer pageSize) {
return R.ok(workflowService.searchPublic(keyword, currentPage, pageSize));
}
@GetMapping("/public/operators")
public R<List<Map<String, String>>> searchPublic() {
List<Map<String, String>> result = new ArrayList<>();
for (OperatorEnum operator : OperatorEnum.values()) {
result.add(Map.of("name", operator.getName(), "desc", operator.getDesc()));
}
return R.ok(result);
}
@GetMapping("/public/component/list")
public R<List<WorkflowComponent>> component() {
return R.ok(workflowComponentService.getAllEnable());
}
}

View File

@@ -0,0 +1,58 @@
package org.ruoyi.workflow.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.workflow.dto.workflow.WfRuntimeNodeDto;
import org.ruoyi.workflow.dto.workflow.WfRuntimeResp;
import org.ruoyi.workflow.dto.workflow.WorkflowResumeReq;
import org.ruoyi.workflow.service.WorkflowRuntimeService;
import org.ruoyi.workflow.workflow.WorkflowStarter;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/workflow/runtime")
@Validated
public class WorkflowRuntimeController {
@Resource
private WorkflowRuntimeService workflowRuntimeService;
@Resource
private WorkflowStarter workflowStarter;
@Operation(summary = "接收用户输入以继续执行剩余流程")
@PostMapping(value = "/resume/{runtimeUuid}")
public R resume(@PathVariable String runtimeUuid, @RequestBody WorkflowResumeReq resumeReq) {
workflowStarter.resumeFlow(runtimeUuid, resumeReq.getFeedbackContent());
return R.ok();
}
@GetMapping("/page")
public R<Page<WfRuntimeResp>> search(@RequestParam String wfUuid,
@NotNull @Min(1) Integer currentPage,
@NotNull @Min(10) Integer pageSize) {
return R.ok(workflowRuntimeService.page(wfUuid, currentPage, pageSize));
}
@GetMapping("/nodes/{runtimeUuid}")
public R<List<WfRuntimeNodeDto>> listByRuntimeId(@PathVariable String runtimeUuid) {
return R.ok(workflowRuntimeService.listByRuntimeUuid(runtimeUuid));
}
@PostMapping("/clear")
public R<Boolean> clear(@RequestParam(defaultValue = "") String wfUuid) {
return R.ok(workflowRuntimeService.deleteAll(wfUuid));
}
@PostMapping("/del/{wfRuntimeUuid}")
public R<Boolean> delete(@PathVariable String wfRuntimeUuid) {
return R.ok(workflowRuntimeService.softDelete(wfRuntimeUuid));
}
}

View File

@@ -0,0 +1,45 @@
package org.ruoyi.workflow.controller.admin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.workflow.dto.workflow.WfComponentReq;
import org.ruoyi.workflow.dto.workflow.WfComponentSearchReq;
import org.ruoyi.workflow.entity.WorkflowComponent;
import org.ruoyi.workflow.service.WorkflowComponentService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/admin/workflow/component")
@Validated
public class AdminWorkflowComponentController {
@Resource
private WorkflowComponentService workflowComponentService;
@PostMapping("/search")
public R<Page<WorkflowComponent>> search(@RequestBody WfComponentSearchReq searchReq, @NotNull @Min(1) Integer currentPage, @NotNull @Min(10) Integer pageSize) {
return R.ok(workflowComponentService.search(searchReq, currentPage, pageSize));
}
@PostMapping("/enable")
public R enable(@RequestParam String uuid, @RequestParam Boolean isEnable) {
workflowComponentService.enable(uuid, isEnable);
return R.ok();
}
@PostMapping("/del/{uuid}")
public R del(@PathVariable String uuid) {
workflowComponentService.deleteByUuid(uuid);
return R.ok();
}
@PostMapping("/addOrUpdate")
public R<WorkflowComponent> addOrUpdate(@Validated @RequestBody WfComponentReq req) {
return R.ok(workflowComponentService.addOrUpdate(req));
}
}

View File

@@ -0,0 +1,35 @@
package org.ruoyi.workflow.controller.admin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.workflow.dto.workflow.WfSearchReq;
import org.ruoyi.workflow.dto.workflow.WorkflowResp;
import org.ruoyi.workflow.service.WorkflowService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/admin/workflow")
@Validated
public class AdminWorkflowController {
@Resource
private WorkflowService workflowService;
@PostMapping("/search")
public R<Page<WorkflowResp>> search(@RequestBody WfSearchReq req,
@RequestParam @NotNull @Min(1) Integer currentPage,
@RequestParam @NotNull @Min(10) Integer pageSize) {
return R.ok(workflowService.search(req.getTitle(), req.getIsPublic(),
req.getIsEnable(), currentPage, pageSize));
}
@PostMapping("/enable")
public R enable(@RequestParam String uuid, @RequestParam Boolean isEnable) {
workflowService.enable(uuid, isEnable);
return R.ok();
}
}