classRuleRouter(AgentRouter):"""Router using predefined rules."""def__init__(self,pool:AgentPool,config:RoutingConfig):self.pool=poolself.config=configasyncdefdecide(self,message:str)->Decision:"""Make decision based on configured rules."""msg=messageifself.config.case_sensitiveelsemessage.lower()# Check each rule in priority orderforruleinsorted(self.config.rules,key=lambdar:r.priority):keyword=rule.keywordifself.config.case_sensitiveelserule.keyword.lower()ifkeywordnotinmsg:continue# Skip if target doesn't existifrule.targetnotinself.pool.list_agents():msg="Target agent %s not available for rule: %s"logger.debug(msg,rule.target,rule.keyword)continue# Skip if capability required but not availableifrule.requires_capability:agent=self.pool.get_agent(rule.target)ifnotagent.context.capabilities.has_capability(rule.requires_capability):msg="Agent %s missing required capability: %s"logger.debug(msg,rule.target,rule.requires_capability)continue# Create appropriate decision using base class methodsifrule.wait_for_response:returnself.get_wait_decision(target=rule.target,reason=rule.reason)returnself.get_route_decision(target=rule.target,reason=rule.reason)# Use default route if configuredifself.config.default_target:returnself.get_wait_decision(target=self.config.default_target,reason=self.config.default_reason,)# End if no route foundreturnself.get_end_decision(reason="No matching rule or default route")
asyncdefdecide(self,message:str)->Decision:"""Make decision based on configured rules."""msg=messageifself.config.case_sensitiveelsemessage.lower()# Check each rule in priority orderforruleinsorted(self.config.rules,key=lambdar:r.priority):keyword=rule.keywordifself.config.case_sensitiveelserule.keyword.lower()ifkeywordnotinmsg:continue# Skip if target doesn't existifrule.targetnotinself.pool.list_agents():msg="Target agent %s not available for rule: %s"logger.debug(msg,rule.target,rule.keyword)continue# Skip if capability required but not availableifrule.requires_capability:agent=self.pool.get_agent(rule.target)ifnotagent.context.capabilities.has_capability(rule.requires_capability):msg="Agent %s missing required capability: %s"logger.debug(msg,rule.target,rule.requires_capability)continue# Create appropriate decision using base class methodsifrule.wait_for_response:returnself.get_wait_decision(target=rule.target,reason=rule.reason)returnself.get_route_decision(target=rule.target,reason=rule.reason)# Use default route if configuredifself.config.default_target:returnself.get_wait_decision(target=self.config.default_target,reason=self.config.default_reason,)# End if no route foundreturnself.get_end_decision(reason="No matching rule or default route")