Show chat tool calls as expandable INPUT/OUTPUT chips

Render each assistant turn's tool calls the way the reference jarvis chat
does: one compact chip per call showing the tool name, its duration and a
spinner while it runs, with consecutive calls of a turn grouped into one
wrapping row so the transcript stays scannable (ChatScreen AssistantParts /
ToolCalls).

Tapping a chip opens a floating panel over the transcript with INPUT as
pretty-printed JSON and OUTPUT as monospace, plus copy actions. The panel is
dismissible by tapping outside or its Close action and is positioned by
ToolPanelPositionProvider so it never moves the conversation (the transcript
scroll position is untouched while the overlay is open).

A failed call is shown as failed and renders both its diff (when present) and
an ERROR block. ERROR text now comes from the new toolErrorText, which prefers
the server's error message over any partial output, while toolOutputText is the
OUTPUT derivation again; toolFailed identifies the error state.

While a turn streams, the transcript follows the newest content with a hard
jump unless the operator has dragged away, driven by
listState.interactionSource.interactions (DragInteraction.Start/Stop/Cancel)
rather than programmatic scrolls. The running indicator and its live elapsed
time derive from the turn's own start (ChatThread.elapsedMillis, an injectable
clock in ChatStore) so they survive a chat switch.

A reconnecting socket now settles every open chat and re-fetches its history
(ChatStore.streamResumed, AppViewModel.observeConnection), and a server-pushed
stream error with no session id settles the sole/active running turn with the
server's message instead of leaving the indicator spinning.

Tests: ChatFormatTest covers toolErrorText; new ChatStreamTest covers the
streaming/elapsed/reconnect/stream-error guarantees against an injected clock;
new ToolPanelPositionProviderTest covers the panel's below/flip/margin
placement. README's chat section documents the follow-the-stream mechanism.
This commit is contained in:
2026-10-16 14:17:50 +01:00
parent d7f650032d
commit 291cec2bda
8 changed files with 813 additions and 35 deletions
@@ -22,6 +22,7 @@ import net.awan.pipelines.data.protocol.BuildInfo
import net.awan.pipelines.data.protocol.ChatSession
import net.awan.pipelines.data.protocol.PipelineDetail
import net.awan.pipelines.data.protocol.PipelineSummary
import net.awan.pipelines.data.protocol.Protocol
import net.awan.pipelines.data.protocol.RunDetail
import net.awan.pipelines.data.protocol.SessionEvidence
import net.awan.pipelines.data.transport.ConnectionState
@@ -113,6 +114,7 @@ class AppViewModel(
reconnect()
}
observeChatEvents()
observeConnection()
}
/**
@@ -128,6 +130,31 @@ class AppViewModel(
// Never silently drop an approval request: it is held per chat and
// answered with chat_reply from the conversation's approval bar.
message.chatPermission?.let { chatStore.permission(it) }
// An unsolicited server error has no request id to match, so it is
// the chat stream failing mid-turn: settle the running chat instead
// of leaving its indicator spinning forever.
if (message.id == null && message.type == Protocol.ERROR) {
chatStore.streamError(message.error?.takeIf { it.isNotBlank() } ?: "The chat stream failed.")
}
}
}
}
/**
* Re-syncs every open chat when the socket (re)connects: the frames sent
* during the gap are gone, so the conversation is re-fetched from history
* rather than trusted. A running chat is marked settled first and a still-live
* stream re-asserts it on its next frame, so a stale `Working…` never sticks.
*/
private fun observeConnection() {
viewModelScope.launch {
var connected = false
connectionState.collect { state ->
val isConnected = state is ConnectionState.Connected
if (isConnected && !connected) {
chatStore.streamResumed().forEach { loadChatHistory(it) }
}
connected = isConnected
}
}
}
@@ -40,11 +40,22 @@ internal fun toolOutputText(state: ChatToolState?): String {
.filter { it.type == "text" && it.text.isNotBlank() }
.joinToString("\n") { it.text }
if (text.isNotBlank()) return text
if (state.error != null) return state.error.message
if (ChatProtocol.toolRunning(state)) return ""
return "(no output)"
}
/**
* The ERROR section of a failed tool call. The server's error message takes
* precedence over any partial output the call had already produced — a failed
* call can carry both, and the output text must not be labelled as the error —
* falling back to the output text so the block is never blank when the failure
* carries no message.
*/
internal fun toolErrorText(state: ChatToolState?): String {
val message = state?.error?.message
return if (!message.isNullOrBlank()) message else toolOutputText(state)
}
/** Wall-clock duration of a tool call, or null while it has no completion time. */
internal fun toolDurationMillis(part: ChatPart): Long? {
val time = part.time ?: return null
@@ -53,6 +64,9 @@ internal fun toolDurationMillis(part: ChatPart): Long? {
return time.completed - start
}
/** True when a tool call ended in an error state. */
internal fun toolFailed(state: ChatToolState?): Boolean = state?.status == ChatProtocol.TOOL_ERROR
/** Compact millisecond duration, e.g. `340ms` / `1.2s` / `2m 3s`. */
internal fun formatToolDuration(millis: Long): String = when {
millis < 1_000 -> "${millis}ms"
@@ -3,9 +3,12 @@ package net.awan.pipelines.ui.chat
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.interaction.DragInteraction
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.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
@@ -31,6 +34,7 @@ import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Build
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.ErrorOutline
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material.icons.filled.Psychology
@@ -55,19 +59,30 @@ import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
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
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
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.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupPositionProvider
import androidx.compose.ui.window.PopupProperties
import androidx.compose.foundation.selection.selectable
import androidx.activity.compose.PredictiveBackHandler
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import net.awan.pipelines.data.protocol.ChatMessage
import net.awan.pipelines.data.protocol.ChatPart
import net.awan.pipelines.data.protocol.ChatPermissionFrame
@@ -85,6 +100,7 @@ import net.awan.pipelines.ui.components.PaneIdentity
import net.awan.pipelines.ui.components.RunState
import net.awan.pipelines.ui.components.RunStatusPalette
import net.awan.pipelines.ui.components.SlidingPaneStack
import net.awan.pipelines.ui.formatDuration
import net.awan.pipelines.ui.theme.pipelinesText
/**
@@ -97,6 +113,15 @@ internal val CHAT_ITEM_GAP = 8.dp
private val CHAT_COMPOSER_GAP = 12.dp
private val CHAT_STATUS_DOT = 8.dp
/** The tool panel floats over the transcript; these bound its width and offsets. */
private val TOOL_PANEL_MAX_WIDTH = 420.dp
private val TOOL_PANEL_GAP = 4.dp
private val TOOL_PANEL_MARGIN = 8.dp
private val TOOL_PANEL_ELEVATION = 12.dp
/** The running indicator's elapsed time ticks once a second, never per frame. */
private const val CHAT_ELAPSED_TICK_MILLIS = 1_000L
/**
* The Chat destination. It is the same adaptive [SlidingPaneStack] the Pipelines
* and Runs destinations use: pane 0 is the server's session list (the operator
@@ -453,9 +478,35 @@ private fun ChatConversationPane(
* The bottom-anchored transcript: content is packed to the bottom (so a short or
* freshly opened chat sits against the composer) and the running indicator sits
* at the newest end while a turn is in flight.
*
* While a turn streams it follows the newest content with a hard jump (not an
* animation, which a fast-growing reply outruns), exactly like the reference
* transcript, unless the operator has scrolled up to read; returning to the end
* resumes following.
*/
@Composable
private fun ChatTranscript(thread: ChatThread, listState: androidx.compose.foundation.lazy.LazyListState) {
var follow by remember(thread.sessionId) { mutableStateOf(true) }
// Only a *user* drag changes whether the transcript follows: programmatic
// jumps (below) never touch `interactionSource`, so they cannot un-stick the
// view. A drag away from the end stops the follow; releasing at the very end
// (no content below) resumes it.
LaunchedEffect(listState) {
listState.interactionSource.interactions.collect { interaction ->
when (interaction) {
is DragInteraction.Start -> follow = false
is DragInteraction.Stop, is DragInteraction.Cancel ->
follow = !listState.canScrollForward
else -> Unit
}
}
}
val itemCount = thread.messages.size + if (thread.running) 1 else 0
LaunchedEffect(thread.running, thread.messages, follow, itemCount) {
if (thread.running && follow && itemCount > 0) {
listState.scrollToItem(itemCount - 1)
}
}
LazyColumn(
state = listState,
modifier = Modifier.fillMaxSize(),
@@ -466,13 +517,20 @@ private fun ChatTranscript(thread: ChatThread, listState: androidx.compose.found
MessageItem(message)
}
if (thread.running) {
item(key = "pending") { RunningIndicator() }
item(key = "pending") { RunningIndicator(thread) }
}
}
}
/**
* 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.
*/
@Composable
private fun RunningIndicator() {
private fun RunningIndicator(thread: ChatThread) {
val elapsedMillis = thread.elapsedMillis(rememberElapsedNow(thread.runningSince))
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP),
@@ -480,9 +538,34 @@ private fun RunningIndicator() {
) {
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,
)
}
}
/**
* The wall clock, re-read once a second while a turn is in flight, so the
* indicator derives the elapsed time with [ChatThread.elapsedMillis] — the one
* derivation its unit test also shares — instead of subtracting the turn's start
* itself. Keyed on [runningSince] and only entered for a live turn, so a settled
* chat schedules nothing.
*/
@Composable
private fun rememberElapsedNow(runningSince: Long): Long {
var nowMillis by remember { mutableLongStateOf(System.currentTimeMillis()) }
LaunchedEffect(runningSince) {
if (runningSince <= 0) return@LaunchedEffect
while (true) {
nowMillis = System.currentTimeMillis()
delay(CHAT_ELAPSED_TICK_MILLIS)
}
}
return nowMillis
}
@Composable
private fun MessageItem(message: ChatMessage) {
val isUser = message.role == ChatProtocol.ROLE_USER
@@ -507,7 +590,7 @@ private fun MessageItem(message: ChatMessage) {
style = MaterialTheme.typography.bodyMedium,
)
} else {
message.parts.forEach { part -> MessagePart(part) }
AssistantParts(message.parts)
}
}
}
@@ -515,6 +598,28 @@ private fun MessageItem(message: ChatMessage) {
}
}
/**
* An assistant turn's parts in order, with each run of consecutive tool calls
* rendered as one wrapping chip group (mirroring the reference transcript's
* `_ToolCalls`, research §1.3): the calls of a turn sit together on as few lines
* as they fit, instead of each taking a row, so the transcript stays scannable.
*/
@Composable
private fun AssistantParts(parts: List<ChatPart>) {
var index = 0
while (index < parts.size) {
if (parts[index].type == ChatProtocol.PART_TOOL) {
var end = index
while (end < parts.size && parts[end].type == ChatProtocol.PART_TOOL) end++
ToolCalls(parts.subList(index, end))
index = end
} else {
MessagePart(parts[index])
index++
}
}
}
@Composable
private fun MessagePart(part: ChatPart) {
when (part.type) {
@@ -522,7 +627,6 @@ private fun MessagePart(part: ChatPart) {
MarkdownText(part.text)
}
ChatProtocol.PART_REASONING -> ReasoningBlock(part.text)
ChatProtocol.PART_TOOL -> ToolCallCard(part)
else -> if (part.text.isNotBlank()) Text(part.text, style = MaterialTheme.typography.bodyMedium)
}
}
@@ -547,24 +651,64 @@ private fun ReasoningBlock(text: String) {
}
/**
* A tool call as a compact chip that expands to INPUT (pretty JSON) and OUTPUT
* (monospace) sections, mirroring the reference chat: the transcript stays
* scannable and the arguments/result appear only on demand.
* One turn's tool calls as a single group of compact chips, mirroring the
* reference transcript's `_ToolCalls`/`_ToolChip` (research §1.3): each call
* shows its name (or the edited file), its duration and a spinner while it runs,
* and the chips of the turn wrap together on as few lines as they fit instead of
* each taking a row.
*
* An `edit`-family call instead renders a diff card (research §1.3 `_CodeRun` /
* Tapping a chip opens its INPUT (pretty JSON) / OUTPUT (monospace) panel as an
* overlay anchored to that chip (research §1.3 `_buildPanel`), dismissed by its
* own X or by tapping outside. Because the panel is a popup and not part of the
* transcript layout, opening or closing it never re-flows the conversation nor
* moves its scroll position — the transcript stays exactly where the operator
* left it.
*
* An `edit`-family call expands to a diff card (research §1.3 `_CodeRun` /
* `_DiffView`): the `+N -M` stat on the chip and a green/red unified diff derived
* from the arguments, since the brokered protocol carries no diff field.
* from the arguments, since the brokered protocol carries no diff field. An
* `error` call is visibly failed — an error-tinted chip and error icon — and its
* panel shows the error text.
*/
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun ToolCallCard(part: ChatPart) {
var expanded by remember(part.id) { mutableStateOf(false) }
private fun ToolCalls(parts: List<ChatPart>) {
// One id at a time: the reference shows a single panel, and a focusable popup
// dismisses the previous one on open. No key is needed: an id that is no
// longer among the calls simply matches nothing, so a streaming rebuild keeps
// the panel the operator just opened without allocating a key list per frame.
var openId by remember { mutableStateOf<String?>(null) }
FlowRow(
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
horizontalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP),
verticalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP),
) {
parts.forEach { part ->
ToolChip(
part = part,
expanded = openId == part.id,
onToggle = { openId = if (openId == part.id) null else part.id },
onDismiss = { openId = null },
)
}
}
}
/** A compact tool-call chip: kind icon, label, diff stat, duration and chevron. */
@Composable
private fun ToolChip(part: ChatPart, expanded: Boolean, onToggle: () -> Unit, onDismiss: () -> Unit) {
val running = ChatProtocol.toolRunning(part.state)
val failed = toolFailed(part.state)
val diff = toolEditDiff(part.state?.input)
Column(Modifier.fillMaxWidth().padding(vertical = 4.dp)) {
Box {
Surface(
shape = RoundedCornerShape(CHAT_ITEM_GAP + 2.dp),
color = MaterialTheme.colorScheme.surfaceContainerHigh,
modifier = Modifier.clickable { expanded = !expanded },
color = if (failed) {
MaterialTheme.colorScheme.errorContainer
} else {
MaterialTheme.colorScheme.surfaceContainerHigh
},
modifier = Modifier.clickable(onClick = onToggle),
) {
Row(
verticalAlignment = Alignment.CenterVertically,
@@ -575,10 +719,14 @@ private fun ToolCallCard(part: ChatPart) {
CircularProgressIndicator(Modifier.size(14.dp), strokeWidth = 2.dp)
} else {
Icon(
if (diff != null) Icons.Default.Edit else Icons.Default.Build,
contentDescription = null,
imageVector = when {
failed -> Icons.Default.ErrorOutline
diff != null -> Icons.Default.Edit
else -> Icons.Default.Build
},
contentDescription = if (failed) "Failed tool call" else null,
modifier = Modifier.size(16.dp),
tint = MaterialTheme.colorScheme.onSurfaceVariant,
tint = if (failed) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurfaceVariant,
)
}
Text(
@@ -586,7 +734,6 @@ private fun ToolCallCard(part: ChatPart) {
style = MaterialTheme.typography.labelMedium,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f, fill = false),
)
diff?.let { DiffStat(it) }
toolDurationMillis(part)?.let {
@@ -600,13 +747,102 @@ private fun ToolCallCard(part: ChatPart) {
}
}
if (expanded) {
Column(verticalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP), modifier = Modifier.padding(top = CHAT_ITEM_GAP)) {
if (diff != null) {
DiffView(diff)
} else {
CodeBlock(label = "INPUT", text = prettyToolInput(part.state?.input))
CodeBlock(label = "OUTPUT", text = toolOutputText(part.state))
Popup(
popupPositionProvider = rememberToolPanelPositionProvider(),
onDismissRequest = onDismiss,
properties = PopupProperties(focusable = true),
) {
ToolPanel(part = part, onDismiss = onDismiss)
}
}
}
}
/**
* Anchors the tool panel just below its chip, or above it when there is no room
* below, keeping it inside the window margins — the same flip the reference
* panel does. Resolved in px, so it is independent of the current density.
*/
internal class ToolPanelPositionProvider(private val gapPx: Int, private val marginPx: Int) : PopupPositionProvider {
override fun calculatePosition(
anchorBounds: IntRect,
windowSize: IntSize,
layoutDirection: LayoutDirection,
popupContentSize: IntSize,
): IntOffset {
val maxX = (windowSize.width - popupContentSize.width - marginPx).coerceAtLeast(marginPx)
val x = anchorBounds.left.coerceIn(marginPx, maxX)
val below = anchorBounds.bottom + gapPx
val above = anchorBounds.top - popupContentSize.height - gapPx
val maxY = (windowSize.height - popupContentSize.height - marginPx).coerceAtLeast(marginPx)
val y = if (below <= maxY) below else above.coerceAtLeast(marginPx)
return IntOffset(x, y)
}
}
@Composable
private fun rememberToolPanelPositionProvider(): PopupPositionProvider {
val density = LocalDensity.current
return remember(density) {
ToolPanelPositionProvider(
gapPx = with(density) { TOOL_PANEL_GAP.roundToPx() },
marginPx = with(density) { TOOL_PANEL_MARGIN.roundToPx() },
)
}
}
/**
* The expanded panel of one tool call, mirroring the reference panel
* (`_buildPanel`, research §1.3): a header naming the call and its duration with
* a close X, then the pretty-printed INPUT and the monospace OUTPUT (or the
* synthesised edit diff). Both content blocks carry the shared whole-block copy
* action from [CodeBlock]. A failed call always renders its error text in an
* ERROR block — even an edit-family call, whose diff is still shown alongside it.
*/
@Composable
private fun ToolPanel(part: ChatPart, onDismiss: () -> Unit, modifier: Modifier = Modifier) {
val diff = toolEditDiff(part.state?.input)
val failed = toolFailed(part.state)
Surface(
shape = RoundedCornerShape(CHAT_ITEM_GAP),
color = MaterialTheme.colorScheme.surfaceContainer,
shadowElevation = TOOL_PANEL_ELEVATION,
modifier = modifier.widthIn(max = TOOL_PANEL_MAX_WIDTH).fillMaxWidth(),
) {
Column(
verticalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP),
modifier = Modifier.fillMaxWidth().padding(CHAT_ITEM_GAP + 2.dp),
) {
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP)) {
Icon(
imageVector = if (failed) Icons.Default.ErrorOutline else Icons.Default.Build,
contentDescription = null,
modifier = Modifier.size(16.dp),
tint = if (failed) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary,
)
Text(
text = toolLabel(part),
style = MaterialTheme.typography.labelLarge,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
toolDurationMillis(part)?.let {
Text(formatToolDuration(it), style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
}
IconButton(onClick = onDismiss, modifier = Modifier.size(28.dp)) {
Icon(Icons.Default.Close, contentDescription = "Close tool call", modifier = Modifier.size(16.dp))
}
}
if (diff != null) {
DiffView(diff)
} else {
CodeBlock(label = "INPUT", text = prettyToolInput(part.state?.input))
}
if (failed) {
CodeBlock(label = "ERROR", text = toolErrorText(part.state))
} else if (diff == null) {
CodeBlock(label = "OUTPUT", text = toolOutputText(part.state))
}
}
}
@@ -37,6 +37,13 @@ data class ChatThread(
val error: String? = null,
/** A turn is in flight for this session; it keeps running when we switch away. */
val running: Boolean = false,
/**
* Wall-clock epoch millis the in-flight turn began, or 0 when it is not
* running. Stored on the thread (not in the composable) so the running
* indicator's elapsed time survives a chat switch, and cleared the moment
* the turn settles.
*/
val runningSince: Long = 0,
/** The unsent composer text for this chat. */
val draft: String = "",
val scroll: ChatScrollPosition = ChatScrollPosition(),
@@ -53,6 +60,14 @@ data class ChatThread(
/** Whether the composer's send action is allowed. */
val canSend: Boolean get() = !readOnly && draft.isNotBlank()
/**
* Non-negative elapsed time of an in-flight turn at [nowMillis], or 0 once it
* has settled. Pure, so the running indicator and its unit test share the one
* derivation instead of each subtracting the start themselves.
*/
fun elapsedMillis(nowMillis: Long): Long =
if (running && runningSince in 1 until nowMillis) nowMillis - runningSince else 0
}
/**
@@ -85,8 +100,11 @@ data class ChatState(
* a running session must re-sync on return so a stale copy is never shown.
* - [event] applies a server frame to its session's thread whether or not that
* thread is on screen, so a background chat never loses its stream.
*
* [now] is injectable so the running indicator's elapsed time is testable without
* a device clock.
*/
class ChatStore {
class ChatStore(private val now: () -> Long = System::currentTimeMillis) {
private val _state = MutableStateFlow(ChatState())
val state: StateFlow<ChatState> = _state.asStateFlow()
@@ -215,7 +233,41 @@ class ChatStore {
it.copy(messages = messages, loading = false, loaded = true, error = null)
}
fun running(sessionId: String, running: Boolean) = update(sessionId) { it.copy(running = running) }
fun running(sessionId: String, running: Boolean) = update(sessionId) { setRunning(it, running) }
/**
* The stream resumed (the socket reconnected): every frame that was feeding an
* in-flight turn is gone, so no thread keeps claiming to run until a fresh
* frame proves otherwise, and the caller re-fetches each open chat's history.
* Returning every open id (not only the running ones) means a gap that
* silently swallowed the end of a turn is repaired too. Re-fetching replaces
* the conversation wholesale, so a resync can neither duplicate nor drop text.
*/
fun streamResumed(): List<String> {
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 },
)
return ids
}
/**
* A server-pushed stream error carries no session id (the brokered protocol
* cannot attribute it), so settle the one turn it can only have come from: the
* sole running chat, or the active one when several are in flight. With
* nothing running there is no turn to settle and it is ignored; the server's
* text is kept so the operator sees why the turn stopped.
*/
fun streamError(message: String) {
val running = _state.value.open.filter { it.running }
val target = when {
running.isEmpty() -> return
running.size == 1 -> running.single()
else -> running.firstOrNull { it.sessionId == _state.value.activeId } ?: return
}
update(target.sessionId) { setRunning(it.copy(error = message), false) }
}
/** The server pushed an approval request for this session; the composer answers it. */
fun permission(frame: ChatPermissionFrame) {
@@ -256,7 +308,7 @@ class ChatStore {
ChatProtocol.PART_TOOL -> applyTool(frame)
ChatProtocol.EVENT_STEP -> {
val started = frame.step?.status != ChatProtocol.STEP_ENDED
update(frame.sessionId) { it.copy(running = started) }
update(frame.sessionId) { setRunning(it, started) }
}
else -> Unit
}
@@ -318,10 +370,22 @@ 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)
thread.copy(messages = messages, running = true)
setRunning(thread.copy(messages = messages), true)
}
}
/**
* Applies a running transition: a turn that begins is stamped with the wall
* clock so the indicator can tick, a redundant `true` keeps the original
* stamp, and settling clears it. The one place `running` is set, so the two
* can never disagree.
*/
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)
}
private fun update(sessionId: String, block: (ChatThread) -> ChatThread) {
val current = _state.value
if (current.open.none { it.sessionId == sessionId }) return
@@ -2,6 +2,7 @@ package net.awan.pipelines.ui.chat
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import net.awan.pipelines.data.protocol.ChatError
import net.awan.pipelines.data.protocol.ChatPart
import net.awan.pipelines.data.protocol.ChatProtocol
import net.awan.pipelines.data.protocol.ChatSession
@@ -26,7 +27,7 @@ class ChatFormatTest {
}
@Test
fun toolOutputFallsBackToNoOutputOrError() {
fun toolOutputFallsBackToNoOutput() {
val completed = ChatToolState(
status = ChatProtocol.TOOL_COMPLETED,
content = listOf(ChatToolContent(type = "text", text = "hello")),
@@ -36,15 +37,24 @@ class ChatFormatTest {
"(no output)",
toolOutputText(ChatToolState(status = ChatProtocol.TOOL_COMPLETED)),
)
val failed = ChatToolState(
status = ChatProtocol.TOOL_ERROR,
error = net.awan.pipelines.data.protocol.ChatError(type = "boom", message = "exploded"),
)
assertEquals("exploded", toolOutputText(failed))
// Still streaming -> nothing yet, never the "(no output)" claim.
assertEquals("", toolOutputText(ChatToolState(status = ChatProtocol.TOOL_RUNNING)))
}
@Test
fun toolErrorPrefersTheMessageOverPartialOutput() {
// A failed call can carry both output content and an error; the ERROR
// block must show the message, never the output text.
val failed = ChatToolState(
status = ChatProtocol.TOOL_ERROR,
content = listOf(ChatToolContent(type = "text", text = "partial output")),
error = ChatError(type = "boom", message = "exploded"),
)
assertEquals("exploded", toolErrorText(failed))
// A failure with no message still renders something rather than a blank block.
assertEquals("(no output)", toolErrorText(ChatToolState(status = ChatProtocol.TOOL_ERROR)))
}
@Test
fun toolDurationUsesRanToCompletedAndIsNullWhileRunning() {
val done = ChatPart(name = "shell", time = ChatTime(created = 5, ran = 10, completed = 35))
@@ -95,6 +105,14 @@ class ChatFormatTest {
assertNull(toolEditDiff(JsonObject(mapOf("command" to JsonPrimitive("ls")))))
}
@Test
fun toolFailedOnlyForTheErrorStatus() {
assertTrue(toolFailed(ChatToolState(status = ChatProtocol.TOOL_ERROR)))
assertFalse(toolFailed(ChatToolState(status = ChatProtocol.TOOL_COMPLETED)))
assertFalse(toolFailed(ChatToolState(status = ChatProtocol.TOOL_RUNNING)))
assertFalse(toolFailed(null))
}
@Test
fun sessionTitleNeverBlank() {
assertEquals("Named", sessionTitle(ChatSession(id = "ses_1", title = "Named")))
@@ -0,0 +1,133 @@
package net.awan.pipelines.ui.chat
import net.awan.pipelines.data.protocol.ChatEventFrame
import net.awan.pipelines.data.protocol.ChatPart
import net.awan.pipelines.data.protocol.ChatProtocol
import net.awan.pipelines.data.protocol.ChatSession
import net.awan.pipelines.data.protocol.ChatStep
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
/**
* The streaming guarantees: a sequence of text deltas grows the rendered
* conversation and the running indicator toggles on and off, the elapsed time is
* derived from the turn's own start, a reconnect settles the indicator before the
* history re-fetch, and a mid-stream failure settles it with the server's text.
* Driven directly against [ChatStore] with an injected clock, so no device,
* server or Compose runtime is needed.
*/
class ChatStreamTest {
private fun session(id: String) = ChatSession(id = id, title = id)
private fun step(status: String) = ChatEventFrame(
sessionId = "s",
kind = ChatProtocol.EVENT_STEP,
step = ChatStep(status = status),
)
private fun delta(sessionId: String, messageId: String, partId: String, text: String) = ChatEventFrame(
sessionId = sessionId,
messageId = messageId,
kind = ChatProtocol.PART_TEXT,
delta = text,
part = ChatPart(type = ChatProtocol.PART_TEXT, id = partId),
)
/** The text of the single assistant message, exactly what the transcript renders. */
private fun renderedText(store: ChatStore, sessionId: String): String =
store.value.thread(sessionId)!!.messages.last().parts.single().text
@Test
fun deltaSequenceGrowsRenderedTextAndTogglesRunning() {
val store = ChatStore()
store.open(session("s"))
store.history("s", emptyList())
assertFalse(store.value.thread("s")!!.running)
// A step starting the turn raises the running indicator.
store.event(step(ChatProtocol.STEP_STARTED))
assertTrue(store.value.thread("s")!!.running)
// Each delta appends to the same part: the rendered text is the exact
// concatenation and strictly grows, never resets.
val chunks = listOf("Hel", "lo ", "wor", "ld")
val lengths = mutableListOf<Int>()
chunks.forEachIndexed { index, chunk ->
store.event(delta("s", "m1", "m1#0", chunk))
assertEquals(chunks.take(index + 1).joinToString(""), renderedText(store, "s"))
lengths += renderedText(store, "s").length
}
assertTrue(lengths.zipWithNext().all { (before, after) -> after > before })
// The step ending the turn lowers the indicator and clears its start.
store.event(step(ChatProtocol.STEP_ENDED))
assertFalse(store.value.thread("s")!!.running)
assertEquals(0L, store.value.thread("s")!!.runningSince)
assertEquals("Hello world", renderedText(store, "s"))
}
@Test
fun elapsedTimeComesFromTheTurnStartAndSurvivesARepeatedStart() {
var clock = 1_000L
val store = ChatStore(now = { clock })
store.open(session("s"))
store.event(step(ChatProtocol.STEP_STARTED))
assertEquals(1_000L, store.value.thread("s")!!.runningSince)
clock = 6_500L
assertEquals(5_500L, store.value.thread("s")!!.elapsedMillis(clock))
// A second start for the same turn must not move the clock forward.
store.event(step(ChatProtocol.STEP_STARTED))
assertEquals(1_000L, store.value.thread("s")!!.runningSince)
assertEquals(5_500L, store.value.thread("s")!!.elapsedMillis(clock))
// Settling drops the elapsed to 0 whatever the clock reads.
store.event(step(ChatProtocol.STEP_ENDED))
assertEquals(0L, store.value.thread("s")!!.elapsedMillis(clock))
}
@Test
fun streamResumedSettlesRunningAndReturnsEveryOpenChat() {
val store = ChatStore()
store.open(session("a"))
store.history("a", emptyList())
store.open(session("b"))
store.history("b", emptyList())
store.running("a", true)
// A reconnect names every open chat so the caller re-fetches each, and no
// thread keeps claiming to run until a fresh frame proves it.
assertEquals(listOf("a", "b"), store.streamResumed())
assertFalse(store.value.thread("a")!!.running)
// A delta after the resync re-asserts the running indicator.
store.event(delta("a", "m1", "m1#0", "still going"))
assertTrue(store.value.thread("a")!!.running)
assertEquals("still going", renderedText(store, "a"))
}
@Test
fun streamErrorSettlesTheRunningTurnAndKeepsTheServerText() {
val store = ChatStore()
store.open(session("s"))
store.running("s", true)
store.streamError("chat stream: boom")
val thread = store.value.thread("s")!!
assertFalse(thread.running)
assertEquals("chat stream: boom", thread.error)
}
@Test
fun streamErrorWithNothingRunningIsIgnored() {
val store = ChatStore()
store.open(session("s"))
store.streamError("chat stream: boom")
assertEquals(null, store.value.thread("s")!!.error)
}
}
@@ -0,0 +1,42 @@
package net.awan.pipelines.ui.chat
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import org.junit.Assert.assertEquals
import org.junit.Test
/**
* The tool panel's placement: below the chip when it fits, flipped above when it
* does not, and always kept inside the window margins. Driven directly against
* the provider, so it needs no device or Compose runtime.
*/
class ToolPanelPositionProviderTest {
private val provider = ToolPanelPositionProvider(gapPx = 4, marginPx = 8)
private val window = IntSize(width = 1000, height = 2000)
private fun place(anchor: IntRect, content: IntSize): IntOffset =
provider.calculatePosition(anchor, window, LayoutDirection.Ltr, content)
@Test
fun placesBelowTheChipWhenItFits() {
val anchor = IntRect(left = 40, top = 100, right = 340, bottom = 160)
assertEquals(IntOffset(40, 164), place(anchor, IntSize(400, 500)))
}
@Test
fun flipsAboveWhenThereIsNoRoomBelow() {
val anchor = IntRect(left = 40, top = 1800, right = 340, bottom = 1860)
// 1860 + 4 + 500 > 2000 - 8, so it goes above: 1800 - 500 - 4 = 1296.
assertEquals(IntOffset(40, 1296), place(anchor, IntSize(400, 500)))
}
@Test
fun clampsToTheRightAndTopMargins() {
val anchor = IntRect(left = 950, top = 20, right = 990, bottom = 60)
// Width 400 cannot start at 950; it is pulled in to the right margin.
assertEquals(IntOffset(592, 64), place(anchor, IntSize(400, 500)))
}
}