diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/InstructionController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/InstructionController.java index fee0083..98b9d1a 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/InstructionController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/InstructionController.java @@ -34,15 +34,17 @@ public class InstructionController extends BaseController { } /** - * 获取历史消息记录 + * 获取历史消息记录(支持关键词搜索,在全部历史数据中匹配) * @param type 消息类型:request(请求) 或 response(响应) - * @param limit 获取数量,默认100条 + * @param limit 获取数量,默认100条;有 keyword 时为返回匹配条数上限,默认200 + * @param keyword 可选,搜索关键词;不为空时在全部数据中过滤后返回 * @return 历史消息列表 */ @GetMapping("/history") public AjaxResult getHistory(@RequestParam(required = false, defaultValue = "request") String type, - @RequestParam(required = false, defaultValue = "100") Integer limit) { - java.util.List history = instructionService.getHistory(type, limit); + @RequestParam(required = false, defaultValue = "100") Integer limit, + @RequestParam(required = false) String keyword) { + java.util.List history = instructionService.getHistory(type, limit, keyword); return AjaxResult.success(history); } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IInstructionService.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IInstructionService.java index 90812ed..af01765 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IInstructionService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IInstructionService.java @@ -35,6 +35,15 @@ public interface IInstructionService { * @return 历史消息列表 */ java.util.List getHistory(String type, Integer limit); + + /** + * 获取历史消息记录(支持关键词搜索,在全部数据中匹配) + * @param type 消息类型:request(请求) 或 response(响应) + * @param limit 返回数量上限,默认200条 + * @param keyword 搜索关键词,为空则按 limit 取最近 N 条 + * @return 历史消息列表 + */ + java.util.List getHistory(String type, Integer limit, String keyword); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java index 649ee0e..8d80a47 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java @@ -142,12 +142,16 @@ public class InstructionServiceImpl implements IInstructionService { @Override public List getHistory(String type, Integer limit) { + return getHistory(type, limit, null); + } + + @Override + public List getHistory(String type, Integer limit, String keyword) { if (stringRedisTemplate == null) { return Collections.emptyList(); } try { - // 确定Redis键 String key; if ("request".equalsIgnoreCase(type)) { key = "instruction:request"; @@ -157,11 +161,23 @@ public class InstructionServiceImpl implements IInstructionService { return Collections.emptyList(); } - // 确定获取数量,默认100条 - int count = (limit != null && limit > 0) ? Math.min(limit, 1000) : 1000; + int maxReturn = (limit != null && limit > 0) ? Math.min(limit, 1000) : 200; + boolean hasKeyword = keyword != null && !keyword.trim().isEmpty(); + String kwLower = hasKeyword ? keyword.trim().toLowerCase() : null; - // 从Redis获取历史消息(索引0到count-1) - List messages = stringRedisTemplate.opsForList().range(key, 0, count - 1); + List messages; + if (hasKeyword) { + // 搜索模式:取全部数据后在内存中按关键词过滤 + messages = stringRedisTemplate.opsForList().range(key, 0, -1); + if (messages == null) messages = Collections.emptyList(); + messages = messages.stream() + .filter(msg -> msg != null && msg.toLowerCase().contains(kwLower)) + .limit(maxReturn) + .collect(Collectors.toList()); + } else { + // 普通模式:只取最近 N 条 + messages = stringRedisTemplate.opsForList().range(key, 0, maxReturn - 1); + } return messages != null ? messages : Collections.emptyList(); } catch (Exception e) {