| 198 | } |
| 199 | |
| 200 | fn parse_event(&self, hook_type: HookType, input: &[u8]) -> AgentResult<TurnEvent> { |
| 201 | let raw_json = self.parse_json(hook_type, input)?; |
| 202 | let timestamp = TimestampFormat::Rfc3339.parse(self.name(), hook_type, &raw_json)?; |
| 203 | let parsed = self.parse_common(hook_type, canonicalize_fields(&raw_json))?; |
| 204 | |
| 205 | let mut raw_json = with_xai_provider(raw_json); |
| 206 | if hook_type == HookType::TurnEnd { |
| 207 | raw_json = normalize_stop_raw(raw_json, parsed.reason.as_deref()); |
| 208 | } |
| 209 | if hook_type == HookType::PostToolUse { |
| 210 | raw_json = normalize_tool_raw(raw_json, parsed.tool_result.as_ref()); |
| 211 | } |
| 212 | |
| 213 | // Promote model/provider into raw_json when present (orchestrator reads them). |
| 214 | if let Some(model) = parsed.model.as_ref() { |
| 215 | insert_if_missing(&mut raw_json, "model", Value::String(model.clone())); |
| 216 | } |
| 217 | if let Some(provider) = parsed.provider.as_ref() { |
| 218 | insert_if_missing(&mut raw_json, "provider", Value::String(provider.clone())); |
| 219 | } |
| 220 | |
| 221 | let mut event = TurnEvent::new(Self::extract_session_id(parsed.session_id), hook_type) |
| 222 | .with_raw_json(raw_json); |
| 223 | |
| 224 | if let Some(path) = parsed.transcript_path.filter(|p| !p.is_empty()) { |
| 225 | event = event.with_transcript_path(path); |
| 226 | } |
| 227 | |
| 228 | match hook_type { |
| 229 | HookType::TurnStart => { |
| 230 | if let Some(prompt) = parsed.prompt.filter(|p| !p.is_empty()) { |
| 231 | // Grok's UserPromptSubmit often wraps the human text in |
| 232 | // `<user_query>…</user_query>` (harness envelope). That |
| 233 | // raw string would otherwise become the Atomic change |
| 234 | // message (`atomic log` shows the tags). Strip it so |
| 235 | // provenance and the commit message carry the real text. |
| 236 | let prompt = normalize_user_prompt(&prompt); |
| 237 | if !prompt.is_empty() { |
| 238 | event = event.with_prompt(prompt); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | HookType::PreToolUse | HookType::PostToolUse => { |
| 243 | if let Some(name) = parsed.tool_name.filter(|n| !n.is_empty()) { |
| 244 | event = event.with_tool_name(name); |
| 245 | } |
| 246 | if let Some(id) = parsed.tool_use_id.filter(|id| !id.is_empty()) { |
| 247 | event = event.with_tool_use_id(id); |
| 248 | } |
| 249 | } |
| 250 | HookType::SessionStart | HookType::SessionEnd | HookType::TurnEnd => {} |
| 251 | } |
| 252 | |
| 253 | Ok(event.with_timestamp(timestamp)) |
| 254 | } |
| 255 | |
| 256 | fn install(&self, _repo_root: &Path) -> AgentResult<usize> { |
| 257 | // Installation is handled by the atomic-grok package via the |