apoc 0.9.1

Agentic process control for LLMs
Documentation
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, incurs::Options)]
pub struct ExecutionStartInput {
    pub protocol_version: u16,
    pub request_id: String,
    pub argv: Vec<String>,
    #[serde(default)]
    pub shell: bool,
    pub cwd: Option<String>,
    #[serde(default)]
    pub env: Vec<String>,
    #[serde(default)]
    pub tty: bool,
    pub timeout_ms: Option<u64>,
    pub image_ref: Option<String>,
    pub vm_bundle: Option<String>,
    pub workspace: Option<String>,
    pub memory_mb: Option<u64>,
    pub cpu_count: Option<u64>,
    pub pids_max: Option<i64>,
    pub network: Option<String>,
    #[serde(default)]
    pub exports: Vec<ExecutionExport>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct ExecutionExport {
    pub source: String,
    pub destination: String,
    pub when: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct ExecutionStartOutput {
    pub provider_execution_id: String,
    pub replayed: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, incurs::Options)]
pub struct ExecutionEventsInput {
    pub execution_id: String,
    pub cursor: Option<String>,
    #[incurs(default = 0)]
    pub wait_ms: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct ExecutionEventsOutput {
    pub cursor: String,
    pub events: Vec<ExecutionEvent>,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, incurs::Options)]
pub struct ExecutionIdInput {
    pub execution_id: String,
    pub request_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, incurs::Options)]
pub struct ExecutionInputInput {
    pub execution_id: String,
    pub data_base64: String,
    pub request_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, incurs::Options)]
pub struct ExecutionSignalInput {
    pub execution_id: String,
    pub signal: String,
    pub request_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct CancelOutput {
    pub execution_id: String,
    pub canceled: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct InputOutput {
    pub execution_id: String,
    pub recipients: u64,
    pub bytes: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SignalOutput {
    pub execution_id: String,
    pub signal: String,
    pub applied: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct IsolationStatus {
    pub provider_version: String,
    pub initialized: bool,
    pub configured_image_path: Option<String>,
    pub image_path: Option<String>,
    pub image_digest: Option<String>,
    pub cpu_count: Option<u64>,
    pub memory_mb: Option<u64>,
    pub snapshot_restore_enabled: Option<bool>,
    pub snapshot_available: Option<bool>,
    pub idle_drain_seconds: Option<f64>,
    pub pool: Option<IsolationPoolStatus>,
    pub drained: Option<bool>,
}

impl IsolationStatus {
    pub fn uninitialized() -> Self {
        Self {
            provider_version: env!("CARGO_PKG_VERSION").to_string(),
            initialized: false,
            configured_image_path: std::env::var("APOC_EXECUTOR_VM_BUNDLE").ok(),
            image_path: None,
            image_digest: None,
            cpu_count: None,
            memory_mb: None,
            snapshot_restore_enabled: None,
            snapshot_available: None,
            idle_drain_seconds: None,
            pool: None,
            drained: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct IsolationPoolStatus {
    pub idle: u64,
    pub waiters: u64,
    pub in_flight: u64,
    pub live: u64,
    pub checked_out: u64,
    pub target: u64,
    pub per_vm_memory_mb: u64,
    pub max_pool_memory_mb: u64,
    pub draining: bool,
    pub shutting_down: bool,
    pub last_boot: Option<IsolationBootDiagnostics>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct IsolationBootDiagnostics {
    pub strategy: String,
    pub boot_ms: Option<u64>,
    pub warmup_ms: Option<u64>,
    pub reconnect_ms: Option<u64>,
    pub total_ms: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, incurs::Options)]
pub struct IsolationWarmupInput {
    #[incurs(default = 60000)]
    pub timeout_ms: u64,
    pub vm_bundle: Option<String>,
    pub pool_size: Option<u64>,
    pub idle_drain_seconds: Option<u64>,
    pub max_pool_memory_mb: Option<u64>,
    #[serde(default)]
    pub snapshot_restore: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct ExecutionEvent {
    #[serde(rename = "type")]
    pub kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_base64: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exit_code: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_code: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub export_index: Option<usize>,
}

impl ExecutionEvent {
    pub fn accepted(request_id: &str) -> Self {
        Self::simple("accepted", request_id)
    }

    pub fn started(request_id: &str) -> Self {
        Self::simple("started", request_id)
    }

    pub fn canceled(request_id: &str) -> Self {
        Self::simple("canceled", request_id)
    }

    pub fn error(request_id: &str, code: &str, message: impl Into<String>) -> Self {
        Self {
            kind: "error".to_string(),
            request_id: Some(request_id.to_string()),
            data: Some(message.into()),
            data_base64: None,
            exit_code: None,
            error_code: Some(code.to_string()),
            export_index: None,
        }
    }

    fn simple(kind: &str, request_id: &str) -> Self {
        Self {
            kind: kind.to_string(),
            request_id: Some(request_id.to_string()),
            data: None,
            data_base64: None,
            exit_code: None,
            error_code: None,
            export_index: None,
        }
    }

    pub fn terminal(&self) -> bool {
        matches!(self.kind.as_str(), "exit" | "error" | "canceled")
    }
}

#[derive(Debug, Clone)]
pub struct ProviderError {
    pub code: String,
    pub message: String,
}

impl ProviderError {
    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            message: message.into(),
        }
    }
}