feat(knowledge): 会话级去重 + 知识域地图注入 Planner 解决 ISS-001 重复检索

- RetrievedDocTracker: sessionId → Set<filePath> 会话级去重,LookupKnowledgeTool Step 5 过滤已检索文档
- KnowledgeDomainService: 域级聚合,LLM 生成 when_to_retrieve,构建 knowledge map YAML
- DocumentFieldEnricher: 上传时 LLM 补全 covers + whenToRetrieve(含同域文档排除上下文)
- KnowledgeDomain entity + V009 迁移: 域级元数据持久化,避免重启重复 LLM 调用
- ChatService: 注入 knowledge map 到 Planner prompt,会话结束时清理去重状态
- KnowledgeIndexService: 手写 JSON 解析替换为 Jackson ObjectMapper,启动时补建缺失域记录
- chat-planner-prompt: 新增知识库检索规则(按域 when_to_retrieve 判断,每域最多一次检索)
- doc-field-enricher-prompt / domain-summary-prompt: 外部化 LLM 提示词
This commit is contained in:
zhuyongxin
2026-07-01 10:47:46 +08:00
parent 2a796da490
commit bb44140901
17 changed files with 642 additions and 106 deletions
@@ -31,6 +31,9 @@ public class LookupKnowledgeTool {
@Autowired
private ToolInvocationRepository toolInvocationRepository;
@Autowired
private RetrievedDocTracker retrievedDocTracker;
/**
* 查询知识库文档
*
@@ -98,6 +101,23 @@ public class LookupKnowledgeTool {
// Step 4: 组装结果
LookupResult result = buildResult(l0Matches, l1Results, highConfidence);
// Step 5: session 级去重过滤
String sessionId = SessionContextHolder.getSessionId();
if (sessionId != null && result.isFound()) {
String docKey = extractDocKey(result);
if (docKey != null && retrievedDocTracker.isAlreadyRetrieved(sessionId, docKey)) {
log.info("[去重] 文档已在本会话中检索过,跳过: {}", docKey);
saveToolInvocation(query, l0Matches, l1Results, highConfidence, startTime, result);
return LookupResult.builder()
.found(false)
.message("文档已在本会话中检索过,无需重复召回: " + docKey)
.build();
}
if (docKey != null) {
retrievedDocTracker.markRetrieved(sessionId, docKey);
}
}
// 记录结构化结果摘要(替代原始 MD 内容预览)
long totalTime = System.currentTimeMillis() - startTime;
log.info("----------------------------------------");
@@ -430,4 +450,14 @@ public class LookupKnowledgeTool {
return "(无有效内容)";
}
private String extractDocKey(LookupResult result) {
if (result.getPrimary() != null && result.getPrimary().getSource() != null) {
return result.getPrimary().getSource();
}
if (result.getSupplement() != null && result.getSupplement().getSource() != null) {
return result.getSupplement().getSource();
}
return null;
}
}