For a person or a model about to write an app; a model gets the same text from oad guide.

Contents

Actions and steps#

An action is a named thing the app can do. It is the only way to change shared state or to reach outside the app, and every surface uses the same actions: a button runs one, an agent calls one as a tool. So a guard written on the action holds for everyone.

An action#

JSON

{
  "description": "Put a book on your list, to read later.",
  "input": {
    "title": { "type": "string", "description": "The book's title." },
    "author": { "type": "string", "description": "Who wrote it.", "default": "" }
  },
  "output": "string",
  "require": [
    { "that": "size(trim(input.title)) > 0", "message": "Give the book a title." }
  ],
  "steps": [
    { "let": "id", "to": "=uuid()" },
    { "append": "state.books", "value": { "id": "=id", "title": "=trim(input.title)", "author": "=trim(input.author)", "status": "toRead", "addedOn": "=env.today" } },
    { "return": "=id" }
  ]
}
Field What it is
description Required. What the action does, for people and agents alike.
input The fields the caller passes, read as input.title.
output The type of what return gives back.
allow An expression: who may run it. When it is not true, the call fails with action.forbidden and the caller is not told why.
require A list of entries with that (an expression) and message: what must hold now. The first that is not true fails the call with action.refused and its message.
confirm true, or an object with a message: a person must agree before it runs.
done An object with a message: what to say once it has been done (below).
internal true keeps it off the agent surface; only this app's own steps and screens can run it.
steps Required. What it does, in order.

Input#

input is a field map: each field has a type and a description.

  • A field is required unless it has a default or says "required": false.
  • A field with a default takes it when the caller leaves it out. Prefer this to "required": false: a left-out field without a default is missing, and reading input.author then fails. Read such a field with default(input.author, '') or has(input.author).
  • The caller's input is checked before anything else: a missing required field, an unknown field or a value of the wrong type fails with action.invalidInput.
  • An enum is written as a schema object in type: { "type": "string", "enum": ["toRead", "reading", "read"] }.

Who may, and whether now#

allow answers "may this caller ever do this?". require answers "can it be done now?", and its message tells the person or agent what to change. Prefer require with a clear message for anything the caller can put right; keep allow for who may act at all, since its refusal says nothing.

JSON

{
  "description": "Decline someone's time off request.",
  "input": { "id": { "type": "string", "description": "Which request." } },
  "confirm": { "message": "The person is told their request is declined." },
  "require": [
    { "that": "'approver' in user.roles", "message": "Only an approver can decide time off." },
    { "that": "state.requests.exists(r, r.id == input.id && r.status == 'pending' && r.personId != user.person.id)", "message": "This request is not waiting for your decision." }
  ],
  "steps": [ { "send": "decline", "to": "approval", "key": "=input.id" } ]
}

confirm asks a person before the steps run. When an agent calls the action, the person the agent works for is asked; an agent never confirms for itself. A declined question fails the call with action.declined.

Ask the person first: a step that waits on a person (a call that picks a file, takes a photo or saves where they choose, or a button's run of an action with confirm) must come before every step of the same work that changes something, and never goes in a rule or a flow (action.waitsOnPerson). Others keep using the app meanwhile, so what must still hold when the person answers goes in require, which is checked again then.

Saying it was done#

When the result of an action is not where the person is looking (the form closes, the screen goes back, a record goes to someone else), say that it was done with done. Whoever started the action hears its message: the person who pressed sees it for a moment outside the screen, also after the screen went back; an agent finds it after the output of its tool call; a voice intent without a reply speaks it. The time off app's form runs askForTimeOff and goes back to the list, so the action says:

JSON

{ "message": "Request sent." }

The message may be an expression over input, the state the steps left and output, what the action gave back. It is said only when the action succeeded, and never for an action that another action, a rule or a flow runs. Leave it out when the change is in front of the person already, such as a box they ticked or a row added to the list they look at. Keep it to a short line: a host cuts it at 200 characters (action.doneTooLong). No step shows a message, and a text kept in the state is never cleared (see mistakes).

Steps#

A step is an object with one kind key, written first.

Step Shape Does
set { "set": "state.goal", "to": value } Writes a stored entry, or a field inside one.
append { "append": "state.books", "value": value } Adds an element at the end of a list.
remove { "remove": "state.books", "where": expression } Removes the elements for which where is true; item is the element.
patch { "patch": "state.books", "where": expression, "with": value } Merges with into each element for which where is true. Without where, merges into an object.
let { "let": "id", "to": value } Names a value for the steps after it.
if { "if": expression, "then": [steps], "else": [steps] } Chooses; else is optional.
each { "each": expression, "as": "b", "do": [steps] } Runs steps for each element of a list.
call { "call": "notify.send", "with": value, "as": "sent" } Uses an operation of a capability (see capabilities).
run { "run": "addBook", "with": value, "as": "id" } Runs another action of this app.
code { "code": "fewestStamps", "with": value, "as": "picked" } Runs a code module of this app (see code).
refresh { "refresh": "state.forecast" } Reads a sourced entry again now.
send { "send": "approve", "to": "approval", "key": value } Sends an event to a flow (see flows).
emit { "emit": "allRead", "with": value } Raises an app event that rules and agents can follow.
return { "return": value } Ends the action with its output.
fail { "fail": "book.notFound", "message": value } Ends the action with an error of your own code and message.

The values (to, value, with, return, message) take = for an expression; where, if and each are always expressions.

A write's path starts with state and names a stored entry, such as state.books. A derived or sourced entry cannot be written (state.readonly), and any other target is write.invalidTarget.

Changing one element of a list#

patch and remove act on every element for which where is true, with item the element. In the with of a patch that has where, item is the element too, so a field can be computed from its old value, as tick does in start. A list inside the element grows the same way: + joins the old list and a list of the new element. This patch adds a note to one book; default covers a book stored before notes:

JSON

[
  { "op": "add", "path": "/types/Book/properties/notes", "value": { "type": "array", "items": { "type": "string" } } },
  { "op": "add", "path": "/actions/addNote", "value": { "description": "Add a note to a book.", "input": { "id": { "type": "string", "description": "Which book." }, "note": { "type": "string", "description": "The note." } }, "steps": [ { "patch": "state.books", "where": "item.id == input.id", "with": { "notes": "=default(item.notes, []) + [input.note]" } } ] } }
]

A path can also pick one element with a selector in square brackets, as in state.books[id=input.id].status. It picks the first element whose field (here id) equals the expression's value:

JSON

[
  { "set": "state.books[id=input.id].status", "to": "=input.status" }
]

A write through a selector that finds no element fails with state.noElement. Selectors are allowed only in the targets of set, append, remove and patch, and in bindings.

When a step fails#

A failed step fails the whole action, unless the step handles it. A call, run or code step may carry:

  • onError: steps that run when it fails, with error.code and error.message to read. "onError": [] handles the failure and does nothing.
  • orElse: the value its as name takes when it fails.

The plant care app asks for a schedule only when needed, and the person may say no. The step handles that, so adding a plant still works:

JSON

{ "call": "clock.wakeAt", "with": { "key": "=id", "at": "=atTime(addDays(env.today, input.everyDays), '09:00')" }, "onError": [] }

All or nothing#

An action's writes are applied together when it ends. If it fails, none of them is kept, and the person or agent sees the error. What a capability already did outside (a message sent, a record filed) cannot be taken back. To make such a call safe to run again, give it once: a key naming the effect, such as "=`report:${input.id}`". A second call with the same key and the same input does not repeat the effect. once is only for operations that change something (call.onceEffect).

Running other actions#

run runs another action of this app, with its own allow, require and input checks. Use an internal action for steps that several actions share.

A run of an action that has confirm would skip the person's question, so it is refused in rules, in flows and in actions that do not confirm themselves (action.confirmBypassed). A screen's handler may run it: the person is asked there. Actions must not run each other in a circle (action.cycle).

Events#

Each action that succeeds raises action:<name>, which rules can follow, with its input and output. emit raises an event of your own, such as allRead in the reading list example; a rule can follow it, and an agent surface can list it.

Next#

  • views to run actions from a screen.
  • capabilities for call and the permissions it needs.
  • rules and flows for steps that run on their own.
  • mistakes for the diagnostics actions produce.