Files
LLeMbas-Android/app/src/main/java/eu/llembas/android/api/LiveReply.kt
T
HomerandClaude Opus 5.5 4f09bc41bb
CI / check (push) Canceled after 0s
LLeMbas for Android 0.1.0 (alpha, unreleased): the first commit
A native client for a LLeMbas instance on the server's /api/v2, and the
phone's digital assistant.

- Sign in by device code; the token sealed in the Android Keystore; several
  instances.
- Chats and agent chats: the list, a chat with its reply streaming as JSON
  server-sent events (reconnecting, whole blocks), thinking, tool calls,
  steered messages, the plan, jobs, stop, edit, regenerate, continue, the
  model, effort and Plan mode, attachments and dictation.
- Cards in a sheet, answered as the page's form says it; first answer wins.
- Notifications through UnifiedPush, no Google services; an approval can be
  answered from its notification.
- The digital assistant: a VoiceInteractionService with its session (a sheet
  over the current app, "Include screen" off by default), a recognition
  service through the instance's speech-to-text, the lock-screen path that
  asks to unlock before sending, a Quick Settings tile and an Ask shortcut.
- Share to LLeMbas; Moria and Shire from the server's tokens; nine
  languages; a self-updater in the `full` flavour, none in `fdroid`.

Generated and pinned from the server, each checked in CI: the theme
(tokens.lock.json), the contract (contract/lock.json, server 2124ba9), the
strings and the icons. Built and tested only on GitHub Actions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 13:44:30 +00:00

73 lines
3.1 KiB
Kotlin

package eu.llembas.android.api
import kotlinx.serialization.json.JsonObject
/**
* A reply being written, as the stream has described it so far.
*
* Every frame carries a whole block, never a delta (the server's rule, `api/v2/stream.py`),
* so applying one is replacing a field. That is what makes a dropped connection cheap: reopen
* the stream, and its first pass rebuilds this from nothing.
*/
data class LiveReply(
val messageId: String,
val steps: List<Step> = emptyList(),
val tail: Tail = Tail(),
val plan: Plan? = null,
val status: String = "",
val cards: List<Card> = emptyList(),
val jobs: List<Job> = emptyList(),
val metrics: Metrics? = null,
val steered: Set<String> = emptySet(),
/** Set by `done`: the finished row, which replaces this in the transcript. */
val finished: Message? = null,
val title: String? = null,
/** A queued prompt the server started when this reply ended: the next stream to follow. */
val next: String? = null,
val closed: Boolean = false,
) {
/** The steps as the transcript draws them: the closed ones, then the open tail. */
val shown: List<Step>
get() = buildList {
addAll(steps)
val index = (steps.maxOfOrNull { it.index } ?: -1) + 1
if (tail.thinking.isNotEmpty()) add(Step(index = index, kind = "thinking", open = true, text = tail.thinking, label = tail.label))
if (tail.text.isNotEmpty()) add(Step(index = index, kind = "text", open = true, markdown = tail.text))
}
/** Apply one frame. Unknown events are ignored: a newer server may send more. */
fun apply(event: String, data: String): LiveReply = when (event) {
"steps" -> decode(StepsFrame.serializer(), data)?.let { copy(steps = it.steps, tail = it.tail) } ?: this
"tail" -> decode(Tail.serializer(), data)?.let { copy(tail = it) } ?: this
"steered" -> decode(SteeredFrame.serializer(), data)?.let { copy(steered = steered + it.ids) } ?: this
"plan" -> copy(plan = decodePlan(data))
"status" -> decode(StatusFrame.serializer(), data)?.let { copy(status = it.text) } ?: this
"ask" -> decode(AskFrame.serializer(), data)?.let { copy(cards = it.cards) } ?: this
"jobs" -> decode(JobsFrame.serializer(), data)?.let { copy(jobs = it.jobs) } ?: this
"metrics" -> decode(Metrics.serializer(), data)?.let { copy(metrics = it) } ?: this
"done" -> decode(DoneFrame.serializer(), data)?.let {
copy(finished = it.message, title = it.chat.title.ifEmpty { null }, next = it.next, cards = emptyList(), status = "")
} ?: this
"close" -> copy(closed = true)
else -> this
}
private fun <T> decode(serializer: kotlinx.serialization.KSerializer<T>, data: String): T? =
runCatching { LembasJson.decodeFromString(serializer, data) }.getOrNull()
private fun decodePlan(data: String): Plan? = runCatching {
val element = LembasJson.parseToJsonElement(data)
if (element is JsonObject && element.isNotEmpty()) LembasJson.decodeFromJsonElement(Plan.serializer(), element) else null
}.getOrNull()
}