tiny_agent/sandbox/
mod.rs1mod host;
2
3pub use async_trait::async_trait;
4pub use host::HostSandbox;
5use std::{fmt, sync::Arc};
6
7#[derive(Debug, Clone)]
9pub struct ExecOutput {
10 pub stdout: String,
11 pub stderr: String,
12 pub exit_code: Option<i32>,
13}
14
15impl ExecOutput {
16 pub fn is_success(&self) -> bool {
18 self.exit_code == Some(0)
19 }
20}
21
22#[derive(Debug)]
24pub enum SandboxError {
25 Io(String),
27 Unsupported,
29 Other(String),
31}
32
33impl fmt::Display for SandboxError {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 match self {
36 SandboxError::Io(e) => write!(f, "sandbox io error: {e}"),
37 SandboxError::Unsupported => {
38 write!(f, "operation not supported by this sandbox")
39 }
40 SandboxError::Other(e) => write!(f, "sandbox error: {e}"),
41 }
42 }
43}
44
45impl std::error::Error for SandboxError {}
46
47impl From<std::io::Error> for SandboxError {
48 fn from(e: std::io::Error) -> Self {
49 SandboxError::Io(e.to_string())
50 }
51}
52
53#[async_trait]
71pub trait Sandbox: Send + Sync {
72 async fn open(self: Arc<Self>, session_id: &str) -> Result<Arc<dyn Sandbox>, SandboxError>;
75
76 async fn execute(&self, command: &str) -> Result<ExecOutput, SandboxError>;
80
81 async fn save(&self) -> Result<(), SandboxError> {
84 Ok(())
85 }
86
87 async fn fork(&self, _to_session: &str) -> Result<(), SandboxError> {
89 Err(SandboxError::Unsupported)
90 }
91}