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.

feat: implement cancellable sleep tool and add to agent system prompts

Rishabh Bajpai18d2533916cc
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index 28bfd5a..3e3d01f 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -895,13 +895,16 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
"You can inspect, read, and query the device, but you CANNOT create, modify, or " +
"delete anything. You control the device only through the provided tools; never " +
"invent tool names or capabilities. If a tool reports a missing permission, " +
- "explain what to grant and ask again. Keep replies short and conversational."
+ "explain what to grant and ask again. Use the sleep tool to pause and wait " +
+ "between operations. Keep replies short and conversational."
AgentMode.OPERATOR ->
"You are Operator, an AI assistant running on the user's Android phone. " +
"You can inspect, read, query, create, modify, and delete on the device. " +
"You control the device only through the provided tools; never invent tool " +
"names or capabilities. If a tool reports a missing permission, explain what " +
"to grant and ask again. After changing device state, confirm what was done. " +
+ "Use the sleep tool to pause and wait between operations (e.g. wait for " +
+ "a recording to finish before reading it). " +
"Keep replies short and conversational. Be careful with destructive actions.\n" +
"If the accessibility service is enabled, you have the ability to control any app on the device.\n" +
"When using uninstall_app: after calling it, the system will open a dialog. " +
diff --git a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt b/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
index 85a137c..54b37d4 100644
--- a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
@@ -1039,6 +1039,24 @@ object ToolDefinitions {
}
)
+ val sleep = tool(
+ "sleep",
+ "Pause execution for a given duration, then automatically resume. " +
+ "Use this to wait before the next action — e.g. let a timer finish, " +
+ "wait for a download, or delay between operations. " +
+ "Can be cancelled anytime via the Stop button. " +
+ "Available to both Monitor and Operator modes.",
+ schema {
+ putJsonObject("properties") {
+ putJsonObject("duration_seconds") {
+ put("type", "integer")
+ put("description", "How long to sleep, in seconds (1-86400).")
+ }
+ }
+ putJsonArray("required") { add("duration_seconds") }
+ }
+ )
+
val glob = tool(
"glob",
"Find files matching a glob pattern within an allowed directory. " +
@@ -1421,6 +1439,8 @@ object ToolDefinitions {
getAudioRecordingStatus, pauseAudioRecording, resumeAudioRecording,
// User interaction
question,
+ // Sleep / delay utility (available to both agents)
+ sleep,
// Task tracking
todowrite,
// Surgical file editing (Operator only)
diff --git a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt b/app/src/main/java/com/gotcha/tools/ToolExecutor.kt
index b2886e3..9393571 100644
--- a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolExecutor.kt
@@ -2,7 +2,9 @@ package com.gotcha.tools
import android.content.Context
import android.util.Log
+import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
@@ -69,6 +71,8 @@ class ToolExecutor(context: Context) {
}
val result = try {
withContext(Dispatchers.IO) { dispatch(name, args) }
+ } catch (e: CancellationException) {
+ throw e
} catch (e: Exception) {
ToolResult.error("Tool '$name' failed: ${e.message}")
}
@@ -295,6 +299,15 @@ class ToolExecutor(context: Context) {
"todowrite" -> todoTool.todowrite(
parseTodoItems(args["items"]) ?: return missing("items")
)
+ "sleep" -> {
+ val secs = args.requireInt("duration_seconds")?.coerceIn(1, 86400)
+ ?: return missing("duration_seconds")
+ for (remaining in secs downTo 1) {
+ Log.d(TAG, "sleep: ${remaining}s remaining")
+ delay(1000L)
+ }
+ ToolResult.ok("Slept for $secs seconds.")
+ }
"read_image" -> fileTool.readFile(args.requireString("path") ?: return missing("path"))
"glob" -> globTool.glob(
path = args.requireString("path") ?: return missing("path"),
diff --git a/app/src/main/java/com/gotcha/tools/ToolRegistry.kt b/app/src/main/java/com/gotcha/tools/ToolRegistry.kt
index 5a794d9..d4ad1ab 100644
--- a/app/src/main/java/com/gotcha/tools/ToolRegistry.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolRegistry.kt
@@ -59,6 +59,7 @@ object ToolRegistry {
"todowrite", "list_alarms", "list_timers",
// User interaction
"question",
+ "sleep",
// Web
"websearch", "webfetch",
// Clipboard (read-only)