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
public void rollbackCommand(String logId) {
AiCommandLog log = this.getById(logId);
if (log == null) {
AiCommandLog commandLog = this.getById(logId);
if (commandLog == null) {
throw new RuntimeException("命令记录不存在");
}
if (log.getExecuteStatus() == null || log.getExecuteStatus() != 1) {
if (commandLog.getExecuteStatus() == null || commandLog.getExecuteStatus() != 1) {
throw new RuntimeException("只能撤销成功执行的命令");
}
// TODO: 实现具体的回滚逻辑
log.info("撤销命令执行: logId={}, function={}", logId, log.getFunctionName());
log.info("撤销命令执行: logId={}, function={}", logId, commandLog.getFunctionName());
throw new UnsupportedOperationException("回滚功能尚未实现");
}
}

View File

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