A2UI.Live behaviour (A2UI v0.3.0)

Copy Markdown View Source

Macro for integrating A2UI into a Phoenix LiveView.

Add use A2UI.Live to your LiveView module to get:

  • @a2ui_surfaces assign initialized via on_mount
  • Automatic handling of {:a2ui_message, msg} info messages
  • Automatic handling of a2ui_action and a2ui_input_change events
  • A handle_a2ui_action/3 callback for your action logic

Example

defmodule MyAppWeb.DemoLive do
  use MyAppWeb, :live_view
  use A2UI.Live

  def mount(_params, _session, socket) do
    # connect to an agent and start receiving messages
    {:ok, socket}
  end

  @impl A2UI.Live
  def handle_a2ui_action(action, metadata, socket) do
    # handle user actions from A2UI components
    {:noreply, socket}
  end
end

Summary

Callbacks

handle_a2ui_action(action, metadata, socket)

@callback handle_a2ui_action(
  action :: A2UI.Protocol.Messages.Action.t(),
  metadata :: map(),
  socket :: Phoenix.LiveView.Socket.t()
) :: {:noreply, Phoenix.LiveView.Socket.t()}

handle_a2ui_error(error, metadata, socket)

@callback handle_a2ui_error(
  error :: A2UI.Protocol.Messages.Error.t(),
  metadata :: map(),
  socket :: Phoenix.LiveView.Socket.t()
) :: {:noreply, Phoenix.LiveView.Socket.t()}