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, TextThreadEditor};
 52pub use ui::preview::{all_agent_previews, get_agent_preview};
 53
 54actions!(
 55    agent,
 56    [
 57        NewTextThread,
 58        NewGeminiThread,
 59        ToggleContextPicker,
 60        ToggleNavigationMenu,
 61        ToggleOptionsMenu,
 62        DeleteRecentlyOpenThread,
 63        ToggleProfileSelector,
 64        RemoveAllContext,
 65        ExpandMessageEditor,
 66        OpenHistory,
 67        AddContextServer,
 68        RemoveSelectedThread,
 69        Chat,
 70        ChatWithFollow,
 71        CycleNextInlineAssist,
 72        CyclePreviousInlineAssist,
 73        FocusUp,
 74        FocusDown,
 75        FocusLeft,
 76        FocusRight,
 77        RemoveFocusedContext,
 78        AcceptSuggestedContext,
 79        OpenActiveThreadAsMarkdown,
 80        OpenAgentDiff,
 81        Keep,
 82        Reject,
 83        RejectAll,
 84        KeepAll,
 85        Follow,
 86        ResetTrialUpsell,
 87        ResetTrialEndUpsell,
 88        ContinueThread,
 89        ContinueWithBurnMode,
 90        ToggleBurnMode,
 91    ]
 92);
 93
 94#[derive(Default, Clone, PartialEq, Deserialize, JsonSchema, Action)]
 95#[action(namespace = agent)]
 96pub struct NewThread {
 97    #[serde(default)]
 98    from_thread_id: Option<ThreadId>,
 99}
100
101#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
102#[action(namespace = agent)]
103pub struct ManageProfiles {
104    #[serde(default)]
105    pub customize_tools: Option<AgentProfileId>,
106}
107
108impl ManageProfiles {
109    pub fn customize_tools(profile_id: AgentProfileId) -> Self {
110        Self {
111            customize_tools: Some(profile_id),
112        }
113    }
114}
115
116#[derive(Clone)]
117pub(crate) enum ModelUsageContext {
118    Thread(Entity<Thread>),
119    InlineAssistant,
120}
121
122impl ModelUsageContext {
123    pub fn configured_model(&self, cx: &App) -> Option<ConfiguredModel> {
124        match self {
125            Self::Thread(thread) => thread.read(cx).configured_model(),
126            Self::InlineAssistant => {
127                LanguageModelRegistry::read_global(cx).inline_assistant_model()
128            }
129        }
130    }
131
132    pub fn language_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {
133        self.configured_model(cx)
134            .map(|configured_model| configured_model.model)
135    }
136}
137
138/// Initializes the `agent` crate.
139pub fn init(
140    fs: Arc<dyn Fs>,
141    client: Arc<Client>,
142    prompt_builder: Arc<PromptBuilder>,
143    language_registry: Arc<LanguageRegistry>,
144    is_eval: bool,
145    cx: &mut App,
146) {
147    AgentSettings::register(cx);
148    SlashCommandSettings::register(cx);
149
150    assistant_context::init(client.clone(), cx);
151    rules_library::init(cx);
152    if !is_eval {
153        // Initializing the language model from the user settings messes with the eval, so we only initialize them when
154        // we're not running inside of the eval.
155        init_language_model_settings(cx);
156    }
157    assistant_slash_command::init(cx);
158    agent::init(cx);
159    agent_panel::init(cx);
160    context_server_configuration::init(language_registry.clone(), fs.clone(), cx);
161    TextThreadEditor::init(cx);
162
163    register_slash_commands(cx);
164    inline_assistant::init(
165        fs.clone(),
166        prompt_builder.clone(),
167        client.telemetry().clone(),
168        cx,
169    );
170    terminal_inline_assistant::init(
171        fs.clone(),
172        prompt_builder.clone(),
173        client.telemetry().clone(),
174        cx,
175    );
176    indexed_docs::init(cx);
177    cx.observe_new(move |workspace, window, cx| {
178        ConfigureContextServerModal::register(workspace, language_registry.clone(), window, cx)
179    })
180    .detach();
181    cx.observe_new(ManageProfilesModal::register).detach();
182}
183
184fn init_language_model_settings(cx: &mut App) {
185    update_active_language_model_from_settings(cx);
186
187    cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
188        .detach();
189    cx.subscribe(
190        &LanguageModelRegistry::global(cx),
191        |_, event: &language_model::Event, cx| match event {
192            language_model::Event::ProviderStateChanged
193            | language_model::Event::AddedProvider(_)
194            | language_model::Event::RemovedProvider(_) => {
195                update_active_language_model_from_settings(cx);
196            }
197            _ => {}
198        },
199    )
200    .detach();
201}
202
203fn update_active_language_model_from_settings(cx: &mut App) {
204    let settings = AgentSettings::get_global(cx);
205
206    fn to_selected_model(selection: &LanguageModelSelection) -> language_model::SelectedModel {
207        language_model::SelectedModel {
208            provider: LanguageModelProviderId::from(selection.provider.0.clone()),
209            model: LanguageModelId::from(selection.model.clone()),
210        }
211    }
212
213    let default = to_selected_model(&settings.default_model);
214    let inline_assistant = settings
215        .inline_assistant_model
216        .as_ref()
217        .map(to_selected_model);
218    let commit_message = settings
219        .commit_message_model
220        .as_ref()
221        .map(to_selected_model);
222    let thread_summary = settings
223        .thread_summary_model
224        .as_ref()
225        .map(to_selected_model);
226    let inline_alternatives = settings
227        .inline_alternatives
228        .iter()
229        .map(to_selected_model)
230        .collect::<Vec<_>>();
231
232    LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
233        registry.select_default_model(Some(&default), cx);
234        registry.select_inline_assistant_model(inline_assistant.as_ref(), cx);
235        registry.select_commit_message_model(commit_message.as_ref(), cx);
236        registry.select_thread_summary_model(thread_summary.as_ref(), cx);
237        registry.select_inline_alternative_models(inline_alternatives, cx);
238    });
239}
240
241fn register_slash_commands(cx: &mut App) {
242    let slash_command_registry = SlashCommandRegistry::global(cx);
243
244    slash_command_registry.register_command(assistant_slash_commands::FileSlashCommand, true);
245    slash_command_registry.register_command(assistant_slash_commands::DeltaSlashCommand, true);
246    slash_command_registry.register_command(assistant_slash_commands::OutlineSlashCommand, true);
247    slash_command_registry.register_command(assistant_slash_commands::TabSlashCommand, true);
248    slash_command_registry
249        .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
250    slash_command_registry.register_command(assistant_slash_commands::PromptSlashCommand, true);
251    slash_command_registry.register_command(assistant_slash_commands::SelectionCommand, true);
252    slash_command_registry.register_command(assistant_slash_commands::DefaultSlashCommand, false);
253    slash_command_registry.register_command(assistant_slash_commands::NowSlashCommand, false);
254    slash_command_registry
255        .register_command(assistant_slash_commands::DiagnosticsSlashCommand, true);
256    slash_command_registry.register_command(assistant_slash_commands::FetchSlashCommand, true);
257
258    cx.observe_flag::<assistant_slash_commands::StreamingExampleSlashCommandFeatureFlag, _>({
259        let slash_command_registry = slash_command_registry.clone();
260        move |is_enabled, _cx| {
261            if is_enabled {
262                slash_command_registry.register_command(
263                    assistant_slash_commands::StreamingExampleSlashCommand,
264                    false,
265                );
266            }
267        }
268    })
269    .detach();
270
271    update_slash_commands_from_settings(cx);
272    cx.observe_global::<SettingsStore>(update_slash_commands_from_settings)
273        .detach();
274}
275
276fn update_slash_commands_from_settings(cx: &mut App) {
277    let slash_command_registry = SlashCommandRegistry::global(cx);
278    let settings = SlashCommandSettings::get_global(cx);
279
280    if settings.docs.enabled {
281        slash_command_registry.register_command(assistant_slash_commands::DocsSlashCommand, true);
282    } else {
283        slash_command_registry.unregister_command(assistant_slash_commands::DocsSlashCommand);
284    }
285
286    if settings.cargo_workspace.enabled {
287        slash_command_registry
288            .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
289    } else {
290        slash_command_registry
291            .unregister_command(assistant_slash_commands::CargoWorkspaceSlashCommand);
292    }
293}