This commit is contained in:
van
2026-03-09 15:25:22 +08:00
parent dc8b0b2fcf
commit b4749f3516
3 changed files with 36 additions and 9 deletions

View File

@@ -35,6 +35,15 @@ public interface IInstructionService {
* @return 历史消息列表
*/
java.util.List<String> getHistory(String type, Integer limit);
/**
* 获取历史消息记录(支持关键词搜索,在全部数据中匹配)
* @param type 消息类型request(请求) 或 response(响应)
* @param limit 返回数量上限默认200条
* @param keyword 搜索关键词,为空则按 limit 取最近 N 条
* @return 历史消息列表
*/
java.util.List<String> getHistory(String type, Integer limit, String keyword);
}

View File

@@ -142,12 +142,16 @@ public class InstructionServiceImpl implements IInstructionService {
@Override
public List<String> getHistory(String type, Integer limit) {
return getHistory(type, limit, null);
}
@Override
public List<String> 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<String> messages = stringRedisTemplate.opsForList().range(key, 0, count - 1);
List<String> 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) {