Skip to main content

Module code

Module code 

Source
Expand description

Built-in "code" node — executes an inline script in a sandboxed Rhai engine and returns the result as JSON.

Mirrors Dify’s Code node. Rhai is a safe, embedded scripting language with Rust-like syntax. It has no file system, network, or OS access by default.

§Config schema

{
  "language": "rhai",
  "code": "let total = inputs.items.len(); #{ total: total }"
}
FieldTypeDescription
languagestringMust be "rhai" (the only supported language)
codestringRhai script body

§Script context

Two variables are injected into the script scope:

  • inputs — map keyed by upstream node ID (equivalent to ctx.inputs)
  • variables — global flow variables (equivalent to ctx.variables)

§Output schema

If the script returns a Rhai object map, it becomes the node output directly:

#{ status: inputs.fetch.status, ok: inputs.fetch.ok }
// → { "status": 200, "ok": true }

Any other return type is wrapped under "output":

inputs.fetch.status == 200
// → { "output": true }

§Safety limits

The engine enforces:

  • Max 100,000 operations (prevents infinite loops)
  • Max string size: 1 MB
  • Max array size: 10,000 elements

§Rhai syntax reference

  • Variables: let x = 42;
  • Maps: #{ key: value }
  • Arrays: [1, 2, 3]
  • String ops: s.len(), s.contains("x"), s.to_upper()
  • Math: +, -, *, /, %
  • Conditionals: if x > 0 { "pos" } else { "neg" }
  • Loops: for item in arr { ... }

Full docs: https://rhai.rs/book/

Structs§

CodeNode
Sandboxed script execution node (Dify-compatible, Rhai engine).