feat: 拆分记录上下文与内容归属并把报告改为结构化

This commit is contained in:
2026-10-09 19:12:04 +08:00
parent 9a86eadb52
commit c74f73ac3c
49 changed files with 1197 additions and 281 deletions
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.protobuf.ServiceException;
import com.guo.learningprogresstracker.common.StudyTimePolicy;
import com.guo.learningprogresstracker.dto.ReportSections;
import com.guo.learningprogresstracker.dto.StudySessionsDto;
import com.guo.learningprogresstracker.dto.response.StudySessionResponse;
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
@@ -118,7 +119,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
@Override
@Transactional
public String endedStudySession(String sessionNum, String content) throws ErrorParameterException {
public String endedStudySession(String sessionNum, ReportSections report) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> sessionNotFound(sessionNum));
@@ -144,7 +145,12 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
StudyReportsEntity studyReportsEntity = new StudyReportsEntity();
studyReportsEntity.setSessionNum(sessionNum);
studyReportsEntity.setContent(content);
// 结构化三段:摘要、达成判断、自由总结。第四部分(按归属分组的碎片)是实时聚合的视图,不落库。
ReportSections sections = (report == null ? new ReportSections(null, null, null) : report)
.orFallbackTo("本次学习未填写总结");
studyReportsEntity.setSessionSummary(sections.sessionSummary());
studyReportsEntity.setExpectationVerdict(sections.expectationVerdict());
studyReportsEntity.setFreeSummary(sections.freeSummary());
studyReportsMapper.insert(studyReportsEntity);
if (wasOngoing && studySessionsEntity.getEffectiveTime() == 0) {
return "本次有效学习时间不足" + (studyTimePolicy.getMinEffectiveTimeSeconds() / 60) + "分钟,不计入总学习时间";
@@ -166,7 +172,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
throw new ErrorParameterException("确认语输入不正确,请核对后重新输入");
}
long fragmentCount = studyReportFragmentsMapper.selectCount(Wrappers.lambdaQuery(StudyReportFragmentsEntity.class)
.eq(StudyReportFragmentsEntity::getSessionNum, sessionNum));
.eq(StudyReportFragmentsEntity::getContextNum, sessionNum));
if (fragmentCount > 0) {
log.warn("会话[{}]已产生{}条学习残片,不能误操作结束", sessionNum, fragmentCount);
throw new ErrorParameterException("这次学习已经产生了学习残片,不能按误操作结束了哦");
@@ -197,7 +203,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
if (this.exists(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))) {
return studyReportFragmentsMapper.selectList(Wrappers.lambdaQuery(StudyReportFragmentsEntity.class)
.eq(StudyReportFragmentsEntity::getSessionNum, sessionNum))
.eq(StudyReportFragmentsEntity::getContextNum, sessionNum))
.stream().map(StudyReportFragmentsEntity::getContent).collect(Collectors.toCollection(ArrayList::new));
} else {
throw sessionNotFound(sessionNum);
@@ -239,14 +245,14 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
* 不能放在读取接口里,否则一个 GET 就会阻塞并按 AI 计费。</p>
*/
@Override
public String buildReportDraft(String sessionNum) throws ErrorParameterException {
public ReportSections buildReportDraft(String sessionNum) throws ErrorParameterException {
StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> sessionNotFound(sessionNum));
ArrayList<String> fragments = getAllFragments(sessionNum);
if (fragments.isEmpty()) {
return "";
return new ReportSections(null, null, null);
}
String taskName = tasksServiceImpl.getOneOpt(
@@ -257,8 +263,23 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
.map(e -> e.getDescription())
.orElse(null);
// AI 可用时由它给出三段草稿;不可用(或失败)时降级为规则拼接:
// 会话摘要 = 碎片条数 + 首条摘要,自由总结留给用户填写,达成判断只在有预期时给一句规则结论。
return aiServiceClient.aggregateReport(taskName, fragments, expectation)
.orElseGet(() -> String.join("\n", fragments));
.orElseGet(() -> ruleBasedReportDraft(fragments, expectation));
}
/**
* 规则降级:不依赖 AI 也能给出一份可编辑的草稿。
* <p>只做能确定的事——摘要按碎片拼接、达成判断只在存在学习预期时给规则结论,
* 自由总结刻意留空由用户填写(AI 不可用时不应该替他编造卡点与下一步)。</p>
*/
private ReportSections ruleBasedReportDraft(List<String> fragments, String expectation) {
String summary = String.join("\n", fragments);
String verdict = (expectation == null || expectation.isBlank())
? null
: "本次学习记录了 " + fragments.size() + " 条知识碎片,请对照预期自行判断达成情况。";
return new ReportSections(summary, verdict, null);
}
// ============ 分页查询任务的历史残片/报告 ============
@@ -266,8 +287,9 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
@Override
public Page<StudyReportFragmentsEntity> getTaskFragments(String taskNum, int page, int size, String keyword) {
Page<StudyReportFragmentsEntity> pg = new Page<>(page, size);
// 归属-01:按内容归属取,而不是按产生于该任务的哪次会话取
var wrapper = Wrappers.lambdaQuery(StudyReportFragmentsEntity.class)
.apply("session_num in (select session_num from study_sessions where task_num = {0})", taskNum)
.eq(StudyReportFragmentsEntity::getOwnerTaskNum, taskNum)
.orderByDesc(StudyReportFragmentsEntity::getCreatedTime);
if (keyword != null && !keyword.isBlank()) {
wrapper.like(StudyReportFragmentsEntity::getContent, keyword);
@@ -282,7 +304,9 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
.apply("session_num in (select session_num from study_sessions where task_num = {0})", taskNum)
.orderByDesc(StudyReportsEntity::getCreatedTime);
if (keyword != null && !keyword.isBlank()) {
wrapper.like(StudyReportsEntity::getContent, keyword);
wrapper.and(w -> w.like(StudyReportsEntity::getFreeSummary, keyword)
.or().like(StudyReportsEntity::getSessionSummary, keyword)
.or().like(StudyReportsEntity::getExpectationVerdict, keyword));
}
Page<StudyReportsEntity> result = studyReportsMapper.selectPage(pg, wrapper);
// 填充每个报告对应会话的预期目标