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 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    ]
 89);
 90
 91#[derive(Default, Clone, PartialEq, Deserialize, JsonSchema)]
 92pub struct NewThread {
 93    #[serde(default)]
 94    from_thread_id: Option<ThreadId>,
 95}
 96
 97#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
 98pub struct ManageProfiles {
 99    #[serde(default)]
100    pub customize_tools: Option<AgentProfileId>,
101}
102
103impl ManageProfiles {
104    pub fn customize_tools(profile_id: AgentProfileId) -> Self {
105        Self {
106            customize_tools: Some(profile_id),
107        }
108    }
109}
110
111impl_actions!(agent, [NewThread, ManageProfiles]);
112
113/// Initializes the `agent` crate.
114pub fn init(
115    fs: Arc<dyn Fs>,
116    client: Arc<Client>,
117    prompt_builder: Arc<PromptBuilder>,
118    language_registry: Arc<LanguageRegistry>,
119    cx: &mut App,
120) {
121    AssistantSettings::register(cx);
122    SlashCommandSettings::register(cx);
123
124    assistant_context_editor::init(client.clone(), cx);
125    rules_library::init(cx);
126    init_language_model_settings(cx);
127    assistant_slash_command::init(cx);
128    thread_store::init(cx);
129    agent_panel::init(cx);
130    context_server_configuration::init(language_registry, cx);
131
132    register_slash_commands(cx);
133    inline_assistant::init(
134        fs.clone(),
135        prompt_builder.clone(),
136        client.telemetry().clone(),
137        cx,
138    );
139    terminal_inline_assistant::init(
140        fs.clone(),
141        prompt_builder.clone(),
142        client.telemetry().clone(),
143        cx,
144    );
145    indexed_docs::init(cx);
146    cx.observe_new(AddContextServerModal::register).detach();
147    cx.observe_new(ManageProfilesModal::register).detach();
148}
149
150fn init_language_model_settings(cx: &mut App) {
151    update_active_language_model_from_settings(cx);
152
153    cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
154        .detach();
155    cx.subscribe(
156        &LanguageModelRegistry::global(cx),
157        |_, event: &language_model::Event, cx| match event {
158            language_model::Event::ProviderStateChanged
159            | language_model::Event::AddedProvider(_)
160            | language_model::Event::RemovedProvider(_) => {
161                update_active_language_model_from_settings(cx);
162            }
163            _ => {}
164        },
165    )
166    .detach();
167}
168
169fn update_active_language_model_from_settings(cx: &mut App) {
170    let settings = AssistantSettings::get_global(cx);
171
172    fn to_selected_model(selection: &LanguageModelSelection) -> language_model::SelectedModel {
173        language_model::SelectedModel {
174            provider: LanguageModelProviderId::from(selection.provider.0.clone()),
175            model: LanguageModelId::from(selection.model.clone()),
176        }
177    }
178
179    let default = to_selected_model(&settings.default_model);
180    let inline_assistant = settings
181        .inline_assistant_model
182        .as_ref()
183        .map(to_selected_model);
184    let commit_message = settings
185        .commit_message_model
186        .as_ref()
187        .map(to_selected_model);
188    let thread_summary = settings
189        .thread_summary_model
190        .as_ref()
191        .map(to_selected_model);
192    let inline_alternatives = settings
193        .inline_alternatives
194        .iter()
195        .map(to_selected_model)
196        .collect::<Vec<_>>();
197
198    LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
199        registry.select_default_model(Some(&default), cx);
200        registry.select_inline_assistant_model(inline_assistant.as_ref(), cx);
201        registry.select_commit_message_model(commit_message.as_ref(), cx);
202        registry.select_thread_summary_model(thread_summary.as_ref(), cx);
203        registry.select_inline_alternative_models(inline_alternatives, cx);
204    });
205}
206
207fn register_slash_commands(cx: &mut App) {
208    let slash_command_registry = SlashCommandRegistry::global(cx);
209
210    slash_command_registry.register_command(assistant_slash_commands::FileSlashCommand, true);
211    slash_command_registry.register_command(assistant_slash_commands::DeltaSlashCommand, true);
212    slash_command_registry.register_command(assistant_slash_commands::OutlineSlashCommand, true);
213    slash_command_registry.register_command(assistant_slash_commands::TabSlashCommand, true);
214    slash_command_registry
215        .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
216    slash_command_registry.register_command(assistant_slash_commands::PromptSlashCommand, true);
217    slash_command_registry.register_command(assistant_slash_commands::SelectionCommand, true);
218    slash_command_registry.register_command(assistant_slash_commands::DefaultSlashCommand, false);
219    slash_command_registry.register_command(assistant_slash_commands::TerminalSlashCommand, true);
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}