feat: 新增标签导图存储与生成

This commit is contained in:
2026-10-10 15:28:06 +08:00
parent d282f1b10a
commit 6a30022af5
29 changed files with 1916 additions and 106 deletions
@@ -9,7 +9,6 @@ import com.guo.learningprogresstracker.entity.*;
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
import com.guo.learningprogresstracker.exception.OperationFailedException;
import com.guo.learningprogresstracker.mapper.*;
import com.guo.learningprogresstracker.service.MindMapAiClient;
import com.guo.learningprogresstracker.service.StandardMindMapService;
import com.guo.learningprogresstracker.utils.CompareResult;
import com.guo.learningprogresstracker.utils.MindMapNode;
@@ -39,7 +38,7 @@ public class StandardMindMapServiceImpl implements StandardMindMapService {
private final TaskApplicationMapper taskApplicationMapper;
private final StudyReportFragmentsMapper studyReportFragmentsMapper;
private final List<MindMapAiClient> aiClients;
private final MindMapOutlineGenerator outlineGenerator;
private final ObjectMapper objectMapper;
private final AiServiceClient aiServiceClient;
@@ -136,9 +135,8 @@ public class StandardMindMapServiceImpl implements StandardMindMapService {
standard = doGenerate(taskNum);
}
// 2. 解析标准树和用户回忆树
// 2. 解析标准树(用户回忆大纲在对比入口里解析,两条复习范围共用同一段逻辑)
MindMapNode standardRoot = MindMapTreeTool.fromJson(standard.getContent(), objectMapper);
MindMapNode recallRoot = MindMapTreeTool.parseOutline(recallOutline);
// 2b. 若指定了起点节点路径,提取该子树作为对比基准
MindMapNode compareRoot = standardRoot;
@@ -150,28 +148,8 @@ public class StandardMindMapServiceImpl implements StandardMindMapService {
}
}
// 3. 执行对比(优先 AI 语义对比,失败时降级为内置算法)
CompareResult result;
if (aiServiceClient.isConfigured()) {
String taskName = tasksMapper.selectOne(
Wrappers.<TaskEntity>lambdaQuery().eq(TaskEntity::getTaskNum, taskNum).last("LIMIT 1"))
.getTaskName();
var optJson = aiServiceClient.compareRecall(
taskName,
MindMapTreeTool.toFullOutline(compareRoot),
recallOutline
);
if (optJson.isPresent()) {
result = buildCompareResultFromAI(optJson.get(), compareRoot);
log.info("AI 回忆对比: taskNum={}, recallRatio={}, evaluation={}",
taskNum, result.getRecallRatio(),
result.getEvaluation() != null ? result.getEvaluation().substring(0, Math.min(50, result.getEvaluation().length())) : "");
} else {
result = compareTrees(compareRoot, recallRoot);
}
} else {
result = compareTrees(compareRoot, recallRoot);
}
// 3. 执行对比(优先 AI 语义对比,失败时降级为内置算法)——与标签导图共用同一套对比口径
CompareResult result = compareWithRecall(resolveScopeName(taskNum), compareRoot, recallOutline);
// 4. 序列化对比结果
String resultJson;
@@ -239,6 +217,36 @@ public class StandardMindMapServiceImpl implements StandardMindMapService {
return result;
}
@Override
public CompareResult compareWithRecall(String scopeName, MindMapNode standardRoot, String recallOutline) {
MindMapNode recallRoot = MindMapTreeTool.parseOutline(recallOutline);
// 优先 AI 语义对比,AI 不可用或返回失败时降级为内置文本相似度匹配
if (aiServiceClient.isConfigured()) {
var optJson = aiServiceClient.compareRecall(
scopeName,
MindMapTreeTool.toFullOutline(standardRoot),
recallOutline);
if (optJson.isPresent()) {
CompareResult result = buildCompareResultFromAI(optJson.get(), standardRoot);
log.info("AI 回忆对比: scope={}, recallRatio={}, evaluation={}",
scopeName, result.getRecallRatio(),
result.getEvaluation() != null
? result.getEvaluation().substring(0, Math.min(50, result.getEvaluation().length()))
: "");
return result;
}
}
return compareTrees(standardRoot, recallRoot);
}
/** 对比口径里的范围名:默认用任务名(导图根节点也是它) */
private String resolveScopeName(String taskNum) {
TaskEntity task = tasksMapper.selectOne(Wrappers.<TaskEntity>lambdaQuery()
.eq(TaskEntity::getTaskNum, taskNum).last("LIMIT 1"));
return task == null || !StringUtils.hasText(task.getTaskName()) ? taskNum : task.getTaskName();
}
// ============ 对比算法核心 ============
/**
@@ -428,34 +436,10 @@ public class StandardMindMapServiceImpl implements StandardMindMapService {
throw new OperationFailedException("这个任务还没开始学习哦,学习后产生知识碎片后再来吧");
}
// 优先选 AI 客户端(非 BUILTIN),其次内置生成器
MindMapAiClient client = aiClients.stream()
.filter(MindMapAiClient::isAvailable)
.min(Comparator.comparing(c -> "BUILTIN".equals(c.generatorName()) ? 1 : 0))
.orElse(null);
// 「碎片 → 缩进大纲」生成链路(AI 优先、内置规则降级)与标签导图共用同一套口径
MindMapOutlineGenerator.GeneratedOutline generated = outlineGenerator.generate(task, fragments, applications);
if (client == null) {
log.warn("任务[{}]没有可用的思维导图生成器", taskNum);
throw new OperationFailedException("思维导图暂时生成不了,请稍后再试");
}
Optional<MindMapNode> optRoot = client.generate(task, fragments, applications, null);
// AI 生成失败时尝试降级到内置生成器
if (optRoot.isEmpty() && !"BUILTIN".equals(client.generatorName())) {
log.info("AI 思维导图生成失败,降级到内置生成器");
MindMapAiClient fallback = aiClients.stream()
.filter(c -> "BUILTIN".equals(c.generatorName()) && c.isAvailable())
.findFirst().orElse(null);
if (fallback != null) {
optRoot = fallback.generate(task, fragments, applications, null);
}
}
if (optRoot.isEmpty()) {
log.warn("任务[{}]思维导图生成失败,已尝试全部生成器", taskNum);
throw new OperationFailedException("思维导图生成失败了,请稍后再试");
}
MindMapNode root = optRoot.get();
MindMapNode root = generated.root();
String contentJson = MindMapTreeTool.toJson(root, objectMapper);
String outline = MindMapTreeTool.toFullOutline(root);
int nodeCount = MindMapTreeTool.countNodes(root);
@@ -472,7 +456,7 @@ public class StandardMindMapServiceImpl implements StandardMindMapService {
entity.setContent(contentJson);
entity.setOutline(outline);
entity.setSummary("共 " + nodeCount + " 个节点,最大层级 " + depth);
entity.setGenerator(client.generatorName());
entity.setGenerator(generated.generatorName());
entity.setGeneratorVersion("1.0");
entity.setSourceReportCount(0);
entity.setSourceFragmentCount(fragments.size());