browsr_types/
agent.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4/// Agent-level configuration for enabling the shared browser runtime
5#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
6#[serde(default, deny_unknown_fields)]
7pub struct BrowserAgentConfig {
8    /// Whether the orchestrator should eagerly initialize the browser
9    pub enabled: bool,
10    /// Persist and restore session cookies/state between runs
11    pub persist_session: bool,
12    /// Optional runtime overrides for the Chromium driver
13    #[serde(skip_serializing_if = "Option::is_none", flatten)]
14    pub runtime: Option<DistriBrowserConfig>,
15}
16
17impl Default for BrowserAgentConfig {
18    fn default() -> Self {
19        Self {
20            enabled: false,
21            persist_session: false,
22            runtime: None,
23        }
24    }
25}
26
27impl BrowserAgentConfig {
28    pub fn is_enabled(&self) -> bool {
29        self.enabled
30    }
31
32    pub fn should_persist_session(&self) -> bool {
33        self.persist_session
34    }
35
36    pub fn runtime_config(&self) -> DistriBrowserConfig {
37        self.runtime.clone().unwrap_or_default()
38    }
39}
40
41/// Supported proxy options for the embedded Chromium browser
42#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
43#[serde(tag = "kind", content = "address", rename_all = "snake_case")]
44pub enum BrowserProxy {
45    /// Route traffic through an HTTPS proxy at the provided host:port
46    Https(String),
47    /// Route traffic through a SOCKS5 proxy at the provided host:port
48    Socks5(String),
49}
50
51/// Chromium driver configuration shared across orchestrator + CLI
52#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
53#[serde(default, deny_unknown_fields)]
54pub struct DistriBrowserConfig {
55    /// Preferred viewport size (defaults to 1920x1080)
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub window_size: Option<(u32, u32)>,
58    /// Whether to run Chromium in headless mode
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub headless: Option<bool>,
61    /// Enable stealth adjustments to reduce detection heuristics
62    pub enable_stealth_mode: bool,
63    /// Enable Spider's "real" emulation for fonts, media, etc.
64    pub enable_real_emulation: bool,
65    /// Optional proxy information passed to Chromium
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub proxy: Option<BrowserProxy>,
68}
69
70impl Default for DistriBrowserConfig {
71    fn default() -> Self {
72        Self {
73            window_size: None,
74            headless: Some(true),
75            enable_stealth_mode: true,
76            enable_real_emulation: false,
77            proxy: None,
78        }
79    }
80}
81
82/// Serializable cookie representation used for session persistence
83#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
84#[serde(deny_unknown_fields)]
85pub struct BrowserCookie {
86    pub name: String,
87    pub value: String,
88    #[serde(default, skip_serializing_if = "Option::is_none")]
89    pub domain: Option<String>,
90    #[serde(default, skip_serializing_if = "Option::is_none")]
91    pub path: Option<String>,
92    #[serde(default, skip_serializing_if = "Option::is_none")]
93    pub expires: Option<f64>,
94    #[serde(default)]
95    pub http_only: bool,
96    #[serde(default)]
97    pub secure: bool,
98    #[serde(default, skip_serializing_if = "Option::is_none")]
99    pub same_site: Option<String>,
100    #[serde(default, skip_serializing_if = "Option::is_none")]
101    pub priority: Option<String>,
102    #[serde(default, skip_serializing_if = "Option::is_none")]
103    pub source_scheme: Option<String>,
104}
105
106/// Captured browser state (cookies + last URL)
107#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
108pub struct BrowserSessionState {
109    pub cookies: Vec<BrowserCookie>,
110    #[serde(default, skip_serializing_if = "Option::is_none")]
111    pub last_url: Option<String>,
112    #[schemars(with = "String")]
113    pub captured_at: chrono::DateTime<chrono::Utc>,
114}
115
116/// Persisted browser session entry keyed by user id
117#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
118pub struct BrowserSessionRecord {
119    pub user_id: String,
120    pub state: BrowserSessionState,
121    #[schemars(with = "String")]
122    pub updated_at: chrono::DateTime<chrono::Utc>,
123}