terraphim_agent 1.16.11

Terraphim AI Agent CLI - Command-line interface with interactive REPL and ASCII graph visualization
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! Web operations module for Terraphim TUI
//!
//! This module provides web-based operations that execute in isolated VM environments,
//! allowing safe web scraping, API interactions, and browser automation without exposing
//! the host system to potential security risks.

#![allow(dead_code)]

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Web operation types supported by the VM sandbox
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum WebOperationType {
    /// HTTP GET request to fetch web content
    HttpGet {
        url: String,
        headers: Option<HashMap<String, String>>,
    },
    /// HTTP POST request with JSON payload
    HttpPost {
        url: String,
        headers: Option<HashMap<String, String>>,
        body: String,
    },
    /// Web scraping with CSS selector
    WebScrape {
        url: String,
        selector: String,
        wait_for_element: Option<String>,
    },
    /// Screenshot capture of a web page
    Screenshot {
        url: String,
        width: Option<u32>,
        height: Option<u32>,
        full_page: Option<bool>,
    },
    /// PDF generation from web content
    PdfGeneration {
        url: String,
        page_size: Option<String>,
    },
    /// Form submission automation
    FormSubmit {
        url: String,
        form_data: HashMap<String, String>,
    },
    /// API interaction with rate limiting
    ApiInteraction {
        base_url: String,
        endpoints: Vec<String>,
        rate_limit_ms: Option<u64>,
    },
}

/// Web operation request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebOperationRequest {
    pub operation: WebOperationType,
    pub vm_id: Option<String>,
    pub timeout_ms: Option<u64>,
    pub user_agent: Option<String>,
    pub proxy: Option<ProxyConfig>,
}

/// Proxy configuration for web operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProxyConfig {
    pub host: String,
    pub port: u16,
    pub username: Option<String>,
    pub password: Option<String>,
}

/// Web operation execution result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebOperationResult {
    pub operation_id: String,
    pub vm_id: String,
    pub operation_type: String,
    pub status: OperationStatus,
    pub result_data: WebResultData,
    pub metadata: OperationMetadata,
    pub error: Option<String>,
    pub started_at: String,
    pub completed_at: String,
}

/// Operation execution status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum OperationStatus {
    Pending,
    Running,
    Completed,
    Failed,
    Timeout,
    Cancelled,
}

/// Result data specific to the operation type
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum WebResultData {
    #[serde(rename = "http_response")]
    HttpResponse {
        status_code: u16,
        status_text: String,
        headers: HashMap<String, String>,
        body: String,
        content_type: String,
        content_length: u64,
    },
    #[serde(rename = "scraped_content")]
    ScrapedContent {
        elements: Vec<ScrapedElement>,
        page_title: String,
        page_url: String,
        scrape_duration_ms: u64,
    },
    #[serde(rename = "screenshot")]
    Screenshot {
        image_data: String, // Base64 encoded
        format: String,
        width: u32,
        height: u32,
        file_size_bytes: u64,
    },
    #[serde(rename = "pdf_data")]
    PdfData {
        pdf_data: String, // Base64 encoded
        filename: String,
        page_count: u32,
        file_size_bytes: u64,
    },
    #[serde(rename = "form_result")]
    FormResult {
        response_status: u16,
        response_headers: HashMap<String, String>,
        response_body: String,
        submitted_fields: Vec<String>,
    },
    #[serde(rename = "api_results")]
    ApiResults {
        results: Vec<ApiEndpointResult>,
        total_requests: u32,
        successful_requests: u32,
        failed_requests: u32,
        total_duration_ms: u64,
    },
}

/// Scraped element from web page
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScrapedElement {
    pub selector: String,
    pub content: String,
    pub html: String,
    pub attributes: HashMap<String, String>,
    pub position: ElementPosition,
}

/// Element position on page
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ElementPosition {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
}

/// API endpoint result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiEndpointResult {
    pub endpoint: String,
    pub method: String,
    pub status_code: u16,
    pub response_time_ms: u64,
    pub response_body: String,
    pub success: bool,
}

/// Operation execution metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationMetadata {
    pub vm_id: String,
    pub user_id: Option<String>,
    pub session_id: Option<String>,
    pub execution_duration_ms: u64,
    pub network_requests_count: u32,
    pub data_transferred_bytes: u64,
    pub security_context: SecurityContext,
}

/// Security context for web operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityContext {
    pub allowed_domains: Vec<String>,
    pub blocked_domains: Vec<String>,
    pub max_response_size_mb: u32,
    pub max_execution_time_ms: u64,
    pub allow_javascript: bool,
    pub allow_cookies: bool,
    pub sandbox_enabled: bool,
}

/// Web operation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebOperationConfig {
    pub default_timeout_ms: u64,
    pub max_concurrent_operations: u32,
    pub security: SecurityContext,
    pub browser_settings: BrowserSettings,
}

/// Browser settings for web operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrowserSettings {
    pub user_agent: String,
    pub viewport_width: u32,
    pub viewport_height: u32,
    pub enable_javascript: bool,
    pub enable_images: bool,
    pub enable_css: bool,
    pub timezone: String,
}

impl Default for WebOperationConfig {
    fn default() -> Self {
        Self {
            default_timeout_ms: 30000, // 30 seconds
            max_concurrent_operations: 5,
            security: SecurityContext {
                allowed_domains: vec![
                    "https://httpbin.org".to_string(),
                    "https://jsonplaceholder.typicode.com".to_string(),
                    "https://api.github.com".to_string(),
                ],
                blocked_domains: vec!["malware.example.com".to_string()],
                max_response_size_mb: 10,
                max_execution_time_ms: 60000, // 1 minute
                allow_javascript: true,
                allow_cookies: false,
                sandbox_enabled: true,
            },
            browser_settings: BrowserSettings {
                user_agent: "Terraphim-TUI-WebBot/1.0".to_string(),
                viewport_width: 1920,
                viewport_height: 1080,
                enable_javascript: true,
                enable_images: true,
                enable_css: true,
                timezone: "UTC".to_string(),
            },
        }
    }
}

/// Builder for web operation requests
pub struct WebOperationBuilder {
    operation: WebOperationType,
    vm_id: Option<String>,
    timeout_ms: Option<u64>,
    user_agent: Option<String>,
    proxy: Option<ProxyConfig>,
}

impl WebOperationBuilder {
    pub fn new(operation: WebOperationType) -> Self {
        Self {
            operation,
            vm_id: None,
            timeout_ms: None,
            user_agent: None,
            proxy: None,
        }
    }

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

    pub fn timeout_ms(mut self, timeout_ms: u64) -> Self {
        self.timeout_ms = Some(timeout_ms);
        self
    }

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

    pub fn proxy(mut self, proxy: ProxyConfig) -> Self {
        self.proxy = Some(proxy);
        self
    }

    pub fn build(self) -> WebOperationRequest {
        WebOperationRequest {
            operation: self.operation,
            vm_id: self.vm_id,
            timeout_ms: self.timeout_ms,
            user_agent: self.user_agent,
            proxy: self.proxy,
        }
    }
}

impl WebOperationType {
    /// Create HTTP GET operation
    pub fn http_get(url: impl Into<String>) -> Self {
        Self::HttpGet {
            url: url.into(),
            headers: None,
        }
    }

    /// Create HTTP GET operation with custom headers
    pub fn http_get_with_headers(url: impl Into<String>, headers: HashMap<String, String>) -> Self {
        Self::HttpGet {
            url: url.into(),
            headers: Some(headers),
        }
    }

    /// Create HTTP POST operation
    pub fn http_post(url: impl Into<String>, body: impl Into<String>) -> Self {
        Self::HttpPost {
            url: url.into(),
            headers: None,
            body: body.into(),
        }
    }

    /// Create HTTP POST operation with custom headers
    pub fn http_post_with_headers(
        url: impl Into<String>,
        headers: HashMap<String, String>,
        body: impl Into<String>,
    ) -> Self {
        Self::HttpPost {
            url: url.into(),
            headers: Some(headers),
            body: body.into(),
        }
    }

    /// Create web scraping operation
    pub fn scrape(url: impl Into<String>, selector: impl Into<String>) -> Self {
        Self::WebScrape {
            url: url.into(),
            selector: selector.into(),
            wait_for_element: None,
        }
    }

    /// Create web scraping operation with wait condition
    pub fn scrape_with_wait(
        url: impl Into<String>,
        selector: impl Into<String>,
        wait_for_element: impl Into<String>,
    ) -> Self {
        Self::WebScrape {
            url: url.into(),
            selector: selector.into(),
            wait_for_element: Some(wait_for_element.into()),
        }
    }

    /// Create screenshot operation
    pub fn screenshot(url: impl Into<String>) -> Self {
        Self::Screenshot {
            url: url.into(),
            width: None,
            height: None,
            full_page: None,
        }
    }

    /// Create screenshot operation with custom dimensions
    pub fn screenshot_with_dimensions(url: impl Into<String>, width: u32, height: u32) -> Self {
        Self::Screenshot {
            url: url.into(),
            width: Some(width),
            height: Some(height),
            full_page: None,
        }
    }

    /// Create full page screenshot
    pub fn full_page_screenshot(url: impl Into<String>) -> Self {
        Self::Screenshot {
            url: url.into(),
            width: None,
            height: None,
            full_page: Some(true),
        }
    }

    /// Create PDF generation operation
    pub fn generate_pdf(url: impl Into<String>) -> Self {
        Self::PdfGeneration {
            url: url.into(),
            page_size: None,
        }
    }

    /// Create PDF generation with custom page size
    pub fn generate_pdf_with_page_size(
        url: impl Into<String>,
        page_size: impl Into<String>,
    ) -> Self {
        Self::PdfGeneration {
            url: url.into(),
            page_size: Some(page_size.into()),
        }
    }

    /// Create form submission operation
    pub fn submit_form(url: impl Into<String>, form_data: HashMap<String, String>) -> Self {
        Self::FormSubmit {
            url: url.into(),
            form_data,
        }
    }

    /// Create API interaction operation
    pub fn api_interaction(base_url: impl Into<String>, endpoints: Vec<impl Into<String>>) -> Self {
        Self::ApiInteraction {
            base_url: base_url.into(),
            endpoints: endpoints.into_iter().map(|e| e.into()).collect(),
            rate_limit_ms: None,
        }
    }

    /// Create API interaction with rate limiting
    pub fn api_interaction_with_rate_limit(
        base_url: impl Into<String>,
        endpoints: Vec<impl Into<String>>,
        rate_limit_ms: u64,
    ) -> Self {
        Self::ApiInteraction {
            base_url: base_url.into(),
            endpoints: endpoints.into_iter().map(|e| e.into()).collect(),
            rate_limit_ms: Some(rate_limit_ms),
        }
    }
}

/// Utility functions for web operations
pub mod utils {
    use super::*;

    /// Create common HTTP headers
    pub fn default_headers() -> HashMap<String, String> {
        let mut headers = HashMap::new();
        headers.insert("Accept".to_string(), "application/json".to_string());
        headers.insert(
            "User-Agent".to_string(),
            "Terraphim-TUI-WebBot/1.0".to_string(),
        );
        headers.insert("Accept-Language".to_string(), "en-US,en;q=0.9".to_string());
        headers
    }

    /// Create headers for form submission
    pub fn form_headers() -> HashMap<String, String> {
        let mut headers = default_headers();
        headers.insert(
            "Content-Type".to_string(),
            "application/x-www-form-urlencoded".to_string(),
        );
        headers
    }

    /// Create headers for JSON API calls
    pub fn json_headers() -> HashMap<String, String> {
        let mut headers = default_headers();
        headers.insert("Content-Type".to_string(), "application/json".to_string());
        headers
    }

    /// Validate URL format
    pub fn validate_url(url: &str) -> Result<()> {
        if url.is_empty() {
            anyhow::bail!("URL cannot be empty");
        }

        if !url.starts_with("http://") && !url.starts_with("https://") {
            anyhow::bail!("URL must start with http:// or https://");
        }

        // Basic URL validation
        let url_parser = reqwest::Url::parse(url);
        if url_parser.is_err() {
            anyhow::bail!("Invalid URL format: {}", url);
        }

        Ok(())
    }

    /// Extract domain from URL
    pub fn extract_domain(url: &str) -> Result<String> {
        let parsed_url =
            reqwest::Url::parse(url).map_err(|e| anyhow::anyhow!("Failed to parse URL: {}", e))?;

        Ok(parsed_url
            .host_str()
            .ok_or_else(|| anyhow::anyhow!("URL has no host"))?
            .to_string())
    }

    /// Generate unique operation ID
    pub fn generate_operation_id() -> String {
        use std::time::{SystemTime, UNIX_EPOCH};
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis();

        format!("webop-{}", timestamp)
    }

    /// Estimate operation complexity
    pub fn estimate_complexity(operation: &WebOperationType) -> OperationComplexity {
        match operation {
            WebOperationType::HttpGet { .. } => OperationComplexity::Low,
            WebOperationType::HttpPost { .. } => OperationComplexity::Medium,
            WebOperationType::WebScrape { .. } => OperationComplexity::High,
            WebOperationType::Screenshot { .. } => OperationComplexity::High,
            WebOperationType::PdfGeneration { .. } => OperationComplexity::High,
            WebOperationType::FormSubmit { .. } => OperationComplexity::Medium,
            WebOperationType::ApiInteraction { endpoints, .. } => {
                if endpoints.len() > 5 {
                    OperationComplexity::High
                } else if endpoints.len() > 2 {
                    OperationComplexity::Medium
                } else {
                    OperationComplexity::Low
                }
            }
        }
    }
}

/// Operation complexity estimation
#[derive(Debug, Clone, PartialEq)]
pub enum OperationComplexity {
    Low,
    Medium,
    High,
}

impl OperationComplexity {
    /// Get recommended timeout in milliseconds
    pub fn recommended_timeout_ms(&self) -> u64 {
        match self {
            OperationComplexity::Low => 10000,    // 10 seconds
            OperationComplexity::Medium => 30000, // 30 seconds
            OperationComplexity::High => 60000,   // 60 seconds
        }
    }

    /// Get recommended retry count
    pub fn recommended_retries(&self) -> u32 {
        match self {
            OperationComplexity::Low => 2,
            OperationComplexity::Medium => 3,
            OperationComplexity::High => 5,
        }
    }
}