android: show a chat's in-flight tool work instead of a bare Running
A session said "Running" with an empty tool chip while it worked, so a silent wait was indistinguishable from a stuck one. Derive the in-flight work from the stream the app already folds and show it on both surfaces. - ChatFormat: runningToolCalls() reads the tool parts still running, in transcript order, so the conversation and the session row share one set; toolDetail() takes the command/description the part actually carries (the first of description/command/filePath/file_path/path/pattern/query/url), collapsed to one line and elided at 72 chars, and returns null rather than a fabricated summary; runningToolElapsedMillis() uses the protocol's own time.ran/created when present and the local per-part stamp otherwise; runningToolStamps() keeps a stable start per part across frames; TOOL_LONG_RUNNING_MILLIS (60s) and toolLongRunning() mark an unusually long call; sessionWorkLabel() names what a working session is doing while a text-streaming turn stays plain "Running" and a busy session never reads "Idle". - ChatStore: ChatThread gains toolStartedAt, stamped when a part turns running and dropped when it settles, plus runningTools/toolElapsedMillis; history() and the frame fold maintain it; setRunning/turn end clear it. - ChatTranscript: RunningIndicator lists one line per running call (name, detail where present, live elapsed) keyed by part id so concurrent calls render together without flicker; no percentage, ETA or progress bar. - ChatListPane: the session row renders sessionWorkLabel(busy, thread). - ChatRunningWorkTest covers name/detail/elapsed, clear on completion, error state, concurrent calls and the no-detail stream. Only fields the harness reports are used: tool name, state.status and input; the live brokered frame carries no timing envelope, so elapsed falls back to the local stamp. Existing chips, INPUT/OUTPUT, reasoning blocks, stop control, activity marks and scroll/follow behaviour are untouched. Checked and unchanged: no packaged template or packaged authoring-skill reference describes this app surface (the chat references in internal/author/skill/references/ document the server's brokered chat_* protocol, which this change does not touch), so none is stale. android/AGENTS.md already names ui/chat/ and its files. The shared documents this change does make stale are recorded, not edited, in docs/notes/chat-in-flight-tool-work.md: the Running-indicator and session-status statements in android/README.md.
This commit is contained in:
@@ -7,6 +7,7 @@ import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.awan.pipelines.data.protocol.ChatPart
|
||||
import net.awan.pipelines.data.protocol.ChatPermissionFrame
|
||||
import net.awan.pipelines.data.protocol.ChatMessage
|
||||
import net.awan.pipelines.data.protocol.ChatProtocol
|
||||
import net.awan.pipelines.data.protocol.ChatSession
|
||||
import net.awan.pipelines.data.protocol.ChatToolState
|
||||
@@ -79,6 +80,126 @@ internal fun formatToolDuration(millis: Long): String = when {
|
||||
/** The chip label for a tool call: its name, or a placeholder when unnamed. */
|
||||
internal fun toolLabel(part: ChatPart): String = part.name.ifBlank { "tool" }
|
||||
|
||||
/**
|
||||
* The in-flight tool calls across a conversation's [messages], in transcript
|
||||
* order, so the conversation's indicator and the session row read the same set
|
||||
* and its order is stable from frame to frame (a chip is never re-ordered by a
|
||||
* rebuild). A call drops out the moment it completes or errors, which is what
|
||||
* clears the indicator, so nothing here ever invents a finish time.
|
||||
*/
|
||||
internal fun runningToolCalls(messages: List<ChatMessage>): List<ChatPart> =
|
||||
messages.flatMap { it.parts }
|
||||
.filter { it.type == ChatProtocol.PART_TOOL && ChatProtocol.toolRunning(it.state) }
|
||||
|
||||
/**
|
||||
* The input keys, in preference order, that carry a human-facing command or a
|
||||
* short description of what a tool call is doing. OpenCode's tools put the
|
||||
* substance in different places (`command` for a shell, `description` for a task,
|
||||
* a path or pattern for the file/search tools), so the first one present is shown
|
||||
* rather than the raw arguments.
|
||||
*/
|
||||
private val TOOL_DETAIL_KEYS = listOf(
|
||||
"description", "command", "filePath", "file_path", "path", "pattern", "query", "url",
|
||||
)
|
||||
|
||||
/** How much of a command/description the phone shows before it is elided. */
|
||||
private const val TOOL_DETAIL_MAX_LENGTH = 72
|
||||
|
||||
private val TOOL_DETAIL_WHITESPACE = Regex("\\s+")
|
||||
|
||||
/**
|
||||
* A one-line command or short description for [part], taken from the input the
|
||||
* part actually carries: the first of [TOOL_DETAIL_KEYS] present, collapsed to a
|
||||
* single line and elided past [TOOL_DETAIL_MAX_LENGTH]. Null when the input
|
||||
* carries no such key (a partial-JSON string while the call is still streaming,
|
||||
* or a tool that reports none), so a caller shows the name alone rather than a
|
||||
* fabricated summary.
|
||||
*/
|
||||
internal fun toolDetail(part: ChatPart): String? {
|
||||
val obj = part.state?.input as? JsonObject ?: return null
|
||||
for (key in TOOL_DETAIL_KEYS) {
|
||||
val value = obj[key] as? JsonPrimitive ?: continue
|
||||
if (!value.isString) continue
|
||||
val text = value.content.trim().replace(TOOL_DETAIL_WHITESPACE, " ")
|
||||
if (text.isBlank()) continue
|
||||
return if (text.length <= TOOL_DETAIL_MAX_LENGTH) {
|
||||
text
|
||||
} else {
|
||||
text.take(TOOL_DETAIL_MAX_LENGTH - 1).trimEnd() + "…"
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* How long a running [part] has been going at [nowMillis]. The protocol's own
|
||||
* timing wins whenever the part carries it (a running call from stored history
|
||||
* has `time.ran`, the moment it started); otherwise the elapsed is measured
|
||||
* locally from [startedAt] — the wall clock at which this client first saw the
|
||||
* part running. Zero when neither is usable, so a caller never shows a time it
|
||||
* cannot stand behind.
|
||||
*/
|
||||
internal fun runningToolElapsedMillis(part: ChatPart, startedAt: Long, nowMillis: Long): Long {
|
||||
val protocolStart = part.time?.let { if (it.ran > 0) it.ran else it.created } ?: 0L
|
||||
val start = if (protocolStart > 0) protocolStart else startedAt
|
||||
return if (start in 1 until nowMillis) nowMillis - start else 0L
|
||||
}
|
||||
|
||||
/**
|
||||
* A tool call running longer than this reads as unusually long on the phone and
|
||||
* is marked, so a genuinely stuck call is visible without a fabricated ETA. One
|
||||
* minute: long enough that ordinary calls (a build, a search, a command) are not
|
||||
* flagged, short enough that a hung call stands out while the operator is still
|
||||
* wondering.
|
||||
*/
|
||||
internal const val TOOL_LONG_RUNNING_MILLIS: Long = 60_000L
|
||||
|
||||
/** Whether a running call has been going long enough to be marked as unusual. */
|
||||
internal fun toolLongRunning(elapsedMillis: Long): Boolean = elapsedMillis >= TOOL_LONG_RUNNING_MILLIS
|
||||
|
||||
/**
|
||||
* The running-tool start stamps after a thread's [messages] changed: every tool
|
||||
* part in flight keeps the stamp it already had, or is stamped with [now] when it
|
||||
* has just turned running, and a part that is no longer running loses its stamp.
|
||||
* Kept free of Compose and of [ChatStore] so the bookkeeping and its unit test
|
||||
* share one rule, and a stamp never moves forward while a call keeps running.
|
||||
*/
|
||||
internal fun runningToolStamps(
|
||||
current: Map<String, Long>,
|
||||
messages: List<ChatMessage>,
|
||||
now: Long,
|
||||
): Map<String, Long> {
|
||||
val stamps = LinkedHashMap<String, Long>()
|
||||
messages.forEach { message ->
|
||||
message.parts.forEach { part ->
|
||||
if (part.id.isNotEmpty() && part.type == ChatProtocol.PART_TOOL && ChatProtocol.toolRunning(part.state)) {
|
||||
stamps[part.id] = current[part.id] ?: now
|
||||
}
|
||||
}
|
||||
}
|
||||
return stamps
|
||||
}
|
||||
|
||||
/**
|
||||
* The status a session row reports while a turn is in flight: it names the tool
|
||||
* call it is parked on (or how many, when several run at once) so a working chat
|
||||
* says what it is doing rather than just `Running`, and a chat only generating
|
||||
* text still reads as plain `Running` — the two are never confused. A chat with
|
||||
* nothing running falls back to the ordinary busy/idle label, so a session
|
||||
* waiting on another client is never shown as idle.
|
||||
*/
|
||||
internal fun sessionWorkLabel(busy: Boolean, thread: ChatThread?): String = when {
|
||||
thread?.running == true -> runningSessionLabel(thread.runningTools)
|
||||
busy -> "Busy · read-only"
|
||||
else -> "Idle"
|
||||
}
|
||||
|
||||
private fun runningSessionLabel(tools: List<ChatPart>): String = when (tools.size) {
|
||||
0 -> "Running"
|
||||
1 -> "Running ${toolLabel(tools.single())}"
|
||||
else -> "Running ${tools.size} tools"
|
||||
}
|
||||
|
||||
/**
|
||||
* A session's short title: the server title, or a truncated id when untitled, so
|
||||
* a row never renders as a blank line.
|
||||
|
||||
@@ -255,8 +255,8 @@ private fun SessionListItem(
|
||||
val thread = state.thread(session.id)
|
||||
SessionRow(
|
||||
session = session,
|
||||
thread = thread,
|
||||
open = thread != null,
|
||||
running = thread?.running == true,
|
||||
unseen = state.isUnseen(session),
|
||||
active = state.activeId == session.id,
|
||||
onClick = { vm.openChatSession(session) },
|
||||
@@ -289,8 +289,8 @@ private const val NO_DIRECTORY_LABEL = "No directory"
|
||||
@Composable
|
||||
private fun SessionRow(
|
||||
session: ChatSession,
|
||||
thread: ChatThread?,
|
||||
open: Boolean,
|
||||
running: Boolean,
|
||||
unseen: Boolean,
|
||||
active: Boolean,
|
||||
onClick: () -> Unit,
|
||||
@@ -298,6 +298,7 @@ private fun SessionRow(
|
||||
onRemove: () -> Unit,
|
||||
) {
|
||||
val texts = pipelinesText()
|
||||
val running = thread?.running == true
|
||||
val activity = sessionActivity(running, unseen)
|
||||
ListRow(selected = active, onClick = onClick) {
|
||||
Row(
|
||||
@@ -321,7 +322,7 @@ private fun SessionRow(
|
||||
SessionMenu(onRename = onRename, onRemove = onRemove)
|
||||
}
|
||||
val meta = buildList {
|
||||
add(sessionStatusLabel(session, running))
|
||||
add(sessionWorkLabel(session.busy, thread))
|
||||
if (session.directory.isNotBlank()) add(session.directory)
|
||||
if (session.model.isNotBlank()) add(session.model)
|
||||
}.joinToString(" · ")
|
||||
|
||||
@@ -70,6 +70,15 @@ data class ChatThread(
|
||||
* the turn settles.
|
||||
*/
|
||||
val runningSince: Long = 0,
|
||||
/**
|
||||
* Wall-clock epoch millis each currently-running tool call was first seen
|
||||
* running, keyed by the tool part's id. The brokered stream pushes a tool part
|
||||
* with no timing envelope, so a running call's elapsed time is measured from
|
||||
* here; a call that settles loses its stamp. Kept on the thread (not in the
|
||||
* composable) so the elapsed time survives a chat switch, exactly like
|
||||
* [runningSince] does for the turn.
|
||||
*/
|
||||
val toolStartedAt: Map<String, Long> = emptyMap(),
|
||||
/** The unsent composer text for this chat. */
|
||||
val draft: String = "",
|
||||
/**
|
||||
@@ -120,6 +129,22 @@ data class ChatThread(
|
||||
*/
|
||||
fun elapsedMillis(nowMillis: Long): Long =
|
||||
if (running && runningSince in 1 until nowMillis) nowMillis - runningSince else 0
|
||||
|
||||
/**
|
||||
* The tool calls still in flight for this session, in transcript order, so the
|
||||
* conversation's in-flight indicator and the session row both read the same
|
||||
* set. Empty once every call has completed or errored, which is what clears
|
||||
* the indicator.
|
||||
*/
|
||||
val runningTools: List<ChatPart> get() = runningToolCalls(messages)
|
||||
|
||||
/**
|
||||
* How long [part] has been running at [nowMillis]: the protocol's own timing
|
||||
* when the part carries it, else the local start recorded in [toolStartedAt]
|
||||
* (see [runningToolElapsedMillis]). Zero when neither is usable.
|
||||
*/
|
||||
fun toolElapsedMillis(part: ChatPart, nowMillis: Long): Long =
|
||||
runningToolElapsedMillis(part, toolStartedAt[part.id] ?: 0L, nowMillis)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -580,7 +605,16 @@ class ChatStore(
|
||||
*/
|
||||
fun history(sessionId: String, messages: List<ChatMessage>) {
|
||||
update(sessionId) {
|
||||
it.copy(messages = messages, loading = false, loaded = true, error = null)
|
||||
it.copy(
|
||||
messages = messages,
|
||||
loading = false,
|
||||
loaded = true,
|
||||
error = null,
|
||||
// A running call the history reports (a turn that started before
|
||||
// this fetch) is stamped now, the best local measurement there is;
|
||||
// a call that already settled drops its stamp.
|
||||
toolStartedAt = runningToolStamps(it.toolStartedAt, messages, now()),
|
||||
)
|
||||
}
|
||||
// A conversation on screen is read as its history arrives: its recency
|
||||
// watermark advances to the newest message the server reports (its own
|
||||
@@ -605,7 +639,9 @@ class ChatStore(
|
||||
val ids = _state.value.open.map { it.sessionId }
|
||||
if (ids.isEmpty()) return ids
|
||||
_state.value = _state.value.copy(
|
||||
open = _state.value.open.map { if (it.running) it.copy(running = false, runningSince = 0) else it },
|
||||
open = _state.value.open.map {
|
||||
if (it.running) it.copy(running = false, runningSince = 0, toolStartedAt = emptyMap()) else it
|
||||
},
|
||||
)
|
||||
return ids
|
||||
}
|
||||
@@ -777,7 +813,8 @@ class ChatStore(
|
||||
val updated = upsert(if (partIndex >= 0) parts[partIndex] else null)
|
||||
if (partIndex >= 0) parts[partIndex] = updated else parts.add(updated)
|
||||
messages[resolvedIndex] = message.copy(parts = parts)
|
||||
setRunning(thread.copy(messages = messages), true)
|
||||
val stamped = runningToolStamps(thread.toolStartedAt, messages, now())
|
||||
setRunning(thread.copy(messages = messages, toolStartedAt = stamped), true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -790,7 +827,7 @@ class ChatStore(
|
||||
private fun setRunning(thread: ChatThread, running: Boolean): ChatThread = when {
|
||||
running && !thread.running -> thread.copy(running = true, runningSince = now())
|
||||
running -> thread
|
||||
else -> thread.copy(running = false, runningSince = 0)
|
||||
else -> thread.copy(running = false, runningSince = 0, toolStartedAt = emptyMap())
|
||||
}
|
||||
|
||||
private fun update(sessionId: String, block: (ChatThread) -> ChatThread) {
|
||||
|
||||
@@ -20,6 +20,7 @@ import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.selection.SelectionContainer
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Build
|
||||
import androidx.compose.material.icons.filled.KeyboardArrowDown
|
||||
import androidx.compose.material.icons.filled.KeyboardArrowUp
|
||||
import androidx.compose.material.icons.filled.Psychology
|
||||
@@ -34,6 +35,7 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.key
|
||||
import androidx.compose.runtime.mutableLongStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -46,6 +48,7 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.collect
|
||||
@@ -57,6 +60,7 @@ import net.awan.pipelines.ui.formatDuration
|
||||
import net.awan.pipelines.ui.theme.CHAT_BUBBLE_MAX_WIDTH_DP
|
||||
import net.awan.pipelines.ui.theme.CHAT_FOLLOW_THRESHOLD_DP
|
||||
import net.awan.pipelines.ui.theme.ICON_INLINE_DP
|
||||
import net.awan.pipelines.ui.theme.ICON_STATUS_DP
|
||||
import net.awan.pipelines.ui.theme.SPACING_XS
|
||||
|
||||
/** The running indicator's elapsed time ticks once a second, never per frame. */
|
||||
@@ -274,24 +278,90 @@ private fun LazyListState.bottomDistancePx(itemCount: Int): Int? {
|
||||
|
||||
/**
|
||||
* The in-flight turn indicator: a spinner, `Working…` and the turn's live elapsed
|
||||
* time. The elapsed value is derived from the turn's own start ([ChatThread.elapsedMillis]),
|
||||
* so it keeps counting across a chat switch, and the tick loop stops the moment
|
||||
* the turn settles.
|
||||
* time, then one compact line for each tool call in flight — its name, the
|
||||
* command or description the part carries, and how long that call has been
|
||||
* running. The elapsed values are derived from the turn's own start
|
||||
* ([ChatThread.elapsedMillis]) and each call's own start
|
||||
* ([ChatThread.toolElapsedMillis]), so they keep counting across a chat switch,
|
||||
* and the tick loop stops the moment the turn settles. This is what makes the
|
||||
* otherwise-silent part of a turn legible: the operator can tell `working on X`
|
||||
* from `waiting on something` instead of watching a bare `Running`.
|
||||
*/
|
||||
@Composable
|
||||
private fun RunningIndicator(thread: ChatThread) {
|
||||
val elapsedMillis = thread.elapsedMillis(rememberElapsedNow(thread.runningSince))
|
||||
val now = rememberElapsedNow(thread.runningSince)
|
||||
val elapsedMillis = thread.elapsedMillis(now)
|
||||
val tools = thread.runningTools
|
||||
Column(
|
||||
verticalArrangement = Arrangement.spacedBy(SPACING_XS),
|
||||
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP),
|
||||
) {
|
||||
CircularProgressIndicator(Modifier.size(16.dp), strokeWidth = 2.dp)
|
||||
Text("Working…", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Text(
|
||||
text = "· ${formatDuration(elapsedMillis / 1_000)}",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
// One line per call in flight, in transcript order and keyed by the part,
|
||||
// so several running at once read together rather than flickering between
|
||||
// them; a call's line drops out the moment it completes or errors.
|
||||
tools.forEach { part ->
|
||||
key(part.id) { RunningToolLine(part, thread.toolElapsedMillis(part, now)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One tool call in flight: its name, the command or short description the part
|
||||
* carries (when it carries one) and how long it has been running. A call that has
|
||||
* been running past [TOOL_LONG_RUNNING_MILLIS] is tinted and labelled so a stuck
|
||||
* call stands out. The elapsed time is the only progress signal; no percentage,
|
||||
* ETA or progress bar is ever invented.
|
||||
*/
|
||||
@Composable
|
||||
private fun RunningToolLine(part: ChatPart, elapsedMillis: Long) {
|
||||
val long = toolLongRunning(elapsedMillis)
|
||||
val tint = if (long) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurfaceVariant
|
||||
val detail = toolDetail(part)
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP),
|
||||
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
|
||||
modifier = Modifier.fillMaxWidth().padding(start = CHAT_GUTTER),
|
||||
) {
|
||||
CircularProgressIndicator(Modifier.size(16.dp), strokeWidth = 2.dp)
|
||||
Text("Working…", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Icon(
|
||||
Icons.Default.Build,
|
||||
contentDescription = null,
|
||||
tint = tint,
|
||||
modifier = Modifier.size(ICON_STATUS_DP),
|
||||
)
|
||||
Text(
|
||||
text = "· ${formatDuration(elapsedMillis / 1_000)}",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
text = toolLabel(part),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = tint,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier = if (detail == null) Modifier.weight(1f) else Modifier,
|
||||
)
|
||||
if (detail != null) {
|
||||
Text(
|
||||
text = detail,
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
}
|
||||
Text(
|
||||
text = formatDuration(elapsedMillis / 1_000) + if (long) " · long" else "",
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = tint,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user