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 ClipboardGetDriver;
#[async_trait::async_trait]
impl Driver for ClipboardGetDriver {
fn name(&self) -> &str {
"os_clipboard_get"
}
fn description(&self) -> &str {
"Get text content from system clipboard"
}
fn usage_hint(&self) -> &str {
"Use this skill when the user wants to retrieve text that was copied to clipboard"
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "os_clipboard_get",
"parameters": {}
}));
}
fn example_output(&self) -> String {
return "Text content from clipboard".to_string();
}
fn category(&self) -> DriverCategory {
return DriverCategory::OperatingSystemBasis;
}
async fn execute(
&self,
_parameters: &HashMap<String, Value>,
_callback: Option<&dyn DriverCallback>,
_context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing os_clipboard_get driver");
#[cfg(target_os = "macos")]
{
debug!("Getting clipboard content on macOS");
let result = exec_async("pbpaste", &[], None)
.await
.map_err(|e| DriverError::execution(format!("Failed to get clipboard content on macOS: {}", e)))?;
if result.success {
info!("Clipboard content retrieved successfully on macOS");
return Ok(result.stdout);
} else {
return Err(DriverError::execution(format!("Failed to get clipboard content: {}", result.stderr)));
}
}
#[cfg(target_os = "linux")]
{
debug!("Getting clipboard content on Linux");
let result = exec_async("xclip", &["-selection", "clipboard", "-o"], None).await;
if let Ok(r) = result {
if r.success {
info!("Clipboard content retrieved successfully using xclip on Linux");
return Ok(r.stdout);
}
}
debug!("Trying xsel as fallback on Linux");
let result = exec_async("xsel", &["--clipboard", "--output"], None)
.await
.map_err(|e| DriverError::execution(format!("Failed to get clipboard content on Linux: {}", e)))?;
if result.success {
info!("Clipboard content retrieved successfully using xsel on Linux");
return Ok(result.stdout);
} else {
return Err(DriverError::execution(format!("Failed to get clipboard content. Install xclip or xsel: {}", result.stderr)));
}
}
#[cfg(target_os = "windows")]
{
debug!("Getting clipboard content on Windows");
use clipboard_win::get_clipboard_string;
let text = get_clipboard_string().map_err(|e| DriverError::execution(format!("Failed to get clipboard content on Windows: {:?}", e)))?;
info!("Clipboard content retrieved successfully on Windows");
return Ok(text);
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
debug!("Clipboard operation not supported on this platform");
return Err(DriverError::execution("Clipboard operation not supported on this platform"));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_os_clipboard_get_metadata() {
let driver = ClipboardGetDriver;
assert_eq!(driver.name(), "os_clipboard_get");
assert_eq!(driver.category(), DriverCategory::OperatingSystemBasis);
}
}