1use std::path::PathBuf;
2use std::sync::Arc;
3
4use anyhow::{anyhow, Result};
5use assistant_context_editor::{
6 make_lsp_adapter_delegate, render_remaining_tokens, AssistantPanelDelegate, ConfigurationError,
7 ContextEditor, SlashCommandCompletionProvider,
8};
9use assistant_settings::{AssistantDockPosition, AssistantSettings};
10use assistant_slash_command::SlashCommandWorkingSet;
11use assistant_tool::ToolWorkingSet;
12
13use client::zed_urls;
14use editor::Editor;
15use fs::Fs;
16use gpui::{
17 prelude::*, Action, AnyElement, App, AsyncWindowContext, Corner, Entity, EventEmitter,
18 FocusHandle, Focusable, FontWeight, Pixels, Subscription, Task, UpdateGlobal, WeakEntity,
19};
20use language::LanguageRegistry;
21use language_model::{LanguageModelProviderTosView, LanguageModelRegistry};
22use project::Project;
23use prompt_library::{open_prompt_library, PromptBuilder, PromptLibrary};
24use settings::{update_settings_file, Settings};
25use time::UtcOffset;
26use ui::{prelude::*, ContextMenu, KeyBinding, PopoverMenu, PopoverMenuHandle, Tab, Tooltip};
27use util::ResultExt as _;
28use workspace::dock::{DockPosition, Panel, PanelEvent};
29use workspace::Workspace;
30use zed_actions::assistant::{DeployPromptLibrary, ToggleFocus};
31
32use crate::active_thread::ActiveThread;
33use crate::assistant_configuration::{AssistantConfiguration, AssistantConfigurationEvent};
34use crate::history_store::{HistoryEntry, HistoryStore};
35use crate::message_editor::MessageEditor;
36use crate::thread::{Thread, ThreadError, ThreadId};
37use crate::thread_history::{PastContext, PastThread, ThreadHistory};
38use crate::thread_store::ThreadStore;
39use crate::{InlineAssistant, NewPromptEditor, NewThread, OpenConfiguration, OpenHistory};
40
41pub fn init(cx: &mut App) {
42 cx.observe_new(
43 |workspace: &mut Workspace, _window, _cx: &mut Context<Workspace>| {
44 workspace
45 .register_action(|workspace, _: &NewThread, window, cx| {
46 if let Some(panel) = workspace.panel::<AssistantPanel>(cx) {
47 panel.update(cx, |panel, cx| panel.new_thread(window, cx));
48 workspace.focus_panel::<AssistantPanel>(window, cx);
49 }
50 })
51 .register_action(|workspace, _: &OpenHistory, window, cx| {
52 if let Some(panel) = workspace.panel::<AssistantPanel>(cx) {
53 workspace.focus_panel::<AssistantPanel>(window, cx);
54 panel.update(cx, |panel, cx| panel.open_history(window, cx));
55 }
56 })
57 .register_action(|workspace, _: &NewPromptEditor, window, cx| {
58 if let Some(panel) = workspace.panel::<AssistantPanel>(cx) {
59 workspace.focus_panel::<AssistantPanel>(window, cx);
60 panel.update(cx, |panel, cx| panel.new_prompt_editor(window, cx));
61 }
62 })
63 .register_action(|workspace, _: &OpenConfiguration, window, cx| {
64 if let Some(panel) = workspace.panel::<AssistantPanel>(cx) {
65 workspace.focus_panel::<AssistantPanel>(window, cx);
66 panel.update(cx, |panel, cx| panel.open_configuration(window, cx));
67 }
68 });
69 },
70 )
71 .detach();
72}
73
74enum ActiveView {
75 Thread,
76 PromptEditor,
77 History,
78 Configuration,
79}
80
81pub struct AssistantPanel {
82 workspace: WeakEntity<Workspace>,
83 project: Entity<Project>,
84 fs: Arc<dyn Fs>,
85 language_registry: Arc<LanguageRegistry>,
86 thread_store: Entity<ThreadStore>,
87 thread: Entity<ActiveThread>,
88 message_editor: Entity<MessageEditor>,
89 context_store: Entity<assistant_context_editor::ContextStore>,
90 context_editor: Option<Entity<ContextEditor>>,
91 configuration: Option<Entity<AssistantConfiguration>>,
92 configuration_subscription: Option<Subscription>,
93 tools: Arc<ToolWorkingSet>,
94 local_timezone: UtcOffset,
95 active_view: ActiveView,
96 history_store: Entity<HistoryStore>,
97 history: Entity<ThreadHistory>,
98 new_item_context_menu_handle: PopoverMenuHandle<ContextMenu>,
99 width: Option<Pixels>,
100 height: Option<Pixels>,
101}
102
103impl AssistantPanel {
104 pub fn load(
105 workspace: WeakEntity<Workspace>,
106 prompt_builder: Arc<PromptBuilder>,
107 cx: AsyncWindowContext,
108 ) -> Task<Result<Entity<Self>>> {
109 cx.spawn(|mut cx| async move {
110 let tools = Arc::new(ToolWorkingSet::default());
111 log::info!("[assistant2-debug] initializing ThreadStore");
112 let thread_store = workspace.update(&mut cx, |workspace, cx| {
113 let project = workspace.project().clone();
114 ThreadStore::new(project, tools.clone(), cx)
115 })??;
116 log::info!("[assistant2-debug] finished initializing ThreadStore");
117
118 let slash_commands = Arc::new(SlashCommandWorkingSet::default());
119 log::info!("[assistant2-debug] initializing ContextStore");
120 let context_store = workspace
121 .update(&mut cx, |workspace, cx| {
122 let project = workspace.project().clone();
123 assistant_context_editor::ContextStore::new(
124 project,
125 prompt_builder.clone(),
126 slash_commands,
127 cx,
128 )
129 })?
130 .await?;
131 log::info!("[assistant2-debug] finished initializing ContextStore");
132
133 workspace.update_in(&mut cx, |workspace, window, cx| {
134 cx.new(|cx| Self::new(workspace, thread_store, context_store, tools, window, cx))
135 })
136 })
137 }
138
139 fn new(
140 workspace: &Workspace,
141 thread_store: Entity<ThreadStore>,
142 context_store: Entity<assistant_context_editor::ContextStore>,
143 tools: Arc<ToolWorkingSet>,
144 window: &mut Window,
145 cx: &mut Context<Self>,
146 ) -> Self {
147 log::info!("[assistant2-debug] AssistantPanel::new");
148 let thread = thread_store.update(cx, |this, cx| this.create_thread(cx));
149 let fs = workspace.app_state().fs.clone();
150 let project = workspace.project().clone();
151 let language_registry = project.read(cx).languages().clone();
152 let workspace = workspace.weak_handle();
153 let weak_self = cx.entity().downgrade();
154
155 let message_editor = cx.new(|cx| {
156 MessageEditor::new(
157 fs.clone(),
158 workspace.clone(),
159 thread_store.downgrade(),
160 thread.clone(),
161 window,
162 cx,
163 )
164 });
165
166 let history_store =
167 cx.new(|cx| HistoryStore::new(thread_store.clone(), context_store.clone(), cx));
168
169 Self {
170 active_view: ActiveView::Thread,
171 workspace: workspace.clone(),
172 project,
173 fs: fs.clone(),
174 language_registry: language_registry.clone(),
175 thread_store: thread_store.clone(),
176 thread: cx.new(|cx| {
177 ActiveThread::new(
178 thread.clone(),
179 thread_store.clone(),
180 workspace,
181 language_registry,
182 tools.clone(),
183 window,
184 cx,
185 )
186 }),
187 message_editor,
188 context_store,
189 context_editor: None,
190 configuration: None,
191 configuration_subscription: None,
192 tools,
193 local_timezone: UtcOffset::from_whole_seconds(
194 chrono::Local::now().offset().local_minus_utc(),
195 )
196 .unwrap(),
197 history_store: history_store.clone(),
198 history: cx.new(|cx| ThreadHistory::new(weak_self, history_store, cx)),
199 new_item_context_menu_handle: PopoverMenuHandle::default(),
200 width: None,
201 height: None,
202 }
203 }
204
205 pub fn toggle_focus(
206 workspace: &mut Workspace,
207 _: &ToggleFocus,
208 window: &mut Window,
209 cx: &mut Context<Workspace>,
210 ) {
211 let settings = AssistantSettings::get_global(cx);
212 if !settings.enabled {
213 return;
214 }
215
216 workspace.toggle_panel_focus::<Self>(window, cx);
217 }
218
219 pub(crate) fn local_timezone(&self) -> UtcOffset {
220 self.local_timezone
221 }
222
223 pub(crate) fn thread_store(&self) -> &Entity<ThreadStore> {
224 &self.thread_store
225 }
226
227 fn cancel(
228 &mut self,
229 _: &editor::actions::Cancel,
230 _window: &mut Window,
231 cx: &mut Context<Self>,
232 ) {
233 self.thread
234 .update(cx, |thread, cx| thread.cancel_last_completion(cx));
235 }
236
237 fn new_thread(&mut self, window: &mut Window, cx: &mut Context<Self>) {
238 let thread = self
239 .thread_store
240 .update(cx, |this, cx| this.create_thread(cx));
241
242 self.active_view = ActiveView::Thread;
243 self.thread = cx.new(|cx| {
244 ActiveThread::new(
245 thread.clone(),
246 self.thread_store.clone(),
247 self.workspace.clone(),
248 self.language_registry.clone(),
249 self.tools.clone(),
250 window,
251 cx,
252 )
253 });
254 self.message_editor = cx.new(|cx| {
255 MessageEditor::new(
256 self.fs.clone(),
257 self.workspace.clone(),
258 self.thread_store.downgrade(),
259 thread,
260 window,
261 cx,
262 )
263 });
264 self.message_editor.focus_handle(cx).focus(window);
265 }
266
267 fn new_prompt_editor(&mut self, window: &mut Window, cx: &mut Context<Self>) {
268 self.active_view = ActiveView::PromptEditor;
269
270 let context = self
271 .context_store
272 .update(cx, |context_store, cx| context_store.create(cx));
273 let lsp_adapter_delegate = make_lsp_adapter_delegate(&self.project, cx)
274 .log_err()
275 .flatten();
276
277 self.context_editor = Some(cx.new(|cx| {
278 let mut editor = ContextEditor::for_context(
279 context,
280 self.fs.clone(),
281 self.workspace.clone(),
282 self.project.clone(),
283 lsp_adapter_delegate,
284 window,
285 cx,
286 );
287 editor.insert_default_prompt(window, cx);
288 editor
289 }));
290
291 if let Some(context_editor) = self.context_editor.as_ref() {
292 context_editor.focus_handle(cx).focus(window);
293 }
294 }
295
296 fn deploy_prompt_library(
297 &mut self,
298 _: &DeployPromptLibrary,
299 _window: &mut Window,
300 cx: &mut Context<Self>,
301 ) {
302 open_prompt_library(
303 self.language_registry.clone(),
304 Box::new(PromptLibraryInlineAssist::new(self.workspace.clone())),
305 Arc::new(|| {
306 Box::new(SlashCommandCompletionProvider::new(
307 Arc::new(SlashCommandWorkingSet::default()),
308 None,
309 None,
310 ))
311 }),
312 cx,
313 )
314 .detach_and_log_err(cx);
315 }
316
317 fn open_history(&mut self, window: &mut Window, cx: &mut Context<Self>) {
318 self.thread_store
319 .update(cx, |thread_store, cx| thread_store.reload(cx))
320 .detach_and_log_err(cx);
321 self.active_view = ActiveView::History;
322 self.history.focus_handle(cx).focus(window);
323 cx.notify();
324 }
325
326 pub(crate) fn open_saved_prompt_editor(
327 &mut self,
328 path: PathBuf,
329 window: &mut Window,
330 cx: &mut Context<Self>,
331 ) -> Task<Result<()>> {
332 let context = self
333 .context_store
334 .update(cx, |store, cx| store.open_local_context(path.clone(), cx));
335 let fs = self.fs.clone();
336 let project = self.project.clone();
337 let workspace = self.workspace.clone();
338
339 let lsp_adapter_delegate = make_lsp_adapter_delegate(&project, cx).log_err().flatten();
340
341 cx.spawn_in(window, |this, mut cx| async move {
342 let context = context.await?;
343 this.update_in(&mut cx, |this, window, cx| {
344 let editor = cx.new(|cx| {
345 ContextEditor::for_context(
346 context,
347 fs,
348 workspace,
349 project,
350 lsp_adapter_delegate,
351 window,
352 cx,
353 )
354 });
355 this.active_view = ActiveView::PromptEditor;
356 this.context_editor = Some(editor);
357
358 anyhow::Ok(())
359 })??;
360 Ok(())
361 })
362 }
363
364 pub(crate) fn open_thread(
365 &mut self,
366 thread_id: &ThreadId,
367 window: &mut Window,
368 cx: &mut Context<Self>,
369 ) -> Task<Result<()>> {
370 let open_thread_task = self
371 .thread_store
372 .update(cx, |this, cx| this.open_thread(thread_id, cx));
373
374 cx.spawn_in(window, |this, mut cx| async move {
375 let thread = open_thread_task.await?;
376 this.update_in(&mut cx, |this, window, cx| {
377 this.active_view = ActiveView::Thread;
378 this.thread = cx.new(|cx| {
379 ActiveThread::new(
380 thread.clone(),
381 this.thread_store.clone(),
382 this.workspace.clone(),
383 this.language_registry.clone(),
384 this.tools.clone(),
385 window,
386 cx,
387 )
388 });
389 this.message_editor = cx.new(|cx| {
390 MessageEditor::new(
391 this.fs.clone(),
392 this.workspace.clone(),
393 this.thread_store.downgrade(),
394 thread,
395 window,
396 cx,
397 )
398 });
399 this.message_editor.focus_handle(cx).focus(window);
400 })
401 })
402 }
403
404 pub(crate) fn open_configuration(&mut self, window: &mut Window, cx: &mut Context<Self>) {
405 self.active_view = ActiveView::Configuration;
406 self.configuration = Some(cx.new(|cx| AssistantConfiguration::new(window, cx)));
407
408 if let Some(configuration) = self.configuration.as_ref() {
409 self.configuration_subscription = Some(cx.subscribe_in(
410 configuration,
411 window,
412 Self::handle_assistant_configuration_event,
413 ));
414
415 configuration.focus_handle(cx).focus(window);
416 }
417 }
418
419 fn handle_assistant_configuration_event(
420 &mut self,
421 _entity: &Entity<AssistantConfiguration>,
422 event: &AssistantConfigurationEvent,
423 window: &mut Window,
424 cx: &mut Context<Self>,
425 ) {
426 match event {
427 AssistantConfigurationEvent::NewThread(provider) => {
428 if LanguageModelRegistry::read_global(cx)
429 .active_provider()
430 .map_or(true, |active_provider| {
431 active_provider.id() != provider.id()
432 })
433 {
434 if let Some(model) = provider.default_model(cx) {
435 update_settings_file::<AssistantSettings>(
436 self.fs.clone(),
437 cx,
438 move |settings, _| settings.set_model(model),
439 );
440 }
441 }
442
443 self.new_thread(window, cx);
444 }
445 }
446 }
447
448 pub(crate) fn active_thread(&self, cx: &App) -> Entity<Thread> {
449 self.thread.read(cx).thread().clone()
450 }
451
452 pub(crate) fn delete_thread(&mut self, thread_id: &ThreadId, cx: &mut Context<Self>) {
453 self.thread_store
454 .update(cx, |this, cx| this.delete_thread(thread_id, cx))
455 .detach_and_log_err(cx);
456 }
457
458 pub(crate) fn active_context_editor(&self) -> Option<Entity<ContextEditor>> {
459 self.context_editor.clone()
460 }
461
462 pub(crate) fn delete_context(&mut self, path: PathBuf, cx: &mut Context<Self>) {
463 self.context_store
464 .update(cx, |this, cx| this.delete_local_context(path, cx))
465 .detach_and_log_err(cx);
466 }
467}
468
469impl Focusable for AssistantPanel {
470 fn focus_handle(&self, cx: &App) -> FocusHandle {
471 match self.active_view {
472 ActiveView::Thread => self.message_editor.focus_handle(cx),
473 ActiveView::History => self.history.focus_handle(cx),
474 ActiveView::PromptEditor => {
475 if let Some(context_editor) = self.context_editor.as_ref() {
476 context_editor.focus_handle(cx)
477 } else {
478 cx.focus_handle()
479 }
480 }
481 ActiveView::Configuration => {
482 if let Some(configuration) = self.configuration.as_ref() {
483 configuration.focus_handle(cx)
484 } else {
485 cx.focus_handle()
486 }
487 }
488 }
489 }
490}
491
492impl EventEmitter<PanelEvent> for AssistantPanel {}
493
494impl Panel for AssistantPanel {
495 fn persistent_name() -> &'static str {
496 "AssistantPanel2"
497 }
498
499 fn position(&self, _window: &Window, cx: &App) -> DockPosition {
500 match AssistantSettings::get_global(cx).dock {
501 AssistantDockPosition::Left => DockPosition::Left,
502 AssistantDockPosition::Bottom => DockPosition::Bottom,
503 AssistantDockPosition::Right => DockPosition::Right,
504 }
505 }
506
507 fn position_is_valid(&self, _: DockPosition) -> bool {
508 true
509 }
510
511 fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
512 settings::update_settings_file::<AssistantSettings>(
513 self.fs.clone(),
514 cx,
515 move |settings, _| {
516 let dock = match position {
517 DockPosition::Left => AssistantDockPosition::Left,
518 DockPosition::Bottom => AssistantDockPosition::Bottom,
519 DockPosition::Right => AssistantDockPosition::Right,
520 };
521 settings.set_dock(dock);
522 },
523 );
524 }
525
526 fn size(&self, window: &Window, cx: &App) -> Pixels {
527 let settings = AssistantSettings::get_global(cx);
528 match self.position(window, cx) {
529 DockPosition::Left | DockPosition::Right => {
530 self.width.unwrap_or(settings.default_width)
531 }
532 DockPosition::Bottom => self.height.unwrap_or(settings.default_height),
533 }
534 }
535
536 fn set_size(&mut self, size: Option<Pixels>, window: &mut Window, cx: &mut Context<Self>) {
537 match self.position(window, cx) {
538 DockPosition::Left | DockPosition::Right => self.width = size,
539 DockPosition::Bottom => self.height = size,
540 }
541 cx.notify();
542 }
543
544 fn set_active(&mut self, _active: bool, _window: &mut Window, _cx: &mut Context<Self>) {}
545
546 fn remote_id() -> Option<proto::PanelId> {
547 Some(proto::PanelId::AssistantPanel)
548 }
549
550 fn icon(&self, _window: &Window, cx: &App) -> Option<IconName> {
551 let settings = AssistantSettings::get_global(cx);
552 if !settings.enabled || !settings.button {
553 return None;
554 }
555
556 Some(IconName::ZedAssistant)
557 }
558
559 fn icon_tooltip(&self, _window: &Window, _cx: &App) -> Option<&'static str> {
560 Some("Assistant Panel")
561 }
562
563 fn toggle_action(&self) -> Box<dyn Action> {
564 Box::new(ToggleFocus)
565 }
566
567 fn activation_priority(&self) -> u32 {
568 3
569 }
570}
571
572impl AssistantPanel {
573 fn render_toolbar(&self, cx: &mut Context<Self>) -> impl IntoElement {
574 let thread = self.thread.read(cx);
575
576 let title = match self.active_view {
577 ActiveView::Thread => {
578 if thread.is_empty() {
579 thread.summary_or_default(cx)
580 } else {
581 thread
582 .summary(cx)
583 .unwrap_or_else(|| SharedString::from("Loading Summary…"))
584 }
585 }
586 ActiveView::PromptEditor => self
587 .context_editor
588 .as_ref()
589 .map(|context_editor| {
590 SharedString::from(context_editor.read(cx).title(cx).to_string())
591 })
592 .unwrap_or_else(|| SharedString::from("Loading Summary…")),
593 ActiveView::History => "History".into(),
594 ActiveView::Configuration => "Assistant Settings".into(),
595 };
596
597 h_flex()
598 .id("assistant-toolbar")
599 .h(Tab::container_height(cx))
600 .flex_none()
601 .justify_between()
602 .gap(DynamicSpacing::Base08.rems(cx))
603 .bg(cx.theme().colors().tab_bar_background)
604 .border_b_1()
605 .border_color(cx.theme().colors().border)
606 .child(
607 div()
608 .id("title")
609 .overflow_x_scroll()
610 .px(DynamicSpacing::Base08.rems(cx))
611 .child(Label::new(title).text_ellipsis()),
612 )
613 .child(
614 h_flex()
615 .h_full()
616 .pl_2()
617 .gap_2()
618 .bg(cx.theme().colors().tab_bar_background)
619 .children(if matches!(self.active_view, ActiveView::PromptEditor) {
620 self.context_editor
621 .as_ref()
622 .and_then(|editor| render_remaining_tokens(editor, cx))
623 } else {
624 None
625 })
626 .child(
627 h_flex()
628 .h_full()
629 .px(DynamicSpacing::Base08.rems(cx))
630 .border_l_1()
631 .border_color(cx.theme().colors().border)
632 .gap(DynamicSpacing::Base02.rems(cx))
633 .child(
634 PopoverMenu::new("assistant-toolbar-new-popover-menu")
635 .trigger_with_tooltip(
636 IconButton::new("new", IconName::Plus)
637 .icon_size(IconSize::Small)
638 .style(ButtonStyle::Subtle),
639 Tooltip::text("New…"),
640 )
641 .anchor(Corner::TopRight)
642 .with_handle(self.new_item_context_menu_handle.clone())
643 .menu(move |window, cx| {
644 Some(ContextMenu::build(
645 window,
646 cx,
647 |menu, _window, _cx| {
648 menu.action("New Thread", NewThread.boxed_clone())
649 .action(
650 "New Prompt Editor",
651 NewPromptEditor.boxed_clone(),
652 )
653 },
654 ))
655 }),
656 )
657 .child(
658 IconButton::new("open-history", IconName::HistoryRerun)
659 .icon_size(IconSize::Small)
660 .style(ButtonStyle::Subtle)
661 .tooltip({
662 let focus_handle = self.focus_handle(cx);
663 move |window, cx| {
664 Tooltip::for_action_in(
665 "History",
666 &OpenHistory,
667 &focus_handle,
668 window,
669 cx,
670 )
671 }
672 })
673 .on_click(move |_event, window, cx| {
674 window.dispatch_action(OpenHistory.boxed_clone(), cx);
675 }),
676 )
677 .child(
678 IconButton::new("configure-assistant", IconName::Settings)
679 .icon_size(IconSize::Small)
680 .style(ButtonStyle::Subtle)
681 .tooltip(Tooltip::text("Assistant Settings"))
682 .on_click(move |_event, window, cx| {
683 window.dispatch_action(OpenConfiguration.boxed_clone(), cx);
684 }),
685 ),
686 ),
687 )
688 }
689
690 fn render_active_thread_or_empty_state(
691 &self,
692 window: &mut Window,
693 cx: &mut Context<Self>,
694 ) -> AnyElement {
695 if self.thread.read(cx).is_empty() {
696 return self
697 .render_thread_empty_state(window, cx)
698 .into_any_element();
699 }
700
701 self.thread.clone().into_any_element()
702 }
703
704 fn configuration_error(&self, cx: &App) -> Option<ConfigurationError> {
705 let Some(provider) = LanguageModelRegistry::read_global(cx).active_provider() else {
706 return Some(ConfigurationError::NoProvider);
707 };
708
709 if !provider.is_authenticated(cx) {
710 return Some(ConfigurationError::ProviderNotAuthenticated);
711 }
712
713 if provider.must_accept_terms(cx) {
714 return Some(ConfigurationError::ProviderPendingTermsAcceptance(provider));
715 }
716
717 None
718 }
719
720 fn render_thread_empty_state(
721 &self,
722 window: &mut Window,
723 cx: &mut Context<Self>,
724 ) -> impl IntoElement {
725 let recent_history = self
726 .history_store
727 .update(cx, |this, cx| this.recent_entries(6, cx));
728
729 let create_welcome_heading = || {
730 h_flex()
731 .w_full()
732 .child(Headline::new("Welcome to the Assistant Panel").size(HeadlineSize::Small))
733 };
734
735 let configuration_error = self.configuration_error(cx);
736 let no_error = configuration_error.is_none();
737
738 v_flex()
739 .p_1p5()
740 .size_full()
741 .justify_end()
742 .gap_1()
743 .map(|parent| {
744 match configuration_error {
745 Some(ConfigurationError::ProviderNotAuthenticated)
746 | Some(ConfigurationError::NoProvider) => {
747 parent.child(
748 v_flex()
749 .px_1p5()
750 .gap_0p5()
751 .child(create_welcome_heading())
752 .child(
753 Label::new(
754 "To start using the assistant, configure at least one LLM provider.",
755 )
756 .color(Color::Muted),
757 )
758 .child(
759 h_flex().mt_1().w_full().child(
760 Button::new("open-configuration", "Configure a Provider")
761 .size(ButtonSize::Compact)
762 .icon(Some(IconName::Sliders))
763 .icon_size(IconSize::Small)
764 .icon_position(IconPosition::Start)
765 .on_click(cx.listener(|this, _, window, cx| {
766 this.open_configuration(window, cx);
767 })),
768 ),
769 ),
770 )
771 }
772 Some(ConfigurationError::ProviderPendingTermsAcceptance(provider)) => parent
773 .child(v_flex().px_1p5().gap_0p5().child(create_welcome_heading()).children(
774 provider.render_accept_terms(
775 LanguageModelProviderTosView::ThreadEmptyState,
776 cx,
777 ),
778 )),
779 None => parent,
780 }
781 })
782 .when(recent_history.is_empty() && no_error, |parent| {
783 parent.child(v_flex().gap_0p5().child(create_welcome_heading()).child(
784 Label::new("Start typing to chat with your codebase").color(Color::Muted),
785 ))
786 })
787 .when(!recent_history.is_empty(), |parent| {
788 parent
789 .child(
790 h_flex()
791 .pl_1p5()
792 .pb_1()
793 .w_full()
794 .justify_between()
795 .border_b_1()
796 .border_color(cx.theme().colors().border_variant)
797 .child(
798 Label::new("Past Interactions")
799 .size(LabelSize::Small)
800 .color(Color::Muted),
801 )
802 .child(
803 Button::new("view-history", "View All")
804 .style(ButtonStyle::Subtle)
805 .label_size(LabelSize::Small)
806 .key_binding(KeyBinding::for_action_in(
807 &OpenHistory,
808 &self.focus_handle(cx),
809 window,
810 cx,
811 ))
812 .on_click(move |_event, window, cx| {
813 window.dispatch_action(OpenHistory.boxed_clone(), cx);
814 }),
815 ),
816 )
817 .child(v_flex().gap_1().children(
818 recent_history.into_iter().map(|entry| {
819 // TODO: Add keyboard navigation.
820 match entry {
821 HistoryEntry::Thread(thread) => {
822 PastThread::new(thread, cx.entity().downgrade(), false)
823 .into_any_element()
824 }
825 HistoryEntry::Context(context) => {
826 PastContext::new(context, cx.entity().downgrade(), false)
827 .into_any_element()
828 }
829 }
830 }),
831 ))
832 })
833 }
834
835 fn render_last_error(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
836 let last_error = self.thread.read(cx).last_error()?;
837
838 Some(
839 div()
840 .absolute()
841 .right_3()
842 .bottom_12()
843 .max_w_96()
844 .py_2()
845 .px_3()
846 .elevation_2(cx)
847 .occlude()
848 .child(match last_error {
849 ThreadError::PaymentRequired => self.render_payment_required_error(cx),
850 ThreadError::MaxMonthlySpendReached => {
851 self.render_max_monthly_spend_reached_error(cx)
852 }
853 ThreadError::Message(error_message) => {
854 self.render_error_message(&error_message, cx)
855 }
856 })
857 .into_any(),
858 )
859 }
860
861 fn render_payment_required_error(&self, cx: &mut Context<Self>) -> AnyElement {
862 const ERROR_MESSAGE: &str = "Free tier exceeded. Subscribe and add payment to continue using Zed LLMs. You'll be billed at cost for tokens used.";
863
864 v_flex()
865 .gap_0p5()
866 .child(
867 h_flex()
868 .gap_1p5()
869 .items_center()
870 .child(Icon::new(IconName::XCircle).color(Color::Error))
871 .child(Label::new("Free Usage Exceeded").weight(FontWeight::MEDIUM)),
872 )
873 .child(
874 div()
875 .id("error-message")
876 .max_h_24()
877 .overflow_y_scroll()
878 .child(Label::new(ERROR_MESSAGE)),
879 )
880 .child(
881 h_flex()
882 .justify_end()
883 .mt_1()
884 .child(Button::new("subscribe", "Subscribe").on_click(cx.listener(
885 |this, _, _, cx| {
886 this.thread.update(cx, |this, _cx| {
887 this.clear_last_error();
888 });
889
890 cx.open_url(&zed_urls::account_url(cx));
891 cx.notify();
892 },
893 )))
894 .child(Button::new("dismiss", "Dismiss").on_click(cx.listener(
895 |this, _, _, cx| {
896 this.thread.update(cx, |this, _cx| {
897 this.clear_last_error();
898 });
899
900 cx.notify();
901 },
902 ))),
903 )
904 .into_any()
905 }
906
907 fn render_max_monthly_spend_reached_error(&self, cx: &mut Context<Self>) -> AnyElement {
908 const ERROR_MESSAGE: &str = "You have reached your maximum monthly spend. Increase your spend limit to continue using Zed LLMs.";
909
910 v_flex()
911 .gap_0p5()
912 .child(
913 h_flex()
914 .gap_1p5()
915 .items_center()
916 .child(Icon::new(IconName::XCircle).color(Color::Error))
917 .child(Label::new("Max Monthly Spend Reached").weight(FontWeight::MEDIUM)),
918 )
919 .child(
920 div()
921 .id("error-message")
922 .max_h_24()
923 .overflow_y_scroll()
924 .child(Label::new(ERROR_MESSAGE)),
925 )
926 .child(
927 h_flex()
928 .justify_end()
929 .mt_1()
930 .child(
931 Button::new("subscribe", "Update Monthly Spend Limit").on_click(
932 cx.listener(|this, _, _, cx| {
933 this.thread.update(cx, |this, _cx| {
934 this.clear_last_error();
935 });
936
937 cx.open_url(&zed_urls::account_url(cx));
938 cx.notify();
939 }),
940 ),
941 )
942 .child(Button::new("dismiss", "Dismiss").on_click(cx.listener(
943 |this, _, _, cx| {
944 this.thread.update(cx, |this, _cx| {
945 this.clear_last_error();
946 });
947
948 cx.notify();
949 },
950 ))),
951 )
952 .into_any()
953 }
954
955 fn render_error_message(
956 &self,
957 error_message: &SharedString,
958 cx: &mut Context<Self>,
959 ) -> AnyElement {
960 v_flex()
961 .gap_0p5()
962 .child(
963 h_flex()
964 .gap_1p5()
965 .items_center()
966 .child(Icon::new(IconName::XCircle).color(Color::Error))
967 .child(
968 Label::new("Error interacting with language model")
969 .weight(FontWeight::MEDIUM),
970 ),
971 )
972 .child(
973 div()
974 .id("error-message")
975 .max_h_32()
976 .overflow_y_scroll()
977 .child(Label::new(error_message.clone())),
978 )
979 .child(
980 h_flex()
981 .justify_end()
982 .mt_1()
983 .child(Button::new("dismiss", "Dismiss").on_click(cx.listener(
984 |this, _, _, cx| {
985 this.thread.update(cx, |this, _cx| {
986 this.clear_last_error();
987 });
988
989 cx.notify();
990 },
991 ))),
992 )
993 .into_any()
994 }
995}
996
997impl Render for AssistantPanel {
998 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
999 v_flex()
1000 .key_context("AssistantPanel2")
1001 .justify_between()
1002 .size_full()
1003 .on_action(cx.listener(Self::cancel))
1004 .on_action(cx.listener(|this, _: &NewThread, window, cx| {
1005 this.new_thread(window, cx);
1006 }))
1007 .on_action(cx.listener(|this, _: &OpenHistory, window, cx| {
1008 this.open_history(window, cx);
1009 }))
1010 .on_action(cx.listener(Self::deploy_prompt_library))
1011 .child(self.render_toolbar(cx))
1012 .map(|parent| match self.active_view {
1013 ActiveView::Thread => parent
1014 .child(self.render_active_thread_or_empty_state(window, cx))
1015 .child(
1016 h_flex()
1017 .border_t_1()
1018 .border_color(cx.theme().colors().border)
1019 .child(self.message_editor.clone()),
1020 )
1021 .children(self.render_last_error(cx)),
1022 ActiveView::History => parent.child(self.history.clone()),
1023 ActiveView::PromptEditor => parent.children(self.context_editor.clone()),
1024 ActiveView::Configuration => parent.children(self.configuration.clone()),
1025 })
1026 }
1027}
1028
1029struct PromptLibraryInlineAssist {
1030 workspace: WeakEntity<Workspace>,
1031}
1032
1033impl PromptLibraryInlineAssist {
1034 pub fn new(workspace: WeakEntity<Workspace>) -> Self {
1035 Self { workspace }
1036 }
1037}
1038
1039impl prompt_library::InlineAssistDelegate for PromptLibraryInlineAssist {
1040 fn assist(
1041 &self,
1042 prompt_editor: &Entity<Editor>,
1043 _initial_prompt: Option<String>,
1044 window: &mut Window,
1045 cx: &mut Context<PromptLibrary>,
1046 ) {
1047 InlineAssistant::update_global(cx, |assistant, cx| {
1048 assistant.assist(&prompt_editor, self.workspace.clone(), None, window, cx)
1049 })
1050 }
1051
1052 fn focus_assistant_panel(
1053 &self,
1054 workspace: &mut Workspace,
1055 window: &mut Window,
1056 cx: &mut Context<Workspace>,
1057 ) -> bool {
1058 workspace
1059 .focus_panel::<AssistantPanel>(window, cx)
1060 .is_some()
1061 }
1062}
1063
1064pub struct ConcreteAssistantPanelDelegate;
1065
1066impl AssistantPanelDelegate for ConcreteAssistantPanelDelegate {
1067 fn active_context_editor(
1068 &self,
1069 workspace: &mut Workspace,
1070 _window: &mut Window,
1071 cx: &mut Context<Workspace>,
1072 ) -> Option<Entity<ContextEditor>> {
1073 let panel = workspace.panel::<AssistantPanel>(cx)?;
1074 panel.update(cx, |panel, _cx| panel.context_editor.clone())
1075 }
1076
1077 fn open_saved_context(
1078 &self,
1079 workspace: &mut Workspace,
1080 path: std::path::PathBuf,
1081 window: &mut Window,
1082 cx: &mut Context<Workspace>,
1083 ) -> Task<Result<()>> {
1084 let Some(panel) = workspace.panel::<AssistantPanel>(cx) else {
1085 return Task::ready(Err(anyhow!("Assistant panel not found")));
1086 };
1087
1088 panel.update(cx, |panel, cx| {
1089 panel.open_saved_prompt_editor(path, window, cx)
1090 })
1091 }
1092
1093 fn open_remote_context(
1094 &self,
1095 _workspace: &mut Workspace,
1096 _context_id: assistant_context_editor::ContextId,
1097 _window: &mut Window,
1098 _cx: &mut Context<Workspace>,
1099 ) -> Task<Result<Entity<ContextEditor>>> {
1100 Task::ready(Err(anyhow!("opening remote context not implemented")))
1101 }
1102
1103 fn quote_selection(
1104 &self,
1105 _workspace: &mut Workspace,
1106 _creases: Vec<(String, String)>,
1107 _window: &mut Window,
1108 _cx: &mut Context<Workspace>,
1109 ) {
1110 }
1111}