Merge branch 'feature_20250930_work_flow' of https://github.com/MuSan-Li/ruoyi-ai into main

# Conflicts:
#	pom.xml
#	ruoyi-admin/pom.xml
#	ruoyi-modules/pom.xml
#	ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/DeepSeekChatImpl.java
This commit is contained in:
lihao05
2025-10-21 10:17:50 +08:00
116 changed files with 6839 additions and 25 deletions

View File

@@ -22,10 +22,9 @@
<module>ruoyi-system</module>
<module>ruoyi-generator</module>
<module>ruoyi-wechat</module>
<!-- 新添加的数字人模块-->
<module>ruoyi-aihuman</module>
<module>ruoyi-workflow</module>
</modules>
<properties>

View File

@@ -1,8 +1,16 @@
package org.ruoyi.chat.service.chat;
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.response.StreamingChatResponseHandler;
import org.ruoyi.common.chat.request.ChatRequest;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.ArrayList;
import java.util.List;
/**
* 对话Service接口
*
@@ -13,9 +21,36 @@ 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("此服务暂不支持工作流场景");
}
default 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();
}
/**
* 获取此服务支持的模型类别
*/

View File

@@ -1,6 +1,10 @@
package org.ruoyi.chat.service.chat.impl;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
@@ -34,6 +38,9 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
/**
* deepseek
*/
@@ -74,7 +81,7 @@ public class DeepSeekChatImpl implements IChatService {
try {
// 构建消息列表,包含历史对话消息和当前用户消息
List<ChatMessage> messages = new ArrayList<>();
// 添加历史对话消息
if (chatRequest.getMessages() != null) {
for (Message message : chatRequest.getMessages()) {
@@ -82,7 +89,7 @@ public class DeepSeekChatImpl implements IChatService {
if (message.getContent() == null || String.valueOf(message.getContent()).trim().isEmpty()) {
continue; // 跳过空消息
}
if (Message.Role.SYSTEM.getName().equals(message.getRole())) {
messages.add(new SystemMessage(String.valueOf(message.getContent())));
} else if (Message.Role.USER.getName().equals(message.getRole())) {
@@ -92,7 +99,7 @@ public class DeepSeekChatImpl implements IChatService {
}
}
}
// 添加当前用户消息
messages.add(new UserMessage(chatRequest.getPrompt()));
@@ -127,6 +134,34 @@ 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.7)
.build();
try {
// 将 ruoyi-ai 的 ChatRequest 转换为 langchain4j 的格式
dev.langchain4j.model.chat.request.ChatRequest chatRequest = convertToLangchainRequest(request);
chatModel.chat(chatRequest, handler);
} catch (Exception e) {
log.error("workflow deepseek请求失败{}", e.getMessage(), e);
throw new RuntimeException("DeepSeek workflow chat failed: " + e.getMessage(), e);
}
}
/**
* 处理启用深度思考的deepseek模型请求
*/
@@ -157,13 +192,13 @@ public class DeepSeekChatImpl implements IChatService {
if (message.getContent() == null || String.valueOf(message.getContent()).trim().isEmpty()) {
continue; // 跳过空消息
}
// DeepSeek模型在深度思考模式下只接受user和assistant角色的消息
if (Message.Role.SYSTEM.getName().equals(message.getRole())) {
// 跳过系统消息
continue;
}
Map<String, Object> historyMessage = new HashMap<>();
historyMessage.put("role", message.getRole());
historyMessage.put("content", String.valueOf(message.getContent()));

View File

@@ -8,13 +8,13 @@ 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;
/**
@@ -22,7 +22,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 +37,6 @@ public class QianWenAiChatServiceImpl implements IChatService {
.build();
// 发送流式消息
try {
model.chat(chatRequest.getPrompt(), new StreamingChatResponseHandler() {
@@ -70,11 +69,34 @@ 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 chatRequest = convertToLangchainRequest(request);
model.chat(chatRequest, handler);
} catch (Exception e) {
log.error("workflow 千问请求失败:{}", e.getMessage(), e);
throw new RuntimeException("QianWen workflow chat failed: " + e.getMessage(), e);
}
}
@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,114 @@
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.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();
}
}