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