use super::shared::*;
use crate::DriverCallback;
use crate::DriverContext;
use crate::DriverResult;
use crate::{
DriverCategory,
types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info, warn};
#[derive(Debug)]
pub struct HaveHeadBrowserTabSwitchDriver;
#[async_trait::async_trait]
impl Driver for HaveHeadBrowserTabSwitchDriver {
fn name(&self) -> &str {
"have_head_browser_tab_switch"
}
fn description(&self) -> &str {
"Switch to a different browser tab by index"
}
fn usage_hint(&self) -> &str {
"Use this skill to switch between open tabs. Index 0 is the first tab."
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![DriverParameter {
name: "index".to_string(),
param_type: "integer".to_string(),
description: "Tab index to switch to (0-based)".to_string(),
required: true,
default: None,
example: Some(Value::Number(0.into())),
enum_values: None,
}];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "have_head_browser_tab_switch",
"parameters": {
"index": 0
}
}));
}
fn example_output(&self) -> String {
return "Switched to tab 0".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_tab_switch driver");
let index = parameters.get("index").and_then(|v| v.as_u64()).ok_or_else(|| {
debug!("Missing required parameter: index (0-based)");
return crate::DriverError::missing_parameter("index");
})? as usize;
debug!("Switching to tab index: {}", index);
let browser = get_or_create_browser().map_err(|e| {
debug!("Failed to get or create browser: {}", e);
return crate::DriverError::execution(format!("Failed to get or create browser: {}", e));
})?;
let tabs_guard = browser.get_tabs();
let tabs = tabs_guard.lock().unwrap();
if index >= tabs.len() {
warn!("Tab index {} out of range ({} tabs available)", index, tabs.len());
return Err(crate::DriverError::validation("index", format!("Tab index {} out of range ({} tabs available)", index, tabs.len())));
}
let target_tab = tabs[index].clone();
set_current_tab(target_tab);
info!("Switched to tab {}", index);
return Ok(format!("Switched to tab {}", index));
}
}