fetch_command.rs

  1use std::cell::RefCell;
  2use std::rc::Rc;
  3use std::sync::atomic::AtomicBool;
  4use std::sync::Arc;
  5
  6use anyhow::{anyhow, bail, Context, Result};
  7use assistant_slash_command::{SlashCommand, SlashCommandOutput, SlashCommandOutputSection};
  8use futures::AsyncReadExt;
  9use gpui::{AppContext, Task, WeakView};
 10use html_to_markdown::{convert_html_to_markdown, markdown, TagHandler};
 11use http::{AsyncBody, HttpClient, HttpClientWithUrl};
 12use language::LspAdapterDelegate;
 13use ui::prelude::*;
 14use workspace::Workspace;
 15
 16#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
 17enum ContentType {
 18    Html,
 19    Plaintext,
 20    Json,
 21}
 22
 23pub(crate) struct FetchSlashCommand;
 24
 25impl FetchSlashCommand {
 26    async fn build_message(http_client: Arc<HttpClientWithUrl>, url: &str) -> Result<String> {
 27        let mut url = url.to_owned();
 28        if !url.starts_with("https://") {
 29            url = format!("https://{url}");
 30        }
 31
 32        let mut response = http_client.get(&url, AsyncBody::default(), true).await?;
 33
 34        let mut body = Vec::new();
 35        response
 36            .body_mut()
 37            .read_to_end(&mut body)
 38            .await
 39            .context("error reading response body")?;
 40
 41        if response.status().is_client_error() {
 42            let text = String::from_utf8_lossy(body.as_slice());
 43            bail!(
 44                "status error {}, response: {text:?}",
 45                response.status().as_u16()
 46            );
 47        }
 48
 49        let Some(content_type) = response.headers().get("content-type") else {
 50            bail!("missing Content-Type header");
 51        };
 52        let content_type = content_type
 53            .to_str()
 54            .context("invalid Content-Type header")?;
 55        let content_type = match content_type {
 56            "text/html" => ContentType::Html,
 57            "text/plain" => ContentType::Plaintext,
 58            "application/json" => ContentType::Json,
 59            _ => ContentType::Html,
 60        };
 61
 62        match content_type {
 63            ContentType::Html => {
 64                let mut handlers: Vec<TagHandler> = vec![
 65                    Rc::new(RefCell::new(markdown::WebpageChromeRemover)),
 66                    Rc::new(RefCell::new(markdown::ParagraphHandler)),
 67                    Rc::new(RefCell::new(markdown::HeadingHandler)),
 68                    Rc::new(RefCell::new(markdown::ListHandler)),
 69                    Rc::new(RefCell::new(markdown::TableHandler::new())),
 70                    Rc::new(RefCell::new(markdown::StyledTextHandler)),
 71                ];
 72                if url.contains("wikipedia.org") {
 73                    use html_to_markdown::structure::wikipedia;
 74
 75                    handlers.push(Rc::new(RefCell::new(wikipedia::WikipediaChromeRemover)));
 76                    handlers.push(Rc::new(RefCell::new(wikipedia::WikipediaInfoboxHandler)));
 77                    handlers.push(Rc::new(
 78                        RefCell::new(wikipedia::WikipediaCodeHandler::new()),
 79                    ));
 80                } else {
 81                    handlers.push(Rc::new(RefCell::new(markdown::CodeHandler)));
 82                }
 83
 84                convert_html_to_markdown(&body[..], &mut handlers)
 85            }
 86            ContentType::Plaintext => Ok(std::str::from_utf8(&body)?.to_owned()),
 87            ContentType::Json => {
 88                let json: serde_json::Value = serde_json::from_slice(&body)?;
 89
 90                Ok(format!(
 91                    "```json\n{}\n```",
 92                    serde_json::to_string_pretty(&json)?
 93                ))
 94            }
 95        }
 96    }
 97}
 98
 99impl SlashCommand for FetchSlashCommand {
100    fn name(&self) -> String {
101        "fetch".into()
102    }
103
104    fn description(&self) -> String {
105        "insert URL contents".into()
106    }
107
108    fn menu_text(&self) -> String {
109        "Insert fetched URL contents".into()
110    }
111
112    fn requires_argument(&self) -> bool {
113        true
114    }
115
116    fn complete_argument(
117        self: Arc<Self>,
118        _query: String,
119        _cancel: Arc<AtomicBool>,
120        _workspace: Option<WeakView<Workspace>>,
121        _cx: &mut AppContext,
122    ) -> Task<Result<Vec<String>>> {
123        Task::ready(Ok(Vec::new()))
124    }
125
126    fn run(
127        self: Arc<Self>,
128        argument: Option<&str>,
129        workspace: WeakView<Workspace>,
130        _delegate: Arc<dyn LspAdapterDelegate>,
131        cx: &mut WindowContext,
132    ) -> Task<Result<SlashCommandOutput>> {
133        let Some(argument) = argument else {
134            return Task::ready(Err(anyhow!("missing URL")));
135        };
136        let Some(workspace) = workspace.upgrade() else {
137            return Task::ready(Err(anyhow!("workspace was dropped")));
138        };
139
140        let http_client = workspace.read(cx).client().http_client();
141        let url = argument.to_string();
142
143        let text = cx.background_executor().spawn({
144            let url = url.clone();
145            async move { Self::build_message(http_client, &url).await }
146        });
147
148        let url = SharedString::from(url);
149        cx.foreground_executor().spawn(async move {
150            let text = text.await?;
151            let range = 0..text.len();
152            Ok(SlashCommandOutput {
153                text,
154                sections: vec![SlashCommandOutputSection {
155                    range,
156                    icon: IconName::AtSign,
157                    label: format!("fetch {}", url).into(),
158                }],
159                run_commands_in_text: false,
160            })
161        })
162    }
163}