| 263 | } |
| 264 | |
| 265 | fn parse_event(&self, hook_type: HookType, input: &[u8]) -> AgentResult<TurnEvent> { |
| 266 | if input.is_empty() { |
| 267 | return Err(AgentError::HookInputEmpty { |
| 268 | agent: self.name().to_string(), |
| 269 | hook_type: hook_type.as_str().to_string(), |
| 270 | }); |
| 271 | } |
| 272 | |
| 273 | // Preserve raw JSON for debugging and downstream use |
| 274 | let raw_json: serde_json::Value = |
| 275 | serde_json::from_slice(input).map_err(|e| AgentError::HookParseFailed { |
| 276 | agent: self.name().to_string(), |
| 277 | hook_type: hook_type.as_str().to_string(), |
| 278 | reason: e.to_string(), |
| 279 | })?; |
| 280 | |
| 281 | match hook_type { |
| 282 | HookType::SessionStart => { |
| 283 | let parsed: SessionStartInput = |
| 284 | serde_json::from_value(raw_json.clone()).map_err(|e| { |
| 285 | AgentError::HookParseFailed { |
| 286 | agent: self.name().to_string(), |
| 287 | hook_type: hook_type.as_str().to_string(), |
| 288 | reason: e.to_string(), |
| 289 | } |
| 290 | })?; |
| 291 | |
| 292 | let mut event = |
| 293 | TurnEvent::new(Self::extract_session_id(parsed.session_id), hook_type) |
| 294 | .with_raw_json(raw_json); |
| 295 | |
| 296 | // Store model in raw_json for the orchestrator |
| 297 | if let Some(model) = parsed.model { |
| 298 | if let Some(ref mut raw) = event.raw_json { |
| 299 | if let Some(obj) = raw.as_object_mut() { |
| 300 | obj.insert("model".to_string(), serde_json::Value::String(model)); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | Ok(event) |
| 306 | } |
| 307 | |
| 308 | HookType::SessionEnd => { |
| 309 | let parsed: SessionEndInput = |
| 310 | serde_json::from_value(raw_json.clone()).map_err(|e| { |
| 311 | AgentError::HookParseFailed { |
| 312 | agent: self.name().to_string(), |
| 313 | hook_type: hook_type.as_str().to_string(), |
| 314 | reason: e.to_string(), |
| 315 | } |
| 316 | })?; |
| 317 | |
| 318 | let event = TurnEvent::new(Self::extract_session_id(parsed.session_id), hook_type) |
| 319 | .with_raw_json(raw_json); |
| 320 | |
| 321 | Ok(event) |
| 322 | } |