fetchkit 0.1.3

AI-friendly web content fetching and HTML-to-Markdown conversion library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Tool builder and contract for FetchKit

use crate::client::{fetch_with_options, FetchOptions};
use crate::dns::DnsPolicy;
use crate::error::FetchError;
use crate::fetchers::FetcherRegistry;
use crate::file_saver::FileSaver;
use crate::types::{FetchRequest, FetchResponse};
use crate::{build_llmtxt, TOOL_DESCRIPTION_BASE, TOOL_DESCRIPTION_SAVE};
use schemars::schema_for;
use serde::{Deserialize, Serialize};

/// Status update during tool execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolStatus {
    /// Current phase (e.g., "validate", "connect", "fetch", "convert")
    pub phase: String,
    /// Optional message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    /// Estimated completion percentage (0-100)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub percent_complete: Option<f32>,
    /// Estimated time remaining in milliseconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub eta_ms: Option<u64>,
}

impl ToolStatus {
    /// Create a new status with phase
    pub fn new(phase: impl Into<String>) -> Self {
        Self {
            phase: phase.into(),
            message: None,
            percent_complete: None,
            eta_ms: None,
        }
    }

    /// Set message
    pub fn with_message(mut self, message: impl Into<String>) -> Self {
        self.message = Some(message.into());
        self
    }

    /// Set completion percentage
    pub fn with_percent(mut self, percent: f32) -> Self {
        self.percent_complete = Some(percent);
        self
    }

    /// Set ETA
    pub fn with_eta(mut self, eta_ms: u64) -> Self {
        self.eta_ms = Some(eta_ms);
        self
    }
}

/// Builder for configuring the FetchKit tool
///
/// # Examples
///
/// ```
/// use fetchkit::ToolBuilder;
///
/// let tool = ToolBuilder::new()
///     .enable_markdown(true)
///     .enable_text(false)
///     .user_agent("MyBot/1.0")
///     .allow_prefix("https://docs.example.com")
///     .block_prefix("https://internal.example.com")
///     .build();
///
/// assert!(!tool.description().is_empty());
/// ```
#[derive(Debug, Clone, Default)]
pub struct ToolBuilder {
    /// Enable as_markdown option
    enable_markdown: bool,
    /// Enable as_text option
    enable_text: bool,
    /// Custom User-Agent
    user_agent: Option<String>,
    /// Allow list of URL prefixes
    allow_prefixes: Vec<String>,
    /// Block list of URL prefixes
    block_prefixes: Vec<String>,
    /// DNS resolution policy for SSRF prevention
    dns_policy: DnsPolicy,
    /// Maximum response body size in bytes
    max_body_size: Option<usize>,
    /// Enable save_to_file parameter (opt-in)
    enable_save_to_file: bool,
}

impl ToolBuilder {
    /// Create a new tool builder with all options enabled
    pub fn new() -> Self {
        Self {
            enable_markdown: true,
            enable_text: true,
            ..Default::default()
        }
    }

    /// Enable as_markdown option
    pub fn enable_markdown(mut self, enable: bool) -> Self {
        self.enable_markdown = enable;
        self
    }

    /// Enable as_text option
    pub fn enable_text(mut self, enable: bool) -> Self {
        self.enable_text = enable;
        self
    }

    /// Set custom User-Agent
    pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
        self.user_agent = Some(ua.into());
        self
    }

    /// Add URL prefix to allow list
    pub fn allow_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.allow_prefixes.push(prefix.into());
        self
    }

    /// Add URL prefix to block list
    pub fn block_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.block_prefixes.push(prefix.into());
        self
    }

    /// Set maximum response body size in bytes
    ///
    /// Limits the amount of data read from responses. Protects against
    /// memory exhaustion from large responses and compressed content bombs.
    /// Default: 10 MB if not set.
    pub fn max_body_size(mut self, size: usize) -> Self {
        self.max_body_size = Some(size);
        self
    }

    /// Enable file download (save_to_file parameter).
    /// Disabled by default — opt-in only.
    pub fn enable_save_to_file(mut self, enable: bool) -> Self {
        self.enable_save_to_file = enable;
        self
    }

    /// Control private/reserved IP range blocking (SSRF prevention)
    ///
    /// Enabled by default. When enabled, FetchKit resolves hostnames to IP
    /// addresses before connecting and validates that the resolved IP is not
    /// in a private or reserved range. DNS pinning prevents rebinding attacks.
    ///
    /// Pass `false` only for local development or testing against loopback
    /// servers. In production, always leave this enabled.
    pub fn block_private_ips(mut self, block: bool) -> Self {
        self.dns_policy = if block {
            DnsPolicy::block_private_ips()
        } else {
            DnsPolicy::allow_all()
        };
        self
    }

    /// Build the tool
    pub fn build(self) -> Tool {
        Tool {
            enable_markdown: self.enable_markdown,
            enable_text: self.enable_text,
            user_agent: self.user_agent,
            allow_prefixes: self.allow_prefixes,
            block_prefixes: self.block_prefixes,
            dns_policy: self.dns_policy,
            max_body_size: self.max_body_size,
            enable_save_to_file: self.enable_save_to_file,
        }
    }
}

/// Configured FetchKit tool
///
/// Created via [`ToolBuilder`]. Provides methods for executing fetch requests,
/// retrieving schemas, and accessing tool metadata.
///
/// # Examples
///
/// ```no_run
/// use fetchkit::{FetchRequest, Tool};
///
/// # async fn example() -> Result<(), fetchkit::FetchError> {
/// let tool = Tool::default();
/// let response = tool.execute(FetchRequest::new("https://example.com")).await?;
/// println!("Status: {}", response.status_code);
/// # Ok(())
/// # }
/// ```
///
/// Tool metadata is available without making any requests:
///
/// ```
/// use fetchkit::Tool;
///
/// let tool = Tool::default();
/// assert!(!tool.description().is_empty());
/// assert!(!tool.llmtxt().is_empty());
///
/// let schema = tool.input_schema();
/// assert!(schema["properties"]["url"].is_object());
/// ```
#[derive(Debug, Clone)]
pub struct Tool {
    enable_markdown: bool,
    enable_text: bool,
    user_agent: Option<String>,
    allow_prefixes: Vec<String>,
    block_prefixes: Vec<String>,
    dns_policy: DnsPolicy,
    max_body_size: Option<usize>,
    enable_save_to_file: bool,
}

impl Default for Tool {
    fn default() -> Self {
        ToolBuilder::new().build()
    }
}

impl Tool {
    /// Create a new tool builder
    pub fn builder() -> ToolBuilder {
        ToolBuilder::new()
    }

    /// Get tool description, reflecting enabled features
    pub fn description(&self) -> String {
        let mut s = TOOL_DESCRIPTION_BASE.to_string();
        if self.enable_save_to_file {
            s.push_str(TOOL_DESCRIPTION_SAVE);
        }
        s
    }

    /// Get system prompt (empty for this tool)
    pub fn system_prompt(&self) -> String {
        String::new()
    }

    /// Get full documentation (llmtxt), reflecting enabled features
    pub fn llmtxt(&self) -> String {
        build_llmtxt(self.enable_save_to_file)
    }

    /// Get input schema as JSON
    pub fn input_schema(&self) -> serde_json::Value {
        let schema = schema_for!(FetchRequest);
        let mut value = serde_json::to_value(schema).unwrap_or_default();

        // Remove disabled options from schema
        if let Some(props) = value.get_mut("properties").and_then(|p| p.as_object_mut()) {
            if !self.enable_markdown {
                props.remove("as_markdown");
            }
            if !self.enable_text {
                props.remove("as_text");
            }
            if !self.enable_save_to_file {
                props.remove("save_to_file");
            }
        }

        value
    }

    /// Get output schema as JSON
    pub fn output_schema(&self) -> serde_json::Value {
        let schema = schema_for!(FetchResponse);
        serde_json::to_value(schema).unwrap_or_default()
    }

    /// Execute the tool with the given request
    pub async fn execute(&self, req: FetchRequest) -> Result<FetchResponse, FetchError> {
        fetch_with_options(req, self.build_options()).await
    }

    /// Execute the tool with status updates
    pub async fn execute_with_status<F>(
        &self,
        req: FetchRequest,
        mut status_callback: F,
    ) -> Result<FetchResponse, FetchError>
    where
        F: FnMut(ToolStatus),
    {
        status_callback(ToolStatus::new("validate").with_percent(0.0));

        // Validate request
        if req.url.is_empty() {
            return Err(FetchError::MissingUrl);
        }

        if !req.url.starts_with("http://") && !req.url.starts_with("https://") {
            return Err(FetchError::InvalidUrlScheme);
        }

        status_callback(ToolStatus::new("connect").with_percent(10.0));

        status_callback(ToolStatus::new("fetch").with_percent(20.0));

        let result = fetch_with_options(req, self.build_options()).await;

        status_callback(ToolStatus::new("complete").with_percent(100.0));

        result
    }

    /// Build FetchOptions from this Tool's configuration
    fn build_options(&self) -> FetchOptions {
        FetchOptions {
            user_agent: self.user_agent.clone(),
            allow_prefixes: self.allow_prefixes.clone(),
            block_prefixes: self.block_prefixes.clone(),
            enable_markdown: self.enable_markdown,
            enable_text: self.enable_text,
            dns_policy: self.dns_policy.clone(),
            max_body_size: self.max_body_size,
            enable_save_to_file: self.enable_save_to_file,
        }
    }

    /// Execute fetch with optional file saving.
    ///
    /// When `req.save_to_file` is set, validates the path via the saver,
    /// fetches content (including binary), and saves through the saver.
    /// Returns metadata without inline content.
    ///
    /// When `req.save_to_file` is `None`, behaves identically to [`execute`](Self::execute).
    pub async fn execute_with_saver(
        &self,
        req: FetchRequest,
        saver: Option<&dyn FileSaver>,
    ) -> Result<FetchResponse, FetchError> {
        if let Some(path) = &req.save_to_file {
            if !self.enable_save_to_file {
                return Err(FetchError::SaverNotAvailable);
            }

            let saver = saver.ok_or(FetchError::SaverNotAvailable)?;

            // Validate path before making HTTP request
            saver
                .validate_path(path)
                .await
                .map_err(|e| FetchError::SaveError(e.to_string()))?;

            let options = self.build_options();
            let registry = FetcherRegistry::with_defaults();
            registry.fetch_to_file(req, options, saver).await
        } else {
            self.execute(req).await
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tool_builder() {
        let tool = Tool::builder()
            .enable_markdown(false)
            .enable_text(true)
            .user_agent("TestAgent/1.0")
            .allow_prefix("https://allowed.com")
            .block_prefix("https://blocked.com")
            .build();

        assert!(!tool.enable_markdown);
        assert!(tool.enable_text);
        assert_eq!(tool.user_agent, Some("TestAgent/1.0".to_string()));
        assert_eq!(tool.allow_prefixes, vec!["https://allowed.com"]);
        assert_eq!(tool.block_prefixes, vec!["https://blocked.com"]);
        // Safe by default: private IPs blocked
        assert!(tool.dns_policy.block_private);
    }

    #[test]
    fn test_tool_builder_opt_out_private_ip_blocking() {
        let tool = Tool::builder().block_private_ips(false).build();
        assert!(!tool.dns_policy.block_private);
    }

    #[test]
    fn test_tool_description() {
        let tool = Tool::default();
        assert!(!tool.description().is_empty());
        assert!(tool.system_prompt().is_empty());
        assert!(!tool.llmtxt().is_empty());

        // Default tool should NOT mention save_to_file
        assert!(!tool.description().contains("save_to_file"));
        assert!(!tool.llmtxt().contains("save_to_file"));

        // Enabled tool SHOULD mention save_to_file
        let tool = Tool::builder().enable_save_to_file(true).build();
        assert!(tool.description().contains("save_to_file"));
        assert!(tool.llmtxt().contains("save_to_file"));
    }

    #[test]
    fn test_tool_schemas() {
        let tool = Tool::default();
        let input_schema = tool.input_schema();
        let output_schema = tool.output_schema();

        // Input schema should have url property
        assert!(input_schema["properties"]["url"].is_object());

        // Output schema should have url and status_code
        assert!(output_schema["properties"]["url"].is_object());
        assert!(output_schema["properties"]["status_code"].is_object());
    }

    #[test]
    fn test_tool_schema_feature_gating() {
        let tool = Tool::builder()
            .enable_markdown(false)
            .enable_text(false)
            .build();

        let schema = tool.input_schema();

        // Disabled options should be removed from schema
        if let Some(props) = schema.get("properties").and_then(|p| p.as_object()) {
            assert!(!props.contains_key("as_markdown"));
            assert!(!props.contains_key("as_text"));
        }
    }

    #[test]
    fn test_tool_status() {
        let status = ToolStatus::new("fetch")
            .with_message("Fetching URL")
            .with_percent(50.0)
            .with_eta(5000);

        assert_eq!(status.phase, "fetch");
        assert_eq!(status.message, Some("Fetching URL".to_string()));
        assert_eq!(status.percent_complete, Some(50.0));
        assert_eq!(status.eta_ms, Some(5000));
    }
}