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, Entity, actions, impl_actions};
37use language::LanguageRegistry;
38use language_model::{
39 ConfiguredModel, LanguageModelId, LanguageModelProviderId, LanguageModelRegistry,
40};
41use prompt_store::PromptBuilder;
42use schemars::JsonSchema;
43use serde::Deserialize;
44use settings::{Settings as _, SettingsStore};
45use thread::ThreadId;
46
47pub use crate::active_thread::ActiveThread;
48use crate::agent_configuration::{AddContextServerModal, ManageProfilesModal};
49pub use crate::agent_panel::{AgentPanel, ConcreteAssistantPanelDelegate};
50pub use crate::context::{ContextLoadResult, LoadedContext};
51pub use crate::inline_assistant::InlineAssistant;
52use crate::slash_command_settings::SlashCommandSettings;
53pub use crate::thread::{Message, MessageSegment, Thread, ThreadEvent};
54pub use crate::thread_store::{SerializedThread, TextThreadStore, ThreadStore};
55pub use agent_diff::{AgentDiffPane, AgentDiffToolbar};
56pub use context_store::ContextStore;
57pub use ui::preview::{all_agent_previews, get_agent_preview};
58
59actions!(
60 agent,
61 [
62 NewTextThread,
63 ToggleContextPicker,
64 ToggleNavigationMenu,
65 ToggleOptionsMenu,
66 DeleteRecentlyOpenThread,
67 ToggleProfileSelector,
68 RemoveAllContext,
69 ExpandMessageEditor,
70 OpenHistory,
71 AddContextServer,
72 RemoveSelectedThread,
73 Chat,
74 ChatWithFollow,
75 CycleNextInlineAssist,
76 CyclePreviousInlineAssist,
77 FocusUp,
78 FocusDown,
79 FocusLeft,
80 FocusRight,
81 RemoveFocusedContext,
82 AcceptSuggestedContext,
83 OpenActiveThreadAsMarkdown,
84 OpenAgentDiff,
85 Keep,
86 Reject,
87 RejectAll,
88 KeepAll,
89 Follow,
90 ResetTrialUpsell,
91 ResetTrialEndUpsell,
92 ContinueThread,
93 ContinueWithBurnMode,
94 ToggleBurnMode,
95 ]
96);
97
98#[derive(Default, Clone, PartialEq, Deserialize, JsonSchema)]
99pub struct NewThread {
100 #[serde(default)]
101 from_thread_id: Option<ThreadId>,
102}
103
104#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
105pub struct ManageProfiles {
106 #[serde(default)]
107 pub customize_tools: Option<AgentProfileId>,
108}
109
110impl ManageProfiles {
111 pub fn customize_tools(profile_id: AgentProfileId) -> Self {
112 Self {
113 customize_tools: Some(profile_id),
114 }
115 }
116}
117
118impl_actions!(agent, [NewThread, ManageProfiles]);
119
120#[derive(Clone)]
121pub(crate) enum ModelUsageContext {
122 Thread(Entity<Thread>),
123 InlineAssistant,
124}
125
126impl ModelUsageContext {
127 pub fn configured_model(&self, cx: &App) -> Option<ConfiguredModel> {
128 match self {
129 Self::Thread(thread) => thread.read(cx).configured_model(),
130 Self::InlineAssistant => {
131 LanguageModelRegistry::read_global(cx).inline_assistant_model()
132 }
133 }
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_editor::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 thread_store::init(cx);
158 agent_panel::init(cx);
159 context_server_configuration::init(language_registry, cx);
160
161 register_slash_commands(cx);
162 inline_assistant::init(
163 fs.clone(),
164 prompt_builder.clone(),
165 client.telemetry().clone(),
166 cx,
167 );
168 terminal_inline_assistant::init(
169 fs.clone(),
170 prompt_builder.clone(),
171 client.telemetry().clone(),
172 cx,
173 );
174 indexed_docs::init(cx);
175 cx.observe_new(AddContextServerModal::register).detach();
176 cx.observe_new(ManageProfilesModal::register).detach();
177}
178
179fn init_language_model_settings(cx: &mut App) {
180 update_active_language_model_from_settings(cx);
181
182 cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
183 .detach();
184 cx.subscribe(
185 &LanguageModelRegistry::global(cx),
186 |_, event: &language_model::Event, cx| match event {
187 language_model::Event::ProviderStateChanged
188 | language_model::Event::AddedProvider(_)
189 | language_model::Event::RemovedProvider(_) => {
190 update_active_language_model_from_settings(cx);
191 }
192 _ => {}
193 },
194 )
195 .detach();
196}
197
198fn update_active_language_model_from_settings(cx: &mut App) {
199 let settings = AgentSettings::get_global(cx);
200
201 fn to_selected_model(selection: &LanguageModelSelection) -> language_model::SelectedModel {
202 language_model::SelectedModel {
203 provider: LanguageModelProviderId::from(selection.provider.0.clone()),
204 model: LanguageModelId::from(selection.model.clone()),
205 }
206 }
207
208 let default = to_selected_model(&settings.default_model);
209 let inline_assistant = settings
210 .inline_assistant_model
211 .as_ref()
212 .map(to_selected_model);
213 let commit_message = settings
214 .commit_message_model
215 .as_ref()
216 .map(to_selected_model);
217 let thread_summary = settings
218 .thread_summary_model
219 .as_ref()
220 .map(to_selected_model);
221 let inline_alternatives = settings
222 .inline_alternatives
223 .iter()
224 .map(to_selected_model)
225 .collect::<Vec<_>>();
226
227 LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
228 registry.select_default_model(Some(&default), cx);
229 registry.select_inline_assistant_model(inline_assistant.as_ref(), cx);
230 registry.select_commit_message_model(commit_message.as_ref(), cx);
231 registry.select_thread_summary_model(thread_summary.as_ref(), cx);
232 registry.select_inline_alternative_models(inline_alternatives, cx);
233 });
234}
235
236fn register_slash_commands(cx: &mut App) {
237 let slash_command_registry = SlashCommandRegistry::global(cx);
238
239 slash_command_registry.register_command(assistant_slash_commands::FileSlashCommand, true);
240 slash_command_registry.register_command(assistant_slash_commands::DeltaSlashCommand, true);
241 slash_command_registry.register_command(assistant_slash_commands::OutlineSlashCommand, true);
242 slash_command_registry.register_command(assistant_slash_commands::TabSlashCommand, true);
243 slash_command_registry
244 .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
245 slash_command_registry.register_command(assistant_slash_commands::PromptSlashCommand, true);
246 slash_command_registry.register_command(assistant_slash_commands::SelectionCommand, true);
247 slash_command_registry.register_command(assistant_slash_commands::DefaultSlashCommand, false);
248 slash_command_registry.register_command(assistant_slash_commands::NowSlashCommand, false);
249 slash_command_registry
250 .register_command(assistant_slash_commands::DiagnosticsSlashCommand, true);
251 slash_command_registry.register_command(assistant_slash_commands::FetchSlashCommand, true);
252
253 cx.observe_flag::<assistant_slash_commands::StreamingExampleSlashCommandFeatureFlag, _>({
254 let slash_command_registry = slash_command_registry.clone();
255 move |is_enabled, _cx| {
256 if is_enabled {
257 slash_command_registry.register_command(
258 assistant_slash_commands::StreamingExampleSlashCommand,
259 false,
260 );
261 }
262 }
263 })
264 .detach();
265
266 update_slash_commands_from_settings(cx);
267 cx.observe_global::<SettingsStore>(update_slash_commands_from_settings)
268 .detach();
269}
270
271fn update_slash_commands_from_settings(cx: &mut App) {
272 let slash_command_registry = SlashCommandRegistry::global(cx);
273 let settings = SlashCommandSettings::get_global(cx);
274
275 if settings.docs.enabled {
276 slash_command_registry.register_command(assistant_slash_commands::DocsSlashCommand, true);
277 } else {
278 slash_command_registry.unregister_command(assistant_slash_commands::DocsSlashCommand);
279 }
280
281 if settings.cargo_workspace.enabled {
282 slash_command_registry
283 .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
284 } else {
285 slash_command_registry
286 .unregister_command(assistant_slash_commands::CargoWorkspaceSlashCommand);
287 }
288}