AIConversation

fun <T> AIConversation(messages: List<T>, key: (T) -> Any, isFromUser: (T) -> Boolean, modifier: Modifier = Modifier, isThinking: Boolean = false, loadOlderMessages: suspend () -> Unit? = null, footer: @Composable () -> Unit = {}, emptyContent: @Composable () -> Unit = {}, colors: AIConversationColors = AIConversationDefaults.colors(), icons: AIConversationIcons = AIConversationDefaults.icons(), contentPadding: PaddingValues = PaddingValues(), messageContent: @Composable (T) -> Unit)

The messages of a conversation with an AI agent, oldest first, shown your own way.

AIConversation(
messages = messages,
key = { it.id },
isFromUser = { it.isMine },
isThinking = isWaitingForReply,
footer = { error?.let { RetryButton(it) } },
emptyContent = { Text("How can I help?") },
) { message ->
MessageRow(message)
}

Each message the person sends starts a turn: that message and the replies after it. The latest turn is at least as tall as the list, so when the person sends a message, the list scrolls it to the top and the reply grows below it without moving anything on screen. A conversation that loads, or replaces the one shown, opens at its end, and a button brings the end back into view whenever it is out of it.

Place the composer below the list, so the list knows how much of it is visible. To float the composer over the list instead, pass the space it covers as contentPadding:

var composerHeight by remember { mutableStateOf(0.dp) }
val density = LocalDensity.current
Box {
AIConversation(
messages = messages,
key = { it.id },
isFromUser = { it.isMine },
contentPadding = PaddingValues(bottom = composerHeight),
) { message ->
MessageRow(message)
}
Composer(
modifier = Modifier
.align(Alignment.BottomCenter)
.onSizeChanged { composerHeight = with(density) { it.height.toDp() } },
)
}

Parameters

messages

The conversation, oldest message first.

key

A stable, unique key for a message. A turn is known by its first message's key, so what is on screen stays in place as messages come and go around it. The key must be saveable in a Bundle, as with any lazy list key.

isFromUser

Whether the person using the app sent a message. Each of their messages starts a turn.

modifier

The modifier to apply to the list.

isThinking

Whether the agent is answering the latest message and its reply shows nothing yet. Meanwhile the latest turn ends with AIThinking.

loadOlderMessages

Loads the messages before the first one, which the list asks for as the person scrolls to its start. It shows a spinner meanwhile, and what is on screen stays in place as they arrive: the message that was first keeps starting a turn, even when the person's message before it arrives. Pass null once there are none left.

footer

Content under the latest turn's messages, such as what the agent is doing, or an error with a way to retry.

emptyContent

What the list shows while there are no messages, centered in it.

colors

The palette of the list's own views: the button that brings the end back into view, and the spinner while older messages load.

icons

The icons of the list's own views.

contentPadding

Space at the edges of the list that something drawn over it covers, such as a composer floating over its bottom. The messages scroll under it but stop clear of it, the latest turn is at least as tall as the part of the list it leaves visible, emptyContent is centered in that part, and the button that brings the end back into view and the spinner while older messages load show inside it. It adds to the list's own spacing.

messageContent

The view of a message.


fun AIConversation(messages: List<AIMessage>, modifier: Modifier = Modifier, pendingMessage: AIPendingMessage? = null, isThinking: Boolean = false, approver: AIToolApprover? = null, loadOlderMessages: suspend () -> Unit? = null, footer: @Composable () -> Unit = {}, emptyContent: @Composable () -> Unit = {}, colors: AIConversationColors = AIConversationDefaults.colors(), icons: AIConversationIcons = AIConversationDefaults.icons(), contentPadding: PaddingValues = PaddingValues(), ui: AIUIHost? = null)

Shows AIMessages with the views of LocalChatAiComponentFactory: each message the person sent with ChatAiComponentFactory.AIConversationUserMessage, each reply with ChatAiComponentFactory.AIConversationAssistantMessage, and under a finished reply, once its answer is written out, ChatAiComponentFactory.AIConversationReplyActions.

A reply joins the list once it shows something; until then the list shows the agent thinking.

Given a ui host, a reply whose AIMessage.uiDocument one of its renderers shows has that UI in place of its text, after its steps:

AIConversation(messages = messages, ui = viewModel.ui)

To float the composer over the list, pass the space it covers as contentPadding:

var composerHeight by remember { mutableStateOf(0.dp) }
val density = LocalDensity.current
Box {
AIConversation(
messages = messages,
contentPadding = PaddingValues(bottom = composerHeight),
)
ChatComposer(
onSendClick = ::send,
onStopClick = ::stop,
isGenerating = isGenerating,
modifier = Modifier
.align(Alignment.BottomCenter)
.onSizeChanged { composerHeight = with(density) { it.height.toDp() } },
)
}

Parameters

messages

The conversation, oldest message first.

modifier

The modifier to apply.

pendingMessage

A message the person sent that is not in messages yet. Keep passing it until its reply starts: the list stops showing it once its copy arrives.

isThinking

Whether the agent is answering the latest message and its reply shows nothing yet. The list also knows it is while pendingMessage waits, and while the latest reply is being written but shows nothing.

approver

Who answers tool calls' questions and sign-ins on this device, to ask them.

loadOlderMessages

Loads the messages before the first one, which the list asks for as the person scrolls to its start. It shows a spinner meanwhile, and what is on screen stays in place as they arrive. Pass null once there are none left.

footer

A view under the latest turn's messages, such as what the agent is doing, or an error with a way to retry.

emptyContent

What the list shows while there are no messages, centered in it.

colors

The palette of the list's own views: the button that brings the end back into view, and the spinner while older messages load.

icons

The icons of the list's own views.

contentPadding

Space at the edges of the list that something drawn over it covers, such as a composer floating over its bottom. The messages scroll under it but stop clear of it, and the list's own views show inside it.

ui

The conversation's generated UI: the renderers of the formats the agent writes, and the app's components. A reply whose UI none of them shows has its text. Keep the same host while the conversation shows, so each reply's UI keeps its state.


fun AIConversation(state: AIConversationState<*>, modifier: Modifier = Modifier, footer: @Composable () -> Unit = {}, emptyContent: @Composable () -> Unit = {}, colors: AIConversationColors = AIConversationDefaults.colors(), icons: AIConversationIcons = AIConversationDefaults.icons(), contentPadding: PaddingValues = PaddingValues(), ui: AIUIHost? = null)

Shows an AIConversationState: its messages and the message on its way, the agent thinking until its reply starts, what the agent says it is doing, and older messages as the person scrolls to the start. The state's AIConversationState.approver answers tool calls' questions.

Column {
AIConversation(state = conversation, modifier = Modifier.weight(1f))
ChatComposer(state = conversation)
}

Under each finished reply, ChatAiComponentFactory.AIConversationReplyActions gets the reply's actions: by default a row that copies, shares and rates it, through AIConversationState.rate.

While the agent says it is doing something besides thinking, such as checking external sources, the latest turn ends with an AITypingIndicator showing what it says, in place of AIThinking.

Parameters

state

The conversation to show.

modifier

The modifier to apply.

footer

A view under the latest turn's messages, such as the state's error with a way to AIConversationState.retry.

emptyContent

What the list shows while there are no messages, centered in it.

colors

The palette of the list's own views: the button that brings the end back into view, and the spinner while older messages load.

icons

The icons of the list's own views.

contentPadding

Space at the edges of the list that something drawn over it covers, such as a composer floating over its bottom.

ui

The conversation's generated UI. Make its host with AIConversationState.send as the action callback, so what the person does in a reply's UI reaches the agent.