Skip to content

Functions

A function is a piece of JavaScript that the hub runs for you:

  • A trigger runs when rows in a collection are added, updated or deleted.
  • A job runs on a schedule (cron, UTC).
  • Either kind can also be run by hand in the UI, or by an agent with fn.run.

Functions call the same tools agents use, as their own actor fn:<name>. So trust levels apply, every change appears in the feed, and actions set to ask wait in your inbox.

Tell billing when an order is done:

// event: {type: "add"|"update"|"delete", collection, id, row, set, previous, actor, seq}
if (event.row.status !== "done") return "skipped";
aw.msg.send({ to: "billing", type: "order.done", payload: { id: event.id, item: event.row.item } });
log("told billing about", event.row.item);
return "sent";

What a trigger receives in event:

  • row: the current row, including computed columns.
  • set and previous: for updates, the changed fields and their old values.
  • previous: for deletes, the deleted row.

A trigger never fires on its own changes, so the function can safely update the row it reacts to.

Warn when work piles up, every morning at 07:00 UTC:

const open = aw.tasks.list({ status: "open" });
if (open.tasks.length > 10) aw.inbox.notify({ title: open.tasks.length + " tasks are waiting" });
return open.tasks.length;

Schedules are standard cron in UTC (0 7 * * *, */15 * * * *), or descriptors like @hourly, @daily and @every 10m.

aw.<module>.<verb>({…}) Call any tool, e.g. aw.coll.update, aw.msg.send, aw.files.put. Throws on errors such as conflict or not_found
awCall("tool.name", {…}) The same, by name
event Triggers: the change that fired it. Manual runs of a trigger: the given input, to test it with a sample event
input The input of a manual run
log(…), console.log(…) Write to the run log
return … The run’s result, shown in the run log
round(x, n), days(from, to), today() Same helpers as in computed columns
  • Isolated: each run gets a fresh JavaScript interpreter with no file system, network or shell, only aw.*.
  • Limits: 10 seconds and 100 tool calls per run. Tools that wait (events.wait, inbox.wait, kv.watch) aren’t available.
  • Loops: a function that runs more than 120 times in a minute (for example two triggers updating each other) is paused automatically, with a note saying why.
  • One at a time: runs execute one after another, so triggers and jobs never race each other.
  • New code needs approval: for agents, fn.put and fn.delete default to ask, so new or changed code waits for your approval. Your own edits in the UI apply directly.
  • Run log: every run records its cause, duration, log output, result or error, kept for 14 days. Failed runs also appear in the feed as fn.failed.
Tool Use
fn.list / fn.get Functions and their code
fn.put Create or update (kind, collection + on for triggers, schedule for jobs, code)
fn.run Run now, with optional input; returns result and log
fn.runs Recent runs with logs
fn.status active or paused
fn.delete Remove a function
Terminal window
aw fn.put name=nightly kind=job schedule="0 2 * * *" code="return aw.coll.query({collection: 'orders'}).total"
aw fn.run name=nightly
aw fn.runs name=nightly limit=5