feat: implement Handoff Graph Tool for managing AI to human transitions

This commit is contained in:
mlogclub
2026-04-10 16:00:10 +08:00
parent d953625a2b
commit 15afa10c7d
9 changed files with 307 additions and 24 deletions
+39
View File
@@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"log/slog"
"cs-agent/internal/models"
"cs-agent/internal/pkg/constants"
@@ -284,6 +285,44 @@ func (s *conversationService) TransferConversation(conversationID, toUserID int6
return nil
}
func (s *conversationService) HandoffByAI(conversationID int64, aiAgent *models.AIAgent, reason string) error {
if conversationID <= 0 {
return errorsx.InvalidParam("会话不存在")
}
if aiAgent == nil {
return errorsx.InvalidParam("AI Agent 不存在")
}
now := time.Now()
if err := sqls.WithTransaction(func(ctx *sqls.TxContext) error {
conversation := repositories.ConversationRepository.Get(ctx.Tx, conversationID)
if conversation == nil {
return errorsx.InvalidParam("会话不存在")
}
if err := repositories.ConversationRepository.Updates(ctx.Tx, conversationID, map[string]any{
"handoff_at": now,
"handoff_reason": strings.TrimSpace(reason),
"status": enums.IMConversationStatusPending,
"current_team_id": 0,
"current_assignee_id": 0,
"update_user_id": 0,
"update_user_name": aiAgent.Name,
"updated_at": now,
}); err != nil {
return err
}
return ConversationEventLogService.CreateEvent(ctx, conversationID, enums.IMEventTypeTransfer, enums.IMSenderTypeAI, aiAgent.ID, "AI转人工", strings.TrimSpace(reason))
}); err != nil {
return err
}
if _, err := ConversationDispatchService.DispatchConversation(conversationID); err != nil {
slog.Warn("auto dispatch conversation after ai handoff failed",
"conversation_id", conversationID,
"ai_agent_id", aiAgent.ID,
"error", err)
}
return nil
}
func (s *conversationService) CloseConversation(conversationID int64, closeReason string, operator *dto.AuthPrincipal) error {
if operator == nil {
return errorsx.Unauthorized("未登录或登录已过期")
+11 -2
View File
@@ -33,7 +33,7 @@ type MCPToolCatalogItem struct {
func (s *toolCatalogService) ListMCPTools(ctx context.Context) ([]MCPToolCatalogItem, error) {
cfg := config.Current()
ret := make([]MCPToolCatalogItem, 0, 2)
ret := make([]MCPToolCatalogItem, 0, 3)
ret = append(ret, MCPToolCatalogItem{
ToolCode: toolx.GraphCreateTicketConfirmToolCode,
ServerCode: toolx.GraphToolCatalogServerCode,
@@ -43,6 +43,15 @@ func (s *toolCatalogService) ListMCPTools(ctx context.Context) ([]MCPToolCatalog
Title: toolx.GraphCreateTicketConfirmToolTitle,
Description: toolx.GraphCreateTicketConfirmToolDescription,
})
ret = append(ret, MCPToolCatalogItem{
ToolCode: toolx.GraphHandoffConversationToolCode,
ServerCode: toolx.GraphToolCatalogServerCode,
ToolName: toolx.GraphHandoffConversationToolName,
SourceType: toolx.GraphToolCatalogServerCode,
AutoInjected: false,
Title: toolx.GraphHandoffConversationToolTitle,
Description: toolx.GraphHandoffConversationToolDescription,
})
if !cfg.MCP.Enabled {
return ret, nil
}
@@ -96,7 +105,7 @@ func (s *toolCatalogService) ValidateToolCode(toolCode string) error {
return errorsx.InvalidParam("toolCode不能为空")
}
switch toolCode {
case toolx.BuiltinToolSearchToolCode, toolx.BuiltinCreateTicketConfirmToolCode, toolx.GraphCreateTicketConfirmToolCode:
case toolx.BuiltinToolSearchToolCode, toolx.BuiltinCreateTicketConfirmToolCode, toolx.GraphCreateTicketConfirmToolCode, toolx.GraphHandoffConversationToolCode:
return nil
}
serverCode, toolName := toolx.SplitMCPToolCode(toolCode)