use crate::{
DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult,
types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info};
#[derive(Debug)]
pub struct OsGetTimeDriver;
#[async_trait::async_trait]
impl Driver for OsGetTimeDriver {
fn name(&self) -> &str {
return "os_get_time";
}
fn description(&self) -> &str {
return "Get current system time and timezone";
}
fn usage_hint(&self) -> &str {
return "Use this skill to check the current date, time, and timezone";
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![DriverParameter {
name: "format".to_string(),
param_type: "string".to_string(),
description: "Output format: full, date, time, timestamp".to_string(),
required: false,
default: Some(json!("full")),
example: Some(json!("date")),
enum_values: Some(vec!["full".to_string(), "date".to_string(), "time".to_string(), "timestamp".to_string()]),
}];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "os_get_time"
}));
}
fn example_output(&self) -> String {
return "Current time: 2024-01-15 14:30:45 (UTC+8)".to_string();
}
fn category(&self) -> DriverCategory {
return DriverCategory::Time;
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
_callback: Option<&dyn DriverCallback>,
_context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing os_get_time driver");
let format = parameters.get("format").and_then(|v| v.as_str()).unwrap_or("full");
let now = chrono::Local::now();
let tz = now.offset();
debug!("Getting system time, format: {}", format);
let result = match format {
"date" => format!("Current date: {}", now.format("%Y-%m-%d")),
"time" => format!("Current time: {}", now.format("%H:%M:%S")),
"timestamp" => format!("Unix timestamp: {}", now.timestamp()),
_ => format!("Current time: {} ({})", now.format("%Y-%m-%d %H:%M:%S"), tz),
};
info!("{}", result);
return Ok(result);
}
fn validate(&self, _parameters: &HashMap<String, Value>) -> DriverResult<()> {
return Ok(());
}
}
#[derive(Debug)]
pub struct OsSetTimeDriver;
#[async_trait::async_trait]
impl Driver for OsSetTimeDriver {
fn name(&self) -> &str {
return "os_set_time";
}
fn description(&self) -> &str {
return "Set system time (requires administrator privileges)";
}
fn usage_hint(&self) -> &str {
return "Use this skill to adjust system time";
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![DriverParameter {
name: "datetime".to_string(),
param_type: "string".to_string(),
description: "New datetime in format 'YYYY-MM-DD HH:MM:SS'".to_string(),
required: true,
default: None,
example: Some(json!("2024-01-15 12:00:00")),
enum_values: None,
}];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "os_set_time",
"parameters": {
"datetime": "2024-01-15 10:00:00"
}
}));
}
fn example_output(&self) -> String {
return "System time updated successfully".to_string();
}
fn category(&self) -> DriverCategory {
return DriverCategory::Time;
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
_callback: Option<&dyn DriverCallback>,
_context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing os_set_time driver");
let datetime = parameters.get("datetime").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("datetime"))?;
debug!("Setting system time to: {}", datetime);
#[cfg(target_os = "windows")]
{
use crate::exec_async;
exec_async("powershell", &["-Command", &format!("Set-Date -Date '{}'", datetime)], None)
.await
.map_err(|e| DriverError::execution(format!("Failed to set system time: {}", e)))?;
}
#[cfg(not(target_os = "windows"))]
{
use crate::exec_async;
exec_async("sudo", &["date", "-s", datetime], None)
.await
.map_err(|e| DriverError::execution(format!("Failed to set system time: {}", e)))?;
}
let result = "System time updated successfully".to_string();
info!("{}", result);
return Ok(result);
}
fn validate(&self, parameters: &HashMap<String, Value>) -> DriverResult<()> {
parameters.get("datetime").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("datetime"))?;
return Ok(());
}
}