Coverage for src/alprina_cli/utils/agent_loader.py: 0%

38 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-11-14 11:27 +0100

1""" 

2Silent Agent Loader Utility 

3 

4Loads optional agent modules silently without spamming warnings to users. 

5Logs are kept at DEBUG level for developers only. 

6""" 

7 

8import logging 

9from typing import Optional, Any 

10 

11# Use Python's standard logging instead of loguru for more control 

12logger = logging.getLogger(__name__) 

13 

14 

15def load_agent_module_silent(module_path: str, agent_name: str) -> tuple[bool, Optional[Any]]: 

16 """ 

17 Silently load an agent module. 

18  

19 Args: 

20 module_path: Full module path (e.g., 'alprina.agents') 

21 agent_name: Name of agent to get 

22  

23 Returns: 

24 Tuple of (success: bool, agent_class: Optional[Any]) 

25 """ 

26 try: 

27 from alprina.agents import get_agent_by_name 

28 logger.debug(f"✓ Agent '{agent_name}' loaded successfully") 

29 return True, get_agent_by_name 

30 except ImportError as e: 

31 # Only log at DEBUG level - invisible to normal users 

32 logger.debug(f"Agent '{agent_name}' not available: {e}") 

33 return False, None 

34 except Exception as e: 

35 logger.debug(f"Failed to load agent '{agent_name}': {e}") 

36 return False, None 

37 

38 

39def get_local_agent(agent_name: str) -> Optional[Any]: 

40 """ 

41 Load a local agent from the agents directory. 

42  

43 Args: 

44 agent_name: Name of the local agent to load 

45  

46 Returns: 

47 Agent instance or None if not found 

48 """ 

49 try: 

50 # Import the local agent wrapper 

51 if agent_name == "cicd_guardian": 

52 from alprina_cli.agents.cicd_guardian import CicdGuardianAgentWrapper 

53 return CicdGuardianAgentWrapper() 

54 elif agent_name == "web3_auditor": 

55 from alprina_cli.agents.web3_auditor import Web3AuditorAgentWrapper 

56 return Web3AuditorAgentWrapper() 

57 # Add more local agents here as they're implemented 

58 else: 

59 from alprina_cli.agents.red_teamer import RedTeamerAgentWrapper 

60 if agent_name == "red_teamer": 

61 return RedTeamerAgentWrapper() 

62 except ImportError as e: 

63 logger.debug(f"Local agent '{agent_name}' not available: {e}") 

64 return None 

65 except Exception as e: 

66 logger.debug(f"Failed to load local agent '{agent_name}': {e}") 

67 return None 

68 

69 

70# Agent availability flag 

71CAI_AVAILABLE = False 

72 

73# Try to import once at module level 

74try: 

75 from alprina.agents import get_agent_by_name 

76 CAI_AVAILABLE = True 

77 logger.debug("Alprina agents framework available") 

78except ImportError: 

79 logger.debug("Alprina agents framework not available (optional dependency)")