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};
  19
  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(
1884                            "New Thread…",
1885                            &ToggleNewThreadMenu,
1886                            &focus_handle,
1887                            cx,
1888                        )
1889                    }
1890                },
1891            )
1892            .anchor(Corner::TopRight)
1893            .with_handle(self.new_thread_menu_handle.clone())
1894            .menu({
1895                let workspace = self.workspace.clone();
1896                let is_via_collab = workspace
1897                    .update(cx, |workspace, cx| {
1898                        workspace.project().read(cx).is_via_collab()
1899                    })
1900                    .unwrap_or_default();
1901
1902                move |window, cx| {
1903                    telemetry::event!("New Thread Clicked");
1904
1905                    let active_thread = active_thread.clone();
1906                    Some(ContextMenu::build(window, cx, |menu, _window, cx| {
1907                        menu.context(focus_handle.clone())
1908                            .header("Zed Agent")
1909                            .when_some(active_thread, |this, active_thread| {
1910                                let thread = active_thread.read(cx);
1911
1912                                if !thread.is_empty() {
1913                                    let session_id = thread.id().clone();
1914                                    this.item(
1915                                        ContextMenuEntry::new("New From Summary")
1916                                            .icon(IconName::ThreadFromSummary)
1917                                            .icon_color(Color::Muted)
1918                                            .handler(move |window, cx| {
1919                                                window.dispatch_action(
1920                                                    Box::new(NewNativeAgentThreadFromSummary {
1921                                                        from_session_id: session_id.clone(),
1922                                                    }),
1923                                                    cx,
1924                                                );
1925                                            }),
1926                                    )
1927                                } else {
1928                                    this
1929                                }
1930                            })
1931                            .item(
1932                                ContextMenuEntry::new("New Thread")
1933                                    .action(NewThread.boxed_clone())
1934                                    .icon(IconName::Thread)
1935                                    .icon_color(Color::Muted)
1936                                    .handler({
1937                                        let workspace = workspace.clone();
1938                                        move |window, cx| {
1939                                            if let Some(workspace) = workspace.upgrade() {
1940                                                workspace.update(cx, |workspace, cx| {
1941                                                    if let Some(panel) =
1942                                                        workspace.panel::<AgentPanel>(cx)
1943                                                    {
1944                                                        panel.update(cx, |panel, cx| {
1945                                                            panel.new_agent_thread(
1946                                                                AgentType::NativeAgent,
1947                                                                window,
1948                                                                cx,
1949                                                            );
1950                                                        });
1951                                                    }
1952                                                });
1953                                            }
1954                                        }
1955                                    }),
1956                            )
1957                            .item(
1958                                ContextMenuEntry::new("New Text Thread")
1959                                    .icon(IconName::TextThread)
1960                                    .icon_color(Color::Muted)
1961                                    .action(NewTextThread.boxed_clone())
1962                                    .handler({
1963                                        let workspace = workspace.clone();
1964                                        move |window, cx| {
1965                                            if let Some(workspace) = workspace.upgrade() {
1966                                                workspace.update(cx, |workspace, cx| {
1967                                                    if let Some(panel) =
1968                                                        workspace.panel::<AgentPanel>(cx)
1969                                                    {
1970                                                        panel.update(cx, |panel, cx| {
1971                                                            panel.new_agent_thread(
1972                                                                AgentType::TextThread,
1973                                                                window,
1974                                                                cx,
1975                                                            );
1976                                                        });
1977                                                    }
1978                                                });
1979                                            }
1980                                        }
1981                                    }),
1982                            )
1983                            .separator()
1984                            .header("External Agents")
1985                            .item(
1986                                ContextMenuEntry::new("New Claude Code")
1987                                    .icon(IconName::AiClaude)
1988                                    .disabled(is_via_collab)
1989                                    .icon_color(Color::Muted)
1990                                    .handler({
1991                                        let workspace = workspace.clone();
1992                                        move |window, cx| {
1993                                            if let Some(workspace) = workspace.upgrade() {
1994                                                workspace.update(cx, |workspace, cx| {
1995                                                    if let Some(panel) =
1996                                                        workspace.panel::<AgentPanel>(cx)
1997                                                    {
1998                                                        panel.update(cx, |panel, cx| {
1999                                                            panel.new_agent_thread(
2000                                                                AgentType::ClaudeCode,
2001                                                                window,
2002                                                                cx,
2003                                                            );
2004                                                        });
2005                                                    }
2006                                                });
2007                                            }
2008                                        }
2009                                    }),
2010                            )
2011                            .item(
2012                                ContextMenuEntry::new("New Codex CLI")
2013                                    .icon(IconName::AiOpenAi)
2014                                    .disabled(is_via_collab)
2015                                    .icon_color(Color::Muted)
2016                                    .handler({
2017                                        let workspace = workspace.clone();
2018                                        move |window, cx| {
2019                                            if let Some(workspace) = workspace.upgrade() {
2020                                                workspace.update(cx, |workspace, cx| {
2021                                                    if let Some(panel) =
2022                                                        workspace.panel::<AgentPanel>(cx)
2023                                                    {
2024                                                        panel.update(cx, |panel, cx| {
2025                                                            panel.new_agent_thread(
2026                                                                AgentType::Codex,
2027                                                                window,
2028                                                                cx,
2029                                                            );
2030                                                        });
2031                                                    }
2032                                                });
2033                                            }
2034                                        }
2035                                    }),
2036                            )
2037                            .item(
2038                                ContextMenuEntry::new("New Gemini CLI")
2039                                    .icon(IconName::AiGemini)
2040                                    .icon_color(Color::Muted)
2041                                    .disabled(is_via_collab)
2042                                    .handler({
2043                                        let workspace = workspace.clone();
2044                                        move |window, cx| {
2045                                            if let Some(workspace) = workspace.upgrade() {
2046                                                workspace.update(cx, |workspace, cx| {
2047                                                    if let Some(panel) =
2048                                                        workspace.panel::<AgentPanel>(cx)
2049                                                    {
2050                                                        panel.update(cx, |panel, cx| {
2051                                                            panel.new_agent_thread(
2052                                                                AgentType::Gemini,
2053                                                                window,
2054                                                                cx,
2055                                                            );
2056                                                        });
2057                                                    }
2058                                                });
2059                                            }
2060                                        }
2061                                    }),
2062                            )
2063                            .map(|mut menu| {
2064                                let agent_server_store_read = agent_server_store.read(cx);
2065                                let agent_names = agent_server_store_read
2066                                    .external_agents()
2067                                    .filter(|name| {
2068                                        name.0 != GEMINI_NAME
2069                                            && name.0 != CLAUDE_CODE_NAME
2070                                            && name.0 != CODEX_NAME
2071                                    })
2072                                    .cloned()
2073                                    .collect::<Vec<_>>();
2074                                let custom_settings = cx
2075                                    .global::<SettingsStore>()
2076                                    .get::<AllAgentServersSettings>(None)
2077                                    .custom
2078                                    .clone();
2079                                for agent_name in agent_names {
2080                                    let icon_path = agent_server_store_read.agent_icon(&agent_name);
2081                                    let mut entry =
2082                                        ContextMenuEntry::new(format!("New {}", agent_name));
2083                                    if let Some(icon_path) = icon_path {
2084                                        entry = entry.custom_icon_svg(icon_path);
2085                                    } else {
2086                                        entry = entry.icon(IconName::Terminal);
2087                                    }
2088                                    entry = entry
2089                                        .icon_color(Color::Muted)
2090                                        .disabled(is_via_collab)
2091                                        .handler({
2092                                            let workspace = workspace.clone();
2093                                            let agent_name = agent_name.clone();
2094                                            let custom_settings = custom_settings.clone();
2095                                            move |window, cx| {
2096                                                if let Some(workspace) = workspace.upgrade() {
2097                                                    workspace.update(cx, |workspace, cx| {
2098                                                        if let Some(panel) =
2099                                                            workspace.panel::<AgentPanel>(cx)
2100                                                        {
2101                                                            panel.update(cx, |panel, cx| {
2102                                                                panel.new_agent_thread(
2103                                                                    AgentType::Custom {
2104                                                                        name: agent_name
2105                                                                            .clone()
2106                                                                            .into(),
2107                                                                        command: custom_settings
2108                                                                            .get(&agent_name.0)
2109                                                                            .map(|settings| {
2110                                                                                settings
2111                                                                                    .command
2112                                                                                    .clone()
2113                                                                            })
2114                                                                            .unwrap_or(
2115                                                                                placeholder_command(
2116                                                                                ),
2117                                                                            ),
2118                                                                    },
2119                                                                    window,
2120                                                                    cx,
2121                                                                );
2122                                                            });
2123                                                        }
2124                                                    });
2125                                                }
2126                                            }
2127                                        });
2128                                    menu = menu.item(entry);
2129                                }
2130
2131                                menu
2132                            })
2133                            .separator()
2134                            .item(
2135                                ContextMenuEntry::new("Add More Agents")
2136                                    .icon(IconName::Plus)
2137                                    .icon_color(Color::Muted)
2138                                    .handler({
2139                                        move |window, cx| {
2140                                            window.dispatch_action(Box::new(zed_actions::Extensions {
2141                                                category_filter: Some(
2142                                                    zed_actions::ExtensionCategoryFilter::AgentServers,
2143                                                ),
2144                                                id: None,
2145                                            }), cx)
2146                                        }
2147                                    }),
2148                            )
2149                    }))
2150                }
2151            });
2152
2153        let selected_agent_label = self.selected_agent.label();
2154
2155        let has_custom_icon = selected_agent_custom_icon.is_some();
2156        let selected_agent = div()
2157            .id("selected_agent_icon")
2158            .when_some(selected_agent_custom_icon, |this, icon_path| {
2159                let label = selected_agent_label.clone();
2160                this.px(DynamicSpacing::Base02.rems(cx))
2161                    .child(Icon::from_external_svg(icon_path).color(Color::Muted))
2162                    .tooltip(move |_window, cx| {
2163                        Tooltip::with_meta(label.clone(), None, "Selected Agent", cx)
2164                    })
2165            })
2166            .when(!has_custom_icon, |this| {
2167                this.when_some(self.selected_agent.icon(), |this, icon| {
2168                    let label = selected_agent_label.clone();
2169                    this.px(DynamicSpacing::Base02.rems(cx))
2170                        .child(Icon::new(icon).color(Color::Muted))
2171                        .tooltip(move |_window, cx| {
2172                            Tooltip::with_meta(label.clone(), None, "Selected Agent", cx)
2173                        })
2174                })
2175            })
2176            .into_any_element();
2177
2178        h_flex()
2179            .id("agent-panel-toolbar")
2180            .h(Tab::container_height(cx))
2181            .max_w_full()
2182            .flex_none()
2183            .justify_between()
2184            .gap_2()
2185            .bg(cx.theme().colors().tab_bar_background)
2186            .border_b_1()
2187            .border_color(cx.theme().colors().border)
2188            .child(
2189                h_flex()
2190                    .size_full()
2191                    .gap(DynamicSpacing::Base04.rems(cx))
2192                    .pl(DynamicSpacing::Base04.rems(cx))
2193                    .child(match &self.active_view {
2194                        ActiveView::History | ActiveView::Configuration => {
2195                            self.render_toolbar_back_button(cx).into_any_element()
2196                        }
2197                        _ => selected_agent.into_any_element(),
2198                    })
2199                    .child(self.render_title_view(window, cx)),
2200            )
2201            .child(
2202                h_flex()
2203                    .flex_none()
2204                    .gap(DynamicSpacing::Base02.rems(cx))
2205                    .pl(DynamicSpacing::Base04.rems(cx))
2206                    .pr(DynamicSpacing::Base06.rems(cx))
2207                    .child(new_thread_menu)
2208                    .child(self.render_recent_entries_menu(
2209                        IconName::MenuAltTemp,
2210                        Corner::TopRight,
2211                        cx,
2212                    ))
2213                    .child(self.render_panel_options_menu(window, cx)),
2214            )
2215    }
2216
2217    fn should_render_trial_end_upsell(&self, cx: &mut Context<Self>) -> bool {
2218        if TrialEndUpsell::dismissed() {
2219            return false;
2220        }
2221
2222        match &self.active_view {
2223            ActiveView::TextThread { .. } => {
2224                if LanguageModelRegistry::global(cx)
2225                    .read(cx)
2226                    .default_model()
2227                    .is_some_and(|model| {
2228                        model.provider.id() != language_model::ZED_CLOUD_PROVIDER_ID
2229                    })
2230                {
2231                    return false;
2232                }
2233            }
2234            ActiveView::ExternalAgentThread { .. }
2235            | ActiveView::History
2236            | ActiveView::Configuration => return false,
2237        }
2238
2239        let plan = self.user_store.read(cx).plan();
2240        let has_previous_trial = self.user_store.read(cx).trial_started_at().is_some();
2241
2242        matches!(
2243            plan,
2244            Some(Plan::V1(PlanV1::ZedFree) | Plan::V2(PlanV2::ZedFree))
2245        ) && has_previous_trial
2246    }
2247
2248    fn should_render_onboarding(&self, cx: &mut Context<Self>) -> bool {
2249        if OnboardingUpsell::dismissed() {
2250            return false;
2251        }
2252
2253        let user_store = self.user_store.read(cx);
2254
2255        if user_store
2256            .plan()
2257            .is_some_and(|plan| matches!(plan, Plan::V1(PlanV1::ZedPro) | Plan::V2(PlanV2::ZedPro)))
2258            && user_store
2259                .subscription_period()
2260                .and_then(|period| period.0.checked_add_days(chrono::Days::new(1)))
2261                .is_some_and(|date| date < chrono::Utc::now())
2262        {
2263            OnboardingUpsell::set_dismissed(true, cx);
2264            return false;
2265        }
2266
2267        match &self.active_view {
2268            ActiveView::History | ActiveView::Configuration => false,
2269            ActiveView::ExternalAgentThread { thread_view, .. }
2270                if thread_view.read(cx).as_native_thread(cx).is_none() =>
2271            {
2272                false
2273            }
2274            _ => {
2275                let history_is_empty = self.history_store.read(cx).is_empty(cx);
2276
2277                let has_configured_non_zed_providers = LanguageModelRegistry::read_global(cx)
2278                    .providers()
2279                    .iter()
2280                    .any(|provider| {
2281                        provider.is_authenticated(cx)
2282                            && provider.id() != language_model::ZED_CLOUD_PROVIDER_ID
2283                    });
2284
2285                history_is_empty || !has_configured_non_zed_providers
2286            }
2287        }
2288    }
2289
2290    fn render_onboarding(
2291        &self,
2292        _window: &mut Window,
2293        cx: &mut Context<Self>,
2294    ) -> Option<impl IntoElement> {
2295        if !self.should_render_onboarding(cx) {
2296            return None;
2297        }
2298
2299        let text_thread_view = matches!(&self.active_view, ActiveView::TextThread { .. });
2300
2301        Some(
2302            div()
2303                .when(text_thread_view, |this| {
2304                    this.bg(cx.theme().colors().editor_background)
2305                })
2306                .child(self.onboarding.clone()),
2307        )
2308    }
2309
2310    fn render_trial_end_upsell(
2311        &self,
2312        _window: &mut Window,
2313        cx: &mut Context<Self>,
2314    ) -> Option<impl IntoElement> {
2315        if !self.should_render_trial_end_upsell(cx) {
2316            return None;
2317        }
2318
2319        let plan = self.user_store.read(cx).plan()?;
2320
2321        Some(
2322            v_flex()
2323                .absolute()
2324                .inset_0()
2325                .size_full()
2326                .bg(cx.theme().colors().panel_background)
2327                .opacity(0.85)
2328                .block_mouse_except_scroll()
2329                .child(EndTrialUpsell::new(
2330                    plan,
2331                    Arc::new({
2332                        let this = cx.entity();
2333                        move |_, cx| {
2334                            this.update(cx, |_this, cx| {
2335                                TrialEndUpsell::set_dismissed(true, cx);
2336                                cx.notify();
2337                            });
2338                        }
2339                    }),
2340                )),
2341        )
2342    }
2343
2344    fn render_configuration_error(
2345        &self,
2346        border_bottom: bool,
2347        configuration_error: &ConfigurationError,
2348        focus_handle: &FocusHandle,
2349        cx: &mut App,
2350    ) -> impl IntoElement {
2351        let zed_provider_configured = AgentSettings::get_global(cx)
2352            .default_model
2353            .as_ref()
2354            .is_some_and(|selection| selection.provider.0.as_str() == "zed.dev");
2355
2356        let callout = if zed_provider_configured {
2357            Callout::new()
2358                .icon(IconName::Warning)
2359                .severity(Severity::Warning)
2360                .when(border_bottom, |this| {
2361                    this.border_position(ui::BorderPosition::Bottom)
2362                })
2363                .title("Sign in to continue using Zed as your LLM provider.")
2364                .actions_slot(
2365                    Button::new("sign_in", "Sign In")
2366                        .style(ButtonStyle::Tinted(ui::TintColor::Warning))
2367                        .label_size(LabelSize::Small)
2368                        .on_click({
2369                            let workspace = self.workspace.clone();
2370                            move |_, _, cx| {
2371                                let Ok(client) =
2372                                    workspace.update(cx, |workspace, _| workspace.client().clone())
2373                                else {
2374                                    return;
2375                                };
2376
2377                                cx.spawn(async move |cx| {
2378                                    client.sign_in_with_optional_connect(true, cx).await
2379                                })
2380                                .detach_and_log_err(cx);
2381                            }
2382                        }),
2383                )
2384        } else {
2385            Callout::new()
2386                .icon(IconName::Warning)
2387                .severity(Severity::Warning)
2388                .when(border_bottom, |this| {
2389                    this.border_position(ui::BorderPosition::Bottom)
2390                })
2391                .title(configuration_error.to_string())
2392                .actions_slot(
2393                    Button::new("settings", "Configure")
2394                        .style(ButtonStyle::Tinted(ui::TintColor::Warning))
2395                        .label_size(LabelSize::Small)
2396                        .key_binding(
2397                            KeyBinding::for_action_in(&OpenSettings, focus_handle, cx)
2398                                .map(|kb| kb.size(rems_from_px(12.))),
2399                        )
2400                        .on_click(|_event, window, cx| {
2401                            window.dispatch_action(OpenSettings.boxed_clone(), cx)
2402                        }),
2403                )
2404        };
2405
2406        match configuration_error {
2407            ConfigurationError::ModelNotFound
2408            | ConfigurationError::ProviderNotAuthenticated(_)
2409            | ConfigurationError::NoProvider => callout.into_any_element(),
2410        }
2411    }
2412
2413    fn render_text_thread(
2414        &self,
2415        text_thread_editor: &Entity<TextThreadEditor>,
2416        buffer_search_bar: &Entity<BufferSearchBar>,
2417        window: &mut Window,
2418        cx: &mut Context<Self>,
2419    ) -> Div {
2420        let mut registrar = buffer_search::DivRegistrar::new(
2421            |this, _, _cx| match &this.active_view {
2422                ActiveView::TextThread {
2423                    buffer_search_bar, ..
2424                } => Some(buffer_search_bar.clone()),
2425                _ => None,
2426            },
2427            cx,
2428        );
2429        BufferSearchBar::register(&mut registrar);
2430        registrar
2431            .into_div()
2432            .size_full()
2433            .relative()
2434            .map(|parent| {
2435                buffer_search_bar.update(cx, |buffer_search_bar, cx| {
2436                    if buffer_search_bar.is_dismissed() {
2437                        return parent;
2438                    }
2439                    parent.child(
2440                        div()
2441                            .p(DynamicSpacing::Base08.rems(cx))
2442                            .border_b_1()
2443                            .border_color(cx.theme().colors().border_variant)
2444                            .bg(cx.theme().colors().editor_background)
2445                            .child(buffer_search_bar.render(window, cx)),
2446                    )
2447                })
2448            })
2449            .child(text_thread_editor.clone())
2450            .child(self.render_drag_target(cx))
2451    }
2452
2453    fn render_drag_target(&self, cx: &Context<Self>) -> Div {
2454        let is_local = self.project.read(cx).is_local();
2455        div()
2456            .invisible()
2457            .absolute()
2458            .top_0()
2459            .right_0()
2460            .bottom_0()
2461            .left_0()
2462            .bg(cx.theme().colors().drop_target_background)
2463            .drag_over::<DraggedTab>(|this, _, _, _| this.visible())
2464            .drag_over::<DraggedSelection>(|this, _, _, _| this.visible())
2465            .when(is_local, |this| {
2466                this.drag_over::<ExternalPaths>(|this, _, _, _| this.visible())
2467            })
2468            .on_drop(cx.listener(move |this, tab: &DraggedTab, window, cx| {
2469                let item = tab.pane.read(cx).item_for_index(tab.ix);
2470                let project_paths = item
2471                    .and_then(|item| item.project_path(cx))
2472                    .into_iter()
2473                    .collect::<Vec<_>>();
2474                this.handle_drop(project_paths, vec![], window, cx);
2475            }))
2476            .on_drop(
2477                cx.listener(move |this, selection: &DraggedSelection, window, cx| {
2478                    let project_paths = selection
2479                        .items()
2480                        .filter_map(|item| this.project.read(cx).path_for_entry(item.entry_id, cx))
2481                        .collect::<Vec<_>>();
2482                    this.handle_drop(project_paths, vec![], window, cx);
2483                }),
2484            )
2485            .on_drop(cx.listener(move |this, paths: &ExternalPaths, window, cx| {
2486                let tasks = paths
2487                    .paths()
2488                    .iter()
2489                    .map(|path| {
2490                        Workspace::project_path_for_path(this.project.clone(), path, false, cx)
2491                    })
2492                    .collect::<Vec<_>>();
2493                cx.spawn_in(window, async move |this, cx| {
2494                    let mut paths = vec![];
2495                    let mut added_worktrees = vec![];
2496                    let opened_paths = futures::future::join_all(tasks).await;
2497                    for entry in opened_paths {
2498                        if let Some((worktree, project_path)) = entry.log_err() {
2499                            added_worktrees.push(worktree);
2500                            paths.push(project_path);
2501                        }
2502                    }
2503                    this.update_in(cx, |this, window, cx| {
2504                        this.handle_drop(paths, added_worktrees, window, cx);
2505                    })
2506                    .ok();
2507                })
2508                .detach();
2509            }))
2510    }
2511
2512    fn handle_drop(
2513        &mut self,
2514        paths: Vec<ProjectPath>,
2515        added_worktrees: Vec<Entity<Worktree>>,
2516        window: &mut Window,
2517        cx: &mut Context<Self>,
2518    ) {
2519        match &self.active_view {
2520            ActiveView::ExternalAgentThread { thread_view } => {
2521                thread_view.update(cx, |thread_view, cx| {
2522                    thread_view.insert_dragged_files(paths, added_worktrees, window, cx);
2523                });
2524            }
2525            ActiveView::TextThread {
2526                text_thread_editor, ..
2527            } => {
2528                text_thread_editor.update(cx, |text_thread_editor, cx| {
2529                    TextThreadEditor::insert_dragged_files(
2530                        text_thread_editor,
2531                        paths,
2532                        added_worktrees,
2533                        window,
2534                        cx,
2535                    );
2536                });
2537            }
2538            ActiveView::History | ActiveView::Configuration => {}
2539        }
2540    }
2541
2542    fn key_context(&self) -> KeyContext {
2543        let mut key_context = KeyContext::new_with_defaults();
2544        key_context.add("AgentPanel");
2545        match &self.active_view {
2546            ActiveView::ExternalAgentThread { .. } => key_context.add("acp_thread"),
2547            ActiveView::TextThread { .. } => key_context.add("text_thread"),
2548            ActiveView::History | ActiveView::Configuration => {}
2549        }
2550        key_context
2551    }
2552}
2553
2554impl Render for AgentPanel {
2555    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2556        // WARNING: Changes to this element hierarchy can have
2557        // non-obvious implications to the layout of children.
2558        //
2559        // If you need to change it, please confirm:
2560        // - The message editor expands (cmd-option-esc) correctly
2561        // - When expanded, the buttons at the bottom of the panel are displayed correctly
2562        // - Font size works as expected and can be changed with cmd-+/cmd-
2563        // - Scrolling in all views works as expected
2564        // - Files can be dropped into the panel
2565        let content = v_flex()
2566            .relative()
2567            .size_full()
2568            .justify_between()
2569            .key_context(self.key_context())
2570            .on_action(cx.listener(|this, action: &NewThread, window, cx| {
2571                this.new_thread(action, window, cx);
2572            }))
2573            .on_action(cx.listener(|this, _: &OpenHistory, window, cx| {
2574                this.open_history(window, cx);
2575            }))
2576            .on_action(cx.listener(|this, _: &OpenSettings, window, cx| {
2577                this.open_configuration(window, cx);
2578            }))
2579            .on_action(cx.listener(Self::open_active_thread_as_markdown))
2580            .on_action(cx.listener(Self::deploy_rules_library))
2581            .on_action(cx.listener(Self::go_back))
2582            .on_action(cx.listener(Self::toggle_navigation_menu))
2583            .on_action(cx.listener(Self::toggle_options_menu))
2584            .on_action(cx.listener(Self::increase_font_size))
2585            .on_action(cx.listener(Self::decrease_font_size))
2586            .on_action(cx.listener(Self::reset_font_size))
2587            .on_action(cx.listener(Self::toggle_zoom))
2588            .on_action(cx.listener(|this, _: &ReauthenticateAgent, window, cx| {
2589                if let Some(thread_view) = this.active_thread_view() {
2590                    thread_view.update(cx, |thread_view, cx| thread_view.reauthenticate(window, cx))
2591                }
2592            }))
2593            .child(self.render_toolbar(window, cx))
2594            .children(self.render_onboarding(window, cx))
2595            .map(|parent| match &self.active_view {
2596                ActiveView::ExternalAgentThread { thread_view, .. } => parent
2597                    .child(thread_view.clone())
2598                    .child(self.render_drag_target(cx)),
2599                ActiveView::History => parent.child(self.acp_history.clone()),
2600                ActiveView::TextThread {
2601                    text_thread_editor,
2602                    buffer_search_bar,
2603                    ..
2604                } => {
2605                    let model_registry = LanguageModelRegistry::read_global(cx);
2606                    let configuration_error =
2607                        model_registry.configuration_error(model_registry.default_model(), cx);
2608                    parent
2609                        .map(|this| {
2610                            if !self.should_render_onboarding(cx)
2611                                && let Some(err) = configuration_error.as_ref()
2612                            {
2613                                this.child(self.render_configuration_error(
2614                                    true,
2615                                    err,
2616                                    &self.focus_handle(cx),
2617                                    cx,
2618                                ))
2619                            } else {
2620                                this
2621                            }
2622                        })
2623                        .child(self.render_text_thread(
2624                            text_thread_editor,
2625                            buffer_search_bar,
2626                            window,
2627                            cx,
2628                        ))
2629                }
2630                ActiveView::Configuration => parent.children(self.configuration.clone()),
2631            })
2632            .children(self.render_trial_end_upsell(window, cx));
2633
2634        match self.active_view.which_font_size_used() {
2635            WhichFontSize::AgentFont => {
2636                WithRemSize::new(ThemeSettings::get_global(cx).agent_ui_font_size(cx))
2637                    .size_full()
2638                    .child(content)
2639                    .into_any()
2640            }
2641            _ => content.into_any(),
2642        }
2643    }
2644}
2645
2646struct PromptLibraryInlineAssist {
2647    workspace: WeakEntity<Workspace>,
2648}
2649
2650impl PromptLibraryInlineAssist {
2651    pub fn new(workspace: WeakEntity<Workspace>) -> Self {
2652        Self { workspace }
2653    }
2654}
2655
2656impl rules_library::InlineAssistDelegate for PromptLibraryInlineAssist {
2657    fn assist(
2658        &self,
2659        prompt_editor: &Entity<Editor>,
2660        initial_prompt: Option<String>,
2661        window: &mut Window,
2662        cx: &mut Context<RulesLibrary>,
2663    ) {
2664        InlineAssistant::update_global(cx, |assistant, cx| {
2665            let Some(project) = self
2666                .workspace
2667                .upgrade()
2668                .map(|workspace| workspace.read(cx).project().downgrade())
2669            else {
2670                return;
2671            };
2672            let prompt_store = None;
2673            let thread_store = None;
2674            let context_store = cx.new(|_| ContextStore::new(project.clone()));
2675            assistant.assist(
2676                prompt_editor,
2677                self.workspace.clone(),
2678                context_store,
2679                project,
2680                prompt_store,
2681                thread_store,
2682                initial_prompt,
2683                window,
2684                cx,
2685            )
2686        })
2687    }
2688
2689    fn focus_agent_panel(
2690        &self,
2691        workspace: &mut Workspace,
2692        window: &mut Window,
2693        cx: &mut Context<Workspace>,
2694    ) -> bool {
2695        workspace.focus_panel::<AgentPanel>(window, cx).is_some()
2696    }
2697}
2698
2699pub struct ConcreteAssistantPanelDelegate;
2700
2701impl AgentPanelDelegate for ConcreteAssistantPanelDelegate {
2702    fn active_text_thread_editor(
2703        &self,
2704        workspace: &mut Workspace,
2705        _window: &mut Window,
2706        cx: &mut Context<Workspace>,
2707    ) -> Option<Entity<TextThreadEditor>> {
2708        let panel = workspace.panel::<AgentPanel>(cx)?;
2709        panel.read(cx).active_text_thread_editor()
2710    }
2711
2712    fn open_local_text_thread(
2713        &self,
2714        workspace: &mut Workspace,
2715        path: Arc<Path>,
2716        window: &mut Window,
2717        cx: &mut Context<Workspace>,
2718    ) -> Task<Result<()>> {
2719        let Some(panel) = workspace.panel::<AgentPanel>(cx) else {
2720            return Task::ready(Err(anyhow!("Agent panel not found")));
2721        };
2722
2723        panel.update(cx, |panel, cx| {
2724            panel.open_saved_text_thread(path, window, cx)
2725        })
2726    }
2727
2728    fn open_remote_text_thread(
2729        &self,
2730        _workspace: &mut Workspace,
2731        _text_thread_id: assistant_text_thread::TextThreadId,
2732        _window: &mut Window,
2733        _cx: &mut Context<Workspace>,
2734    ) -> Task<Result<Entity<TextThreadEditor>>> {
2735        Task::ready(Err(anyhow!("opening remote context not implemented")))
2736    }
2737
2738    fn quote_selection(
2739        &self,
2740        workspace: &mut Workspace,
2741        selection_ranges: Vec<Range<Anchor>>,
2742        buffer: Entity<MultiBuffer>,
2743        window: &mut Window,
2744        cx: &mut Context<Workspace>,
2745    ) {
2746        let Some(panel) = workspace.panel::<AgentPanel>(cx) else {
2747            return;
2748        };
2749
2750        if !panel.focus_handle(cx).contains_focused(window, cx) {
2751            workspace.toggle_panel_focus::<AgentPanel>(window, cx);
2752        }
2753
2754        panel.update(cx, |_, cx| {
2755            // Wait to create a new context until the workspace is no longer
2756            // being updated.
2757            cx.defer_in(window, move |panel, window, cx| {
2758                if let Some(thread_view) = panel.active_thread_view() {
2759                    thread_view.update(cx, |thread_view, cx| {
2760                        thread_view.insert_selections(window, cx);
2761                    });
2762                } else if let Some(text_thread_editor) = panel.active_text_thread_editor() {
2763                    let snapshot = buffer.read(cx).snapshot(cx);
2764                    let selection_ranges = selection_ranges
2765                        .into_iter()
2766                        .map(|range| range.to_point(&snapshot))
2767                        .collect::<Vec<_>>();
2768
2769                    text_thread_editor.update(cx, |text_thread_editor, cx| {
2770                        text_thread_editor.quote_ranges(selection_ranges, snapshot, window, cx)
2771                    });
2772                }
2773            });
2774        });
2775    }
2776}
2777
2778struct OnboardingUpsell;
2779
2780impl Dismissable for OnboardingUpsell {
2781    const KEY: &'static str = "dismissed-trial-upsell";
2782}
2783
2784struct TrialEndUpsell;
2785
2786impl Dismissable for TrialEndUpsell {
2787    const KEY: &'static str = "dismissed-trial-end-upsell";
2788}