crb_agent/agent.rs
1use crate::address::{Address, Link};
2use crate::context::{AgentContext, Context};
3use crate::performers::Next;
4use crate::runtime::RunAgent;
5use anyhow::{Error, Result};
6use async_trait::async_trait;
7use crb_runtime::{InteractiveTask, ManagedContext};
8use std::any::type_name;
9
10/// `Agent` is a universal trait of a hybrid (transactional) actor.
11/// It has a rich lifecycle, beginning its execution with the `initialize` method,
12/// which in turn calls the `begin` method. This nesting allows for initialization
13/// with context or simply setting an initial state.
14///
15/// `Agent` is an actor that either reactively processes incoming messages
16/// or executes special states, similar to a finite state machine,
17/// temporarily blocking message processing. This enables the actor to be prepared
18/// for operation or reconfigured in a transactional mode.
19#[async_trait]
20pub trait Agent: Sized + Send + 'static {
21    /// `Context` is the functional environment in which an actor operates.
22    type Context: AgentContext<Self>;
23
24    /// `Link` is a means of interacting with an actor—it essentially defines
25    /// the available methods that send specific types of messages.
26    ///
27    /// If a special interface is not required, it makes sense to simply use `Address<Self>`.
28    type Link: From<Address<Self>>;
29
30    /// The `initialize` method is called first when the actor starts.
31    /// It should return a `Next` state, which the actor will transition to.
32    ///
33    /// An execution context is passed as a parameter.
34    ///
35    /// By default, the method implementation calls the `begin` method.
36    fn initialize(&mut self, _ctx: &mut Context<Self>) -> Next<Self> {
37        self.begin()
38    }
39
40    /// The `begin` method is an initialization method without context.
41    ///
42    /// It is usually the most commonly used method to start the actor
43    /// in transactional mode by initiating finite state machine message processing.
44    ///
45    /// If the method is not overridden, it starts the actor's mode—reactive
46    /// message processing. You can achieve this by calling the `Next::events()` method.
47    fn begin(&mut self) -> Next<Self> {
48        Next::events()
49    }
50
51    /// The method is called when an attempt is made to interrupt the agent's execution.
52    ///
53    /// It is triggered only in actor mode. If the agent is in finite state machine mode,
54    /// it will not be called until it transitions to a reactive state
55    /// by invoking `Next::events()`.
56    ///
57    /// The default implementation interrupts the agent's execution by calling
58    /// the `Context::shutdown()` method.
59    ///
60    /// By overriding this method, you can define your own termination rules,
61    /// for example, initiating a finalization process that determines whether
62    /// the agent should actually be terminated.
63    fn interrupt(&mut self, ctx: &mut Context<Self>) {
64        ctx.shutdown();
65    }
66
67    /// The method responsible for handling messages in actor mode.
68    ///
69    /// The default implementation calls the `next_envelope()` method of the agent's context,
70    /// thereby retrieving a message with a handler and executing it by calling the `handle()` method.
71    ///
72    /// If the channel has been closed, the `stop()` method of the context will be called,
73    /// immediately halting execution. The request message for interrupting the actor is received
74    /// through the same mechanism and closes the channel.
75    async fn event(&mut self, ctx: &mut Context<Self>) -> Result<()> {
76        let envelope = ctx.next_envelope();
77        if let Some(envelope) = envelope.await {
78            envelope.handle(self, ctx).await?;
79        } else {
80            // Terminates the runtime when the channel has drained
81            ctx.stop();
82        }
83        Ok(())
84    }
85
86    /// This method is called every time a handler call ends with an unhandled error.
87    ///
88    /// By default, it simply logs the error, but you can also define additional actions
89    /// since the method has access to the agent's context.
90    fn failed(&mut self, err: Error, _ctx: &mut Context<Self>) {
91        log::error!("Agent [{}] failed: {err}", type_name::<Self>());
92    }
93
94    /// The `rollback` method is called when the agent is completely terminated due to an error.
95    /// In this case, the method receives a reference to the agent instance, if it was successfully
96    /// preserved, as well as the error that caused the agent's runtime to fail fatally.
97    /// Additionally, a context is available to extract additional data.
98    async fn rollback(_this: Option<&mut Self>, _err: Error, _ctx: &mut Context<Self>) {}
99
100    /// The `finalize` method is called when the agent has fully terminated. This method has access
101    /// to the context. By default, its implementation calls another agent method, `end`.
102    fn finalize(&mut self, _ctx: &mut Context<Self>) {
103        self.end()
104    }
105
106    /// The `end` method is called when the agent's runtime has fully terminated to perform
107    /// final actions with the agent.
108    fn end(&mut self) {}
109}
110
111pub trait Standalone: Agent {
112    fn spawn(self) -> Link<Self>
113    where
114        Self::Context: Default,
115    {
116        RunAgent::new(self).spawn_connected().into()
117    }
118
119    // TODO: spawn_with_context()
120}
121
122pub trait Runnable: Agent {
123    fn run(self) -> RunAgent<Self>;
124}
125
126impl<A: Agent> Runnable for A
127where
128    Self::Context: Default,
129{
130    fn run(self) -> RunAgent<Self> {
131        RunAgent::new(self)
132    }
133}