Skip to main content

tracevault_cli/hooks/
claude_code.rs

1use std::path::Path;
2use tracevault_core::streaming::StreamEventRequest;
3
4use super::HookAdapter;
5
6pub struct ClaudeCodeAdapter;
7
8impl HookAdapter for ClaudeCodeAdapter {
9    fn tool_name(&self) -> &str {
10        "claude-code"
11    }
12
13    fn parse_event(&self, raw: &str) -> Result<StreamEventRequest, String> {
14        let mut req: StreamEventRequest =
15            serde_json::from_str(raw).map_err(|e| format!("Failed to parse event JSON: {e}"))?;
16        req.tool = Some("claude-code".to_string());
17        if req.protocol_version == 0 {
18            req.protocol_version = 2;
19        }
20        Ok(req)
21    }
22
23    fn parse_transcript(&self, path: &Path) -> Result<Vec<serde_json::Value>, String> {
24        let content =
25            std::fs::read_to_string(path).map_err(|e| format!("Failed to read transcript: {e}"))?;
26        let mut lines = Vec::new();
27        for line in content.lines() {
28            if line.trim().is_empty() {
29                continue;
30            }
31            match serde_json::from_str(line) {
32                Ok(v) => lines.push(v),
33                Err(_) => continue,
34            }
35        }
36        Ok(lines)
37    }
38}