# Writing an automation an agent can run (an action)

An **action** lets one of the account's hub agents (the one that answers their phone, talks in their
app, or chats on their website) run an n8n automation in the middle of a conversation: book a
callback, look up an order, add a contact. This page is how to build that automation so it works the
first time, then how to put it on the agent with `awesomate_agents_change`.

This is the hub's own agents (Agents in the hub). It is not n8n's own Agents tab (see
[n8n-agents.md](n8n-agents.md)) and not an AI Agent node inside a workflow (see
[ai-agents.md](ai-agents.md)).

## How an action runs

The agent never calls the automation directly. The hub writes one **Agent actions gateway** workflow
into the client's n8n when the owner makes the agent live. The gateway checks the agent's key, then
runs the named automation as a sub-workflow (Execute Workflow) with the inputs the agent filled in,
and hands the automation's answer back to the agent. Never edit or call the gateway yourself, and
never copy its webhook into anything: the hub rewrites it whenever a "Make changes live" changes what
it runs.

## The automation, step by step

1. **Trigger: "When executed by another workflow"** (`n8n-nodes-base.executeWorkflowTrigger`), and it
   must be **enabled**. An automation that starts with a Webhook, a Schedule or anything else cannot
   be an action: `awesomate_agents workflows` shows it with `qualifies: false` and a `why_not`.
2. **Declare the inputs on the trigger** with "Define using fields below" (`workflowInputs.values`):
   one field per thing the agent must supply. Names are lower-case letters, digits and `_`, starting
   with a letter (`phone`, `when_free`, `order_number`): any other name is left out of the action.
   Types are String, Number or Boolean. "Accept all data" works too, but then the action has no
   inputs until you name them yourself, so prefer declared fields.
3. **Two input names mean something:**
   - `is_test` (Boolean): set to `true` on a test run from the hub or `test_action`. When the
     automation sends, books or charges, branch on it and do nothing real (answer as if it had).
   - `replaces`: lets the action update something it did earlier in the same conversation (the
     agent passes the earlier `reference`). Only add it when the automation can really update.
4. **Do the work quickly.** On a phone call the agent waits **8 seconds** for the answer, then tells
   the caller it could not do it (an action called `caller_context`, read before the greeting, gets
   2.5 seconds). Anything slow (a long AI step, a big search, an email with attachments) goes after
   the answer: answer first, then carry on in a sub-workflow started without waiting.
5. **End with the answer for the agent.** The LAST node's JSON is what the agent gets, and only these
   four fields reach it (three for a run-then-tell action: `context` is dropped there). An action
   **held for a person's OK** is different: the caller hears the hub's own "I've passed that on"
   sentence, the automation runs only after the OK, and its answer goes to the hub, not the caller,
   so never rely on `say` being heard for a held action:

   ```json
   {
     "ok": true,
     "say": "Done, Sam will ring you back at 3pm today.",
     "reference": "CB-1042",
     "context": "Callback booked for 15:00 with Sam."
   }
   ```

   - `ok`: `true` when it worked. `false` (with a `say`) when it could not, so the agent tells the
     person and does not pretend.
   - `say`: one or two plain sentences the agent can say as they are. No markdown, no lists, no links
     (it may be spoken).
   - `reference`: a short id the person can quote back (up to 80 characters), or leave it out.
   - `context`: anything the agent should know but not read out, or leave it out.

   A Set (Edit Fields) node at the end is the clearest way. An automation that ends with nothing to
   say is reported as "gave the agent nothing to say" when tested.
6. **Errors:** catch failures inside the automation and end on `{ "ok": false, "say": "..." }`. An
   automation that throws gives the agent no words to use.
7. **Activate it** if it will also run on its own; an Execute Workflow call runs it either way.

## Putting it on the agent

1. `awesomate_agents list`, then `awesomate_agents get {agentId}` for `draft_hash` and what is there.
2. `awesomate_agents workflows {agentId}`: the automation should show `qualifies: true` and its inputs.
3. `awesomate_agents_change add_action {agentId, baseHash, workflowId, when, inputs?}`. `when` is one
   sentence on when to use it ("The caller wants someone to ring them back"). For each input, the
   default is `mode: ai` (the agent asks or works it out). Use `mode: fixed` with a `value` for a
   constant, and `mode: session` with a conversation fact (`caller_number`, `caller_id`, `call_id`,
   `business_id`, `called_at`, `user_ref`) for what the conversation already knows. Set
   `from_caller: true` on anything that must come from the person's own mouth.
4. `awesomate_agents_change test_action {agentId, actionId, samples}` runs it once through a test
   gateway with your sample values, and `is_test: true` when the automation declares that input. It
   really runs the automation, so an automation that may send or charge is refused from Claude Code
   (`test_would_send`) unless it declares `is_test` and skips the real step when it is true. Up to 10
   runs per agent in 10 minutes. Reading automations, adding an action and testing need **Let Claude
   Code work with your automations** in Settings, Privacy.
5. Tell the user it is in the **draft**: they make it live with "Make changes live" on the agent's
   Build page in the hub (`/agents/<id>/build`), or you ask them with
   `awesomate_agents_change request_live {agentId, baseHash}`, which puts a one-tap "Make changes
   live" card in their Tasks for exactly this draft (after the same checks: `fix_first` lists what
   to fix). Never say it is live until they say yes.

If the action reaches a customer (`effect: customer`) or spends money (`effect: money`), the business
may hold it for a person's OK before it runs (see [decisions.md](decisions.md)). Claude Code can
tighten an action's authority but never loosen it: a loosening is held as it was and becomes the
owner's one-tap card in Tasks; their yes puts exactly that change in the draft. Switching off the read-back (`requireApproval: false`) and changing or
clearing the message input a person deciding reads (`draftInput`) count as loosenings too.

## A workflow your website agent can run

Website actions are given account by account (`features.agent_website_actions` in
`awesomate_get_context`). Without them the website chat answers from its knowledge only: say so if
the user expects a visitor to book something through the chat.

With them, an action runs on the website only when it is **ticked** for it (`channels` includes
`website`); an empty list still means the phone and the app, never the website. Check where an agent
stands with `awesomate_agent_website {agentId}`: each action's `may_run` and, when it may not,
`why_not`, plus `draft_problem` for a tick the owner could not make live. You can tick it from
`awesomate_agents_change` (`update_action` with `channels: ["phone", "website"]`); the tick widens
what the agent may do, so it is held as it was and the owner gets a suggestion in Tasks. An action on
the website only is refused (`owner_only_website`), and a skill never takes `website`.

- **Inputs from the conversation** (`mode: session`): `conversation_id` (also `call_id`),
  `contact_id` (the Contacts person saved in this chat), `visitor_email` (set ONLY when the visitor
  passed an emailed code, never from what they typed), `visitor_name` and `visitor_mobile` (as they
  gave them, unchecked), `page_url`, `photo_handles` (this chat's photos, comma separated),
  `business_id`, `called_at`. A call's facts (`caller_number`, `caller_id`, `user_ref`) do not exist
  on the website, so an action using one cannot be ticked for it.
- **Money never runs on the website.** An action that reaches a customer (`effect: customer`) may only
  send to the visitor's own checked email: give it an input filled from `visitor_email`, and no other
  input that could name where it goes.
- **The answer:** end on `{ "ok": true, "say": "...", "options": [...], "reference": "..." }`. The
  visitor's agent sees `say` (up to 500 characters), up to 5 `options` (short labels, e.g. three
  times to pick from) and `reference` (letters, digits, `-` and `_` only, e.g. a booking number);
  everything else is dropped, and email addresses and phone numbers the visitor did not give and the business has not published are
  masked.
- **Never return other people's details.** A website visitor is a member of the public: an automation
  that looks something up must answer about this visitor only (match on `contact_id` or
  `visitor_email`), never list other customers, jobs or bookings.
- An action that reaches a customer may still be held for a person's OK before it runs (see
  [decisions.md](decisions.md)), so write `say` for the moment it does run.

The website chat's own settings (actions on, the privacy link, photos, the records it may save, who
a visitor is handed to) are `set_settings` on `awesomate_agents_change` with `channelSettings.website`.
Records and the photo field name only fields AI may read in Contacts: `get` and
`awesomate_agent_website` show only those, `website_fields_hidden` counts the owner's others (they
stay as they are when you save), and naming any other field is refused as missing. Switching actions
on, a new saved field, photos, the privacy link and the hand-off widen what the chat may do, so they
are held as they were and the owner gets a suggestion in Tasks; narrowing applies.

## Skills

A **skill** is written steps for one kind of request ("Booking a service", "Price questions"), with
optional reference files the agent reads only when the skill needs them (a price list, opening hours).
A skill can name the actions it may use (`actionIds`). Add one with
`awesomate_agents_change add_skill {agentId, baseHash, name, when, steps}` or from a file with
`markdown` (front matter `name:` and `when:`, then the steps). Limits: 20 skills, 10 reference files
each (32,768 characters a file), steps up to 65,536 characters.

A skill can instead **follow a procedure** the business already keeps: one kept in the hub (its id
from `awesomate_business_map` `procedures`), or a published 1Brain page on one of the roles' 1Brain
systems (`<1Brain account id>:<page id>`, from `awesomate_onebrain` `systems_for_role`). Send
`procedure: {source: 'hub', id: '12'}` or `{source: 'onebrain', id: '1985:500'}` with no `steps`
(`add_skill` only). The hub reads it as the person whose
key this is, names the skill after it unless you give a name, and copies its steps in again each time
changes go live. Steps sent with it are refused (`skill_follows_procedure`), and so is editing a
linked skill's steps later: change the procedure instead. Other refusals:

| Code | What it means |
|---|---|
| `procedure_not_readable` | Their own 1Brain could not open the page, or it is in a 1Brain account the map is not linked to. Check both, then try once more. |
| `procedure_not_on_map` | The page is not on any role's 1Brain systems. Add it to one first (`awesomate_onebrain_change`). |
| `procedure_not_published` | Publish the page in 1Brain first. |
| `not_found` (404) | That hub procedure is not kept any more. |
| `procedure_unreadable` (503) | The hub procedure could not be read just now. Try again shortly. |
| `procedure_too_long` | Longer than a skill holds (65,536 characters). |
| `feature_unavailable` | Procedures for agents are not on for the account. |

## Memory, after the call, and channels

`awesomate_agents get` also returns the agent's **settings** for the draft and for what is live:
`memory` (whether it remembers callers and where it looks them up), `after_call` (the fields it notes
from each call and where it sends them) and `channels` (the phone line and In your app). Change them
with `awesomate_agents_change set_settings {agentId, baseHash, memory?, afterCall?, channelSettings?}`,
in the hub's own field names. Only what you name changes, but a list (after-call fields, pages,
origins) replaces the whole list, so read it first and send it complete: every field with its
`description` and `choices`, every page with its `when`, exactly as `get` showed them. It is still the **draft**.

The after-call post (`call_analyzed`) to `after_call.webhook_url` is tried up to three times: again
about 30 seconds and 2 minutes after the first when it fails to connect or answers 408, 425, 429 or
5xx (a hub restart in that window drops the remaining tries). It is never tried again after another 4xx, a redirect (never followed) or no answer within 10
seconds (counted from the start, so a connection that hangs counts too; the automation may have the
post: answer the webhook at once rather than when the automation finishes). Every try
carries the same body and `call_id`, a fresh `X-Awesomate-Signature` and the try number in
`X-Awesomate-Attempt` (1 to 3). When you build the receiving automation, make it skip a `call_id`
it has already handled, so a retry never acts twice.

Some of these settings belong to the owner. Each refusal names the field in `path`:

| Code | What it means |
|---|---|
| `owner_sets_url` | `after_call.webhook_url` and `channels.in_app.context_webhook_url` must be https on the account's own n8n (build the receiving automation there). Of websites (`origins`), Claude Code may add only `localhost` for testing; the owner adds the business's own sites in the hub. |
| `owner_sets_access` | Switching customer groups (`segments`) off. Switching them on is fine. |
| `owner_sets_number` | The number customers ring (`forward_from`) and its carrier. |
| `invalid_page` | A page path that is not a path inside the app (`/orders`, no web address, no `..`). |
| `invalid_origin` | An origin that is not a host (use `app.example.com`, not a URL). |
| `try_again` (503) | The account's n8n address could not be checked just now (on `add_skill`, the procedure could not be read just now). Try again shortly. |

For an `owner_sets_*` refusal, tell the user what to set and that the owner does it on the agent's
Build page; never retry it. The greeting, voice, putting calls through and making changes live are
always done in the hub.

## An action a confirmed caller can run (Confirm who I'm talking to)

A hub agent can confirm who it is talking to with an emailed code, and then run the owner's ticked
actions for that person only. Read where it stands with `awesomate_contact_verify` and set the
agent's section with `awesomate_agents_change` `set_settings` `{verify}`.

When you build an automation such a person may run (cancel or move their own booking):

- Take `verified_contact_id` as an input in mode `session`. The hub fills it from the conversation
  after the code passes; the model never sets it and never sees it.
- Act only on records linked to that contact. Check the link inside the automation: the record's
  customer must be `verified_contact_id`, or refuse.
- Never return another person's details. A booking shared by two people shows only the caller's
  own side.
- Never change an email, phone, name or company, and never move money.
- Widening anything a stranger can reach (switching it on, a channel, a longer time, a new read,
  write or action) is the owner's: it is held as a suggestion in Tasks. Say so, never retry it.
- Say plainly: a code goes only to the address on file, so anyone who reads that inbox can be
  confirmed.
