//! Conversation blocks — semantic lines for transcript and tool activity rendering. //! //! Keeps per-block presentation separate from viewport anchoring, wrapping, and input layout. mod group; mod message; mod tool; use super::tool_card; use crate::app::ConvoBlock; use crate::glyphs::{icon_label, Icon}; use crate::graphics::ImageKey; use crate::markdown::{layout_markdown, ImageRow, MarkdownBlockKind, MarkdownLine}; use crate::theme::styles::ThemeStyles; pub(super) use group::{run_is_working, tool_group_lines}; use message::{prefixed_text_lines, system_lines, thinking_lines, user_lines}; use ratatui::{ style::Style, text::{Line, Span}, }; use tool::tool_lines; pub(super) use tool::{header_subject_column, BODY_INDENT}; #[cfg(test)] use { crate::{ app::ConversationItem, widgets::{tool_glyph, GENERIC_TOOL}, }, message::{chipped_lines, line_text}, ratatui::style::Modifier, tool::format_tool_duration, }; /// Everything a block needs about the frame it is being drawn into. /// /// `spinner_frame` is the app-wide animation tick (the same counter the statusline uses), so a /// running tool card spins instead of showing a frozen glyph. `never_sent` marks a submission the /// engine never received, and `expanded` shows a long user message past its preview cap. The /// card fields travel with it because a tool card's body has to be laid out against a real width — /// two diff columns are only worth it if the card can seat them. pub(super) struct BlockContext<'a> { pub(crate) styles: &'a ThemeStyles, pub(crate) spinner_frame: usize, /// Whether the turn is waiting on the reader: a question to answer or a permission to give. pub(crate) waiting_on_user: bool, pub(crate) never_sent: bool, pub(crate) expanded: bool, /// Every image the transcript can draw, and how big each one wants to be. pub(crate) images: &'a crate::graphics::ImageStore, /// Which images each tool call produced. pub(crate) tool_images: &'a std::collections::HashMap<(String, String), Vec>, /// Cells an inline image may occupy across and down. pub(crate) image_bounds: (u16, u16), /// Cells a wide block's content may occupy, after the card's own indent. pub(crate) card_width: u16, /// Cells a prose block may occupy — the measure a collapsed reasoning peek is cut to. pub(crate) prose_width: u16, pub(crate) diff_layout: luxe_core::config::DiffLayout, /// Whether every tool card is showing its full output. pub(crate) tool_output_expanded: bool, /// The chord that toggles that, named in a truncation footer. pub(crate) expand_chord: Option, /// How long the call in flight has been running, in milliseconds. /// /// Handed in rather than read off the clock, so the figure moves with the instant the frame is /// drawn against — a recording pins that, and a timer that consults the wall reports how fast /// the recorder rendered. pub(crate) running_ms: Option, } impl BlockContext<'_> { fn card(&self) -> tool_card::CardContext<'_> { tool_card::CardContext { styles: self.styles, width: self.card_width, layout: self.diff_layout, expanded: self.tool_output_expanded, expand_chord: self.expand_chord.clone(), } } } /// The rows an inline image reserves, or the line that stands in for it. /// /// One or the other, never both. A terminal that cannot draw images gets a line naming what it is /// not showing — and, when the picture is a real file on disk, an affordance that opens it in /// whatever the desktop uses for images. That is the honest version of this feature on a terminal /// without it: not a low-fidelity imitation of the picture, but the picture, one click away. fn image_lines(key: &ImageKey, ctx: &BlockContext<'_>, style: Style) -> Vec { let Some((width, height)) = ctx.images.source_size(key) else { return vec![MarkdownLine::wide(Line::from(Span::styled( icon_label(Icon::Image.glyph(), "image unavailable"), ctx.styles.muted, )))]; }; let (max_cols, max_rows) = ctx.image_bounds; if ctx.images.tier().draws_pixels() { if let Some(fit) = ctx.images.fit(key, max_cols, max_rows) { return (0..fit.rows) .map(|row| MarkdownLine::image_row(ImageRow { key: key.clone(), fit, row })) .collect(); } } let origin = ctx.images.origin(key); let label = origin.map_or("image", |origin| origin.label.as_str()); let mut spans = vec![Span::styled( icon_label(Icon::Image.glyph(), &format!("{label} \u{b7} {width}\u{d7}{height}")), style, )]; let openable = origin.and_then(|origin| origin.path.as_deref()); if let Some(path) = openable { let name = path.file_name().map_or_else( || luxe_core::paths::display(path), |name| name.to_string_lossy().into_owned(), ); spans.push(Span::styled(format!(" {name}"), ctx.styles.muted)); spans.push(Span::styled(" open \u{2197}", ctx.styles.accent)); } let line = MarkdownLine::wide(Line::from(spans)); vec![if openable.is_some() { line.opens_image(key.clone()) } else { line }] } /// Renders one conversation block into semantic lines. pub(super) fn lines(block: &ConvoBlock, ctx: &BlockContext<'_>) -> Vec { let styles = ctx.styles; let style = block_style(block, styles); match block { ConvoBlock::User { text, attached, chips } => user_lines(text, attached, chips, style, ctx), ConvoBlock::Image { key, .. } => image_lines(key, ctx, style), ConvoBlock::Assistant(text) => { // What the agent wrote for Luxe — a remediation report, a draft — is shown where it lands let text = &*luxe_core::briefs::for_the_reader(text); if text.trim().is_empty() { return Vec::new(); } let rendered = layout_markdown(text, styles); if rendered.is_empty() { layout_lines(prefixed_text_lines("", text, style), MarkdownBlockKind::Prose) } else { rendered } } ConvoBlock::Thinking { text, expanded } => thinking_lines(text, *expanded, ctx), ConvoBlock::Tool(item) => tool_lines(item, style, ctx), ConvoBlock::System(text) => layout_lines( system_lines(Icon::SystemLine.glyph(), text, style, styles), MarkdownBlockKind::Prose, ), // Wide: a row is a label column, a figure column and a fixed-width meter, so reflowing it // for a narrow canvas would shear the alignment that makes the rows comparable. ConvoBlock::Context(report) => { layout_lines(super::context_report::lines(report, styles), MarkdownBlockKind::Wide) } } } /// Background band style for blocks that should stand out from plain assistant prose (user /// messages and tool calls). `None` renders with no band. pub(super) fn background(block: &ConvoBlock, styles: &ThemeStyles) -> Option