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