From 87b94e9350122eebb979a62b1792fab278e6cf47 Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期三, 20 五月 2026 16:05:29 +0800
Subject: [PATCH] refactor(controller): 将控制器响应结果统一为R类型并继承BaseController
---
src/main/java/com/ruoyi/ai/controller/XiaozhiController.java | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 159 insertions(+), 9 deletions(-)
diff --git a/src/main/java/com/ruoyi/ai/controller/XiaozhiController.java b/src/main/java/com/ruoyi/ai/controller/XiaozhiController.java
index e1223cc..668dd31 100644
--- a/src/main/java/com/ruoyi/ai/controller/XiaozhiController.java
+++ b/src/main/java/com/ruoyi/ai/controller/XiaozhiController.java
@@ -2,35 +2,185 @@
import com.ruoyi.ai.assistant.ApproveTodoAgent;
import com.ruoyi.ai.assistant.ApproveTodoIntentExecutor;
+import com.ruoyi.ai.assistant.FileAnalyzeAgent;
import com.ruoyi.ai.bean.ChatForm;
+import com.ruoyi.ai.context.AiSessionUserContext;
+import com.ruoyi.ai.service.AiChatSessionService;
+import com.ruoyi.ai.service.AiFileTextExtractor;
+import com.ruoyi.ai.store.MongoChatMemoryStore;
+import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.framework.security.LoginUser;
+import com.ruoyi.framework.web.controller.BaseController;
+import com.ruoyi.framework.web.domain.R;
+import dev.langchain4j.data.message.AiMessage;
+import dev.langchain4j.data.message.UserMessage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
-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.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.UUID;
@Tag(name = "鍗忓悓鍔炲叕鍔╂墜")
@RestController
@RequestMapping("/xiaozhi")
-public class XiaozhiController {
+public class XiaozhiController extends BaseController {
+
+ private static final String FILE_ANALYZE_MEMORY_PREFIX = "file-analyze::";
private final ApproveTodoAgent approveTodoAgent;
private final ApproveTodoIntentExecutor approveTodoIntentExecutor;
+ private final FileAnalyzeAgent fileAnalyzeAgent;
+ private final AiSessionUserContext aiSessionUserContext;
+ private final MongoChatMemoryStore mongoChatMemoryStore;
+ private final AiFileTextExtractor aiFileTextExtractor;
+ private final AiChatSessionService aiChatSessionService;
- public XiaozhiController(ApproveTodoAgent approveTodoAgent, ApproveTodoIntentExecutor approveTodoIntentExecutor) {
+ public XiaozhiController(ApproveTodoAgent approveTodoAgent,
+ ApproveTodoIntentExecutor approveTodoIntentExecutor,
+ FileAnalyzeAgent fileAnalyzeAgent,
+ AiSessionUserContext aiSessionUserContext,
+ MongoChatMemoryStore mongoChatMemoryStore,
+ AiFileTextExtractor aiFileTextExtractor,
+ AiChatSessionService aiChatSessionService) {
this.approveTodoAgent = approveTodoAgent;
this.approveTodoIntentExecutor = approveTodoIntentExecutor;
+ this.fileAnalyzeAgent = fileAnalyzeAgent;
+ this.aiSessionUserContext = aiSessionUserContext;
+ this.mongoChatMemoryStore = mongoChatMemoryStore;
+ this.aiFileTextExtractor = aiFileTextExtractor;
+ this.aiChatSessionService = aiChatSessionService;
}
@Operation(summary = "瀵硅瘽")
@PostMapping(value = "/chat", produces = "text/stream;charset=utf-8")
public Flux<String> chat(@RequestBody ChatForm chatForm) {
- String directResponse = approveTodoIntentExecutor.tryExecute(chatForm.getMessage());
- if (directResponse != null) {
+ if (!StringUtils.hasText(chatForm.getMemoryId())) {
+ return Flux.just("memoryId涓嶈兘涓虹┖");
+ }
+ if (!StringUtils.hasText(chatForm.getMessage())) {
+ return Flux.just("message涓嶈兘涓虹┖");
+ }
+ LoginUser loginUser = SecurityUtils.getLoginUser();
+ String memoryId = chatForm.getMemoryId();
+ String userMessage = chatForm.getMessage();
+
+ aiSessionUserContext.bind(memoryId, loginUser);
+ aiChatSessionService.touchSession(memoryId, loginUser, userMessage);
+
+ String directResponse = approveTodoIntentExecutor.tryExecute(memoryId, userMessage);
+ if (StringUtils.isNotEmpty(directResponse)) {
+ mongoChatMemoryStore.appendMessages(
+ memoryId,
+ List.of(UserMessage.from(userMessage), AiMessage.from(directResponse))
+ );
+ aiChatSessionService.refreshSessionStats(memoryId, loginUser);
return Flux.just(directResponse);
}
- return approveTodoAgent.chat(chatForm.getMemoryId(), chatForm.getMessage());
+
+ if (isApproveTodoBusinessIntent(userMessage)) {
+ String noGuessResponse = "鏈瘑鍒埌鍙墽琛岀殑瀹℃壒寰呭姙鎿嶄綔鏉′欢銆備负淇濊瘉缁撴灉鍑嗙‘锛屽綋鍓嶄笉浼氭帹娴嬫垨缂栭�犲鎵规暟鎹紝璇疯ˉ鍏呮祦绋嬬紪鍙枫�佹椂闂磋寖鍥存垨鏄庣‘鎿嶄綔鎸囦护鍚庡啀璇曘��";
+ mongoChatMemoryStore.appendMessages(
+ memoryId,
+ List.of(UserMessage.from(userMessage), AiMessage.from(noGuessResponse))
+ );
+ aiChatSessionService.refreshSessionStats(memoryId, loginUser);
+ return Flux.just(noGuessResponse);
+ }
+
+ return approveTodoAgent.chat(memoryId, userMessage)
+ .doOnComplete(() -> aiChatSessionService.refreshSessionStats(memoryId, loginUser))
+ .doOnError(ex -> aiChatSessionService.refreshSessionStats(memoryId, loginUser));
+ }
+
+ @Operation(summary = "涓婁紶鏂囦欢鍒嗘瀽")
+ @PostMapping(value = "/analyze-file", consumes = "multipart/form-data", produces = "text/stream;charset=utf-8")
+ public Flux<String> analyzeFile(@RequestParam("file") MultipartFile file,
+ @RequestParam(value = "message", required = false) String message,
+ @RequestParam(value = "memoryId", required = false) String memoryId) {
+ String rawMemoryId = StringUtils.hasText(memoryId) ? memoryId : UUID.randomUUID().toString();
+ String finalMemoryId = rawMemoryId.startsWith(FILE_ANALYZE_MEMORY_PREFIX)
+ ? rawMemoryId
+ : FILE_ANALYZE_MEMORY_PREFIX + rawMemoryId;
+
+ LoginUser loginUser = SecurityUtils.getLoginUser();
+ aiSessionUserContext.bind(finalMemoryId, loginUser);
+
+ String finalMessage = StringUtils.hasText(message) ? message : "璇峰垎鏋愯繖涓枃浠剁殑鏍稿績鍐呭骞剁粰鍑烘�荤粨";
+ String fileText;
+ try {
+ fileText = aiFileTextExtractor.extractText(file);
+ } catch (IllegalArgumentException ex) {
+ return Flux.just(ex.getMessage());
+ } catch (IOException ex) {
+ return Flux.just("鏂囦欢璇诲彇澶辫触");
+ }
+
+ if (!StringUtils.hasText(fileText)) {
+ return Flux.just("鏈彁鍙栧埌鏈夋晥鏂囦欢鍐呭");
+ }
+
+ String limitedContent = fileText.length() > 12000 ? fileText.substring(0, 12000) : fileText;
+ String userPrompt = "浣犲皢鍒嗘瀽鐢ㄦ埛涓婁紶鐨勬枃浠跺唴瀹广�俓n"
+ + "鏂囦欢鍚�: " + file.getOriginalFilename() + "\n"
+ + "鐢ㄦ埛闂: " + finalMessage + "\n"
+ + "鏂囦欢鍐呭濡備笅:\n"
+ + limitedContent;
+
+ aiChatSessionService.touchSession(finalMemoryId, loginUser, "鏂囦欢鍒嗘瀽: " + finalMessage);
+
+ return Flux.defer(() -> fileAnalyzeAgent.chat(finalMemoryId, userPrompt))
+ .onErrorResume(NoSuchElementException.class, ex -> {
+ // DashScope 鍦ㄥ巻鍙叉秷鎭吋瀹瑰満鏅笅鍙兘鎶� NoSuchElementException锛屾竻绌哄悗閲嶈瘯涓�娆�
+ mongoChatMemoryStore.deleteMessages(finalMemoryId);
+ return fileAnalyzeAgent.chat(finalMemoryId, userPrompt);
+ })
+ .doOnComplete(() -> aiChatSessionService.refreshSessionStats(finalMemoryId, loginUser))
+ .doOnError(ex -> aiChatSessionService.refreshSessionStats(finalMemoryId, loginUser));
+ }
+
+ @Operation(summary = "浼氳瘽鍒楄〃")
+ @GetMapping("/history/sessions")
+ public R listSessions() {
+ return R.ok(aiChatSessionService.listCurrentUserSessions(SecurityUtils.getLoginUser()));
+ }
+
+ @Operation(summary = "浼氳瘽娑堟伅")
+ @GetMapping("/history/messages/{memoryId}")
+ public R listMessages(@PathVariable String memoryId) {
+ return R.ok(aiChatSessionService.listCurrentUserMessages(memoryId, SecurityUtils.getLoginUser()));
+ }
+
+ @Operation(summary = "鍒犻櫎浼氳瘽")
+ @DeleteMapping("/history/{memoryId}")
+ public R deleteSession(@PathVariable String memoryId) {
+ aiSessionUserContext.remove(memoryId);
+ return R.ok(aiChatSessionService.deleteCurrentUserSession(memoryId, SecurityUtils.getLoginUser()));
+ }
+
+ private boolean isApproveTodoBusinessIntent(String message) {
+ if (!StringUtils.hasText(message)) {
+ return false;
+ }
+ String text = message.trim();
+ boolean hasDomainWord = containsAny(text,
+ "瀹℃壒", "寰呭姙", "娴佺▼缂栧彿", "娴佺▼鍙�", "瀹℃壒娴佽浆", "瀹℃壒鑺傜偣", "褰撳墠瀹℃壒浜�", "椹冲洖", "閫氳繃", "鎾ら攢", "鍒犻櫎");
+ boolean hasIntentWord = containsAny(text,
+ "鏌ヨ", "鏌ョ湅", "鍒楀嚭", "缁熻", "鍒嗘瀽", "鍒嗗竷", "閫氳繃", "椹冲洖", "鎾ら攢", "鍒犻櫎", "淇敼", "鏈夊摢浜�", "鍗″湪");
+ return hasDomainWord && hasIntentWord;
+ }
+
+ private boolean containsAny(String text, String... keywords) {
+ for (String keyword : keywords) {
+ if (text.contains(keyword)) {
+ return true;
+ }
+ }
+ return false;
}
}
--
Gitblit v1.9.3