AIConversationState
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
How the state reaches the agent and learns what the conversation holds. The state connects to it straight away.
Where requests, stops and the reply timeout run.
Who answers tool calls' questions and sign-ins on this device, which the conversation asks them.
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
Properties
What the agent says it is doing besides what its reply shows, such as checking sources.
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.
Whether the conversation has messages before the first one, which loadOlderMessages loads.
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.
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.
How the person rated replies on this device, by the reply's id.
Where the latest exchange with the agent is.
Functions
Forgets error, and the request that failed.
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.
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.
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.
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.
Sends what the person wrote in the composer.
Sends a message. A message with no text and no attachments is not sent.