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;
40
41/// Crate version
42pub const VERSION: &str = env!("CARGO_PKG_VERSION");
43
44/// Crate name
45pub const NAME: &str = env!("CARGO_PKG_NAME");
46
47/// Generate a unique request ID (UUID v4)
48pub fn generate_request_id() -> String {
49    uuid::Uuid::new_v4().to_string()
50}
51
52// Re-export key components for convenience
53pub use browser::controller::BrowserController;
54pub use error::Error;
55pub use extraction::content::ContentExtractor;
56pub use extraction::links::LinkExtractor;
57pub use extraction::metadata::MetadataExtractor;
58pub use mcp::McpServer;
59
60/// Configuration defaults
61pub mod config {
62    /// Default connection timeout in milliseconds
63    pub const DEFAULT_TIMEOUT_MS: u64 = 30_000;
64
65    /// Default viewport width
66    pub const DEFAULT_VIEWPORT_WIDTH: u32 = 1920;
67
68    /// Default viewport height
69    pub const DEFAULT_VIEWPORT_HEIGHT: u32 = 1080;
70}
71
72/// Browser connector state for WASM environments
73#[cfg(target_arch = "wasm32")]
74#[wasm_bindgen]
75pub struct WasmBrowserConnector {
76    connected: bool,
77    url: String,
78}
79
80#[cfg(target_arch = "wasm32")]
81#[wasm_bindgen]
82impl WasmBrowserConnector {
83    /// Create a new WASM browser connector
84    #[wasm_bindgen(constructor)]
85    pub fn new() -> Self {
86        Self {
87            connected: false,
88            url: String::new(),
89        }
90    }
91
92    /// Check if connected
93    #[wasm_bindgen(getter)]
94    pub fn connected(&self) -> bool {
95        self.connected
96    }
97
98    /// Get current URL
99    #[wasm_bindgen(getter)]
100    pub fn url(&self) -> String {
101        self.url.clone()
102    }
103
104    /// Connect to a URL
105    pub fn connect(&mut self, url: &str) -> bool {
106        self.url = url.to_string();
107        self.connected = true;
108        true
109    }
110
111    /// Disconnect
112    pub fn disconnect(&mut self) {
113        self.connected = false;
114        self.url.clear();
115    }
116}
117
118#[cfg(target_arch = "wasm32")]
119impl Default for WasmBrowserConnector {
120    fn default() -> Self {
121        Self::new()
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    #[test]
130    fn test_config_defaults() {
131        assert_eq!(config::DEFAULT_TIMEOUT_MS, 30_000);
132        assert_eq!(config::DEFAULT_VIEWPORT_WIDTH, 1920);
133        assert_eq!(config::DEFAULT_VIEWPORT_HEIGHT, 1080);
134    }
135}