Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
refactor(plugin): Wave 11.D.2 — inline code-executor bodies
trohitge94f7c8345c1
diff --git a/server/nodes/code/_nodejs.py b/server/nodes/code/_nodejs.pynew file mode 100644index 00000000..1fa208e6--- /dev/null+++ b/server/nodes/code/_nodejs.py@@ -0,0 +1,22 @@+"""Shared Node.js executor client for JS/TS plugins.++Both ``javascript_executor.py`` and ``typescript_executor.py`` dispatch+through the same persistent Node.js server (see ``services/nodejs/``).+This helper owns the singleton client so both plugins share one HTTP+session.+"""++from __future__ import annotations++from typing import Optional++from services.nodejs_client import NodeJSClient++_client: Optional[NodeJSClient] = None+++def get_nodejs_client(base_url: str = "http://localhost:3020", timeout: int = 30) -> NodeJSClient:+ global _client+ if _client is None:+ _client = NodeJSClient(base_url, timeout)+ return _clientdiff --git a/server/nodes/code/javascript_executor.py b/server/nodes/code/javascript_executor.pyindex 59b471ae..6fd684c1 100644--- a/server/nodes/code/javascript_executor.py+++ b/server/nodes/code/javascript_executor.py@@ -18,13 +18,24 @@ class JavaScriptExecutorNode(CodeExecutorBase):@Operation("execute")async def execute_op(self, ctx: NodeContext, params: CodeExecutorParams) -> Any:- from services.handlers.code import handle_javascript_executor- outputs = ctx.raw.get("connected_outputs") or {}- response = await handle_javascript_executor(- node_id=ctx.node_id, node_type=self.type,- parameters=params.model_dump(by_alias=True),- context=ctx.raw, connected_outputs=outputs,+ """Inlined from handlers/code.py (Wave 11.D.2). Dispatches to the+ persistent Node.js server via the shared NodeJSClient singleton."""+ from ._nodejs import get_nodejs_client++ if not params.code.strip():+ raise RuntimeError("No code provided")+ input_data = dict(ctx.raw.get("connected_outputs") or {})+ input_data["workspace_dir"] = ctx.workspace_dir or ""++ result = await get_nodejs_client().execute(+ code=params.code,+ input_data=input_data,+ timeout=params.timeout * 1000,+ language="javascript",)- if response.get("success"):- return response.get("result") or response- raise RuntimeError(response.get("error") or "JavaScript executor failed")+ if not result.get("success"):+ raise RuntimeError(result.get("error") or "JavaScript executor failed")+ return {+ "output": result.get("output"),+ "console_output": result.get("console_output", ""),+ }diff --git a/server/nodes/code/python_executor.py b/server/nodes/code/python_executor.pyindex 12fe1a7e..2c5f9c2f 100644--- a/server/nodes/code/python_executor.py+++ b/server/nodes/code/python_executor.py@@ -18,13 +18,45 @@ class PythonExecutorNode(CodeExecutorBase):@Operation("execute")async def execute_op(self, ctx: NodeContext, params: CodeExecutorParams) -> Any:- from services.handlers.code import handle_python_executor- outputs = ctx.raw.get("connected_outputs") or {}- response = await handle_python_executor(- node_id=ctx.node_id, node_type=self.type,- parameters=params.model_dump(by_alias=True),- context=ctx.raw, connected_outputs=outputs,- )- if response.get("success"):- return response.get("result") or response- raise RuntimeError(response.get("error") or "Python executor failed")+ """Inlined from handlers/code.py (Wave 11.D.2).++ Executes user code in a restricted namespace with stdout capture.+ ``input_data`` exposes ``connected_outputs`` so upstream node+ results are reachable; ``workspace_dir`` is the per-workflow+ scratch directory.+ """+ import io+ import json as json_module+ import math++ if not params.code.strip():+ raise RuntimeError("No code provided")++ input_data = ctx.raw.get("connected_outputs") or {}+ stdout_capture = io.StringIO()++ def captured_print(*args, **kwargs):+ kwargs["file"] = stdout_capture+ print(*args, **kwargs)++ safe_builtins = {+ "abs": abs, "all": all, "any": any, "bool": bool,+ "dict": dict, "enumerate": enumerate, "filter": filter,+ "float": float, "int": int, "len": len, "list": list,+ "map": map, "max": max, "min": min, "print": captured_print,+ "range": range, "round": round, "set": set, "sorted": sorted,+ "str": str, "sum": sum, "tuple": tuple, "type": type, "zip": zip,+ "True": True, "False": False, "None": None,+ "math": math, "json": json_module,+ }+ namespace = {+ "__builtins__": safe_builtins,+ "input_data": input_data,+ "workspace_dir": ctx.workspace_dir or "",+ "output": None,+ }+ exec(params.code, namespace) # noqa: S102 — sandboxed namespace+ return {+ "output": namespace.get("output"),+ "console_output": stdout_capture.getvalue(),+ }diff --git a/server/nodes/code/typescript_executor.py b/server/nodes/code/typescript_executor.pyindex a61b7f74..0a28c394 100644--- a/server/nodes/code/typescript_executor.py+++ b/server/nodes/code/typescript_executor.py@@ -18,13 +18,23 @@ class TypeScriptExecutorNode(CodeExecutorBase):@Operation("execute")async def execute_op(self, ctx: NodeContext, params: CodeExecutorParams) -> Any:- from services.handlers.code import handle_typescript_executor- outputs = ctx.raw.get("connected_outputs") or {}- response = await handle_typescript_executor(- node_id=ctx.node_id, node_type=self.type,- parameters=params.model_dump(by_alias=True),- context=ctx.raw, connected_outputs=outputs,+ """Inlined from handlers/code.py (Wave 11.D.2)."""+ from ._nodejs import get_nodejs_client++ if not params.code.strip():+ raise RuntimeError("No code provided")+ input_data = dict(ctx.raw.get("connected_outputs") or {})+ input_data["workspace_dir"] = ctx.workspace_dir or ""++ result = await get_nodejs_client().execute(+ code=params.code,+ input_data=input_data,+ timeout=params.timeout * 1000,+ language="typescript",)- if response.get("success"):- return response.get("result") or response- raise RuntimeError(response.get("error") or "TypeScript executor failed")+ if not result.get("success"):+ raise RuntimeError(result.get("error") or "TypeScript executor failed")+ return {+ "output": result.get("output"),+ "console_output": result.get("console_output", ""),+ }