CI / check (push) Canceled after 0s
The server's /api/v2 now carries the 3.4.0 subagents (2fd3c03):
- the stream's `tree` frame {tree} (event_tree.json): LiveReply keeps it,
ChatState keeps the last one after the reply ends (cleared when another
chat opens), and the activity sheet draws it as the reduced list. The
provisional `Activity.tree` is gone -- the tree never came that way.
- an ask item's `subagent {id, name, position}` (event_ask.json card4): the
approval names it with the page's sentence ("Subagent parser (s1),
reviews the parser, asks:"), translated from the server's catalogue.
- an agent chat's `subagents.offered` is true where the instance allows
them (chat_agent.json): the Subagents setting shows for an existing agent
chat by the chat's own word.
ContractTest: the tree frame, the subagent's card; the agent chat offered.
Screenshot 24-approval-subagent; 26-subagents from the tree.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
3.5 KiB
Kotlin
79 lines
3.5 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 = "",
|
|
/** The structured progress beside [status] (spec 3.4.0); null from an older server, or idle. */
|
|
val progress: Progress? = null,
|
|
val cards: List<Card> = emptyList(),
|
|
val jobs: List<Job> = emptyList(),
|
|
/** The subagent tree (spec 3.4.0), from the `tree` frame; null until there is one. */
|
|
val tree: SubagentTree? = null,
|
|
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, progress = it.progress) } ?: this
|
|
|
|
"ask" -> decode(AskFrame.serializer(), data)?.let { copy(cards = it.cards) } ?: this
|
|
|
|
"jobs" -> decode(JobsFrame.serializer(), data)?.let { copy(jobs = it.jobs) } ?: this
|
|
|
|
"tree" -> decode(TreeFrame.serializer(), data)?.let { copy(tree = it.tree) } ?: 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 = "", progress = null)
|
|
} ?: 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()
|
|
}
|