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