mention_set.rs

   1use acp_thread::{MentionUri, selection_name};
   2use agent::{ThreadStore, outline};
   3use agent_client_protocol as acp;
   4use agent_servers::{AgentServer, AgentServerDelegate};
   5use anyhow::{Context as _, Result, anyhow};
   6use assistant_slash_commands::{codeblock_fence_for_path, collect_diagnostics_output};
   7use collections::{HashMap, HashSet};
   8use editor::{
   9    Anchor, Editor, EditorSnapshot, ExcerptId, FoldPlaceholder, ToOffset,
  10    display_map::{Crease, CreaseId, CreaseMetadata, FoldId},
  11    scroll::Autoscroll,
  12};
  13use futures::{AsyncReadExt as _, FutureExt as _, future::Shared};
  14use gpui::{
  15    AppContext, ClipboardEntry, Context, Empty, Entity, EntityId, Image, ImageFormat, Img,
  16    SharedString, Task, WeakEntity,
  17};
  18use http_client::{AsyncBody, HttpClientWithUrl};
  19use itertools::Either;
  20use language::Buffer;
  21use language_model::LanguageModelImage;
  22use multi_buffer::MultiBufferRow;
  23use postage::stream::Stream as _;
  24use project::{Project, ProjectItem, ProjectPath, Worktree};
  25use prompt_store::{PromptId, PromptStore};
  26use rope::Point;
  27use std::{
  28    cell::RefCell,
  29    ffi::OsStr,
  30    fmt::Write,
  31    ops::{Range, RangeInclusive},
  32    path::{Path, PathBuf},
  33    rc::Rc,
  34    sync::Arc,
  35};
  36use text::OffsetRangeExt;
  37use ui::{Disclosure, Toggleable, prelude::*};
  38use util::{ResultExt, debug_panic, rel_path::RelPath};
  39use workspace::{Workspace, notifications::NotifyResultExt as _};
  40
  41use crate::ui::MentionCrease;
  42
  43pub type MentionTask = Shared<Task<Result<Mention, String>>>;
  44
  45#[derive(Debug, Clone, Eq, PartialEq)]
  46pub enum Mention {
  47    Text {
  48        content: String,
  49        tracked_buffers: Vec<Entity<Buffer>>,
  50    },
  51    Image(MentionImage),
  52    Link,
  53}
  54
  55#[derive(Clone, Debug, Eq, PartialEq)]
  56pub struct MentionImage {
  57    pub data: SharedString,
  58    pub format: ImageFormat,
  59}
  60
  61pub struct MentionSet {
  62    project: WeakEntity<Project>,
  63    thread_store: Option<Entity<ThreadStore>>,
  64    prompt_store: Option<Entity<PromptStore>>,
  65    mentions: HashMap<CreaseId, (MentionUri, MentionTask)>,
  66}
  67
  68impl MentionSet {
  69    pub fn new(
  70        project: WeakEntity<Project>,
  71        thread_store: Option<Entity<ThreadStore>>,
  72        prompt_store: Option<Entity<PromptStore>>,
  73    ) -> Self {
  74        Self {
  75            project,
  76            thread_store,
  77            prompt_store,
  78            mentions: HashMap::default(),
  79        }
  80    }
  81
  82    pub fn contents(
  83        &self,
  84        full_mention_content: bool,
  85        cx: &mut App,
  86    ) -> Task<Result<HashMap<CreaseId, (MentionUri, Mention)>>> {
  87        let Some(project) = self.project.upgrade() else {
  88            return Task::ready(Err(anyhow!("Project not found")));
  89        };
  90        let mentions = self.mentions.clone();
  91        cx.spawn(async move |cx| {
  92            let mut contents = HashMap::default();
  93            for (crease_id, (mention_uri, task)) in mentions {
  94                let content = if full_mention_content
  95                    && let MentionUri::Directory { abs_path } = &mention_uri
  96                {
  97                    cx.update(|cx| full_mention_for_directory(&project, abs_path, cx))
  98                        .await?
  99                } else {
 100                    task.await.map_err(|e| anyhow!("{e}"))?
 101                };
 102
 103                contents.insert(crease_id, (mention_uri, content));
 104            }
 105            Ok(contents)
 106        })
 107    }
 108
 109    pub fn remove_invalid(&mut self, snapshot: &EditorSnapshot) {
 110        for (crease_id, crease) in snapshot.crease_snapshot.creases() {
 111            if !crease.range().start.is_valid(snapshot.buffer_snapshot()) {
 112                self.mentions.remove(&crease_id);
 113            }
 114        }
 115    }
 116
 117    pub fn insert_mention(&mut self, crease_id: CreaseId, uri: MentionUri, task: MentionTask) {
 118        self.mentions.insert(crease_id, (uri, task));
 119    }
 120
 121    /// Creates the appropriate confirmation task for a mention based on its URI type.
 122    /// This is used when pasting mention links to properly load their content.
 123    pub fn confirm_mention_for_uri(
 124        &mut self,
 125        mention_uri: MentionUri,
 126        supports_images: bool,
 127        http_client: Arc<HttpClientWithUrl>,
 128        cx: &mut Context<Self>,
 129    ) -> Task<Result<Mention>> {
 130        match mention_uri {
 131            MentionUri::Fetch { url } => self.confirm_mention_for_fetch(url, http_client, cx),
 132            MentionUri::Directory { .. } => Task::ready(Ok(Mention::Link)),
 133            MentionUri::Thread { id, .. } => self.confirm_mention_for_thread(id, cx),
 134            MentionUri::TextThread { .. } => {
 135                Task::ready(Err(anyhow!("Text thread mentions are no longer supported")))
 136            }
 137            MentionUri::File { abs_path } => {
 138                self.confirm_mention_for_file(abs_path, supports_images, cx)
 139            }
 140            MentionUri::Symbol {
 141                abs_path,
 142                line_range,
 143                ..
 144            } => self.confirm_mention_for_symbol(abs_path, line_range, cx),
 145            MentionUri::Rule { id, .. } => self.confirm_mention_for_rule(id, cx),
 146            MentionUri::Diagnostics {
 147                include_errors,
 148                include_warnings,
 149            } => self.confirm_mention_for_diagnostics(include_errors, include_warnings, cx),
 150            MentionUri::PastedImage
 151            | MentionUri::Selection { .. }
 152            | MentionUri::TerminalSelection { .. } => {
 153                Task::ready(Err(anyhow!("Unsupported mention URI type for paste")))
 154            }
 155        }
 156    }
 157
 158    pub fn remove_mention(&mut self, crease_id: &CreaseId) {
 159        self.mentions.remove(crease_id);
 160    }
 161
 162    pub fn creases(&self) -> HashSet<CreaseId> {
 163        self.mentions.keys().cloned().collect()
 164    }
 165
 166    pub fn mentions(&self) -> HashSet<MentionUri> {
 167        self.mentions.values().map(|(uri, _)| uri.clone()).collect()
 168    }
 169
 170    pub fn set_mentions(&mut self, mentions: HashMap<CreaseId, (MentionUri, MentionTask)>) {
 171        self.mentions = mentions;
 172    }
 173
 174    pub fn clear(&mut self) -> impl Iterator<Item = (CreaseId, (MentionUri, MentionTask))> {
 175        self.mentions.drain()
 176    }
 177
 178    #[cfg(test)]
 179    pub fn has_thread_store(&self) -> bool {
 180        self.thread_store.is_some()
 181    }
 182
 183    pub fn confirm_mention_completion(
 184        &mut self,
 185        crease_text: SharedString,
 186        start: text::Anchor,
 187        content_len: usize,
 188        mention_uri: MentionUri,
 189        supports_images: bool,
 190        editor: Entity<Editor>,
 191        workspace: &Entity<Workspace>,
 192        window: &mut Window,
 193        cx: &mut Context<Self>,
 194    ) -> Task<()> {
 195        let Some(project) = self.project.upgrade() else {
 196            return Task::ready(());
 197        };
 198
 199        let snapshot = editor.update(cx, |editor, cx| editor.snapshot(window, cx));
 200        let Some(start_anchor) = snapshot.buffer_snapshot().as_singleton_anchor(start) else {
 201            return Task::ready(());
 202        };
 203        let excerpt_id = start_anchor.excerpt_id;
 204        let end_anchor = snapshot.buffer_snapshot().anchor_before(
 205            start_anchor.to_offset(&snapshot.buffer_snapshot()) + content_len + 1usize,
 206        );
 207
 208        let crease = if let MentionUri::File { abs_path } = &mention_uri
 209            && let Some(extension) = abs_path.extension()
 210            && let Some(extension) = extension.to_str()
 211            && Img::extensions().contains(&extension)
 212            && !extension.contains("svg")
 213        {
 214            let Some(project_path) = project
 215                .read(cx)
 216                .project_path_for_absolute_path(&abs_path, cx)
 217            else {
 218                log::error!("project path not found");
 219                return Task::ready(());
 220            };
 221            let image_task = project.update(cx, |project, cx| project.open_image(project_path, cx));
 222            let image = cx
 223                .spawn(async move |_, cx| {
 224                    let image = image_task.await.map_err(|e| e.to_string())?;
 225                    let image = image.update(cx, |image, _| image.image.clone());
 226                    Ok(image)
 227                })
 228                .shared();
 229            insert_crease_for_mention(
 230                excerpt_id,
 231                start,
 232                content_len,
 233                mention_uri.name().into(),
 234                IconName::Image.path().into(),
 235                Some(image),
 236                editor.clone(),
 237                window,
 238                cx,
 239            )
 240        } else {
 241            insert_crease_for_mention(
 242                excerpt_id,
 243                start,
 244                content_len,
 245                crease_text,
 246                mention_uri.icon_path(cx),
 247                None,
 248                editor.clone(),
 249                window,
 250                cx,
 251            )
 252        };
 253        let Some((crease_id, tx)) = crease else {
 254            return Task::ready(());
 255        };
 256
 257        let task = match mention_uri.clone() {
 258            MentionUri::Fetch { url } => {
 259                self.confirm_mention_for_fetch(url, workspace.read(cx).client().http_client(), cx)
 260            }
 261            MentionUri::Directory { .. } => Task::ready(Ok(Mention::Link)),
 262            MentionUri::Thread { id, .. } => self.confirm_mention_for_thread(id, cx),
 263            MentionUri::TextThread { .. } => {
 264                Task::ready(Err(anyhow!("Text thread mentions are no longer supported")))
 265            }
 266            MentionUri::File { abs_path } => {
 267                self.confirm_mention_for_file(abs_path, supports_images, cx)
 268            }
 269            MentionUri::Symbol {
 270                abs_path,
 271                line_range,
 272                ..
 273            } => self.confirm_mention_for_symbol(abs_path, line_range, cx),
 274            MentionUri::Rule { id, .. } => self.confirm_mention_for_rule(id, cx),
 275            MentionUri::Diagnostics {
 276                include_errors,
 277                include_warnings,
 278            } => self.confirm_mention_for_diagnostics(include_errors, include_warnings, cx),
 279            MentionUri::PastedImage => {
 280                debug_panic!("pasted image URI should not be included in completions");
 281                Task::ready(Err(anyhow!(
 282                    "pasted imaged URI should not be included in completions"
 283                )))
 284            }
 285            MentionUri::Selection { .. } => {
 286                debug_panic!("unexpected selection URI");
 287                Task::ready(Err(anyhow!("unexpected selection URI")))
 288            }
 289            MentionUri::TerminalSelection { .. } => {
 290                debug_panic!("unexpected terminal URI");
 291                Task::ready(Err(anyhow!("unexpected terminal URI")))
 292            }
 293        };
 294        let task = cx
 295            .spawn(async move |_, _| task.await.map_err(|e| e.to_string()))
 296            .shared();
 297        self.mentions.insert(crease_id, (mention_uri, task.clone()));
 298
 299        // Notify the user if we failed to load the mentioned context
 300        cx.spawn_in(window, async move |this, cx| {
 301            let result = task.await.notify_async_err(cx);
 302            drop(tx);
 303            if result.is_none() {
 304                this.update(cx, |this, cx| {
 305                    editor.update(cx, |editor, cx| {
 306                        // Remove mention
 307                        editor.edit([(start_anchor..end_anchor, "")], cx);
 308                    });
 309                    this.mentions.remove(&crease_id);
 310                })
 311                .ok();
 312            }
 313        })
 314    }
 315
 316    pub fn confirm_mention_for_file(
 317        &self,
 318        abs_path: PathBuf,
 319        supports_images: bool,
 320        cx: &mut Context<Self>,
 321    ) -> Task<Result<Mention>> {
 322        let Some(project) = self.project.upgrade() else {
 323            return Task::ready(Err(anyhow!("project not found")));
 324        };
 325
 326        let Some(project_path) = project
 327            .read(cx)
 328            .project_path_for_absolute_path(&abs_path, cx)
 329        else {
 330            return Task::ready(Err(anyhow!("project path not found")));
 331        };
 332        let extension = abs_path
 333            .extension()
 334            .and_then(OsStr::to_str)
 335            .unwrap_or_default();
 336
 337        if Img::extensions().contains(&extension) && !extension.contains("svg") {
 338            if !supports_images {
 339                return Task::ready(Err(anyhow!("This model does not support images yet")));
 340            }
 341            let task = project.update(cx, |project, cx| project.open_image(project_path, cx));
 342            return cx.spawn(async move |_, cx| {
 343                let image = task.await?;
 344                let image = image.update(cx, |image, _| image.image.clone());
 345                let image = cx
 346                    .update(|cx| LanguageModelImage::from_image(image, cx))
 347                    .await;
 348                if let Some(image) = image {
 349                    Ok(Mention::Image(MentionImage {
 350                        data: image.source,
 351                        format: LanguageModelImage::FORMAT,
 352                    }))
 353                } else {
 354                    Err(anyhow!("Failed to convert image"))
 355                }
 356            });
 357        }
 358
 359        let buffer = project.update(cx, |project, cx| project.open_buffer(project_path, cx));
 360        cx.spawn(async move |_, cx| {
 361            let buffer = buffer.await?;
 362            let buffer_content = outline::get_buffer_content_or_outline(
 363                buffer.clone(),
 364                Some(&abs_path.to_string_lossy()),
 365                &cx,
 366            )
 367            .await?;
 368
 369            Ok(Mention::Text {
 370                content: buffer_content.text,
 371                tracked_buffers: vec![buffer],
 372            })
 373        })
 374    }
 375
 376    fn confirm_mention_for_fetch(
 377        &self,
 378        url: url::Url,
 379        http_client: Arc<HttpClientWithUrl>,
 380        cx: &mut Context<Self>,
 381    ) -> Task<Result<Mention>> {
 382        cx.background_executor().spawn(async move {
 383            let content = fetch_url_content(http_client, url.to_string()).await?;
 384            Ok(Mention::Text {
 385                content,
 386                tracked_buffers: Vec::new(),
 387            })
 388        })
 389    }
 390
 391    fn confirm_mention_for_symbol(
 392        &self,
 393        abs_path: PathBuf,
 394        line_range: RangeInclusive<u32>,
 395        cx: &mut Context<Self>,
 396    ) -> Task<Result<Mention>> {
 397        let Some(project) = self.project.upgrade() else {
 398            return Task::ready(Err(anyhow!("project not found")));
 399        };
 400        let Some(project_path) = project
 401            .read(cx)
 402            .project_path_for_absolute_path(&abs_path, cx)
 403        else {
 404            return Task::ready(Err(anyhow!("project path not found")));
 405        };
 406        let buffer = project.update(cx, |project, cx| project.open_buffer(project_path, cx));
 407        cx.spawn(async move |_, cx| {
 408            let buffer = buffer.await?;
 409            let mention = buffer.update(cx, |buffer, cx| {
 410                let start = Point::new(*line_range.start(), 0).min(buffer.max_point());
 411                let end = Point::new(*line_range.end() + 1, 0).min(buffer.max_point());
 412                let content = buffer.text_for_range(start..end).collect();
 413                Mention::Text {
 414                    content,
 415                    tracked_buffers: vec![cx.entity()],
 416                }
 417            });
 418            Ok(mention)
 419        })
 420    }
 421
 422    fn confirm_mention_for_rule(
 423        &mut self,
 424        id: PromptId,
 425        cx: &mut Context<Self>,
 426    ) -> Task<Result<Mention>> {
 427        let Some(prompt_store) = self.prompt_store.as_ref() else {
 428            return Task::ready(Err(anyhow!("Missing prompt store")));
 429        };
 430        let prompt = prompt_store.read(cx).load(id, cx);
 431        cx.spawn(async move |_, _| {
 432            let prompt = prompt.await?;
 433            Ok(Mention::Text {
 434                content: prompt,
 435                tracked_buffers: Vec::new(),
 436            })
 437        })
 438    }
 439
 440    pub fn confirm_mention_for_selection(
 441        &mut self,
 442        source_range: Range<text::Anchor>,
 443        selections: Vec<(Entity<Buffer>, Range<text::Anchor>, Range<usize>)>,
 444        editor: Entity<Editor>,
 445        window: &mut Window,
 446        cx: &mut Context<Self>,
 447    ) {
 448        let Some(project) = self.project.upgrade() else {
 449            return;
 450        };
 451
 452        let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
 453        let Some(start) = snapshot.as_singleton_anchor(source_range.start) else {
 454            return;
 455        };
 456
 457        let offset = start.to_offset(&snapshot);
 458
 459        for (buffer, selection_range, range_to_fold) in selections {
 460            let range = snapshot.anchor_after(offset + range_to_fold.start)
 461                ..snapshot.anchor_after(offset + range_to_fold.end);
 462
 463            let abs_path = buffer
 464                .read(cx)
 465                .project_path(cx)
 466                .and_then(|project_path| project.read(cx).absolute_path(&project_path, cx));
 467            let snapshot = buffer.read(cx).snapshot();
 468
 469            let text = snapshot
 470                .text_for_range(selection_range.clone())
 471                .collect::<String>();
 472            let point_range = selection_range.to_point(&snapshot);
 473            let line_range = point_range.start.row..=point_range.end.row;
 474
 475            let uri = MentionUri::Selection {
 476                abs_path: abs_path.clone(),
 477                line_range: line_range.clone(),
 478            };
 479            let crease = crease_for_mention(
 480                selection_name(abs_path.as_deref(), &line_range).into(),
 481                uri.icon_path(cx),
 482                range,
 483                editor.downgrade(),
 484            );
 485
 486            let crease_id = editor.update(cx, |editor, cx| {
 487                let crease_ids = editor.insert_creases(vec![crease.clone()], cx);
 488                editor.fold_creases(vec![crease], false, window, cx);
 489                crease_ids.first().copied().unwrap()
 490            });
 491
 492            self.mentions.insert(
 493                crease_id,
 494                (
 495                    uri,
 496                    Task::ready(Ok(Mention::Text {
 497                        content: text,
 498                        tracked_buffers: vec![buffer],
 499                    }))
 500                    .shared(),
 501                ),
 502            );
 503        }
 504
 505        // Take this explanation with a grain of salt but, with creases being
 506        // inserted, GPUI's recomputes the editor layout in the next frames, so
 507        // directly calling `editor.request_autoscroll` wouldn't work as
 508        // expected. We're leveraging `cx.on_next_frame` to wait 2 frames and
 509        // ensure that the layout has been recalculated so that the autoscroll
 510        // request actually shows the cursor's new position.
 511        cx.on_next_frame(window, move |_, window, cx| {
 512            cx.on_next_frame(window, move |_, _, cx| {
 513                editor.update(cx, |editor, cx| {
 514                    editor.request_autoscroll(Autoscroll::fit(), cx)
 515                });
 516            });
 517        });
 518    }
 519
 520    fn confirm_mention_for_thread(
 521        &mut self,
 522        id: acp::SessionId,
 523        cx: &mut Context<Self>,
 524    ) -> Task<Result<Mention>> {
 525        let Some(thread_store) = self.thread_store.clone() else {
 526            return Task::ready(Err(anyhow!(
 527                "Thread mentions are only supported for the native agent"
 528            )));
 529        };
 530        let Some(project) = self.project.upgrade() else {
 531            return Task::ready(Err(anyhow!("project not found")));
 532        };
 533
 534        let server = Rc::new(agent::NativeAgentServer::new(
 535            project.read(cx).fs().clone(),
 536            thread_store,
 537        ));
 538        let delegate = AgentServerDelegate::new(
 539            project.read(cx).agent_server_store().clone(),
 540            project.clone(),
 541            None,
 542            None,
 543        );
 544        let connection = server.connect(None, delegate, cx);
 545        cx.spawn(async move |_, cx| {
 546            let (agent, _) = connection.await?;
 547            let agent = agent.downcast::<agent::NativeAgentConnection>().unwrap();
 548            let summary = agent
 549                .0
 550                .update(cx, |agent, cx| agent.thread_summary(id, cx))
 551                .await?;
 552            Ok(Mention::Text {
 553                content: summary.to_string(),
 554                tracked_buffers: Vec::new(),
 555            })
 556        })
 557    }
 558
 559    fn confirm_mention_for_diagnostics(
 560        &self,
 561        include_errors: bool,
 562        include_warnings: bool,
 563        cx: &mut Context<Self>,
 564    ) -> Task<Result<Mention>> {
 565        let Some(project) = self.project.upgrade() else {
 566            return Task::ready(Err(anyhow!("project not found")));
 567        };
 568
 569        let diagnostics_task = collect_diagnostics_output(
 570            project,
 571            assistant_slash_commands::Options {
 572                include_errors,
 573                include_warnings,
 574                path_matcher: None,
 575            },
 576            cx,
 577        );
 578        cx.spawn(async move |_, _| {
 579            let output = diagnostics_task.await?;
 580            let content = output
 581                .map(|output| output.text)
 582                .unwrap_or_else(|| "No diagnostics found.".into());
 583            Ok(Mention::Text {
 584                content,
 585                tracked_buffers: Vec::new(),
 586            })
 587        })
 588    }
 589}
 590
 591#[cfg(test)]
 592mod tests {
 593    use super::*;
 594
 595    use fs::FakeFs;
 596    use gpui::TestAppContext;
 597    use project::Project;
 598    use prompt_store;
 599    use release_channel;
 600    use semver::Version;
 601    use serde_json::json;
 602    use settings::SettingsStore;
 603    use std::path::Path;
 604    use theme;
 605    use util::path;
 606
 607    fn init_test(cx: &mut TestAppContext) {
 608        let settings_store = cx.update(SettingsStore::test);
 609        cx.set_global(settings_store);
 610        cx.update(|cx| {
 611            theme::init(theme::LoadThemes::JustBase, cx);
 612            release_channel::init(Version::new(0, 0, 0), cx);
 613            prompt_store::init(cx);
 614        });
 615    }
 616
 617    #[gpui::test]
 618    async fn test_thread_mentions_disabled(cx: &mut TestAppContext) {
 619        init_test(cx);
 620
 621        let fs = FakeFs::new(cx.executor());
 622        fs.insert_tree("/project", json!({"file": ""})).await;
 623        let project = Project::test(fs, [Path::new(path!("/project"))], cx).await;
 624        let thread_store = None;
 625        let mention_set = cx.new(|_cx| MentionSet::new(project.downgrade(), thread_store, None));
 626
 627        let task = mention_set.update(cx, |mention_set, cx| {
 628            mention_set.confirm_mention_for_thread(acp::SessionId::new("thread-1"), cx)
 629        });
 630
 631        let error = task.await.unwrap_err();
 632        assert!(
 633            error
 634                .to_string()
 635                .contains("Thread mentions are only supported for the native agent"),
 636            "Unexpected error: {error:#}"
 637        );
 638    }
 639}
 640
 641/// Inserts a list of images into the editor as context mentions.
 642/// This is the shared implementation used by both paste and file picker operations.
 643pub(crate) async fn insert_images_as_context(
 644    images: Vec<gpui::Image>,
 645    editor: Entity<Editor>,
 646    mention_set: Entity<MentionSet>,
 647    cx: &mut gpui::AsyncWindowContext,
 648) {
 649    if images.is_empty() {
 650        return;
 651    }
 652
 653    let replacement_text = MentionUri::PastedImage.as_link().to_string();
 654
 655    for image in images {
 656        let Some((excerpt_id, text_anchor, multibuffer_anchor)) = editor
 657            .update_in(cx, |editor, window, cx| {
 658                let snapshot = editor.snapshot(window, cx);
 659                let (excerpt_id, _, buffer_snapshot) =
 660                    snapshot.buffer_snapshot().as_singleton().unwrap();
 661
 662                let cursor_anchor = editor.selections.newest_anchor().start.text_anchor;
 663                let text_anchor = cursor_anchor.bias_left(&buffer_snapshot);
 664                let multibuffer_anchor = snapshot
 665                    .buffer_snapshot()
 666                    .anchor_in_excerpt(*excerpt_id, text_anchor);
 667                editor.insert(&format!("{replacement_text} "), window, cx);
 668                (*excerpt_id, text_anchor, multibuffer_anchor)
 669            })
 670            .ok()
 671        else {
 672            break;
 673        };
 674
 675        let content_len = replacement_text.len();
 676        let Some(start_anchor) = multibuffer_anchor else {
 677            continue;
 678        };
 679        let end_anchor = editor.update(cx, |editor, cx| {
 680            let snapshot = editor.buffer().read(cx).snapshot(cx);
 681            snapshot.anchor_before(start_anchor.to_offset(&snapshot) + content_len)
 682        });
 683        let image = Arc::new(image);
 684        let Ok(Some((crease_id, tx))) = cx.update(|window, cx| {
 685            insert_crease_for_mention(
 686                excerpt_id,
 687                text_anchor,
 688                content_len,
 689                MentionUri::PastedImage.name().into(),
 690                IconName::Image.path().into(),
 691                Some(Task::ready(Ok(image.clone())).shared()),
 692                editor.clone(),
 693                window,
 694                cx,
 695            )
 696        }) else {
 697            continue;
 698        };
 699        let task = cx
 700            .spawn(async move |cx| {
 701                let image = cx
 702                    .update(|_, cx| LanguageModelImage::from_image(image, cx))
 703                    .map_err(|e| e.to_string())?
 704                    .await;
 705                drop(tx);
 706                if let Some(image) = image {
 707                    Ok(Mention::Image(MentionImage {
 708                        data: image.source,
 709                        format: LanguageModelImage::FORMAT,
 710                    }))
 711                } else {
 712                    Err("Failed to convert image".into())
 713                }
 714            })
 715            .shared();
 716
 717        mention_set.update(cx, |mention_set, _cx| {
 718            mention_set.insert_mention(crease_id, MentionUri::PastedImage, task.clone())
 719        });
 720
 721        if task.await.notify_async_err(cx).is_none() {
 722            editor.update(cx, |editor, cx| {
 723                editor.edit([(start_anchor..end_anchor, "")], cx);
 724            });
 725            mention_set.update(cx, |mention_set, _cx| {
 726                mention_set.remove_mention(&crease_id)
 727            });
 728        }
 729    }
 730}
 731
 732pub(crate) fn paste_images_as_context(
 733    editor: Entity<Editor>,
 734    mention_set: Entity<MentionSet>,
 735    window: &mut Window,
 736    cx: &mut App,
 737) -> Option<Task<()>> {
 738    let clipboard = cx.read_from_clipboard()?;
 739    Some(window.spawn(cx, async move |cx| {
 740        use itertools::Itertools;
 741        let (mut images, paths) = clipboard
 742            .into_entries()
 743            .filter_map(|entry| match entry {
 744                ClipboardEntry::Image(image) => Some(Either::Left(image)),
 745                ClipboardEntry::ExternalPaths(paths) => Some(Either::Right(paths)),
 746                _ => None,
 747            })
 748            .partition_map::<Vec<_>, Vec<_>, _, _, _>(std::convert::identity);
 749
 750        if !paths.is_empty() {
 751            images.extend(
 752                cx.background_spawn(async move {
 753                    let mut images = vec![];
 754                    for path in paths.into_iter().flat_map(|paths| paths.paths().to_owned()) {
 755                        let Ok(content) = async_fs::read(path).await else {
 756                            continue;
 757                        };
 758                        let Ok(format) = image::guess_format(&content) else {
 759                            continue;
 760                        };
 761                        images.push(gpui::Image::from_bytes(
 762                            match format {
 763                                image::ImageFormat::Png => gpui::ImageFormat::Png,
 764                                image::ImageFormat::Jpeg => gpui::ImageFormat::Jpeg,
 765                                image::ImageFormat::WebP => gpui::ImageFormat::Webp,
 766                                image::ImageFormat::Gif => gpui::ImageFormat::Gif,
 767                                image::ImageFormat::Bmp => gpui::ImageFormat::Bmp,
 768                                image::ImageFormat::Tiff => gpui::ImageFormat::Tiff,
 769                                image::ImageFormat::Ico => gpui::ImageFormat::Ico,
 770                                _ => continue,
 771                            },
 772                            content,
 773                        ));
 774                    }
 775                    images
 776                })
 777                .await,
 778            );
 779        }
 780
 781        cx.update(|_window, cx| {
 782            cx.stop_propagation();
 783        })
 784        .ok();
 785
 786        insert_images_as_context(images, editor, mention_set, cx).await;
 787    }))
 788}
 789
 790pub(crate) fn insert_crease_for_mention(
 791    excerpt_id: ExcerptId,
 792    anchor: text::Anchor,
 793    content_len: usize,
 794    crease_label: SharedString,
 795    crease_icon: SharedString,
 796    // abs_path: Option<Arc<Path>>,
 797    image: Option<Shared<Task<Result<Arc<Image>, String>>>>,
 798    editor: Entity<Editor>,
 799    window: &mut Window,
 800    cx: &mut App,
 801) -> Option<(CreaseId, postage::barrier::Sender)> {
 802    let (tx, rx) = postage::barrier::channel();
 803
 804    let crease_id = editor.update(cx, |editor, cx| {
 805        let snapshot = editor.buffer().read(cx).snapshot(cx);
 806
 807        let start = snapshot.anchor_in_excerpt(excerpt_id, anchor)?;
 808
 809        let start = start.bias_right(&snapshot);
 810        let end = snapshot.anchor_before(start.to_offset(&snapshot) + content_len);
 811
 812        let placeholder = FoldPlaceholder {
 813            render: render_mention_fold_button(
 814                crease_label.clone(),
 815                crease_icon.clone(),
 816                start..end,
 817                rx,
 818                image,
 819                cx.weak_entity(),
 820                cx,
 821            ),
 822            merge_adjacent: false,
 823            ..Default::default()
 824        };
 825
 826        let crease = Crease::Inline {
 827            range: start..end,
 828            placeholder,
 829            render_toggle: None,
 830            render_trailer: None,
 831            metadata: Some(CreaseMetadata {
 832                label: crease_label,
 833                icon_path: crease_icon,
 834            }),
 835        };
 836
 837        let ids = editor.insert_creases(vec![crease.clone()], cx);
 838        editor.fold_creases(vec![crease], false, window, cx);
 839
 840        Some(ids[0])
 841    })?;
 842
 843    Some((crease_id, tx))
 844}
 845
 846pub(crate) fn crease_for_mention(
 847    label: SharedString,
 848    icon_path: SharedString,
 849    range: Range<Anchor>,
 850    editor_entity: WeakEntity<Editor>,
 851) -> Crease<Anchor> {
 852    let placeholder = FoldPlaceholder {
 853        render: render_fold_icon_button(icon_path.clone(), label.clone(), editor_entity),
 854        merge_adjacent: false,
 855        ..Default::default()
 856    };
 857
 858    let render_trailer = move |_row, _unfold, _window: &mut Window, _cx: &mut App| Empty.into_any();
 859
 860    Crease::inline(range, placeholder, fold_toggle("mention"), render_trailer)
 861        .with_metadata(CreaseMetadata { icon_path, label })
 862}
 863
 864fn render_fold_icon_button(
 865    icon_path: SharedString,
 866    label: SharedString,
 867    editor: WeakEntity<Editor>,
 868) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
 869    Arc::new({
 870        move |fold_id, fold_range, cx| {
 871            let is_in_text_selection = editor
 872                .update(cx, |editor, cx| editor.is_range_selected(&fold_range, cx))
 873                .unwrap_or_default();
 874
 875            MentionCrease::new(fold_id, icon_path.clone(), label.clone())
 876                .is_toggled(is_in_text_selection)
 877                .into_any_element()
 878        }
 879    })
 880}
 881
 882fn fold_toggle(
 883    name: &'static str,
 884) -> impl Fn(
 885    MultiBufferRow,
 886    bool,
 887    Arc<dyn Fn(bool, &mut Window, &mut App) + Send + Sync>,
 888    &mut Window,
 889    &mut App,
 890) -> AnyElement {
 891    move |row, is_folded, fold, _window, _cx| {
 892        Disclosure::new((name, row.0 as u64), !is_folded)
 893            .toggle_state(is_folded)
 894            .on_click(move |_e, window, cx| fold(!is_folded, window, cx))
 895            .into_any_element()
 896    }
 897}
 898
 899fn full_mention_for_directory(
 900    project: &Entity<Project>,
 901    abs_path: &Path,
 902    cx: &mut App,
 903) -> Task<Result<Mention>> {
 904    fn collect_files_in_path(worktree: &Worktree, path: &RelPath) -> Vec<(Arc<RelPath>, String)> {
 905        let mut files = Vec::new();
 906
 907        for entry in worktree.child_entries(path) {
 908            if entry.is_dir() {
 909                files.extend(collect_files_in_path(worktree, &entry.path));
 910            } else if entry.is_file() {
 911                files.push((
 912                    entry.path.clone(),
 913                    worktree
 914                        .full_path(&entry.path)
 915                        .to_string_lossy()
 916                        .to_string(),
 917                ));
 918            }
 919        }
 920
 921        files
 922    }
 923
 924    let Some(project_path) = project
 925        .read(cx)
 926        .project_path_for_absolute_path(&abs_path, cx)
 927    else {
 928        return Task::ready(Err(anyhow!("project path not found")));
 929    };
 930    let Some(entry) = project.read(cx).entry_for_path(&project_path, cx) else {
 931        return Task::ready(Err(anyhow!("project entry not found")));
 932    };
 933    let directory_path = entry.path.clone();
 934    let worktree_id = project_path.worktree_id;
 935    let Some(worktree) = project.read(cx).worktree_for_id(worktree_id, cx) else {
 936        return Task::ready(Err(anyhow!("worktree not found")));
 937    };
 938    let project = project.clone();
 939    cx.spawn(async move |cx| {
 940        let file_paths = worktree.read_with(cx, |worktree, _cx| {
 941            collect_files_in_path(worktree, &directory_path)
 942        });
 943        let descendants_future = cx.update(|cx| {
 944            futures::future::join_all(file_paths.into_iter().map(
 945                |(worktree_path, full_path): (Arc<RelPath>, String)| {
 946                    let rel_path = worktree_path
 947                        .strip_prefix(&directory_path)
 948                        .log_err()
 949                        .map_or_else(|| worktree_path.clone(), |rel_path| rel_path.into());
 950
 951                    let open_task = project.update(cx, |project, cx| {
 952                        project.buffer_store().update(cx, |buffer_store, cx| {
 953                            let project_path = ProjectPath {
 954                                worktree_id,
 955                                path: worktree_path,
 956                            };
 957                            buffer_store.open_buffer(project_path, cx)
 958                        })
 959                    });
 960
 961                    cx.spawn(async move |cx| {
 962                        let buffer = open_task.await.log_err()?;
 963                        let buffer_content = outline::get_buffer_content_or_outline(
 964                            buffer.clone(),
 965                            Some(&full_path),
 966                            &cx,
 967                        )
 968                        .await
 969                        .ok()?;
 970
 971                        Some((rel_path, full_path, buffer_content.text, buffer))
 972                    })
 973                },
 974            ))
 975        });
 976
 977        let contents = cx
 978            .background_spawn(async move {
 979                let (contents, tracked_buffers): (Vec<_>, Vec<_>) = descendants_future
 980                    .await
 981                    .into_iter()
 982                    .flatten()
 983                    .map(|(rel_path, full_path, rope, buffer)| {
 984                        ((rel_path, full_path, rope), buffer)
 985                    })
 986                    .unzip();
 987                Mention::Text {
 988                    content: render_directory_contents(contents),
 989                    tracked_buffers,
 990                }
 991            })
 992            .await;
 993        anyhow::Ok(contents)
 994    })
 995}
 996
 997fn render_directory_contents(entries: Vec<(Arc<RelPath>, String, String)>) -> String {
 998    let mut output = String::new();
 999    for (_relative_path, full_path, content) in entries {
1000        let fence = codeblock_fence_for_path(Some(&full_path), None);
1001        write!(output, "\n{fence}\n{content}\n```").unwrap();
1002    }
1003    output
1004}
1005
1006fn render_mention_fold_button(
1007    label: SharedString,
1008    icon: SharedString,
1009    range: Range<Anchor>,
1010    mut loading_finished: postage::barrier::Receiver,
1011    image_task: Option<Shared<Task<Result<Arc<Image>, String>>>>,
1012    editor: WeakEntity<Editor>,
1013    cx: &mut App,
1014) -> Arc<dyn Send + Sync + Fn(FoldId, Range<Anchor>, &mut App) -> AnyElement> {
1015    let loading = cx.new(|cx| {
1016        let loading = cx.spawn(async move |this, cx| {
1017            loading_finished.recv().await;
1018            this.update(cx, |this: &mut LoadingContext, cx| {
1019                this.loading = None;
1020                cx.notify();
1021            })
1022            .ok();
1023        });
1024        LoadingContext {
1025            id: cx.entity_id(),
1026            label,
1027            icon,
1028            range,
1029            editor,
1030            loading: Some(loading),
1031            image: image_task.clone(),
1032        }
1033    });
1034    Arc::new(move |_fold_id, _fold_range, _cx| loading.clone().into_any_element())
1035}
1036
1037struct LoadingContext {
1038    id: EntityId,
1039    label: SharedString,
1040    icon: SharedString,
1041    range: Range<Anchor>,
1042    editor: WeakEntity<Editor>,
1043    loading: Option<Task<()>>,
1044    image: Option<Shared<Task<Result<Arc<Image>, String>>>>,
1045}
1046
1047impl Render for LoadingContext {
1048    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1049        let is_in_text_selection = self
1050            .editor
1051            .update(cx, |editor, cx| editor.is_range_selected(&self.range, cx))
1052            .unwrap_or_default();
1053
1054        let id = ElementId::from(("loading_context", self.id));
1055
1056        MentionCrease::new(id, self.icon.clone(), self.label.clone())
1057            .is_toggled(is_in_text_selection)
1058            .is_loading(self.loading.is_some())
1059            .when_some(self.image.clone(), |this, image_task| {
1060                this.image_preview(move |_, cx| {
1061                    let image = image_task.peek().cloned().transpose().ok().flatten();
1062                    let image_task = image_task.clone();
1063                    cx.new::<ImageHover>(|cx| ImageHover {
1064                        image,
1065                        _task: cx.spawn(async move |this, cx| {
1066                            if let Ok(image) = image_task.clone().await {
1067                                this.update(cx, |this, cx| {
1068                                    if this.image.replace(image).is_none() {
1069                                        cx.notify();
1070                                    }
1071                                })
1072                                .ok();
1073                            }
1074                        }),
1075                    })
1076                    .into()
1077                })
1078            })
1079    }
1080}
1081
1082struct ImageHover {
1083    image: Option<Arc<Image>>,
1084    _task: Task<()>,
1085}
1086
1087impl Render for ImageHover {
1088    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1089        if let Some(image) = self.image.clone() {
1090            div()
1091                .p_1p5()
1092                .elevation_2(cx)
1093                .child(gpui::img(image).h_auto().max_w_96().rounded_sm())
1094                .into_any_element()
1095        } else {
1096            gpui::Empty.into_any_element()
1097        }
1098    }
1099}
1100
1101async fn fetch_url_content(http_client: Arc<HttpClientWithUrl>, url: String) -> Result<String> {
1102    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
1103    enum ContentType {
1104        Html,
1105        Plaintext,
1106        Json,
1107    }
1108    use html_to_markdown::{TagHandler, convert_html_to_markdown, markdown};
1109
1110    let url = if !url.starts_with("https://") && !url.starts_with("http://") {
1111        format!("https://{url}")
1112    } else {
1113        url
1114    };
1115
1116    let mut response = http_client.get(&url, AsyncBody::default(), true).await?;
1117    let mut body = Vec::new();
1118    response
1119        .body_mut()
1120        .read_to_end(&mut body)
1121        .await
1122        .context("error reading response body")?;
1123
1124    if response.status().is_client_error() {
1125        let text = String::from_utf8_lossy(body.as_slice());
1126        anyhow::bail!(
1127            "status error {}, response: {text:?}",
1128            response.status().as_u16()
1129        );
1130    }
1131
1132    let Some(content_type) = response.headers().get("content-type") else {
1133        anyhow::bail!("missing Content-Type header");
1134    };
1135    let content_type = content_type
1136        .to_str()
1137        .context("invalid Content-Type header")?;
1138    let content_type = match content_type {
1139        "text/html" => ContentType::Html,
1140        "text/plain" => ContentType::Plaintext,
1141        "application/json" => ContentType::Json,
1142        _ => ContentType::Html,
1143    };
1144
1145    match content_type {
1146        ContentType::Html => {
1147            let mut handlers: Vec<TagHandler> = vec![
1148                Rc::new(RefCell::new(markdown::WebpageChromeRemover)),
1149                Rc::new(RefCell::new(markdown::ParagraphHandler)),
1150                Rc::new(RefCell::new(markdown::HeadingHandler)),
1151                Rc::new(RefCell::new(markdown::ListHandler)),
1152                Rc::new(RefCell::new(markdown::TableHandler::new())),
1153                Rc::new(RefCell::new(markdown::StyledTextHandler)),
1154            ];
1155            if url.contains("wikipedia.org") {
1156                use html_to_markdown::structure::wikipedia;
1157
1158                handlers.push(Rc::new(RefCell::new(wikipedia::WikipediaChromeRemover)));
1159                handlers.push(Rc::new(RefCell::new(wikipedia::WikipediaInfoboxHandler)));
1160                handlers.push(Rc::new(
1161                    RefCell::new(wikipedia::WikipediaCodeHandler::new()),
1162                ));
1163            } else {
1164                handlers.push(Rc::new(RefCell::new(markdown::CodeHandler)));
1165            }
1166            convert_html_to_markdown(&body[..], &mut handlers)
1167        }
1168        ContentType::Plaintext => Ok(std::str::from_utf8(&body)?.to_owned()),
1169        ContentType::Json => {
1170            let json: serde_json::Value = serde_json::from_slice(&body)?;
1171
1172            Ok(format!(
1173                "```json\n{}\n```",
1174                serde_json::to_string_pretty(&json)?
1175            ))
1176        }
1177    }
1178}