Skip to main content

Sandbox

Trait Sandbox 

Source
pub trait Sandbox:
    Send
    + Sync
    + 'static {
    // Required methods
    fn execute_streaming(
        &self,
        command: &str,
        options: ExecuteOptions,
    ) -> ExecutionStream;
    fn execute_code_streaming(
        &self,
        code: &str,
        language: &str,
        options: ExecuteOptions,
    ) -> ExecutionStream;
    fn read_file<'life0, 'life1, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn write_file<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
        content: &'life2 [u8],
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn remove_file<'life0, 'life1, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn list_files<'life0, 'life1, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<FileInfo>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn get_tools(&self) -> Vec<Arc<dyn Tool>> { ... }
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 self,
        command: &'life1 str,
        options: ExecuteOptions,
    ) -> Pin<Box<dyn Future<Output = Result<ExecutionResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn execute_code<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        code: &'life1 str,
        language: &'life2 str,
        options: ExecuteOptions,
    ) -> Pin<Box<dyn Future<Output = Result<ExecutionResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn read_text<'life0, 'life1, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn write_text<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
        content: &'life2 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
}
Expand description

An execution environment: where tools run commands and code and read and write files (Sandbox).

Several tools share one sandbox, so they see the same working directory and filesystem. The streaming methods are the primitives; execute and execute_code consume them and return the final result.

Implement PosixShellSandbox instead when the environment only offers a POSIX shell: it supplies everything except execute_streaming.

use futures::stream;
use strands::sandbox::{ExecuteOptions, ExecutionResult, ExecutionStream, FileInfo, Sandbox};
use strands::{Error, Result, async_trait};

struct Echo;

#[async_trait]
impl Sandbox for Echo {
    fn execute_streaming(&self, command: &str, _options: ExecuteOptions) -> ExecutionStream {
        let result = ExecutionResult::new(0, format!("{command}\n"), "");
        Box::pin(stream::iter([Ok(result.into())]))
    }

    fn execute_code_streaming(&self, code: &str, _language: &str, options: ExecuteOptions) -> ExecutionStream {
        self.execute_streaming(code, options)
    }

    async fn read_file(&self, path: &str) -> Result<Vec<u8>> {
        Err(strands::SandboxPathNotFoundError::new(path).into())
    }

    async fn write_file(&self, _path: &str, _content: &[u8]) -> Result<()> {
        Err(Error::other("read-only"))
    }

    async fn remove_file(&self, _path: &str) -> Result<()> {
        Err(Error::other("read-only"))
    }

    async fn list_files(&self, _path: &str) -> Result<Vec<FileInfo>> {
        Ok(Vec::new())
    }
}

let result = Echo.execute("hello", ExecuteOptions::default()).await?;
assert_eq!(result.stdout, "hello\n");

Required Methods§

Source

fn execute_streaming( &self, command: &str, options: ExecuteOptions, ) -> ExecutionStream

Runs a shell command, streaming its output.

Yields a StreamChunk for each piece of stdout and stderr as it arrives, then one ExecutionResult with the exit code and the complete output. A non-zero exit code is a result, not an error.

§Errors

The stream ends with SandboxTimeoutError when options.timeout elapses, SandboxAbortError when options.signal fires, and another error when the command cannot be started or an environment variable name is invalid.

Source

fn execute_code_streaming( &self, code: &str, language: &str, options: ExecuteOptions, ) -> ExecutionStream

Runs source code through a language interpreter such as python3 or node, streaming its output.

§Errors

As execute_streaming; the stream also fails when language does not match LANGUAGE_PATTERN. A missing interpreter is a result with exit code 127.

Source

fn read_file<'life0, 'life1, 'async_trait>( &'life0 self, path: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Reads a file as raw bytes. See read_text for text.

§Errors

SandboxPathNotFoundError when the path does not exist, SandboxIsADirectoryError when it is a directory, SandboxPermissionError when it cannot be read, and another error for any other failure.

Source

fn write_file<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, path: &'life1 str, content: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Writes raw bytes to a file, creating missing parent directories.

§Errors

When the file cannot be written.

Source

fn remove_file<'life0, 'life1, 'async_trait>( &'life0 self, path: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Removes a file.

§Errors

When the file does not exist or cannot be removed.

Source

fn list_files<'life0, 'life1, 'async_trait>( &'life0 self, path: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<FileInfo>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Lists a directory.

§Errors

SandboxPathNotFoundError when the directory does not exist or a path component is a file, and another error for any other failure.

Provided Methods§

Source

fn get_tools(&self) -> Vec<Arc<dyn Tool>>

Tools this sandbox vends to an agent. The agent registers them at initialization, skipping any whose name the user already registered. None by default.

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, command: &'life1 str, options: ExecuteOptions, ) -> Pin<Box<dyn Future<Output = Result<ExecutionResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Runs a shell command and returns its result (execute), consuming execute_streaming.

§Errors

As execute_streaming, and when the stream ends without a result.

Source

fn execute_code<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, code: &'life1 str, language: &'life2 str, options: ExecuteOptions, ) -> Pin<Box<dyn Future<Output = Result<ExecutionResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Runs source code through an interpreter and returns its result, consuming execute_code_streaming.

§Errors

As execute_code_streaming, and when the stream ends without a result.

Source

fn read_text<'life0, 'life1, 'async_trait>( &'life0 self, path: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Reads a file as UTF-8 text, replacing invalid sequences with U+FFFD and dropping a leading byte order mark.

§Errors

As read_file.

Source

fn write_text<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, path: &'life1 str, content: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Writes text to a file as UTF-8.

§Errors

As write_file.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§