Junjie
8 天以前 9a8018c3fbc94f99d5d184c8cb1ef23d7366cea0
src/main/java/com/zy/ai/controller/AutoTuneConsoleController.java
@@ -6,11 +6,15 @@
import com.zy.ai.domain.autotune.AutoTuneApplyResult;
import com.zy.ai.entity.AiAutoTuneChange;
import com.zy.ai.entity.AiAutoTuneJob;
import com.zy.ai.entity.AiAutoTuneMcpCall;
import com.zy.ai.enums.AiPromptScene;
import com.zy.ai.service.AiAutoTuneChangeService;
import com.zy.ai.service.AiAutoTuneJobService;
import com.zy.ai.service.AiAutoTuneMcpCallService;
import com.zy.ai.service.AutoTuneApplyService;
import com.zy.ai.service.AutoTuneCoordinatorService;
import com.zy.ai.service.AutoTuneSnapshotService;
import com.zy.ai.utils.AutoTuneWriteBehaviorUtils;
import com.zy.common.web.BaseController;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
@@ -37,6 +41,7 @@
    private final AutoTuneApplyService autoTuneApplyService;
    private final AiAutoTuneJobService aiAutoTuneJobService;
    private final AiAutoTuneChangeService aiAutoTuneChangeService;
    private final AiAutoTuneMcpCallService aiAutoTuneMcpCallService;
    @GetMapping("/snapshot/auth")
    @ManagerAuth(memo = "查看AI自动调参快照")
@@ -49,6 +54,7 @@
    public R jobs(@RequestParam(value = "limit", required = false) Integer limit) {
        int safeLimit = normalizeLimit(limit);
        List<AiAutoTuneJob> jobs = aiAutoTuneJobService.list(new QueryWrapper<AiAutoTuneJob>()
                .eq("prompt_scene_code", AiPromptScene.AUTO_TUNE_DISPATCH.getCode())
                .orderByDesc("start_time")
                .orderByDesc("id")
                .last("limit " + safeLimit));
@@ -112,30 +118,116 @@
        item.put("promptTokens", job.getPromptTokens());
        item.put("completionTokens", job.getCompletionTokens());
        item.put("totalTokens", job.getTotalTokens());
        item.put("changes", listChangeSummaries(job.getId()));
        List<AiAutoTuneMcpCall> mcpCalls = listMcpCalls(job.getId());
        List<Map<String, Object>> mcpCallSummaries = toMcpCallSummaries(mcpCalls);
        List<Map<String, Object>> changeSummaries = listChangeSummaries(job, mcpCalls);
        AutoTuneWriteBehaviorUtils.addWriteBehavior(item,
                AutoTuneWriteBehaviorUtils.resolveJobWriteBehavior(job, mcpCallSummaries, changeSummaries));
        item.put("mcpCallCount", mcpCalls.size());
        item.put("mcpCalls", mcpCallSummaries);
        item.put("changes", changeSummaries);
        return item;
    }
    private List<Map<String, Object>> listChangeSummaries(Long jobId) {
    private List<AiAutoTuneMcpCall> listMcpCalls(Long agentJobId) {
        if (agentJobId == null) {
            return new ArrayList<>();
        }
        List<AiAutoTuneMcpCall> mcpCalls = aiAutoTuneMcpCallService.list(new QueryWrapper<AiAutoTuneMcpCall>()
                .eq("agent_job_id", agentJobId)
                .orderByAsc("call_seq")
                .orderByAsc("id"));
        return mcpCalls == null ? new ArrayList<>() : mcpCalls;
    }
    private List<Map<String, Object>> toMcpCallSummaries(List<AiAutoTuneMcpCall> mcpCalls) {
        List<Map<String, Object>> result = new ArrayList<>();
        if (jobId == null) {
        if (mcpCalls == null || mcpCalls.isEmpty()) {
            return result;
        }
        for (AiAutoTuneMcpCall mcpCall : mcpCalls) {
            result.add(toMcpCallSummary(mcpCall));
        }
        return result;
    }
    private Map<String, Object> toMcpCallSummary(AiAutoTuneMcpCall mcpCall) {
        LinkedHashMap<String, Object> item = new LinkedHashMap<>();
        item.put("id", mcpCall.getId());
        item.put("callSeq", mcpCall.getCallSeq());
        item.put("toolName", mcpCall.getToolName());
        item.put("status", mcpCall.getStatus());
        item.put("dryRun", toBoolean(mcpCall.getDryRun()));
        item.put("applyJobId", mcpCall.getApplyJobId());
        item.put("successCount", mcpCall.getSuccessCount());
        item.put("rejectCount", mcpCall.getRejectCount());
        item.put("durationMs", mcpCall.getDurationMs());
        item.put("requestJson", mcpCall.getRequestJson());
        item.put("responseJson", mcpCall.getResponseJson());
        item.put("errorMessage", mcpCall.getErrorMessage());
        item.put("createTime", mcpCall.getCreateTime());
        AutoTuneWriteBehaviorUtils.addWriteBehavior(item,
                AutoTuneWriteBehaviorUtils.resolveMcpWriteBehavior(mcpCall));
        return item;
    }
    private Boolean toBoolean(Integer value) {
        if (value == null) {
            return null;
        }
        return value == 1;
    }
    private List<Map<String, Object>> listChangeSummaries(AiAutoTuneJob job, List<AiAutoTuneMcpCall> mcpCalls) {
        List<Map<String, Object>> result = new ArrayList<>();
        Map<Long, String> ownerTriggerTypes = collectChangeOwnerTriggerTypes(job, mcpCalls);
        List<Long> applyJobIds = new ArrayList<>(ownerTriggerTypes.keySet());
        if (applyJobIds.isEmpty()) {
            return result;
        }
        List<AiAutoTuneChange> changes = aiAutoTuneChangeService.list(new QueryWrapper<AiAutoTuneChange>()
                .eq("job_id", jobId)
                .in("job_id", applyJobIds)
                .orderByAsc("job_id")
                .orderByAsc("id"));
        if (changes == null || changes.isEmpty()) {
            return result;
        }
        for (AiAutoTuneChange change : changes) {
            result.add(toChangeSummary(change));
            result.add(toChangeSummary(change, ownerTriggerTypes.get(change.getJobId())));
        }
        return result;
    }
    private Map<String, Object> toChangeSummary(AiAutoTuneChange change) {
    private Map<Long, String> collectChangeOwnerTriggerTypes(AiAutoTuneJob job, List<AiAutoTuneMcpCall> mcpCalls) {
        LinkedHashMap<Long, String> result = new LinkedHashMap<>();
        if (job != null && job.getId() != null) {
            result.put(job.getId(), job.getTriggerType());
        }
        if (mcpCalls == null || mcpCalls.isEmpty()) {
            return result;
        }
        for (AiAutoTuneMcpCall mcpCall : mcpCalls) {
            Long applyJobId = mcpCall.getApplyJobId();
            if (applyJobId == null || result.containsKey(applyJobId)) {
                continue;
            }
            result.put(applyJobId, resolveMcpApplyJobTriggerType(mcpCall));
        }
        return result;
    }
    private String resolveMcpApplyJobTriggerType(AiAutoTuneMcpCall mcpCall) {
        if (mcpCall == null || mcpCall.getToolName() == null) {
            return null;
        }
        String toolName = mcpCall.getToolName().toLowerCase();
        return toolName.contains("revert_last_auto_tune_job") || toolName.contains("rollback") ? "rollback" : null;
    }
    private Map<String, Object> toChangeSummary(AiAutoTuneChange change, String ownerTriggerType) {
        LinkedHashMap<String, Object> item = new LinkedHashMap<>();
        item.put("id", change.getId());
        item.put("jobId", change.getJobId());
        item.put("targetType", change.getTargetType());
        item.put("targetId", change.getTargetId());
        item.put("targetKey", change.getTargetKey());
@@ -146,6 +238,8 @@
        item.put("rejectReason", change.getRejectReason());
        item.put("cooldownExpireTime", change.getCooldownExpireTime());
        item.put("createTime", change.getCreateTime());
        AutoTuneWriteBehaviorUtils.addWriteBehavior(item,
                AutoTuneWriteBehaviorUtils.resolveChangeWriteBehavior(change, ownerTriggerType));
        return item;
    }
}