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 assistant_settings::{AgentProfileId, AssistantSettings, 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 CycleNextInlineAssist,
73 CyclePreviousInlineAssist,
74 FocusUp,
75 FocusDown,
76 FocusLeft,
77 FocusRight,
78 RemoveFocusedContext,
79 AcceptSuggestedContext,
80 OpenActiveThreadAsMarkdown,
81 OpenAgentDiff,
82 Keep,
83 Reject,
84 RejectAll,
85 KeepAll,
86 Follow,
87 ResetTrialUpsell,
88 ResetTrialEndUpsell,
89 ]
90);
91
92#[derive(Default, Clone, PartialEq, Deserialize, JsonSchema)]
93pub struct NewThread {
94 #[serde(default)]
95 from_thread_id: Option<ThreadId>,
96}
97
98#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
99pub struct ManageProfiles {
100 #[serde(default)]
101 pub customize_tools: Option<AgentProfileId>,
102}
103
104impl ManageProfiles {
105 pub fn customize_tools(profile_id: AgentProfileId) -> Self {
106 Self {
107 customize_tools: Some(profile_id),
108 }
109 }
110}
111
112impl_actions!(agent, [NewThread, ManageProfiles]);
113
114/// Initializes the `agent` crate.
115pub fn init(
116 fs: Arc<dyn Fs>,
117 client: Arc<Client>,
118 prompt_builder: Arc<PromptBuilder>,
119 language_registry: Arc<LanguageRegistry>,
120 is_eval: bool,
121 cx: &mut App,
122) {
123 AssistantSettings::register(cx);
124 SlashCommandSettings::register(cx);
125
126 assistant_context_editor::init(client.clone(), cx);
127 rules_library::init(cx);
128 if !is_eval {
129 // Initializing the language model from the user settings messes with the eval, so we only initialize them when
130 // we're not running inside of the eval.
131 init_language_model_settings(cx);
132 }
133 assistant_slash_command::init(cx);
134 thread_store::init(cx);
135 agent_panel::init(cx);
136 context_server_configuration::init(language_registry, cx);
137
138 register_slash_commands(cx);
139 inline_assistant::init(
140 fs.clone(),
141 prompt_builder.clone(),
142 client.telemetry().clone(),
143 cx,
144 );
145 terminal_inline_assistant::init(
146 fs.clone(),
147 prompt_builder.clone(),
148 client.telemetry().clone(),
149 cx,
150 );
151 indexed_docs::init(cx);
152 cx.observe_new(AddContextServerModal::register).detach();
153 cx.observe_new(ManageProfilesModal::register).detach();
154}
155
156fn init_language_model_settings(cx: &mut App) {
157 update_active_language_model_from_settings(cx);
158
159 cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
160 .detach();
161 cx.subscribe(
162 &LanguageModelRegistry::global(cx),
163 |_, event: &language_model::Event, cx| match event {
164 language_model::Event::ProviderStateChanged
165 | language_model::Event::AddedProvider(_)
166 | language_model::Event::RemovedProvider(_) => {
167 update_active_language_model_from_settings(cx);
168 }
169 _ => {}
170 },
171 )
172 .detach();
173}
174
175fn update_active_language_model_from_settings(cx: &mut App) {
176 let settings = AssistantSettings::get_global(cx);
177
178 fn to_selected_model(selection: &LanguageModelSelection) -> language_model::SelectedModel {
179 language_model::SelectedModel {
180 provider: LanguageModelProviderId::from(selection.provider.0.clone()),
181 model: LanguageModelId::from(selection.model.clone()),
182 }
183 }
184
185 let default = to_selected_model(&settings.default_model);
186 let inline_assistant = settings
187 .inline_assistant_model
188 .as_ref()
189 .map(to_selected_model);
190 let commit_message = settings
191 .commit_message_model
192 .as_ref()
193 .map(to_selected_model);
194 let thread_summary = settings
195 .thread_summary_model
196 .as_ref()
197 .map(to_selected_model);
198 let inline_alternatives = settings
199 .inline_alternatives
200 .iter()
201 .map(to_selected_model)
202 .collect::<Vec<_>>();
203
204 LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
205 registry.select_default_model(Some(&default), cx);
206 registry.select_inline_assistant_model(inline_assistant.as_ref(), cx);
207 registry.select_commit_message_model(commit_message.as_ref(), cx);
208 registry.select_thread_summary_model(thread_summary.as_ref(), cx);
209 registry.select_inline_alternative_models(inline_alternatives, cx);
210 });
211}
212
213fn register_slash_commands(cx: &mut App) {
214 let slash_command_registry = SlashCommandRegistry::global(cx);
215
216 slash_command_registry.register_command(assistant_slash_commands::FileSlashCommand, true);
217 slash_command_registry.register_command(assistant_slash_commands::DeltaSlashCommand, true);
218 slash_command_registry.register_command(assistant_slash_commands::OutlineSlashCommand, true);
219 slash_command_registry.register_command(assistant_slash_commands::TabSlashCommand, true);
220 slash_command_registry
221 .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
222 slash_command_registry.register_command(assistant_slash_commands::PromptSlashCommand, true);
223 slash_command_registry.register_command(assistant_slash_commands::SelectionCommand, true);
224 slash_command_registry.register_command(assistant_slash_commands::DefaultSlashCommand, false);
225 slash_command_registry.register_command(assistant_slash_commands::NowSlashCommand, false);
226 slash_command_registry
227 .register_command(assistant_slash_commands::DiagnosticsSlashCommand, true);
228 slash_command_registry.register_command(assistant_slash_commands::FetchSlashCommand, true);
229
230 cx.observe_flag::<assistant_slash_commands::StreamingExampleSlashCommandFeatureFlag, _>({
231 let slash_command_registry = slash_command_registry.clone();
232 move |is_enabled, _cx| {
233 if is_enabled {
234 slash_command_registry.register_command(
235 assistant_slash_commands::StreamingExampleSlashCommand,
236 false,
237 );
238 }
239 }
240 })
241 .detach();
242
243 update_slash_commands_from_settings(cx);
244 cx.observe_global::<SettingsStore>(update_slash_commands_from_settings)
245 .detach();
246}
247
248fn update_slash_commands_from_settings(cx: &mut App) {
249 let slash_command_registry = SlashCommandRegistry::global(cx);
250 let settings = SlashCommandSettings::get_global(cx);
251
252 if settings.docs.enabled {
253 slash_command_registry.register_command(assistant_slash_commands::DocsSlashCommand, true);
254 } else {
255 slash_command_registry.unregister_command(assistant_slash_commands::DocsSlashCommand);
256 }
257
258 if settings.cargo_workspace.enabled {
259 slash_command_registry
260 .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
261 } else {
262 slash_command_registry
263 .unregister_command(assistant_slash_commands::CargoWorkspaceSlashCommand);
264 }
265}