1use std::any::{Any, TypeId};
2
3use command_palette_hooks::CommandPaletteFilter;
4use feature_flags::{FeatureFlagAppExt as _, PredictEditsRateCompletionsFeatureFlag};
5use gpui::actions;
6use language::language_settings::EditPredictionProvider;
7use project::DisableAiSettings;
8use settings::{Settings, SettingsStore, update_settings_file};
9use ui::App;
10use workspace::Workspace;
11
12use crate::{RateCompletionModal, onboarding_modal::ZedPredictModal};
13
14actions!(
15 edit_prediction,
16 [
17 /// Resets the edit prediction onboarding state.
18 ResetOnboarding,
19 /// Opens the rate completions modal.
20 RateCompletions
21 ]
22);
23
24pub fn init(cx: &mut App) {
25 feature_gate_predict_edits_actions(cx);
26
27 cx.observe_new(move |workspace: &mut Workspace, _, _cx| {
28 workspace.register_action(|workspace, _: &RateCompletions, window, cx| {
29 if cx.has_flag::<PredictEditsRateCompletionsFeatureFlag>() {
30 RateCompletionModal::toggle(workspace, window, cx);
31 }
32 });
33
34 workspace.register_action(
35 move |workspace, _: &zed_actions::OpenZedPredictOnboarding, window, cx| {
36 ZedPredictModal::toggle(
37 workspace,
38 workspace.user_store().clone(),
39 workspace.client().clone(),
40 window,
41 cx,
42 )
43 },
44 );
45
46 workspace.register_action(|workspace, _: &ResetOnboarding, _window, cx| {
47 update_settings_file(workspace.app_state().fs.clone(), cx, move |settings, _| {
48 settings
49 .project
50 .all_languages
51 .features
52 .get_or_insert_default()
53 .edit_prediction_provider = Some(EditPredictionProvider::None)
54 });
55 });
56 })
57 .detach();
58}
59
60fn feature_gate_predict_edits_actions(cx: &mut App) {
61 let rate_completion_action_types = [TypeId::of::<RateCompletions>()];
62 let reset_onboarding_action_types = [TypeId::of::<ResetOnboarding>()];
63 let zeta_all_action_types = [
64 TypeId::of::<RateCompletions>(),
65 TypeId::of::<ResetOnboarding>(),
66 zed_actions::OpenZedPredictOnboarding.type_id(),
67 TypeId::of::<crate::ClearHistory>(),
68 TypeId::of::<crate::ThumbsUpActiveCompletion>(),
69 TypeId::of::<crate::ThumbsDownActiveCompletion>(),
70 TypeId::of::<crate::NextEdit>(),
71 TypeId::of::<crate::PreviousEdit>(),
72 ];
73
74 CommandPaletteFilter::update_global(cx, |filter, _cx| {
75 filter.hide_action_types(&rate_completion_action_types);
76 filter.hide_action_types(&reset_onboarding_action_types);
77 filter.hide_action_types(&[zed_actions::OpenZedPredictOnboarding.type_id()]);
78 });
79
80 cx.observe_global::<SettingsStore>(move |cx| {
81 let is_ai_disabled = DisableAiSettings::get_global(cx).disable_ai;
82 let has_feature_flag = cx.has_flag::<PredictEditsRateCompletionsFeatureFlag>();
83
84 CommandPaletteFilter::update_global(cx, |filter, _cx| {
85 if is_ai_disabled {
86 filter.hide_action_types(&zeta_all_action_types);
87 } else if has_feature_flag {
88 filter.show_action_types(&rate_completion_action_types);
89 } else {
90 filter.hide_action_types(&rate_completion_action_types);
91 }
92 });
93 })
94 .detach();
95
96 cx.observe_flag::<PredictEditsRateCompletionsFeatureFlag, _>(move |is_enabled, cx| {
97 if !DisableAiSettings::get_global(cx).disable_ai {
98 if is_enabled {
99 CommandPaletteFilter::update_global(cx, |filter, _cx| {
100 filter.show_action_types(&rate_completion_action_types);
101 });
102 } else {
103 CommandPaletteFilter::update_global(cx, |filter, _cx| {
104 filter.hide_action_types(&rate_completion_action_types);
105 });
106 }
107 }
108 })
109 .detach();
110}