assistant.rs

  1#![cfg_attr(target_os = "windows", allow(unused, dead_code))]
  2
  3pub mod assistant_panel;
  4pub mod assistant_settings;
  5mod context;
  6pub mod context_store;
  7mod inline_assistant;
  8mod patch;
  9mod prompt_library;
 10mod prompts;
 11mod slash_command;
 12pub(crate) mod slash_command_picker;
 13pub mod slash_command_settings;
 14mod slash_command_working_set;
 15mod streaming_diff;
 16mod terminal_inline_assistant;
 17mod tool_working_set;
 18mod tools;
 19
 20use crate::slash_command::project_command::ProjectSlashCommandFeatureFlag;
 21pub use crate::slash_command_working_set::{SlashCommandId, SlashCommandWorkingSet};
 22pub use crate::tool_working_set::{ToolId, ToolWorkingSet};
 23pub use assistant_panel::{AssistantPanel, AssistantPanelEvent};
 24use assistant_settings::AssistantSettings;
 25use assistant_slash_command::SlashCommandRegistry;
 26use assistant_tool::ToolRegistry;
 27use client::{proto, Client};
 28use command_palette_hooks::CommandPaletteFilter;
 29pub use context::*;
 30pub use context_store::*;
 31use feature_flags::FeatureFlagAppExt;
 32use fs::Fs;
 33use gpui::impl_actions;
 34use gpui::{actions, AppContext, Global, SharedString, UpdateGlobal};
 35pub(crate) use inline_assistant::*;
 36use language_model::{
 37    LanguageModelId, LanguageModelProviderId, LanguageModelRegistry, LanguageModelResponseMessage,
 38};
 39pub use patch::*;
 40pub use prompts::PromptBuilder;
 41use prompts::PromptLoadingParams;
 42use semantic_index::{CloudEmbeddingProvider, SemanticDb};
 43use serde::{Deserialize, Serialize};
 44use settings::{update_settings_file, Settings, SettingsStore};
 45use slash_command::search_command::SearchSlashCommandFeatureFlag;
 46use slash_command::{
 47    auto_command, cargo_workspace_command, default_command, delta_command, diagnostics_command,
 48    docs_command, fetch_command, file_command, now_command, project_command, prompt_command,
 49    search_command, selection_command, symbols_command, tab_command, terminal_command,
 50};
 51use std::path::PathBuf;
 52use std::sync::Arc;
 53pub(crate) use streaming_diff::*;
 54use util::ResultExt;
 55
 56use crate::slash_command::streaming_example_command;
 57use crate::slash_command_settings::SlashCommandSettings;
 58
 59actions!(
 60    assistant,
 61    [
 62        Assist,
 63        Edit,
 64        Split,
 65        CopyCode,
 66        CycleMessageRole,
 67        QuoteSelection,
 68        InsertIntoEditor,
 69        ToggleFocus,
 70        InsertActivePrompt,
 71        DeployHistory,
 72        DeployPromptLibrary,
 73        ConfirmCommand,
 74        NewContext,
 75        ToggleModelSelector,
 76        CycleNextInlineAssist,
 77        CyclePreviousInlineAssist
 78    ]
 79);
 80
 81#[derive(PartialEq, Clone, Deserialize)]
 82pub enum InsertDraggedFiles {
 83    ProjectPaths(Vec<PathBuf>),
 84    ExternalFiles(Vec<PathBuf>),
 85}
 86
 87impl_actions!(assistant, [InsertDraggedFiles]);
 88
 89const DEFAULT_CONTEXT_LINES: usize = 50;
 90
 91#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
 92pub struct MessageId(clock::Lamport);
 93
 94impl MessageId {
 95    pub fn as_u64(self) -> u64 {
 96        self.0.as_u64()
 97    }
 98}
 99
100#[derive(Deserialize, Debug)]
101pub struct LanguageModelUsage {
102    pub prompt_tokens: u32,
103    pub completion_tokens: u32,
104    pub total_tokens: u32,
105}
106
107#[derive(Deserialize, Debug)]
108pub struct LanguageModelChoiceDelta {
109    pub index: u32,
110    pub delta: LanguageModelResponseMessage,
111    pub finish_reason: Option<String>,
112}
113
114#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
115pub enum MessageStatus {
116    Pending,
117    Done,
118    Error(SharedString),
119    Canceled,
120}
121
122impl MessageStatus {
123    pub fn from_proto(status: proto::ContextMessageStatus) -> MessageStatus {
124        match status.variant {
125            Some(proto::context_message_status::Variant::Pending(_)) => MessageStatus::Pending,
126            Some(proto::context_message_status::Variant::Done(_)) => MessageStatus::Done,
127            Some(proto::context_message_status::Variant::Error(error)) => {
128                MessageStatus::Error(error.message.into())
129            }
130            Some(proto::context_message_status::Variant::Canceled(_)) => MessageStatus::Canceled,
131            None => MessageStatus::Pending,
132        }
133    }
134
135    pub fn to_proto(&self) -> proto::ContextMessageStatus {
136        match self {
137            MessageStatus::Pending => proto::ContextMessageStatus {
138                variant: Some(proto::context_message_status::Variant::Pending(
139                    proto::context_message_status::Pending {},
140                )),
141            },
142            MessageStatus::Done => proto::ContextMessageStatus {
143                variant: Some(proto::context_message_status::Variant::Done(
144                    proto::context_message_status::Done {},
145                )),
146            },
147            MessageStatus::Error(message) => proto::ContextMessageStatus {
148                variant: Some(proto::context_message_status::Variant::Error(
149                    proto::context_message_status::Error {
150                        message: message.to_string(),
151                    },
152                )),
153            },
154            MessageStatus::Canceled => proto::ContextMessageStatus {
155                variant: Some(proto::context_message_status::Variant::Canceled(
156                    proto::context_message_status::Canceled {},
157                )),
158            },
159        }
160    }
161}
162
163/// The state pertaining to the Assistant.
164#[derive(Default)]
165struct Assistant {
166    /// Whether the Assistant is enabled.
167    enabled: bool,
168}
169
170impl Global for Assistant {}
171
172impl Assistant {
173    const NAMESPACE: &'static str = "assistant";
174
175    fn set_enabled(&mut self, enabled: bool, cx: &mut AppContext) {
176        if self.enabled == enabled {
177            return;
178        }
179
180        self.enabled = enabled;
181
182        if !enabled {
183            CommandPaletteFilter::update_global(cx, |filter, _cx| {
184                filter.hide_namespace(Self::NAMESPACE);
185            });
186
187            return;
188        }
189
190        CommandPaletteFilter::update_global(cx, |filter, _cx| {
191            filter.show_namespace(Self::NAMESPACE);
192        });
193    }
194}
195
196pub fn init(
197    fs: Arc<dyn Fs>,
198    client: Arc<Client>,
199    stdout_is_a_pty: bool,
200    cx: &mut AppContext,
201) -> Arc<PromptBuilder> {
202    cx.set_global(Assistant::default());
203    AssistantSettings::register(cx);
204    SlashCommandSettings::register(cx);
205
206    // TODO: remove this when 0.148.0 is released.
207    if AssistantSettings::get_global(cx).using_outdated_settings_version {
208        update_settings_file::<AssistantSettings>(fs.clone(), cx, {
209            let fs = fs.clone();
210            |content, cx| {
211                content.update_file(fs, cx);
212            }
213        });
214    }
215
216    cx.spawn(|mut cx| {
217        let client = client.clone();
218        async move {
219            let is_search_slash_command_enabled = cx
220                .update(|cx| cx.wait_for_flag::<SearchSlashCommandFeatureFlag>())?
221                .await;
222            let is_project_slash_command_enabled = cx
223                .update(|cx| cx.wait_for_flag::<ProjectSlashCommandFeatureFlag>())?
224                .await;
225
226            if !is_search_slash_command_enabled && !is_project_slash_command_enabled {
227                return Ok(());
228            }
229
230            let embedding_provider = CloudEmbeddingProvider::new(client.clone());
231            let semantic_index = SemanticDb::new(
232                paths::embeddings_dir().join("semantic-index-db.0.mdb"),
233                Arc::new(embedding_provider),
234                &mut cx,
235            )
236            .await?;
237
238            cx.update(|cx| cx.set_global(semantic_index))
239        }
240    })
241    .detach();
242
243    context_store::init(&client.clone().into());
244    prompt_library::init(cx);
245    init_language_model_settings(cx);
246    assistant_slash_command::init(cx);
247    assistant_tool::init(cx);
248    assistant_panel::init(cx);
249    context_servers::init(cx);
250
251    let prompt_builder = prompts::PromptBuilder::new(Some(PromptLoadingParams {
252        fs: fs.clone(),
253        repo_path: stdout_is_a_pty
254            .then(|| std::env::current_dir().log_err())
255            .flatten(),
256        cx,
257    }))
258    .log_err()
259    .map(Arc::new)
260    .unwrap_or_else(|| Arc::new(prompts::PromptBuilder::new(None).unwrap()));
261    register_slash_commands(Some(prompt_builder.clone()), cx);
262    register_tools(cx);
263    inline_assistant::init(
264        fs.clone(),
265        prompt_builder.clone(),
266        client.telemetry().clone(),
267        cx,
268    );
269    terminal_inline_assistant::init(
270        fs.clone(),
271        prompt_builder.clone(),
272        client.telemetry().clone(),
273        cx,
274    );
275    indexed_docs::init(cx);
276
277    CommandPaletteFilter::update_global(cx, |filter, _cx| {
278        filter.hide_namespace(Assistant::NAMESPACE);
279    });
280    Assistant::update_global(cx, |assistant, cx| {
281        let settings = AssistantSettings::get_global(cx);
282
283        assistant.set_enabled(settings.enabled, cx);
284    });
285    cx.observe_global::<SettingsStore>(|cx| {
286        Assistant::update_global(cx, |assistant, cx| {
287            let settings = AssistantSettings::get_global(cx);
288            assistant.set_enabled(settings.enabled, cx);
289        });
290    })
291    .detach();
292
293    prompt_builder
294}
295
296fn init_language_model_settings(cx: &mut AppContext) {
297    update_active_language_model_from_settings(cx);
298
299    cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
300        .detach();
301    cx.subscribe(
302        &LanguageModelRegistry::global(cx),
303        |_, event: &language_model::Event, cx| match event {
304            language_model::Event::ProviderStateChanged
305            | language_model::Event::AddedProvider(_)
306            | language_model::Event::RemovedProvider(_) => {
307                update_active_language_model_from_settings(cx);
308            }
309            _ => {}
310        },
311    )
312    .detach();
313}
314
315fn update_active_language_model_from_settings(cx: &mut AppContext) {
316    let settings = AssistantSettings::get_global(cx);
317    let provider_name = LanguageModelProviderId::from(settings.default_model.provider.clone());
318    let model_id = LanguageModelId::from(settings.default_model.model.clone());
319    let inline_alternatives = settings
320        .inline_alternatives
321        .iter()
322        .map(|alternative| {
323            (
324                LanguageModelProviderId::from(alternative.provider.clone()),
325                LanguageModelId::from(alternative.model.clone()),
326            )
327        })
328        .collect::<Vec<_>>();
329    LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
330        registry.select_active_model(&provider_name, &model_id, cx);
331        registry.select_inline_alternative_models(inline_alternatives, cx);
332    });
333}
334
335fn register_slash_commands(prompt_builder: Option<Arc<PromptBuilder>>, cx: &mut AppContext) {
336    let slash_command_registry = SlashCommandRegistry::global(cx);
337
338    slash_command_registry.register_command(file_command::FileSlashCommand, true);
339    slash_command_registry.register_command(delta_command::DeltaSlashCommand, true);
340    slash_command_registry.register_command(symbols_command::OutlineSlashCommand, true);
341    slash_command_registry.register_command(tab_command::TabSlashCommand, true);
342    slash_command_registry
343        .register_command(cargo_workspace_command::CargoWorkspaceSlashCommand, true);
344    slash_command_registry.register_command(prompt_command::PromptSlashCommand, true);
345    slash_command_registry.register_command(selection_command::SelectionCommand, true);
346    slash_command_registry.register_command(default_command::DefaultSlashCommand, false);
347    slash_command_registry.register_command(terminal_command::TerminalSlashCommand, true);
348    slash_command_registry.register_command(now_command::NowSlashCommand, false);
349    slash_command_registry.register_command(diagnostics_command::DiagnosticsSlashCommand, true);
350    slash_command_registry.register_command(fetch_command::FetchSlashCommand, false);
351    slash_command_registry.register_command(fetch_command::FetchSlashCommand, false);
352
353    if let Some(prompt_builder) = prompt_builder {
354        cx.observe_flag::<project_command::ProjectSlashCommandFeatureFlag, _>({
355            let slash_command_registry = slash_command_registry.clone();
356            move |is_enabled, _cx| {
357                if is_enabled {
358                    slash_command_registry.register_command(
359                        project_command::ProjectSlashCommand::new(prompt_builder.clone()),
360                        true,
361                    );
362                }
363            }
364        })
365        .detach();
366    }
367
368    cx.observe_flag::<auto_command::AutoSlashCommandFeatureFlag, _>({
369        let slash_command_registry = slash_command_registry.clone();
370        move |is_enabled, _cx| {
371            if is_enabled {
372                // [#auto-staff-ship] TODO remove this when /auto is no longer staff-shipped
373                slash_command_registry.register_command(auto_command::AutoCommand, true);
374            }
375        }
376    })
377    .detach();
378
379    cx.observe_flag::<streaming_example_command::StreamingExampleSlashCommandFeatureFlag, _>({
380        let slash_command_registry = slash_command_registry.clone();
381        move |is_enabled, _cx| {
382            if is_enabled {
383                slash_command_registry.register_command(
384                    streaming_example_command::StreamingExampleSlashCommand,
385                    false,
386                );
387            }
388        }
389    })
390    .detach();
391
392    update_slash_commands_from_settings(cx);
393    cx.observe_global::<SettingsStore>(update_slash_commands_from_settings)
394        .detach();
395
396    cx.observe_flag::<search_command::SearchSlashCommandFeatureFlag, _>({
397        let slash_command_registry = slash_command_registry.clone();
398        move |is_enabled, _cx| {
399            if is_enabled {
400                slash_command_registry.register_command(search_command::SearchSlashCommand, true);
401            }
402        }
403    })
404    .detach();
405}
406
407fn update_slash_commands_from_settings(cx: &mut AppContext) {
408    let slash_command_registry = SlashCommandRegistry::global(cx);
409    let settings = SlashCommandSettings::get_global(cx);
410
411    if settings.docs.enabled {
412        slash_command_registry.register_command(docs_command::DocsSlashCommand, true);
413    } else {
414        slash_command_registry.unregister_command(docs_command::DocsSlashCommand);
415    }
416
417    if settings.cargo_workspace.enabled {
418        slash_command_registry
419            .register_command(cargo_workspace_command::CargoWorkspaceSlashCommand, true);
420    } else {
421        slash_command_registry
422            .unregister_command(cargo_workspace_command::CargoWorkspaceSlashCommand);
423    }
424}
425
426fn register_tools(cx: &mut AppContext) {
427    let tool_registry = ToolRegistry::global(cx);
428    tool_registry.register_tool(tools::now_tool::NowTool);
429}
430
431pub fn humanize_token_count(count: usize) -> String {
432    match count {
433        0..=999 => count.to_string(),
434        1000..=9999 => {
435            let thousands = count / 1000;
436            let hundreds = (count % 1000 + 50) / 100;
437            if hundreds == 0 {
438                format!("{}k", thousands)
439            } else if hundreds == 10 {
440                format!("{}k", thousands + 1)
441            } else {
442                format!("{}.{}k", thousands, hundreds)
443            }
444        }
445        _ => format!("{}k", (count + 500) / 1000),
446    }
447}
448
449#[cfg(test)]
450#[ctor::ctor]
451fn init_logger() {
452    if std::env::var("RUST_LOG").is_ok() {
453        env_logger::init();
454    }
455}