import io.agentscope.core.ReActAgent;
import io.agentscope.extensions.model.dashscope.formatter.DashScopeMultiAgentFormatter;
import io.agentscope.core.memory.InMemoryMemory;
import io.agentscope.core.message.Msg;
import io.agentscope.core.message.MsgRole;
import io.agentscope.core.message.TextBlock;
import io.agentscope.extensions.model.dashscope.DashScopeChatModel;
import io.agentscope.core.pipeline.MsgHub;
// Create model with MultiAgentFormatter (important!)
DashScopeChatModel model = DashScopeChatModel.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.modelName("qwen3-max")
.formatter(new DashScopeMultiAgentFormatter())
.build();
// Create agents
ReActAgent alice = ReActAgent.builder()
.name("Alice")
.sysPrompt("You are Alice, a friendly teacher. Be brief in your responses.")
.model(model)
.memory(new InMemoryMemory())
.build();
ReActAgent bob = ReActAgent.builder()
.name("Bob")
.sysPrompt("You are Bob, a curious student. Be brief in your responses.")
.model(model)
.memory(new InMemoryMemory())
.build();
ReActAgent charlie = ReActAgent.builder()
.name("Charlie")
.sysPrompt("You are Charlie, a thoughtful observer. Be brief in your responses.")
.model(model)
.memory(new InMemoryMemory())
.build();
// Create announcement message
Msg announcement = Msg.builder()
.name("system")
.role(MsgRole.SYSTEM)
.content(TextBlock.builder()
.text("Welcome to the discussion! Please introduce yourself briefly.")
.build())
.build();
// Use MsgHub with try-with-resources
try (MsgHub hub = MsgHub.builder()
.name("Introduction")
.participants(alice, bob, charlie)
.announcement(announcement)
.enableAutoBroadcast(true) // Default is true
.build()) {
// Enter the hub (broadcasts announcement to all participants)
hub.enter().block();
// Each agent introduces themselves
// Their responses are automatically broadcast to others
Msg aliceReply = alice.call().block();
System.out.println("Alice: " + aliceReply.getTextContent());
Msg bobReply = bob.call().block();
System.out.println("Bob: " + bobReply.getTextContent());
Msg charlieReply = charlie.call().block();
System.out.println("Charlie: " + charlieReply.getTextContent());
}
// Hub is automatically closed, subscribers are cleaned up