crb_agent/
agent.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::context::AgentContext;
use crate::performers::Next;
use crate::runtime::RunAgent;
use anyhow::{Error, Result};
use async_trait::async_trait;
use crb_runtime::{Context, InteractiveTask, ManagedContext};

#[async_trait]
pub trait Agent: Sized + Send + 'static {
    type Context: AgentContext<Self>;
    type Output: Output;

    fn initialize(&mut self, _ctx: &mut Self::Context) -> Next<Self> {
        self.begin()
    }

    fn begin(&mut self) -> Next<Self> {
        Next::events()
    }

    fn interrupt(&mut self, ctx: &mut Self::Context) {
        // Closes the channel
        ctx.session().shutdown();
    }

    async fn event(&mut self, ctx: &mut Self::Context) -> Result<()> {
        let envelope = ctx.session().joint().next_envelope();
        if let Some(envelope) = envelope.await {
            envelope.handle(self, ctx).await?;
        } else {
            // Terminates the runtime when the channel has drained
            ctx.session().controller().stop(false)?;
        }
        Ok(())
    }

    fn failed(&mut self, err: &Error, _ctx: &mut Self::Context) {
        log::error!("Agent failed: {err}");
    }

    fn finalize(self, _ctx: &mut Self::Context) -> Option<Self::Output> {
        self.end()
    }

    fn end(self) -> Option<Self::Output> {
        None
    }
}

pub trait Output: Sync + Send + 'static {}

impl<T> Output for T where T: Sync + Send + 'static {}

pub trait Standalone: Agent {
    fn spawn(self) -> <Self::Context as Context>::Address
    where
        Self::Context: Default,
    {
        RunAgent::new(self).spawn_connected()
    }
    // TODO: spawn_with_context()
}

#[async_trait]
pub trait Runnable: Agent {
    async fn run(self) -> Result<Option<Self::Output>>;
}

#[async_trait]
impl<A: Agent> Runnable for A
where
    Self::Context: Default,
    A::Output: Clone,
{
    async fn run(self) -> Result<Option<Self::Output>> {
        let mut runtime = RunAgent::new(self);
        runtime.perform_routine().await?;
        let output = runtime.context.address().clone().join().await?.output();
        Ok(output)
    }
}