8. Entity Scripts and Advanced Extensions
On top of the built-in mathematical formula engine, MindLogic provides an even more powerful feature: Entity Scripts. You can write native JavaScript code to dynamically intercept and modify the state of a node during inference calculation, enabling high-level logic interactions.
What is an Entity Script?
At the very bottom of the Inspector panel, you will see a multi-line code input box named "Entity Script". During the forward propagation of the graph, after the engine aggregates all upstream input variables and before executing the current node's output formulas, it will automatically execute this script.
The node Object
Within the context of the entity script, the system exposes a global proxy object: node. This object represents the "entity currently being calculated".
By reading and assigning values to the properties of the node object, you can utilize upstream variables to change the node's display and behavior.
Supported Writable Properties
node.id(Number): The unique ID of the current node (Read-only).node.title(String): The main title of the node.node.customClassName(String): The custom class name of the node.node.classID(String): The underlying category ID of the node. For example, a built-ingoalor the unique ID of a custom category (which can be viewed and copied in the Domain Inspector on the right). Modifying this property allows you to dynamically change the node's category.node.confidence(Number, 0~1): The confidence state of the node.node.annotation(String): The notes/annotations of the node.node.symbolName(String): The node's icon (SF Symbols name, e.g.,star.fill).node.frameType(String): The shape of the frame. Supported values includenone,roundedRectangle,capsule,circle,diamond,hexagon, etc.node.contentType(String): The content type. Supported values includeplainText,markdown,image.node.imagePath(String): Whennode.contentTypeis set toimage, this property specifies the local path or remote URL of the image to display (e.g.,https://example.com/img.png).node.outputs(Dictionary Object): Store any new variables you want to pass downstream through the script!
Accessing Input Parameters
Properties passed down from upstream nodes (Outputs) or custom properties of the current node are automatically injected into the JS engine as global variables. You can read them directly by their variable names:
- Direct Global Variables: Whether there is an upstream parameter named
cost, or the current node itself has defined a custom property namedcost, you can just typecostto get its value. If multiple nodes provide parameters with the same name, typingcostwill yield their sum (for numbers) or the first encountered value (for strings). The custom properties of the current node are also aggregated into this total. - ID Indexing: These global variables also act as objects. You can precisely fetch the value passed from a specific node by its display ID (the number in the top right corner). For example,
cost[1]gets thecostfrom the node with ID 1 (which could be an upstream node or the current node itself). - Raw Collections: The system also injects an
inputsarray (containing all dictionary objects from upstream) and aselfdictionary (specifically containing the custom properties of the current node), which you can iterate over for advanced operations (for example, usingself.costto strictly get the current node'scost).
Note: If you want to access the current node's basic information (such as title, shape, etc.), please read the properties of the node object directly (e.g., node.title).
Script Examples
1. Dynamic Warning State
When the sum of the upstream cost parameters exceeds a threshold, automatically turn the current node into a danger warning node:
// Assuming an upstream variable named 'cost' flows in
if (SUM(cost) > 1000) {
node.title = "π¨ Cost Overrun Alert!";
node.confidence = 0.9;
node.frameType = "diamond"; // Change shape to diamond
node.outputs["isOverBudget"] = true; // Pass judgment to downstream
} else {
node.title = "Cost Safe";
node.outputs["isOverBudget"] = false;
}
2. Dynamic Display Format
If a specific external input exists, convert the node to a rich text markdown display:
if (ISBLANK(reportData)) {
node.title = "No Report Data";
} else {
node.contentType = "markdown";
node.outputs["markdownOutput"] = "# Report Summary\nThe data is very healthy!";
}
Important Notes
- Side-Effect Overwrite Mechanism: If you hardcode
node.title = "Something"in your script, every time the canvas refreshes or recalculates, the script's assignment will forcefully overwrite any title you manually edited on the canvas. Therefore, it is recommended to use conditional statements (if...else) to precisely control when to change properties. - Environment Isolation: The script for each node runs in a completely isolated scope containing only the current calculation context. You cannot directly manipulate other nodes on the canvas; you can only pass data downstream via
node.outputs. This ensures absolute unidirectional data flow and determinism for the inference engine.
