| 1 | //! Compact transcript rendering for agent and activity metadata cells. |
| 2 | |
| 3 | use ratatui::style::Style; |
| 4 | use ratatui::text::{Line, Span}; |
| 5 | |
| 6 | use codewhale_localization::{Locale, MessageId, tr}; |
| 7 | use codewhale_palette as palette; |
| 8 | |
| 9 | use super::{GenericToolCell, render_tool_header_with_family_and_summary, truncate_text}; |
| 10 | |
| 11 | pub(super) fn render_agent_compact( |
| 12 | cell: &GenericToolCell, |
| 13 | low_motion: bool, |
| 14 | locale: Locale, |
| 15 | ) -> Vec<Line<'static>> { |
| 16 | let family = crate::tui::widgets::tool_card::ToolFamily::Delegate; |
| 17 | let agent_id = cell |
| 18 | .output |
| 19 | .as_deref() |
| 20 | .and_then(extract_agent_id) |
| 21 | .map(str::to_string) |
| 22 | .unwrap_or_else(|| delegate_identity_fallback(cell)); |
| 23 | // Inspections and joins must not draw the same "delegate done" line as a |
| 24 | // spawn — during a fan-out session every peek/status/wait would otherwise |
| 25 | // read as yet another completed delegate (#4112, dogfood A5). The action |
| 26 | // is stamped at the front of the args summary by tool_routing. |
| 27 | let state_label = match (agent_inspection_action(cell), cell.status) { |
| 28 | (Some(AgentCompactAction::Check), super::ToolStatus::Running) => { |
| 29 | Some(tr(locale, MessageId::AgentActivityChecking)) |
| 30 | } |
| 31 | (Some(AgentCompactAction::Check), super::ToolStatus::Success) => { |
| 32 | Some(tr(locale, MessageId::AgentActivityChecked)) |
| 33 | } |
| 34 | (Some(AgentCompactAction::Wait), super::ToolStatus::Running) => { |
| 35 | Some(tr(locale, MessageId::AgentActivityWaiting)) |
| 36 | } |
| 37 | (Some(AgentCompactAction::Wait), super::ToolStatus::Success) => { |
| 38 | Some(tr(locale, MessageId::AgentActivityWaited)) |
| 39 | } |
| 40 | _ => None, |
| 41 | }; |
| 42 | vec![render_tool_header_with_family_and_summary( |
| 43 | family, |
| 44 | Some(agent_id.as_str()), |
| 45 | state_label.as_deref(), |
| 46 | cell.status, |
| 47 | None, |
| 48 | low_motion, |
| 49 | locale, |
| 50 | )] |
| 51 | } |
| 52 | |
| 53 | enum AgentCompactAction { |
| 54 | /// Read-only inspection: peek / status / progress / list / inspect. |
| 55 | Check, |
| 56 | /// Blocking join: wait / join / await / block. |
| 57 | Wait, |
| 58 | } |
| 59 | |
| 60 | /// Whether this `agent` cell is a read-only inspection or join rather than a |
| 61 | /// spawn — those stay compact even in Transcript mode. |
| 62 | pub(super) fn is_agent_inspection(cell: &GenericToolCell) -> bool { |
| 63 | agent_inspection_action(cell).is_some() |
| 64 | } |
| 65 | |
| 66 | fn agent_inspection_action(cell: &GenericToolCell) -> Option<AgentCompactAction> { |
| 67 | let summary = cell.input_summary.as_deref()?; |
| 68 | let action = summary.strip_prefix("action:")?.trim_start(); |
| 69 | let action = action.split_whitespace().next().unwrap_or(""); |
| 70 | match action.trim_end_matches(',') { |
| 71 | "peek" | "progress" | "status" | "list" | "inspect" => Some(AgentCompactAction::Check), |
| 72 | "wait" | "join" | "await" | "block" => Some(AgentCompactAction::Wait), |
| 73 | _ => None, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | pub(super) fn render_activity_group( |
| 78 | cell: &GenericToolCell, |
| 79 | width: u16, |
| 80 | locale: Locale, |
| 81 | ) -> Vec<Line<'static>> { |
| 82 | let fallback = super::tool_run::sentence_case_activity( |
| 83 | tr(locale, MessageId::ActivityUpdatedMetadata).into_owned(), |
| 84 | ); |
| 85 | let summary = cell.input_summary.as_deref().unwrap_or(&fallback); |
| 86 | let suffix = cell |
| 87 | .output_summary |
| 88 | .as_deref() |
| 89 | .map_or(" ›".to_string(), |count| format!(" {count} ›")); |
| 90 | let budget = usize::from(width) |
| 91 | .saturating_sub(unicode_width::UnicodeWidthStr::width(suffix.as_str())) |
| 92 | .max(1); |
| 93 | vec![Line::from(Span::styled( |
| 94 | format!("{}{suffix}", truncate_text(summary, budget)), |
| 95 | Style::default().fg(palette::TEXT_MUTED), |
| 96 | ))] |
| 97 | } |
| 98 | |
| 99 | pub(super) fn render_turn_work(cell: &GenericToolCell, width: u16) -> Vec<Line<'static>> { |
| 100 | let budget = usize::from(width).saturating_sub(2).max(1); |
| 101 | let headline = cell.input_summary.as_deref().unwrap_or_default(); |
| 102 | let mut lines = vec![Line::from(Span::styled( |
| 103 | format!("{} \u{203A}", truncate_text(headline, budget)), |
| 104 | Style::default().fg(palette::TEXT_MUTED), |
| 105 | ))]; |
| 106 | if let Some(changes) = cell.output_summary.as_deref() { |
| 107 | lines.push(Line::from(Span::styled( |
| 108 | truncate_text(changes, usize::from(width).max(1)), |
| 109 | Style::default().fg(palette::TEXT_DIM), |
| 110 | ))); |
| 111 | } |
| 112 | lines |
| 113 | } |
| 114 | |
| 115 | pub(super) fn render_agent_line(cell: &GenericToolCell, width: u16) -> Vec<Line<'static>> { |
| 116 | let width = usize::from(width).max(1); |
| 117 | let tag = truncate_text(cell.output_summary.as_deref().unwrap_or("agent"), width); |
| 118 | let tag_width = unicode_width::UnicodeWidthStr::width(tag.as_str()); |
| 119 | let rest = cell.input_summary.as_deref().unwrap_or_default(); |
| 120 | let rest_budget = width.saturating_sub(tag_width + 2); |
| 121 | let mut spans = vec![Span::styled( |
| 122 | tag, |
| 123 | Style::default() |
| 124 | .fg(palette::TEXT_MUTED) |
| 125 | .add_modifier(ratatui::style::Modifier::BOLD), |
| 126 | )]; |
| 127 | if rest_budget > 0 { |
| 128 | spans.push(Span::styled( |
| 129 | format!("{} \u{203A}", truncate_text(rest, rest_budget)), |
| 130 | Style::default().fg(palette::TEXT_DIM), |
| 131 | )); |
| 132 | } |
| 133 | vec![Line::from(spans)] |
| 134 | } |
| 135 | |
| 136 | fn delegate_identity_fallback(cell: &GenericToolCell) -> String { |
| 137 | if let Some(summary) = cell.input_summary.as_deref() { |
| 138 | let summary = summary.trim(); |
| 139 | if let Some(rest) = summary.strip_prefix("role:") { |
| 140 | let role = rest.split_whitespace().next().unwrap_or(rest).trim(); |
| 141 | if !role.is_empty() { |
| 142 | return role.to_string(); |
| 143 | } |
| 144 | } |
| 145 | if let Some(rest) = summary.strip_prefix("prompt:") { |
| 146 | let title = rest.trim(); |
| 147 | if !title.is_empty() { |
| 148 | let slug: String = title |
| 149 | .chars() |
| 150 | .take(24) |
| 151 | .map(|ch| { |
| 152 | if ch.is_ascii_alphanumeric() { |
| 153 | ch.to_ascii_lowercase() |
| 154 | } else { |
| 155 | '-' |
| 156 | } |
| 157 | }) |
| 158 | .collect(); |
| 159 | let slug = slug.trim_matches('-'); |
| 160 | if !slug.is_empty() { |
| 161 | return slug.to_string(); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | // #4148: never surface the raw internal fallback token ("unknown child") |
| 167 | // in the default transcript. When we can't resolve a concrete role, slug, |
| 168 | // or agent id, a friendly, non-leaky label reads best next to the |
| 169 | // "delegate" verb ("delegate running · subagent"). |
| 170 | "subagent".to_string() |
| 171 | } |
| 172 | |
| 173 | pub(super) fn extract_agent_id(output: &str) -> Option<&str> { |
| 174 | let key = "\"agent_id\""; |
| 175 | let key_idx = output.find(key)?; |
| 176 | let rest = &output[key_idx + key.len()..]; |
| 177 | let colon = rest.find(':')?; |
| 178 | let after_colon = rest[colon + 1..].trim_start(); |
| 179 | let after_colon = after_colon.strip_prefix('"')?; |
| 180 | let end = after_colon.find('"')?; |
| 181 | let id = &after_colon[..end]; |
| 182 | (!id.is_empty()).then_some(id) |
| 183 | } |
| 184 |