Initialize the tool context manager.
(self, tool_names: Optional[List[str]] = None)
| 82 | self._variables_lock = asyncio.Lock() # Lock for get/set trainable variables |
| 83 | |
| 84 | async def initialize(self, tool_names: Optional[List[str]] = None): |
| 85 | """Initialize the tool context manager.""" |
| 86 | |
| 87 | # Register tool-related symbols for auto-injection in dynamic code |
| 88 | dynamic_manager.register_symbol("TOOL", TOOL) |
| 89 | dynamic_manager.register_symbol("Tool", Tool) |
| 90 | dynamic_manager.register_symbol("ToolResponse", ToolResponse) |
| 91 | |
| 92 | # Register tool context provider for automatic import injection |
| 93 | def tool_context_provider(): |
| 94 | """Provide tool-related imports for dynamic tool classes.""" |
| 95 | return { |
| 96 | "TOOL": TOOL, |
| 97 | "Tool": Tool, |
| 98 | "ToolResponse": ToolResponse, |
| 99 | } |
| 100 | dynamic_manager.register_context_provider("tool", tool_context_provider) |
| 101 | |
| 102 | # Initialize Faiss service for tool embedding |
| 103 | self._faiss_service = FaissService( |
| 104 | base_dir=self.base_dir, |
| 105 | model_name=self.model_name |
| 106 | ) |
| 107 | |
| 108 | # Load tools from TOOL registry |
| 109 | tool_configs = {} |
| 110 | registry_tool_configs: Dict[str, ToolConfig] = await self._load_from_registry() |
| 111 | tool_configs.update(registry_tool_configs) |
| 112 | |
| 113 | # Load tools from code |
| 114 | code_tool_configs: Dict[str, ToolConfig] = await self._load_from_code() |
| 115 | |
| 116 | # Merge code configs with registry configs, only override if code version is strictly greater |
| 117 | for tool_name, code_config in code_tool_configs.items(): |
| 118 | if tool_name in tool_configs: |
| 119 | registry_config = tool_configs[tool_name] |
| 120 | # Compare versions: only override if code version is strictly greater |
| 121 | if version_manager.compare_versions(code_config.version, registry_config.version) > 0: |
| 122 | logger.info(f"| 🔄 Overriding tool {tool_name} from registry (v{registry_config.version}) with code version (v{code_config.version})") |
| 123 | tool_configs[tool_name] = code_config |
| 124 | else: |
| 125 | logger.info(f"| 📌 Keeping tool {tool_name} from registry (v{registry_config.version}), code version (v{code_config.version}) is not greater") |
| 126 | # If versions are equal, update the history with registry config (which has real class, not dynamic) |
| 127 | if version_manager.compare_versions(code_config.version, registry_config.version) == 0: |
| 128 | # Replace the code config in history with registry config to preserve real class reference |
| 129 | if tool_name in self._tool_history_versions: |
| 130 | self._tool_history_versions[tool_name][registry_config.version] = registry_config |
| 131 | else: |
| 132 | # New tool from code, add it |
| 133 | tool_configs[tool_name] = code_config |
| 134 | |
| 135 | # Filter tools by names if provided |
| 136 | if tool_names is not None: |
| 137 | tool_configs = {name: tool_configs[name] for name in tool_names} |
| 138 | |
| 139 | # Build all tools concurrently with a concurrency limit |
| 140 | tool_names = list(tool_configs.keys()) |
| 141 | tasks = [ |
no test coverage detected