browser_use/tools/
tab_list.rs1use crate::error::Result;
2use crate::tools::{Tool, ToolContext, ToolResult};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
8pub struct TabInfo {
9 pub index: usize,
11 pub active: bool,
13 pub title: String,
15 pub url: String,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
21pub struct TabListParams {}
22
23#[derive(Default)]
25pub struct TabListTool;
26
27impl Tool for TabListTool {
28 type Params = TabListParams;
29
30 fn name(&self) -> &str {
31 "tab_list"
32 }
33
34 fn execute_typed(&self, _params: TabListParams, context: &mut ToolContext) -> Result<ToolResult> {
35 let tabs = context.session.get_tabs()?;
37 let active_tab = context.session.tab();
38
39 let mut tab_list = Vec::new();
41 for (index, tab) in tabs.iter().enumerate() {
42 let is_active = std::sync::Arc::ptr_eq(tab, active_tab);
44
45 let title = tab.get_title().unwrap_or_default();
47
48 let url = tab.get_url();
50
51 tab_list.push(TabInfo {
52 index,
53 active: is_active,
54 title,
55 url,
56 });
57 }
58
59 let active_index = tab_list.iter().position(|t| t.active).unwrap_or(0);
61 let active_info = &tab_list[active_index];
62
63 let summary = if !tab_list.is_empty() {
64 let all_tabs_str = tab_list
65 .iter()
66 .map(|tab| format!("[{}] Title: {} (URL: {})", tab.index, tab.title, tab.url))
67 .collect::<Vec<_>>()
68 .join("\n");
69
70 format!(
71 "Current Tab: [{}] {}\nAll Tabs:\n{}",
72 active_index, active_info.title, all_tabs_str
73 )
74 } else {
75 "No tabs available".to_string()
76 };
77
78 Ok(ToolResult::success_with(serde_json::json!({
79 "tab_list": tab_list,
80 "count": tab_list.len(),
81 "summary": summary
82 })))
83 }
84}