Skip to main content
Script steps allow you to embed JavaScript logic directly into a plan workflow. Use them for routing decisions, document inspection, data extraction, content tagging, knowledge feature assignment, and any custom processing that needs to run between other plan steps.

Overview

A script step runs inline JavaScript in a sandboxed runtime. It has access to:
  • The current task and its properties
  • Document families attached to the task
  • The ability to load KDDB documents and inspect or modify their content
  • Knowledge feature lookups for classification workflows
  • Logging for diagnostics
  • Service bridge calls to external APIs (project-scoped)
  • LLM invocations with automatic cost tracking
Scripts return an action that determines which downstream path the workflow follows, enabling conditional branching in your plan.

Adding a Script Step

1

Enable Planned Sub-Tasks

In the task template editor, check Enable Planned Sub-Tasks on the Details tab.
2

Open the Planned Tab

Navigate to the Planned tab to open the visual flow editor.
3

Add a Script Node

Drag a Script node from the left palette onto the canvas. Script nodes appear in violet.
4

Write Your Script

Select the script node and use the properties panel on the right to write JavaScript. Click the Expand button to open the full-screen editor with the snippet browser.
5

Define Actions

Add one or more actions in the Script Actions section. These are the possible outcomes your script can return. Each action becomes a connection point on the node for conditional branching.
6

Connect Dependencies

Draw edges from upstream nodes to your script node, and from the script node’s action handles to downstream nodes.

Script Structure

Every script must return an object with an action property that matches one of the declared action names:
// Inspect task data, documents, or knowledge features
var doc = loadDocument(families[0].id);
var content = doc.GetRootNode().GetAllContent(" ", true);

// Make a routing decision
var isInvoice = content.indexOf("INVOICE") !== -1;

// Return the action to follow
return { action: isInvoice ? "invoice" : "other" };
Optionally, the return value can include a features array to assign knowledge features to document families:
return {
  action: "classified",
  features: [
    { documentFamilyId: families[0].id, featureId: "feature-uuid" }
  ]
};

Context Objects

Scripts have access to several pre-bound context objects. These are available as global variables in your script.

task

The current task being processed.
PropertyTypeDescription
idstringTask ID
titlestringTask title
statusstringCurrent task status slug
metadataobjectTask metadata (from the template)
dataobjectTask properties/data (custom key-value pairs)

families

An array of document families attached to the task.
PropertyTypeDescription
idstringDocument family ID
namestringFile path / name
metadataobjectFamily metadata
mixinsstring[]Mixin slugs applied to this family
featureIdsstring[]Knowledge feature IDs assigned to this family
contentObjectsarrayContent objects (documents and native files)
Each content object has:
PropertyTypeDescription
idstringContent object ID
contentTypenumber0 = KDDB document, 1 = native file
mimeTypestringMIME type (e.g., application/pdf)
sizenumberFile size in bytes

org

The current organization.
PropertyTypeDescription
idstringOrganization ID
slugstringOrganization slug

Helper Functions

log(level, message)

Write a log entry. Level can be "debug", "info", "warn", or "error".
log("info", "Processing document: " + families[0].name);
log("warn", "No content found in document");

loadDocument(familyId)

Load a KDDB document by its family ID. Returns a document object with methods for reading and modifying content. Limited to 5 calls per script execution.
var doc = loadDocument(families[0].id);
// ... inspect or modify the document ...
// Do NOT call doc.Close() — the orchestrator handles persistence automatically
Do not call doc.Close() in your scripts. The orchestrator automatically persists any document modifications as new content object versions after the script completes. Calling Close() prematurely closes the underlying database, which prevents the orchestrator from saving your changes.

lookupFeature(slug)

Look up a single knowledge feature by its slug. Returns the feature object or null.
var feature = lookupFeature("doc-type-invoice");
if (feature) {
  log("info", "Found feature: " + feature.id);
}

lookupFeatureType(slug)

Look up a knowledge feature type by its slug. Returns { id, name, slug } or null.
var featureType = lookupFeatureType("document-classification");
if (featureType) {
  log("info", "Feature type: " + featureType.name);
}

lookupFeaturesByType(typeSlug)

Get all knowledge features belonging to a feature type. Returns an array of { id, slug } objects.
var features = lookupFeaturesByType("document-classification");
for (var i = 0; i < features.length; i++) {
  log("info", "Feature: " + features[i].slug);
}

Working with Documents

When you call loadDocument(), you receive a document object with both read and write capabilities.

Reading Document Content

var doc = loadDocument(families[0].id);

// Get the root content node
var root = doc.GetRootNode();

// Get all text content joined with spaces
var fullText = root.GetAllContent(" ", true);

// Get document metadata
var metadata = doc.GetMetadata();
log("info", "Document type: " + metadata.documentType);

// Get document labels
var labels = doc.GetLabels();
log("info", "Labels: " + labels.join(", "));

Selecting Content Nodes

Use selector expressions to find specific content nodes in the document tree:
var doc = loadDocument(families[0].id);

// Find all page nodes
var pages = doc.Select("//page");
log("info", "Document has " + pages.length + " pages");

// Find lines containing specific text
var matches = doc.Select("//line[contains(@content, 'TOTAL')]");

// Find the first matching node
var header = doc.SelectFirst("//line[position() = 1]");
if (header) {
  log("info", "First line: " + header.GetContent());
}

Content nodes form a tree structure (document > page > line > word). You can navigate it programmatically:
var doc = loadDocument(families[0].id);
var root = doc.GetRootNode();

// Get child nodes
var pages = root.GetChildren();
for (var i = 0; i < pages.length; i++) {
  var page = pages[i];
  log("info", "Page " + page.GetPage() + ": " + page.GetNodeType());

  // Get lines on this page
  var lines = page.GetChildren();
  for (var j = 0; j < lines.length; j++) {
    log("debug", "  Line: " + lines[j].GetContent());
  }
}

Content Node Properties

Each content node provides:
MethodReturnsDescription
GetContent()stringText content of this node
GetNodeType()stringNode type (page, line, word, etc.)
GetChildren()node[]Child content nodes
GetParent()nodeParent content node
GetDescendants()node[]All descendant nodes
GetAllContent(sep, strip)stringJoin all descendant text
GetPage()numberPage number
GetBoundingBox()object{ x, y, width, height } or null
GetConfidence()numberOCR confidence score
GetTags()arrayTags on this node
HasTag(name)booleanCheck for a specific tag
GetFeatures()arrayFeatures on this node
HasFeature(type, name)booleanCheck for a specific feature
GetFeatureValue(type, name)anyGet a feature value

Modifying Documents

Script steps can modify documents. Changes are automatically persisted as new content object versions after the script completes.

Setting Metadata and Labels

var doc = loadDocument(families[0].id);

// Set metadata values
doc.SetMetadata("processedAt", new Date().toISOString());
doc.SetMetadata("documentType", "invoice");

// Add and remove labels
doc.AddLabel("reviewed");
doc.RemoveLabel("pending");

Tagging Content Nodes

Tags mark content nodes for downstream processing or data extraction:
var doc = loadDocument(families[0].id);

// Find and tag specific content
var nodes = doc.Select("//line[contains(@content, 'TOTAL')]");
for (var i = 0; i < nodes.length; i++) {
  nodes[i].Tag("financial/total-line", {
    confidence: 0.95,
    value: nodes[i].GetContent()
  });
}

Tag options:
OptionTypeDescription
valuestringTag value
confidencenumberConfidence score (0.0–1.0)
groupUuidstringGroup UUID for data object grouping
parentGroupUuidstringParent group UUID for hierarchy
cellIndexnumberInstance index within a group
indexnumberOrdering index
notestringUser note
statusstringTag status
ownerUristringProvenance URI

Adding Features to Content Nodes

var doc = loadDocument(families[0].id);

var pages = doc.Select("//page");
for (var i = 0; i < pages.length; i++) {
  var page = pages[i];
  var content = page.GetAllContent(" ", true);

  // Classify page type based on content
  var pageType = "body";
  if (content.indexOf("Table of Contents") !== -1) {
    pageType = "toc";
  } else if (content.match(/appendix\s+[a-z]/i)) {
    pageType = "appendix";
  }

  page.SetFeature("classification", "page_type", pageType);
}

Creating Data Objects

Data objects represent structured, extracted data:
var doc = loadDocument(families[0].id);

// Create a top-level data object
var invoice = doc.CreateDataObject({
  path: "invoice",
  taxonomyRef: "taxonomy://acme/invoice-taxonomy"
});

// Add attributes
invoice.AddAttribute({
  tag: "vendor_name",
  path: "invoice/vendor_name",
  value: "Acme Corp",
  type: "STRING"
});

invoice.AddAttribute({
  tag: "total_amount",
  path: "invoice/total_amount",
  value: "$1,234.56",
  type: "CURRENCY",
  decimalValue: 1234.56
});

Nested Data Objects

Build hierarchical data structures with parent/child relationships:
var doc = loadDocument(families[0].id);

var order = doc.CreateDataObject({
  path: "purchase_order",
  taxonomyRef: "taxonomy://acme/po-taxonomy"
});

order.AddAttribute({
  tag: "po_number",
  path: "purchase_order/po_number",
  value: "PO-2026-0042",
  type: "STRING"
});

var items = [
  { sku: "WDG-100", qty: 50, price: 12.99 },
  { sku: "WDG-200", qty: 25, price: 24.50 }
];

for (var i = 0; i < items.length; i++) {
  var lineItem = order.AddChild({
    path: "purchase_order/line_item",
    sourceOrdering: String(i)
  });

  lineItem.AddAttribute({
    tag: "sku",
    path: "purchase_order/line_item/sku",
    value: items[i].sku,
    type: "STRING"
  });

  lineItem.AddAttribute({
    tag: "unit_price",
    path: "purchase_order/line_item/unit_price",
    value: "$" + items[i].price.toFixed(2),
    type: "CURRENCY",
    decimalValue: items[i].price
  });
}

Reading Data Objects

var doc = loadDocument(families[0].id);

// Get all data objects
var allObjects = doc.GetAllDataObjects();
log("info", "Total data objects: " + allObjects.length);

// Find objects by path
for (var i = 0; i < allObjects.length; i++) {
  var obj = allObjects[i];
  if (obj.GetPath() === "invoice") {
    // List attributes
    var attrs = obj.GetAttributes();
    for (var j = 0; j < attrs.length; j++) {
      log("info", "  " + attrs[j].GetValue());
    }

    // Find attribute by name
    var amount = obj.GetAttributeByName("total_amount");
    if (amount) {
      log("info", "Total: " + amount.GetValue());
    }

    // Find children by path
    var lineItems = obj.GetChildrenByPath("invoice/line_item");
    log("info", "Line items: " + lineItems.length);
  }
}

Attribute Data Types

When adding attributes, specify the type to control how the value is stored and displayed:
TypeDescriptionTyped Value Property
STRINGFree textstringValue
CURRENCYMonetary amountdecimalValue
NUMBERNumeric valuedecimalValue
PERCENTAGEPercentagedecimalValue
DECIMALDecimal numberdecimalValue
INTEGERWhole numberdecimalValue
DATEDatedateValue (ISO string)
DATETIMEDate and timedateValue (ISO string)
BOOLEANTrue/falsebooleanValue
SELECTIONEnumerated valuestringValue
URLWeb addressstringValue
EMAILEmail addressstringValue
PHONEPhone numberstringValue

Knowledge Feature Assignment

Scripts can assign knowledge features to document families as part of their return value. This is useful for classification workflows where the script analyzes document content and assigns the appropriate features.
var doc = loadDocument(families[0].id);
var content = doc.GetRootNode().GetAllContent(" ", true).toLowerCase();
var features = [];

// Check for document characteristics
if (content.indexOf("invoice") !== -1) {
  var feature = lookupFeature("doc-type-invoice");
  if (feature) {
    features.push({
      documentFamilyId: families[0].id,
      featureId: feature.id
    });
  }
}

// Add a label for tracking
if (features.length > 0) {
  doc.AddLabel("classified");
  doc.SetMetadata("classifiedAt", new Date().toISOString());
}

return {
  action: features.length > 0 ? "classified" : "unclassified",
  features: features
};

Calling Service Bridges

Script steps can call external APIs through service bridges that are configured as project resources. Only bridges that have been added to the task’s project are accessible — scripts cannot call arbitrary bridges in the organization.

Discovering Available Bridges

Use serviceBridge.list() to see which bridges and endpoints are available:
var bridges = serviceBridge.list();
for (var i = 0; i < bridges.length; i++) {
  log("info", "Bridge: " + bridges[i].slug);
  for (var j = 0; j < bridges[i].endpoints.length; j++) {
    var ep = bridges[i].endpoints[j];
    log("info", "  " + ep.method + " " + ep.name + " — " + ep.description);
  }
}

return { action: "done" };
Each bridge in the returned array has:
PropertyTypeDescription
slugstringBridge slug identifier
namestringDisplay name
endpointsarrayAvailable endpoints
Each endpoint has:
PropertyTypeDescription
namestringEndpoint name (used in call())
descriptionstringHuman-readable description
methodstringHTTP method (GET, POST, etc.)
pathstringURL path appended to the bridge base URL

Calling an Endpoint

Use serviceBridge.call(bridgeSlug, endpointName, body?) to make HTTP requests through a bridge:
// GET request (no body)
var data = serviceBridge.call("my-api-bridge", "get-status");
log("info", "Status: " + data.status);

// POST request with body
var result = serviceBridge.call("my-api-bridge", "submit", {
  documentId: families[0].id,
  metadata: task.metadata
});
The response is automatically parsed as JSON if possible, otherwise returned as a string. Limited to 10 calls per script execution with a 10-second timeout per call.

Example: Enriching Documents via External API

var doc = loadDocument(families[0].id);
var content = doc.GetRootNode().GetAllContent(" ", true);

// Send text to external classification API
var classification = serviceBridge.call("classifier-bridge", "classify", {
  text: content.substring(0, 5000)
});

doc.SetMetadata("externalClassification", classification.category);
doc.SetMetadata("classificationConfidence", classification.confidence);

if (classification.confidence > 0.8) {
  doc.AddLabel("auto-classified");
}

return { action: classification.category };
Service bridges must be added as project resources before they can be used in script steps. If a bridge exists in the organization but is not linked to the project, scripts will receive a “bridge not found” error.

Making LLM Calls

Script steps can invoke the platform’s LLM (Large Language Model) service directly. All calls are automatically tracked in model costs for billing and reporting.

Basic LLM Call

Use llm.invoke(prompt, options?) to send a prompt and get a response:
var response = llm.invoke(
  "Classify this document title into one of: invoice, receipt, " +
  "contract, other. Title: " + families[0].name +
  ". Reply with just the category name."
);

log("info", "LLM classified as: " + response);

var category = response.trim().toLowerCase();
return { action: category };
The optional options parameter accepts:
OptionTypeDescription
notestringDescriptive label stored in the model cost record for billing visibility
var response = llm.invoke(
  "Summarize this document metadata: " + JSON.stringify(task.metadata),
  { note: "Document summary for task " + task.id }
);

Using Prompt Templates

If you have prompt templates configured as project resources, use llm.invokeWithPromptRef(promptSlug, parameters?, options?) to resolve and render them:
var response = llm.invokeWithPromptRef(
  "document-classifier",         // prompt template slug
  {                              // template parameter values
    documentName: families[0].name,
    documentType: families[0].metadata.mimeType || "unknown",
    taskTitle: task.title
  },
  { note: "Classify " + families[0].name }
);

log("info", "Classification: " + response);
return { action: response.trim().toLowerCase() };
Prompt templates support {variable} placeholders that are replaced with the provided parameter values.

Analyzing Document Content

Combine document loading with LLM calls for AI-powered document analysis:
var doc = loadDocument(families[0].id);
var content = doc.GetRootNode().GetAllContent(" ", true);

var analysis = llm.invoke(
  "Analyze this document and return a JSON object with:\n" +
  "- documentType: the type of document\n" +
  "- keyEntities: array of important entity names\n" +
  "- summary: one-sentence summary\n\n" +
  "Document text:\n" + content.substring(0, 3000),
  { note: "Document analysis: " + families[0].name }
);

try {
  var result = JSON.parse(analysis);
  doc.SetMetadata("aiDocumentType", result.documentType);
  doc.SetMetadata("aiSummary", result.summary);
  doc.SetMetadata("aiEntities", result.keyEntities);
} catch (e) {
  log("warn", "Could not parse LLM response as JSON");
}

return { action: "analyzed" };
LLM calls are limited to 5 per script execution and count against your organization’s model costs. Each call records the input and output token counts in the platform’s billing system.If the LLM service is not configured on your environment, calls will throw an error with a clear message.

Full Workflow Example

This example combines document inspection, tagging, data extraction, and knowledge feature assignment into a single script:
var doc = loadDocument(families[0].id);
var features = [];

// 1. Classify document type
var root = doc.GetRootNode();
var fullText = root.GetAllContent(" ", true);
var isInvoice = fullText.indexOf("INVOICE") !== -1;

// 2. Set metadata and labels
doc.SetMetadata("documentType", isInvoice ? "invoice" : "unknown");
doc.AddLabel(isInvoice ? "invoice" : "unclassified");

// 3. Assign knowledge feature
if (isInvoice) {
  var feat = lookupFeature("doc-type-invoice");
  if (feat) {
    features.push({
      documentFamilyId: families[0].id,
      featureId: feat.id
    });
  }
}

// 4. Tag header lines
var headerNodes = doc.Select("//line[position() <= 5]");
for (var i = 0; i < headerNodes.length; i++) {
  headerNodes[i].Tag("document/header-line");
  headerNodes[i].SetFeature("layout", "section", "header");
}

// 5. Extract data if invoice
if (isInvoice) {
  var invoice = doc.CreateDataObject({
    path: "invoice",
    taxonomyRef: "taxonomy://acme/invoice"
  });

  var invNumNode = doc.SelectFirst(
    "//line[contains(@content, 'Invoice #')]"
  );
  if (invNumNode) {
    var text = invNumNode.GetContent();
    var match = text.match(/Invoice #\s*(\S+)/);
    if (match) {
      invoice.AddAttribute({
        tag: "invoice_number",
        path: "invoice/invoice_number",
        value: match[1],
        type: "STRING",
        confidence: 0.85
      });
    }
  }
}

return {
  action: isInvoice ? "invoice-processed" : "skipped",
  features: features
};

The Script Editor

The plan flow editor provides two ways to edit script code:

Inline Editor

When you select a script node, the properties panel on the right shows a Monaco code editor with:
  • Syntax highlighting for JavaScript
  • IntelliSense auto-completion for all context objects, helper functions, and document APIs
  • Script Actions management below the editor

Full-Screen Editor

Click the Expand button to open the full-screen script editor, which provides:
  • A large code editing area
  • A Snippet Browser panel on the right with categorized code templates
  • Insert or Replace buttons to use snippets as starting points
  • Script action management in the footer

Snippet Categories

The snippet browser includes ready-to-use templates organized by category:
CategoryWhat’s Included
Getting StartedBasic script skeleton with document loading
Service BridgesList bridges, call GET/POST endpoints, enrich documents via external APIs
LLM CallsBasic prompts, cost-tracked calls, prompt templates, document analysis
Tagging NodesTag nodes by selector or by ID list
Data ObjectsCreate, nest, and look up data objects and attributes
Knowledge FeaturesClassify documents and assign features
Content AnalysisPage classification by content heuristics
Full WorkflowsEnd-to-end examples combining all capabilities

Viewing Script Logs

Script step logs are captured in CloudWatch and viewable directly in the task plan UI.

How It Works

Every script execution automatically records:
  • A start log entry when the script begins
  • All log() calls made during execution (debug, info, warn, error)
  • An end log entry when the script completes (with success or error status)

Viewing Logs in the UI

To view logs for a completed script step:
  1. Open the task and navigate to the plan view
  2. Click on a completed SCRIPT plan step to open its status dialog
  3. The Logs section displays all log entries with timestamps
Logs are paginated for scripts that produce large amounts of output. Use the search field to filter log entries by message content.
Add log("info", ...) calls at key decision points in your scripts. The logs persist after execution, making them invaluable for debugging routing decisions and understanding why a script chose a particular action.

Loading Shared Modules

Script steps can pre-load reusable JavaScript modules so that their functions and variables are available in global scope within your script. This lets you build a library of shared utilities — string helpers, validation logic, API wrappers — and reuse them across multiple script steps without duplicating code. To configure module pre-loading, use the Module Refs picker in the script step’s properties panel. Select one or more JavaScript modules from your organization. The selected modules are fetched and executed in order before your script runs.
// Assuming a module "my-org/string-utils" is loaded via Module Refs,
// its exported functions are available directly:
var cleaned = cleanText(families[0].name);
var emails = extractEmails(cleaned);

log("info", "Found " + emails.length + " emails in filename");
return { action: "processed" };
Shared modules must be JavaScript modules deployed with scriptLanguage: "javascript" and an inline script field. A maximum of 10 modules can be pre-loaded per script step.

Execution Details

  • Runtime: JavaScript (ES5+) via the Goja engine
  • Timeout: 15 seconds per script execution (all operations — document loads, bridge calls, LLM calls — count against this limit)
  • Document loads: Maximum 5 per script execution
  • Service bridge calls: Maximum 10 per script execution, 10-second timeout per call, project-scoped
  • LLM calls: Maximum 5 per script execution, token usage recorded in model costs
  • Persistence: Any document modifications (tags, metadata, labels, data objects) are automatically saved as new content object versions after the script completes. Do not call Close() yourself.
  • Action matching: The returned action name is matched case-insensitively against the declared script actions