| 207 | } |
| 208 | |
| 209 | fn parse_event(&self, hook_type: HookType, input: &[u8]) -> AgentResult<TurnEvent> { |
| 210 | if input.is_empty() { |
| 211 | return Err(AgentError::HookInputEmpty { |
| 212 | agent: self.name().to_string(), |
| 213 | hook_type: hook_type.as_str().to_string(), |
| 214 | }); |
| 215 | } |
| 216 | |
| 217 | // Preserve raw JSON for debugging and downstream use |
| 218 | let raw_json: serde_json::Value = |
| 219 | serde_json::from_slice(input).map_err(|e| AgentError::HookParseFailed { |
| 220 | agent: self.name().to_string(), |
| 221 | hook_type: hook_type.as_str().to_string(), |
| 222 | reason: e.to_string(), |
| 223 | })?; |
| 224 | |
| 225 | match hook_type { |
| 226 | HookType::SessionStart => { |
| 227 | let parsed: SessionStartInput = |
| 228 | serde_json::from_value(raw_json.clone()).map_err(|e| { |
| 229 | AgentError::HookParseFailed { |
| 230 | agent: self.name().to_string(), |
| 231 | hook_type: hook_type.as_str().to_string(), |
| 232 | reason: e.to_string(), |
| 233 | } |
| 234 | })?; |
| 235 | |
| 236 | let mut event = |
| 237 | TurnEvent::new(Self::extract_session_id(parsed.session_id), hook_type) |
| 238 | .with_raw_json(raw_json); |
| 239 | |
| 240 | if let Some(model) = parsed.model { |
| 241 | if let Some(ref mut raw) = event.raw_json { |
| 242 | if let Some(obj) = raw.as_object_mut() { |
| 243 | obj.insert("model".to_string(), serde_json::Value::String(model)); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | Ok(event) |
| 249 | } |
| 250 | |
| 251 | HookType::SessionEnd => { |
| 252 | let parsed: SessionEndInput = |
| 253 | serde_json::from_value(raw_json.clone()).map_err(|e| { |
| 254 | AgentError::HookParseFailed { |
| 255 | agent: self.name().to_string(), |
| 256 | hook_type: hook_type.as_str().to_string(), |
| 257 | reason: e.to_string(), |
| 258 | } |
| 259 | })?; |
| 260 | |
| 261 | let event = TurnEvent::new(Self::extract_session_id(parsed.session_id), hook_type) |
| 262 | .with_raw_json(raw_json); |
| 263 | |
| 264 | Ok(event) |
| 265 | } |
| 266 | |