The Functions Microservice enables you to execute custom code (NodeJS or Python) within your Prisme.ai applications. This powerful capability allows you to extend the platform with custom logic, integrate with external systems, and implement complex business processes.

Overview

The Functions Microservice provides a secure, isolated environment for running custom code within the Prisme.ai platform. It supports:

  • NodeJS Functions: JavaScript code execution with access to selected built-in modules
  • Python Functions: Python code execution with standard library access
  • Shared Execution Context: Functions within the same workspace can call each other
  • Memory Management: Configurable resource limits and garbage collection
  • Dependency Management: Automatic installation of required packages

The Functions Microservice is designed with security in mind, running code in isolated environments with configurable resource limits to prevent abuse or resource exhaustion.

Installation Prerequisites

Before deploying the Functions Microservice, ensure you have:

NPM Registry Access

The service must have access to a npm registry for installing JavaScript dependencies. You can use the NPM_CONFIG_REGISTRY environment variable to specify your own registry.

Storage Volume

The service requires a volume for storing code, dependencies, and execution data. We recommend using volumes with high I/O performance as they may experience heavy usage when installing dependencies.

Configuration

Environment Variables

Configure the Functions Microservice with the following environment variables:

Resource Considerations

When deploying the Functions Microservice, consider the following resource recommendations:

Memory Allocation

Allocate sufficient memory based on your expected workload:

  • Minimum: 1GB
  • Recommended: 2GB or more for production environments
  • Consider additional memory if functions will process large datasets

CPU Resources

Ensure adequate CPU resources:

  • Minimum: 0.5 vCPU
  • Recommended: 1 vCPU or more for production environments
  • Functions with complex calculations may benefit from additional CPU

Disk Space

Plan for storage requirements:

  • Minimum: 1GB
  • Recommended: 5GB or more for environments with many dependencies
  • Consider storage needs for dependency caching and function code

Network Configuration

Ensure proper network setup:

  • Accessible by other microservices (especially API Gateway)
  • Access to required external resources (NPM registry, etc.)
  • Consider network policies for security

Microservice Testing

After deploying the Functions Microservice, verify its operation with these simple tests:

1

Create a Test Function

Create a function workspace named test containing a hello function:

curl --location 'http://localhost:4000/v2/functions/test' \
--header 'Content-Type: application/json' \
--data '{
    "functions": {
        "hello": {
            "code": "return \"Hello \" + name;",
            "parameters": {
                "name": {
                    "type": "string"
                }
            }
        }
    }
}'

If successful, the same body should be returned in the response.

2

Execute the Function

Run the function with a test parameter:

curl --location 'http://localhost:4000/v2/functions/test/run/hello' \
--header 'Content-Type: application/json' \
--data '{
    "parameters": {
        "name": "world"
    }
}'

If successful, you should receive a response like:

{
    "result": "Hello world",
    "logs": [],
    "duration": 38
}
3

Verify Resource Limits

Test that resource limits are enforced by creating a function that attempts to use excessive resources:

curl --location 'http://localhost:4000/v2/functions/test' \
--header 'Content-Type: application/json' \
--data '{
    "functions": {
        "resourceTest": {
            "code": "const array = []; while(true) { array.push(new Array(1000000).fill(\"test\")); }",
            "parameters": {}
        }
    }
}'

When executing this function, it should terminate due to resource limits or timeout:

curl --location 'http://localhost:4000/v2/functions/test/run/resourceTest' \
--header 'Content-Type: application/json' \
--data '{
    "parameters": {}
}'

If all tests pass, congratulations! Your Functions Microservice is up and running correctly.

Advanced Features

The Functions Microservice includes several advanced capabilities that enhance its flexibility and power:

Integration with Prisme.ai

The Functions Microservice integrates with other Prisme.ai components:

AI Builder

Within AI Builder, you can create custom automations that execute functions, allowing you to extend workflows with custom logic.

AI Knowledge

Custom functions can be used as tools in AI Knowledge agents, enabling them to perform specialized operations like data transformation or external API calls.

AI Store

Functions can power features in agents published to the AI Store, adding custom capabilities to shared agents.

Custom Code App

The Custom Code app provides a user-friendly interface for managing, testing, and monitoring functions.

Security Considerations

When deploying and using the Functions Microservice, keep these security considerations in mind:

Code Isolation

Functions run in isolated environments to prevent interference between different workspaces and functions.

Resource Limits

Configure appropriate resource limits to prevent denial-of-service attacks or resource exhaustion.

Access Control

Implement proper authentication and authorization to control who can create and execute functions.

Dependency Scanning

Consider scanning dependencies for vulnerabilities before allowing their installation.

Function code is executed in a restricted environment, but it still has access to specified Node.js built-in modules and network resources. Be careful when allowing users to create functions, and consider implementing additional security controls for sensitive environments.

Troubleshooting