agent_ui.rs

  1mod active_thread;
  2mod agent_configuration;
  3mod agent_diff;
  4mod agent_model_selector;
  5mod agent_panel;
  6mod buffer_codegen;
  7mod burn_mode_tooltip;
  8mod context_picker;
  9mod context_server_configuration;
 10mod context_strip;
 11mod debug;
 12mod inline_assistant;
 13mod inline_prompt_editor;
 14mod language_model_selector;
 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        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)]
 95#[serde(deny_unknown_fields)]
 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)]
103#[serde(deny_unknown_fields)]
104pub struct ManageProfiles {
105    #[serde(default)]
106    pub customize_tools: Option<AgentProfileId>,
107}
108
109impl ManageProfiles {
110    pub fn customize_tools(profile_id: AgentProfileId) -> Self {
111        Self {
112            customize_tools: Some(profile_id),
113        }
114    }
115}
116
117#[derive(Clone)]
118pub(crate) enum ModelUsageContext {
119    Thread(Entity<Thread>),
120    InlineAssistant,
121}
122
123impl ModelUsageContext {
124    pub fn configured_model(&self, cx: &App) -> Option<ConfiguredModel> {
125        match self {
126            Self::Thread(thread) => thread.read(cx).configured_model(),
127            Self::InlineAssistant => {
128                LanguageModelRegistry::read_global(cx).inline_assistant_model()
129            }
130        }
131    }
132
133    pub fn language_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {
134        self.configured_model(cx)
135            .map(|configured_model| configured_model.model)
136    }
137}
138
139/// Initializes the `agent` crate.
140pub fn init(
141    fs: Arc<dyn Fs>,
142    client: Arc<Client>,
143    prompt_builder: Arc<PromptBuilder>,
144    language_registry: Arc<LanguageRegistry>,
145    is_eval: bool,
146    cx: &mut App,
147) {
148    AgentSettings::register(cx);
149    SlashCommandSettings::register(cx);
150
151    assistant_context::init(client.clone(), cx);
152    rules_library::init(cx);
153    if !is_eval {
154        // Initializing the language model from the user settings messes with the eval, so we only initialize them when
155        // we're not running inside of the eval.
156        init_language_model_settings(cx);
157    }
158    assistant_slash_command::init(cx);
159    agent::init(cx);
160    agent_panel::init(cx);
161    context_server_configuration::init(language_registry.clone(), fs.clone(), cx);
162    TextThreadEditor::init(cx);
163
164    register_slash_commands(cx);
165    inline_assistant::init(
166        fs.clone(),
167        prompt_builder.clone(),
168        client.telemetry().clone(),
169        cx,
170    );
171    terminal_inline_assistant::init(
172        fs.clone(),
173        prompt_builder.clone(),
174        client.telemetry().clone(),
175        cx,
176    );
177    indexed_docs::init(cx);
178    cx.observe_new(move |workspace, window, cx| {
179        ConfigureContextServerModal::register(workspace, language_registry.clone(), window, cx)
180    })
181    .detach();
182    cx.observe_new(ManageProfilesModal::register).detach();
183}
184
185fn init_language_model_settings(cx: &mut App) {
186    update_active_language_model_from_settings(cx);
187
188    cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
189        .detach();
190    cx.subscribe(
191        &LanguageModelRegistry::global(cx),
192        |_, event: &language_model::Event, cx| match event {
193            language_model::Event::ProviderStateChanged
194            | language_model::Event::AddedProvider(_)
195            | language_model::Event::RemovedProvider(_) => {
196                update_active_language_model_from_settings(cx);
197            }
198            _ => {}
199        },
200    )
201    .detach();
202}
203
204fn update_active_language_model_from_settings(cx: &mut App) {
205    let settings = AgentSettings::get_global(cx);
206
207    fn to_selected_model(selection: &LanguageModelSelection) -> language_model::SelectedModel {
208        language_model::SelectedModel {
209            provider: LanguageModelProviderId::from(selection.provider.0.clone()),
210            model: LanguageModelId::from(selection.model.clone()),
211        }
212    }
213
214    let default = settings.default_model.as_ref().map(to_selected_model);
215    let inline_assistant = settings
216        .inline_assistant_model
217        .as_ref()
218        .map(to_selected_model);
219    let commit_message = settings
220        .commit_message_model
221        .as_ref()
222        .map(to_selected_model);
223    let thread_summary = settings
224        .thread_summary_model
225        .as_ref()
226        .map(to_selected_model);
227    let inline_alternatives = settings
228        .inline_alternatives
229        .iter()
230        .map(to_selected_model)
231        .collect::<Vec<_>>();
232
233    LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
234        registry.select_default_model(default.as_ref(), cx);
235        registry.select_inline_assistant_model(inline_assistant.as_ref(), cx);
236        registry.select_commit_message_model(commit_message.as_ref(), cx);
237        registry.select_thread_summary_model(thread_summary.as_ref(), cx);
238        registry.select_inline_alternative_models(inline_alternatives, cx);
239    });
240}
241
242fn register_slash_commands(cx: &mut App) {
243    let slash_command_registry = SlashCommandRegistry::global(cx);
244
245    slash_command_registry.register_command(assistant_slash_commands::FileSlashCommand, true);
246    slash_command_registry.register_command(assistant_slash_commands::DeltaSlashCommand, true);
247    slash_command_registry.register_command(assistant_slash_commands::OutlineSlashCommand, true);
248    slash_command_registry.register_command(assistant_slash_commands::TabSlashCommand, true);
249    slash_command_registry
250        .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
251    slash_command_registry.register_command(assistant_slash_commands::PromptSlashCommand, true);
252    slash_command_registry.register_command(assistant_slash_commands::SelectionCommand, true);
253    slash_command_registry.register_command(assistant_slash_commands::DefaultSlashCommand, false);
254    slash_command_registry.register_command(assistant_slash_commands::NowSlashCommand, false);
255    slash_command_registry
256        .register_command(assistant_slash_commands::DiagnosticsSlashCommand, true);
257    slash_command_registry.register_command(assistant_slash_commands::FetchSlashCommand, true);
258
259    cx.observe_flag::<assistant_slash_commands::StreamingExampleSlashCommandFeatureFlag, _>({
260        let slash_command_registry = slash_command_registry.clone();
261        move |is_enabled, _cx| {
262            if is_enabled {
263                slash_command_registry.register_command(
264                    assistant_slash_commands::StreamingExampleSlashCommand,
265                    false,
266                );
267            }
268        }
269    })
270    .detach();
271
272    update_slash_commands_from_settings(cx);
273    cx.observe_global::<SettingsStore>(update_slash_commands_from_settings)
274        .detach();
275}
276
277fn update_slash_commands_from_settings(cx: &mut App) {
278    let slash_command_registry = SlashCommandRegistry::global(cx);
279    let settings = SlashCommandSettings::get_global(cx);
280
281    if settings.docs.enabled {
282        slash_command_registry.register_command(assistant_slash_commands::DocsSlashCommand, true);
283    } else {
284        slash_command_registry.unregister_command(assistant_slash_commands::DocsSlashCommand);
285    }
286
287    if settings.cargo_workspace.enabled {
288        slash_command_registry
289            .register_command(assistant_slash_commands::CargoWorkspaceSlashCommand, true);
290    } else {
291        slash_command_registry
292            .unregister_command(assistant_slash_commands::CargoWorkspaceSlashCommand);
293    }
294}