| 1 | //! Parsing and rendering for archived-context transcript cells. |
| 2 | |
| 3 | use ratatui::style::{Modifier, Style}; |
| 4 | use ratatui::text::{Line, Span}; |
| 5 | |
| 6 | use codewhale_palette as palette; |
| 7 | |
| 8 | use super::{HistoryCell, TRANSCRIPT_RAIL}; |
| 9 | |
| 10 | /// Parse an `<archived_context>` block from an assistant Text block. |
| 11 | /// |
| 12 | /// Returns `Some(HistoryCell::ArchivedContext)` when the text contains a |
| 13 | /// well-formed `<archived_context>...</archived_context>` block, or `None` |
| 14 | /// if the text is regular assistant content. |
| 15 | pub(super) fn parse_archived_context(text: &str) -> Option<HistoryCell> { |
| 16 | let (tag, summary) = codewhale_engine::runtime_handoff::archived_context_parts(text)?; |
| 17 | |
| 18 | let raw_level = archived_context_attr(tag, "level"); |
| 19 | let Some(level) = raw_level.as_deref().and_then(|v| v.parse::<u8>().ok()) else { |
| 20 | tracing::debug!(level = ?raw_level, "archived_context tag has no valid level"); |
| 21 | return None; |
| 22 | }; |
| 23 | |
| 24 | let range = archived_context_attr(tag, "range").unwrap_or_default(); |
| 25 | |
| 26 | let tokens = archived_context_attr(tag, "tokens").unwrap_or_default(); |
| 27 | |
| 28 | let density = archived_context_attr(tag, "density").unwrap_or_default(); |
| 29 | |
| 30 | let model = archived_context_attr(tag, "model").unwrap_or_default(); |
| 31 | |
| 32 | let timestamp = archived_context_attr(tag, "timestamp").unwrap_or_default(); |
| 33 | |
| 34 | Some(HistoryCell::ArchivedContext { |
| 35 | level, |
| 36 | range, |
| 37 | tokens, |
| 38 | density, |
| 39 | model, |
| 40 | timestamp, |
| 41 | summary: summary.to_string(), |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | fn archived_context_attr(tag: &str, name: &str) -> Option<String> { |
| 46 | let needle = format!("{name}=\""); |
| 47 | let start = tag.find(&needle)? + needle.len(); |
| 48 | let rest = &tag[start..]; |
| 49 | let end = rest.find('"')?; |
| 50 | Some(rest[..end].to_string()) |
| 51 | } |
| 52 | |
| 53 | /// Render an `<archived_context>` block with dimmed/italic styling. |
| 54 | pub(super) fn render_archived_context( |
| 55 | cell: &HistoryCell, |
| 56 | width: u16, |
| 57 | palette_mode: palette::PaletteMode, |
| 58 | ) -> Vec<Line<'static>> { |
| 59 | let HistoryCell::ArchivedContext { |
| 60 | level, |
| 61 | range, |
| 62 | tokens, |
| 63 | density, |
| 64 | model, |
| 65 | timestamp, |
| 66 | summary, |
| 67 | } = cell |
| 68 | else { |
| 69 | return Vec::new(); |
| 70 | }; |
| 71 | |
| 72 | let body = if summary.is_empty() { |
| 73 | "(no summary)".to_string() |
| 74 | } else { |
| 75 | summary.clone() |
| 76 | }; |
| 77 | |
| 78 | let label = format!("Context L{level}"); |
| 79 | let label_style = Style::default() |
| 80 | .fg(palette::TEXT_DIM) |
| 81 | .add_modifier(Modifier::BOLD); |
| 82 | let body_style = Style::default().fg(palette::TEXT_DIM).italic(); |
| 83 | |
| 84 | let content_width = width.saturating_sub(4).max(1); |
| 85 | |
| 86 | let mut lines = Vec::new(); |
| 87 | |
| 88 | let range_display = if range.is_empty() { |
| 89 | String::new() |
| 90 | } else { |
| 91 | range.to_string() |
| 92 | }; |
| 93 | let mut header = format!("{label} {range_display}"); |
| 94 | if !tokens.is_empty() { |
| 95 | header.push_str(&format!(" {tokens}")); |
| 96 | } |
| 97 | if !density.is_empty() && density != tokens { |
| 98 | header.push_str(&format!(" {density}")); |
| 99 | } |
| 100 | lines.push(Line::from(Span::styled(header, label_style))); |
| 101 | |
| 102 | let model_display = if model.is_empty() { |
| 103 | String::new() |
| 104 | } else { |
| 105 | format!("via {model}") |
| 106 | }; |
| 107 | let ts_display = if timestamp.is_empty() { |
| 108 | String::new() |
| 109 | } else { |
| 110 | timestamp.clone() |
| 111 | }; |
| 112 | let mut sub = String::new(); |
| 113 | if !model_display.is_empty() { |
| 114 | sub.push_str(&model_display); |
| 115 | } |
| 116 | if !ts_display.is_empty() { |
| 117 | if !sub.is_empty() { |
| 118 | sub.push_str(" · "); |
| 119 | } |
| 120 | sub.push_str(&ts_display); |
| 121 | } |
| 122 | if !sub.is_empty() { |
| 123 | lines.push(Line::from(Span::styled( |
| 124 | sub, |
| 125 | Style::default().fg(palette::TEXT_MUTED), |
| 126 | ))); |
| 127 | } |
| 128 | |
| 129 | let rendered = codewhale_tui_kit::markdown_render::render_markdown_tagged_with_palette( |
| 130 | &body, |
| 131 | content_width, |
| 132 | body_style, |
| 133 | palette_mode, |
| 134 | ); |
| 135 | for (idx, line) in rendered |
| 136 | .into_iter() |
| 137 | .map(|rendered| rendered.line) |
| 138 | .enumerate() |
| 139 | { |
| 140 | if idx == 0 { |
| 141 | let mut spans = vec![Span::styled( |
| 142 | TRANSCRIPT_RAIL.to_string(), |
| 143 | Style::default().fg(palette::TEXT_DIM), |
| 144 | )]; |
| 145 | spans.extend(line.spans); |
| 146 | lines.push(Line::from(spans)); |
| 147 | } else { |
| 148 | let mut spans = vec![Span::raw(" ")]; |
| 149 | spans.extend(line.spans); |
| 150 | lines.push(Line::from(spans)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | lines.push(Line::from("")); |
| 155 | |
| 156 | lines |
| 157 | } |
| 158 |