Skip to main content

adk_browser/
computer.rs

1//! A browser viewport as a [`ComputerActuator`].
2
3use crate::session::BrowserSession;
4use crate::viewport::{self, Viewport};
5use adk_core::{ActuationOutput, ComputerAction, ComputerActionKind, ComputerActuator, Result};
6use async_trait::async_trait;
7use std::sync::Arc;
8
9/// A [`ComputerActuator`] whose display is a WebDriver browser viewport.
10///
11/// Screenshots are the viewport, resized when needed to fit Claude's image limits, and every
12/// coordinate is in their pixel space; the actuator maps it to CSS pixels, device pixel ratio
13/// included. It supports every action, `zoom` included. Run the browser headless, in an
14/// isolated profile, behind the network controls the task needs.
15///
16/// # Example
17///
18/// ```rust,no_run
19/// use std::sync::Arc;
20/// use adk_browser::{BrowserConfig, BrowserSession, WebDriverComputer};
21/// use adk_core::ComputerActuator;
22///
23/// let browser = Arc::new(BrowserSession::new(BrowserConfig::new().viewport(1280, 800)));
24/// let computer = WebDriverComputer::new(browser);
25/// assert_eq!(computer.display_size(), Some((1280, 800)));
26/// ```
27pub struct WebDriverComputer {
28    viewport: Viewport,
29    display: (u32, u32),
30}
31
32impl WebDriverComputer {
33    /// The actuator over `browser`'s viewport, which starts on the first action.
34    pub fn new(browser: Arc<BrowserSession>) -> Self {
35        let config = browser.config();
36        let display = viewport::fit(config.viewport_width, config.viewport_height);
37        Self { viewport: Viewport::new(browser), display }
38    }
39
40    /// The browser session the actuator drives.
41    pub fn session(&self) -> &Arc<BrowserSession> {
42        self.viewport.session()
43    }
44}
45
46#[async_trait]
47impl ComputerActuator for WebDriverComputer {
48    fn display_size(&self) -> Option<(u32, u32)> {
49        Some(self.display)
50    }
51
52    fn supports(&self, _kind: ComputerActionKind) -> bool {
53        true
54    }
55
56    async fn perform(&self, action: ComputerAction) -> Result<ActuationOutput> {
57        let viewport = &self.viewport;
58        match action {
59            ComputerAction::Screenshot => {
60                return Ok(ActuationOutput::Image(viewport.screenshot().await?));
61            }
62            ComputerAction::Zoom { region } => {
63                return Ok(ActuationOutput::Image(viewport.zoom(region).await?));
64            }
65            ComputerAction::CursorPosition => {
66                return Ok(ActuationOutput::Cursor(viewport.cursor()));
67            }
68            ComputerAction::Click { button, count, at, modifiers } => {
69                viewport.click(at, button, count, &viewport::modifiers(&modifiers)?).await?;
70            }
71            ComputerAction::Drag { from, to, modifiers } => {
72                viewport.drag(from, to, &viewport::modifiers(&modifiers)?).await?;
73            }
74            ComputerAction::MoveTo { at } => viewport.move_to(at).await?,
75            ComputerAction::MouseDown => viewport.button(None, true).await?,
76            ComputerAction::MouseUp => viewport.button(None, false).await?,
77            ComputerAction::Scroll { at, direction, amount, modifiers: _ } => {
78                viewport.scroll(at, direction, amount).await?;
79            }
80            ComputerAction::Type { text } => viewport.type_text(&text).await?,
81            ComputerAction::Key { keys, repeat } => viewport.press(&keys, repeat).await?,
82            ComputerAction::HoldKey { keys, duration } => viewport.hold(&keys, duration).await?,
83            ComputerAction::Wait { duration } => tokio::time::sleep(duration).await,
84            other => {
85                return Err(adk_core::AdkError::tool(format!(
86                    "Error: the browser computer does not perform {:?}",
87                    other.kind()
88                )));
89            }
90        }
91        Ok(ActuationOutput::Done)
92    }
93}