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