Portal Community

What Are AI Functions?

An AI Function is a named, versioned piece of code stored in SQL. It is not compiled into the application — it is loaded at runtime and executed in an isolated sandbox. This means administrators can add, update, or disable AI Functions without any application deployment:

// AI Function: "format_currency"
// Stored in Octopus_AIFunctions table, executed by AIFunctionRuntime
function execute(input) {
    const amount   = parseFloat(input.amount);
    const currency = input.currency || "USD";
    const locale   = input.locale   || "en-US";
    return {
        formatted: new Intl.NumberFormat(locale, {
            style:    "currency",
            currency: currency
        }).format(amount),
        raw:      amount,
        currency: currency
    };
}

AI Function vs. Custom C# Tool

AI Function

JavaScript in SQL. No deployment. Admin-editable. Sandboxed — no filesystem, no network. Best for: data transformation, formatting, calculations, simple lookups via injected services.

Custom C# Tool (Plugin)

Compiled C# with full .NET access. Requires deployment. Full DI container access. Best for: database queries, external API calls, complex business logic, authentication-required operations.

Storage Structure

CREATE TABLE Octopus_AIFunctions (
    FunctionId   UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID(),
    TenantId     UNIQUEIDENTIFIER NOT NULL,
    AgentId      UNIQUEIDENTIFIER NULL,       -- NULL = shared tenant-wide
    Name         NVARCHAR(200)    NOT NULL,
    Description  NVARCHAR(MAX)    NULL,
    Language     NVARCHAR(20)     NOT NULL DEFAULT 'JavaScript',  -- JavaScript | TypeScript
    Code         NVARCHAR(MAX)    NOT NULL,
    InputSchema  NVARCHAR(MAX)    NULL,       -- JSON Schema for LLM tool registration
    IsActive     BIT              NOT NULL DEFAULT 1,
    Version      INT              NOT NULL DEFAULT 1,
    CreatedAt    DATETIME2        NOT NULL DEFAULT SYSUTCDATETIME(),
    UpdatedAt    DATETIME2        NOT NULL DEFAULT SYSUTCDATETIME(),

    CONSTRAINT PK_AIFunctions PRIMARY KEY (FunctionId)
);

Execution Environments

ContextHow AI Functions Are Used
As MCP ToolAgent calls the function via its tool schema; result returned as tool result JSON
As Flow Studio NodeAI Function Node in a workflow passes node input data to the function; output flows to next node
As Procedure StepA procedural memory step can reference a function by name to execute a specific transformation