Skip to main content

aither_agent/
browser_backend.rs

1//! Browser backend abstraction for interactive browser automation.
2
3use std::future::Future;
4use std::pin::Pin;
5
6/// Error returned by browser backend operations.
7#[derive(Debug, Clone)]
8pub struct BrowserBackendError {
9    message: String,
10}
11
12impl BrowserBackendError {
13    /// Create a new browser backend error.
14    #[must_use]
15    pub fn new(message: impl Into<String>) -> Self {
16        Self {
17            message: message.into(),
18        }
19    }
20}
21
22impl std::fmt::Display for BrowserBackendError {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        write!(f, "browser backend error: {}", self.message)
25    }
26}
27
28impl std::error::Error for BrowserBackendError {}
29
30/// Backend abstraction for browser automation runtimes.
31///
32/// Implementations may connect to local Chromium, remote CDP endpoints, or
33/// cloud browser providers.
34pub trait BrowserBackend: Send + Sync {
35    /// Returns a CDP endpoint URL for Playwright/browser clients.
36    fn cdp_endpoint(
37        &self,
38    ) -> Pin<Box<dyn Future<Output = Result<String, BrowserBackendError>> + Send + '_>>;
39}
40
41/// Fixed CDP backend backed by a static endpoint string.
42#[derive(Debug, Clone)]
43pub struct StaticCdpBrowser {
44    endpoint: String,
45}
46
47impl StaticCdpBrowser {
48    /// Create a static CDP backend.
49    #[must_use]
50    pub fn new(endpoint: impl Into<String>) -> Self {
51        Self {
52            endpoint: endpoint.into(),
53        }
54    }
55}
56
57impl BrowserBackend for StaticCdpBrowser {
58    fn cdp_endpoint(
59        &self,
60    ) -> Pin<Box<dyn Future<Output = Result<String, BrowserBackendError>> + Send + '_>> {
61        Box::pin(async move { Ok(self.endpoint.clone()) })
62    }
63}