A2UI.Transport behaviour (A2UI v0.3.0)

Copy Markdown View Source

Behaviour for A2UI message transport.

A transport handles bidirectional communication between a client (LiveView or external) and an agent:

On connect/1, the transport creates an A2UI.Connection struct and registers with the agent. The agent dispatches messages back through deliver_message/2 using the connection's transport module and ref.

Dispatch Helpers

Instead of calling a specific transport module directly, you can use the dispatch functions on this module:

A2UI.Transport.send_action(transport, action, metadata)

These dispatch to the correct implementation based on the transport struct.

Summary

Functions

Disconnects the transport. Dispatches based on the struct module.

Sends an action through the transport. Dispatches based on the struct module.

Sends an error through the transport. Dispatches based on the struct module.

Types

t()

@type t() :: struct()

Callbacks

connect(opts)

(optional)
@callback connect(opts :: keyword()) :: {:ok, t()} | {:error, any()}

deliver_message(ref, message)

@callback deliver_message(ref :: term(), message :: struct()) :: :ok | {:error, any()}

Delivers an A2UI protocol message to the connected client.

Called by A2UI.Agent.send_message/2 using the connection's transport module and ref. The ref argument is transport-specific — for Local transport it is the LiveView pid; for SSE it is the handler process pid.

disconnect(t)

(optional)
@callback disconnect(t()) :: :ok

send_action(t, t, metadata)

(optional)
@callback send_action(t(), A2UI.Protocol.Messages.Action.t(), metadata :: map()) ::
  :ok | {:error, any()}

send_error(t, t, metadata)

(optional)
@callback send_error(t(), A2UI.Protocol.Messages.Error.t(), metadata :: map()) ::
  :ok | {:error, any()}

Functions

disconnect(transport)

@spec disconnect(t()) :: :ok

Disconnects the transport. Dispatches based on the struct module.

send_action(transport, action, metadata)

@spec send_action(t(), A2UI.Protocol.Messages.Action.t(), map()) ::
  :ok | {:error, any()}

Sends an action through the transport. Dispatches based on the struct module.

send_error(transport, error, metadata)

@spec send_error(t(), A2UI.Protocol.Messages.Error.t(), map()) ::
  :ok | {:error, any()}

Sends an error through the transport. Dispatches based on the struct module.