62 lines
2.5 KiB
Java
62 lines
2.5 KiB
Java
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.GetMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
/**
|
|
* 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));
|
|
}
|
|
}
|