Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.
fix: a chat that opened with an image alone is never untitled (#95)
Durganshu7972025066cb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4b05a02..1df5954 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -231,6 +231,9 @@ All notable changes to Gotcha are documented here.assistant reads is hidden from you; the import preview says when this happens(#83). And a setting opened from search stays on screen when the page aboveit finishes loading, such as the Samosa account card on AI Configuration (#92).+ A chat started with an image and no text is called "Image chat" instead of+ having a blank name, and gets a proper title from the first message you write+ in it (#95).- **A task keeps running when you swipe Gotcha away** (#111). Swiping Gotcha outof your recent apps while it worked on a task, or pressing Back on its homescreen mid-task on Android 11, used to stop the task as if you had presseddiff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.ktindex 63502a0..3709d0f 100644--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt@@ -295,9 +295,8 @@ class AgentEngine(*/private var lastModelRequestFailed = false- /** Falls back to the truncated first user message until [generatedTitle] is set. */- fun currentTitle(): String =- generatedTitle ?: history.firstOrNull { it.role == "user" }?.textContent?.take(30) ?: "New Chat"+ /** Falls back to [ChatTitle.fallback] until [generatedTitle] is set. */+ fun currentTitle(): String = generatedTitle ?: ChatTitle.fallback(history)/*** Restores title state when switching the engine to another session. Pass `null`@@ -318,8 +317,9 @@ class AgentEngine(if (titleGenerationAttempted) return// Not marked attempted: the next run that reaches the model titles the chat.if (lastModelRequestFailed) return- val firstUserText = history.firstOrNull { it.role == "user" }?.textContent?.trim()- if (firstUserText.isNullOrBlank()) return+ // A chat that opened with images alone has nothing to title yet: left+ // unattempted, so the first message with words in it titles the chat.+ val firstUserText = ChatTitle.openingText(history) ?: returntitleGenerationAttempted = truetry {val messages = listOf(diff --git a/app/src/main/java/com/gotcha/agent/ChatRunner.kt b/app/src/main/java/com/gotcha/agent/ChatRunner.ktindex 427a769..44045bf 100644--- a/app/src/main/java/com/gotcha/agent/ChatRunner.kt+++ b/app/src/main/java/com/gotcha/agent/ChatRunner.kt@@ -948,7 +948,8 @@ class ChatRunner(private val app: Application) : AgentEvents {*/private fun runningChat(sessionId: String): RunningChat {val title = engine.generatedTitle- ?: engine.history.firstOrNull { it.role == "user" }?.textContent?.let(::shortTitle)+ ?: ChatTitle.openingText(engine.history)?.let(::shortTitle)+ ?: ChatTitle.fallback(engine.history)return RunningChat(sessionId, title.takeIf { mayNameChat(sessionId) })}@@ -1117,10 +1118,8 @@ private fun nextIdAfter(messages: List<UiMessage>): Long = messages.maxOfOrNull* fallback rather than an LLM-generated title, so it's eligible to be* (re)generated next time the session is saved.*/-internal fun ChatSession.isFallbackTitle(): Boolean {- val fallback = messages.firstOrNull { it.role == "user" }?.textContent?.take(30)- return title.isBlank() || title == fallback-}+internal fun ChatSession.isFallbackTitle(): Boolean =+ title.isBlank() || title == ChatTitle.fallback(messages)/*** Builds the LLM user message for the given prompt and attachments: everydiff --git a/app/src/main/java/com/gotcha/agent/ChatTitle.kt b/app/src/main/java/com/gotcha/agent/ChatTitle.ktindex d5cf6d3..98943e3 100644--- a/app/src/main/java/com/gotcha/agent/ChatTitle.kt+++ b/app/src/main/java/com/gotcha/agent/ChatTitle.kt@@ -1,5 +1,8 @@package com.gotcha.agent+import com.gotcha.data.isScreenCapture+import com.gotcha.llm.ChatMessage+/*** Validation for LLM-generated chat titles.*@@ -22,6 +25,35 @@ object ChatTitle {"i've", "i have", "i'll", "i will", "i can", "i'm", "i am")+ /** How much of the opening text names a chat until it has a title. */+ const val FALLBACK_CHARS = 30++ /** The name of a chat that opened with images alone, until it has a title. */+ const val IMAGE_CHAT = "Image chat"++ private const val NEW_CHAT = "New Chat"++ /**+ * The first thing the user wrote in the chat, trimmed, or null while every+ * message so far is attachments alone. An image-only message carries a+ * single space for the model (see [com.gotcha.llm.IMAGE_ONLY_TEXT]), and+ * the agent's own screen captures are user messages too; neither is+ * something to name a chat by.+ */+ fun openingText(messages: List<ChatMessage>): String? =+ messages.firstOrNull { it.role == "user" && it.textContent.isNotBlank() && !isScreenCapture(it.textContent) }+ ?.textContent+ ?.trim()++ /**+ * What a chat is called until it has a generated title: the start of its+ * [openingText], or [IMAGE_CHAT] when it opened with images alone. Never+ * blank, so the chat list and the top bar always have something to show.+ */+ fun fallback(messages: List<ChatMessage>): String =+ openingText(messages)?.take(FALLBACK_CHARS)+ ?: if (messages.any { it.role == "user" && it.hasImage }) IMAGE_CHAT else NEW_CHAT+/** Returns a usable title, or null if [raw] doesn't look like one. */fun sanitize(raw: String?): String? {val firstLine = raw?.lineSequence()diff --git a/app/src/main/java/com/gotcha/data/ChatImporter.kt b/app/src/main/java/com/gotcha/data/ChatImporter.ktindex f38a8a6..5ac416d 100644--- a/app/src/main/java/com/gotcha/data/ChatImporter.kt+++ b/app/src/main/java/com/gotcha/data/ChatImporter.kt@@ -1,5 +1,6 @@package com.gotcha.data+import com.gotcha.agent.ChatTitleimport com.gotcha.agent.MessageKindimport com.gotcha.llm.ChatMessageimport com.gotcha.llm.DOCUMENTS_ONLY_PROMPT@@ -334,10 +335,6 @@ class ChatImporter(return if (prompt == DOCUMENT_ONLY_PROMPT || prompt == DOCUMENTS_ONLY_PROMPT) "" else prompt}- /** A screen capture the agent added as a user message; the transcript shows it as a capture, not a prompt. */- private fun isScreenCapture(content: String): Boolean =- content.startsWith("[Screen State]") || content.startsWith("Screen text:")-/** [session] with its system messages as notes, saying so in [notes] when there were any. */private fun asNotes(session: ChatSession, notes: MutableList<String>): ChatSession {val count = session.messages.count { it.role == "system" }@@ -378,8 +375,7 @@ class ChatImporter(/** The engine's own untitled fallback, so a title is still generated for it later. */private fun fallbackTitle(messages: List<ChatMessage>): String =- messages.firstOrNull { it.role == "user" }?.textContent?.take(30)?.takeIf { it.isNotBlank() }- ?: "Imported chat"+ ChatTitle.openingText(messages)?.take(ChatTitle.FALLBACK_CHARS) ?: "Imported chat"companion object {/** A backup with every image in it can be large, but it is read into memory whole. */diff --git a/app/src/main/java/com/gotcha/data/ChatMarkdown.kt b/app/src/main/java/com/gotcha/data/ChatMarkdown.ktindex 42722b3..a3eee9b 100644--- a/app/src/main/java/com/gotcha/data/ChatMarkdown.kt+++ b/app/src/main/java/com/gotcha/data/ChatMarkdown.kt@@ -31,6 +31,13 @@ internal fun documentPromptText(content: String): String? {return if (index >= 0) content.substring(0, index).trim() else null}+/**+ * A screen capture the agent added to the history as a user message. The chat+ * shows it as a capture, never as something the user said.+ */+internal fun isScreenCapture(content: String): Boolean =+ content.startsWith("[Screen State]") || content.startsWith("Screen text:")+/** How many `[Attached file: …]` sections a user message's text part carries. */internal fun countAttachedFiles(content: String): Int =content.split(ATTACHED_FILE_MARKER).size - 1diff --git a/app/src/test/java/com/gotcha/agent/AgentLoopTest.kt b/app/src/test/java/com/gotcha/agent/AgentLoopTest.ktindex 6fd8c0b..bbab9a9 100644--- a/app/src/test/java/com/gotcha/agent/AgentLoopTest.kt+++ b/app/src/test/java/com/gotcha/agent/AgentLoopTest.kt@@ -7,6 +7,7 @@ import com.gotcha.data.LlmProviderimport com.gotcha.data.Settingsimport com.gotcha.llm.ChatRequestimport com.gotcha.llm.LLMClient+import com.gotcha.llm.visionUserMessageimport com.gotcha.testsupport.FakeAndroidKeyStoreimport com.gotcha.testsupport.ShadowExternalStorageManagerimport com.gotcha.tools.AgentMode@@ -677,6 +678,31 @@ class AgentLoopTest {assertEquals("Wifi fix", engine.generatedTitle)}+ /**+ * An image sent with no text reaches the model as a single space (#95), so+ * there is nothing to title yet: no title request, a name that isn't blank,+ * and the first message with words in it titles the chat.+ */+ @Test+ fun `a chat that opened with an image alone is titled by its first words`() = runBlocking {+ engine.history += visionUserMessage("", "QUJD", "jpeg")+ enqueueToolCall("finish_task", """{"summary":"A red box."}""")++ engine.run(AgentMode.OPERATOR)+ engine.saveCurrentSession()++ assertEquals("only the model request, no title request", 1, server.requestCount)+ assertEquals(ChatTitle.IMAGE_CHAT, engine.currentTitle())++ addUserMessage("What colour is the box?")+ enqueueToolCall("finish_task", """{"summary":"Red."}""")+ enqueueTextReply("Box colour question")++ engine.run(AgentMode.OPERATOR)++ assertEquals("Box colour question", engine.generatedTitle)+ }+@Testfun `finish_task emits a succeeded run summary`() = runTest {enqueueToolCall("finish_task", """{"summary":"All done."}""")diff --git a/app/src/test/java/com/gotcha/agent/ChatTitleTest.kt b/app/src/test/java/com/gotcha/agent/ChatTitleTest.ktindex 13fd53f..93e5ff2 100644--- a/app/src/test/java/com/gotcha/agent/ChatTitleTest.kt+++ b/app/src/test/java/com/gotcha/agent/ChatTitleTest.kt@@ -1,5 +1,8 @@package com.gotcha.agent+import com.gotcha.llm.ChatMessage+import com.gotcha.llm.visionUserMessage+import kotlinx.serialization.json.JsonPrimitiveimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Test@@ -58,4 +61,32 @@ class ChatTitleTest {ChatTitle.sanitize("one two three four five six seven eight nine ten"))}++ // ---- the fallback, until a title is generated ----++ private fun user(text: String) = ChatMessage(role = "user", content = JsonPrimitive(text))++ @Test+ fun `a chat is named by the start of its first message`() {+ val messages = listOf(user("Find the best ramen in Tokyo tonight please"))+ assertEquals("Find the best ramen in Tokyo t", ChatTitle.fallback(messages))+ }++ /** #95 sends an image-only message as a single space, which used to name the chat " ". */+ @Test+ fun `a chat that opened with an image alone is never blank`() {+ val messages = listOf(visionUserMessage("", "QUJD", "jpeg"))+ assertNull(ChatTitle.openingText(messages))+ assertEquals(ChatTitle.IMAGE_CHAT, ChatTitle.fallback(messages))+ }++ @Test+ fun `the first message with words names the chat, not an image before it or a screen capture`() {+ val messages = listOf(+ visionUserMessage("", "QUJD", "jpeg"),+ user("[Screen State] Home screen, 12 icons"),+ user("What colour is the box?")+ )+ assertEquals("What colour is the box?", ChatTitle.fallback(messages))+ }}