Skip to main content

rs_adk/code_executors/
base.rs

1use super::types::{CodeExecutionInput, CodeExecutionResult};
2use async_trait::async_trait;
3
4/// Errors from code executor operations.
5#[derive(Debug, thiserror::Error)]
6pub enum CodeExecutorError {
7    /// The code execution failed at runtime.
8    #[error("Code execution failed: {0}")]
9    ExecutionFailed(String),
10    /// The requested model does not support code execution.
11    #[error("Unsupported model: {0}")]
12    UnsupportedModel(String),
13    /// The execution timed out.
14    #[error("Execution timed out after {0}s")]
15    Timeout(u64),
16    /// A catch-all for other code executor errors.
17    #[error("{0}")]
18    Other(String),
19}
20
21/// Trait for sandboxed code execution in agent pipelines.
22#[async_trait]
23pub trait CodeExecutor: Send + Sync {
24    /// Delimiters for identifying code blocks in model output.
25    fn code_block_delimiters(&self) -> Vec<(String, String)> {
26        vec![
27            ("```tool_code\n".into(), "\n```".into()),
28            ("```python\n".into(), "\n```".into()),
29        ]
30    }
31
32    /// Delimiters for wrapping execution results.
33    fn execution_result_delimiters(&self) -> (String, String) {
34        ("```tool_output\n".into(), "\n```".into())
35    }
36
37    /// Number of retry attempts on error.
38    fn error_retry_attempts(&self) -> u32 {
39        2
40    }
41
42    /// Whether this executor maintains state across executions.
43    fn stateful(&self) -> bool {
44        false
45    }
46
47    /// Execute code and return the result.
48    async fn execute_code(
49        &self,
50        input: CodeExecutionInput,
51    ) -> Result<CodeExecutionResult, CodeExecutorError>;
52}