Skip to main content

rs_adk/code_executors/
built_in.rs

1use async_trait::async_trait;
2
3use crate::llm::LlmRequest;
4use crate::utils::model_name::is_gemini2_or_above;
5
6use super::base::{CodeExecutor, CodeExecutorError};
7use super::types::{CodeExecutionInput, CodeExecutionResult};
8
9/// Server-side code executor (Gemini 2.0+). Does not run code locally.
10/// Adds `{codeExecution: {}}` to the LLM request tools.
11pub struct BuiltInCodeExecutor;
12
13impl BuiltInCodeExecutor {
14    /// Add code execution capability to the LLM request.
15    /// Returns error if model is not Gemini 2.0+.
16    pub fn process_llm_request(
17        &self,
18        request: &mut LlmRequest,
19        model: &str,
20    ) -> Result<(), CodeExecutorError> {
21        if !is_gemini2_or_above(model) {
22            return Err(CodeExecutorError::UnsupportedModel(format!(
23                "Built-in code execution requires Gemini 2.0+, got: {}",
24                model
25            )));
26        }
27        request
28            .tools
29            .push(rs_genai::prelude::Tool::code_execution());
30        Ok(())
31    }
32}
33
34#[async_trait]
35impl CodeExecutor for BuiltInCodeExecutor {
36    async fn execute_code(
37        &self,
38        _input: CodeExecutionInput,
39    ) -> Result<CodeExecutionResult, CodeExecutorError> {
40        // Server-side execution — no local result needed
41        Ok(CodeExecutionResult::empty())
42    }
43}