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