Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add some refinements to the draft thread UX (#56482)
Danilo Leala1306744da21
diff --git a/crates/agent_ui/src/agent_panel.rs b/crates/agent_ui/src/agent_panel.rsindex 1ca2fb44cec..31a821d870d 100644--- a/crates/agent_ui/src/agent_panel.rs+++ b/crates/agent_ui/src/agent_panel.rs@@ -816,6 +816,7 @@ pub struct AgentPanel {_active_thread_focus_subscription: Option<Subscription>,_base_view_observation: Option<Subscription>,_draft_editor_observation: Option<Subscription>,+ _active_draft_reclaim_observation: Option<Subscription>,_thread_metadata_store_subscription: Subscription,last_context_source: Option<AgentContextSource>,@@ -1150,6 +1151,7 @@ impl AgentPanel {new_user_onboarding_upsell_dismissed: AtomicBool::new(OnboardingUpsell::dismissed(cx)),_base_view_observation: None,_draft_editor_observation: None,+ _active_draft_reclaim_observation: None,_thread_metadata_store_subscription,last_context_source: None,is_active: false,@@ -1344,6 +1346,7 @@ impl AgentPanel {if let Some(draft) = self.draft_thread.clone()&& self.active_thread_is_draft(cx)&& !self.active_view_is_new_draft(cx)+ && *draft.read(cx).agent_key() == self.selected_agent{self.set_base_view(BaseView::AgentThread {@@ -1356,13 +1359,20 @@ impl AgentPanel {return;}- if let Some(draft) = self.draft_thread.clone()- && self.draft_has_content(&draft, cx)- {- let draft_id = draft.read(cx).thread_id;- self.draft_thread = None;- self._draft_editor_observation = None;- self.retained_threads.insert(draft_id, draft);+ if let Some(draft) = self.draft_thread.clone() {+ if self.draft_has_content(&draft, cx) {+ let draft_id = draft.read(cx).thread_id;+ self.draft_thread = None;+ self._draft_editor_observation = None;+ self.retained_threads.insert(draft_id, draft);+ } else if *draft.read(cx).agent_key() != self.selected_agent {+ let old_draft_id = draft.read(cx).thread_id;+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {+ store.delete(old_draft_id, cx);+ });+ self.draft_thread = None;+ self._draft_editor_observation = None;+ }}self.activate_draft(focus, trigger, window, cx);}@@ -2055,12 +2065,22 @@ impl AgentPanel {BaseView::AgentThread { conversation_view }if conversation_view.entity_id() == draft_entity);+if agent_matches || has_editor_content || !draft_is_active {return draft.clone();}++ // Clean up the old empty draft's metadata so it doesn't+ // linger as a ghost entry in the sidebar.+ let old_draft_id = draft.read(cx).thread_id;+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {+ store.delete(old_draft_id, cx);+ });+self.draft_thread = None;self._draft_editor_observation = None;}+let thread = self.create_agent_thread_with_server(desired_agent,None,@@ -2072,6 +2092,7 @@ impl AgentPanel {window,cx,);+self.draft_thread = Some(thread.conversation_view.clone());self.observe_draft_editor(&thread.conversation_view, cx);thread.conversation_view@@ -2085,8 +2106,22 @@ impl AgentPanel {if let Some(acp_thread) = conversation_view.read(cx).root_thread(cx) {self._draft_editor_observation = Some(cx.subscribe(&acp_thread,- |this, _, e: &AcpThreadEvent, cx| {- if let AcpThreadEvent::PromptUpdated = e {+ |this, acp_thread, event: &AcpThreadEvent, cx| {+ if !acp_thread.read(cx).is_draft_thread()+ && this.draft_thread.as_ref().is_some_and(|draft| {+ draft+ .read(cx)+ .root_thread(cx)+ .is_some_and(|thread| thread.entity_id() == acp_thread.entity_id())+ })+ {+ this.draft_thread = None;+ this._draft_editor_observation = None;+ this.serialize(cx);+ return;+ }++ if let AcpThreadEvent::PromptUpdated = event {this.serialize(cx);}},@@ -2101,6 +2136,125 @@ impl AgentPanel {}}+ /// Sets up an editor observation on the active view that reclaims+ /// it as ephemeral when the editor becomes empty. Only activates+ /// for non-ephemeral draft threads.+ fn observe_active_draft_for_empty_editor(+ &mut self,+ conversation_view: &Entity<ConversationView>,+ cx: &mut Context<Self>,+ ) {+ let thread_id = conversation_view.read(cx).thread_id;+ let is_ephemeral = self+ .draft_thread+ .as_ref()+ .is_some_and(|d| d.read(cx).thread_id == thread_id);+ if is_ephemeral {+ self._active_draft_reclaim_observation = None;+ return;+ }+ let is_draft = conversation_view+ .read(cx)+ .root_thread(cx)+ .is_some_and(|t| t.read(cx).is_draft_thread());+ if !is_draft {+ self._active_draft_reclaim_observation = None;+ return;+ }+ let Some(editor) = conversation_view+ .read(cx)+ .active_thread()+ .map(|tv| tv.read(cx).message_editor.clone())+ else {+ self._active_draft_reclaim_observation = None;+ return;+ };+ let cv = conversation_view.clone();+ self._active_draft_reclaim_observation =+ Some(cx.observe(&editor, move |this, _editor, cx| {+ let editor_has_text = cv.read(cx).active_thread().is_some_and(|tv| {+ !tv.read(cx)+ .message_editor+ .read(cx)+ .text(cx)+ .trim()+ .is_empty()+ });+ if editor_has_text {+ return;+ }+ if this.ephemeral_draft_thread_id(cx) == Some(thread_id) {+ return;+ }+ if this.active_thread_id(cx) != Some(thread_id) {+ return;+ }+ if this.try_make_empty_draft_ephemeral(cv.clone(), cx) {+ this._active_draft_reclaim_observation = None;+ cx.emit(AgentPanelEvent::EntryChanged);+ cx.notify();+ }+ }));+ }++ fn try_make_empty_draft_ephemeral(+ &mut self,+ conversation_view: Entity<ConversationView>,+ cx: &mut Context<Self>,+ ) -> bool {+ let (thread_id, is_draft, is_empty) = {+ let conversation = conversation_view.read(cx);+ let thread_id = conversation.thread_id;+ let is_draft = conversation+ .root_thread(cx)+ .is_some_and(|thread| thread.read(cx).is_draft_thread());+ let is_empty = if let Some(thread_view) = conversation.active_thread() {+ thread_view+ .read(cx)+ .message_editor+ .read(cx)+ .text(cx)+ .trim()+ .is_empty()+ } else {+ !self.draft_has_content(&conversation_view, cx)+ };++ (thread_id, is_draft, is_empty)+ };++ if !is_draft || !is_empty {+ return false;+ }++ self.retained_threads.remove(&thread_id);+ self.set_ephemeral_draft(conversation_view, cx);+ true+ }++ /// Moves a conversation view into the ephemeral `draft_thread` slot,+ /// cleaning up any previous ephemeral draft and deleting the thread's+ /// metadata so it no longer appears in the sidebar.+ fn set_ephemeral_draft(+ &mut self,+ conversation_view: Entity<ConversationView>,+ cx: &mut Context<Self>,+ ) {+ if let Some(old_draft) = self.draft_thread.take() {+ let old_id = old_draft.read(cx).thread_id;+ let new_id = conversation_view.read(cx).thread_id;+ if old_id != new_id {+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {+ store.delete(old_id, cx);+ });+ }+ self._draft_editor_observation = None;+ }+ self.draft_thread = Some(conversation_view.clone());+ self.observe_draft_editor(&conversation_view, cx);+ self.serialize(cx);+ }+pub fn activate_retained_thread(&mut self,id: ThreadId,@@ -2109,6 +2263,7 @@ impl AgentPanel {cx: &mut Context<Self>,) {let conversation_view = if let Some(view) = self.retained_threads.remove(&id) {+ self.try_make_empty_draft_ephemeral(view.clone(), cx);view} else if let Some(draft) = &self.draft_thread {if draft.read(cx).thread_id == id {@@ -2164,9 +2319,12 @@ impl AgentPanel {}pub fn ephemeral_draft_thread_id(&self, cx: &App) -> Option<ThreadId> {- self.draft_thread- .as_ref()- .map(|draft| draft.read(cx).thread_id)+ let draft = self.draft_thread.as_ref()?;+ let draft = draft.read(cx);+ draft+ .root_thread(cx)+ .is_some_and(|thread| thread.read(cx).is_draft_thread())+ .then_some(draft.thread_id)}pub fn active_terminal_id(&self) -> Option<TerminalId> {@@ -2197,6 +2355,10 @@ impl AgentPanel {}pub fn editor_text(&self, id: ThreadId, cx: &App) -> Option<String> {+ self.editor_text_if_in_memory(id, cx).flatten()+ }++ pub fn editor_text_if_in_memory(&self, id: ThreadId, cx: &App) -> Option<Option<String>> {let cv = self.retained_threads.get(&id)@@ -2216,9 +2378,9 @@ impl AgentPanel {let tv = cv.read(cx).root_thread_view()?;let text = tv.read(cx).message_editor.read(cx).text(cx);if text.trim().is_empty() {- None+ Some(None)} else {- Some(text)+ Some(Some(text))}}@@ -2890,6 +3052,13 @@ impl AgentPanel {.as_ref().is_some_and(|d| d.entity_id() == conversation_view.entity_id()){+ if self.draft_has_content(&conversation_view, cx) {+ let thread_id = conversation_view.read(cx).thread_id;+ self.draft_thread = None;+ self._draft_editor_observation = None;+ self.retained_threads.insert(thread_id, conversation_view);+ self.cleanup_retained_threads(cx);+ }return;}@@ -3008,17 +3177,16 @@ impl AgentPanel {cx.emit(AgentPanelEvent::ActiveViewFocused);cx.notify();}));- Some(cx.observe_in(- conversation_view,- window,- |this, server_view, window, cx| {- this._thread_view_subscription =- Self::subscribe_to_active_thread_view(&server_view, window, cx);- cx.emit(AgentPanelEvent::ActiveViewChanged);- this.serialize(cx);- cx.notify();- },- ))+ let cv = conversation_view.clone();+ self.observe_active_draft_for_empty_editor(&cv, cx);+ Some(cx.observe_in(&cv, window, |this, server_view, window, cx| {+ this._thread_view_subscription =+ Self::subscribe_to_active_thread_view(&server_view, window, cx);+ this.observe_active_draft_for_empty_editor(&server_view, cx);+ cx.emit(AgentPanelEvent::ActiveViewChanged);+ this.serialize(cx);+ cx.notify();+ }))}BaseView::Terminal { terminal_id } => {self._thread_view_subscription = None;@@ -3100,10 +3268,11 @@ impl AgentPanel {// promoted to a real thread. Clear the ephemeral// pointer; the ConversationView itself stays put as// the active base view.- if this.draft_thread.as_ref().is_some_and(|d| {- this.active_conversation_view()- .is_some_and(|active| active.entity_id() == d.entity_id())- }) {+ if this+ .draft_thread+ .as_ref()+ .is_some_and(|draft| draft.read(cx).thread_id == thread_id)+ {this.draft_thread = None;this._draft_editor_observation = None;}@@ -3202,6 +3371,7 @@ impl AgentPanel {return;}if let Some(conversation_view) = self.retained_threads.remove(&thread_id) {+ self.try_make_empty_draft_ephemeral(conversation_view.clone(), cx);self.set_base_view(BaseView::AgentThread { conversation_view },focus,@@ -3386,10 +3556,15 @@ impl AgentPanel {}/// Whether the active view is in the **ephemeral** new-draft slot- pub fn active_view_is_new_draft(&self, _cx: &App) -> bool {+ pub fn active_view_is_new_draft(&self, cx: &App) -> bool {self.draft_thread.as_ref().is_some_and(|draft| {- self.active_conversation_view()- .is_some_and(|active| active.entity_id() == draft.entity_id())+ draft+ .read(cx)+ .root_thread(cx)+ .is_some_and(|thread| thread.read(cx).is_draft_thread())+ && self+ .active_conversation_view()+ .is_some_and(|active| active.entity_id() == draft.entity_id())})}/// Whether the active thread is any kind of draft@@ -6024,8 +6199,8 @@ mod tests {});// 4. Switch the active view back to the real thread. The ephemeral- // draft remains in `draft_thread` (the ephemeral slot) but is- // no longer the active view.+ // draft has content, so it gets parked into `retained_threads`+ // immediately (the `draft_thread` slot is cleared).panel.update_in(cx, |panel, window, cx| {panel.load_agent_thread(Agent::Stub,@@ -6054,8 +6229,9 @@ mod tests {.expect("panel load should succeed");cx.run_until_parked();- // 6. The real thread is the active view on reload AND the draft- // slot is still occupied by the ephemeral new-draft thread.+ // 6. The real thread is the active view on reload. The draft+ // was parked when the user navigated away, so the draft_thread+ // slot is empty.loaded_panel.read_with(cx, |panel, cx| {assert_eq!(panel.active_thread_id(cx),@@ -6066,11 +6242,9 @@ mod tests {!panel.active_thread_is_draft(cx),"real thread is not a draft");- let restored_draft_id = panel.draft_thread.as_ref().map(|d| d.read(cx).thread_id);- assert_eq!(- restored_draft_id,- Some(draft_thread_id),- "new-draft slot should be repopulated with the pre-reload draft"+ assert!(+ panel.draft_thread.is_none(),+ "draft_thread slot should be empty since the draft was parked on navigate-away");});@@ -6097,9 +6271,9 @@ mod tests {assert_eq!(real_row.session_id.as_ref(), Some(&real_session_id));});- // 8. Opening the ephemeral draft via load_agent_thread activates- // the restored new-draft ConversationView and exposes its- // kvp-seeded prompt text in the editor.+ // 8. Opening the parked draft via load_agent_thread activates+ // a fresh ConversationView and exposes its kvp-seeded prompt+ // text in the editor.loaded_panel.update_in(cx, |panel, window, cx| {panel.load_agent_thread(Agent::Stub,@@ -7660,16 +7834,19 @@ mod tests {parked_thread_id,"sanity: parked draft should be the active view after load_agent_thread");+ // The parked draft has content, so it was NOT reclaimed as+ // ephemeral. The previous ephemeral draft should still be in+ // the draft_thread slot.panel.read_with(cx, |panel, _cx| {assert_eq!(panel.draft_thread.as_ref().unwrap().entity_id(),ephemeral_entity_id,- "sanity: the ephemeral draft slot should still hold the fresh draft"+ "ephemeral draft slot should still hold the fresh draft");});// Now press `+`. The ephemeral draft should become the active- // view; no new draft should be created.+ // view since it matches the selected agent.panel.update_in(cx, |panel, window, cx| {panel.new_thread(&NewThread, window, cx);});@@ -7693,6 +7870,108 @@ mod tests {});}+ /// When viewing a parked draft (agent A) and selecting a different+ /// agent (B) from the dropdown menu, the panel should create a fresh+ /// draft for agent B — not reuse the existing ephemeral draft that+ /// was bound to agent A.+ #[gpui::test]+ async fn test_new_external_agent_replaces_mismatched_ephemeral_draft(cx: &mut TestAppContext) {+ init_test(cx);+ let fs = FakeFs::new(cx.executor());+ cx.update(|cx| {+ agent::ThreadStore::init_global(cx);+ language_model::LanguageModelRegistry::test(cx);+ <dyn fs::Fs>::set_global(fs.clone(), cx);+ });++ let project = Project::test(fs.clone(), [], cx).await;+ let multi_workspace =+ cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));+ let workspace = multi_workspace+ .read_with(cx, |multi_workspace, _cx| {+ multi_workspace.workspace().clone()+ })+ .unwrap();+ workspace.update(cx, |workspace, _cx| workspace.set_random_database_id());+ let cx = &mut VisualTestContext::from_window(multi_workspace.into(), cx);+ let panel = workspace.update_in(cx, |workspace, window, cx| {+ let panel = cx.new(|cx| AgentPanel::new(workspace, None, window, cx));+ workspace.add_panel(panel.clone(), window, cx);+ panel+ });++ // Create a draft with Stub agent, type into it, then press `+`+ // to park it — this also creates a fresh ephemeral draft (Stub).+ panel.update_in(cx, |panel, window, cx| {+ panel.selected_agent = Agent::Stub;+ panel.activate_draft(true, "agent_panel", window, cx);+ });+ cx.run_until_parked();+ let parked_thread_id = crate::test_support::active_thread_id(&panel, cx);+ crate::test_support::type_draft_prompt(&panel, "parked prompt", cx);+ panel.update_in(cx, |panel, window, cx| {+ panel.new_thread(&NewThread, window, cx);+ });+ cx.run_until_parked();++ let ephemeral_thread_id = crate::test_support::active_thread_id(&panel, cx);+ assert_ne!(ephemeral_thread_id, parked_thread_id);+ panel.read_with(cx, |panel, cx| {+ assert_eq!(+ panel.draft_thread.as_ref().unwrap().read(cx).agent_key(),+ &Agent::Stub,+ "ephemeral draft should be Stub agent"+ );+ });++ // Navigate back to the parked draft (simulates sidebar click).+ panel.update_in(cx, |panel, window, cx| {+ panel.load_agent_thread(+ Agent::Stub,+ parked_thread_id,+ None,+ None,+ true,+ "sidebar",+ window,+ cx,+ );+ });+ cx.run_until_parked();+ assert_eq!(+ crate::test_support::active_thread_id(&panel, cx),+ parked_thread_id,+ );++ // Now switch to NativeAgent (simulates selecting a different+ // agent from the toolbar dropdown). This should NOT reuse the+ // Stub ephemeral draft — it should replace it with one bound to+ // NativeAgent.+ panel.update_in(cx, |panel, window, cx| {+ panel.selected_agent = Agent::NativeAgent;+ panel.activate_new_thread(true, "agent_panel", window, cx);+ });+ cx.run_until_parked();++ panel.read_with(cx, |panel, cx| {+ let draft = panel.draft_thread.as_ref().expect("draft should exist");+ assert_eq!(+ draft.read(cx).agent_key(),+ &Agent::NativeAgent,+ "ephemeral draft should be bound to NativeAgent, not Stub"+ );+ let active_id = panel.active_thread_id(cx).unwrap();+ assert_ne!(+ active_id, ephemeral_thread_id,+ "old Stub ephemeral draft should have been replaced"+ );+ assert!(+ panel.retained_threads.contains_key(&parked_thread_id),+ "parked draft should still be in retained_threads"+ );+ });+ }+#[gpui::test]async fn test_typed_draft_is_parked_when_switching_agents(cx: &mut TestAppContext) {init_test(cx);diff --git a/crates/agent_ui/src/draft_prompt_store.rs b/crates/agent_ui/src/draft_prompt_store.rsindex 4070b549190..b32b8e811cf 100644--- a/crates/agent_ui/src/draft_prompt_store.rs+++ b/crates/agent_ui/src/draft_prompt_store.rs@@ -111,13 +111,13 @@ pub fn display_label_for_draft(thread_id: ThreadId,cx: &App,) -> Option<SharedString> {- let from_editor = workspace+ let in_memory = workspace.and_then(|ws| ws.read(cx).panel::<AgentPanel>(cx))- .and_then(|panel| panel.read(cx).editor_text(thread_id, cx));- if let Some(raw) = from_editor- && let Some(label) = truncate_draft_label(&raw)- {- return Some(label);+ .and_then(|panel| panel.read(cx).editor_text_if_in_memory(thread_id, cx));+ match in_memory {+ Some(Some(raw)) => return truncate_draft_label(&raw),+ Some(None) => return None,+ None => {}}let blocks = read(thread_id, cx)?;diff --git a/crates/sidebar/src/sidebar.rs b/crates/sidebar/src/sidebar.rsindex 5f39556893e..3f997f59b24 100644--- a/crates/sidebar/src/sidebar.rs+++ b/crates/sidebar/src/sidebar.rs@@ -1407,14 +1407,13 @@ impl Sidebar {ThreadEntryWorkspace::Open(workspace) => Some(workspace),ThreadEntryWorkspace::Closed { .. } => None,};- if let Some(text) = agent_ui::draft_prompt_store::display_label_for_draft(+ thread.metadata.title = agent_ui::draft_prompt_store::display_label_for_draft(workspace,thread.metadata.thread_id,cx,- ) {- thread.metadata.title = Some(text);- }+ );}+ threads.retain(|thread| !thread.is_draft || thread.metadata.title.is_some());// Build a lookup from live_infos and compute running/waiting// counts in a single pass.@@ -1659,15 +1658,14 @@ impl Sidebar {subscriptions.push(cx.observe(&editor, |this, _editor, cx| {this.update_entries(cx);}));- } else {- // Editor hasn't been constructed yet (still Loading). Observe- // the ConversationView and re-run this wiring once it flips- // to Connected.- subscriptions.push(cx.observe(&cv, |this, _cv, cx| {- this.refresh_draft_editor_observations(cx);- this.update_entries(cx);- }));}+ // Also observe the ConversationView itself so that editor+ // replacements during lifecycle transitions (Loading →+ // Connected) re-wire the editor observation above.+ subscriptions.push(cx.observe(&cv, |this, _cv, cx| {+ this.refresh_draft_editor_observations(cx);+ this.update_entries(cx);+ }));}self._draft_editor_observations = subscriptions;}@@ -4063,7 +4061,6 @@ impl Sidebar {None}ListEntry::Thread(thread) => {- let session_id = thread.metadata.session_id.clone()?;let workspace = match &thread.workspace {ThreadEntryWorkspace::Open(workspace) => Some(workspace.clone()),ThreadEntryWorkspace::Closed { .. } => {@@ -4083,7 +4080,6 @@ impl Sidebar {format_history_entry_timestamp(Self::thread_display_time(&thread.metadata)).into();Some(ThreadSwitcherEntry::Thread(ThreadSwitcherThreadEntry {- session_id,title: thread.metadata.display_title(),icon: thread.icon,icon_from_external_svg: thread.icon_from_external_svg.clone(),@@ -4101,6 +4097,7 @@ impl Sidebar {}).collect(),diff_stats: thread.diff_stats,+ is_draft: thread.is_draft,is_title_generating: thread.is_title_generating,notified,timestamp,@@ -4434,20 +4431,18 @@ impl Sidebar {cx.flag_value::<AgentThreadWorktreeLabelFlag>(),);- let icon = if is_draft {- IconName::Plus- } else {- thread.icon- };- let icon_svg = if is_draft {- None+ let (icon, icon_svg) = if is_draft {+ (IconName::Circle, None)} else {- thread.icon_from_external_svg.clone()+ (thread.icon, thread.icon_from_external_svg.clone())};ThreadItem::new(id, title).base_bg(sidebar_bg).icon(icon)+ .when(is_draft, |this| {+ this.icon_color(Color::Custom(cx.theme().colors().icon_muted.opacity(0.2)))+ }).status(thread.status).is_remote(is_remote).when_some(icon_svg, |this, svg| {@@ -4517,10 +4512,10 @@ impl Sidebar {}).when(is_hovered && !is_running && is_draft, |this| {this.action_slot(- IconButton::new("close-draft", IconName::Close)+ IconButton::new("discard_thread", IconName::Close).icon_size(IconSize::Small).icon_color(Color::Muted)- .tooltip(Tooltip::text("Remove Draft"))+ .tooltip(Tooltip::text("Discard Draft")).on_click({let thread_workspace = thread_workspace.clone();cx.listener(move |this, _, window, cx| {diff --git a/crates/sidebar/src/sidebar_tests.rs b/crates/sidebar/src/sidebar_tests.rsindex a06af5d464b..3334cd3c1a7 100644--- a/crates/sidebar/src/sidebar_tests.rs+++ b/crates/sidebar/src/sidebar_tests.rs@@ -3247,6 +3247,58 @@ async fn test_draft_title_updates_from_editor_text(cx: &mut TestAppContext) {);}+#[gpui::test]+async fn test_thread_switcher_includes_parked_draft(cx: &mut TestAppContext) {+ let project = init_test_project_with_agent_panel("/my-project", cx).await;+ let (multi_workspace, cx) =+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));+ let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);+ cx.run_until_parked();++ save_thread_metadata(+ acp::SessionId::new(Arc::from("thread-existing")),+ Some("Existing Thread".into()),+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),+ None,+ None,+ &project,+ cx,+ );++ let connection = StubAgentConnection::new();+ agent_ui::test_support::open_draft_with_connection(&panel, connection, cx);+ cx.run_until_parked();+ let draft_id = panel.read_with(cx, |panel, cx| panel.active_thread_id(cx).unwrap());+ agent_ui::test_support::type_draft_prompt(&panel, "Fix the login bug", cx);++ panel.update_in(cx, |panel, window, cx| {+ panel.new_thread(&NewThread, window, cx);+ });+ cx.run_until_parked();++ sidebar.read_with(cx, |sidebar, _cx| {+ assert!(sidebar.contents.entries.iter().any(|entry| {+ matches!(entry, ListEntry::Thread(thread) if thread.metadata.thread_id == draft_id)+ }));+ });++ focus_sidebar(&sidebar, cx);+ sidebar.update_in(cx, |sidebar, window, cx| {+ sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx);+ });+ cx.run_until_parked();++ sidebar.read_with(cx, |sidebar, cx| {+ let switcher = sidebar+ .thread_switcher+ .as_ref()+ .expect("switcher should be open");+ assert!(switcher.read(cx).entries().iter().any(|entry| {+ matches!(entry.thread_id(), Some(thread_id) if thread_id == draft_id)+ }));+ });+}+#[gpui::test]async fn test_plus_button_reuses_empty_draft(cx: &mut TestAppContext) {// Clicking `+` when an empty draft is already active should focus itdiff --git a/crates/sidebar/src/thread_switcher.rs b/crates/sidebar/src/thread_switcher.rsindex ce3b3750685..a91ae5fe067 100644--- a/crates/sidebar/src/thread_switcher.rs+++ b/crates/sidebar/src/thread_switcher.rs@@ -1,5 +1,4 @@use action_log::DiffStats;-use agent_client_protocol::schema as acp;use agent_ui::{TerminalId, thread_metadata_store::ThreadMetadata};use gpui::{Action as _, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, Modifiers,@@ -11,7 +10,6 @@ use zed_actions::agents_sidebar::ToggleThreadSwitcher;#[derive(Clone)]pub(crate) struct ThreadSwitcherThreadEntry {- pub session_id: acp::SessionId,pub title: SharedString,pub icon: IconName,pub icon_from_external_svg: Option<SharedString>,@@ -21,6 +19,7 @@ pub(crate) struct ThreadSwitcherThreadEntry {pub project_name: Option<SharedString>,pub worktrees: Vec<ThreadItemWorktreeInfo>,pub diff_stats: DiffStats,+ pub is_draft: bool,pub is_title_generating: bool,pub notified: bool,pub timestamp: SharedString,@@ -72,9 +71,10 @@ impl ThreadSwitcherEntry {fn element_id(&self) -> SharedString {match self {- Self::Thread(entry) => {- SharedString::from(format!("thread-switcher-thread-{}", entry.session_id))- }+ Self::Thread(entry) => SharedString::from(format!(+ "thread-switcher-thread-{:?}",+ entry.metadata.thread_id+ )),Self::Terminal(entry) => {SharedString::from(format!("thread-switcher-terminal-{}", entry.terminal_id))}@@ -90,6 +90,7 @@ impl ThreadSwitcherEntry {fn icon(&self) -> IconName {match self {+ Self::Thread(entry) if entry.is_draft => IconName::Circle,Self::Thread(entry) => entry.icon,Self::Terminal(_) => IconName::Terminal,}@@ -97,6 +98,7 @@ impl ThreadSwitcherEntry {fn icon_from_external_svg(&self) -> Option<SharedString> {match self {+ Self::Thread(entry) if entry.is_draft => None,Self::Thread(entry) => entry.icon_from_external_svg.clone(),Self::Terminal(_) => None,}@@ -130,6 +132,13 @@ impl ThreadSwitcherEntry {}}+ fn is_draft(&self) -> bool {+ match self {+ Self::Thread(entry) => entry.is_draft,+ Self::Terminal(_) => false,+ }+ }+fn is_title_generating(&self) -> bool {match self {Self::Thread(entry) => entry.is_title_generating,@@ -366,6 +375,11 @@ impl Render for ThreadSwitcher {ThreadItem::new(entry.element_id(), entry.title()).rounded(true).icon(entry.icon())+ .when(entry.is_draft(), |this| {+ this.icon_color(Color::Custom(+ cx.theme().colors().icon_muted.opacity(0.2),+ ))+ }).status(entry.status()).when_some(entry.icon_from_external_svg(), |this, svg| {this.custom_icon_from_external_svg(svg)