use std::sync::Arc;
use ratatui::text::{Line, Span};
use vtcode_commons::diagnostics::{Diagnostic, DiagnosticSeverity, diagnostic_is_user_visible, redact_diagnostic};
use vtcode_commons::ui_protocol::{InlineMessageKind, InlineSegment, InlineTextStyle, convert_style};
use super::{Session, message::TranscriptLine};
use crate::tui::core_tui::style::ratatui_style_from_inline;
use crate::tui::ui::markdown::{
CodeBlockWeight, MarkdownLine, MarkdownSegment, RenderMarkdownOptions, render_markdown_to_lines_with_options,
theme_matched_highlight_config,
};
use crate::tui::ui::theme::ThemeStyles;
#[derive(Clone)]
pub struct DiagnosticPresentation {
pub payload: Diagnostic,
pub expanded: bool,
}
impl Session {
pub(crate) fn upsert_diagnostic(&mut self, diagnostic: Diagnostic) {
let diagnostic = redact_diagnostic(&diagnostic);
let previous_max_offset = self.current_max_scroll_offset();
let existing = self.lines.iter().position(|line| {
line.diagnostic
.as_ref()
.is_some_and(|entry| entry.payload.correlation_id == diagnostic.correlation_id)
});
let kind = match diagnostic.severity {
DiagnosticSeverity::Info => InlineMessageKind::Info,
DiagnosticSeverity::Warning => InlineMessageKind::Warning,
DiagnosticSeverity::Error => InlineMessageKind::Error,
};
self.clear_thinking_spinner_if_active(kind);
let segments = vec![InlineSegment {
text: diagnostic.summary.clone(),
style: Arc::new(InlineTextStyle::default()),
}];
let index = if let Some(index) = existing {
if self.lines[index]
.diagnostic
.as_ref()
.is_some_and(|entry| entry.payload == diagnostic)
{
return;
}
let revision = self.next_revision();
let line = &mut self.lines[index];
line.kind = kind;
line.segments = segments;
line.revision = revision;
index
} else {
self.push_line(kind, segments);
self.lines.len().saturating_sub(1)
};
let expanded = self.lines[index].diagnostic.as_ref().is_some_and(|entry| entry.expanded);
self.lines[index].diagnostic = Some(Box::new(DiagnosticPresentation { payload: diagnostic, expanded }));
self.mark_line_dirty(index);
self.invalidate_scroll_metrics();
self.adjust_scroll_after_change(previous_max_offset);
self.request_transcript_clear();
}
pub(crate) fn toggle_diagnostic(&mut self, index: usize) -> bool {
let previous_max_offset = self.current_max_scroll_offset();
let revision = self.next_revision();
let Some(line) = self.lines.get_mut(index) else {
return false;
};
let Some(diagnostic) = line.diagnostic.as_mut() else {
return false;
};
let kind = line.kind;
if !details_disclosed_in_transcript(&diagnostic.payload.agent_details, kind) {
return false;
}
diagnostic.expanded.toggle();
let expanded_now = diagnostic.expanded;
line.revision = revision;
let was_following = self.scroll_manager.offset() == 0;
self.mark_line_dirty(index);
self.invalidate_scroll_metrics();
self.adjust_scroll_after_change(previous_max_offset);
if expanded_now && was_following {
self.pin_viewport_after_expansion(previous_max_offset);
}
self.request_transcript_clear();
true
}
fn diagnostic_is_hidden(&self, payload: &Diagnostic) -> bool {
!diagnostic_is_user_visible(self.appearance.communication_style, &payload.code)
}
pub(crate) fn toggle_latest_diagnostic(&mut self) -> bool {
let style = self.appearance.communication_style;
let index = self.lines.iter().rposition(|line| {
line.diagnostic.as_ref().is_some_and(|entry| {
!entry.payload.agent_details.trim().is_empty() && diagnostic_is_user_visible(style, &entry.payload.code)
})
});
index.is_some_and(|index| self.toggle_diagnostic(index))
}
pub(crate) fn toggle_diagnostic_at_row(&mut self, width: u16, row: usize) -> bool {
let target = {
let cache = self.ensure_reflow_cache(width);
cache
.row_offsets
.partition_point(|&start| start <= row)
.checked_sub(1)
.and_then(|index| {
let start = *cache.row_offsets.get(index)?;
let line = cache.messages.get(index)?.lines.get(row - start)?;
line.line
.spans
.iter()
.map(|span| span.content.as_ref())
.collect::<String>()
.trim_start()
.starts_with("Details (")
.then_some(index)
})
};
target.is_some_and(|index| self.toggle_diagnostic(index))
}
pub(super) fn reflow_diagnostic(&self, index: usize, width: u16, review: bool) -> Vec<TranscriptLine> {
let Some(line) = self.lines.get(index) else {
return Vec::new();
};
let Some(entry) = line.diagnostic.as_ref() else {
return Vec::new();
};
let payload = &entry.payload;
if !review && self.diagnostic_is_hidden(payload) {
return Vec::new();
}
let expanded = entry.expanded || review;
let styles = crate::theme::active_styles();
let base_style = anstyle::Style::new().fg_color(self.text_fallback(line.kind));
let render_options = RenderMarkdownOptions {
table_max_width: (width > 0).then_some(usize::from(width)),
..Default::default()
};
let severity_label = if self.appearance.screen_reader_mode {
match payload.severity {
DiagnosticSeverity::Info => "[Info] ",
DiagnosticSeverity::Warning => "[Warning] ",
DiagnosticSeverity::Error => "[Error] ",
}
} else {
""
};
let mut summary = format!("{severity_label}{}", payload.summary);
if !payload.actions.is_empty() {
summary.push_str("\n\nAvailable actions: ");
summary.push_str(
&payload
.actions
.iter()
.map(|action| action.label.as_str())
.collect::<Vec<_>>()
.join("; "),
);
}
let mut rendered = render_markdown_to_lines_with_options(&summary, base_style, &styles, None, render_options);
if !payload.agent_details.is_empty() {
if review || details_disclosed_in_transcript(&payload.agent_details, line.kind) {
push_markdown_separator(&mut rendered);
let affordance = if expanded {
"Details (click or F8 to collapse)"
} else {
"Details (click or F8 to expand)"
};
rendered.push(MarkdownLine {
segments: vec![MarkdownSegment {
style: disclosure_affordance_style(&styles),
text: affordance.to_string(),
link_target: None,
}],
line_background: None,
});
if expanded {
push_markdown_separator(&mut rendered);
let highlight_config = theme_matched_highlight_config(&self.appearance.syntax_highlighting);
let body_options = RenderMarkdownOptions {
code_block_weight: CodeBlockWeight::Muted,
..render_options
};
rendered.extend(render_markdown_to_lines_with_options(
&details_source(&payload.agent_details),
base_style,
&styles,
Some(&highlight_config),
body_options,
));
}
}
}
let mut output = Vec::new();
for markdown_line in rendered {
let spans = markdown_line
.segments
.into_iter()
.map(|segment| {
Span::styled(
segment.text,
ratatui_style_from_inline(&convert_style(segment.style), self.text_fallback(line.kind)),
)
})
.collect::<Vec<_>>();
let rendered_line = Line::from(spans);
let rows = if width == 0 {
vec![rendered_line]
} else {
self.wrap_line(rendered_line, usize::from(width))
};
output.extend(
rows.into_iter().map(|line| {
super::reflow::transcript_line_with_detected_links(line, self.workspace_root.as_deref())
}),
);
}
output.push(TranscriptLine::default());
output
}
}
fn push_markdown_separator(lines: &mut Vec<MarkdownLine>) {
if lines.last().is_some_and(|line| !line.is_empty()) {
lines.push(MarkdownLine::default());
}
}
const MUTED_AFFORDANCE_BLEND_RATIO: f32 = 0.35;
const MAX_STRUCTURED_DETAILS_BYTES: usize = 64 * 1024;
fn is_structured_details(details: &str) -> bool {
let trimmed = details.trim_start();
if !trimmed.starts_with('{') && !trimmed.starts_with('[') {
return false;
}
if trimmed.len() > MAX_STRUCTURED_DETAILS_BYTES {
return true;
}
if vtcode_commons::fs::try_parse_json_value(trimmed).is_some() {
return true;
}
trimmed
.split('\n')
.next()
.and_then(vtcode_commons::fs::try_parse_json_value)
.is_some()
}
fn details_disclosed_in_transcript(details: &str, kind: InlineMessageKind) -> bool {
if details.trim().is_empty() {
return false;
}
if !is_structured_details(details) {
return true;
}
kind != InlineMessageKind::Info
}
fn details_source(details: &str) -> String {
let trimmed = details.trim();
if !is_structured_details(details) {
return details.to_string();
}
let longest_run = trimmed.split(|c: char| c != '`').map(|run| run.len()).max().unwrap_or(0);
let fence = "`".repeat(longest_run.max(2) + 1);
format!("{fence}json\n{trimmed}\n{fence}")
}
fn disclosure_affordance_style(theme_styles: &ThemeStyles) -> anstyle::Style {
let receded = crate::theme::recede_toward_background(
theme_styles.foreground,
theme_styles.background,
MUTED_AFFORDANCE_BLEND_RATIO,
crate::theme::get_minimum_contrast(),
);
anstyle::Style::new().fg_color(Some(receded))
}
#[cfg(test)]
mod tests;