From af290cd7d539da86d140e7412c1bd4448cce76d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Bene=C5=A1?= Date: Fri, 13 Nov 2026 11:38:14 +0000 Subject: [PATCH] Review: a report's conversation, ids from outside, the form's notify, the drawer, the channels Contract at server dev 40813d1 (84 files): a chat's `controls` and `report`, a text step's `pages` and `text`, a report's `schedule_title`. - B1 (must): a `user` row the server wrote (`machine`) is never the person's bubble -- a quiet note; in a report's conversation the first such line is left out. A report chat's bar takes its title from the chat's own `report` (so it survives the app being closed), and its menu offers only what `controls` allows. - A1: ids from a push payload or an intent extra pass `safeId` (^[A-Za-z0-9_-]{1,64}$) before they become a route: chat, schedule, run, report. - C1-C3: an empty "When..." is sent empty (the server's default) and the field shows the default as a placeholder; editing sends `notify` only once the schedule was read or the person chose, and a failed read says so and disables Save. - B2: Reports is in the drawer only where the instance has the route. - A text step with pages is drawn from the server's `text`; a report from its `text`. A fence with a space before `html-view` is a code block, as the spec has it. - A push of a kind the app does not know shows nothing. An important report uses a channel of high importance. A report read or deleted in the app cancels its notification. - stop() and compact() post an empty body again: a search-and-replace in adc1c4f had changed them to "{}" by mistake. One commit: the model, the screens and the push share these files. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 21 ++++++-- .../main/java/eu/llembas/android/LembasApp.kt | 2 + .../java/eu/llembas/android/MainActivity.kt | 11 ++-- .../java/eu/llembas/android/api/ApiClient.kt | 8 +-- .../java/eu/llembas/android/api/Display.kt | 29 ++++++++++ .../java/eu/llembas/android/api/Models.kt | 22 ++++++++ .../java/eu/llembas/android/api/Reports.kt | 16 +++++- .../main/java/eu/llembas/android/push/Push.kt | 20 +++++-- .../eu/llembas/android/push/ReportNotice.kt | 9 +++- .../llembas/android/push/ScheduledNotice.kt | 6 ++- .../eu/llembas/android/ui/chat/ChatScreen.kt | 54 +++++++++++++------ .../eu/llembas/android/ui/chat/Transcript.kt | 24 ++++++++- .../java/eu/llembas/android/ui/home/Drawer.kt | 26 +++++++-- .../android/ui/reports/ReportsScreen.kt | 10 +++- .../android/ui/scheduled/ScheduledScreens.kt | 43 +++++++++++---- app/src/main/res/values-cs/strings.xml | 1 + app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values-es/strings.xml | 1 + app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values-hu/strings.xml | 1 + app/src/main/res/values-it/strings.xml | 1 + app/src/main/res/values-pl/strings.xml | 1 + app/src/main/res/values-sk/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + .../java/eu/llembas/android/DisplayTest.kt | 18 +++++++ .../llembas/android/ReportNotificationTest.kt | 22 ++++++++ .../eu/llembas/android/ReportsContractTest.kt | 37 +++++++++++++ .../java/eu/llembas/android/ReportsTest.kt | 50 +++++++++++++++++ .../eu/llembas/android/shots/ScreenShots.kt | 2 +- contract/chat.json | 10 +++- contract/chat_agent.json | 10 +++- contract/chat_html_pages.json | 4 ++ contract/chat_report.json | 32 +++++++++++ contract/chats_listed.json | 10 +++- contract/event_reports.json | 4 +- contract/lock.json | 6 +-- contract/models.json | 9 ++-- contract/models_agent.json | 9 ++-- contract/page.json | 6 +++ contract/report.json | 1 + contract/report_orphan.json | 22 ++++++++ contract/reports.json | 1 + contract/schedule_models.json | 2 + contract/step_text_page.json | 13 +++++ scripts/strings.py | 1 + scripts/strings_own.json | 8 +++ 46 files changed, 519 insertions(+), 68 deletions(-) create mode 100644 contract/chat_html_pages.json create mode 100644 contract/chat_report.json create mode 100644 contract/page.json create mode 100644 contract/report_orphan.json create mode 100644 contract/step_text_page.json diff --git a/CHANGELOG.md b/CHANGELOG.md index d2dbd41..704bdbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,10 +26,11 @@ For LLeMbas server 2.0.0-beta.12 (harness spec 3.8.0); an older instance works a - **Last wake** in Settings › Wake word: when the phrase was last heard, what the app tried (the assistant's sheet, the sheet as a screen of the app, a notification) and exactly how it - went -- opened; Android had not connected LLeMbas as the assistant; Android refused, in its - own words; or it did not refuse and still no sheet came -- with whether LLeMbas was the - digital assistant app then and when Android had connected or dropped it. A tap shows the last - five. Nothing from your account is in it. + went -- opened, and after how long; Android had not connected LLeMbas as the assistant; + Android refused, in its own words; or it did not refuse and still no sheet came -- with what + Android itself said at that moment: whether LLeMbas was the digital assistant app, whether + Android was really using it as its voice assistant, and when it had connected or dropped it. + A tap shows the last five. Nothing from your account is in it. ### Fixed @@ -41,6 +42,14 @@ For LLeMbas server 2.0.0-beta.12 (harness spec 3.8.0); an older instance works a really came up before it counts the wake as opened; when it did not, the notification follows and says *Could not open by itself — tap to ask*. What makes a phone drop that connection is not fixed by this: the record is there to find out. +- Settings says so when LLeMbas is chosen as the digital assistant app but Android is not using + it as its voice assistant (which Android can undo at a restart): the long press then opens + it as an ordinary screen, and the wake word can only send a notification until LLeMbas is + chosen again. +- The wake word stayed paused "while the assistant is open" after the sheet was left with the + Home button, or after the assistant was asked for twice in a row. +- A report's conversation showed the line the server opens it with as if you had written it; + such lines are a quiet note now, and that first one is left out. - The screen is turned on when the wake word opens the sheet (with *Only while the screen is on* switched off, the phrase can come while it is dark). @@ -51,6 +60,10 @@ For LLeMbas server 2.0.0-beta.12 (harness spec 3.8.0); an older instance works a the report; *Mark read* asks for the phone to be unlocked. A run that filed no report notifies nothing; a failed run still says that it failed. An instance on 2.0.0-beta.11 keeps sending its runs' notifications, and they work as before. +- A report the model called important arrives on a channel of its own, *Important reports*, + which may come up over what is on screen. A report read or deleted in the app takes its + notification away. *Reports* is in the drawer only where the instance has reports. An empty + *When…* means the instance's own default, which the field shows as a placeholder. - **A page in an answer is not drawn on the phone** (LLeMbas 2.0.0-beta.12 can let a model add HTML pages to an answer; the web and LLeMbas Desktop show them). The phone shows one quiet card -- *This answer has a page: …*, and where to open it -- in a chat and in a report, never diff --git a/app/src/main/java/eu/llembas/android/LembasApp.kt b/app/src/main/java/eu/llembas/android/LembasApp.kt index 40878c1..8d6b648 100644 --- a/app/src/main/java/eu/llembas/android/LembasApp.kt +++ b/app/src/main/java/eu/llembas/android/LembasApp.kt @@ -43,6 +43,7 @@ class LembasApp : Application() { NotificationChannel(CHANNEL_SCHEDULED, getString(R.string.channel_scheduled), NotificationManager.IMPORTANCE_DEFAULT), NotificationChannel(CHANNEL_SCHEDULED_FAILURES, getString(R.string.channel_scheduled_failures), NotificationManager.IMPORTANCE_HIGH), NotificationChannel(CHANNEL_REPORTS, getString(R.string.channel_reports), NotificationManager.IMPORTANCE_DEFAULT), + NotificationChannel(CHANNEL_REPORTS_HIGH, getString(R.string.channel_reports_high), NotificationManager.IMPORTANCE_HIGH), ).onEach { it.lockscreenVisibility = android.app.Notification.VISIBILITY_PRIVATE } + // A new version of the app says nothing about the account. NotificationChannel(CHANNEL_UPDATES, getString(R.string.channel_updates), NotificationManager.IMPORTANCE_LOW), @@ -60,6 +61,7 @@ class LembasApp : Application() { const val CHANNEL_SCHEDULED = "scheduled_results" const val CHANNEL_SCHEDULED_FAILURES = "scheduled_failures" const val CHANNEL_REPORTS = "reports_filed" + const val CHANNEL_REPORTS_HIGH = "reports_important" const val CHANNEL_UPDATES = "updates" } } diff --git a/app/src/main/java/eu/llembas/android/MainActivity.kt b/app/src/main/java/eu/llembas/android/MainActivity.kt index 5843053..bae73c2 100644 --- a/app/src/main/java/eu/llembas/android/MainActivity.kt +++ b/app/src/main/java/eu/llembas/android/MainActivity.kt @@ -35,6 +35,7 @@ import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.rememberNavController import eu.llembas.android.api.Chat import eu.llembas.android.api.Folder +import eu.llembas.android.api.safeId import eu.llembas.android.auth.Instance import eu.llembas.android.auth.Prefs import eu.llembas.android.push.Notifications @@ -138,11 +139,13 @@ class MainActivity : ComponentActivity() { if (intent?.getBooleanExtra(WakeBootReceiver.EXTRA_RESUME, false) == true) { lifecycleScope.launch { if (lembas.instances.prefs.first().wakeWord) WakeWordService.start(this@MainActivity) } } - val chat = intent?.getStringExtra(Notifications.EXTRA_CHAT) - val schedule = intent?.getStringExtra(Notifications.EXTRA_SCHEDULE) - val run = intent?.getStringExtra(Notifications.EXTRA_RUN) + // Extras are whatever the sender put there (this activity can be started from outside): + // only ids that look like ids go on to become routes. + val chat = safeId(intent?.getStringExtra(Notifications.EXTRA_CHAT)) + val schedule = safeId(intent?.getStringExtra(Notifications.EXTRA_SCHEDULE)) + val run = safeId(intent?.getStringExtra(Notifications.EXTRA_RUN)) val instance = intent?.getStringExtra(Notifications.EXTRA_INSTANCE) - val report = intent?.getStringExtra(Notifications.EXTRA_REPORT) + val report = safeId(intent?.getStringExtra(Notifications.EXTRA_REPORT)) if (chat == null && schedule == null && report == null) return opening.value = Opening(chat, schedule, run, instance, report) // A notification from another of the person's instances: that one comes into use, and diff --git a/app/src/main/java/eu/llembas/android/api/ApiClient.kt b/app/src/main/java/eu/llembas/android/api/ApiClient.kt index bdf1293..456d10a 100644 --- a/app/src/main/java/eu/llembas/android/api/ApiClient.kt +++ b/app/src/main/java/eu/llembas/android/api/ApiClient.kt @@ -291,7 +291,8 @@ class ApiClient(val base: String, private val token: String, val http: OkHttpCli 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("{}".toRequestBody(JSON)).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) }) @@ -333,7 +334,7 @@ class ApiClient(val base: String, private val token: String, val http: OkHttpCli * server refuses in its own words (409) while a reply is being written or with nothing to * summarise; the transcript is read again after, for the divider it leaves. */ - suspend fun compact(chatId: String) = empty(authed("$v2/chats/$chatId/compact").post("{}".toRequestBody(JSON)).build()) + suspend fun compact(chatId: String) = empty(authed("$v2/chats/$chatId/compact").post(ByteArray(0).toRequestBody(null)).build()) suspend fun team(chatId: String, since: Int = 0): TeamView = get("/chats/$chatId/team?since=$since").team @@ -380,7 +381,8 @@ class ApiClient(val base: String, private val token: String, val http: OkHttpCli // Notify when (spec 3.8.0): left out where the form did not say, so an older server is sent nothing new. if (notify != null) { put("notify", notify) - if (notify == "when") put("notify_when", notifyWhen.trim().ifBlank { NOTIFY_DEFAULT_CONDITION }) + // The person's own words, or none: an empty condition is the server's default, in its own words. + if (notify == "when") put("notify_when", notifyWhen.trim()) } } return if (id == null) { diff --git a/app/src/main/java/eu/llembas/android/api/Display.kt b/app/src/main/java/eu/llembas/android/api/Display.kt index 651c658..994d22c 100644 --- a/app/src/main/java/eu/llembas/android/api/Display.kt +++ b/app/src/main/java/eu/llembas/android/api/Display.kt @@ -115,3 +115,32 @@ fun localeNumber(n: Double, decimals: Int, locale: Locale): String { format.roundingMode = java.math.RoundingMode.HALF_UP return format.format(n) } + +/** How a message is drawn in the transcript. */ +enum class Drawn { + /** The person's own words: their bubble. */ + Person, + + /** The model's reply. */ + Reply, + + /** A line the server wrote in the person's place (`machine`): a quiet note, never their bubble. */ + Note, + + /** Not drawn at all. */ + Hidden, +} + +/** + * How [message] is drawn. A `user` row the server wrote itself (`machine`: a schedule's + * instruction, the line that opens a report's conversation) is not something the person said, + * so it is never their bubble: a quiet note -- and in a report's conversation ([reportChat]) the + * [first] such line, which only tells the model what the conversation continues, is left out, + * since the report itself is one tap back. + */ +fun drawn(message: Message, first: Boolean, reportChat: Boolean): Drawn = when { + !message.isUser -> Drawn.Reply + !message.machine -> Drawn.Person + reportChat && first -> Drawn.Hidden + else -> Drawn.Note +} diff --git a/app/src/main/java/eu/llembas/android/api/Models.kt b/app/src/main/java/eu/llembas/android/api/Models.kt index 0ea00aa..c578743 100644 --- a/app/src/main/java/eu/llembas/android/api/Models.kt +++ b/app/src/main/java/eu/llembas/android/api/Models.kt @@ -314,6 +314,10 @@ data class Chat( @SerialName("created_at") val createdAt: String? = null, @SerialName("updated_at") val updatedAt: String? = null, @SerialName("system_prompt") val systemPrompt: String? = null, + /** What may be done to this chat (`rename`, `delete`, `archive`, `pin`, `folder`); null from a server that does not say: everything. */ + val controls: List? = null, + /** The report this chat continues (`kind` `report`): such a chat lives under Reports, not among the chats. */ + val report: ChatReport? = null, val plan: JsonObject? = null, /** The team's size: how many agents, its own included, the chat may have. */ val agents: AgentsSetting? = null, @@ -321,8 +325,14 @@ data class Chat( val subagents: SubagentsOffer? = null, ) { val isAgent: Boolean get() = kind == "agent" + + fun allows(control: String): Boolean = controls?.contains(control) ?: true } +/** The report a chat continues: its id and title. */ +@Serializable +data class ChatReport(val id: String = "", val title: String = "") + /** A chat's `agents {min, max}` (harness spec 3.2.0): `1 ≤ min ≤ max ≤` the instance's cap. */ @Serializable data class AgentsSetting(val min: Int = 1, val max: Int = 4) { @@ -523,6 +533,8 @@ data class Step( val markdown: String? = null, /** A `page` step's title (spec 3.8.0): the page itself is not sent to a client that does not draw it. */ val title: String? = null, + /** A `text` step's pages (spec 3.8.0): where it has any, [text] is its markdown with each page as the one line `[page: Title]`. */ + val pages: List = emptyList(), val calls: List = emptyList(), val messages: List = emptyList(), ) @@ -535,6 +547,16 @@ data class Step( @Serializable data class MessageEnd(val phase: String = "", val reason: String = "", val text: String = "") +/** A page in a reply's text: its title (the page itself is fetched by its digest, by a client that draws pages). */ +@Serializable +data class StepPage(val title: String = "", val digest: String = "") + +/** + * What a client that draws no pages shows of a `text` step: the server's own text where the step + * has pages (each page its one line), else the markdown as it is. + */ +val Step.shownMarkdown: String get() = text?.takeIf { pages.isNotEmpty() && it.isNotBlank() } ?: markdown.orEmpty() + @Serializable data class Message( val id: String, diff --git a/app/src/main/java/eu/llembas/android/api/Reports.kt b/app/src/main/java/eu/llembas/android/api/Reports.kt index 6c37612..ca6a814 100644 --- a/app/src/main/java/eu/llembas/android/api/Reports.kt +++ b/app/src/main/java/eu/llembas/android/api/Reports.kt @@ -1,6 +1,7 @@ package eu.llembas.android.api import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonNames @@ -35,6 +36,8 @@ data class Report( val summary: String = "", val importance: String = "normal", val schedule: ReportSchedule? = null, + /** The schedule's name as it was when the report was filed: what is left once the schedule itself is gone. */ + @SerialName("schedule_title") val scheduleTitle: String = "", @JsonNames("created_at") val created: String? = null, val unread: Boolean = false, val body: String? = null, @@ -45,6 +48,9 @@ data class Report( ) { val high: Boolean get() = importance == "high" + /** The name of the task it came from: the schedule's as it is now, else as it was. */ + val from: String get() = schedule?.title?.ifBlank { null } ?: scheduleTitle + /** The chat a reply to this report continues in; null before the first reply. */ val chatId: String? get() = idOf(chat) } @@ -85,5 +91,11 @@ fun notifyChoice(notify: String): String = when (notify) { else -> "when" } -/** `scheduled.notify.default_condition`: what "When…" starts from, in the spec's words. */ -const val NOTIFY_DEFAULT_CONDITION = "something needs my attention" +/** + * An id as the server makes them -- letters, digits, `_` and `-`, at most 64 -- or null. Anything + * that came from outside the app (a push payload, an intent's extra) is passed through this + * before it becomes part of a route or an address: a `/` in it must not open another page. + */ +fun safeId(id: String?): String? = id?.takeIf { SAFE_ID.matches(it) } + +private val SAFE_ID = Regex("^[A-Za-z0-9_-]{1,64}$") diff --git a/app/src/main/java/eu/llembas/android/push/Push.kt b/app/src/main/java/eu/llembas/android/push/Push.kt index 0b12fd6..4397528 100644 --- a/app/src/main/java/eu/llembas/android/push/Push.kt +++ b/app/src/main/java/eu/llembas/android/push/Push.kt @@ -17,6 +17,7 @@ import eu.llembas.android.R import eu.llembas.android.api.CardAnswer import eu.llembas.android.api.LembasJson import eu.llembas.android.api.PushPayload +import eu.llembas.android.api.safeId import eu.llembas.android.auth.Instance import eu.llembas.android.lembas import kotlinx.coroutines.CoroutineScope @@ -102,11 +103,16 @@ object Notifications { const val EXTRA_RUN = "eu.llembas.android.RUN" const val EXTRA_REPORT = "eu.llembas.android.REPORT" + /** Whether [kind] is about a chat: a finished reply (which older payloads leave unnamed) or a question or an approval. */ + fun chatKind(kind: String): Boolean = kind == "" || kind == "reply" || kind == "ask" + fun show(context: Context, instance: String, payload: PushPayload) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) return if (payload.kind == "schedule" || payload.kind == "schedule_failed") return scheduled(context, instance, payload) if (payload.kind == ReportNotice.KIND) return report(context, instance, payload) - val chatId = payload.chatId ?: payload.url.removePrefix("/chat/").takeIf { payload.url.startsWith("/chat/") } + // A kind this app does not know -- a newer server's -- is not guessed at: no notification. + if (!chatKind(payload.kind)) return + val chatId = safeId(payload.chatId ?: payload.url.removePrefix("/chat/").takeIf { payload.url.startsWith("/chat/") }) val id = (chatId ?: payload.url).hashCode() val open = opener(context, instance, id, chatId = chatId) val channel = when (payload.kind) { @@ -195,7 +201,10 @@ object Notifications { fun reportNotification(context: Context, instance: String, notice: ReportNotice): Notification { val fixed = context.getString(R.string.report_new_public) val open = opener(context, instance, notice.id, chatId = null, reportId = notice.reportId) - val public = NotificationCompat.Builder(context, LembasApp.CHANNEL_REPORTS) + // A report the model called important goes on a channel of its own that may come up over + // what is on screen: on Android 8 and later the channel decides that, not the notification. + val channel = if (notice.high) LembasApp.CHANNEL_REPORTS_HIGH else LembasApp.CHANNEL_REPORTS + val public = NotificationCompat.Builder(context, channel) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(context.getString(R.string.app_name)) .setContentText(fixed) @@ -209,7 +218,7 @@ object Notifications { .putExtra("notification", notice.id), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT, ) - return NotificationCompat.Builder(context, LembasApp.CHANNEL_REPORTS) + return NotificationCompat.Builder(context, channel) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(notice.title.ifBlank { fixed }) .setContentText(notice.summary) @@ -225,6 +234,11 @@ object Notifications { .build() } + /** The report was read or deleted in the app: its notification has nothing left to say. */ + fun reportSeen(context: Context, reportId: String) { + NotificationManagerCompat.from(context).cancel(ReportNotice.notificationId(reportId)) + } + /** A notification action Android runs only once the phone is unlocked. */ private fun unlocked(title: String, intent: PendingIntent): NotificationCompat.Action = NotificationCompat.Action.Builder(0, title, intent).setAuthenticationRequired(true).build() diff --git a/app/src/main/java/eu/llembas/android/push/ReportNotice.kt b/app/src/main/java/eu/llembas/android/push/ReportNotice.kt index cd5d5f4..5ba1cbf 100644 --- a/app/src/main/java/eu/llembas/android/push/ReportNotice.kt +++ b/app/src/main/java/eu/llembas/android/push/ReportNotice.kt @@ -1,6 +1,7 @@ package eu.llembas.android.push import eu.llembas.android.api.PushPayload +import eu.llembas.android.api.safeId /** * What a filed report's push asks the app to show (harness spec 3.8.0, `reports.md` §4: @@ -11,14 +12,18 @@ import eu.llembas.android.api.PushPayload */ data class ReportNotice(val reportId: String, val title: String, val summary: String, val high: Boolean) { /** One notification per report: a later push about the same report replaces it. */ - val id: Int get() = "report:$reportId".hashCode() + val id: Int get() = notificationId(reportId) companion object { const val KIND = "report" + /** The notification of the report with this id: what is cancelled once the report is read or deleted in the app. */ + fun notificationId(reportId: String): Int = "report:$reportId".hashCode() + fun from(payload: PushPayload): ReportNotice? { if (payload.kind != KIND) return null - val id = payload.reportId?.takeIf { it.isNotBlank() } ?: fromUrl(payload.url) ?: return null + // Only an id that looks like one: it becomes part of a route and of an address. + val id = safeId(payload.reportId?.takeIf { it.isNotBlank() } ?: fromUrl(payload.url)) ?: return null // The report's own words are in the payload's `notification` (the payload's top is // what an older client would show: the app's name and "New report"). A payload // without one is read by its top. diff --git a/app/src/main/java/eu/llembas/android/push/ScheduledNotice.kt b/app/src/main/java/eu/llembas/android/push/ScheduledNotice.kt index 58a4391..e0c6aca 100644 --- a/app/src/main/java/eu/llembas/android/push/ScheduledNotice.kt +++ b/app/src/main/java/eu/llembas/android/push/ScheduledNotice.kt @@ -1,6 +1,7 @@ package eu.llembas.android.push import eu.llembas.android.api.PushPayload +import eu.llembas.android.api.safeId /** * What a scheduled run's push asks the app to show, read from the server's payload alone @@ -40,8 +41,9 @@ data class ScheduledNotice( fun from(payload: PushPayload): ScheduledNotice? { if (payload.kind != KIND_RESULT && payload.kind != KIND_FAILED) return null val (urlSchedule, urlRun) = fromUrl(payload.url) - val schedule = payload.scheduleId?.takeIf { it.isNotBlank() } ?: urlSchedule ?: return null - val run = payload.runId?.takeIf { it.isNotBlank() } ?: urlRun + // Only ids that look like ids: they become parts of a route and of an address. + val schedule = safeId(payload.scheduleId?.takeIf { it.isNotBlank() } ?: urlSchedule) ?: return null + val run = safeId(payload.runId?.takeIf { it.isNotBlank() } ?: urlRun) val failed = payload.kind == KIND_FAILED val words = payload.notification val ask = words?.ask?.takeIf { it.isNotBlank() } ?: payload.ask diff --git a/app/src/main/java/eu/llembas/android/ui/chat/ChatScreen.kt b/app/src/main/java/eu/llembas/android/ui/chat/ChatScreen.kt index 7d67d3f..422bc28 100644 --- a/app/src/main/java/eu/llembas/android/ui/chat/ChatScreen.kt +++ b/app/src/main/java/eu/llembas/android/ui/chat/ChatScreen.kt @@ -91,8 +91,10 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.repeatOnLifecycle import androidx.lifecycle.viewmodel.compose.viewModel import eu.llembas.android.R +import eu.llembas.android.api.Drawn import eu.llembas.android.api.Message import eu.llembas.android.api.ShowCard +import eu.llembas.android.api.drawn import eu.llembas.android.ui.ModelAvatar import eu.llembas.android.ui.ProgressStatus import eu.llembas.android.ui.theme.LocalPalette @@ -244,7 +246,13 @@ fun ChatContent( ModelTitle( model = info, fallback = state.modelId ?: stringResource(R.string.app_name), - subtitle = if (report != null) reportTitle(report) else chat?.title?.ifBlank { null }, + // A report's conversation says which report: by what it was entered with, else by + // what the chat itself says (after the app was closed in between). + subtitle = if (report != null || chat?.report != null) { + reportTitle(report?.ifBlank { null } ?: chat?.report?.title.orEmpty()) + } else { + chat?.title?.ifBlank { null } + }, onClick = { picking = true }, ) }, @@ -264,10 +272,13 @@ fun ChatContent( menu = false actions.continueChat() } - MenuItem(Icons.Filled.Archive, stringResource(R.string.archive)) { - menu = false - actions.change("archived", "true") - onLeave() + // Only what may be done to this chat: a report's conversation is not archived. + if (chat.allows("archive")) { + MenuItem(Icons.Filled.Archive, stringResource(R.string.archive)) { + menu = false + actions.change("archived", "true") + onLeave() + } } MenuItem(Icons.Filled.Delete, stringResource(R.string.delete)) { menu = false @@ -348,20 +359,29 @@ fun ChatContent( item { TextButton(onClick = actions::older) { Text(stringResource(R.string.older)) } } } items(rows, key = { it.id }) { message -> - when { - message.isUser -> UserMessage(message, canEdit = !message.machine && live == null, onEdit = { editing = message }) + // The first row is the chat's first message only once nothing older is left to load. + val first = !state.more && message.id == rows.firstOrNull()?.id + when (drawn(message, first, reportChat = report != null || chat?.report != null)) { + // The server's own line in the person's place: nothing, or a note. + Drawn.Hidden -> Unit - live != null && live.messageId == message.id && live.finished == null -> - AssistantMessage(message, live.shown, writing = true, canRegenerate = false, onRegenerate = {}, onShow = onShow) + Drawn.Note -> MachineNote(message.text.orEmpty()) - else -> AssistantMessage( - message, - message.steps, - writing = false, - canRegenerate = live == null && message == rows.lastOrNull(), - onRegenerate = { actions.regenerate(message) }, - onShow = onShow, - ) + Drawn.Person -> UserMessage(message, canEdit = live == null, onEdit = { editing = message }) + + Drawn.Reply -> when { + live != null && live.messageId == message.id && live.finished == null -> + AssistantMessage(message, live.shown, writing = true, canRegenerate = false, onRegenerate = {}, onShow = onShow) + + else -> AssistantMessage( + message, + message.steps, + writing = false, + canRegenerate = live == null && message == rows.lastOrNull(), + onRegenerate = { actions.regenerate(message) }, + onShow = onShow, + ) + } } } } diff --git a/app/src/main/java/eu/llembas/android/ui/chat/Transcript.kt b/app/src/main/java/eu/llembas/android/ui/chat/Transcript.kt index aabaa14..2169bfa 100644 --- a/app/src/main/java/eu/llembas/android/ui/chat/Transcript.kt +++ b/app/src/main/java/eu/llembas/android/ui/chat/Transcript.kt @@ -87,6 +87,7 @@ import eu.llembas.android.api.ShowCard import eu.llembas.android.api.Step import eu.llembas.android.api.ToolCall import eu.llembas.android.api.ending +import eu.llembas.android.api.shownMarkdown import eu.llembas.android.lembas import eu.llembas.android.ui.MarkdownText import eu.llembas.android.ui.Segment @@ -170,6 +171,25 @@ fun UserMessage(message: Message, canEdit: Boolean, onEdit: () -> Unit, modifier } } +/** + * A line the server wrote in the person's place (a schedule's instruction, what opens a report's + * conversation): a quiet note across the width, a few lines of it -- never the person's bubble, + * and never markdown. + */ +@Composable +fun MachineNote(text: String, modifier: Modifier = Modifier) { + val palette = LocalPalette.current + if (text.isBlank()) return + Text( + text.trim(), + style = MaterialTheme.typography.bodySmall, + color = palette.inkFaint, + maxLines = 4, + overflow = TextOverflow.Ellipsis, + modifier = modifier.fillMaxWidth().padding(horizontal = 8.dp), + ) +} + /** * The model's turn, full width and without a bubble: its steps (thinking and tools folded into * chips that open), its words as markdown, and under it Copy, Regenerate and what it cost. While @@ -301,7 +321,7 @@ fun AssistantMessage( fun replyText(message: Message): String = pageProjection( message.steps.mapNotNull { step -> when (step.kind) { - "text" -> step.markdown + "text" -> step.shownMarkdown "page" -> "[page: ${step.title.orEmpty()}]" else -> null } @@ -368,7 +388,7 @@ fun StepsCompact(steps: List, modifier: Modifier = Modifier, onShow: ((Sho when (step.kind) { "thinking" -> ThinkingChip(step) - "text" -> RichMarkdown(step.markdown.orEmpty(), onLongPress = onLongPress) + "text" -> RichMarkdown(step.shownMarkdown, onLongPress = onLongPress) // A page the server took out of the text as a step of its own: the same card. "page" -> PageCard(step.title.orEmpty()) diff --git a/app/src/main/java/eu/llembas/android/ui/home/Drawer.kt b/app/src/main/java/eu/llembas/android/ui/home/Drawer.kt index 948b31c..823c297 100644 --- a/app/src/main/java/eu/llembas/android/ui/home/Drawer.kt +++ b/app/src/main/java/eu/llembas/android/ui/home/Drawer.kt @@ -73,6 +73,7 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope import eu.llembas.android.R +import eu.llembas.android.api.ApiException import eu.llembas.android.api.Chat import eu.llembas.android.api.Doorstep import eu.llembas.android.api.Folder @@ -106,6 +107,8 @@ data class ChatsState( val foundFor: String = "", /** How many reports are unread: the badge on Reports. */ val unreadReports: Int = 0, + /** Whether the instance has Reports at all: one before 2.0.0-beta.12 has no such page to open. */ + val reports: Boolean = false, ) /** The drawer's list: every chat of the instance in use, both kinds, newest first. */ @@ -139,8 +142,23 @@ class ChatsViewModel(app: Application) : AndroidViewModel(app) { // Folders are a side dish: the chats show without them if they cannot be read. val folders = runCatching { api.folders() }.getOrDefault(_state.value.folders) // So is the count of unread reports: an instance before Reports simply has none. - val unread = runCatching { api.unreadReports() }.getOrDefault(0) - _state.value = ChatsState(chats = page.chats, more = page.more, folders = folders, unreadReports = unread) + val unread = runCatching { api.unreadReports() } + // No such route (404, 405): no Reports. Any other trouble leaves what was known. + val refused = (unread.exceptionOrNull() as? ApiException)?.status + val has = if (unread.isSuccess) { + true + } else if (refused == 404 || refused == 405) { + false + } else { + _state.value.reports + } + _state.value = ChatsState( + chats = page.chats, + more = page.more, + folders = folders, + unreadReports = unread.getOrDefault(_state.value.unreadReports), + reports = has, + ) } catch (e: Exception) { _state.update { it.copy(loading = false, error = getApplication().explain(e)) } } @@ -337,7 +355,9 @@ fun DrawerContent( item { DrawerItem(Icons.Filled.EditNote, stringResource(R.string.new_chat), onClick = actions::newChat) } item { DrawerItem(Icons.Filled.Terminal, stringResource(R.string.new_agent_chat), onClick = actions::newAgentChat) } item { DrawerItem(Icons.Filled.Folder, stringResource(R.string.files), onClick = actions::files) } - item { DrawerItem(Icons.Filled.Summarize, stringResource(R.string.reports), badge = state.unreadReports, onClick = actions::reports) } + if (state.reports) { + item { DrawerItem(Icons.Filled.Summarize, stringResource(R.string.reports), badge = state.unreadReports, onClick = actions::reports) } + } item { DrawerItem(Icons.Filled.Schedule, stringResource(R.string.scheduled), onClick = actions::scheduled) } item { DrawerItem(Icons.Filled.Inventory2, stringResource(R.string.archived), onClick = actions::archived) } item { DrawerItem(Icons.Filled.Psychology, stringResource(R.string.memory), onClick = actions::memory) } diff --git a/app/src/main/java/eu/llembas/android/ui/reports/ReportsScreen.kt b/app/src/main/java/eu/llembas/android/ui/reports/ReportsScreen.kt index 09c4a2d..4c8192e 100644 --- a/app/src/main/java/eu/llembas/android/ui/reports/ReportsScreen.kt +++ b/app/src/main/java/eu/llembas/android/ui/reports/ReportsScreen.kt @@ -68,6 +68,7 @@ import eu.llembas.android.api.ApiException import eu.llembas.android.api.Report import eu.llembas.android.api.sortedReports import eu.llembas.android.lembas +import eu.llembas.android.push.Notifications import eu.llembas.android.ui.chat.RichMarkdown import eu.llembas.android.ui.explain import eu.llembas.android.ui.pageProjection @@ -115,11 +116,13 @@ class ReportsViewModel(app: Application) : AndroidViewModel(app) { /** Read: it stays in the list, among those read. A failure is put right by reading the list again. */ fun markRead(report: Report) { _state.update { s -> s.copy(reports = sortedReports(s.reports.map { if (it.id == report.id) it.copy(unread = false) else it })) } + Notifications.reportSeen(getApplication(), report.id) viewModelScope.launch { runCatching { getApplication().api().readReport(report.id) }.onFailure { fail(it) } } } fun delete(report: Report) { _state.update { s -> s.copy(reports = s.reports.filterNot { it.id == report.id }) } + Notifications.reportSeen(getApplication(), report.id) viewModelScope.launch { runCatching { getApplication().api().deleteReport(report.id) }.onFailure { fail(it) } } } @@ -292,7 +295,7 @@ private fun ReportRow(report: Report, onOpen: () -> Unit, onRead: () -> Unit, on @Composable private fun ReportFacts(report: Report) { val palette = LocalPalette.current - val facts = listOfNotNull(report.schedule?.title?.ifBlank { null }, ScheduleLogic.moment(report.created)).joinToString(" · ") + val facts = listOfNotNull(report.from.ifBlank { null }, ScheduleLogic.moment(report.created)).joinToString(" · ") if (facts.isNotBlank()) Text(facts, style = MaterialTheme.typography.bodySmall, color = palette.inkFaint, maxLines = 1, overflow = TextOverflow.Ellipsis) } @@ -312,6 +315,8 @@ class ReportViewModel(app: Application) : AndroidViewModel(app) { val api = getApplication().api() val report = api.report(id) _state.value = ReportState(report = report.copy(unread = false), loading = false) + // Read here: its notification, if one is still up, goes. + Notifications.reportSeen(getApplication(), id) if (report.unread) runCatching { api.readReport(id) } } catch (e: Exception) { _state.update { it.copy(loading = false, error = getApplication().explain(e)) } @@ -424,7 +429,8 @@ fun ReportContent(state: ReportState, onBack: () -> Unit = {}, onRetry: () -> Un } } ReportFacts(report) - RichMarkdown(report.body.orEmpty().ifBlank { report.summary }) + // The server's own text of the body, where each page is its one line; else the body. + RichMarkdown((report.text?.ifBlank { null } ?: report.body.orEmpty()).ifBlank { report.summary }) } } } diff --git a/app/src/main/java/eu/llembas/android/ui/scheduled/ScheduledScreens.kt b/app/src/main/java/eu/llembas/android/ui/scheduled/ScheduledScreens.kt index 44fadf5..c177c96 100644 --- a/app/src/main/java/eu/llembas/android/ui/scheduled/ScheduledScreens.kt +++ b/app/src/main/java/eu/llembas/android/ui/scheduled/ScheduledScreens.kt @@ -804,8 +804,26 @@ data class EditState( val reports: Boolean = false, /** The server's default condition in the account's language, when it said one. */ val notifyDefault: String = "", + /** Editing: the schedule was read, so the form shows what it really has; or reading it failed, and nothing may be saved over it. */ + val loaded: Boolean = false, + val loadFailed: Boolean = false, + /** The person chose a "notify when" themselves. */ + val notifyTouched: Boolean = false, ) +/** + * The `notify` a save sends, or null for none: only to an instance with Reports, and when + * [editing] only what the form really knows -- the schedule's own value once it was read, or the + * person's choice. A form that could not read the schedule must not write its default over an + * "every run" or a "never". + */ +fun notifyToSend(state: EditState, editing: Boolean): String? = when { + !state.reports -> null + !editing -> state.notify + state.loaded || state.notifyTouched -> state.notify + else -> null +} + class ScheduleEditViewModel(app: Application) : AndroidViewModel(app) { private val _state = MutableStateFlow(EditState()) val state = _state.asStateFlow() @@ -828,23 +846,26 @@ class ScheduleEditViewModel(app: Application) : AndroidViewModel(app) { runCatching { getApplication().api().schedule(id) }.onSuccess { s -> _state.update { it.copy( - words = s.request.ifBlank { - s.instruction - }, - notify = notifyChoice(s.notify), - notifyWhen = s.notifyWhen, + words = s.request.ifBlank { s.instruction }, + // What the person chose while it was read stays chosen. + notify = if (it.notifyTouched) it.notify else notifyChoice(s.notify), + notifyWhen = if (it.notifyTouched) it.notifyWhen else s.notifyWhen, notifyDefault = s.notifyDefault, + loaded = true, ) } + }.onFailure { e -> + // Not read: said, and Save waits -- the form would write its own defaults over the schedule. + _state.update { it.copy(loadFailed = true, error = getApplication().explain(e)) } } } } fun words(text: String) = _state.update { it.copy(words = text, compiled = null) } - fun notify(value: String) = _state.update { it.copy(notify = notifyChoice(value)) } + fun notify(value: String) = _state.update { it.copy(notify = notifyChoice(value), notifyTouched = true) } - fun notifyWhen(text: String) = _state.update { it.copy(notifyWhen = text) } + fun notifyWhen(text: String) = _state.update { it.copy(notifyWhen = text, notifyTouched = true) } /** The server reads the words: the title, the instruction, the rule and the next runs. */ fun preview() { @@ -873,7 +894,7 @@ class ScheduleEditViewModel(app: Application) : AndroidViewModel(app) { compiled, now.words.trim(), editing, - notify = now.notify.takeIf { now.reports }, + notify = notifyToSend(now, editing = editing != null), notifyWhen = now.notifyWhen, ) then(saved.id) @@ -930,7 +951,7 @@ fun ScheduleEditContent( Box(Modifier.background(palette.bg).navigationBarsPadding().imePadding().padding(16.dp)) { Button( onClick = onSave, - enabled = state.compiled?.savable == true && !state.saving, + enabled = state.compiled?.savable == true && !state.saving && !state.loadFailed, modifier = Modifier.fillMaxWidth().heightIn(min = 52.dp), ) { Text(stringResource(R.string.save)) } } @@ -1004,9 +1025,11 @@ private fun NotifyWhen(notify: String, words: String, default: String, onNotify: if (value == "when" && notify == "when") { val usual = default.ifBlank { stringResource(R.string.notify_when_default) } OutlinedTextField( - value = words.ifEmpty { usual }, + // Empty is allowed and means the default, which stands there as the placeholder. + value = words, onValueChange = onWords, label = { Text(stringResource(R.string.notify_when_label)) }, + placeholder = { Text(usual) }, shape = RoundedCornerShape(16.dp), modifier = Modifier.fillMaxWidth().padding(start = 52.dp, end = 16.dp, bottom = 8.dp), ) diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index fe87f3f..ac2ca02 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -271,6 +271,7 @@ Dá vědět, když: %1$s Otevřít její zprávu Zprávy + Důležité zprávy Nová zpráva Spustit znovu Povolit to? diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 30e918b..385ead2 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -271,6 +271,7 @@ Meldet sich, wenn: %1$s Zugehörigen Bericht öffnen Berichte + Wichtige Berichte Neuer Bericht Erneut ausführen Das erlauben? diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 603163d..36673fa 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -271,6 +271,7 @@ Avisa cuando: %1$s Abrir su informe Informes + Informes importantes Informe nuevo Ejecutar de nuevo ¿Permitir esto? diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index f90818f..33604ad 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -271,6 +271,7 @@ Prévient quand : %1$s Ouvrir son rapport Rapports + Rapports importants Nouveau rapport Relancer Autoriser ceci ? diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index a7b08d1..fa943d0 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -271,6 +271,7 @@ Értesít, amikor: %1$s A jelentésének megnyitása Jelentések + Fontos jelentések Új jelentés Futtatás újra Engedélyezzük ezt? diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index ba7b3e4..403dad2 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -271,6 +271,7 @@ Avvisa quando: %1$s Apri il suo rapporto Rapporti + Rapporti importanti Nuovo rapporto Esegui di nuovo Consentire questo? diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 1bffafe..a2cea20 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -271,6 +271,7 @@ Powiadamia, gdy: %1$s Otwórz jego raport Raporty + Ważne raporty Nowy raport Uruchom ponownie Zezwolić na to? diff --git a/app/src/main/res/values-sk/strings.xml b/app/src/main/res/values-sk/strings.xml index a4518af..ebfbc55 100644 --- a/app/src/main/res/values-sk/strings.xml +++ b/app/src/main/res/values-sk/strings.xml @@ -271,6 +271,7 @@ Dá vedieť, keď: %1$s Otvoriť jej správu Správy + Dôležité správy Nová správa Spustiť znova Povoliť to? diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b5235a7..f133afd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -272,6 +272,7 @@ Tells you when: %1$s Open its report Reports + Important reports New report Run again Allow this? diff --git a/app/src/test/java/eu/llembas/android/DisplayTest.kt b/app/src/test/java/eu/llembas/android/DisplayTest.kt index a2e1c35..b99150c 100644 --- a/app/src/test/java/eu/llembas/android/DisplayTest.kt +++ b/app/src/test/java/eu/llembas/android/DisplayTest.kt @@ -1,6 +1,7 @@ package eu.llembas.android import eu.llembas.android.api.CardItem +import eu.llembas.android.api.Drawn import eu.llembas.android.api.LembasJson import eu.llembas.android.api.LiveReply import eu.llembas.android.api.Message @@ -8,6 +9,7 @@ import eu.llembas.android.api.Progress import eu.llembas.android.api.ProgressWords import eu.llembas.android.api.ToolCall import eu.llembas.android.api.callDuration +import eu.llembas.android.api.drawn import eu.llembas.android.api.ending import eu.llembas.android.api.localeNumber import eu.llembas.android.api.progressLine @@ -181,4 +183,20 @@ class DisplayTest { } } } + + @Test + fun aLineTheServerWroteIsNeverThePersonsBubble() { + val person = Message(id = "m1", role = "user", text = "How many are left?") + val machine = Message(id = "m0", role = "user", machine = true, text = "[report] This conversation continues a report.") + val reply = Message(id = "m2", role = "assistant") + assertEquals(Drawn.Person, drawn(person, first = true, reportChat = false)) + assertEquals(Drawn.Reply, drawn(reply, first = false, reportChat = true)) + // In any chat: a quiet note. + assertEquals(Drawn.Note, drawn(machine, first = true, reportChat = false)) + assertEquals(Drawn.Note, drawn(machine, first = false, reportChat = true)) + // In a report's conversation the line that opens it is left out: the report is one tap back. + assertEquals(Drawn.Hidden, drawn(machine, first = true, reportChat = true)) + // What the person wrote first in such a chat is still theirs. + assertEquals(Drawn.Person, drawn(person, first = true, reportChat = true)) + } } diff --git a/app/src/test/java/eu/llembas/android/ReportNotificationTest.kt b/app/src/test/java/eu/llembas/android/ReportNotificationTest.kt index d8a570a..d50383d 100644 --- a/app/src/test/java/eu/llembas/android/ReportNotificationTest.kt +++ b/app/src/test/java/eu/llembas/android/ReportNotificationTest.kt @@ -2,16 +2,19 @@ package eu.llembas.android import android.app.Application import android.app.Notification +import android.app.NotificationManager import eu.llembas.android.push.Notifications import eu.llembas.android.push.ReportNotice 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 import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config /** @@ -57,4 +60,23 @@ class ReportNotificationTest { // A report without a title still says something that is the app's own. assertEquals("New report", Notifications.reportNotification(app, "home", notice.copy(title = "")).words()[0]) } + + @Test + fun anImportantReportHasAChannelThatComesUp() { + assertEquals(LembasApp.CHANNEL_REPORTS_HIGH, Notifications.reportNotification(app, "home", notice.copy(high = true)).channelId) + val manager = app.getSystemService(NotificationManager::class.java) + assertEquals(NotificationManager.IMPORTANCE_HIGH, manager.getNotificationChannel(LembasApp.CHANNEL_REPORTS_HIGH).importance) + assertEquals(NotificationManager.IMPORTANCE_DEFAULT, manager.getNotificationChannel(LembasApp.CHANNEL_REPORTS).importance) + // Both stay private on the lock screen. + assertEquals(Notification.VISIBILITY_PRIVATE, manager.getNotificationChannel(LembasApp.CHANNEL_REPORTS_HIGH).lockscreenVisibility) + } + + @Test + fun readingTheReportInTheAppTakesItsNotificationAway() { + val manager = app.getSystemService(NotificationManager::class.java) + manager.notify(notice.id, Notifications.reportNotification(app, "home", notice)) + assertNotNull(shadowOf(manager).getNotification(notice.id)) + Notifications.reportSeen(app, "rep1") + assertNull(shadowOf(manager).getNotification(notice.id)) + } } diff --git a/app/src/test/java/eu/llembas/android/ReportsContractTest.kt b/app/src/test/java/eu/llembas/android/ReportsContractTest.kt index 3d71351..5bbcc7f 100644 --- a/app/src/test/java/eu/llembas/android/ReportsContractTest.kt +++ b/app/src/test/java/eu/llembas/android/ReportsContractTest.kt @@ -1,5 +1,6 @@ package eu.llembas.android +import eu.llembas.android.api.Chat import eu.llembas.android.api.LembasJson import eu.llembas.android.api.PushPayload import eu.llembas.android.api.Report @@ -7,6 +8,8 @@ import eu.llembas.android.api.ReportList import eu.llembas.android.api.ReportReply import eu.llembas.android.api.ReportsFrame import eu.llembas.android.api.RunResult +import eu.llembas.android.api.Step +import eu.llembas.android.api.shownMarkdown import eu.llembas.android.push.ReportNotice import eu.llembas.android.push.ScheduledNotice import eu.llembas.android.ui.Segment @@ -79,4 +82,38 @@ class ReportsContractTest { // And the run that filed it says which report. assertEquals("rep1", read("schedule_run", RunResult.serializer()).run.reportId) } + + @Test + fun aReportsConversationSaysWhichReportAndWhatMayBeDoneToIt() { + val chat = read("chat_report", Chat.serializer()) + assertEquals("report", chat.kind) + assertEquals("rep1", chat.report?.id) + assertEquals("Two cakes are unwrapped", chat.report?.title) + // Rename and delete; not archive, pin or a folder. + assertTrue(chat.allows("rename") && chat.allows("delete")) + assertFalse(chat.allows("archive") || chat.allows("pin") || chat.allows("folder")) + // An ordinary chat allows all of them, and so does a server that does not say. + assertTrue(read("chat", Chat.serializer()).allows("archive")) + assertNull(read("chat", Chat.serializer()).report) + assertTrue(LembasJson.decodeFromString(Chat.serializer(), """{"id":"c1"}""").allows("archive")) + } + + @Test + fun aReportWhoseScheduleIsGoneKeepsItsName() { + val orphan = LembasJson.decodeFromJsonElement(Report.serializer(), LembasJson.parseToJsonElement(text("report_orphan")).jsonObject.getValue("report")) + assertNull(orphan.schedule) + assertEquals("Waybread check", orphan.from) + assertEquals("Waybread check", read("reports", ReportList.serializer()).reports.single().from) + } + + @Test + fun aStepWithAPageIsShownByItsText() { + val step = read("step_text_page", Step.serializer()) + assertEquals("Cakes", step.pages.single().title) + // The server's own text, where the page is its one line -- which the phone draws as a card. + assertEquals("Two are unwrapped.\n\n[page: Cakes]\n", step.shownMarkdown) + assertEquals(listOf(Segment.Prose("Two are unwrapped."), Segment.Page("Cakes")), splitFences(step.shownMarkdown)) + // A step without pages is its markdown. + assertEquals("plain", Step(kind = "text", markdown = "plain", text = "other").shownMarkdown) + } } diff --git a/app/src/test/java/eu/llembas/android/ReportsTest.kt b/app/src/test/java/eu/llembas/android/ReportsTest.kt index 8daab69..aba69ec 100644 --- a/app/src/test/java/eu/llembas/android/ReportsTest.kt +++ b/app/src/test/java/eu/llembas/android/ReportsTest.kt @@ -8,12 +8,16 @@ import eu.llembas.android.api.ReportReply import eu.llembas.android.api.Schedule import eu.llembas.android.api.ScheduleRun import eu.llembas.android.api.notifyChoice +import eu.llembas.android.api.safeId import eu.llembas.android.api.sortedReports +import eu.llembas.android.push.Notifications import eu.llembas.android.push.ReportNotice import eu.llembas.android.push.ScheduledNotice import eu.llembas.android.ui.Segment import eu.llembas.android.ui.pageProjection import eu.llembas.android.ui.reports.shareText +import eu.llembas.android.ui.scheduled.EditState +import eu.llembas.android.ui.scheduled.notifyToSend import eu.llembas.android.ui.splitFences import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse @@ -148,6 +152,52 @@ class ReportsTest { assertTrue(ScheduledNotice.from(result.copy(kind = "schedule_failed"))!!.failed) } + @Test + fun onlyWhatLooksLikeAnIdBecomesARoute() { + assertEquals("rep1", safeId("rep1")) + assertEquals("a-b_C9", safeId("a-b_C9")) + // A slash, a query, a dot, nothing, too much: none of these is an id. + for (bad in listOf("a/b", "rep1?x=1", "..", "", "rep 1", "x".repeat(65))) assertNull(bad, safeId(bad)) + assertNull(safeId(null)) + // A push whose id is no id shows nothing rather than open somewhere else. + val push = PushPayload(kind = "report", reportId = "rep1/../../chat/c1", url = "/reports/rep1") + assertNull(ReportNotice.from(push)) + assertNull(ReportNotice.from(PushPayload(kind = "report", url = "/reports/a%2Fb"))) + assertNull(ScheduledNotice.from(PushPayload(kind = "schedule", scheduleId = "s1/x"))) + // The run's id alone being bad drops the run, not the notification. + assertNull(ScheduledNotice.from(PushPayload(kind = "schedule", scheduleId = "s1", runId = "r/1"))!!.runId) + } + + @Test + fun aPushOfAKindThisAppDoesNotKnowIsNotAChat() { + assertTrue(Notifications.chatKind("")) + assertTrue(Notifications.chatKind("reply")) + assertTrue(Notifications.chatKind("ask")) + assertFalse(Notifications.chatKind("digest")) + assertFalse(Notifications.chatKind("report")) + } + + @Test + fun theFormSendsNotifyOnlyWhenItKnowsWhatItSends() { + val fresh = EditState(reports = true) + // A new schedule: what the form shows is what is meant. + assertEquals("when", notifyToSend(fresh, editing = false)) + // An instance without Reports is sent nothing new. + assertNull(notifyToSend(EditState(reports = false, notify = "never", notifyTouched = true), editing = false)) + // Editing before the schedule was read (or when reading it failed): the default is not written over it... + assertNull(notifyToSend(fresh, editing = true)) + assertNull(notifyToSend(fresh.copy(loadFailed = true), editing = true)) + // ...unless the person chose, or it was read. + assertEquals("never", notifyToSend(fresh.copy(notify = "never", notifyTouched = true), editing = true)) + assertEquals("always", notifyToSend(fresh.copy(notify = "always", loaded = true), editing = true)) + } + + @Test + fun aSpaceBeforeTheWordIsNotAPage() { + // The spec: `html-view` stands straight after the fence. With a space it is a code block, here as on the server. + assertEquals(listOf(Segment.Code("html-view", "

x

")), splitFences("``` html-view Cakes\n

x

\n```")) + } + @Test fun aPageInAnAnswerIsNeverItsSource() { val reply = "Sales by month:\n\n```html-view Monthly sales\n\n\n```\n\nThat is all." diff --git a/app/src/test/java/eu/llembas/android/shots/ScreenShots.kt b/app/src/test/java/eu/llembas/android/shots/ScreenShots.kt index ebcf5a3..40177c8 100644 --- a/app/src/test/java/eu/llembas/android/shots/ScreenShots.kt +++ b/app/src/test/java/eu/llembas/android/shots/ScreenShots.kt @@ -143,7 +143,7 @@ class ScreenShots { drawerState = rememberDrawerState(DrawerValue.Open), drawerContent = { DrawerContent( - Fakes.chats.copy(unreadReports = 2), + Fakes.chats.copy(reports = true, unreadReports = 2), current = "c1", account = "Home · frodo@example.org", actions = NoDrawerActions, diff --git a/contract/chat.json b/contract/chat.json index ecb2e02..1895ede 100644 --- a/contract/chat.json +++ b/contract/chat.json @@ -24,5 +24,13 @@ "cap": 32 }, "created_at": "2026-01-02T03:04:05+00:00", - "updated_at": "2026-01-02T03:04:05+00:00" + "updated_at": "2026-01-02T03:04:05+00:00", + "controls": [ + "rename", + "delete", + "archive", + "pin", + "folder" + ], + "report": null } diff --git a/contract/chat_agent.json b/contract/chat_agent.json index efc07d4..bc8752c 100644 --- a/contract/chat_agent.json +++ b/contract/chat_agent.json @@ -27,5 +27,13 @@ "cap": 32 }, "created_at": "2026-01-02T03:04:05+00:00", - "updated_at": "2026-01-02T03:04:05+00:00" + "updated_at": "2026-01-02T03:04:05+00:00", + "controls": [ + "rename", + "delete", + "archive", + "pin", + "folder" + ], + "report": null } diff --git a/contract/chat_html_pages.json b/contract/chat_html_pages.json new file mode 100644 index 0000000..a21087d --- /dev/null +++ b/contract/chat_html_pages.json @@ -0,0 +1,4 @@ +{ + "offered": true, + "on": false +} diff --git a/contract/chat_report.json b/contract/chat_report.json new file mode 100644 index 0000000..4e5e8d1 --- /dev/null +++ b/contract/chat_report.json @@ -0,0 +1,32 @@ +{ + "id": "chat9", + "kind": "report", + "title": "Two cakes are unwrapped", + "model": "qwen", + "folder_id": null, + "pinned": false, + "archived": false, + "temporary": false, + "unread": true, + "running": false, + "mode": "", + "effort": "low", + "device": null, + "device_session_id": null, + "parent_chat_id": null, + "agents": { + "min": 1, + "max": 4 + }, + "subagents": null, + "created_at": "2026-01-02T03:04:05+00:00", + "updated_at": "2026-01-02T03:04:05+00:00", + "controls": [ + "rename", + "delete" + ], + "report": { + "id": "rep1", + "title": "Two cakes are unwrapped" + } +} diff --git a/contract/chats_listed.json b/contract/chats_listed.json index e1e6467..7033a2f 100644 --- a/contract/chats_listed.json +++ b/contract/chats_listed.json @@ -22,7 +22,15 @@ }, "subagents": null, "created_at": "2026-01-02T03:04:05+00:00", - "updated_at": "2026-01-02T03:04:05+00:00" + "updated_at": "2026-01-02T03:04:05+00:00", + "controls": [ + "rename", + "delete", + "archive", + "pin", + "folder" + ], + "report": null } ], "more": false diff --git a/contract/event_reports.json b/contract/event_reports.json index ea46a1a..b6bd855 100644 --- a/contract/event_reports.json +++ b/contract/event_reports.json @@ -9,7 +9,9 @@ "id": "sched1", "title": "Waybread check" }, + "schedule_title": "Waybread check", "created": "2026-01-02T03:04:05+00:00", "unread": true - } + }, + "latest_created": "2026-01-02T03:04:05+00:00" } diff --git a/contract/lock.json b/contract/lock.json index 30f2141..1f14027 100644 --- a/contract/lock.json +++ b/contract/lock.json @@ -1,5 +1,5 @@ { - "ref": "ee74edc9f3bf81f50c5c6b14e448211ee502cfdc", - "commit": "ee74edc9f3bf81f50c5c6b14e448211ee502cfdc", - "sha256": "d19ab4ed34614c065bbd360fc88a59844292a9f791c3bdf97d2b59f6127f8770" + "ref": "40813d1f7e880a0a52e29755ce8425e00b8557c3", + "commit": "40813d1f7e880a0a52e29755ce8425e00b8557c3", + "sha256": "fcdfaca53bec6edefc22ae0d600bf284c3fe3c8880ed959ae9db8d539fed5c31" } diff --git a/contract/models.json b/contract/models.json index 36ae696..8ed6c30 100644 --- a/contract/models.json +++ b/contract/models.json @@ -22,7 +22,8 @@ "subagents": { "offered": true, "named_only": false - } + }, + "html_pages": false }, { "id": "gpt-oss", @@ -49,7 +50,8 @@ "subagents": { "offered": true, "named_only": true - } + }, + "html_pages": false }, { "id": "vision-small", @@ -70,7 +72,8 @@ "subagents": { "offered": false, "named_only": false - } + }, + "html_pages": false } ], "subagents_cap": 32 diff --git a/contract/models_agent.json b/contract/models_agent.json index 2d8e087..0212c2f 100644 --- a/contract/models_agent.json +++ b/contract/models_agent.json @@ -22,7 +22,8 @@ "subagents": { "offered": true, "named_only": false - } + }, + "html_pages": false }, { "id": "gpt-oss", @@ -49,7 +50,8 @@ "subagents": { "offered": true, "named_only": false - } + }, + "html_pages": false }, { "id": "vision-small", @@ -70,7 +72,8 @@ "subagents": { "offered": true, "named_only": false - } + }, + "html_pages": false } ], "subagents_cap": 32, diff --git a/contract/page.json b/contract/page.json new file mode 100644 index 0000000..7553263 --- /dev/null +++ b/contract/page.json @@ -0,0 +1,6 @@ +{ + "title": "Cakes", + "digest": "526b3a6b58f09769d9f81f628942cc7f", + "html": "\nCakes\n

10 wrapped, 2 not

", + "policy": "sandbox; default-src 'none'; script-src 'none'; style-src 'unsafe-inline'; img-src data: blob:; font-src data:; media-src 'none'; connect-src 'none'; worker-src 'none'; child-src 'none'; frame-src 'none'; object-src 'none'; manifest-src 'none'; form-action 'none'; base-uri 'none'; webrtc 'block'; frame-ancestors 'self'" +} diff --git a/contract/report.json b/contract/report.json index 036e501..a88bd86 100644 --- a/contract/report.json +++ b/contract/report.json @@ -8,6 +8,7 @@ "id": "sched1", "title": "Waybread check" }, + "schedule_title": "Waybread check", "created": "2026-01-02T03:04:05+00:00", "unread": true, "body": "## What\n\nTwo of the twelve cakes have lost their leaves.\n\n```html-view Cakes\n

10 wrapped, 2 not

\n```\n", diff --git a/contract/report_orphan.json b/contract/report_orphan.json new file mode 100644 index 0000000..3b83c94 --- /dev/null +++ b/contract/report_orphan.json @@ -0,0 +1,22 @@ +{ + "report": { + "id": "rep1", + "title": "Two cakes are unwrapped", + "summary": "Two of the twelve cakes have lost their leaves.", + "importance": "high", + "schedule": null, + "schedule_title": "Waybread check", + "created": "2026-01-02T03:04:05+00:00", + "unread": true, + "body": "## What\n\nTwo of the twelve cakes have lost their leaves.\n\n```html-view Cakes\n

10 wrapped, 2 not

\n```\n", + "text": "## What\n\nTwo of the twelve cakes have lost their leaves.\n\n[page: Cakes]\n", + "pages": [ + { + "title": "Cakes", + "digest": "526b3a6b58f09769d9f81f628942cc7f" + } + ], + "model": "gpt-oss", + "chat": "chat9" + } +} diff --git a/contract/reports.json b/contract/reports.json index 6371180..6b00dc0 100644 --- a/contract/reports.json +++ b/contract/reports.json @@ -9,6 +9,7 @@ "id": "sched1", "title": "Waybread check" }, + "schedule_title": "Waybread check", "created": "2026-01-02T03:04:05+00:00", "unread": true } diff --git a/contract/schedule_models.json b/contract/schedule_models.json index fb70a00..7b6111d 100644 --- a/contract/schedule_models.json +++ b/contract/schedule_models.json @@ -26,6 +26,7 @@ "offered": false, "named_only": false }, + "html_pages": false, "free": false, "busy": "it is answering someone", "chosen": true @@ -53,6 +54,7 @@ "offered": false, "named_only": false }, + "html_pages": false, "free": true, "busy": null, "chosen": false diff --git a/contract/step_text_page.json b/contract/step_text_page.json new file mode 100644 index 0000000..62f6ea2 --- /dev/null +++ b/contract/step_text_page.json @@ -0,0 +1,13 @@ +{ + "index": 2, + "kind": "text", + "open": false, + "markdown": "Two are unwrapped.\n\n```html-view Cakes\n

10 wrapped, 2 not

\n```\n", + "pages": [ + { + "title": "Cakes", + "digest": "526b3a6b58f09769d9f81f628942cc7f" + } + ], + "text": "Two are unwrapped.\n\n[page: Cakes]\n" +} diff --git a/scripts/strings.py b/scripts/strings.py index 9a49d00..a27d2e3 100755 --- a/scripts/strings.py +++ b/scripts/strings.py @@ -288,6 +288,7 @@ STRINGS: dict[str, str] = { "notify_when_words": "Tells you when: %1$s", "run_open_report": "Open its report", "channel_reports": "Reports", + "channel_reports_high": "Important reports", "report_new_public": "New report", "schedule_run_again": "Run again", # Cards diff --git a/scripts/strings_own.json b/scripts/strings_own.json index 5e36bf1..e924e9f 100644 --- a/scripts/strings_own.json +++ b/scripts/strings_own.json @@ -91,6 +91,7 @@ "card_reason": "Proč ne? (nepovinné)", "channel_asks": "Otázky a schválení", "channel_reports": "Zprávy", + "channel_reports_high": "Důležité zprávy", "channel_scheduled": "Výsledky naplánovaných úloh", "channel_scheduled_failures": "Selhání naplánovaných úloh", "channel_updates": "Aktualizace aplikace", @@ -472,6 +473,7 @@ "card_reason": "Warum nicht? (optional)", "channel_asks": "Fragen und Bestätigungen", "channel_reports": "Berichte", + "channel_reports_high": "Wichtige Berichte", "channel_scheduled": "Geplante Ergebnisse", "channel_scheduled_failures": "Geplante Fehler", "channel_updates": "App-Updates", @@ -854,6 +856,7 @@ "card_reason": "¿Por qué no? (opcional)", "channel_asks": "Preguntas y aprobaciones", "channel_reports": "Informes", + "channel_reports_high": "Informes importantes", "channel_scheduled": "Resultados programados", "channel_scheduled_failures": "Fallos programados", "channel_updates": "Actualizaciones de la aplicación", @@ -1235,6 +1238,7 @@ "card_reason": "Pourquoi pas ? (facultatif)", "channel_asks": "Questions et approbations", "channel_reports": "Rapports", + "channel_reports_high": "Rapports importants", "channel_scheduled": "Résultats planifiés", "channel_scheduled_failures": "Échecs planifiés", "channel_updates": "Mises à jour de l’application", @@ -1617,6 +1621,7 @@ "card_reason": "Miért nem? (opcionális)", "channel_asks": "Kérdések és jóváhagyások", "channel_reports": "Jelentések", + "channel_reports_high": "Fontos jelentések", "channel_scheduled": "Ütemezett eredmények", "channel_scheduled_failures": "Ütemezett hibák", "channel_updates": "Alkalmazásfrissítések", @@ -1999,6 +2004,7 @@ "card_reason": "Perché no? (facoltativo)", "channel_asks": "Domande e approvazioni", "channel_reports": "Rapporti", + "channel_reports_high": "Rapporti importanti", "channel_scheduled": "Risultati pianificati", "channel_scheduled_failures": "Errori pianificati", "channel_updates": "Aggiornamenti dell’app", @@ -2380,6 +2386,7 @@ "card_reason": "Czemu nie? (opcjonalnie)", "channel_asks": "Pytania i zatwierdzenia", "channel_reports": "Raporty", + "channel_reports_high": "Ważne raporty", "channel_scheduled": "Wyniki zaplanowanych zadań", "channel_scheduled_failures": "Błędy zaplanowanych zadań", "channel_updates": "Aktualizacje aplikacji", @@ -2761,6 +2768,7 @@ "card_reason": "Prečo nie? (nepovinné)", "channel_asks": "Otázky a schválenia", "channel_reports": "Správy", + "channel_reports_high": "Dôležité správy", "channel_scheduled": "Výsledky naplánovaných úloh", "channel_scheduled_failures": "Zlyhania naplánovaných úloh", "channel_updates": "Aktualizácie aplikácie",