use crate::error::Result;
use crate::tools::utils::validate_navigation_url;
use crate::tools::{
DocumentEnvelopeOptions, Tool, ToolContext, ToolResult, build_document_envelope,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct NewTabParams {
pub url: String,
#[serde(default)]
pub allow_unsafe: bool,
}
#[derive(Default)]
pub struct NewTabTool;
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct NewTabOutput {
#[serde(flatten)]
pub envelope: crate::tools::DocumentEnvelope,
pub action: String,
pub url: String,
pub message: String,
}
impl Tool for NewTabTool {
type Params = NewTabParams;
type Output = NewTabOutput;
fn name(&self) -> &str {
"new_tab"
}
fn execute_typed(&self, params: NewTabParams, context: &mut ToolContext) -> Result<ToolResult> {
let normalized_url = validate_navigation_url(¶ms.url, params.allow_unsafe)?;
context.session.open_tab(&normalized_url)?;
context.invalidate_dom();
Ok(ToolResult::success_with(NewTabOutput {
envelope: build_document_envelope(context, None, DocumentEnvelopeOptions::minimal())?,
action: "new_tab".to_string(),
message: format!("Opened a new tab for {}", normalized_url),
url: normalized_url,
}))
}
}