Skip to content

feat(flows): add execute-javascript step with isolated-vm sandbox - #802

Open
realcodesiman wants to merge 1 commit into
mainfrom
feat/execute-javascript-step
Open

feat(flows): add execute-javascript step with isolated-vm sandbox#802
realcodesiman wants to merge 1 commit into
mainfrom
feat/execute-javascript-step

Conversation

@realcodesiman

Copy link
Copy Markdown
Contributor

Summary

  • Adds a new "Execute JavaScript" flow step so a flow can run user-authored JavaScript and map the returned value into contact custom fields.
  • Execution is sandboxed via isolated-vm (8MB memory limit, 500ms timeout, no Node/network globals), with code length (10k chars) and mapping count (20) bounded at the schema and sandbox level.
  • Sandbox logic lives in a new @chatbotx.io/javascript-sandbox package as the single source of truth; packages/business wraps it with ChatbotXException mapping and the custom-field write path (bounded to 64KB per field).

Changes

  • packages/javascript-sandbox (new): pure executeJavascript() sandbox — isolate/context lifecycle, timeout/memory error classification, MAX_CODE_LENGTH guard.
  • packages/business/src/javascript-execution: javascriptExecutionService.execute() / .executeAndMap(), wraps sandbox errors as ChatbotXException, writes mapped values via contactCustomFieldService.
  • packages/flow-config: executeJavascript step schema/defaults, registered in stepTypes, toolSteps, and the package barrel.
  • apps/builder: editor dialog (code textarea + JSON-path → custom-field mapping rows), viewer, and menu entry for the new step.
  • apps/worker: handleExecuteJavascript step handler wired into flowStepHandlers; isolated-vm kept out of the tsdown bundle (native addon) via neverBundle.
  • pnpm-workspace.yaml: isolated-vm added to onlyBuiltDependencies.

Test plan

  • pnpm --filter @chatbotx.io/javascript-sandbox test — 5/5 passing
  • pnpm --filter @chatbotx.io/business test — 458/458 passing
  • pnpm --filter @chatbotx.io/flow-config test — 132/132 passing
  • pnpm --filter worker check-types — clean
  • pnpm lint — clean
  • Manual verification: create an Execute JavaScript step in the flow builder, confirm code executes and mapped fields are written to a test contact

Adds a new "Execute JavaScript" flow step that runs user-authored
JavaScript in an isolated-vm sandbox (8MB memory limit, 500ms timeout,
no Node/network globals) and maps the returned value into contact
custom fields via JSON-path mappings.

- packages/javascript-sandbox: pure executeJavascript() sandbox,
  single source of truth for the isolate/context lifecycle
- packages/business: javascriptExecutionService wraps the sandbox
  with ChatbotXException mapping and executeAndMap() custom-field
  writes (bounded to 64KB per field)
- packages/flow-config: executeJavascript step schema/defaults,
  registered in stepTypes, toolSteps, and the barrel export
- apps/builder: editor/viewer/menu entry for the new step
- apps/worker: flow step handler wired into flowStepHandlers,
  isolated-vm kept out of the tsdown bundle (native addon)

Schema enforces max code length (10k chars) and max mapping count
(20) to bound worst-case payload size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant