|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from cecli.commands.utils.base_command import BaseCommand |
| 4 | +from cecli.helpers.agents.service import AgentService, SubAgentInfo, SubAgentStatus |
| 5 | + |
| 6 | + |
| 7 | +class TreeNode: |
| 8 | + """A node in the agent hierarchy tree.""" |
| 9 | + |
| 10 | + def __init__(self, agent_info: SubAgentInfo): |
| 11 | + self.agent_info = agent_info |
| 12 | + self.children: List[TreeNode] = [] |
| 13 | + |
| 14 | + def add_child(self, child_node: "TreeNode"): |
| 15 | + self.children.append(child_node) |
| 16 | + |
| 17 | + |
| 18 | +class AgentTreeCommand(BaseCommand): |
| 19 | + """Command to display the hierarchy of active agents.""" |
| 20 | + |
| 21 | + NORM_NAME = "agent-tree" |
| 22 | + DESCRIPTION = "Display the hierarchy of active agents." |
| 23 | + |
| 24 | + @classmethod |
| 25 | + async def execute(cls, io, coder, args, **kwargs): |
| 26 | + """Execute the /agent-tree command.""" |
| 27 | + agent_service = AgentService.get_instance(coder) |
| 28 | + if not agent_service: |
| 29 | + io.tool_output("Agent service not available.") |
| 30 | + return |
| 31 | + |
| 32 | + # Collect all agents: primary coder + sub-agents from sub_agents dict |
| 33 | + all_infos: List[SubAgentInfo] = [] |
| 34 | + |
| 35 | + # Primary agent as a synthetic SubAgentInfo |
| 36 | + primary_coder = agent_service.coder |
| 37 | + primary_info = SubAgentInfo( |
| 38 | + name="primary", |
| 39 | + coder=primary_coder, |
| 40 | + parent_uuid=str(primary_coder.parent_uuid or ""), |
| 41 | + status=SubAgentStatus.RUNNING, |
| 42 | + ) |
| 43 | + all_infos.append(primary_info) |
| 44 | + |
| 45 | + # Sub-agents from the service's sub_agents dict |
| 46 | + for info in agent_service.sub_agents.values(): |
| 47 | + if info and info.coder: |
| 48 | + all_infos.append(info) |
| 49 | + |
| 50 | + if len(all_infos) <= 1: |
| 51 | + io.tool_output("No active sub-agents found.") |
| 52 | + return |
| 53 | + |
| 54 | + root_nodes = cls._build_tree(all_infos) |
| 55 | + tree_output = cls._render_tree(root_nodes) |
| 56 | + io.tool_output(tree_output) |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def _build_tree(cls, all_agents: List[SubAgentInfo]) -> List[TreeNode]: |
| 60 | + """Build the agent tree from a flat list of agents.""" |
| 61 | + nodes = {str(agent.coder.uuid): TreeNode(agent) for agent in all_agents} |
| 62 | + root_nodes = [] |
| 63 | + |
| 64 | + for agent in all_agents: |
| 65 | + parent_uuid = str(agent.coder.parent_uuid) |
| 66 | + if parent_uuid and parent_uuid in nodes: |
| 67 | + parent_node = nodes[parent_uuid] |
| 68 | + child_node = nodes[str(agent.coder.uuid)] |
| 69 | + parent_node.add_child(child_node) |
| 70 | + else: |
| 71 | + root_nodes.append(nodes[str(agent.coder.uuid)]) |
| 72 | + |
| 73 | + return root_nodes |
| 74 | + |
| 75 | + @classmethod |
| 76 | + def _render_tree(cls, nodes: List[TreeNode]) -> str: |
| 77 | + """Render the agent tree to a string.""" |
| 78 | + output = [] |
| 79 | + |
| 80 | + def render_node(node: TreeNode, prefix: str = "", is_last: bool = True): |
| 81 | + connector = "└── " if is_last else "├── " |
| 82 | + output.append( |
| 83 | + f"{prefix}{connector}{node.agent_info.name}" |
| 84 | + f" ({node.agent_info.status.value})" |
| 85 | + f" - {str(node.agent_info.coder.uuid)}" |
| 86 | + ) |
| 87 | + |
| 88 | + child_prefix = prefix + (" " if is_last else "│ ") |
| 89 | + for i, child in enumerate(node.children): |
| 90 | + render_node(child, child_prefix, i == len(node.children) - 1) |
| 91 | + |
| 92 | + for i, node in enumerate(nodes): |
| 93 | + render_node(node, is_last=i == len(nodes) - 1) |
| 94 | + |
| 95 | + return "\n".join(output) |
0 commit comments