Skip to main content

rs_adk/code_executors/
vertex_ai.rs

1//! Vertex AI code executor — runs code via Vertex AI Code Execution API.
2//!
3//! Mirrors ADK-Python's `vertex_ai_code_executor`. Executes code
4//! using the Vertex AI managed code execution service.
5
6use async_trait::async_trait;
7
8use super::base::{CodeExecutor, CodeExecutorError};
9use super::types::{CodeExecutionInput, CodeExecutionResult};
10
11/// Configuration for Vertex AI code execution.
12#[derive(Debug, Clone)]
13pub struct VertexAiCodeExecutorConfig {
14    /// Google Cloud project ID.
15    pub project: String,
16    /// Google Cloud location (e.g., "us-central1").
17    pub location: String,
18    /// Execution timeout in seconds.
19    pub timeout_secs: u64,
20}
21
22/// Code executor that runs code via the Vertex AI managed service.
23///
24/// Uses the Vertex AI Code Execution API to run Python code in
25/// a Google-managed sandboxed environment.
26#[derive(Debug, Clone)]
27pub struct VertexAiCodeExecutor {
28    config: VertexAiCodeExecutorConfig,
29}
30
31impl VertexAiCodeExecutor {
32    /// Create a new Vertex AI code executor.
33    pub fn new(config: VertexAiCodeExecutorConfig) -> Self {
34        Self { config }
35    }
36
37    /// Returns the configured project ID.
38    pub fn project(&self) -> &str {
39        &self.config.project
40    }
41
42    /// Returns the configured location.
43    pub fn location(&self) -> &str {
44        &self.config.location
45    }
46}
47
48#[async_trait]
49impl CodeExecutor for VertexAiCodeExecutor {
50    async fn execute_code(
51        &self,
52        input: CodeExecutionInput,
53    ) -> Result<CodeExecutionResult, CodeExecutorError> {
54        // In a real integration, this would call the Vertex AI Code Execution API:
55        // POST https://{location}-aiplatform.googleapis.com/v1beta1/projects/{project}/locations/{location}/codeExecutions
56        //
57        // The actual API integration requires the Google Cloud SDK and authentication.
58        let _ = &input;
59        Ok(CodeExecutionResult {
60            stdout: String::new(),
61            stderr: String::new(),
62            output_files: vec![],
63        })
64    }
65
66    fn stateful(&self) -> bool {
67        true
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    fn test_config() -> VertexAiCodeExecutorConfig {
76        VertexAiCodeExecutorConfig {
77            project: "test-project".into(),
78            location: "us-central1".into(),
79            timeout_secs: 60,
80        }
81    }
82
83    #[test]
84    fn executor_metadata() {
85        let exec = VertexAiCodeExecutor::new(test_config());
86        assert_eq!(exec.project(), "test-project");
87        assert_eq!(exec.location(), "us-central1");
88        assert!(exec.stateful());
89    }
90
91    #[tokio::test]
92    async fn execute_returns_empty_stub() {
93        let exec = VertexAiCodeExecutor::new(test_config());
94        let input = CodeExecutionInput {
95            code: "print(42)".into(),
96            input_files: vec![],
97            execution_id: None,
98        };
99        let result = exec.execute_code(input).await.unwrap();
100        assert!(result.stdout.is_empty());
101    }
102}