Plugin JS
The Plugin JS provides a secure, sandboxed environment for executing JavaScript code within the Brain Framework. It offers a robust solution to run arbitrary JavaScript snippets safely.
Installation
- Install the required package:
Terminal window npm i @iqai/plugin-jsTerminal window pnpm add @iqai/plugin-jsTerminal window yarn add @iqai/plugin-js - Ensure you meet all the requirements here before using the plugin.
Overview
The Plugin JS comprises two primary components:
- Execution Service: Securely executes JavaScript code in an isolated virtual machine.
- Action Handler: Extracts code from messages and formats responses, enabling safe testing and experimentation with JavaScript snippets.
Configuration
Configure the plugin with the following parameters:
Param Name | Description | Default Value |
---|---|---|
memoryLimit | Maximum memory available to JavaScript execution (MB) | 128 |
timeout | Maximum execution time allowed (ms) | 5000 |
Usage
Basic Setup
import { createJsPlugin } from "@iqai/plugin-js";
// Initialize the plugin with default settingsconst plugin = await createJsPlugin();
Custom Setup
import { createJsPlugin } from "@iqai/plugin-js";
// Initialize the plugin with custom memory and timeout settingsconst plugin = await createJsPlugin({ memoryLimit: 256, // 256MB memory limit timeout: 10000 // 10-second timeout});
Once initialized, the plugin adds the JS_EXECUTE
action to your agent, which can be triggered with commands like βrun this JavaScriptβ or βexecute this codeβ.
Error Handling
The plugin handles various error scenarios, including:
- Syntax Errors: Detected during code compilation.
- Runtime Errors: Caught during code execution.
- Timeout Errors: Occur when execution exceeds the configured time limit.
- Memory Errors: Triggered when code exceeds the allocated memory.
- Catastrophic Errors: Severe VM failures that require environment recreation.
Security Considerations
The Plugin JS incorporates several security measures:
- Isolation: Code runs in a fully isolated virtual machine.
- Resource Limiting: Strict limits on memory and execution time.
- Console Access Only: No access to Node.js modules, filesystem, or network.
- Error Containment: Prevents errors from escaping the sandbox environment.
- Automatic Resource Cleanup: VM resources are disposed of after execution.
Always review your code before deploying in production environments.
Example Interaction
// Example: Run this JavaScriptconst numbers = [1, 2, 3, 4, 5];console.log(numbers.map(n => n * 2));return numbers.reduce((sum, n) => sum + n, 0);
// Expected Output:// Console: [2, 4, 6, 8, 10]// Result: 15
Learn More
For additional details on secure JavaScript execution in isolated environments, visit: