Package com.tnsai.protocols
Class ProtocolManager
java.lang.Object
com.tnsai.protocols.ProtocolManager
- All Implemented Interfaces:
com.tnsai.protocols.ProtocolManagerHandle
public final class ProtocolManager
extends Object
implements com.tnsai.protocols.ProtocolManagerHandle
Protocol Manager - Orchestrates all communication paradigms for an agent.
ProtocolManager is the central hub for inter-agent communication in TnsAI. It automatically discovers and configures protocol handlers based on annotations on the agent class, providing a unified API for messaging.
Supported Communication Paradigms
| Paradigm | Annotation | Use Case |
|---|---|---|
| Direct Message | DirectMessage | Point-to-point, action execution |
| Broadcast | Broadcast | One-to-many on single topic |
| Pub/Sub | PubSub | Multi-topic subscribe/publish |
| Request/Reply | RequestReply | Synchronous with timeout |
| Debate | Debate | Multi-agent deliberation |
Quick Start
// 1. Define agent with protocol annotations
@DirectMessage
@PubSub(topics = {"alerts", "updates"})
public class NotificationAgent extends Agent {
// Passes an ActionExecutor wired with a principal and an authority scope through the
// four-argument Agent constructor, so the actions it is asked to run are not refused.
// ...
}
// 2. Protocol manager auto-starts via Agent constructor
Agent agent = new NotificationAgent(principal, scope);
// 3. Use protocols via agent's protocol manager
ProtocolManager pm = agent.getProtocolManager();
// Direct message to another agent
Message response = pm.executeAction("receiver-id", "processOrder", Map.of(
"orderId", "12345"
));
// Publish to topic
pm.publish("alerts", Map.of("level", "HIGH", "message", "System alert"));
Transport Modes
Each protocol supports multiple transports:
TransportMode.INTERNAL- In-memory, same JVM (fastest)TransportMode.HTTP- HTTP/REST for remote agentsTransportMode.AUTO- Automatic selection based on config
Message Flow
Sender Agent Receiver Agent
| |
| executeAction(...) |
|---> ProtocolManager |
| | |
| | DirectMessageHandler |
| |---> Transport ------->|---> ProtocolManager
| | | |
| | | | Message received
| | | | Execute action
| | | |
|invalid input: '<'------| Response invalid input: '<'------------|invalid input: '<'------| Reply
Natural Language Messaging
Agents can send natural language messages that the receiver's LLM parses:
// LLM-powered messaging (receiver's LLM processes and responds)
pm.sendMessage("receiver-id", "Please summarize the quarterly report");
Debate Protocol
For multi-agent deliberation and consensus building:
// Start a debate
DebateSession session = pm.startDebate(
"Should we deploy to production?",
List.of("dev-agent", "qa-agent", "ops-agent")
);
// Submit position
pm.submitDebatePosition(session.getId(), "YES", 8, "All tests pass");
// Vote on another's position
pm.voteInDebate(session.getId(), "NO", false);
Thread Safety
This class is thread-safe. Handlers are stored in a ConcurrentHashMap and all public methods are safe to call from multiple threads.
Lifecycle
- Created during Agent initialization
- Auto-configures handlers from annotations
- Started automatically if annotations present
- Stopped when
stop()is called or agent terminates
- Since:
- 0.0.2
- Author:
- TnsAI Team
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.tnsai.protocols.ProtocolManagerHandle
com.tnsai.protocols.ProtocolManagerHandle.Factory -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidBroadcasts a message on the configured topic.voidcounterArgument(String debateId, String targetAgent, String counterArgument, String alternative) Submits a counter-argument in an ongoing debate.com.tnsai.protocols.models.MessageExecutes an action on another agent's specific role.com.tnsai.protocols.models.MessageExecutes an action on another agent (direct message).com.tnsai.agents.AgentgetAgent()Gets the agent managed by this protocol manager.Gets an active debate session.Gets all configured paradigms.booleanhasParadigm(String paradigm) Checks if a paradigm is configured.booleanChecks if manager is started.voidPublishes a message on a specific topic.com.tnsai.protocols.models.MessageSends a request and waits for reply.com.tnsai.protocols.models.MessagesendMessage(String receiverId, String content) Sends a natural language message to another agent (LLM-powered).voidstart()Starts all configured handlers.startDebate(String topic, List<String> participantIds) Starts a new debate with the given participants.voidstop()Stops all handlers.voidsubmitDebatePosition(String debateId, String stance, int confidence, String reasoning) Submits a position in an ongoing debate.voidvoteInDebate(String debateId, String targetStance, boolean support) Votes on a position in an ongoing debate.
-
Constructor Details
-
ProtocolManager
public ProtocolManager(com.tnsai.agents.Agent agent)
-
-
Method Details
-
start
Starts all configured handlers.- Specified by:
startin interfacecom.tnsai.protocols.ProtocolManagerHandle- Throws:
ProtocolHandler.HandlerException
-
stop
public void stop()Stops all handlers.- Specified by:
stopin interfacecom.tnsai.protocols.ProtocolManagerHandle
-
executeAction
public com.tnsai.protocols.models.Message executeAction(String receiverId, String actionName, Map<String, Object> parameters) throws ProtocolHandler.HandlerExceptionExecutes an action on another agent (direct message).- Parameters:
receiverId- Target agent IDactionName- Action to executeparameters- Action parameters- Returns:
- Response message with result
- Throws:
ProtocolHandler.HandlerException
-
executeAction
public com.tnsai.protocols.models.Message executeAction(String receiverId, String roleId, String actionName, Map<String, Object> parameters) throws ProtocolHandler.HandlerExceptionExecutes an action on another agent's specific role.- Parameters:
receiverId- Target agent IDroleId- Target role ID (optional)actionName- Action to executeparameters- Action parameters- Returns:
- Response message with result
- Throws:
ProtocolHandler.HandlerException
-
sendMessage
public com.tnsai.protocols.models.Message sendMessage(String receiverId, String content) throws ProtocolHandler.HandlerException Sends a natural language message to another agent (LLM-powered). The receiver's LLM will parse the message and execute appropriate actions via tool calling.- Parameters:
receiverId- Target agent IDcontent- Natural language message content- Returns:
- Response message with result
- Throws:
ProtocolHandler.HandlerException
-
broadcast
Broadcasts a message on the configured topic.- Parameters:
content- Message content (can include action, parameters, beliefs, etc.)- Throws:
ProtocolHandler.HandlerException
-
publish
public void publish(String topic, Map<String, Object> content) throws ProtocolHandler.HandlerExceptionPublishes a message on a specific topic.- Parameters:
topic- Topic namecontent- Message content- Throws:
ProtocolHandler.HandlerException
-
request
public com.tnsai.protocols.models.Message request(String receiverId, String actionName, Map<String, Object> parameters) throws ProtocolHandler.HandlerExceptionSends a request and waits for reply.- Parameters:
receiverId- Target agent IDactionName- Action to executeparameters- Action parameters- Returns:
- Reply message with result
- Throws:
ProtocolHandler.HandlerException
-
startDebate
public DebateProtocolHandler.DebateSession startDebate(String topic, List<String> participantIds) throws ProtocolHandler.HandlerException Starts a new debate with the given participants.- Parameters:
topic- Debate topicparticipantIds- IDs of participating agents- Returns:
- DebateSession for tracking the debate
- Throws:
ProtocolHandler.HandlerException
-
submitDebatePosition
public void submitDebatePosition(String debateId, String stance, int confidence, String reasoning) throws ProtocolHandler.HandlerException Submits a position in an ongoing debate.- Parameters:
debateId- Debate session IDstance- Position being takenconfidence- Confidence level (1-10)reasoning- Explanation for the position- Throws:
ProtocolHandler.HandlerException
-
voteInDebate
public void voteInDebate(String debateId, String targetStance, boolean support) throws ProtocolHandler.HandlerException Votes on a position in an ongoing debate.- Parameters:
debateId- Debate session IDtargetStance- Stance being voted onsupport- True to support, false to oppose- Throws:
ProtocolHandler.HandlerException
-
counterArgument
public void counterArgument(String debateId, String targetAgent, String counterArgument, String alternative) throws ProtocolHandler.HandlerException Submits a counter-argument in an ongoing debate.- Parameters:
debateId- Debate session IDtargetAgent- Agent being counteredcounterArgument- The counter-argumentalternative- Optional alternative proposal- Throws:
ProtocolHandler.HandlerException
-
getDebate
Gets an active debate session.- Parameters:
debateId- Debate session ID- Returns:
- DebateSession or null if not found
-
getAgent
public com.tnsai.agents.Agent getAgent()Gets the agent managed by this protocol manager.- Specified by:
getAgentin interfacecom.tnsai.protocols.ProtocolManagerHandle- Returns:
- Agent instance
-
isStarted
public boolean isStarted()Checks if manager is started.- Specified by:
isStartedin interfacecom.tnsai.protocols.ProtocolManagerHandle- Returns:
- true if started
-
hasParadigm
Checks if a paradigm is configured.- Specified by:
hasParadigmin interfacecom.tnsai.protocols.ProtocolManagerHandle- Parameters:
paradigm- Paradigm name (DIRECT, BROADCAST, PUBSUB, REQUEST_REPLY)- Returns:
- true if configured
-
getParadigms
Gets all configured paradigms.- Specified by:
getParadigmsin interfacecom.tnsai.protocols.ProtocolManagerHandle- Returns:
- Set of paradigm names
-