use super::shared::*;
use crate::DriverCallback;
use crate::DriverContext;
use crate::DriverResult;
use crate::{
DriverCategory,
types::{Driver, DriverParameter},
};
use headless_chrome::protocol::cdp::Page::CaptureScreenshotFormatOption;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::fs;
use tracing::{debug, info, warn};
#[derive(Debug)]
pub struct HaveHeadBrowserScreenshotDriver;
#[async_trait::async_trait]
impl Driver for HaveHeadBrowserScreenshotDriver {
fn name(&self) -> &str {
"have_head_browser_screenshot"
}
fn description(&self) -> &str {
"Take a screenshot of the current page and save to file"
}
fn usage_hint(&self) -> &str {
"Use this skill to capture visual state of the current page"
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![
DriverParameter {
name: "path".to_string(),
param_type: "string".to_string(),
description: "File path to save screenshot (PNG format)".to_string(),
required: true,
default: None,
example: Some(Value::String("screenshot.png".to_string())),
enum_values: None,
},
DriverParameter {
name: "full_page".to_string(),
param_type: "boolean".to_string(),
description: "Capture full page (not just viewport)".to_string(),
required: false,
default: Some(Value::Bool(false)),
example: Some(Value::Bool(true)),
enum_values: None,
},
];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "have_head_browser_screenshot",
"parameters": {
"path": "./screenshot.png"
}
}));
}
fn example_output(&self) -> String {
return "Screenshot saved to ./screenshot.png".to_string();
}
fn category(&self) -> DriverCategory {
return DriverCategory::HaveHeadBrowser;
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
callback: Option<&dyn DriverCallback>,
context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing have_head_browser_screenshot driver");
let path = parameters.get("path").and_then(|v| v.as_str()).ok_or_else(|| {
debug!("Missing required parameter: path");
return crate::DriverError::missing_parameter("path");
})?;
let full_page = parameters.get("full_page").and_then(|v| v.as_bool()).unwrap_or(false);
debug!("Taking screenshot: {} (full_page: {})", path, full_page);
let tab = get_current_tab().map_err(|e| {
debug!("Failed to get current tab: {}", e);
return crate::DriverError::execution(format!("Failed to get current tab: {}", e));
})?;
let png_data = if full_page {
tab.capture_screenshot(CaptureScreenshotFormatOption::Png, None, None, true).map_err(|e| {
warn!("Failed to capture full page screenshot: {}", e);
return crate::DriverError::execution(format!("Failed to capture full page screenshot: {}", e));
})?
} else {
tab.capture_screenshot(CaptureScreenshotFormatOption::Png, None, None, false).map_err(|e| {
warn!("Failed to capture viewport screenshot: {}", e);
return crate::DriverError::execution(format!("Failed to capture viewport screenshot: {}", e));
})?
};
fs::write(path, &png_data).map_err(|e| {
warn!("Failed to save screenshot: {}", e);
return crate::DriverError::io(format!("Failed to save screenshot: {}", e));
})?;
info!("Screenshot saved to {} ({} bytes)", path, png_data.len());
return Ok(format!("Screenshot saved to {}", path));
}
}