| 49 | self._tool_engine.register_executor("noop", NoOpExecutor()) |
| 50 | |
| 51 | async def execute(self, node: Node, inputs: dict[str, Any], context: dict[str, Any]) -> Any: |
| 52 | config = node.config if isinstance(node.config, dict) else {} |
| 53 | tool_id = _extract_tool_id(config) |
| 54 | if tool_id is None: |
| 55 | raise DomainError("tool node requires config.tool_id") |
| 56 | |
| 57 | params_raw = config.get("params", config.get("args", config.get("arguments", {}))) |
| 58 | if params_raw is None: |
| 59 | params: dict[str, Any] = {} |
| 60 | elif isinstance(params_raw, dict): |
| 61 | params = params_raw |
| 62 | else: |
| 63 | raise DomainError("tool node config.params must be an object") |
| 64 | |
| 65 | timeout_raw = config.get("timeout") |
| 66 | timeout = float(timeout_raw) if isinstance(timeout_raw, int | float) else 30.0 |
| 67 | if timeout <= 0: |
| 68 | timeout = 30.0 |
| 69 | |
| 70 | # Load tool by ID (authoritative), then execute via ToolEngine. |
| 71 | session = self._session_factory() |
| 72 | try: |
| 73 | from src.infrastructure.database.repositories.tool_repository import ( |
| 74 | SQLAlchemyToolRepository, |
| 75 | ) |
| 76 | |
| 77 | tool = SQLAlchemyToolRepository(session).get_by_id(tool_id) |
| 78 | except NotFoundError as exc: |
| 79 | raise DomainError(f"tool not found: {tool_id}") from exc |
| 80 | finally: |
| 81 | try: |
| 82 | session.close() |
| 83 | except Exception: |
| 84 | pass |
| 85 | |
| 86 | if tool.status is ToolStatus.DEPRECATED: |
| 87 | raise DomainError(f"tool is deprecated: {tool_id}") |
| 88 | |
| 89 | # ToolEngine indexes by tool.name; we register the fetched Tool every time to ensure freshness. |
| 90 | self._tool_engine.register(tool) |
| 91 | |
| 92 | tool_context = ToolExecutionContext( |
| 93 | caller_type="workflow_node", |
| 94 | workflow_id=context.get("workflow_id") |
| 95 | if isinstance(context.get("workflow_id"), str) |
| 96 | else None, |
| 97 | timeout=timeout, |
| 98 | variables={ |
| 99 | "node_id": node.id, |
| 100 | "inputs": inputs, |
| 101 | "initial_input": context.get("initial_input"), |
| 102 | }, |
| 103 | ) |
| 104 | |
| 105 | result = await self._tool_engine.execute( |
| 106 | tool_name=tool.name, |
| 107 | params=params, |
| 108 | context=tool_context, |