Skip to main content

adk_browser/
lib.rs

1//! # adk-browser
2#![allow(clippy::result_large_err)]
3//!
4//! Browser automation tools for ADK agents using WebDriver (via thirtyfour).
5//!
6//! ## Overview
7//!
8//! This crate provides browser automation capabilities as ADK tools, allowing
9//! LLM agents to interact with web pages. Tools are designed to work with any
10//! LlmAgent and inherit all ADK benefits (callbacks, session management, etc.).
11//!
12//! ## Quick Start
13//!
14//! ```rust,no_run
15//! use adk_browser::{BrowserConfig, BrowserProfile, BrowserSession, BrowserToolset};
16//! use std::sync::Arc;
17//!
18//! # async fn example() -> adk_core::Result<()> {
19//! // Create a browser session; it starts on first use
20//! let config = BrowserConfig::new().headless(true).viewport(1920, 1080);
21//! let browser = Arc::new(BrowserSession::new(config));
22//!
23//! // Create a toolset and register it with `LlmAgentBuilder::toolset`
24//! let toolset = BrowserToolset::with_profile(browser.clone(), BrowserProfile::Scraping);
25//! assert_eq!(toolset.all_tools().len(), 14);
26//!
27//! // Clean up
28//! browser.stop().await?;
29//! # Ok(())
30//! # }
31//! ```
32//!
33//! `BrowserToolset::new` offers 45 tools. `browser_evaluate_js` and `browser_file_upload` are
34//! opt-in, navigation accepts only `http` and `https`, and private network addresses are refused.
35//!
36//! ## Available Tools
37//!
38//! ### Navigation
39//! - `browser_navigate` - Navigate to a URL (`http`/`https` unless
40//!   `BrowserToolset::with_allowed_schemes` says otherwise; private network
41//!   addresses, including redirect targets, refused unless
42//!   `BrowserToolset::with_private_network_access` permits them; host names that
43//!   do not resolve on the agent host refused unless
44//!   `BrowserToolset::with_unresolved_hosts` permits them)
45//! - `browser_back` - Go back in history
46//! - `browser_forward` - Go forward in history
47//! - `browser_refresh` - Refresh the page
48//!
49//! The history tools check the page they land on against the same private
50//! network policy.
51//!
52//! ### Interaction
53//! - `browser_click` - Click on an element
54//! - `browser_double_click` - Double-click an element
55//! - `browser_type` - Type text into an input
56//! - `browser_clear` - Clear an input field
57//! - `browser_select` - Select from a dropdown
58//!
59//! ### Extraction
60//! - `browser_extract_text` - Get text from elements
61//! - `browser_extract_attribute` - Get attribute values
62//! - `browser_extract_links` - Get all links on page
63//! - `browser_page_info` - Get current URL and title
64//! - `browser_page_source` - Get HTML source
65//! - `browser_elements` - List interactive elements with rectangles in CSS pixels and a
66//!   stable selector each, for set-of-marks grounding: the chosen mark becomes a
67//!   `browser_click` by selector
68//!
69//! ### Screenshots
70//! - `browser_screenshot` - Capture page or element screenshot
71//!
72//! ### Waiting
73//! - `browser_wait_for_element` - Wait for element to appear
74//! - `browser_wait` - Wait for a duration
75//! - `browser_wait_for_page_load` - Wait for page to load
76//! - `browser_wait_for_text` - Wait for text to appear
77//!
78//! ### JavaScript
79//! - `browser_evaluate_js` - Execute JavaScript code (opt-in via
80//!   `BrowserToolset::with_evaluate_js(true)`)
81//! - `browser_scroll` - Scroll the page
82//! - `browser_hover` - Hover over an element
83//! - `browser_handle_alert` - Handle JavaScript alerts
84//!
85//! ### Cookies
86//! - `browser_get_cookies` - Get all cookies
87//! - `browser_get_cookie` - Get a specific cookie
88//! - `browser_add_cookie` - Add a cookie
89//! - `browser_delete_cookie` - Delete a cookie
90//! - `browser_delete_all_cookies` - Delete all cookies
91//!
92//! ### Windows/Tabs
93//! - `browser_list_windows` - List all windows/tabs
94//! - `browser_new_tab` - Open a new tab
95//! - `browser_new_window` - Open a new window
96//! - `browser_switch_window` - Switch to a window
97//! - `browser_close_window` - Close current window
98//! - `browser_maximize_window` - Maximize window
99//! - `browser_minimize_window` - Minimize window
100//! - `browser_set_window_size` - Set window size
101//!
102//! ### Frames
103//! - `browser_switch_to_frame` - Switch to an iframe
104//! - `browser_switch_to_parent_frame` - Exit current iframe
105//! - `browser_switch_to_default_content` - Exit all iframes
106//!
107//! ### Advanced Actions
108//! - `browser_drag_and_drop` - Drag and drop elements
109//! - `browser_right_click` - Right-click (context menu)
110//! - `browser_focus` - Focus on an element
111//! - `browser_element_state` - Check element state
112//! - `browser_press_key` - Press keyboard keys
113//! - `browser_file_upload` - Upload files from allowed directories (opt-in via
114//!   `BrowserToolset::with_file_upload(roots)`)
115//! - `browser_print_to_pdf` - Print page to PDF
116//!
117//! ## Requirements
118//!
119//! A WebDriver server (like ChromeDriver, geckodriver, or Selenium) must be
120//! running and accessible. By default, tools connect to `http://localhost:4444`.
121//!
122//! ### Starting ChromeDriver
123//!
124//! ```bash
125//! # Install ChromeDriver (macOS)
126//! brew install chromedriver
127//!
128//! # Start ChromeDriver
129//! chromedriver --port=4444
130//! ```
131//!
132//! ### Using Docker
133//!
134//! ```bash
135//! docker run -d -p 4444:4444 selenium/standalone-chrome
136//! ```
137//!
138//! ## Architecture
139//!
140//! Tools are implemented using the ADK `Tool` trait, allowing them to:
141//! - Work with any LLM model (Gemini, OpenAI, Anthropic)
142//! - Use callbacks for monitoring and control
143//! - Access session state and artifacts
144//! - Compose with other tools and agents
145//!
146//! ```text
147//! ┌─────────────────────────────────────────────────┐
148//! │                   LlmAgent                       │
149//! │  (with callbacks, session, artifacts, memory)   │
150//! └─────────────────────────────────────────────────┘
151//!                        │
152//!                        ▼
153//! ┌─────────────────────────────────────────────────┐
154//! │               BrowserToolset                     │
155//! │  NavigateTool, ClickTool, TypeTool, ...         │
156//! └─────────────────────────────────────────────────┘
157//!                        │
158//!                        ▼
159//! ┌─────────────────────────────────────────────────┐
160//! │              BrowserSession                      │
161//! │         (wraps thirtyfour WebDriver)            │
162//! └─────────────────────────────────────────────────┘
163//!                        │
164//!                        ▼
165//!              WebDriver Server
166//!           (ChromeDriver, etc.)
167//! ```
168
169mod browser_use;
170mod computer;
171mod config;
172mod escape;
173pub mod frames;
174mod pool;
175mod session;
176pub mod tools;
177mod toolset;
178mod viewport;
179
180// Re-export main types
181pub use browser_use::{
182    BROWSER_BATCH_HALT_TEXT, BROWSER_TOOLSET, BrowserUseMember, BrowserUseToolset,
183};
184pub use computer::WebDriverComputer;
185pub use config::{BrowserConfig, BrowserType};
186pub use escape::escape_js_string;
187pub use frames::{Frame, FrameFeed, MIN_FRAME_INTERVAL};
188pub use pool::BrowserSessionPool;
189pub use session::{BrowserSession, ElementState, shared_session};
190pub use toolset::{BrowserProfile, BrowserToolset, minimal_browser_tools, readonly_browser_tools};
191
192// Re-export individual tools for selective use
193pub use tools::{
194    // Cookies
195    AddCookieTool,
196    // JavaScript
197    AlertTool,
198    // Navigation
199    BackTool,
200    // Interaction
201    ClearTool,
202    ClickTool,
203    // Windows/Tabs
204    CloseWindowTool,
205    // Navigation scheme allowlist
206    DEFAULT_ALLOWED_SCHEMES,
207    DeleteAllCookiesTool,
208    DeleteCookieTool,
209    DoubleClickTool,
210    // Advanced Actions
211    DragAndDropTool,
212    ElementStateTool,
213    // Set-of-marks grounding
214    ElementsTool,
215    EvaluateJsTool,
216    // Extraction
217    ExtractAttributeTool,
218    ExtractLinksTool,
219    ExtractTextTool,
220    FileUploadTool,
221    FocusTool,
222    ForwardTool,
223    GetCookieTool,
224    GetCookiesTool,
225    HoverTool,
226    ListWindowsTool,
227    MaximizeWindowTool,
228    MinimizeWindowTool,
229    NavigateTool,
230    NewTabTool,
231    NewWindowTool,
232    PageInfoTool,
233    PageSourceTool,
234    PressKeyTool,
235    PrintToPdfTool,
236    RefreshTool,
237    RightClickTool,
238    // Screenshots
239    ScreenshotTool,
240    ScrollTool,
241    SelectTool,
242    SetWindowSizeTool,
243    // Frames
244    SwitchToDefaultContentTool,
245    SwitchToFrameTool,
246    SwitchToParentFrameTool,
247    SwitchWindowTool,
248    TypeTool,
249    // Waiting
250    WaitForElementTool,
251    WaitForPageLoadTool,
252    WaitForTextTool,
253    WaitTool,
254};
255
256/// Prelude module for convenient imports.
257pub mod prelude {
258    pub use crate::config::{BrowserConfig, BrowserType};
259    pub use crate::pool::BrowserSessionPool;
260    pub use crate::session::{BrowserSession, shared_session};
261    pub use crate::toolset::{
262        BrowserProfile, BrowserToolset, minimal_browser_tools, readonly_browser_tools,
263    };
264}