diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 949bcd74e0..d3e8cf877c 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -694,6 +694,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) AgentGuest.readyListener = agentReadyListener + com.droiddeck.launcher.agent.AgentAttachments.gameFrame = gameFrame if (com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.isRemoving() || com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.isMaintaining()) { runtimeRemovalBlocked = true @@ -3764,37 +3765,46 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde runningGame = { gestureGame }, )) - private fun captureScreenshot(onDone: (Boolean) -> Unit) { + private fun captureScreenshot(onDone: (Boolean) -> Unit) = copyGameFrame { bitmap -> + if (bitmap == null) { + onDone(false) + return@copyGameFrame + } + Thread { + val saved = com.droiddeck.launcher.session.BottomScreenshots.save(applicationContext, bitmap) + bitmap.recycle() + runOnUiThread { onDone(saved) } + }.start() + } + + private val gameFrame: ((android.graphics.Bitmap?) -> Unit) -> Unit = { copyGameFrame(it) } + + /** The game's own surface, without anything DroidDeck draws over it; the caller recycles the bitmap. */ + private fun copyGameFrame(onFrame: (android.graphics.Bitmap?) -> Unit) { val source = if (glassesOutputActive) glassesScreen?.surfaceView else if (bottomOutputActive) bottomGame?.surfaceView else if (::surfaceView.isInitialized) surfaceView else null val width = source?.width ?: 0 val height = source?.height ?: 0 if (source == null || width <= 0 || height <= 0 || source.holder.surface?.isValid != true) { - onDone(false) + onFrame(null) return } val bitmap = try { android.graphics.Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888) } catch (_: OutOfMemoryError) { - onDone(false) + onFrame(null) return } try { android.view.PixelCopy.request(source, bitmap, { result -> if (result != android.view.PixelCopy.SUCCESS) { bitmap.recycle() - onDone(false) - return@request - } - Thread { - val saved = com.droiddeck.launcher.session.BottomScreenshots.save(applicationContext, bitmap) - bitmap.recycle() - runOnUiThread { onDone(saved) } - }.start() + onFrame(null) + } else onFrame(bitmap) }, uiHandler) } catch (_: IllegalArgumentException) { bitmap.recycle() - onDone(false) + onFrame(null) } } @@ -5670,6 +5680,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde frameGenRequests.incrementAndGet() } if (AgentGuest.readyListener === agentReadyListener) AgentGuest.readyListener = null + if (com.droiddeck.launcher.agent.AgentAttachments.gameFrame === gameFrame) com.droiddeck.launcher.agent.AgentAttachments.gameFrame = null com.droiddeck.launcher.input.HomeButtonShortcuts.detach(this) relativeMouseRequests.shutdownNow() backQamInput.cancel() diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentAttachments.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentAttachments.kt index 6c843df797..b4f9a09dff 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentAttachments.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentAttachments.kt @@ -1,9 +1,13 @@ package com.droiddeck.launcher.agent import android.content.Context +import android.graphics.Bitmap import android.net.Uri import android.provider.OpenableColumns +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import com.droiddeck.launcher.runtime.LinuxRuntime import java.io.File @@ -50,6 +54,22 @@ object AgentAttachments { if (host(context, path).renameTo(moved)) "$GUEST_DIR/$chat/${moved.name}" else path } + /** Copies the running game's frame; set while a session is open. */ + var gameFrame by mutableStateOf<(((Bitmap?) -> Unit) -> Unit)?>(null) + + /** Saves [frame] for [chat] (or a new conversation) on the calling thread; returns its guest path. */ + fun saveFrame(context: Context, chat: String?, frame: Bitmap): String? = runCatching { + val folder = chat ?: NEW + val dir = File(hostDir(context), folder).apply { mkdirs() } + val target = File(dir, "game-${System.currentTimeMillis()}.jpg") + val written = target.outputStream().use { frame.compress(Bitmap.CompressFormat.JPEG, 88, it) } + if (!written) { + target.delete() + return@runCatching null + } + "$GUEST_DIR/$folder/${target.name}" + }.getOrNull() + /** Deletes copies that will not be sent. */ fun discard(context: Context, files: List) = files.forEach { host(context, it).delete() } diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentChats.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentChats.kt index aedd840dd0..be9be4fd0e 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentChats.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentChats.kt @@ -37,6 +37,21 @@ object AgentChats { } .sortedByDescending { it.updated } + /** The chats among [chats] where the user or the agent wrote [query], ignoring case. */ + fun search(context: Context, chats: List, query: String): List { + val q = query.trim() + if (q.isEmpty()) return chats + return chats.filter { chat -> + chat.title.contains(q, ignoreCase = true) || read(file(context, chat.id)).entries.any { matches(it, q) } + } + } + + internal fun matches(entry: Entry, query: String) = when (entry) { + is Entry.Asked -> entry.text.contains(query, ignoreCase = true) + is Entry.Said -> entry.text.contains(query, ignoreCase = true) + else -> false + } + fun delete(context: Context, id: String) { file(context, id).delete() File(AgentAttachments.hostDir(context), id).deleteRecursively() @@ -72,6 +87,7 @@ object AgentChats { is Entry.Thought -> JSONObject().put("thought", entry.text) is Entry.Result -> JSONObject().put("result", entry.id).put("output", entry.output).put("failed", entry.failed) is Entry.Used -> JSONObject().put("used", entry.tool).put("detail", entry.detail).put("id", entry.id).put("input", entry.input) + .apply { if (entry.parent.isNotEmpty()) put("parent", entry.parent) } .apply { entry.output?.let { put("output", it).put("failed", entry.failed) } } is Entry.Finished -> JSONObject().put("finished", true).put("error", entry.error ?: JSONObject.NULL).put("stopped", entry.stopped).apply { if (entry.planned) put("planned", true) @@ -105,7 +121,7 @@ object AgentChats { o.has("said") -> Entry.Said(o.getString("said")) o.has("thought") -> Entry.Thought(o.getString("thought")) o.has("used") -> Entry.Used(o.optString("id"), o.getString("used"), o.optString("detail"), o.optString("input"), - if (o.has("output")) o.optString("output") else null, o.optBoolean("failed")) + if (o.has("output")) o.optString("output") else null, o.optBoolean("failed"), o.optString("parent")) o.has("result") -> Entry.Result(o.getString("result"), o.optString("output"), o.optBoolean("failed")) o.has("finished") -> Entry.Finished( if (o.isNull("error")) null else o.optString("error"), o.optBoolean("stopped"), diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentHarness.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentHarness.kt index 6709399b01..d1d58e98db 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentHarness.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentHarness.kt @@ -108,8 +108,11 @@ object ClaudeCode : AgentHarness { override fun parse(line: String): AgentEvent? { val o = runCatching { JSONObject(line) }.getOrNull() ?: return null val conversation = o.optString("session_id").takeIf { it.isNotEmpty() } + // A subagent's messages carry the call that started it; only its tool calls are shown, under that call. + val parent = o.optString("parent_tool_use_id").takeIf { it.isNotEmpty() && it != "null" } return when (o.optString("type")) { "stream_event" -> { + if (parent != null) return null val delta = o.optJSONObject("event")?.takeIf { it.optString("type") == "content_block_delta" }?.optJSONObject("delta") ?: return null when (delta.optString("type")) { "text_delta" -> AgentEvent(conversation, text = delta.optString("text")) @@ -123,18 +126,18 @@ object ClaudeCode : AgentHarness { val entries = (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) } - "thinking" -> block.optString("thinking").trim().takeIf { it.isNotEmpty() }?.let { AgentHost.Entry.Thought(it) } + "text" -> if (parent != null) null else block.optString("text").trim().takeIf { it.isNotEmpty() }?.let { AgentHost.Entry.Said(it) } + "thinking" -> if (parent != null) null else block.optString("thinking").trim().takeIf { it.isNotEmpty() }?.let { AgentHost.Entry.Thought(it) } "tool_use" -> { val input = block.optJSONObject("input") - AgentHost.Entry.Used(block.optString("id"), block.optString("name"), detail(input), fullInput(block.optString("name"), input)) + AgentHost.Entry.Used(block.optString("id"), block.optString("name"), detail(input), fullInput(block.optString("name"), input), parent = parent.orEmpty()) } else -> null } } val usage = message?.optJSONObject("usage") val context = usage?.let { it.optLong("input_tokens") + it.optLong("cache_read_input_tokens") + it.optLong("cache_creation_input_tokens") + it.optLong("output_tokens") } - AgentEvent(conversation, entries, context = context?.takeIf { it > 0 }) + AgentEvent(conversation, entries, context = context?.takeIf { it > 0 && parent == null }) } "user" -> { val content = o.optJSONObject("message")?.optJSONArray("content") ?: return conversation?.let { AgentEvent(it) } diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentHost.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentHost.kt index 63e60f3d5d..8200200516 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentHost.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentHost.kt @@ -29,8 +29,11 @@ object AgentHost { data class Asked(val text: String, val files: List = emptyList()) : Entry data class Said(val text: String) : Entry data class Thought(val text: String) : Entry - /** A tool call; [output] stays null until its result arrives. */ - data class Used(val id: String, val tool: String, val detail: String, val input: String = "", val output: String? = null, val failed: Boolean = false) : Entry + /** A tool call; [output] stays null until its result arrives. [parent] is the subagent call it was made under, if any. */ + data class Used( + val id: String, val tool: String, val detail: String, val input: String = "", val output: String? = null, val failed: Boolean = false, + val parent: String = "", + ) : Entry data class Result(val id: String, val output: String, val failed: Boolean) : Entry /** [planned]: the turn ran in plan mode, so it ends on a plan that nothing has carried out yet. */ data class Finished(val error: String?, val stopped: Boolean = false, val usage: Usage? = null, val planned: Boolean = false) : Entry @@ -71,7 +74,9 @@ object AgentHost { "Run it with run_in_background and keep checking its output, so the user can talk to you meanwhile. " + "The restart ends this process, and this conversation continues on the new build. " + "Files the user attaches are listed at the end of their message, under /root/.cache/droiddeck/attachments. " + - "Commits and pull requests you make are the user's own: add no co-author line, AI credit or generated-by note." + "Commits and pull requests you make are the user's own: add no co-author line, AI credit or generated-by note. " + + "To have the user pick between options, end your reply with a fenced code block whose language is choices, one short option per line, " + + "and end your turn: DroidDeck shows them as buttons and sends the one picked as the next message." val entries = mutableStateListOf() val queue = mutableStateListOf() @@ -142,6 +147,7 @@ object AgentHost { fun seen() { unseen = false + appContext?.let { AgentNotice.clear(it) } } fun refresh(context: Context) { @@ -462,7 +468,19 @@ object AgentHost { if (!stopped && queue.isNotEmpty()) { val next = queue.removeAt(0) send(app, next.text, next.files) + } else if (!stopped) notifyEnded(app, error) + } + + private fun notifyEnded(app: Context, error: String?) { + val start = entries.indexOfLast { it is Entry.Asked } + val said = entries.subList(start + 1, entries.size).lastOrNull { it is Entry.Said } as? Entry.Said + val reply = said?.let { choices(it.text) } + val kind = when { + error != null -> AgentNotice.Kind.FAILED + !reply?.options.isNullOrEmpty() -> AgentNotice.Kind.QUESTION + else -> AgentNotice.Kind.DONE } + AgentNotice.post(app, kind, error ?: reply?.text?.takeIf { it.isNotBlank() }?.let { firstLine(it) } ?: "") } /** A message typed or retried by the user replaces the automatic resend. */ @@ -501,7 +519,7 @@ object AgentHost { } /** The latest todo list the agent wrote in this conversation. */ - fun todos(): String? = entries.lastOrNull { it is Entry.Used && it.tool == "TodoWrite" }?.let { (it as Entry.Used).input } + fun todos(): String? = entries.lastOrNull { it is Entry.Used && it.tool == "TodoWrite" && it.parent.isEmpty() }?.let { (it as Entry.Used).input } private fun tick() { now = SystemClock.elapsedRealtime() @@ -645,3 +663,34 @@ object AgentHost { internal fun prefs(context: Context) = context.getSharedPreferences("agent", Context.MODE_PRIVATE) } + +/** A reply split into its text and the options of a trailing choices block. */ +internal data class Choices(val text: String, val options: List) + +private val FENCE = Regex("""[ \t]{0,3}(`{3,}|~{3,})(.*)""") + +/** Walks Markdown fences so a choices block counts only at the top level and only as the last thing in the reply. */ +internal fun choices(reply: String): Choices { + val lines = reply.split('\n') + var open = "" + var opener = -1 + var openChoices = false + var block: IntRange? = null + lines.forEachIndexed { i, line -> + val fence = FENCE.matchEntire(line) + if (open.isEmpty()) { + if (fence != null) { + open = fence.groupValues[1] + opener = i + openChoices = fence.groupValues[2].trim() == "choices" + block = null + } else if (line.isNotBlank()) block = null + } else if (fence != null && fence.groupValues[1][0] == open[0] && fence.groupValues[1].length >= open.length && fence.groupValues[2].isBlank()) { + block = if (openChoices) opener..i else null + open = "" + } + } + val found = block?.takeIf { open.isEmpty() } ?: return Choices(reply, emptyList()) + val options = lines.subList(found.first + 1, found.last).map { it.trim().removePrefix("- ").removePrefix("* ").trim() }.filter { it.isNotEmpty() } + return Choices(lines.subList(0, found.first).joinToString("\n").trimEnd(), options) +} diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentNotice.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentNotice.kt new file mode 100644 index 0000000000..edba77a0c8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentNotice.kt @@ -0,0 +1,58 @@ +package com.droiddeck.launcher.agent + +import android.app.ActivityManager +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import com.droiddeck.launcher.FrontEndActivity +import com.droiddeck.launcher.R +import com.droiddeck.launcher.ui.AgentPet + +/** Tells the user a turn has ended when nothing on screen already does. */ +internal object AgentNotice { + private const val CHANNEL_ID = "agent_done" + private const val NOTIFICATION_ID = 4108 + + enum class Kind { DONE, FAILED, QUESTION } + + fun post(context: Context, kind: Kind, text: String) { + if (onScreen(context)) return + val manager = context.getSystemService(NotificationManager::class.java) ?: return + manager.createNotificationChannel(NotificationChannel(CHANNEL_ID, context.getString(R.string.agent_done_channel), NotificationManager.IMPORTANCE_DEFAULT)) + val open = PendingIntent.getActivity(context, 2, + Intent(context, FrontEndActivity::class.java) + .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) + .putExtra(com.droiddeck.launcher.stores.download.StoreDownloadService.EXTRA_NAV, "agent"), + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) + val title = context.getString(when (kind) { + Kind.DONE -> R.string.agent_done + Kind.FAILED -> R.string.agent_done_failed + Kind.QUESTION -> R.string.agent_done_question + }) + runCatching { + manager.notify(NOTIFICATION_ID, Notification.Builder(context, CHANNEL_ID) + .setSmallIcon(R.drawable.ic_stat_session) + .setContentTitle(title) + .setContentText(text) + .setStyle(Notification.BigTextStyle().bigText(text)) + .setContentIntent(open) + .setAutoCancel(true) + .setCategory(Notification.CATEGORY_STATUS) + .build()) + } + } + + fun clear(context: Context) { + context.getSystemService(NotificationManager::class.java)?.cancel(NOTIFICATION_ID) + } + + /** The chat in a DroidDeck window in front, or the pet, which shows the reply in its own bubble. */ + private fun onScreen(context: Context): Boolean { + if (AgentPet.floating) return true + val state = ActivityManager.RunningAppProcessInfo().also { ActivityManager.getMyMemoryState(it) } + return AgentPet.agentOnScreen > 0 && state.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentTools.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentTools.kt new file mode 100644 index 0000000000..9451b79c85 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentTools.kt @@ -0,0 +1,143 @@ +package com.droiddeck.launcher.agent + +import android.content.Context +import com.droiddeck.launcher.runtime.LinuxRuntime +import java.io.File +import java.nio.file.FileVisitResult +import java.nio.file.Files +import java.nio.file.LinkOption +import java.nio.file.Path +import java.nio.file.SimpleFileVisitor +import java.nio.file.attribute.BasicFileAttributes +import java.util.concurrent.TimeUnit + +/** Claude Code's MCP servers and skills in the userland, through its own CLI and skills folder. */ +internal object AgentTools { + data class Server(val name: String, val target: String, val status: String) { + /** Connectors come from the claude.ai account and are managed there, not by the CLI. */ + val account get() = name.startsWith("claude.ai ") + } + + data class Skill(val name: String, val description: String) + + class Result(val ok: Boolean, val output: String) + + private const val SKILLS = "root/.claude/skills" + private val SERVER = Regex("""^(.+?): (.+) - (.+)$""") + + fun servers(context: Context): List? { + val result = run(context, listOf(ClaudeCode.binary, "mcp", "list")) + if (!result.ok) return null + return parseServers(result.output) + } + + internal fun parseServers(output: String): List = output.lines().mapNotNull { line -> + SERVER.find(line.trim())?.destructured?.let { (name, target, status) -> Server(name, target, status) } + } + + /** A URL is added as an HTTP server; anything else is the command that starts a stdio server. */ + internal fun addCommand(name: String, target: String): List { + val t = target.trim() + val base = listOf(ClaudeCode.binary, "mcp", "add", "--scope", "user") + return if (t.startsWith("http://") || t.startsWith("https://")) base + listOf("--transport", "http", name.trim(), t) + else base + listOf(name.trim(), "--") + words(t) + } + + /** Splits a command line the way a shell would for quotes and backslashes, without expanding anything. */ + internal fun words(line: String): List { + val words = mutableListOf() + val word = StringBuilder() + var inWord = false + var quote: Char? = null + var i = 0 + while (i < line.length) { + val c = line[i] + when { + quote == '\'' -> if (c == '\'') quote = null else word.append(c) + c == '\\' && i + 1 < line.length && (quote == null || line[i + 1] == '"' || line[i + 1] == '\\') -> { + word.append(line[++i]) + inWord = true + } + quote == '"' -> if (c == '"') quote = null else word.append(c) + c == '\'' || c == '"' -> { + quote = c + inWord = true + } + c.isWhitespace() -> if (inWord) { + words += word.toString() + word.clear() + inWord = false + } + else -> { + word.append(c) + inWord = true + } + } + i++ + } + if (inWord) words += word.toString() + return words + } + + fun add(context: Context, name: String, target: String) = run(context, addCommand(name, target)) + + fun remove(context: Context, name: String) = run(context, listOf(ClaudeCode.binary, "mcp", "remove", name)) + + fun skills(context: Context): List = + File(LinuxRuntime.rootDir(context), SKILLS).listFiles { f -> f.isDirectory }.orEmpty().sortedBy { it.name.lowercase() }.map { dir -> + Skill(dir.name, description(File(dir, "SKILL.md"))) + } + + internal fun description(file: File): String = runCatching { + val lines = file.readLines() + if (lines.firstOrNull()?.trim() != "---") return@runCatching "" + lines.drop(1).takeWhile { it.trim() != "---" }.firstOrNull { it.startsWith("description:") } + ?.removePrefix("description:")?.trim()?.trim('"', '\'').orEmpty() + }.getOrDefault("") + + fun deleteSkill(context: Context, name: String): Boolean { + if (name.isEmpty() || name.contains('/') || name.startsWith(".")) return false + val root = File(LinuxRuntime.rootDir(context), SKILLS) + val dir = File(root, name) + if (dir.parentFile?.path != root.path) return false + return deleteTree(dir.toPath()) + } + + /** Deletes [path] and what is under it; a symbolic link is removed itself, never what it points to. */ + internal fun deleteTree(path: Path): Boolean = runCatching { + if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) return@runCatching true + Files.walkFileTree(path, object : SimpleFileVisitor() { + override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { + Files.delete(file) + return FileVisitResult.CONTINUE + } + + override fun postVisitDirectory(dir: Path, exc: java.io.IOException?): FileVisitResult { + if (exc != null) throw exc + Files.delete(dir) + return FileVisitResult.CONTINUE + } + }) + true + }.getOrDefault(false) + + private fun run(context: Context, command: List): Result { + val child = runCatching { AgentHost.guest(context.applicationContext, command, connectAdb = false).start() } + .getOrElse { return Result(false, it.message.orEmpty()) } + val output = StringBuffer() + // The CLI can hold its output open past the deadline, so it is read beside the wait rather than before it. + val reader = Thread({ runCatching { child.inputStream.bufferedReader().forEachLine { output.append(it).append('\n') } } }, "agent-tools").apply { start() } + return try { + val ended = child.waitFor(90, TimeUnit.SECONDS) + if (!ended) child.destroyForcibly() + reader.join(2_000) + Result(ended && child.exitValue() == 0, output.toString().trim()) + } catch (e: InterruptedException) { + child.destroyForcibly() + Thread.currentThread().interrupt() + Result(false, output.toString().trim()) + } finally { + runCatching { child.inputStream.close() } + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AgentEntries.kt b/app/src/main/java/com/droiddeck/launcher/ui/AgentEntries.kt index 3300b7f8a7..1ea5094ed7 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AgentEntries.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AgentEntries.kt @@ -6,6 +6,8 @@ import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.ExperimentalLayoutApi +import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.heightIn @@ -19,6 +21,7 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.CheckCircle import androidx.compose.material.icons.outlined.Close import androidx.compose.material.icons.outlined.Description +import androidx.compose.material.icons.outlined.ContentCopy import androidx.compose.material.icons.outlined.ErrorOutline import androidx.compose.material.icons.outlined.ExpandLess import androidx.compose.material.icons.outlined.ExpandMore @@ -39,6 +42,8 @@ import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.AnnotatedString +import androidx.compose.ui.platform.LocalClipboardManager import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.stringResource @@ -53,9 +58,13 @@ import com.droiddeck.launcher.R import com.droiddeck.launcher.agent.AgentAttachments import com.droiddeck.launcher.agent.AgentHost import com.droiddeck.launcher.agent.AgentHost.Entry +import com.droiddeck.launcher.agent.choices @Composable -internal fun AgentEntry(entry: Entry, running: Boolean, last: Boolean = false, changes: Changes? = null, onRevise: () -> Unit = {}) { +internal fun AgentEntry( + entry: Entry, running: Boolean, last: Boolean = false, changes: Changes? = null, answerable: Boolean = false, + children: List = emptyList(), turn: List = emptyList(), onRevise: () -> Unit = {}, onSent: () -> Unit = {}, +) { val colors = MaterialTheme.colorScheme val pal = LocalPalette.current when (entry) { @@ -66,24 +75,43 @@ internal fun AgentEntry(entry: Entry, running: Boolean, last: Boolean = false, c modifier = Modifier.widthIn(max = 560.dp).clip(Shape16).background(pal.primary).padding(horizontal = 14.dp, vertical = 10.dp)) } } - is Entry.Said -> SelectionContainer { AgentMarkdown(entry.text, Modifier.widthIn(max = 760.dp).padding(horizontal = 4.dp)) } + is Entry.Said -> { + val reply = remember(entry.text) { choices(entry.text) } + Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { + if (reply.text.isNotBlank()) SelectionContainer { AgentMarkdown(reply.text, Modifier.widthIn(max = 760.dp).padding(horizontal = 4.dp)) } + if (reply.options.isNotEmpty()) AgentChoices(reply.options, answerable, onSent) + } + } is Entry.Thought -> AgentThought(entry.text, live = false) - is Entry.Used -> if (entry.tool == "TodoWrite") AgentTodos(entry.input) else AgentToolCall(entry, running) + is Entry.Used -> if (entry.tool == "TodoWrite") AgentTodos(entry.input) else AgentToolCall(entry, running, children) is Entry.Result -> Unit is Entry.Finished -> when { last && AgentHost.answerless(AgentHost.entries) -> AgentNoReply() last && entry.planned -> Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { - AgentTurnEnd(entry, changes) - AgentPlanActions(onRevise) + AgentTurnEnd(entry, changes, turn) + AgentPlanActions(onRevise, onSent) } - else -> AgentTurnEnd(entry, changes) + else -> AgentTurnEnd(entry, changes, turn) + } + } +} + +/** The options a reply ends with; picking one sends it, while the reply is still the latest. */ +@OptIn(ExperimentalLayoutApi::class) +@Composable +private fun AgentChoices(options: List, answerable: Boolean, onSent: () -> Unit) { + val context = LocalContext.current + FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.padding(horizontal = 4.dp)) { + for (option in options) SecondaryButton(option, enabled = answerable, compact = true, modifier = Modifier.testTag("agent-choice")) { + AgentHost.send(context, option) + onSent() } } } /** A plan turn changed nothing; Go carries the plan out with plan mode off, Revise asks for another. */ @Composable -private fun AgentPlanActions(onRevise: () -> Unit) { +private fun AgentPlanActions(onRevise: () -> Unit, onSent: () -> Unit) { val context = LocalContext.current val pal = LocalPalette.current val go = stringResource(R.string.agent_plan_go_message) @@ -91,6 +119,7 @@ private fun AgentPlanActions(onRevise: () -> Unit) { PrimaryButton(stringResource(R.string.agent_plan_go), compact = true, modifier = Modifier.testTag("agent-plan-go")) { AgentHost.choosePlan(context, false) AgentHost.send(context, go) + onSent() } SecondaryButton(stringResource(R.string.agent_plan_revise), compact = true, modifier = Modifier.testTag("agent-plan-revise")) { AgentHost.choosePlan(context, true) @@ -106,13 +135,18 @@ internal data class Changes(val files: Int, val added: Int, val removed: Int) /** The edits of the turn that ends at [end], or null when it changed no file. */ internal fun turnChanges(entries: List, end: Int): Changes? { val start = entries.subList(0, end).indexOfLast { it is Entry.Asked } - // Plan mode saves its plan as a file of its own, which is not a change to anything. - val edits = entries.subList(start + 1, end).filterIsInstance().filter { !it.failed }.mapNotNull { diffOf(it) } - .filter { !it.path.contains("/.claude/plans/") } + val edits = turnEdits(entries.subList(start + 1, end)) if (edits.isEmpty()) return null return Changes(edits.map { it.path }.distinct().size, edits.sumOf { it.added }, edits.sumOf { it.removed }) } +/** The edits in [turn] that took, in order. Plan mode saves its plan as a file of its own, which is not a change to anything. */ +internal fun turnEdits(turn: List): List = + turn.filterIsInstance().filter { !it.failed }.mapNotNull { diffOf(it) }.filter { !it.path.contains("/.claude/plans/") } + +/** What the agent said in [turn], to copy as one text. */ +internal fun turnReply(turn: List): String = turn.filterIsInstance().joinToString("\n\n") { it.text } + internal class Diff(val path: String, val lines: List) { val added = lines.count { it.startsWith("+") } val removed = lines.count { it.startsWith("-") } @@ -129,9 +163,11 @@ internal fun diffOf(entry: Entry.Used): Diff? { } @Composable -private fun AgentTurnEnd(entry: Entry.Finished, changes: Changes?) { +private fun AgentTurnEnd(entry: Entry.Finished, changes: Changes?, turn: List) { val colors = MaterialTheme.colorScheme val pal = LocalPalette.current + var open by rememberSaveable { mutableStateOf(false) } + val reply = turnReply(turn) val text = when { entry.stopped -> stringResource(R.string.agent_stopped) entry.error != null -> stringResource(R.string.agent_failed, entry.error) @@ -143,18 +179,38 @@ private fun AgentTurnEnd(entry: Entry.Finished, changes: Changes?) { }.joinToString(" · ") } } - if (text.isNullOrEmpty() && changes == null) return + if (text.isNullOrEmpty() && changes == null && reply.isEmpty()) return Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp), modifier = Modifier.padding(horizontal = 4.dp)) { if (changes != null) Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp), - modifier = Modifier.clip(Shape12).background(pal.surfaceVariant).padding(horizontal = 9.dp, vertical = 4.dp).testTag("agent-changes"), + modifier = Modifier.clip(Shape12).background(if (open) pal.primary.copy(alpha = 0.2f) else pal.surfaceVariant).clickable { open = !open } + .padding(horizontal = 9.dp, vertical = 4.dp).testTag("agent-changes"), ) { Text(pluralStringResource(R.plurals.agent_files_changed, changes.files, changes.files), fontSize = 12.sp, color = pal.onSurfaceVariant) Text("+${changes.added}", fontSize = 12.sp, fontFamily = FontFamily.Monospace, color = pal.good) Text("−${changes.removed}", fontSize = 12.sp, fontFamily = FontFamily.Monospace, color = colors.error) + Icon(if (open) Icons.Outlined.ExpandLess else Icons.Outlined.ExpandMore, null, tint = pal.onSurfaceVariant, modifier = Modifier.size(16.dp)) } - if (!text.isNullOrEmpty()) Text(text, fontSize = 12.sp, color = if (entry.error != null && !entry.stopped) colors.error else pal.onSurfaceVariant) + if (!text.isNullOrEmpty()) Text(text, fontSize = 12.sp, color = if (entry.error != null && !entry.stopped) colors.error else pal.onSurfaceVariant, + modifier = Modifier.weight(1f, fill = false)) + if (reply.isNotEmpty()) CopyButton(reply, stringResource(R.string.agent_copy_reply)) } + if (open && changes != null) Column(Modifier.padding(start = 4.dp, end = 4.dp, top = 8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { + for (diff in turnEdits(turn)) { + Text(diff.path, fontSize = 11.5.sp, fontFamily = FontFamily.Monospace, color = pal.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis) + DiffBox(diff) + } + } +} + +@Composable +private fun CopyButton(text: String, label: String) { + val clipboard = LocalClipboardManager.current + val pal = LocalPalette.current + Icon( + Icons.Outlined.ContentCopy, label, tint = pal.onSurfaceVariant, + modifier = Modifier.size(28.dp).clip(Shape12).clickable { clipboard.setText(AnnotatedString(text)) }.padding(6.dp).testTag("agent-copy"), + ) } @Composable @@ -216,7 +272,7 @@ private fun AgentTodos(list: String) { /** A tool call: one line with its status, opening to the full input and output, or to the diff of an edit. */ @Composable -private fun AgentToolCall(entry: Entry.Used, running: Boolean) { +private fun AgentToolCall(entry: Entry.Used, running: Boolean, children: List = emptyList()) { val colors = MaterialTheme.colorScheme val pal = LocalPalette.current var open by rememberSaveable(entry.id.ifEmpty { entry.detail }) { mutableStateOf(false) } @@ -245,6 +301,17 @@ private fun AgentToolCall(entry: Entry.Used, running: Boolean) { } Icon(if (open) Icons.Outlined.ExpandLess else Icons.Outlined.ExpandMore, null, tint = pal.onSurfaceVariant, modifier = Modifier.size(18.dp)) } + if (children.isNotEmpty()) { + val latest = children.last() + Text( + pluralStringResource(R.plurals.agent_subagent_steps, children.size, children.size) + " · " + latest.tool + " " + latest.detail, + fontSize = 12.sp, fontFamily = FontFamily.Monospace, color = pal.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, + modifier = Modifier.padding(start = 34.dp, end = 10.dp, bottom = 8.dp), + ) + } + if (open && children.isNotEmpty()) Column(Modifier.padding(start = 10.dp, end = 4.dp, bottom = 6.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) { + for (child in children) AgentToolCall(child, running && child.output == null) + } if (open && diff != null) Column(Modifier.padding(start = 10.dp, end = 10.dp, bottom = 10.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) { if (diff.path.contains('/')) Text(diff.path, fontSize = 11.5.sp, fontFamily = FontFamily.Monospace, color = pal.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis) DiffBox(diff) @@ -289,7 +356,11 @@ private fun DiffBox(diff: Diff) { private fun CodeBox(label: String, text: String, color: Color) { val pal = LocalPalette.current Column { - Text(label, fontSize = 11.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 1.sp, color = pal.onSurfaceVariant, modifier = Modifier.padding(bottom = 3.dp)) + Row(verticalAlignment = Alignment.CenterVertically) { + Text(label, fontSize = 11.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 1.sp, color = pal.onSurfaceVariant, + modifier = Modifier.weight(1f).padding(bottom = 3.dp)) + CopyButton(text, stringResource(R.string.agent_copy)) + } SelectionContainer { Text( text, fontSize = 12.sp, fontFamily = FontFamily.Monospace, color = color, lineHeight = 17.sp, diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AgentPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/AgentPage.kt index 34561b28ee..3bc13a5f20 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AgentPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AgentPage.kt @@ -43,6 +43,10 @@ import androidx.compose.foundation.text.KeyboardActions 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.Extension +import androidx.compose.material.icons.outlined.PhotoCamera +import androidx.compose.material.icons.outlined.MicOff +import androidx.compose.material.icons.outlined.Mic import androidx.compose.material.icons.outlined.Add import androidx.compose.material.icons.outlined.ArrowDownward import androidx.compose.material.icons.outlined.AttachFile @@ -66,12 +70,12 @@ import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.MutableState import androidx.compose.runtime.Stable -import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.runtime.snapshotFlow @@ -93,6 +97,7 @@ import androidx.compose.ui.input.nestedscroll.NestedScrollSource import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.input.pointer.PointerEventPass import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalFocusManager @@ -116,34 +121,35 @@ import com.droiddeck.launcher.agent.AgentHost.Entry import com.droiddeck.launcher.agent.AgentSetup import kotlinx.coroutines.launch -internal enum class AgentPanel { NONE, MODEL, USAGE, HISTORY, PROCESSES } +internal enum class AgentPanel { NONE, MODEL, USAGE, HISTORY, PROCESSES, TOOLS } /** Slash commands the app handles itself; others go to the CLI, which has its own. */ private val LOCAL_COMMANDS = listOf("/new", "/model", "/plan", "/history", "/processes", "/stop") private val CLI_COMMANDS = listOf("/compact", "/cost", "/review", "/init") -private const val COMPOSER_SLIDE_MS = 350L /** - * The chat with the composer at the bottom. The composer is the full editor only while it is - * being typed in; reading shrinks it to one line, and scrolling back through older messages - * hides it behind a small pill. [startTyping] opens an empty conversation ready to type. + * The chat with the model, usage and menu on top and the editor at the bottom. Scrolling back + * through older messages hides the editor behind a small pill and stops following new output. + * [inputModifier] goes on the first control, where a controller lands without raising the keyboard. */ +@Suppress("UNUSED_PARAMETER") @Composable internal fun AgentPage( modifier: Modifier, inputModifier: Modifier = Modifier, visible: Boolean = true, startTyping: Boolean = true, hidesPet: Boolean = true, menuSlot: AgentMenuSlot? = null, -) = AgentPageContent(modifier, inputModifier, visible, startTyping, hidesPet, showControlsWithoutKeyboard = false, menuSlot) +) = AgentPageContent(modifier, inputModifier, visible, hidesPet, menuSlot) +// Lab overlays run against the installed APK, so these entry points keep their JVM signatures. +@Suppress("UNUSED_PARAMETER") @Composable internal fun AgentPageWithControls( modifier: Modifier, inputModifier: Modifier = Modifier, visible: Boolean = true, startTyping: Boolean = true, showControlsWithoutKeyboard: Boolean, -) = AgentPageContent(modifier, inputModifier, visible, startTyping, hidesPet = true, showControlsWithoutKeyboard = showControlsWithoutKeyboard) +) = AgentPageContent(modifier, inputModifier, visible, hidesPet = true) @Composable private fun AgentPageContent( - modifier: Modifier, inputModifier: Modifier, visible: Boolean, startTyping: Boolean, hidesPet: Boolean, - showControlsWithoutKeyboard: Boolean, menuSlot: AgentMenuSlot? = null, + modifier: Modifier, inputModifier: Modifier, visible: Boolean, hidesPet: Boolean, menuSlot: AgentMenuSlot? = null, ) { val context = LocalContext.current val view = LocalView.current @@ -159,8 +165,16 @@ private fun AgentPageContent( } remember { AgentHost.refresh(context) } LaunchedEffect(AgentHost.unseen, visible) { if (visible) AgentHost.seen() } - DisposableEffect(visible, hidesPet) { - val counts = visible && hidesPet + // A stopped activity keeps its composition; the page is only on screen while its host is started. + val lifecycle = androidx.compose.ui.platform.LocalLifecycleOwner.current.lifecycle + var started by remember(lifecycle) { mutableStateOf(lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.STARTED)) } + DisposableEffect(lifecycle) { + val observer = androidx.lifecycle.LifecycleEventObserver { _, _ -> started = lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.STARTED) } + lifecycle.addObserver(observer) + onDispose { lifecycle.removeObserver(observer) } + } + DisposableEffect(visible, hidesPet, started) { + val counts = visible && hidesPet && started if (counts) AgentPet.agentOnScreen++ onDispose { if (counts) AgentPet.agentOnScreen-- } } @@ -172,17 +186,19 @@ private fun AgentPageContent( return } var panel by rememberSaveable { mutableStateOf(AgentPanel.NONE) } - val composer = remember { ComposerState(startTyping && AgentHost.entries.isEmpty()) } + val composer = remember { ComposerState() } val focusManager = LocalFocusManager.current val togglePanel: (AgentPanel) -> Unit = { panel = if (panel == it) AgentPanel.NONE else it } var menuOpen by rememberSaveable { mutableStateOf(false) } - val menu: @Composable () -> Unit = { - AgentMenu(menuOpen, { menuOpen = it }, onPanel = { panel = it }, onSetup = { setup = true }) + val topBar: @Composable () -> Unit = { + AgentTopBar(panel, togglePanel, inputModifier) { + AgentMenu(menuOpen, { menuOpen = it }, onPanel = { panel = it }, onSetup = { setup = true }) + } } - // A host with a title bar shows the menu there; otherwise it floats over the conversation's top corner. - val inSlot = menuSlot != null && panel != AgentPanel.HISTORY && panel != AgentPanel.PROCESSES + // A host with a title bar shows the top bar there. + val inSlot = menuSlot != null && panel != AgentPanel.HISTORY && panel != AgentPanel.PROCESSES && panel != AgentPanel.TOOLS if (menuSlot != null) DisposableEffect(menuSlot, inSlot) { - menuSlot.content = if (inSlot) menu else null + menuSlot.content = if (inSlot) topBar else null onDispose { menuSlot.content = null } } // The keyboard leaves too little height for a panel and the editor together, so typing closes the panel. @@ -191,7 +207,7 @@ private fun AgentPageContent( Column( modifier.fillMaxSize().imePadding().onPreviewKeyEvent { e -> val k = e.nativeKeyEvent - if (k.keyCode != KeyEvent.KEYCODE_BUTTON_Y || composer.typing) return@onPreviewKeyEvent false + if (k.keyCode != KeyEvent.KEYCODE_BUTTON_Y || composer.focused) return@onPreviewKeyEvent false if (k.action == KeyEvent.ACTION_UP) composer.open() true }, @@ -205,11 +221,22 @@ private fun AgentPageContent( AgentPanelBar(stringResource(R.string.agent_processes)) { panel = AgentPanel.NONE } AgentProcessList(Modifier.weight(1f)) } + AgentPanel.TOOLS -> { + AgentPanelBar(stringResource(R.string.agent_tools)) { panel = AgentPanel.NONE } + AgentToolsPanel(Modifier.weight(1f)) + } else -> { + if (menuSlot == null) Box(Modifier.padding(bottom = 6.dp)) { topBar() } + when (panel) { + AgentPanel.MODEL -> Box(Modifier.padding(bottom = 8.dp)) { AgentModelPicker() } + AgentPanel.USAGE -> Box(Modifier.padding(bottom = 8.dp)) { AgentUsage() } + else -> Unit + } val list = rememberLazyListState() val scope = rememberCoroutineScope() - // Only dragging back through older messages hides the composer; showing it again shrinks the list, which must not hide it once more. + // Only dragging back through older messages stops following the latest; reaching the bottom again resumes it. var readingBack by remember { mutableStateOf(false) } + LaunchedEffect(AgentHost.chat) { readingBack = false } LaunchedEffect(list) { snapshotFlow { list.canScrollForward }.collect { if (!it) readingBack = false } } val dragBack = remember { object : NestedScrollConnection { @@ -223,57 +250,53 @@ private fun AgentPageContent( readingBack = false scope.launch { list.scrollToItem(maxOf(0, list.layoutInfo.totalItemsCount - 1), Int.MAX_VALUE / 2) } } - val hidden = !composer.typing && readingBack && AgentHost.entries.isNotEmpty() - var wasHidden by remember { mutableStateOf(false) } - LaunchedEffect(hidden) { - if (hidden) wasHidden = true - else if (wasHidden) { - wasHidden = false - // The composer takes its height back as it slides in; the latest message stays above it. - kotlinx.coroutines.delay(COMPOSER_SLIDE_MS) - list.scrollToItem(maxOf(0, list.layoutInfo.totalItemsCount - 1), Int.MAX_VALUE / 2) - } - } - Box(Modifier.weight(1f).fillMaxWidth().clipToBounds().nestedScroll(dragBack)) { + val hidden = !composer.focused && !composer.hasUnconsumedFocusRequest() && readingBack && AgentHost.entries.isNotEmpty() + var listHeight by remember { mutableIntStateOf(0) } + Box( + Modifier.weight(1f).fillMaxWidth().clipToBounds().nestedScroll(dragBack).onGloballyPositioned { + // The editor sliding in or the keyboard rising shrinks the list from below; a list following the latest keeps it in view, even mid-drag. + // Scrolling remeasures the list, which Compose refuses during a measure pass, so this runs once positioning is done. + val height = it.size.height + val shrunk = listHeight - height + listHeight = height + if (shrunk > 0 && !readingBack) list.dispatchRawDelta(shrunk.toFloat()) + }, + ) { AgentConversation( Modifier.fillMaxSize().pointerInput(Unit) { awaitEachGesture { awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Initial) if (panel == AgentPanel.MODEL || panel == AgentPanel.USAGE) panel = AgentPanel.NONE - if (composer.typing) focusManager.clearFocus() + if (composer.focused) focusManager.clearFocus() } }, - list, onRevise = { composer.open() }, + list, following = !readingBack, onRevise = { composer.open() }, onSent = toLatest, ) androidx.compose.animation.AnimatedVisibility( hidden, enter = fadeIn(), exit = fadeOut(), modifier = Modifier.align(Alignment.BottomCenter).padding(bottom = 8.dp), ) { AgentJumpPill(toLatest) } - if (menuSlot == null) Box(Modifier.align(Alignment.TopEnd).padding(top = 4.dp)) { menu() } } androidx.compose.animation.AnimatedVisibility(!hidden, enter = expandVertically() + fadeIn(), exit = shrinkVertically() + fadeOut()) { - AgentComposer(composer, draft, inputModifier, panel, togglePanel, showControlsWithoutKeyboard) + AgentComposer(composer, draft, visible, onPanel = { panel = it }, onSent = toLatest) } } } } } -/** Whether the composer is the full editor; [open] also puts the cursor in it. */ +/** Whether the editor has the cursor; [open] puts it there. */ @Stable -private class ComposerState(typing: Boolean) { - var typing by mutableStateOf(typing) +private class ComposerState { + var focused by mutableStateOf(false) var focusRequests by mutableIntStateOf(0) private set private var consumedFocusRequests by mutableIntStateOf(0) - /** Opened and waiting for the text field to take focus; focus passes through nothing on the way. */ - var opening = false fun open() { - typing = true - opening = true focusRequests++ + if (focused) consumeFocusRequest() } fun hasUnconsumedFocusRequest() = focusRequests > consumedFocusRequests @@ -289,6 +312,30 @@ internal class AgentMenuSlot { var content by mutableStateOf<(@Composable () -> Unit)?>(null) } +/** The model and how full the context is, then the [menu]. */ +@Composable +private fun AgentTopBar(panel: AgentPanel, onPanel: (AgentPanel) -> Unit, firstControl: Modifier, menu: @Composable () -> Unit) { + val context = LocalContext.current + val pal = LocalPalette.current + Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) { + Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.weight(1f)) { + val model = AgentHost.models(context).firstOrNull { it.id == AgentHost.model }?.label ?: AgentHost.model ?: stringResource(R.string.agent_model_default) + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.weight(1f, fill = false).height(36.dp).clip(Shape12).then(firstControl).clickable { onPanel(AgentPanel.MODEL) } + .padding(horizontal = 8.dp).testTag("agent-model"), + ) { + val parts = listOfNotNull(AgentHost.harness.name, model, AgentHost.effort, stringResource(R.string.agent_plan_short).takeIf { AgentHost.plan && AgentHost.harness.plans }) + Text(parts.joinToString(" · "), fontSize = 13.sp, color = if (panel == AgentPanel.MODEL) pal.primary else pal.onSurfaceVariant, + maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f, fill = false)) + Icon(Icons.Outlined.ExpandMore, null, tint = pal.onSurfaceVariant, modifier = Modifier.size(16.dp)) + } + ContextRing("agent-usage", onClick = { onPanel(AgentPanel.USAGE) }) + } + menu() + } +} + /** History, processes, a new conversation, the pet and setup, folded behind one button. */ @Composable private fun AgentMenu(open: Boolean, onOpen: (Boolean) -> Unit, onPanel: (AgentPanel) -> Unit, onSetup: () -> Unit) { @@ -306,6 +353,9 @@ private fun AgentMenu(open: Boolean, onOpen: (Boolean) -> Unit, onPanel: (AgentP Row(verticalAlignment = Alignment.CenterVertically) { HeaderAction(Icons.Outlined.History, stringResource(R.string.agent_history), size = 36.dp, tag = "agent-history") { onOpen(false); onPanel(AgentPanel.HISTORY) } HeaderAction(Icons.Outlined.Memory, stringResource(R.string.agent_processes), size = 36.dp, tag = "agent-processes") { onOpen(false); onPanel(AgentPanel.PROCESSES) } + if (AgentHost.harness == com.droiddeck.launcher.agent.ClaudeCode) HeaderAction( + Icons.Outlined.Extension, stringResource(R.string.agent_tools), size = 36.dp, tag = "agent-tools", + ) { onOpen(false); onPanel(AgentPanel.TOOLS) } HeaderAction(Icons.Outlined.Add, stringResource(R.string.agent_new), enabled = !working && AgentHost.entries.isNotEmpty(), size = 36.dp, tag = "agent-new") { onOpen(false) AgentHost.forget(context) @@ -352,18 +402,20 @@ internal fun HeaderAction( } @Composable -private fun AgentConversation(modifier: Modifier, list: LazyListState, onRevise: () -> Unit) { +private fun AgentConversation(modifier: Modifier, list: LazyListState, following: Boolean, onRevise: () -> Unit, onSent: () -> Unit) { val pal = LocalPalette.current val working = AgentHost.state == AgentHost.State.WORKING - val atBottom by remember { derivedStateOf { !list.canScrollForward } } - val items = AgentHost.entries.size + (if (working) 1 else 0) + // A subagent's calls are shown inside the call that started it. + val shown = AgentHost.entries.indices.filter { (AgentHost.entries[it] as? Entry.Used)?.parent.isNullOrEmpty() } + val items = shown.size + (if (working) 1 else 0) + val follow by rememberUpdatedState(following) LaunchedEffect(items) { - if (items == 0) return@LaunchedEffect + if (items == 0 || !follow) return@LaunchedEffect list.animateScrollToItem(items - 1) list.scrollToItem(items - 1, Int.MAX_VALUE / 2) } LaunchedEffect(AgentHost.live.length / 80, AgentHost.liveThinking.length / 80) { - if (atBottom && items > 0) list.scrollToItem(items - 1, Int.MAX_VALUE / 2) + if (follow && items > 0) list.scrollToItem(items - 1, Int.MAX_VALUE / 2) } if (AgentHost.entries.isEmpty() && !working) { Column(modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { @@ -375,11 +427,15 @@ private fun AgentConversation(modifier: Modifier, list: LazyListState, onRevise: } val start = AgentHost.entries.indexOfLast { it is Entry.Asked } LazyColumn(modifier.fillMaxWidth(), state = list, verticalArrangement = Arrangement.spacedBy(10.dp)) { - items(AgentHost.entries.size) { i -> + items(shown.size) { n -> + val i = shown[n] val entry = AgentHost.entries[i] AgentEntry( entry, running = working && i > start && entry is Entry.Used && entry.output == null, last = !working && i == AgentHost.entries.lastIndex, - changes = if (entry is Entry.Finished) turnChanges(AgentHost.entries, i) else null, onRevise = onRevise, + children = if (entry is Entry.Used) AgentHost.entries.filter { it is Entry.Used && it.parent == entry.id && entry.id.isNotEmpty() }.map { it as Entry.Used } else emptyList(), + changes = if (entry is Entry.Finished) turnChanges(AgentHost.entries, i) else null, + turn = if (entry is Entry.Finished) AgentHost.entries.subList(AgentHost.entries.subList(0, i).indexOfLast { it is Entry.Asked } + 1, i) else emptyList(), + answerable = !working && i > start && AgentHost.queue.isEmpty(), onRevise = onRevise, onSent = onSent, ) } if (working) item { AgentLive() } @@ -564,15 +620,11 @@ internal fun usagePercent(): Int? { internal fun liveUsed(limit: AgentHost.Limit) = if (limit.resetsAt > 0 && limit.resetsAt * 1000 < System.currentTimeMillis()) 0 else limit.usedPercent @Composable -private fun AgentComposer( - composer: ComposerState, draftState: MutableState, inputModifier: Modifier, panel: AgentPanel, onPanel: (AgentPanel) -> Unit, - showControlsWithoutKeyboard: Boolean, -) { +private fun AgentComposer(composer: ComposerState, draftState: MutableState, visible: Boolean, onPanel: (AgentPanel) -> Unit, onSent: () -> Unit) { val context = LocalContext.current var draft by draftState var labOpen by rememberSaveable { mutableStateOf(false) } if (AgentHost.lab == null) labOpen = false - var hadFocus by remember { mutableStateOf(false) } val send = send@{ val text = draft.trim() when (text.substringBefore(' ')) { @@ -586,90 +638,33 @@ private fun AgentComposer( if (text.isEmpty() && AgentAttachments.pending.isEmpty()) return@send AgentHost.send(context, text, AgentAttachments.pending.toList()) AgentAttachments.pending.clear() + onSent() } } draft = "" } val keyboardOpen = WindowInsets.ime.getBottom(LocalDensity.current) > 0 - // Leaving the composer for the conversation, by touch or D-pad, is reading. - Column( - Modifier.fillMaxWidth().padding(bottom = if (keyboardOpen) 4.dp else 8.dp).onFocusChanged { - if (it.hasFocus) hadFocus = true - else if (hadFocus) { - hadFocus = false - if (composer.typing && !composer.opening) composer.typing = false - } - }, - ) { + Column(Modifier.fillMaxWidth().padding(bottom = if (keyboardOpen) 4.dp else 8.dp)) { if (labOpen) AgentLabCard() - when (panel) { - AgentPanel.MODEL -> Box(Modifier.padding(top = 8.dp)) { AgentModelPicker() } - AgentPanel.USAGE -> Box(Modifier.padding(top = 8.dp)) { AgentUsage() } - else -> Unit - } - if (composer.typing) { - if (hasActivity()) Row( - horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.padding(top = 8.dp).fillMaxWidth().horizontalScroll(rememberScrollState()), - ) { AgentActivity(labOpen) { labOpen = !labOpen } } - AgentEditor(composer, draftState, inputModifier, panel, onPanel, highlightControls = showControlsWithoutKeyboard, send = send) - } else AgentReadingBar( - composer, draft, inputModifier, labOpen, onLab = { labOpen = !labOpen }, onUsage = { onPanel(AgentPanel.USAGE) }, - ) { - // The session drawer is driven by a controller, so its editor opens without raising the keyboard. - if (showControlsWithoutKeyboard) { - composer.typing = true - composer.opening = true - } else composer.open() - } - } -} - -/** The composer while reading: one line holding what is going on, opened by a press or by Y. */ -@Composable -private fun AgentReadingBar( - composer: ComposerState, draft: String, inputModifier: Modifier, labOpen: Boolean, onLab: () -> Unit, onUsage: () -> Unit, onOpen: () -> Unit, -) { - val context = LocalContext.current - val colors = MaterialTheme.colorScheme - val pal = LocalPalette.current - val working = AgentHost.state == AgentHost.State.WORKING - var focused by remember { mutableStateOf(false) } - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.fillMaxWidth().padding(top = 8.dp).height(44.dp).clip(Shape16).background(colors.surface) - .border(if (focused) 2.dp else 1.dp, if (focused) pal.primary else pal.line, Shape16) - .then(inputModifier).onFocusChanged { focused = it.isFocused }.clickable(onClick = onOpen).testTag("agent-composer-bar") - .padding(start = 14.dp, end = 4.dp), - ) { - Text( - draft.ifBlank { stringResource(if (working) R.string.agent_hint_working else R.string.agent_message) }, fontSize = 15.sp, - color = if (draft.isBlank()) pal.onSurfaceVariant else colors.onSurface, maxLines = 1, overflow = TextOverflow.Ellipsis, - modifier = Modifier.weight(1f, fill = !hasActivity()).widthIn(min = 72.dp), - ) if (hasActivity()) Row( - horizontalArrangement = Arrangement.spacedBy(6.dp), verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.weight(1f).padding(start = 8.dp).horizontalScroll(rememberScrollState()), - ) { AgentActivity(labOpen, onLab) } - ContextRing("agent-usage-bar", onUsage) - if (working) Box( - contentAlignment = Alignment.Center, - modifier = Modifier.padding(start = 4.dp).size(36.dp).clip(CircleShape).background(pal.error.copy(alpha = 0.22f)) - .clickable { AgentHost.stop(context) }.testTag("agent-stop"), - ) { Icon(Icons.Outlined.Stop, stringResource(R.string.agent_stop), tint = colors.error, modifier = Modifier.size(20.dp)) } + horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.padding(top = 8.dp).fillMaxWidth().horizontalScroll(rememberScrollState()), + ) { AgentActivity(labOpen) { labOpen = !labOpen } } + AgentEditor(composer, draftState, visible, send) } } -/** The full editor: model and usage on top, the text with attach, stop and send under them. */ +/** The text with attach, stop and send beside it. */ @Composable -private fun AgentEditor( - composer: ComposerState, draftState: MutableState, inputModifier: Modifier, panel: AgentPanel, onPanel: (AgentPanel) -> Unit, - highlightControls: Boolean = false, send: () -> Unit, -) { +private fun AgentEditor(composer: ComposerState, draftState: MutableState, visible: Boolean, send: () -> Unit) { val context = LocalContext.current val colors = MaterialTheme.colorScheme val pal = LocalPalette.current var draft by draftState + val voice = remember { AgentVoice(context.applicationContext, { draftState.value }) { draftState.value = it } } + val voiceAvailable = remember { AgentVoice.available(context) } + DisposableEffect(voice) { onDispose { voice.stop() } } + LaunchedEffect(visible) { if (!visible) voice.stop() } val working = AgentHost.state == AgentHost.State.WORKING if (draft.startsWith("/") && !draft.contains(' ')) { val matches = (LOCAL_COMMANDS + CLI_COMMANDS).filter { it.startsWith(draft) && it != draft } @@ -694,26 +689,11 @@ private fun AgentEditor( LaunchedEffect(composer.focusRequests) { if (composer.hasUnconsumedFocusRequest()) runCatching { field.requestFocus() } } Column( Modifier.fillMaxWidth().padding(top = 8.dp).clip(Shape16).background(colors.surface) - .border(if (focused || highlightControls) 2.dp else 1.dp, if (focused || highlightControls) pal.primary else pal.line, Shape16).padding(6.dp), + .border(if (focused) 2.dp else 1.dp, if (focused) pal.primary else pal.line, Shape16).padding(6.dp), ) { - Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().height(36.dp)) { - val model = AgentHost.models(context).firstOrNull { it.id == AgentHost.model }?.label ?: AgentHost.model ?: stringResource(R.string.agent_model_default) - Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.weight(1f)) { - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.weight(1f, fill = false).height(30.dp).clip(Shape12).clickable { onPanel(AgentPanel.MODEL) } - .padding(horizontal = 8.dp).testTag("agent-model"), - ) { - val parts = listOfNotNull(AgentHost.harness.name, model, AgentHost.effort, stringResource(R.string.agent_plan_short).takeIf { AgentHost.plan && AgentHost.harness.plans }) - Text(parts.joinToString(" · "), fontSize = 13.sp, color = if (panel == AgentPanel.MODEL) pal.primary else pal.onSurfaceVariant, - maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f, fill = false)) - Icon(Icons.Outlined.ExpandMore, null, tint = pal.onSurfaceVariant, modifier = Modifier.size(16.dp)) - } - ContextRing("agent-usage", onClick = { onPanel(AgentPanel.USAGE) }) - } - } Row(verticalAlignment = Alignment.Bottom) { if (LocalActivityResultRegistryOwner.current != null) AttachButton() + if (AgentAttachments.gameFrame != null) GameFrameButton() Box(Modifier.weight(1f).padding(start = 8.dp, top = 10.dp, bottom = 10.dp)) { if (draft.isEmpty()) Text( stringResource(if (working) R.string.agent_hint_working else R.string.agent_hint), fontSize = 16.sp, @@ -723,15 +703,16 @@ private fun AgentEditor( draft, { draft = it }, Modifier.fillMaxWidth().focusRequester(field).onFocusChanged { focused = it.isFocused - if (it.isFocused) { - composer.consumeFocusRequest() - composer.opening = false - } - }.testTag("agent-input").then(inputModifier), + composer.focused = it.isFocused + if (it.isFocused) composer.consumeFocusRequest() + }.testTag("agent-input"), textStyle = TextStyle(fontSize = 16.sp, color = colors.onSurface), cursorBrush = SolidColor(pal.primary), keyboardOptions = KeyboardOptions(imeAction = ImeAction.Send), - keyboardActions = KeyboardActions(onSend = { send() }), + keyboardActions = KeyboardActions(onSend = { + voice.stop() + send() + }), minLines = 1, maxLines = 8, ) @@ -740,10 +721,63 @@ private fun AgentEditor( AgentHost.stop(context) } val canSend = draft.isNotBlank() || AgentAttachments.pending.isNotEmpty() - RoundButton( + // An empty message has nothing to send, so the button there dictates one instead. + if ((!canSend || voice.listening) && voiceAvailable) VoiceButton(voice) { voice.start(draft) } + else RoundButton( Icons.AutoMirrored.Outlined.Send, stringResource(if (working) R.string.agent_queue else R.string.agent_send), if (canSend) pal.primary else pal.surfaceVariant, if (canSend) pal.onPrimary else pal.onSurfaceVariant, "agent-send", enabled = canSend, - ) { send() } + ) { + voice.stop() + send() + } + } + } +} + +@Composable +private fun VoiceButton(voice: AgentVoice, onStart: () -> Unit) { + val context = LocalContext.current + val pal = LocalPalette.current + val ask = if (LocalActivityResultRegistryOwner.current != null) rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> + if (granted) onStart() else android.widget.Toast.makeText(context, R.string.agent_voice_denied, android.widget.Toast.LENGTH_SHORT).show() + } else null + RoundButton( + if (voice.listening) Icons.Outlined.MicOff else Icons.Outlined.Mic, stringResource(if (voice.listening) R.string.agent_voice_stop else R.string.agent_voice), + if (voice.listening) pal.primary else pal.surfaceVariant, if (voice.listening) pal.onPrimary else pal.onBackground, "agent-voice", + ) { + when { + voice.listening -> voice.finish() + context.checkSelfPermission(android.Manifest.permission.RECORD_AUDIO) == android.content.pm.PackageManager.PERMISSION_GRANTED -> onStart() + ask != null -> ask.launch(android.Manifest.permission.RECORD_AUDIO) + else -> android.widget.Toast.makeText(context, R.string.agent_voice_denied, android.widget.Toast.LENGTH_SHORT).show() + } + } +} + +/** Attaches what the running game shows, taken from its own surface so nothing drawn over it is in the picture. */ +@Composable +private fun GameFrameButton() { + val context = LocalContext.current + val pal = LocalPalette.current + RoundButton(Icons.Outlined.PhotoCamera, stringResource(R.string.agent_attach_game), pal.surfaceVariant, pal.onBackground, "agent-game-frame") { + val grab = AgentAttachments.gameFrame ?: return@RoundButton + val chat = AgentHost.chat + grab frame@{ frame -> + if (frame == null) { + android.widget.Toast.makeText(context, R.string.agent_attach_game_failed, android.widget.Toast.LENGTH_SHORT).show() + return@frame + } + Thread({ + val path = AgentAttachments.saveFrame(context, chat, frame) + frame.recycle() + android.os.Handler(android.os.Looper.getMainLooper()).post { + when { + path == null -> android.widget.Toast.makeText(context, R.string.agent_attach_game_failed, android.widget.Toast.LENGTH_SHORT).show() + AgentHost.chat == chat -> AgentAttachments.pending.add(path) + else -> AgentAttachments.discard(context, listOf(path)) + } + } + }, "agent-game-frame").start() } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AgentPanels.kt b/app/src/main/java/com/droiddeck/launcher/ui/AgentPanels.kt index eed2fd6d13..ce4135a73e 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AgentPanels.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AgentPanels.kt @@ -41,6 +41,18 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.text.BasicTextField +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.Close +import androidx.compose.material.icons.outlined.Search +import androidx.compose.material3.Icon +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.ui.graphics.SolidColor +import androidx.compose.ui.text.TextStyle +import androidx.compose.runtime.rememberCoroutineScope +import kotlinx.coroutines.launch +import com.droiddeck.launcher.agent.AgentTools import com.droiddeck.launcher.R import com.droiddeck.launcher.agent.AgentChats import com.droiddeck.launcher.agent.AgentHarness @@ -100,47 +112,88 @@ internal fun AgentHistory(modifier: Modifier, onOpened: () -> Unit) { } return } + var query by rememberSaveable { mutableStateOf("") } + val found by produceState?>(null, chats, query) { + if (query.isNotBlank()) delay(250) + value = withContext(Dispatchers.IO) { AgentChats.search(context, chats, query) } + } val working = AgentHost.state == AgentHost.State.WORKING - LazyColumn(modifier.fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, pal.line, Shape14)) { - items(chats, key = { it.id }) { chat -> - var armed by remember(chat.id) { mutableStateOf(false) } - val current = chat.id == AgentHost.chat - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.fillMaxWidth().heightIn(min = 62.dp) - .clickable(enabled = !working || current) { - AgentHost.open(context, chat.id) - onOpened() - } - .background(if (current) pal.primary.copy(alpha = 0.12f) else colors.surface) - .padding(horizontal = 14.dp, vertical = 10.dp).testTag("agent-chat"), - ) { - Column(Modifier.weight(1f)) { - Text(chat.title, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis) - Text( - if (armed) stringResource(R.string.agent_history_delete_warning) - else AgentHarness.byId(chat.harness).name + " · " + when { - System.currentTimeMillis() - chat.updated < 60_000 -> stringResource(R.string.agent_just_now) - else -> DateUtils.getRelativeTimeSpanString(chat.updated, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS).toString() - }, - fontSize = 12.5.sp, color = if (armed) AttentionAmber else pal.onSurfaceVariant, - ) - } - SecondaryButton( - stringResource(if (armed) R.string.agent_storage_confirm else R.string.agent_history_delete), compact = true, - enabled = !(current && working), - ) { - if (armed) { - AgentHost.delete(context, chat.id) - version++ - } else armed = true - } - } - Box(Modifier.fillMaxWidth().height(1.dp).background(pal.line)) + Column(modifier.fillMaxWidth()) { + AgentHistorySearch(query) { query = it } + val shown = found ?: chats + if (shown.isEmpty()) Box(Modifier.weight(1f).fillMaxWidth(), contentAlignment = Alignment.Center) { + Text(stringResource(R.string.agent_history_no_match), fontSize = 15.sp, color = pal.onSurfaceVariant) + } else LazyColumn(Modifier.weight(1f).fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, pal.line, Shape14)) { + items(shown, key = { it.id }) { chat -> AgentHistoryRow(chat, working, onOpened) { version++ } } } } } +@Composable +private fun AgentHistorySearch(query: String, onQuery: (String) -> Unit) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp).height(44.dp).clip(Shape14).background(colors.surface) + .border(1.dp, pal.line, Shape14).padding(horizontal = 12.dp), + ) { + Icon(Icons.Outlined.Search, null, tint = pal.onSurfaceVariant, modifier = Modifier.size(18.dp)) + Box(Modifier.weight(1f).padding(start = 8.dp)) { + if (query.isEmpty()) Text(stringResource(R.string.agent_history_search), fontSize = 15.sp, color = pal.onSurfaceVariant, maxLines = 1) + BasicTextField( + query, onQuery, singleLine = true, textStyle = TextStyle(fontSize = 15.sp, color = colors.onSurface), + cursorBrush = SolidColor(pal.primary), modifier = Modifier.fillMaxWidth().testTag("agent-history-search"), + ) + } + if (query.isNotEmpty()) Icon(Icons.Outlined.Close, stringResource(R.string.agent_close), tint = pal.onSurfaceVariant, + modifier = Modifier.size(28.dp).clip(CircleShape).clickable { onQuery("") }.padding(5.dp)) + } +} + +@Composable +private fun AgentHistoryRow(chat: AgentChats.Chat, working: Boolean, onOpened: () -> Unit, onDeleted: () -> Unit) { + val context = LocalContext.current + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + Column { + var armed by remember(chat.id) { mutableStateOf(false) } + val current = chat.id == AgentHost.chat + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth().heightIn(min = 62.dp) + .clickable(enabled = !working || current) { + AgentHost.open(context, chat.id) + onOpened() + } + .background(if (current) pal.primary.copy(alpha = 0.12f) else colors.surface) + .padding(horizontal = 14.dp, vertical = 10.dp).testTag("agent-chat"), + ) { + Column(Modifier.weight(1f)) { + Text(chat.title, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis) + Text( + if (armed) stringResource(R.string.agent_history_delete_warning) + else AgentHarness.byId(chat.harness).name + " · " + when { + System.currentTimeMillis() - chat.updated < 60_000 -> stringResource(R.string.agent_just_now) + else -> DateUtils.getRelativeTimeSpanString(chat.updated, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS).toString() + }, + fontSize = 12.5.sp, color = if (armed) AttentionAmber else pal.onSurfaceVariant, + ) + } + SecondaryButton( + stringResource(if (armed) R.string.agent_storage_confirm else R.string.agent_history_delete), compact = true, + enabled = !(current && working), + ) { + if (armed) { + AgentHost.delete(context, chat.id) + onDeleted() + } else armed = true + } + } + Box(Modifier.fillMaxWidth().height(1.dp).background(pal.line)) + } +} + /** Processes in the Linux userland, the agent's own first, each with a way to end it. */ @Composable internal fun AgentProcessList(modifier: Modifier) { @@ -260,3 +313,117 @@ private fun resetTime(context: android.content.Context, epochSeconds: Long): Str else DateUtils.FORMAT_SHOW_WEEKDAY or DateUtils.FORMAT_ABBREV_WEEKDAY or DateUtils.FORMAT_SHOW_TIME return DateUtils.formatDateTime(context, ms, flags) } + +/** Claude Code's MCP servers, with a way to add and remove them, and the skills in its skills folder. */ +@Composable +internal fun AgentToolsPanel(modifier: Modifier) { + val context = LocalContext.current + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val scope = rememberCoroutineScope() + var version by remember { mutableIntStateOf(0) } + var loading by remember { mutableStateOf(true) } + val servers by produceState?>(null, version) { + loading = true + value = withContext(Dispatchers.IO) { AgentTools.servers(context) } + loading = false + } + val skills by produceState(emptyList(), version) { value = withContext(Dispatchers.IO) { AgentTools.skills(context) } } + var name by rememberSaveable { mutableStateOf("") } + var target by rememberSaveable { mutableStateOf("") } + var busy by remember { mutableStateOf(false) } + var message by remember { mutableStateOf(null) } + fun act(added: Boolean = false, action: suspend () -> AgentTools.Result) { + busy = true + message = null + scope.launch { + val result = withContext(Dispatchers.IO) { action() } + busy = false + if (!result.ok) message = result.output.ifBlank { context.getString(R.string.agent_tools_failed) } + else if (added) { + name = "" + target = "" + } + version++ + } + } + LazyColumn(modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { + item { ToolsHeading(stringResource(R.string.agent_tools_servers)) } + val list = servers + when { + loading && list == null -> item { Text(stringResource(R.string.agent_tools_loading), fontSize = 13.sp, color = pal.onSurfaceVariant) } + list == null -> item { Text(stringResource(R.string.agent_tools_failed), fontSize = 13.sp, color = AttentionAmber) } + list.isEmpty() -> item { Text(stringResource(R.string.agent_tools_no_servers), fontSize = 13.sp, color = pal.onSurfaceVariant) } + else -> items(list, key = { "mcp:" + it.name }) { server -> + var armed by remember(server.name) { mutableStateOf(false) } + ToolsRow(server.name, server.target + " · " + server.status) { + if (!server.account) SecondaryButton( + stringResource(if (armed) R.string.agent_storage_confirm else R.string.agent_tools_remove), compact = true, enabled = !busy, + ) { if (armed) act { AgentTools.remove(context, server.name) } else armed = true } + } + } + } + item { + Column( + Modifier.fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, pal.line, Shape14).padding(12.dp), + verticalArrangement = Arrangement.spacedBy(8.dp), + ) { + ToolsField(name, { name = it }, stringResource(R.string.agent_tools_name), "agent-tools-name") + ToolsField(target, { target = it }, stringResource(R.string.agent_tools_target), "agent-tools-target") + Row(verticalAlignment = Alignment.CenterVertically) { + Text(stringResource(R.string.agent_tools_add_hint), fontSize = 12.sp, color = pal.onSurfaceVariant, modifier = Modifier.weight(1f)) + PrimaryButton(stringResource(R.string.agent_tools_add), enabled = !busy && name.isNotBlank() && target.isNotBlank(), compact = true) { + act(added = true) { AgentTools.add(context, name, target) } + } + } + message?.let { Text(it, fontSize = 12.sp, fontFamily = FontFamily.Monospace, color = AttentionAmber) } + } + } + item { ToolsHeading(stringResource(R.string.agent_tools_skills)) } + if (skills.isEmpty()) item { Text(stringResource(R.string.agent_tools_no_skills), fontSize = 13.sp, color = pal.onSurfaceVariant) } + items(skills, key = { "skill:" + it.name }) { skill -> + var armed by remember(skill.name) { mutableStateOf(false) } + ToolsRow(skill.name, skill.description) { + SecondaryButton(stringResource(if (armed) R.string.agent_storage_confirm else R.string.agent_history_delete), compact = true, enabled = !busy) { + if (armed) act { + AgentTools.Result(AgentTools.deleteSkill(context, skill.name), "") + } else armed = true + } + } + } + } +} + +@Composable +private fun ToolsHeading(text: String) { + Text(text, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, color = LocalPalette.current.onSurfaceVariant, modifier = Modifier.padding(top = 6.dp)) +} + +@Composable +private fun ToolsRow(title: String, detail: String, action: @Composable () -> Unit) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, pal.line, Shape14).padding(horizontal = 14.dp, vertical = 10.dp), + ) { + Column(Modifier.weight(1f)) { + Text(title, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis) + if (detail.isNotBlank()) Text(detail, fontSize = 12.5.sp, color = pal.onSurfaceVariant, maxLines = 2, overflow = TextOverflow.Ellipsis) + } + action() + } +} + +@Composable +private fun ToolsField(value: String, onValue: (String) -> Unit, hint: String, tag: String) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + Box(Modifier.fillMaxWidth().clip(Shape12).background(pal.surfaceVariant).padding(horizontal = 12.dp, vertical = 10.dp)) { + if (value.isEmpty()) Text(hint, fontSize = 14.sp, color = pal.onSurfaceVariant, maxLines = 1) + BasicTextField( + value, onValue, singleLine = true, textStyle = TextStyle(fontSize = 14.sp, fontFamily = FontFamily.Monospace, color = colors.onSurface), + cursorBrush = SolidColor(pal.primary), modifier = Modifier.fillMaxWidth().testTag(tag), + ) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AgentPetWindows.kt b/app/src/main/java/com/droiddeck/launcher/ui/AgentPetWindows.kt index b79bed3726..cea5fcaf3b 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AgentPetWindows.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AgentPetWindows.kt @@ -22,7 +22,6 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height @@ -34,7 +33,6 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.Close import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableFloatStateOf @@ -62,9 +60,7 @@ import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.input.key.onPreviewKeyEvent import androidx.compose.ui.platform.ComposeView 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 androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleRegistry @@ -557,10 +553,7 @@ internal fun PetChat(open: Boolean, motion: PetMotion, onClose: () -> Unit, onCl .padding(start = 12.dp, end = 12.dp, top = 6.dp), ) { Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().padding(bottom = 2.dp)) { - Text(stringResource(R.string.agent_pet_name), fontSize = 16.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, - modifier = Modifier.padding(start = 4.dp)) - Spacer(Modifier.weight(1f)) - menuSlot.content?.invoke() + Box(Modifier.weight(1f)) { menuSlot.content?.invoke() } HeaderAction(Icons.Outlined.Close, stringResource(R.string.agent_close), size = 34.dp, tag = "agent-pet-close", onClick = onClose) } AgentPage(Modifier.weight(1f).fillMaxWidth(), visible = open, startTyping = AgentHost.entries.isEmpty(), hidesPet = false, menuSlot = menuSlot) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AgentVoice.kt b/app/src/main/java/com/droiddeck/launcher/ui/AgentVoice.kt new file mode 100644 index 0000000000..a6d78e599e --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/AgentVoice.kt @@ -0,0 +1,74 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.speech.RecognitionListener +import android.speech.RecognizerIntent +import android.speech.SpeechRecognizer +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue + +/** Dictation into the Agent editor: what is heard so far follows the text already typed, until the user edits the draft. */ +internal class AgentVoice(private val context: Context, private val draft: () -> String, private val onText: (String) -> Unit) { + var listening by mutableStateOf(false) + private set + private var recognizer: SpeechRecognizer? = null + private var base = "" + private var written = "" + + fun start(typed: String) { + stop() + val speech = runCatching { SpeechRecognizer.createSpeechRecognizer(context) }.getOrNull() ?: return + recognizer = speech + base = typed.trimEnd().let { if (it.isEmpty()) it else "$it " } + written = typed + speech.setRecognitionListener(object : RecognitionListener { + override fun onReadyForSpeech(params: Bundle?) { listening = true } + override fun onBeginningOfSpeech() {} + override fun onRmsChanged(rmsdB: Float) {} + override fun onBufferReceived(buffer: ByteArray?) {} + override fun onEndOfSpeech() {} + override fun onError(error: Int) { if (recognizer === speech) stop() } + override fun onResults(results: Bundle?) { + if (recognizer !== speech) return + heard(results) + stop() + } + override fun onPartialResults(partialResults: Bundle?) { if (recognizer === speech) heard(partialResults) } + override fun onEvent(eventType: Int, params: Bundle?) {} + }) + val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) + .putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) + .putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true) + .putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.packageName) + runCatching { speech.startListening(intent) }.onFailure { stop() } + } + + private fun heard(results: Bundle?) { + val spoken = results?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)?.firstOrNull()?.takeIf { it.isNotBlank() } ?: return + if (draft() != written) return stop() + written = base + spoken + onText(written) + } + + /** Ends listening but keeps the recognizer until its final result arrives. */ + fun finish() { + val speech = recognizer ?: return + listening = false + runCatching { speech.stopListening() }.onFailure { stop() } + } + + fun stop() { + listening = false + val speech = recognizer ?: return + recognizer = null + runCatching { speech.cancel() } + runCatching { speech.destroy() } + } + + companion object { + fun available(context: Context) = SpeechRecognizer.isRecognitionAvailable(context) + } +} diff --git a/app/src/main/res/values-ar/strings.xml b/app/src/main/res/values-ar/strings.xml index f288b97098..191f4a79e3 100644 --- a/app/src/main/res/values-ar/strings.xml +++ b/app/src/main/res/values-ar/strings.xml @@ -1179,7 +1179,6 @@ لم يتغير شيء بعد المضي قدماً في الخطة. إرسال الآن - رسالة المزيد إغلاق الأحدث @@ -2683,7 +2682,6 @@ فوق التطبيقات الأخرى اسمح لـ DeeDee بالظهور فوق كل التطبيقات وفتح المحادثة هناك السماح - DeeDee الدردشة مع DeeDee العرض بطاقات أكبر diff --git a/app/src/main/res/values-b+es+419/strings.xml b/app/src/main/res/values-b+es+419/strings.xml index 3d2a2ed8fa..c892f88c8e 100644 --- a/app/src/main/res/values-b+es+419/strings.xml +++ b/app/src/main/res/values-b+es+419/strings.xml @@ -1197,7 +1197,6 @@ Aún no hay cambios Adelante con el plan. Enviar ahora - Mensaje Más Cerrar Más reciente @@ -2615,7 +2614,6 @@ Sobre otras aplicaciones Permite que DeeDee flote sobre todas las aplicaciones y abra su chat allí Permitir - DeeDee Chatear con DeeDee Vista Tarjetas más grandes diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 089077750e..a2760406e6 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -1167,7 +1167,6 @@ Още нищо не е променено Изпълни плана. Изпрати сега - Съобщение Още Затвори Най-новото @@ -2579,7 +2578,6 @@ Над други приложения Позволи на DeeDee да се показва над всяко приложение и да отваря чата си там Разреши - DeeDee Чат с DeeDee Изглед По-големи карти diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 241b3903e0..c0d54caa91 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -1173,7 +1173,6 @@ Zatím se nic nezměnilo Proveď plán. Odeslat nyní - Zpráva Více Zavřít Nejnovější @@ -2631,7 +2630,6 @@ Nad ostatními aplikacemi Umožněte DeeDee zobrazovat se nad všemi aplikacemi a otevírat v nich chat. Povolit - DeeDee Chat s DeeDee Zobrazení Větší karty diff --git a/app/src/main/res/values-da/strings.xml b/app/src/main/res/values-da/strings.xml index 57cbc9f72d..443b2a96cb 100644 --- a/app/src/main/res/values-da/strings.xml +++ b/app/src/main/res/values-da/strings.xml @@ -1167,7 +1167,6 @@ Intet ændret endnu Gå i gang med planen. Send nu - Besked Mere Luk Seneste @@ -2579,7 +2578,6 @@ Over andre apps Lad DeeDee svæve over alle apps og åbne chatten der Tillad - DeeDee Chat med DeeDee Visning Større kort diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 6d14e4b65f..41e8e685eb 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -1159,7 +1159,6 @@ Noch nichts geändert Führe den Plan aus. Jetzt senden - Nachricht Mehr Schließen Neueste @@ -2571,7 +2570,6 @@ Über anderen Apps DeeDee über allen Apps schweben lassen und den Chat dort öffnen Erlauben - DeeDee Mit DeeDee chatten Anzeige Größere Karten diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index 9b27f1a637..70c09dfcde 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -1167,7 +1167,6 @@ Δεν έχει αλλάξει τίποτα ακόμη Προχώρησε με το σχέδιο. Αποστολή τώρα - Μήνυμα Περισσότερα Κλείσιμο Πιο πρόσφατο @@ -2579,7 +2578,6 @@ Πάνω από άλλες εφαρμογές Αφήστε το DeeDee να εμφανίζεται πάνω από κάθε εφαρμογή και να ανοίγει εκεί τη συνομιλία Να επιτρέπεται - DeeDee Συνομιλία με DeeDee Προβολή Μεγαλύτερες κάρτες diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 06576af8fd..dedf2b43ff 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -1170,7 +1170,6 @@ Aún no hay cambios Adelante con el plan. Enviar ahora - Mensaje Más Cerrar Más reciente @@ -2605,7 +2604,6 @@ Sobre otras aplicaciones Permite que DeeDee flote sobre todas las aplicaciones y abra su chat allí Permitir - DeeDee Chat con DeeDee Vista Tarjetas más grandes diff --git a/app/src/main/res/values-fi/strings.xml b/app/src/main/res/values-fi/strings.xml index 47243b27f1..8d0e351def 100644 --- a/app/src/main/res/values-fi/strings.xml +++ b/app/src/main/res/values-fi/strings.xml @@ -1167,7 +1167,6 @@ Ei muutoksia vielä Aloita suunnitelman toteutus. Lähetä nyt - Viesti Lisää Sulje Uusin @@ -2579,7 +2578,6 @@ Muiden sovellusten päällä Anna DeeDee:n leijua kaikkien sovellusten päällä ja avata keskustelunsa siellä Salli - DeeDee Keskustele DeeDee:n kanssa Näkymä Suuremmat kortit diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 5942840c08..b2752c83c2 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -1170,7 +1170,6 @@ Rien n’a encore changé Exécute le plan. Envoyer maintenant - Message Plus Fermer Dernier @@ -2604,7 +2603,6 @@ Au-dessus des autres applications Autorise DeeDee à flotter au-dessus de toutes les applications et à y ouvrir sa discussion Autoriser - DeeDee Discuter avec DeeDee Affichage Cartes plus grandes diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml index 42b60b68d3..87be428351 100644 --- a/app/src/main/res/values-hi/strings.xml +++ b/app/src/main/res/values-hi/strings.xml @@ -1167,7 +1167,6 @@ अभी तक कुछ नहीं बदला योजना के साथ आगे बढ़ें। अभी भेजें - संदेश अधिक बंद करें नवीनतम @@ -2579,7 +2578,6 @@ अन्य ऐप्स के ऊपर दिखाएँ DeeDee को हर ऐप के ऊपर दिखने दें और वहीं इसकी चैट खोलें अनुमति दें - DeeDee DeeDee के साथ चैट करें डिस्प्ले बड़े कार्ड diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index 1831f4ffa6..f99551869c 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -1167,7 +1167,6 @@ Még semmi sem változott Hajtsd végre a tervet. Küldés most - Üzenet Továbbiak Bezárás Legújabb @@ -2579,7 +2578,6 @@ Más alkalmazások felett Engedd, hogy DeeDee minden alkalmazás felett lebegjen és ott nyissa meg a csevegést Engedélyezés - DeeDee Csevegés DeeDee-vel Megjelenítés Nagyobb kártyák diff --git a/app/src/main/res/values-in/strings.xml b/app/src/main/res/values-in/strings.xml index 983958bbfd..9c81a06c3f 100644 --- a/app/src/main/res/values-in/strings.xml +++ b/app/src/main/res/values-in/strings.xml @@ -1164,7 +1164,6 @@ Belum ada perubahan Jalankan rencana ini. Kirim sekarang - Pesan Lainnya Tutup Terbaru @@ -2554,7 +2553,6 @@ Di atas aplikasi lain Izinkan DeeDee melayang di atas semua aplikasi dan membuka obrolannya di sana Izinkan - DeeDee Chat dengan DeeDee Tampilan Kartu lebih besar diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index ffc9b59f63..cbca952ea9 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -1162,7 +1162,6 @@ Ancora nessuna modifica Procedi con il piano. Invia ora - Messaggio Altro Chiudi Più recente @@ -2596,7 +2595,6 @@ Sopra altre app Consenti a DeeDee di apparire sopra tutte le app e di aprire lì la sua chat Consenti - DeeDee Apri la chat con DeeDee Visualizzazione Schede più grandi diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 7e292b3f77..93dda1d018 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -1164,7 +1164,6 @@ まだ変更はありません 計画を進めてください。 今すぐ送信 - メッセージ その他 閉じる 最新 @@ -2554,7 +2553,6 @@ 他のアプリの上に表示 DeeDee をすべてのアプリの上に表示し、そこでチャットを開けるようにします 許可 - DeeDee DeeDee とチャット 表示 大きいカード diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 3ce35f034f..048dced14f 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -1164,7 +1164,6 @@ 아직 변경된 내용 없음 계획대로 진행해 주세요. 지금 보내기 - 메시지 더보기 닫기 최신 @@ -2554,7 +2553,6 @@ 다른 앱 위에 표시 DeeDee가 모든 앱 위에 표시되도록 허용하고 해당 앱에서 채팅을 열 수 있게 합니다. 허용 - DeeDee DeeDee와 채팅하기 표시 카드 크게 diff --git a/app/src/main/res/values-ms/strings.xml b/app/src/main/res/values-ms/strings.xml index 4f0cbd5362..7d0a634ed9 100644 --- a/app/src/main/res/values-ms/strings.xml +++ b/app/src/main/res/values-ms/strings.xml @@ -1164,7 +1164,6 @@ Belum ada perubahan Teruskan dengan pelan ini. Hantar sekarang - Mesej Lagi Tutup Terkini @@ -2554,7 +2553,6 @@ Di atas apl lain Benarkan DeeDee terapung di atas setiap apl dan buka sembangnya di situ Benarkan - DeeDee Sembang dengan DeeDee Paparan Kad lebih besar diff --git a/app/src/main/res/values-nb/strings.xml b/app/src/main/res/values-nb/strings.xml index 21d08282a7..43678cb4ad 100644 --- a/app/src/main/res/values-nb/strings.xml +++ b/app/src/main/res/values-nb/strings.xml @@ -1167,7 +1167,6 @@ Ingen endringer ennå Gå videre med planen. Send nå - Melding Mer Lukk Nyeste @@ -2579,7 +2578,6 @@ Over andre apper La DeeDee flyte over alle apper og åpne chatten sin der Tillat - DeeDee Chat med DeeDee Visning Større kort diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 4b514189f2..5247d1fedd 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -1167,7 +1167,6 @@ Nog niets gewijzigd Ga door met het plan. Nu verzenden - Bericht Meer Sluiten Nieuwste @@ -2579,7 +2578,6 @@ Boven andere apps Laat DeeDee boven elke app zweven en open daar de chat Toestaan - DeeDee Chatten met DeeDee Weergave Grotere kaarten diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 5c0f3ec1b0..de994d5239 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -1173,7 +1173,6 @@ Nic jeszcze nie zmieniono Wykonaj plan. Wyślij teraz - Wiadomość Więcej Zamknij Najnowsze @@ -2631,7 +2630,6 @@ Nad innymi aplikacjami Pozwól DeeDee wyświetlać się nad każdą aplikacją i otwierać tam jej czat Zezwól - DeeDee Czat z DeeDee Wyświetlanie Większe karty diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index ef63016613..dc549260d8 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -1162,7 +1162,6 @@ Ainda nada mudou Avançar com o plano. Enviar agora - Mensagem Mais Fechar Mais recente @@ -2596,7 +2595,6 @@ Sobre outras aplicações Permitir que a DeeDee flutue sobre todas as aplicações e abra aí a conversa Permitir - DeeDee Conversar com a DeeDee Apresentação Cartões maiores diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index b559645180..7f8d3196b6 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -1162,7 +1162,6 @@ Nada mudou ainda Siga em frente com o plano. Enviar agora - Mensagem Mais Fechar Mais recente @@ -2597,7 +2596,6 @@ Sobre outros apps Permita que DeeDee flutue sobre todos os apps e abra o chat dela ali Permitir - DeeDee Conversar com DeeDee Exibição Cards maiores diff --git a/app/src/main/res/values-ro/strings.xml b/app/src/main/res/values-ro/strings.xml index fb1390fd11..9c28d24295 100644 --- a/app/src/main/res/values-ro/strings.xml +++ b/app/src/main/res/values-ro/strings.xml @@ -1170,7 +1170,6 @@ Nimic nu s-a schimbat încă Continuă cu planul. Trimite acum - Mesaj Mai multe Închide Cele mai recente @@ -2605,7 +2604,6 @@ Peste alte aplicații Permite ca DeeDee să plutească peste orice aplicație și să deschidă chatul acolo Permite - DeeDee Discută cu DeeDee Afișare Carduri mai mari diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 4499bc9d88..8a98c2b2a2 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -1173,7 +1173,6 @@ Изменений пока нет Действуйте по плану. Отправить сейчас - Сообщение Ещё Закрыть Последняя версия @@ -2631,7 +2630,6 @@ Поверх других приложений Разрешить DeeDee отображаться поверх всех приложений и открывать там чат с DeeDee Разрешить - DeeDee Чат с DeeDee Вид Крупные карточки diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index 05705053ca..427d722263 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -1167,7 +1167,6 @@ Inget har ändrats ännu Kör planen. Skicka nu - Meddelande Mer Stäng Senaste @@ -2579,7 +2578,6 @@ Över andra appar Låt DeeDee sväva över alla appar och öppna chatten där Tillåt - DeeDee Chatta med DeeDee Visning Större kort diff --git a/app/src/main/res/values-th/strings.xml b/app/src/main/res/values-th/strings.xml index 25399225fc..fb4139f53b 100644 --- a/app/src/main/res/values-th/strings.xml +++ b/app/src/main/res/values-th/strings.xml @@ -1164,7 +1164,6 @@ ยังไม่มีการเปลี่ยนแปลง ดำเนินการตามแผนได้เลย ส่งตอนนี้ - ข้อความ เพิ่มเติม ปิด ล่าสุด @@ -2554,7 +2553,6 @@ เหนือแอปอื่น ให้ DeeDee ลอยเหนือทุกแอปและเปิดแชทได้จากตรงนั้น อนุญาต - DeeDee แชทกับ DeeDee การแสดงผล การ์ดใหญ่ขึ้น diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index 4deeaafe3d..9623e13648 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -1159,7 +1159,6 @@ Henüz bir şey değişmedi Planla devam et. Şimdi gönder - Mesaj Diğer Kapat En son @@ -2571,7 +2570,6 @@ Diğer uygulamaların üzerinde DeeDee\'nin her uygulamanın üzerinde görünmesine ve sohbetini orada açmasına izin ver İzin ver - DeeDee DeeDee ile sohbet et Görünüm Daha büyük kartlar diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index a459dc4444..15ab8c9c4d 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -1173,7 +1173,6 @@ Поки нічого не змінено Виконайте план. Надіслати зараз - Повідомлення Ще Закрити Найновіше @@ -2631,7 +2630,6 @@ Поверх інших застосунків Дозволити DeeDee відображатися поверх усіх застосунків і відкривати там чат Дозволити - DeeDee Чат з DeeDee Вигляд Великі картки diff --git a/app/src/main/res/values-vi/strings.xml b/app/src/main/res/values-vi/strings.xml index c048cfd5ac..f133ef3d94 100644 --- a/app/src/main/res/values-vi/strings.xml +++ b/app/src/main/res/values-vi/strings.xml @@ -1164,7 +1164,6 @@ Chưa có thay đổi nào Hãy tiến hành kế hoạch. Gửi ngay - Tin nhắn Khác Đóng Mới nhất @@ -2554,7 +2553,6 @@ Trên các ứng dụng khác Cho DeeDee nổi trên mọi ứng dụng và mở khung trò chuyện của DeeDee ngay tại đó Cho phép - DeeDee Trò chuyện với DeeDee Hiển thị Thẻ lớn hơn diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index ec65c282eb..0b5996252e 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -1247,7 +1247,6 @@ 尚未更改 按计划执行。 立即发送 - 消息 更多 关闭 最新 @@ -2631,7 +2630,6 @@ 显示在其他应用上层 让 DeeDee 悬浮在所有应用上层,并在那里打开聊天 允许 - DeeDee 与 DeeDee 聊天 显示 较大卡片 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 3b5883d3a3..709bda78c9 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -1163,7 +1163,6 @@ 尚未有任何更改 按計劃執行。 立即傳送 - 訊息 更多 關閉 最新 @@ -2551,7 +2550,6 @@ 浮動於其他應用程式之上 讓 DeeDee 浮動於所有應用程式之上,並可在該處開啟對話 允許 - DeeDee 與 DeeDee 對話 顯示 較大卡片 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 7661d16baa..c080ab3d47 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -1163,7 +1163,6 @@ 尚未變更 按計畫執行。 立即傳送 - 訊息 更多 關閉 最新 @@ -2551,7 +2550,6 @@ 顯示在其他應用程式上層 讓 DeeDee 浮動顯示在所有應用程式上方,並在該處開啟與 DeeDee 的聊天視窗 允許 - DeeDee 與 DeeDee 聊天 顯示 較大的卡片 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2d6a82851b..5cedd4524d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1182,6 +1182,35 @@ Agent Agent Agent is working + MCP servers and skills + MCP servers + Skills in ~/.claude/skills + Asking Claude Code… + Claude Code could not do that + No MCP servers yet + No skills yet + Remove + Name + URL, or the command that starts it + Added for every project + Add + Dictate + Stop dictating + DroidDeck needs the microphone to dictate + Attach what the game shows + Could not capture the game + Agent replies + Agent finished + Agent stopped with an error + Agent has a question + + %d step + %d steps + + Copy + Copy the reply + Search conversations + No conversation mentions that Install Claude Code in the Linux userland to use the agent. Ask about a game, a crash, or a change to DroidDeck Send @@ -1229,7 +1258,6 @@ Nothing changed yet Go ahead with the plan. Send now - Message More Close Latest @@ -2725,7 +2753,6 @@ Over other apps Let DeeDee float over every app and open its chat there Allow - DeeDee Chat with DeeDee Display Larger cards diff --git a/app/src/test/java/com/droiddeck/launcher/agent/AgentChoicesTest.kt b/app/src/test/java/com/droiddeck/launcher/agent/AgentChoicesTest.kt new file mode 100644 index 0000000000..8751b64b09 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/agent/AgentChoicesTest.kt @@ -0,0 +1,68 @@ +package com.droiddeck.launcher.agent + +import org.junit.Assert.assertEquals +import org.junit.Test + +class AgentChoicesTest { + @Test fun aTrailingChoicesBlockBecomesOptions() { + val reply = choices("Which build?\n\n```choices\n- Debug\n- Release\n```\n") + assertEquals("Which build?", reply.text) + assertEquals(listOf("Debug", "Release"), reply.options) + } + + @Test fun plainLinesAreOptionsToo() { + assertEquals(listOf("Yes", "No"), choices("Go on?\n```choices\nYes\nNo\n```").options) + } + + @Test fun aReplyWithoutTheBlockStaysWhole() { + val text = "Done.\n```kotlin\nval a = 1\n```" + assertEquals(Choices(text, emptyList()), choices(text)) + } + + @Test fun aChoicesBlockFollowedByAnotherFenceIsNotAQuestion() { + val text = "```choices\nA\nB\n```\nExplanation.\n```kotlin\nval x = 1\n```" + assertEquals(Choices(text, emptyList()), choices(text)) + } + + @Test fun aChoicesBlockFollowedByTextIsNotAQuestion() { + val text = "```choices\nA\nB\n```\nMore after it." + assertEquals(Choices(text, emptyList()), choices(text)) + } + + @Test fun backticksInsideAChoiceAreKept() { + val reply = choices("How?\n```choices\nUse ``` for code\nUse indentation\n```") + assertEquals("How?", reply.text) + assertEquals(listOf("Use ``` for code", "Use indentation"), reply.options) + } + + @Test fun aLongerOpenerNeedsAClosingFenceAsLong() { + val reply = choices("````choices\nA\n```\nB\n````") + assertEquals(listOf("A", "```", "B"), reply.options) + } + + @Test fun aChoicesExampleInsideACodeBlockStaysLiteral() { + val text = "Example:\n````markdown\n```choices\nA\nB\n````" + assertEquals(Choices(text, emptyList()), choices(text)) + } + + @Test fun anUnclosedChoicesBlockIsNotAQuestion() { + val text = "Pick:\n```choices\nA\nB" + assertEquals(Choices(text, emptyList()), choices(text)) + } + + @Test fun aChoicesExampleInsideAnUnclosedTildeFenceStaysLiteral() { + val text = "Example:\n~~~markdown\n```choices\nA\nB\n```" + assertEquals(Choices(text, emptyList()), choices(text)) + } + + @Test fun aChoicesBlockAfterAClosedTildeFenceCounts() { + val reply = choices("~~~\ncode\n~~~\nPick:\n```choices\nA\nB\n```") + assertEquals(listOf("A", "B"), reply.options) + } + + @Test fun aTildeChoicesBlockBecomesOptions() { + val reply = choices("Pick:\n~~~choices\nYes\nNo\n~~~") + assertEquals("Pick:", reply.text) + assertEquals(listOf("Yes", "No"), reply.options) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/agent/AgentHostTest.kt b/app/src/test/java/com/droiddeck/launcher/agent/AgentHostTest.kt index 11ecd7aea8..5cfc53ddee 100644 --- a/app/src/test/java/com/droiddeck/launcher/agent/AgentHostTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/agent/AgentHostTest.kt @@ -32,6 +32,22 @@ class AgentHostTest { assertEquals(105L, event.context) } + @Test fun aSubagentShowsOnlyItsToolCallsUnderItsParent() { + val event = ClaudeCode.parse( + """{"type":"assistant","session_id":"abc","parent_tool_use_id":"task1","message":{"content":[ + {"type":"text","text":"I will look."}, + {"type":"tool_use","id":"t2","name":"Grep","input":{"pattern":"crash"}}],"usage":{"input_tokens":10,"output_tokens":5}}}""", + )!! + assertEquals(listOf(Entry.Used("t2", "Grep", "crash", "{\n \"pattern\": \"crash\"\n}", parent = "task1")), event.entries) + assertNull(event.context) + assertNull(ClaudeCode.parse("""{"type":"stream_event","parent_tool_use_id":"task1","event":{"type":"content_block_delta","delta":{"type":"text_delta","text":"x"}}}""")) + } + + @Test fun aParentlessMessageReadsTheNullAsNoParent() { + val event = ClaudeCode.parse("""{"type":"assistant","parent_tool_use_id":null,"message":{"content":[{"type":"text","text":"hi"}]}}""")!! + assertEquals(listOf(Entry.Said("hi")), event.entries) + } + @Test fun claudeStreamsTextAsItIsWritten() { val event = ClaudeCode.parse("""{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hel"}}}""")!! assertEquals("Hel", event.text) diff --git a/app/src/test/java/com/droiddeck/launcher/agent/AgentToolsTest.kt b/app/src/test/java/com/droiddeck/launcher/agent/AgentToolsTest.kt new file mode 100644 index 0000000000..a806832b4e --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/agent/AgentToolsTest.kt @@ -0,0 +1,53 @@ +package com.droiddeck.launcher.agent + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import java.io.File +import java.nio.file.Files + +class AgentToolsTest { + @Test fun serversAreReadFromTheListOutput() { + val output = "Checking MCP server health…\n\nclaude.ai Claude Docs: https://api.anthropic.com/v1/pages/mcp - ✔ Connected\n" + + "files: npx -y @modelcontextprotocol/server-filesystem /root - ✗ Failed to connect\n" + val servers = AgentTools.parseServers(output) + assertEquals(listOf("claude.ai Claude Docs", "files"), servers.map { it.name }) + assertEquals("npx -y @modelcontextprotocol/server-filesystem /root", servers[1].target) + assertTrue(servers[0].account) + } + + @Test fun aUrlIsAddedOverHttpAndACommandOverStdio() { + assertEquals(listOf("mcp", "add", "--scope", "user", "--transport", "http", "sentry", "https://mcp.sentry.dev/mcp"), + AgentTools.addCommand("sentry", " https://mcp.sentry.dev/mcp ").drop(1)) + assertEquals(listOf("mcp", "add", "--scope", "user", "files", "--", "npx", "-y", "server", "/root"), + AgentTools.addCommand("files", "npx -y server /root").drop(1)) + } + + @Test fun aCommandKeepsItsQuotedArguments() { + assertEquals(listOf("python", "/root/My Tools/server.py", "--name", "it's", "a b"), + AgentTools.words("""python "/root/My Tools/server.py" --name "it's" a\ b""")) + assertEquals(listOf("sh", "-c", "echo \"hi\""), AgentTools.words("""sh -c 'echo "hi"'""")) + } + + @Test fun deletingASkillLeavesWhatItsLinksPointTo() { + val outside = Files.createTempDirectory("outside").toFile() + File(outside, "keep.txt").writeText("keep") + val skill = Files.createTempDirectory("skill").toFile() + File(skill, "SKILL.md").writeText("x") + Files.createSymbolicLink(File(skill, "resources").toPath(), outside.toPath()) + assertTrue(AgentTools.deleteTree(skill.toPath())) + assertTrue(!skill.exists()) + assertTrue(File(outside, "keep.txt").isFile) + outside.deleteRecursively() + } + + @Test fun aSkillsDescriptionComesFromItsFrontMatter() { + val file = Files.createTempFile("skill", ".md").toFile() + file.writeText("---\nname: deploy\ndescription: \"Ship the build\"\n---\nBody\n") + assertEquals("Ship the build", AgentTools.description(file)) + file.writeText("No front matter\ndescription: nope\n") + assertEquals("", AgentTools.description(file)) + file.delete() + assertEquals("", AgentTools.description(File(file.path))) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/AgentChangesTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/AgentChangesTest.kt index 38eddd563b..2481c64c74 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/AgentChangesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/AgentChangesTest.kt @@ -38,4 +38,9 @@ class AgentChangesTest { assertEquals("1.5M", tokens(1_500_000)) assertEquals("23k", tokens(23_400)) } + + @Test fun aTurnsReplyIsEverythingItSaid() { + val turn = listOf(Entry.Said("One."), Entry.Used("e1", "Bash", "ls"), Entry.Said("Two.")) + assertEquals("One.\n\nTwo.", turnReply(turn)) + } }