Package com.tnsai.agents.hierarchy
Class AgentHierarchyManager
java.lang.Object
com.tnsai.agents.hierarchy.AgentHierarchyManager
Manages hierarchical relationships between agents.
This class extracts parent-child hierarchy management from the Agent class, providing methods for:
- Parent-child relationship management (get/set parent, add/remove children)
- Task delegation to child agents
- Task escalation to parent agent
Usage:
// In Agent constructor
this.hierarchyManager = new AgentHierarchyManager(this);
// Delegate hierarchy methods
public Agent getParent() {
return hierarchyManager.getParent();
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for accessing Agent internals needed for hierarchy management. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new hierarchy manager for the given agent context. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a child agent to this agent's hierarchy.voidaddChildren(Agent... children) Adds multiple child agents at once.voidDelegates an action to a child agent.voidescalateToParent(String actionName, Map<String, Object> parameters) Escalates an action to the parent agent.Returns the child agents in a hierarchical structure.Returns the parent agent in a hierarchical structure.voidremoveChild(Agent child) Removes a child agent from this agent's hierarchy.voidSets the parent agent in a hierarchical structure.
-
Constructor Details
-
AgentHierarchyManager
Creates a new hierarchy manager for the given agent context.- Parameters:
context- the agent context providing access to agent internals
-
-
Method Details
-
getParent
Returns the parent agent in a hierarchical structure.- Returns:
- parent agent, or null if this is a root agent
-
setParent
Sets the parent agent in a hierarchical structure.This method automatically maintains bidirectional consistency:
- Removes this agent from the previous parent's children (if any)
- Adds this agent to the new parent's children list
- Parameters:
parent- the new parent agent (null to make this a root agent)
-
getChildren
Returns the child agents in a hierarchical structure.- Returns:
- unmodifiable list of child agents (never null)
-
addChild
Adds a child agent to this agent's hierarchy.This method automatically maintains bidirectional consistency by setting this agent as the child's parent.
Example (building a team):
Agent supervisor = new SupervisorAgent(); Agent developer = new DeveloperAgent(); Agent tester = new TesterAgent(); supervisor.addChild(developer); supervisor.addChild(tester); // supervisor.getChildren() contains both developer and tester // developer.getParent() == supervisor- Parameters:
child- the child agent to add
-
addChildren
Adds multiple child agents at once.Example:
supervisor.addChildren(developer, tester, designer);- Parameters:
children- the child agents to add
-
removeChild
Removes a child agent from this agent's hierarchy.- Parameters:
child- the child agent to remove
-
delegateToChild
Delegates an action to a child agent.Sends a task request message to the specified child agent, asking it to execute the named action with the given parameters.
Example:
// Supervisor delegates coding task to developer supervisor.delegateToChild(developer.id(), "writeCode", Map.of( "task", "Implement user authentication", "language", "Java" ));- Parameters:
childId- the ID of the child agentactionName- the action to executeparameters- the action parameters- Throws:
IllegalStateException- if no communication manager or child not found
-
escalateToParent
Escalates an action to the parent agent.Sends a task request message to the parent agent, asking it to handle the named action. Useful when:
- The child lacks permissions for an action
- The task requires coordination across siblings
- An error requires higher-level handling
Example:
// Developer escalates to supervisor when blocked developer.escalateToParent("requestApproval", Map.of( "reason", "Deployment to production requires sign-off" ));- Parameters:
actionName- the action to executeparameters- the action parameters- Throws:
IllegalStateException- if no parent or no communication manager
-