AIConversationState

class AIConversationState<M : AIMessage>(transport: AIConversationTransport<M>, scope: CoroutineScope, val approver: AIToolApprover? = null, replyTimeout: Duration? = DefaultReplyTimeout)

The state behind a conversation with an AI agent, and what the person does in it.

It holds the conversation's messages, where the latest exchange with the agent is and why it failed, and it sends what the person asks through its AIConversationTransport. Give it to AIConversation(state) and ChatComposer(state), which show it and act on it:

class ChatViewModel(transport: AgentTransport) : ViewModel() {
val conversation = AIConversationState(transport, viewModelScope)
}

Column {
AIConversation(
state = viewModel.conversation,
modifier = Modifier.weight(1f),
footer = {
if (status == AIConversationStatus.Error) {
Button(onClick = viewModel.conversation::retry) { Text("Try again") }
}
},
)
ChatComposer(state = viewModel.conversation)
}

What the person sends shows straight away as pendingMessage, and the agent thinks until its reply starts. Requests and stops are numbered, so the outcome of one that a newer one replaced is ignored. After a stop, the reply counts as finished even while the transport still marks it as generating, and one that showed nothing is left out of messages.

It is confined to the thread of scope: pass one that runs on the main thread, such as a viewModelScope, and call it and its feed there. It lives as long as scope.

Parameters

transport

How the state reaches the agent and learns what the conversation holds. The state connects to it straight away.

scope

Where requests, stops and the reply timeout run.

approver

Who answers tool calls' questions and sign-ins on this device, which the conversation asks them.

replyTimeout

How long the state waits, once the agent has a request, for the agent to start its reply or say what it is doing, before it shows an AIAgentError. While the agent says it is working, such as thinking, it waits as long as that takes. Null or Duration.INFINITE waits as long as it takes.

Constructors

Link copied to clipboard
constructor(transport: AIConversationTransport<M>, scope: CoroutineScope, approver: AIToolApprover? = null, replyTimeout: Duration? = DefaultReplyTimeout)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val activity: StateFlow<AIAgentActivity?>

What the agent says it is doing besides what its reply shows, such as checking sources.

Link copied to clipboard
Link copied to clipboard
val error: StateFlow<Throwable?>

Why the latest request failed: what the transport threw, or, as an AIAgentError, what the agent said when it couldn't answer or that it didn't answer in time. Also why loading older messages failed. It stays until the next request, until clearError or retry, or until a reply starts after all. A failed load's error also goes once older messages load.

Link copied to clipboard
val hasOlderMessages: StateFlow<Boolean>

Whether the conversation has messages before the first one, which loadOlderMessages loads.

Link copied to clipboard
val messages: StateFlow<List<M>>

The conversation as it shows, oldest message first: the transport's messages, minus those a request replaces until its reply starts, and minus replies with nothing to show that won't show more: one that finished empty, such as the one a failed turn ends with, and one stopped before it showed anything.

Link copied to clipboard

The message the person sent last that is not in the conversation yet. It stays until its reply starts, and after it was stopped or failed, while retry can send it again.

Link copied to clipboard
val ratings: StateFlow<Map<String, AIMessageRating>>

How the person rated replies on this device, by the reply's id.

Link copied to clipboard

Where the latest exchange with the agent is.

Functions

Link copied to clipboard

Forgets error, and the request that failed.

Link copied to clipboard
suspend fun loadOlderMessages()

Loads the messages before the first one, one load at a time, while there are any. A load that fails shows as error and can be made again; one that succeeds forgets it.

Link copied to clipboard
fun rate(reply: M, rating: AIMessageRating?)

Rates a reply, or takes the rating back with null. Ratings reach the transport in the order the person gives them, and the reply's rating goes back to the one the transport has if it can't take the new one.

Link copied to clipboard
fun retry()

Tries again after error. When the transport couldn't send the person's message, it sends it again, with the same message id. Otherwise nothing is sent: the agent had the request but couldn't answer it, stopped partway through its reply, or didn't answer in time, or older messages couldn't load. Sending the message again would add it twice, so retrying forgets the error, as clearError does, and the person asks again. Does nothing without an error.

Link copied to clipboard
fun send(action: AIUIAction)

Sends what the person did in a reply's generated UI, as their next message: its label is the text, and the request's AIConversationRequest.uiAction the action. Pass it as the action callback of the conversation's AIUIHost.

fun send(message: MessageData)

Sends what the person wrote in the composer.

fun send(text: String, attachments: List<Uri> = emptyList())

Sends a message. A message with no text and no attachments is not sent.

Link copied to clipboard
fun stop()

Stops the reply the agent is writing. What it wrote so far stays. When the reply hasn't started yet, the person's message stays, and a reply that still arrives is finished whatever it says. Does nothing while the agent isn't answering.