Spaces:
Runtime error

File size: 1,742 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
from guardrails.tool_permissions import is_tool_allowed
from guardrails.validator import validate_tool_args, ValidationResult
from guardrails.auth_gate import check_action_authorized, GuardrailResult
from guardrails.scope import check_forbidden_actions, ScopeResult
from guardrails.rate_limiter import check_rate_limits, increment_tool_call, increment_turn, RateLimitResult
from dataclasses import dataclass
from typing import Optional


@dataclass
class PipelineResult:
    allowed: bool
    stage: str = ""
    reason: str = ""
    inject_message: Optional[str] = None
    action: Optional[str] = None


def run_guardrail_pipeline(
    call_id: str,
    tool_name: str,
    tool_args: dict,
    agent_type: str = "voice_agent",
) -> PipelineResult:
    # Stage 1 — format + type validation
    v = validate_tool_args(tool_name, tool_args)
    if not v.valid:
        return PipelineResult(allowed=False, stage="validator", reason=v.error)

    # Stage 2 — auth state gate
    a = check_action_authorized(call_id, tool_name)
    if not a.allowed:
        return PipelineResult(
            allowed=False, stage="auth",
            reason=a.reason, inject_message=a.inject_message,
        )

    # Stage 3 — scope boundary
    s = check_forbidden_actions(agent_type, tool_name)
    if not s.allowed:
        return PipelineResult(
            allowed=False, stage="scope",
            reason=s.reason, inject_message=s.inject_message,
        )

    # Stage 4 — rate limits
    r = check_rate_limits(call_id)
    if not r.allowed:
        return PipelineResult(
            allowed=False, stage="rate",
            reason=r.reason, action=r.action,
        )

    increment_tool_call(call_id)
    return PipelineResult(allowed=True)