ai-robot-channel/src/main/java/com/wecom/robot/service/TransferService.java

93 lines
2.7 KiB
Java

package com.wecom.robot.service;
import com.wecom.robot.config.TransferConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
@RequiredArgsConstructor
public class TransferService {
private final TransferConfig transferConfig;
public boolean shouldTransferToManual(String message, double confidence, int messageCount, LocalDateTime sessionCreatedAt) {
if (!transferConfig.isEnabled()) {
log.info("转人工功能已关闭,不转人工");
return false;
}
if (containsKeywords(message)) {
log.info("触发转人工: 关键词匹配");
return true;
}
/*if (confidence < transferConfig.getConfidenceThreshold()) {
log.info("触发转人工: AI置信度过低 confidence={}", confidence);
return true;
}
if (messageCount >= transferConfig.getMaxMessageRounds()) {
log.info("触发转人工: 消息轮次过多 count={}", messageCount);
return true;
}
if (sessionCreatedAt != null) {
long duration = System.currentTimeMillis() -
sessionCreatedAt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
if (duration >= transferConfig.getMaxSessionDuration()) {
log.info("触发转人工: 会话时长超限 duration={}ms", duration);
return true;
}
}*/
return false;
}
public String getTransferReason(String message, double confidence, int messageCount) {
List<String> keywords = transferConfig.getKeywords();
if (keywords != null) {
for (String keyword : keywords) {
if (message != null && message.contains(keyword)) {
return "关键词触发: " + keyword;
}
}
}
if (confidence < transferConfig.getConfidenceThreshold()) {
return "AI置信度过低: " + confidence;
}
if (messageCount >= transferConfig.getMaxMessageRounds()) {
return "消息轮次过多: " + messageCount;
}
return "其他原因";
}
private boolean containsKeywords(String message) {
if (message == null) {
return false;
}
List<String> keywords = transferConfig.getKeywords();
if (keywords == null) {
return false;
}
for (String keyword : keywords) {
if (message.contains(keyword)) {
return true;
}
}
return false;
}
}