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 onAction and the three steps that never do;
  • write the ActionEvent a given button produces, including params, humanFriendlyMessage and formState;
  • 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.

StepWhat it doesWho 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

Button click triggerAction @Set / @Reset @Run(ref) @ToAssistant("…") @OpenUrl("…") @CopyToClipboard("…") no action at all Runtime (inside the Renderer) $variables change, tools are called, the UI re-renders never delivered to onAction onAction(event) in your app type: "continue_conversation" type: "open_url" params.url type: "CopyToClipboard" continue_conversation + label
  1. 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.
  2. @Set, @Reset and @Run are handled inside the runtime. Variables change, tools are called, the screen updates. Your onAction is never called for them, so you cannot observe them there.
  3. @ToAssistant("…") reaches onAction as continue_conversation. The message is in humanFriendlyMessage, and an optional second argument arrives in params.context.
  4. @OpenUrl("…") reaches onAction as open_url, with the address in params.url. The Renderer does not navigate for you; your handler does.
  5. A custom action reaches onAction with its own name as type, and its arguments, named by the key order of its schema, in params.
  6. A button written with no action at all still does something: it sends continue_conversation with 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"
}
FieldHolds
type"continue_conversation", "open_url", or the name of a custom action.
paramsStep-specific data: {url} for open_url, {context} for @ToAssistant with a second argument (otherwise {}), the named arguments for a custom action.
humanFriendlyMessageThe text for chat: the @ToAssistant message, or the button label for a button with no action. Empty for open_url and custom actions.
formStateThe 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.
formNameThe 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

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

    Break it

    Predict each outcome before you try it.

    Quick check

    A button is Button("Go", Action([@Set($page, 2), @Run(items), @OpenUrl("/next")])). How many times is your onAction called per click?

    • Three, once per step
    • Never, because the chain contains a @Run
    • Once, with type: "open_url" and params.url set to "/next"
    @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?

    • It sends continue_conversation with the label as the message
    • Nothing, because the model did not write an action to run
    • It opens the Inspector so the user can see the event
    A button with no action behaves like @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?

    • Nothing. The failed mutation halted the chain before @ToAssistant
    • continue_conversation with "Approved", after logging the error
    • A custom event of type error, carrying the thrown message
    A failed @Run on a mutation stops the actions after it. The failure surfaces through the mutation's status and onError, not as an ActionEvent.

    Recap