refactor: implement off-hours handoff logic and enhance related tests

This commit is contained in:
mlogclub
2026-05-08 23:26:25 +08:00
parent 6f4c026320
commit 664e418115
6 changed files with 254 additions and 4 deletions
@@ -45,6 +45,25 @@ func newConversationHumanDispatchService() *conversationHumanDispatchService {
return &conversationHumanDispatchService{}
}
func (s *conversationHumanDispatchService) TryOffHoursHandoffByAI(conversationID int64, aiAgent models.AIAgent, reason string) (bool, error) {
conversation := ConversationService.Get(conversationID)
if conversation == nil {
return false, errorsx.InvalidParam("会话不存在")
}
teamIDs := orderedPositiveIDs(aiAgent.TeamIDs)
activeTeamIDs := ConversationDispatchService.findActiveScheduleTeamIDs(teamIDs, time.Now())
if len(activeTeamIDs) > 0 {
return false, nil
}
if err := s.createEvent(conversationID, enums.IMEventTypeTransfer, enums.IMSenderTypeAI, aiAgent.ID, "转人工失败:非服务时间", strings.TrimSpace(reason)); err != nil {
return true, err
}
if err := s.sendAIText(conversationID, aiAgent.ID, HandoffOffHoursMessage); err != nil {
return true, err
}
return true, nil
}
func (s *conversationHumanDispatchService) HandoffByAI(conversationID int64, aiAgent models.AIAgent, reason string) (*HandoffDecisionResult, error) {
conversation := ConversationService.Get(conversationID)
if conversation == nil {
@@ -53,10 +72,7 @@ func (s *conversationHumanDispatchService) HandoffByAI(conversationID int64, aiA
teamIDs := orderedPositiveIDs(aiAgent.TeamIDs)
activeTeamIDs := ConversationDispatchService.findActiveScheduleTeamIDs(teamIDs, time.Now())
if len(activeTeamIDs) == 0 {
if err := s.createEvent(conversationID, enums.IMEventTypeTransfer, enums.IMSenderTypeAI, aiAgent.ID, "转人工失败:非服务时间", strings.TrimSpace(reason)); err != nil {
return nil, err
}
if err := s.sendAIText(conversationID, aiAgent.ID, HandoffOffHoursMessage); err != nil {
if _, err := s.TryOffHoursHandoffByAI(conversationID, aiAgent, reason); err != nil {
return nil, err
}
return &HandoffDecisionResult{Decision: HandoffDecisionOffHours, Message: HandoffOffHoursMessage}, nil
+14
View File
@@ -360,6 +360,20 @@ func (s *conversationService) HandoffByAI(conversationID int64, aiAgent models.A
return err
}
func (s *conversationService) TryOffHoursHandoffByAI(conversationID int64, aiAgent models.AIAgent, reason string) (bool, error) {
if conversationID <= 0 {
return false, errorsx.InvalidParam("会话不存在")
}
handled, err := ConversationHumanDispatchService.TryOffHoursHandoffByAI(conversationID, aiAgent, reason)
if err != nil {
slog.Warn("off-hours ai handoff failed",
"conversation_id", conversationID,
"ai_agent_id", aiAgent.ID,
"error", err)
}
return handled, err
}
func (s *conversationService) CloseConversation(conversationID int64, closeReason string, operator *dto.AuthPrincipal) error {
if operator == nil {
return errorsx.Unauthorized("未登录或登录已过期")