Add sidebar image previews and present generated images after answers (#61919)

## What changed

- Open PNG, JPEG, GIF, and WebP file links as sidebar image previews, fitted to the available space with format and dimension labels. Show a fallback when terminal graphics are unavailable, and preserve text previews for files such as Git LFS pointers.
- In the owned transcript, place completed, saved image results after the assistant's answer, with larger, left-aligned previews and a clickable `Generated image` label. Preserve this ordering during replay and history pagination, including interrupted turns.
- Reuse native Kitty image IDs and move existing placements without retransmitting their pixels. Probe Kitty graphics support in `herdr` before enabling previews.

## Testing

Add regression coverage for sidebar preview fallbacks, generated-image ordering, pagination and search, preview layout, Kitty placement reuse, and graphics probe replies interleaved with pasted input.

GitOrigin-RevId: 22d8aa2724f17bfa7f8280a2d11d4a7e2cb66728
This commit is contained in:
Felipe Coury
2026-11-17 00:13:04 +00:00
committed by copyberry
parent f8e58064fc
commit a669d8ffb2
45 changed files with 1242 additions and 186 deletions
+11 -1
View File
@@ -89,9 +89,19 @@ fn cell_content_layout(
if let Some(protocol) = presentation.image_protocol
&& let Some(path) = cell.image_path()
{
use crate::transcript_image::PreviewKind;
let kind = if cell
.as_any()
.is::<crate::history_cell::ImageGenerationHistoryCell>()
{
PreviewKind::Generated
} else {
PreviewKind::Inspected
};
layout.image =
crate::transcript_image::TranscriptImage::load(path.as_path(), width, protocol)
crate::transcript_image::TranscriptImage::load(path.as_path(), width, protocol, kind)
.map(Arc::new);
layout.image_left_aligned = kind == PreviewKind::Generated;
}
layout
}
@@ -164,16 +164,29 @@ fn fullscreen_image_previews_follow_layout_without_entering_text_surfaces() {
image::RgbaImage::from_pixel(480, 160, image::Rgba([255, 0, 0, 255]))
.save(&path)
.unwrap();
let square_path = dir.path().join("generated.png");
image::RgbImage::new(768, 768).save(&square_path).unwrap();
let cells: Vec<Arc<dyn HistoryCell>> = vec![
Arc::new(new_view_image_tool_call(
"view-call".to_string(),
LegacyAppPathString::from_string(path.to_string_lossy().into_owned()),
)),
Arc::new(
crate::history_cell::AgentMarkdownCell::new(
"Here is the image.".to_string(),
dir.path(),
)
.with_phase(Some(codex_protocol::models::MessagePhase::FinalAnswer)),
),
Arc::new(new_image_generation_call(
"image-call".to_string(),
"completed",
Some("A red rectangle".to_string()),
Some(AbsolutePathBuf::try_from(path.clone()).unwrap()),
Some("A square".to_string()),
Some(AbsolutePathBuf::try_from(square_path.clone()).unwrap()),
)),
Arc::new(crate::history_cell::FinalMessageSeparator::new(
Some(2),
/*runtime_metrics*/ None,
)),
];
let area = Rect::new(
@@ -193,8 +206,18 @@ fn fullscreen_image_previews_follow_layout_without_entering_text_surfaces() {
let text_only = buf.clone();
view.image_protocol = Some(ImageProtocol::Kitty);
view.render(area, &mut buf, &cells);
view.sync_turn_details(&cells, /*enabled*/ true, /*active*/ None);
view.render(area, &mut buf, &cells);
let collapsed = frame(&buf);
let collapsed_areas: Vec<_> = view.image_placements().iter().map(|p| p.area).collect();
assert_eq!(collapsed_areas.len(), 1);
assert!(
collapsed.find("Here is the image.").unwrap() < collapsed.find("Generated image").unwrap()
);
assert_eq!(collapsed_areas[0].x, area.x + 2);
assert!(collapsed_areas[0].height > 6);
view.sync_turn_details(&cells, /*enabled*/ false, /*active*/ None);
view.render(area, &mut buf, &cells);
// Find may request full text, but only manual expansion may reveal inspected pixels.
assert!(
view.entry_layout(&cells, /*index*/ 0, /*full_content*/ true)
@@ -220,7 +243,8 @@ fn fullscreen_image_previews_follow_layout_without_entering_text_surfaces() {
frame(&buf)
)
);
for (index, expected) in text.iter().enumerate() {
for index in [0, 2] {
let expected = &text[index];
let layout = view.layout(&cells, index).unwrap();
assert_eq!(layout.text(), expected);
let pinned_narrow = layout.rewrap(/*width*/ 8);
@@ -231,7 +255,7 @@ fn fullscreen_image_previews_follow_layout_without_entering_text_surfaces() {
}
// Short, inset viewports may begin in an image. Its placement stays inside the viewport.
let short = Rect::new(
/*x*/ 7, /*y*/ 5, /*width*/ 16, /*height*/ 3,
/*x*/ 7, /*y*/ 5, /*width*/ 16, /*height*/ 5,
);
view.jump_to_latest();
view.render(short, &mut Buffer::empty(short), &cells);
@@ -266,6 +290,7 @@ fn fullscreen_image_previews_follow_layout_without_entering_text_surfaces() {
// Resumed sessions with a deleted file fall back to the original text entry.
std::fs::remove_file(&path).unwrap();
std::fs::remove_file(&square_path).unwrap();
view.image_protocol = Some(ImageProtocol::Kitty);
view.render(area, &mut buf, &cells);
assert_eq!(buf, text_only);
@@ -595,6 +595,15 @@ impl TranscriptView {
&mut self,
cells: &[Arc<dyn HistoryCell>],
inserted: Range<usize>,
) {
self.history_page_loaded(cells, inserted, /*inserted_images*/ None);
}
pub(crate) fn history_page_loaded(
&mut self,
cells: &[Arc<dyn HistoryCell>],
inserted: Range<usize>,
inserted_images: Option<Range<usize>>,
) {
self.invalidate_turn_details_from(/*index*/ 0);
// With only a header and live output, the first older page extends the canonical tail.
@@ -604,7 +613,17 @@ impl TranscriptView {
{
self.last_tail = cells.last().map(EntryKey::cell);
}
let image_span = inserted_images.map(|images| {
// The older-page prepend shifts the separately inserted images.
let len = inserted.len();
self.prepend_snapshot_history(cells, images.start + len..images.end + len)
});
let inserted = self.prepend_snapshot_history(cells, inserted);
let inserted = if inserted.is_empty() {
image_span.unwrap_or(inserted)
} else {
inserted
};
if let Position::Reading(anchor) = self.position {
let index = self.resolve(cells, anchor);
self.position = Position::Reading(Anchor {
@@ -918,6 +918,33 @@ fn search_scans_a_page_inserted_after_the_retained_session_header() {
assert!(Arc::ptr_eq(&cells[0], &header));
}
#[test]
fn search_resumes_from_the_text_in_a_page_with_a_relocated_image() {
let mut cells = vec![cell("answer"), cell("completion")];
let mut view = TranscriptView {
area: Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 6,
),
history: TranscriptHistoryState::Partial,
..TranscriptView::default()
};
view.begin_search();
view.paste_search("needle");
finish_scan(&mut view, &cells);
assert!(view.search.needs_history(view.history));
let match_cell = cell("older needle");
cells.insert(/*index*/ 1, cell("Generated image"));
cells.insert(/*index*/ 0, Arc::clone(&match_cell));
view.history = TranscriptHistoryState::Complete;
view.history_page_loaded(&cells, 0..1, Some(1..2));
finish_scan(&mut view, &cells);
assert_eq!(
view.search.current.as_ref().unwrap().anchor.key,
EntryKey::cell(&match_cell)
);
}
#[test]
fn search_finds_the_first_older_entry_after_replaying_the_session_header() {
let header = || -> Arc<dyn HistoryCell> {
@@ -1,16 +1,42 @@
---
source: tui/src/transcript_view/layout_tests.rs
assertion_line: 210
assertion_line: 239
expression: "format!(\"Collapsed inspection / generated output\\n{collapsed}\\nGraphics: {collapsed_areas:?}\\n\\nExpanded inspection / generated output\\n{}\\nGraphics: {expanded_areas:?}\",\nframe(&buf))"
---
Collapsed inspection / generated output
• Viewed image artifact.png
+ Show details
▸ Viewed image /workspace/artifact.png
• Generated Image:
└ A red rectangle
└ Saved to: file:///workspace/artifact.png
Graphics: [Rect { x: 52, y: 6, width: 36, height: 6 }]
• Here is the image.
Generated image
Worked for 2s
Graphics: [Rect { x: 2, y: 5, width: 50, height: 25 }]
Expanded inspection / generated output
• Viewed image /workspace/artifact.png
@@ -22,7 +48,34 @@ Expanded inspection / generated output
• Generated Image:
└ A red rectangle
└ Saved to: file:///workspace/artifact.png
Graphics: [Rect { x: 52, y: 2, width: 36, height: 6 }, Rect { x: 52, y: 12, width: 36, height: 6 }]
• Here is the image.
Generated image
Worked for 2s
Graphics: [Rect { x: 52, y: 2, width: 36, height: 6 }, Rect { x: 2, y: 12, width: 50, height: 25 }]
+3
View File
@@ -47,6 +47,7 @@ use ratatui::text::Line;
/// One cell's logical text and its width-dependent display rows.
pub(crate) struct TextLayout {
pub(super) image: Option<std::sync::Arc<crate::transcript_image::TranscriptImage>>,
pub(super) image_left_aligned: bool,
logical: Vec<LogicalLine>,
text: String,
rows: Vec<TextRow>,
@@ -212,6 +213,7 @@ impl TextLayout {
layout = layout.with_control(control.label.clone(), control.anchor);
}
layout.image = self.image.clone();
layout.image_left_aligned = self.image_left_aligned;
if self.leading_blank_hidden {
layout = layout.without_leading_blank_row();
}
@@ -941,6 +943,7 @@ impl TextLayout {
}
Self {
image: None,
image_left_aligned: false,
logical,
text,
rows,