pub struct AgentHandle {
pub parent: Option<Box<AgentHandle>>,
pub broker: Box<Option<AgentHandle>>,
/* private fields */
}Expand description
A clonable handle for interacting with an agent.
AgentHandle provides the primary mechanism for communicating with and managing
an agent from outside its own execution context. It encapsulates the necessary
information to send messages to the agent’s inbox (outbox), identify the agent (id),
manage its lifecycle (stop), track its tasks (tracker), and navigate the
supervision hierarchy (parent, children, supervise).
Handles can be cloned freely, allowing multiple parts of the system to hold references to the same agent. Sending messages through the handle is asynchronous.
Key functionalities are exposed through implemented traits:
AgentHandleInterface: Core methods for interaction (sending messages, stopping, etc.).Broker: Methods for broadcasting messages via the system broker.Subscriber: Method for accessing the system broker handle.
Equality and hashing are based solely on the agent’s unique identifier (id).
Fields§
§parent: Option<Box<AgentHandle>>Optional reference to the parent (supervisor) agent’s handle.
None if this is a top-level agent. Boxed to manage AgentHandle size.
broker: Box<Option<AgentHandle>>Optional reference to the system message broker’s handle.
Boxed to manage AgentHandle size.
Implementations§
Source§impl AgentHandle
impl AgentHandle
Sourcepub async fn supervise<State: Default + Send + Debug + 'static>(
&self,
child: ManagedAgent<Idle, State>,
) -> Result<AgentHandle>
pub async fn supervise<State: Default + Send + Debug + 'static>( &self, child: ManagedAgent<Idle, State>, ) -> Result<AgentHandle>
Starts a child agent and registers it under this agent’s supervision.
This method takes a ManagedAgent configured in the Idle state,
starts its execution by calling its start method, and then stores
the resulting child AgentHandle in this parent handle’s children map.
§Type Parameters
State: The user-defined state type of the child agent. Must implementDefault,Send,Debug, and be'static.
§Arguments
child: TheManagedAgent<Idle, State>instance representing the child agent to be started and supervised.
§Returns
A Result containing:
Ok(AgentHandle): The handle of the successfully started and registered child agent.Err(anyhow::Error): If starting the child agent fails.
Trait Implementations§
Source§impl AgentHandleInterface for AgentHandle
Implements the core interface for interacting with an agent.
impl AgentHandleInterface for AgentHandle
Implements the core interface for interacting with an agent.
Source§fn reply_address(&self) -> MessageAddress
fn reply_address(&self) -> MessageAddress
Returns the MessageAddress for this agent, used for sending replies.
Source§fn create_envelope(
&self,
recipient_address: Option<MessageAddress>,
) -> OutboundEnvelope
fn create_envelope( &self, recipient_address: Option<MessageAddress>, ) -> OutboundEnvelope
Creates an OutboundEnvelope for sending a message from this agent.
§Arguments
recipient_address: An optionalMessageAddressspecifying the recipient. IfNone, the envelope is created without a specific recipient (e.g., for broadcasting or when the recipient is set later).
§Returns
An OutboundEnvelope with the return_address set to this agent’s address.
Source§fn children(&self) -> DashMap<String, AgentHandle>
fn children(&self) -> DashMap<String, AgentHandle>
Returns a clone of the internal map containing handles to the agent’s direct children.
Provides a snapshot of the currently supervised children. Modifications to the returned map will not affect the agent’s actual children list.
Source§fn find_child(&self, ern: &Ern) -> Option<AgentHandle>
fn find_child(&self, ern: &Ern) -> Option<AgentHandle>
Source§fn tracker(&self) -> TaskTracker
fn tracker(&self) -> TaskTracker
Returns a clone of the agent’s task tracker.
The tracker can be used to monitor the agent’s main task.
Source§fn name(&self) -> String
fn name(&self) -> String
Returns the agent’s root name (the first part of its Ern) as a String.
Source§fn clone_ref(&self) -> AgentHandle
fn clone_ref(&self) -> AgentHandle
Returns a clone of this AgentHandle.
Source§fn stop(&self) -> impl Future<Output = Result<()>> + Send + Sync + '_
fn stop(&self) -> impl Future<Output = Result<()>> + Send + Sync + '_
Sends a [SystemSignal::Terminate] message to the agent and waits for its task to complete.
This initiates a graceful shutdown of the agent. It sends the Terminate signal
to the agent’s inbox and then waits on the agent’s TaskTracker until the main
task (and potentially associated tasks) have finished execution.
The agent’s wake loop is responsible for handling the Terminate signal,
potentially running before_stop and after_stop hooks, and stopping child agents.
§Returns
An anyhow::Result<()> indicating success or failure. Failure typically occurs
if sending the Terminate signal to the agent’s inbox fails (e.g., if the channel
is already closed).
Source§fn send(
&self,
message: impl ActonMessage,
) -> impl Future<Output = ()> + Send + Sync + '_where
Self: Sync,
fn send(
&self,
message: impl ActonMessage,
) -> impl Future<Output = ()> + Send + Sync + '_where
Self: Sync,
Source§fn send_sync(
&self,
message: impl ActonMessage,
recipient: &AgentHandle,
) -> Result<()>where
Self: Sized,
fn send_sync(
&self,
message: impl ActonMessage,
recipient: &AgentHandle,
) -> Result<()>where
Self: Sized,
Source§impl Broker for AgentHandle
Implements the Broker trait, allowing broadcasting via the associated broker.
impl Broker for AgentHandle
Implements the Broker trait, allowing broadcasting via the associated broker.
Source§fn broadcast(
&self,
message: impl ActonMessage,
) -> impl Future<Output = ()> + Send + Sync + '_
fn broadcast( &self, message: impl ActonMessage, ) -> impl Future<Output = ()> + Send + Sync + '_
Sends a message to the associated system broker for broadcasting.
This method wraps the provided message in a BrokerRequest and sends it
to the broker handle stored within this AgentHandle. If no broker handle
is configured, an error is logged.
§Arguments
message: The message payload (must implementActonMessage) to be broadcast.
Source§fn broadcast_sync(&self, message: impl ActonMessage) -> Result<()>where
Self: AgentHandleInterface + Sized,
fn broadcast_sync(&self, message: impl ActonMessage) -> Result<()>where
Self: AgentHandleInterface + Sized,
Source§impl Clone for AgentHandle
impl Clone for AgentHandle
Source§fn clone(&self) -> AgentHandle
fn clone(&self) -> AgentHandle
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AgentHandle
impl Debug for AgentHandle
Source§impl Default for AgentHandle
impl Default for AgentHandle
Source§impl Hash for AgentHandle
Implements hashing based on the agent’s unique ID (Ern).
impl Hash for AgentHandle
Implements hashing based on the agent’s unique ID (Ern).
Source§impl PartialEq for AgentHandle
Implements equality comparison based on the agent’s unique ID (Ern).
impl PartialEq for AgentHandle
Implements equality comparison based on the agent’s unique ID (Ern).
Source§impl Subscriber for AgentHandle
Implements the Subscriber trait, allowing access to the broker.
impl Subscriber for AgentHandle
Implements the Subscriber trait, allowing access to the broker.
Source§fn get_broker(&self) -> Option<AgentHandle>
fn get_broker(&self) -> Option<AgentHandle>
Returns a clone of the optional broker handle associated with this agent.
Returns None if the agent was not configured with a broker reference.
impl Eq for AgentHandle
Derives Eq based on the PartialEq implementation.
Auto Trait Implementations§
impl Freeze for AgentHandle
impl !RefUnwindSafe for AgentHandle
impl Send for AgentHandle
impl Sync for AgentHandle
impl Unpin for AgentHandle
impl UnwindSafe for AgentHandle
Blanket Implementations§
Source§impl<T> ActonMessage for T
impl<T> ActonMessage for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Subscribable for T
impl<T> Subscribable for T
Source§fn subscribe<M>(&self) -> impl Future<Output = ()> + Send + Sync
fn subscribe<M>(&self) -> impl Future<Output = ()> + Send + Sync
Sends a [SubscribeBroker] message to the broker.
Source§fn unsubscribe<M>(&self)where
M: ActonMessage,
fn unsubscribe<M>(&self)where
M: ActonMessage,
Spawns a task to send an [UnsubscribeBroker] message to the broker.
Note: The current UnsubscribeBroker message structure might be incomplete.