1use std::ops::{Not, Range};
2use std::path::Path;
3use std::rc::Rc;
4use std::sync::Arc;
5use std::time::Duration;
6
7use acp_thread::AcpThreadMetadata;
8use agent_servers::AgentServer;
9use agent2::HistoryEntry;
10use db::kvp::{Dismissable, KEY_VALUE_STORE};
11use serde::{Deserialize, Serialize};
12
13use crate::acp::{AcpThreadHistory, ThreadHistoryEvent};
14use crate::agent_diff::AgentDiffThread;
15use crate::{
16 AddContextServer, AgentDiffPane, ContinueThread, ContinueWithBurnMode,
17 DeleteRecentlyOpenThread, ExpandMessageEditor, Follow, InlineAssistant, NewTextThread,
18 NewThread, OpenActiveThreadAsMarkdown, OpenAgentDiff, OpenHistory, ResetTrialEndUpsell,
19 ResetTrialUpsell, ToggleBurnMode, ToggleContextPicker, ToggleNavigationMenu,
20 ToggleNewThreadMenu, ToggleOptionsMenu,
21 acp::AcpThreadView,
22 active_thread::{self, ActiveThread, ActiveThreadEvent},
23 agent_configuration::{AgentConfiguration, AssistantConfigurationEvent},
24 agent_diff::AgentDiff,
25 message_editor::{MessageEditor, MessageEditorEvent},
26 slash_command::SlashCommandCompletionProvider,
27 text_thread_editor::{
28 AgentPanelDelegate, TextThreadEditor, humanize_token_count, make_lsp_adapter_delegate,
29 render_remaining_tokens,
30 },
31 thread_history::{HistoryEntryElement, ThreadHistory},
32 ui::{AgentOnboardingModal, EndTrialUpsell},
33};
34use crate::{ExternalAgent, NewExternalAgentThread};
35use agent::{
36 Thread, ThreadError, ThreadEvent, ThreadId, ThreadSummary, TokenUsageRatio,
37 context_store::ContextStore,
38 history_store::{HistoryEntryId, HistoryStore},
39 thread_store::{TextThreadStore, ThreadStore},
40};
41use agent_settings::{AgentDockPosition, AgentSettings, CompletionMode, DefaultView};
42use ai_onboarding::AgentPanelOnboarding;
43use anyhow::{Result, anyhow};
44use assistant_context::{AssistantContext, ContextEvent, ContextSummary};
45use assistant_slash_command::SlashCommandWorkingSet;
46use assistant_tool::ToolWorkingSet;
47use client::{UserStore, zed_urls};
48use cloud_llm_client::{CompletionIntent, Plan, UsageLimit};
49use editor::{Anchor, AnchorRangeExt as _, Editor, EditorEvent, MultiBuffer};
50use feature_flags::{self, FeatureFlagAppExt};
51use fs::Fs;
52use gpui::{
53 Action, Animation, AnimationExt as _, AnyElement, App, AsyncWindowContext, ClipboardItem,
54 Corner, DismissEvent, Entity, EventEmitter, ExternalPaths, FocusHandle, Focusable, Hsla,
55 KeyContext, Pixels, Subscription, Task, UpdateGlobal, WeakEntity, prelude::*,
56 pulsating_between,
57};
58use language::LanguageRegistry;
59use language_model::{
60 ConfigurationError, ConfiguredModel, LanguageModelProviderTosView, LanguageModelRegistry,
61};
62use project::{DisableAiSettings, Project, ProjectPath, Worktree};
63use prompt_store::{PromptBuilder, PromptStore, UserPromptId};
64use rules_library::{RulesLibrary, open_rules_library};
65use search::{BufferSearchBar, buffer_search};
66use settings::{Settings, update_settings_file};
67use theme::ThemeSettings;
68use time::UtcOffset;
69use ui::utils::WithRemSize;
70use ui::{
71 Banner, Callout, ContextMenu, ContextMenuEntry, Divider, ElevationIndex, KeyBinding,
72 PopoverMenu, PopoverMenuHandle, ProgressBar, Tab, Tooltip, prelude::*,
73};
74use util::ResultExt as _;
75use workspace::{
76 CollaboratorId, DraggedSelection, DraggedTab, ToggleZoom, ToolbarItemView, Workspace,
77 dock::{DockPosition, Panel, PanelEvent},
78};
79use zed_actions::{
80 DecreaseBufferFontSize, IncreaseBufferFontSize, ResetBufferFontSize,
81 agent::{OpenOnboardingModal, OpenSettings, ResetOnboarding, ToggleModelSelector},
82 assistant::{OpenRulesLibrary, ToggleFocus},
83};
84
85const AGENT_PANEL_KEY: &str = "agent_panel";
86
87#[derive(Serialize, Deserialize)]
88struct SerializedAgentPanel {
89 width: Option<Pixels>,
90 selected_agent: Option<AgentType>,
91}
92
93pub fn init(cx: &mut App) {
94 cx.observe_new(
95 |workspace: &mut Workspace, _window, _cx: &mut Context<Workspace>| {
96 workspace
97 .register_action(|workspace, action: &NewThread, window, cx| {
98 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
99 panel.update(cx, |panel, cx| panel.new_thread(action, window, cx));
100 workspace.focus_panel::<AgentPanel>(window, cx);
101 }
102 })
103 .register_action(|workspace, _: &OpenHistory, window, cx| {
104 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
105 workspace.focus_panel::<AgentPanel>(window, cx);
106 panel.update(cx, |panel, cx| panel.open_history(window, cx));
107 }
108 })
109 .register_action(|workspace, _: &OpenSettings, window, cx| {
110 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
111 workspace.focus_panel::<AgentPanel>(window, cx);
112 panel.update(cx, |panel, cx| panel.open_configuration(window, cx));
113 }
114 })
115 .register_action(|workspace, _: &NewTextThread, window, cx| {
116 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
117 workspace.focus_panel::<AgentPanel>(window, cx);
118 panel.update(cx, |panel, cx| panel.new_prompt_editor(window, cx));
119 }
120 })
121 .register_action(|workspace, action: &NewExternalAgentThread, window, cx| {
122 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
123 workspace.focus_panel::<AgentPanel>(window, cx);
124 panel.update(cx, |panel, cx| {
125 panel.new_external_thread(action.agent, None, window, cx)
126 });
127 }
128 })
129 .register_action(|workspace, action: &OpenRulesLibrary, window, cx| {
130 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
131 workspace.focus_panel::<AgentPanel>(window, cx);
132 panel.update(cx, |panel, cx| {
133 panel.deploy_rules_library(action, window, cx)
134 });
135 }
136 })
137 .register_action(|workspace, _: &OpenAgentDiff, window, cx| {
138 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
139 workspace.focus_panel::<AgentPanel>(window, cx);
140 match &panel.read(cx).active_view {
141 ActiveView::Thread { thread, .. } => {
142 let thread = thread.read(cx).thread().clone();
143 AgentDiffPane::deploy_in_workspace(thread, workspace, window, cx);
144 }
145 ActiveView::ExternalAgentThread { .. }
146 | ActiveView::TextThread { .. }
147 | ActiveView::History
148 | ActiveView::Configuration => {}
149 }
150 }
151 })
152 .register_action(|workspace, _: &Follow, window, cx| {
153 workspace.follow(CollaboratorId::Agent, window, cx);
154 })
155 .register_action(|workspace, _: &ExpandMessageEditor, window, cx| {
156 let Some(panel) = workspace.panel::<AgentPanel>(cx) else {
157 return;
158 };
159 workspace.focus_panel::<AgentPanel>(window, cx);
160 panel.update(cx, |panel, cx| {
161 if let Some(message_editor) = panel.active_message_editor() {
162 message_editor.update(cx, |editor, cx| {
163 editor.expand_message_editor(&ExpandMessageEditor, window, cx);
164 });
165 }
166 });
167 })
168 .register_action(|workspace, _: &ToggleNavigationMenu, window, cx| {
169 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
170 workspace.focus_panel::<AgentPanel>(window, cx);
171 panel.update(cx, |panel, cx| {
172 panel.toggle_navigation_menu(&ToggleNavigationMenu, window, cx);
173 });
174 }
175 })
176 .register_action(|workspace, _: &ToggleOptionsMenu, window, cx| {
177 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
178 workspace.focus_panel::<AgentPanel>(window, cx);
179 panel.update(cx, |panel, cx| {
180 panel.toggle_options_menu(&ToggleOptionsMenu, window, cx);
181 });
182 }
183 })
184 .register_action(|workspace, _: &ToggleNewThreadMenu, window, cx| {
185 if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
186 workspace.focus_panel::<AgentPanel>(window, cx);
187 panel.update(cx, |panel, cx| {
188 panel.toggle_new_thread_menu(&ToggleNewThreadMenu, window, cx);
189 });
190 }
191 })
192 .register_action(|workspace, _: &OpenOnboardingModal, window, cx| {
193 AgentOnboardingModal::toggle(workspace, window, cx)
194 })
195 .register_action(|_workspace, _: &ResetOnboarding, window, cx| {
196 window.dispatch_action(workspace::RestoreBanner.boxed_clone(), cx);
197 window.refresh();
198 })
199 .register_action(|_workspace, _: &ResetTrialUpsell, _window, cx| {
200 OnboardingUpsell::set_dismissed(false, cx);
201 })
202 .register_action(|_workspace, _: &ResetTrialEndUpsell, _window, cx| {
203 TrialEndUpsell::set_dismissed(false, cx);
204 });
205 },
206 )
207 .detach();
208}
209
210enum ActiveView {
211 Thread {
212 thread: Entity<ActiveThread>,
213 change_title_editor: Entity<Editor>,
214 message_editor: Entity<MessageEditor>,
215 _subscriptions: Vec<gpui::Subscription>,
216 },
217 ExternalAgentThread {
218 thread_view: Entity<AcpThreadView>,
219 },
220 TextThread {
221 context_editor: Entity<TextThreadEditor>,
222 title_editor: Entity<Editor>,
223 buffer_search_bar: Entity<BufferSearchBar>,
224 _subscriptions: Vec<gpui::Subscription>,
225 },
226 History,
227 Configuration,
228}
229
230enum WhichFontSize {
231 AgentFont,
232 BufferFont,
233 None,
234}
235
236#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
237pub enum AgentType {
238 #[default]
239 Zed,
240 TextThread,
241 Gemini,
242 ClaudeCode,
243 NativeAgent,
244}
245
246impl AgentType {
247 fn label(self) -> impl Into<SharedString> {
248 match self {
249 Self::Zed | Self::TextThread => "Zed Agent",
250 Self::NativeAgent => "Agent 2",
251 Self::Gemini => "Google Gemini",
252 Self::ClaudeCode => "Claude Code",
253 }
254 }
255
256 fn icon(self) -> IconName {
257 match self {
258 Self::Zed | Self::TextThread => IconName::AiZed,
259 Self::NativeAgent => IconName::ZedAssistant,
260 Self::Gemini => IconName::AiGemini,
261 Self::ClaudeCode => IconName::AiClaude,
262 }
263 }
264}
265
266impl ActiveView {
267 pub fn which_font_size_used(&self) -> WhichFontSize {
268 match self {
269 ActiveView::Thread { .. }
270 | ActiveView::ExternalAgentThread { .. }
271 | ActiveView::History => WhichFontSize::AgentFont,
272 ActiveView::TextThread { .. } => WhichFontSize::BufferFont,
273 ActiveView::Configuration => WhichFontSize::None,
274 }
275 }
276
277 pub fn thread(
278 active_thread: Entity<ActiveThread>,
279 message_editor: Entity<MessageEditor>,
280 window: &mut Window,
281 cx: &mut Context<AgentPanel>,
282 ) -> Self {
283 let summary = active_thread.read(cx).summary(cx).or_default();
284
285 let editor = cx.new(|cx| {
286 let mut editor = Editor::single_line(window, cx);
287 editor.set_text(summary.clone(), window, cx);
288 editor
289 });
290
291 let subscriptions = vec![
292 cx.subscribe(&message_editor, |this, _, event, cx| match event {
293 MessageEditorEvent::Changed | MessageEditorEvent::EstimatedTokenCount => {
294 cx.notify();
295 }
296 MessageEditorEvent::ScrollThreadToBottom => match &this.active_view {
297 ActiveView::Thread { thread, .. } => {
298 thread.update(cx, |thread, cx| {
299 thread.scroll_to_bottom(cx);
300 });
301 }
302 ActiveView::ExternalAgentThread { .. } => {}
303 ActiveView::TextThread { .. }
304 | ActiveView::History
305 | ActiveView::Configuration => {}
306 },
307 }),
308 window.subscribe(&editor, cx, {
309 {
310 let thread = active_thread.clone();
311 move |editor, event, window, cx| match event {
312 EditorEvent::BufferEdited => {
313 let new_summary = editor.read(cx).text(cx);
314
315 thread.update(cx, |thread, cx| {
316 thread.thread().update(cx, |thread, cx| {
317 thread.set_summary(new_summary, cx);
318 });
319 })
320 }
321 EditorEvent::Blurred => {
322 if editor.read(cx).text(cx).is_empty() {
323 let summary = thread.read(cx).summary(cx).or_default();
324
325 editor.update(cx, |editor, cx| {
326 editor.set_text(summary, window, cx);
327 });
328 }
329 }
330 _ => {}
331 }
332 }
333 }),
334 cx.subscribe(&active_thread, |_, _, event, cx| match &event {
335 ActiveThreadEvent::EditingMessageTokenCountChanged => {
336 cx.notify();
337 }
338 }),
339 cx.subscribe_in(&active_thread.read(cx).thread().clone(), window, {
340 let editor = editor.clone();
341 move |_, thread, event, window, cx| match event {
342 ThreadEvent::SummaryGenerated => {
343 let summary = thread.read(cx).summary().or_default();
344
345 editor.update(cx, |editor, cx| {
346 editor.set_text(summary, window, cx);
347 })
348 }
349 ThreadEvent::MessageAdded(_) => {
350 cx.notify();
351 }
352 _ => {}
353 }
354 }),
355 ];
356
357 Self::Thread {
358 change_title_editor: editor,
359 thread: active_thread,
360 message_editor: message_editor,
361 _subscriptions: subscriptions,
362 }
363 }
364
365 pub fn prompt_editor(
366 context_editor: Entity<TextThreadEditor>,
367 history_store: Entity<HistoryStore>,
368 language_registry: Arc<LanguageRegistry>,
369 window: &mut Window,
370 cx: &mut App,
371 ) -> Self {
372 let title = context_editor.read(cx).title(cx).to_string();
373
374 let editor = cx.new(|cx| {
375 let mut editor = Editor::single_line(window, cx);
376 editor.set_text(title, window, cx);
377 editor
378 });
379
380 // This is a workaround for `editor.set_text` emitting a `BufferEdited` event, which would
381 // cause a custom summary to be set. The presence of this custom summary would cause
382 // summarization to not happen.
383 let mut suppress_first_edit = true;
384
385 let subscriptions = vec![
386 window.subscribe(&editor, cx, {
387 {
388 let context_editor = context_editor.clone();
389 move |editor, event, window, cx| match event {
390 EditorEvent::BufferEdited => {
391 if suppress_first_edit {
392 suppress_first_edit = false;
393 return;
394 }
395 let new_summary = editor.read(cx).text(cx);
396
397 context_editor.update(cx, |context_editor, cx| {
398 context_editor
399 .context()
400 .update(cx, |assistant_context, cx| {
401 assistant_context.set_custom_summary(new_summary, cx);
402 })
403 })
404 }
405 EditorEvent::Blurred => {
406 if editor.read(cx).text(cx).is_empty() {
407 let summary = context_editor
408 .read(cx)
409 .context()
410 .read(cx)
411 .summary()
412 .or_default();
413
414 editor.update(cx, |editor, cx| {
415 editor.set_text(summary, window, cx);
416 });
417 }
418 }
419 _ => {}
420 }
421 }
422 }),
423 window.subscribe(&context_editor.read(cx).context().clone(), cx, {
424 let editor = editor.clone();
425 move |assistant_context, event, window, cx| match event {
426 ContextEvent::SummaryGenerated => {
427 let summary = assistant_context.read(cx).summary().or_default();
428
429 editor.update(cx, |editor, cx| {
430 editor.set_text(summary, window, cx);
431 })
432 }
433 ContextEvent::PathChanged { old_path, new_path } => {
434 history_store.update(cx, |history_store, cx| {
435 if let Some(old_path) = old_path {
436 history_store
437 .replace_recently_opened_text_thread(old_path, new_path, cx);
438 } else {
439 history_store.push_recently_opened_entry(
440 HistoryEntryId::Context(new_path.clone()),
441 cx,
442 );
443 }
444 });
445 }
446 _ => {}
447 }
448 }),
449 ];
450
451 let buffer_search_bar =
452 cx.new(|cx| BufferSearchBar::new(Some(language_registry), window, cx));
453 buffer_search_bar.update(cx, |buffer_search_bar, cx| {
454 buffer_search_bar.set_active_pane_item(Some(&context_editor), window, cx)
455 });
456
457 Self::TextThread {
458 context_editor,
459 title_editor: editor,
460 buffer_search_bar,
461 _subscriptions: subscriptions,
462 }
463 }
464}
465
466pub struct AgentPanel {
467 workspace: WeakEntity<Workspace>,
468 user_store: Entity<UserStore>,
469 project: Entity<Project>,
470 fs: Arc<dyn Fs>,
471 language_registry: Arc<LanguageRegistry>,
472 thread_store: Entity<ThreadStore>,
473 _default_model_subscription: Subscription,
474 context_store: Entity<TextThreadStore>,
475 prompt_store: Option<Entity<PromptStore>>,
476 inline_assist_context_store: Entity<ContextStore>,
477 configuration: Option<Entity<AgentConfiguration>>,
478 configuration_subscription: Option<Subscription>,
479 local_timezone: UtcOffset,
480 active_view: ActiveView,
481 previous_view: Option<ActiveView>,
482 history_store: Entity<HistoryStore>,
483 history: Entity<ThreadHistory>,
484 acp_history: Entity<AcpThreadHistory>,
485 hovered_recent_history_item: Option<usize>,
486 new_thread_menu_handle: PopoverMenuHandle<ContextMenu>,
487 agent_panel_menu_handle: PopoverMenuHandle<ContextMenu>,
488 assistant_navigation_menu_handle: PopoverMenuHandle<ContextMenu>,
489 assistant_navigation_menu: Option<Entity<ContextMenu>>,
490 width: Option<Pixels>,
491 height: Option<Pixels>,
492 zoomed: bool,
493 pending_serialization: Option<Task<Result<()>>>,
494 onboarding: Entity<AgentPanelOnboarding>,
495 selected_agent: AgentType,
496}
497
498impl AgentPanel {
499 fn serialize(&mut self, cx: &mut Context<Self>) {
500 let width = self.width;
501 let selected_agent = self.selected_agent;
502 self.pending_serialization = Some(cx.background_spawn(async move {
503 KEY_VALUE_STORE
504 .write_kvp(
505 AGENT_PANEL_KEY.into(),
506 serde_json::to_string(&SerializedAgentPanel {
507 width,
508 selected_agent: Some(selected_agent),
509 })?,
510 )
511 .await?;
512 anyhow::Ok(())
513 }));
514 }
515 pub fn load(
516 workspace: WeakEntity<Workspace>,
517 prompt_builder: Arc<PromptBuilder>,
518 mut cx: AsyncWindowContext,
519 ) -> Task<Result<Entity<Self>>> {
520 let prompt_store = cx.update(|_window, cx| PromptStore::global(cx));
521 cx.spawn(async move |cx| {
522 let prompt_store = match prompt_store {
523 Ok(prompt_store) => prompt_store.await.ok(),
524 Err(_) => None,
525 };
526 let tools = cx.new(|_| ToolWorkingSet::default())?;
527 let thread_store = workspace
528 .update(cx, |workspace, cx| {
529 let project = workspace.project().clone();
530 ThreadStore::load(
531 project,
532 tools.clone(),
533 prompt_store.clone(),
534 prompt_builder.clone(),
535 cx,
536 )
537 })?
538 .await?;
539
540 let slash_commands = Arc::new(SlashCommandWorkingSet::default());
541 let context_store = workspace
542 .update(cx, |workspace, cx| {
543 let project = workspace.project().clone();
544 assistant_context::ContextStore::new(
545 project,
546 prompt_builder.clone(),
547 slash_commands,
548 cx,
549 )
550 })?
551 .await?;
552
553 let serialized_panel = if let Some(panel) = cx
554 .background_spawn(async move { KEY_VALUE_STORE.read_kvp(AGENT_PANEL_KEY) })
555 .await
556 .log_err()
557 .flatten()
558 {
559 Some(serde_json::from_str::<SerializedAgentPanel>(&panel)?)
560 } else {
561 None
562 };
563
564 let panel = workspace.update_in(cx, |workspace, window, cx| {
565 let panel = cx.new(|cx| {
566 Self::new(
567 workspace,
568 thread_store,
569 context_store,
570 prompt_store,
571 window,
572 cx,
573 )
574 });
575 if let Some(serialized_panel) = serialized_panel {
576 panel.update(cx, |panel, cx| {
577 panel.width = serialized_panel.width.map(|w| w.round());
578 if let Some(selected_agent) = serialized_panel.selected_agent {
579 panel.selected_agent = selected_agent;
580 panel.new_agent_thread(selected_agent, window, cx);
581 }
582 cx.notify();
583 });
584 }
585 panel
586 })?;
587
588 Ok(panel)
589 })
590 }
591
592 fn new(
593 workspace: &Workspace,
594 thread_store: Entity<ThreadStore>,
595 context_store: Entity<TextThreadStore>,
596 prompt_store: Option<Entity<PromptStore>>,
597 window: &mut Window,
598 cx: &mut Context<Self>,
599 ) -> Self {
600 let thread = thread_store.update(cx, |this, cx| this.create_thread(cx));
601 let fs = workspace.app_state().fs.clone();
602 let user_store = workspace.app_state().user_store.clone();
603 let project = workspace.project();
604 let language_registry = project.read(cx).languages().clone();
605 let client = workspace.client().clone();
606 let workspace = workspace.weak_handle();
607 let weak_self = cx.entity().downgrade();
608
609 let message_editor_context_store =
610 cx.new(|_cx| ContextStore::new(project.downgrade(), Some(thread_store.downgrade())));
611 let inline_assist_context_store =
612 cx.new(|_cx| ContextStore::new(project.downgrade(), Some(thread_store.downgrade())));
613
614 let thread_id = thread.read(cx).id().clone();
615
616 let history_store = cx.new(|cx| {
617 HistoryStore::new(
618 thread_store.clone(),
619 context_store.clone(),
620 [HistoryEntryId::Thread(thread_id)],
621 cx,
622 )
623 });
624
625 let message_editor = cx.new(|cx| {
626 MessageEditor::new(
627 fs.clone(),
628 workspace.clone(),
629 message_editor_context_store.clone(),
630 prompt_store.clone(),
631 thread_store.downgrade(),
632 context_store.downgrade(),
633 Some(history_store.downgrade()),
634 thread.clone(),
635 window,
636 cx,
637 )
638 });
639
640 cx.observe(&history_store, |_, _, cx| cx.notify()).detach();
641
642 let active_thread = cx.new(|cx| {
643 ActiveThread::new(
644 thread.clone(),
645 thread_store.clone(),
646 context_store.clone(),
647 message_editor_context_store.clone(),
648 language_registry.clone(),
649 workspace.clone(),
650 window,
651 cx,
652 )
653 });
654
655 let panel_type = AgentSettings::get_global(cx).default_view;
656 let active_view = match panel_type {
657 DefaultView::Thread => ActiveView::thread(active_thread, message_editor, window, cx),
658 DefaultView::TextThread => {
659 let context =
660 context_store.update(cx, |context_store, cx| context_store.create(cx));
661 let lsp_adapter_delegate = make_lsp_adapter_delegate(&project.clone(), cx).unwrap();
662 let context_editor = cx.new(|cx| {
663 let mut editor = TextThreadEditor::for_context(
664 context,
665 fs.clone(),
666 workspace.clone(),
667 project.clone(),
668 lsp_adapter_delegate,
669 window,
670 cx,
671 );
672 editor.insert_default_prompt(window, cx);
673 editor
674 });
675 ActiveView::prompt_editor(
676 context_editor,
677 history_store.clone(),
678 language_registry.clone(),
679 window,
680 cx,
681 )
682 }
683 };
684
685 AgentDiff::set_active_thread(&workspace, thread.clone(), window, cx);
686
687 let weak_panel = weak_self.clone();
688
689 window.defer(cx, move |window, cx| {
690 let panel = weak_panel.clone();
691 let assistant_navigation_menu =
692 ContextMenu::build_persistent(window, cx, move |mut menu, _window, cx| {
693 if let Some(panel) = panel.upgrade() {
694 menu = Self::populate_recently_opened_menu_section(menu, panel, cx);
695 }
696 menu.action("View All", Box::new(OpenHistory))
697 .end_slot_action(DeleteRecentlyOpenThread.boxed_clone())
698 .fixed_width(px(320.).into())
699 .keep_open_on_confirm(false)
700 .key_context("NavigationMenu")
701 });
702 weak_panel
703 .update(cx, |panel, cx| {
704 cx.subscribe_in(
705 &assistant_navigation_menu,
706 window,
707 |_, menu, _: &DismissEvent, window, cx| {
708 menu.update(cx, |menu, _| {
709 menu.clear_selected();
710 });
711 cx.focus_self(window);
712 },
713 )
714 .detach();
715 panel.assistant_navigation_menu = Some(assistant_navigation_menu);
716 })
717 .ok();
718 });
719
720 let _default_model_subscription = cx.subscribe(
721 &LanguageModelRegistry::global(cx),
722 |this, _, event: &language_model::Event, cx| match event {
723 language_model::Event::DefaultModelChanged => match &this.active_view {
724 ActiveView::Thread { thread, .. } => {
725 thread
726 .read(cx)
727 .thread()
728 .clone()
729 .update(cx, |thread, cx| thread.get_or_init_configured_model(cx));
730 }
731 ActiveView::ExternalAgentThread { .. }
732 | ActiveView::TextThread { .. }
733 | ActiveView::History
734 | ActiveView::Configuration => {}
735 },
736 _ => {}
737 },
738 );
739
740 let onboarding = cx.new(|cx| {
741 AgentPanelOnboarding::new(
742 user_store.clone(),
743 client,
744 |_window, cx| {
745 OnboardingUpsell::set_dismissed(true, cx);
746 },
747 cx,
748 )
749 });
750
751 let acp_history = cx.new(|cx| AcpThreadHistory::new(&project, window, cx));
752 cx.subscribe_in(
753 &acp_history,
754 window,
755 |this, _, event, window, cx| match event {
756 ThreadHistoryEvent::Open(HistoryEntry::AcpThread(thread)) => {
757 let agent_choice = match thread.agent.0.as_ref() {
758 "Claude Code" => Some(ExternalAgent::ClaudeCode),
759 "Gemini" => Some(ExternalAgent::Gemini),
760 "Native Agent" => Some(ExternalAgent::NativeAgent),
761 _ => None,
762 };
763 this.new_external_thread(agent_choice, Some(thread.clone()), window, cx);
764 }
765 ThreadHistoryEvent::Open(HistoryEntry::TextThread(thread)) => {
766 todo!()
767 }
768 },
769 )
770 .detach();
771
772 Self {
773 active_view,
774 workspace,
775 user_store,
776 project: project.clone(),
777 fs: fs.clone(),
778 language_registry,
779 thread_store: thread_store.clone(),
780 _default_model_subscription,
781 context_store,
782 prompt_store,
783 configuration: None,
784 configuration_subscription: None,
785 local_timezone: UtcOffset::from_whole_seconds(
786 chrono::Local::now().offset().local_minus_utc(),
787 )
788 .unwrap(),
789 inline_assist_context_store,
790 previous_view: None,
791 history_store: history_store.clone(),
792 history: cx.new(|cx| ThreadHistory::new(weak_self, history_store, window, cx)),
793 acp_history,
794 hovered_recent_history_item: None,
795 new_thread_menu_handle: PopoverMenuHandle::default(),
796 agent_panel_menu_handle: PopoverMenuHandle::default(),
797 assistant_navigation_menu_handle: PopoverMenuHandle::default(),
798 assistant_navigation_menu: None,
799 width: None,
800 height: None,
801 zoomed: false,
802 pending_serialization: None,
803 onboarding,
804 selected_agent: AgentType::default(),
805 }
806 }
807
808 pub fn toggle_focus(
809 workspace: &mut Workspace,
810 _: &ToggleFocus,
811 window: &mut Window,
812 cx: &mut Context<Workspace>,
813 ) {
814 if workspace
815 .panel::<Self>(cx)
816 .is_some_and(|panel| panel.read(cx).enabled(cx))
817 && !DisableAiSettings::get_global(cx).disable_ai
818 {
819 workspace.toggle_panel_focus::<Self>(window, cx);
820 }
821 }
822
823 pub(crate) fn local_timezone(&self) -> UtcOffset {
824 self.local_timezone
825 }
826
827 pub(crate) fn prompt_store(&self) -> &Option<Entity<PromptStore>> {
828 &self.prompt_store
829 }
830
831 pub(crate) fn inline_assist_context_store(&self) -> &Entity<ContextStore> {
832 &self.inline_assist_context_store
833 }
834
835 pub(crate) fn thread_store(&self) -> &Entity<ThreadStore> {
836 &self.thread_store
837 }
838
839 pub(crate) fn text_thread_store(&self) -> &Entity<TextThreadStore> {
840 &self.context_store
841 }
842
843 fn cancel(&mut self, _: &editor::actions::Cancel, window: &mut Window, cx: &mut Context<Self>) {
844 match &self.active_view {
845 ActiveView::Thread { thread, .. } => {
846 thread.update(cx, |thread, cx| thread.cancel_last_completion(window, cx));
847 }
848 ActiveView::ExternalAgentThread { .. }
849 | ActiveView::TextThread { .. }
850 | ActiveView::History
851 | ActiveView::Configuration => {}
852 }
853 }
854
855 fn active_message_editor(&self) -> Option<&Entity<MessageEditor>> {
856 match &self.active_view {
857 ActiveView::Thread { message_editor, .. } => Some(message_editor),
858 ActiveView::ExternalAgentThread { .. }
859 | ActiveView::TextThread { .. }
860 | ActiveView::History
861 | ActiveView::Configuration => None,
862 }
863 }
864
865 fn new_thread(&mut self, action: &NewThread, window: &mut Window, cx: &mut Context<Self>) {
866 // Preserve chat box text when using creating new thread
867 let preserved_text = self
868 .active_message_editor()
869 .map(|editor| editor.read(cx).get_text(cx).trim().to_string());
870
871 let thread = self
872 .thread_store
873 .update(cx, |this, cx| this.create_thread(cx));
874
875 let context_store = cx.new(|_cx| {
876 ContextStore::new(
877 self.project.downgrade(),
878 Some(self.thread_store.downgrade()),
879 )
880 });
881
882 if let Some(other_thread_id) = action.from_thread_id.clone() {
883 let other_thread_task = self.thread_store.update(cx, |this, cx| {
884 this.open_thread(&other_thread_id, window, cx)
885 });
886
887 cx.spawn({
888 let context_store = context_store.clone();
889
890 async move |_panel, cx| {
891 let other_thread = other_thread_task.await?;
892
893 context_store.update(cx, |this, cx| {
894 this.add_thread(other_thread, false, cx);
895 })?;
896 anyhow::Ok(())
897 }
898 })
899 .detach_and_log_err(cx);
900 }
901
902 let active_thread = cx.new(|cx| {
903 ActiveThread::new(
904 thread.clone(),
905 self.thread_store.clone(),
906 self.context_store.clone(),
907 context_store.clone(),
908 self.language_registry.clone(),
909 self.workspace.clone(),
910 window,
911 cx,
912 )
913 });
914
915 let message_editor = cx.new(|cx| {
916 MessageEditor::new(
917 self.fs.clone(),
918 self.workspace.clone(),
919 context_store.clone(),
920 self.prompt_store.clone(),
921 self.thread_store.downgrade(),
922 self.context_store.downgrade(),
923 Some(self.history_store.downgrade()),
924 thread.clone(),
925 window,
926 cx,
927 )
928 });
929
930 if let Some(text) = preserved_text {
931 message_editor.update(cx, |editor, cx| {
932 editor.set_text(text, window, cx);
933 });
934 }
935
936 message_editor.focus_handle(cx).focus(window);
937
938 let thread_view = ActiveView::thread(active_thread.clone(), message_editor, window, cx);
939 self.set_active_view(thread_view, window, cx);
940
941 AgentDiff::set_active_thread(&self.workspace, thread.clone(), window, cx);
942 }
943
944 fn new_prompt_editor(&mut self, window: &mut Window, cx: &mut Context<Self>) {
945 let context = self
946 .context_store
947 .update(cx, |context_store, cx| context_store.create(cx));
948 let lsp_adapter_delegate = make_lsp_adapter_delegate(&self.project, cx)
949 .log_err()
950 .flatten();
951
952 let context_editor = cx.new(|cx| {
953 let mut editor = TextThreadEditor::for_context(
954 context,
955 self.fs.clone(),
956 self.workspace.clone(),
957 self.project.clone(),
958 lsp_adapter_delegate,
959 window,
960 cx,
961 );
962 editor.insert_default_prompt(window, cx);
963 editor
964 });
965
966 self.set_active_view(
967 ActiveView::prompt_editor(
968 context_editor.clone(),
969 self.history_store.clone(),
970 self.language_registry.clone(),
971 window,
972 cx,
973 ),
974 window,
975 cx,
976 );
977 context_editor.focus_handle(cx).focus(window);
978 }
979
980 fn new_external_thread(
981 &mut self,
982 agent_choice: Option<crate::ExternalAgent>,
983 restore_thread: Option<AcpThreadMetadata>,
984 window: &mut Window,
985 cx: &mut Context<Self>,
986 ) {
987 let workspace = self.workspace.clone();
988 let project = self.project.clone();
989 let fs = self.fs.clone();
990
991 const LAST_USED_EXTERNAL_AGENT_KEY: &str = "agent_panel__last_used_external_agent";
992
993 #[derive(Default, Serialize, Deserialize)]
994 struct LastUsedExternalAgent {
995 agent: crate::ExternalAgent,
996 }
997
998 let thread_store = self.thread_store.clone();
999 let text_thread_store = self.context_store.clone();
1000
1001 cx.spawn_in(window, async move |this, cx| {
1002 let server: Rc<dyn AgentServer> = match agent_choice {
1003 Some(agent) => {
1004 cx.background_spawn(async move {
1005 if let Some(serialized) =
1006 serde_json::to_string(&LastUsedExternalAgent { agent }).log_err()
1007 {
1008 KEY_VALUE_STORE
1009 .write_kvp(LAST_USED_EXTERNAL_AGENT_KEY.to_string(), serialized)
1010 .await
1011 .log_err();
1012 }
1013 })
1014 .detach();
1015
1016 agent.server(fs)
1017 }
1018 None => cx
1019 .background_spawn(async move {
1020 KEY_VALUE_STORE.read_kvp(LAST_USED_EXTERNAL_AGENT_KEY)
1021 })
1022 .await
1023 .log_err()
1024 .flatten()
1025 .and_then(|value| {
1026 serde_json::from_str::<LastUsedExternalAgent>(&value).log_err()
1027 })
1028 .unwrap_or_default()
1029 .agent
1030 .server(fs),
1031 };
1032
1033 this.update_in(cx, |this, window, cx| {
1034 let thread_view = cx.new(|cx| {
1035 crate::acp::AcpThreadView::new(
1036 server,
1037 workspace.clone(),
1038 project,
1039 thread_store.clone(),
1040 text_thread_store.clone(),
1041 restore_thread,
1042 window,
1043 cx,
1044 )
1045 });
1046
1047 this.set_active_view(ActiveView::ExternalAgentThread { thread_view }, window, cx);
1048 })
1049 })
1050 .detach_and_log_err(cx);
1051 }
1052
1053 fn deploy_rules_library(
1054 &mut self,
1055 action: &OpenRulesLibrary,
1056 _window: &mut Window,
1057 cx: &mut Context<Self>,
1058 ) {
1059 open_rules_library(
1060 self.language_registry.clone(),
1061 Box::new(PromptLibraryInlineAssist::new(self.workspace.clone())),
1062 Rc::new(|| {
1063 Rc::new(SlashCommandCompletionProvider::new(
1064 Arc::new(SlashCommandWorkingSet::default()),
1065 None,
1066 None,
1067 ))
1068 }),
1069 action
1070 .prompt_to_select
1071 .map(|uuid| UserPromptId(uuid).into()),
1072 cx,
1073 )
1074 .detach_and_log_err(cx);
1075 }
1076
1077 fn open_history(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1078 if matches!(self.active_view, ActiveView::History) {
1079 if let Some(previous_view) = self.previous_view.take() {
1080 self.set_active_view(previous_view, window, cx);
1081 }
1082 } else {
1083 self.thread_store
1084 .update(cx, |thread_store, cx| thread_store.reload(cx))
1085 .detach_and_log_err(cx);
1086 self.set_active_view(ActiveView::History, window, cx);
1087 }
1088 cx.notify();
1089 }
1090
1091 pub(crate) fn open_saved_prompt_editor(
1092 &mut self,
1093 path: Arc<Path>,
1094 window: &mut Window,
1095 cx: &mut Context<Self>,
1096 ) -> Task<Result<()>> {
1097 let context = self
1098 .context_store
1099 .update(cx, |store, cx| store.open_local_context(path, cx));
1100 cx.spawn_in(window, async move |this, cx| {
1101 let context = context.await?;
1102 this.update_in(cx, |this, window, cx| {
1103 this.open_prompt_editor(context, window, cx);
1104 })
1105 })
1106 }
1107
1108 pub(crate) fn open_prompt_editor(
1109 &mut self,
1110 context: Entity<AssistantContext>,
1111 window: &mut Window,
1112 cx: &mut Context<Self>,
1113 ) {
1114 let lsp_adapter_delegate = make_lsp_adapter_delegate(&self.project.clone(), cx)
1115 .log_err()
1116 .flatten();
1117 let editor = cx.new(|cx| {
1118 TextThreadEditor::for_context(
1119 context,
1120 self.fs.clone(),
1121 self.workspace.clone(),
1122 self.project.clone(),
1123 lsp_adapter_delegate,
1124 window,
1125 cx,
1126 )
1127 });
1128 self.set_active_view(
1129 ActiveView::prompt_editor(
1130 editor.clone(),
1131 self.history_store.clone(),
1132 self.language_registry.clone(),
1133 window,
1134 cx,
1135 ),
1136 window,
1137 cx,
1138 );
1139 }
1140
1141 pub(crate) fn open_thread_by_id(
1142 &mut self,
1143 thread_id: &ThreadId,
1144 window: &mut Window,
1145 cx: &mut Context<Self>,
1146 ) -> Task<Result<()>> {
1147 let open_thread_task = self
1148 .thread_store
1149 .update(cx, |this, cx| this.open_thread(thread_id, window, cx));
1150 cx.spawn_in(window, async move |this, cx| {
1151 let thread = open_thread_task.await?;
1152 this.update_in(cx, |this, window, cx| {
1153 this.open_thread(thread, window, cx);
1154 anyhow::Ok(())
1155 })??;
1156 Ok(())
1157 })
1158 }
1159
1160 pub(crate) fn open_thread(
1161 &mut self,
1162 thread: Entity<Thread>,
1163 window: &mut Window,
1164 cx: &mut Context<Self>,
1165 ) {
1166 let context_store = cx.new(|_cx| {
1167 ContextStore::new(
1168 self.project.downgrade(),
1169 Some(self.thread_store.downgrade()),
1170 )
1171 });
1172
1173 let active_thread = cx.new(|cx| {
1174 ActiveThread::new(
1175 thread.clone(),
1176 self.thread_store.clone(),
1177 self.context_store.clone(),
1178 context_store.clone(),
1179 self.language_registry.clone(),
1180 self.workspace.clone(),
1181 window,
1182 cx,
1183 )
1184 });
1185
1186 let message_editor = cx.new(|cx| {
1187 MessageEditor::new(
1188 self.fs.clone(),
1189 self.workspace.clone(),
1190 context_store,
1191 self.prompt_store.clone(),
1192 self.thread_store.downgrade(),
1193 self.context_store.downgrade(),
1194 Some(self.history_store.downgrade()),
1195 thread.clone(),
1196 window,
1197 cx,
1198 )
1199 });
1200 message_editor.focus_handle(cx).focus(window);
1201
1202 let thread_view = ActiveView::thread(active_thread.clone(), message_editor, window, cx);
1203 self.set_active_view(thread_view, window, cx);
1204 AgentDiff::set_active_thread(&self.workspace, thread.clone(), window, cx);
1205 }
1206
1207 pub fn go_back(&mut self, _: &workspace::GoBack, window: &mut Window, cx: &mut Context<Self>) {
1208 match self.active_view {
1209 ActiveView::Configuration | ActiveView::History => {
1210 if let Some(previous_view) = self.previous_view.take() {
1211 self.active_view = previous_view;
1212
1213 match &self.active_view {
1214 ActiveView::Thread { message_editor, .. } => {
1215 message_editor.focus_handle(cx).focus(window);
1216 }
1217 ActiveView::ExternalAgentThread { thread_view } => {
1218 thread_view.focus_handle(cx).focus(window);
1219 }
1220 ActiveView::TextThread { context_editor, .. } => {
1221 context_editor.focus_handle(cx).focus(window);
1222 }
1223 ActiveView::History | ActiveView::Configuration => {}
1224 }
1225 }
1226 cx.notify();
1227 }
1228 _ => {}
1229 }
1230 }
1231
1232 pub fn toggle_navigation_menu(
1233 &mut self,
1234 _: &ToggleNavigationMenu,
1235 window: &mut Window,
1236 cx: &mut Context<Self>,
1237 ) {
1238 self.assistant_navigation_menu_handle.toggle(window, cx);
1239 }
1240
1241 pub fn toggle_options_menu(
1242 &mut self,
1243 _: &ToggleOptionsMenu,
1244 window: &mut Window,
1245 cx: &mut Context<Self>,
1246 ) {
1247 self.agent_panel_menu_handle.toggle(window, cx);
1248 }
1249
1250 pub fn toggle_new_thread_menu(
1251 &mut self,
1252 _: &ToggleNewThreadMenu,
1253 window: &mut Window,
1254 cx: &mut Context<Self>,
1255 ) {
1256 self.new_thread_menu_handle.toggle(window, cx);
1257 }
1258
1259 pub fn increase_font_size(
1260 &mut self,
1261 action: &IncreaseBufferFontSize,
1262 _: &mut Window,
1263 cx: &mut Context<Self>,
1264 ) {
1265 self.handle_font_size_action(action.persist, px(1.0), cx);
1266 }
1267
1268 pub fn decrease_font_size(
1269 &mut self,
1270 action: &DecreaseBufferFontSize,
1271 _: &mut Window,
1272 cx: &mut Context<Self>,
1273 ) {
1274 self.handle_font_size_action(action.persist, px(-1.0), cx);
1275 }
1276
1277 fn handle_font_size_action(&mut self, persist: bool, delta: Pixels, cx: &mut Context<Self>) {
1278 match self.active_view.which_font_size_used() {
1279 WhichFontSize::AgentFont => {
1280 if persist {
1281 update_settings_file::<ThemeSettings>(
1282 self.fs.clone(),
1283 cx,
1284 move |settings, cx| {
1285 let agent_font_size =
1286 ThemeSettings::get_global(cx).agent_font_size(cx) + delta;
1287 let _ = settings
1288 .agent_font_size
1289 .insert(Some(theme::clamp_font_size(agent_font_size).into()));
1290 },
1291 );
1292 } else {
1293 theme::adjust_agent_font_size(cx, |size| size + delta);
1294 }
1295 }
1296 WhichFontSize::BufferFont => {
1297 // Prompt editor uses the buffer font size, so allow the action to propagate to the
1298 // default handler that changes that font size.
1299 cx.propagate();
1300 }
1301 WhichFontSize::None => {}
1302 }
1303 }
1304
1305 pub fn reset_font_size(
1306 &mut self,
1307 action: &ResetBufferFontSize,
1308 _: &mut Window,
1309 cx: &mut Context<Self>,
1310 ) {
1311 if action.persist {
1312 update_settings_file::<ThemeSettings>(self.fs.clone(), cx, move |settings, _| {
1313 settings.agent_font_size = None;
1314 });
1315 } else {
1316 theme::reset_agent_font_size(cx);
1317 }
1318 }
1319
1320 pub fn toggle_zoom(&mut self, _: &ToggleZoom, window: &mut Window, cx: &mut Context<Self>) {
1321 if self.zoomed {
1322 cx.emit(PanelEvent::ZoomOut);
1323 } else {
1324 if !self.focus_handle(cx).contains_focused(window, cx) {
1325 cx.focus_self(window);
1326 }
1327 cx.emit(PanelEvent::ZoomIn);
1328 }
1329 }
1330
1331 pub fn open_agent_diff(
1332 &mut self,
1333 _: &OpenAgentDiff,
1334 window: &mut Window,
1335 cx: &mut Context<Self>,
1336 ) {
1337 match &self.active_view {
1338 ActiveView::Thread { thread, .. } => {
1339 let thread = thread.read(cx).thread().clone();
1340 self.workspace
1341 .update(cx, |workspace, cx| {
1342 AgentDiffPane::deploy_in_workspace(
1343 AgentDiffThread::Native(thread),
1344 workspace,
1345 window,
1346 cx,
1347 )
1348 })
1349 .log_err();
1350 }
1351 ActiveView::ExternalAgentThread { .. }
1352 | ActiveView::TextThread { .. }
1353 | ActiveView::History
1354 | ActiveView::Configuration => {}
1355 }
1356 }
1357
1358 pub(crate) fn open_configuration(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1359 let context_server_store = self.project.read(cx).context_server_store();
1360 let tools = self.thread_store.read(cx).tools();
1361 let fs = self.fs.clone();
1362
1363 self.set_active_view(ActiveView::Configuration, window, cx);
1364 self.configuration = Some(cx.new(|cx| {
1365 AgentConfiguration::new(
1366 fs,
1367 context_server_store,
1368 tools,
1369 self.language_registry.clone(),
1370 self.workspace.clone(),
1371 window,
1372 cx,
1373 )
1374 }));
1375
1376 if let Some(configuration) = self.configuration.as_ref() {
1377 self.configuration_subscription = Some(cx.subscribe_in(
1378 configuration,
1379 window,
1380 Self::handle_agent_configuration_event,
1381 ));
1382
1383 configuration.focus_handle(cx).focus(window);
1384 }
1385 }
1386
1387 pub(crate) fn open_active_thread_as_markdown(
1388 &mut self,
1389 _: &OpenActiveThreadAsMarkdown,
1390 window: &mut Window,
1391 cx: &mut Context<Self>,
1392 ) {
1393 let Some(workspace) = self.workspace.upgrade() else {
1394 return;
1395 };
1396
1397 match &self.active_view {
1398 ActiveView::Thread { thread, .. } => {
1399 active_thread::open_active_thread_as_markdown(
1400 thread.read(cx).thread().clone(),
1401 workspace,
1402 window,
1403 cx,
1404 )
1405 .detach_and_log_err(cx);
1406 }
1407 ActiveView::ExternalAgentThread { thread_view } => {
1408 thread_view
1409 .update(cx, |thread_view, cx| {
1410 thread_view.open_thread_as_markdown(workspace, window, cx)
1411 })
1412 .detach_and_log_err(cx);
1413 }
1414 ActiveView::TextThread { .. } | ActiveView::History | ActiveView::Configuration => {}
1415 }
1416 }
1417
1418 fn handle_agent_configuration_event(
1419 &mut self,
1420 _entity: &Entity<AgentConfiguration>,
1421 event: &AssistantConfigurationEvent,
1422 window: &mut Window,
1423 cx: &mut Context<Self>,
1424 ) {
1425 match event {
1426 AssistantConfigurationEvent::NewThread(provider) => {
1427 if LanguageModelRegistry::read_global(cx)
1428 .default_model()
1429 .map_or(true, |model| model.provider.id() != provider.id())
1430 {
1431 if let Some(model) = provider.default_model(cx) {
1432 update_settings_file::<AgentSettings>(
1433 self.fs.clone(),
1434 cx,
1435 move |settings, _| settings.set_model(model),
1436 );
1437 }
1438 }
1439
1440 self.new_thread(&NewThread::default(), window, cx);
1441 if let Some((thread, model)) =
1442 self.active_thread(cx).zip(provider.default_model(cx))
1443 {
1444 thread.update(cx, |thread, cx| {
1445 thread.set_configured_model(
1446 Some(ConfiguredModel {
1447 provider: provider.clone(),
1448 model,
1449 }),
1450 cx,
1451 );
1452 });
1453 }
1454 }
1455 }
1456 }
1457
1458 pub(crate) fn active_thread(&self, cx: &App) -> Option<Entity<Thread>> {
1459 match &self.active_view {
1460 ActiveView::Thread { thread, .. } => Some(thread.read(cx).thread().clone()),
1461 _ => None,
1462 }
1463 }
1464
1465 pub(crate) fn delete_thread(
1466 &mut self,
1467 thread_id: &ThreadId,
1468 cx: &mut Context<Self>,
1469 ) -> Task<Result<()>> {
1470 self.thread_store
1471 .update(cx, |this, cx| this.delete_thread(thread_id, cx))
1472 }
1473
1474 fn continue_conversation(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1475 let ActiveView::Thread { thread, .. } = &self.active_view else {
1476 return;
1477 };
1478
1479 let thread_state = thread.read(cx).thread().read(cx);
1480 if !thread_state.tool_use_limit_reached() {
1481 return;
1482 }
1483
1484 let model = thread_state.configured_model().map(|cm| cm.model.clone());
1485 if let Some(model) = model {
1486 thread.update(cx, |active_thread, cx| {
1487 active_thread.thread().update(cx, |thread, cx| {
1488 thread.insert_invisible_continue_message(cx);
1489 thread.advance_prompt_id();
1490 thread.send_to_model(
1491 model,
1492 CompletionIntent::UserPrompt,
1493 Some(window.window_handle()),
1494 cx,
1495 );
1496 });
1497 });
1498 } else {
1499 log::warn!("No configured model available for continuation");
1500 }
1501 }
1502
1503 fn toggle_burn_mode(
1504 &mut self,
1505 _: &ToggleBurnMode,
1506 _window: &mut Window,
1507 cx: &mut Context<Self>,
1508 ) {
1509 let ActiveView::Thread { thread, .. } = &self.active_view else {
1510 return;
1511 };
1512
1513 thread.update(cx, |active_thread, cx| {
1514 active_thread.thread().update(cx, |thread, _cx| {
1515 let current_mode = thread.completion_mode();
1516
1517 thread.set_completion_mode(match current_mode {
1518 CompletionMode::Burn => CompletionMode::Normal,
1519 CompletionMode::Normal => CompletionMode::Burn,
1520 });
1521 });
1522 });
1523 }
1524
1525 pub(crate) fn active_context_editor(&self) -> Option<Entity<TextThreadEditor>> {
1526 match &self.active_view {
1527 ActiveView::TextThread { context_editor, .. } => Some(context_editor.clone()),
1528 _ => None,
1529 }
1530 }
1531
1532 pub(crate) fn delete_context(
1533 &mut self,
1534 path: Arc<Path>,
1535 cx: &mut Context<Self>,
1536 ) -> Task<Result<()>> {
1537 self.context_store
1538 .update(cx, |this, cx| this.delete_local_context(path, cx))
1539 }
1540
1541 fn set_active_view(
1542 &mut self,
1543 new_view: ActiveView,
1544 window: &mut Window,
1545 cx: &mut Context<Self>,
1546 ) {
1547 let current_is_history = matches!(self.active_view, ActiveView::History);
1548 let new_is_history = matches!(new_view, ActiveView::History);
1549
1550 let current_is_config = matches!(self.active_view, ActiveView::Configuration);
1551 let new_is_config = matches!(new_view, ActiveView::Configuration);
1552
1553 let current_is_special = current_is_history || current_is_config;
1554 let new_is_special = new_is_history || new_is_config;
1555
1556 match &self.active_view {
1557 ActiveView::Thread { thread, .. } => {
1558 let thread = thread.read(cx);
1559 if thread.is_empty() {
1560 let id = thread.thread().read(cx).id().clone();
1561 self.history_store.update(cx, |store, cx| {
1562 store.remove_recently_opened_thread(id, cx);
1563 });
1564 }
1565 }
1566 _ => {}
1567 }
1568
1569 match &new_view {
1570 ActiveView::Thread { thread, .. } => self.history_store.update(cx, |store, cx| {
1571 let id = thread.read(cx).thread().read(cx).id().clone();
1572 store.push_recently_opened_entry(HistoryEntryId::Thread(id), cx);
1573 }),
1574 ActiveView::TextThread { context_editor, .. } => {
1575 self.history_store.update(cx, |store, cx| {
1576 if let Some(path) = context_editor.read(cx).context().read(cx).path() {
1577 store.push_recently_opened_entry(HistoryEntryId::Context(path.clone()), cx)
1578 }
1579 })
1580 }
1581 ActiveView::ExternalAgentThread { .. } => {}
1582 ActiveView::History | ActiveView::Configuration => {}
1583 }
1584
1585 if current_is_special && !new_is_special {
1586 self.active_view = new_view;
1587 } else if !current_is_special && new_is_special {
1588 self.previous_view = Some(std::mem::replace(&mut self.active_view, new_view));
1589 } else {
1590 if !new_is_special {
1591 self.previous_view = None;
1592 }
1593 self.active_view = new_view;
1594 }
1595
1596 self.focus_handle(cx).focus(window);
1597 }
1598
1599 fn populate_recently_opened_menu_section(
1600 mut menu: ContextMenu,
1601 panel: Entity<Self>,
1602 cx: &mut Context<ContextMenu>,
1603 ) -> ContextMenu {
1604 let entries = panel
1605 .read(cx)
1606 .history_store
1607 .read(cx)
1608 .recently_opened_entries(cx);
1609
1610 if entries.is_empty() {
1611 return menu;
1612 }
1613
1614 menu = menu.header("Recently Opened");
1615
1616 for entry in entries {
1617 let title = entry.title().clone();
1618 let id = entry.id();
1619
1620 menu = menu.entry_with_end_slot_on_hover(
1621 title,
1622 None,
1623 {
1624 let panel = panel.downgrade();
1625 let id = id.clone();
1626 move |window, cx| {
1627 let id = id.clone();
1628 panel
1629 .update(cx, move |this, cx| match id {
1630 HistoryEntryId::Thread(id) => this
1631 .open_thread_by_id(&id, window, cx)
1632 .detach_and_log_err(cx),
1633 HistoryEntryId::Context(path) => this
1634 .open_saved_prompt_editor(path.clone(), window, cx)
1635 .detach_and_log_err(cx),
1636 })
1637 .ok();
1638 }
1639 },
1640 IconName::Close,
1641 "Close Entry".into(),
1642 {
1643 let panel = panel.downgrade();
1644 let id = id.clone();
1645 move |_window, cx| {
1646 panel
1647 .update(cx, |this, cx| {
1648 this.history_store.update(cx, |history_store, cx| {
1649 history_store.remove_recently_opened_entry(&id, cx);
1650 });
1651 })
1652 .ok();
1653 }
1654 },
1655 );
1656 }
1657
1658 menu = menu.separator();
1659
1660 menu
1661 }
1662
1663 pub fn set_selected_agent(
1664 &mut self,
1665 agent: AgentType,
1666 window: &mut Window,
1667 cx: &mut Context<Self>,
1668 ) {
1669 if self.selected_agent != agent {
1670 self.selected_agent = agent;
1671 self.serialize(cx);
1672 self.new_agent_thread(agent, window, cx);
1673 }
1674 }
1675
1676 pub fn selected_agent(&self) -> AgentType {
1677 self.selected_agent
1678 }
1679
1680 pub fn new_agent_thread(
1681 &mut self,
1682 agent: AgentType,
1683 window: &mut Window,
1684 cx: &mut Context<Self>,
1685 ) {
1686 match agent {
1687 AgentType::Zed => {
1688 window.dispatch_action(
1689 NewThread {
1690 from_thread_id: None,
1691 }
1692 .boxed_clone(),
1693 cx,
1694 );
1695 }
1696 AgentType::TextThread => {
1697 window.dispatch_action(NewTextThread.boxed_clone(), cx);
1698 }
1699 AgentType::NativeAgent => {
1700 self.new_external_thread(Some(crate::ExternalAgent::NativeAgent), window, cx)
1701 }
1702 AgentType::Gemini => {
1703 self.new_external_thread(Some(crate::ExternalAgent::Gemini), window, cx)
1704 }
1705 AgentType::ClaudeCode => {
1706 self.new_external_thread(Some(crate::ExternalAgent::ClaudeCode), window, cx)
1707 }
1708 }
1709 }
1710}
1711
1712impl Focusable for AgentPanel {
1713 fn focus_handle(&self, cx: &App) -> FocusHandle {
1714 match &self.active_view {
1715 ActiveView::Thread { message_editor, .. } => message_editor.focus_handle(cx),
1716 ActiveView::ExternalAgentThread { thread_view, .. } => thread_view.focus_handle(cx),
1717 ActiveView::History => {
1718 if cx.has_flag::<feature_flags::AcpFeatureFlag>() {
1719 self.acp_history.focus_handle(cx)
1720 } else {
1721 self.history.focus_handle(cx)
1722 }
1723 }
1724
1725 ActiveView::TextThread { context_editor, .. } => context_editor.focus_handle(cx),
1726 ActiveView::Configuration => {
1727 if let Some(configuration) = self.configuration.as_ref() {
1728 configuration.focus_handle(cx)
1729 } else {
1730 cx.focus_handle()
1731 }
1732 }
1733 }
1734 }
1735}
1736
1737fn agent_panel_dock_position(cx: &App) -> DockPosition {
1738 match AgentSettings::get_global(cx).dock {
1739 AgentDockPosition::Left => DockPosition::Left,
1740 AgentDockPosition::Bottom => DockPosition::Bottom,
1741 AgentDockPosition::Right => DockPosition::Right,
1742 }
1743}
1744
1745impl EventEmitter<PanelEvent> for AgentPanel {}
1746
1747impl Panel for AgentPanel {
1748 fn persistent_name() -> &'static str {
1749 "AgentPanel"
1750 }
1751
1752 fn position(&self, _window: &Window, cx: &App) -> DockPosition {
1753 agent_panel_dock_position(cx)
1754 }
1755
1756 fn position_is_valid(&self, position: DockPosition) -> bool {
1757 position != DockPosition::Bottom
1758 }
1759
1760 fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
1761 settings::update_settings_file::<AgentSettings>(self.fs.clone(), cx, move |settings, _| {
1762 let dock = match position {
1763 DockPosition::Left => AgentDockPosition::Left,
1764 DockPosition::Bottom => AgentDockPosition::Bottom,
1765 DockPosition::Right => AgentDockPosition::Right,
1766 };
1767 settings.set_dock(dock);
1768 });
1769 }
1770
1771 fn size(&self, window: &Window, cx: &App) -> Pixels {
1772 let settings = AgentSettings::get_global(cx);
1773 match self.position(window, cx) {
1774 DockPosition::Left | DockPosition::Right => {
1775 self.width.unwrap_or(settings.default_width)
1776 }
1777 DockPosition::Bottom => self.height.unwrap_or(settings.default_height),
1778 }
1779 }
1780
1781 fn set_size(&mut self, size: Option<Pixels>, window: &mut Window, cx: &mut Context<Self>) {
1782 match self.position(window, cx) {
1783 DockPosition::Left | DockPosition::Right => self.width = size,
1784 DockPosition::Bottom => self.height = size,
1785 }
1786 self.serialize(cx);
1787 cx.notify();
1788 }
1789
1790 fn set_active(&mut self, _active: bool, _window: &mut Window, _cx: &mut Context<Self>) {}
1791
1792 fn remote_id() -> Option<proto::PanelId> {
1793 Some(proto::PanelId::AssistantPanel)
1794 }
1795
1796 fn icon(&self, _window: &Window, cx: &App) -> Option<IconName> {
1797 (self.enabled(cx) && AgentSettings::get_global(cx).button).then_some(IconName::ZedAssistant)
1798 }
1799
1800 fn icon_tooltip(&self, _window: &Window, _cx: &App) -> Option<&'static str> {
1801 Some("Agent Panel")
1802 }
1803
1804 fn toggle_action(&self) -> Box<dyn Action> {
1805 Box::new(ToggleFocus)
1806 }
1807
1808 fn activation_priority(&self) -> u32 {
1809 3
1810 }
1811
1812 fn enabled(&self, cx: &App) -> bool {
1813 DisableAiSettings::get_global(cx).disable_ai.not() && AgentSettings::get_global(cx).enabled
1814 }
1815
1816 fn is_zoomed(&self, _window: &Window, _cx: &App) -> bool {
1817 self.zoomed
1818 }
1819
1820 fn set_zoomed(&mut self, zoomed: bool, _window: &mut Window, cx: &mut Context<Self>) {
1821 self.zoomed = zoomed;
1822 cx.notify();
1823 }
1824}
1825
1826impl AgentPanel {
1827 fn render_title_view(&self, _window: &mut Window, cx: &Context<Self>) -> AnyElement {
1828 const LOADING_SUMMARY_PLACEHOLDER: &str = "Loading Summary…";
1829
1830 let content = match &self.active_view {
1831 ActiveView::Thread {
1832 thread: active_thread,
1833 change_title_editor,
1834 ..
1835 } => {
1836 let state = {
1837 let active_thread = active_thread.read(cx);
1838 if active_thread.is_empty() {
1839 &ThreadSummary::Pending
1840 } else {
1841 active_thread.summary(cx)
1842 }
1843 };
1844
1845 match state {
1846 ThreadSummary::Pending => Label::new(ThreadSummary::DEFAULT.clone())
1847 .truncate()
1848 .into_any_element(),
1849 ThreadSummary::Generating => Label::new(LOADING_SUMMARY_PLACEHOLDER)
1850 .truncate()
1851 .into_any_element(),
1852 ThreadSummary::Ready(_) => div()
1853 .w_full()
1854 .child(change_title_editor.clone())
1855 .into_any_element(),
1856 ThreadSummary::Error => h_flex()
1857 .w_full()
1858 .child(change_title_editor.clone())
1859 .child(
1860 IconButton::new("retry-summary-generation", IconName::RotateCcw)
1861 .icon_size(IconSize::Small)
1862 .on_click({
1863 let active_thread = active_thread.clone();
1864 move |_, _window, cx| {
1865 active_thread.update(cx, |thread, cx| {
1866 thread.regenerate_summary(cx);
1867 });
1868 }
1869 })
1870 .tooltip(move |_window, cx| {
1871 cx.new(|_| {
1872 Tooltip::new("Failed to generate title")
1873 .meta("Click to try again")
1874 })
1875 .into()
1876 }),
1877 )
1878 .into_any_element(),
1879 }
1880 }
1881 ActiveView::ExternalAgentThread { thread_view } => {
1882 Label::new(thread_view.read(cx).title(cx))
1883 .truncate()
1884 .into_any_element()
1885 }
1886 ActiveView::TextThread {
1887 title_editor,
1888 context_editor,
1889 ..
1890 } => {
1891 let summary = context_editor.read(cx).context().read(cx).summary();
1892
1893 match summary {
1894 ContextSummary::Pending => Label::new(ContextSummary::DEFAULT)
1895 .truncate()
1896 .into_any_element(),
1897 ContextSummary::Content(summary) => {
1898 if summary.done {
1899 div()
1900 .w_full()
1901 .child(title_editor.clone())
1902 .into_any_element()
1903 } else {
1904 Label::new(LOADING_SUMMARY_PLACEHOLDER)
1905 .truncate()
1906 .into_any_element()
1907 }
1908 }
1909 ContextSummary::Error => h_flex()
1910 .w_full()
1911 .child(title_editor.clone())
1912 .child(
1913 IconButton::new("retry-summary-generation", IconName::RotateCcw)
1914 .icon_size(IconSize::Small)
1915 .on_click({
1916 let context_editor = context_editor.clone();
1917 move |_, _window, cx| {
1918 context_editor.update(cx, |context_editor, cx| {
1919 context_editor.regenerate_summary(cx);
1920 });
1921 }
1922 })
1923 .tooltip(move |_window, cx| {
1924 cx.new(|_| {
1925 Tooltip::new("Failed to generate title")
1926 .meta("Click to try again")
1927 })
1928 .into()
1929 }),
1930 )
1931 .into_any_element(),
1932 }
1933 }
1934 ActiveView::History => Label::new("History").truncate().into_any_element(),
1935 ActiveView::Configuration => Label::new("Settings").truncate().into_any_element(),
1936 };
1937
1938 h_flex()
1939 .key_context("TitleEditor")
1940 .id("TitleEditor")
1941 .flex_grow()
1942 .w_full()
1943 .max_w_full()
1944 .overflow_x_scroll()
1945 .child(content)
1946 .into_any()
1947 }
1948
1949 fn render_panel_options_menu(
1950 &self,
1951 window: &mut Window,
1952 cx: &mut Context<Self>,
1953 ) -> impl IntoElement {
1954 let user_store = self.user_store.read(cx);
1955 let usage = user_store.model_request_usage();
1956 let account_url = zed_urls::account_url(cx);
1957
1958 let focus_handle = self.focus_handle(cx);
1959
1960 let full_screen_label = if self.is_zoomed(window, cx) {
1961 "Disable Full Screen"
1962 } else {
1963 "Enable Full Screen"
1964 };
1965
1966 PopoverMenu::new("agent-options-menu")
1967 .trigger_with_tooltip(
1968 IconButton::new("agent-options-menu", IconName::Ellipsis)
1969 .icon_size(IconSize::Small),
1970 {
1971 let focus_handle = focus_handle.clone();
1972 move |window, cx| {
1973 Tooltip::for_action_in(
1974 "Toggle Agent Menu",
1975 &ToggleOptionsMenu,
1976 &focus_handle,
1977 window,
1978 cx,
1979 )
1980 }
1981 },
1982 )
1983 .anchor(Corner::TopRight)
1984 .with_handle(self.agent_panel_menu_handle.clone())
1985 .menu({
1986 let focus_handle = focus_handle.clone();
1987 move |window, cx| {
1988 Some(ContextMenu::build(window, cx, |mut menu, _window, _| {
1989 menu = menu.context(focus_handle.clone());
1990 if let Some(usage) = usage {
1991 menu = menu
1992 .header_with_link("Prompt Usage", "Manage", account_url.clone())
1993 .custom_entry(
1994 move |_window, cx| {
1995 let used_percentage = match usage.limit {
1996 UsageLimit::Limited(limit) => {
1997 Some((usage.amount as f32 / limit as f32) * 100.)
1998 }
1999 UsageLimit::Unlimited => None,
2000 };
2001
2002 h_flex()
2003 .flex_1()
2004 .gap_1p5()
2005 .children(used_percentage.map(|percent| {
2006 ProgressBar::new("usage", percent, 100., cx)
2007 }))
2008 .child(
2009 Label::new(match usage.limit {
2010 UsageLimit::Limited(limit) => {
2011 format!("{} / {limit}", usage.amount)
2012 }
2013 UsageLimit::Unlimited => {
2014 format!("{} / ∞", usage.amount)
2015 }
2016 })
2017 .size(LabelSize::Small)
2018 .color(Color::Muted),
2019 )
2020 .into_any_element()
2021 },
2022 move |_, cx| cx.open_url(&zed_urls::account_url(cx)),
2023 )
2024 .separator()
2025 }
2026
2027 menu = menu
2028 .header("MCP Servers")
2029 .action(
2030 "View Server Extensions",
2031 Box::new(zed_actions::Extensions {
2032 category_filter: Some(
2033 zed_actions::ExtensionCategoryFilter::ContextServers,
2034 ),
2035 id: None,
2036 }),
2037 )
2038 .action("Add Custom Server…", Box::new(AddContextServer))
2039 .separator();
2040
2041 menu = menu
2042 .action("Rules…", Box::new(OpenRulesLibrary::default()))
2043 .action("Settings", Box::new(OpenSettings))
2044 .separator()
2045 .action(full_screen_label, Box::new(ToggleZoom));
2046 menu
2047 }))
2048 }
2049 })
2050 }
2051
2052 fn render_recent_entries_menu(&self, cx: &mut Context<Self>) -> impl IntoElement {
2053 let focus_handle = self.focus_handle(cx);
2054
2055 PopoverMenu::new("agent-nav-menu")
2056 .trigger_with_tooltip(
2057 IconButton::new("agent-nav-menu", IconName::MenuAlt).icon_size(IconSize::Small),
2058 {
2059 let focus_handle = focus_handle.clone();
2060 move |window, cx| {
2061 Tooltip::for_action_in(
2062 "Toggle Recent Threads",
2063 &ToggleNavigationMenu,
2064 &focus_handle,
2065 window,
2066 cx,
2067 )
2068 }
2069 },
2070 )
2071 .anchor(Corner::TopLeft)
2072 .with_handle(self.assistant_navigation_menu_handle.clone())
2073 .menu({
2074 let menu = self.assistant_navigation_menu.clone();
2075 move |window, cx| {
2076 if let Some(menu) = menu.as_ref() {
2077 menu.update(cx, |_, cx| {
2078 cx.defer_in(window, |menu, window, cx| {
2079 menu.rebuild(window, cx);
2080 });
2081 })
2082 }
2083 menu.clone()
2084 }
2085 })
2086 }
2087
2088 fn render_toolbar_back_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
2089 let focus_handle = self.focus_handle(cx);
2090
2091 IconButton::new("go-back", IconName::ArrowLeft)
2092 .icon_size(IconSize::Small)
2093 .on_click(cx.listener(|this, _, window, cx| {
2094 this.go_back(&workspace::GoBack, window, cx);
2095 }))
2096 .tooltip({
2097 let focus_handle = focus_handle.clone();
2098
2099 move |window, cx| {
2100 Tooltip::for_action_in("Go Back", &workspace::GoBack, &focus_handle, window, cx)
2101 }
2102 })
2103 }
2104
2105 fn render_toolbar_old(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2106 let focus_handle = self.focus_handle(cx);
2107
2108 let active_thread = match &self.active_view {
2109 ActiveView::Thread { thread, .. } => Some(thread.read(cx).thread().clone()),
2110 ActiveView::ExternalAgentThread { .. }
2111 | ActiveView::TextThread { .. }
2112 | ActiveView::History
2113 | ActiveView::Configuration => None,
2114 };
2115
2116 let new_thread_menu = PopoverMenu::new("new_thread_menu")
2117 .trigger_with_tooltip(
2118 IconButton::new("new_thread_menu_btn", IconName::Plus).icon_size(IconSize::Small),
2119 Tooltip::text("New Thread…"),
2120 )
2121 .anchor(Corner::TopRight)
2122 .with_handle(self.new_thread_menu_handle.clone())
2123 .menu({
2124 let focus_handle = focus_handle.clone();
2125 move |window, cx| {
2126 let active_thread = active_thread.clone();
2127 Some(ContextMenu::build(window, cx, |mut menu, _window, cx| {
2128 menu = menu
2129 .context(focus_handle.clone())
2130 .when_some(active_thread, |this, active_thread| {
2131 let thread = active_thread.read(cx);
2132
2133 if !thread.is_empty() {
2134 let thread_id = thread.id().clone();
2135 this.item(
2136 ContextMenuEntry::new("New From Summary")
2137 .icon(IconName::ThreadFromSummary)
2138 .icon_color(Color::Muted)
2139 .handler(move |window, cx| {
2140 window.dispatch_action(
2141 Box::new(NewThread {
2142 from_thread_id: Some(thread_id.clone()),
2143 }),
2144 cx,
2145 );
2146 }),
2147 )
2148 } else {
2149 this
2150 }
2151 })
2152 .item(
2153 ContextMenuEntry::new("New Thread")
2154 .icon(IconName::Thread)
2155 .icon_color(Color::Muted)
2156 .action(NewThread::default().boxed_clone())
2157 .handler(move |window, cx| {
2158 window.dispatch_action(
2159 NewThread::default().boxed_clone(),
2160 cx,
2161 );
2162 }),
2163 )
2164 .item(
2165 ContextMenuEntry::new("New Text Thread")
2166 .icon(IconName::TextThread)
2167 .icon_color(Color::Muted)
2168 .action(NewTextThread.boxed_clone())
2169 .handler(move |window, cx| {
2170 window.dispatch_action(NewTextThread.boxed_clone(), cx);
2171 }),
2172 );
2173 menu
2174 }))
2175 }
2176 });
2177
2178 h_flex()
2179 .id("assistant-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 .pl_1()
2192 .gap_1()
2193 .child(match &self.active_view {
2194 ActiveView::History | ActiveView::Configuration => div()
2195 .pl(DynamicSpacing::Base04.rems(cx))
2196 .child(self.render_toolbar_back_button(cx))
2197 .into_any_element(),
2198 _ => self.render_recent_entries_menu(cx).into_any_element(),
2199 })
2200 .child(self.render_title_view(window, cx)),
2201 )
2202 .child(
2203 h_flex()
2204 .h_full()
2205 .gap_2()
2206 .children(self.render_token_count(cx))
2207 .child(
2208 h_flex()
2209 .h_full()
2210 .gap(DynamicSpacing::Base02.rems(cx))
2211 .px(DynamicSpacing::Base08.rems(cx))
2212 .border_l_1()
2213 .border_color(cx.theme().colors().border)
2214 .child(new_thread_menu)
2215 .child(self.render_panel_options_menu(window, cx)),
2216 ),
2217 )
2218 }
2219
2220 fn render_toolbar_new(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2221 let focus_handle = self.focus_handle(cx);
2222
2223 let active_thread = match &self.active_view {
2224 ActiveView::Thread { thread, .. } => Some(thread.read(cx).thread().clone()),
2225 ActiveView::ExternalAgentThread { .. }
2226 | ActiveView::TextThread { .. }
2227 | ActiveView::History
2228 | ActiveView::Configuration => None,
2229 };
2230
2231 let new_thread_menu = PopoverMenu::new("new_thread_menu")
2232 .trigger_with_tooltip(
2233 IconButton::new("new_thread_menu_btn", IconName::Plus).icon_size(IconSize::Small),
2234 {
2235 let focus_handle = focus_handle.clone();
2236 move |window, cx| {
2237 Tooltip::for_action_in(
2238 "New…",
2239 &ToggleNewThreadMenu,
2240 &focus_handle,
2241 window,
2242 cx,
2243 )
2244 }
2245 },
2246 )
2247 .anchor(Corner::TopLeft)
2248 .with_handle(self.new_thread_menu_handle.clone())
2249 .menu({
2250 let focus_handle = focus_handle.clone();
2251 let workspace = self.workspace.clone();
2252
2253 move |window, cx| {
2254 let active_thread = active_thread.clone();
2255 Some(ContextMenu::build(window, cx, |mut menu, _window, cx| {
2256 menu = menu
2257 .context(focus_handle.clone())
2258 .header("Zed Agent")
2259 .when_some(active_thread, |this, active_thread| {
2260 let thread = active_thread.read(cx);
2261
2262 if !thread.is_empty() {
2263 let thread_id = thread.id().clone();
2264 this.item(
2265 ContextMenuEntry::new("New From Summary")
2266 .icon(IconName::ThreadFromSummary)
2267 .icon_color(Color::Muted)
2268 .handler(move |window, cx| {
2269 window.dispatch_action(
2270 Box::new(NewThread {
2271 from_thread_id: Some(thread_id.clone()),
2272 }),
2273 cx,
2274 );
2275 }),
2276 )
2277 } else {
2278 this
2279 }
2280 })
2281 .item(
2282 ContextMenuEntry::new("New Thread")
2283 .icon(IconName::Thread)
2284 .icon_color(Color::Muted)
2285 .action(NewThread::default().boxed_clone())
2286 .handler({
2287 let workspace = workspace.clone();
2288 move |window, cx| {
2289 if let Some(workspace) = workspace.upgrade() {
2290 workspace.update(cx, |workspace, cx| {
2291 if let Some(panel) =
2292 workspace.panel::<AgentPanel>(cx)
2293 {
2294 panel.update(cx, |panel, cx| {
2295 panel.set_selected_agent(
2296 AgentType::Zed,
2297 window,
2298 cx,
2299 );
2300 });
2301 }
2302 });
2303 }
2304 }
2305 }),
2306 )
2307 .item(
2308 ContextMenuEntry::new("New Text Thread")
2309 .icon(IconName::TextThread)
2310 .icon_color(Color::Muted)
2311 .action(NewTextThread.boxed_clone())
2312 .handler({
2313 let workspace = workspace.clone();
2314 move |window, cx| {
2315 if let Some(workspace) = workspace.upgrade() {
2316 workspace.update(cx, |workspace, cx| {
2317 if let Some(panel) =
2318 workspace.panel::<AgentPanel>(cx)
2319 {
2320 panel.update(cx, |panel, cx| {
2321 panel.set_selected_agent(
2322 AgentType::TextThread,
2323 window,
2324 cx,
2325 );
2326 });
2327 }
2328 });
2329 }
2330 }
2331 }),
2332 )
2333 .item(
2334 ContextMenuEntry::new("New Native Agent Thread")
2335 .icon(IconName::ZedAssistant)
2336 .icon_color(Color::Muted)
2337 .handler({
2338 let workspace = workspace.clone();
2339 move |window, cx| {
2340 if let Some(workspace) = workspace.upgrade() {
2341 workspace.update(cx, |workspace, cx| {
2342 if let Some(panel) =
2343 workspace.panel::<AgentPanel>(cx)
2344 {
2345 panel.update(cx, |panel, cx| {
2346 panel.set_selected_agent(
2347 AgentType::NativeAgent,
2348 window,
2349 cx,
2350 );
2351 });
2352 }
2353 });
2354 }
2355 }
2356 }),
2357 )
2358 .separator()
2359 .header("External Agents")
2360 .item(
2361 ContextMenuEntry::new("New Gemini Thread")
2362 .icon(IconName::AiGemini)
2363 .icon_color(Color::Muted)
2364 .handler({
2365 let workspace = workspace.clone();
2366 move |window, cx| {
2367 if let Some(workspace) = workspace.upgrade() {
2368 workspace.update(cx, |workspace, cx| {
2369 if let Some(panel) =
2370 workspace.panel::<AgentPanel>(cx)
2371 {
2372 panel.update(cx, |panel, cx| {
2373 panel.set_selected_agent(
2374 AgentType::Gemini,
2375 window,
2376 cx,
2377 );
2378 });
2379 }
2380 });
2381 }
2382 }
2383 }),
2384 )
2385 .item(
2386 ContextMenuEntry::new("New Claude Code Thread")
2387 .icon(IconName::AiClaude)
2388 .icon_color(Color::Muted)
2389 .handler({
2390 let workspace = workspace.clone();
2391 move |window, cx| {
2392 if let Some(workspace) = workspace.upgrade() {
2393 workspace.update(cx, |workspace, cx| {
2394 if let Some(panel) =
2395 workspace.panel::<AgentPanel>(cx)
2396 {
2397 panel.update(cx, |panel, cx| {
2398 panel.set_selected_agent(
2399 AgentType::ClaudeCode,
2400 window,
2401 cx,
2402 );
2403 });
2404 }
2405 });
2406 }
2407 }
2408 }),
2409 );
2410 menu
2411 }))
2412 }
2413 });
2414
2415 let selected_agent_label = self.selected_agent.label().into();
2416 let selected_agent = div()
2417 .id("selected_agent_icon")
2418 .px(DynamicSpacing::Base02.rems(cx))
2419 .child(Icon::new(self.selected_agent.icon()).color(Color::Muted))
2420 .tooltip(move |window, cx| {
2421 Tooltip::with_meta(
2422 selected_agent_label.clone(),
2423 None,
2424 "Selected Agent",
2425 window,
2426 cx,
2427 )
2428 })
2429 .into_any_element();
2430
2431 h_flex()
2432 .id("agent-panel-toolbar")
2433 .h(Tab::container_height(cx))
2434 .max_w_full()
2435 .flex_none()
2436 .justify_between()
2437 .gap_2()
2438 .bg(cx.theme().colors().tab_bar_background)
2439 .border_b_1()
2440 .border_color(cx.theme().colors().border)
2441 .child(
2442 h_flex()
2443 .size_full()
2444 .gap(DynamicSpacing::Base04.rems(cx))
2445 .pl(DynamicSpacing::Base04.rems(cx))
2446 .child(match &self.active_view {
2447 ActiveView::History | ActiveView::Configuration => {
2448 self.render_toolbar_back_button(cx).into_any_element()
2449 }
2450 _ => h_flex()
2451 .gap_1()
2452 .child(self.render_recent_entries_menu(cx))
2453 .child(Divider::vertical())
2454 .child(selected_agent)
2455 .into_any_element(),
2456 })
2457 .child(self.render_title_view(window, cx)),
2458 )
2459 .child(
2460 h_flex()
2461 .h_full()
2462 .gap_2()
2463 .children(self.render_token_count(cx))
2464 .child(
2465 h_flex()
2466 .h_full()
2467 .gap(DynamicSpacing::Base02.rems(cx))
2468 .pl(DynamicSpacing::Base04.rems(cx))
2469 .pr(DynamicSpacing::Base06.rems(cx))
2470 .border_l_1()
2471 .border_color(cx.theme().colors().border)
2472 .child(new_thread_menu)
2473 .child(self.render_panel_options_menu(window, cx)),
2474 ),
2475 )
2476 }
2477
2478 fn render_toolbar(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2479 if cx.has_flag::<feature_flags::AcpFeatureFlag>() {
2480 self.render_toolbar_new(window, cx).into_any_element()
2481 } else {
2482 self.render_toolbar_old(window, cx).into_any_element()
2483 }
2484 }
2485
2486 fn render_token_count(&self, cx: &App) -> Option<AnyElement> {
2487 match &self.active_view {
2488 ActiveView::Thread {
2489 thread,
2490 message_editor,
2491 ..
2492 } => {
2493 let active_thread = thread.read(cx);
2494 let message_editor = message_editor.read(cx);
2495
2496 let editor_empty = message_editor.is_editor_fully_empty(cx);
2497
2498 if active_thread.is_empty() && editor_empty {
2499 return None;
2500 }
2501
2502 let thread = active_thread.thread().read(cx);
2503 let is_generating = thread.is_generating();
2504 let conversation_token_usage = thread.total_token_usage()?;
2505
2506 let (total_token_usage, is_estimating) =
2507 if let Some((editing_message_id, unsent_tokens)) =
2508 active_thread.editing_message_id()
2509 {
2510 let combined = thread
2511 .token_usage_up_to_message(editing_message_id)
2512 .add(unsent_tokens);
2513
2514 (combined, unsent_tokens > 0)
2515 } else {
2516 let unsent_tokens =
2517 message_editor.last_estimated_token_count().unwrap_or(0);
2518 let combined = conversation_token_usage.add(unsent_tokens);
2519
2520 (combined, unsent_tokens > 0)
2521 };
2522
2523 let is_waiting_to_update_token_count =
2524 message_editor.is_waiting_to_update_token_count();
2525
2526 if total_token_usage.total == 0 {
2527 return None;
2528 }
2529
2530 let token_color = match total_token_usage.ratio() {
2531 TokenUsageRatio::Normal if is_estimating => Color::Default,
2532 TokenUsageRatio::Normal => Color::Muted,
2533 TokenUsageRatio::Warning => Color::Warning,
2534 TokenUsageRatio::Exceeded => Color::Error,
2535 };
2536
2537 let token_count = h_flex()
2538 .id("token-count")
2539 .flex_shrink_0()
2540 .gap_0p5()
2541 .when(!is_generating && is_estimating, |parent| {
2542 parent
2543 .child(
2544 h_flex()
2545 .mr_1()
2546 .size_2p5()
2547 .justify_center()
2548 .rounded_full()
2549 .bg(cx.theme().colors().text.opacity(0.1))
2550 .child(
2551 div().size_1().rounded_full().bg(cx.theme().colors().text),
2552 ),
2553 )
2554 .tooltip(move |window, cx| {
2555 Tooltip::with_meta(
2556 "Estimated New Token Count",
2557 None,
2558 format!(
2559 "Current Conversation Tokens: {}",
2560 humanize_token_count(conversation_token_usage.total)
2561 ),
2562 window,
2563 cx,
2564 )
2565 })
2566 })
2567 .child(
2568 Label::new(humanize_token_count(total_token_usage.total))
2569 .size(LabelSize::Small)
2570 .color(token_color)
2571 .map(|label| {
2572 if is_generating || is_waiting_to_update_token_count {
2573 label
2574 .with_animation(
2575 "used-tokens-label",
2576 Animation::new(Duration::from_secs(2))
2577 .repeat()
2578 .with_easing(pulsating_between(0.6, 1.)),
2579 |label, delta| label.alpha(delta),
2580 )
2581 .into_any()
2582 } else {
2583 label.into_any_element()
2584 }
2585 }),
2586 )
2587 .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
2588 .child(
2589 Label::new(humanize_token_count(total_token_usage.max))
2590 .size(LabelSize::Small)
2591 .color(Color::Muted),
2592 )
2593 .into_any();
2594
2595 Some(token_count)
2596 }
2597 ActiveView::TextThread { context_editor, .. } => {
2598 let element = render_remaining_tokens(context_editor, cx)?;
2599
2600 Some(element.into_any_element())
2601 }
2602 ActiveView::ExternalAgentThread { .. }
2603 | ActiveView::History
2604 | ActiveView::Configuration => {
2605 return None;
2606 }
2607 }
2608 }
2609
2610 fn should_render_trial_end_upsell(&self, cx: &mut Context<Self>) -> bool {
2611 if TrialEndUpsell::dismissed() {
2612 return false;
2613 }
2614
2615 match &self.active_view {
2616 ActiveView::Thread { thread, .. } => {
2617 if thread
2618 .read(cx)
2619 .thread()
2620 .read(cx)
2621 .configured_model()
2622 .map_or(false, |model| {
2623 model.provider.id() != language_model::ZED_CLOUD_PROVIDER_ID
2624 })
2625 {
2626 return false;
2627 }
2628 }
2629 ActiveView::TextThread { .. } => {
2630 if LanguageModelRegistry::global(cx)
2631 .read(cx)
2632 .default_model()
2633 .map_or(false, |model| {
2634 model.provider.id() != language_model::ZED_CLOUD_PROVIDER_ID
2635 })
2636 {
2637 return false;
2638 }
2639 }
2640 ActiveView::ExternalAgentThread { .. }
2641 | ActiveView::History
2642 | ActiveView::Configuration => return false,
2643 }
2644
2645 let plan = self.user_store.read(cx).plan();
2646 let has_previous_trial = self.user_store.read(cx).trial_started_at().is_some();
2647
2648 matches!(plan, Some(Plan::ZedFree)) && has_previous_trial
2649 }
2650
2651 fn should_render_onboarding(&self, cx: &mut Context<Self>) -> bool {
2652 if OnboardingUpsell::dismissed() {
2653 return false;
2654 }
2655
2656 match &self.active_view {
2657 ActiveView::History | ActiveView::Configuration => false,
2658 ActiveView::ExternalAgentThread { thread_view, .. }
2659 if thread_view.read(cx).as_native_thread(cx).is_none() =>
2660 {
2661 false
2662 }
2663 _ => {
2664 let history_is_empty = self
2665 .history_store
2666 .update(cx, |store, cx| store.recent_entries(1, cx).is_empty());
2667
2668 let has_configured_non_zed_providers = LanguageModelRegistry::read_global(cx)
2669 .providers()
2670 .iter()
2671 .any(|provider| {
2672 provider.is_authenticated(cx)
2673 && provider.id() != language_model::ZED_CLOUD_PROVIDER_ID
2674 });
2675
2676 history_is_empty || !has_configured_non_zed_providers
2677 }
2678 }
2679 }
2680
2681 fn render_onboarding(
2682 &self,
2683 _window: &mut Window,
2684 cx: &mut Context<Self>,
2685 ) -> Option<impl IntoElement> {
2686 if !self.should_render_onboarding(cx) {
2687 return None;
2688 }
2689
2690 let thread_view = matches!(&self.active_view, ActiveView::Thread { .. });
2691 let text_thread_view = matches!(&self.active_view, ActiveView::TextThread { .. });
2692
2693 Some(
2694 div()
2695 .when(thread_view, |this| {
2696 this.size_full().bg(cx.theme().colors().panel_background)
2697 })
2698 .when(text_thread_view, |this| {
2699 this.bg(cx.theme().colors().editor_background)
2700 })
2701 .child(self.onboarding.clone()),
2702 )
2703 }
2704
2705 fn render_backdrop(&self, cx: &mut Context<Self>) -> impl IntoElement {
2706 div()
2707 .size_full()
2708 .absolute()
2709 .inset_0()
2710 .bg(cx.theme().colors().panel_background)
2711 .opacity(0.8)
2712 .block_mouse_except_scroll()
2713 }
2714
2715 fn render_trial_end_upsell(
2716 &self,
2717 _window: &mut Window,
2718 cx: &mut Context<Self>,
2719 ) -> Option<impl IntoElement> {
2720 if !self.should_render_trial_end_upsell(cx) {
2721 return None;
2722 }
2723
2724 Some(
2725 v_flex()
2726 .absolute()
2727 .inset_0()
2728 .size_full()
2729 .bg(cx.theme().colors().panel_background)
2730 .opacity(0.85)
2731 .block_mouse_except_scroll()
2732 .child(EndTrialUpsell::new(Arc::new({
2733 let this = cx.entity();
2734 move |_, cx| {
2735 this.update(cx, |_this, cx| {
2736 TrialEndUpsell::set_dismissed(true, cx);
2737 cx.notify();
2738 });
2739 }
2740 }))),
2741 )
2742 }
2743
2744 fn render_empty_state_section_header(
2745 &self,
2746 label: impl Into<SharedString>,
2747 action_slot: Option<AnyElement>,
2748 cx: &mut Context<Self>,
2749 ) -> impl IntoElement {
2750 h_flex()
2751 .mt_2()
2752 .pl_1p5()
2753 .pb_1()
2754 .w_full()
2755 .justify_between()
2756 .border_b_1()
2757 .border_color(cx.theme().colors().border_variant)
2758 .child(
2759 Label::new(label.into())
2760 .size(LabelSize::Small)
2761 .color(Color::Muted),
2762 )
2763 .children(action_slot)
2764 }
2765
2766 fn render_thread_empty_state(
2767 &self,
2768 window: &mut Window,
2769 cx: &mut Context<Self>,
2770 ) -> impl IntoElement {
2771 let recent_history = self
2772 .history_store
2773 .update(cx, |this, cx| this.recent_entries(6, cx));
2774
2775 let model_registry = LanguageModelRegistry::read_global(cx);
2776
2777 let configuration_error =
2778 model_registry.configuration_error(model_registry.default_model(), cx);
2779
2780 let no_error = configuration_error.is_none();
2781 let focus_handle = self.focus_handle(cx);
2782
2783 v_flex()
2784 .size_full()
2785 .bg(cx.theme().colors().panel_background)
2786 .when(recent_history.is_empty(), |this| {
2787 this.child(
2788 v_flex()
2789 .size_full()
2790 .mx_auto()
2791 .justify_center()
2792 .items_center()
2793 .gap_1()
2794 .child(h_flex().child(Headline::new("Welcome to the Agent Panel")))
2795 .when(no_error, |parent| {
2796 parent
2797 .child(h_flex().child(
2798 Label::new("Ask and build anything.").color(Color::Muted),
2799 ))
2800 .child(
2801 v_flex()
2802 .mt_2()
2803 .gap_1()
2804 .max_w_48()
2805 .child(
2806 Button::new("context", "Add Context")
2807 .label_size(LabelSize::Small)
2808 .icon(IconName::FileCode)
2809 .icon_position(IconPosition::Start)
2810 .icon_size(IconSize::Small)
2811 .icon_color(Color::Muted)
2812 .full_width()
2813 .key_binding(KeyBinding::for_action_in(
2814 &ToggleContextPicker,
2815 &focus_handle,
2816 window,
2817 cx,
2818 ))
2819 .on_click(|_event, window, cx| {
2820 window.dispatch_action(
2821 ToggleContextPicker.boxed_clone(),
2822 cx,
2823 )
2824 }),
2825 )
2826 .child(
2827 Button::new("mode", "Switch Model")
2828 .label_size(LabelSize::Small)
2829 .icon(IconName::DatabaseZap)
2830 .icon_position(IconPosition::Start)
2831 .icon_size(IconSize::Small)
2832 .icon_color(Color::Muted)
2833 .full_width()
2834 .key_binding(KeyBinding::for_action_in(
2835 &ToggleModelSelector,
2836 &focus_handle,
2837 window,
2838 cx,
2839 ))
2840 .on_click(|_event, window, cx| {
2841 window.dispatch_action(
2842 ToggleModelSelector.boxed_clone(),
2843 cx,
2844 )
2845 }),
2846 )
2847 .child(
2848 Button::new("settings", "View Settings")
2849 .label_size(LabelSize::Small)
2850 .icon(IconName::Settings)
2851 .icon_position(IconPosition::Start)
2852 .icon_size(IconSize::Small)
2853 .icon_color(Color::Muted)
2854 .full_width()
2855 .key_binding(KeyBinding::for_action_in(
2856 &OpenSettings,
2857 &focus_handle,
2858 window,
2859 cx,
2860 ))
2861 .on_click(|_event, window, cx| {
2862 window.dispatch_action(
2863 OpenSettings.boxed_clone(),
2864 cx,
2865 )
2866 }),
2867 ),
2868 )
2869 })
2870 .when_some(configuration_error.as_ref(), |this, err| {
2871 this.child(self.render_configuration_error(
2872 err,
2873 &focus_handle,
2874 window,
2875 cx,
2876 ))
2877 }),
2878 )
2879 })
2880 .when(!recent_history.is_empty(), |parent| {
2881 let focus_handle = focus_handle.clone();
2882 parent
2883 .overflow_hidden()
2884 .p_1p5()
2885 .justify_end()
2886 .gap_1()
2887 .child(
2888 self.render_empty_state_section_header(
2889 "Recent",
2890 Some(
2891 Button::new("view-history", "View All")
2892 .style(ButtonStyle::Subtle)
2893 .label_size(LabelSize::Small)
2894 .key_binding(
2895 KeyBinding::for_action_in(
2896 &OpenHistory,
2897 &self.focus_handle(cx),
2898 window,
2899 cx,
2900 )
2901 .map(|kb| kb.size(rems_from_px(12.))),
2902 )
2903 .on_click(move |_event, window, cx| {
2904 window.dispatch_action(OpenHistory.boxed_clone(), cx);
2905 })
2906 .into_any_element(),
2907 ),
2908 cx,
2909 ),
2910 )
2911 .child(
2912 v_flex()
2913 .gap_1()
2914 .children(recent_history.into_iter().enumerate().map(
2915 |(index, entry)| {
2916 // TODO: Add keyboard navigation.
2917 let is_hovered =
2918 self.hovered_recent_history_item == Some(index);
2919 HistoryEntryElement::new(entry.clone(), cx.entity().downgrade())
2920 .hovered(is_hovered)
2921 .on_hover(cx.listener(
2922 move |this, is_hovered, _window, cx| {
2923 if *is_hovered {
2924 this.hovered_recent_history_item = Some(index);
2925 } else if this.hovered_recent_history_item
2926 == Some(index)
2927 {
2928 this.hovered_recent_history_item = None;
2929 }
2930 cx.notify();
2931 },
2932 ))
2933 .into_any_element()
2934 },
2935 )),
2936 )
2937 .when_some(configuration_error.as_ref(), |this, err| {
2938 this.child(self.render_configuration_error(err, &focus_handle, window, cx))
2939 })
2940 })
2941 }
2942
2943 fn render_configuration_error(
2944 &self,
2945 configuration_error: &ConfigurationError,
2946 focus_handle: &FocusHandle,
2947 window: &mut Window,
2948 cx: &mut App,
2949 ) -> impl IntoElement {
2950 match configuration_error {
2951 ConfigurationError::ModelNotFound
2952 | ConfigurationError::ProviderNotAuthenticated(_)
2953 | ConfigurationError::NoProvider => Banner::new()
2954 .severity(ui::Severity::Warning)
2955 .child(Label::new(configuration_error.to_string()))
2956 .action_slot(
2957 Button::new("settings", "Configure Provider")
2958 .style(ButtonStyle::Tinted(ui::TintColor::Warning))
2959 .label_size(LabelSize::Small)
2960 .key_binding(
2961 KeyBinding::for_action_in(&OpenSettings, &focus_handle, window, cx)
2962 .map(|kb| kb.size(rems_from_px(12.))),
2963 )
2964 .on_click(|_event, window, cx| {
2965 window.dispatch_action(OpenSettings.boxed_clone(), cx)
2966 }),
2967 ),
2968 ConfigurationError::ProviderPendingTermsAcceptance(provider) => {
2969 Banner::new().severity(ui::Severity::Warning).child(
2970 h_flex().w_full().children(
2971 provider.render_accept_terms(
2972 LanguageModelProviderTosView::ThreadEmptyState,
2973 cx,
2974 ),
2975 ),
2976 )
2977 }
2978 }
2979 }
2980
2981 fn render_tool_use_limit_reached(
2982 &self,
2983 window: &mut Window,
2984 cx: &mut Context<Self>,
2985 ) -> Option<AnyElement> {
2986 let active_thread = match &self.active_view {
2987 ActiveView::Thread { thread, .. } => thread,
2988 ActiveView::ExternalAgentThread { .. } => {
2989 return None;
2990 }
2991 ActiveView::TextThread { .. } | ActiveView::History | ActiveView::Configuration => {
2992 return None;
2993 }
2994 };
2995
2996 let thread = active_thread.read(cx).thread().read(cx);
2997
2998 let tool_use_limit_reached = thread.tool_use_limit_reached();
2999 if !tool_use_limit_reached {
3000 return None;
3001 }
3002
3003 let model = thread.configured_model()?.model;
3004
3005 let focus_handle = self.focus_handle(cx);
3006
3007 let banner = Banner::new()
3008 .severity(ui::Severity::Info)
3009 .child(Label::new("Consecutive tool use limit reached.").size(LabelSize::Small))
3010 .action_slot(
3011 h_flex()
3012 .gap_1()
3013 .child(
3014 Button::new("continue-conversation", "Continue")
3015 .layer(ElevationIndex::ModalSurface)
3016 .label_size(LabelSize::Small)
3017 .key_binding(
3018 KeyBinding::for_action_in(
3019 &ContinueThread,
3020 &focus_handle,
3021 window,
3022 cx,
3023 )
3024 .map(|kb| kb.size(rems_from_px(10.))),
3025 )
3026 .on_click(cx.listener(|this, _, window, cx| {
3027 this.continue_conversation(window, cx);
3028 })),
3029 )
3030 .when(model.supports_burn_mode(), |this| {
3031 this.child(
3032 Button::new("continue-burn-mode", "Continue with Burn Mode")
3033 .style(ButtonStyle::Filled)
3034 .style(ButtonStyle::Tinted(ui::TintColor::Accent))
3035 .layer(ElevationIndex::ModalSurface)
3036 .label_size(LabelSize::Small)
3037 .key_binding(
3038 KeyBinding::for_action_in(
3039 &ContinueWithBurnMode,
3040 &focus_handle,
3041 window,
3042 cx,
3043 )
3044 .map(|kb| kb.size(rems_from_px(10.))),
3045 )
3046 .tooltip(Tooltip::text("Enable Burn Mode for unlimited tool use."))
3047 .on_click({
3048 let active_thread = active_thread.clone();
3049 cx.listener(move |this, _, window, cx| {
3050 active_thread.update(cx, |active_thread, cx| {
3051 active_thread.thread().update(cx, |thread, _cx| {
3052 thread.set_completion_mode(CompletionMode::Burn);
3053 });
3054 });
3055 this.continue_conversation(window, cx);
3056 })
3057 }),
3058 )
3059 }),
3060 );
3061
3062 Some(div().px_2().pb_2().child(banner).into_any_element())
3063 }
3064
3065 fn create_copy_button(&self, message: impl Into<String>) -> impl IntoElement {
3066 let message = message.into();
3067
3068 IconButton::new("copy", IconName::Copy)
3069 .icon_size(IconSize::Small)
3070 .icon_color(Color::Muted)
3071 .tooltip(Tooltip::text("Copy Error Message"))
3072 .on_click(move |_, _, cx| {
3073 cx.write_to_clipboard(ClipboardItem::new_string(message.clone()))
3074 })
3075 }
3076
3077 fn dismiss_error_button(
3078 &self,
3079 thread: &Entity<ActiveThread>,
3080 cx: &mut Context<Self>,
3081 ) -> impl IntoElement {
3082 IconButton::new("dismiss", IconName::Close)
3083 .icon_size(IconSize::Small)
3084 .icon_color(Color::Muted)
3085 .tooltip(Tooltip::text("Dismiss Error"))
3086 .on_click(cx.listener({
3087 let thread = thread.clone();
3088 move |_, _, _, cx| {
3089 thread.update(cx, |this, _cx| {
3090 this.clear_last_error();
3091 });
3092
3093 cx.notify();
3094 }
3095 }))
3096 }
3097
3098 fn upgrade_button(
3099 &self,
3100 thread: &Entity<ActiveThread>,
3101 cx: &mut Context<Self>,
3102 ) -> impl IntoElement {
3103 Button::new("upgrade", "Upgrade")
3104 .label_size(LabelSize::Small)
3105 .style(ButtonStyle::Tinted(ui::TintColor::Accent))
3106 .on_click(cx.listener({
3107 let thread = thread.clone();
3108 move |_, _, _, cx| {
3109 thread.update(cx, |this, _cx| {
3110 this.clear_last_error();
3111 });
3112
3113 cx.open_url(&zed_urls::upgrade_to_zed_pro_url(cx));
3114 cx.notify();
3115 }
3116 }))
3117 }
3118
3119 fn error_callout_bg(&self, cx: &Context<Self>) -> Hsla {
3120 cx.theme().status().error.opacity(0.08)
3121 }
3122
3123 fn render_payment_required_error(
3124 &self,
3125 thread: &Entity<ActiveThread>,
3126 cx: &mut Context<Self>,
3127 ) -> AnyElement {
3128 const ERROR_MESSAGE: &str =
3129 "You reached your free usage limit. Upgrade to Zed Pro for more prompts.";
3130
3131 let icon = Icon::new(IconName::XCircle)
3132 .size(IconSize::Small)
3133 .color(Color::Error);
3134
3135 div()
3136 .border_t_1()
3137 .border_color(cx.theme().colors().border)
3138 .child(
3139 Callout::new()
3140 .icon(icon)
3141 .title("Free Usage Exceeded")
3142 .description(ERROR_MESSAGE)
3143 .tertiary_action(self.upgrade_button(thread, cx))
3144 .secondary_action(self.create_copy_button(ERROR_MESSAGE))
3145 .primary_action(self.dismiss_error_button(thread, cx))
3146 .bg_color(self.error_callout_bg(cx)),
3147 )
3148 .into_any_element()
3149 }
3150
3151 fn render_model_request_limit_reached_error(
3152 &self,
3153 plan: Plan,
3154 thread: &Entity<ActiveThread>,
3155 cx: &mut Context<Self>,
3156 ) -> AnyElement {
3157 let error_message = match plan {
3158 Plan::ZedPro => "Upgrade to usage-based billing for more prompts.",
3159 Plan::ZedProTrial | Plan::ZedFree => "Upgrade to Zed Pro for more prompts.",
3160 };
3161
3162 let icon = Icon::new(IconName::XCircle)
3163 .size(IconSize::Small)
3164 .color(Color::Error);
3165
3166 div()
3167 .border_t_1()
3168 .border_color(cx.theme().colors().border)
3169 .child(
3170 Callout::new()
3171 .icon(icon)
3172 .title("Model Prompt Limit Reached")
3173 .description(error_message)
3174 .tertiary_action(self.upgrade_button(thread, cx))
3175 .secondary_action(self.create_copy_button(error_message))
3176 .primary_action(self.dismiss_error_button(thread, cx))
3177 .bg_color(self.error_callout_bg(cx)),
3178 )
3179 .into_any_element()
3180 }
3181
3182 fn render_error_message(
3183 &self,
3184 header: SharedString,
3185 message: SharedString,
3186 thread: &Entity<ActiveThread>,
3187 cx: &mut Context<Self>,
3188 ) -> AnyElement {
3189 let message_with_header = format!("{}\n{}", header, message);
3190
3191 let icon = Icon::new(IconName::XCircle)
3192 .size(IconSize::Small)
3193 .color(Color::Error);
3194
3195 let retry_button = Button::new("retry", "Retry")
3196 .icon(IconName::RotateCw)
3197 .icon_position(IconPosition::Start)
3198 .icon_size(IconSize::Small)
3199 .label_size(LabelSize::Small)
3200 .on_click({
3201 let thread = thread.clone();
3202 move |_, window, cx| {
3203 thread.update(cx, |thread, cx| {
3204 thread.clear_last_error();
3205 thread.thread().update(cx, |thread, cx| {
3206 thread.retry_last_completion(Some(window.window_handle()), cx);
3207 });
3208 });
3209 }
3210 });
3211
3212 div()
3213 .border_t_1()
3214 .border_color(cx.theme().colors().border)
3215 .child(
3216 Callout::new()
3217 .icon(icon)
3218 .title(header)
3219 .description(message.clone())
3220 .primary_action(retry_button)
3221 .secondary_action(self.dismiss_error_button(thread, cx))
3222 .tertiary_action(self.create_copy_button(message_with_header))
3223 .bg_color(self.error_callout_bg(cx)),
3224 )
3225 .into_any_element()
3226 }
3227
3228 fn render_retryable_error(
3229 &self,
3230 message: SharedString,
3231 can_enable_burn_mode: bool,
3232 thread: &Entity<ActiveThread>,
3233 cx: &mut Context<Self>,
3234 ) -> AnyElement {
3235 let icon = Icon::new(IconName::XCircle)
3236 .size(IconSize::Small)
3237 .color(Color::Error);
3238
3239 let retry_button = Button::new("retry", "Retry")
3240 .icon(IconName::RotateCw)
3241 .icon_position(IconPosition::Start)
3242 .icon_size(IconSize::Small)
3243 .label_size(LabelSize::Small)
3244 .on_click({
3245 let thread = thread.clone();
3246 move |_, window, cx| {
3247 thread.update(cx, |thread, cx| {
3248 thread.clear_last_error();
3249 thread.thread().update(cx, |thread, cx| {
3250 thread.retry_last_completion(Some(window.window_handle()), cx);
3251 });
3252 });
3253 }
3254 });
3255
3256 let mut callout = Callout::new()
3257 .icon(icon)
3258 .title("Error")
3259 .description(message.clone())
3260 .bg_color(self.error_callout_bg(cx))
3261 .primary_action(retry_button);
3262
3263 if can_enable_burn_mode {
3264 let burn_mode_button = Button::new("enable_burn_retry", "Enable Burn Mode and Retry")
3265 .icon(IconName::ZedBurnMode)
3266 .icon_position(IconPosition::Start)
3267 .icon_size(IconSize::Small)
3268 .label_size(LabelSize::Small)
3269 .on_click({
3270 let thread = thread.clone();
3271 move |_, window, cx| {
3272 thread.update(cx, |thread, cx| {
3273 thread.clear_last_error();
3274 thread.thread().update(cx, |thread, cx| {
3275 thread.enable_burn_mode_and_retry(Some(window.window_handle()), cx);
3276 });
3277 });
3278 }
3279 });
3280 callout = callout.secondary_action(burn_mode_button);
3281 }
3282
3283 div()
3284 .border_t_1()
3285 .border_color(cx.theme().colors().border)
3286 .child(callout)
3287 .into_any_element()
3288 }
3289
3290 fn render_prompt_editor(
3291 &self,
3292 context_editor: &Entity<TextThreadEditor>,
3293 buffer_search_bar: &Entity<BufferSearchBar>,
3294 window: &mut Window,
3295 cx: &mut Context<Self>,
3296 ) -> Div {
3297 let mut registrar = buffer_search::DivRegistrar::new(
3298 |this, _, _cx| match &this.active_view {
3299 ActiveView::TextThread {
3300 buffer_search_bar, ..
3301 } => Some(buffer_search_bar.clone()),
3302 _ => None,
3303 },
3304 cx,
3305 );
3306 BufferSearchBar::register(&mut registrar);
3307 registrar
3308 .into_div()
3309 .size_full()
3310 .relative()
3311 .map(|parent| {
3312 buffer_search_bar.update(cx, |buffer_search_bar, cx| {
3313 if buffer_search_bar.is_dismissed() {
3314 return parent;
3315 }
3316 parent.child(
3317 div()
3318 .p(DynamicSpacing::Base08.rems(cx))
3319 .border_b_1()
3320 .border_color(cx.theme().colors().border_variant)
3321 .bg(cx.theme().colors().editor_background)
3322 .child(buffer_search_bar.render(window, cx)),
3323 )
3324 })
3325 })
3326 .child(context_editor.clone())
3327 .child(self.render_drag_target(cx))
3328 }
3329
3330 fn render_drag_target(&self, cx: &Context<Self>) -> Div {
3331 let is_local = self.project.read(cx).is_local();
3332 div()
3333 .invisible()
3334 .absolute()
3335 .top_0()
3336 .right_0()
3337 .bottom_0()
3338 .left_0()
3339 .bg(cx.theme().colors().drop_target_background)
3340 .drag_over::<DraggedTab>(|this, _, _, _| this.visible())
3341 .drag_over::<DraggedSelection>(|this, _, _, _| this.visible())
3342 .when(is_local, |this| {
3343 this.drag_over::<ExternalPaths>(|this, _, _, _| this.visible())
3344 })
3345 .on_drop(cx.listener(move |this, tab: &DraggedTab, window, cx| {
3346 let item = tab.pane.read(cx).item_for_index(tab.ix);
3347 let project_paths = item
3348 .and_then(|item| item.project_path(cx))
3349 .into_iter()
3350 .collect::<Vec<_>>();
3351 this.handle_drop(project_paths, vec![], window, cx);
3352 }))
3353 .on_drop(
3354 cx.listener(move |this, selection: &DraggedSelection, window, cx| {
3355 let project_paths = selection
3356 .items()
3357 .filter_map(|item| this.project.read(cx).path_for_entry(item.entry_id, cx))
3358 .collect::<Vec<_>>();
3359 this.handle_drop(project_paths, vec![], window, cx);
3360 }),
3361 )
3362 .on_drop(cx.listener(move |this, paths: &ExternalPaths, window, cx| {
3363 let tasks = paths
3364 .paths()
3365 .into_iter()
3366 .map(|path| {
3367 Workspace::project_path_for_path(this.project.clone(), &path, false, cx)
3368 })
3369 .collect::<Vec<_>>();
3370 cx.spawn_in(window, async move |this, cx| {
3371 let mut paths = vec![];
3372 let mut added_worktrees = vec![];
3373 let opened_paths = futures::future::join_all(tasks).await;
3374 for entry in opened_paths {
3375 if let Some((worktree, project_path)) = entry.log_err() {
3376 added_worktrees.push(worktree);
3377 paths.push(project_path);
3378 }
3379 }
3380 this.update_in(cx, |this, window, cx| {
3381 this.handle_drop(paths, added_worktrees, window, cx);
3382 })
3383 .ok();
3384 })
3385 .detach();
3386 }))
3387 }
3388
3389 fn handle_drop(
3390 &mut self,
3391 paths: Vec<ProjectPath>,
3392 added_worktrees: Vec<Entity<Worktree>>,
3393 window: &mut Window,
3394 cx: &mut Context<Self>,
3395 ) {
3396 match &self.active_view {
3397 ActiveView::Thread { thread, .. } => {
3398 let context_store = thread.read(cx).context_store().clone();
3399 context_store.update(cx, move |context_store, cx| {
3400 let mut tasks = Vec::new();
3401 for project_path in &paths {
3402 tasks.push(context_store.add_file_from_path(
3403 project_path.clone(),
3404 false,
3405 cx,
3406 ));
3407 }
3408 cx.background_spawn(async move {
3409 futures::future::join_all(tasks).await;
3410 // Need to hold onto the worktrees until they have already been used when
3411 // opening the buffers.
3412 drop(added_worktrees);
3413 })
3414 .detach();
3415 });
3416 }
3417 ActiveView::ExternalAgentThread { thread_view } => {
3418 thread_view.update(cx, |thread_view, cx| {
3419 thread_view.insert_dragged_files(paths, added_worktrees, window, cx);
3420 });
3421 }
3422 ActiveView::TextThread { context_editor, .. } => {
3423 context_editor.update(cx, |context_editor, cx| {
3424 TextThreadEditor::insert_dragged_files(
3425 context_editor,
3426 paths,
3427 added_worktrees,
3428 window,
3429 cx,
3430 );
3431 });
3432 }
3433 ActiveView::History | ActiveView::Configuration => {}
3434 }
3435 }
3436
3437 fn key_context(&self) -> KeyContext {
3438 let mut key_context = KeyContext::new_with_defaults();
3439 key_context.add("AgentPanel");
3440 match &self.active_view {
3441 ActiveView::ExternalAgentThread { .. } => key_context.add("external_agent_thread"),
3442 ActiveView::TextThread { .. } => key_context.add("prompt_editor"),
3443 ActiveView::Thread { .. } | ActiveView::History | ActiveView::Configuration => {}
3444 }
3445 key_context
3446 }
3447}
3448
3449impl Render for AgentPanel {
3450 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
3451 // WARNING: Changes to this element hierarchy can have
3452 // non-obvious implications to the layout of children.
3453 //
3454 // If you need to change it, please confirm:
3455 // - The message editor expands (cmd-option-esc) correctly
3456 // - When expanded, the buttons at the bottom of the panel are displayed correctly
3457 // - Font size works as expected and can be changed with cmd-+/cmd-
3458 // - Scrolling in all views works as expected
3459 // - Files can be dropped into the panel
3460 let content = v_flex()
3461 .relative()
3462 .size_full()
3463 .justify_between()
3464 .key_context(self.key_context())
3465 .on_action(cx.listener(Self::cancel))
3466 .on_action(cx.listener(|this, action: &NewThread, window, cx| {
3467 this.new_thread(action, window, cx);
3468 }))
3469 .on_action(cx.listener(|this, _: &OpenHistory, window, cx| {
3470 this.open_history(window, cx);
3471 }))
3472 .on_action(cx.listener(|this, _: &OpenSettings, window, cx| {
3473 this.open_configuration(window, cx);
3474 }))
3475 .on_action(cx.listener(Self::open_active_thread_as_markdown))
3476 .on_action(cx.listener(Self::deploy_rules_library))
3477 .on_action(cx.listener(Self::open_agent_diff))
3478 .on_action(cx.listener(Self::go_back))
3479 .on_action(cx.listener(Self::toggle_navigation_menu))
3480 .on_action(cx.listener(Self::toggle_options_menu))
3481 .on_action(cx.listener(Self::increase_font_size))
3482 .on_action(cx.listener(Self::decrease_font_size))
3483 .on_action(cx.listener(Self::reset_font_size))
3484 .on_action(cx.listener(Self::toggle_zoom))
3485 .on_action(cx.listener(|this, _: &ContinueThread, window, cx| {
3486 this.continue_conversation(window, cx);
3487 }))
3488 .on_action(cx.listener(|this, _: &ContinueWithBurnMode, window, cx| {
3489 match &this.active_view {
3490 ActiveView::Thread { thread, .. } => {
3491 thread.update(cx, |active_thread, cx| {
3492 active_thread.thread().update(cx, |thread, _cx| {
3493 thread.set_completion_mode(CompletionMode::Burn);
3494 });
3495 });
3496 this.continue_conversation(window, cx);
3497 }
3498 ActiveView::ExternalAgentThread { .. } => {}
3499 ActiveView::TextThread { .. }
3500 | ActiveView::History
3501 | ActiveView::Configuration => {}
3502 }
3503 }))
3504 .on_action(cx.listener(Self::toggle_burn_mode))
3505 .child(self.render_toolbar(window, cx))
3506 .children(self.render_onboarding(window, cx))
3507 .map(|parent| match &self.active_view {
3508 ActiveView::Thread {
3509 thread,
3510 message_editor,
3511 ..
3512 } => parent
3513 .child(
3514 if thread.read(cx).is_empty() && !self.should_render_onboarding(cx) {
3515 self.render_thread_empty_state(window, cx)
3516 .into_any_element()
3517 } else {
3518 thread.clone().into_any_element()
3519 },
3520 )
3521 .children(self.render_tool_use_limit_reached(window, cx))
3522 .when_some(thread.read(cx).last_error(), |this, last_error| {
3523 this.child(
3524 div()
3525 .child(match last_error {
3526 ThreadError::PaymentRequired => {
3527 self.render_payment_required_error(thread, cx)
3528 }
3529 ThreadError::ModelRequestLimitReached { plan } => self
3530 .render_model_request_limit_reached_error(plan, thread, cx),
3531 ThreadError::Message { header, message } => {
3532 self.render_error_message(header, message, thread, cx)
3533 }
3534 ThreadError::RetryableError {
3535 message,
3536 can_enable_burn_mode,
3537 } => self.render_retryable_error(
3538 message,
3539 can_enable_burn_mode,
3540 thread,
3541 cx,
3542 ),
3543 })
3544 .into_any(),
3545 )
3546 })
3547 .child(h_flex().relative().child(message_editor.clone()).when(
3548 !LanguageModelRegistry::read_global(cx).has_authenticated_provider(cx),
3549 |this| this.child(self.render_backdrop(cx)),
3550 ))
3551 .child(self.render_drag_target(cx)),
3552 ActiveView::ExternalAgentThread { thread_view, .. } => parent
3553 .child(thread_view.clone())
3554 .child(self.render_drag_target(cx)),
3555 ActiveView::History => {
3556 if cx.has_flag::<feature_flags::AcpFeatureFlag>() {
3557 parent.child(self.acp_history.clone())
3558 } else {
3559 parent.child(self.history.clone())
3560 }
3561 }
3562 ActiveView::TextThread {
3563 context_editor,
3564 buffer_search_bar,
3565 ..
3566 } => {
3567 let model_registry = LanguageModelRegistry::read_global(cx);
3568 let configuration_error =
3569 model_registry.configuration_error(model_registry.default_model(), cx);
3570 parent
3571 .map(|this| {
3572 if !self.should_render_onboarding(cx)
3573 && let Some(err) = configuration_error.as_ref()
3574 {
3575 this.child(
3576 div().bg(cx.theme().colors().editor_background).p_2().child(
3577 self.render_configuration_error(
3578 err,
3579 &self.focus_handle(cx),
3580 window,
3581 cx,
3582 ),
3583 ),
3584 )
3585 } else {
3586 this
3587 }
3588 })
3589 .child(self.render_prompt_editor(
3590 context_editor,
3591 buffer_search_bar,
3592 window,
3593 cx,
3594 ))
3595 }
3596 ActiveView::Configuration => parent.children(self.configuration.clone()),
3597 })
3598 .children(self.render_trial_end_upsell(window, cx));
3599
3600 match self.active_view.which_font_size_used() {
3601 WhichFontSize::AgentFont => {
3602 WithRemSize::new(ThemeSettings::get_global(cx).agent_font_size(cx))
3603 .size_full()
3604 .child(content)
3605 .into_any()
3606 }
3607 _ => content.into_any(),
3608 }
3609 }
3610}
3611
3612struct PromptLibraryInlineAssist {
3613 workspace: WeakEntity<Workspace>,
3614}
3615
3616impl PromptLibraryInlineAssist {
3617 pub fn new(workspace: WeakEntity<Workspace>) -> Self {
3618 Self { workspace }
3619 }
3620}
3621
3622impl rules_library::InlineAssistDelegate for PromptLibraryInlineAssist {
3623 fn assist(
3624 &self,
3625 prompt_editor: &Entity<Editor>,
3626 initial_prompt: Option<String>,
3627 window: &mut Window,
3628 cx: &mut Context<RulesLibrary>,
3629 ) {
3630 InlineAssistant::update_global(cx, |assistant, cx| {
3631 let Some(project) = self
3632 .workspace
3633 .upgrade()
3634 .map(|workspace| workspace.read(cx).project().downgrade())
3635 else {
3636 return;
3637 };
3638 let prompt_store = None;
3639 let thread_store = None;
3640 let text_thread_store = None;
3641 let context_store = cx.new(|_| ContextStore::new(project.clone(), None));
3642 assistant.assist(
3643 &prompt_editor,
3644 self.workspace.clone(),
3645 context_store,
3646 project,
3647 prompt_store,
3648 thread_store,
3649 text_thread_store,
3650 initial_prompt,
3651 window,
3652 cx,
3653 )
3654 })
3655 }
3656
3657 fn focus_agent_panel(
3658 &self,
3659 workspace: &mut Workspace,
3660 window: &mut Window,
3661 cx: &mut Context<Workspace>,
3662 ) -> bool {
3663 workspace.focus_panel::<AgentPanel>(window, cx).is_some()
3664 }
3665}
3666
3667pub struct ConcreteAssistantPanelDelegate;
3668
3669impl AgentPanelDelegate for ConcreteAssistantPanelDelegate {
3670 fn active_context_editor(
3671 &self,
3672 workspace: &mut Workspace,
3673 _window: &mut Window,
3674 cx: &mut Context<Workspace>,
3675 ) -> Option<Entity<TextThreadEditor>> {
3676 let panel = workspace.panel::<AgentPanel>(cx)?;
3677 panel.read(cx).active_context_editor()
3678 }
3679
3680 fn open_saved_context(
3681 &self,
3682 workspace: &mut Workspace,
3683 path: Arc<Path>,
3684 window: &mut Window,
3685 cx: &mut Context<Workspace>,
3686 ) -> Task<Result<()>> {
3687 let Some(panel) = workspace.panel::<AgentPanel>(cx) else {
3688 return Task::ready(Err(anyhow!("Agent panel not found")));
3689 };
3690
3691 panel.update(cx, |panel, cx| {
3692 panel.open_saved_prompt_editor(path, window, cx)
3693 })
3694 }
3695
3696 fn open_remote_context(
3697 &self,
3698 _workspace: &mut Workspace,
3699 _context_id: assistant_context::ContextId,
3700 _window: &mut Window,
3701 _cx: &mut Context<Workspace>,
3702 ) -> Task<Result<Entity<TextThreadEditor>>> {
3703 Task::ready(Err(anyhow!("opening remote context not implemented")))
3704 }
3705
3706 fn quote_selection(
3707 &self,
3708 workspace: &mut Workspace,
3709 selection_ranges: Vec<Range<Anchor>>,
3710 buffer: Entity<MultiBuffer>,
3711 window: &mut Window,
3712 cx: &mut Context<Workspace>,
3713 ) {
3714 let Some(panel) = workspace.panel::<AgentPanel>(cx) else {
3715 return;
3716 };
3717
3718 if !panel.focus_handle(cx).contains_focused(window, cx) {
3719 workspace.toggle_panel_focus::<AgentPanel>(window, cx);
3720 }
3721
3722 panel.update(cx, |_, cx| {
3723 // Wait to create a new context until the workspace is no longer
3724 // being updated.
3725 cx.defer_in(window, move |panel, window, cx| {
3726 if let Some(message_editor) = panel.active_message_editor() {
3727 message_editor.update(cx, |message_editor, cx| {
3728 message_editor.context_store().update(cx, |store, cx| {
3729 let buffer = buffer.read(cx);
3730 let selection_ranges = selection_ranges
3731 .into_iter()
3732 .flat_map(|range| {
3733 let (start_buffer, start) =
3734 buffer.text_anchor_for_position(range.start, cx)?;
3735 let (end_buffer, end) =
3736 buffer.text_anchor_for_position(range.end, cx)?;
3737 if start_buffer != end_buffer {
3738 return None;
3739 }
3740 Some((start_buffer, start..end))
3741 })
3742 .collect::<Vec<_>>();
3743
3744 for (buffer, range) in selection_ranges {
3745 store.add_selection(buffer, range, cx);
3746 }
3747 })
3748 })
3749 } else if let Some(context_editor) = panel.active_context_editor() {
3750 let snapshot = buffer.read(cx).snapshot(cx);
3751 let selection_ranges = selection_ranges
3752 .into_iter()
3753 .map(|range| range.to_point(&snapshot))
3754 .collect::<Vec<_>>();
3755
3756 context_editor.update(cx, |context_editor, cx| {
3757 context_editor.quote_ranges(selection_ranges, snapshot, window, cx)
3758 });
3759 }
3760 });
3761 });
3762 }
3763}
3764
3765struct OnboardingUpsell;
3766
3767impl Dismissable for OnboardingUpsell {
3768 const KEY: &'static str = "dismissed-trial-upsell";
3769}
3770
3771struct TrialEndUpsell;
3772
3773impl Dismissable for TrialEndUpsell {
3774 const KEY: &'static str = "dismissed-trial-end-upsell";
3775}