diff --git a/CHANGELOG.md b/CHANGELOG.md index 4476ce3..ff6ed3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,10 @@ is in alpha and releases come off `main`. - The approval card offers the spec's four actions: *Allow once*, *Always allow*, *Deny*, and *Deny, and say why*. The plan card approves together with the mode to continue in -- *Approve, continue in Edit* or *Approve, continue in Manual* -- and *Revise*. +- **A reply ends when it is saved, and the chat's name follows** (with a server that sends `done` + first: LLeMbas 2.0.0-beta.11). The app treats `done` as the end -- the transcript, a spoken + answer and a queued prompt no longer wait for a model to name a new chat -- and takes the name + from the `title` frame when it comes. - **Wake phrases the engine can hear.** "Hey Lembas" is gone: Lembas is not a word the engine's model was ever trained on, so it had to be repeated until it happened to land. The phrase is now a greeting and a name, each chosen in Settings › Wake word: **Hey**, **OK** or **Hello**, then diff --git a/app/src/main/java/eu/llembas/android/api/LiveReply.kt b/app/src/main/java/eu/llembas/android/api/LiveReply.kt index 0ccb4ae..0df2d49 100644 --- a/app/src/main/java/eu/llembas/android/api/LiveReply.kt +++ b/app/src/main/java/eu/llembas/android/api/LiveReply.kt @@ -25,7 +25,11 @@ data class LiveReply( val tree: SubagentTree? = null, val metrics: Metrics? = null, val steered: Set = emptySet(), - /** Set by `done`: the finished row, which replaces this in the transcript. */ + /** + * Set by `done`: the finished row, which replaces this in the transcript. `done` is the end + * of the reply -- the stream may stay open after it for one `title` frame, which nobody + * waits for. + */ val finished: Message? = null, val title: String? = null, /** A queued prompt the server started when this reply ended: the next stream to follow. */ @@ -79,6 +83,9 @@ data class LiveReply( ) } ?: this + // After `done`, when a model has named the chat: the name, and nothing else changes. + "title" -> decode(TitleFrame.serializer(), data)?.let { named -> copy(title = named.chat.title.ifEmpty { title }) } ?: this + "close" -> copy(closed = true) else -> this 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 127982c..d9f96e5 100644 --- a/app/src/main/java/eu/llembas/android/api/Models.kt +++ b/app/src/main/java/eu/llembas/android/api/Models.kt @@ -637,10 +637,19 @@ data class JobsFrame(val jobs: List = emptyList()) @Serializable data class DoneFrame(val message: Message, val chat: DoneChat = DoneChat(), val next: String? = null) { + /** + * [titlePending]: a model is still naming the chat, so [title] is the one made from the + * prompt and a `title` frame may follow before `close`. `done` is the end of the reply all + * the same. + */ @Serializable - data class DoneChat(val id: String = "", val title: String = "") + data class DoneChat(val id: String = "", val title: String = "", @SerialName("title_pending") val titlePending: Boolean = false) } +/** The `title` frame: the name a model gave the chat, after `done`. */ +@Serializable +data class TitleFrame(val chat: DoneFrame.DoneChat = DoneFrame.DoneChat()) + /** The push payload the server sends (services/push.py), as the app reads it. */ @Serializable data class PushPayload( diff --git a/app/src/main/java/eu/llembas/android/assist/AssistController.kt b/app/src/main/java/eu/llembas/android/assist/AssistController.kt index 0bef4d3..2d85be7 100644 --- a/app/src/main/java/eu/llembas/android/assist/AssistController.kt +++ b/app/src/main/java/eu/llembas/android/assist/AssistController.kt @@ -28,6 +28,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.transformWhile import kotlinx.coroutines.flow.update import kotlinx.coroutines.isActive import kotlinx.coroutines.launch @@ -432,7 +433,12 @@ class AssistController(private val context: Context, private val scope: Coroutin } } var live = LiveReply(replyId) - StreamClient(api).follow(inChat, replyId).collect { frame -> + // To `done`, which is the end of the reply: the stream may stay open after it for + // the chat's name, and a spoken answer must not wait for that. + StreamClient(api).follow(inChat, replyId).transformWhile { frame -> + emit(frame) + frame.event != "done" + }.collect { frame -> live = live.apply(frame.event, frame.data) _state.update { s -> s.copy(turns = s.turns.dropLast(1) + turn.copy(reply = live), busy = live.finished == null) } } diff --git a/app/src/main/java/eu/llembas/android/ui/chat/ChatViewModel.kt b/app/src/main/java/eu/llembas/android/ui/chat/ChatViewModel.kt index 422a819..4369a20 100644 --- a/app/src/main/java/eu/llembas/android/ui/chat/ChatViewModel.kt +++ b/app/src/main/java/eu/llembas/android/ui/chat/ChatViewModel.kt @@ -279,17 +279,47 @@ class ChatViewModel(app: Application) : following?.cancel() following = viewModelScope.launch { _state.update { it.copy(live = LiveReply(messageId)) } + // Set at `done`, which is the end of the reply: from then on the stream is followed + // only for the name a model may still give the chat (the `title` frame). + var ended: LiveReply? = null try { StreamClient(api()).follow(chat.id, messageId).collect { frame -> - _state.update { s -> - val live = (s.live ?: LiveReply(messageId)).apply(frame.event, frame.data) + val before = ended + if (before != null) { + val named = before.apply(frame.event, frame.data) + ended = named + if (named.title != before.title) _state.update { s -> s.copy(chat = s.chat?.let { c -> named.title?.let { c.copy(title = it) } ?: c }) } + return@collect + } + val live = (_state.value.live ?: LiveReply(messageId)).apply(frame.event, frame.data) + val finished = live.finished + if (finished == null) { // The tree outlives the reply: kept for the activity sheet once it ends. - s.copy(live = live, tree = live.tree ?: s.tree) + _state.update { s -> s.copy(live = live, tree = live.tree ?: s.tree) } + return@collect + } + ended = live + _state.update { s -> + s.copy( + live = null, + tree = live.tree ?: s.tree, + messages = s.messages.map { if (it.id == finished.id) finished else it }, + chat = s.chat?.let { c -> live.title?.let { c.copy(title = it) } ?: c }, + ) + } + // The queue handed a waiting prompt to a fresh reply: the transcript has moved + // on the server, so it is read again rather than patched -- and that reply is + // followed instead of this stream's tail. + live.next?.let { next -> + refresh() + follow(next) } } } catch (e: CancellationException) { throw e } catch (e: Exception) { + // After `done` only the name was still to come: nothing to say, nothing to redo. + if (ended != null) return@launch fail(e) // The reply goes on on the server; what it has written so far is read back // rather than left half-drawn. @@ -297,21 +327,8 @@ class ChatViewModel(app: Application) : refresh() return@launch } - val ended = _state.value.live - _state.update { s -> - val finished = ended?.finished - s.copy( - live = null, - messages = if (finished != null) s.messages.map { if (it.id == finished.id) finished else it } else s.messages, - chat = s.chat?.let { c -> ended?.title?.let { c.copy(title = it) } ?: c }, - ) - } - // The queue handed a waiting prompt to a fresh reply: the transcript has moved on - // the server, so it is read again rather than patched. - ended?.next?.let { next -> - refresh() - follow(next) - } + // The stream closed without a `done` (an older reply already gone): nothing is live. + if (ended == null) _state.update { it.copy(live = null) } } } diff --git a/app/src/test/java/eu/llembas/android/ContractTest.kt b/app/src/test/java/eu/llembas/android/ContractTest.kt index 1d16217..2b89076 100644 --- a/app/src/test/java/eu/llembas/android/ContractTest.kt +++ b/app/src/test/java/eu/llembas/android/ContractTest.kt @@ -27,6 +27,7 @@ import eu.llembas.android.api.SteeredFrame import eu.llembas.android.api.StepsFrame import eu.llembas.android.api.SubagentTranscript import eu.llembas.android.api.Tail +import eu.llembas.android.api.TitleFrame import eu.llembas.android.api.ToolCall import eu.llembas.android.api.TreeFrame import eu.llembas.android.api.TreeView @@ -315,6 +316,26 @@ class ContractTest { assertEquals("Waybread", read("event_done", DoneFrame.serializer()).chat.title) } + @Test + fun doneIsTheEndAndTheChatsNameMayFollow() { + // A new chat's first reply: saved and done at once, under the name made from the prompt. + val pending = read("event_done_title_pending", DoneFrame.serializer()) + assertTrue(pending.chat.titlePending) + assertEquals("How is lembas made?", pending.chat.title) + assertFalse(read("event_done", DoneFrame.serializer()).chat.titlePending) + var live = LiveReply("msg2").apply("done", text("event_done_title_pending")) + assertNotNull(live.finished) + assertEquals("How is lembas made?", live.title) + assertFalse(live.closed) + // Then, if a model names it before the stream closes, one `title` frame: only the name changes. + val finished = live.finished + live = live.apply("title", text("event_title")) + assertEquals("Waybread", live.title) + assertEquals(finished, live.finished) + assertEquals("chat1", read("event_title", TitleFrame.serializer()).chat.id) + assertTrue(live.apply("close", text("event_close")).closed) + } + @Test fun aWholeStreamBuildsAReply() { var live = LiveReply("msg2") diff --git a/contract/event_done.json b/contract/event_done.json index 61da9de..172ae12 100644 --- a/contract/event_done.json +++ b/contract/event_done.json @@ -88,7 +88,8 @@ }, "chat": { "id": "chat1", - "title": "Waybread" + "title": "Waybread", + "title_pending": false }, "next": null } diff --git a/contract/event_done_title_pending.json b/contract/event_done_title_pending.json new file mode 100644 index 0000000..c97fd8d --- /dev/null +++ b/contract/event_done_title_pending.json @@ -0,0 +1,95 @@ +{ + "message": { + "id": "msg2", + "role": "assistant", + "created_at": "2026-01-02T03:04:05+00:00", + "complete": true, + "stopped": false, + "error": null, + "queued": false, + "machine": false, + "origin": "", + "model": "qwen", + "attachments": [], + "steps": [ + { + "index": 0, + "kind": "thinking", + "open": false, + "text": "The reader wants a recipe.", + "label": "for 1 second · 6 tokens" + }, + { + "index": 0, + "kind": "text", + "open": false, + "markdown": "Here is how it is made:\n\n```sh\nbake --slow\n```\n" + }, + { + "index": 0, + "kind": "tools", + "open": false, + "calls": [ + { + "id": "call1", + "name": "bash", + "kind": "shell", + "status": "ok", + "args": { + "command": "bake --slow" + }, + "purpose": "Bake one loaf", + "text": "baked\n", + "exit": 0, + "block": { + "kind": "shell", + "title": "bake --slow", + "chips": [ + "exit 0" + ], + "added": 0, + "removed": 0 + } + } + ] + }, + { + "index": 0, + "kind": "steer", + "open": false, + "messages": [ + { + "id": "msg9", + "text": "and add honey" + } + ] + }, + { + "index": 1, + "kind": "text", + "open": false, + "markdown": "Done. *Wrap it in a leaf.*" + } + ], + "plan": null, + "metrics": { + "prompt_tokens": 120, + "completion_tokens": 40, + "total_tokens": 160, + "context_tokens": 160, + "context_limit": 8192, + "estimated": false, + "elapsed_ms": 4000, + "rounds": 2, + "thinking_ms": 1200, + "percent": 2, + "tok_s": 10.0 + } + }, + "chat": { + "id": "chat1", + "title": "How is lembas made?", + "title_pending": true + }, + "next": null +} diff --git a/contract/event_title.json b/contract/event_title.json new file mode 100644 index 0000000..2162344 --- /dev/null +++ b/contract/event_title.json @@ -0,0 +1,6 @@ +{ + "chat": { + "id": "chat1", + "title": "Waybread" + } +} diff --git a/contract/lock.json b/contract/lock.json index 4708e9e..c346fc8 100644 --- a/contract/lock.json +++ b/contract/lock.json @@ -1,5 +1,5 @@ { - "ref": "v2.0.0-beta.10", - "commit": "6d1ab36371c8492ee2590fbe25d112d73513a9ae", - "sha256": "d82cfc3a93ec70127c6bdff1a1b0f564d4276428ffc6ffb7a66cf13bfe746acd" + "ref": "5044168b337309aa52cea6af5ab12b2744896bb5", + "commit": "5044168b337309aa52cea6af5ab12b2744896bb5", + "sha256": "c417d917e5801e2f6378ee7fbf1d75eb398a4e11ea6f5a03a28554bcbb61e80d" }