diff --git a/android/README.md b/android/README.md index c78e777..8f27955 100644 --- a/android/README.md +++ b/android/README.md @@ -77,8 +77,11 @@ manages the device fleet the claw keeps. **subscribe first, then POST**, the reply arrives on the stream. Thinking deltas render dim and collapsible, tool rows show name/summary with error coloring (bash-like tools render as shell lines instead: dim `$` prefix, - the command line, and an exit-code badge), pasted payloads embedded in a - message as paired `U+FFFC` marks render as compact `▣N 5L` chips, and + the command line, and an exit-code badge), a message's paste chips render + as compact `▣N 5L` labels sliced from the span record its + `user_message` carries beside the plain text (rows recorded before + spans rode the stream still scan the legacy paired-`U+FFFC` form in + the text), and `attachment` events render as cards: file name, size, caption, an inline preview for small (≤ 2 MB) images, and a download-and-open row (bytes from `GET /v1/attachments/{id}` land in the app cache and open through a diff --git a/android/app/src/main/java/rs/rhema/phone/chat/ChatController.kt b/android/app/src/main/java/rs/rhema/phone/chat/ChatController.kt index a115f96..60f2009 100644 --- a/android/app/src/main/java/rs/rhema/phone/chat/ChatController.kt +++ b/android/app/src/main/java/rs/rhema/phone/chat/ChatController.kt @@ -11,6 +11,7 @@ import rs.rhema.phone.net.ClawApi import rs.rhema.phone.net.ClawEvent import rs.rhema.phone.net.ClawResult import rs.rhema.phone.net.OutboundImage +import rs.rhema.phone.net.PasteSpan import rs.rhema.phone.net.SseFrame import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -61,6 +62,10 @@ class ChatController( override val id: Long, val text: String, val images: List = emptyList(), + // The chip record this surface's rows never carry (its + // composer sends plain text) but a replayed row — a web user's + // chips in this conversation — does; the render slices on it. + val pastes: List = emptyList(), ) : Item data class Thinking(override val id: Long, val text: String) : Item data class ToolCall( @@ -355,14 +360,15 @@ class ChatController( is ClawEvent.UserMessage -> { // The recorded question — replayed on reload, echoed live // for a message this surface just sent. An identical last - // row — same text, same image count — is that echo's - // stand-in; skip it, previews and all. + // row — same text, same image count, same chip record — + // is that echo's stand-in; skip it, previews and all. val last = list.lastOrNull() - if (!(last is UserMessage && last.text == event.text && last.images.size == event.images.size)) { + if (!(last is UserMessage && last.text == event.text && last.images.size == event.images.size && last.pastes == event.pastes)) { list = list + UserMessage( newId(), event.text, event.images.map { SentImage(attachmentId = it.id, file = null, mediaType = it.mediaType) }, + event.pastes, ) } } diff --git a/android/app/src/main/java/rs/rhema/phone/net/Models.kt b/android/app/src/main/java/rs/rhema/phone/net/Models.kt index 29e86d7..5842a08 100644 --- a/android/app/src/main/java/rs/rhema/phone/net/Models.kt +++ b/android/app/src/main/java/rs/rhema/phone/net/Models.kt @@ -117,6 +117,13 @@ data class RhemaNotification( @Serializable data class UserImage(val id: String, @SerialName("media_type") val mediaType: String) +// One paste chip a user_message records: the byte span (UTF-8) of the +// payload in the message's plain text, plus the payload's line count +// — the session annotation's own shape (rhema-core `PasteSpan`), so a +// chip renders from the record instead of rebuilding the wire form. +@Serializable +data class PasteSpan(val start: Int = 0, val end: Int = 0, val lines: Int = 0) + // One event of GET /v1/sessions/{key}/history: the record's seq for it, // beside the event itself. Seq order is the wire order. @Serializable @@ -138,11 +145,13 @@ data class HistoryPage(val events: List = emptyList(), val more: B sealed interface ClawEvent { @Serializable @SerialName("user_message") - // Rows recorded before images rode the stream carry no field at - // all; absent folds to none, never a guessed shape. + // Rows recorded before images — and then before chips — rode the + // stream carry no field at all; absent folds to none, never a + // guessed shape. data class UserMessage( val text: String = "", val images: List = emptyList(), + val pastes: List = emptyList(), ) : ClawEvent @Serializable diff --git a/android/app/src/main/java/rs/rhema/phone/ui/ChatScreen.kt b/android/app/src/main/java/rs/rhema/phone/ui/ChatScreen.kt index 8692b04..15aeb9b 100644 --- a/android/app/src/main/java/rs/rhema/phone/ui/ChatScreen.kt +++ b/android/app/src/main/java/rs/rhema/phone/ui/ChatScreen.kt @@ -82,6 +82,7 @@ import rs.rhema.phone.chat.MAX_STAGED_IMAGES import rs.rhema.phone.data.AgentIdentityCache import rs.rhema.phone.di.AppContainer import rs.rhema.phone.net.ClawResult +import rs.rhema.phone.net.PasteSpan import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.launch @@ -438,6 +439,7 @@ private sealed interface TurnGroup { val id: Long, val text: String, val images: List, + val pastes: List = emptyList(), ) : TurnGroup { override val key: String = "u$id" } @@ -451,7 +453,7 @@ private fun turnGroups(items: List): List { val groups = mutableListOf() for (item in items) { when (item) { - is ChatController.UserMessage -> groups += TurnGroup.User(item.id, item.text, item.images) + is ChatController.UserMessage -> groups += TurnGroup.User(item.id, item.text, item.images, item.pastes) else -> { val last = groups.lastOrNull() if (last is TurnGroup.Agent) last.items += item else groups += TurnGroup.Agent(mutableListOf(item)) @@ -501,10 +503,13 @@ private fun TurnGroupRow( } } } - // Embedded pastes render as chips, not payloads; the - // text around them stays verbatim. + // Chips render as chips, not payloads; the text + // around them stays verbatim. The row's chip record + // slices them out of the plain text; a row from + // before the record rode the stream carries none + // and still scans the mark form in the text. var chip = 0 - pasteParts(group.text).forEach { part -> + messageParts(group.text, group.pastes).forEach { part -> when (part) { is PastePart.Text -> Text(part.text, style = MaterialTheme.typography.bodyLarge) is PastePart.Paste -> { @@ -740,20 +745,67 @@ private fun PasteChip(label: String) { ) } -// --- embedded pastes -------------------------------------------------- -// The wire form a long paste travels in: MARK, payload, MARK — one -// logical unit inside otherwise plain text (rhema-core's paste module -// is the definition; these are its client-side shape). A mark with no -// partner is literal text. +// --- paste chips ------------------------------------------------------ +// A long paste rides as a chip the render stands in for its payload. +// The record is the span list a user_message carries (the session +// annotation's own shape); the marks in the text are the record's +// legacy spelling, kept for rows that predate it — a mark with no +// partner is literal text (rhema-core's paste module is the +// definition; these are its client-side shape). private const val PASTE_MARK = '\uFFFC' -/** One scanned piece of embedded-form text. */ +/** One piece of a rendered message: literal text, or a chip standing + * for its payload. */ private sealed interface PastePart { data class Text(val text: String) : PastePart data class Paste(val payload: String) : PastePart } +/** A message's parts. The chip record is the modern form — each span + * names the payload's byte range in the plain text — and rows recorded + * before it rode the stream carry none; those still scan the legacy + * mark form in the text itself. */ +private fun messageParts(text: String, pastes: List = emptyList()): List = + if (pastes.isEmpty()) pasteParts(text) else spanParts(text, pastes) + +/** Splits a record-carrying message into its parts, left to right. The + * spans' offsets are UTF-8 bytes, the plain form the fold measured — + * every character boundary is mapped in one walk, and a span that + * splits a character maps to none: the render skips it rather than cut + * one in half. Malformed input never throws. */ +private fun spanParts(text: String, pastes: List): List { + var bytes = 0 + var units = 0 + val at = mutableMapOf(0 to 0) + var index = 0 + while (index < text.length) { + val codePoint = text.codePointAt(index) + val width = Character.charCount(codePoint) + index += width + units = index + bytes += when { + codePoint < 0x80 -> 1 + codePoint < 0x800 -> 2 + codePoint < 0x10000 -> 3 + else -> 4 + } + at[bytes] = units + } + val parts = mutableListOf() + var cut = 0 + for (span in pastes.sortedBy { it.start }) { + val start = at[span.start] + val end = at[span.end] + if (start == null || end == null || start < cut || end < start) continue + if (start > cut) parts += PastePart.Text(text.substring(cut, start)) + parts += PastePart.Paste(text.substring(start, end)) + cut = end + } + if (cut < text.length) parts += PastePart.Text(text.substring(cut)) + return parts +} + /** Splits embedded-form text into its parts, left to right. A mark with * no partner is literal text; malformed input never throws. */ private fun pasteParts(text: String): List {