feat: AI 调用改为异步任务并支持结果重复获取

This commit is contained in:
2026-10-09 15:07:15 +08:00
parent 0b695f54bf
commit cf380c5f09
37 changed files with 1911 additions and 189 deletions
@@ -0,0 +1,54 @@
package com.guo.learningprogresstracker.dto;
import com.guo.learningprogresstracker.entity.AiTaskEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.Map;
/**
* AI 任务对外视图(AI-02)。
* <p>提交与查询都返回它:前端只依赖 {@code status} 与 {@code result},
* 不需要在请求线程里等 AI(也就不会出现「AI 成功了但客户端已超时」)。</p>
*/
@Data
public class AiTaskResponse {
@Schema(description = "AI 任务编号")
private String taskNum;
@Schema(description = "任务类型:RECALL_COMPARE / MIND_MAP / REPORT_DRAFT")
private String type;
@Schema(description = "目标编号:学习任务编号或学习会话编号")
private String targetNum;
@Schema(description = "状态:PENDING / RUNNING / SUCCEEDED / FAILED")
private String status;
@Schema(description = "失败原因(用户可见文案)")
private String errorMessage;
@Schema(description = "提交时间")
private LocalDateTime createdTime;
@Schema(description = "结束时间")
private LocalDateTime finishedTime;
@Schema(description = "成功结果,随任务类型不同;未完成时为 null")
private Map<String, Object> result;
public static AiTaskResponse from(AiTaskEntity entity, Map<String, Object> result) {
AiTaskResponse response = new AiTaskResponse();
response.setTaskNum(entity.getTaskNum());
response.setType(entity.getAiType());
response.setTargetNum(entity.getTargetNum());
response.setStatus(entity.getStatus());
response.setErrorMessage(entity.getErrorMessage());
response.setCreatedTime(entity.getCreatedTime());
response.setFinishedTime(entity.getFinishedTime());
response.setResult(result);
return response;
}
}
@@ -0,0 +1,30 @@
package com.guo.learningprogresstracker.dto;
/**
* 一次回忆对比的结果(AI-02)。
*
* <p>对比在 AI 任务执行器里完成,因此不再把「标准导图实体」直接返回给提交接口;
* 这个结果写进 AI 任务的 {@code result_payload},由前端在轮询到成功后再取详情。</p>
*
* @param standardMapId 对比所用的标准导图 ID
* @param recallRecordId 本次生成的回忆记录 ID
* @param recallRatio 覆盖率(0-1)
* @param matchedCount 命中节点数
* @param missedCount 遗漏节点数
* @param extraCount 额外节点数
*/
public record RecallCompareOutcome(Integer standardMapId, Integer recallRecordId, Double recallRatio,
Integer matchedCount, Integer missedCount, Integer extraCount) {
/** 转成 AI 任务结果载荷 */
public java.util.Map<String, Object> toPayload() {
java.util.Map<String, Object> payload = new java.util.LinkedHashMap<>();
payload.put("standardMapId", standardMapId);
payload.put("recallRecordId", recallRecordId);
payload.put("recallRatio", recallRatio);
payload.put("matchedCount", matchedCount);
payload.put("missedCount", missedCount);
payload.put("extraCount", extraCount);
return payload;
}
}
@@ -0,0 +1,43 @@
package com.guo.learningprogresstracker.dto.request;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import java.util.Map;
/**
* 提交 AI 任务请求(AI-02)。
*/
@Data
public class AiTaskSubmitRequest {
@NotBlank(message = "请先指定要处理的类型")
private String type;
@NotBlank(message = "请先指定要处理的对象")
private String targetNum;
/** 类型相关的参数;无需参数时可为空 */
private Map<String, Object> params;
/** 阅读友好度:null 表示不关心 */
public String paramAsString(String key) {
Object value = params == null ? null : params.get(key);
return value == null ? null : String.valueOf(value);
}
public Integer paramAsInteger(String key) {
Object value = params == null ? null : params.get(key);
if (value instanceof Number number) {
return number.intValue();
}
if (value instanceof String text && !text.isBlank()) {
try {
return Integer.valueOf(text.trim());
} catch (NumberFormatException e) {
return null;
}
}
return null;
}
}