mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat(agent): Agent tab refinements: one-state composer, no snapping, notifications, choices, subagents, dictation, game frame, copy, History search (#2626)
* fix(agent): one-state composer with model and usage on top; reading back stays detached while the agent streams - The composer no longer has a compact reading bar that a first tap expands: the editor is always the editor, and the model picker and context ring move to the top beside the menu. - The pet chat drops its DeeDee title so that top bar gets the whole width. - Scrolling back stops following new output; new entries and streamed text only scroll the list while it is at the bottom, and sending a message resumes following. - When the editor slides back in or the keyboard rises, a list at the bottom keeps its last lines above the editor, even mid-drag. * fix(agent): a revise while reading back shows the editor it focuses * feat(agent): dictate a message, and attach what the running game shows - With nothing typed, the editor's send button dictates instead, through Android's speech recognizer; what is heard follows the text already there. It asks for the microphone the first time. - While a game session is open, a camera button attaches the game's current frame. It is copied from the game's own surface, as the bottom screen's screenshots are, so the chat or drawer over it is not in the picture. * feat(agent): a notification when a turn ends unseen, and replies that end in choices offer them as buttons - A turn that ends while neither the Agent page nor the pet is in view posts a notification: finished, failed or asking, with the reply's first line. Opening the Agent page clears it. - Claude Code's question tool is not available in print mode, so the brief asks the agent to end a reply with a choices block instead. The chat shows its options as buttons, and the one picked is sent as the next message. * feat(agent): a subagent's work shows under the call that started it Claude Code tags a subagent's messages with the call that started it. Its text, thinking and streamed deltas used to show as the main agent's reply, and its tool calls mixed into the turn. Now only its tool calls are kept, stored with their parent, and shown inside the parent's card: a live line with the step count and the latest call, and every call when the card is opened. Its token counts no longer overwrite the context reading. * feat(agent): copy a reply or a tool's text, and open a turn's changes in one place - A turn's footer has a copy button for everything the agent said in it, and tool input and output boxes have one each. - The files-changed chip opens every edit the turn made, file by file. * feat(agent): search conversations in History A field above the list keeps the conversations whose title, messages or replies hold the text, ignoring case. Chat files are read off the main thread, a quarter second after the last keystroke. * fix(agent): sending while reading back jumps to the latest * feat(agent): manage Claude Code's MCP servers and skills from the Agent menu A new panel lists the MCP servers claude mcp list reports, adds one for every project from a URL (over HTTP) or a start command (over stdio), and removes it; connectors from the claude.ai account are listed but managed there. It also lists the skills in ~/.claude/skills with their descriptions, and deletes one after a confirming second tap. * fix(agent): review follow-ups for the Agent tab refinements - Choices only come from one fenced block that ends the reply. - The Agent page counts as on screen only while its activity is started, so a turn ending during a game still notifies. - Picking a choice or Go jumps to the latest like sending does. - Dictation stops when the chat is hidden or the keyboard sends; stopping it by hand keeps the recognizer until its final result. - A failed JPEG encode is not attached. - The copied reply is read from the turn each time. - MCP start commands keep their quoted arguments; the CLI is read beside its 90 s deadline; deleting a skill runs off the main thread and removes links without following them. * fix(agent): scroll the shrinking list after layout, not during measure * fix(agent): fence-aware choices parsing; dictation stops overwriting edited drafts * fix(agent): ignore choices blocks nested in a code fence * fix(agent): treat tilde fences like backtick fences when finding choices * test(agent): cover a tilde-fenced choices block
This commit is contained in:
1 parent
a3be5f7ae2
commit
eb190f2dd7
49 files changed
+1040
-296
No files matched your search
@@ -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()
|
||||
|
||||
@@ -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<String>) = files.forEach { host(context, it).delete() }
|
||||
|
||||
|
||||
@@ -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<Chat>, query: String): List<Chat> {
|
||||
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"),
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -29,8 +29,11 @@ object AgentHost {
|
||||
data class Asked(val text: String, val files: List<String> = 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<Entry>()
|
||||
val queue = mutableStateListOf<Queued>()
|
||||
@@ -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<String>)
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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<Server>? {
|
||||
val result = run(context, listOf(ClaudeCode.binary, "mcp", "list"))
|
||||
if (!result.ok) return null
|
||||
return parseServers(result.output)
|
||||
}
|
||||
|
||||
internal fun parseServers(output: String): List<Server> = 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<String> {
|
||||
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<String> {
|
||||
val words = mutableListOf<String>()
|
||||
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<Skill> =
|
||||
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<Path>() {
|
||||
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<String>): 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() }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Entry.Used> = emptyList(), turn: List<Entry> = 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<String>, 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<Entry>, 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<Entry.Used>().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<Entry>): List<Diff> =
|
||||
turn.filterIsInstance<Entry.Used>().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<Entry>): String = turn.filterIsInstance<Entry.Said>().joinToString("\n\n") { it.text }
|
||||
|
||||
internal class Diff(val path: String, val lines: List<String>) {
|
||||
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<Entry>) {
|
||||
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<Entry.Used> = 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,
|
||||
|
||||
@@ -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<String>, inputModifier: Modifier, panel: AgentPanel, onPanel: (AgentPanel) -> Unit,
|
||||
showControlsWithoutKeyboard: Boolean,
|
||||
) {
|
||||
private fun AgentComposer(composer: ComposerState, draftState: MutableState<String>, 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<String>, inputModifier: Modifier, panel: AgentPanel, onPanel: (AgentPanel) -> Unit,
|
||||
highlightControls: Boolean = false, send: () -> Unit,
|
||||
) {
|
||||
private fun AgentEditor(composer: ComposerState, draftState: MutableState<String>, 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<List<AgentChats.Chat>?>(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<List<AgentTools.Server>?>(null, version) {
|
||||
loading = true
|
||||
value = withContext(Dispatchers.IO) { AgentTools.servers(context) }
|
||||
loading = false
|
||||
}
|
||||
val skills by produceState(emptyList<AgentTools.Skill>(), 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<String?>(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),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user