feat: AI 调用改为异步任务并支持结果重复获取
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package com.guo.learningprogresstracker.controller;
|
||||
|
||||
import com.guo.learningprogresstracker.dto.AiTaskResponse;
|
||||
import com.guo.learningprogresstracker.dto.request.AiTaskSubmitRequest;
|
||||
import com.guo.learningprogresstracker.entity.CommonResult;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
||||
import com.guo.learningprogresstracker.service.AiTaskService;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* AI 异步任务控制层(AI-02)。
|
||||
*
|
||||
* <p>提交与查询分离:提交接口秒级返回任务标识,前端随后轮询查询接口拿结果。
|
||||
* 这样刷新或离开页面都不会丢结果,AI 已消耗的 token 也不会白费。</p>
|
||||
*/
|
||||
@RequestMapping("/ai/tasks")
|
||||
@RestController
|
||||
@Validated
|
||||
@RequiredArgsConstructor
|
||||
public class AiTaskController {
|
||||
|
||||
private final AiTaskService aiTaskService;
|
||||
|
||||
/**
|
||||
* 提交 AI 任务。
|
||||
* <p>同一目标已有进行中任务时直接返回该任务(AI-04:对外是任务状态,而不是可能为空的实体)。</p>
|
||||
*/
|
||||
@PostMapping
|
||||
public CommonResult<AiTaskResponse> submit(@Valid @RequestBody AiTaskSubmitRequest request)
|
||||
throws ErrorParameterException, NotFindEntitiesException {
|
||||
return CommonResult.success(aiTaskService.submit(request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询任务状态与结果;结果可重复获取。
|
||||
*/
|
||||
@GetMapping("/{taskNum}")
|
||||
public CommonResult<AiTaskResponse> getTask(@PathVariable String taskNum) throws NotFindEntitiesException {
|
||||
return CommonResult.success(aiTaskService.getByTaskNum(taskNum));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询某目标最近的 AI 任务,用于离开页面再回来时恢复生成状态。
|
||||
*
|
||||
* @return 无任务时 data 为 null
|
||||
*/
|
||||
@GetMapping("/latest")
|
||||
public CommonResult<AiTaskResponse> getLatestTask(@RequestParam String type,
|
||||
@RequestParam String targetNum) {
|
||||
return CommonResult.success(aiTaskService.findLatest(type, targetNum));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user