oxicode_agent/tools/browse/
browse_tool.rs1use super::config::BrowseConfig;
7use super::engine::BrowserEngine;
8use super::helpers;
9use super::tab_guard::TabGuard;
10use crate::tools::{AgentTool, AgentToolResult, ToolContext, ToolError, ToolExecutionMode};
11use async_trait::async_trait;
12use parking_lot::Mutex;
13use serde_json::{Value, json};
14use std::sync::Arc;
15use tokio::sync::oneshot;
16
17pub struct BrowseTool {
21 engine: Arc<dyn BrowserEngine>,
22 config: BrowseConfig,
23 callbacks: super::callback_mixin::BrowseCallbacks,
25 tab_id_slot: Mutex<Arc<parking_lot::Mutex<Option<uuid::Uuid>>>>,
29}
30
31impl BrowseTool {
32 pub fn new(engine: Arc<dyn BrowserEngine>) -> Self {
34 Self {
35 engine,
36 config: BrowseConfig::default(),
37 callbacks: super::callback_mixin::BrowseCallbacks::new(),
38 tab_id_slot: Mutex::new(Arc::new(parking_lot::Mutex::new(None))),
39 }
40 }
41
42 pub fn with_config(engine: Arc<dyn BrowserEngine>, config: BrowseConfig) -> Self {
44 Self {
45 engine,
46 config,
47 callbacks: super::callback_mixin::BrowseCallbacks::new(),
48 tab_id_slot: Mutex::new(Arc::new(parking_lot::Mutex::new(None))),
49 }
50 }
51}
52
53#[async_trait]
54impl AgentTool for BrowseTool {
55 fn name(&self) -> &str {
56 "browse"
57 }
58
59 fn label(&self) -> &str {
60 "Browse"
61 }
62
63 fn description(&self) -> &str {
64 "Browse a web page with a built-in headless browser. Renders JavaScript-powered \
65 pages and returns content as markdown (default), html, or links. Use when \
66 web_search results are insufficient and you need to read the actual page content. \
67 Supports waiting for dynamic content via CSS selectors."
68 }
69
70 fn parameters_schema(&self) -> Value {
71 json!({
72 "type": "object",
73 "properties": {
74 "url": {
75 "type": "string",
76 "description": "URL to browse"
77 },
78 "format": {
79 "type": "string",
80 "enum": ["markdown", "html", "text", "links"],
81 "default": "markdown",
82 "description": "Output format: markdown (default), html, plain text, or list of links"
83 },
84 "selector": {
85 "type": "string",
86 "description": "CSS selector to extract only matching elements"
87 },
88 "wait_for": {
89 "type": "string",
90 "description": "CSS selector to wait for before extracting (for JS-rendered content)"
91 },
92 "screenshot": {
93 "type": "boolean",
94 "default": false,
95 "description": "Include a PNG screenshot as an image block"
96 }
97 },
98 "required": ["url"]
99 })
100 }
101
102 fn on_progress(&self, callback: crate::tools::ProgressCallback) {
103 self.callbacks.store_progress(callback);
104 }
105
106 fn on_browse_progress(&self, callback: Arc<dyn Fn(super::BrowseProgress) + Send + Sync>) {
107 self.callbacks.store_browse(callback);
108 }
109
110 fn execution_mode(&self) -> ToolExecutionMode {
117 ToolExecutionMode::SequentialOnly
118 }
119
120 fn current_tab_id(&self) -> Option<uuid::Uuid> {
121 *self.tab_id_slot.lock().lock()
122 }
123
124 fn set_tab_id_slot(&self, slot: Arc<parking_lot::Mutex<Option<uuid::Uuid>>>) {
125 *self.tab_id_slot.lock() = slot;
126 }
127
128 async fn execute(
129 &self,
130 _tool_call_id: &str,
131 params: Value,
132 _signal: Option<oneshot::Receiver<()>>,
133 _ctx: &ToolContext,
134 ) -> Result<AgentToolResult, ToolError> {
135 let url = params["url"]
136 .as_str()
137 .ok_or_else(|| "Missing required parameter: url".to_string())?;
138
139 let format = params["format"].as_str().unwrap_or("markdown");
140 let selector = params["selector"].as_str();
141 let wait_for = params["wait_for"].as_str();
142 let want_screenshot = params["screenshot"].as_bool().unwrap_or(false);
143
144 tracing::info!(url = %url, format = %format, "browsing page");
145
146 let raw_tab = self
148 .engine
149 .new_tab()
150 .await
151 .map_err(|e| format!("Failed to open browser tab: {}", e))?;
152
153 let tab_id = raw_tab.tab_id();
156 *self.tab_id_slot.lock().lock() = Some(tab_id);
157
158 self.callbacks.register_on_tab(raw_tab.as_ref());
160
161 let guard = TabGuard::new(raw_tab);
162 let tab = guard.tab();
163
164 let page = tab
166 .goto(url)
167 .await
168 .map_err(|e| format!("Navigation failed: {}", e))?;
169
170 if let Some(sel) = wait_for {
172 tab.wait_for(sel, self.config.default_wait_timeout_ms)
173 .await
174 .map_err(|e| format!("wait_for '{}' failed: {}", sel, e))?;
175 }
176
177 let output = match format {
179 "html" => {
180 if let Some(sel) = selector {
181 tab.query_all(sel)
182 .await
183 .map_err(|e| e.to_string())?
184 .join("\n\n")
185 } else {
186 page.html.clone()
187 }
188 }
189 "links" => {
190 let links = helpers::extract_links(tab).await?;
191 helpers::format_links(&links)
192 }
193 "text" => {
194 if let Some(sel) = selector {
195 tab.query_all(sel)
196 .await
197 .map_err(|e| e.to_string())?
198 .join("\n")
199 } else {
200 page.markdown.clone()
201 }
202 }
203 _ => {
204 if let Some(sel) = selector {
206 tab.query_all(sel)
207 .await
208 .map_err(|e| e.to_string())?
209 .join("\n\n")
210 } else {
211 page.markdown.clone()
212 }
213 }
214 };
215
216 let title = page.title.clone();
217 let final_url = page.url.clone();
218 let status = page.status;
219
220 let screenshot_blocks = if want_screenshot {
222 match tab.screenshot(self.config.screenshot_width).await {
223 Ok(png) => {
224 let b64 =
225 base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &png);
226 let img = oxicode_ai::ContentBlock::Image(oxicode_ai::ImageContent::new(
227 b64,
228 "image/png",
229 ));
230 Some(vec![img])
231 }
232 Err(e) => {
233 tracing::warn!("screenshot failed for {}: {}", final_url, e);
234 None
235 }
236 }
237 } else {
238 None
239 };
240
241 guard.close().await;
243 *self.tab_id_slot.lock().lock() = None;
244
245 let mut result = AgentToolResult::success(output).with_metadata(json!({
246 "url": final_url,
247 "title": title,
248 "status": status,
249 }));
250
251 if let Some(blocks) = screenshot_blocks {
252 result = result.with_content_blocks(blocks);
253 }
254
255 Ok(result)
256 }
257}
258
259#[cfg(test)]
260mod tests {
261 use super::*;
262 use crate::tools::browse::engine::{BrowserError, BrowserTab};
263
264 struct MockEngine;
268
269 #[async_trait]
270 impl BrowserEngine for MockEngine {
271 async fn new_tab(&self) -> Result<Box<dyn BrowserTab>, BrowserError> {
272 Err(BrowserError::Backend("MockEngine: no real browser".into()))
273 }
274
275 async fn close(&self) -> Result<(), BrowserError> {
276 Ok(())
277 }
278
279 async fn is_alive(&self) -> bool {
280 false
281 }
282 }
283
284 #[test]
285 fn browse_tool_is_sequential_only() {
286 let tool = BrowseTool::new(std::sync::Arc::new(MockEngine));
287 assert!(matches!(
288 tool.execution_mode(),
289 crate::tools::ToolExecutionMode::SequentialOnly
290 ));
291 }
292
293 #[test]
294 fn browse_tool_tab_id_slot_receives_id_from_agent_loop() {
295 let tool = BrowseTool::new(std::sync::Arc::new(MockEngine));
298
299 assert!(tool.current_tab_id().is_none());
301
302 let slot: Arc<parking_lot::Mutex<Option<uuid::Uuid>>> =
304 Arc::new(parking_lot::Mutex::new(None));
305 tool.set_tab_id_slot(Arc::clone(&slot));
306
307 let tab_id = uuid::Uuid::new_v4();
309 *slot.lock() = Some(tab_id);
310
311 assert_eq!(tool.current_tab_id(), Some(tab_id));
313
314 *slot.lock() = None;
316 assert!(tool.current_tab_id().is_none());
317 }
318}