use std::{fmt, future::Future, pin::Pin, sync::Arc};
use crate::{Agent, AgentError, Message};
pub type FlowFuture<'a> =
Pin<Box<dyn Future<Output = Result<Message, AgentError>> + Send + 'a>>;
pub type FlowFn =
Arc<dyn for<'a> Fn(&'a mut Agent, String) -> FlowFuture<'a> + Send + Sync>;
#[derive(Clone)]
pub enum Flow {
Default,
Func(FlowFn)
}
impl Flow {
pub fn from_fn<F>(f: F) -> Self
where
F: for<'a> Fn(&'a mut Agent, String) -> FlowFuture<'a> + Send + Sync + 'static,
{
Flow::Func(Arc::new(f))
}
}
impl fmt::Debug for Flow {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Flow::Default => write!(f, "Simple"),
Flow::Func(_) => write!(f, "CustomFlow(<fn>)"),
}
}
}
pub trait FlowCallable: Send + Sync + 'static {
type Fut<'a>: Future<Output = Result<Message, AgentError>> + Send + 'a
where
Self: 'a;
fn call<'a>(&'a self, agent: &'a mut Agent, prompt: String) -> Self::Fut<'a>;
}