fetch_command.rs

  1use std::cell::RefCell;
  2use std::rc::Rc;
  3use std::sync::Arc;
  4use std::sync::atomic::AtomicBool;
  5
  6use anyhow::{Context as _, 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 = if content_type.starts_with("text/html") {
 59            ContentType::Html
 60        } else if content_type.starts_with("text/plain") {
 61            ContentType::Plaintext
 62        } else if content_type.starts_with("application/json") {
 63            ContentType::Json
 64        } else {
 65            ContentType::Html
 66        };
 67
 68        match content_type {
 69            ContentType::Html => {
 70                let mut handlers: Vec<TagHandler> = vec![
 71                    Rc::new(RefCell::new(markdown::WebpageChromeRemover)),
 72                    Rc::new(RefCell::new(markdown::ParagraphHandler)),
 73                    Rc::new(RefCell::new(markdown::HeadingHandler)),
 74                    Rc::new(RefCell::new(markdown::ListHandler)),
 75                    Rc::new(RefCell::new(markdown::TableHandler::new())),
 76                    Rc::new(RefCell::new(markdown::StyledTextHandler)),
 77                ];
 78                if url.contains("wikipedia.org") {
 79                    use html_to_markdown::structure::wikipedia;
 80
 81                    handlers.push(Rc::new(RefCell::new(wikipedia::WikipediaChromeRemover)));
 82                    handlers.push(Rc::new(RefCell::new(wikipedia::WikipediaInfoboxHandler)));
 83                    handlers.push(Rc::new(
 84                        RefCell::new(wikipedia::WikipediaCodeHandler::new()),
 85                    ));
 86                } else {
 87                    handlers.push(Rc::new(RefCell::new(markdown::CodeHandler)));
 88                }
 89
 90                convert_html_to_markdown(&body[..], &mut handlers)
 91            }
 92            ContentType::Plaintext => Ok(std::str::from_utf8(&body)?.to_owned()),
 93            ContentType::Json => {
 94                let json: serde_json::Value = serde_json::from_slice(&body)?;
 95
 96                Ok(format!(
 97                    "```json\n{}\n```",
 98                    serde_json::to_string_pretty(&json)?
 99                ))
100            }
101        }
102    }
103}
104
105impl SlashCommand for FetchSlashCommand {
106    fn name(&self) -> String {
107        "fetch".into()
108    }
109
110    fn description(&self) -> String {
111        "Insert fetched URL contents".into()
112    }
113
114    fn icon(&self) -> IconName {
115        IconName::ToolWeb
116    }
117
118    fn menu_text(&self) -> String {
119        self.description()
120    }
121
122    fn requires_argument(&self) -> bool {
123        true
124    }
125
126    fn complete_argument(
127        self: Arc<Self>,
128        _arguments: &[String],
129        _cancel: Arc<AtomicBool>,
130        _workspace: Option<WeakEntity<Workspace>>,
131        _window: &mut Window,
132        _cx: &mut App,
133    ) -> Task<Result<Vec<ArgumentCompletion>>> {
134        Task::ready(Ok(Vec::new()))
135    }
136
137    fn run(
138        self: Arc<Self>,
139        arguments: &[String],
140        _context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
141        _context_buffer: BufferSnapshot,
142        workspace: WeakEntity<Workspace>,
143        _delegate: Option<Arc<dyn LspAdapterDelegate>>,
144        _: &mut Window,
145        cx: &mut App,
146    ) -> Task<SlashCommandResult> {
147        let Some(argument) = arguments.first() else {
148            return Task::ready(Err(anyhow!("missing URL")));
149        };
150        let Some(workspace) = workspace.upgrade() else {
151            return Task::ready(Err(anyhow!("workspace was dropped")));
152        };
153
154        let http_client = workspace.read(cx).client().http_client();
155        let url = argument.to_string();
156
157        let text = cx.background_spawn({
158            let url = url.clone();
159            async move { Self::build_message(http_client, &url).await }
160        });
161
162        let url = SharedString::from(url);
163        cx.foreground_executor().spawn(async move {
164            let text = text.await?;
165            if text.trim().is_empty() {
166                bail!("no textual content found");
167            }
168
169            let range = 0..text.len();
170            Ok(SlashCommandOutput {
171                text,
172                sections: vec![SlashCommandOutputSection {
173                    range,
174                    icon: IconName::ToolWeb,
175                    label: format!("fetch {}", url).into(),
176                    metadata: None,
177                }],
178                run_commands_in_text: false,
179            }
180            .into_event_stream())
181        })
182    }
183}