agent.rs

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