Files
LLeMbas-Android/app/src/main/java/eu/llembas/android/api/ApiClient.kt
T
HomerandClaude Opus 5.5 0e8897b7da
CI / check (push) Canceled after 0s
A chat app's shape: new chat first, chats in a drawer, Scheduled
The app opens on a new chat (greeting, suggestions, composer) instead of a
list. The model is the top bar's title (picture, name, caret) and opens a
bottom-sheet picker with pictures, what each model can do and its levels.
Chats live in a navigation drawer: search, New chat, New agent chat, Files,
Scheduled, then Today / Earlier / Agent chats, Settings with the account at
the foot.

The transcript: the person's words in a bubble on the right, the model's
full width without one; thinking and each round of tools fold into chips that
open; fenced code becomes its own block with the language and Copy; attached
pictures are thumbnails; a breathing dot while a reply is written; Copy and
Regenerate under a reply, and a long-press menu (Copy, Select text, Edit,
Regenerate). The composer is one rounded field: attach (+), an Options
button that opens the tune sheet (Thinking switch or effort levels, team
size, Plan first or the agent's mode, and a new agent chat's device and
folder: only what the model offers), the microphone, and Send that becomes
Stop. Settings are Material 3 grouped lists.

Scheduled (phone first): the list (next run, last status, pause switch), a
schedule's page (rule, Run now, Pause, next runs, history with model and
Open), and create/edit in words with a preview before saving. Notification
channels Scheduled results and Scheduled failures, private on the lock
screen with a public version, actions Open and Pause. The /api/v2 routes are
provisional (api/Schedules.kt) until the server's contract lands.

Screens take their state as data (ChatContent, DrawerContent, SettingsContent,
Scheduled*Content), so the screenshot tests draw every one with fake data.
60 strings, in all eight languages. Unit tests for the fence splitter, the
drawer's grouping, the chat state the controls read, and schedule times.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 12:55:09 +00:00

443 lines
21 KiB
Kotlin

package eu.llembas.android.api
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.put
import kotlinx.serialization.serializer
import okhttp3.Call
import okhttp3.Callback
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import java.io.IOException
import java.util.concurrent.TimeUnit
import kotlin.coroutines.resumeWithException
/** Where a new agent chat runs: a linked device ([LinkedDevice.id]) and a folder on it. */
data class AgentTarget(val device: String, val directory: String = "")
/** A refusal from the server, in its own words (`{"detail": "…"}`). */
class ApiException(val status: Int, message: String) : IOException(message)
private val JSON = "application/json; charset=utf-8".toMediaType()
/** One HTTP client for the whole app: one connection pool, one TLS setup. */
val SharedHttp: OkHttpClient by lazy {
OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.build()
}
suspend fun Call.await(): Response = suspendCancellableCoroutine { cont ->
enqueue(
object : Callback {
override fun onResponse(call: Call, response: Response) = cont.resumeWith(Result.success(response))
override fun onFailure(call: Call, e: IOException) {
if (!cont.isCancelled) cont.resumeWithException(e)
}
},
)
cont.invokeOnCancellation { runCatching { cancel() } }
}
/** The address a person typed, as the instance's base URL: https assumed, no trailing slash. */
fun normaliseBase(typed: String): String {
var text = typed.trim().trimEnd('/')
if (!text.startsWith("http://") && !text.startsWith("https://")) text = "https://$text"
return text.toHttpUrl().toString().trimEnd('/')
}
/** The calls that need no token: finding the instance and signing in. */
class Doorstep(private val http: OkHttpClient = SharedHttp) {
suspend fun discover(base: String): Discovery {
val response = http.newCall(Request.Builder().url("$base/.well-known/lembas.json").build()).await()
return response.use {
if (!it.isSuccessful) throw ApiException(it.code, "Not a LLeMbas instance (${it.code}).")
LembasJson.decodeFromString(Discovery.serializer(), it.body!!.string())
}
}
suspend fun deviceCode(url: String, deviceName: String, model: String): DeviceCode {
val body = buildJsonObject {
put("scope", "app models")
put("client_id", "lembas-android")
put("device_name", deviceName)
put("device_host", model)
put("device_platform", "android")
}
val response = http.newCall(post(url, body)).await()
return response.use {
if (!it.isSuccessful) throw ApiException(it.code, detail(it))
LembasJson.decodeFromString(DeviceCode.serializer(), it.body!!.string())
}
}
/** One poll. A pending login is a 400 with `authorization_pending` -- not a failure. */
suspend fun deviceToken(url: String, deviceCode: String): DeviceToken {
val body = buildJsonObject {
put("grant_type", "urn:ietf:params:oauth:grant-type:device_code")
put("device_code", deviceCode)
}
val response = http.newCall(post(url, body)).await()
return response.use { LembasJson.decodeFromString(DeviceToken.serializer(), it.body!!.string()) }
}
private fun post(url: String, body: JsonObject) = Request.Builder().url(url).post(body.toString().toRequestBody(JSON)).build()
}
private fun agentsJson(agents: AgentsSetting) = buildJsonObject {
put("min", agents.min)
put("max", agents.max)
}
internal fun detail(response: Response): String {
val text = runCatching { response.body?.string().orEmpty() }.getOrDefault("")
val said = runCatching {
LembasJson.parseToJsonElement(text).let { it as JsonObject }["detail"]?.jsonPrimitive?.content
}.getOrNull()
return said ?: "The server answered ${response.code}."
}
/**
* Everything the app asks one instance, with its token. Every call is the server's
* `/api/v2` (or `/v1` for speech), and every refusal comes back as an [ApiException]
* carrying the server's own sentence, which is what the app shows.
*/
class ApiClient(val base: String, private val token: String, val http: OkHttpClient = SharedHttp) {
private val v2 = "$base/api/v2"
fun authed(url: String): Request.Builder = Request.Builder().url(url).header("Authorization", "Bearer $token").header("Accept", "application/json")
private suspend fun <T> call(request: Request, serializer: KSerializer<T>): T = withContext(Dispatchers.IO) {
http.newCall(request).await().use { response ->
if (!response.isSuccessful) throw ApiException(response.code, detail(response))
LembasJson.decodeFromString(serializer, response.body!!.string())
}
}
private suspend fun empty(request: Request) = withContext(Dispatchers.IO) {
http.newCall(request).await().use { response ->
if (!response.isSuccessful) throw ApiException(response.code, detail(response))
}
}
private suspend inline fun <reified T> get(path: String): T = call(authed("$v2$path").get().build(), serializer())
private suspend inline fun <reified T> send(method: String, path: String, body: JsonObject?): T =
call(authed("$v2$path").method(method, (body ?: JsonObject(emptyMap())).toString().toRequestBody(JSON)).build(), serializer())
// --- Account -----------------------------------------------------------------------------
suspend fun me(): Me = get("/me")
suspend fun models(chatId: String? = null): List<ModelInfo> = get<ModelList>(if (chatId != null) "/models?chat=$chatId" else "/models").models
suspend fun folders(): List<Folder> = get<FolderList>("/folders").folders
suspend fun devices(): List<LinkedDevice> = get<DeviceList>("/devices").devices
/** The token revokes itself (`lembas logout`'s route). */
suspend fun signOut() = empty(authed("$base/api/v1/token").delete().build())
// --- Chats -------------------------------------------------------------------------------
suspend fun chats(kind: String = "", archived: Boolean = false, before: String? = null): ChatPage {
val url = "$v2/chats".toHttpUrl().newBuilder()
.addQueryParameter("archived", archived.toString())
.apply { if (kind.isNotEmpty()) addQueryParameter("kind", kind) }
.apply { before?.let { addQueryParameter("before", it) } }
.build()
return call(authed(url.toString()).get().build(), ChatPage.serializer())
}
suspend fun unread(): List<String> = get<Unread>("/chats/unread").unread
suspend fun chat(id: String): ChatDetail = get("/chats/$id")
suspend fun messages(chatId: String, before: String): MessagePage = get("/chats/$chatId/messages?before=$before")
suspend fun createChat(
text: String,
attachments: List<String> = emptyList(),
model: String? = null,
temporary: Boolean = false,
effort: String? = null,
mode: String? = null,
agent: AgentTarget? = null,
agents: AgentsSetting? = null,
): NewChat = send(
"POST",
"/chats",
buildJsonObject {
put("text", text)
put("attachments", JsonArray(attachments.map { JsonPrimitive(it) }))
model?.let { put("model", it) }
effort?.let { put("effort", it) }
mode?.let { put("mode", it) }
agents?.let { put("agents", agentsJson(it)) }
put("temporary", temporary)
// An agent chat runs on a linked device; an empty directory is the device's first
// allowed one. A device that is no longer linked makes it an ordinary chat on the
// server, rather than losing the message.
agent?.let {
put("kind", "agent")
put("device", it.device)
if (it.directory.isNotBlank()) put("directory", it.directory.trim())
}
},
)
suspend fun sendMessage(chatId: String, text: String, attachments: List<String> = emptyList()): Turn = send(
"POST",
"/chats/$chatId/messages",
buildJsonObject {
put("text", text)
put("attachments", JsonArray(attachments.map { JsonPrimitive(it) }))
},
)
suspend fun updateChat(chatId: String, changes: JsonObject): Chat = send<ChatEnvelope>("PATCH", "/chats/$chatId", changes).chat
/** The team's size, both ends at once: the server checks the pair (422 outside its bounds). */
suspend fun setAgents(chatId: String, agents: AgentsSetting): Chat = updateChat(chatId, buildJsonObject { put("agents", agentsJson(agents)) })
suspend fun deleteChat(chatId: String) = empty(authed("$v2/chats/$chatId").delete().build())
suspend fun stop(chatId: String, messageId: String) =
empty(authed("$v2/chats/$chatId/messages/$messageId/stop").post(ByteArray(0).toRequestBody(null)).build())
suspend fun edit(chatId: String, messageId: String, text: String): Turn =
send("POST", "/chats/$chatId/messages/$messageId/edit", buildJsonObject { put("text", text) })
suspend fun regenerate(chatId: String, messageId: String): Turn = send("POST", "/chats/$chatId/messages/$messageId/regenerate", null)
suspend fun continueChat(chatId: String): Turn = send("POST", "/chats/$chatId/continue", null)
suspend fun executePlan(chatId: String): Turn = send("POST", "/chats/$chatId/plan/execute", null)
suspend fun answer(chatId: String, cardId: String, answer: CardAnswer) {
val body = LembasJson.encodeToString(CardAnswer.serializer(), answer).toRequestBody(JSON)
empty(authed("$v2/chats/$chatId/interactions/$cardId").post(body).build())
}
// --- The activity panel, background agents and the team -----------------------------------
/** Everything the panel shows; `context = false` leaves out the costly context count. */
suspend fun activity(chatId: String, context: Boolean = true): Activity = get<ActivityEnvelope>("/chats/$chatId/activity?context=$context").activity
/** The events route: an `activity` frame whenever the panel's facts change. */
fun activityEvents(chatId: String): Request = authed("$v2/chats/$chatId/activity/events").header("Accept", "text/event-stream").build()
/** One background agent and its whole conversation, read-only. `kind` is `member` or `job`. */
suspend fun agent(chatId: String, kind: String, id: String): AgentDetail = get("/chats/$chatId/agents/${segment(kind)}/${segment(id)}")
/** Stop a background agent (a member leaves the team with those under it). */
suspend fun stopAgent(chatId: String, kind: String, id: String): List<String> =
send<Stopped>("POST", "/chats/$chatId/agents/${segment(kind)}/${segment(id)}/stop", null).stopped
suspend fun team(chatId: String, since: Int = 0): TeamView = get<TeamEnvelope>("/chats/$chatId/team?since=$since").team
suspend fun stopMember(chatId: String, name: String): List<String> = send<Stopped>("POST", "/chats/$chatId/team/${segment(name)}/stop", null).stopped
// --- Scheduled tasks (provisional routes; see api/Schedules.kt) ---------------------------
suspend fun schedules(): List<Schedule> = get<ScheduleList>("/schedules").schedules
suspend fun schedule(id: String): Schedule = get<ScheduleEnvelope>("/schedules/${segment(id)}").schedule
suspend fun scheduleRuns(id: String, before: String? = null): ScheduleRuns =
get("/schedules/${segment(id)}/runs" + (before?.let { "?before=${segment(it)}" } ?: ""))
/** Words to a rule, an instruction and the next runs; nothing is saved. */
suspend fun compileSchedule(words: String): SchedulePreview =
send<SchedulePreviewEnvelope>("POST", "/schedules/compile", buildJsonObject { put("text", words) }).preview
/** Save a preview: a new schedule, or (with [id]) the one being edited. */
suspend fun saveSchedule(preview: SchedulePreview, id: String? = null): Schedule {
val body = buildJsonObject {
put("title", preview.title)
put("instruction", preview.instruction)
preview.rule?.let { put("rule", it) }
}
return if (id == null) {
send<ScheduleEnvelope>("POST", "/schedules", body).schedule
} else {
send<ScheduleEnvelope>("PATCH", "/schedules/${segment(id)}", body).schedule
}
}
suspend fun pauseSchedule(id: String, paused: Boolean): Schedule =
send<ScheduleEnvelope>("POST", "/schedules/${segment(id)}/" + if (paused) "pause" else "resume", null).schedule
suspend fun runScheduleNow(id: String) {
send<JsonObject>("POST", "/schedules/${segment(id)}/run", null)
}
suspend fun deleteSchedule(id: String) = empty(authed("$v2/schedules/${segment(id)}").delete().build())
// --- Model pictures --------------------------------------------------------------------------
/**
* A model's picture, with the token: only a path under the instance's own
* `/api/v2/models/images/` is fetched -- a list that named anything else, another host
* included, gets the initial instead. At most [IMAGE_MAX_BYTES]; null when there is none.
*/
suspend fun modelImage(path: String): ByteArray? = withContext(Dispatchers.IO) {
val url = modelImageUrl(base, path) ?: return@withContext null
http.newCall(authed(url).header("Accept", "image/*").get().build()).await().use {
if (!it.isSuccessful) return@use null
val body = it.body ?: return@use null
if (body.contentLength() > IMAGE_MAX_BYTES) return@use null
val bytes = body.source().use { source ->
source.request(IMAGE_MAX_BYTES + 1)
source.buffer.readByteArray(minOf(source.buffer.size, IMAGE_MAX_BYTES + 1))
}
bytes.takeIf { b -> b.size <= IMAGE_MAX_BYTES }
}
}
// --- Attachments -------------------------------------------------------------------------
suspend fun upload(name: String, mime: String, bytes: ByteArray, chatId: String? = null): Attachment {
val body = MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file", name, bytes.toRequestBody(mime.toMediaType()))
.build()
val url = "$v2/attachments" + (chatId?.let { "?chat=$it" } ?: "")
return call(authed(url).post(body).build(), AttachmentEnvelope.serializer()).attachment
}
suspend fun attachmentBytes(id: String): ByteArray = withContext(Dispatchers.IO) {
http.newCall(authed("$v2/attachments/$id").get().build()).await().use {
if (!it.isSuccessful) throw ApiException(it.code, detail(it))
it.body!!.bytes()
}
}
// --- What a `show` card opens -------------------------------------------------------------
/**
* The text of a file a `show` card named, as the chat sees it now:
* `GET /api/v2/chats/{chat}/show?node=<node_id>&version=<version_id>` for a file in the tree,
* `?shown=<shown_id>` for the copy a linked device sent ([ShowCard.query]). The server rebuilds
* the chat's view and checks the path again, so a grant taken away since takes the file away
* too (404, in the server's words). `text/plain; charset=utf-8`, at most 1 MB: above it a 413,
* and a 415 for a file that is not text. Always plain text -- an HTML file is its source, and
* the app never asks for a page to render.
*/
suspend fun shownText(chatId: String, show: ShowCard): String = withContext(Dispatchers.IO) {
val url = "$v2/chats/$chatId/show".toHttpUrl().newBuilder()
.apply { show.query.forEach { (k, v) -> addQueryParameter(k, v) } }
.build()
http.newCall(authed(url.toString()).header("Accept", "text/plain").get().build()).await().use {
if (!it.isSuccessful) throw ApiException(it.code, detail(it))
val body = it.body!!
// The server shows at most 1 MB; anything much larger is not a file it meant.
if (body.contentLength() > SHOWN_MAX_BYTES) throw ApiException(413, "")
val bytes = body.source().use { source ->
source.request(SHOWN_MAX_BYTES + 1)
source.buffer.readByteArray(minOf(source.buffer.size, SHOWN_MAX_BYTES + 1))
}
if (bytes.size > SHOWN_MAX_BYTES) throw ApiException(413, "")
bytes.toString(Charsets.UTF_8)
}
}
// --- The person's files ------------------------------------------------------------------
/** A directory, as the person may see it (no chat's grants): `GET /api/v2/files?path=`. */
suspend fun files(path: String = "/"): FileListing {
val url = "$v2/files".toHttpUrl().newBuilder().addQueryParameter("path", path).build()
return call(authed(url.toString()).get().build(), FileListing.serializer())
}
/**
* A file's text: `GET /api/v2/files/content?path=`. A file the server will not hand over as
* text (not text, over 1 MB) is not an error: it comes back with `withheld` saying why.
*/
suspend fun fileContent(path: String): FileContent {
val url = "$v2/files/content".toHttpUrl().newBuilder().addQueryParameter("path", path).build()
return call(authed(url.toString()).get().build(), FileContent.serializer())
}
/** A file of the tree in the web UI's own viewer (`/files/view`), for what the app does not draw. */
fun webFile(path: String): String = "$base/files/view".toHttpUrl().newBuilder().addQueryParameter("path", path).build().toString()
/** The same file in the web UI's viewer, which renders HTML in its sealed frame. */
fun webViewer(chatId: String, show: ShowCard): String = "$base/chat/$chatId/show".toHttpUrl().newBuilder()
.apply { show.query.forEach { (k, v) -> addQueryParameter(k, v) } }
.apply { if (show.line > 0) addQueryParameter("line", show.line.toString()) }
.build()
.toString()
// --- Push --------------------------------------------------------------------------------
suspend fun pushKey(): PushKey = get("/push")
suspend fun registerPush(endpoint: String, p256dh: String, auth: String) {
val body = buildJsonObject {
put("endpoint", endpoint)
put("p256dh", p256dh)
put("auth", auth)
}
empty(authed("$v2/push").post(body.toString().toRequestBody(JSON)).build())
}
suspend fun forgetPush() = empty(authed("$v2/push").delete().build())
// --- Voice (/v1, the `models` scope) -----------------------------------------------------
suspend fun transcribe(wav: ByteArray, language: String? = null): String {
val body = MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file", "speech.wav", wav.toRequestBody("audio/wav".toMediaType()))
.apply { language?.let { addFormDataPart("language", it) } }
.build()
return call(authed("$base/v1/audio/transcriptions").post(body).build(), Transcript.serializer()).text
}
suspend fun speech(text: String): ByteArray = withContext(Dispatchers.IO) {
val body = buildJsonObject {
put("input", text.take(20_000))
put("response_format", "mp3")
}
http.newCall(authed("$base/v1/audio/speech").post(body.toString().toRequestBody(JSON)).build()).await().use {
if (!it.isSuccessful) throw ApiException(it.code, detail(it))
it.body!!.bytes()
}
}
fun streamRequest(chatId: String, messageId: String): Request =
authed("$v2/chats/$chatId/messages/$messageId/stream").header("Accept", "text/event-stream").build()
companion object {
/** 1 MB, the server's own limit on a shown file, with room for a little more. */
const val SHOWN_MAX_BYTES = 1_100_000L
fun emptyBody(): RequestBody = ByteArray(0).toRequestBody(null)
/** A model's picture is small; anything larger is not one. */
const val IMAGE_MAX_BYTES = 2_000_000L
private const val IMAGE_PATH = "/api/v2/models/images/"
/** The picture's URL on [base], or null for a path that is not one of the instance's pictures. */
fun modelImageUrl(base: String, path: String?): String? {
if (path.isNullOrBlank() || !path.startsWith(IMAGE_PATH)) return null
val name = path.removePrefix(IMAGE_PATH)
if (name.isEmpty() || !name.all { it.isLetterOrDigit() || it == '.' || it == '_' || it == '-' } || name.startsWith(".")) return null
return "$base$IMAGE_PATH$name"
}
/** One path segment, escaped: an agent's name or a job's id is a model's choice. */
fun segment(value: String): String = java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20")
}
}