Custom Workflow Functions

Execute custom code in your AI workflow

Functions in MindStudio empower you to extend the capabilities of your workflows by running JavaScript or Python code directly within your automation. Functions are created within the Editor and are executed in a workflow via the Run Function Block.

When working with the Function Tab in MindStudio, the interface is designed to to write and test code, and supports dynamic inputs, configurations, and debugging features to ensure smooth execution.

Create a New Function

There are two primary ways to create a new function in MindStudio. Both approaches will create a new function in your Functions Folder, and open a blank function editor where you can specify the environment, write your code, configure inputs, and test the function.

From the Explorer

  1. Navigate to the Explorer panel on the left-hand side of the editor.

  2. Hover over the Functions folder.

  3. Click the + button or right-click and select New Function.

  4. A new function tab will open where you can begin writing your custom code.

From the Run Function Block

  1. Add a Run Function Block to your workflow.

  2. In the block configuration, click New... to create a new function.

  3. A new function tab will open, allowing you to define and configure the function.


Function Setup

Functions in MindStudio can be written in either JavaScript or Python, giving you flexibility to choose the language that best suits your needs. The function editor provides a modern development environment with syntax highlighting, auto-completion, and real-time error checking.

1. Configure Function Details

These details are displayed in the Function Details panel on the right. Provide a Name and optional Description for your function. Then select the programming environment for your function: JavaScript (Node.js) or Python.

Note: For Python functions, you can import external libraries to extend functionality.

2. Write the Function

Use the Code Tab to write the logic for your function. Utilize the available methods (see reference table below) to integrate your function seamlessly with configurations.

3. (Optional) Create Configurations

Use the Configurations Tab to define JSON for customizable settings that users can modify when implementing your function in their workflows. These settings can include input fields, drop-downs, toggles, and other UI elements that make your function more flexible and user-friendly.

Code Tab

The Code Tab is the central workspace for writing the logic of your function. Here, you can write code in either JavaScript or Python, depending on the selected environment. The editor features syntax highlighting, making the code more readable and easier to debug.

See available methods

Configuration Tab

The Configuration Tab enables developers to define a configuration JSON file for their function. This JSON allows you to set up customizable settings, such as text inputs, drop-downs, or other UI elements, that non-technical users can configure with when they add the function to the Run Function Block in workflows.

Configuration JSON Structure

Test Data Tab

The Test Data Tab is a dedicated space for verifying the behavior of your function with predefined inputs. You can simulate runtime variables such as ai.vars and ai.config by defining mock data to test different scenarios.

Data defined in the Test Tab will be used when executing the function with the Test button in the bottom right of your code editor.

This feature allows you to ensure that your function behaves as expected without needing to integrate it into a full workflow.

Test Data JSON Structure

The above JSON structure will allow you to read the following variable and config values when testing:

Right Hand Panel Controls

Function Details Panel

Displays key information about your function. Here, you can set the function’s name and description, which will be used to identify it in the Run Function Block. You can also select the environment—either JavaScript (Node.js) or Python—for your function.

Configuration Preview

Live preview of the configuration interface, giving you immediate feedback on how your JSON structure set the Configuration Tab will appear to users when they edit the block.

Quick Help Tab

Built-in reference guide. Displays the guide relevant to what you are editing.

Available Methods

Method
Description

ai.config

Object containing configuration variables defined in MindStudio

ai.vars

Object containing runtime variables defined by other functions or blocks.

ai.log(value)

Update the progress text for the user. If your function takes a long time to run, this can be helpful in communicating what is happening to the user.

ai.scrapeUrl(url)

Scrape the contents of a URL and return an object containing the text extracted from the page, the raw HTML, and some structured metadata (page title, description, resolved URL, thumbnail image URL).

ai.searchGoogle(query)

Search Google for a query and return the first page of results. Returns an object containing all the results as a block of text, as well as individually as an array of objects containing the title, description, and URL for each result.

ai.queryDataSource(dataSourceId, query, numResults)

Perform a query against a data source defined in a project. Returns a string result. If numResults is not provided, only one chunk will be returned.

ai.uploadFile(body)

Upload a file and return a URL. File must be a valid Base 64 data URL.

ai.concatenateAudio(urls)

Combine an array of URLs to audio files into one mp3.

ai.concatenateVideo(urls)

Combine an array of URLs to video files into one mp4.

Quick Examples

Assigning value to a variable

  • Get data from a Search Google function

  • Assign that data to the variable output

JavaScript

Python

Simple Input Field in Custom Function Config

Configuration Tab:

Access the value of this input field in the code like so:

JavaScript

Python

Execution Environments

Sandbox

Sandbox is the default execution environment for custom functions. MindStudio's Sandbox is optimized for speed and performance and should be preferred for most functions. For JavaScript, the Sandbox uses Isolates to run your code in secure environments. Python code is evaluated using Pyodide. In most cases, these details do not matter and the Sandbox will happily run any code you provide it.

However, complex or advanced functionality, like installing NPM packages, is not supported in Sandbox Execution. For cases where you need the full power of NodeJS or Python, you can use the Virtual Machine execution environment.

Virtual Machine

Virtual Machine (VM) execution gives you full access to NodeJS or Python, including third-party dependencies and anything else your function might need. However, this power comes at the cost of speed and latency. Where the Sandbox can return results in <50ms, starting a VM and installing packages will take, at minimum, around 5 seconds (and, depending on the workload and packages you add, could take even longer). This means you should be considerate about when and how you use VMs, especially in user-facing Agents where the added latency might lead to a degraded user experience.

When using VMs to execute your functions, you must provide a handler function to be invoked by the VM. In JavaScript, that handler must be a named export called handler . In Python, it must be a function called handler .

Example JavaScript Virtual Machine Function

Example Python Virtual Machine Function

Last updated