2026-04-13 17:17:13 +08:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
2026-07-25 12:04:06 +08:00
|
|
|
|
2026-08-21 00:41:07 +08:00
|
|
|
aitooling "code.tczkiot.com/wlw/ai-agent/internal/ai/tooling"
|
2026-04-13 17:17:13 +08:00
|
|
|
"time"
|
|
|
|
|
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/dto"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/repositories"
|
|
|
|
|
svc "code.tczkiot.com/wlw/ai-agent/internal/services"
|
2026-04-13 17:17:13 +08:00
|
|
|
|
|
|
|
|
"github.com/mlogclub/simple/sqls"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type replyCommitService struct{}
|
|
|
|
|
|
2026-04-19 11:19:51 +08:00
|
|
|
type replyCommitInput struct {
|
|
|
|
|
Conversation models.Conversation
|
|
|
|
|
Message models.Message
|
|
|
|
|
AIAgent models.AIAgent
|
|
|
|
|
ReplyText string
|
|
|
|
|
ClientPrefix string
|
|
|
|
|
IncrementRound bool
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 17:17:13 +08:00
|
|
|
func newReplyCommitService() *replyCommitService {
|
|
|
|
|
return &replyCommitService{}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 11:19:51 +08:00
|
|
|
func (s *replyCommitService) SendAIReply(input replyCommitInput) (*models.Message, error) {
|
2026-07-25 12:04:06 +08:00
|
|
|
replyText, err := aitooling.NormalizeCustomerReply(input.ReplyText)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2026-04-13 17:17:13 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
replyMessage, err := svc.MessageService.SendAIMessageWithRequestID(
|
2026-04-19 11:19:51 +08:00
|
|
|
input.Conversation.ID,
|
|
|
|
|
input.AIAgent.ID,
|
|
|
|
|
fmt.Sprintf("%s_%d", strings.TrimSpace(input.ClientPrefix), input.Message.ID),
|
2026-04-13 17:17:13 +08:00
|
|
|
enums.IMMessageTypeText,
|
|
|
|
|
replyText,
|
|
|
|
|
"",
|
2026-04-19 11:19:51 +08:00
|
|
|
s.buildAIPrincipal(input.AIAgent),
|
2026-05-27 22:18:43 +08:00
|
|
|
input.Message.RequestID,
|
2026-04-13 17:17:13 +08:00
|
|
|
)
|
2026-04-19 11:19:51 +08:00
|
|
|
if err != nil || !input.IncrementRound {
|
|
|
|
|
return replyMessage, err
|
2026-04-13 19:50:48 +08:00
|
|
|
}
|
2026-04-19 11:19:51 +08:00
|
|
|
if err := s.IncrementAIReplyRounds(input.Conversation.ID, input.Conversation.AIReplyRounds+1, input.AIAgent.Name); err != nil {
|
2026-04-13 19:50:48 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-04-19 11:19:51 +08:00
|
|
|
return replyMessage, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *replyCommitService) CommitAIReply(input replyCommitInput) (*models.Message, error) {
|
|
|
|
|
input.IncrementRound = true
|
|
|
|
|
return s.SendAIReply(input)
|
2026-04-13 19:50:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 17:17:13 +08:00
|
|
|
func (s *replyCommitService) IncrementAIReplyRounds(conversationID int64, nextRounds int, aiAgentName string) error {
|
|
|
|
|
return repositories.ConversationRepository.Updates(sqls.DB(), conversationID, map[string]any{
|
|
|
|
|
"ai_reply_rounds": nextRounds,
|
|
|
|
|
"update_user_id": 0,
|
|
|
|
|
"update_user_name": strings.TrimSpace(aiAgentName),
|
|
|
|
|
"updated_at": time.Now(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *replyCommitService) buildAIPrincipal(aiAgent models.AIAgent) *dto.AuthPrincipal {
|
|
|
|
|
username := "AI"
|
|
|
|
|
if strings.TrimSpace(aiAgent.Name) != "" {
|
|
|
|
|
username = aiAgent.Name
|
|
|
|
|
}
|
|
|
|
|
return &dto.AuthPrincipal{
|
|
|
|
|
UserID: 0,
|
|
|
|
|
Username: username,
|
|
|
|
|
Nickname: username,
|
|
|
|
|
}
|
|
|
|
|
}
|