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 cx: &mut App,
121) {
122 AssistantSettings::register(cx);
123 SlashCommandSettings::register(cx);
124
125 assistant_context_editor::init(client.clone(), cx);
126 rules_library::init(cx);
127 init_language_model_settings(cx);
128 assistant_slash_command::init(cx);
129 thread_store::init(cx);
130 agent_panel::init(cx);
131 context_server_configuration::init(language_registry, cx);
132
133 register_slash_commands(cx);
134 inline_assistant::init(
135 fs.clone(),
136 prompt_builder.clone(),
137 client.telemetry().clone(),
138 cx,
139 );
140 terminal_inline_assistant::init(
141 fs.clone(),
142 prompt_builder.clone(),
143 client.telemetry().clone(),
144 cx,
145 );
146 indexed_docs::init(cx);
147 cx.observe_new(AddContextServerModal::register).detach();
148 cx.observe_new(ManageProfilesModal::register).detach();
149}
150
151fn init_language_model_settings(cx: &mut App) {
152 update_active_language_model_from_settings(cx);
153
154 cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
155 .detach();
156 cx.subscribe(
157 &LanguageModelRegistry::global(cx),
158 |_, event: &language_model::Event, cx| match event {
159 language_model::Event::ProviderStateChanged
160 | language_model::Event::AddedProvider(_)
161 | language_model::Event::RemovedProvider(_) => {
162 update_active_language_model_from_settings(cx);
163 }
164 _ => {}
165 },
166 )
167 .detach();
168}
169
170fn update_active_language_model_from_settings(cx: &mut App) {
171 let settings = AssistantSettings::get_global(cx);
172
173 fn to_selected_model(selection: &LanguageModelSelection) -> language_model::SelectedModel {
174 language_model::SelectedModel {
175 provider: LanguageModelProviderId::from(selection.provider.0.clone()),
176 model: LanguageModelId::from(selection.model.clone()),
177 }
178 }
179
180 let default = to_selected_model(&settings.default_model);
181 let inline_assistant = settings
182 .inline_assistant_model
183 .as_ref()
184 .map(to_selected_model);
185 let commit_message = settings
186 .commit_message_model
187 .as_ref()
188 .map(to_selected_model);
189 let thread_summary = settings
190 .thread_summary_model
191 .as_ref()
192 .map(to_selected_model);
193 let inline_alternatives = settings
194 .inline_alternatives
195 .iter()
196 .map(to_selected_model)
197 .collect::<Vec<_>>();
198
199 LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
200 registry.select_default_model(Some(&default), cx);
201 registry.select_inline_assistant_model(inline_assistant.as_ref(), cx);
202 registry.select_commit_message_model(commit_message.as_ref(), cx);
203 registry.select_thread_summary_model(thread_summary.as_ref(), cx);
204 registry.select_inline_alternative_models(inline_alternatives, cx);
205 });
206}
207
208fn register_slash_commands(cx: &mut App) {
209 let slash_command_registry = SlashCommandRegistry::global(cx);
210
211 slash_command_registry.register_command(assistant_slash_commands::FileSlashCommand, true);
212 slash_command_registry.register_command(assistant_slash_commands::DeltaSlashCommand, true);
213 slash_command_registry.register_command(assistant_slash_commands::OutlineSlashCommand, true);
214 slash_command_registry.register_command(assistant_slash_commands::TabSlashCommand, true);
215 slash_command_registry
216 .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
217 slash_command_registry.register_command(assistant_slash_commands::PromptSlashCommand, true);
218 slash_command_registry.register_command(assistant_slash_commands::SelectionCommand, true);
219 slash_command_registry.register_command(assistant_slash_commands::DefaultSlashCommand, false);
220 slash_command_registry.register_command(assistant_slash_commands::NowSlashCommand, false);
221 slash_command_registry
222 .register_command(assistant_slash_commands::DiagnosticsSlashCommand, true);
223 slash_command_registry.register_command(assistant_slash_commands::FetchSlashCommand, true);
224
225 cx.observe_flag::<assistant_slash_commands::StreamingExampleSlashCommandFeatureFlag, _>({
226 let slash_command_registry = slash_command_registry.clone();
227 move |is_enabled, _cx| {
228 if is_enabled {
229 slash_command_registry.register_command(
230 assistant_slash_commands::StreamingExampleSlashCommand,
231 false,
232 );
233 }
234 }
235 })
236 .detach();
237
238 update_slash_commands_from_settings(cx);
239 cx.observe_global::<SettingsStore>(update_slash_commands_from_settings)
240 .detach();
241}
242
243fn update_slash_commands_from_settings(cx: &mut App) {
244 let slash_command_registry = SlashCommandRegistry::global(cx);
245 let settings = SlashCommandSettings::get_global(cx);
246
247 if settings.docs.enabled {
248 slash_command_registry.register_command(assistant_slash_commands::DocsSlashCommand, true);
249 } else {
250 slash_command_registry.unregister_command(assistant_slash_commands::DocsSlashCommand);
251 }
252
253 if settings.cargo_workspace.enabled {
254 slash_command_registry
255 .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
256 } else {
257 slash_command_registry
258 .unregister_command(assistant_slash_commands::CargoWorkspaceSlashCommand);
259 }
260}