AgentHandle

Struct AgentHandle 

Source
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

Source

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 implement Default, Send, Debug, and be 'static.
§Arguments
§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.

Source§

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

Creates an OutboundEnvelope for sending a message from this agent.

§Arguments
  • recipient_address: An optional MessageAddress specifying the recipient. If None, 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>

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>

Searches for a direct child agent by its unique identifier (Ern).

§Arguments
  • ern: The Ern of the child agent to find.
§Returns
  • Some(AgentHandle): If a direct child with the matching Ern is found.
  • None: If no direct child with the specified Ern exists.
Source§

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 id(&self) -> Ern

Returns a clone of the agent’s unique Entity Resource Name (Ern).

Source§

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

Returns a clone of this AgentHandle.

Source§

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,

Sends a message asynchronously to this agent handle’s associated agent. Read more
Source§

fn send_sync( &self, message: impl ActonMessage, recipient: &AgentHandle, ) -> Result<()>
where Self: Sized,

Sends a message synchronously to a specified recipient agent. Read more
Source§

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 + '_

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 implement ActonMessage) to be broadcast.
Source§

fn broadcast_sync(&self, message: impl ActonMessage) -> Result<()>

Synchronously sends a message to the broker for broadcasting. Read more
Source§

impl Clone for AgentHandle

Source§

fn clone(&self) -> AgentHandle

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentHandle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AgentHandle

Source§

fn default() -> Self

Creates a default, placeholder AgentHandle.

This handle is typically initialized with a default Ern, a closed channel, and no parent, broker, or children. It’s primarily used as a starting point before being properly configured when a ManagedAgent is created.

Source§

impl Hash for AgentHandle

Implements hashing based on the agent’s unique ID (Ern).

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for AgentHandle

Implements equality comparison based on the agent’s unique ID (Ern).

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Subscriber for AgentHandle

Implements the Subscriber trait, allowing access to the broker.

Source§

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.

Source§

impl Eq for AgentHandle

Derives Eq based on the PartialEq implementation.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> ActonMessage for T
where T: Any + Send + Sync + Debug + DynClone + 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Implementation of as_any for the blanket impl.

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Implementation of as_any_mut for the blanket impl.

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Subscribable for T
where T: AgentHandleInterface + Subscriber + Send + Sync + 'static,

Source§

fn subscribe<M>(&self) -> impl Future<Output = ()> + Send + Sync
where M: ActonMessage + Send + Sync + 'static,

Sends a [SubscribeBroker] message to the broker.

Source§

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.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more