hippox-drivers 0.4.2

🦛All indivisible atomic driver units in Hippox.
//! Image resize driver module
//!
//! This module provides functionality to resize images to specified dimensions
//! with aspect ratio preservation support and multiple resampling filters.
use crate::{
    DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult, file_exists,
    types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info};
/// Driver for resizing images
#[derive(Debug)]
pub struct ImageResizeDriver;
#[async_trait::async_trait]
impl Driver for ImageResizeDriver {
    /// Returns the unique name of this driver
    fn name(&self) -> &str {
        return "image_resize";
    }
    /// Returns a brief description of the driver's functionality
    fn description(&self) -> &str {
        return "Resize an image to specified width and height";
    }
    /// Returns detailed usage guidance for LLMs
    fn usage_hint(&self) -> &str {
        return "Use this skill when you need to change the dimensions of an image. \
        Supports maintaining aspect ratio with the 'preserve_aspect' parameter.";
    }
    /// Returns the parameter definitions for this driver
    fn parameters(&self) -> Vec<DriverParameter> {
        return vec![
            DriverParameter {
                name: "source".to_string(),
                param_type: "string".to_string(),
                description: "Source image file path".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("/path/to/input.jpg".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "destination".to_string(),
                param_type: "string".to_string(),
                description: "Destination file path".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("/path/to/output.jpg".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "width".to_string(),
                param_type: "integer".to_string(),
                description: "Target width in pixels".to_string(),
                required: true,
                default: None,
                example: Some(Value::Number(800.into())),
                enum_values: None,
            },
            DriverParameter {
                name: "height".to_string(),
                param_type: "integer".to_string(),
                description: "Target height in pixels".to_string(),
                required: true,
                default: None,
                example: Some(Value::Number(600.into())),
                enum_values: None,
            },
            DriverParameter {
                name: "preserve_aspect".to_string(),
                param_type: "boolean".to_string(),
                description: "Whether to preserve the original aspect ratio".to_string(),
                required: false,
                default: Some(Value::Bool(true)),
                example: Some(Value::Bool(false)),
                enum_values: None,
            },
            DriverParameter {
                name: "filter".to_string(),
                param_type: "string".to_string(),
                description: "Resampling filter (nearest, triangle, catmullrom, gaussian, lanczos3)".to_string(),
                required: false,
                default: Some(Value::String("lanczos3".to_string())),
                example: Some(Value::String("gaussian".to_string())),
                enum_values: Some(vec![
                    "nearest".to_string(),
                    "triangle".to_string(),
                    "catmullrom".to_string(),
                    "gaussian".to_string(),
                    "lanczos3".to_string(),
                ]),
            },
        ];
    }
    /// Returns an example call for this driver
    fn example_call(&self) -> DriverResult<Value> {
        return Ok(json!({
            "action": "image_resize",
            "parameters": {
                "source": "/photos/original.jpg",
                "destination": "/photos/thumbnail.jpg",
                "width": 300,
                "height": 300,
                "preserve_aspect": true
            }
        }));
    }
    /// Returns an example output from this driver
    fn example_output(&self) -> String {
        return "Successfully resized image from 1920x1080 to 300x225".to_string();
    }
    /// Returns the category of this driver
    fn category(&self) -> DriverCategory {
        return DriverCategory::Media;
    }
    /// Executes the driver with the given parameters
    async fn execute(
        &self,
        parameters: &HashMap<String, Value>,
        callback: Option<&dyn DriverCallback>,
        context: Option<&DriverContext>,
    ) -> DriverResult<String> {
        debug!("Executing image_resize driver");
        let task_id = context.as_ref().and_then(|c| c.task_id()).map(String::from);
        let driver_index = context.as_ref().and_then(|c| c.driver_index());
        let step_name = context.as_ref().and_then(|c| c.driver_name()).map(String::from);
        // Notify callback of start
        if let Some(cb) = callback {
            cb.on_start(task_id.clone(), driver_index, step_name);
            cb.on_log(task_id.clone(), driver_index, Some("Starting image resize operation".to_string()));
            cb.on_progress(task_id.clone(), driver_index, Some(10), None);
        }
        // Extract required parameters
        let source = parameters.get("source").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("source"))?;
        let destination = parameters.get("destination").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("destination"))?;
        let width = parameters.get("width").and_then(|v| v.as_u64()).ok_or_else(|| DriverError::missing_parameter("width"))? as u32;
        let height = parameters.get("height").and_then(|v| v.as_u64()).ok_or_else(|| DriverError::missing_parameter("height"))? as u32;
        let preserve_aspect = parameters.get("preserve_aspect").and_then(|v| v.as_bool()).unwrap_or(true);
        let filter_name = parameters.get("filter").and_then(|v| v.as_str()).unwrap_or("lanczos3");
        debug!("Resizing image: source={}, target={}x{}, preserve_aspect={}", source, width, height, preserve_aspect);
        if let Some(cb) = callback {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!(
                    "Source: {}, destination: {}, width: {}, height: {}, preserve_aspect: {}, filter: {}",
                    source, destination, width, height, preserve_aspect, filter_name
                )),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(20), None);
        }
        // Validate source file exists
        if !file_exists(source) {
            return Err(DriverError::execution(format!("Source image not found: {}", source)));
        }
        if let Some(cb) = callback {
            cb.on_log(task_id.clone(), driver_index, Some(format!("Source file verified: {}", source)));
            cb.on_progress(task_id.clone(), driver_index, Some(30), None);
        }
        // Open image
        use image::GenericImageView;
        let img = image::open(source).map_err(|e| DriverError::execution(format!("Failed to open image '{}': {}", source, e)))?;
        let (orig_w, orig_h) = img.dimensions();
        if let Some(cb) = callback {
            cb.on_log(task_id.clone(), driver_index, Some(format!("Original dimensions: {}x{}", orig_w, orig_h)));
            cb.on_progress(task_id.clone(), driver_index, Some(40), None);
        }
        // Calculate new dimensions
        let (new_w, new_h) = if preserve_aspect {
            let ratio = orig_w as f32 / orig_h as f32;
            let target_ratio = width as f32 / height as f32;
            if ratio > target_ratio {
                let new_w = width;
                let new_h = (width as f32 / ratio).round() as u32;
                (new_w, new_h.max(1))
            } else {
                let new_h = height;
                let new_w = (height as f32 * ratio).round() as u32;
                (new_w.max(1), new_h)
            }
        } else {
            (width, height)
        };
        if let Some(cb) = callback {
            cb.on_log(task_id.clone(), driver_index, Some(format!("Calculated new dimensions: {}x{}", new_w, new_h)));
            cb.on_progress(task_id.clone(), driver_index, Some(60), None);
        }
        // Select filter
        let filter = match filter_name {
            "nearest" => image::imageops::FilterType::Nearest,
            "triangle" => image::imageops::FilterType::Triangle,
            "catmullrom" => image::imageops::FilterType::CatmullRom,
            "gaussian" => image::imageops::FilterType::Gaussian,
            _ => image::imageops::FilterType::Lanczos3,
        };
        if let Some(cb) = callback {
            cb.on_log(task_id.clone(), driver_index, Some("Resizing image...".to_string()));
            cb.on_progress(task_id.clone(), driver_index, Some(70), None);
        }
        // Resize and save
        let resized = img.resize(new_w, new_h, filter);
        if let Some(cb) = callback {
            cb.on_log(task_id.clone(), driver_index, Some("Saving resized image...".to_string()));
            cb.on_progress(task_id.clone(), driver_index, Some(85), None);
        }
        resized.save(destination).map_err(|e| DriverError::execution(format!("Failed to save: {}", e)))?;
        let result = format!("Successfully resized image from {}x{} to {}x{}", orig_w, orig_h, new_w, new_h);
        if let Some(cb) = callback {
            cb.on_log(task_id.clone(), driver_index, Some(format!("Result: {}", result)));
            cb.on_progress(task_id.clone(), driver_index, Some(100), None);
            cb.on_complete(task_id.clone(), driver_index, Some("image_resize".to_string()), Some(result.clone()));
        }
        info!("Image resize completed: {}", result);
        return Ok(result);
    }
    /// Validates the parameters before execution
    fn validate(&self, parameters: &HashMap<String, Value>) -> DriverResult<()> {
        parameters.get("source").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("source"))?;
        parameters.get("destination").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("destination"))?;
        parameters.get("width").and_then(|v| v.as_u64()).ok_or_else(|| DriverError::missing_parameter("width"))?;
        parameters.get("height").and_then(|v| v.as_u64()).ok_or_else(|| DriverError::missing_parameter("height"))?;
        return Ok(());
    }
}