use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VmExecutionConfig {
pub enabled: bool,
pub api_base_url: String,
pub vm_pool_size: u32,
pub default_vm_type: String,
pub execution_timeout_ms: u64,
pub allowed_languages: Vec<String>,
pub auto_provision: bool,
pub code_validation: bool,
pub max_code_length: usize,
#[serde(default)]
pub history: HistoryConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryConfig {
pub enabled: bool,
pub snapshot_on_execution: bool,
pub snapshot_on_failure: bool,
pub auto_rollback_on_failure: bool,
pub max_history_entries: usize,
pub persist_history: bool,
pub integration_mode: String,
}
impl Default for HistoryConfig {
fn default() -> Self {
Self {
enabled: true,
snapshot_on_execution: false,
snapshot_on_failure: true,
auto_rollback_on_failure: false,
max_history_entries: 100,
persist_history: true,
integration_mode: "http".to_string(),
}
}
}
impl Default for VmExecutionConfig {
fn default() -> Self {
Self {
enabled: false,
api_base_url: "http://localhost:8080".to_string(),
vm_pool_size: 1,
default_vm_type: "focal-optimized".to_string(),
execution_timeout_ms: 30000,
allowed_languages: vec![
"python".to_string(),
"javascript".to_string(),
"bash".to_string(),
"rust".to_string(),
],
auto_provision: true,
code_validation: true,
max_code_length: 10000,
history: HistoryConfig::default(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeBlock {
pub language: String,
pub code: String,
pub execution_confidence: f64,
pub start_pos: usize,
pub end_pos: usize,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VmExecuteRequest {
pub agent_id: String,
pub language: String,
pub code: String,
pub vm_id: Option<String>,
pub requirements: Vec<String>,
pub timeout_seconds: Option<u64>,
pub working_dir: Option<String>,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VmExecuteResponse {
pub execution_id: String,
pub vm_id: String,
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
pub duration_ms: u64,
pub started_at: DateTime<Utc>,
pub completed_at: DateTime<Utc>,
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParseExecuteRequest {
pub agent_id: String,
pub llm_response: String,
pub auto_execute: bool,
pub auto_execute_threshold: f64,
pub vm_config: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParseExecuteResponse {
pub code_blocks: Vec<CodeBlock>,
pub execution_results: Vec<VmExecuteResponse>,
pub errors: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VmInstance {
pub id: String,
pub name: String,
pub vm_type: String,
pub status: String,
pub ip_address: Option<String>,
pub created_at: DateTime<Utc>,
pub last_activity: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VmPoolResponse {
pub agent_id: String,
pub available_vms: Vec<VmInstance>,
pub in_use_vms: Vec<VmInstance>,
pub pool_config: VmExecutionConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionIntent {
pub confidence: f64,
pub trigger_keywords: Vec<String>,
pub context_clues: Vec<String>,
pub suggested_action: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LanguageConfig {
pub name: String,
pub extension: String,
pub execute_command: String,
pub common_packages: Vec<String>,
pub restrictions: Vec<String>,
pub timeout_multiplier: f64,
}
impl Default for LanguageConfig {
fn default() -> Self {
Self {
name: "unknown".to_string(),
extension: "txt".to_string(),
execute_command: "cat".to_string(),
common_packages: vec![],
restrictions: vec![],
timeout_multiplier: 1.0,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandHistoryEntry {
pub id: String,
pub vm_id: String,
pub agent_id: String,
pub command: String,
pub language: String,
pub snapshot_id: Option<String>,
pub success: bool,
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
pub executed_at: DateTime<Utc>,
pub duration_ms: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryQueryRequest {
pub vm_id: String,
pub agent_id: Option<String>,
pub limit: Option<usize>,
pub failures_only: bool,
pub start_date: Option<DateTime<Utc>>,
pub end_date: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryQueryResponse {
pub vm_id: String,
pub entries: Vec<CommandHistoryEntry>,
pub total: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RollbackRequest {
pub vm_id: String,
pub snapshot_id: String,
pub create_pre_rollback_snapshot: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RollbackResponse {
pub vm_id: String,
pub restored_snapshot_id: String,
pub pre_rollback_snapshot_id: Option<String>,
pub rolled_back_at: DateTime<Utc>,
pub success: bool,
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum VmExecutionError {
VmNotAvailable(String),
ValidationFailed(String),
Timeout(u64),
UnsupportedLanguage(String),
ApiError(String),
Internal(String),
HistoryError(String),
SnapshotNotFound(String),
RollbackFailed(String),
SessionNotFound(String),
ConnectionError(String),
ConfigError(String),
ExecutionFailed(String),
SnapshotFailed(String),
}
impl std::fmt::Display for VmExecutionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VmExecutionError::VmNotAvailable(vm_id) => write!(f, "VM not available: {}", vm_id),
VmExecutionError::ValidationFailed(reason) => {
write!(f, "Code validation failed: {}", reason)
}
VmExecutionError::Timeout(duration) => {
write!(f, "Execution timeout after {}ms", duration)
}
VmExecutionError::UnsupportedLanguage(lang) => {
write!(f, "Language not supported: {}", lang)
}
VmExecutionError::ApiError(msg) => write!(f, "API error: {}", msg),
VmExecutionError::Internal(msg) => write!(f, "Internal error: {}", msg),
VmExecutionError::HistoryError(msg) => write!(f, "History error: {}", msg),
VmExecutionError::SnapshotNotFound(id) => write!(f, "Snapshot not found: {}", id),
VmExecutionError::RollbackFailed(msg) => write!(f, "Rollback failed: {}", msg),
VmExecutionError::SessionNotFound(id) => write!(f, "Session not found: {}", id),
VmExecutionError::ConnectionError(msg) => write!(f, "Connection error: {}", msg),
VmExecutionError::ConfigError(msg) => write!(f, "Configuration error: {}", msg),
VmExecutionError::ExecutionFailed(msg) => write!(f, "Execution failed: {}", msg),
VmExecutionError::SnapshotFailed(msg) => write!(f, "Snapshot creation failed: {}", msg),
}
}
}
impl std::error::Error for VmExecutionError {}