Message Channel
The engine/subscription module in AgentOpera provides a publish-subscribe messaging system that allows agents to communicate with each other efficiently. This document explains the core concepts, components, and usage patterns of this communication mechanism.
Core Concepts
Message Channel
A MessageChannel in AgentOpera defines the scope of a broadcast message. Conceptually, it is equivalent to a Topic in many publish-subscribe systems, composed of two parts:
Topic: The type of event the message channel contains. It must match a specific pattern and follow cloud event specifications.
Source: The context identifier where an event happened.
This structure can be represented as:
MessageChannel = (Topic, Source)The topic specifies the type of message being sent, while the source identifies the specific instance or context of that topic type.
Subscription
A Subscription is a protocol that defines how agents express interest in certain message channels. When a message is published to a channel, it is delivered to all subscribed agents, according to matching rules defined by their subscriptions.
Key Components
MessageChannel
@dataclass(eq=True, frozen=True)
class MessageChannel:
"""
MessageChannel defines the scope of a broadcast message. In essence, agent engine
implements a publish-subscribe model through its broadcast API: when publishing
a message, the topic must be specified.
"""
topic: str
source: strDefaultMessageChannel
DefaultMessageChannel simplifies the creation of message channels by providing sensible defaults:
The topic defaults to "default"
The source automatically uses the current agent's ID if available, otherwise "default"
Subscription Protocol
The Subscription protocol defines the interface that all subscription implementations must follow:
id: A unique identifier for the subscriptionis_match: Determines if a given message channel matches the subscriptionmap_to_agent: Maps a matching message channel to an agent that should receive the message
Topic-Based Subscriptions
AgentOpera provides two primary implementations for topic-based subscriptions:
TopicSubscription
TopicSubscription matches messages with an exact topic match and maps them to agents where:
The agent type is predefined in the subscription
The agent key is derived from the message channel's source
DefaultSubscription
DefaultSubscription extends TopicSubscription with sensible defaults:
Topic defaults to "default"
Agent type is automatically detected from the context if not specified
Publishing Messages
The agent runtime provides a publish_message method to send messages to subscribed agents:
This method:
Takes a message of any type and a message channel
Optionally accepts a sender ID, cancellation token, and message ID
Delivers the message to all agents that have subscribed to the matching channel
This is a one-way communication method and does not return responses.
Subscription Patterns
AgentOpera supports various subscription patterns similar to those in the Microsoft AutoGen framework:
1. Single-Tenant, Single Topic
All agents operate within a single tenant and communicate via a single topic:
2. Multi-Tenant, Single Topic
Each tenant has its own isolated topic through the source identifier:
3. Single-Tenant, Multiple Topics
Messages are published to different topics within a single tenant:
4. Multi-Tenant, Multiple Topics
The most flexible pattern combines multiple topics with multiple tenants:
Usage Example
Here's a complete example showing how to set up subscriptions and publish messages using the agent registration pattern:
In this example:
Agents register themselves using the
registerclass method, which internally configures them with the runtimeSubscriptions are added for each agent type with specific topics
Messages are published to the message channels, which are routed to the appropriate agents based on the subscriptions
This approach follows the pattern used in production code where agents typically implement their own registration logic.
Best Practices
Use meaningful topic names that reflect the nature of the messages being published.
Structure source IDs to represent the specific instances of your application contexts.
Choose the right subscription type:
TopicSubscriptionfor exact matchingDefaultSubscriptionfor simple use cases
Last updated