agent_panel.rs

   1use std::ops::Range;
   2use std::path::Path;
   3use std::rc::Rc;
   4use std::sync::Arc;
   5
   6use acp_thread::AcpThread;
   7use agent::{ContextServerRegistry, DbThreadMetadata, HistoryEntry, HistoryStore};
   8use db::kvp::{Dismissable, KEY_VALUE_STORE};
   9use project::{
  10    ExternalAgentServerName,
  11    agent_server_store::{
  12        AgentServerCommand, AllAgentServersSettings, CLAUDE_CODE_NAME, CODEX_NAME, GEMINI_NAME,
  13    },
  14};
  15use serde::{Deserialize, Serialize};
  16use settings::{
  17    DefaultAgentView as DefaultView, LanguageModelProviderSetting, LanguageModelSelection,
  18};
  19use zed_actions::OpenBrowser;
  20use zed_actions::agent::{OpenClaudeCodeOnboardingModal, ReauthenticateAgent};
  21
  22use crate::ui::{AcpOnboardingModal, ClaudeCodeOnboardingModal};
  23use crate::{
  24    AddContextServer, AgentDiffPane, DeleteRecentlyOpenThread, Follow, InlineAssistant,
  25    NewTextThread, NewThread, OpenActiveThreadAsMarkdown, OpenAgentDiff, OpenHistory,
  26    ResetTrialEndUpsell, ResetTrialUpsell, ToggleNavigationMenu, ToggleNewThreadMenu,
  27    ToggleOptionsMenu,
  28    acp::AcpThreadView,
  29    agent_configuration::{AgentConfiguration, AssistantConfigurationEvent},
  30    slash_command::SlashCommandCompletionProvider,
  31    text_thread_editor::{AgentPanelDelegate, TextThreadEditor, make_lsp_adapter_delegate},
  32    ui::{AgentOnboardingModal, EndTrialUpsell},
  33};
  34use crate::{
  35    ExpandMessageEditor,
  36    acp::{AcpThreadHistory, ThreadHistoryEvent},
  37};
  38use crate::{
  39    ExternalAgent, NewExternalAgentThread, NewNativeAgentThreadFromSummary, placeholder_command,
  40};
  41use crate::{ManageProfiles, context_store::ContextStore};
  42use agent_settings::AgentSettings;
  43use ai_onboarding::AgentPanelOnboarding;
  44use anyhow::{Result, anyhow};
  45use assistant_slash_command::SlashCommandWorkingSet;
  46use assistant_text_thread::{TextThread, TextThreadEvent, TextThreadSummary};
  47use client::{UserStore, zed_urls};
  48use cloud_llm_client::{Plan, PlanV1, PlanV2, UsageLimit};
  49use editor::{Anchor, AnchorRangeExt as _, Editor, EditorEvent, MultiBuffer};
  50use extension::ExtensionEvents;
  51use extension_host::ExtensionStore;
  52use fs::Fs;
  53use gpui::{
  54    Action, AnyElement, App, AsyncWindowContext, Corner, DismissEvent, Entity, EventEmitter,
  55    ExternalPaths, FocusHandle, Focusable, KeyContext, Pixels, Subscription, Task, UpdateGlobal,
  56    WeakEntity, prelude::*,
  57};
  58use language::LanguageRegistry;
  59use language_model::{ConfigurationError, LanguageModelRegistry};
  60use project::{Project, ProjectPath, Worktree};
  61use prompt_store::{PromptBuilder, PromptStore, UserPromptId};
  62use rules_library::{RulesLibrary, open_rules_library};
  63use search::{BufferSearchBar, buffer_search};
  64use settings::{Settings, SettingsStore, update_settings_file};
  65use theme::ThemeSettings;
  66use ui::utils::WithRemSize;
  67use ui::{
  68    Callout, ContextMenu, ContextMenuEntry, KeyBinding, PopoverMenu, PopoverMenuHandle,
  69    ProgressBar, Tab, Tooltip, prelude::*,
  70};
  71use util::ResultExt as _;
  72use workspace::{
  73    CollaboratorId, DraggedSelection, DraggedTab, ToggleZoom, ToolbarItemView, Workspace,
  74    dock::{DockPosition, Panel, PanelEvent},
  75};
  76use zed_actions::{
  77    DecreaseBufferFontSize, IncreaseBufferFontSize, ResetBufferFontSize,
  78    agent::{
  79        OpenAcpOnboardingModal, OpenOnboardingModal, OpenSettings, ResetAgentZoom, ResetOnboarding,
  80    },
  81    assistant::{OpenRulesLibrary, ToggleFocus},
  82};
  83
  84const AGENT_PANEL_KEY: &str = "agent_panel";
  85
  86#[derive(Serialize, Deserialize, Debug)]
  87struct SerializedAgentPanel {
  88    width: Option<Pixels>,
  89    selected_agent: Option<AgentType>,
  90}
  91
  92pub fn init(cx: &mut App) {
  93    cx.observe_new(
  94        |workspace: &mut Workspace, _window, _cx: &mut Context<Workspace>| {
  95            workspace
  96                .register_action(|workspace, action: &NewThread, window, cx| {
  97                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
  98                        panel.update(cx, |panel, cx| panel.new_thread(action, window, cx));
  99                        workspace.focus_panel::<AgentPanel>(window, cx);
 100                    }
 101                })
 102                .register_action(
 103                    |workspace, action: &NewNativeAgentThreadFromSummary, window, cx| {
 104                        if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 105                            panel.update(cx, |panel, cx| {
 106                                panel.new_native_agent_thread_from_summary(action, window, cx)
 107                            });
 108                            workspace.focus_panel::<AgentPanel>(window, cx);
 109                        }
 110                    },
 111                )
 112                .register_action(|workspace, _: &ExpandMessageEditor, window, cx| {
 113                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 114                        workspace.focus_panel::<AgentPanel>(window, cx);
 115                        panel.update(cx, |panel, cx| panel.expand_message_editor(window, cx));
 116                    }
 117                })
 118                .register_action(|workspace, _: &OpenHistory, window, cx| {
 119                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 120                        workspace.focus_panel::<AgentPanel>(window, cx);
 121                        panel.update(cx, |panel, cx| panel.open_history(window, cx));
 122                    }
 123                })
 124                .register_action(|workspace, _: &OpenSettings, window, cx| {
 125                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 126                        workspace.focus_panel::<AgentPanel>(window, cx);
 127                        panel.update(cx, |panel, cx| panel.open_configuration(window, cx));
 128                    }
 129                })
 130                .register_action(|workspace, _: &NewTextThread, window, cx| {
 131                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 132                        workspace.focus_panel::<AgentPanel>(window, cx);
 133                        panel.update(cx, |panel, cx| panel.new_text_thread(window, cx));
 134                    }
 135                })
 136                .register_action(|workspace, action: &NewExternalAgentThread, window, cx| {
 137                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 138                        workspace.focus_panel::<AgentPanel>(window, cx);
 139                        panel.update(cx, |panel, cx| {
 140                            panel.external_thread(action.agent.clone(), None, None, window, cx)
 141                        });
 142                    }
 143                })
 144                .register_action(|workspace, action: &OpenRulesLibrary, window, cx| {
 145                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 146                        workspace.focus_panel::<AgentPanel>(window, cx);
 147                        panel.update(cx, |panel, cx| {
 148                            panel.deploy_rules_library(action, window, cx)
 149                        });
 150                    }
 151                })
 152                .register_action(|workspace, _: &Follow, window, cx| {
 153                    workspace.follow(CollaboratorId::Agent, window, cx);
 154                })
 155                .register_action(|workspace, _: &OpenAgentDiff, window, cx| {
 156                    let thread = workspace
 157                        .panel::<AgentPanel>(cx)
 158                        .and_then(|panel| panel.read(cx).active_thread_view().cloned())
 159                        .and_then(|thread_view| thread_view.read(cx).thread().cloned());
 160
 161                    if let Some(thread) = thread {
 162                        AgentDiffPane::deploy_in_workspace(thread, workspace, window, cx);
 163                    }
 164                })
 165                .register_action(|workspace, _: &ToggleNavigationMenu, window, cx| {
 166                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 167                        workspace.focus_panel::<AgentPanel>(window, cx);
 168                        panel.update(cx, |panel, cx| {
 169                            panel.toggle_navigation_menu(&ToggleNavigationMenu, window, cx);
 170                        });
 171                    }
 172                })
 173                .register_action(|workspace, _: &ToggleOptionsMenu, window, cx| {
 174                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 175                        workspace.focus_panel::<AgentPanel>(window, cx);
 176                        panel.update(cx, |panel, cx| {
 177                            panel.toggle_options_menu(&ToggleOptionsMenu, window, cx);
 178                        });
 179                    }
 180                })
 181                .register_action(|workspace, _: &ToggleNewThreadMenu, window, cx| {
 182                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 183                        workspace.focus_panel::<AgentPanel>(window, cx);
 184                        panel.update(cx, |panel, cx| {
 185                            panel.toggle_new_thread_menu(&ToggleNewThreadMenu, window, cx);
 186                        });
 187                    }
 188                })
 189                .register_action(|workspace, _: &OpenOnboardingModal, window, cx| {
 190                    AgentOnboardingModal::toggle(workspace, window, cx)
 191                })
 192                .register_action(|workspace, _: &OpenAcpOnboardingModal, window, cx| {
 193                    AcpOnboardingModal::toggle(workspace, window, cx)
 194                })
 195                .register_action(|workspace, _: &OpenClaudeCodeOnboardingModal, window, cx| {
 196                    ClaudeCodeOnboardingModal::toggle(workspace, window, cx)
 197                })
 198                .register_action(|_workspace, _: &ResetOnboarding, window, cx| {
 199                    window.dispatch_action(workspace::RestoreBanner.boxed_clone(), cx);
 200                    window.refresh();
 201                })
 202                .register_action(|_workspace, _: &ResetTrialUpsell, _window, cx| {
 203                    OnboardingUpsell::set_dismissed(false, cx);
 204                })
 205                .register_action(|_workspace, _: &ResetTrialEndUpsell, _window, cx| {
 206                    TrialEndUpsell::set_dismissed(false, cx);
 207                })
 208                .register_action(|workspace, _: &ResetAgentZoom, window, cx| {
 209                    if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
 210                        panel.update(cx, |panel, cx| {
 211                            panel.reset_agent_zoom(window, cx);
 212                        });
 213                    }
 214                });
 215        },
 216    )
 217    .detach();
 218}
 219
 220enum ActiveView {
 221    ExternalAgentThread {
 222        thread_view: Entity<AcpThreadView>,
 223    },
 224    TextThread {
 225        text_thread_editor: Entity<TextThreadEditor>,
 226        title_editor: Entity<Editor>,
 227        buffer_search_bar: Entity<BufferSearchBar>,
 228        _subscriptions: Vec<gpui::Subscription>,
 229    },
 230    History,
 231    Configuration,
 232}
 233
 234enum WhichFontSize {
 235    AgentFont,
 236    BufferFont,
 237    None,
 238}
 239
 240// TODO unify this with ExternalAgent
 241#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
 242pub enum AgentType {
 243    #[default]
 244    NativeAgent,
 245    TextThread,
 246    Gemini,
 247    ClaudeCode,
 248    Codex,
 249    Custom {
 250        name: SharedString,
 251        command: AgentServerCommand,
 252    },
 253}
 254
 255impl AgentType {
 256    fn label(&self) -> SharedString {
 257        match self {
 258            Self::NativeAgent | Self::TextThread => "Zed Agent".into(),
 259            Self::Gemini => "Gemini CLI".into(),
 260            Self::ClaudeCode => "Claude Code".into(),
 261            Self::Codex => "Codex".into(),
 262            Self::Custom { name, .. } => name.into(),
 263        }
 264    }
 265
 266    fn icon(&self) -> Option<IconName> {
 267        match self {
 268            Self::NativeAgent | Self::TextThread => None,
 269            Self::Gemini => Some(IconName::AiGemini),
 270            Self::ClaudeCode => Some(IconName::AiClaude),
 271            Self::Codex => Some(IconName::AiOpenAi),
 272            Self::Custom { .. } => Some(IconName::Terminal),
 273        }
 274    }
 275}
 276
 277impl From<ExternalAgent> for AgentType {
 278    fn from(value: ExternalAgent) -> Self {
 279        match value {
 280            ExternalAgent::Gemini => Self::Gemini,
 281            ExternalAgent::ClaudeCode => Self::ClaudeCode,
 282            ExternalAgent::Codex => Self::Codex,
 283            ExternalAgent::Custom { name, command } => Self::Custom { name, command },
 284            ExternalAgent::NativeAgent => Self::NativeAgent,
 285        }
 286    }
 287}
 288
 289impl ActiveView {
 290    pub fn which_font_size_used(&self) -> WhichFontSize {
 291        match self {
 292            ActiveView::ExternalAgentThread { .. } | ActiveView::History => {
 293                WhichFontSize::AgentFont
 294            }
 295            ActiveView::TextThread { .. } => WhichFontSize::BufferFont,
 296            ActiveView::Configuration => WhichFontSize::None,
 297        }
 298    }
 299
 300    pub fn native_agent(
 301        fs: Arc<dyn Fs>,
 302        prompt_store: Option<Entity<PromptStore>>,
 303        history_store: Entity<agent::HistoryStore>,
 304        project: Entity<Project>,
 305        workspace: WeakEntity<Workspace>,
 306        window: &mut Window,
 307        cx: &mut App,
 308    ) -> Self {
 309        let thread_view = cx.new(|cx| {
 310            crate::acp::AcpThreadView::new(
 311                ExternalAgent::NativeAgent.server(fs, history_store.clone()),
 312                None,
 313                None,
 314                workspace,
 315                project,
 316                history_store,
 317                prompt_store,
 318                window,
 319                cx,
 320            )
 321        });
 322
 323        Self::ExternalAgentThread { thread_view }
 324    }
 325
 326    pub fn text_thread(
 327        text_thread_editor: Entity<TextThreadEditor>,
 328        acp_history_store: Entity<agent::HistoryStore>,
 329        language_registry: Arc<LanguageRegistry>,
 330        window: &mut Window,
 331        cx: &mut App,
 332    ) -> Self {
 333        let title = text_thread_editor.read(cx).title(cx).to_string();
 334
 335        let editor = cx.new(|cx| {
 336            let mut editor = Editor::single_line(window, cx);
 337            editor.set_text(title, window, cx);
 338            editor
 339        });
 340
 341        // This is a workaround for `editor.set_text` emitting a `BufferEdited` event, which would
 342        // cause a custom summary to be set. The presence of this custom summary would cause
 343        // summarization to not happen.
 344        let mut suppress_first_edit = true;
 345
 346        let subscriptions = vec![
 347            window.subscribe(&editor, cx, {
 348                {
 349                    let text_thread_editor = text_thread_editor.clone();
 350                    move |editor, event, window, cx| match event {
 351                        EditorEvent::BufferEdited => {
 352                            if suppress_first_edit {
 353                                suppress_first_edit = false;
 354                                return;
 355                            }
 356                            let new_summary = editor.read(cx).text(cx);
 357
 358                            text_thread_editor.update(cx, |text_thread_editor, cx| {
 359                                text_thread_editor
 360                                    .text_thread()
 361                                    .update(cx, |text_thread, cx| {
 362                                        text_thread.set_custom_summary(new_summary, cx);
 363                                    })
 364                            })
 365                        }
 366                        EditorEvent::Blurred => {
 367                            if editor.read(cx).text(cx).is_empty() {
 368                                let summary = text_thread_editor
 369                                    .read(cx)
 370                                    .text_thread()
 371                                    .read(cx)
 372                                    .summary()
 373                                    .or_default();
 374
 375                                editor.update(cx, |editor, cx| {
 376                                    editor.set_text(summary, window, cx);
 377                                });
 378                            }
 379                        }
 380                        _ => {}
 381                    }
 382                }
 383            }),
 384            window.subscribe(&text_thread_editor.read(cx).text_thread().clone(), cx, {
 385                let editor = editor.clone();
 386                move |text_thread, event, window, cx| match event {
 387                    TextThreadEvent::SummaryGenerated => {
 388                        let summary = text_thread.read(cx).summary().or_default();
 389
 390                        editor.update(cx, |editor, cx| {
 391                            editor.set_text(summary, window, cx);
 392                        })
 393                    }
 394                    TextThreadEvent::PathChanged { old_path, new_path } => {
 395                        acp_history_store.update(cx, |history_store, cx| {
 396                            if let Some(old_path) = old_path {
 397                                history_store
 398                                    .replace_recently_opened_text_thread(old_path, new_path, cx);
 399                            } else {
 400                                history_store.push_recently_opened_entry(
 401                                    agent::HistoryEntryId::TextThread(new_path.clone()),
 402                                    cx,
 403                                );
 404                            }
 405                        });
 406                    }
 407                    _ => {}
 408                }
 409            }),
 410        ];
 411
 412        let buffer_search_bar =
 413            cx.new(|cx| BufferSearchBar::new(Some(language_registry), window, cx));
 414        buffer_search_bar.update(cx, |buffer_search_bar, cx| {
 415            buffer_search_bar.set_active_pane_item(Some(&text_thread_editor), window, cx)
 416        });
 417
 418        Self::TextThread {
 419            text_thread_editor,
 420            title_editor: editor,
 421            buffer_search_bar,
 422            _subscriptions: subscriptions,
 423        }
 424    }
 425}
 426
 427pub struct AgentPanel {
 428    workspace: WeakEntity<Workspace>,
 429    loading: bool,
 430    user_store: Entity<UserStore>,
 431    project: Entity<Project>,
 432    fs: Arc<dyn Fs>,
 433    language_registry: Arc<LanguageRegistry>,
 434    acp_history: Entity<AcpThreadHistory>,
 435    history_store: Entity<agent::HistoryStore>,
 436    text_thread_store: Entity<assistant_text_thread::TextThreadStore>,
 437    prompt_store: Option<Entity<PromptStore>>,
 438    context_server_registry: Entity<ContextServerRegistry>,
 439    inline_assist_context_store: Entity<ContextStore>,
 440    configuration: Option<Entity<AgentConfiguration>>,
 441    configuration_subscription: Option<Subscription>,
 442    active_view: ActiveView,
 443    previous_view: Option<ActiveView>,
 444    new_thread_menu_handle: PopoverMenuHandle<ContextMenu>,
 445    agent_panel_menu_handle: PopoverMenuHandle<ContextMenu>,
 446    agent_navigation_menu_handle: PopoverMenuHandle<ContextMenu>,
 447    agent_navigation_menu: Option<Entity<ContextMenu>>,
 448    _extension_subscription: Option<Subscription>,
 449    width: Option<Pixels>,
 450    height: Option<Pixels>,
 451    zoomed: bool,
 452    pending_serialization: Option<Task<Result<()>>>,
 453    onboarding: Entity<AgentPanelOnboarding>,
 454    selected_agent: AgentType,
 455}
 456
 457impl AgentPanel {
 458    fn serialize(&mut self, cx: &mut Context<Self>) {
 459        let width = self.width;
 460        let selected_agent = self.selected_agent.clone();
 461        self.pending_serialization = Some(cx.background_spawn(async move {
 462            KEY_VALUE_STORE
 463                .write_kvp(
 464                    AGENT_PANEL_KEY.into(),
 465                    serde_json::to_string(&SerializedAgentPanel {
 466                        width,
 467                        selected_agent: Some(selected_agent),
 468                    })?,
 469                )
 470                .await?;
 471            anyhow::Ok(())
 472        }));
 473    }
 474
 475    pub fn load(
 476        workspace: WeakEntity<Workspace>,
 477        prompt_builder: Arc<PromptBuilder>,
 478        mut cx: AsyncWindowContext,
 479    ) -> Task<Result<Entity<Self>>> {
 480        let prompt_store = cx.update(|_window, cx| PromptStore::global(cx));
 481        cx.spawn(async move |cx| {
 482            let prompt_store = match prompt_store {
 483                Ok(prompt_store) => prompt_store.await.ok(),
 484                Err(_) => None,
 485            };
 486            let serialized_panel = if let Some(panel) = cx
 487                .background_spawn(async move { KEY_VALUE_STORE.read_kvp(AGENT_PANEL_KEY) })
 488                .await
 489                .log_err()
 490                .flatten()
 491            {
 492                serde_json::from_str::<SerializedAgentPanel>(&panel).log_err()
 493            } else {
 494                None
 495            };
 496
 497            let slash_commands = Arc::new(SlashCommandWorkingSet::default());
 498            let text_thread_store = workspace
 499                .update(cx, |workspace, cx| {
 500                    let project = workspace.project().clone();
 501                    assistant_text_thread::TextThreadStore::new(
 502                        project,
 503                        prompt_builder,
 504                        slash_commands,
 505                        cx,
 506                    )
 507                })?
 508                .await?;
 509
 510            let panel = workspace.update_in(cx, |workspace, window, cx| {
 511                let panel =
 512                    cx.new(|cx| Self::new(workspace, text_thread_store, prompt_store, window, cx));
 513
 514                panel.as_mut(cx).loading = true;
 515                if let Some(serialized_panel) = serialized_panel {
 516                    panel.update(cx, |panel, cx| {
 517                        panel.width = serialized_panel.width.map(|w| w.round());
 518                        if let Some(selected_agent) = serialized_panel.selected_agent {
 519                            panel.selected_agent = selected_agent.clone();
 520                            panel.new_agent_thread(selected_agent, window, cx);
 521                        }
 522                        cx.notify();
 523                    });
 524                } else {
 525                    panel.update(cx, |panel, cx| {
 526                        panel.new_agent_thread(AgentType::NativeAgent, window, cx);
 527                    });
 528                }
 529                panel.as_mut(cx).loading = false;
 530                panel
 531            })?;
 532
 533            Ok(panel)
 534        })
 535    }
 536
 537    fn new(
 538        workspace: &Workspace,
 539        text_thread_store: Entity<assistant_text_thread::TextThreadStore>,
 540        prompt_store: Option<Entity<PromptStore>>,
 541        window: &mut Window,
 542        cx: &mut Context<Self>,
 543    ) -> Self {
 544        let fs = workspace.app_state().fs.clone();
 545        let user_store = workspace.app_state().user_store.clone();
 546        let project = workspace.project();
 547        let language_registry = project.read(cx).languages().clone();
 548        let client = workspace.client().clone();
 549        let workspace = workspace.weak_handle();
 550
 551        let inline_assist_context_store = cx.new(|_cx| ContextStore::new(project.downgrade()));
 552        let context_server_registry =
 553            cx.new(|cx| ContextServerRegistry::new(project.read(cx).context_server_store(), cx));
 554
 555        let history_store = cx.new(|cx| agent::HistoryStore::new(text_thread_store.clone(), cx));
 556        let acp_history = cx.new(|cx| AcpThreadHistory::new(history_store.clone(), window, cx));
 557        cx.subscribe_in(
 558            &acp_history,
 559            window,
 560            |this, _, event, window, cx| match event {
 561                ThreadHistoryEvent::Open(HistoryEntry::AcpThread(thread)) => {
 562                    this.external_thread(
 563                        Some(crate::ExternalAgent::NativeAgent),
 564                        Some(thread.clone()),
 565                        None,
 566                        window,
 567                        cx,
 568                    );
 569                }
 570                ThreadHistoryEvent::Open(HistoryEntry::TextThread(thread)) => {
 571                    this.open_saved_text_thread(thread.path.clone(), window, cx)
 572                        .detach_and_log_err(cx);
 573                }
 574            },
 575        )
 576        .detach();
 577
 578        let panel_type = AgentSettings::get_global(cx).default_view;
 579        let active_view = match panel_type {
 580            DefaultView::Thread => ActiveView::native_agent(
 581                fs.clone(),
 582                prompt_store.clone(),
 583                history_store.clone(),
 584                project.clone(),
 585                workspace.clone(),
 586                window,
 587                cx,
 588            ),
 589            DefaultView::TextThread => {
 590                let context = text_thread_store.update(cx, |store, cx| store.create(cx));
 591                let lsp_adapter_delegate = make_lsp_adapter_delegate(&project.clone(), cx).unwrap();
 592                let text_thread_editor = cx.new(|cx| {
 593                    let mut editor = TextThreadEditor::for_text_thread(
 594                        context,
 595                        fs.clone(),
 596                        workspace.clone(),
 597                        project.clone(),
 598                        lsp_adapter_delegate,
 599                        window,
 600                        cx,
 601                    );
 602                    editor.insert_default_prompt(window, cx);
 603                    editor
 604                });
 605                ActiveView::text_thread(
 606                    text_thread_editor,
 607                    history_store.clone(),
 608                    language_registry.clone(),
 609                    window,
 610                    cx,
 611                )
 612            }
 613        };
 614
 615        let weak_panel = cx.entity().downgrade();
 616
 617        window.defer(cx, move |window, cx| {
 618            let panel = weak_panel.clone();
 619            let agent_navigation_menu =
 620                ContextMenu::build_persistent(window, cx, move |mut menu, _window, cx| {
 621                    if let Some(panel) = panel.upgrade() {
 622                        menu = Self::populate_recently_opened_menu_section(menu, panel, cx);
 623                    }
 624                    menu.action("View All", Box::new(OpenHistory))
 625                        .end_slot_action(DeleteRecentlyOpenThread.boxed_clone())
 626                        .fixed_width(px(320.).into())
 627                        .keep_open_on_confirm(false)
 628                        .key_context("NavigationMenu")
 629                });
 630            weak_panel
 631                .update(cx, |panel, cx| {
 632                    cx.subscribe_in(
 633                        &agent_navigation_menu,
 634                        window,
 635                        |_, menu, _: &DismissEvent, window, cx| {
 636                            menu.update(cx, |menu, _| {
 637                                menu.clear_selected();
 638                            });
 639                            cx.focus_self(window);
 640                        },
 641                    )
 642                    .detach();
 643                    panel.agent_navigation_menu = Some(agent_navigation_menu);
 644                })
 645                .ok();
 646        });
 647
 648        let onboarding = cx.new(|cx| {
 649            AgentPanelOnboarding::new(
 650                user_store.clone(),
 651                client,
 652                |_window, cx| {
 653                    OnboardingUpsell::set_dismissed(true, cx);
 654                },
 655                cx,
 656            )
 657        });
 658
 659        // Subscribe to extension events to sync agent servers when extensions change
 660        let extension_subscription = if let Some(extension_events) = ExtensionEvents::try_global(cx)
 661        {
 662            Some(
 663                cx.subscribe(&extension_events, |this, _source, event, cx| match event {
 664                    extension::Event::ExtensionInstalled(_)
 665                    | extension::Event::ExtensionUninstalled(_)
 666                    | extension::Event::ExtensionsInstalledChanged => {
 667                        this.sync_agent_servers_from_extensions(cx);
 668                    }
 669                    _ => {}
 670                }),
 671            )
 672        } else {
 673            None
 674        };
 675
 676        let mut panel = Self {
 677            active_view,
 678            workspace,
 679            user_store,
 680            project: project.clone(),
 681            fs: fs.clone(),
 682            language_registry,
 683            text_thread_store,
 684            prompt_store,
 685            configuration: None,
 686            configuration_subscription: None,
 687            context_server_registry,
 688            inline_assist_context_store,
 689            previous_view: None,
 690            new_thread_menu_handle: PopoverMenuHandle::default(),
 691            agent_panel_menu_handle: PopoverMenuHandle::default(),
 692            agent_navigation_menu_handle: PopoverMenuHandle::default(),
 693            agent_navigation_menu: None,
 694            _extension_subscription: extension_subscription,
 695            width: None,
 696            height: None,
 697            zoomed: false,
 698            pending_serialization: None,
 699            onboarding,
 700            acp_history,
 701            history_store,
 702            selected_agent: AgentType::default(),
 703            loading: false,
 704        };
 705
 706        // Initial sync of agent servers from extensions
 707        panel.sync_agent_servers_from_extensions(cx);
 708        panel
 709    }
 710
 711    pub fn toggle_focus(
 712        workspace: &mut Workspace,
 713        _: &ToggleFocus,
 714        window: &mut Window,
 715        cx: &mut Context<Workspace>,
 716    ) {
 717        if workspace
 718            .panel::<Self>(cx)
 719            .is_some_and(|panel| panel.read(cx).enabled(cx))
 720        {
 721            workspace.toggle_panel_focus::<Self>(window, cx);
 722        }
 723    }
 724
 725    pub(crate) fn prompt_store(&self) -> &Option<Entity<PromptStore>> {
 726        &self.prompt_store
 727    }
 728
 729    pub(crate) fn inline_assist_context_store(&self) -> &Entity<ContextStore> {
 730        &self.inline_assist_context_store
 731    }
 732
 733    pub(crate) fn thread_store(&self) -> &Entity<HistoryStore> {
 734        &self.history_store
 735    }
 736
 737    pub(crate) fn context_server_registry(&self) -> &Entity<ContextServerRegistry> {
 738        &self.context_server_registry
 739    }
 740
 741    pub fn is_hidden(workspace: &Entity<Workspace>, cx: &App) -> bool {
 742        let workspace_read = workspace.read(cx);
 743
 744        workspace_read
 745            .panel::<AgentPanel>(cx)
 746            .map(|panel| {
 747                let panel_id = Entity::entity_id(&panel);
 748
 749                let is_visible = workspace_read.all_docks().iter().any(|dock| {
 750                    dock.read(cx)
 751                        .visible_panel()
 752                        .is_some_and(|visible_panel| visible_panel.panel_id() == panel_id)
 753                });
 754
 755                !is_visible
 756            })
 757            .unwrap_or(true)
 758    }
 759
 760    fn active_thread_view(&self) -> Option<&Entity<AcpThreadView>> {
 761        match &self.active_view {
 762            ActiveView::ExternalAgentThread { thread_view, .. } => Some(thread_view),
 763            ActiveView::TextThread { .. } | ActiveView::History | ActiveView::Configuration => None,
 764        }
 765    }
 766
 767    fn new_thread(&mut self, _action: &NewThread, window: &mut Window, cx: &mut Context<Self>) {
 768        self.new_agent_thread(AgentType::NativeAgent, window, cx);
 769    }
 770
 771    fn new_native_agent_thread_from_summary(
 772        &mut self,
 773        action: &NewNativeAgentThreadFromSummary,
 774        window: &mut Window,
 775        cx: &mut Context<Self>,
 776    ) {
 777        let Some(thread) = self
 778            .history_store
 779            .read(cx)
 780            .thread_from_session_id(&action.from_session_id)
 781        else {
 782            return;
 783        };
 784
 785        self.external_thread(
 786            Some(ExternalAgent::NativeAgent),
 787            None,
 788            Some(thread.clone()),
 789            window,
 790            cx,
 791        );
 792    }
 793
 794    fn new_text_thread(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 795        telemetry::event!("Agent Thread Started", agent = "zed-text");
 796
 797        let context = self
 798            .text_thread_store
 799            .update(cx, |context_store, cx| context_store.create(cx));
 800        let lsp_adapter_delegate = make_lsp_adapter_delegate(&self.project, cx)
 801            .log_err()
 802            .flatten();
 803
 804        let text_thread_editor = cx.new(|cx| {
 805            let mut editor = TextThreadEditor::for_text_thread(
 806                context,
 807                self.fs.clone(),
 808                self.workspace.clone(),
 809                self.project.clone(),
 810                lsp_adapter_delegate,
 811                window,
 812                cx,
 813            );
 814            editor.insert_default_prompt(window, cx);
 815            editor
 816        });
 817
 818        if self.selected_agent != AgentType::TextThread {
 819            self.selected_agent = AgentType::TextThread;
 820            self.serialize(cx);
 821        }
 822
 823        self.set_active_view(
 824            ActiveView::text_thread(
 825                text_thread_editor.clone(),
 826                self.history_store.clone(),
 827                self.language_registry.clone(),
 828                window,
 829                cx,
 830            ),
 831            window,
 832            cx,
 833        );
 834        text_thread_editor.focus_handle(cx).focus(window);
 835    }
 836
 837    fn external_thread(
 838        &mut self,
 839        agent_choice: Option<crate::ExternalAgent>,
 840        resume_thread: Option<DbThreadMetadata>,
 841        summarize_thread: Option<DbThreadMetadata>,
 842        window: &mut Window,
 843        cx: &mut Context<Self>,
 844    ) {
 845        let workspace = self.workspace.clone();
 846        let project = self.project.clone();
 847        let fs = self.fs.clone();
 848        let is_via_collab = self.project.read(cx).is_via_collab();
 849
 850        const LAST_USED_EXTERNAL_AGENT_KEY: &str = "agent_panel__last_used_external_agent";
 851
 852        #[derive(Serialize, Deserialize)]
 853        struct LastUsedExternalAgent {
 854            agent: crate::ExternalAgent,
 855        }
 856
 857        let loading = self.loading;
 858        let history = self.history_store.clone();
 859
 860        cx.spawn_in(window, async move |this, cx| {
 861            let ext_agent = match agent_choice {
 862                Some(agent) => {
 863                    cx.background_spawn({
 864                        let agent = agent.clone();
 865                        async move {
 866                            if let Some(serialized) =
 867                                serde_json::to_string(&LastUsedExternalAgent { agent }).log_err()
 868                            {
 869                                KEY_VALUE_STORE
 870                                    .write_kvp(LAST_USED_EXTERNAL_AGENT_KEY.to_string(), serialized)
 871                                    .await
 872                                    .log_err();
 873                            }
 874                        }
 875                    })
 876                    .detach();
 877
 878                    agent
 879                }
 880                None => {
 881                    if is_via_collab {
 882                        ExternalAgent::NativeAgent
 883                    } else {
 884                        cx.background_spawn(async move {
 885                            KEY_VALUE_STORE.read_kvp(LAST_USED_EXTERNAL_AGENT_KEY)
 886                        })
 887                        .await
 888                        .log_err()
 889                        .flatten()
 890                        .and_then(|value| {
 891                            serde_json::from_str::<LastUsedExternalAgent>(&value).log_err()
 892                        })
 893                        .map(|agent| agent.agent)
 894                        .unwrap_or(ExternalAgent::NativeAgent)
 895                    }
 896                }
 897            };
 898
 899            let server = ext_agent.server(fs, history);
 900
 901            if !loading {
 902                telemetry::event!("Agent Thread Started", agent = server.telemetry_id());
 903            }
 904
 905            this.update_in(cx, |this, window, cx| {
 906                let selected_agent = ext_agent.into();
 907                if this.selected_agent != selected_agent {
 908                    this.selected_agent = selected_agent;
 909                    this.serialize(cx);
 910                }
 911
 912                let thread_view = cx.new(|cx| {
 913                    crate::acp::AcpThreadView::new(
 914                        server,
 915                        resume_thread,
 916                        summarize_thread,
 917                        workspace.clone(),
 918                        project,
 919                        this.history_store.clone(),
 920                        this.prompt_store.clone(),
 921                        window,
 922                        cx,
 923                    )
 924                });
 925
 926                this.set_active_view(ActiveView::ExternalAgentThread { thread_view }, window, cx);
 927            })
 928        })
 929        .detach_and_log_err(cx);
 930    }
 931
 932    fn deploy_rules_library(
 933        &mut self,
 934        action: &OpenRulesLibrary,
 935        _window: &mut Window,
 936        cx: &mut Context<Self>,
 937    ) {
 938        open_rules_library(
 939            self.language_registry.clone(),
 940            Box::new(PromptLibraryInlineAssist::new(self.workspace.clone())),
 941            Rc::new(|| {
 942                Rc::new(SlashCommandCompletionProvider::new(
 943                    Arc::new(SlashCommandWorkingSet::default()),
 944                    None,
 945                    None,
 946                ))
 947            }),
 948            action
 949                .prompt_to_select
 950                .map(|uuid| UserPromptId(uuid).into()),
 951            cx,
 952        )
 953        .detach_and_log_err(cx);
 954    }
 955
 956    fn expand_message_editor(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 957        if let Some(thread_view) = self.active_thread_view() {
 958            thread_view.update(cx, |view, cx| {
 959                view.expand_message_editor(&ExpandMessageEditor, window, cx);
 960                view.focus_handle(cx).focus(window);
 961            });
 962        }
 963    }
 964
 965    fn open_history(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 966        if matches!(self.active_view, ActiveView::History) {
 967            if let Some(previous_view) = self.previous_view.take() {
 968                self.set_active_view(previous_view, window, cx);
 969            }
 970        } else {
 971            self.set_active_view(ActiveView::History, window, cx);
 972        }
 973        cx.notify();
 974    }
 975
 976    pub(crate) fn open_saved_text_thread(
 977        &mut self,
 978        path: Arc<Path>,
 979        window: &mut Window,
 980        cx: &mut Context<Self>,
 981    ) -> Task<Result<()>> {
 982        let text_thread_task = self
 983            .history_store
 984            .update(cx, |store, cx| store.load_text_thread(path, cx));
 985        cx.spawn_in(window, async move |this, cx| {
 986            let text_thread = text_thread_task.await?;
 987            this.update_in(cx, |this, window, cx| {
 988                this.open_text_thread(text_thread, window, cx);
 989            })
 990        })
 991    }
 992
 993    pub(crate) fn open_text_thread(
 994        &mut self,
 995        text_thread: Entity<TextThread>,
 996        window: &mut Window,
 997        cx: &mut Context<Self>,
 998    ) {
 999        let lsp_adapter_delegate = make_lsp_adapter_delegate(&self.project.clone(), cx)
1000            .log_err()
1001            .flatten();
1002        let editor = cx.new(|cx| {
1003            TextThreadEditor::for_text_thread(
1004                text_thread,
1005                self.fs.clone(),
1006                self.workspace.clone(),
1007                self.project.clone(),
1008                lsp_adapter_delegate,
1009                window,
1010                cx,
1011            )
1012        });
1013
1014        if self.selected_agent != AgentType::TextThread {
1015            self.selected_agent = AgentType::TextThread;
1016            self.serialize(cx);
1017        }
1018
1019        self.set_active_view(
1020            ActiveView::text_thread(
1021                editor,
1022                self.history_store.clone(),
1023                self.language_registry.clone(),
1024                window,
1025                cx,
1026            ),
1027            window,
1028            cx,
1029        );
1030    }
1031
1032    pub fn go_back(&mut self, _: &workspace::GoBack, window: &mut Window, cx: &mut Context<Self>) {
1033        match self.active_view {
1034            ActiveView::Configuration | ActiveView::History => {
1035                if let Some(previous_view) = self.previous_view.take() {
1036                    self.active_view = previous_view;
1037
1038                    match &self.active_view {
1039                        ActiveView::ExternalAgentThread { thread_view } => {
1040                            thread_view.focus_handle(cx).focus(window);
1041                        }
1042                        ActiveView::TextThread {
1043                            text_thread_editor, ..
1044                        } => {
1045                            text_thread_editor.focus_handle(cx).focus(window);
1046                        }
1047                        ActiveView::History | ActiveView::Configuration => {}
1048                    }
1049                }
1050                cx.notify();
1051            }
1052            _ => {}
1053        }
1054    }
1055
1056    pub fn toggle_navigation_menu(
1057        &mut self,
1058        _: &ToggleNavigationMenu,
1059        window: &mut Window,
1060        cx: &mut Context<Self>,
1061    ) {
1062        self.agent_navigation_menu_handle.toggle(window, cx);
1063    }
1064
1065    pub fn toggle_options_menu(
1066        &mut self,
1067        _: &ToggleOptionsMenu,
1068        window: &mut Window,
1069        cx: &mut Context<Self>,
1070    ) {
1071        self.agent_panel_menu_handle.toggle(window, cx);
1072    }
1073
1074    pub fn toggle_new_thread_menu(
1075        &mut self,
1076        _: &ToggleNewThreadMenu,
1077        window: &mut Window,
1078        cx: &mut Context<Self>,
1079    ) {
1080        self.new_thread_menu_handle.toggle(window, cx);
1081    }
1082
1083    pub fn increase_font_size(
1084        &mut self,
1085        action: &IncreaseBufferFontSize,
1086        _: &mut Window,
1087        cx: &mut Context<Self>,
1088    ) {
1089        self.handle_font_size_action(action.persist, px(1.0), cx);
1090    }
1091
1092    pub fn decrease_font_size(
1093        &mut self,
1094        action: &DecreaseBufferFontSize,
1095        _: &mut Window,
1096        cx: &mut Context<Self>,
1097    ) {
1098        self.handle_font_size_action(action.persist, px(-1.0), cx);
1099    }
1100
1101    fn handle_font_size_action(&mut self, persist: bool, delta: Pixels, cx: &mut Context<Self>) {
1102        match self.active_view.which_font_size_used() {
1103            WhichFontSize::AgentFont => {
1104                if persist {
1105                    update_settings_file(self.fs.clone(), cx, move |settings, cx| {
1106                        let agent_ui_font_size =
1107                            ThemeSettings::get_global(cx).agent_ui_font_size(cx) + delta;
1108                        let agent_buffer_font_size =
1109                            ThemeSettings::get_global(cx).agent_buffer_font_size(cx) + delta;
1110
1111                        let _ = settings
1112                            .theme
1113                            .agent_ui_font_size
1114                            .insert(theme::clamp_font_size(agent_ui_font_size).into());
1115                        let _ = settings
1116                            .theme
1117                            .agent_buffer_font_size
1118                            .insert(theme::clamp_font_size(agent_buffer_font_size).into());
1119                    });
1120                } else {
1121                    theme::adjust_agent_ui_font_size(cx, |size| size + delta);
1122                    theme::adjust_agent_buffer_font_size(cx, |size| size + delta);
1123                }
1124            }
1125            WhichFontSize::BufferFont => {
1126                // Prompt editor uses the buffer font size, so allow the action to propagate to the
1127                // default handler that changes that font size.
1128                cx.propagate();
1129            }
1130            WhichFontSize::None => {}
1131        }
1132    }
1133
1134    pub fn reset_font_size(
1135        &mut self,
1136        action: &ResetBufferFontSize,
1137        _: &mut Window,
1138        cx: &mut Context<Self>,
1139    ) {
1140        if action.persist {
1141            update_settings_file(self.fs.clone(), cx, move |settings, _| {
1142                settings.theme.agent_ui_font_size = None;
1143                settings.theme.agent_buffer_font_size = None;
1144            });
1145        } else {
1146            theme::reset_agent_ui_font_size(cx);
1147            theme::reset_agent_buffer_font_size(cx);
1148        }
1149    }
1150
1151    pub fn reset_agent_zoom(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
1152        theme::reset_agent_ui_font_size(cx);
1153        theme::reset_agent_buffer_font_size(cx);
1154    }
1155
1156    pub fn toggle_zoom(&mut self, _: &ToggleZoom, window: &mut Window, cx: &mut Context<Self>) {
1157        if self.zoomed {
1158            cx.emit(PanelEvent::ZoomOut);
1159        } else {
1160            if !self.focus_handle(cx).contains_focused(window, cx) {
1161                cx.focus_self(window);
1162            }
1163            cx.emit(PanelEvent::ZoomIn);
1164        }
1165    }
1166
1167    pub(crate) fn open_configuration(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1168        let agent_server_store = self.project.read(cx).agent_server_store().clone();
1169        let context_server_store = self.project.read(cx).context_server_store();
1170        let fs = self.fs.clone();
1171
1172        self.set_active_view(ActiveView::Configuration, window, cx);
1173        self.configuration = Some(cx.new(|cx| {
1174            AgentConfiguration::new(
1175                fs,
1176                agent_server_store,
1177                context_server_store,
1178                self.context_server_registry.clone(),
1179                self.language_registry.clone(),
1180                self.workspace.clone(),
1181                window,
1182                cx,
1183            )
1184        }));
1185
1186        if let Some(configuration) = self.configuration.as_ref() {
1187            self.configuration_subscription = Some(cx.subscribe_in(
1188                configuration,
1189                window,
1190                Self::handle_agent_configuration_event,
1191            ));
1192
1193            configuration.focus_handle(cx).focus(window);
1194        }
1195    }
1196
1197    pub(crate) fn open_active_thread_as_markdown(
1198        &mut self,
1199        _: &OpenActiveThreadAsMarkdown,
1200        window: &mut Window,
1201        cx: &mut Context<Self>,
1202    ) {
1203        let Some(workspace) = self.workspace.upgrade() else {
1204            return;
1205        };
1206
1207        match &self.active_view {
1208            ActiveView::ExternalAgentThread { thread_view } => {
1209                thread_view
1210                    .update(cx, |thread_view, cx| {
1211                        thread_view.open_thread_as_markdown(workspace, window, cx)
1212                    })
1213                    .detach_and_log_err(cx);
1214            }
1215            ActiveView::TextThread { .. } | ActiveView::History | ActiveView::Configuration => {}
1216        }
1217    }
1218
1219    fn handle_agent_configuration_event(
1220        &mut self,
1221        _entity: &Entity<AgentConfiguration>,
1222        event: &AssistantConfigurationEvent,
1223        window: &mut Window,
1224        cx: &mut Context<Self>,
1225    ) {
1226        match event {
1227            AssistantConfigurationEvent::NewThread(provider) => {
1228                if LanguageModelRegistry::read_global(cx)
1229                    .default_model()
1230                    .is_none_or(|model| model.provider.id() != provider.id())
1231                    && let Some(model) = provider.default_model(cx)
1232                {
1233                    update_settings_file(self.fs.clone(), cx, move |settings, _| {
1234                        let provider = model.provider_id().0.to_string();
1235                        let model = model.id().0.to_string();
1236                        settings
1237                            .agent
1238                            .get_or_insert_default()
1239                            .set_model(LanguageModelSelection {
1240                                provider: LanguageModelProviderSetting(provider),
1241                                model,
1242                            })
1243                    });
1244                }
1245
1246                self.new_thread(&NewThread, window, cx);
1247                if let Some((thread, model)) = self
1248                    .active_native_agent_thread(cx)
1249                    .zip(provider.default_model(cx))
1250                {
1251                    thread.update(cx, |thread, cx| {
1252                        thread.set_model(model, cx);
1253                    });
1254                }
1255            }
1256        }
1257    }
1258
1259    pub(crate) fn active_agent_thread(&self, cx: &App) -> Option<Entity<AcpThread>> {
1260        match &self.active_view {
1261            ActiveView::ExternalAgentThread { thread_view, .. } => {
1262                thread_view.read(cx).thread().cloned()
1263            }
1264            _ => None,
1265        }
1266    }
1267
1268    pub(crate) fn active_native_agent_thread(&self, cx: &App) -> Option<Entity<agent::Thread>> {
1269        match &self.active_view {
1270            ActiveView::ExternalAgentThread { thread_view, .. } => {
1271                thread_view.read(cx).as_native_thread(cx)
1272            }
1273            _ => None,
1274        }
1275    }
1276
1277    pub(crate) fn active_text_thread_editor(&self) -> Option<Entity<TextThreadEditor>> {
1278        match &self.active_view {
1279            ActiveView::TextThread {
1280                text_thread_editor, ..
1281            } => Some(text_thread_editor.clone()),
1282            _ => None,
1283        }
1284    }
1285
1286    fn set_active_view(
1287        &mut self,
1288        new_view: ActiveView,
1289        window: &mut Window,
1290        cx: &mut Context<Self>,
1291    ) {
1292        let current_is_history = matches!(self.active_view, ActiveView::History);
1293        let new_is_history = matches!(new_view, ActiveView::History);
1294
1295        let current_is_config = matches!(self.active_view, ActiveView::Configuration);
1296        let new_is_config = matches!(new_view, ActiveView::Configuration);
1297
1298        let current_is_special = current_is_history || current_is_config;
1299        let new_is_special = new_is_history || new_is_config;
1300
1301        match &new_view {
1302            ActiveView::TextThread {
1303                text_thread_editor, ..
1304            } => self.history_store.update(cx, |store, cx| {
1305                if let Some(path) = text_thread_editor.read(cx).text_thread().read(cx).path() {
1306                    store.push_recently_opened_entry(
1307                        agent::HistoryEntryId::TextThread(path.clone()),
1308                        cx,
1309                    )
1310                }
1311            }),
1312            ActiveView::ExternalAgentThread { .. } => {}
1313            ActiveView::History | ActiveView::Configuration => {}
1314        }
1315
1316        if current_is_special && !new_is_special {
1317            self.active_view = new_view;
1318        } else if !current_is_special && new_is_special {
1319            self.previous_view = Some(std::mem::replace(&mut self.active_view, new_view));
1320        } else {
1321            if !new_is_special {
1322                self.previous_view = None;
1323            }
1324            self.active_view = new_view;
1325        }
1326
1327        self.focus_handle(cx).focus(window);
1328    }
1329
1330    fn populate_recently_opened_menu_section(
1331        mut menu: ContextMenu,
1332        panel: Entity<Self>,
1333        cx: &mut Context<ContextMenu>,
1334    ) -> ContextMenu {
1335        let entries = panel
1336            .read(cx)
1337            .history_store
1338            .read(cx)
1339            .recently_opened_entries(cx);
1340
1341        if entries.is_empty() {
1342            return menu;
1343        }
1344
1345        menu = menu.header("Recently Opened");
1346
1347        for entry in entries {
1348            let title = entry.title().clone();
1349
1350            menu = menu.entry_with_end_slot_on_hover(
1351                title,
1352                None,
1353                {
1354                    let panel = panel.downgrade();
1355                    let entry = entry.clone();
1356                    move |window, cx| {
1357                        let entry = entry.clone();
1358                        panel
1359                            .update(cx, move |this, cx| match &entry {
1360                                agent::HistoryEntry::AcpThread(entry) => this.external_thread(
1361                                    Some(ExternalAgent::NativeAgent),
1362                                    Some(entry.clone()),
1363                                    None,
1364                                    window,
1365                                    cx,
1366                                ),
1367                                agent::HistoryEntry::TextThread(entry) => this
1368                                    .open_saved_text_thread(entry.path.clone(), window, cx)
1369                                    .detach_and_log_err(cx),
1370                            })
1371                            .ok();
1372                    }
1373                },
1374                IconName::Close,
1375                "Close Entry".into(),
1376                {
1377                    let panel = panel.downgrade();
1378                    let id = entry.id();
1379                    move |_window, cx| {
1380                        panel
1381                            .update(cx, |this, cx| {
1382                                this.history_store.update(cx, |history_store, cx| {
1383                                    history_store.remove_recently_opened_entry(&id, cx);
1384                                });
1385                            })
1386                            .ok();
1387                    }
1388                },
1389            );
1390        }
1391
1392        menu = menu.separator();
1393
1394        menu
1395    }
1396
1397    pub fn selected_agent(&self) -> AgentType {
1398        self.selected_agent.clone()
1399    }
1400
1401    fn sync_agent_servers_from_extensions(&mut self, cx: &mut Context<Self>) {
1402        if let Some(extension_store) = ExtensionStore::try_global(cx) {
1403            let (manifests, extensions_dir) = {
1404                let store = extension_store.read(cx);
1405                let installed = store.installed_extensions();
1406                let manifests: Vec<_> = installed
1407                    .iter()
1408                    .map(|(id, entry)| (id.clone(), entry.manifest.clone()))
1409                    .collect();
1410                let extensions_dir = paths::extensions_dir().join("installed");
1411                (manifests, extensions_dir)
1412            };
1413
1414            self.project.update(cx, |project, cx| {
1415                project.agent_server_store().update(cx, |store, cx| {
1416                    let manifest_refs: Vec<_> = manifests
1417                        .iter()
1418                        .map(|(id, manifest)| (id.as_ref(), manifest.as_ref()))
1419                        .collect();
1420                    store.sync_extension_agents(manifest_refs, extensions_dir, cx);
1421                });
1422            });
1423        }
1424    }
1425
1426    pub fn new_agent_thread(
1427        &mut self,
1428        agent: AgentType,
1429        window: &mut Window,
1430        cx: &mut Context<Self>,
1431    ) {
1432        match agent {
1433            AgentType::TextThread => {
1434                window.dispatch_action(NewTextThread.boxed_clone(), cx);
1435            }
1436            AgentType::NativeAgent => self.external_thread(
1437                Some(crate::ExternalAgent::NativeAgent),
1438                None,
1439                None,
1440                window,
1441                cx,
1442            ),
1443            AgentType::Gemini => {
1444                self.external_thread(Some(crate::ExternalAgent::Gemini), None, None, window, cx)
1445            }
1446            AgentType::ClaudeCode => {
1447                self.selected_agent = AgentType::ClaudeCode;
1448                self.serialize(cx);
1449                self.external_thread(
1450                    Some(crate::ExternalAgent::ClaudeCode),
1451                    None,
1452                    None,
1453                    window,
1454                    cx,
1455                )
1456            }
1457            AgentType::Codex => {
1458                self.selected_agent = AgentType::Codex;
1459                self.serialize(cx);
1460                self.external_thread(Some(crate::ExternalAgent::Codex), None, None, window, cx)
1461            }
1462            AgentType::Custom { name, command } => self.external_thread(
1463                Some(crate::ExternalAgent::Custom { name, command }),
1464                None,
1465                None,
1466                window,
1467                cx,
1468            ),
1469        }
1470    }
1471
1472    pub fn load_agent_thread(
1473        &mut self,
1474        thread: DbThreadMetadata,
1475        window: &mut Window,
1476        cx: &mut Context<Self>,
1477    ) {
1478        self.external_thread(
1479            Some(ExternalAgent::NativeAgent),
1480            Some(thread),
1481            None,
1482            window,
1483            cx,
1484        );
1485    }
1486}
1487
1488impl Focusable for AgentPanel {
1489    fn focus_handle(&self, cx: &App) -> FocusHandle {
1490        match &self.active_view {
1491            ActiveView::ExternalAgentThread { thread_view, .. } => thread_view.focus_handle(cx),
1492            ActiveView::History => self.acp_history.focus_handle(cx),
1493            ActiveView::TextThread {
1494                text_thread_editor, ..
1495            } => text_thread_editor.focus_handle(cx),
1496            ActiveView::Configuration => {
1497                if let Some(configuration) = self.configuration.as_ref() {
1498                    configuration.focus_handle(cx)
1499                } else {
1500                    cx.focus_handle()
1501                }
1502            }
1503        }
1504    }
1505}
1506
1507fn agent_panel_dock_position(cx: &App) -> DockPosition {
1508    AgentSettings::get_global(cx).dock.into()
1509}
1510
1511impl EventEmitter<PanelEvent> for AgentPanel {}
1512
1513impl Panel for AgentPanel {
1514    fn persistent_name() -> &'static str {
1515        "AgentPanel"
1516    }
1517
1518    fn panel_key() -> &'static str {
1519        AGENT_PANEL_KEY
1520    }
1521
1522    fn position(&self, _window: &Window, cx: &App) -> DockPosition {
1523        agent_panel_dock_position(cx)
1524    }
1525
1526    fn position_is_valid(&self, position: DockPosition) -> bool {
1527        position != DockPosition::Bottom
1528    }
1529
1530    fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
1531        settings::update_settings_file(self.fs.clone(), cx, move |settings, _| {
1532            settings
1533                .agent
1534                .get_or_insert_default()
1535                .set_dock(position.into());
1536        });
1537    }
1538
1539    fn size(&self, window: &Window, cx: &App) -> Pixels {
1540        let settings = AgentSettings::get_global(cx);
1541        match self.position(window, cx) {
1542            DockPosition::Left | DockPosition::Right => {
1543                self.width.unwrap_or(settings.default_width)
1544            }
1545            DockPosition::Bottom => self.height.unwrap_or(settings.default_height),
1546        }
1547    }
1548
1549    fn set_size(&mut self, size: Option<Pixels>, window: &mut Window, cx: &mut Context<Self>) {
1550        match self.position(window, cx) {
1551            DockPosition::Left | DockPosition::Right => self.width = size,
1552            DockPosition::Bottom => self.height = size,
1553        }
1554        self.serialize(cx);
1555        cx.notify();
1556    }
1557
1558    fn set_active(&mut self, _active: bool, _window: &mut Window, _cx: &mut Context<Self>) {}
1559
1560    fn remote_id() -> Option<proto::PanelId> {
1561        Some(proto::PanelId::AssistantPanel)
1562    }
1563
1564    fn icon(&self, _window: &Window, cx: &App) -> Option<IconName> {
1565        (self.enabled(cx) && AgentSettings::get_global(cx).button).then_some(IconName::ZedAssistant)
1566    }
1567
1568    fn icon_tooltip(&self, _window: &Window, _cx: &App) -> Option<&'static str> {
1569        Some("Agent Panel")
1570    }
1571
1572    fn toggle_action(&self) -> Box<dyn Action> {
1573        Box::new(ToggleFocus)
1574    }
1575
1576    fn activation_priority(&self) -> u32 {
1577        3
1578    }
1579
1580    fn enabled(&self, cx: &App) -> bool {
1581        AgentSettings::get_global(cx).enabled(cx)
1582    }
1583
1584    fn is_zoomed(&self, _window: &Window, _cx: &App) -> bool {
1585        self.zoomed
1586    }
1587
1588    fn set_zoomed(&mut self, zoomed: bool, _window: &mut Window, cx: &mut Context<Self>) {
1589        self.zoomed = zoomed;
1590        cx.notify();
1591    }
1592}
1593
1594impl AgentPanel {
1595    fn render_title_view(&self, _window: &mut Window, cx: &Context<Self>) -> AnyElement {
1596        const LOADING_SUMMARY_PLACEHOLDER: &str = "Loading Summary…";
1597
1598        let content = match &self.active_view {
1599            ActiveView::ExternalAgentThread { thread_view } => {
1600                if let Some(title_editor) = thread_view.read(cx).title_editor() {
1601                    div()
1602                        .w_full()
1603                        .on_action({
1604                            let thread_view = thread_view.downgrade();
1605                            move |_: &menu::Confirm, window, cx| {
1606                                if let Some(thread_view) = thread_view.upgrade() {
1607                                    thread_view.focus_handle(cx).focus(window);
1608                                }
1609                            }
1610                        })
1611                        .on_action({
1612                            let thread_view = thread_view.downgrade();
1613                            move |_: &editor::actions::Cancel, window, cx| {
1614                                if let Some(thread_view) = thread_view.upgrade() {
1615                                    thread_view.focus_handle(cx).focus(window);
1616                                }
1617                            }
1618                        })
1619                        .child(title_editor)
1620                        .into_any_element()
1621                } else {
1622                    Label::new(thread_view.read(cx).title(cx))
1623                        .color(Color::Muted)
1624                        .truncate()
1625                        .into_any_element()
1626                }
1627            }
1628            ActiveView::TextThread {
1629                title_editor,
1630                text_thread_editor,
1631                ..
1632            } => {
1633                let summary = text_thread_editor.read(cx).text_thread().read(cx).summary();
1634
1635                match summary {
1636                    TextThreadSummary::Pending => Label::new(TextThreadSummary::DEFAULT)
1637                        .color(Color::Muted)
1638                        .truncate()
1639                        .into_any_element(),
1640                    TextThreadSummary::Content(summary) => {
1641                        if summary.done {
1642                            div()
1643                                .w_full()
1644                                .child(title_editor.clone())
1645                                .into_any_element()
1646                        } else {
1647                            Label::new(LOADING_SUMMARY_PLACEHOLDER)
1648                                .truncate()
1649                                .color(Color::Muted)
1650                                .into_any_element()
1651                        }
1652                    }
1653                    TextThreadSummary::Error => h_flex()
1654                        .w_full()
1655                        .child(title_editor.clone())
1656                        .child(
1657                            IconButton::new("retry-summary-generation", IconName::RotateCcw)
1658                                .icon_size(IconSize::Small)
1659                                .on_click({
1660                                    let text_thread_editor = text_thread_editor.clone();
1661                                    move |_, _window, cx| {
1662                                        text_thread_editor.update(cx, |text_thread_editor, cx| {
1663                                            text_thread_editor.regenerate_summary(cx);
1664                                        });
1665                                    }
1666                                })
1667                                .tooltip(move |_window, cx| {
1668                                    cx.new(|_| {
1669                                        Tooltip::new("Failed to generate title")
1670                                            .meta("Click to try again")
1671                                    })
1672                                    .into()
1673                                }),
1674                        )
1675                        .into_any_element(),
1676                }
1677            }
1678            ActiveView::History => Label::new("History").truncate().into_any_element(),
1679            ActiveView::Configuration => Label::new("Settings").truncate().into_any_element(),
1680        };
1681
1682        h_flex()
1683            .key_context("TitleEditor")
1684            .id("TitleEditor")
1685            .flex_grow()
1686            .w_full()
1687            .max_w_full()
1688            .overflow_x_scroll()
1689            .child(content)
1690            .into_any()
1691    }
1692
1693    fn render_panel_options_menu(
1694        &self,
1695        window: &mut Window,
1696        cx: &mut Context<Self>,
1697    ) -> impl IntoElement {
1698        let user_store = self.user_store.read(cx);
1699        let usage = user_store.model_request_usage();
1700        let account_url = zed_urls::account_url(cx);
1701
1702        let focus_handle = self.focus_handle(cx);
1703
1704        let full_screen_label = if self.is_zoomed(window, cx) {
1705            "Disable Full Screen"
1706        } else {
1707            "Enable Full Screen"
1708        };
1709
1710        let selected_agent = self.selected_agent.clone();
1711
1712        PopoverMenu::new("agent-options-menu")
1713            .trigger_with_tooltip(
1714                IconButton::new("agent-options-menu", IconName::Ellipsis)
1715                    .icon_size(IconSize::Small),
1716                {
1717                    let focus_handle = focus_handle.clone();
1718                    move |_window, cx| {
1719                        Tooltip::for_action_in(
1720                            "Toggle Agent Menu",
1721                            &ToggleOptionsMenu,
1722                            &focus_handle,
1723                            cx,
1724                        )
1725                    }
1726                },
1727            )
1728            .anchor(Corner::TopRight)
1729            .with_handle(self.agent_panel_menu_handle.clone())
1730            .menu({
1731                move |window, cx| {
1732                    Some(ContextMenu::build(window, cx, |mut menu, _window, _| {
1733                        menu = menu.context(focus_handle.clone());
1734                        if let Some(usage) = usage {
1735                            menu = menu
1736                                .header_with_link("Prompt Usage", "Manage", account_url.clone())
1737                                .custom_entry(
1738                                    move |_window, cx| {
1739                                        let used_percentage = match usage.limit {
1740                                            UsageLimit::Limited(limit) => {
1741                                                Some((usage.amount as f32 / limit as f32) * 100.)
1742                                            }
1743                                            UsageLimit::Unlimited => None,
1744                                        };
1745
1746                                        h_flex()
1747                                            .flex_1()
1748                                            .gap_1p5()
1749                                            .children(used_percentage.map(|percent| {
1750                                                ProgressBar::new("usage", percent, 100., cx)
1751                                            }))
1752                                            .child(
1753                                                Label::new(match usage.limit {
1754                                                    UsageLimit::Limited(limit) => {
1755                                                        format!("{} / {limit}", usage.amount)
1756                                                    }
1757                                                    UsageLimit::Unlimited => {
1758                                                        format!("{} / ∞", usage.amount)
1759                                                    }
1760                                                })
1761                                                .size(LabelSize::Small)
1762                                                .color(Color::Muted),
1763                                            )
1764                                            .into_any_element()
1765                                    },
1766                                    move |_, cx| cx.open_url(&zed_urls::account_url(cx)),
1767                                )
1768                                .separator()
1769                        }
1770
1771                        menu = menu
1772                            .header("MCP Servers")
1773                            .action(
1774                                "View Server Extensions",
1775                                Box::new(zed_actions::Extensions {
1776                                    category_filter: Some(
1777                                        zed_actions::ExtensionCategoryFilter::ContextServers,
1778                                    ),
1779                                    id: None,
1780                                }),
1781                            )
1782                            .action("Add Custom Server…", Box::new(AddContextServer))
1783                            .separator()
1784                            .action("Rules", Box::new(OpenRulesLibrary::default()))
1785                            .action("Profiles", Box::new(ManageProfiles::default()))
1786                            .action("Settings", Box::new(OpenSettings))
1787                            .separator()
1788                            .action(full_screen_label, Box::new(ToggleZoom));
1789
1790                        if selected_agent == AgentType::Gemini {
1791                            menu = menu.action("Reauthenticate", Box::new(ReauthenticateAgent))
1792                        }
1793
1794                        menu
1795                    }))
1796                }
1797            })
1798    }
1799
1800    fn render_recent_entries_menu(
1801        &self,
1802        icon: IconName,
1803        corner: Corner,
1804        cx: &mut Context<Self>,
1805    ) -> impl IntoElement {
1806        let focus_handle = self.focus_handle(cx);
1807
1808        PopoverMenu::new("agent-nav-menu")
1809            .trigger_with_tooltip(
1810                IconButton::new("agent-nav-menu", icon).icon_size(IconSize::Small),
1811                {
1812                    move |_window, cx| {
1813                        Tooltip::for_action_in(
1814                            "Toggle Recent Threads",
1815                            &ToggleNavigationMenu,
1816                            &focus_handle,
1817                            cx,
1818                        )
1819                    }
1820                },
1821            )
1822            .anchor(corner)
1823            .with_handle(self.agent_navigation_menu_handle.clone())
1824            .menu({
1825                let menu = self.agent_navigation_menu.clone();
1826                move |window, cx| {
1827                    telemetry::event!("View Thread History Clicked");
1828
1829                    if let Some(menu) = menu.as_ref() {
1830                        menu.update(cx, |_, cx| {
1831                            cx.defer_in(window, |menu, window, cx| {
1832                                menu.rebuild(window, cx);
1833                            });
1834                        })
1835                    }
1836                    menu.clone()
1837                }
1838            })
1839    }
1840
1841    fn render_toolbar_back_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
1842        let focus_handle = self.focus_handle(cx);
1843
1844        IconButton::new("go-back", IconName::ArrowLeft)
1845            .icon_size(IconSize::Small)
1846            .on_click(cx.listener(|this, _, window, cx| {
1847                this.go_back(&workspace::GoBack, window, cx);
1848            }))
1849            .tooltip({
1850                move |_window, cx| {
1851                    Tooltip::for_action_in("Go Back", &workspace::GoBack, &focus_handle, cx)
1852                }
1853            })
1854    }
1855
1856    fn render_toolbar(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1857        let agent_server_store = self.project.read(cx).agent_server_store().clone();
1858        let focus_handle = self.focus_handle(cx);
1859
1860        // Get custom icon path for selected agent before building menu (to avoid borrow issues)
1861        let selected_agent_custom_icon =
1862            if let AgentType::Custom { name, .. } = &self.selected_agent {
1863                agent_server_store
1864                    .read(cx)
1865                    .agent_icon(&ExternalAgentServerName(name.clone()))
1866            } else {
1867                None
1868            };
1869
1870        let active_thread = match &self.active_view {
1871            ActiveView::ExternalAgentThread { thread_view } => {
1872                thread_view.read(cx).as_native_thread(cx)
1873            }
1874            ActiveView::TextThread { .. } | ActiveView::History | ActiveView::Configuration => None,
1875        };
1876
1877        let new_thread_menu = PopoverMenu::new("new_thread_menu")
1878            .trigger_with_tooltip(
1879                IconButton::new("new_thread_menu_btn", IconName::Plus).icon_size(IconSize::Small),
1880                {
1881                    let focus_handle = focus_handle.clone();
1882                    move |_window, cx| {
1883                        Tooltip::for_action_in("New…", &ToggleNewThreadMenu, &focus_handle, cx)
1884                    }
1885                },
1886            )
1887            .anchor(Corner::TopRight)
1888            .with_handle(self.new_thread_menu_handle.clone())
1889            .menu({
1890                let workspace = self.workspace.clone();
1891                let is_via_collab = workspace
1892                    .update(cx, |workspace, cx| {
1893                        workspace.project().read(cx).is_via_collab()
1894                    })
1895                    .unwrap_or_default();
1896
1897                move |window, cx| {
1898                    telemetry::event!("New Thread Clicked");
1899
1900                    let active_thread = active_thread.clone();
1901                    Some(ContextMenu::build(window, cx, |menu, _window, cx| {
1902                        menu.context(focus_handle.clone())
1903                            .header("Zed Agent")
1904                            .when_some(active_thread, |this, active_thread| {
1905                                let thread = active_thread.read(cx);
1906
1907                                if !thread.is_empty() {
1908                                    let session_id = thread.id().clone();
1909                                    this.item(
1910                                        ContextMenuEntry::new("New From Summary")
1911                                            .icon(IconName::ThreadFromSummary)
1912                                            .icon_color(Color::Muted)
1913                                            .handler(move |window, cx| {
1914                                                window.dispatch_action(
1915                                                    Box::new(NewNativeAgentThreadFromSummary {
1916                                                        from_session_id: session_id.clone(),
1917                                                    }),
1918                                                    cx,
1919                                                );
1920                                            }),
1921                                    )
1922                                } else {
1923                                    this
1924                                }
1925                            })
1926                            .item(
1927                                ContextMenuEntry::new("New Thread")
1928                                    .action(NewThread.boxed_clone())
1929                                    .icon(IconName::Thread)
1930                                    .icon_color(Color::Muted)
1931                                    .handler({
1932                                        let workspace = workspace.clone();
1933                                        move |window, cx| {
1934                                            if let Some(workspace) = workspace.upgrade() {
1935                                                workspace.update(cx, |workspace, cx| {
1936                                                    if let Some(panel) =
1937                                                        workspace.panel::<AgentPanel>(cx)
1938                                                    {
1939                                                        panel.update(cx, |panel, cx| {
1940                                                            panel.new_agent_thread(
1941                                                                AgentType::NativeAgent,
1942                                                                window,
1943                                                                cx,
1944                                                            );
1945                                                        });
1946                                                    }
1947                                                });
1948                                            }
1949                                        }
1950                                    }),
1951                            )
1952                            .item(
1953                                ContextMenuEntry::new("New Text Thread")
1954                                    .icon(IconName::TextThread)
1955                                    .icon_color(Color::Muted)
1956                                    .action(NewTextThread.boxed_clone())
1957                                    .handler({
1958                                        let workspace = workspace.clone();
1959                                        move |window, cx| {
1960                                            if let Some(workspace) = workspace.upgrade() {
1961                                                workspace.update(cx, |workspace, cx| {
1962                                                    if let Some(panel) =
1963                                                        workspace.panel::<AgentPanel>(cx)
1964                                                    {
1965                                                        panel.update(cx, |panel, cx| {
1966                                                            panel.new_agent_thread(
1967                                                                AgentType::TextThread,
1968                                                                window,
1969                                                                cx,
1970                                                            );
1971                                                        });
1972                                                    }
1973                                                });
1974                                            }
1975                                        }
1976                                    }),
1977                            )
1978                            .separator()
1979                            .header("External Agents")
1980                            .item(
1981                                ContextMenuEntry::new("New Claude Code Thread")
1982                                    .icon(IconName::AiClaude)
1983                                    .disabled(is_via_collab)
1984                                    .icon_color(Color::Muted)
1985                                    .handler({
1986                                        let workspace = workspace.clone();
1987                                        move |window, cx| {
1988                                            if let Some(workspace) = workspace.upgrade() {
1989                                                workspace.update(cx, |workspace, cx| {
1990                                                    if let Some(panel) =
1991                                                        workspace.panel::<AgentPanel>(cx)
1992                                                    {
1993                                                        panel.update(cx, |panel, cx| {
1994                                                            panel.new_agent_thread(
1995                                                                AgentType::ClaudeCode,
1996                                                                window,
1997                                                                cx,
1998                                                            );
1999                                                        });
2000                                                    }
2001                                                });
2002                                            }
2003                                        }
2004                                    }),
2005                            )
2006                            .item(
2007                                ContextMenuEntry::new("New Codex Thread")
2008                                    .icon(IconName::AiOpenAi)
2009                                    .disabled(is_via_collab)
2010                                    .icon_color(Color::Muted)
2011                                    .handler({
2012                                        let workspace = workspace.clone();
2013                                        move |window, cx| {
2014                                            if let Some(workspace) = workspace.upgrade() {
2015                                                workspace.update(cx, |workspace, cx| {
2016                                                    if let Some(panel) =
2017                                                        workspace.panel::<AgentPanel>(cx)
2018                                                    {
2019                                                        panel.update(cx, |panel, cx| {
2020                                                            panel.new_agent_thread(
2021                                                                AgentType::Codex,
2022                                                                window,
2023                                                                cx,
2024                                                            );
2025                                                        });
2026                                                    }
2027                                                });
2028                                            }
2029                                        }
2030                                    }),
2031                            )
2032                            .item(
2033                                ContextMenuEntry::new("New Gemini CLI Thread")
2034                                    .icon(IconName::AiGemini)
2035                                    .icon_color(Color::Muted)
2036                                    .disabled(is_via_collab)
2037                                    .handler({
2038                                        let workspace = workspace.clone();
2039                                        move |window, cx| {
2040                                            if let Some(workspace) = workspace.upgrade() {
2041                                                workspace.update(cx, |workspace, cx| {
2042                                                    if let Some(panel) =
2043                                                        workspace.panel::<AgentPanel>(cx)
2044                                                    {
2045                                                        panel.update(cx, |panel, cx| {
2046                                                            panel.new_agent_thread(
2047                                                                AgentType::Gemini,
2048                                                                window,
2049                                                                cx,
2050                                                            );
2051                                                        });
2052                                                    }
2053                                                });
2054                                            }
2055                                        }
2056                                    }),
2057                            )
2058                            .map(|mut menu| {
2059                                let agent_server_store_read = agent_server_store.read(cx);
2060                                let agent_names = agent_server_store_read
2061                                    .external_agents()
2062                                    .filter(|name| {
2063                                        name.0 != GEMINI_NAME
2064                                            && name.0 != CLAUDE_CODE_NAME
2065                                            && name.0 != CODEX_NAME
2066                                    })
2067                                    .cloned()
2068                                    .collect::<Vec<_>>();
2069                                let custom_settings = cx
2070                                    .global::<SettingsStore>()
2071                                    .get::<AllAgentServersSettings>(None)
2072                                    .custom
2073                                    .clone();
2074                                for agent_name in agent_names {
2075                                    let icon_path = agent_server_store_read.agent_icon(&agent_name);
2076                                    let mut entry =
2077                                        ContextMenuEntry::new(format!("New {} Thread", agent_name));
2078                                    if let Some(icon_path) = icon_path {
2079                                        entry = entry.custom_icon_path(icon_path);
2080                                    } else {
2081                                        entry = entry.icon(IconName::Terminal);
2082                                    }
2083                                    entry = entry
2084                                        .icon_color(Color::Muted)
2085                                        .disabled(is_via_collab)
2086                                        .handler({
2087                                            let workspace = workspace.clone();
2088                                            let agent_name = agent_name.clone();
2089                                            let custom_settings = custom_settings.clone();
2090                                            move |window, cx| {
2091                                                if let Some(workspace) = workspace.upgrade() {
2092                                                    workspace.update(cx, |workspace, cx| {
2093                                                        if let Some(panel) =
2094                                                            workspace.panel::<AgentPanel>(cx)
2095                                                        {
2096                                                            panel.update(cx, |panel, cx| {
2097                                                                panel.new_agent_thread(
2098                                                                    AgentType::Custom {
2099                                                                        name: agent_name
2100                                                                            .clone()
2101                                                                            .into(),
2102                                                                        command: custom_settings
2103                                                                            .get(&agent_name.0)
2104                                                                            .map(|settings| {
2105                                                                                settings
2106                                                                                    .command
2107                                                                                    .clone()
2108                                                                            })
2109                                                                            .unwrap_or(
2110                                                                                placeholder_command(
2111                                                                                ),
2112                                                                            ),
2113                                                                    },
2114                                                                    window,
2115                                                                    cx,
2116                                                                );
2117                                                            });
2118                                                        }
2119                                                    });
2120                                                }
2121                                            }
2122                                        });
2123                                    menu = menu.item(entry);
2124                                }
2125
2126                                menu
2127                            })
2128                            .separator()
2129                            .link(
2130                                "Add Other Agents",
2131                                OpenBrowser {
2132                                    url: zed_urls::external_agents_docs(cx),
2133                                }
2134                                .boxed_clone(),
2135                            )
2136                    }))
2137                }
2138            });
2139
2140        let selected_agent_label = self.selected_agent.label();
2141
2142        let has_custom_icon = selected_agent_custom_icon.is_some();
2143        let selected_agent = div()
2144            .id("selected_agent_icon")
2145            .when_some(selected_agent_custom_icon, |this, icon_path| {
2146                let label = selected_agent_label.clone();
2147                this.px(DynamicSpacing::Base02.rems(cx))
2148                    .child(Icon::from_path(icon_path).color(Color::Muted))
2149                    .tooltip(move |_window, cx| {
2150                        Tooltip::with_meta(label.clone(), None, "Selected Agent", cx)
2151                    })
2152            })
2153            .when(!has_custom_icon, |this| {
2154                this.when_some(self.selected_agent.icon(), |this, icon| {
2155                    let label = selected_agent_label.clone();
2156                    this.px(DynamicSpacing::Base02.rems(cx))
2157                        .child(Icon::new(icon).color(Color::Muted))
2158                        .tooltip(move |_window, cx| {
2159                            Tooltip::with_meta(label.clone(), None, "Selected Agent", cx)
2160                        })
2161                })
2162            })
2163            .into_any_element();
2164
2165        h_flex()
2166            .id("agent-panel-toolbar")
2167            .h(Tab::container_height(cx))
2168            .max_w_full()
2169            .flex_none()
2170            .justify_between()
2171            .gap_2()
2172            .bg(cx.theme().colors().tab_bar_background)
2173            .border_b_1()
2174            .border_color(cx.theme().colors().border)
2175            .child(
2176                h_flex()
2177                    .size_full()
2178                    .gap(DynamicSpacing::Base04.rems(cx))
2179                    .pl(DynamicSpacing::Base04.rems(cx))
2180                    .child(match &self.active_view {
2181                        ActiveView::History | ActiveView::Configuration => {
2182                            self.render_toolbar_back_button(cx).into_any_element()
2183                        }
2184                        _ => selected_agent.into_any_element(),
2185                    })
2186                    .child(self.render_title_view(window, cx)),
2187            )
2188            .child(
2189                h_flex()
2190                    .flex_none()
2191                    .gap(DynamicSpacing::Base02.rems(cx))
2192                    .pl(DynamicSpacing::Base04.rems(cx))
2193                    .pr(DynamicSpacing::Base06.rems(cx))
2194                    .child(new_thread_menu)
2195                    .child(self.render_recent_entries_menu(
2196                        IconName::MenuAltTemp,
2197                        Corner::TopRight,
2198                        cx,
2199                    ))
2200                    .child(self.render_panel_options_menu(window, cx)),
2201            )
2202    }
2203
2204    fn should_render_trial_end_upsell(&self, cx: &mut Context<Self>) -> bool {
2205        if TrialEndUpsell::dismissed() {
2206            return false;
2207        }
2208
2209        match &self.active_view {
2210            ActiveView::TextThread { .. } => {
2211                if LanguageModelRegistry::global(cx)
2212                    .read(cx)
2213                    .default_model()
2214                    .is_some_and(|model| {
2215                        model.provider.id() != language_model::ZED_CLOUD_PROVIDER_ID
2216                    })
2217                {
2218                    return false;
2219                }
2220            }
2221            ActiveView::ExternalAgentThread { .. }
2222            | ActiveView::History
2223            | ActiveView::Configuration => return false,
2224        }
2225
2226        let plan = self.user_store.read(cx).plan();
2227        let has_previous_trial = self.user_store.read(cx).trial_started_at().is_some();
2228
2229        matches!(
2230            plan,
2231            Some(Plan::V1(PlanV1::ZedFree) | Plan::V2(PlanV2::ZedFree))
2232        ) && has_previous_trial
2233    }
2234
2235    fn should_render_onboarding(&self, cx: &mut Context<Self>) -> bool {
2236        if OnboardingUpsell::dismissed() {
2237            return false;
2238        }
2239
2240        let user_store = self.user_store.read(cx);
2241
2242        if user_store
2243            .plan()
2244            .is_some_and(|plan| matches!(plan, Plan::V1(PlanV1::ZedPro) | Plan::V2(PlanV2::ZedPro)))
2245            && user_store
2246                .subscription_period()
2247                .and_then(|period| period.0.checked_add_days(chrono::Days::new(1)))
2248                .is_some_and(|date| date < chrono::Utc::now())
2249        {
2250            OnboardingUpsell::set_dismissed(true, cx);
2251            return false;
2252        }
2253
2254        match &self.active_view {
2255            ActiveView::History | ActiveView::Configuration => false,
2256            ActiveView::ExternalAgentThread { thread_view, .. }
2257                if thread_view.read(cx).as_native_thread(cx).is_none() =>
2258            {
2259                false
2260            }
2261            _ => {
2262                let history_is_empty = self.history_store.read(cx).is_empty(cx);
2263
2264                let has_configured_non_zed_providers = LanguageModelRegistry::read_global(cx)
2265                    .providers()
2266                    .iter()
2267                    .any(|provider| {
2268                        provider.is_authenticated(cx)
2269                            && provider.id() != language_model::ZED_CLOUD_PROVIDER_ID
2270                    });
2271
2272                history_is_empty || !has_configured_non_zed_providers
2273            }
2274        }
2275    }
2276
2277    fn render_onboarding(
2278        &self,
2279        _window: &mut Window,
2280        cx: &mut Context<Self>,
2281    ) -> Option<impl IntoElement> {
2282        if !self.should_render_onboarding(cx) {
2283            return None;
2284        }
2285
2286        let text_thread_view = matches!(&self.active_view, ActiveView::TextThread { .. });
2287
2288        Some(
2289            div()
2290                .when(text_thread_view, |this| {
2291                    this.bg(cx.theme().colors().editor_background)
2292                })
2293                .child(self.onboarding.clone()),
2294        )
2295    }
2296
2297    fn render_trial_end_upsell(
2298        &self,
2299        _window: &mut Window,
2300        cx: &mut Context<Self>,
2301    ) -> Option<impl IntoElement> {
2302        if !self.should_render_trial_end_upsell(cx) {
2303            return None;
2304        }
2305
2306        let plan = self.user_store.read(cx).plan()?;
2307
2308        Some(
2309            v_flex()
2310                .absolute()
2311                .inset_0()
2312                .size_full()
2313                .bg(cx.theme().colors().panel_background)
2314                .opacity(0.85)
2315                .block_mouse_except_scroll()
2316                .child(EndTrialUpsell::new(
2317                    plan,
2318                    Arc::new({
2319                        let this = cx.entity();
2320                        move |_, cx| {
2321                            this.update(cx, |_this, cx| {
2322                                TrialEndUpsell::set_dismissed(true, cx);
2323                                cx.notify();
2324                            });
2325                        }
2326                    }),
2327                )),
2328        )
2329    }
2330
2331    fn render_configuration_error(
2332        &self,
2333        border_bottom: bool,
2334        configuration_error: &ConfigurationError,
2335        focus_handle: &FocusHandle,
2336        cx: &mut App,
2337    ) -> impl IntoElement {
2338        let zed_provider_configured = AgentSettings::get_global(cx)
2339            .default_model
2340            .as_ref()
2341            .is_some_and(|selection| selection.provider.0.as_str() == "zed.dev");
2342
2343        let callout = if zed_provider_configured {
2344            Callout::new()
2345                .icon(IconName::Warning)
2346                .severity(Severity::Warning)
2347                .when(border_bottom, |this| {
2348                    this.border_position(ui::BorderPosition::Bottom)
2349                })
2350                .title("Sign in to continue using Zed as your LLM provider.")
2351                .actions_slot(
2352                    Button::new("sign_in", "Sign In")
2353                        .style(ButtonStyle::Tinted(ui::TintColor::Warning))
2354                        .label_size(LabelSize::Small)
2355                        .on_click({
2356                            let workspace = self.workspace.clone();
2357                            move |_, _, cx| {
2358                                let Ok(client) =
2359                                    workspace.update(cx, |workspace, _| workspace.client().clone())
2360                                else {
2361                                    return;
2362                                };
2363
2364                                cx.spawn(async move |cx| {
2365                                    client.sign_in_with_optional_connect(true, cx).await
2366                                })
2367                                .detach_and_log_err(cx);
2368                            }
2369                        }),
2370                )
2371        } else {
2372            Callout::new()
2373                .icon(IconName::Warning)
2374                .severity(Severity::Warning)
2375                .when(border_bottom, |this| {
2376                    this.border_position(ui::BorderPosition::Bottom)
2377                })
2378                .title(configuration_error.to_string())
2379                .actions_slot(
2380                    Button::new("settings", "Configure")
2381                        .style(ButtonStyle::Tinted(ui::TintColor::Warning))
2382                        .label_size(LabelSize::Small)
2383                        .key_binding(
2384                            KeyBinding::for_action_in(&OpenSettings, focus_handle, cx)
2385                                .map(|kb| kb.size(rems_from_px(12.))),
2386                        )
2387                        .on_click(|_event, window, cx| {
2388                            window.dispatch_action(OpenSettings.boxed_clone(), cx)
2389                        }),
2390                )
2391        };
2392
2393        match configuration_error {
2394            ConfigurationError::ModelNotFound
2395            | ConfigurationError::ProviderNotAuthenticated(_)
2396            | ConfigurationError::NoProvider => callout.into_any_element(),
2397        }
2398    }
2399
2400    fn render_text_thread(
2401        &self,
2402        text_thread_editor: &Entity<TextThreadEditor>,
2403        buffer_search_bar: &Entity<BufferSearchBar>,
2404        window: &mut Window,
2405        cx: &mut Context<Self>,
2406    ) -> Div {
2407        let mut registrar = buffer_search::DivRegistrar::new(
2408            |this, _, _cx| match &this.active_view {
2409                ActiveView::TextThread {
2410                    buffer_search_bar, ..
2411                } => Some(buffer_search_bar.clone()),
2412                _ => None,
2413            },
2414            cx,
2415        );
2416        BufferSearchBar::register(&mut registrar);
2417        registrar
2418            .into_div()
2419            .size_full()
2420            .relative()
2421            .map(|parent| {
2422                buffer_search_bar.update(cx, |buffer_search_bar, cx| {
2423                    if buffer_search_bar.is_dismissed() {
2424                        return parent;
2425                    }
2426                    parent.child(
2427                        div()
2428                            .p(DynamicSpacing::Base08.rems(cx))
2429                            .border_b_1()
2430                            .border_color(cx.theme().colors().border_variant)
2431                            .bg(cx.theme().colors().editor_background)
2432                            .child(buffer_search_bar.render(window, cx)),
2433                    )
2434                })
2435            })
2436            .child(text_thread_editor.clone())
2437            .child(self.render_drag_target(cx))
2438    }
2439
2440    fn render_drag_target(&self, cx: &Context<Self>) -> Div {
2441        let is_local = self.project.read(cx).is_local();
2442        div()
2443            .invisible()
2444            .absolute()
2445            .top_0()
2446            .right_0()
2447            .bottom_0()
2448            .left_0()
2449            .bg(cx.theme().colors().drop_target_background)
2450            .drag_over::<DraggedTab>(|this, _, _, _| this.visible())
2451            .drag_over::<DraggedSelection>(|this, _, _, _| this.visible())
2452            .when(is_local, |this| {
2453                this.drag_over::<ExternalPaths>(|this, _, _, _| this.visible())
2454            })
2455            .on_drop(cx.listener(move |this, tab: &DraggedTab, window, cx| {
2456                let item = tab.pane.read(cx).item_for_index(tab.ix);
2457                let project_paths = item
2458                    .and_then(|item| item.project_path(cx))
2459                    .into_iter()
2460                    .collect::<Vec<_>>();
2461                this.handle_drop(project_paths, vec![], window, cx);
2462            }))
2463            .on_drop(
2464                cx.listener(move |this, selection: &DraggedSelection, window, cx| {
2465                    let project_paths = selection
2466                        .items()
2467                        .filter_map(|item| this.project.read(cx).path_for_entry(item.entry_id, cx))
2468                        .collect::<Vec<_>>();
2469                    this.handle_drop(project_paths, vec![], window, cx);
2470                }),
2471            )
2472            .on_drop(cx.listener(move |this, paths: &ExternalPaths, window, cx| {
2473                let tasks = paths
2474                    .paths()
2475                    .iter()
2476                    .map(|path| {
2477                        Workspace::project_path_for_path(this.project.clone(), path, false, cx)
2478                    })
2479                    .collect::<Vec<_>>();
2480                cx.spawn_in(window, async move |this, cx| {
2481                    let mut paths = vec![];
2482                    let mut added_worktrees = vec![];
2483                    let opened_paths = futures::future::join_all(tasks).await;
2484                    for entry in opened_paths {
2485                        if let Some((worktree, project_path)) = entry.log_err() {
2486                            added_worktrees.push(worktree);
2487                            paths.push(project_path);
2488                        }
2489                    }
2490                    this.update_in(cx, |this, window, cx| {
2491                        this.handle_drop(paths, added_worktrees, window, cx);
2492                    })
2493                    .ok();
2494                })
2495                .detach();
2496            }))
2497    }
2498
2499    fn handle_drop(
2500        &mut self,
2501        paths: Vec<ProjectPath>,
2502        added_worktrees: Vec<Entity<Worktree>>,
2503        window: &mut Window,
2504        cx: &mut Context<Self>,
2505    ) {
2506        match &self.active_view {
2507            ActiveView::ExternalAgentThread { thread_view } => {
2508                thread_view.update(cx, |thread_view, cx| {
2509                    thread_view.insert_dragged_files(paths, added_worktrees, window, cx);
2510                });
2511            }
2512            ActiveView::TextThread {
2513                text_thread_editor, ..
2514            } => {
2515                text_thread_editor.update(cx, |text_thread_editor, cx| {
2516                    TextThreadEditor::insert_dragged_files(
2517                        text_thread_editor,
2518                        paths,
2519                        added_worktrees,
2520                        window,
2521                        cx,
2522                    );
2523                });
2524            }
2525            ActiveView::History | ActiveView::Configuration => {}
2526        }
2527    }
2528
2529    fn key_context(&self) -> KeyContext {
2530        let mut key_context = KeyContext::new_with_defaults();
2531        key_context.add("AgentPanel");
2532        match &self.active_view {
2533            ActiveView::ExternalAgentThread { .. } => key_context.add("acp_thread"),
2534            ActiveView::TextThread { .. } => key_context.add("text_thread"),
2535            ActiveView::History | ActiveView::Configuration => {}
2536        }
2537        key_context
2538    }
2539}
2540
2541impl Render for AgentPanel {
2542    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2543        // WARNING: Changes to this element hierarchy can have
2544        // non-obvious implications to the layout of children.
2545        //
2546        // If you need to change it, please confirm:
2547        // - The message editor expands (cmd-option-esc) correctly
2548        // - When expanded, the buttons at the bottom of the panel are displayed correctly
2549        // - Font size works as expected and can be changed with cmd-+/cmd-
2550        // - Scrolling in all views works as expected
2551        // - Files can be dropped into the panel
2552        let content = v_flex()
2553            .relative()
2554            .size_full()
2555            .justify_between()
2556            .key_context(self.key_context())
2557            .on_action(cx.listener(|this, action: &NewThread, window, cx| {
2558                this.new_thread(action, window, cx);
2559            }))
2560            .on_action(cx.listener(|this, _: &OpenHistory, window, cx| {
2561                this.open_history(window, cx);
2562            }))
2563            .on_action(cx.listener(|this, _: &OpenSettings, window, cx| {
2564                this.open_configuration(window, cx);
2565            }))
2566            .on_action(cx.listener(Self::open_active_thread_as_markdown))
2567            .on_action(cx.listener(Self::deploy_rules_library))
2568            .on_action(cx.listener(Self::go_back))
2569            .on_action(cx.listener(Self::toggle_navigation_menu))
2570            .on_action(cx.listener(Self::toggle_options_menu))
2571            .on_action(cx.listener(Self::increase_font_size))
2572            .on_action(cx.listener(Self::decrease_font_size))
2573            .on_action(cx.listener(Self::reset_font_size))
2574            .on_action(cx.listener(Self::toggle_zoom))
2575            .on_action(cx.listener(|this, _: &ReauthenticateAgent, window, cx| {
2576                if let Some(thread_view) = this.active_thread_view() {
2577                    thread_view.update(cx, |thread_view, cx| thread_view.reauthenticate(window, cx))
2578                }
2579            }))
2580            .child(self.render_toolbar(window, cx))
2581            .children(self.render_onboarding(window, cx))
2582            .map(|parent| match &self.active_view {
2583                ActiveView::ExternalAgentThread { thread_view, .. } => parent
2584                    .child(thread_view.clone())
2585                    .child(self.render_drag_target(cx)),
2586                ActiveView::History => parent.child(self.acp_history.clone()),
2587                ActiveView::TextThread {
2588                    text_thread_editor,
2589                    buffer_search_bar,
2590                    ..
2591                } => {
2592                    let model_registry = LanguageModelRegistry::read_global(cx);
2593                    let configuration_error =
2594                        model_registry.configuration_error(model_registry.default_model(), cx);
2595                    parent
2596                        .map(|this| {
2597                            if !self.should_render_onboarding(cx)
2598                                && let Some(err) = configuration_error.as_ref()
2599                            {
2600                                this.child(self.render_configuration_error(
2601                                    true,
2602                                    err,
2603                                    &self.focus_handle(cx),
2604                                    cx,
2605                                ))
2606                            } else {
2607                                this
2608                            }
2609                        })
2610                        .child(self.render_text_thread(
2611                            text_thread_editor,
2612                            buffer_search_bar,
2613                            window,
2614                            cx,
2615                        ))
2616                }
2617                ActiveView::Configuration => parent.children(self.configuration.clone()),
2618            })
2619            .children(self.render_trial_end_upsell(window, cx));
2620
2621        match self.active_view.which_font_size_used() {
2622            WhichFontSize::AgentFont => {
2623                WithRemSize::new(ThemeSettings::get_global(cx).agent_ui_font_size(cx))
2624                    .size_full()
2625                    .child(content)
2626                    .into_any()
2627            }
2628            _ => content.into_any(),
2629        }
2630    }
2631}
2632
2633struct PromptLibraryInlineAssist {
2634    workspace: WeakEntity<Workspace>,
2635}
2636
2637impl PromptLibraryInlineAssist {
2638    pub fn new(workspace: WeakEntity<Workspace>) -> Self {
2639        Self { workspace }
2640    }
2641}
2642
2643impl rules_library::InlineAssistDelegate for PromptLibraryInlineAssist {
2644    fn assist(
2645        &self,
2646        prompt_editor: &Entity<Editor>,
2647        initial_prompt: Option<String>,
2648        window: &mut Window,
2649        cx: &mut Context<RulesLibrary>,
2650    ) {
2651        InlineAssistant::update_global(cx, |assistant, cx| {
2652            let Some(project) = self
2653                .workspace
2654                .upgrade()
2655                .map(|workspace| workspace.read(cx).project().downgrade())
2656            else {
2657                return;
2658            };
2659            let prompt_store = None;
2660            let thread_store = None;
2661            let context_store = cx.new(|_| ContextStore::new(project.clone()));
2662            assistant.assist(
2663                prompt_editor,
2664                self.workspace.clone(),
2665                context_store,
2666                project,
2667                prompt_store,
2668                thread_store,
2669                initial_prompt,
2670                window,
2671                cx,
2672            )
2673        })
2674    }
2675
2676    fn focus_agent_panel(
2677        &self,
2678        workspace: &mut Workspace,
2679        window: &mut Window,
2680        cx: &mut Context<Workspace>,
2681    ) -> bool {
2682        workspace.focus_panel::<AgentPanel>(window, cx).is_some()
2683    }
2684}
2685
2686pub struct ConcreteAssistantPanelDelegate;
2687
2688impl AgentPanelDelegate for ConcreteAssistantPanelDelegate {
2689    fn active_text_thread_editor(
2690        &self,
2691        workspace: &mut Workspace,
2692        _window: &mut Window,
2693        cx: &mut Context<Workspace>,
2694    ) -> Option<Entity<TextThreadEditor>> {
2695        let panel = workspace.panel::<AgentPanel>(cx)?;
2696        panel.read(cx).active_text_thread_editor()
2697    }
2698
2699    fn open_local_text_thread(
2700        &self,
2701        workspace: &mut Workspace,
2702        path: Arc<Path>,
2703        window: &mut Window,
2704        cx: &mut Context<Workspace>,
2705    ) -> Task<Result<()>> {
2706        let Some(panel) = workspace.panel::<AgentPanel>(cx) else {
2707            return Task::ready(Err(anyhow!("Agent panel not found")));
2708        };
2709
2710        panel.update(cx, |panel, cx| {
2711            panel.open_saved_text_thread(path, window, cx)
2712        })
2713    }
2714
2715    fn open_remote_text_thread(
2716        &self,
2717        _workspace: &mut Workspace,
2718        _text_thread_id: assistant_text_thread::TextThreadId,
2719        _window: &mut Window,
2720        _cx: &mut Context<Workspace>,
2721    ) -> Task<Result<Entity<TextThreadEditor>>> {
2722        Task::ready(Err(anyhow!("opening remote context not implemented")))
2723    }
2724
2725    fn quote_selection(
2726        &self,
2727        workspace: &mut Workspace,
2728        selection_ranges: Vec<Range<Anchor>>,
2729        buffer: Entity<MultiBuffer>,
2730        window: &mut Window,
2731        cx: &mut Context<Workspace>,
2732    ) {
2733        let Some(panel) = workspace.panel::<AgentPanel>(cx) else {
2734            return;
2735        };
2736
2737        if !panel.focus_handle(cx).contains_focused(window, cx) {
2738            workspace.toggle_panel_focus::<AgentPanel>(window, cx);
2739        }
2740
2741        panel.update(cx, |_, cx| {
2742            // Wait to create a new context until the workspace is no longer
2743            // being updated.
2744            cx.defer_in(window, move |panel, window, cx| {
2745                if let Some(thread_view) = panel.active_thread_view() {
2746                    thread_view.update(cx, |thread_view, cx| {
2747                        thread_view.insert_selections(window, cx);
2748                    });
2749                } else if let Some(text_thread_editor) = panel.active_text_thread_editor() {
2750                    let snapshot = buffer.read(cx).snapshot(cx);
2751                    let selection_ranges = selection_ranges
2752                        .into_iter()
2753                        .map(|range| range.to_point(&snapshot))
2754                        .collect::<Vec<_>>();
2755
2756                    text_thread_editor.update(cx, |text_thread_editor, cx| {
2757                        text_thread_editor.quote_ranges(selection_ranges, snapshot, window, cx)
2758                    });
2759                }
2760            });
2761        });
2762    }
2763}
2764
2765struct OnboardingUpsell;
2766
2767impl Dismissable for OnboardingUpsell {
2768    const KEY: &'static str = "dismissed-trial-upsell";
2769}
2770
2771struct TrialEndUpsell;
2772
2773impl Dismissable for TrialEndUpsell {
2774    const KEY: &'static str = "dismissed-trial-end-upsell";
2775}