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
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-25 07:19 +0800
1"""Abstract base class for input guards."""
3from __future__ import annotations
5from abc import ABC, abstractmethod
6from typing import TYPE_CHECKING, Any
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
14class AbstractInputGuard(ABC):
15 """Base class for all input content guards.
17 Subclasses implement :meth:`check` to evaluate a piece of input
18 content and return a :class:`~lexigram.ai.guard.pipeline.result.GuardCheckResult`.
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 """
26 def __init__(self, action: str = "block") -> None:
27 """Initialise the guard with a default action.
29 Args:
30 action: Action taken when the guard triggers.
31 """
32 self._action = action
34 @property
35 def name(self) -> str:
36 """GuardProtocol identifier derived from the class name."""
37 return type(self).__name__
39 @property
40 def action(self) -> str:
41 """Configured action for this guard."""
42 return self._action
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.
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.).
59 Returns:
60 Result indicating the outcome.
61 """
64__all__ = ["AbstractInputGuard"]