Skip to content

Self-improving AI Employees for everyone

fix(handlers/tools): plumb canvas state into LLM tool-call NodeContext

trohitge856ba86023c
diff --git a/server/services/handlers/tools.py b/server/services/handlers/tools.py
index c9d3b7b4..59173524 100644
--- a/server/services/handlers/tools.py
+++ b/server/services/handlers/tools.py
@@ -145,8 +145,19 @@ async def _dispatch_tool(tool_name: str, tool_args: Dict[str, Any],
node_type = config.get('node_type', '')
node_params = config.get('parameters', {})
- # Execution context available to all tool handlers (workspace, etc.)
- context = {'workspace_dir': config.get('workspace_dir', '')}
+ # Execution context for tool handlers. Includes canvas state
+ # (nodes/edges/workflow_id) that callers (chat_tool_executor in
+ # ai.py / rlm adapter / deepagents adapter) plumb in via `config`.
+ # Tools that need canvas awareness (e.g. agentBuilder.inspect_canvas)
+ # read these via NodeContext.nodes / .edges / .workflow_id; tools
+ # that don't simply ignore them.
+ context = {
+ 'workspace_dir': config.get('workspace_dir', ''),
+ 'nodes': config.get('nodes', []),
+ 'edges': config.get('edges', []),
+ 'workflow_id': config.get('workflow_id'),
+ 'parent_node_id': config.get('parent_node_id'),
+ }
logger.info("[Tool] Executing '%s' (node_type=%s, workspace=%s)", tool_name, node_type, context['workspace_dir'])
diff --git a/server/tests/test_status_broadcasts.py b/server/tests/test_status_broadcasts.py
index c3ccf39f..a3270045 100644
--- a/server/tests/test_status_broadcasts.py
+++ b/server/tests/test_status_broadcasts.py
@@ -97,6 +97,15 @@ _LEGACY_RAW_DICT_CALLSITES: FrozenSet[str] = frozenset({
# Task-delegation completion. Migration would namespace as
# ``agent.task.{succeeded,failed}``; matched by taskTrigger.
"services/handlers/tools.py",
+ # Agent Builder canvas-mutation broadcast. Emits
+ # ``workflow_ops_apply`` carrying a flat
+ # ``{workflow_id, caller_node_id, operations}`` shape consumed by
+ # ``client/src/hooks/useWorkflowOpsListener.ts``. Migration to
+ # WorkflowEvent would change the wire format (envelope vs flat),
+ # requiring a parallel rewrite of the frontend listener +
+ # invariant test fixtures. Deferred — typed-envelope migration
+ # tracked alongside the credentials.* / agent.task.* migrations.
+ "nodes/tool/agent_builder.py",
})