Contract at server dev 9ab0818: spec 3.7.0 from the server -- end, ms, always, the builds
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
- Pinned: 75 files, with the spec's display.json (DisplayTest runs its
duration and end cases) and progress.json's budget stop.
- Not additive: a call's chips no longer carry "exit 0", and an edit's are
one "+N -M" (the app draws chips only, so nothing is said twice);
block.title follows the spec's key order.
- A message's `end {phase, reason, text}` is what a stopped or failed reply
is drawn from, so a reloaded one says "Stopped: by you" too; the app
writes the line in its own language from the phase and the reason.
- A call's time is block.duration (the server's), else made from `ms`;
its state is block.state; a call without a title shows block.label.
- "Always allow" only when every item of the approval has `always`.
- The notifications build's updater takes its file from latest.json, else
from GET /apps' `notifications {url, sha256}` of the same version.
- ApiClient.mentions and attachFrom with their models and fixtures; the
composer does not use them yet.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
38 files changed
+1343
-48
No files matched your search
@@ -174,6 +174,27 @@ class ApiClient(val base: String, private val token: String, val http: OkHttpCli
|
||||
/** The app releases this server recommends; 404 from a server older than the route. */
|
||||
suspend fun apps(): AppsInfo = get("/apps")
|
||||
|
||||
/** What `@` offers from the server for [q]: the chat's earlier attachments, files of the tree, a web address. */
|
||||
suspend fun mentions(q: String, chatId: String? = null): Mentions = get("/mentions?q=${segment(q)}" + (chatId?.let { "&chat=${segment(it)}" } ?: ""))
|
||||
|
||||
/**
|
||||
* Attach without uploading: a copy of an earlier attachment, a file of the tree as it is now,
|
||||
* or a web page's text -- whichever [mention] is. 404 for what cannot be reached, 400 for a
|
||||
* page that cannot be fetched.
|
||||
*/
|
||||
suspend fun attachFrom(mention: Mention, chatId: String? = null): Attachment = send<AttachmentEnvelope>(
|
||||
"POST",
|
||||
"/attachments/from",
|
||||
buildJsonObject {
|
||||
when (mention.kind) {
|
||||
"attachment" -> put("attachment", mention.id)
|
||||
"url" -> put("url", mention.url)
|
||||
else -> put("path", mention.path)
|
||||
}
|
||||
chatId?.let { put("chat", it) }
|
||||
},
|
||||
).attachment
|
||||
|
||||
/** The web's search in JSON: chats by their words (and meaning, where indexed), files, memories. */
|
||||
suspend fun search(q: String, within: String = "all", limit: Int = 20): SearchResults = get("/search?q=${segment(q)}&in=${segment(within)}&limit=$limit")
|
||||
|
||||
|
||||
@@ -32,12 +32,31 @@ const val TITLE_MAX = 80
|
||||
* what `/api/v2` says of it.
|
||||
*/
|
||||
fun ToolCall.state(): String = when {
|
||||
// The server's own word, from spec 3.7.0; an older one leaves it to what the call carries.
|
||||
block.state in CALL_STATES -> block.state
|
||||
|
||||
status == "denied" -> "denied"
|
||||
|
||||
status == "error" || !error.isNullOrEmpty() -> "failed"
|
||||
|
||||
status == "running" || status == "pending" -> "running"
|
||||
|
||||
else -> "done"
|
||||
}
|
||||
|
||||
private val CALL_STATES = setOf("running", "done", "failed", "denied")
|
||||
|
||||
/**
|
||||
* What is written after a call's title for its time: the server's own [ToolBlock.duration] where
|
||||
* it sends one (it measured the call), else made from the call's `ms`; nothing for a call that is
|
||||
* running or was denied.
|
||||
*/
|
||||
fun ToolCall.took(): String? {
|
||||
val state = state()
|
||||
if (state == "running" || state == "denied") return null
|
||||
return block.duration.ifBlank { null } ?: callDuration(state, ms)
|
||||
}
|
||||
|
||||
/**
|
||||
* How long a finished call took (`display.md` §2): tenths under a minute (`0.4s`), then
|
||||
* `1m 05s`, then `2h 03m`. None for a call that is running or was denied, or whose time was not
|
||||
@@ -59,16 +78,24 @@ fun callDuration(state: String, ms: Long?): String? {
|
||||
|
||||
/**
|
||||
* How a reply ended, when it did not finish (`display.md` §4): the `stopped` or `failed` progress
|
||||
* whose line stays on it. From the end the stream said (kept on the message as its `progress`,
|
||||
* with the reason: by you, step limit, budget), else from what the stored row knows -- that it
|
||||
* was stopped, or its error, of which the first line is the reason. Null for a reply that
|
||||
* whose line stays on it. From the row's own `end` (spec 3.7.0: the phase and the reason, stored
|
||||
* with the reply); from an older server, the end the stream said (kept on the message as its
|
||||
* `progress`), else what the row knows -- that it was stopped, or its error, of which the first
|
||||
* line is the reason. Null for a reply that
|
||||
* finished, a user's message, or one still queued.
|
||||
*/
|
||||
fun Message.end(): Progress? = when {
|
||||
fun Message.ending(): Progress? = when {
|
||||
role != "assistant" -> null
|
||||
|
||||
// What the server stored with the reply (spec 3.7.0): it stays true after a reload.
|
||||
end?.phase in END_PHASES -> Progress(phase = end?.phase.orEmpty(), reason = end?.reason?.ifBlank { null })
|
||||
|
||||
progress?.phase in END_PHASES -> progress
|
||||
|
||||
!error.isNullOrBlank() -> Progress(phase = "failed", reason = error.lineSequence().firstOrNull { it.isNotBlank() }?.trim())
|
||||
|
||||
stopped -> Progress(phase = "stopped")
|
||||
|
||||
else -> null
|
||||
}
|
||||
|
||||
|
||||
@@ -222,7 +222,26 @@ data class SearchResults(
|
||||
|
||||
/** One app release the server recommends (`GET /api/v2/apps`), with the page its files are on. */
|
||||
@Serializable
|
||||
data class AppRelease(val version: String = "", val tag: String = "", val channel: String = "", val page: String = "")
|
||||
data class AppRelease(
|
||||
val version: String = "",
|
||||
val tag: String = "",
|
||||
val channel: String = "",
|
||||
val page: String = "",
|
||||
/** The release's other builds, for an updater that installs its own kind: `{url, sha256}` each, where the server knows them. */
|
||||
val notifications: AppBuild? = null,
|
||||
val fdroid: AppBuild? = null,
|
||||
)
|
||||
|
||||
/** One APK of a release: where it is and its SHA-256. */
|
||||
@Serializable
|
||||
data class AppBuild(val url: String = "", val sha256: String = "")
|
||||
|
||||
/** What `@` offers from the server (`GET /api/v2/mentions`): an earlier `attachment` by [id], a `file` of the tree by [path], or a `url`. */
|
||||
@Serializable
|
||||
data class Mention(val kind: String = "", val id: String = "", val path: String = "", val url: String = "", val name: String = "", val detail: String = "")
|
||||
|
||||
@Serializable
|
||||
data class Mentions(val items: List<Mention> = emptyList())
|
||||
|
||||
@Serializable
|
||||
data class ServerChannel(val version: String = "", val channel: String = "")
|
||||
@@ -355,8 +374,23 @@ data class Metrics(
|
||||
@SerialName("tok_s") val tokPerSecond: Double = 0.0,
|
||||
)
|
||||
|
||||
/**
|
||||
* How a call is drawn (`display.md`): its [kind], the one short [title], the facts after it as
|
||||
* [chips] (an edit's `+N −M`, a non-zero `exit N`, the hits), and from spec 3.7.0 its [state]
|
||||
* (running, done, failed, denied), the [duration] of a finished call as the spec writes it, and
|
||||
* the tool's name as a person reads it ([label]). The last three are empty from an older server.
|
||||
*/
|
||||
@Serializable
|
||||
data class ToolBlock(val kind: String = "tool", val title: String = "", val chips: List<String> = emptyList(), val added: Int = 0, val removed: Int = 0)
|
||||
data class ToolBlock(
|
||||
val kind: String = "tool",
|
||||
val title: String = "",
|
||||
val chips: List<String> = emptyList(),
|
||||
val added: Int = 0,
|
||||
val removed: Int = 0,
|
||||
val state: String = "",
|
||||
val duration: String = "",
|
||||
val label: String = "",
|
||||
)
|
||||
|
||||
/** One tool call. What the tool wrote varies by tool, so the free-form parts stay JSON. */
|
||||
@Serializable
|
||||
@@ -491,6 +525,14 @@ data class Step(
|
||||
val messages: List<SteerNote> = emptyList(),
|
||||
)
|
||||
|
||||
/**
|
||||
* A reply's end when it did not finish: `stopped` with `person`, `steps`, `budget` or no reason,
|
||||
* or `failed` with the error's first line. [text] is the line in the account's language; the app
|
||||
* writes its own from [phase] and [reason], in the app's language.
|
||||
*/
|
||||
@Serializable
|
||||
data class MessageEnd(val phase: String = "", val reason: String = "", val text: String = "")
|
||||
|
||||
@Serializable
|
||||
data class Message(
|
||||
val id: String,
|
||||
@@ -499,6 +541,8 @@ data class Message(
|
||||
val complete: Boolean = true,
|
||||
val stopped: Boolean = false,
|
||||
val error: String? = null,
|
||||
/** How a reply that did not finish ended (spec 3.7.0); null for one that finished, or from an older server. */
|
||||
val end: MessageEnd? = null,
|
||||
val queued: Boolean = false,
|
||||
val machine: Boolean = false,
|
||||
val origin: String = "",
|
||||
@@ -540,6 +584,8 @@ data class CardItem(
|
||||
val options: List<Option> = emptyList(),
|
||||
val multiple: Boolean = false,
|
||||
@SerialName("free_text") val freeText: Boolean = true,
|
||||
/** An approval: whether "Always allow" is offered beside "Allow once" (spec 3.7.0). An older server says nothing, and offered it always. */
|
||||
val always: Boolean = true,
|
||||
val editable: Boolean = false,
|
||||
val plan: JsonObject? = null,
|
||||
@SerialName("mode_choice") val modeChoice: Boolean = false,
|
||||
|
||||
@@ -51,8 +51,7 @@ import eu.llembas.android.api.Message
|
||||
import eu.llembas.android.api.ShowCard
|
||||
import eu.llembas.android.api.Step
|
||||
import eu.llembas.android.api.ToolCall
|
||||
import eu.llembas.android.api.callDuration
|
||||
import eu.llembas.android.api.state
|
||||
import eu.llembas.android.api.took
|
||||
import eu.llembas.android.ui.MarkdownText
|
||||
import eu.llembas.android.ui.theme.LocalPalette
|
||||
|
||||
@@ -99,7 +98,8 @@ fun ToolCallBlock(call: ToolCall, depth: Int = 0, onShow: ((ShowCard) -> Unit)?
|
||||
Icon(iconFor(call.block.kind), contentDescription = null, modifier = Modifier.size(16.dp), tint = palette.inkMuted)
|
||||
Spacer(Modifier.width(6.dp))
|
||||
Text(
|
||||
call.block.title.ifBlank { call.name },
|
||||
// The one short title; a call without one is named as a person reads the tool.
|
||||
call.block.title.ifBlank { call.block.label.ifBlank { call.name } },
|
||||
fontFamily = if (call.block.kind == "shell") FontFamily.Monospace else null,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
maxLines = if (open) 6 else 1,
|
||||
@@ -110,7 +110,7 @@ fun ToolCallBlock(call: ToolCall, depth: Int = 0, onShow: ((ShowCard) -> Unit)?
|
||||
Text(chip, style = MaterialTheme.typography.labelSmall, color = palette.inkFaint, modifier = Modifier.padding(start = 6.dp))
|
||||
}
|
||||
// How long a finished call took (display.md §2); a running or a denied one shows none.
|
||||
callDuration(call.state(), call.ms)?.let { took ->
|
||||
call.took()?.let { took ->
|
||||
Text(took, style = MaterialTheme.typography.labelSmall, color = palette.inkFaint, maxLines = 1, modifier = Modifier.padding(start = 6.dp))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,8 +121,11 @@ private fun Approval(card: Card, onAnswer: (CardAnswer) -> Unit) {
|
||||
} else {
|
||||
FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Button(onClick = { onAnswer(CardAnswer(verdict = "allow", texts = texts)) }) { Text(stringResource(R.string.card_allow_once)) }
|
||||
OutlinedButton(onClick = { onAnswer(CardAnswer(verdict = "allow_always", texts = texts)) }) {
|
||||
Text(stringResource(R.string.card_allow_always))
|
||||
// Only where the request offers it: a call that may only ever be allowed once has none.
|
||||
if (offersAlways(card)) {
|
||||
OutlinedButton(onClick = { onAnswer(CardAnswer(verdict = "allow_always", texts = texts)) }) {
|
||||
Text(stringResource(R.string.card_allow_always))
|
||||
}
|
||||
}
|
||||
// The spec's four actions (display.md §5): Deny at once, or with the person's words to the model.
|
||||
TextButton(onClick = { onAnswer(CardAnswer(verdict = "deny")) }) { Text(stringResource(R.string.card_deny)) }
|
||||
@@ -234,6 +237,9 @@ private fun PlanCard(card: Card, onAnswer: (CardAnswer) -> Unit) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether an approval card offers "Always allow": only when every call on it may be remembered (`always`). */
|
||||
internal fun offersAlways(card: Card): Boolean = card.items.isNotEmpty() && card.items.all { it.always }
|
||||
|
||||
/** A plan's approval as the server reads it: allow, and the mode to continue in where one was chosen. */
|
||||
internal fun planApproval(item: CardItem, mode: String?): CardAnswer =
|
||||
CardAnswer(verdict = "allow", choices = if (item.modeChoice && mode != null) mapOf(item.key to listOf(mode)) else emptyMap())
|
||||
|
||||
@@ -85,7 +85,7 @@ import eu.llembas.android.api.Message
|
||||
import eu.llembas.android.api.ShowCard
|
||||
import eu.llembas.android.api.Step
|
||||
import eu.llembas.android.api.ToolCall
|
||||
import eu.llembas.android.api.end
|
||||
import eu.llembas.android.api.ending
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.ui.MarkdownText
|
||||
import eu.llembas.android.ui.Segment
|
||||
@@ -228,7 +228,7 @@ fun AssistantMessage(
|
||||
// How a reply that did not finish ended (display.md §4): the progress text alone --
|
||||
// "Stopped: by you", "Failed: …" -- staying at the end of the reply; an error in
|
||||
// the error colour, a stop muted. Nothing else says it a second time.
|
||||
message.end()?.let { end ->
|
||||
message.ending()?.let { end ->
|
||||
Text(
|
||||
progressLineOf(end, withElapsed = false) ?: message.error.orEmpty(),
|
||||
color = if (end.phase == "failed") palette.danger else palette.inkMuted,
|
||||
|
||||
@@ -10,9 +10,11 @@ import android.net.Uri
|
||||
import android.provider.Settings
|
||||
import eu.llembas.android.BuildConfig
|
||||
import eu.llembas.android.R
|
||||
import eu.llembas.android.api.AppRelease
|
||||
import eu.llembas.android.api.LembasJson
|
||||
import eu.llembas.android.api.SharedHttp
|
||||
import eu.llembas.android.api.await
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.notify.NotificationAccess
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -38,6 +40,16 @@ data class Latest(val version: String, val url: String, val sha256: String, val
|
||||
* same key: Android takes it), so without its own file it installs nothing.
|
||||
*/
|
||||
fun fileFor(notificationsBuild: Boolean): LatestFile? = if (notificationsBuild) notifications else LatestFile(url, sha256)
|
||||
|
||||
/**
|
||||
* The same, with what the instance says of the release it recommends (`GET /api/v2/apps`,
|
||||
* its entry's `notifications` build) for a manifest that names no notifications file -- but
|
||||
* only when both speak of the same version, and the address is https.
|
||||
*/
|
||||
fun fileFor(notificationsBuild: Boolean, recommended: AppRelease?): LatestFile? = fileFor(notificationsBuild)
|
||||
?: recommended?.takeIf { notificationsBuild && it.version == version }?.notifications
|
||||
?.takeIf { it.url.startsWith("https://") && it.sha256.isNotBlank() }
|
||||
?.let { LatestFile(it.url, it.sha256) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +75,14 @@ class Updater(private val context: Context) {
|
||||
)
|
||||
return@withContext context.getString(R.string.update_allow_installs)
|
||||
}
|
||||
val file = latest.fileFor(NotificationAccess.AVAILABLE) ?: return@withContext context.getString(R.string.update_notifications_manual)
|
||||
// Its own kind: from the manifest, else from the instance's recommendation of the same release.
|
||||
val recommended = if (NotificationAccess.AVAILABLE && latest.notifications == null) {
|
||||
runCatching { context.lembas.instances.client()?.apps()?.android }.getOrNull()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val file = latest.fileFor(NotificationAccess.AVAILABLE, recommended)
|
||||
?: return@withContext context.getString(R.string.update_notifications_manual)
|
||||
val apk = File(context.cacheDir, "update.apk")
|
||||
SharedHttp.newCall(Request.Builder().url(file.url).build()).await().use { response ->
|
||||
if (!response.isSuccessful) return@withContext context.getString(R.string.update_unreachable)
|
||||
|
||||
Reference in new issue
Block a user