agent_ui.rs

  1mod active_thread;
  2mod agent_configuration;
  3mod agent_diff;
  4mod agent_model_selector;
  5mod agent_panel;
  6mod buffer_codegen;
  7mod context_picker;
  8mod context_server_configuration;
  9mod context_strip;
 10mod debug;
 11mod inline_assistant;
 12mod inline_prompt_editor;
 13mod language_model_selector;
 14mod max_mode_tooltip;
 15mod message_editor;
 16mod profile_selector;
 17mod slash_command;
 18mod slash_command_picker;
 19mod slash_command_settings;
 20mod terminal_codegen;
 21mod terminal_inline_assistant;
 22mod text_thread_editor;
 23mod thread_history;
 24mod tool_compatibility;
 25mod ui;
 26
 27use std::sync::Arc;
 28
 29use agent::{Thread, ThreadId};
 30use agent_settings::{AgentProfileId, AgentSettings, LanguageModelSelection};
 31use assistant_slash_command::SlashCommandRegistry;
 32use client::Client;
 33use feature_flags::FeatureFlagAppExt as _;
 34use fs::Fs;
 35use gpui::{Action, App, Entity, actions};
 36use language::LanguageRegistry;
 37use language_model::{
 38    ConfiguredModel, LanguageModel, LanguageModelId, LanguageModelProviderId, LanguageModelRegistry,
 39};
 40use prompt_store::PromptBuilder;
 41use schemars::JsonSchema;
 42use serde::Deserialize;
 43use settings::{Settings as _, SettingsStore};
 44
 45pub use crate::active_thread::ActiveThread;
 46use crate::agent_configuration::{ConfigureContextServerModal, ManageProfilesModal};
 47pub use crate::agent_panel::{AgentPanel, ConcreteAssistantPanelDelegate};
 48pub use crate::inline_assistant::InlineAssistant;
 49use crate::slash_command_settings::SlashCommandSettings;
 50pub use agent_diff::{AgentDiffPane, AgentDiffToolbar};
 51pub use text_thread_editor::AgentPanelDelegate;
 52pub use ui::preview::{all_agent_previews, get_agent_preview};
 53
 54actions!(
 55    agent,
 56    [
 57        NewTextThread,
 58        ToggleContextPicker,
 59        ToggleNavigationMenu,
 60        ToggleOptionsMenu,
 61        DeleteRecentlyOpenThread,
 62        ToggleProfileSelector,
 63        RemoveAllContext,
 64        ExpandMessageEditor,
 65        OpenHistory,
 66        AddContextServer,
 67        RemoveSelectedThread,
 68        Chat,
 69        ChatWithFollow,
 70        CycleNextInlineAssist,
 71        CyclePreviousInlineAssist,
 72        FocusUp,
 73        FocusDown,
 74        FocusLeft,
 75        FocusRight,
 76        RemoveFocusedContext,
 77        AcceptSuggestedContext,
 78        OpenActiveThreadAsMarkdown,
 79        OpenAgentDiff,
 80        Keep,
 81        Reject,
 82        RejectAll,
 83        KeepAll,
 84        Follow,
 85        ResetTrialUpsell,
 86        ResetTrialEndUpsell,
 87        ContinueThread,
 88        ContinueWithBurnMode,
 89        ToggleBurnMode,
 90    ]
 91);
 92
 93#[derive(Default, Clone, PartialEq, Deserialize, JsonSchema, Action)]
 94#[action(namespace = agent)]
 95pub struct NewThread {
 96    #[serde(default)]
 97    from_thread_id: Option<ThreadId>,
 98}
 99
100#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
101#[action(namespace = agent)]
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
115#[derive(Clone)]
116pub(crate) enum ModelUsageContext {
117    Thread(Entity<Thread>),
118    InlineAssistant,
119}
120
121impl ModelUsageContext {
122    pub fn configured_model(&self, cx: &App) -> Option<ConfiguredModel> {
123        match self {
124            Self::Thread(thread) => thread.read(cx).configured_model(),
125            Self::InlineAssistant => {
126                LanguageModelRegistry::read_global(cx).inline_assistant_model()
127            }
128        }
129    }
130
131    pub fn language_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {
132        self.configured_model(cx)
133            .map(|configured_model| configured_model.model)
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::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    agent::init(cx);
158    agent_panel::init(cx);
159    context_server_configuration::init(language_registry.clone(), fs.clone(), 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(move |workspace, window, cx| {
176        ConfigureContextServerModal::register(workspace, language_registry.clone(), window, cx)
177    })
178    .detach();
179    cx.observe_new(ManageProfilesModal::register).detach();
180}
181
182fn init_language_model_settings(cx: &mut App) {
183    update_active_language_model_from_settings(cx);
184
185    cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
186        .detach();
187    cx.subscribe(
188        &LanguageModelRegistry::global(cx),
189        |_, event: &language_model::Event, cx| match event {
190            language_model::Event::ProviderStateChanged
191            | language_model::Event::AddedProvider(_)
192            | language_model::Event::RemovedProvider(_) => {
193                update_active_language_model_from_settings(cx);
194            }
195            _ => {}
196        },
197    )
198    .detach();
199}
200
201fn update_active_language_model_from_settings(cx: &mut App) {
202    let settings = AgentSettings::get_global(cx);
203
204    fn to_selected_model(selection: &LanguageModelSelection) -> language_model::SelectedModel {
205        language_model::SelectedModel {
206            provider: LanguageModelProviderId::from(selection.provider.0.clone()),
207            model: LanguageModelId::from(selection.model.clone()),
208        }
209    }
210
211    let default = to_selected_model(&settings.default_model);
212    let inline_assistant = settings
213        .inline_assistant_model
214        .as_ref()
215        .map(to_selected_model);
216    let commit_message = settings
217        .commit_message_model
218        .as_ref()
219        .map(to_selected_model);
220    let thread_summary = settings
221        .thread_summary_model
222        .as_ref()
223        .map(to_selected_model);
224    let inline_alternatives = settings
225        .inline_alternatives
226        .iter()
227        .map(to_selected_model)
228        .collect::<Vec<_>>();
229
230    LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
231        registry.select_default_model(Some(&default), cx);
232        registry.select_inline_assistant_model(inline_assistant.as_ref(), cx);
233        registry.select_commit_message_model(commit_message.as_ref(), cx);
234        registry.select_thread_summary_model(thread_summary.as_ref(), cx);
235        registry.select_inline_alternative_models(inline_alternatives, cx);
236    });
237}
238
239fn register_slash_commands(cx: &mut App) {
240    let slash_command_registry = SlashCommandRegistry::global(cx);
241
242    slash_command_registry.register_command(assistant_slash_commands::FileSlashCommand, true);
243    slash_command_registry.register_command(assistant_slash_commands::DeltaSlashCommand, true);
244    slash_command_registry.register_command(assistant_slash_commands::OutlineSlashCommand, true);
245    slash_command_registry.register_command(assistant_slash_commands::TabSlashCommand, true);
246    slash_command_registry
247        .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
248    slash_command_registry.register_command(assistant_slash_commands::PromptSlashCommand, true);
249    slash_command_registry.register_command(assistant_slash_commands::SelectionCommand, true);
250    slash_command_registry.register_command(assistant_slash_commands::DefaultSlashCommand, false);
251    slash_command_registry.register_command(assistant_slash_commands::NowSlashCommand, false);
252    slash_command_registry
253        .register_command(assistant_slash_commands::DiagnosticsSlashCommand, true);
254    slash_command_registry.register_command(assistant_slash_commands::FetchSlashCommand, true);
255
256    cx.observe_flag::<assistant_slash_commands::StreamingExampleSlashCommandFeatureFlag, _>({
257        let slash_command_registry = slash_command_registry.clone();
258        move |is_enabled, _cx| {
259            if is_enabled {
260                slash_command_registry.register_command(
261                    assistant_slash_commands::StreamingExampleSlashCommand,
262                    false,
263                );
264            }
265        }
266    })
267    .detach();
268
269    update_slash_commands_from_settings(cx);
270    cx.observe_global::<SettingsStore>(update_slash_commands_from_settings)
271        .detach();
272}
273
274fn update_slash_commands_from_settings(cx: &mut App) {
275    let slash_command_registry = SlashCommandRegistry::global(cx);
276    let settings = SlashCommandSettings::get_global(cx);
277
278    if settings.docs.enabled {
279        slash_command_registry.register_command(assistant_slash_commands::DocsSlashCommand, true);
280    } else {
281        slash_command_registry.unregister_command(assistant_slash_commands::DocsSlashCommand);
282    }
283
284    if settings.cargo_workspace.enabled {
285        slash_command_registry
286            .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
287    } else {
288        slash_command_registry
289            .unregister_command(assistant_slash_commands::CargoWorkspaceSlashCommand);
290    }
291}