refactor(ai): 重构AI命令服务实现类

- 重命名变量名 log 为 commandLog 以提高代码可读性
This commit is contained in:
Ray.Hao
2025-12-10 11:57:25 +08:00
parent 1f650fb469
commit 1b46b60b3f
2 changed files with 269 additions and 269 deletions

View File

@@ -32,17 +32,17 @@ public class AiCommandLogServiceImpl extends ServiceImpl<AiCommandLogMapper, AiC
@Override @Override
public void rollbackCommand(String logId) { public void rollbackCommand(String logId) {
AiCommandLog log = this.getById(logId); AiCommandLog commandLog = this.getById(logId);
if (log == null) { if (commandLog == null) {
throw new RuntimeException("命令记录不存在"); throw new RuntimeException("命令记录不存在");
} }
if (log.getExecuteStatus() == null || log.getExecuteStatus() != 1) { if (commandLog.getExecuteStatus() == null || commandLog.getExecuteStatus() != 1) {
throw new RuntimeException("只能撤销成功执行的命令"); throw new RuntimeException("只能撤销成功执行的命令");
} }
// TODO: 实现具体的回滚逻辑 // TODO: 实现具体的回滚逻辑
log.info("撤销命令执行: logId={}, function={}", logId, log.getFunctionName()); log.info("撤销命令执行: logId={}, function={}", logId, commandLog.getFunctionName());
throw new UnsupportedOperationException("回滚功能尚未实现"); throw new UnsupportedOperationException("回滚功能尚未实现");
} }
} }

View File

@@ -71,13 +71,13 @@ public class AiCommandServiceImpl implements AiCommandService {
String username = SecurityUtils.getUsername(); String username = SecurityUtils.getUsername();
String ipAddress = JakartaServletUtil.getClientIP(httpRequest); String ipAddress = JakartaServletUtil.getClientIP(httpRequest);
AiCommandLog log = new AiCommandLog(); AiCommandLog commandLog = new AiCommandLog();
log.setUserId(userId); commandLog.setUserId(userId);
log.setUsername(username); commandLog.setUsername(username);
log.setOriginalCommand(command); commandLog.setOriginalCommand(command);
log.setIpAddress(ipAddress); commandLog.setIpAddress(ipAddress);
log.setAiProvider("spring-ai"); commandLog.setAiProvider("spring-ai");
log.setAiModel("auto"); commandLog.setAiModel("auto");
String systemPrompt = buildSystemPrompt(); String systemPrompt = buildSystemPrompt();
String userPrompt = buildUserPrompt(request); String userPrompt = buildUserPrompt(request);
@@ -95,20 +95,20 @@ public class AiCommandServiceImpl implements AiCommandService {
ParseResult parseResult = parseAiResponse(rawContent); ParseResult parseResult = parseAiResponse(rawContent);
log.setAiProvider(StrUtil.emptyToDefault(parseResult.provider(), "spring-ai")); commandLog.setAiProvider(StrUtil.emptyToDefault(parseResult.provider(), "spring-ai"));
log.setAiModel(StrUtil.emptyToDefault(parseResult.model(), "auto")); commandLog.setAiModel(StrUtil.emptyToDefault(parseResult.model(), "auto"));
log.setParseStatus(parseResult.success() ? 1 : 0); commandLog.setParseStatus(parseResult.success() ? 1 : 0);
log.setExplanation(parseResult.explanation()); commandLog.setExplanation(parseResult.explanation());
log.setFunctionCalls(JSONUtil.toJsonStr(parseResult.functionCalls())); commandLog.setFunctionCalls(JSONUtil.toJsonStr(parseResult.functionCalls()));
log.setConfidence(parseResult.confidence() != null ? BigDecimal.valueOf(parseResult.confidence()) : null); commandLog.setConfidence(parseResult.confidence() != null ? BigDecimal.valueOf(parseResult.confidence()) : null);
log.setParseErrorMessage(parseResult.success() ? null : StrUtil.emptyToDefault(parseResult.error(), "解析失败")); commandLog.setParseErrorMessage(parseResult.success() ? null : StrUtil.emptyToDefault(parseResult.error(), "解析失败"));
long duration = System.currentTimeMillis() - startTime; long duration = System.currentTimeMillis() - startTime;
log.setParseDurationMs((int) duration); commandLog.setParseDurationMs((int) duration);
logService.save(log); logService.save(commandLog);
AiParseResponseDTO response = AiParseResponseDTO.builder() AiParseResponseDTO response = AiParseResponseDTO.builder()
.parseLogId(log.getId()) .parseLogId(commandLog.getId())
.success(parseResult.success()) .success(parseResult.success())
.functionCalls(parseResult.functionCalls()) .functionCalls(parseResult.functionCalls())
.explanation(parseResult.explanation()) .explanation(parseResult.explanation())
@@ -120,17 +120,17 @@ public class AiCommandServiceImpl implements AiCommandService {
if (!parseResult.success()) { if (!parseResult.success()) {
log.warn("❗️ AI 未能解析命令: {}", parseResult.error()); log.warn("❗️ AI 未能解析命令: {}", parseResult.error());
} else { } else {
log.info("✅ 解析成功审计记录ID: {}", log.getId()); log.info("✅ 解析成功审计记录ID: {}", commandLog.getId());
} }
return response; return response;
} catch (Exception e) { } catch (Exception e) {
long duration = System.currentTimeMillis() - startTime; long duration = System.currentTimeMillis() - startTime;
log.setParseStatus(0); commandLog.setParseStatus(0);
log.setFunctionCalls(JSONUtil.toJsonStr(Collections.emptyList())); commandLog.setFunctionCalls(JSONUtil.toJsonStr(Collections.emptyList()));
log.setParseErrorMessage(e.getMessage()); commandLog.setParseErrorMessage(e.getMessage());
log.setParseDurationMs((int) duration); commandLog.setParseDurationMs((int) duration);
logService.save(log); logService.save(commandLog);
log.error("❌ 解析命令失败: {}", e.getMessage(), e); log.error("❌ 解析命令失败: {}", e.getMessage(), e);
throw new RuntimeException("解析命令失败: " + e.getMessage(), e); throw new RuntimeException("解析命令失败: " + e.getMessage(), e);
@@ -232,52 +232,52 @@ public class AiCommandServiceImpl implements AiCommandService {
AiFunctionCallDTO functionCall = request.getFunctionCall(); AiFunctionCallDTO functionCall = request.getFunctionCall();
// 根据解析日志ID获取审计记录如果不存在则创建新记录 // 根据解析日志ID获取审计记录如果不存在则创建新记录
AiCommandLog log; AiCommandLog commandLog;
if (StrUtil.isNotBlank(request.getParseLogId())) { if (StrUtil.isNotBlank(request.getParseLogId())) {
// 更新已存在的审计记录(解析阶段已创建) // 更新已存在的审计记录(解析阶段已创建)
log = logService.getById(request.getParseLogId()); commandLog = logService.getById(request.getParseLogId());
if (log == null) { if (commandLog == null) {
throw new IllegalStateException("未找到对应的解析记录ID: " + request.getParseLogId()); throw new IllegalStateException("未找到对应的解析记录ID: " + request.getParseLogId());
} }
} else { } else {
// 如果没有解析日志ID创建新记录兼容直接执行的情况 // 如果没有解析日志ID创建新记录兼容直接执行的情况
log = new AiCommandLog(); commandLog = new AiCommandLog();
log.setUserId(userId); commandLog.setUserId(userId);
log.setUsername(username); commandLog.setUsername(username);
log.setOriginalCommand(request.getOriginalCommand()); commandLog.setOriginalCommand(request.getOriginalCommand());
log.setIpAddress(ipAddress); commandLog.setIpAddress(ipAddress);
logService.save(log); logService.save(commandLog);
} }
// 更新执行相关字段 // 更新执行相关字段
log.setFunctionName(functionCall.getName()); commandLog.setFunctionName(functionCall.getName());
log.setFunctionArguments(JSONUtil.toJsonStr(functionCall.getArguments())); commandLog.setFunctionArguments(JSONUtil.toJsonStr(functionCall.getArguments()));
log.setExecuteStatus(0); // 0-待执行 commandLog.setExecuteStatus(0); // 0-待执行
try { try {
// 🎯 执行具体的函数调用 // 🎯 执行具体的函数调用
Object result = executeFunctionCall(functionCall); Object result = executeFunctionCall(functionCall);
// 更新执行成功 // 更新执行成功
log.setExecuteStatus(1); // 1-成功 commandLog.setExecuteStatus(1); // 1-成功
log.setExecuteErrorMessage(null); commandLog.setExecuteErrorMessage(null);
// 更新审计记录 // 更新审计记录
logService.updateById(log); logService.updateById(commandLog);
log.info("✅ 命令执行成功审计记录ID: {}", log.getId()); log.info("✅ 命令执行成功审计记录ID: {}", commandLog.getId());
return result; return result;
} catch (Exception e) { } catch (Exception e) {
// 更新执行失败 // 更新执行失败
log.setExecuteStatus(-1); // -1-失败 commandLog.setExecuteStatus(-1); // -1-失败
log.setExecuteErrorMessage(e.getMessage()); commandLog.setExecuteErrorMessage(e.getMessage());
// 更新审计记录 // 更新审计记录
logService.updateById(log); logService.updateById(commandLog);
log.error("❌ 命令执行失败审计记录ID: {}", log.getId(), e); log.error("❌ 命令执行失败审计记录ID: {}", commandLog.getId(), e);
// 抛出异常,由 Controller 统一处理 // 抛出异常,由 Controller 统一处理
throw e; throw e;