A2UIRenderer

class A2UIRenderer(val catalog: A2UICatalog = A2UICatalog.Basic, val catalogId: String = DEFAULT_CATALOG_ID, instructions: String? = null, mediaUrl: (url: String) -> String?? = null, onError: (error: A2UIError, messageId: String) -> Unit? = null) : AIUIRenderer

Shows the replies an agent writes in A2UI 1.0 in the generative UI of stream-chat-android-ai-compose, with the basic catalog and the app's components beside the catalog's own.

val ui = AIUIHost(renderers = listOf(A2UIRenderer()), components = listOf(placeCard)) { action ->
viewModelScope.launch {
channel.sendMessage(Message(text = action.label, extraData = action.customData)).await()
}
}

A reply's document holds its A2UI messages: as the agent wrote them, one JSON object after another, or as JSON. Each reply has surfaces of its own, kept while the host is, so what the person enters in them survives scrolling. A button's event reaches the host as an AIUIAction named after the event, labelled with the button's text, and with its surfaceId, sourceComponentId, resolved context and the event's userMessage in its payload, with the surface's dataModel too when the agent created it with sendDataModel. The label is never the agent's words, which could say something the person didn't choose. A function the catalog doesn't define runs as one of the host's tools: a read-only one wherever the agent calls it, any other only from a person's tap, with its output as the function's value. Links open through the host, which decides the kinds of link that open.

An app component joins the catalog under its A2UI name, its name in PascalCase (place_card is PlaceCard), with its parameters as its properties. One whose name the basic catalog already has, or that isn't a valid A2UI name, is left out, so it never replaces one of the catalog's own. The catalog with the app's components goes by catalogId, and the basic catalog's id shows them as well.

Parameters

catalog

How each component of the basic catalog looks, and which of them the agent can use: one removed with A2UICatalog.removing isn't in the catalog the agent is told about. The app's components look as they draw themselves.

catalogId

The id the agent names the catalog with the app's components by.

instructions

What the agent's model is told before A2UI's own instructions, such as which answers to write as UI and when to use the app's components.

mediaUrl

The URL an image, video or audio clip loads from, given the one the agent wrote, or null to not load it, such as to load media from the app's own storage or through its CDN. The agent writes these URLs, so return only ones the app trusts. It runs while the reply draws, on the main thread. Images load only from web links whatever it returns. Without it, only web links load.

onError

Receives each problem with a reply's A2UI that A2UI reports to the agent, such as a message that doesn't read, with the reply's message id. A chat has no channel for them, so forward them to your backend if the agent should hear about them. Without it, the problems go to the log.

Constructors

Link copied to clipboard
constructor(catalog: A2UICatalog = A2UICatalog.Basic, catalogId: String = DEFAULT_CATALOG_ID, instructions: String? = null, mediaUrl: (url: String) -> String?? = null, onError: (error: A2UIError, messageId: String) -> Unit? = null)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

The info string of the fenced block an agent writes a reply's A2UI in: ui:a2ui@1.0.

Link copied to clipboard
open override val format: AIUIFormat

Functions

Link copied to clipboard
open override fun capabilities(components: List<AIUIComponent>, tools: List<AIClientTool>): Map<String, Any?>

The fence the agent writes its UI in (fence), the id of the catalog with the app's components (catalog_id), and the instructions its model needs to write UI with that catalog and call the app's tools (instructions): the renderer's own instructions first, then how a reply carries A2UI, the protocol, the catalog's guidelines, each tool with whether it only reads, what it does and the schema of its arguments, and the catalog itself, in Markdown.

Link copied to clipboard
open override fun makeEngine(host: AIUIHost): AIUIEngine
Link copied to clipboard
open override fun supports(version: String?): Boolean

Reads documents of A2UI 1.0, under any of the names agents give it, or of no version.