use std::time::Duration;
use crate::tools::tool::ToolError;
#[derive(Debug, Clone)]
pub struct SandboxConfig {
pub max_execution_time: Duration,
pub max_memory_bytes: u64,
pub allowed_hosts: Vec<String>,
pub allowed_paths: Vec<String>,
pub env_vars: Vec<(String, String)>,
}
impl Default for SandboxConfig {
fn default() -> Self {
Self {
max_execution_time: Duration::from_secs(30),
max_memory_bytes: 128 * 1024 * 1024, allowed_hosts: vec![],
allowed_paths: vec![],
env_vars: vec![],
}
}
}
#[derive(Debug)]
pub struct SandboxResult {
pub stdout: String,
pub stderr: String,
pub exit_code: i32,
pub duration: Duration,
pub memory_used: Option<u64>,
}
pub struct ToolSandbox {
#[allow(dead_code)] config: SandboxConfig,
}
impl ToolSandbox {
pub fn new(config: SandboxConfig) -> Self {
Self { config }
}
pub async fn execute(
&self,
code: &str,
language: &str,
input: &str,
) -> Result<SandboxResult, ToolError> {
match language {
"python" => self.execute_python(code, input).await,
"javascript" | "js" => self.execute_javascript(code, input).await,
_ => Err(ToolError::Sandbox(format!(
"Unsupported language: {}",
language
))),
}
}
async fn execute_python(&self, _code: &str, _input: &str) -> Result<SandboxResult, ToolError> {
Err(ToolError::Sandbox(
"Python sandbox execution not yet implemented".to_string(),
))
}
async fn execute_javascript(
&self,
_code: &str,
_input: &str,
) -> Result<SandboxResult, ToolError> {
Err(ToolError::Sandbox(
"JavaScript sandbox execution not yet implemented".to_string(),
))
}
pub fn is_available() -> bool {
false
}
}
impl Default for ToolSandbox {
fn default() -> Self {
Self::new(SandboxConfig::default())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sandbox_config_default() {
let config = SandboxConfig::default();
assert_eq!(config.max_execution_time, Duration::from_secs(30));
assert!(config.allowed_hosts.is_empty());
}
}