Package com.tnsai.agents.async
Class AsyncMsgHub
java.lang.Object
com.tnsai.agents.async.AsyncMsgHub
- All Implemented Interfaces:
AutoCloseable
Async message hub for multi-agent coordination.
Inspired by AgentScope's MsgHub, provides:
- Dynamic participant management (add/remove at runtime)
- Broadcast messaging
- Sequential and parallel pipeline execution
- Announcement messages
Usage
try (var hub = AsyncMsgHub.create(agent1, agent2, agent3)) {
// Broadcast to all
hub.broadcast(announcement).join();
// Sequential pipeline
List<String> results = hub.sequentialPipeline("Discuss topic X").join();
// Parallel execution
List<String> results = hub.parallelPipeline("Analyze from your perspective").join();
}
-
Constructor Summary
ConstructorsConstructorDescriptionAsyncMsgHub(List<Agent> participants) Creates a new AsyncMsgHub with the given participants. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a participant to the hub.Broadcasts a message to all participants.voidclose()static AsyncMsgHubCreates a hub with the given agents.Runs a discussion round where each agent responds to the previous.Gets the current participants.booleanisEmpty()Checks if the hub is empty.parallelPipeline(String input) Executes a parallel pipeline where all agents process simultaneously.booleanRemoves a participant from the hub.sequentialPipeline(String input) Executes a sequential pipeline where each agent processes in order.intsize()Gets the number of participants.
-
Constructor Details
-
AsyncMsgHub
Creates a new AsyncMsgHub with the given participants.
-
-
Method Details
-
create
Creates a hub with the given agents. -
add
Adds a participant to the hub. -
remove
Removes a participant from the hub. -
getParticipants
Gets the current participants. -
broadcast
Broadcasts a message to all participants.- Parameters:
message- The message to broadcast- Returns:
- A future that completes when all agents have processed the message
-
sequentialPipeline
Executes a sequential pipeline where each agent processes in order.Each agent receives the input plus the accumulated responses from previous agents.
- Parameters:
input- The initial input- Returns:
- A future with all responses in order
-
parallelPipeline
Executes a parallel pipeline where all agents process simultaneously.- Parameters:
input- The input for all agents- Returns:
- A future with all responses (order matches participant order)
-
discuss
Runs a discussion round where each agent responds to the previous.- Parameters:
topic- The discussion topicrounds- Number of discussion rounds- Returns:
- A future with the discussion transcript
-
size
public int size()Gets the number of participants. -
isEmpty
public boolean isEmpty()Checks if the hub is empty. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-