Spaces:
Runtime error

File size: 2,180 Bytes
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import re
from dataclasses import dataclass


@dataclass
class ValidationResult:
    valid: bool
    error: str = ""


TOOL_VALIDATORS: dict[str, dict[str, any]] = {
    "lookup_account": {
        "identifier_type": lambda v: v in {"phone", "order_id"},
        "identifier": lambda v: len(str(v)) > 5,
    },
    "send_2fa": {
        "customer_id": lambda v: len(str(v)) > 0,
    },
    "verify_2fa": {
        "customer_id": lambda v: len(str(v)) > 0,
        "code": lambda v: re.match(r"^\d{6}$", str(v)) is not None,
    },
    "update_record": {
        "customer_id": lambda v: len(str(v)) > 0,
        "field": lambda v: v in {"address", "email", "phone", "account_type"},
        "value": lambda v: 0 < len(str(v)) < 500,
    },
    "confirm_action": {
        "customer_id": lambda v: len(str(v)) > 0,
        "action_type": lambda v: v in {"refund", "cancel_order", "update_plan", "close_ticket"},
        "action_summary": lambda v: len(str(v)) > 10,
    },
    "escalate_to_human": {
        "reason": lambda v: v in {"low_sentiment", "customer_request", "auth_failure", "complex_issue"},
        "transcript_summary": lambda v: len(str(v)) > 10,
    },
    "get_order_status": {
        "customer_id": lambda v: len(str(v)) > 0,
    },
    "get_call_history": {
        "customer_id": lambda v: len(str(v)) > 0,
    },
    "crm_search": {
        "query": lambda v: len(str(v)) > 0,
        "customer_id": lambda v: len(str(v)) > 0,
    },
}


def validate_tool_args(tool_name: str, args: dict) -> ValidationResult:
    validators = TOOL_VALIDATORS.get(tool_name, {})
    for field_name, validator in validators.items():
        if field_name in args:
            try:
                if not validator(args[field_name]):
                    return ValidationResult(
                        valid=False,
                        error=f"Invalid value for '{field_name}' in tool '{tool_name}'",
                    )
            except Exception:
                return ValidationResult(
                    valid=False,
                    error=f"Validation error for '{field_name}' in tool '{tool_name}'",
                )
    return ValidationResult(valid=True)