Pin the server contract, tokens and strings to server dev a25db24; app API 2

The contract moves from f5b3dbb to the server's dev at a25db24971a0: version.json says
{"app_api": 2}, and the server now writes its own show cards (tool_show.json: one from the tree,
one from a device) and the files routes (files_listing.json, file_content.json,
file_content_withheld.json). Tokens from the same commit change only in the header; the strings
regenerate unchanged.

- APP_API is 2, and it is now the floor as well as the ceiling: a protocol-3 server that still
  says app API 1 is a 2.0 build from before the show route and the files, and is refused as too
  old, as 1.x is.
- The files routes are modelled (FileEntry, FileListing, FileContent, Withheld) and ApiClient
  reads them: files(path), fileContent(path), and webFile(path) for the web UI's /files/view.
- shownText is documented against the server's real route, GET /api/v2/chats/{chat}/show with
  ?node=&version= or ?shown=, plain text up to 1 MB, 413 / 415 / 404; ClientTest pins the paths.
- The hand-written show cards leave ShowAndServerTest; ContractTest reads the server's.

Nothing in the app reads /api/v2/devices for its own token (devices() has no caller), so apps no
longer being listed there changes nothing here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Opus 5.5 committed 2026-11-03 14:48:03 +00:00
1 parent ac0c1c58e0
commit 0c5aa4d45f
15 files changed
+302 -57

No files matched your search

@@ -230,9 +230,13 @@ class ApiClient(val base: String, private val token: String, val http: OkHttpCli
// --- What a `show` card opens -------------------------------------------------------------
/**
* The text of a file a `show` card named, as the chat sees it now: the server rebuilds the
* chat's view and checks the path again, so a grant taken away since takes the file away too.
* Always plain text -- the app never asks for a page to render.
* 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()
@@ -252,6 +256,26 @@ class ApiClient(val base: String, private val token: String, val http: OkHttpCli
}
}
// --- 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) } }
@@ -1,7 +1,12 @@
package eu.llembas.android.api
/** The newest app API this app reads. A server with a newer one is told about, not guessed at. */
const val APP_API = 1
/**
* The app API this app reads: 2, the server's 2.0 (no canvas, no `knowledge_bases`, the `show`
* card and its route, and the person's files). A server with a newer one is told about, not
* guessed at; one with an older one is refused, because 1 has neither the show route nor the
* files this app opens -- a 2.0 `dev` server from before the files routes still said 1.
*/
const val APP_API = 2
/**
* The device-link protocol of LLeMbas 2.0. This app is built for 2.0 -- the file tree, the
@@ -19,7 +24,7 @@ fun fit(found: Discovery): ServerFit {
val protocol = maxOf(found.protocol, found.protocols.maxOrNull() ?: 0)
return when {
found.service != "lembas" -> ServerFit.NOT_LEMBAS
protocol < MIN_PROTOCOL || found.appApi < 1 || "app" !in found.scopes -> ServerFit.TOO_OLD
protocol < MIN_PROTOCOL || found.appApi < APP_API || "app" !in found.scopes -> ServerFit.TOO_OLD
found.appApi > APP_API -> ServerFit.TOO_NEW
else -> ServerFit.FITS
}
@@ -233,6 +233,50 @@ data class ShowCard(
}
}
// --- The person's files (`GET /files`, `GET /files/content`) ----------------------------------
/** One entry of a directory: `kind` is `file`, `dir` or `mount`. */
@Serializable
data class FileEntry(
val name: String = "",
val path: String = "",
val kind: String = "file",
val size: Long = 0,
val mime: String = "",
@SerialName("updated_at") val updatedAt: String? = null,
) {
/** A directory or a mount: something to open as a listing, not in the viewer. */
val isFolder: Boolean get() = kind == "dir" || kind == "mount"
}
/** A directory, through the person's own access. `parent` is null at `/`. */
@Serializable
data class FileListing(val path: String = "/", val parent: String? = null, val entries: List<FileEntry> = emptyList())
/** Why a file's text did not come: `reason` is `not_text` or `too_large`; `message` says it. */
@Serializable
data class Withheld(val reason: String = "", val message: String = "")
/**
* A file's text, as the explorer's viewer reads it. An HTML file is its source, never rendered;
* a file that is not text or is over 1 MB has an empty [text] and says why in [withheld].
*/
@Serializable
data class FileContent(
val path: String = "",
val name: String = "",
val size: Long = 0,
val mime: String = "",
val kind: String = "text",
val language: String = "",
val sha: String = "",
val text: String = "",
val withheld: Withheld? = null,
) {
/** The kind the viewer draws, by the `show` card's rule: anything unknown is plain text. */
val viewKind: String get() = if (kind in ShowCard.VIEW_KINDS) kind else "text"
}
@Serializable
data class SteerNote(val id: String = "", val text: String = "")
@@ -1,4 +1,4 @@
// Generated by scripts/fetch-tokens.py from the LLeMbas server's tokens.css at f5b3dbb965823ade56d57bf5e1d09900eb5946f2. Do not edit.
// Generated by scripts/fetch-tokens.py from the LLeMbas server's tokens.css at a25db24971a07823bda9f8dbaf517f15960536a7. Do not edit.
package eu.llembas.android.ui.theme
import androidx.compose.ui.graphics.Color
@@ -3,6 +3,7 @@ package eu.llembas.android
import eu.llembas.android.api.ApiClient
import eu.llembas.android.api.ApiException
import eu.llembas.android.api.CardAnswer
import eu.llembas.android.api.ShowCard
import eu.llembas.android.api.normaliseBase
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
@@ -61,4 +62,39 @@ class ClientTest {
val body = server.takeRequest().body.readUtf8()
assertTrue(body, body.contains(""""choices":{"q0":["Tea"]}"""))
}
@Test
fun aShownFileIsTheShowRouteAsPlainText() = runTest {
server.enqueue(MockResponse().setHeader("Content-Type", "text/plain; charset=utf-8").setBody("# Lembas\n"))
server.enqueue(MockResponse().setBody("x"))
val api = client()
val tree = ShowCard(path = "/home/frodo/recipe.md", nodeId = "node1", versionId = "ver1")
assertEquals("# Lembas\n", api.shownText("chat1", tree))
assertEquals("/api/v2/chats/chat1/show?node=node1&version=ver1", server.takeRequest().path)
api.shownText("chat1", ShowCard(path = "src/oven.py", source = "device", shownId = "shown1"))
assertEquals("/api/v2/chats/chat1/show?shown=shown1", server.takeRequest().path)
}
@Test
fun aShownFileTheServerWillNotGiveIsItsStatus() = runTest {
server.enqueue(MockResponse().setResponseCode(415).setBody("""{"detail":"This is not a text file, so it is not shown here."}"""))
try {
client().shownText("chat1", ShowCard(nodeId = "n", versionId = "v"))
fail("a 415 must throw")
} catch (e: ApiException) {
assertEquals(415, e.status)
assertEquals("This is not a text file, so it is not shown here.", e.message)
}
}
@Test
fun theFilesRoutesTakeThePathAsAQuery() = runTest {
server.enqueue(MockResponse().setBody("""{"path":"/home/frodo","parent":"/home","entries":[]}"""))
server.enqueue(MockResponse().setBody("""{"path":"/home/frodo/a b.md","name":"a b.md","text":"hi"}"""))
val api = client()
assertEquals("/home", api.files("/home/frodo").parent)
assertEquals("/api/v2/files?path=%2Fhome%2Ffrodo", server.takeRequest().path)
assertEquals("hi", api.fileContent("/home/frodo/a b.md").text)
assertEquals("/api/v2/files/content?path=%2Fhome%2Ffrodo%2Fa%20b.md", server.takeRequest().path)
}
}
@@ -1,8 +1,11 @@
package eu.llembas.android
import eu.llembas.android.api.APP_API
import eu.llembas.android.api.AskFrame
import eu.llembas.android.api.Chat
import eu.llembas.android.api.DoneFrame
import eu.llembas.android.api.FileContent
import eu.llembas.android.api.FileListing
import eu.llembas.android.api.JobsFrame
import eu.llembas.android.api.LembasJson
import eu.llembas.android.api.LiveReply
@@ -13,8 +16,12 @@ import eu.llembas.android.api.StatusFrame
import eu.llembas.android.api.SteeredFrame
import eu.llembas.android.api.StepsFrame
import eu.llembas.android.api.Tail
import eu.llembas.android.api.ToolCall
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.jsonObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
@@ -32,7 +39,59 @@ class ContractTest {
@Test
fun theVersionIsOneThisAppReads() {
val version = LembasJson.parseToJsonElement(text("version")).toString()
assertEquals("""{"app_api":1}""", version)
assertEquals("""{"app_api":$APP_API}""", version)
}
@Test
fun aShowCardFromTheTreeAndOneFromADevice() {
// The server's own `show` cards (`tool_show.json`): what the app reads them by, and the
// query that fetches the text from `GET /api/v2/chats/{chat}/show`.
val calls = LembasJson.parseToJsonElement(text("tool_show")).jsonObject.getValue("calls")
val (tree, device) = LembasJson.decodeFromJsonElement(ListSerializer(ToolCall.serializer()), calls)
val show = tree.show!!
assertEquals("show", tree.name)
assertEquals("recipe.md", show.name)
assertEquals("/home/frodo/recipe.md", show.path)
assertEquals("markdown", show.viewKind)
assertEquals(3, show.line)
assertEquals(120L, show.size)
assertEquals(listOf("node" to "node1", "version" to "ver1"), show.query)
val kept = device.show!!
assertEquals("device", kept.source)
assertEquals("code", kept.viewKind)
assertEquals("python", kept.language)
// `line: null` reads as no line, and the tree's two ids are empty on a device's card.
assertEquals(0, kept.line)
assertEquals(listOf("shown" to "shown1"), kept.query)
}
@Test
fun aDirectoryOfThePersonsFiles() {
val listing = read("files_listing", FileListing.serializer())
assertEquals("/home/frodo", listing.path)
assertEquals("/home", listing.parent)
assertEquals(listOf("recipes", "lembas.md"), listing.entries.map { it.name })
assertTrue(listing.entries[0].isFolder)
val file = listing.entries[1]
assertFalse(file.isFolder)
assertEquals(27L, file.size)
assertEquals("text/markdown", file.mime)
}
@Test
fun aFileAndAWithheldOne() {
val file = read("file_content", FileContent.serializer())
assertNull(file.withheld)
assertEquals("markdown", file.viewKind)
assertTrue(file.text.startsWith("# Lembas"))
val image = read("file_content_withheld", FileContent.serializer())
assertEquals("", image.text)
assertEquals("not_text", image.withheld!!.reason)
assertTrue(image.withheld!!.message.isNotBlank())
// An image is no kind the viewer draws, so if its text ever came it would be plain text.
assertEquals("text", image.viewKind)
}
@Test
@@ -10,47 +10,13 @@ import org.junit.Assert.assertNull
import org.junit.Test
/**
* The `show` card and the server gate. The card's shape is the server's own payload
* (`services/tools/files.py` `_run_show` for a tree file, `services/shown.py` `payload` for a
* device's) -- written out here because the server's contract fixtures do not carry a `show`
* call yet. When they do, this moves into `ContractTest`.
* What the server's contract fixtures cannot say: how the app reads a `show` card the server
* never wrote (an unknown kind), and which servers it works with at all. The server's own `show`
* cards are in `contract/tool_show.json` and read by `ContractTest`.
*/
class ShowAndServerTest {
private fun call(json: String) = LembasJson.decodeFromString(ToolCall.serializer(), json)
@Test
fun aTreeFileShowCard() {
val c = call(
"""
{"id": "call3", "name": "show", "status": "ok", "path": "/home/frodo/notes/road.md",
"show": {"path": "/home/frodo/notes/road.md", "name": "road.md", "kind": "markdown",
"language": "", "line": 12, "node_id": "n1", "version_id": "v7",
"sha": "ab12", "size": 2048, "mime": "text/markdown"},
"block": {"kind": "show", "title": "/home/frodo/notes/road.md"}}
""",
)
val show = c.show!!
assertEquals("road.md", show.name)
assertEquals("markdown", show.viewKind)
assertEquals(12, show.line)
assertEquals(listOf("node" to "n1", "version" to "v7"), show.query)
}
@Test
fun aDeviceFileShowCardNamesTheKeptCopy() {
val c = call(
"""
{"name": "show", "show": {"path": "src/main.rs", "name": "main.rs", "kind": "code",
"language": "rust", "line": 0, "node_id": "", "version_id": "", "sha": "cd34",
"size": 10, "mime": "text/plain", "source": "device", "shown_id": "s9",
"url": "/api/chats/chat1/shown/s9"}}
""",
)
assertEquals("device", c.show!!.source)
assertEquals(listOf("shown" to "s9"), c.show!!.query)
assertEquals("rust", c.show!!.language)
}
@Test
fun anUnknownKindIsPlainText() {
val c = call("""{"name": "show", "show": {"path": "x.svg", "kind": "svg"}}""")
@@ -61,17 +27,19 @@ class ShowAndServerTest {
private fun discovery(json: String) = LembasJson.decodeFromString(Discovery.serializer(), json)
@Test
fun onlyAServerOnTheTwoPointOhProtocolFits() {
fun onlyAServerOnTheTwoPointOhProtocolAndAppApiTwoFits() {
val two = """{"service": "lembas", "version": "2.0.0-beta.1", "protocol": 3, "protocols": [3],
"app_api": 1, "scopes": ["app", "models"]}"""
"app_api": 2, "scopes": ["app", "models"]}"""
assertEquals(ServerFit.FITS, fit(discovery(two)))
// A 2.0 `dev` server from before the show route and the files: protocol 3, app API 1.
assertEquals(ServerFit.TOO_OLD, fit(discovery(two.replace("\"app_api\": 2", "\"app_api\": 1"))))
// 1.7: protocol 2, the app API already there -- and still refused.
val old = """{"service": "lembas", "version": "1.7.3", "protocol": 2, "protocols": [1, 2],
"app_api": 1, "scopes": ["app", "models"]}"""
assertEquals(ServerFit.TOO_OLD, fit(discovery(old)))
// A server with no protocol at all is older still.
assertEquals(ServerFit.TOO_OLD, fit(discovery("""{"service": "lembas", "app_api": 1, "scopes": ["app"]}""")))
assertEquals(ServerFit.TOO_NEW, fit(discovery(two.replace("\"app_api\": 1", "\"app_api\": 2"))))
assertEquals(ServerFit.TOO_OLD, fit(discovery("""{"service": "lembas", "app_api": 2, "scopes": ["app"]}""")))
assertEquals(ServerFit.TOO_NEW, fit(discovery(two.replace("\"app_api\": 2", "\"app_api\": 3"))))
assertEquals(ServerFit.NOT_LEMBAS, fit(discovery("""{"service": "other", "protocol": 3}""")))
}
}