Coverage for /home/admin/Documents/AI/applications/lexigram-dev/lexigram/experimental/ai/lexigram-ai-guard/src/lexigram/ai/guard/input/base.py: 87%

15 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 07:19 +0800

1"""Abstract base class for input guards.""" 

2 

3from __future__ import annotations 

4 

5from abc import ABC, abstractmethod 

6from typing import TYPE_CHECKING, Any 

7 

8if TYPE_CHECKING: 

9 from lexigram.contracts.ai.exceptions import GuardError 

10 from lexigram.contracts.ai.guards import GuardResultProtocol 

11 from lexigram.result import Result 

12 

13 

14class AbstractInputGuard(ABC): 

15 """Base class for all input content guards. 

16 

17 Subclasses implement :meth:`check` to evaluate a piece of input 

18 content and return a :class:`~lexigram.ai.guard.pipeline.result.GuardCheckResult`. 

19 

20 Args: 

21 action: Default action to take when this guard triggers 

22 (``"block"``, ``"warn"``, or ``"redact"``). 

23 Not all guards support all actions. 

24 """ 

25 

26 def __init__(self, action: str = "block") -> None: 

27 """Initialise the guard with a default action. 

28 

29 Args: 

30 action: Action taken when the guard triggers. 

31 """ 

32 self._action = action 

33 

34 @property 

35 def name(self) -> str: 

36 """GuardProtocol identifier derived from the class name.""" 

37 return type(self).__name__ 

38 

39 @property 

40 def action(self) -> str: 

41 """Configured action for this guard.""" 

42 return self._action 

43 

44 @abstractmethod 

45 async def check( 

46 self, 

47 content: str, 

48 *, 

49 messages: list[Any] | None = None, 

50 metadata: dict[str, Any] | None = None, 

51 ) -> Result[GuardResultProtocol, GuardError]: 

52 """Evaluate the content and return a result. 

53 

54 Args: 

55 content: Raw text content to evaluate. 

56 messages: Optional structured message list for context. 

57 metadata: Optional metadata (user_id, model, etc.). 

58 

59 Returns: 

60 Result indicating the outcome. 

61 """ 

62 

63 

64__all__ = ["AbstractInputGuard"]