A message of the person's with `queued` true says how it waits under its
bubble -- queued behind a reply, of unknown arrival at the linked computer,
or held by the computer for its next turn (the server's sentences) -- and
has two buttons there: Send now and Discard.
Send now asks `POST …/messages/{id}/send`; the message stops waiting where
it stood, the chat is read once (a linked computer's chat sorts the message
to where it was sent) and the reply the send started is followed as one the
person asked for by typing. A computer that still knew the message does not
run it again and the reply's row is removed: then that reply is not
followed. Discard asks once, then `POST …/discard`, and the row goes.
A 409 or a 404 is said in the instance's words and the chat is read again,
once nothing is being written on the screen. Any other failure is said and
the message stays as it is. While one action is asked the buttons rest.
A waiting message is no longer offered Edit: the instance refuses it.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
22 files changed
+699
-13
No files matched your search
@@ -342,6 +342,17 @@ class ApiClient(val base: String, private val token: String, val http: OkHttpCli
|
||||
},
|
||||
)
|
||||
|
||||
/**
|
||||
* Send a message that waits (`queued`) now: the message as it is after, and the reply started
|
||||
* for it, as a message sent answers. 409 in the instance's words while a reply is being
|
||||
* written in the chat, and when the message no longer waits.
|
||||
*/
|
||||
suspend fun sendWaiting(chatId: String, messageId: String): Turn = send("POST", "/chats/$chatId/messages/${segment(messageId)}/send", DECLARED)
|
||||
|
||||
/** Withdraw a message that waits: its row is gone. 409 in words when it no longer waits, 404 when it is not there. */
|
||||
suspend fun discardWaiting(chatId: String, messageId: String): Discarded =
|
||||
call(authed("$v2/chats/$chatId/messages/${segment(messageId)}/discard").post(emptyBody()).build(), Discarded.serializer())
|
||||
|
||||
suspend fun regenerate(chatId: String, messageId: String): Turn = send("POST", "/chats/$chatId/messages/$messageId/regenerate", DECLARED)
|
||||
|
||||
suspend fun continueChat(chatId: String): Turn = send("POST", "/chats/$chatId/continue", DECLARED)
|
||||
|
||||
@@ -161,6 +161,41 @@ const val ORIGIN_UNSURE = "unsure"
|
||||
*/
|
||||
val Message.fateUnknown: Boolean get() = isUser && queued && origin == ORIGIN_UNSURE
|
||||
|
||||
/** The `origin` of a message the linked computer has said it holds for its next turn. */
|
||||
const val ORIGIN_CARRIED = "carried"
|
||||
|
||||
/** Why a message of the person's has not been given to a model yet. */
|
||||
enum class Waiting {
|
||||
/** A reply was being written when it was sent: it goes when its turn comes. */
|
||||
Queued,
|
||||
|
||||
/** Handed to the linked computer, its arrival never heard: the instance never sends it by itself. */
|
||||
Unsure,
|
||||
|
||||
/** The linked computer holds it and takes it in with its next turn. */
|
||||
Carried,
|
||||
}
|
||||
|
||||
/**
|
||||
* How this message waits, or null for one that does not: a message of the person's with
|
||||
* `queued` true. Such a message is not a turn yet, so it is sent now or discarded, never
|
||||
* edited. An origin this app does not know waits as a plain queued one does.
|
||||
*/
|
||||
val Message.waiting: Waiting?
|
||||
get() = when {
|
||||
!isUser || !queued -> null
|
||||
origin == ORIGIN_UNSURE -> Waiting.Unsure
|
||||
origin == ORIGIN_CARRIED -> Waiting.Carried
|
||||
else -> Waiting.Queued
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether Edit is offered on this message of the person's: not while a reply is being
|
||||
* [writing], and never on one that still waits -- the instance refuses that, since it has not
|
||||
* been a turn yet and a linked computer may still hold its old words.
|
||||
*/
|
||||
fun Message.editable(writing: Boolean): Boolean = isUser && !writing && !queued
|
||||
|
||||
/** Whether nobody typed this row: the server wrote it (`machine`), or a device's harness did. */
|
||||
val Message.byMachine: Boolean get() = machine || origin == ORIGIN_HARNESS
|
||||
|
||||
|
||||
@@ -725,6 +725,10 @@ data class Message(
|
||||
@Serializable
|
||||
data class Turn(val message: Message? = null, val reply: Message? = null)
|
||||
|
||||
/** The answer to discarding a waiting message: the row that is gone, by its id. */
|
||||
@Serializable
|
||||
data class Discarded(val id: String = "", val discarded: Boolean = false)
|
||||
|
||||
@Serializable
|
||||
data class NewChat(val chat: Chat, val message: Message? = null, val reply: Message? = null)
|
||||
|
||||
|
||||
@@ -103,6 +103,7 @@ import eu.llembas.android.api.Message
|
||||
import eu.llembas.android.api.Raw
|
||||
import eu.llembas.android.api.ShowCard
|
||||
import eu.llembas.android.api.drawn
|
||||
import eu.llembas.android.api.editable
|
||||
import eu.llembas.android.api.unrecordedReplies
|
||||
import eu.llembas.android.ui.ModelAvatar
|
||||
import eu.llembas.android.ui.ProgressStatus
|
||||
@@ -275,6 +276,7 @@ fun ChatContent(
|
||||
var tuning by remember { mutableStateOf(false) }
|
||||
var renaming by remember { mutableStateOf(false) }
|
||||
var editing by remember { mutableStateOf<Message?>(null) }
|
||||
var discarding by remember { mutableStateOf<Message?>(null) }
|
||||
var suggestion by remember { mutableStateOf("") }
|
||||
var suggested by remember { mutableIntStateOf(0) }
|
||||
val copy: (String) -> Unit = { text ->
|
||||
@@ -502,7 +504,15 @@ fun ChatContent(
|
||||
|
||||
Drawn.Note -> MachineNote(message.text.orEmpty())
|
||||
|
||||
Drawn.Person -> UserMessage(message, canEdit = live == null, onEdit = { editing = message })
|
||||
// A message that waits is sent now or discarded, never edited.
|
||||
Drawn.Person -> UserMessage(
|
||||
message,
|
||||
canEdit = message.editable(writing = live != null),
|
||||
onEdit = { editing = message },
|
||||
busy = state.acting != null,
|
||||
onSendNow = { actions.sendNow(message) },
|
||||
onDiscard = { discarding = message },
|
||||
)
|
||||
|
||||
// One call for a reply being written and the same reply finished:
|
||||
// what the person opened in it (a thinking chip) stays open at `done`.
|
||||
@@ -571,6 +581,21 @@ fun ChatContent(
|
||||
)
|
||||
}
|
||||
|
||||
// Discarding cannot be taken back, so it is asked once -- in the page's own words.
|
||||
discarding?.let { message ->
|
||||
AlertDialog(
|
||||
onDismissRequest = { discarding = null },
|
||||
text = { Text(stringResource(R.string.discard_message_confirm)) },
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
actions.discard(message)
|
||||
discarding = null
|
||||
}) { Text(stringResource(R.string.discard)) }
|
||||
},
|
||||
dismissButton = { TextButton(onClick = { discarding = null }) { Text(stringResource(R.string.cancel)) } },
|
||||
)
|
||||
}
|
||||
|
||||
editing?.let { message ->
|
||||
var text by remember(message.id) { mutableStateOf(message.text.orEmpty()) }
|
||||
AlertDialog(
|
||||
|
||||
@@ -115,6 +115,8 @@ data class ChatState(
|
||||
val picked: String? = null,
|
||||
/** The chat's long generations (a video, a track, a 3D model), by id, as each was last read. */
|
||||
val mediaJobs: Map<String, JobSeen> = emptyMap(),
|
||||
/** The waiting message that is being sent now or discarded, by its id, until the instance has answered. */
|
||||
val acting: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -311,6 +313,12 @@ interface ChatActions {
|
||||
fun stop() {}
|
||||
fun regenerate(message: Message) {}
|
||||
fun edit(message: Message, text: String) {}
|
||||
|
||||
/** Send a message that waits ([Message.waiting]) now, as a turn with a reply of its own. */
|
||||
fun sendNow(message: Message) {}
|
||||
|
||||
/** Withdraw a message that waits: it was never given to a model, and its row goes. */
|
||||
fun discard(message: Message) {}
|
||||
fun continueChat() {}
|
||||
fun executePlan() {}
|
||||
fun answer(card: Card, answer: CardAnswer) {}
|
||||
@@ -721,6 +729,34 @@ class ChatViewModel(app: Application) :
|
||||
turn.reply?.let { follow(it.id, own = true) }
|
||||
}
|
||||
|
||||
/**
|
||||
* A waiting message's two actions. Each is one request a tap starts. The reply a send
|
||||
* starts is the person's own, followed as one they typed for; a refusal's read waits until
|
||||
* nothing is being written on this screen (a reply's own stream says everything until
|
||||
* then, and a read beside it could draw a stale row over a finished one), and at the gate.
|
||||
*/
|
||||
private val waiting = WaitingActions(
|
||||
state = _state,
|
||||
read = { refresh() },
|
||||
follow = { follow(it.reply, it.own) },
|
||||
say = ::fail,
|
||||
catchUp = {
|
||||
val mark = reads.mark()
|
||||
_state.first { !it.running }
|
||||
rereadAndFollow(rounds = 1, mark = mark)
|
||||
},
|
||||
)
|
||||
|
||||
override fun sendNow(message: Message) {
|
||||
val chat = _state.value.chat ?: return
|
||||
viewModelScope.launch { waiting.sendNow(message) { api().sendWaiting(chat.id, message.id) } }
|
||||
}
|
||||
|
||||
override fun discard(message: Message) {
|
||||
val chat = _state.value.chat ?: return
|
||||
viewModelScope.launch { waiting.discard(message) { api().discardWaiting(chat.id, message.id) } }
|
||||
}
|
||||
|
||||
override fun continueChat() = control { api, chat -> took(api.continueChat(chat.id)) }
|
||||
|
||||
override fun executePlan() = control { api, chat ->
|
||||
|
||||
@@ -19,6 +19,8 @@ import androidx.compose.foundation.horizontalScroll
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.FlowRow
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
@@ -91,10 +93,11 @@ import eu.llembas.android.api.ModelInfo
|
||||
import eu.llembas.android.api.ShowCard
|
||||
import eu.llembas.android.api.Step
|
||||
import eu.llembas.android.api.ToolCall
|
||||
import eu.llembas.android.api.Waiting
|
||||
import eu.llembas.android.api.ending
|
||||
import eu.llembas.android.api.fateUnknown
|
||||
import eu.llembas.android.api.replyModel
|
||||
import eu.llembas.android.api.shownMarkdown
|
||||
import eu.llembas.android.api.waiting
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.ui.MarkdownText
|
||||
import eu.llembas.android.ui.Segment
|
||||
@@ -117,13 +120,28 @@ private val Pill = RoundedCornerShape(50)
|
||||
private val FootTop = 2.dp
|
||||
private val FootHeight = 48.dp
|
||||
|
||||
/** A waiting message's two buttons: a tap target's height. */
|
||||
private val WaitingButton = 48.dp
|
||||
|
||||
/**
|
||||
* The person's turn: their words in a bubble on the right (never markdown), what they attached
|
||||
* above it as thumbnails. A long press offers Copy and, where the turn can still change, Edit.
|
||||
*
|
||||
* A message that waits says how it waits in words under the bubble, and -- where [onSendNow]
|
||||
* and [onDiscard] are given, which only the chat's own screen does -- offers the two things a
|
||||
* person does with it. They rest while [busy]: one of them is being asked of the instance.
|
||||
*/
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@OptIn(ExperimentalFoundationApi::class, ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
fun UserMessage(message: Message, canEdit: Boolean, onEdit: () -> Unit, modifier: Modifier = Modifier) {
|
||||
fun UserMessage(
|
||||
message: Message,
|
||||
canEdit: Boolean,
|
||||
onEdit: () -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
busy: Boolean = false,
|
||||
onSendNow: (() -> Unit)? = null,
|
||||
onDiscard: (() -> Unit)? = null,
|
||||
) {
|
||||
val palette = LocalPalette.current
|
||||
val copy = LocalCopy.current
|
||||
var menu by remember { mutableStateOf(false) }
|
||||
@@ -170,15 +188,32 @@ fun UserMessage(message: Message, canEdit: Boolean, onEdit: () -> Unit, modifier
|
||||
}
|
||||
}
|
||||
}
|
||||
if (message.queued) {
|
||||
// The server's queued progress ("Queued, 2 ahead · 12s") where it sends one.
|
||||
// Not one whose arrival at the computer is unknown: that one waits for the person, not for its turn.
|
||||
message.waiting?.let { waiting ->
|
||||
// How it waits, in words. Queued behind a reply: the server's progress ("Queued: 2
|
||||
// ahead · 12s") where it sends one. Not for one a linked computer was handed: that
|
||||
// one waits for the person or for the computer, not for its turn here.
|
||||
Text(
|
||||
if (message.fateUnknown) stringResource(R.string.fate_unknown) else progressLineOf(message.progress) ?: stringResource(R.string.queued),
|
||||
when (waiting) {
|
||||
Waiting.Unsure -> stringResource(R.string.fate_unknown)
|
||||
Waiting.Carried -> stringResource(R.string.fate_carried)
|
||||
Waiting.Queued -> progressLineOf(message.progress) ?: stringResource(R.string.queued)
|
||||
},
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = if (message.fateUnknown) palette.warning else palette.inkFaint,
|
||||
color = if (waiting == Waiting.Unsure) palette.warning else palette.inkFaint,
|
||||
modifier = Modifier.padding(top = 4.dp, end = 4.dp),
|
||||
)
|
||||
if (onSendNow != null && onDiscard != null) {
|
||||
// Two buttons of unknown width (eight languages, any type size): they share a
|
||||
// row that wraps, each a full tap target, from the bubble's edge.
|
||||
FlowRow(horizontalArrangement = Arrangement.spacedBy(4.dp, Alignment.End)) {
|
||||
TextButton(onClick = onDiscard, enabled = !busy, modifier = Modifier.heightIn(min = WaitingButton)) {
|
||||
Text(stringResource(R.string.discard))
|
||||
}
|
||||
TextButton(onClick = onSendNow, enabled = !busy, modifier = Modifier.heightIn(min = WaitingButton)) {
|
||||
Text(stringResource(R.string.send_now))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package eu.llembas.android.ui.chat
|
||||
|
||||
import eu.llembas.android.api.ApiException
|
||||
import eu.llembas.android.api.ChatDetail
|
||||
import eu.llembas.android.api.Message
|
||||
import eu.llembas.android.api.Turn
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
|
||||
/**
|
||||
* A waiting message was sent now ([turn], the instance's answer): its row is the message as it
|
||||
* is after -- no longer waiting -- where it stood, and the reply started for it is the last row.
|
||||
* No row is there twice: the list's rows are keyed by id.
|
||||
*/
|
||||
fun ChatState.sentNow(turn: Turn): ChatState {
|
||||
val sent = turn.message
|
||||
val reply = turn.reply
|
||||
val rows = messages.filterNot { it.id == reply?.id }.map { if (it.id == sent?.id) sent else it }
|
||||
return copy(messages = rows + listOfNotNull(reply), lastSent = sent?.id ?: lastSent)
|
||||
}
|
||||
|
||||
/** A waiting message was discarded: its row is gone, and nothing else moved. */
|
||||
fun ChatState.discarded(id: String): ChatState = copy(messages = messages.filterNot { it.id == id })
|
||||
|
||||
/**
|
||||
* Whether a refusal says the screen showed a message as waiting that is not so any more, or
|
||||
* could not be acted on as shown: 409 (a reply is being written; the message no longer waits)
|
||||
* and 404 (it is not there). The chat is then read again. Anything else -- no network, a server
|
||||
* error -- says nothing about the message, and the row stays as it is.
|
||||
*/
|
||||
fun staleWaiting(e: Throwable): Boolean = e is ApiException && (e.status == 409 || e.status == 404)
|
||||
|
||||
/** A reply to follow, and whether the person asked for it from this screen. */
|
||||
data class Followed(val reply: String, val own: Boolean)
|
||||
|
||||
/**
|
||||
* Which reply is followed once a waiting message was sent now: the one the send started
|
||||
* ([reply]), as a reply the person asked for from this screen -- unless the chat, [read] after
|
||||
* the send, no longer holds it as being written. That happens by design: a linked computer
|
||||
* that still knew the message does not run it a second time, and the reply started for it is
|
||||
* removed. Then whatever else the read says is being written is followed, as nobody's.
|
||||
*/
|
||||
fun followedAfterSend(reply: String?, read: ChatDetail?): Followed? {
|
||||
if (reply != null && (read == null || read.live == reply || read.messages.any { it.id == reply && !it.complete })) {
|
||||
return Followed(reply, own = true)
|
||||
}
|
||||
return read?.live?.let { Followed(it, own = false) }
|
||||
}
|
||||
|
||||
/**
|
||||
* The two things a person does with a message that waits, as the chat's screen does them. The
|
||||
* instance is asked through each action's own request; what the screen shows is [state].
|
||||
*
|
||||
* Sent: the row stops waiting at once, the chat is read ([read]; a chat on a linked computer
|
||||
* sorts the message to where it was sent), and the reply started for it is followed ([follow])
|
||||
* exactly as one the person asked for by typing. Discarded: the row is gone. Refused with a
|
||||
* 409 or a 404: the instance's sentence is said ([say]) and the chat is read again
|
||||
* ([catchUp]), because what the screen showed as waiting is not so any more. While one of the
|
||||
* two is under way for a message, [ChatState.acting] names it and its buttons rest.
|
||||
*/
|
||||
class WaitingActions(
|
||||
private val state: MutableStateFlow<ChatState>,
|
||||
private val read: suspend () -> ChatDetail?,
|
||||
private val follow: (Followed) -> Unit,
|
||||
private val say: (Throwable) -> Unit,
|
||||
private val catchUp: suspend () -> Unit,
|
||||
) {
|
||||
suspend fun sendNow(message: Message, send: suspend () -> Turn) = act(message) {
|
||||
val turn = send()
|
||||
state.update { it.sentNow(turn) }
|
||||
followedAfterSend(turn.reply?.id, read())?.let(follow)
|
||||
}
|
||||
|
||||
suspend fun discard(message: Message, withdraw: suspend () -> Unit) = act(message) {
|
||||
withdraw()
|
||||
state.update { it.discarded(message.id) }
|
||||
}
|
||||
|
||||
/** One action at a time: a second tap while the first is asked is not a second request. */
|
||||
private suspend fun act(message: Message, action: suspend () -> Unit) {
|
||||
if (state.value.acting != null) return
|
||||
state.update { it.copy(acting = message.id) }
|
||||
var stale = false
|
||||
try {
|
||||
action()
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
say(e)
|
||||
stale = staleWaiting(e)
|
||||
} finally {
|
||||
state.update { if (it.acting == message.id) it.copy(acting = null) else it }
|
||||
}
|
||||
// After the buttons are free again: the read may wait for a reply being written here.
|
||||
if (stale) catchUp()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user