use std::sync::Arc;
use facet::Facet;
use rig::tool::ToolDyn;
use crate::core::{Module, Signature};
use crate::modules::react::ReAct;
use crate::{BamlType, PredictError, Predicted};
#[derive(Facet)]
#[facet(crate = facet)]
pub struct Agent<S>
where
S: Signature,
S::Input: BamlType + Clone,
S::Output: BamlType,
{
react: ReAct<S>,
}
impl<S> Agent<S>
where
S: Signature,
S::Input: BamlType + Clone,
S::Output: BamlType,
{
pub fn new(tools: Vec<Arc<dyn ToolDyn>>, max_iters: usize) -> Self {
let react = ReAct::<S>::builder()
.with_tools(tools)
.max_steps(max_iters.max(1))
.build();
Self { react }
}
pub fn from_react(react: ReAct<S>) -> Self {
Self { react }
}
pub fn into_react(self) -> ReAct<S> {
self.react
}
}
impl<S> Module for Agent<S>
where
S: Signature,
S::Input: BamlType + Clone + for<'a> Facet<'a> + Send + Sync,
S::Output: BamlType + for<'a> Facet<'a> + Send + Sync,
{
type Input = S::Input;
type Output = S::Output;
async fn forward(&self, input: Self::Input) -> Result<Predicted<Self::Output>, PredictError> {
self.react.forward(input).await
}
}