Coverage for src/moai_adk/templates/.claude/hooks/moai/lib/models.py: 84.62%

26 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-11-20 09:47 +0900

1""" 

2Hook model classes - Data structures for hook payloads and results. 

3 

4Provides HookPayload and HookResult classes used by hook handlers 

5to process events and return execution results to Claude Code. 

6""" 

7 

8from dataclasses import asdict, dataclass 

9from typing import Any, Dict, List, Optional 

10 

11 

12class HookPayload(dict): 

13 """ 

14 A dictionary subclass for hook event payloads. 

15 

16 Provides dict-like access to hook event data with .get() method support. 

17 Used to pass event data from Claude Code to hook handlers. 

18 

19 Example: 

20 payload = HookPayload({ 

21 "tool": "Read", 

22 "cwd": "/path/to/project", 

23 "userPrompt": "Read the file" 

24 }) 

25 

26 tool_name = payload.get("tool", "") 

27 working_dir = payload.get("cwd", ".") 

28 """ 

29 

30 def __init__(self, data: Optional[Dict[str, Any]] = None): 

31 """Initialize HookPayload with optional data dictionary.""" 

32 super().__init__(data or {}) 

33 

34 def get(self, key: str, default: Any = None) -> Any: 

35 """Get value from payload with optional default.""" 

36 return super().get(key, default) 

37 

38 def __setitem__(self, key: str, value: Any) -> None: 

39 """Set value in payload.""" 

40 super().__setitem__(key, value) 

41 

42 def update(self, other: Dict[str, Any]) -> None: 

43 """Update payload with another dictionary.""" 

44 super().update(other) 

45 

46 

47@dataclass 

48class HookResult: 

49 """ 

50 A class representing the result of a hook execution. 

51 

52 Used by hook handlers to return execution results, messages, and metadata 

53 back to Claude Code. Supports JSON serialization via to_dict() method. 

54 

55 Attributes: 

56 system_message (Optional[str]): Message to display to user 

57 continue_execution (bool): Whether to continue execution (default: True) 

58 context_files (List[str]): List of context file paths to load 

59 hook_specific_output (Optional[Dict[str, Any]]): Hook-specific data 

60 block_execution (bool): Whether to block execution (default: False) 

61 

62 Example: 

63 # Simple result with just a message 

64 return HookResult(system_message="Operation completed") 

65 

66 # Result with context files 

67 return HookResult( 

68 system_message="Loaded 3 context files", 

69 context_files=["README.md", "config.json"] 

70 ) 

71 

72 # Result that stops execution 

73 return HookResult( 

74 system_message="Dangerous operation blocked", 

75 continue_execution=False, 

76 block_execution=True 

77 ) 

78 """ 

79 

80 system_message: Optional[str] = None 

81 continue_execution: bool = True 

82 context_files: List[str] = None 

83 hook_specific_output: Optional[Dict[str, Any]] = None 

84 block_execution: bool = False 

85 

86 def __post_init__(self): 

87 """Post-initialization to set default values.""" 

88 if self.context_files is None: 

89 self.context_files = [] 

90 if self.hook_specific_output is None: 

91 self.hook_specific_output = {} 

92 

93 def to_dict(self) -> Dict[str, Any]: 

94 """ 

95 Convert HookResult to a dictionary for JSON serialization. 

96 

97 Returns: 

98 Dict[str, Any]: Dictionary representation of the HookResult 

99 """ 

100 result = asdict(self) 

101 # Remove empty/None values to keep output clean 

102 return {k: v for k, v in result.items() if v or v is False or v == 0}