1mod active_thread;
2mod agent_configuration;
3mod agent_diff;
4mod agent_model_selector;
5mod agent_panel;
6mod buffer_codegen;
7mod burn_mode_tooltip;
8mod context_picker;
9mod context_server_configuration;
10mod context_strip;
11mod debug;
12mod inline_assistant;
13mod inline_prompt_editor;
14mod language_model_selector;
15mod message_editor;
16mod profile_selector;
17mod slash_command;
18mod slash_command_picker;
19mod slash_command_settings;
20mod terminal_codegen;
21mod terminal_inline_assistant;
22mod text_thread_editor;
23mod thread_history;
24mod tool_compatibility;
25mod ui;
26
27use std::sync::Arc;
28
29use agent::{ThreadId, ZedAgentThread};
30use agent_settings::{AgentProfileId, AgentSettings, LanguageModelSelection};
31use assistant_slash_command::SlashCommandRegistry;
32use client::Client;
33use feature_flags::FeatureFlagAppExt as _;
34use fs::Fs;
35use gpui::{Action, App, Entity, actions};
36use language::LanguageRegistry;
37use language_model::{
38 ConfiguredModel, LanguageModel, LanguageModelId, LanguageModelProviderId, LanguageModelRegistry,
39};
40use prompt_store::PromptBuilder;
41use schemars::JsonSchema;
42use serde::Deserialize;
43use settings::{Settings as _, SettingsStore};
44
45pub use crate::active_thread::ActiveThread;
46use crate::agent_configuration::{ConfigureContextServerModal, ManageProfilesModal};
47pub use crate::agent_panel::{AgentPanel, ConcreteAssistantPanelDelegate};
48pub use crate::inline_assistant::InlineAssistant;
49use crate::slash_command_settings::SlashCommandSettings;
50pub use agent_diff::{AgentDiffPane, AgentDiffToolbar};
51pub use text_thread_editor::{AgentPanelDelegate, TextThreadEditor};
52pub use ui::preview::{all_agent_previews, get_agent_preview};
53
54actions!(
55 agent,
56 [
57 NewTextThread,
58 ToggleContextPicker,
59 ToggleNavigationMenu,
60 ToggleOptionsMenu,
61 DeleteRecentlyOpenThread,
62 ToggleProfileSelector,
63 RemoveAllContext,
64 ExpandMessageEditor,
65 OpenHistory,
66 AddContextServer,
67 RemoveSelectedThread,
68 Chat,
69 ChatWithFollow,
70 CycleNextInlineAssist,
71 CyclePreviousInlineAssist,
72 FocusUp,
73 FocusDown,
74 FocusLeft,
75 FocusRight,
76 RemoveFocusedContext,
77 AcceptSuggestedContext,
78 OpenActiveThreadAsMarkdown,
79 OpenAgentDiff,
80 Keep,
81 Reject,
82 RejectAll,
83 KeepAll,
84 Follow,
85 ResetTrialUpsell,
86 ResetTrialEndUpsell,
87 ContinueThread,
88 ContinueWithBurnMode,
89 ToggleBurnMode,
90 ]
91);
92
93#[derive(Default, Clone, PartialEq, Deserialize, JsonSchema, Action)]
94#[action(namespace = agent)]
95pub struct NewThread {
96 #[serde(default)]
97 from_thread_id: Option<ThreadId>,
98}
99
100#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
101#[action(namespace = agent)]
102pub struct ManageProfiles {
103 #[serde(default)]
104 pub customize_tools: Option<AgentProfileId>,
105}
106
107impl ManageProfiles {
108 pub fn customize_tools(profile_id: AgentProfileId) -> Self {
109 Self {
110 customize_tools: Some(profile_id),
111 }
112 }
113}
114
115#[derive(Clone)]
116pub(crate) enum ModelUsageContext {
117 Thread(Entity<ZedAgentThread>),
118 InlineAssistant,
119}
120
121impl ModelUsageContext {
122 pub fn configured_model(&self, cx: &App) -> Option<ConfiguredModel> {
123 match self {
124 Self::Thread(thread) => thread.read(cx).configured_model(),
125 Self::InlineAssistant => {
126 LanguageModelRegistry::read_global(cx).inline_assistant_model()
127 }
128 }
129 }
130
131 pub fn language_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {
132 self.configured_model(cx)
133 .map(|configured_model| configured_model.model)
134 }
135}
136
137/// Initializes the `agent` crate.
138pub fn init(
139 fs: Arc<dyn Fs>,
140 client: Arc<Client>,
141 prompt_builder: Arc<PromptBuilder>,
142 language_registry: Arc<LanguageRegistry>,
143 is_eval: bool,
144 cx: &mut App,
145) {
146 AgentSettings::register(cx);
147 SlashCommandSettings::register(cx);
148
149 assistant_context::init(client.clone(), cx);
150 rules_library::init(cx);
151 if !is_eval {
152 // Initializing the language model from the user settings messes with the eval, so we only initialize them when
153 // we're not running inside of the eval.
154 init_language_model_settings(cx);
155 }
156 assistant_slash_command::init(cx);
157 agent::init(cx);
158 agent_panel::init(cx);
159 context_server_configuration::init(language_registry.clone(), fs.clone(), cx);
160 TextThreadEditor::init(cx);
161
162 register_slash_commands(cx);
163 inline_assistant::init(
164 fs.clone(),
165 prompt_builder.clone(),
166 client.telemetry().clone(),
167 cx,
168 );
169 terminal_inline_assistant::init(
170 fs.clone(),
171 prompt_builder.clone(),
172 client.telemetry().clone(),
173 cx,
174 );
175 indexed_docs::init(cx);
176 cx.observe_new(move |workspace, window, cx| {
177 ConfigureContextServerModal::register(workspace, language_registry.clone(), window, cx)
178 })
179 .detach();
180 cx.observe_new(ManageProfilesModal::register).detach();
181}
182
183fn init_language_model_settings(cx: &mut App) {
184 update_active_language_model_from_settings(cx);
185
186 cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
187 .detach();
188 cx.subscribe(
189 &LanguageModelRegistry::global(cx),
190 |_, event: &language_model::Event, cx| match event {
191 language_model::Event::ProviderStateChanged
192 | language_model::Event::AddedProvider(_)
193 | language_model::Event::RemovedProvider(_) => {
194 update_active_language_model_from_settings(cx);
195 }
196 _ => {}
197 },
198 )
199 .detach();
200}
201
202fn update_active_language_model_from_settings(cx: &mut App) {
203 let settings = AgentSettings::get_global(cx);
204
205 fn to_selected_model(selection: &LanguageModelSelection) -> language_model::SelectedModel {
206 language_model::SelectedModel {
207 provider: LanguageModelProviderId::from(selection.provider.0.clone()),
208 model: LanguageModelId::from(selection.model.clone()),
209 }
210 }
211
212 let default = to_selected_model(&settings.default_model);
213 let inline_assistant = settings
214 .inline_assistant_model
215 .as_ref()
216 .map(to_selected_model);
217 let commit_message = settings
218 .commit_message_model
219 .as_ref()
220 .map(to_selected_model);
221 let thread_summary = settings
222 .thread_summary_model
223 .as_ref()
224 .map(to_selected_model);
225 let inline_alternatives = settings
226 .inline_alternatives
227 .iter()
228 .map(to_selected_model)
229 .collect::<Vec<_>>();
230
231 LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
232 registry.select_default_model(Some(&default), cx);
233 registry.select_inline_assistant_model(inline_assistant.as_ref(), cx);
234 registry.select_commit_message_model(commit_message.as_ref(), cx);
235 registry.select_thread_summary_model(thread_summary.as_ref(), cx);
236 registry.select_inline_alternative_models(inline_alternatives, cx);
237 });
238}
239
240fn register_slash_commands(cx: &mut App) {
241 let slash_command_registry = SlashCommandRegistry::global(cx);
242
243 slash_command_registry.register_command(assistant_slash_commands::FileSlashCommand, true);
244 slash_command_registry.register_command(assistant_slash_commands::DeltaSlashCommand, true);
245 slash_command_registry.register_command(assistant_slash_commands::OutlineSlashCommand, true);
246 slash_command_registry.register_command(assistant_slash_commands::TabSlashCommand, true);
247 slash_command_registry
248 .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
249 slash_command_registry.register_command(assistant_slash_commands::PromptSlashCommand, true);
250 slash_command_registry.register_command(assistant_slash_commands::SelectionCommand, true);
251 slash_command_registry.register_command(assistant_slash_commands::DefaultSlashCommand, false);
252 slash_command_registry.register_command(assistant_slash_commands::NowSlashCommand, false);
253 slash_command_registry
254 .register_command(assistant_slash_commands::DiagnosticsSlashCommand, true);
255 slash_command_registry.register_command(assistant_slash_commands::FetchSlashCommand, true);
256
257 cx.observe_flag::<assistant_slash_commands::StreamingExampleSlashCommandFeatureFlag, _>({
258 let slash_command_registry = slash_command_registry.clone();
259 move |is_enabled, _cx| {
260 if is_enabled {
261 slash_command_registry.register_command(
262 assistant_slash_commands::StreamingExampleSlashCommand,
263 false,
264 );
265 }
266 }
267 })
268 .detach();
269
270 update_slash_commands_from_settings(cx);
271 cx.observe_global::<SettingsStore>(update_slash_commands_from_settings)
272 .detach();
273}
274
275fn update_slash_commands_from_settings(cx: &mut App) {
276 let slash_command_registry = SlashCommandRegistry::global(cx);
277 let settings = SlashCommandSettings::get_global(cx);
278
279 if settings.docs.enabled {
280 slash_command_registry.register_command(assistant_slash_commands::DocsSlashCommand, true);
281 } else {
282 slash_command_registry.unregister_command(assistant_slash_commands::DocsSlashCommand);
283 }
284
285 if settings.cargo_workspace.enabled {
286 slash_command_registry
287 .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
288 } else {
289 slash_command_registry
290 .unregister_command(assistant_slash_commands::CargoWorkspaceSlashCommand);
291 }
292}