Agent
Agent Objects
class Agent(Sink)
An agent that interacts within a communication environment.
Attributes:
- Name
_name
- Type
- string
- Description
The name of the agent.
- Name
_port
- Type
- _int_
- Description
The port on which the agent's server runs.
- Name
_background_tasks
- Type
- _Set[asyncio.Task]_
- Description
Set of background tasks associated with the agent.
- Name
_resolver
- Type
- _Resolver_
- Description
The resolver for agent communication.
- Name
_loop
- Type
- _asyncio.AbstractEventLoop_
- Description
The asyncio event loop used by the agent.
- Name
_logger
- Type
- Description
The logger instance for logging agent activities
- Name
_endpoints
- Type
- _List[dict]_
- Description
List of endpoints at which the agent is reachable.
- Name
_use_mailbox
- Type
- _bool_
- Description
Indicates if the agent uses a mailbox for communication.
- Name
_agentverse
- Type
- _dict_
- Description
Agentverse configuration settings.
- Name
_mailbox_client
- Type
- _MailboxClient_
- Description
The client for interacting with the agentverse mailbox.
- Name
_ledger
- Type
- Description
The client for interacting with the blockchain ledger.
- Name
_almanac_contract
- Type
- Description
The almanac contract for registering agent addresses to endpoints.
- Name
_storage
- Type
- Description
Key-value store for agent data storage.
- Name
_interval_handlers
- Type
- _List[Tuple[IntervalCallback, float]]_
- Description
List of interval handlers and their periods.
- Name
_interval_messages
- Type
- _Set[str]_
- Description
Set of message digests that may be sent by interval tasks.
- Name
_signed_message_handlers
- Type
- _Dict[str, MessageCallback]_
- Description
Handlers for signed messages.
- Name
_unsigned_message_handlers
- Type
- _Dict[str, MessageCallback]_
- Description
Handlers for unsigned messages.
- Name
_models
- Type
- _Dict[str, Type[Model]]_
- Description
Dictionary mapping supported message digests to messages.
- Name
_replies
- Type
- _Dict[str, Set[Type[Model]]]_
- Description
Dictionary of allowed reply digests for each type of incoming message.
- Name
_queries
- Type
- _Dict[str, asyncio.Future]_
- Description
Dictionary mapping query senders to their response Futures.
- Name
_dispatcher
- Type
- Description
The dispatcher for message handling.
- Name
_message_queue
- Type
- Description
Asynchronous queue for incoming messages.
- Name
_on_startup
- Type
- _List[Callable]_
- Description
List of functions to run on agent startup.
- Name
_on_shutdown
- Type
- _List[Callable]_
- Description
List of functions to run on agent shutdown.
- Name
_version
- Type
- _str_
- Description
The version of the agent.
- Name
_protocol
- Type
- _Protocol_
- Description
The internal agent protocol consisting of all interval and message handlers assigned with agent decorators.
- Name
protocols
- Type
- _Dict[str, Protocol]_
- Description
Dictionary mapping all supported protocol digests to their corresponding protocols.
- Name
_ctx
- Type
- _Context_
- Description
The context for agent interactions.
Properties:
- Name
name
- Type
- string
- Description
The name of the agent.
- Name
address
- Type
- string
- Description
The address of the agent used for communication.
- Name
wallet
- Type
- _LocalWallet_
- Description
The agent's wallet for transacting on the ledger.
- Name
storage
- Type
- _KeyValueStore_
- Description
The key-value store for storage operations.
- Name
mailbox
- Type
- _Dict[str, str]_
- Description
The mailbox configuration for the agent (deprecated and replaced by agentverse).
- Name
agentverse
- Type
- _Dict[str, str]_
- Description
The agentverse configuration for the agent.
- Name
mailbox_client
- Type
- _MailboxClient_
- Description
The client for interacting with the agentverse mailbox.
- Name
protocols
- Type
- _Dict[str, Protocol]_
- Description
Dictionary mapping all supported protocol digests to their corresponding protocols.
__
init__
def __init__(name: Optional[str] = None, port: Optional[int] = None, seed: Optional[str] = None, endpoint: Optional[Union[str, List[str], Dict[str, dict]]] = None, agentverse: Optional[Union[str, Dict[str, str]]] = None, mailbox: Optional[Union[str, Dict[str, str]]] = None, resolve: Optional[Resolver] = None, version: Optional[str] = None)
Initialize an Agent instance.
Arguments:
name
Optional[str] - The name of the agent.port
Optional[int] - The port on which the agent's server will run.seed
Optional[str] - The seed for generating keys.endpoint
Optional[Union[str, List[str], Dict[str, dict]]] - The endpoint configuration.agentverse
Optional[Union[str, Dict[str, str]]] - The agentverse configuration.mailbox
Optional[Union[str, Dict[str, str]]] - The mailbox configuration.resolve
Optional[Resolver] - The resolver to use for agent communication.version
Optional[str] - The version of the agent.
name
@property def name() -> str
Get the name of the agent.
Returns:
str
- The name of the agent.
address
@property def address() -> str
Get the address of the agent used for communication.
Returns:
str
- The agent's address.
wallet
@property def wallet() -> LocalWallet
Get the wallet of the agent.
Returns:
LocalWallet
- The agent's wallet.
storage
@property def storage() -> KeyValueStore
Get the key-value store used by the agent for data storage.
Returns:
KeyValueStore
- The key-value store instance.
mailbox
@property def mailbox() -> Dict[str, str]
Get the mailbox configuration of the agent (deprecated and replaced by agentverse).
Returns:
Dict[str, str]: The mailbox configuration.
agentverse
@property def agentverse() -> Dict[str, str]
Get the agentverse configuration of the agent.
Returns:
Dict[str, str]: The agentverse configuration.
mailbox_
client
@property def mailbox_client() -> MailboxClient
Get the mailbox client used by the agent for mailbox communication.
Returns:
MailboxClient
- The mailbox client instance.
mailbox
@mailbox.setter def mailbox(config: Union[str, Dict[str, str]])
Set the mailbox configuration for the agent (deprecated and replaced by agentverse).
Arguments:
config
Union[str, Dict[str, str]] - The new mailbox configuration.
agentverse
@agentverse.setter def agentverse(config: Union[str, Dict[str, str]])
Set the agentverse configuration for the agent.
Arguments:
config
Union[str, Dict[str, str]] - The new agentverse configuration.
sign
def sign(data: bytes) -> str
Sign the provided data.
Arguments:
data
bytes - The data to be signed.
Returns:
str
- The signature of the data.
sign_
digest
def sign_digest(digest: bytes) -> str
Sign the provided digest.
Arguments:
digest
bytes - The digest to be signed.
Returns:
str
- The signature of the digest.
sign_
registration
def sign_registration() -> str
Sign the registration data for Almanac contract.
Returns:
str
- The signature of the registration data.
Raises:
AssertionError
- If the Almanac contract address is None.
update_
endpoints
def update_endpoints(endpoints: List[Dict[str, Any]])
Update the list of endpoints.
Arguments:
endpoints
List[Dict[str, Any]] - List of endpoint dictionaries.
update_
loop
def update_loop(loop)
Update the event loop.
Arguments:
loop
- The event loop.
update_
queries
def update_queries(queries)
Update the queries attribute.
Arguments:
queries
- The queries attribute.
register
async def register()
Register with the Almanac contract.
This method checks for registration conditions and performs registration if necessary.
on_
interval
def on_interval(period: float, messages: Optional[Union[Type[Model], Set[Type[Model]]]] = None)
Decorator to register an interval handler for the provided period.
Arguments:
period
float - The interval period.messages
Optional[Union[Type[Model], Set[Type[Model]]]] - Optional message types.
Returns:
Callable
- The decorator function for registering interval handlers.
on_
query
def on_query(model: Type[Model], replies: Optional[Union[Model, Set[Model]]] = None)
Set up a query event with a callback.
Arguments:
model
Type[Model] - The query model.replies
Optional[Union[Model, Set[Model]]] - Optional reply models.
Returns:
Callable
- The decorator function for registering query handlers.
on_
message
def on_message(model: Type[Model], replies: Optional[Union[Type[Model], Set[Type[Model]]]] = None, allow_unverified: Optional[bool] = False)
Decorator to register an message handler for the provided message model.
Arguments:
model
Type[Model] - The message model.replies
Optional[Union[Type[Model], Set[Type[Model]]]] - Optional reply models.allow_unverified
Optional[bool] - Allow unverified messages.
Returns:
Callable
- The decorator function for registering message handlers.
on_
event
def on_event(event_type: str)
Decorator to register an event handler for a specific event type.
Arguments:
event_type
str - The type of event.
Returns:
Callable
- The decorator function for registering event handlers.
include
def include(protocol: Protocol, publish_manifest: Optional[bool] = False)
Include a protocol into the agent's capabilities.
Arguments:
protocol
Protocol - The protocol to include.publish_manifest
Optional[bool] - Flag to publish the protocol's manifest.
Raises:
RuntimeError
- If a duplicate model, signed message handler, or message handler is encountered.
publish_
manifest
def publish_manifest(manifest: Dict[str, Any])
Publish a protocol manifest to the Almanac service.
Arguments:
manifest
Dict[str, Any] - The protocol manifest.
handle_
message
async def handle_message(sender, schema_digest: str, message: JsonStr, session: uuid.UUID)
Handle an incoming message.
Arguments:
sender
- The sender of the message.schema_digest
str - The digest of the message schema.message
JsonStr - The message content in JSON format.session
uuid.UUID - The session UUID.
setup
def setup()
Include the internal agent protocol, run startup tasks, and start background tasks.
start_
background_
tasks
def start_background_tasks()
Start background tasks for the agent.
run
def run()
Run the agent.
Bureau Objects
class Bureau()
A class representing a Bureau of agents.
This class manages a collection of agents and orchestrates their execution.
Arguments:
port
Optional[int] - The port number for the server.endpoint
Optional[Union[str, List[str], Dict[str, dict]]] - Configuration for agent endpoints.
Attributes:
_loop
asyncio.AbstractEventLoop - The event loop._agents
List[Agent] - The list of agents contained in the bureau._endpoints
List[Dict[str, Any]] - The endpoint configuration for the bureau._port
int - The port on which the bureau's server runs._queries
Dict[str, asyncio.Future] - Dictionary mapping query senders to their response Futures._logger
Logger - The logger instance._server
ASGIServer - The ASGI server instance for handling requests._use_mailbox
bool - A flag indicating whether mailbox functionality is enabled for any of the agents.
__
init__
def __init__(port: Optional[int] = None, endpoint: Optional[Union[str, List[str], Dict[str, dict]]] = None)
Initialize a Bureau instance.
Arguments:
port
Optional[int] - The port on which the bureau's server will run.endpoint
Optional[Union[str, List[str], Dict[str, dict]]] - The endpoint configuration for the bureau.
add
def add(agent: Agent)
Add an agent to the bureau.
Arguments:
agent
Agent - The agent to be added.
run
def run()
Run the agents managed by the bureau.