Parse a single JSONL line into a `CostTurn`, if it's an assistant message with usage data.
(line: &str, project_hash: &str, session_id: &str)
| 76 | /// Parse a single JSONL line into a `CostTurn`, if it's an assistant message |
| 77 | /// with usage data. |
| 78 | fn parse_line(line: &str, project_hash: &str, session_id: &str) -> Option<CostTurn> { |
| 79 | let v: Value = serde_json::from_str(line).ok()?; |
| 80 | |
| 81 | // Only process assistant messages |
| 82 | if v.get("type")?.as_str()? != "assistant" { |
| 83 | return None; |
| 84 | } |
| 85 | |
| 86 | let msg = v.get("message")?; |
| 87 | let message_id = msg.get("id")?.as_str()?; |
| 88 | let model = msg.get("model")?.as_str()?; |
| 89 | |
| 90 | let usage = msg.get("usage")?; |
| 91 | let input_tokens = usage.get("input_tokens")?.as_u64().unwrap_or(0); |
| 92 | let output_tokens = usage.get("output_tokens")?.as_u64().unwrap_or(0); |
| 93 | let cache_write_tokens = usage |
| 94 | .get("cache_creation_input_tokens") |
| 95 | .and_then(serde_json::Value::as_u64) |
| 96 | .unwrap_or(0); |
| 97 | let cache_read_tokens = usage |
| 98 | .get("cache_read_input_tokens") |
| 99 | .and_then(serde_json::Value::as_u64) |
| 100 | .unwrap_or(0); |
| 101 | |
| 102 | // Parse timestamp from the outer object (ISO 8601) |
| 103 | let timestamp = parse_timestamp(v.get("timestamp")?.as_str()?)?; |
| 104 | |
| 105 | // Extract tool names and bash commands for classification |
| 106 | let content = msg.get("content").and_then(|c| c.as_array()); |
| 107 | let mut tool_names_vec: Vec<String> = Vec::new(); |
| 108 | let mut bash_commands: Vec<String> = Vec::new(); |
| 109 | |
| 110 | if let Some(blocks) = content { |
| 111 | for block in blocks { |
| 112 | if block.get("type").and_then(|t| t.as_str()) == Some("tool_use") { |
| 113 | if let Some(name) = block.get("name").and_then(|n| n.as_str()) { |
| 114 | tool_names_vec.push(name.to_string()); |
| 115 | if name == "Bash" { |
| 116 | if let Some(cmd) = block |
| 117 | .get("input") |
| 118 | .and_then(|i| i.get("command")) |
| 119 | .and_then(|c| c.as_str()) |
| 120 | { |
| 121 | bash_commands.push(cmd.to_string()); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Classify |
| 130 | let tool_refs: Vec<&str> = tool_names_vec |
| 131 | .iter() |
| 132 | .map(std::string::String::as_str) |
| 133 | .collect(); |
| 134 | let bash_refs: Vec<&str> = bash_commands |
| 135 | .iter() |