| 1 | use std::path::Path; |
| 2 | |
| 3 | use serde_json::Value; |
| 4 | |
| 5 | use super::{RunEvent, truncate}; |
| 6 | |
| 7 | const COMMAND_LIMIT: usize = 60; |
| 8 | |
| 9 | pub(super) fn parse_line(line: &str) -> Vec<RunEvent> { |
| 10 | let Ok(value) = serde_json::from_str::<Value>(line) else { |
| 11 | return Vec::new(); |
| 12 | }; |
| 13 | match value.get("type").and_then(Value::as_str) { |
| 14 | Some("system") if value.get("subtype").and_then(Value::as_str) == Some("init") => { |
| 15 | session_id_events(&value) |
| 16 | } |
| 17 | Some("assistant") => assistant_events(&value), |
| 18 | Some("result") => result_events(&value), |
| 19 | _ => Vec::new(), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | fn session_id_events(value: &Value) -> Vec<RunEvent> { |
| 24 | value |
| 25 | .get("session_id") |
| 26 | .and_then(Value::as_str) |
| 27 | .map(|session_id| vec![RunEvent::SessionId(session_id.to_string())]) |
| 28 | .unwrap_or_default() |
| 29 | } |
| 30 | |
| 31 | fn assistant_events(value: &Value) -> Vec<RunEvent> { |
| 32 | let Some(blocks) = value.pointer("/message/content").and_then(Value::as_array) else { |
| 33 | return Vec::new(); |
| 34 | }; |
| 35 | blocks |
| 36 | .iter() |
| 37 | .filter_map(|block| match block.get("type").and_then(Value::as_str) { |
| 38 | Some("tool_use") => Some(RunEvent::Step(tool_label(block))), |
| 39 | Some("text") => block |
| 40 | .get("text") |
| 41 | .and_then(Value::as_str) |
| 42 | .filter(|text| !text.trim().is_empty()) |
| 43 | .map(|text| RunEvent::Text(text.to_string())), |
| 44 | _ => None, |
| 45 | }) |
| 46 | .collect() |
| 47 | } |
| 48 | |
| 49 | fn result_events(value: &Value) -> Vec<RunEvent> { |
| 50 | let mut events = session_id_events(value); |
| 51 | let subtype = value.get("subtype").and_then(Value::as_str).unwrap_or(""); |
| 52 | let is_error = value |
| 53 | .get("is_error") |
| 54 | .and_then(Value::as_bool) |
| 55 | .unwrap_or(false); |
| 56 | let result = value.get("result").and_then(Value::as_str); |
| 57 | if subtype == "success" && !is_error { |
| 58 | events.push(RunEvent::Final(result.unwrap_or_default().to_string())); |
| 59 | } else { |
| 60 | let message = result.filter(|text| !text.is_empty()).unwrap_or(subtype); |
| 61 | events.push(RunEvent::Failed(message.to_string())); |
| 62 | } |
| 63 | events |
| 64 | } |
| 65 | |
| 66 | fn input_text<'a>(input: &'a Value, key: &str) -> &'a str { |
| 67 | input.get(key).and_then(Value::as_str).unwrap_or("") |
| 68 | } |
| 69 | |
| 70 | fn file_name(path: &str) -> String { |
| 71 | Path::new(path) |
| 72 | .file_name() |
| 73 | .map(|name| name.to_string_lossy().into_owned()) |
| 74 | .unwrap_or_else(|| path.to_string()) |
| 75 | } |
| 76 | |
| 77 | fn host_of(url: &str) -> String { |
| 78 | let without_scheme = url.split_once("://").map_or(url, |(_, rest)| rest); |
| 79 | without_scheme |
| 80 | .split(['/', ':', '?', '#']) |
| 81 | .next() |
| 82 | .unwrap_or(without_scheme) |
| 83 | .to_string() |
| 84 | } |
| 85 | |
| 86 | fn tool_label(block: &Value) -> String { |
| 87 | let name = block.get("name").and_then(Value::as_str).unwrap_or("tool"); |
| 88 | let null = Value::Null; |
| 89 | let input = block.get("input").unwrap_or(&null); |
| 90 | match name { |
| 91 | "Read" => format!("Reading {}", file_name(input_text(input, "file_path"))), |
| 92 | "Edit" | "Write" | "MultiEdit" | "NotebookEdit" => { |
| 93 | let path = match input_text(input, "file_path") { |
| 94 | "" => input_text(input, "notebook_path"), |
| 95 | path => path, |
| 96 | }; |
| 97 | format!("Editing {}", file_name(path)) |
| 98 | } |
| 99 | "Bash" => { |
| 100 | let description = input_text(input, "description"); |
| 101 | let detail = if description.is_empty() { |
| 102 | truncate(input_text(input, "command"), COMMAND_LIMIT) |
| 103 | } else { |
| 104 | description.to_string() |
| 105 | }; |
| 106 | format!("Running {detail}") |
| 107 | } |
| 108 | "Grep" => format!("Searching \"{}\"", input_text(input, "pattern")), |
| 109 | "Glob" => format!("Finding {}", input_text(input, "pattern")), |
| 110 | "WebSearch" => format!("Searching web: {}", input_text(input, "query")), |
| 111 | "WebFetch" => format!("Reading {}", host_of(input_text(input, "url"))), |
| 112 | "Task" | "Agent" => "Running subagent".to_string(), |
| 113 | "TodoWrite" => "Planning".to_string(), |
| 114 | "mcp__teams__propose_message" => "Proposing reply".to_string(), |
| 115 | other if other.starts_with("mcp__teams__") => "Reading Teams".to_string(), |
| 116 | other => other.to_string(), |
| 117 | } |
| 118 | } |