refactor: 将客服后端重构为宿主可嵌入模块
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/ai"
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/pkg/errorsx"
|
||||
svc "code.tczkiot.com/wlw/ai-agent/internal/services"
|
||||
@@ -38,7 +40,7 @@ func NewAgentApplicationService() *AgentApplicationService {
|
||||
}
|
||||
|
||||
func (s *AgentApplicationService) Run(ctx context.Context, input ApplicationRunInput) (*RunResult, error) {
|
||||
req, err := s.loadRequest(input)
|
||||
req, err := s.loadRequestWithContext(ctx, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -53,7 +55,7 @@ func (s *AgentApplicationService) RunPrepared(ctx context.Context, req RunInput)
|
||||
}
|
||||
|
||||
func (s *AgentApplicationService) Resume(ctx context.Context, input ApplicationResumeInput) (*RunResult, error) {
|
||||
req, err := s.loadRequest(input.ApplicationRunInput)
|
||||
req, err := s.loadRequestWithContext(ctx, input.ApplicationRunInput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -80,6 +82,10 @@ func (s *AgentApplicationService) ResumePrepared(ctx context.Context, req Resume
|
||||
}
|
||||
|
||||
func (s *AgentApplicationService) loadRequest(input ApplicationRunInput) (RunInput, error) {
|
||||
return s.loadRequestWithContext(context.Background(), input)
|
||||
}
|
||||
|
||||
func (s *AgentApplicationService) loadRequestWithContext(ctx context.Context, input ApplicationRunInput) (RunInput, error) {
|
||||
if input.ConversationID <= 0 || input.MessageID <= 0 || input.AIAgentID <= 0 {
|
||||
return RunInput{}, errorsx.InvalidParam("conversation, message and agent are required")
|
||||
}
|
||||
@@ -98,9 +104,30 @@ func (s *AgentApplicationService) loadRequest(input ApplicationRunInput) (RunInp
|
||||
if conversation.AIAgentID > 0 && conversation.AIAgentID != agent.ID {
|
||||
return RunInput{}, errorsx.InvalidParam("agent does not belong to conversation")
|
||||
}
|
||||
config := svc.AIConfigService.Get(agent.AIConfigID)
|
||||
if config == nil || config.Status != enums.StatusOk {
|
||||
return RunInput{}, errorsx.InvalidParam("ai config is unavailable")
|
||||
config, err := ResolveRuntimeAIConfigForMessage(ctx, agent.AIConfigID, message.MessageType)
|
||||
if err != nil {
|
||||
return RunInput{}, err
|
||||
}
|
||||
return RunInput{Conversation: *conversation, UserMessage: *message, AIAgent: *agent, AIConfig: *config}, nil
|
||||
}
|
||||
|
||||
// ResolveRuntimeAIConfig is the single model-source boundary for every Agent
|
||||
// runtime entry point, including prepared online replies and offline tests.
|
||||
func ResolveRuntimeAIConfig(ctx context.Context, customConfigID int64) (*models.AIConfig, error) {
|
||||
config, err := ai.ResolveAIConfig(ctx, enums.AIModelTypeLLM, customConfigID)
|
||||
if err != nil {
|
||||
return nil, errorsx.InvalidParam(err.Error())
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func ResolveRuntimeAIConfigForMessage(ctx context.Context, customConfigID int64, messageType enums.IMMessageType) (*models.AIConfig, error) {
|
||||
if messageType != enums.IMMessageTypeImage {
|
||||
return ResolveRuntimeAIConfig(ctx, customConfigID)
|
||||
}
|
||||
config, err := ai.ResolveVisionAIConfig(ctx, customConfigID)
|
||||
if err != nil {
|
||||
return nil, errorsx.InvalidParam(err.Error())
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user