use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use crate::Result;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ScreenPoint {
pub x: i64,
pub y: i64,
}
impl ScreenPoint {
pub fn new(x: i64, y: i64) -> Self {
Self { x, y }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MouseButton {
Left,
Right,
Middle,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScrollDirection {
Up,
Down,
Left,
Right,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum ComputerAction {
Screenshot,
Zoom {
region: [i64; 4],
},
Click {
button: MouseButton,
count: u8,
at: Option<ScreenPoint>,
modifiers: Vec<String>,
},
Drag {
from: ScreenPoint,
to: ScreenPoint,
modifiers: Vec<String>,
},
MoveTo {
at: ScreenPoint,
},
MouseDown,
MouseUp,
CursorPosition,
Scroll {
at: Option<ScreenPoint>,
direction: ScrollDirection,
amount: u32,
modifiers: Vec<String>,
},
Type {
text: String,
},
Key {
keys: String,
repeat: u32,
},
HoldKey {
keys: String,
duration: Duration,
},
Wait {
duration: Duration,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ComputerActionKind {
Screenshot,
Zoom,
Click,
Drag,
MoveTo,
MouseDown,
MouseUp,
CursorPosition,
Scroll,
Type,
Key,
HoldKey,
Wait,
}
impl ComputerActionKind {
pub fn is_observation(self) -> bool {
matches!(self, Self::Screenshot | Self::Zoom | Self::CursorPosition)
}
}
impl ComputerAction {
pub fn kind(&self) -> ComputerActionKind {
match self {
Self::Screenshot => ComputerActionKind::Screenshot,
Self::Zoom { .. } => ComputerActionKind::Zoom,
Self::Click { .. } => ComputerActionKind::Click,
Self::Drag { .. } => ComputerActionKind::Drag,
Self::MoveTo { .. } => ComputerActionKind::MoveTo,
Self::MouseDown => ComputerActionKind::MouseDown,
Self::MouseUp => ComputerActionKind::MouseUp,
Self::CursorPosition => ComputerActionKind::CursorPosition,
Self::Scroll { .. } => ComputerActionKind::Scroll,
Self::Type { .. } => ComputerActionKind::Type,
Self::Key { .. } => ComputerActionKind::Key,
Self::HoldKey { .. } => ComputerActionKind::HoldKey,
Self::Wait { .. } => ComputerActionKind::Wait,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Screenshot {
pub mime_type: String,
pub data: Vec<u8>,
pub width: u32,
pub height: u32,
}
impl Screenshot {
pub fn png(data: Vec<u8>) -> Option<Self> {
if data.len() < 24 || &data[..8] != b"\x89PNG\r\n\x1a\n" || &data[12..16] != b"IHDR" {
return None;
}
let width = u32::from_be_bytes([data[16], data[17], data[18], data[19]]);
let height = u32::from_be_bytes([data[20], data[21], data[22], data[23]]);
Some(Self { mime_type: "image/png".to_string(), data, width, height })
}
pub fn fits_current_claude_limits(&self) -> bool {
let tiles = self.width.div_ceil(28) as u64 * self.height.div_ceil(28) as u64;
self.width.max(self.height) <= 2_576 && tiles <= 4_784
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ActuationOutput {
Done,
Image(Screenshot),
Cursor(ScreenPoint),
}
#[async_trait]
pub trait ComputerActuator: Send + Sync {
fn display_size(&self) -> Option<(u32, u32)>;
fn supports(&self, kind: ComputerActionKind) -> bool {
kind != ComputerActionKind::Zoom
}
async fn perform(&self, action: ComputerAction) -> Result<ActuationOutput>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn png_size_comes_from_the_header() {
assert!(Screenshot::png(b"not a png".to_vec()).is_none());
let mut data = b"\x89PNG\r\n\x1a\n\0\0\0\x0dIHDR".to_vec();
data.extend_from_slice(&3024u32.to_be_bytes());
data.extend_from_slice(&1964u32.to_be_bytes());
let shot = Screenshot::png(data).unwrap();
assert_eq!((shot.width, shot.height), (3024, 1964));
assert!(!shot.fits_current_claude_limits());
let fitting = Screenshot { width: 1280, height: 800, ..shot };
assert!(fitting.fits_current_claude_limits());
}
#[test]
fn actions_serialize_with_their_kind() {
let action = ComputerAction::Scroll {
at: None,
direction: ScrollDirection::Down,
amount: 3,
modifiers: Vec::new(),
};
let json = serde_json::to_value(&action).unwrap();
assert_eq!(json["type"], "scroll");
assert_eq!(serde_json::from_value::<ComputerAction>(json).unwrap(), action);
}
}