fetch_command.rs

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