use crate::context::{AgentContext, Context};
use crate::performers::Next;
use crate::runtime::RunAgent;
use anyhow::{Error, Result};
use async_trait::async_trait;
use crb_runtime::{InteractiveTask, ManagedContext, ReachableContext};
use std::any::type_name;
#[async_trait]
pub trait Agent: Sized + Send + 'static {
type Context: AgentContext<Self>;
fn initialize(&mut self, _ctx: &mut Context<Self>) -> Next<Self> {
self.begin()
}
fn begin(&mut self) -> Next<Self> {
Next::events()
}
fn interrupt(&mut self, ctx: &mut Context<Self>) {
ctx.shutdown();
}
async fn event(&mut self, ctx: &mut Context<Self>) -> Result<()> {
let envelope = ctx.next_envelope();
if let Some(envelope) = envelope.await {
envelope.handle(self, ctx).await?;
} else {
ctx.stop();
}
Ok(())
}
fn failed(&mut self, err: Error, _ctx: &mut Context<Self>) {
log::error!("Agent [{}] failed: {err}", type_name::<Self>());
}
async fn rollback(_this: Option<&mut Self>, _err: Error, _ctx: &mut Context<Self>) {}
fn finalize(&mut self, _ctx: &mut Context<Self>) {
self.end()
}
fn end(&mut self) {}
}
pub trait Standalone: Agent {
fn spawn(self) -> <Self::Context as ReachableContext>::Address
where
Self::Context: Default,
{
RunAgent::new(self).spawn_connected()
}
}
pub trait Runnable: Agent {
fn run(self) -> RunAgent<Self>;
}
impl<A: Agent> Runnable for A
where
Self::Context: Default,
{
fn run(self) -> RunAgent<Self> {
RunAgent::new(self)
}
}