Route the query based on tool selection results.
(self)
| 431 | raise |
| 432 | |
| 433 | async def route_query_based_on_tools(self): |
| 434 | """Route the query based on tool selection results.""" |
| 435 | |
| 436 | # Check if we have tool routing results |
| 437 | if not hasattr(self, 'tool_routing_results') or not self.tool_routing_results: |
| 438 | # No tool routing results, falling back to get_ranked_answers |
| 439 | await self.get_ranked_answers() |
| 440 | return |
| 441 | |
| 442 | top_tool = self.tool_routing_results[0] |
| 443 | tool = top_tool['tool'] |
| 444 | tool_name = tool.name |
| 445 | params = top_tool['result'] |
| 446 | |
| 447 | # Selected tool: {tool_name} with score: {top_tool.get('score', 0)} |
| 448 | # Tool handler class: {tool.handler_class} |
| 449 | |
| 450 | # Check if tool has a handler class defined |
| 451 | if tool.handler_class: |
| 452 | try: |
| 453 | # For non-search tools, clear any items that FastTrack might have populated |
| 454 | if tool_name != "search": |
| 455 | # Clearing items for non-search tool |
| 456 | self.final_retrieved_items = [] |
| 457 | self.retrieved_items = [] |
| 458 | |
| 459 | # Dynamic import of handler module and class |
| 460 | module_path, class_name = tool.handler_class.rsplit('.', 1) |
| 461 | # Importing handler class |
| 462 | module = importlib.import_module(module_path) |
| 463 | handler_class = getattr(module, class_name) |
| 464 | |
| 465 | # Instantiate and execute handler |
| 466 | # Creating handler instance |
| 467 | handler_instance = handler_class(params, self) |
| 468 | |
| 469 | # Standard handler pattern with do() method |
| 470 | # Executing handler's do() method |
| 471 | await handler_instance.do() |
| 472 | # Handler completed |
| 473 | |
| 474 | except Exception as e: |
| 475 | logger.error(f"ERROR executing {tool_name}: {e}") |
| 476 | import traceback |
| 477 | traceback.print_exc() |
| 478 | # Fall back to search |
| 479 | # Falling back to get_ranked_answers |
| 480 | await self.get_ranked_answers() |
| 481 | else: |
| 482 | # Default behavior for tools without handlers (like search) |
| 483 | # Tool has no handler class, using get_ranked_answers |
| 484 | await self.get_ranked_answers() |
no test coverage detected