Parse raw JSON input from a Claude Code hook into a [`TurnEvent`]. This is the core parsing logic extracted from the `AgentHook::parse_event` implementation. Each hook type has a different JSON schema.
(
hook: &super::ClaudeCodeHook,
hook_type: HookType,
input: &[u8],
)
| 95 | /// This is the core parsing logic extracted from the `AgentHook::parse_event` |
| 96 | /// implementation. Each hook type has a different JSON schema. |
| 97 | pub(super) fn parse_hook_event( |
| 98 | hook: &super::ClaudeCodeHook, |
| 99 | hook_type: HookType, |
| 100 | input: &[u8], |
| 101 | ) -> AgentResult<TurnEvent> { |
| 102 | let agent_name = hook.name(); |
| 103 | if input.is_empty() { |
| 104 | return Err(AgentError::HookInputEmpty { |
| 105 | agent: agent_name.to_string(), |
| 106 | hook_type: hook_type.as_str().to_string(), |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | // Preserve raw JSON for debugging |
| 111 | let raw_json: serde_json::Value = |
| 112 | serde_json::from_slice(input).map_err(|e| AgentError::HookParseFailed { |
| 113 | agent: agent_name.to_string(), |
| 114 | hook_type: hook_type.as_str().to_string(), |
| 115 | reason: e.to_string(), |
| 116 | })?; |
| 117 | |
| 118 | match hook_type { |
| 119 | HookType::SessionStart => parse_session_start(agent_name, hook_type, raw_json), |
| 120 | HookType::SessionEnd | HookType::TurnEnd => { |
| 121 | parse_session_info(agent_name, hook_type, raw_json) |
| 122 | } |
| 123 | HookType::TurnStart => parse_user_prompt(agent_name, hook_type, raw_json), |
| 124 | HookType::PreToolUse => parse_pre_tool(agent_name, hook_type, raw_json), |
| 125 | HookType::PostToolUse => parse_post_tool(agent_name, hook_type, raw_json), |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// Extract the session_id from parsed input, returning a default if missing. |
| 130 | fn extract_session_id(session_id: Option<String>) -> String { |
no test coverage detected