Build the hub-spoke topology with handoff routing.
(self)
| 111 | |
| 112 | @masf_hook(Node.Hook.BUILD) |
| 113 | def build(self): |
| 114 | """Build the hub-spoke topology with handoff routing.""" |
| 115 | if self._is_built: |
| 116 | return |
| 117 | |
| 118 | # Build routing conditions |
| 119 | routes: dict[str, Callable] = {} |
| 120 | |
| 121 | # Hub: route when no handoff pending |
| 122 | def route_to_hub(message: dict, attributes: dict) -> bool: |
| 123 | handoff = attributes.get("_handoff_to") |
| 124 | return handoff is None or handoff == "" or handoff not in self._spoke_names_set |
| 125 | routes[self._hub_name] = route_to_hub |
| 126 | |
| 127 | # Spokes: route when handoff matches |
| 128 | for spoke_name in self._spoke_names: |
| 129 | def make_spoke_condition(target: str) -> Callable: |
| 130 | def condition(message: dict, attributes: dict) -> bool: |
| 131 | return attributes.get("_handoff_to") == target |
| 132 | return condition |
| 133 | routes[spoke_name] = make_spoke_condition(spoke_name) |
| 134 | |
| 135 | # Create router |
| 136 | router = self.create_node( |
| 137 | LogicSwitch, |
| 138 | name=f"{self.name}_router", |
| 139 | routes=routes, |
| 140 | ) |
| 141 | |
| 142 | # Clear handoff after dispatch |
| 143 | def clear_handoff(_node: Node, _result: object, _message: dict) -> None: |
| 144 | self._attributes_store["_handoff_to"] = None |
| 145 | router.hooks.register(Node.Hook.MESSAGE_DISPATCH_OUT.AFTER, clear_handoff) |
| 146 | |
| 147 | # Create handoff tools |
| 148 | handoff_tools = [self._create_handoff_tool(sn) for sn in self._spoke_names] |
| 149 | |
| 150 | # Create hub agent |
| 151 | hub_existing_instructions = self._hub_template.prototype_config.get("instructions", "") |
| 152 | hub_combined_instructions = ( |
| 153 | f"{self._graph_instructions}\n{hub_existing_instructions}".strip() |
| 154 | if self._graph_instructions else hub_existing_instructions |
| 155 | ) |
| 156 | hub_existing_tools = list(self._hub_template.prototype_config.get("tools", [])) |
| 157 | |
| 158 | hub_agent = self.create_node( |
| 159 | self._hub_template.node_cls, |
| 160 | name=self._hub_name, |
| 161 | **{ |
| 162 | **self._hub_template.render_config(), |
| 163 | "instructions": hub_combined_instructions, |
| 164 | "memories": [self._shared_memory], |
| 165 | "model": self._model, |
| 166 | "tools": hub_existing_tools + handoff_tools, |
| 167 | }, |
| 168 | ) |
| 169 | |
| 170 | # Track hub output |
nothing calls this directly
no test coverage detected