Actions and onAction
A button the model writes does one of two things when clicked. The runtime handles the step itself, or it hands your app an event. Knowing which steps do which is the whole lesson.
By the end of this lesson you can
- read an
Action([...])chain and say, step by step, who runs each step; - list the three event types that reach
onActionand the three steps that never do; - write the
ActionEventa given button produces, includingparams,humanFriendlyMessageandformState; - define a custom action and handle it, and explain what a button with no action sends.
An action is a list of steps
The model writes an action into a component's action prop. One step needs no wrapper. Several steps go in Action([...]) and run in order. The runtime waits for @Run of a Mutation to finish, and stops the action if it fails. Host steps (@ToAssistant, @OpenUrl and custom actions) are handed to onAction and not awaited.
$title = ""
askBtn = Button("Explain this", @ToAssistant("Explain the revenue chart"))
docsBtn = Button("Docs", @OpenUrl("https://www.openui.com/docs"))
saveBtn = Button("Save", Action([@Run(createTicket), @Run(tickets), @Reset($title)]), "primary")
There are six kinds of step. Three are run inside the Renderer and are invisible to your code. Three produce an event for your onAction handler.
| Step | What it does | Who handles it |
|---|---|---|
@Run(ref) | Runs a Mutation, or fetches a Query again. A failed mutation stops the steps after it. | Runtime |
@Set($var, value) | Sets a $variable. The value is evaluated when the step runs. | Runtime |
@Reset($a, $b) | Restores $variables to their declared defaults. | Runtime |
@ToAssistant(message, context?) | Asks the model to continue the conversation. | Your app, as continue_conversation |
@OpenUrl(url) | Asks the host to navigate. | Your app, as open_url |
@Name(...) | A custom action from your library, such as @CopyToClipboard("ORD-1042"). | Your app, with type: "Name" |
OpenUI rule
The renderer runs @Set, @Reset and @Run itself. @ToAssistant, @OpenUrl and custom actions reach your app as events.
Where each step goes
Step through a click on a button whose chain mixes every kind of step. The runtime keeps the first group; the second group crosses the line into your code.
Runtime steps stay inside; event steps reach onAction
- The user clicks. The component calls
triggerAction(label, formName, action)with the evaluated action, which is a list of steps. The chain runs them in order. @Set,@Resetand@Runare handled inside the runtime. Variables change, tools are called, the screen updates. YouronActionis never called for them, so you cannot observe them there.@ToAssistant("…")reachesonActionascontinue_conversation. The message is inhumanFriendlyMessage, and an optional second argument arrives inparams.context.@OpenUrl("…")reachesonActionasopen_url, with the address inparams.url. The Renderer does not navigate for you; your handler does.- A custom action reaches
onActionwith its own name astype, and its arguments, named by the key order of its schema, inparams. - A button written with no action at all still does something: it sends
continue_conversationwith its own label as the message.
One handler, three event types
Your onAction only ever sees continue_conversation, open_url, or a type you defined yourself. If a button "does nothing" in your handler, check whether its steps are all @Set, @Reset and @Run: that is working as designed.
The ActionEvent
Every event has the same five fields. The ActionEvent your handler receives for Button("Approve", @ToAssistant("Approve this order", {orderId: 42})) inside a form named review looks like this:
{
"type": "continue_conversation",
"params": { "context": { "orderId": 42 } },
"humanFriendlyMessage": "Approve this order",
"formState": { "review": { "note": { "value": "ok", "componentType": "Input" } } },
"formName": "review"
}
| Field | Holds |
|---|---|
type | "continue_conversation", "open_url", or the name of a custom action. |
params | Step-specific data: {url} for open_url, {context} for @ToAssistant with a second argument (otherwise {}), the named arguments for a custom action. |
humanFriendlyMessage | The text for chat: the @ToAssistant message, or the button label for a button with no action. Empty for open_url and custom actions. |
formState | The field values at the moment of the click, in the runtime's own wrapper shape. Inside a form it is that form's fields, which is how a submitted form reaches the model; outside a form it is a snapshot of all $variables. |
formName | The enclosing Form's name if the button sits in one, otherwise undefined (the key is absent in JSON). |
This is the handler from this repo's reference code. It is the whole contract: a branch per event type.
Myth: the Renderer talks to the model
The Renderer never calls a model. continue_conversation is a request to your code. In a chat app, the chat component turns it into a new user message and starts a new model turn, which streams a new program. In a standalone Renderer, nothing happens until your onAction does it.
One click, step by step
Pick a scenario and step through it. The lanes are the user, the Renderer, your API, your app's onAction, and the model. The bottom panel shows what each side holds. The order card is the one from the OpenUI docs: an "Approve" button that runs a mutation, refreshes a list, then tells the model.
Things to look for
- The Renderer-to-API arrows never touch
onAction. Only the last step of the chain crosses into your app. - In the failing scenario,
@ToAssistantis skipped, because it comes after the failed@Run. The model is never told "approved" about an order that was not approved. - In the third scenario, no
Actionexists in the program at all, and acontinue_conversationstill arrives.
Custom actions
The built-in steps send messages, open links, change state and run tools. Something only your app can do, such as copying text or opening a panel, is a custom action. You declare it once with defineAction and add it to the library. The key order of params is the positional order of the arguments, as with component props.
The name and signature land in the generated prompt, so the model can write the action like a built-in one, alone or inside a chain. Defaults apply when it leaves an argument out.
copyBtn = Button("Copy order ID", @CopyToClipboard("ORD-1042"))
both = Button("Copy and close", Action([@CopyToClipboard("ORD-1042"), @Set($open, false)]))
On click the Renderer sends {type: "CopyToClipboard", params: {text: "ORD-1042", format: "plain"}, humanFriendlyMessage: ""} to onAction, and your handler does the work. The Renderer runs nothing for it. Custom actions work in the React renderer; the Vue, Svelte and Angular renderers do not support them yet.
OpenUI rule
A custom action is a name and a schema. The Renderer does not run it. It sends an event whose type is the action name, and your onAction handles it.
Watch the events arrive
Three recorded programs. The right panel is a live log of every ActionEvent the Renderer delivers to onAction, printed as exact JSON. Nothing is filtered, so a click that produces no entry is a click the runtime handled itself. Click Submit on the contact form to see a form's formState; use the order card for all three event types and a custom action; use the todo list to see tool calls that never reach onAction.
onAction log
Empty. Click a button once the stream has finished.
Simulated: no model is called. When a continue_conversation event arrives, this page plays the host's part and writes a "simulated model turn" line, where a real chat app would post the thread and stream a new program. Copying is not performed. The parser and runtime are a teaching subset of @openuidev/react-lang 1.0.1.
Things to look for
- The Inspector's State tab shows
$countrising as you press Count, while the log stays still. That is a runtime-handled@Set. - The Ask with context entry has
params.contextset, and the Tell me more entry hasparams: {}and the button's label as its message. - The chain button logs two events, in the order its steps are written.
Break it
Predict each outcome before you try it.
- On the contact form, click Submit with the fields empty.
What should happen
No event. A primary button in a form validates the fields first when its action includes
@ToAssistant, a custom action or a mutation@Run, and stops when they fail, so nothing reachesonAction. A primary button whose action only sets state or re-fetches a query skips validation. Fill in all four fields and Submit again: onecontinue_conversationarrives, with the message"Submit"and every field value informState. Cancel, a secondary button, skips validation and sends its event even with empty fields. - On the todo list, add a todo, then toggle and delete one.
What should happen
The log stays empty. Every one of those buttons is built from
@Setand@Runsteps, which the runtime handles. Open the Inspector's Tool calls tab (switch it from Actions) to see the traffic that did happen. If your app needs to know a todo changed, the model has to add a step that reaches you, such as a custom action at the end of the chain. - On the order card, click Approve and ask.
What should happen
One event:
continue_conversationwith the message "I approved order ORD-1042". The@Set($count, …)in front of it ran silently first. The page then plays the host: it writes a simulated model turn line, which is what@ToAssistantis for.
Quick check
A button is Button("Go", Action([@Set($page, 2), @Run(items), @OpenUrl("/next")])). How many times is your onAction called per click?
@Set and @Run are handled in the runtime and never arrive. @OpenUrl is one of the three kinds of step that produce an event, so exactly one event arrives.The model writes Button("Tell me more") with no action. What does the click do?
@ToAssistant(label): the label becomes the message. The generated prompt says this too, so the model can write conversational buttons without any wrapper.The chain is Action([@Run(approveOrder), @ToAssistant("Approved")]) and approve_order throws. What reaches onAction?
@Run on a mutation stops the actions after it. The failure surfaces through the mutation's status and onError, not as an ActionEvent.Recap
- An action is a list of steps.
Action([...])runs them in order; a single step needs no wrapper. @Run,@Setand@Resetare handled in the runtime and never reachonAction.@ToAssistant,@OpenUrland custom actions do.- An
ActionEventhastype,params,humanFriendlyMessage,formStateandformName.open_urlcarriesparams.url;@ToAssistantcarries its second argument inparams.context. - A button with no action sends its label as
continue_conversation. A failed mutation halts the chain, so later steps never run. - A custom action is a name plus a schema from
defineAction. The Renderer sends it to your handler and does nothing else.