@@ -46,7 +46,7 @@ use paths::{
46
46
local_debug_file_relative_path, local_settings_file_relative_path,
47
47
local_tasks_file_relative_path,
48
48
} ;
49
- use project:: { DirectoryLister , ProjectItem } ;
49
+ use project:: { DirectoryLister , DisableAiSettings , ProjectItem } ;
50
50
use project_panel:: ProjectPanel ;
51
51
use prompt_store:: PromptBuilder ;
52
52
use quick_action_bar:: QuickActionBar ;
@@ -604,29 +604,63 @@ fn initialize_panels(
604
604
workspace. add_panel ( debug_panel, window, cx) ;
605
605
} ) ?;
606
606
607
- let is_assistant2_enabled = !cfg ! ( test) ;
608
- let agent_panel = if is_assistant2_enabled {
609
- let agent_panel =
610
- agent_ui:: AgentPanel :: load ( workspace_handle. clone ( ) , prompt_builder, cx. clone ( ) )
611
- . await ?;
607
+ fn setup_or_teardown_agent_panel (
608
+ workspace : & mut Workspace ,
609
+ prompt_builder : Arc < PromptBuilder > ,
610
+ window : & mut Window ,
611
+ cx : & mut Context < Workspace > ,
612
+ ) -> Task < anyhow:: Result < ( ) > > {
613
+ let disable_ai = SettingsStore :: global ( cx)
614
+ . get :: < DisableAiSettings > ( None )
615
+ . disable_ai
616
+ || cfg ! ( test) ;
617
+ let existing_panel = workspace. panel :: < agent_ui:: AgentPanel > ( cx) ;
618
+ match ( disable_ai, existing_panel) {
619
+ ( false , None ) => cx. spawn_in ( window, async move |workspace, cx| {
620
+ let panel =
621
+ agent_ui:: AgentPanel :: load ( workspace. clone ( ) , prompt_builder, cx. clone ( ) )
622
+ . await ?;
623
+ workspace. update_in ( cx, |workspace, window, cx| {
624
+ let disable_ai = SettingsStore :: global ( cx)
625
+ . get :: < DisableAiSettings > ( None )
626
+ . disable_ai ;
627
+ let have_panel = workspace. panel :: < agent_ui:: AgentPanel > ( cx) . is_some ( ) ;
628
+ if !disable_ai && !have_panel {
629
+ workspace. add_panel ( panel, window, cx) ;
630
+ }
631
+ } )
632
+ } ) ,
633
+ ( true , Some ( existing_panel) ) => {
634
+ workspace. remove_panel :: < agent_ui:: AgentPanel > ( & existing_panel, window, cx) ;
635
+ Task :: ready ( Ok ( ( ) ) )
636
+ }
637
+ _ => Task :: ready ( Ok ( ( ) ) ) ,
638
+ }
639
+ }
612
640
613
- Some ( agent_panel)
614
- } else {
615
- None
616
- } ;
641
+ workspace_handle
642
+ . update_in ( cx, |workspace, window, cx| {
643
+ setup_or_teardown_agent_panel ( workspace, prompt_builder. clone ( ) , window, cx)
644
+ } ) ?
645
+ . await ?;
617
646
618
647
workspace_handle. update_in ( cx, |workspace, window, cx| {
619
- if let Some ( agent_panel) = agent_panel {
620
- workspace. add_panel ( agent_panel, window, cx) ;
621
- }
648
+ cx. observe_global_in :: < SettingsStore > ( window, {
649
+ let prompt_builder = prompt_builder. clone ( ) ;
650
+ move |workspace, window, cx| {
651
+ setup_or_teardown_agent_panel ( workspace, prompt_builder. clone ( ) , window, cx)
652
+ . detach_and_log_err ( cx) ;
653
+ }
654
+ } )
655
+ . detach ( ) ;
622
656
623
657
// Register the actions that are shared between `assistant` and `assistant2`.
624
658
//
625
659
// We need to do this here instead of within the individual `init`
626
660
// functions so that we only register the actions once.
627
661
//
628
662
// Once we ship `assistant2` we can push this back down into `agent::agent_panel::init`.
629
- if is_assistant2_enabled {
663
+ if ! cfg ! ( test ) {
630
664
<dyn AgentPanelDelegate >:: set_global (
631
665
Arc :: new ( agent_ui:: ConcreteAssistantPanelDelegate ) ,
632
666
cx,
0 commit comments