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::{
8 ArgumentCompletion, SlashCommand, SlashCommandOutput, SlashCommandOutputSection,
9 SlashCommandResult,
10};
11use futures::AsyncReadExt;
12use gpui::{Task, WeakView};
13use html_to_markdown::{convert_html_to_markdown, markdown, TagHandler};
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(crate) 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<WeakView<Workspace>>,
128 _cx: &mut WindowContext,
129 ) -> Task<Result<Vec<ArgumentCompletion>>> {
130 Task::ready(Ok(Vec::new()))
131 }
132
133 fn run(
134 self: Arc<Self>,
135 arguments: &[String],
136 _context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
137 _context_buffer: BufferSnapshot,
138 workspace: WeakView<Workspace>,
139 _delegate: Option<Arc<dyn LspAdapterDelegate>>,
140 cx: &mut WindowContext,
141 ) -> Task<SlashCommandResult> {
142 let Some(argument) = arguments.first() else {
143 return Task::ready(Err(anyhow!("missing URL")));
144 };
145 let Some(workspace) = workspace.upgrade() else {
146 return Task::ready(Err(anyhow!("workspace was dropped")));
147 };
148
149 let http_client = workspace.read(cx).client().http_client();
150 let url = argument.to_string();
151
152 let text = cx.background_executor().spawn({
153 let url = url.clone();
154 async move { Self::build_message(http_client, &url).await }
155 });
156
157 let url = SharedString::from(url);
158 cx.foreground_executor().spawn(async move {
159 let text = text.await?;
160 if text.trim().is_empty() {
161 bail!("no textual content found");
162 }
163
164 let range = 0..text.len();
165 Ok(SlashCommandOutput {
166 text,
167 sections: vec![SlashCommandOutputSection {
168 range,
169 icon: IconName::Globe,
170 label: format!("fetch {}", url).into(),
171 metadata: None,
172 }],
173 run_commands_in_text: false,
174 }
175 .to_event_stream())
176 })
177 }
178}