webpuppet 0.1.5-alpha

Web browser programmatic automation and control library for research, testing, and workflow automation
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
//! Configuration for webpuppet browser automation.

use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::time::Duration;

/// Main configuration for WebPuppet.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    /// Browser configuration.
    pub browser: BrowserConfig,
    /// Provider-specific settings.
    pub providers: ProvidersConfig,
    /// Session management settings.
    pub session: SessionConfig,
    /// Rate limiting settings.
    pub rate_limit: RateLimitConfig,
}

/// Browser-specific configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrowserConfig {
    /// Run browser in headless mode.
    pub headless: bool,
    /// Path to browser executable (auto-detect if None).
    pub executable_path: Option<PathBuf>,
    /// User data directory for profiles.
    pub user_data_dir: Option<PathBuf>,
    /// Browser window width.
    pub window_width: u32,
    /// Browser window height.
    pub window_height: u32,
    /// Additional browser arguments.
    pub args: Vec<String>,
    /// Request timeout.
    #[serde(with = "humantime_serde")]
    pub timeout: Duration,
    /// Enable devtools (debug mode).
    pub devtools: bool,
    /// Sandbox mode (disable for containers).
    pub sandbox: bool,
    /// Dual-head mode: launches a visible monitoring window alongside headless automation.
    pub dual_head: bool,
}

impl Default for BrowserConfig {
    fn default() -> Self {
        Self {
            headless: true,
            executable_path: None,
            user_data_dir: None,
            window_width: 1920,
            window_height: 1080,
            args: vec![
                "--disable-gpu".into(),
                "--disable-dev-shm-usage".into(),
                "--no-first-run".into(),
            ],
            timeout: Duration::from_secs(60),
            devtools: false,
            sandbox: true,
            dual_head: false,
        }
    }
}

/// Provider-specific configurations.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ProvidersConfig {
    /// Grok (X.ai) configuration.
    #[cfg(feature = "grok")]
    pub grok: GrokConfig,
    /// Claude (Anthropic) configuration.
    #[cfg(feature = "claude")]
    pub claude: ClaudeConfig,
    /// Gemini (Google) configuration.
    #[cfg(feature = "gemini")]
    pub gemini: GeminiConfig,
}

/// Grok-specific settings.
#[cfg(feature = "grok")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrokConfig {
    /// Login URL.
    pub login_url: String,
    /// Chat URL.
    pub chat_url: String,
    /// CSS selector for input field.
    pub input_selector: String,
    /// CSS selector for submit button.
    pub submit_selector: String,
    /// CSS selector for response container.
    pub response_selector: String,
    /// CSS selector to wait for page ready.
    pub ready_selector: String,
    /// CSS selector for file input (if supported).
    pub file_input_selector: Option<String>,
    /// Model variant to use.
    pub model: String,
}

#[cfg(feature = "grok")]
impl Default for GrokConfig {
    fn default() -> Self {
        Self {
            login_url: "https://x.com/i/grok".into(),
            chat_url: "https://x.com/i/grok".into(),
            input_selector: r#"textarea[data-testid="grokInput"]"#.into(),
            submit_selector: r#"button[data-testid="grokSend"]"#.into(),
            response_selector: r#"div[data-testid="grokResponse"]"#.into(),
            ready_selector: r#"textarea[data-testid="grokInput"]"#.into(),
            file_input_selector: Some(r#"input[type="file"]"#.into()),
            model: "grok-2".into(),
        }
    }
}

/// Claude-specific settings.
#[cfg(feature = "claude")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeConfig {
    /// Login URL.
    pub login_url: String,
    /// Chat URL.
    pub chat_url: String,
    /// CSS selector for input field.
    pub input_selector: String,
    /// CSS selector for submit button.
    pub submit_selector: String,
    /// CSS selector for response container.
    pub response_selector: String,
    /// CSS selector to wait for page ready.
    pub ready_selector: String,
    /// CSS selector for file input.
    pub file_input_selector: Option<String>,
    /// Organization (if applicable).
    pub organization: Option<String>,
}

#[cfg(feature = "claude")]
impl Default for ClaudeConfig {
    fn default() -> Self {
        Self {
            login_url: "https://claude.ai/login".into(),
            chat_url: "https://claude.ai/new".into(),
            input_selector: r#"div[contenteditable="true"]"#.into(),
            submit_selector: r#"button[aria-label="Send message"]"#.into(),
            response_selector: r#"div.prose"#.into(),
            ready_selector: r#"div[contenteditable="true"]"#.into(),
            file_input_selector: Some(r#"input[type="file"]"#.into()),
            organization: None,
        }
    }
}

/// Gemini-specific settings.
#[cfg(feature = "gemini")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeminiConfig {
    /// Login URL.
    pub login_url: String,
    /// Chat URL.
    pub chat_url: String,
    /// CSS selector for input field.
    pub input_selector: String,
    /// CSS selector for submit button.
    pub submit_selector: String,
    /// CSS selector for response container.
    pub response_selector: String,
    /// CSS selector to wait for page ready.
    pub ready_selector: String,
    /// CSS selector for file input.
    pub file_input_selector: Option<String>,
    /// Google account to use.
    pub google_account: Option<String>,
}

#[cfg(feature = "gemini")]
impl Default for GeminiConfig {
    fn default() -> Self {
        Self {
            login_url: "https://gemini.google.com".into(),
            chat_url: "https://gemini.google.com/app".into(),
            input_selector: r#"rich-textarea"#.into(),
            submit_selector: r#"button[aria-label="Send message"]"#.into(),
            response_selector: r#"message-content"#.into(),
            ready_selector: r#"rich-textarea"#.into(),
            file_input_selector: Some(r#"input[type="file"]"#.into()),
            google_account: None,
        }
    }
}

/// ChatGPT (OpenAI) configuration.
#[cfg(feature = "chatgpt")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatGptConfig {
    /// Login URL.
    pub login_url: String,
    /// Chat URL.
    pub chat_url: String,
    /// CSS selector for input field.
    pub input_selector: String,
    /// CSS selector for submit button.
    pub submit_selector: String,
    /// CSS selector for response container.
    pub response_selector: String,
    /// CSS selector to wait for page ready.
    pub ready_selector: String,
    /// CSS selector for file input.
    pub file_input_selector: Option<String>,
    /// Model to use (gpt-4o, gpt-4, etc).
    pub model: String,
}

#[cfg(feature = "chatgpt")]
impl Default for ChatGptConfig {
    fn default() -> Self {
        Self {
            login_url: "https://chat.openai.com".into(),
            chat_url: "https://chat.openai.com".into(),
            input_selector: r#"textarea[data-id="root"]"#.into(),
            submit_selector: r#"button[data-testid="send-button"]"#.into(),
            response_selector: r#"div[data-message-author-role="assistant"]"#.into(),
            ready_selector: r#"textarea[data-id="root"]"#.into(),
            file_input_selector: Some(r#"input[type="file"]"#.into()),
            model: "gpt-4o".into(),
        }
    }
}

/// Perplexity AI configuration.
#[cfg(feature = "perplexity")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerplexityConfig {
    /// Login URL.
    pub login_url: String,
    /// Chat URL.
    pub chat_url: String,
    /// CSS selector for input field.
    pub input_selector: String,
    /// CSS selector for submit button.
    pub submit_selector: String,
    /// CSS selector for response container.
    pub response_selector: String,
    /// CSS selector to wait for page ready.
    pub ready_selector: String,
    /// CSS selector for file input.
    pub file_input_selector: Option<String>,
}

#[cfg(feature = "perplexity")]
impl Default for PerplexityConfig {
    fn default() -> Self {
        Self {
            login_url: "https://www.perplexity.ai".into(),
            chat_url: "https://www.perplexity.ai".into(),
            input_selector: r#"textarea[placeholder*="Ask"]"#.into(),
            submit_selector: r#"button[aria-label="Submit query"]"#.into(),
            response_selector: r#"div.prose"#.into(),
            ready_selector: r#"textarea[placeholder*="Ask"]"#.into(),
            file_input_selector: Some(r#"input[type="file"]"#.into()),
        }
    }
}

/// NotebookLM (Google) configuration.
#[cfg(feature = "notebooklm")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotebookLmConfig {
    /// Login URL.
    pub login_url: String,
    /// Chat URL.
    pub chat_url: String,
    /// CSS selector for input field.
    pub input_selector: String,
    /// CSS selector for submit button.
    pub submit_selector: String,
    /// CSS selector for response container.
    pub response_selector: String,
    /// CSS selector to wait for page ready.
    pub ready_selector: String,
    /// CSS selector for file input.
    pub file_input_selector: Option<String>,
}

#[cfg(feature = "notebooklm")]
impl Default for NotebookLmConfig {
    fn default() -> Self {
        Self {
            login_url: "https://notebooklm.google.com".into(),
            chat_url: "https://notebooklm.google.com".into(),
            input_selector: r#"textarea[aria-label*="Ask"]"#.into(),
            submit_selector: r#"button[aria-label="Send"]"#.into(),
            response_selector: r#"div.response-content"#.into(),
            ready_selector: r#"textarea[aria-label*="Ask"]"#.into(),
            file_input_selector: Some(r#"button[aria-label="Add source"]"#.into()),
        }
    }
}

/// Session management configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionConfig {
    /// Directory for session storage.
    pub storage_dir: Option<PathBuf>,
    /// Session timeout before re-auth.
    #[serde(with = "humantime_serde")]
    pub timeout: Duration,
    /// Keep cookies between sessions.
    pub persist_cookies: bool,
    /// Encrypt stored session data.
    pub encrypt_storage: bool,
}

impl Default for SessionConfig {
    fn default() -> Self {
        Self {
            storage_dir: None,
            timeout: Duration::from_secs(3600 * 24), // 24 hours
            persist_cookies: true,
            encrypt_storage: true,
        }
    }
}

/// Rate limiting configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateLimitConfig {
    /// Minimum delay between requests.
    #[serde(with = "humantime_serde")]
    pub min_delay: Duration,
    /// Maximum delay between requests.
    #[serde(with = "humantime_serde")]
    pub max_delay: Duration,
    /// Requests per minute limit.
    pub requests_per_minute: u32,
    /// Add human-like delays.
    pub humanize: bool,
    /// Jitter percentage for delays (0-100).
    pub jitter_percent: u8,
}

impl Default for RateLimitConfig {
    fn default() -> Self {
        Self {
            min_delay: Duration::from_secs(2),
            max_delay: Duration::from_secs(10),
            requests_per_minute: 20,
            humanize: true,
            jitter_percent: 30,
        }
    }
}

impl Config {
    /// Load configuration from file.
    pub fn from_file(path: &std::path::Path) -> crate::Result<Self> {
        let content = std::fs::read_to_string(path)?;
        toml::from_str(&content).map_err(|e| crate::Error::Config(e.to_string()))
    }

    /// Save configuration to file.
    pub fn save(&self, path: &std::path::Path) -> crate::Result<()> {
        let content =
            toml::to_string_pretty(self).map_err(|e| crate::Error::Config(e.to_string()))?;
        std::fs::write(path, content)?;
        Ok(())
    }

    /// Create a builder for configuration.
    pub fn builder() -> ConfigBuilder {
        ConfigBuilder::default()
    }
}

/// Builder for Config.
#[derive(Debug, Default)]
pub struct ConfigBuilder {
    config: Config,
}

impl ConfigBuilder {
    /// Set headless mode.
    pub fn headless(mut self, headless: bool) -> Self {
        self.config.browser.headless = headless;
        self
    }

    /// Set browser executable path.
    pub fn executable_path(mut self, path: PathBuf) -> Self {
        self.config.browser.executable_path = Some(path);
        self
    }

    /// Set user data directory.
    pub fn user_data_dir(mut self, path: PathBuf) -> Self {
        self.config.browser.user_data_dir = Some(path);
        self
    }

    /// Set request timeout.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.config.browser.timeout = timeout;
        self
    }

    /// Enable devtools.
    pub fn devtools(mut self, enabled: bool) -> Self {
        self.config.browser.devtools = enabled;
        self
    }

    /// Disable sandbox (for containers).
    pub fn no_sandbox(mut self) -> Self {
        self.config.browser.sandbox = false;
        self.config.browser.args.push("--no-sandbox".into());
        self
    }

    /// Set session storage directory.
    pub fn session_dir(mut self, path: PathBuf) -> Self {
        self.config.session.storage_dir = Some(path);
        self
    }

    /// Set rate limit.
    pub fn rate_limit(mut self, requests_per_minute: u32) -> Self {
        self.config.rate_limit.requests_per_minute = requests_per_minute;
        self
    }

    /// Build the configuration.
    pub fn build(self) -> Config {
        self.config
    }
}