类 MsgHub
- 所有已实现的接口:
AutoCloseable
MsgHub manages message broadcasting and subscription in multi-agent conversations. When agents are added to a MsgHub, they automatically observe each other's messages without explicit message passing code.
Features:
- Automatic Broadcasting: Messages from any participant are automatically broadcast to all other participants
- Dynamic Participants: Add or remove agents during conversation
- Manual Broadcasting: Broadcast messages manually when needed
- Announcement Support: Send initial messages when entering the hub
- Lifecycle Management: Automatic cleanup with try-with-resources
Usage Example:
// Create agents
ReActAgent alice = ReActAgent.builder()
.name("Alice")
.model(model)
.formatter(new DashScopeMultiAgentFormatter())
.build();
ReActAgent bob = ReActAgent.builder()
.name("Bob")
.model(model)
.formatter(new DashScopeMultiAgentFormatter())
.build();
// Use MsgHub for multi-agent conversation
Msg announcement = new Msg("system", "Let's start the discussion", MsgRole.SYSTEM);
try (MsgHub hub = MsgHub.builder()
.participants(alice, bob)
.announcement(announcement)
.build()) {
hub.enter().block();
// Alice's reply will be automatically broadcast to Bob
alice.call().block();
// Bob's reply will be automatically broadcast to Alice
bob.call().block();
}
This is much simpler than manual message passing:
// Without MsgHub (verbose and error-prone)
Msg x1 = alice.call().block();
bob.observe(x1).block();
Msg x2 = bob.call().block();
alice.observe(x2).block();
Thread Safety: MsgHub uses CopyOnWriteArrayList for thread-safe participant management. However, individual agent instances should not be invoked concurrently.
- 另请参阅:
-
嵌套类概要
嵌套类 -
方法概要
修饰符和类型方法说明reactor.core.publisher.Mono<Void> Add new participants to this hub.reactor.core.publisher.Mono<Void> Add new participants to this hub.reactor.core.publisher.Mono<Void> Broadcast a message to all participants.reactor.core.publisher.Mono<Void> Broadcast multiple messages to all participants.static MsgHub.Builderbuilder()Create a new MsgHub builder.voidclose()Close the MsgHub and cleanup resources.reactor.core.publisher.Mono<Void> Remove participants from this hub.reactor.core.publisher.Mono<Void> Remove participants from this hub.reactor.core.publisher.Mono<MsgHub> enter()Enter the MsgHub context.reactor.core.publisher.Mono<Void> exit()Exit the MsgHub context.getName()Get the name of this MsgHub.Get the list of current participants.booleanCheck if auto-broadcast is enabled.voidsetAutoBroadcast(boolean enable) Enable or disable automatic broadcasting.
-
方法详细资料
-
getName
Get the name of this MsgHub.- 返回:
- MsgHub name
-
getParticipants
Get the list of current participants.- 返回:
- Unmodifiable view of participants
-
isAutoBroadcastEnabled
public boolean isAutoBroadcastEnabled()Check if auto-broadcast is enabled.- 返回:
- True if auto-broadcast is enabled
-
enter
Enter the MsgHub context. This method initializes subscriber relationships and broadcasts announcement messages.Must be called before using the hub. Typically called in a try-with-resources block or explicitly managed with enter()/exit().
- 返回:
- Mono containing this MsgHub instance
-
exit
Exit the MsgHub context. This method cleans up subscriber relationships.Called automatically when using try-with-resources via close().
- 返回:
- Mono that completes when cleanup is done
-
close
public void close()Close the MsgHub and cleanup resources. This is the AutoCloseable implementation for try-with-resources support.- 指定者:
close在接口中AutoCloseable
-
add
Add new participants to this hub. Automatically updates subscriber relationships if the hub is active.- 参数:
newParticipants- Agents to add (varargs)- 返回:
- Mono that completes when participants are added
-
add
Add new participants to this hub. Automatically updates subscriber relationships if the hub is active.- 参数:
newParticipants- List of agents to add- 返回:
- Mono that completes when participants are added
-
delete
Remove participants from this hub. Automatically updates subscriber relationships if the hub is active.- 参数:
toRemove- Agents to remove (varargs)- 返回:
- Mono that completes when participants are removed
-
delete
Remove participants from this hub. Automatically updates subscriber relationships if the hub is active.- 参数:
toRemove- List of agents to remove- 返回:
- Mono that completes when participants are removed
-
broadcast
Broadcast a message to all participants. All participants will receive the message via their observe() method.- 参数:
msg- Message to broadcast- 返回:
- Mono that completes when all participants have observed the message
-
broadcast
Broadcast multiple messages to all participants. All participants will receive all messages via their observe() method.- 参数:
msgs- Messages to broadcast- 返回:
- Mono that completes when all participants have observed all messages
-
setAutoBroadcast
public void setAutoBroadcast(boolean enable) Enable or disable automatic broadcasting.When enabled, each participant's reply will be automatically broadcast to all other participants. When disabled, the MsgHub only serves as a manual broadcaster.
- 参数:
enable- True to enable auto-broadcast, false to disable
-
builder
Create a new MsgHub builder.- 返回:
- New builder instance
-