refactor: 将客服后端重构为宿主可嵌入模块
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
aitooling "code.tczkiot.com/wlw/ai-agent/internal/ai/tooling"
|
||||
"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/dto/request"
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/pkg/toolx"
|
||||
)
|
||||
|
||||
@@ -41,7 +40,7 @@ func newBusinessToolExecutor(registry *aitooling.Registry) *businessToolExecutor
|
||||
return &businessToolExecutor{registry: registry}
|
||||
}
|
||||
|
||||
func (e *businessToolExecutor) Execute(_ context.Context, input BusinessToolInput) (*BusinessToolResult, error) {
|
||||
func (e *businessToolExecutor) Execute(ctx context.Context, input BusinessToolInput) (*BusinessToolResult, error) {
|
||||
toolCode := toolx.NormalizeToolCodeAlias(strings.TrimSpace(input.ToolCode))
|
||||
definition, err := e.registry.Resolve(toolCode)
|
||||
if err != nil {
|
||||
@@ -63,16 +62,27 @@ func (e *businessToolExecutor) Execute(_ context.Context, input BusinessToolInpu
|
||||
if claim.Completed {
|
||||
return &BusinessToolResult{Definition: definition, ResultData: claim.Item.ResultData, Reused: true}, nil
|
||||
}
|
||||
if claim.UnknownOutcome {
|
||||
return nil, fmt.Errorf("business tool outcome requires reconciliation; refusing replay: %s", definition.Code)
|
||||
}
|
||||
if !claim.Acquired {
|
||||
return nil, fmt.Errorf("business tool invocation is already running: %s", definition.Code)
|
||||
}
|
||||
|
||||
resultData, err := e.execute(definition.Code, input)
|
||||
if err != nil {
|
||||
_ = AgentToolInvocationService.Fail(claim.Item, err)
|
||||
// Built-in write executors may have committed before returning an error.
|
||||
// Unless the host explicitly marks the failure as pre-side-effect, never
|
||||
// replay the same idempotency key automatically.
|
||||
if businessActionFailureIsRetryable(ctx, err) {
|
||||
_ = AgentToolInvocationService.FailRetryable(claim.Item, err)
|
||||
} else {
|
||||
_ = AgentToolInvocationService.MarkUnknownOutcome(claim.Item, err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if err := AgentToolInvocationService.Complete(claim.Item, resultData); err != nil {
|
||||
_ = AgentToolInvocationService.MarkUnknownOutcome(claim.Item, err)
|
||||
return nil, err
|
||||
}
|
||||
return &BusinessToolResult{Definition: definition, ResultData: resultData}, nil
|
||||
@@ -80,24 +90,12 @@ func (e *businessToolExecutor) Execute(_ context.Context, input BusinessToolInpu
|
||||
|
||||
func (e *businessToolExecutor) execute(toolCode string, input BusinessToolInput) (string, error) {
|
||||
switch toolCode {
|
||||
case toolx.GraphCreateTicketConfirm.Code:
|
||||
item, err := TicketService.CreateFromConversation(request.CreateTicketFromConversationRequest{
|
||||
ConversationID: input.Conversation.ID,
|
||||
Title: businessToolString(input.Arguments["title"]),
|
||||
Description: businessToolString(input.Arguments["description"]),
|
||||
TagIDs: businessToolInt64Slice(input.Arguments["tagIds"]),
|
||||
CurrentAssigneeID: businessToolInt64(input.Arguments["assigneeId"]),
|
||||
}, businessToolPrincipal(input.AIAgent))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return businessToolJSON(map[string]any{"ticketId": item.ID, "ticketNo": item.TicketNo, "created": true})
|
||||
case toolx.GraphHandoffConversation.Code:
|
||||
result, err := ConversationHumanDispatchService.HandoffByAIWithRequestID(input.Conversation.ID, input.AIAgent, businessToolString(input.Arguments["reason"]), input.IdempotencyKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return businessToolJSON(map[string]any{"decision": result.Decision, "teamId": result.TeamID, "assigneeId": result.AssigneeID, "message": result.Message})
|
||||
return businessToolJSON(map[string]any{"decision": result.Decision, "team_id": result.TeamID, "assignee_id": result.AssigneeID, "message": result.Message})
|
||||
default:
|
||||
return "", fmt.Errorf("business tool is not executable: %s", toolCode)
|
||||
}
|
||||
@@ -108,36 +106,6 @@ func businessToolString(value any) string {
|
||||
return strings.TrimSpace(text)
|
||||
}
|
||||
|
||||
func businessToolInt64(value any) int64 {
|
||||
switch typed := value.(type) {
|
||||
case int64:
|
||||
return typed
|
||||
case int:
|
||||
return int64(typed)
|
||||
case float64:
|
||||
return int64(typed)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func businessToolInt64Slice(value any) []int64 {
|
||||
switch typed := value.(type) {
|
||||
case []int64:
|
||||
return typed
|
||||
case []any:
|
||||
ret := make([]int64, 0, len(typed))
|
||||
for _, item := range typed {
|
||||
if id := businessToolInt64(item); id > 0 {
|
||||
ret = append(ret, id)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func businessToolPrincipal(agent models.AIAgent) *dto.AuthPrincipal {
|
||||
name := strings.TrimSpace(agent.Name)
|
||||
if name == "" {
|
||||
|
||||
Reference in New Issue
Block a user