A file an agent shows is in the chat by its kind; an edit's diff is on its approval card
CI / check (push) Canceled after 0s

A tool call's `media` with `shown` is drawn where generated media is: a
picture with its name under it, audio and video with Play, a PDF with Open
(its pages over the chat, by node id), and a 3D model, an archive, a text
file, a binary and any kind the app does not know as a row with the name,
the kind in words and the size. Every card keeps Open in Files, Download,
Share and Open with. `audio` plays as `music` does.

An approval item's `diffs` are worked out on the phone line by line
(api/Diff.kt: shared head and tail, the longest shared run in between up to
a bounded size, three lines of context) and drawn as one plain monospaced
text per file in a sideways scroller of its own, folded past twelve lines
and laid out to four hundred. The diff stands where the call's arguments
did; Allow and Deny are unchanged.

A picture in a chat now waits at the gate before it is fetched. Parsed and
not drawn: a device's files, files_writable and files_that_run_code, and
the editor's two models.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Fable 5.2 committed 2026-11-24 13:31:30 +00:00
1 parent 660ff90b3a
commit 5e5e63353a
24 files changed
+1076 -63

No files matched your search

@@ -0,0 +1,114 @@
package eu.llembas.android.api
// An edit's diff on its approval card, worked out on the phone from the two texts the card
// carries (`contract/event_ask_edit.json`). Lines only: what is drawn is text with a sign, and
// every step is bounded, because both texts are a model's and a file's words of any size.
/** What a line of a diff is: in both texts, only in the new one, only in the old one, or the mark of lines left out between two changes. */
enum class DiffSign { Same, Added, Removed, Gap }
data class DiffLine(val sign: DiffSign, val text: String)
/** One file's change as the card draws it: the changed lines with a little around them, and how many were added and removed. */
data class DiffView(val lines: List<DiffLine>, val added: Int, val removed: Int) {
/** The lines drawn: the first [DIFF_FOLD] until the person opens it, and never more than [DIFF_MOST]. */
fun shown(open: Boolean): List<DiffLine> = lines.take(if (open) DIFF_MOST else DIFF_FOLD)
/** Whether there is more than the folded card shows. */
val folds: Boolean get() = lines.size > DIFF_FOLD
/** The lines not drawn even when open. */
val beyond: Int get() = (lines.size - DIFF_MOST).coerceAtLeast(0)
}
/** How many lines of a file's diff the card shows before it is opened. */
const val DIFF_FOLD = 12
/** The most lines of one file's diff the card lays out: a card is for deciding, not for reading a megabyte. */
const val DIFF_MOST = 400
/** The lines kept around a change. */
const val DIFF_CONTEXT = 3
/** Past this many pairs of lines the two middles are not compared: the old one is shown going and the new one coming. */
private const val DIFF_CELLS = 1_000_000L
/** A text's lines; the newline that ends the last one starts no empty line. */
internal fun textLines(text: String): List<String> {
if (text.isEmpty()) return emptyList()
val lines = text.split('\n')
return if (lines.last().isEmpty()) lines.dropLast(1) else lines
}
/**
* [old] turned into [new], line by line: what both begin and end with is the same without being
* compared, and the middle is the longest run of lines the two share -- or, where the middles
* are too long to compare on a phone, the old middle removed and the new one added.
*/
fun diffLines(old: List<String>, new: List<String>): List<DiffLine> {
var head = 0
while (head < old.size && head < new.size && old[head] == new[head]) head++
var tail = 0
while (tail < old.size - head && tail < new.size - head && old[old.size - 1 - tail] == new[new.size - 1 - tail]) tail++
val a = old.subList(head, old.size - tail)
val b = new.subList(head, new.size - tail)
val out = ArrayList<DiffLine>(old.size + b.size)
for (i in 0 until head) out += DiffLine(DiffSign.Same, old[i])
if (a.isEmpty() || b.isEmpty() || a.size.toLong() * b.size > DIFF_CELLS) {
a.forEach { out += DiffLine(DiffSign.Removed, it) }
b.forEach { out += DiffLine(DiffSign.Added, it) }
} else {
// shared[i][j]: how many lines the rest of a from i and the rest of b from j have in common, in order.
val shared = Array(a.size + 1) { IntArray(b.size + 1) }
for (i in a.size - 1 downTo 0) {
for (j in b.size - 1 downTo 0) {
shared[i][j] = if (a[i] == b[j]) shared[i + 1][j + 1] + 1 else maxOf(shared[i + 1][j], shared[i][j + 1])
}
}
var i = 0
var j = 0
while (i < a.size && j < b.size) {
when {
a[i] == b[j] -> {
out += DiffLine(DiffSign.Same, a[i])
i++
j++
}
shared[i + 1][j] >= shared[i][j + 1] -> out += DiffLine(DiffSign.Removed, a[i++])
else -> out += DiffLine(DiffSign.Added, b[j++])
}
}
while (i < a.size) out += DiffLine(DiffSign.Removed, a[i++])
while (j < b.size) out += DiffLine(DiffSign.Added, b[j++])
}
for (i in old.size - tail until old.size) out += DiffLine(DiffSign.Same, old[i])
return out
}
/** The changed lines of [lines] with [context] unchanged ones around each, and one [DiffSign.Gap] where lines between two changes are left out. */
fun hunks(lines: List<DiffLine>, context: Int = DIFF_CONTEXT): List<DiffLine> {
val keep = BooleanArray(lines.size)
lines.forEachIndexed { at, line ->
if (line.sign != DiffSign.Same) for (k in maxOf(0, at - context)..minOf(lines.size - 1, at + context)) keep[k] = true
}
val out = ArrayList<DiffLine>()
var left = false
lines.forEachIndexed { at, line ->
if (keep[at]) {
if (left && out.isNotEmpty()) out += DiffLine(DiffSign.Gap, "")
left = false
out += line
} else {
left = true
}
}
return out
}
/** This file's change for the card: a file that is made is all added, one that goes is all removed. */
fun FileDiff.view(): DiffView {
val all = diffLines(textLines(oldText.orEmpty()), textLines(if (deleted) "" else newText))
return DiffView(hunks(all), added = all.count { it.sign == DiffSign.Added }, removed = all.count { it.sign == DiffSign.Removed })
}
@@ -12,6 +12,11 @@ import kotlinx.serialization.Serializable
* `model3d`), and the file is in the person's Files at [path] -- a moment's truth, since they may
* move it; [nodeId] names it wherever it is. Its bytes are `GET /files/raw?node=`. [width],
* [height] and [seconds] are 0 where the server did not know them.
*
* With [shown] it is a file an agent put in front of the person (`contract/tool_media_shown.json`)
* and [kind] is what the file is, read from its bytes: `image`, `audio`, `video`, `pdf`,
* `model3d`, `archive`, `binary`, `text`, or a word this app does not know yet. Whatever it is,
* its card names it and leads to the file; what the phone can draw is drawn as well.
*/
@Serializable
data class GeneratedMedia(
@@ -25,16 +30,22 @@ data class GeneratedMedia(
val width: Int = 0,
val height: Int = 0,
val seconds: Double = 0.0,
/** An agent showed this file (the `show` tool on a file that is not text); it did not make it. */
val shown: Boolean = false,
) {
/** How the card shows it, by what was asked for: an unknown kind is its details, never a player. */
/** How the card shows it, by its kind: an unknown kind is its details, never a player. */
val view: MediaView
get() = when (kind) {
"image", "image_edit" -> MediaView.Picture
"video" -> MediaView.Video
"music" -> MediaView.Sound
"music", "audio" -> MediaView.Sound
"pdf" -> MediaView.Document
else -> MediaView.Details
}
/** The kinds this app has a word for; anything else is said as "File" when shown, never as nothing. */
val knownKind: Boolean get() = kind in KINDS
/** What to call it: its name, else the last name of its path. */
val title: String get() = name.ifBlank { FileKinds.name(path) }
@@ -43,10 +54,18 @@ data class GeneratedMedia(
/** The folder of the person's Files it was written to. */
val folder: String get() = FileKinds.parent(path)
companion object {
val KINDS = setOf("image", "image_edit", "video", "music", "audio", "model3d", "pdf", "archive", "binary", "text")
}
}
/** What a generated file's card is: a picture drawn, a player, or (a 3D model, anything unknown) the file's details. */
enum class MediaView { Picture, Video, Sound, Details }
/**
* What a file's card is: a picture drawn, a player, a document with pages the phone can lay out
* (a PDF), or -- a 3D model, an archive, anything unknown -- the file's details. Every one of
* them names the file and has the ways to it.
*/
enum class MediaView { Picture, Video, Sound, Document, Details }
/** The job a tool call started (`media_job`): [state] as it was when the call returned. */
@Serializable
@@ -385,6 +385,12 @@ data class LinkedDevice(
@SerialName("max_mode") val maxMode: String = "",
/** Whether the device remembers the model last used in a directory (spec 3.9.0). */
@SerialName("directory_model") val directoryModel: Boolean = false,
/** Whether the device hands its files over the link at all (spec 3.11.0, `fs`). Parsed; no screen reads it yet. */
val files: Boolean = false,
/** False where its highest mode is plan: its files can be read and not changed. */
@SerialName("files_writable") val filesWritable: Boolean = false,
/** Whether the device lets files that run code (git's own, an agent's configuration) be changed from here. */
@SerialName("files_that_run_code") val filesThatRunCode: Boolean = false,
) {
/** What to call it: its name, else its host. */
val label: String get() = name.ifBlank { host.ifBlank { id } }
@@ -765,8 +771,40 @@ data class CardItem(
* action's own fields, as the server checked them. Carried out by `actions/ActionRunner`.
*/
val phone: JsonObject? = null,
/** An edit's changes, one per file (`contract/event_ask_edit.json`); empty on a card without them. */
val diffs: List<FileDiff> = emptyList(),
)
/**
* What an edit would do to one file, on its approval card: [oldText] is null for a file that is
* made, [deleted] marks one that goes. Both texts are a model's and a file's words: drawn as
* text by the app's own diff view (`api/Diff.kt`), never rendered.
*/
@Serializable
data class FileDiff(
val path: String = "",
@SerialName("old_text") val oldText: String? = null,
@SerialName("new_text") val newText: String = "",
val deleted: Boolean = false,
) {
/** A file that does not exist yet. */
val made: Boolean get() = oldText == null && !deleted
}
/**
* A person's two models for an editor (`GET /editor`, `contract/editor.json`): parsed so the
* shape is pinned; this app has no screen for them.
*/
@Serializable
data class EditorModels(val autocomplete: Autocomplete = Autocomplete(), val main: Main = Main()) {
/** [source]: `person`, `instance` or null with no model. [allowed]: the refs an administrator allows, null for any. */
@Serializable
data class Autocomplete(val model: String? = null, val source: String? = null, val allowed: List<String>? = null)
@Serializable
data class Main(val model: String? = null)
}
@Serializable
data class CardSubagent(val id: String = "", val name: String = "", val position: String = "")