Skip to main content

gemini_adk_rs/text/
fn_agent.rs

1use async_trait::async_trait;
2
3use super::TextAgent;
4use crate::error::AgentError;
5use crate::state::State;
6
7/// Closure that turns the session state into a reply or an error.
8type StateFn = Box<dyn Fn(&State) -> Result<String, AgentError> + Send + Sync>;
9
10/// Zero-cost state transform agent — executes a closure, no LLM call.
11pub struct FnTextAgent {
12    name: String,
13    func: StateFn,
14}
15
16impl FnTextAgent {
17    /// Create a new function agent.
18    pub fn new(
19        name: impl Into<String>,
20        f: impl Fn(&State) -> Result<String, AgentError> + Send + Sync + 'static,
21    ) -> Self {
22        Self {
23            name: name.into(),
24            func: Box::new(f),
25        }
26    }
27}
28
29#[async_trait]
30impl TextAgent for FnTextAgent {
31    fn name(&self) -> &str {
32        &self.name
33    }
34
35    async fn run(&self, state: &State) -> Result<String, AgentError> {
36        (self.func)(state)
37    }
38}