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.

Merge development into fix/subagent-permission-prompts

Durganshu483357c78ed0
diff --cc app/src/main/java/com/gotcha/agent/ChatRunner.kt
index 569f90f,c311a7c..a9a23ff
--- a/app/src/main/java/com/gotcha/agent/ChatRunner.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatRunner.kt
@@@ -454,6 -459,47 +464,47 @@@ class ChatRunner(private val app: Appli
}
}
+ /**
+ * Starts the run of scheduled [task] (issue #132), with its prompt and
+ * [attachments] as the user's message: in a new chat, or at the end of the
+ * task's chat in same-chat mode, falling back to a new chat when that one
+ * is gone. Never runs alongside another run. The screen is left where it is:
+ * it only follows the run when the user is already looking at that chat.
+ */
+ internal suspend fun startScheduled(task: ScheduledTask, attachments: List<ComposerAttachment>): ScheduledStart {
+ startup.join()
+ if (isRunning || agentJob?.isActive == true) return ScheduledStart.Busy
+ // With no model the engine returns without a word, which would read as "Done".
+ // The chat screen can't send then either.
+ if (client == null) return ScheduledStart.Failed(app.getString(R.string.scheduled_task_ai_not_set_up))
+ val saved = task.chatSessionId?.takeIf { task.sameChat }?.let { historyRepository.loadSession(it) }
+ // Loading the chat suspended: a send may have started meanwhile.
+ if (isRunning || agentJob?.isActive == true) return ScheduledStart.Busy
+ // The alarm may have started the process: with no screen ever shown,
+ // nothing has said yet that Gotcha is out of sight.
- if ((app as? GotchaApp)?.hasStartedActivity == false) appInForeground = false
++ if ((app as? GotchaApp)?.hasStartedActivity == false) foreground.value = false
+ if (saved != null) {
+ // The run uses the task's mode, but the chat keeps its own: a task
+ // must not leave the user's chat switched to another mode.
+ val chatMode = saved.agentMode?.let { runCatching { AgentMode.valueOf(it) }.getOrNull() } ?: AgentMode.MONITOR
+ bindSaved(saved, chatMode, saved.displayMessages)
+ } else {
+ bindFresh(java.util.UUID.randomUUID().toString(), task.mode)
+ engine.sessionPersonaId = task.personaId
+ }
+ val runningId = engine.sessionId ?: return ScheduledStart.Failed("No chat to run in.")
+ scheduledTask = task
+ engine.scheduledRun = true
+ currentRunIsVoice = false
+ val msg = buildUserMessage(task.prompt, attachments)
+ engine.history += msg
+ appendTranscript(MessageKind.USER, userDisplayText(task.prompt, msg, attachments), attachments = attachments)
+ // Main.immediate: executeRun marks the run busy and starts the keep-alive
+ // service before this returns, while the alarm's grace period still holds.
+ agentJob = scope.launch { executeRun(task.mode, runningId) }
+ return ScheduledStart.Started(runningId)
+ }
+
/**
* Replaces the user message [targetId] in [view] with [text] and
* [attachments] (null keeps the original ones), drops everything after it