use crate::error::Result;
use crate::tools::snapshot::{RenderMode, render_aria_tree};
use crate::tools::utils::normalize_url;
use crate::tools::{Tool, ToolContext, ToolResult};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct NavigateParams {
pub url: String,
#[serde(default = "default_wait")]
pub wait_for_load: bool,
}
fn default_wait() -> bool {
true
}
#[derive(Default)]
pub struct NavigateTool;
impl Tool for NavigateTool {
type Params = NavigateParams;
fn name(&self) -> &str {
"navigate"
}
fn execute_typed(
&self,
params: NavigateParams,
context: &mut ToolContext,
) -> Result<ToolResult> {
let normalized_url = normalize_url(¶ms.url);
context.session.navigate(&normalized_url)?;
if params.wait_for_load {
context.session.wait_for_navigation()?;
}
let snapshot = {
let dom = context.get_dom()?;
render_aria_tree(&dom.root, RenderMode::Ai, None)
};
Ok(ToolResult::success_with(serde_json::json!({
"snapshot": snapshot
})))
}
}