Run code when something happens

Declare hooks in an app, run its code on Laze and GitHub events without an agent turn, and start GitHub workflows from a server function.

An app hook is JavaScript in an app that Laze runs when something happens: a message in a conversation, a reaction, a press on a card, a change to the app's records, a timer or a GitHub push or workflow run. The hook decides in code what follows, and it calls the app's agent only when it asks for a turn, so most events cost no model tokens.

Declare a hook

A hook is a file hooks/<name>.js in the app's build, declared under "hooks" in laze.app.json:

{
  "hooks": {
    "intake": { "on": ["message", "reaction"], "conversations": ["#help"] },
    "chase": { "on": ["timer"], "every": "15m" },
    "deploys": { "on": ["github_workflow_run"], "github": ["acme/widgets@main"] }
  }
}
export default {
  async event(event, env) {
    if (event.kind === "message") {
      await env.LAZE.records("tickets").create({
        thread: event.threadRootId,
        text: event.message.text,
        status: "open",
      });
    }
  },
};

An app declares at most 20 hooks. A push with a missing hook file or an unknown event is refused with app_hook_invalid, which lists the events a hook takes. Only the published version of an app runs hooks.

Events a hook takes

EventWhen it runs
messageA new message in a conversation the hook names, from Laze, Slack or WhatsApp
message_editedA message there is edited
reactionSomeone reacts to a message there
card_actionSomeone presses a button on a card the hook posted
card_doneSomeone marks a feed card the hook posted as done
recordA record in one of the hook's collections changes
timerEvery every, at most once every 5 minutes
commentSomeone comments or replies on one of the app's pages
github_pushA push to a branch named in github
github_workflow_runA workflow run starts or finishes on a branch named in github
github_commit_statusA commit status is set on a branch named in github

The app's agent's own messages and the hook's own posts do not run it. An event heard twice runs once.

event holds id, kind, hook, at, app, conversation and threadRootId, plus the field of its kind: message (with text, author and link), edit, reaction, record, timer, card, comment (with text, author, anchor and threadId) or github. A field the kind does not use is null. history lists up to 20 earlier events of the same thread, newest first.

Who a hook hears

A hook acts as its app, inside what the app's agent may read. It hears and posts only in conversations that agent is a member of; a hook on another conversation is refused on push with app_hook_conversation_not_reachable. When the agent leaves a conversation, the hook stops hearing it.

A person's own WhatsApp reaches only apps of that person's own personal agent, and only chats they opted in.

What a hook can do

CallWhat it does
env.LAZE.records(name)Reads and writes the app's records, as hook:<name>
env.LAZE.events.list(...)Reads the app's event log, newest first
env.LAZE.wake({ prompt, payload, key, quietSeconds })Asks for a turn of the app's agent
env.LAZE.post, reply, reactWrites in a conversation the hook hears, as the app's agent
env.LAZE.feed.post, update, move, closePuts a card in a person's feed and changes it
env.LAZE.cards.post, cards.update, cards.answerPosts a conversation card as the app and answers a press on it
env.LAZE.settings()Reads the app's settings

With a key, such as a thread id, and quietSeconds up to 600, calls to wake with the same key gather into one turn, sent once the key has been quiet that long.

In a card_action run, reply({ as: "presser" }) posts as the person who pressed, into a conversation they are in. This is how Send it on a draft card works.

Limits

A hook has no network and no vault: fetch answers 403 hook_no_network. It cannot reach another app's records or a computer.

Each run may use 50 ms of CPU, 128 MB of memory and 10 seconds. A run over a limit fails with function_limit_exceeded. Each app runs at most 120 hooks a minute; events over it wait and run in order, and none is dropped.

A failed run is tried 3 times, then its event is parked with the error. Parked events stay 14 days. A run that is retried does not post twice.

Read the log and retry

laze app hooks
laze app hooks events --hook intake --state parked --limit 20
laze app hooks retry <event-id>
laze app hooks retry --parked --hook intake

laze app hooks events takes --state pending|running|done|parked.

React to GitHub

A hook with github_push, github_workflow_run or github_commit_status names its branches in "github": ["owner/name@branch"], up to 20. Each repository must be covered by the workspace's Laze GitHub App; one it does not cover is refused on push with agent_github_repo_not_installed.

The run gets event.github with the repository, branch, sha, link, actor and the first line of the commit message. Pushes and runs on other branches do not run the hook. A hook has no GitHub write, so nothing it does can start another run.

A hook on github_workflow_run can also get one small JSON file from the run. Name the artifact in laze.app.json:

{
  "hooks": {
    "deploys": {
      "on": ["github_workflow_run"],
      "github": ["acme/widgets@main"],
      "artifact": "deploy-result"
    }
  }
}

In the workflow, upload one JSON file of at most 64 KB under that name:

- uses: actions/upload-artifact@v4
  if: always()
  with:
    name: deploy-result
    path: result.json

When the run finishes, event.github.artifact holds { name, data } with the parsed JSON, or { name, problem } when there is none. The hook runs either way. Before that, and for a hook that names no artifact, it is null.

Laze reads the file with the workspace's Laze GitHub App, so the app gets no token and no download link, and the event log keeps nothing of the file. Only runs started by a push, a schedule, a dispatch or another workflow run are read, never a pull request. A run can follow a pull request's run, so never let that workflow copy anything from the pull request into the file. A file that holds a key or a password is refused.

problem is not_found, expired, too_large, not_one_file, not_json, holds_secret, trigger_not_allowed, repository_not_covered, actions_permission_missing or github_unavailable. On github_unavailable, throw an error: Laze runs the event again and reads the file again.

Start a GitHub workflow from an app

A server function can call https://api.github.com through the workspace's Laze GitHub App, for example to dispatch a workflow. Declare the installation's connection for the function in laze.app.json under functions.<name>.connections, then a workspace admin allows it with the repositories it may touch:

laze app connections
laze app connection allow github-acme --repo acme/site --permission actions:write
laze app connection revoke github-acme

On the app's Connections tab, allowing the installation asks for at least one repository. The grant defaults to actions write and contents read. deployments:write, statuses:write, checks:read and metadata:read are also accepted; contents:write and administration:write are refused.

Each call gets a token GitHub mints for that scope only. The function never sees it: the apps host writes it into the request, and an answer that echoes it reads [hidden by Laze]. A call to a repository the grant does not name is refused with app_github_repo_not_granted. Only a workspace admin, as themselves, allows or revokes the connection.

To have a person approve a deploy before the workflow runs, see Signed approvals.

Also check: Build apps with agents, Feed.