Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

agent_ui: Inherit source agent without draft content (#58636)

Ben Brandt116e4bc18485
diff --git a/crates/agent_ui/src/agent_panel.rs b/crates/agent_ui/src/agent_panel.rs
index 25a2e783c6d..2210e0c1465 100644
--- a/crates/agent_ui/src/agent_panel.rs
+++ b/crates/agent_ui/src/agent_panel.rs
@@ -201,6 +201,11 @@ struct LastCreatedEntryKind {
entry_kind: AgentPanelEntryKind,
}
+struct SourcePanelInitialization {
+ agent: Agent,
+ initial_content: Option<AgentInitialContent>,
+}
+
/// Reads the most recently used agent across all workspaces. Used as a fallback
/// when opening a workspace that has no per-workspace agent preference yet.
fn read_global_last_used_agent(kvp: &KeyValueStore) -> Option<Agent> {
@@ -5292,17 +5297,14 @@ impl AgentPanel {
fn source_panel_initialization(
source_workspace: &WeakEntity<Workspace>,
cx: &App,
- ) -> Option<(Agent, AgentInitialContent)> {
+ ) -> Option<SourcePanelInitialization> {
let source_workspace = source_workspace.upgrade()?;
let source_panel = source_workspace.read(cx).panel::<AgentPanel>(cx)?;
let source_panel = source_panel.read(cx);
- let initial_content = source_panel.active_initial_content(cx)?;
- let agent = if source_panel.project.read(cx).is_via_collab() {
- Agent::NativeAgent
- } else {
- source_panel.selected_agent.clone()
- };
- Some((agent, initial_content))
+ Some(SourcePanelInitialization {
+ agent: source_panel.selected_agent(cx),
+ initial_content: source_panel.active_initial_content(cx),
+ })
}
pub fn initialize_from_source_workspace_if_needed(
@@ -5319,28 +5321,50 @@ impl AgentPanel {
return false;
}
- let Some((agent, initial_content)) =
- Self::source_panel_initialization(&source_workspace, cx)
- else {
+ let Some(initialization) = Self::source_panel_initialization(&source_workspace, cx) else {
return false;
};
- let thread = self.create_agent_thread_with_server(
- agent,
- None,
- None,
- None,
- None,
- Some(initial_content),
- None,
- AgentThreadSource::AgentPanel,
- window,
- cx,
- );
- self.draft_thread = Some(thread.conversation_view.clone());
- self.observe_draft_editor(&thread.conversation_view, cx);
- self.set_base_view(thread.into(), false, window, cx);
- true
+ let mut initialized = false;
+ if self.selected_agent != initialization.agent {
+ self.selected_agent = initialization.agent.clone();
+ self.serialize(cx);
+ initialized = true;
+ }
+
+ if let Some(initial_content) = initialization.initial_content {
+ let thread = self.create_agent_thread_with_server(
+ initialization.agent,
+ None,
+ None,
+ None,
+ None,
+ Some(initial_content),
+ None,
+ AgentThreadSource::AgentPanel,
+ window,
+ cx,
+ );
+ self.draft_thread = Some(thread.conversation_view.clone());
+ self.observe_draft_editor(&thread.conversation_view, cx);
+ self.set_base_view(thread.into(), false, window, cx);
+ true
+ } else {
+ if initialized
+ && matches!(
+ &self.base_view,
+ BaseView::AgentThread { conversation_view }
+ if self.draft_thread.as_ref().is_some_and(|draft| {
+ draft.entity_id() == conversation_view.entity_id()
+ })
+ )
+ {
+ self.activate_draft(false, AgentThreadSource::AgentPanel, window, cx);
+ } else if initialized {
+ cx.notify();
+ }
+ initialized
+ }
}
fn is_title_editor_focused(&self, window: &Window, cx: &Context<Self>) -> bool {
@@ -13056,6 +13080,162 @@ mod tests {
});
}
+ #[gpui::test]
+ async fn test_initialize_from_source_inherits_agent_without_draft_content(
+ cx: &mut TestAppContext,
+ ) {
+ init_test(cx);
+ cx.update(|cx| {
+ agent::ThreadStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project_a", json!({ "file.txt": "" }))
+ .await;
+ fs.insert_tree("/project_b", json!({ "file.txt": "" }))
+ .await;
+ let project_a = Project::test(fs.clone(), [Path::new("/project_a")], cx).await;
+ let project_b = Project::test(fs.clone(), [Path::new("/project_b")], cx).await;
+
+ let multi_workspace =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+
+ let workspace_a = multi_workspace
+ .read_with(cx, |mw, _cx| mw.workspace().clone())
+ .unwrap();
+
+ let workspace_b = multi_workspace
+ .update(cx, |multi_workspace, window, cx| {
+ multi_workspace.test_add_workspace(project_b.clone(), window, cx)
+ })
+ .unwrap();
+
+ let cx = &mut VisualTestContext::from_window(multi_workspace.into(), cx);
+
+ let panel_a = workspace_a.update_in(cx, |workspace, window, cx| {
+ let panel = cx.new(|cx| AgentPanel::new(workspace, window, cx));
+ workspace.add_panel(panel.clone(), window, cx);
+ panel
+ });
+
+ panel_a.update(cx, |panel, _cx| {
+ panel.selected_agent = Agent::Stub;
+ });
+
+ let panel_b = workspace_b.update_in(cx, |workspace, window, cx| {
+ let panel = cx.new(|cx| AgentPanel::new(workspace, window, cx));
+ workspace.add_panel(panel.clone(), window, cx);
+ panel
+ });
+
+ let initialized = panel_b.update_in(cx, |panel, window, cx| {
+ panel.initialize_from_source_workspace_if_needed(workspace_a.downgrade(), window, cx)
+ });
+ assert!(
+ initialized,
+ "fresh destination panel should inherit the source agent"
+ );
+
+ panel_b.read_with(cx, |panel, _cx| {
+ assert_eq!(
+ panel.selected_agent,
+ Agent::Stub,
+ "destination panel should inherit the source panel's selected agent"
+ );
+ assert!(
+ panel.active_conversation_view().is_none(),
+ "agent-only initialization should not create a draft thread"
+ );
+ });
+ }
+
+ #[gpui::test]
+ async fn test_initialize_from_source_retargets_empty_destination_draft_agent(
+ cx: &mut TestAppContext,
+ ) {
+ init_test(cx);
+ cx.update(|cx| {
+ agent::ThreadStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+ fs.insert_tree("/project_a", json!({ "file.txt": "" }))
+ .await;
+ fs.insert_tree("/project_b", json!({ "file.txt": "" }))
+ .await;
+ let project_a = Project::test(fs.clone(), [Path::new("/project_a")], cx).await;
+ let project_b = Project::test(fs.clone(), [Path::new("/project_b")], cx).await;
+
+ let multi_workspace =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+
+ let workspace_a = multi_workspace
+ .read_with(cx, |mw, _cx| mw.workspace().clone())
+ .unwrap();
+
+ let workspace_b = multi_workspace
+ .update(cx, |multi_workspace, window, cx| {
+ multi_workspace.test_add_workspace(project_b.clone(), window, cx)
+ })
+ .unwrap();
+
+ let cx = &mut VisualTestContext::from_window(multi_workspace.into(), cx);
+
+ let panel_a = workspace_a.update_in(cx, |workspace, window, cx| {
+ let panel = cx.new(|cx| AgentPanel::new(workspace, window, cx));
+ workspace.add_panel(panel.clone(), window, cx);
+ panel
+ });
+
+ panel_a.update(cx, |panel, _cx| {
+ panel.selected_agent = Agent::Stub;
+ });
+
+ let panel_b = workspace_b.update_in(cx, |workspace, window, cx| {
+ let panel = cx.new(|cx| AgentPanel::new(workspace, window, cx));
+ workspace.add_panel(panel.clone(), window, cx);
+ panel
+ });
+ panel_b.update_in(cx, |panel, window, cx| {
+ panel.activate_new_thread(false, AgentThreadSource::AgentPanel, window, cx);
+ });
+
+ let original_draft = panel_b.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,
+ "destination draft should start on the default agent"
+ );
+ draft.entity_id()
+ });
+
+ let initialized = panel_b.update_in(cx, |panel, window, cx| {
+ panel.initialize_from_source_workspace_if_needed(workspace_a.downgrade(), window, cx)
+ });
+ assert!(
+ initialized,
+ "fresh destination draft should inherit the source agent"
+ );
+
+ panel_b.read_with(cx, |panel, cx| {
+ let draft = panel.draft_thread.as_ref().expect("draft should exist");
+ assert_ne!(
+ draft.entity_id(),
+ original_draft,
+ "empty destination draft should be replaced when the inherited agent differs"
+ );
+ assert_eq!(
+ *draft.read(cx).agent_key(),
+ Agent::Stub,
+ "empty destination draft should be rebound to the inherited agent"
+ );
+ });
+ }
+
#[gpui::test]
async fn test_initialize_from_source_does_not_overwrite_existing_content(
cx: &mut TestAppContext,