browser_use/tools/
new_tab.rs

1use crate::error::Result;
2use crate::tools::snapshot::{RenderMode, render_aria_tree};
3use crate::tools::utils::normalize_url;
4use crate::tools::{Tool, ToolContext, ToolResult};
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// Parameters for the new_tab tool
9#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
10pub struct NewTabParams {
11    /// URL to open in the new tab
12    pub url: String,
13}
14
15/// Tool for opening a new tab
16#[derive(Default)]
17pub struct NewTabTool;
18
19impl Tool for NewTabTool {
20    type Params = NewTabParams;
21
22    fn name(&self) -> &str {
23        "new_tab"
24    }
25
26    fn execute_typed(&self, params: NewTabParams, context: &mut ToolContext) -> Result<ToolResult> {
27        let normalized_url = normalize_url(&params.url);
28        let tab = context.session.browser().new_tab().map_err(|e| {
29            crate::error::BrowserError::TabOperationFailed(format!("Failed to create tab: {}", e))
30        })?;
31
32        // Navigate to the normalized URL
33        tab.navigate_to(&normalized_url).map_err(|e| {
34            crate::error::BrowserError::NavigationFailed(format!(
35                "Failed to navigate to {}: {}",
36                normalized_url, e
37            ))
38        })?;
39
40        // Wait for navigation to complete
41        tab.wait_until_navigated().map_err(|e| {
42            crate::error::BrowserError::NavigationFailed(format!(
43                "Navigation to {} did not complete: {}",
44                normalized_url, e
45            ))
46        })?;
47
48        // Bring the new tab to front
49        tab.activate().map_err(|e| {
50            crate::error::BrowserError::TabOperationFailed(format!("Failed to activate tab: {}", e))
51        })?;
52
53        let snapshot = {
54            let dom = context.get_dom()?;
55            render_aria_tree(&dom.root, RenderMode::Ai, None)
56        };
57
58        Ok(ToolResult::success_with(serde_json::json!({
59            "snapshot": snapshot
60        })))
61    }
62}