1use futures::{channel::oneshot, FutureExt as _};
2use gpui::{AppContext, Global, Subscription, ViewContext};
3use std::{
4 future::Future,
5 pin::Pin,
6 task::{Context, Poll},
7};
8
9#[derive(Default)]
10struct FeatureFlags {
11 flags: Vec<String>,
12 staff: bool,
13}
14
15impl FeatureFlags {
16 fn has_flag<T: FeatureFlag>(&self) -> bool {
17 if self.staff && T::enabled_for_staff() {
18 return true;
19 }
20
21 self.flags.iter().any(|f| f.as_str() == T::NAME)
22 }
23}
24
25impl Global for FeatureFlags {}
26
27/// To create a feature flag, implement this trait on a trivial type and use it as
28/// a generic parameter when called [`FeatureFlagAppExt::has_flag`].
29///
30/// Feature flags are enabled for members of Zed staff by default. To disable this behavior
31/// so you can test flags being disabled, set ZED_DISABLE_STAFF=1 in your environment,
32/// which will force Zed to treat the current user as non-staff.
33pub trait FeatureFlag {
34 const NAME: &'static str;
35
36 /// Returns whether this feature flag is enabled for Zed staff.
37 fn enabled_for_staff() -> bool {
38 true
39 }
40}
41
42pub struct Assistant2FeatureFlag;
43
44impl FeatureFlag for Assistant2FeatureFlag {
45 const NAME: &'static str = "assistant2";
46
47 fn enabled_for_staff() -> bool {
48 false
49 }
50}
51
52pub struct ToolUseFeatureFlag;
53
54impl FeatureFlag for ToolUseFeatureFlag {
55 const NAME: &'static str = "assistant-tool-use";
56
57 fn enabled_for_staff() -> bool {
58 false
59 }
60}
61
62pub struct ZetaFeatureFlag;
63impl FeatureFlag for ZetaFeatureFlag {
64 const NAME: &'static str = "zeta";
65}
66
67pub struct Remoting {}
68impl FeatureFlag for Remoting {
69 const NAME: &'static str = "remoting";
70}
71
72pub struct LanguageModels {}
73impl FeatureFlag for LanguageModels {
74 const NAME: &'static str = "language-models";
75}
76
77pub struct LlmClosedBeta {}
78impl FeatureFlag for LlmClosedBeta {
79 const NAME: &'static str = "llm-closed-beta";
80}
81
82pub struct ZedPro {}
83impl FeatureFlag for ZedPro {
84 const NAME: &'static str = "zed-pro";
85}
86
87pub struct NotebookFeatureFlag;
88
89impl FeatureFlag for NotebookFeatureFlag {
90 const NAME: &'static str = "notebooks";
91}
92
93pub struct AutoCommand {}
94impl FeatureFlag for AutoCommand {
95 const NAME: &'static str = "auto-command";
96
97 fn enabled_for_staff() -> bool {
98 false
99 }
100}
101
102pub trait FeatureFlagViewExt<V: 'static> {
103 fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
104 where
105 F: Fn(bool, &mut V, &mut ViewContext<V>) + Send + Sync + 'static;
106}
107
108impl<V> FeatureFlagViewExt<V> for ViewContext<'_, V>
109where
110 V: 'static,
111{
112 fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
113 where
114 F: Fn(bool, &mut V, &mut ViewContext<V>) + 'static,
115 {
116 self.observe_global::<FeatureFlags>(move |v, cx| {
117 let feature_flags = cx.global::<FeatureFlags>();
118 callback(feature_flags.has_flag::<T>(), v, cx);
119 })
120 }
121}
122
123pub trait FeatureFlagAppExt {
124 fn wait_for_flag<T: FeatureFlag>(&mut self) -> WaitForFlag;
125 fn update_flags(&mut self, staff: bool, flags: Vec<String>);
126 fn set_staff(&mut self, staff: bool);
127 fn has_flag<T: FeatureFlag>(&self) -> bool;
128 fn is_staff(&self) -> bool;
129
130 fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
131 where
132 F: FnMut(bool, &mut AppContext) + 'static;
133}
134
135impl FeatureFlagAppExt for AppContext {
136 fn update_flags(&mut self, staff: bool, flags: Vec<String>) {
137 let feature_flags = self.default_global::<FeatureFlags>();
138 feature_flags.staff = staff;
139 feature_flags.flags = flags;
140 }
141
142 fn set_staff(&mut self, staff: bool) {
143 let feature_flags = self.default_global::<FeatureFlags>();
144 feature_flags.staff = staff;
145 }
146
147 fn has_flag<T: FeatureFlag>(&self) -> bool {
148 self.try_global::<FeatureFlags>()
149 .map(|flags| flags.has_flag::<T>())
150 .unwrap_or(false)
151 }
152
153 fn is_staff(&self) -> bool {
154 self.try_global::<FeatureFlags>()
155 .map(|flags| flags.staff)
156 .unwrap_or(false)
157 }
158
159 fn observe_flag<T: FeatureFlag, F>(&mut self, mut callback: F) -> Subscription
160 where
161 F: FnMut(bool, &mut AppContext) + 'static,
162 {
163 self.observe_global::<FeatureFlags>(move |cx| {
164 let feature_flags = cx.global::<FeatureFlags>();
165 callback(feature_flags.has_flag::<T>(), cx);
166 })
167 }
168
169 fn wait_for_flag<T: FeatureFlag>(&mut self) -> WaitForFlag {
170 let (tx, rx) = oneshot::channel::<bool>();
171 let mut tx = Some(tx);
172 let subscription: Option<Subscription>;
173
174 match self.try_global::<FeatureFlags>() {
175 Some(feature_flags) => {
176 subscription = None;
177 tx.take().unwrap().send(feature_flags.has_flag::<T>()).ok();
178 }
179 None => {
180 subscription = Some(self.observe_global::<FeatureFlags>(move |cx| {
181 let feature_flags = cx.global::<FeatureFlags>();
182 if let Some(tx) = tx.take() {
183 tx.send(feature_flags.has_flag::<T>()).ok();
184 }
185 }));
186 }
187 }
188
189 WaitForFlag(rx, subscription)
190 }
191}
192
193pub struct WaitForFlag(oneshot::Receiver<bool>, Option<Subscription>);
194
195impl Future for WaitForFlag {
196 type Output = bool;
197
198 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
199 self.0.poll_unpin(cx).map(|result| {
200 self.1.take();
201 result.unwrap_or(false)
202 })
203 }
204}