Skip to content

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(agent): the ongoing notification names the chat by its title once it has one (#105)

Durganshu91fdf465e05f
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index cbe3163..4352d83 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -145,6 +145,15 @@ internal val ATTACHMENT_PLACEHOLDERS = setOf("(image attached)", "(document atta
* notification is switched on and Android allows it (issue #97); otherwise the
* opt-in buzz and chime are all there is.
*/
+/** [text] on one line, cut at a word to at most [max] characters, with "…" when cut. */
+internal fun shortTitle(text: String, max: Int = 40): String {
+ val line = text.trim().replace(Regex("\\s+"), " ")
+ if (line.length <= max) return line
+ val cut = line.take(max)
+ val atWord = cut.substringBeforeLast(' ').takeIf { it.length >= max / 2 } ?: cut
+ return atWord.trimEnd(',', '.', ';', ':', ' ') + "…"
+}
+
internal fun backgroundHintText(vibrate: Boolean, chime: Boolean, notify: Boolean = false): String {
val base = "Gotcha is working in the background. You can use another app while it works"
val signal = when {
@@ -468,6 +477,8 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
if (viewingEngineSession()) {
_uiState.update { it.copy(activity = activity) }
}
+ // Once per round: picks up the chat's title when the first save generates it.
+ _uiState.value.runningSessionId?.let { ChatRunService.update(runningChat(it)) }
}
override fun onTokenCount(totalTokens: Int) {
@@ -825,10 +836,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
// Issue #105: hold a foreground slot for the run, so Android keeps the
// process alive if the user leaves mid-task. Started here, while the user
// who just sent the message is still in Gotcha.
- ChatRunService.start(
- getApplication(),
- RunningChat(runningId, agentEngine.currentTitle().takeIf { mayNameChat(runningId) })
- )
+ ChatRunService.start(getApplication(), runningChat(runningId))
runHadError = false
var stopped = false
try {
@@ -882,6 +890,17 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
settings.notificationsMentionChats &&
!localNotificationStore.isChatSensitive(sessionId, agentEngine.sessionPersonaId)
+ /**
+ * The run in [sessionId] as its ongoing notification shows it: the chat's
+ * title once one is generated, until then the start of the first message,
+ * cut at a word.
+ */
+ private fun runningChat(sessionId: String): RunningChat {
+ val title = agentEngine.generatedTitle
+ ?: agentEngine.history.firstOrNull { it.role == "user" }?.textContent?.let(::shortTitle)
+ return RunningChat(sessionId, title.takeIf { mayNameChat(sessionId) })
+ }
+
private fun currentBackgroundHint(): String = backgroundHintText(
vibrate = settings.notifyVibrationEnabled,
chime = settings.notifyChimeEnabled,
diff --git a/app/src/main/java/com/gotcha/service/ChatRunService.kt b/app/src/main/java/com/gotcha/service/ChatRunService.kt
index b76820c..d40ba04 100644
--- a/app/src/main/java/com/gotcha/service/ChatRunService.kt
+++ b/app/src/main/java/com/gotcha/service/ChatRunService.kt
@@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
+import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
/** The chat run the ongoing notification is about. [chatTitle] null: the chat may not be named (issue #100). */
@@ -127,6 +128,14 @@ class ChatRunService : Service() {
}
}
+ /**
+ * Updates the notification of the run in progress, e.g. once its chat has a
+ * title. Never starts the service: a run that has none keeps going without.
+ */
+ fun update(chat: RunningChat) {
+ _running.update { current -> if (current?.sessionId == chat.sessionId) chat else current }
+ }
+
/** The run has ended: the service removes its notification and stops. */
fun stop() {
_running.value = null
@@ -134,7 +143,8 @@ class ChatRunService : Service() {
internal fun title(chat: RunningChat?): String {
val name = chat?.chatTitle?.takeIf { it.isNotBlank() } ?: return PUBLIC_TITLE
- return "Gotcha is working on “$name”…"
+ // A name already cut short with "…" doesn't need a second one.
+ return if (name.endsWith("…")) "Gotcha is working on “$name”" else "Gotcha is working on “$name”…"
}
internal fun buildNotification(context: Context, chat: RunningChat?): android.app.Notification {
diff --git a/app/src/test/java/com/gotcha/agent/ShortTitleTest.kt b/app/src/test/java/com/gotcha/agent/ShortTitleTest.kt
new file mode 100644
index 0000000..7401b94
--- /dev/null
+++ b/app/src/test/java/com/gotcha/agent/ShortTitleTest.kt
@@ -0,0 +1,31 @@
+package com.gotcha.agent
+
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+/** The placeholder chat name in the ongoing notification (issue #105). */
+class ShortTitleTest {
+
+ @Test
+ fun `a short message is kept whole`() {
+ assertEquals("Plan my trip", shortTitle(" Plan my trip "))
+ }
+
+ @Test
+ fun `a long message is cut at a word`() {
+ assertEquals(
+ "Find the 10 largest files in my…",
+ shortTitle("Find the 10 largest files in my Downloads folder and describe each one")
+ )
+ }
+
+ @Test
+ fun `line breaks become spaces`() {
+ assertEquals("First line second line", shortTitle("First line\nsecond line"))
+ }
+
+ @Test
+ fun `one long word is cut where it has to be`() {
+ assertEquals("a".repeat(40) + "…", shortTitle("a".repeat(60)))
+ }
+}
diff --git a/app/src/test/java/com/gotcha/service/ChatRunServiceTest.kt b/app/src/test/java/com/gotcha/service/ChatRunServiceTest.kt
index 84e73ca..686f2ab 100644
--- a/app/src/test/java/com/gotcha/service/ChatRunServiceTest.kt
+++ b/app/src/test/java/com/gotcha/service/ChatRunServiceTest.kt
@@ -135,4 +135,36 @@ class ChatRunServiceTest {
request.await()
assertEquals(ChatRunService.ACTION_STOP_RUN, shadowOf(stopAction.actionIntent).savedIntent.action)
}
+
+ @Test
+ fun `update changes the notification of the run in progress`() {
+ val controller = startedService()
+
+ ChatRunService.update(chat.copy(chatTitle = "Trip to Lisbon"))
+ ShadowLooper.idleMainLooper()
+
+ assertEquals(
+ "Gotcha is working on “Trip to Lisbon”…",
+ controller.notification().extras.getString(NotificationCompat.EXTRA_TITLE)
+ )
+ }
+
+ @Test
+ fun `update never starts a run, nor touches another chat's`() {
+ ChatRunService.update(chat)
+ assertNull(ChatRunService.running.value)
+ assertNull(shadowOf(application).nextStartedService)
+
+ ChatRunService.start(application, chat)
+ ChatRunService.update(RunningChat("other", "Other"))
+ assertEquals(chat, ChatRunService.running.value)
+ }
+
+ @Test
+ fun `a name cut short keeps a single ellipsis`() {
+ assertEquals(
+ "Gotcha is working on “Find the largest files in my…”",
+ ChatRunService.title(chat.copy(chatTitle = "Find the largest files in my…"))
+ )
+ }
}