reasonkit_web/
lib.rs

1//! ReasonKit Web - High-Performance Browser Connector
2//!
3//! A Rust-powered MCP server for browser automation, web capture, and content extraction.
4//!
5//! # Features
6//!
7//! - **CDP Integration**: Chrome DevTools Protocol client for browser automation
8//! - **MCP Server**: Model Context Protocol server for AI agent integration
9//! - **Content Extraction**: HTML parsing and structured data extraction
10//! - **WASM Support**: Browser-native execution via WebAssembly
11//!
12//! # Example
13//!
14//! ```rust,ignore
15//! use reasonkit_web::BrowserConnector;
16//!
17//! #[tokio::main]
18//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
19//!     let connector = BrowserConnector::new().await?;
20//!     let page = connector.navigate("https://example.com").await?;
21//!     let content = page.capture_content().await?;
22//!     Ok(())
23//! }
24//! ```
25
26#![cfg_attr(docsrs, feature(doc_cfg))]
27#![warn(missing_docs)]
28#![warn(clippy::all)]
29
30pub mod browser;
31pub mod buffer;
32pub mod cors;
33pub mod error;
34pub mod extraction;
35pub mod handlers;
36pub mod mcp;
37pub mod metrics;
38pub mod processing;
39pub mod shutdown;
40pub mod stripe;
41
42/// Crate version
43pub const VERSION: &str = env!("CARGO_PKG_VERSION");
44
45/// Crate name
46pub const NAME: &str = env!("CARGO_PKG_NAME");
47
48/// Generate a unique request ID (UUID v4)
49pub fn generate_request_id() -> String {
50    uuid::Uuid::new_v4().to_string()
51}
52
53// Re-export key components for convenience
54pub use browser::controller::BrowserController;
55pub use error::Error;
56pub use extraction::content::ContentExtractor;
57pub use extraction::links::LinkExtractor;
58pub use extraction::metadata::MetadataExtractor;
59pub use mcp::McpServer;
60
61/// Configuration defaults
62pub mod config {
63    /// Default connection timeout in milliseconds
64    pub const DEFAULT_TIMEOUT_MS: u64 = 30_000;
65
66    /// Default viewport width
67    pub const DEFAULT_VIEWPORT_WIDTH: u32 = 1920;
68
69    /// Default viewport height
70    pub const DEFAULT_VIEWPORT_HEIGHT: u32 = 1080;
71}
72
73/// Browser connector state for WASM environments
74#[cfg(target_arch = "wasm32")]
75#[wasm_bindgen]
76pub struct WasmBrowserConnector {
77    connected: bool,
78    url: String,
79}
80
81#[cfg(target_arch = "wasm32")]
82#[wasm_bindgen]
83impl WasmBrowserConnector {
84    /// Create a new WASM browser connector
85    #[wasm_bindgen(constructor)]
86    pub fn new() -> Self {
87        Self {
88            connected: false,
89            url: String::new(),
90        }
91    }
92
93    /// Check if connected
94    #[wasm_bindgen(getter)]
95    pub fn connected(&self) -> bool {
96        self.connected
97    }
98
99    /// Get current URL
100    #[wasm_bindgen(getter)]
101    pub fn url(&self) -> String {
102        self.url.clone()
103    }
104
105    /// Connect to a URL
106    pub fn connect(&mut self, url: &str) -> bool {
107        self.url = url.to_string();
108        self.connected = true;
109        true
110    }
111
112    /// Disconnect
113    pub fn disconnect(&mut self) {
114        self.connected = false;
115        self.url.clear();
116    }
117}
118
119#[cfg(target_arch = "wasm32")]
120impl Default for WasmBrowserConnector {
121    fn default() -> Self {
122        Self::new()
123    }
124}
125
126#[cfg(test)]
127mod tests {
128    use super::*;
129
130    #[test]
131    fn test_config_defaults() {
132        assert_eq!(config::DEFAULT_TIMEOUT_MS, 30_000);
133        assert_eq!(config::DEFAULT_VIEWPORT_WIDTH, 1920);
134        assert_eq!(config::DEFAULT_VIEWPORT_HEIGHT, 1080);
135    }
136}