Package com.tnsai.agents.groups
Interface AgentGroup
public interface AgentGroup
AgentGroup - Core abstraction for collections of cooperating agents.
An AgentGroup provides a structured way to organize multiple agents that work together toward common goals. It manages membership, lifecycle, communication, and coordination patterns.
Key Concepts
- Membership: Dynamic agent join/leave with policy enforcement
- Lifecycle: Formation → Active → Dissolving → Dissolved
- Communication: Intra-group messaging via GroupEventBus
- Coordination: Scatter/gather, leader election, task assignment
Usage Example
AgentGroup group = DefaultAgentGroup.builder()
.name("Research Team")
.policy(GroupPolicy.builder()
.maxMembers(5)
.memberTimeout(Duration.ofMinutes(30))
.build())
.build();
group.addMember(agent1);
group.addMember(agent2);
group.start();
// Scatter a message to all members
Map<String, String> responses = group.scatter("Analyze this data");
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddLifecycleListener(GroupLifecycleListener listener) Adds a lifecycle listener.booleanAdds an agent to this group.voidaddMembershipListener(MembershipListener listener) Adds a membership listener.assignTask(GroupTask task) Assigns a task to a member based on the configured assignment strategy.voidbroadcast(AgentMessage message) Broadcasts a message to all members.default voidBroadcasts a simple text message to all members.static AgentGroupBuilderbuilder()Creates a new builder for AgentGroup.voiddissolve()Dissolves the group permanently.Elects a leader among members using the configured strategy.electLeader(LeaderElectionStrategy strategy) Elects a leader using a specific strategy.findMembers(Predicate<Agent> predicate) Finds members matching a predicate.Returns optional description of this group's purpose.Returns the event bus for intra-group communication.getId()Returns the unique identifier of this group.Returns the current leader if one has been elected.Returns the current lifecycle state of this group.Gets a member by ID.default intReturns the number of current members.Returns the current members of this group.Returns the membership manager for advanced membership operations.Returns group metrics snapshot.getName()Returns the human-readable name of this group.Returns the policy governing this group's behavior.booleanChecks if an agent is a member of this group.default booleanisActive()Checks if the group is active and accepting work.default booleanChecks if the group is dissolved and cannot be used.voidRemoves a lifecycle listener.booleanremoveMember(String agentId) Removes an agent from this group.voidremoveMembershipListener(MembershipListener listener) Removes a membership listener.Scatters a prompt to all members and gathers responses.Scatters a prompt to all members with custom timeout.scatterAsync(String prompt) Async version of scatter.voidsetPolicy(GroupPolicy policy) Updates the group policy.voidstart()Starts the group, transitioning from FORMED to ACTIVE.voidstop()Stops the group gracefully, allowing in-flight operations to complete.voidstopNow()Stops the group immediately, canceling in-flight operations.
-
Method Details
-
getId
String getId()Returns the unique identifier of this group.- Returns:
- group ID (UUID format)
-
getName
String getName()Returns the human-readable name of this group.- Returns:
- group name
-
getDescription
Returns optional description of this group's purpose.- Returns:
- description or empty if not set
-
getPolicy
GroupPolicy getPolicy()Returns the policy governing this group's behavior.- Returns:
- group policy
-
setPolicy
Updates the group policy. Some policy changes may not take effect until the group is restarted.- Parameters:
policy- new policy- Throws:
IllegalStateException- if group is dissolved
-
getMembers
Returns the current members of this group.- Returns:
- unmodifiable list of member agents
-
getMemberCount
default int getMemberCount()Returns the number of current members.- Returns:
- member count
-
addMember
Adds an agent to this group.- Parameters:
agent- agent to add- Returns:
- true if added, false if already a member or policy rejected
- Throws:
IllegalStateException- if group is dissolvedGroupPolicyViolationException- if policy constraints are violated
-
removeMember
Removes an agent from this group.- Parameters:
agentId- ID of agent to remove- Returns:
- true if removed, false if not a member
- Throws:
IllegalStateException- if group is dissolved
-
hasMember
Checks if an agent is a member of this group.- Parameters:
agentId- agent ID to check- Returns:
- true if member
-
getMember
Gets a member by ID.- Parameters:
agentId- agent ID- Returns:
- agent if found
-
findMembers
Finds members matching a predicate.- Parameters:
predicate- filter condition- Returns:
- matching members
-
getLifecycle
GroupLifecycle getLifecycle()Returns the current lifecycle state of this group.- Returns:
- lifecycle state
-
start
void start()Starts the group, transitioning from FORMED to ACTIVE.- Throws:
IllegalStateException- if not in FORMED stateGroupPolicyViolationException- if minimum members not met
-
stop
void stop()Stops the group gracefully, allowing in-flight operations to complete. -
stopNow
void stopNow()Stops the group immediately, canceling in-flight operations. -
dissolve
void dissolve()Dissolves the group permanently. Members are removed and the group cannot be restarted. -
isActive
default boolean isActive()Checks if the group is active and accepting work.- Returns:
- true if active
-
isDissolved
default boolean isDissolved()Checks if the group is dissolved and cannot be used.- Returns:
- true if dissolved
-
getEventBus
GroupEventBus getEventBus()Returns the event bus for intra-group communication.- Returns:
- group event bus
-
broadcast
Broadcasts a message to all members.- Parameters:
message- message to broadcast
-
broadcast
Broadcasts a simple text message to all members.- Parameters:
content- message content
-
scatter
Scatters a prompt to all members and gathers responses. Uses default timeout from policy.- Parameters:
prompt- prompt to scatter- Returns:
- map of agent ID to response
-
scatter
Scatters a prompt to all members with custom timeout.- Parameters:
prompt- prompt to scattertimeout- timeout for responses- Returns:
- map of agent ID to response
-
scatterAsync
Async version of scatter.- Parameters:
prompt- prompt to scatter- Returns:
- future completing with responses
-
getMembershipManager
MembershipManager getMembershipManager()Returns the membership manager for advanced membership operations.- Returns:
- membership manager
-
electLeader
Elects a leader among members using the configured strategy.- Returns:
- elected leader, or empty if election failed
-
electLeader
Elects a leader using a specific strategy.- Parameters:
strategy- election strategy- Returns:
- elected leader, or empty if election failed
-
getLeader
Returns the current leader if one has been elected.- Returns:
- current leader
-
assignTask
Assigns a task to a member based on the configured assignment strategy.- Parameters:
task- task to assign- Returns:
- assigned agent, or empty if no suitable agent found
-
getMetrics
GroupMetrics getMetrics()Returns group metrics snapshot.- Returns:
- metrics
-
addLifecycleListener
Adds a lifecycle listener.- Parameters:
listener- listener to add
-
removeLifecycleListener
Removes a lifecycle listener.- Parameters:
listener- listener to remove
-
addMembershipListener
Adds a membership listener.- Parameters:
listener- listener to add
-
removeMembershipListener
Removes a membership listener.- Parameters:
listener- listener to remove
-
builder
Creates a new builder for AgentGroup.- Returns:
- new builder instance
-