Register a tool class or instance. This will: - Create (or reuse) a tool instance - Create a `ToolConfig` - Store it as the current config and append to version history - Register the version in `version_manager` and FAISS index
(self,
tool_cls: Type[Tool],
tool_config_dict: Optional[Dict[str, Any]] = None,
override: bool = False,
version: Optional[str] = None)
| 420 | raise |
| 421 | |
| 422 | async def register(self, |
| 423 | tool_cls: Type[Tool], |
| 424 | tool_config_dict: Optional[Dict[str, Any]] = None, |
| 425 | override: bool = False, |
| 426 | version: Optional[str] = None) -> ToolConfig: |
| 427 | """Register a tool class or instance. |
| 428 | |
| 429 | This will: |
| 430 | - Create (or reuse) a tool instance |
| 431 | - Create a `ToolConfig` |
| 432 | - Store it as the current config and append to version history |
| 433 | - Register the version in `version_manager` and FAISS index |
| 434 | """ |
| 435 | |
| 436 | try: |
| 437 | if tool_config_dict is None: |
| 438 | # Fallback to global config by class name |
| 439 | tool_config_key = inflection.underscore(tool_cls.__name__) |
| 440 | tool_config_dict = config.get(tool_config_key, {}) |
| 441 | |
| 442 | # Instantiate tool immediately (register is a runtime operation) |
| 443 | try: |
| 444 | tool_instance = tool_cls(**tool_config_dict) |
| 445 | except Exception as e: |
| 446 | logger.error(f"| ❌ Failed to create tool instance for {tool_cls.__name__}: {e}") |
| 447 | raise ValueError(f"Failed to instantiate tool {tool_cls.__name__} with provided config: {e}") |
| 448 | |
| 449 | tool_name = tool_instance.name |
| 450 | tool_description = tool_instance.description |
| 451 | tool_metadata = tool_instance.metadata |
| 452 | # Get require_grad from tool_config_dict if provided, otherwise from tool_instance |
| 453 | tool_require_grad = tool_config_dict.get("require_grad", tool_instance.require_grad) if tool_config_dict and "require_grad" in tool_config_dict else tool_instance.require_grad |
| 454 | |
| 455 | # Get or generate version from version_manager |
| 456 | if version is None: |
| 457 | tool_version = await version_manager.get_version("tool", tool_name) |
| 458 | else: |
| 459 | tool_version = version |
| 460 | |
| 461 | # Get tool code |
| 462 | tool_code = dynamic_manager.get_source_code(tool_cls) |
| 463 | if not tool_code: |
| 464 | logger.warning(f"| ⚠️ Tool {tool_name} is dynamic but source code cannot be extracted") |
| 465 | |
| 466 | # Get tool parameters |
| 467 | tool_parameters = dynamic_manager.get_parameters(tool_cls) |
| 468 | tool_function_calling = dynamic_manager.build_function_calling(tool_name, tool_description, tool_parameters) |
| 469 | tool_text = dynamic_manager.build_text_representation(tool_name, tool_description, tool_parameters) |
| 470 | tool_args_schema = dynamic_manager.build_args_schema(tool_name, tool_parameters) |
| 471 | |
| 472 | # --- Build ToolConfig --- |
| 473 | tool_config = ToolConfig( |
| 474 | name=tool_name, |
| 475 | description=tool_description, |
| 476 | metadata=tool_metadata, |
| 477 | require_grad=tool_require_grad, |
| 478 | version=tool_version, |
| 479 | cls=tool_cls, |
nothing calls this directly
no test coverage detected