Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
agent_ui: Fix pasted screenshots being inserted twice (#65626)
qystishere664935cb5e26
diff --git a/crates/agent_ui/src/inline_prompt_editor.rs b/crates/agent_ui/src/inline_prompt_editor.rsindex 3d5baa652fd..75f205e67a9 100644--- a/crates/agent_ui/src/inline_prompt_editor.rs+++ b/crates/agent_ui/src/inline_prompt_editor.rs@@ -420,6 +420,9 @@ impl<T: 'static> PromptEditor<T> {cx,){+ // Capture-phase actions propagate by default, so stop the editor from also+ // pasting the clipboard's file paths as text.+ cx.stop_propagation();task.detach();}}@@ -1633,7 +1636,7 @@ mod tests {use project::Project;use settings::SettingsStore;use std::cell::RefCell;- use std::path::Path;+ use std::path::{Path, PathBuf};use std::rc::Rc;use terminal::TerminalBuilder;use terminal::terminal_settings::CursorShape;@@ -1819,4 +1822,105 @@ mod tests {"Expected ConfirmRequested with execute: false");}++ #[gpui::test]+ async fn test_paste_image_does_not_paste_file_paths_as_text(cx: &mut TestAppContext) {+ let mut png = std::io::Cursor::new(Vec::new());+ image::DynamicImage::new_rgb8(1, 1)+ .write_to(&mut png, image::ImageFormat::Png)+ .expect("encode png");+ let prompt = paste_into_prompt_editor(+ vec![+ gpui::ClipboardEntry::Image(gpui::Image::from_bytes(+ gpui::ImageFormat::Png,+ png.into_inner(),+ )),+ gpui::ClipboardEntry::ExternalPaths(gpui::ExternalPaths(+ vec![PathBuf::from(path!("/tmp/screenshot.png"))].into(),+ )),+ ],+ cx,+ )+ .await;++ let image_uri = acp_thread::MentionUri::PastedImage {+ name: "Image".into(),+ };+ assert_eq!(prompt, format!("{} ", image_uri.as_link()));+ }++ #[gpui::test]+ async fn test_paste_non_image_file_pastes_path_as_text(cx: &mut TestAppContext) {+ let path = PathBuf::from(path!("/tmp/notes.txt"));+ let prompt = paste_into_prompt_editor(+ vec![gpui::ClipboardEntry::ExternalPaths(gpui::ExternalPaths(+ vec![path.clone()].into(),+ ))],+ cx,+ )+ .await;++ assert_eq!(prompt, path.display().to_string());+ }++ async fn paste_into_prompt_editor(+ entries: Vec<gpui::ClipboardEntry>,+ cx: &mut TestAppContext,+ ) -> String {+ init_test(cx);+ cx.update(|cx| {+ LanguageModelRegistry::global(cx).update(cx, |registry, cx| {+ let mut model = registry.default_model().expect("fake default model");+ model.supports_images = true;+ registry.set_inline_assistant_model(Some(model), cx);+ });+ });++ let fs = FakeFs::new(cx.executor());+ fs.insert_tree("/project", serde_json::json!({"file": ""}))+ .await;+ let project = Project::test(fs.clone(), [Path::new(path!("/project"))], cx).await;+ let (workspace, _) =+ cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));+ let workspace = workspace.downgrade();+ let (prompt_editor, cx) = cx.add_window_view(|window, cx| {+ let terminal = cx.new(|cx| {+ TerminalBuilder::new_display_only(+ CursorShape::default(),+ settings::AlternateScroll::On,+ None,+ 0,+ cx.background_executor(),+ PathStyle::local(),+ )+ .subscribe(cx)+ });+ let session_id = Uuid::new_v4();+ let codegen = cx.new(|_| TerminalCodegen::new(terminal, session_id));+ let prompt_buffer =+ cx.new(|cx| MultiBuffer::singleton(cx.new(|cx| Buffer::local("", cx)), cx));+ PromptEditor::new_terminal(+ TerminalInlineAssistId::default(),+ VecDeque::new(),+ prompt_buffer,+ codegen,+ session_id,+ fs,+ cx.new(|cx| ThreadStore::new(cx)),+ project.downgrade(),+ workspace,+ window,+ cx,+ )+ });++ cx.write_to_clipboard(ClipboardItem { entries });+ prompt_editor.update_in(cx, |prompt_editor, window, cx| {+ prompt_editor.editor.focus_handle(cx).focus(window, cx);+ });+ cx.dispatch_action(Paste);+ cx.run_until_parked();++ prompt_editor.read_with(cx, |prompt_editor, cx| prompt_editor.prompt(cx))+ }}diff --git a/crates/agent_ui/src/mention_set.rs b/crates/agent_ui/src/mention_set.rsindex 13fd7505121..d6f0cd6ef2e 100644--- a/crates/agent_ui/src/mention_set.rs+++ b/crates/agent_ui/src/mention_set.rs@@ -1050,6 +1050,10 @@ pub(crate) fn paste_images_as_context(return None;}+ // When this handles the paste, the caller stops the editor's own text paste, so fall+ // back to pasting the clipboard's text (e.g. non-image file paths) if no images load.+ let fallback_text = clipboard.text();+Some(window.spawn(cx, async move |mut cx| {use itertools::Itertools;let default_name: SharedString = "Image".into();@@ -1062,21 +1066,26 @@ pub(crate) fn paste_images_as_context(}).partition_map::<Vec<_>, Vec<_>, _, _, _>(std::convert::identity);- if !paths.is_empty() {- images.extend(- cx.background_spawn(async move {+ // A bitmap and image files in the clipboard are usually the same image (e.g. a+ // screenshot saved to a temporary file), so only load files when there's no bitmap.+ if images.is_empty() && !paths.is_empty() {+ images = cx+ .background_spawn(async move {paths.into_iter().flat_map(|paths| paths.paths().to_owned()).filter_map(|path| load_external_image_from_path(&path, &default_name)).collect::<Vec<_>>()})- .await,- );+ .await;}if !images.is_empty() {insert_images_as_context(images, editor, mention_set, workspace, &mut cx).await;+ } else if let Some(text) = fallback_text {+ editor+ .update_in(cx, |editor, window, cx| editor.insert(&text, window, cx))+ .ok();}}))}diff --git a/crates/agent_ui/src/message_editor.rs b/crates/agent_ui/src/message_editor.rsindex 605817d05b8..717f9f14f0d 100644--- a/crates/agent_ui/src/message_editor.rs+++ b/crates/agent_ui/src/message_editor.rs@@ -328,6 +328,11 @@ async fn resolve_pasted_context_items(let mut items = Vec::new();let mut added_worktrees = Vec::new();let default_image_name: SharedString = "Image".into();+ // A bitmap and image files in the clipboard are usually the same image (e.g. a+ // screenshot saved to a temporary file), so skip image files when there's a bitmap.+ let has_clipboard_image = entries+ .iter()+ .any(|entry| matches!(entry, ClipboardEntry::Image(_)));for entry in entries {match entry {@@ -355,7 +360,7 @@ async fn resolve_pasted_context_items(}).await{- if supports_images {+ if supports_images && !has_clipboard_image {items.push(ResolvedPastedContextItem::Image(image, name));}continue;@@ -5430,6 +5435,171 @@ mod tests {}));}+ #[gpui::test]+ async fn test_paste_raw_image_takes_priority_over_image_files(cx: &mut TestAppContext) {+ init_test(cx);+ let directory = tempfile::tempdir().expect("create image directory");+ let (image, raw_image) = paste_test_image();+ let paths = ["first.png", "second.png"].map(|name| directory.path().join(name));+ for path in &paths {+ image+ .save_with_format(path, image::ImageFormat::Png)+ .expect("write png");+ }++ for inline in [false, true] {+ for raw_first in [false, true] {+ for image_count in [1, 2] {+ let (message_editor, editor, mut cx) =+ setup_paste_test_message_editor(json!({"file.txt": "content"}), cx).await;+ let image_paths = paths.iter().take(image_count).cloned().collect();+ let mut entries = vec![+ ClipboardEntry::ExternalPaths(ExternalPaths(image_paths)),+ ClipboardEntry::Image(raw_image.clone()),+ ];+ if raw_first {+ entries.reverse();+ }+ paste_image_context(&message_editor, entries, inline, &mut cx);++ let image_uri = MentionUri::PastedImage {+ name: "Image".into(),+ };+ editor.update(&mut cx, |editor, cx| {+ assert_eq!(editor.text(cx), format!("{} ", image_uri.as_link()));+ });+ let contents = mention_contents(&message_editor, &mut cx).await;+ assert_eq!(contents.len(), 1);+ assert!(matches!(+ contents.first(),+ Some((uri, Mention::Image(_))) if uri == &image_uri+ ));+ }+ }+ }+ }++ #[gpui::test]+ async fn test_paste_raw_image_keeps_non_image_files(cx: &mut TestAppContext) {+ init_test(cx);+ let directory = tempfile::tempdir().expect("create clipboard directory");+ std::fs::write(directory.path().join("notes.txt"), "plain text")+ .expect("write non-image file");+ let (image, raw_image) = paste_test_image();++ let path = directory.path().join("notes.txt");++ for inline in [false, true] {+ let (message_editor, editor, mut cx) =+ setup_paste_test_message_editor(json!({"file.txt": "content"}), cx).await;+ let fs = message_editor.read_with(&cx, |message_editor, cx| {+ message_editor+ .workspace+ .upgrade()+ .expect("workspace")+ .read(cx)+ .project()+ .read(cx)+ .fs()+ .clone()+ });+ fs.as_fake()+ .insert_tree(directory.path(), json!({"notes.txt": "plain text"}))+ .await;+ paste_image_context(+ &message_editor,+ vec![+ ClipboardEntry::ExternalPaths(ExternalPaths(vec![path.clone()].into())),+ ClipboardEntry::Image(raw_image.clone()),+ ],+ inline,+ &mut cx,+ );++ let image_uri = MentionUri::PastedImage {+ name: "Image".into(),+ };+ let contents = mention_contents(&message_editor, &mut cx).await;+ let Some((_, Mention::Image(pasted_image))) =+ contents.iter().find(|(uri, _)| uri == &image_uri)+ else {+ panic!("Expected raw image");+ };+ let bytes = base64::prelude::BASE64_STANDARD+ .decode(pasted_image.data.as_ref())+ .expect("decode pasted image");+ assert_eq!(+ image::load_from_memory(&bytes)+ .expect("load pasted image")+ .to_rgb8(),+ image.to_rgb8(),+ );++ // The inline prompt only handles images, so it ignores other files.+ let mut expected_text = String::new();+ if !inline {+ let file_uri = MentionUri::File {+ abs_path: path.clone(),+ };+ expected_text.push_str(&format!("{} ", file_uri.as_link()));+ assert!(contents.iter().any(|(uri, mention)| {+ uri == &file_uri+ && matches!(mention, Mention::Text { content, .. } if content == "plain text")+ }));+ }+ expected_text.push_str(&format!("{} ", image_uri.as_link()));+ assert_eq!(contents.len(), if inline { 1 } else { 2 });+ editor.update(&mut cx, |editor, cx| {+ assert_eq!(editor.text(cx), expected_text);+ });+ }+ }++ fn paste_test_image() -> (image::DynamicImage, gpui::Image) {+ let image = image::DynamicImage::ImageRgb8(image::RgbImage::from_pixel(+ 2,+ 2,+ image::Rgb([32, 96, 160]),+ ));+ let mut bitmap = std::io::Cursor::new(Vec::new());+ image+ .write_to(&mut bitmap, image::ImageFormat::Bmp)+ .expect("encode clipboard bitmap");+ let raw_image = gpui::Image::from_bytes(gpui::ImageFormat::Bmp, bitmap.into_inner());+ (image, raw_image)+ }++ fn paste_image_context(+ message_editor: &Entity<MessageEditor>,+ entries: Vec<ClipboardEntry>,+ inline: bool,+ cx: &mut VisualTestContext,+ ) {+ cx.write_to_clipboard(ClipboardItem { entries });+ message_editor.update_in(cx, |message_editor, window, cx| {+ message_editor+ .session_capabilities+ .write()+ .set_prompt_capabilities(+ acp_v2::PromptCapabilities::new().image(acp_v2::PromptImageCapabilities::new()),+ );+ if inline {+ crate::mention_set::paste_images_as_context(+ message_editor.editor().clone(),+ message_editor.mention_set().clone(),+ message_editor.workspace.clone(),+ window,+ cx,+ )+ .expect("handle image clipboard")+ .detach();+ } else {+ message_editor.paste(&Paste, window, cx);+ }+ });+ cx.run_until_parked();+ }+async fn setup_paste_test_message_editor(project_tree: Value,cx: &mut TestAppContext,