1use 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
9pub struct WebDriverComputer {
28 viewport: Viewport,
29 display: (u32, u32),
30}
31
32impl WebDriverComputer {
33 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 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}