Skip to main content

strands/sandbox/
types.rs

1//! Data types for sandbox operations: execution results, file metadata and streaming chunks.
2
3/// Which output stream a [`StreamChunk`] came from (`StreamType`).
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[non_exhaustive]
6pub enum StreamType {
7    /// Standard output (`'stdout'`).
8    Stdout,
9    /// Standard error (`'stderr'`).
10    Stderr,
11}
12
13impl StreamType {
14    /// The stream's name: `"stdout"` or `"stderr"`.
15    #[must_use]
16    pub fn as_str(self) -> &'static str {
17        match self {
18            Self::Stdout => "stdout",
19            Self::Stderr => "stderr",
20        }
21    }
22}
23
24/// A chunk of output from a running command or code execution (`StreamChunk`).
25#[derive(Debug, Clone, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct StreamChunk {
28    /// The decoded text.
29    pub data: String,
30    /// The stream the text was written to.
31    pub stream_type: StreamType,
32}
33
34impl StreamChunk {
35    /// Creates a chunk of `data` written to `stream_type`.
36    pub fn new(data: impl Into<String>, stream_type: StreamType) -> Self {
37        Self {
38            data: data.into(),
39            stream_type,
40        }
41    }
42}
43
44/// Metadata about a file or directory in a sandbox (`FileInfo`).
45///
46/// `is_dir` and `size` are `None` when the backend cannot determine them.
47#[derive(Debug, Clone, PartialEq, Eq)]
48#[non_exhaustive]
49pub struct FileInfo {
50    /// The entry name, without its directory.
51    pub name: String,
52    /// Whether the entry is a directory.
53    pub is_dir: Option<bool>,
54    /// The size in bytes.
55    pub size: Option<u64>,
56}
57
58impl FileInfo {
59    /// Creates an entry with unknown type and size.
60    pub fn new(name: impl Into<String>) -> Self {
61        Self {
62            name: name.into(),
63            is_dir: None,
64            size: None,
65        }
66    }
67
68    /// Sets whether the entry is a directory.
69    #[must_use]
70    pub fn with_is_dir(mut self, is_dir: bool) -> Self {
71        self.is_dir = Some(is_dir);
72        self
73    }
74
75    /// Sets the size in bytes.
76    #[must_use]
77    pub fn with_size(mut self, size: u64) -> Self {
78        self.size = Some(size);
79        self
80    }
81}
82
83/// A file produced by code execution, such as a chart or a PDF (`OutputFile`).
84///
85/// Shell-based sandboxes return none; Jupyter- or API-backed sandboxes can.
86#[derive(Debug, Clone, PartialEq, Eq)]
87#[non_exhaustive]
88pub struct OutputFile {
89    /// The file name.
90    pub name: String,
91    /// The raw content.
92    pub content: Vec<u8>,
93    /// The MIME type, for example `image/png`.
94    pub mime_type: String,
95}
96
97impl OutputFile {
98    /// Creates an output file.
99    pub fn new(name: impl Into<String>, content: impl Into<Vec<u8>>, mime_type: impl Into<String>) -> Self {
100        Self {
101            name: name.into(),
102            content: content.into(),
103            mime_type: mime_type.into(),
104        }
105    }
106}
107
108/// The result of a command or code execution (`ExecutionResult`).
109#[derive(Debug, Clone, PartialEq, Eq)]
110#[non_exhaustive]
111pub struct ExecutionResult {
112    /// The exit code; `0` is success. A missing command or interpreter is `127`, and a process
113    /// killed by a signal is `128` plus the signal number.
114    pub exit_code: i32,
115    /// Everything written to standard output.
116    pub stdout: String,
117    /// Everything written to standard error.
118    pub stderr: String,
119    /// Files the execution produced.
120    pub output_files: Vec<OutputFile>,
121}
122
123impl ExecutionResult {
124    /// Creates a result with no output files.
125    pub fn new(exit_code: i32, stdout: impl Into<String>, stderr: impl Into<String>) -> Self {
126        Self {
127            exit_code,
128            stdout: stdout.into(),
129            stderr: stderr.into(),
130            output_files: Vec::new(),
131        }
132    }
133
134    /// Sets the files the execution produced.
135    #[must_use]
136    pub fn with_output_files(mut self, output_files: Vec<OutputFile>) -> Self {
137        self.output_files = output_files;
138        self
139    }
140}
141
142/// An item of an execution stream: a [`StreamChunk`] or the final [`ExecutionResult`].
143///
144/// A stream yields chunks as output arrives and ends with exactly one
145/// [`ExecutionResult`](ExecutionStreamItem::ExecutionResult), or with an error.
146#[derive(Debug, Clone, PartialEq, Eq)]
147#[non_exhaustive]
148pub enum ExecutionStreamItem {
149    /// Output written while the command runs (`type: 'streamChunk'`).
150    StreamChunk(StreamChunk),
151    /// The final result (`type: 'executionResult'`).
152    ExecutionResult(ExecutionResult),
153}
154
155impl From<StreamChunk> for ExecutionStreamItem {
156    fn from(chunk: StreamChunk) -> Self {
157        Self::StreamChunk(chunk)
158    }
159}
160
161impl From<ExecutionResult> for ExecutionStreamItem {
162    fn from(result: ExecutionResult) -> Self {
163        Self::ExecutionResult(result)
164    }
165}