codewhale-tui 0.8.67

Terminal UI for open-source and open-weight coding models
//! Compact transcript rendering for agent and activity metadata cells.

use ratatui::style::Style;
use ratatui::text::{Line, Span};

use crate::palette;

use super::{
    GenericToolCell, render_tool_header_with_family_and_summary, tool_status_label, truncate_text,
};

pub(super) fn render_agent_compact(cell: &GenericToolCell, low_motion: bool) -> Vec<Line<'static>> {
    let family = crate::tui::widgets::tool_card::ToolFamily::Delegate;
    let agent_id = cell
        .output
        .as_deref()
        .and_then(extract_agent_id)
        .map(str::to_string)
        .unwrap_or_else(|| delegate_identity_fallback(cell));
    vec![render_tool_header_with_family_and_summary(
        family,
        Some(agent_id.as_str()),
        tool_status_label(cell.status),
        cell.status,
        None,
        low_motion,
    )]
}

pub(super) fn render_activity_group(cell: &GenericToolCell, width: u16) -> Vec<Line<'static>> {
    let summary = cell.input_summary.as_deref().unwrap_or("Updated metadata");
    let budget = usize::from(width).max(1);
    vec![Line::from(Span::styled(
        truncate_text(summary, budget),
        Style::default().fg(palette::TEXT_MUTED),
    ))]
}

fn delegate_identity_fallback(cell: &GenericToolCell) -> String {
    if let Some(summary) = cell.input_summary.as_deref() {
        let summary = summary.trim();
        if let Some(rest) = summary.strip_prefix("role:") {
            let role = rest.split_whitespace().next().unwrap_or(rest).trim();
            if !role.is_empty() {
                return role.to_string();
            }
        }
        if let Some(rest) = summary.strip_prefix("prompt:") {
            let title = rest.trim();
            if !title.is_empty() {
                let slug: String = title
                    .chars()
                    .take(24)
                    .map(|ch| {
                        if ch.is_ascii_alphanumeric() {
                            ch.to_ascii_lowercase()
                        } else {
                            '-'
                        }
                    })
                    .collect();
                let slug = slug.trim_matches('-');
                if !slug.is_empty() {
                    return slug.to_string();
                }
            }
        }
    }
    "unknown child".to_string()
}

pub(super) fn extract_agent_id(output: &str) -> Option<&str> {
    let key = "\"agent_id\"";
    let key_idx = output.find(key)?;
    let rest = &output[key_idx + key.len()..];
    let colon = rest.find(':')?;
    let after_colon = rest[colon + 1..].trim_start();
    let after_colon = after_colon.strip_prefix('"')?;
    let end = after_colon.find('"')?;
    let id = &after_colon[..end];
    (!id.is_empty()).then_some(id)
}