Skip to main content

tiny_loop/tool/
web.rs

1use tiny_loop_macros::tool_internal;
2
3use super::utils::truncate_text;
4
5/// Fetch a webpage and convert HTML to Markdown
6#[tool_internal]
7pub async fn fetch(
8    /// URL to fetch
9    url: String,
10    /// Optional start character index (default: 0)
11    start: Option<usize>,
12    /// Optional length in characters (default: 5000)
13    len: Option<usize>,
14) -> String {
15    let response = match reqwest::get(&url).await {
16        Ok(r) => r,
17        Err(e) => return format!("Error fetching URL: {}", e),
18    };
19
20    let html = match response.text().await {
21        Ok(h) => h,
22        Err(e) => return format!("Error reading response: {}", e),
23    };
24
25    let markdown = html2md::parse_html(&html);
26    truncate_text(markdown, start.unwrap_or(0), len.unwrap_or(5000))
27}