Class CodeExecutor
java.lang.Object
ai.nervemind.app.executor.CodeExecutor
- All Implemented Interfaces:
NodeExecutor
Executor for the "code" node type - executes dynamic JavaScript or Python
code.
This executor enables custom logic within workflows by running user-defined code snippets using GraalVM Polyglot for multi-language support. It provides a sandboxed execution environment with access to workflow data.
Node Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| code | String | "" | The code to execute |
| language | String | "js" | Language: "js", "javascript", "py", or "python" |
Available Bindings
The following variables are available within the code:
- $input / input - Object containing data from previous nodes
- $node / node - Object containing node parameters
Output Handling
- If the code returns an object with members, each member is added to output
- If the code returns a non-null primitive, it's stored under key "result"
- All input data is preserved in output (merged with returned values)
Example Usage (JavaScript)
// Access input data
const data = $input.previousNodeOutput;
// Transform data
return {
processed: data.map(item => item.toUpperCase()),
count: data.length,
timestamp: new Date().toISOString()
};
Example Usage (Python)
# Access input data
data = input.get('previousNodeOutput', [])
# Transform data
return {
'processed': [item.upper() for item in data],
'count': len(data),
'timestamp': __import__('datetime').datetime.now().isoformat()
}
Security Notes
Code execution is sandboxed with allowAllAccess(false) to prevent
access to the host system. However, be cautious with user-provided code.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCodeExecutor(List<ScriptExecutionStrategy> strategyList, SettingsService settingsService) Create a new CodeExecutor with available script execution strategies. -
Method Summary
Modifier and TypeMethodDescriptionexecute(ai.nervemind.common.domain.Node node, Map<String, Object> input, ExecutionService.ExecutionContext context) Executes the business logic for this node type.Get available script languages.Unique identifier for the node type this executor handles.
-
Constructor Details
-
CodeExecutor
Create a new CodeExecutor with available script execution strategies.- Parameters:
strategyList- list of available strategies (auto-injected by Spring)settingsService- settings service for Python mode configuration
-
-
Method Details
-
execute
public Map<String,Object> execute(ai.nervemind.common.domain.Node node, Map<String, Object> input, ExecutionService.ExecutionContext context) Description copied from interface:NodeExecutorExecutes the business logic for this node type.- Specified by:
executein interfaceNodeExecutor- Parameters:
node- The node definition containing parameters and configuration. UseNode.parameters()to access user settings.input- Combined output from all upstream nodes that connected to this node. For simple flows, this contains the direct predecessor's output. For merge nodes, it contains combined data.context- Verification context providing access to workflow-scoped services, logger, and execution metadata.- Returns:
- A Map containing the results of this node's execution.
Keys in this map become available variables for downstream nodes.
Note: Returningnullis treated as an empty map.
-
getAvailableLanguages
-
getNodeType
Description copied from interface:NodeExecutorUnique identifier for the node type this executor handles. This must match the 'type' field in the JSON definition of the node.- Specified by:
getNodeTypein interfaceNodeExecutor- Returns:
- The unique type string (e.g., "httpRequest", "llmChat").
-