AIUIHost

class AIUIHost(renderers: List<AIUIRenderer>, components: List<AIUIComponent> = emptyList(), tools: List<AIClientTool> = emptyList(), linkSchemes: Set<String> = setOf(HTTP, HTTPS, MAILTO), send: (AIUIAction) -> Unit)

The generated UI of one conversation: the renderers of the formats it reads, the app's components and tools, each reply's state, and where what the person does in it goes.

A reply carries its UI as an AIUIDocument, which shows in place of its text when one of the host's renderers reads its format. Keep one host for as long as its conversation shows, such as in a ViewModel, so each reply's UI keeps its state across scrolling and configuration changes. It is confined to the main thread.

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

AIConversation(messages = messages, ui = ui)

Parameters

renderers

The renderer of each format the agent writes, the first one listed asked first.

components

The app's views the agent can place.

tools

The app's tools a reply's UI can run on this device, which the agent's instructions list. They run without asking the person. One that is AIClientTool.isReadOnly runs whenever the UI asks, such as to load data while it shows; any other only when the person does something that runs it.

linkSchemes

The kinds of link a reply's UI may open: web and email links by default (http, https and mailto). The agent writes every link, so add another scheme only when the app wants it.

send

Sends what the person did to the agent, as their next message.

Constructors

Link copied to clipboard
constructor(renderers: List<AIUIRenderer>, components: List<AIUIComponent> = emptyList(), tools: List<AIClientTool> = emptyList(), linkSchemes: Set<String> = setOf(HTTP, HTTPS, MAILTO), send: (AIUIAction) -> Unit)

Properties

Link copied to clipboard

What the agent's backend needs to write UI this app shows, as plain Kotlin values: each format it reads, with what its renderer adds, such as the instructions that list the app's tools, and the app's components.

Link copied to clipboard

The app's views the agent can place.

Link copied to clipboard

The renderer of each format the agent writes, the first one listed asked first.

Functions

Link copied to clipboard

Whether a reply's UI may open url: a link of one of the host's linkSchemes, web and email links by default, with a host of its own when it is a web link.

Link copied to clipboard
fun canShow(document: AIUIDocument): Boolean

Whether one of its renderers reads document. The one that does can still show nothing of its own for a reply, which then shows its text, as showsUI tells.

Link copied to clipboard

The app's component the agent places by name.

Link copied to clipboard
fun open(url: String, uriHandler: UriHandler): Boolean

Opens a link from a reply's UI with uriHandler, when canOpen lets it.

Link copied to clipboard
fun receive(messages: List<AIMessage>)

Gives its renderers the UI of the conversation's replies, oldest first, as each arrives or changes, whether or not the reply is on screen, so a format whose later replies change an earlier one's UI follows them all. It also tells them when a reply leaves the conversation, or its UI does, such as when it is deleted. AIConversation does it: call it with the conversation's messages when you show replies yourself with AIUIView.

Link copied to clipboard
suspend fun runTool(name: String, arguments: Map<String, Any?> = emptyMap(), userInitiated: Boolean = false): Any?

Runs one of the app's tools for a reply's UI, such as to load data on this device.

Link copied to clipboard
fun send(action: AIUIAction)

Sends what the person did to the agent.

Link copied to clipboard
fun setState(messageId: String, state: Any?)

Keeps the state a reply's UI was left in, or forgets it when state is null.

Link copied to clipboard
fun showsUI(document: AIUIDocument, messageId: String): Boolean

Whether a reply shows its UI rather than its text: one of the host's renderers reads document, and shows something of its own for the reply.

Link copied to clipboard
fun state(messageId: String): Any?

The state a reply's UI was left in, such as a form's values, kept while the host is.