1use async_trait::async_trait;
2
3use super::TextAgent;
4use crate::error::AgentError;
5use crate::state::State;
6
7pub struct FnTextAgent {
9 name: String,
10 #[allow(clippy::type_complexity)]
11 func: Box<dyn Fn(&State) -> Result<String, AgentError> + Send + Sync>,
12}
13
14impl FnTextAgent {
15 pub fn new(
17 name: impl Into<String>,
18 f: impl Fn(&State) -> Result<String, AgentError> + Send + Sync + 'static,
19 ) -> Self {
20 Self {
21 name: name.into(),
22 func: Box::new(f),
23 }
24 }
25}
26
27#[async_trait]
28impl TextAgent for FnTextAgent {
29 fn name(&self) -> &str {
30 &self.name
31 }
32
33 async fn run(&self, state: &State) -> Result<String, AgentError> {
34 (self.func)(state)
35 }
36}