feat(agent): first-run setup in the Agent tab, a harness choice with Codex, and no permission prompts

This commit is contained in:
Kurt Himebauch committed 2026-11-10 00:40:35 -05:00
1 parent 8337166cc9
commit 8ffac40b4f
12 files changed
+589 -139

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
#!/bin/sh
# Opens a URL in Android's browser on the display the agent is being used on ($DD_BROWSER_DISPLAY).
url=$1
[ -n "$url" ] || exit 1
serial=${ADB_SERIAL:-127.0.0.1:5555}
adb connect "$serial" >/dev/null 2>&1
quoted=$(printf "%s" "$url" | sed "s/'/'\\''/g")
adb -s "$serial" shell "am start --display ${DD_BROWSER_DISPLAY:-0} -a android.intent.action.VIEW -d '$quoted'" >/dev/null 2>&1 ||
echo "Open this on any device: $url" >&2
+24
View File
@@ -0,0 +1,24 @@
#!/bin/bash
# GitHub sign-in through Android's browser, with the one-time code typed in for the person.
serial=${ADB_SERIAL:-127.0.0.1:5555}
display=${DD_BROWSER_DISPLAY:-0}
adb connect "$serial" >/dev/null 2>&1
coproc GH { BROWSER=true gh auth login --hostname github.com --git-protocol https --web 2>&1; }
typed=
while IFS= read -r line <&"${GH[0]}"; do
printf '%s\n' "$line"
code=$(grep -oE '[A-Z0-9]{4}-[A-Z0-9]{4}' <<<"$line" | head -1)
if [ -n "$code" ] && [ -z "$typed" ]; then
typed=1
echo >&"${GH[1]}"
adb -s "$serial" shell "am start --display $display -a android.intent.action.VIEW -d https://github.com/login/device" >/dev/null 2>&1
# Chrome drops characters from `input text` and from fast key events on this page.
keys=
for ((i = 0; i < ${#code}; i++)); do
c=${code:i:1}
[ "$c" = - ] || keys+="input -d $display keyevent KEYCODE_$c; sleep 0.4; "
done
(sleep 7; adb -s "$serial" shell "$keys" >/dev/null 2>&1) &
fi
done
wait "$GH_PID"
@@ -0,0 +1,44 @@
package com.droiddeck.launcher.agent
import android.content.Context
import com.droiddeck.launcher.core.WirelessAdbFix
import com.droiddeck.launcher.runtime.LinuxRuntime
import java.io.File
/**
* Loopback adb for the agent, so droiddeckctl in the Linux userland reaches the app. It reuses
* the Wireless debugging pairing DroidDeck already made (for the phantom process fix): the paired
* key goes to the guest as an adb vendor key, and the serial is the paired TLS connect port.
* Without a pairing it falls back to adbd on 127.0.0.1:5555, which some devices and `adb tcpip`
* leave listening.
*/
object AgentAdb {
data class Link(val serial: String, val vendorKeys: String, val paired: Boolean)
private const val LEGACY = "127.0.0.1:5555"
private const val VENDOR_KEY = "/root/.android/droiddeck-paired.adbkey"
/** The display the browser opens on for sign-ins: the one the agent is being used on. */
@Volatile var browserDisplay = 0
/** Blocks while it looks up the connect port when none is saved; call it off the main thread. */
fun prepare(app: Context): Link {
val key = File(app.noBackupFilesDir, "wireless-adb-key.pem")
val address = WirelessAdbFix.savedConnectionAddress(app)
if (address == null || !key.isFile) return Link(LEGACY, VENDOR_KEY, false)
val guestKey = File(LinuxRuntime.rootDir(app), VENDOR_KEY.removePrefix("/"))
runCatching {
if (!guestKey.isFile || !guestKey.readBytes().contentEquals(key.readBytes())) {
guestKey.parentFile?.mkdirs()
guestKey.writeBytes(key.readBytes())
guestKey.setReadable(false, false)
guestKey.setReadable(true, true)
}
}
val host = address.substringBeforeLast(':')
val port = address.substringAfterLast(':').toIntOrNull()
?: WirelessAdbFix.findConnectPort(app, host.trim('[', ']'))
?: return Link(LEGACY, VENDOR_KEY, false)
return Link("$host:$port", VENDOR_KEY, true)
}
}
@@ -0,0 +1,130 @@
package com.droiddeck.launcher.agent
import androidx.annotation.VisibleForTesting
import org.json.JSONArray
import org.json.JSONObject
/** One coding agent CLI in the Linux userland, run one process per turn. */
interface AgentHarness {
val id: String
val name: String
/** Absolute guest path of the CLI. */
val binary: String
/** A guest shell script that installs the CLI. */
val install: String
/** A guest command whose exit status is 0 when the CLI is signed in. */
val signedIn: List<String>
/** A guest command that signs in through $BROWSER. */
val signIn: List<String>
/** The guest command for one turn; [conversation] is null for the first one. */
fun turn(message: String, conversation: String?): List<String>
/** One line of the CLI's output, or null when it carries nothing to show. */
fun parse(line: String): AgentEvent?
companion object {
val all: List<AgentHarness> = listOf(ClaudeCode, Codex)
fun byId(id: String?): AgentHarness = all.firstOrNull { it.id == id } ?: ClaudeCode
}
}
data class AgentEvent(val conversation: String?, val entries: List<AgentHost.Entry>)
/** Claude Code: `claude -p` with stream-json output, resumed by session id. */
object ClaudeCode : AgentHarness {
override val id = "claude"
override val name = "Claude Code"
override val binary = "/root/.local/bin/claude"
override val install = "curl -fsSL https://claude.ai/install.sh | bash"
override val signedIn = listOf("sh", "-c", "$binary auth status | grep -q '\"loggedIn\": true'")
override val signIn = listOf(binary, "auth", "login", "--claudeai")
// DroidDeck runs the agent unattended on the user's own device; permission prompts have no one to answer them.
override fun turn(message: String, conversation: String?) = buildList {
addAll(listOf(binary, "-p", message, "--output-format", "stream-json", "--verbose", "--permission-mode", "bypassPermissions"))
if (conversation != null) addAll(listOf("--resume", conversation))
}
override fun parse(line: String): AgentEvent? {
val o = runCatching { JSONObject(line) }.getOrNull() ?: return null
val conversation = o.optString("session_id").takeIf { it.isNotEmpty() }
val entries = when (o.optString("type")) {
"assistant" -> {
val content = o.optJSONObject("message")?.optJSONArray("content") ?: JSONArray()
(0 until content.length()).mapNotNull { i ->
val block = content.optJSONObject(i) ?: return@mapNotNull null
when (block.optString("type")) {
"text" -> block.optString("text").trim().takeIf { it.isNotEmpty() }?.let { AgentHost.Entry.Said(it) }
"tool_use" -> AgentHost.Entry.Used(block.optString("name"), detail(block.optJSONObject("input")))
else -> null
}
}
}
"result" -> listOf(AgentHost.Entry.Finished(
if (o.optBoolean("is_error")) o.optString("result").ifEmpty { o.optString("subtype") } else null,
))
else -> emptyList()
}
return if (conversation == null && entries.isEmpty()) null else AgentEvent(conversation, entries)
}
private fun detail(input: JSONObject?): String {
input ?: return ""
for (key in listOf("command", "file_path", "pattern", "url", "description")) {
input.optString(key).takeIf { it.isNotEmpty() }?.let { return firstLine(it) }
}
return ""
}
}
/** OpenAI Codex: `codex exec --json`, resumed by thread id. */
object Codex : AgentHarness {
override val id = "codex"
override val name = "Codex"
override val binary = "/root/.local/bin/codex"
override val install = """
set -e
url=${'$'}(curl -fsSL https://api.github.com/repos/openai/codex/releases/latest | grep -o 'https://[^"]*codex-aarch64-unknown-linux-musl.tar.gz' | head -1)
mkdir -p /root/.local/bin
curl -fsSL "${'$'}url" | tar -xz -C /tmp
install -m755 /tmp/codex-aarch64-unknown-linux-musl $binary
""".trimIndent()
override val signedIn = listOf(binary, "login", "status")
override val signIn = listOf(binary, "login")
// Codex's own sandbox needs Landlock and seccomp, which proot does not provide.
override fun turn(message: String, conversation: String?) = buildList {
addAll(listOf(binary, "exec"))
if (conversation != null) addAll(listOf("resume", conversation))
addAll(listOf("--json", "--dangerously-bypass-approvals-and-sandbox", "--skip-git-repo-check", message))
}
override fun parse(line: String): AgentEvent? {
val o = runCatching { JSONObject(line) }.getOrNull() ?: return null
val item = o.optJSONObject("item")
val entries = when (o.optString("type")) {
"thread.started" -> return AgentEvent(o.optString("thread_id").takeIf { it.isNotEmpty() }, emptyList())
"item.started" -> when (item?.optString("type")) {
"command_execution" -> listOf(AgentHost.Entry.Used("Shell", firstLine(item.optString("command"))))
else -> emptyList()
}
"item.completed" -> when (item?.optString("type")) {
"agent_message" -> item.optString("text").trim().takeIf { it.isNotEmpty() }?.let { listOf(AgentHost.Entry.Said(it)) }.orEmpty()
"file_change" -> listOf(AgentHost.Entry.Used("Edit", changedFiles(item)))
else -> emptyList()
}
"turn.completed" -> listOf(AgentHost.Entry.Finished(null))
"turn.failed" -> listOf(AgentHost.Entry.Finished(o.optJSONObject("error")?.optString("message") ?: "turn failed"))
"error" -> listOf(AgentHost.Entry.Finished(o.optString("message").ifEmpty { "error" }))
else -> emptyList()
}
return if (entries.isEmpty()) null else AgentEvent(null, entries)
}
private fun changedFiles(item: JSONObject): String {
val changes = item.optJSONArray("changes") ?: return ""
return (0 until changes.length()).mapNotNull { changes.optJSONObject(it)?.optString("path") }.joinToString(", ").take(160)
}
}
@VisibleForTesting
internal fun firstLine(text: String) = text.lineSequence().first().take(160)
@@ -12,16 +12,14 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.droiddeck.launcher.runtime.LinuxRuntime
import com.droiddeck.launcher.session.SessionPrefs
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.io.Writer
import java.nio.file.Files
/**
* The launcher's agent: Claude Code from the Linux userland, run headless with stream-json on
* stdin and stdout, outside any game or desktop session. The conversation id and transcript are
* kept on disk, so a restart (a Lab build, a crash) continues the same conversation.
* The launcher's agent: a coding agent CLI ([AgentHarness]) from the Linux userland, run headless
* one process per turn, outside any game or desktop session. The conversation id and transcript
* are kept on disk, so a restart (a Lab build, a crash) continues the same conversation.
*/
object AgentHost {
sealed interface Entry {
@@ -34,7 +32,6 @@ object AgentHost {
enum class State { MISSING, IDLE, WORKING }
private const val TAG = "AgentHost"
private const val CLAUDE = "/root/.local/bin/claude"
private const val WORKSPACE = "/root/DroidDeck"
val entries = mutableStateListOf<Entry>()
@@ -42,24 +39,29 @@ object AgentHost {
private set
var installed by mutableStateOf(false)
private set
/** The Linux runtime is there, so the agent can be set up. */
var available by mutableStateOf(false)
private set
var harness by mutableStateOf<AgentHarness>(ClaudeCode)
private set
/** A reply finished since the conversation was last on screen. */
var unseen by mutableStateOf(false)
private set
var failed by mutableStateOf(false)
private set
private val main = Handler(Looper.getMainLooper())
private var process: Process? = null
private var loaded = false
fun seen() {
unseen = false
}
private val main = Handler(Looper.getMainLooper())
private var process: Process? = null
private var input: Writer? = null
private var loaded = false
fun refresh(context: Context) {
val claude = File(LinuxRuntime.rootDir(context), CLAUDE.removePrefix("/"))
installed = claude.exists() || Files.isSymbolicLink(claude.toPath())
harness = AgentHarness.byId(prefs(context).getString(HARNESS_KEY, null))
available = LinuxRuntime.isInstalled(context)
installed = guestFileExists(context, harness.binary)
if (!installed) state = State.MISSING else if (state == State.MISSING) state = State.IDLE
if (!loaded) {
loaded = true
@@ -67,97 +69,106 @@ object AgentHost {
}
}
fun choose(context: Context, choice: AgentHarness) {
if (choice == harness || state == State.WORKING) return
prefs(context).edit().putString(HARNESS_KEY, choice.id).remove(CONVERSATION_KEY).apply()
refresh(context)
}
fun send(context: Context, text: String) {
val app = context.applicationContext
val message = text.trim()
if (message.isEmpty() || state != State.IDLE) return
val writer = input?.takeIf { process?.isAlive == true } ?: start(app) ?: return
record(app, Entry.Asked(message))
state = State.WORKING
AgentHostService.start(app)
val current = harness
val conversation = prefs(app).getString(CONVERSATION_KEY, null)
val workdir = workspace(app)
Thread({
runCatching {
writer.write(userMessage(message))
writer.write("\n")
writer.flush()
}.onFailure { e ->
Log.w(TAG, "could not reach the agent", e)
main.post { finish(app, e.message ?: "agent stopped") }
val child = try {
guest(app, current.turn(message, conversation), workdir).start()
} catch (e: Exception) {
Log.w(TAG, "could not start ${current.name}", e)
main.post { finish(app, e.message ?: "could not start") }
return@Thread
}
}, "agent-send").start()
main.post { process = child }
child.outputStream.close()
var ended = false
runCatching {
child.inputStream.bufferedReader().forEachLine { line ->
val event = current.parse(line) ?: return@forEachLine
if (event.entries.any { it is Entry.Finished }) ended = true
main.post { apply(app, child, event) }
}
}
val status = runCatching { child.waitFor() }.getOrDefault(-1)
main.post {
if (process === child) {
process = null
if (state == State.WORKING && !ended) finish(app, "${current.name} stopped (exit $status)")
}
}
}, "agent-output").start()
}
fun stop() {
process?.destroy()
process = null
input = null
}
fun forget(context: Context) {
stop()
entries.clear()
transcript(context).delete()
prefs(context).edit().remove(SESSION_KEY).apply()
prefs(context).edit().remove(CONVERSATION_KEY).apply()
state = if (installed) State.IDLE else State.MISSING
}
private fun start(app: Context): Writer? {
val workspace = if (File(LinuxRuntime.rootDir(app), WORKSPACE.removePrefix("/")).isDirectory) WORKSPACE else "/root"
val resume = prefs(app).getString(SESSION_KEY, null)
val claude = buildList {
addAll(listOf(CLAUDE, "-p", "--input-format", "stream-json", "--output-format", "stream-json", "--verbose"))
if (resume != null) addAll(listOf("--resume", resume))
}
val guest = listOf(
"/usr/bin/env", "HOME=/root", "PATH=/root/.local/bin:/usr/local/bin:/usr/bin:/bin",
"ADB_SERIAL=127.0.0.1:5555", "BROWSER=/root/.local/bin/android-open",
// A fresh adb server lists the device's own adbd as emulator-5554; droiddeckctl needs the serial connected.
"/bin/sh", "-c", "adb connect \"\$ADB_SERIAL\" >/dev/null 2>&1; cd \"$1\" && shift && exec \"$@\"", "sh", workspace,
) + claude
val command = LinuxRuntime.command(app, null, null, Environment.getExternalStorageDirectory(), guest)
val builder = ProcessBuilder(command).redirectErrorStream(true)
builder.environment().apply {
remove("LD_PRELOAD"); remove("LD_AUDIT"); remove("LD_LIBRARY_PATH")
put("PROOT_LOADER", LinuxRuntime.prootLoader(app).path)
put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(app).path)
if (SessionPrefs.prootNoSeccomp(app)) put("PROOT_NO_SECCOMP", "1")
LinuxRuntime.prootLibraryPath(app).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) }
}
val child = try {
builder.start()
} catch (e: Exception) {
Log.w(TAG, "could not start the agent", e)
record(app, Entry.Finished(e.message ?: "could not start"))
return null
}
process = child
val writer = child.outputStream.bufferedWriter()
input = writer
Thread({
runCatching {
child.inputStream.bufferedReader().forEachLine { line ->
val event = parse(line) ?: return@forEachLine
main.post { apply(app, child, event) }
}
/**
* A process for [command] in the Linux userland, run the way the store helpers run theirs:
* proot over the installed rootfs, without a session. Loopback adb is connected first so
* droiddeckctl reaches the app.
*/
fun guest(app: Context, command: List<String>, workdir: String = "/root"): ProcessBuilder {
val adb = AgentAdb.prepare(app)
val env = listOf(
"/usr/bin/env", "HOME=/root", "PATH=/root/.local/bin:/usr/local/bin:/usr/bin:/bin", "LANG=C.UTF-8",
"ADB_SERIAL=${adb.serial}", "ADB_VENDOR_KEYS=${adb.vendorKeys}",
"BROWSER=/root/.local/bin/android-open", "DD_BROWSER_DISPLAY=${AgentAdb.browserDisplay}",
)
val shell = listOf(
"/bin/sh", "-c",
"adb connect \"\$ADB_SERIAL\" >/dev/null 2>&1; cd \"$1\" 2>/dev/null; shift; exec \"$@\"", "sh", workdir,
)
val proot = LinuxRuntime.command(app, null, null, Environment.getExternalStorageDirectory(), env + shell + command)
return ProcessBuilder(proot).redirectErrorStream(true).apply {
environment().apply {
remove("LD_PRELOAD"); remove("LD_AUDIT"); remove("LD_LIBRARY_PATH")
put("PROOT_LOADER", LinuxRuntime.prootLoader(app).path)
put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(app).path)
if (SessionPrefs.prootNoSeccomp(app)) put("PROOT_NO_SECCOMP", "1")
LinuxRuntime.prootLibraryPath(app).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) }
}
main.post {
if (process === child) {
process = null
input = null
if (state == State.WORKING) finish(app, "agent stopped")
}
}
}, "agent-output").start()
return writer
}
}
private fun apply(app: Context, child: Process, event: Event) {
fun guestFileExists(context: Context, path: String): Boolean {
val file = File(LinuxRuntime.rootDir(context), path.removePrefix("/"))
return file.exists() || Files.isSymbolicLink(file.toPath())
}
private fun workspace(app: Context) = if (guestFileExists(app, WORKSPACE)) WORKSPACE else "/root"
private fun apply(app: Context, child: Process, event: AgentEvent) {
if (process !== child) return
event.session?.let { prefs(app).edit().putString(SESSION_KEY, it).apply() }
event.conversation?.let { prefs(app).edit().putString(CONVERSATION_KEY, it).apply() }
event.entries.forEach { if (it is Entry.Finished) finish(app, it.error) else record(app, it) }
}
private fun finish(app: Context, error: String?) {
if (state != State.WORKING) return
record(app, Entry.Finished(error))
state = State.IDLE
unseen = true
@@ -170,54 +181,13 @@ object AgentHost {
runCatching { transcript(app).apply { parentFile?.mkdirs() }.appendText(encode(entry) + "\n") }
}
private const val SESSION_KEY = "session_id"
private const val CONVERSATION_KEY = "session_id"
private const val HARNESS_KEY = "harness"
private fun prefs(context: Context) = context.getSharedPreferences("agent", Context.MODE_PRIVATE)
internal fun prefs(context: Context) = context.getSharedPreferences("agent", Context.MODE_PRIVATE)
private fun transcript(context: Context) = File(context.filesDir, "agent/transcript.jsonl")
@VisibleForTesting
internal data class Event(val session: String?, val entries: List<Entry>)
@VisibleForTesting
internal fun userMessage(text: String): String = JSONObject()
.put("type", "user")
.put("message", JSONObject().put("role", "user").put("content", JSONArray().put(JSONObject().put("type", "text").put("text", text))))
.toString()
/** One stream-json line from Claude Code, or null when it carries nothing to show. */
@VisibleForTesting
internal fun parse(line: String): Event? {
val o = runCatching { JSONObject(line) }.getOrNull() ?: return null
val session = o.optString("session_id").takeIf { it.isNotEmpty() }
val entries = when (o.optString("type")) {
"assistant" -> {
val content = o.optJSONObject("message")?.optJSONArray("content") ?: JSONArray()
(0 until content.length()).mapNotNull { i ->
val block = content.optJSONObject(i) ?: return@mapNotNull null
when (block.optString("type")) {
"text" -> block.optString("text").trim().takeIf { it.isNotEmpty() }?.let { Entry.Said(it) }
"tool_use" -> Entry.Used(block.optString("name"), toolDetail(block.optJSONObject("input")))
else -> null
}
}
}
"result" -> listOf(Entry.Finished(
if (o.optBoolean("is_error")) o.optString("result").ifEmpty { o.optString("subtype") } else null,
))
else -> emptyList()
}
return if (session == null && entries.isEmpty()) null else Event(session, entries)
}
private fun toolDetail(input: JSONObject?): String {
input ?: return ""
for (key in listOf("command", "file_path", "pattern", "url", "description")) {
input.optString(key).takeIf { it.isNotEmpty() }?.let { return it.lineSequence().first().take(160) }
}
return ""
}
@VisibleForTesting
internal fun encode(entry: Entry): String = when (entry) {
is Entry.Asked -> JSONObject().put("asked", entry.text)
@@ -0,0 +1,127 @@
package com.droiddeck.launcher.agent
import android.content.Context
import android.os.Handler
import android.os.Looper
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.droiddeck.launcher.runtime.LinuxRuntime
import java.io.File
import java.util.concurrent.TimeUnit
/**
* First-run setup for the agent, as steps with a check and an action each. The first three are
* needed to talk to the agent; GitHub and the build tools are only needed to open pull requests
* and to make Lab builds on the device.
*/
object AgentSetup {
enum class Step(val required: Boolean) { CONNECTION(true), INSTALL(true), SIGN_IN(true), GITHUB(false), BUILD_TOOLS(false) }
enum class Status { UNKNOWN, DONE, TODO, WORKING, FAILED }
val status = mutableStateMapOf<Step, Status>()
/** The last line of output from the step that is running or just failed. */
val detail = mutableStateMapOf<Step, String>()
var runtimeInstalled by mutableStateOf(true)
private set
val ready: Boolean get() = Step.entries.filter { it.required }.all { status[it] == Status.DONE }
/** Set once the required steps have all passed, so the chat opens without waiting for the checks. */
fun completed(context: Context) = AgentHost.prefs(context).getBoolean(COMPLETED_KEY, false)
private const val COMPLETED_KEY = "setup_completed"
private val main = Handler(Looper.getMainLooper())
private const val REPO = "https://github.com/Droid-Deck/DroidDeck.git"
private const val CHECKOUT = "/root/DroidDeck"
private val HELPERS = listOf("android-open", "dd-gh-login")
fun check(context: Context) {
val app = context.applicationContext
runtimeInstalled = LinuxRuntime.isInstalled(app)
if (!runtimeInstalled) return
AgentHost.refresh(app)
val harness = AgentHost.harness
for (step in Step.entries) if (status[step] != Status.WORKING) {
when (step) {
Step.INSTALL -> set(step, if (AgentHost.guestFileExists(app, harness.binary)) Status.DONE else Status.TODO)
Step.BUILD_TOOLS -> set(step, if (AgentHost.guestFileExists(app, "/root/aapt2/aapt2") &&
AgentHost.guestFileExists(app, "$CHECKOUT/.git")) Status.DONE else Status.TODO)
Step.CONNECTION -> probe(app, step, listOf("sh", "-c", "adb -s \"\$ADB_SERIAL\" get-state | grep -q device"))
Step.SIGN_IN -> if (AgentHost.guestFileExists(app, harness.binary)) probe(app, step, harness.signedIn) else set(step, Status.TODO)
Step.GITHUB -> probe(app, step, listOf("gh", "auth", "status"))
}
}
}
/** Runs [step]'s action. [CONNECTION] is the app's own pairing flow, so the caller opens it. */
fun run(context: Context, step: Step) {
val app = context.applicationContext
val harness = AgentHost.harness
val command = when (step) {
Step.CONNECTION -> return
Step.INSTALL -> {
installHelpers(app)
listOf("sh", "-c", "set -e; export DEBIAN_FRONTEND=noninteractive; " +
"apt-get install -y --no-install-recommends git gh adb curl ca-certificates python3; ${harness.install}")
}
Step.SIGN_IN -> harness.signIn
Step.GITHUB -> listOf("dd-gh-login")
Step.BUILD_TOOLS -> listOf("sh", "-c", "set -e; [ -d $CHECKOUT/.git ] || git clone --filter=blob:none $REPO $CHECKOUT; " +
"cd $CHECKOUT; [ -x tools/agent/setup ] || { echo 'This DroidDeck version has no tools/agent/setup yet'; exit 1; }; tools/agent/setup")
}
set(step, Status.WORKING)
detail.remove(step)
Thread({
val result = runCatching {
val child = AgentHost.guest(app, command).start()
child.outputStream.close()
child.inputStream.bufferedReader().forEachLine { line ->
val text = line.trim()
if (text.isNotEmpty()) main.post { detail[step] = text.take(160) }
}
child.waitFor()
}
main.post {
if (result.getOrNull() == 0) {
detail.remove(step)
status[step] = Status.UNKNOWN
check(app)
} else {
status[step] = Status.FAILED
if (result.isFailure) detail[step] = result.exceptionOrNull()?.message ?: "failed"
}
}
}, "agent-setup-${step.name.lowercase()}").start()
}
private fun probe(app: Context, step: Step, command: List<String>) {
Thread({
val ok = runCatching {
val child = AgentHost.guest(app, command).start()
child.outputStream.close()
child.inputStream.readBytes()
child.waitFor(60, TimeUnit.SECONDS) && child.exitValue() == 0
}.getOrDefault(false)
main.post {
if (status[step] != Status.WORKING) status[step] = if (ok) Status.DONE else Status.TODO
if (ready) AgentHost.prefs(app).edit().putBoolean(COMPLETED_KEY, true).apply()
}
}, "agent-check-${step.name.lowercase()}").start()
}
private fun set(step: Step, value: Status) {
if (Looper.myLooper() == Looper.getMainLooper()) status[step] = value else main.post { status[step] = value }
}
private fun installHelpers(app: Context) {
val bin = File(LinuxRuntime.rootDir(app), "root/.local/bin").apply { mkdirs() }
for (name in HELPERS) {
val target = File(bin, name)
app.assets.open("agent/$name").use { input -> target.outputStream().use { input.copyTo(it) } }
target.setExecutable(true, false)
}
}
}
@@ -18,6 +18,7 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.Send
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
@@ -58,8 +59,11 @@ internal fun AgentPage(modifier: Modifier, inputModifier: Modifier = Modifier, v
val context = LocalContext.current
remember { AgentHost.refresh(context) }
LaunchedEffect(AgentHost.unseen, visible) { if (visible) AgentHost.seen() }
if (AgentHost.state == AgentHost.State.MISSING) {
Column(modifier) { Note(stringResource(R.string.agent_missing)) }
remember { com.droiddeck.launcher.agent.AgentSetup.check(context) }
var setup by rememberSaveable { mutableStateOf(false) }
if (setup || AgentHost.state == AgentHost.State.MISSING ||
!(com.droiddeck.launcher.agent.AgentSetup.ready || com.droiddeck.launcher.agent.AgentSetup.completed(context))) {
AgentSetupView(modifier, onDone = { setup = false })
return
}
val colors = MaterialTheme.colorScheme
@@ -88,6 +92,9 @@ internal fun AgentPage(modifier: Modifier, inputModifier: Modifier = Modifier, v
IconButton(onClick = { AgentHost.forget(context) }, enabled = !working) {
Icon(Icons.Outlined.Add, stringResource(R.string.agent_new))
}
IconButton(onClick = { setup = true }, enabled = !working) {
Icon(androidx.compose.material.icons.Icons.Outlined.Settings, stringResource(R.string.agent_setup_title))
}
OutlinedTextField(
draft, { draft = it }, Modifier.weight(1f).then(inputModifier),
placeholder = { Text(stringResource(R.string.agent_hint)) },
@@ -0,0 +1,102 @@
package com.droiddeck.launcher.ui
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.droiddeck.launcher.PairingGuideActivity
import com.droiddeck.launcher.R
import com.droiddeck.launcher.agent.AgentHarness
import com.droiddeck.launcher.agent.AgentHost
import com.droiddeck.launcher.agent.AgentSetup
import com.droiddeck.launcher.agent.AgentSetup.Status
import com.droiddeck.launcher.agent.AgentSetup.Step
import com.droiddeck.launcher.core.WirelessAdbPairingService
@Composable
internal fun AgentSetupView(modifier: Modifier, onDone: () -> Unit = {}) {
val context = LocalContext.current
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
LaunchedEffect(AgentHost.harness) { AgentSetup.check(context) }
Column(modifier.verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(14.dp)) {
Text(stringResource(R.string.agent_setup_title), fontSize = 22.sp, fontWeight = FontWeight.Bold, color = colors.onBackground)
if (!AgentSetup.runtimeInstalled) {
Note(stringResource(R.string.agent_setup_runtime))
return@Column
}
Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
for (harness in AgentHarness.all) {
val chosen = harness == AgentHost.harness
if (chosen) PrimaryButton(harness.name, compact = true) {} else SecondaryButton(harness.name, compact = true) { AgentHost.choose(context, harness) }
}
}
Column(Modifier.fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, pal.line, Shape14)) {
val steps = Step.entries
steps.forEachIndexed { index, step ->
val status = AgentSetup.status[step] ?: Status.UNKNOWN
val (title, hint) = labels(step, AgentHost.harness.name)
val detail = AgentSetup.detail[step] ?: hint
CheckRow(
when (status) {
Status.DONE -> CheckState.OK
Status.WORKING, Status.UNKNOWN -> CheckState.BUSY
Status.TODO, Status.FAILED -> CheckState.WARN
},
title, detail, divider = index < steps.lastIndex,
) {
if (status == Status.TODO || status == Status.FAILED) {
val label = stringResource(if (status == Status.FAILED) R.string.agent_setup_retry else action(step))
val run = { if (step == Step.CONNECTION) pair(context) else AgentSetup.run(context, step) }
if (step.required) PrimaryButton(label, compact = true, onClick = run) else SecondaryButton(label, compact = true, onClick = run)
}
}
}
}
if (AgentSetup.ready) PrimaryButton(stringResource(R.string.agent_setup_done), main = true, onClick = onDone)
}
}
@Composable
private fun labels(step: Step, harness: String): Pair<String, String> = when (step) {
Step.CONNECTION -> stringResource(R.string.agent_step_connection) to stringResource(R.string.agent_step_connection_hint)
Step.INSTALL -> stringResource(R.string.agent_step_install, harness) to stringResource(R.string.agent_step_install_hint)
Step.SIGN_IN -> stringResource(R.string.agent_step_sign_in, harness) to stringResource(R.string.agent_step_sign_in_hint)
Step.GITHUB -> stringResource(R.string.agent_step_github) to stringResource(R.string.agent_step_github_hint)
Step.BUILD_TOOLS -> stringResource(R.string.agent_step_build) to stringResource(R.string.agent_step_build_hint)
}
private fun action(step: Step): Int = when (step) {
Step.CONNECTION -> R.string.agent_action_pair
Step.INSTALL -> R.string.agent_action_install
Step.SIGN_IN, Step.GITHUB -> R.string.agent_action_sign_in
Step.BUILD_TOOLS -> R.string.agent_action_install
}
/** The pairing DroidDeck already uses for the phantom process fix; the agent reuses its key. */
private fun pair(context: android.content.Context) {
if (!WirelessAdbPairingService.notificationsEnabled(context)) {
context.startActivity(android.content.Intent(android.provider.Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, context.packageName)
.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK))
return
}
WirelessAdbPairingService.start(context)
PairingGuideActivity.start(context, null, false)
}
@@ -125,7 +125,7 @@ internal fun navigationKeys(s: FrontEndState) = buildList {
add("steam"); add("games")
if (s.gameStoresEnabled) add("stores")
add("desktop")
if (com.droiddeck.launcher.agent.AgentHost.installed) add("agent")
if (com.droiddeck.launcher.agent.AgentHost.available) add("agent")
add("components")
if (s.isHomeApp) add("android-apps")
add("setup"); add("updates")
@@ -84,7 +84,7 @@ import com.droiddeck.launcher.stores.download.StoreDownloadTier
import com.droiddeck.launcher.R
private enum class CheckState { OK, WARN, BUSY }
internal enum class CheckState { OK, WARN, BUSY }
private const val STORAGE_TAB = 3
private const val LAUNCHER_TAB = 2
@@ -97,7 +97,7 @@ private const val GLASSES_TAB = 6
private const val SYNC_TAB = 4
@Composable
private fun CheckRow(state: CheckState, title: String, detail: String?, divider: Boolean = true, action: (@Composable () -> Unit)? = null) {
internal fun CheckRow(state: CheckState, title: String, detail: String?, divider: Boolean = true, action: (@Composable () -> Unit)? = null) {
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val tint = when (state) {
@@ -690,7 +690,7 @@ internal fun SetupPanel(
}
@Composable
private fun ReadinessCard(passing: Int, total: Int, line: String, open: Boolean, onDetails: () -> Unit, checks: @Composable () -> Unit) {
internal fun ReadinessCard(passing: Int, total: Int, line: String, open: Boolean, onDetails: () -> Unit, checks: @Composable () -> Unit) {
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val ready = passing == total
+17
View File
@@ -1149,6 +1149,23 @@
<string name="agent_send">Send</string>
<string name="agent_new">New conversation</string>
<string name="agent_failed">Stopped: %1$s</string>
<string name="agent_setup_title">Set up the agent</string>
<string name="agent_setup_runtime">Install the Linux runtime in Setup first.</string>
<string name="agent_setup_retry">Retry</string>
<string name="agent_setup_done">Start chatting</string>
<string name="agent_step_connection">Device connection</string>
<string name="agent_step_connection_hint">Wireless debugging, so the agent can see and drive DroidDeck</string>
<string name="agent_step_install">Install %1$s</string>
<string name="agent_step_install_hint">Into the Linux userland, with git, gh and adb</string>
<string name="agent_step_sign_in">Sign in to %1$s</string>
<string name="agent_step_sign_in_hint">Opens your browser</string>
<string name="agent_step_github">GitHub</string>
<string name="agent_step_github_hint">Optional, to open pull requests</string>
<string name="agent_step_build">Build tools</string>
<string name="agent_step_build_hint">Optional, for Lab builds on this device · about 3 GB</string>
<string name="agent_action_pair">Pair</string>
<string name="agent_action_install">Install</string>
<string name="agent_action_sign_in">Sign in</string>
<string name="upd_pr">PR #%1$d</string>
<string name="upd_not_built">Not built for this copy of DroidDeck</string>
<string name="upd_updated">Updated %1$s</string>
@@ -1,9 +1,9 @@
package com.droiddeck.launcher.agent
import com.droiddeck.launcher.agent.AgentHost.Entry
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@@ -14,14 +14,14 @@ import java.nio.file.Files
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class AgentHostTest {
@Test fun theInitLineCarriesOnlyTheConversation() {
val event = AgentHost.parse("""{"type":"system","subtype":"init","session_id":"abc","tools":[]}""")!!
assertEquals("abc", event.session)
@Test fun claudeInitCarriesOnlyTheConversation() {
val event = ClaudeCode.parse("""{"type":"system","subtype":"init","session_id":"abc","tools":[]}""")!!
assertEquals("abc", event.conversation)
assertEquals(emptyList<Entry>(), event.entries)
}
@Test fun assistantTextAndToolUseBecomeEntries() {
val event = AgentHost.parse(
@Test fun claudeTextAndToolUseBecomeEntries() {
val event = ClaudeCode.parse(
"""{"type":"assistant","session_id":"abc","message":{"content":[
{"type":"text","text":" Looking at the log. "},
{"type":"tool_use","name":"Bash","input":{"command":"droiddeckctl state\nmore"}},
@@ -30,21 +30,41 @@ class AgentHostTest {
assertEquals(listOf(Entry.Said("Looking at the log."), Entry.Used("Bash", "droiddeckctl state")), event.entries)
}
@Test fun aResultEndsTheTurn() {
assertEquals(listOf(Entry.Finished(null)), AgentHost.parse("""{"type":"result","subtype":"success","is_error":false,"result":"done"}""")!!.entries)
@Test fun aClaudeResultEndsTheTurn() {
assertEquals(listOf(Entry.Finished(null)), ClaudeCode.parse("""{"type":"result","subtype":"success","is_error":false,"result":"done"}""")!!.entries)
assertEquals(listOf(Entry.Finished("Invalid API key")),
AgentHost.parse("""{"type":"result","subtype":"success","is_error":true,"result":"Invalid API key"}""")!!.entries)
ClaudeCode.parse("""{"type":"result","subtype":"success","is_error":true,"result":"Invalid API key"}""")!!.entries)
assertNull(ClaudeCode.parse("not json"))
}
@Test fun linesWithNothingToShowAreSkipped() {
assertNull(AgentHost.parse("not json"))
assertNull(AgentHost.parse("""{"type":"user","message":{"content":[{"type":"tool_result","content":"ok"}]}}"""))
@Test fun claudeTurnsBypassPermissionsAndResume() {
val first = ClaudeCode.turn("hi", null)
assertTrue("bypassPermissions" in first && "--resume" !in first)
assertEquals("abc", ClaudeCode.turn("hi", "abc").let { it[it.indexOf("--resume") + 1] })
}
@Test fun aMessageIsSentAsOneStreamJsonUserLine() {
val o = JSONObject(AgentHost.userMessage("why did it crash?"))
assertEquals("user", o.getString("type"))
assertEquals("why did it crash?", o.getJSONObject("message").getJSONArray("content").getJSONObject(0).getString("text"))
@Test fun codexEventsBecomeEntries() {
assertEquals("t1", Codex.parse("""{"type":"thread.started","thread_id":"t1"}""")!!.conversation)
assertEquals(listOf(Entry.Used("Shell", "uname -m")),
Codex.parse("""{"type":"item.started","item":{"id":"i1","type":"command_execution","command":"uname -m","status":"in_progress"}}""")!!.entries)
assertEquals(listOf(Entry.Said("It is aarch64.")),
Codex.parse("""{"type":"item.completed","item":{"id":"i2","type":"agent_message","text":"It is aarch64."}}""")!!.entries)
assertEquals(listOf(Entry.Used("Edit", "a.kt, b.kt")),
Codex.parse("""{"type":"item.completed","item":{"type":"file_change","changes":[{"path":"a.kt"},{"path":"b.kt"}]}}""")!!.entries)
assertEquals(listOf(Entry.Finished(null)), Codex.parse("""{"type":"turn.completed","usage":{}}""")!!.entries)
assertEquals(listOf(Entry.Finished("quota")), Codex.parse("""{"type":"turn.failed","error":{"message":"quota"}}""")!!.entries)
assertNull(Codex.parse("""{"type":"item.started","item":{"type":"reasoning"}}"""))
}
@Test fun codexTurnsSkipItsSandboxAndResume() {
assertEquals(listOf("/root/.local/bin/codex", "exec", "--json", "--dangerously-bypass-approvals-and-sandbox", "--skip-git-repo-check", "hi"),
Codex.turn("hi", null))
assertEquals(listOf("exec", "resume", "t1"), Codex.turn("hi", "t1").subList(1, 4))
}
@Test fun anUnknownHarnessFallsBackToClaudeCode() {
assertEquals(Codex, AgentHarness.byId("codex"))
assertEquals(ClaudeCode, AgentHarness.byId("gone"))
}
@Test fun theTranscriptSurvivesARestart() {