A form and its guards
When. Before an item enters the selection, the person must answer something (an address), a condition must hold, or the tenant must say yes and give a price.
Steps
- Write the guards in
select.guards, an ordered list; each guard is one of:form,require,check,update,permission,policy. form: what the person fills in on the card. Each field has atype(from the table of types), andrequired,label,placeholder,enum;multiline: truefor a long text;value: "$fields.<field>"prefills it from the card;help: {label, url}adds a link. The answers become the item's fields:$item.fields.<name>.require: a condition Core reads itself, with anameand areason(the sentence the person and the agent read).check: a read operation of the tenant says whether the item is eligible; it receives the item's fields, with itsargsover them.update: the tenant sets the price (update: {price: <operation>}) or fields of the line (update: {fields: {status: <operation>.<key>}}), last.- The submit sends what the guards gave:
$item.fields.<name>, and$item.pricefor anupdate's price.
Core runs them by phase: who chooses, then the form and the conditions, then what calls the tenant. The tenant is never called for an item already refused.
The example
form-and-guards.yaml
# A form and its guards: before a delivery slot enters the selection, the person answers a form,
# a condition must hold, the tenant checks the address, then quotes the price. Guards run by
# phase — the form and the conditions first, then what calls the tenant — so the tenant is never
# called for a slot already refused. Replace api.example.com by the API.
station:
id: green-grocer
name: Green Grocer
global:
language: en
currency: EUR
locale: en-GB
domain:
domain_type: retail
entity_name: slot
entity_name_plural: slots
voice:
tone: friendly
persona: "a grocer who delivers in town"
rules:
- "Never promise a slot or a price the tool did not give."
apis:
- name: grocer
openapi_url: https://api.example.com/openapi.json
base_url: https://api.example.com
operations:
- {id: listSlots, access: read}
- {id: checkAddress, access: read} # a check is a read: it changes nothing
- {id: quoteDelivery, access: read} # so is a quote
- {id: bookSlot, access: write} # the one write: only a confirmed submit calls it
auth: {type: none}
profiles:
everyone:
when: "true"
grants: [grocer.listSlots, grocer.checkAddress, grocer.quoteDelivery, grocer.bookSlot]
skills:
- name: delivery-slots
cognitive:
objective: offer the delivery slots, for the person to choose one
invoke: {method: listSlots}
kinds: {slot: {}}
present:
fields:
name: {from: label, type: text, role: title}
zone: {from: zone, type: text}
select:
max_items: 1 # one slot: choosing another replaces it
guards:
- form: # what the PERSON answers, on the card
customer_name: {type: text, required: true, label: "Your name"} # never `name`
address: {type: text, required: true, label: "Delivery address"}
note: {type: text, multiline: true, label: "For the courier"}
- name: served-zone # a condition Core reads itself
require: "$item.fields.zone in ['north', 'centre']"
reason: "We do not deliver to this zone yet."
- check: checkAddress # the tenant says yes or no, in its own words
args: {body.address: "$item.fields.address"}
- update: {price: quoteDelivery} # the tenant quotes THIS delivery's price, last
args: {body.address: "$item.fields.address"}
submit:
- name: book
mode: request
tool: bookSlot
args:
body.slot: "$item.ref"
body.customer_name: "$item.fields.customer_name"
body.address: "$item.fields.address"
body.note: "$item.fields.note"
body.price: "$item.price" # the quoted price leaves with the line
body.idempotency_key: "$action.key"
render: {component: cards}
submit_guard:
confirm: true
The pitfalls
- Do not call a form field
name. It overwrites the item's displayed name: the line would show the customer's name instead of the slot. Writecustomer_name. Core does not refuse it. $item.fields.<name>, never$intake.<name>.$intakeis not a source: it would reach the tenant as that text. Theargsof acheckor anupdateare a literal,$params.<name>or$item.fields.<name>.- An
update: {price: …}without$item.pricein the submit is refused: the person would see a price the tenant never receives. - No
$item.pricein a condition beside anupdate(it would read the price from before the quote), and no$item.quantity. - A literal of the field's type:
$item.fields.zone == 149on atextfield is refused; write'149'. An absent value isnull:!=andnot inare true for it. - A
checkor anupdatecalls a read. Awriteoperation called when an item is chosen is refused. - At most one
formand oneupdateper list.update.fieldscannot targetname,price,image_url,code,slugorid. multilinegoes on atextonly;valueonly in the form of aselectguard (not thesubmit_guard's).- A line that carries a filled form counts one: its quantity is 1.
Reference: Select guards, Conditions.