Class CodeExecutor

java.lang.Object
ai.nervemind.app.executor.CodeExecutor
All Implemented Interfaces:
NodeExecutor

@Component public class CodeExecutor extends Object implements 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

Code node configuration 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 Details

    • CodeExecutor

      public CodeExecutor(List<ScriptExecutionStrategy> strategyList, SettingsService settingsService)
      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: NodeExecutor
      Executes the business logic for this node type.
      Specified by:
      execute in interface NodeExecutor
      Parameters:
      node - The node definition containing parameters and configuration. Use Node.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: Returning null is treated as an empty map.
    • getAvailableLanguages

      public Map<String,String> getAvailableLanguages()
      Get available script languages.
      Returns:
      map of language ID to display name
    • getNodeType

      public String getNodeType()
      Description copied from interface: NodeExecutor
      Unique identifier for the node type this executor handles. This must match the 'type' field in the JSON definition of the node.
      Specified by:
      getNodeType in interface NodeExecutor
      Returns:
      The unique type string (e.g., "httpRequest", "llmChat").