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(agent): tell the model to put commands in code blocks (#109)

Durganshub2dc8ffd557c
diff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
index ead4cb2..4ce99b1 100644
--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt
+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
@@ -1310,6 +1310,19 @@ class AgentEngine(
} else {
""
}
+ // A command the user runs by hand has to be copied exactly; the chat and the
+ // question dialog draw fenced blocks with a Copy button (issue #109), inline
+ // code gets none. Not in a voice call, where nothing is shown.
+ val commandsDirective = if (callMode) {
+ ""
+ } else {
+ "\n\nWhen the user has to run a command themselves (in Termux, a terminal, adb), " +
+ "put it in a fenced code block tagged with its shell (```bash), never inline in a " +
+ "sentence — the app shows each block with a Copy button that copies all of it, so " +
+ "put only the lines to paste inside, and separate commands that are run at " +
+ "different steps into separate blocks. " +
+ "The same goes for a command inside a question you ask with the question tool."
+ }
val core = when (agent) {
AgentMode.MONITOR -> monitorCore()
AgentMode.OPERATOR -> operatorCore()
@@ -1337,7 +1350,7 @@ class AgentEngine(
} +
"</system-reminder>"
}
- return core + serveDirective + languageDirective + personaDirective + styleDirective + reminder
+ return core + serveDirective + commandsDirective + languageDirective + personaDirective + styleDirective + reminder
}
/** The Monitor-mode core instruction block; split out so [agentInstructionText] stays readable. */
diff --git a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt b/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
index 12fc357..c6820c0 100644
--- a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
+++ b/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
@@ -32,11 +32,21 @@ internal object TermuxMessages {
fun permissionNeeded() = ToolResult.permissionNeeded(
ToolResult.TERMUX_ACCESS,
"Running commands in Termux needs Termux's \"Run commands\" permission, which has not been granted. " +
- "I have asked for it — please allow it, then in Termux add the line `allow-external-apps=true` to " +
- "`~/.termux/termux.properties` and restart Termux, and ask again. (If no permission dialog appeared, " +
- "Termux was installed after Gotcha — reinstall or update Gotcha so Android can grant it.)"
+ "I have asked for it — please allow it, then in Termux run these two lines, and ask again." +
+ externalAppsFix() +
+ "\n(If no permission dialog appeared, Termux was installed after Gotcha — reinstall or update " +
+ "Gotcha so Android can grant it.)"
)
+ /**
+ * The commands that set `allow-external-apps`, as a fenced block with the instruction to keep
+ * them in one when passing them on: the user types them into Termux, and a fenced block is
+ * what the chat shows with a Copy button (issue #109).
+ */
+ fun externalAppsFix(): String =
+ "\n\nThe user runs this in Termux:\n\n```bash\n${TermuxTool.SETUP_COMMANDS}\n```\n\n" +
+ "When you tell the user, give them exactly this fenced block, not the commands inline."
+
/**
* Seen on-device as `Not allowed to start service … app is in background`: from Android 12,
* an app in the background cannot start another app's foreground service, and Termux's
diff --git a/app/src/main/java/com/gotcha/tools/TermuxTool.kt b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
index f06f0b8..22fb02c 100644
--- a/app/src/main/java/com/gotcha/tools/TermuxTool.kt
+++ b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
@@ -254,7 +254,8 @@ class TermuxTool(
if (err != ERRNO_SUCCESS) {
return ToolResult.error(
"Termux refused or could not run the command (${TermuxMessages.errnoLabel(err)})" +
- if (errmsg.isEmpty()) "." else ": ${cap(errmsg)}"
+ (if (errmsg.isEmpty()) "." else ": ${cap(errmsg)}") +
+ if ("allow-external-apps" in errmsg) TermuxMessages.externalAppsFix() else ""
)
}
val exit = bundle.numeric(RESULT_EXIT_CODE) ?: -1
@@ -403,6 +404,15 @@ class TermuxTool(
*/
const val TERMUX_FDROID_URL = "https://f-droid.org/en/packages/com.termux/"
+ /**
+ * The two lines the user runs in Termux to let other apps send it commands: the setup
+ * screen's Copy button shares them, and the tool messages hand them to the model as a
+ * fenced block so the reply shows them with a Copy button too (issue #109).
+ */
+ const val SETUP_COMMANDS =
+ "echo 'allow-external-apps=true' >> ~/.termux/termux.properties\n" +
+ "termux-reload-settings"
+
/** An `allow-external-apps` failure returns within a second; this is a generous ceiling. */
private const val PROBE_TIMEOUT_SECONDS = 5
diff --git a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt b/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
index f38f5cb..16e69dc 100644
--- a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
@@ -1810,12 +1810,14 @@ object ToolDefinitions {
val question = tool(
"question",
"Ask the user for a decision or clarification instead of guessing their intent. " +
- "Offer concise options where you can.",
+ "Offer concise options where you can. The question is shown as Markdown under the " +
+ "title \"Gotcha has a question\": keep it short, and put any command the user must " +
+ "run in a fenced ```bash block so it gets a Copy button.",
schema {
putJsonObject("properties") {
putJsonObject("question") {
put("type", "string")
- put("description", "The question to ask the user.")
+ put("description", "The question to ask the user, as Markdown.")
}
putJsonObject("options") {
put("type", "array")
diff --git a/app/src/main/java/com/gotcha/ui/TermuxSetupScreen.kt b/app/src/main/java/com/gotcha/ui/TermuxSetupScreen.kt
index 9f12c63..6a3c21e 100644
--- a/app/src/main/java/com/gotcha/ui/TermuxSetupScreen.kt
+++ b/app/src/main/java/com/gotcha/ui/TermuxSetupScreen.kt
@@ -40,11 +40,6 @@ import com.gotcha.tools.ToolResult
import com.gotcha.ui.theme.GotchaMono
import kotlinx.coroutines.launch
-/** The two lines the user runs in Termux to enable external apps; also what the Copy button shares. */
-private const val TERMUX_SETUP_COMMANDS =
- "echo 'allow-external-apps=true' >> ~/.termux/termux.properties\n" +
- "termux-reload-settings"
-
/**
* Guided Termux setup: a live checklist of the four things that must be true for
* `run_termux_command` to work, each with the action to fix it. The cheap checks
@@ -202,7 +197,7 @@ fun TermuxSetupScreen(onBack: () -> Unit) {
modifier = Modifier.fillMaxWidth()
) {
Text(
- TERMUX_SETUP_COMMANDS,
+ TermuxTool.SETUP_COMMANDS,
style = MaterialTheme.typography.bodySmall.copy(fontFamily = GotchaMono),
modifier = Modifier.padding(12.dp)
)
@@ -211,7 +206,7 @@ fun TermuxSetupScreen(onBack: () -> Unit) {
OutlinedButton(
onClick = {
clipboard?.setPrimaryClip(
- ClipData.newPlainText("Termux setup", TERMUX_SETUP_COMMANDS)
+ ClipData.newPlainText("Termux setup", TermuxTool.SETUP_COMMANDS)
)
overlay.show("Copied to clipboard.")
},
diff --git a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
index 167fe8f..65c9346 100644
--- a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
+++ b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
@@ -137,6 +137,10 @@ class TermuxToolTest {
"must name the property the user has to set: ${result.message}",
result.message.contains("allow-external-apps")
)
+ assertTrue(
+ "the setup commands go to the model as a fenced block: ${result.message}",
+ result.message.contains("```bash\n${TermuxTool.SETUP_COMMANDS}\n```")
+ )
}
@Test
@@ -643,6 +647,10 @@ class TermuxToolTest {
assertTrue(result.message.contains("failed"))
assertTrue("the actionable part must survive", result.message.contains("allow-external-apps"))
assertTrue(result.message.contains("termux.properties"))
+ assertTrue(
+ "the fix is handed over as a fenced block, which the chat shows with a Copy button",
+ result.message.contains("```bash\n${TermuxTool.SETUP_COMMANDS}\n```")
+ )
}
@Test