修复(AI客服): 退款操作统一转人工处理
隐藏并拦截退款类业务动作,兼容旧确认流程,确保所有客户类型的退款请求进入人工支持,并补充回归测试。
This commit is contained in:
@@ -13,6 +13,15 @@ import (
|
||||
|
||||
var BusinessActionToolService = &businessActionToolService{}
|
||||
|
||||
const RefundHumanSupportMessage = "退款需要由人工客服核实并处理,AI 无法代您申请或办理退款。请回复“人工客服”联系人工处理。"
|
||||
|
||||
// BusinessActionRequiresHuman also covers the legacy mall application tool,
|
||||
// whose refund_only and return_refund actions do not mention refund in its code.
|
||||
func BusinessActionRequiresHuman(code string) bool {
|
||||
code = strings.ToLower(strings.TrimSpace(code))
|
||||
return code == "business/mall_apply_after_sale" || strings.Contains(code, "refund")
|
||||
}
|
||||
|
||||
type businessActionToolService struct {
|
||||
mu sync.RWMutex
|
||||
tools map[string]contract.BusinessActionTool
|
||||
@@ -55,7 +64,7 @@ func (s *businessActionToolService) ListForCustomerType(customerType string) []c
|
||||
defer s.mu.RUnlock()
|
||||
ret := make([]contract.BusinessActionTool, 0, len(s.tools))
|
||||
for _, tool := range s.tools {
|
||||
if businessActionToolSupportsCustomerType(tool, customerType) {
|
||||
if !BusinessActionRequiresHuman(tool.Code) && businessActionToolSupportsCustomerType(tool, customerType) {
|
||||
ret = append(ret, tool)
|
||||
}
|
||||
}
|
||||
@@ -67,7 +76,7 @@ func (s *businessActionToolService) ResolveForCustomerType(code, customerType st
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
tool, ok := s.tools[strings.TrimSpace(code)]
|
||||
if !ok || !businessActionToolSupportsCustomerType(tool, customerType) {
|
||||
if !ok || BusinessActionRequiresHuman(tool.Code) || !businessActionToolSupportsCustomerType(tool, customerType) {
|
||||
return contract.BusinessActionTool{}, false
|
||||
}
|
||||
return tool, true
|
||||
@@ -77,14 +86,23 @@ func (s *businessActionToolService) Resolve(code string) (contract.BusinessActio
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
tool, ok := s.tools[strings.TrimSpace(code)]
|
||||
if ok && BusinessActionRequiresHuman(tool.Code) {
|
||||
return contract.BusinessActionTool{}, false
|
||||
}
|
||||
return tool, ok
|
||||
}
|
||||
|
||||
func (s *businessActionToolService) Preview(ctx context.Context, tool contract.BusinessActionTool, businessContext contract.BusinessReadContext, arguments map[string]any) (string, error) {
|
||||
if BusinessActionRequiresHuman(tool.Code) {
|
||||
return "", contract.NewBusinessActionError(RefundHumanSupportMessage, nil)
|
||||
}
|
||||
return tool.Preview(ctx, businessContext, arguments)
|
||||
}
|
||||
|
||||
func (s *businessActionToolService) Execute(ctx context.Context, conversationID, aiAgentID int64, idempotencyKey string, tool contract.BusinessActionTool, businessContext contract.BusinessReadContext, arguments map[string]any) (*contract.BusinessActionResult, bool, error) {
|
||||
if BusinessActionRequiresHuman(tool.Code) {
|
||||
return nil, false, contract.NewBusinessActionError(RefundHumanSupportMessage, nil)
|
||||
}
|
||||
businessContext.CheckPointID = strings.TrimSpace(idempotencyKey)
|
||||
if tool.AuthorizeConfirmation != nil {
|
||||
if err := tool.AuthorizeConfirmation(ctx, businessContext, arguments, businessContext.CheckPointID); err != nil {
|
||||
|
||||
@@ -240,3 +240,55 @@ func TestBusinessActionToolRetriesExplicitPreSideEffectFailure(t *testing.T) {
|
||||
t.Fatalf("retry result=%#v reused=%t executions=%d err=%v", result, reused, executions, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefundActionsAreHiddenAndBlockedBeforeHostCallbacks(t *testing.T) {
|
||||
t.Cleanup(func() { _ = SetBusinessActionTools(nil) })
|
||||
for _, code := range []string{"business/mall_apply_after_sale", "business/card_package_refund", "business/device_balance_refund", "business/mall_deposit_refund"} {
|
||||
t.Run(code, func(t *testing.T) {
|
||||
tool := contract.BusinessActionTool{
|
||||
Code: code, Description: "refund",
|
||||
Preview: func(context.Context, contract.BusinessReadContext, map[string]any) (string, error) {
|
||||
t.Fatal("refund preview must not reach the host")
|
||||
return "", nil
|
||||
},
|
||||
AuthorizeConfirmation: func(context.Context, contract.BusinessReadContext, map[string]any, string) error {
|
||||
t.Fatal("refund must be blocked before authorizing or claiming an invocation")
|
||||
return nil
|
||||
},
|
||||
Execute: func(context.Context, contract.BusinessReadContext, map[string]any) (*contract.BusinessActionResult, error) {
|
||||
t.Fatal("refund must not execute, even with an existing confirmation")
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
if err := SetBusinessActionTools([]contract.BusinessActionTool{tool}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, customerType := range []string{"card", "device", "mall_user", ""} {
|
||||
if got := BusinessActionToolService.ListForCustomerType(customerType); len(got) != 0 {
|
||||
t.Fatalf("refund exposed in catalog: %#v", got)
|
||||
}
|
||||
if _, ok := BusinessActionToolService.ResolveForCustomerType(code, customerType); ok {
|
||||
t.Fatal("refund resolved for customer")
|
||||
}
|
||||
}
|
||||
if _, ok := BusinessActionToolService.Resolve(code); ok {
|
||||
t.Fatal("refund resolved for tool definitions")
|
||||
}
|
||||
_, err := BusinessActionToolService.Preview(context.Background(), tool, contract.BusinessReadContext{}, nil)
|
||||
assertRefundHumanSupportError(t, err)
|
||||
result, reused, err := BusinessActionToolService.Execute(context.Background(), 1, 2, "legacy-refund-confirmation", tool, contract.BusinessReadContext{}, nil)
|
||||
assertRefundHumanSupportError(t, err)
|
||||
if result != nil || reused {
|
||||
t.Fatalf("refund returned an execution result: %#v reused=%v", result, reused)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertRefundHumanSupportError(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
var publicErr *contract.BusinessActionError
|
||||
if !errors.As(err, &publicErr) || publicErr.Message != RefundHumanSupportMessage {
|
||||
t.Fatalf("expected human support guidance, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user