academy.handle¶
Handle
¶
Agent handle protocol.
A handle enables an agent or user to invoke actions on another agent.
action
async
¶
Invoke an action on the agent.
Parameters:
-
action
(str
) –Action to invoke.
-
args
(Any
, default:()
) –Positional arguments for the action.
-
kwargs
(Any
, default:{}
) –Keywords arguments for the action.
Returns:
-
R
–Result of the action.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
Exception
–Any exception raised by the action.
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
close
async
¶
ping
async
¶
Ping the agent.
Ping the agent and wait to get a response. Agents process messages in order so the round-trip time will include processing time of earlier messages in the queue.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the response.
Returns:
-
float
–Round-trip time in seconds.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
async
¶
shutdown(*, terminate: bool | None = None) -> None
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Parameters:
-
terminate
(bool | None
, default:None
) –Override the termination behavior of the agent defined in the
RuntimeConfig
.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
HandleDict
¶
HandleDict(
values: (
Mapping[K, Handle[AgentT]]
| Iterable[tuple[K, Handle[AgentT]]]
) = (),
/,
**kwargs: dict[str, Handle[AgentT]],
)
HandleList
¶
List of handles.
Tip
The HandleList
is required when storing a list of handles as
attributes of a Agent
so that those handles get bound to the
correct agent when running.
Source code in academy/handle.py
ProxyHandle
¶
ProxyHandle(agent: AgentT)
Proxy handle.
A proxy handle is thin wrapper around a
Agent
instance that is useful for testing
agents that are initialized with a handle to another agent without
needing to spawn agents. This wrapper invokes actions synchronously.
Source code in academy/handle.py
action
async
¶
Invoke an action on the agent.
Parameters:
-
action
(str
) –Action to invoke.
-
args
(Any
, default:()
) –Positional arguments for the action.
-
kwargs
(Any
, default:{}
) –Keywords arguments for the action.
Returns:
-
R
–Result of the action.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
Exception
–Any exception raised by the action.
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
close
async
¶
Close this handle.
Note
This is a no-op for proxy handles.
Parameters:
-
wait_futures
(bool
, default:True
) –Wait to return until all pending futures are done executing. If
False
, pending futures are cancelled. -
timeout
(float | None
, default:None
) –Optional timeout used when
wait=True
.
Source code in academy/handle.py
ping
async
¶
Ping the agent.
Ping the agent and wait to get a response. Agents process messages in order so the round-trip time will include processing time of earlier messages in the queue.
Note
This is a no-op for proxy handles and returns 0 latency.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the response.
Returns:
-
float
–Round-trip time in seconds.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
async
¶
shutdown(*, terminate: bool | None = None) -> None
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Parameters:
-
terminate
(bool | None
, default:None
) –Override the termination behavior of the agent defined in the
RuntimeConfig
.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
RemoteHandle
¶
RemoteHandle(
agent_id: AgentId[AgentT],
exchange: ExchangeClient[Any] | None = None,
)
Handle to a remote agent bound to an exchange client.
Parameters:
-
exchange
(ExchangeClient[Any] | None
, default:None
) –Exchange client used for agent communication.
-
agent_id
(AgentId[AgentT]
) –EntityId of the target agent of this handle.
Source code in academy/handle.py
exchange
property
¶
exchange: ExchangeClient[Any]
Exchange client used to send messages.
Returns:
-
ExchangeClient[Any]
–The ExchangeClient
Raises:
-
HandleNotBoundError
–If the exchange client can't be found.
clone
¶
clone() -> RemoteHandle[AgentT]
close
async
¶
Close this handle.
Note
This does not close the exchange client.
Parameters:
-
wait_futures
(bool
, default:True
) –Wait to return until all pending futures are done executing. If
False
, pending futures are cancelled. -
timeout
(float | None
, default:None
) –Optional timeout used when
wait=True
.
Source code in academy/handle.py
action
async
¶
Invoke an action on the agent.
Parameters:
-
action
(str
) –Action to invoke.
-
args
(Any
, default:()
) –Positional arguments for the action.
-
kwargs
(Any
, default:{}
) –Keywords arguments for the action.
Returns:
-
R
–Result of the action.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
Exception
–Any exception raised by the action.
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
ping
async
¶
Ping the agent.
Ping the agent and wait to get a response. Agents process messages in order so the round-trip time will include processing time of earlier messages in the queue.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the response.
Returns:
-
float
–Round-trip time in seconds.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
async
¶
shutdown(*, terminate: bool | None = None) -> None
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Parameters:
-
terminate
(bool | None
, default:None
) –Override the termination behavior of the agent defined in the
RuntimeConfig
.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.