fix(memory): enhance null-safe in memory-related modules

This commit is contained in:
2026-04-21 09:18:49 +08:00
parent 43c2c648ac
commit 4af8feb178
3 changed files with 40 additions and 10 deletions

View File

@@ -96,6 +96,9 @@ public class MemoryRuntime extends AbstractAgentModule.Standalone implements Sta
}
public String fixTopicPath(String topicPath) {
if (topicPath == null || topicPath.isBlank()) {
return "";
}
String[] parts = topicPath.split("->");
List<String> cleanedParts = new ArrayList<>();
for (String part : parts) {

View File

@@ -155,6 +155,9 @@ public class MemorySelector extends AbstractAgentModule.Running<PartnerRunningFl
private void setMemoryCandidates(LinkedHashMap<String, ActivatedMemorySlice> candidates, List<ExtractorResult.ExtractorMatchData> matches) {
for (ExtractorResult.ExtractorMatchData match : matches) {
if (match == null || match.getType() == null || match.getText() == null || match.getText().isBlank()) {
continue;
}
try {
List<ActivatedMemorySlice> recalledSlices = switch (match.getType()) {
case ExtractorResult.ExtractorMatchData.Constant.TOPIC ->

View File

@@ -18,6 +18,7 @@ import work.slhaf.partner.module.memory.selector.extractor.entity.ExtractorInput
import work.slhaf.partner.module.memory.selector.extractor.entity.ExtractorResult;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class MemoryRecallCueExtractor extends AbstractAgentModule.Sub<ExtractorInput, ExtractorResult> implements ActivateModel {
@@ -155,17 +156,40 @@ public class MemoryRecallCueExtractor extends AbstractAgentModule.Sub<ExtractorI
}
private ExtractorResult fix(ExtractorResult extractorResult) {
extractorResult.getMatches().forEach(m -> {
if (m.getType().equals(ExtractorResult.ExtractorMatchData.Constant.DATE)) {
return;
}
m.setText(memoryRuntime.fixTopicPath(m.getText()));
});
if (extractorResult.getMatches().isEmpty()) {
return extractorResult;
ExtractorResult safeResult = extractorResult == null ? new ExtractorResult() : extractorResult;
List<ExtractorResult.ExtractorMatchData> rawMatches = safeResult.getMatches();
if (rawMatches == null || rawMatches.isEmpty()) {
safeResult.setMatches(List.of());
return safeResult;
}
extractorResult.getMatches().removeIf(m -> m.getText().split("->")[0].isEmpty());
return extractorResult;
List<ExtractorResult.ExtractorMatchData> normalizedMatches = new ArrayList<>();
for (ExtractorResult.ExtractorMatchData match : rawMatches) {
if (match == null) {
continue;
}
String type = match.getType();
String text = match.getText();
if (text == null || text.isBlank()) {
continue;
}
if (ExtractorResult.ExtractorMatchData.Constant.TOPIC.equals(type)) {
text = memoryRuntime.fixTopicPath(text);
if (text.isBlank() || text.split("->")[0].isEmpty()) {
continue;
}
match.setText(text);
normalizedMatches.add(match);
continue;
}
if (ExtractorResult.ExtractorMatchData.Constant.DATE.equals(type)) {
normalizedMatches.add(match);
}
}
safeResult.setMatches(normalizedMatches);
return safeResult;
}
@Override