From bbe8d6a6541245d6d39058ac874752170dce607c Mon Sep 17 00:00:00 2001 From: Danilo Leal <67129314+danilo-leal@users.noreply.github.com> Date: Mon, 28 Apr 2025 18:51:41 -0300 Subject: [PATCH] agent: Cancel pending in-edit user message upon new message submit (#29565) Previously, if you clicked on a user message to edit it, and then, while the user message has the editor pending, sent a new message via the textarea, the whole thread would be grayed out because we hadn't dismissed the to-be-edited pending user message. That's now fixed. Release Notes: - agent: Fixed a bug that would make the whole thread be grayed out upon sending a new message while a user message had a pending edit. --- crates/agent/src/active_thread.rs | 5 +++++ crates/agent/src/message_editor.rs | 8 ++++++++ crates/agent/src/thread.rs | 9 +++++++++ crates/eval/src/example.rs | 3 ++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/agent/src/active_thread.rs b/crates/agent/src/active_thread.rs index 2b60d976971e220954075bde95d5ab555b460692..e8e5351c1aa7c7df3cd66e9d56055f55046c003c 100644 --- a/crates/agent/src/active_thread.rs +++ b/crates/agent/src/active_thread.rs @@ -903,6 +903,11 @@ impl ActiveThread { cx: &mut Context, ) { match event { + ThreadEvent::CancelEditing => { + if self.editing_message.is_some() { + self.cancel_editing_message(&menu::Cancel, window, cx); + } + } ThreadEvent::ShowError(error) => { self.last_error = Some(error.clone()); } diff --git a/crates/agent/src/message_editor.rs b/crates/agent/src/message_editor.rs index 17fbcfb5a0b5e0fa22792914f23ea98b045090bc..65ae2db0b57c0c08b1a409489b8417f1d80ab2f9 100644 --- a/crates/agent/src/message_editor.rs +++ b/crates/agent/src/message_editor.rs @@ -244,6 +244,10 @@ impl MessageEditor { return; } + self.thread.update(cx, |thread, cx| { + thread.cancel_editing(cx); + }); + if self.thread.read(cx).is_generating() { self.stop_current_and_send_new_message(window, cx); return; @@ -312,6 +316,10 @@ impl MessageEditor { } fn stop_current_and_send_new_message(&mut self, window: &mut Window, cx: &mut Context) { + self.thread.update(cx, |thread, cx| { + thread.cancel_editing(cx); + }); + let cancelled = self.thread.update(cx, |thread, cx| { thread.cancel_last_completion(Some(window.window_handle()), cx) }); diff --git a/crates/agent/src/thread.rs b/crates/agent/src/thread.rs index 88a56bf2a5b8670f3ca5c682c9360a9c1f8bd99e..a723d082c1f5901d6bf0413ece10b14c4cbd39fe 100644 --- a/crates/agent/src/thread.rs +++ b/crates/agent/src/thread.rs @@ -1866,6 +1866,14 @@ impl Thread { canceled } + /// Signals that any in-progress editing should be canceled. + /// + /// This method is used to notify listeners (like ActiveThread) that + /// they should cancel any editing operations. + pub fn cancel_editing(&mut self, cx: &mut Context) { + cx.emit(ThreadEvent::CancelEditing); + } + pub fn feedback(&self) -> Option { self.feedback } @@ -2384,6 +2392,7 @@ pub enum ThreadEvent { }, CheckpointChanged, ToolConfirmationNeeded, + CancelEditing, } impl EventEmitter for Thread {} diff --git a/crates/eval/src/example.rs b/crates/eval/src/example.rs index c0c3c4cd99f3a4aa3198c791a1cbf9e6229ecca7..9a9b7fd577ffba0b6fb635539741405615477b97 100644 --- a/crates/eval/src/example.rs +++ b/crates/eval/src/example.rs @@ -276,7 +276,8 @@ impl ExampleContext { | ThreadEvent::ReceivedTextChunk | ThreadEvent::StreamedToolUse { .. } | ThreadEvent::CheckpointChanged - | ThreadEvent::UsageUpdated(_) => { + | ThreadEvent::UsageUpdated(_) + | ThreadEvent::CancelEditing => { tx.try_send(Ok(())).ok(); if std::env::var("ZED_EVAL_DEBUG").is_ok() { println!("{}Event: {:#?}", log_prefix, event);