rsclaw 0.0.1-alpha.1

rsclaw: High-performance AI agent (BETA). Optimized for M4 Max and 2GB VPS. 100% compatible with openclaw
Documentation
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Arc;

/// Hook event types.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum HookEvent {
    SystemStart,
    SystemStop,
    ConfigLoaded,
    GatewayStart,
    GatewayStop,
    AgentCreate,
    AgentChat,
    AgentDestroy,
    SkillLoaded,
    SkillRunStart,
    SkillRunEnd,
    CronJobExecute,
    MessageReceived,
    MessageSent,
    Custom(Arc<str>),
}

impl std::fmt::Display for HookEvent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            HookEvent::SystemStart => write!(f, "system_start"),
            HookEvent::SystemStop => write!(f, "system_stop"),
            HookEvent::ConfigLoaded => write!(f, "config_loaded"),
            HookEvent::GatewayStart => write!(f, "gateway_start"),
            HookEvent::GatewayStop => write!(f, "gateway_stop"),
            HookEvent::AgentCreate => write!(f, "agent_create"),
            HookEvent::AgentChat => write!(f, "agent_chat"),
            HookEvent::AgentDestroy => write!(f, "agent_destroy"),
            HookEvent::SkillLoaded => write!(f, "skill_loaded"),
            HookEvent::SkillRunStart => write!(f, "skill_run_start"),
            HookEvent::SkillRunEnd => write!(f, "skill_run_end"),
            HookEvent::CronJobExecute => write!(f, "cron_job_execute"),
            HookEvent::MessageReceived => write!(f, "message_received"),
            HookEvent::MessageSent => write!(f, "message_sent"),
            HookEvent::Custom(name) => write!(f, "{}", name),
        }
    }
}

impl HookEvent {
    /// Parse event from string.
    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "system_start" => Some(HookEvent::SystemStart),
            "system_stop" => Some(HookEvent::SystemStop),
            "config_loaded" => Some(HookEvent::ConfigLoaded),
            "gateway_start" => Some(HookEvent::GatewayStart),
            "gateway_stop" => Some(HookEvent::GatewayStop),
            "agent_create" => Some(HookEvent::AgentCreate),
            "agent_chat" => Some(HookEvent::AgentChat),
            "agent_destroy" => Some(HookEvent::AgentDestroy),
            "skill_loaded" => Some(HookEvent::SkillLoaded),
            "skill_run_start" => Some(HookEvent::SkillRunStart),
            "skill_run_end" => Some(HookEvent::SkillRunEnd),
            "cron_job_execute" => Some(HookEvent::CronJobExecute),
            "message_received" => Some(HookEvent::MessageReceived),
            "message_sent" => Some(HookEvent::MessageSent),
            _ => Some(HookEvent::Custom(Arc::from(s))),
        }
    }
}

/// Hook priority (0-255, lower = higher priority).
pub type HookPriority = u8;

/// Hook execution mode.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum HookMode {
    Sync,
    Async,
}

/// Hook definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Hook {
    pub id: Arc<str>,
    pub event: HookEvent,
    pub name: Arc<str>,
    pub priority: HookPriority,
    pub mode: HookMode,
    pub enabled: bool,
    pub action: HookAction,
}

/// Hook action type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HookAction {
    Log(Arc<str>),
    Command(Arc<str>),
    Skill(Arc<str>),
    Agent(Arc<str>),
    None,
}

impl Hook {
    /// Create a new hook.
    pub fn new(
        event: HookEvent,
        name: Arc<str>,
        priority: HookPriority,
        action: HookAction,
    ) -> Self {
        Self {
            id: Arc::from(uuid::Uuid::new_v4().to_string()),
            event,
            name,
            priority,
            mode: HookMode::Sync,
            enabled: true,
            action,
        }
    }
}

/// Event context passed to hooks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HookContext {
    pub event: HookEvent,
    pub timestamp: u64,
    pub data: Value,
}

impl HookContext {
    /// Create a new hook context.
    pub fn new(event: HookEvent, data: Value) -> Self {
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            event,
            timestamp,
            data,
        }
    }
}

/// Hook execution result.
#[derive(Debug, Clone)]
pub struct HookResult {
    pub hook_id: Arc<str>,
    pub success: bool,
    pub error: Option<Arc<str>>,
    pub duration_ms: u64,
}