| | |
| | | import com.zy.ai.domain.autotune.AutoTuneJobStatus; |
| | | import com.zy.ai.domain.autotune.AutoTuneTriggerType; |
| | | import com.zy.ai.entity.AiAutoTuneJob; |
| | | import com.zy.ai.enums.AiPromptScene; |
| | | import com.zy.ai.service.AiAutoTuneJobService; |
| | | import com.zy.ai.service.AutoTuneAgentService; |
| | | import com.zy.ai.service.AutoTuneCoordinatorService; |
| | |
| | | private static final int MAX_INTERVAL_MINUTES = 60; |
| | | private static final int RUNNING_LOCK_SECONDS = 20 * 60; |
| | | private static final long SYSTEM_USER_ID = 9527L; |
| | | private static final int SUMMARY_MAX_LENGTH = 512; |
| | | private static final int ERROR_MESSAGE_MAX_LENGTH = 1024; |
| | | private static final List<Long> FINAL_WRK_STS_LIST = Arrays.asList( |
| | | WrkStsType.COMPLETE_INBOUND.sts, |
| | | WrkStsType.SETTLE_INBOUND.sts, |
| | |
| | | } |
| | | |
| | | AutoTuneAgentService.AutoTuneAgentResult agentResult = null; |
| | | Date startTime = new Date(); |
| | | try { |
| | | if (writeGuard && intervalMinutes != null) { |
| | | safeMarkLastTriggerGuard(intervalMinutes); |
| | | } |
| | | agentResult = autoTuneAgentService.runAutoTune(triggerType); |
| | | safeWriteOperateLog(agentResult); |
| | | safeWriteAgentRunAudit(triggerType, startTime, agentResult); |
| | | return AutoTuneCoordinatorResult.triggered(agentResult); |
| | | } catch (Exception exception) { |
| | | log.error("Auto tune coordinator failed to run agent", exception); |
| | | agentResult = failedAgentResult(triggerType, exception); |
| | | safeWriteOperateLog(agentResult); |
| | | safeWriteAgentRunAudit(triggerType, startTime, agentResult); |
| | | return AutoTuneCoordinatorResult.triggered(agentResult); |
| | | } finally { |
| | | redisUtil.compareAndDelete(lockKey, lockToken); |
| | |
| | | return result; |
| | | } |
| | | |
| | | private void safeWriteAgentRunAudit(String triggerType, |
| | | Date startTime, |
| | | AutoTuneAgentService.AutoTuneAgentResult agentResult) { |
| | | try { |
| | | writeAgentRunAudit(triggerType, startTime, agentResult); |
| | | } catch (Exception exception) { |
| | | log.warn("Auto tune coordinator failed to write agent run audit", exception); |
| | | } |
| | | } |
| | | |
| | | private void writeAgentRunAudit(String triggerType, |
| | | Date startTime, |
| | | AutoTuneAgentService.AutoTuneAgentResult agentResult) { |
| | | if (agentResult == null) { |
| | | return; |
| | | } |
| | | Date finishTime = new Date(); |
| | | Integer intervalMinutes = resolveIntervalMinutes(); |
| | | AiAutoTuneJob job = new AiAutoTuneJob(); |
| | | job.setTriggerType(AutoTuneTriggerType.normalize(triggerType)); |
| | | job.setStatus(Boolean.TRUE.equals(agentResult.getSuccess()) |
| | | ? AutoTuneJobStatus.SUCCESS.getCode() |
| | | : AutoTuneJobStatus.FAILED.getCode()); |
| | | job.setStartTime(startTime == null ? finishTime : startTime); |
| | | job.setFinishTime(finishTime); |
| | | job.setHasActiveTasks(resolveHasActiveTasksForAudit()); |
| | | job.setPromptSceneCode(AiPromptScene.AUTO_TUNE_DISPATCH.getCode()); |
| | | job.setSummary(limitText(agentResult.getSummary(), SUMMARY_MAX_LENGTH)); |
| | | job.setIntervalBefore(intervalMinutes); |
| | | job.setIntervalAfter(intervalMinutes); |
| | | job.setSuccessCount(0); |
| | | job.setRejectCount(0); |
| | | if (!Boolean.TRUE.equals(agentResult.getSuccess())) { |
| | | job.setErrorMessage(limitText(agentResult.getSummary(), ERROR_MESSAGE_MAX_LENGTH)); |
| | | } |
| | | job.setLlmCallCount(agentResult.getLlmCallCount() == null ? 0 : agentResult.getLlmCallCount()); |
| | | job.setPromptTokens(toSafeInteger(agentResult.getPromptTokens())); |
| | | job.setCompletionTokens(toSafeInteger(agentResult.getCompletionTokens())); |
| | | job.setTotalTokens(toSafeInteger(agentResult.getTotalTokens())); |
| | | job.setCreateTime(job.getStartTime()); |
| | | if (!aiAutoTuneJobService.save(job)) { |
| | | log.warn("Auto tune coordinator failed to save agent run audit"); |
| | | } |
| | | } |
| | | |
| | | private int resolveHasActiveTasksForAudit() { |
| | | try { |
| | | return hasActiveTasks() ? 1 : 0; |
| | | } catch (RuntimeException exception) { |
| | | log.warn("Auto tune coordinator failed to query active tasks for audit", exception); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | private Integer toSafeInteger(Long value) { |
| | | if (value == null) { |
| | | return 0; |
| | | } |
| | | if (value > Integer.MAX_VALUE) { |
| | | return Integer.MAX_VALUE; |
| | | } |
| | | if (value < Integer.MIN_VALUE) { |
| | | return Integer.MIN_VALUE; |
| | | } |
| | | return value.intValue(); |
| | | } |
| | | |
| | | private String limitText(String value, int maxLength) { |
| | | if (value == null || value.length() <= maxLength) { |
| | | return value; |
| | | } |
| | | return value.substring(0, maxLength); |
| | | } |
| | | |
| | | private void safeWriteOperateLog(AutoTuneAgentService.AutoTuneAgentResult agentResult) { |
| | | try { |
| | | writeOperateLog(agentResult); |