18c9354095
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package runtime
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
aitooling "code.tczkiot.com/wlw/ai-agent/internal/ai/tooling"
|
|
"time"
|
|
|
|
"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"
|
|
|
|
"github.com/mlogclub/simple/sqls"
|
|
)
|
|
|
|
type replyCommitService struct{}
|
|
|
|
type replyCommitInput struct {
|
|
Conversation models.Conversation
|
|
Message models.Message
|
|
AIAgent models.AIAgent
|
|
ReplyText string
|
|
ClientPrefix string
|
|
IncrementRound bool
|
|
}
|
|
|
|
func newReplyCommitService() *replyCommitService {
|
|
return &replyCommitService{}
|
|
}
|
|
|
|
func (s *replyCommitService) SendAIReply(input replyCommitInput) (*models.Message, error) {
|
|
replyText, err := aitooling.NormalizeCustomerReply(input.ReplyText)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
replyMessage, err := svc.MessageService.SendAIMessageWithRequestID(
|
|
input.Conversation.ID,
|
|
input.AIAgent.ID,
|
|
fmt.Sprintf("%s_%d", strings.TrimSpace(input.ClientPrefix), input.Message.ID),
|
|
enums.IMMessageTypeText,
|
|
replyText,
|
|
"",
|
|
s.buildAIPrincipal(input.AIAgent),
|
|
input.Message.RequestID,
|
|
)
|
|
if err != nil || !input.IncrementRound {
|
|
return replyMessage, err
|
|
}
|
|
if err := s.IncrementAIReplyRounds(input.Conversation.ID, input.Conversation.AIReplyRounds+1, input.AIAgent.Name); err != nil {
|
|
return nil, err
|
|
}
|
|
return replyMessage, err
|
|
}
|
|
|
|
func (s *replyCommitService) CommitAIReply(input replyCommitInput) (*models.Message, error) {
|
|
input.IncrementRound = true
|
|
return s.SendAIReply(input)
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|