Build the mesh topology: controller -> router -> agents -> controller.
(self)
| 98 | |
| 99 | @masf_hook(Node.Hook.BUILD) |
| 100 | def build(self): |
| 101 | """Build the mesh topology: controller -> router -> agents -> controller.""" |
| 102 | if self._is_built: |
| 103 | return |
| 104 | |
| 105 | # Build routing conditions |
| 106 | routes: dict[str, Callable] = {} |
| 107 | for i, agent_name in enumerate(self._agent_names): |
| 108 | routes[agent_name] = self._create_round_robin_condition(i) |
| 109 | |
| 110 | # Create router |
| 111 | router = self.create_node( |
| 112 | LogicSwitch, |
| 113 | name=f"{self.name}_router", |
| 114 | routes=routes, |
| 115 | ) |
| 116 | |
| 117 | # Create agent nodes |
| 118 | for i, agent_tpl in enumerate(self._agent_templates): |
| 119 | agent_name = self._agent_names[i] |
| 120 | |
| 121 | # Combine instructions |
| 122 | existing_instructions = agent_tpl.prototype_config.get("instructions", "") |
| 123 | combined_instructions = ( |
| 124 | f"{self._graph_instructions}\n{existing_instructions}".strip() |
| 125 | if self._graph_instructions else existing_instructions |
| 126 | ) |
| 127 | |
| 128 | # Create agent with enhanced config |
| 129 | agent = self.create_node( |
| 130 | agent_tpl.node_cls, |
| 131 | name=agent_name, |
| 132 | **{ |
| 133 | **agent_tpl.render_config(), |
| 134 | "instructions": combined_instructions, |
| 135 | "memories": [self._shared_memory], |
| 136 | "model": self._model, |
| 137 | }, |
| 138 | ) |
| 139 | |
| 140 | # Wire: router -> agent |
| 141 | self.create_edge(sender=router, receiver=agent) |
| 142 | |
| 143 | # Wire: agent -> controller |
| 144 | self.edge_to_controller(sender=agent) |
| 145 | |
| 146 | # Wire: controller -> router |
| 147 | self.edge_from_controller(receiver=router) |
| 148 | |
| 149 | super().build() |
| 150 | |
| 151 | @property |
| 152 | def agent_names(self) -> list[str]: |
no test coverage detected