diff --git a/android/README.md b/android/README.md index 5806bfad..be295849 100644 --- a/android/README.md +++ b/android/README.md @@ -2420,3 +2420,247 @@ Not verified: the live brokered server (separate lane), streaming against real OpenCode, and attachments — unchanged from the passes above. The markdown subset is intentionally smaller than full CommonMark (no HTML, no reference links, no nested lists, no strikethrough). + +## Streaming replies (16 Oct 2026) + +The live delta/streaming pass promised by the first slice. `android/` only; no new +dependency (still just the declared `material-icons-extended`), no Pipelines +server/Go/installer/Docker file touched, no run started from the app, and +`emulator-5556` only (lane A's `emulator-5554` was never addressed). + +- **Turn lifecycle in the model.** `ChatStore` now owns `ChatThread.runningSince` + and one private `setRunning` transition, so a turn that begins is stamped with + the wall clock and settling clears it; `ChatThread.elapsedMillis(now)` is the one + pure derivation the indicator and its test share. `event` already appended + `text`/`reasoning` deltas to the target part and upserted tool parts, wherever the + frame's chat sits in the open order. +- **Running indicator.** The transcript's indicator shows the spinner, `Working…` + and the live elapsed time (ticked once a second, never per frame, through the + shared `formatDuration`). Its tick loop is keyed on the turn's start and stops the + moment the turn settles, and the elapsed value keeps counting across a chat + switch because it lives on the thread, not the composable. +- **Follow the stream.** The transcript hard-jumps to the newest item while a turn + runs — a fast-growing reply outruns an animation — unless the operator has + scrolled up to read; returning to the end resumes following. Consuming the list + state's `interactionSource.interactions` (`DragInteraction.Start`/`Stop`/`Cancel`) + distinguishes a user drag from the programmatic jump, so the jump never un-sticks + the view. +- **Reconnect resync.** `AppViewModel.observeConnection` re-fetches every open + chat's history whenever the socket transitions to `Connected`, after + `ChatStore.streamResumed()` settles each running thread (a still-live stream + re-asserts `running` on its next frame, so no stale `Working…` sticks). The + resync *replaces* the conversation wholesale, so a gap can neither duplicate nor + drop text. +- **Failure settle.** A server-pushed `error` frame carries no session id (the + brokered protocol cannot attribute it), so `ChatStore.streamError` settles the + sole running chat — or the active one when several are in flight — and keeps the + server's text; with nothing running it is ignored. The prompt-call failure path + already settled the turn and is unchanged. +- **Non-blocking.** Unsolicited frames reach the UI through the client's bounded + `SharedFlow` (`tryEmit` from the socket thread, `DROP_OLDEST` capacity 256), so a + slow collector never blocks the read loop; each mutation touches only its chat and + the `LazyColumn` keys limit recomposition to the changed item. + +Evidence (macOS, JDK 17 target, Gradle 9.7.1, +`ANDROID_HOME=/Users/alan/Library/Android/sdk`): + +- `gradle :app:assembleDebug :app:testDebugUnitTest` → `BUILD SUCCESSFUL`; the JUnit + XML totals **248 tests, 0 failures, 0 errors, 0 skipped** (new `ChatStreamTest`: + a delta sequence grows the rendered text and toggles the running indicator, the + elapsed time comes from the turn's own start and a repeated start keeps it, + `streamResumed` settles every open chat and returns them for re-fetch, + `streamError` settles the running turn with the server's text and is ignored with + nothing running). +- The debug APK was installed on `emulator-5556` only and driven with + `uiautomator dump`/`input tap` against `.stream_check/stream_stub.py` (a stdlib + `/connect` WebSocket double that streams `chat_event` frames; test scaffolding, + not the app — the deployed server still predates chat). Captures were validated + with `android/scripts/capture_png.sh -s emulator-5556`: a mid-stream reply with + the growing transcript and the indicator at `Working… · 1s` + (`chat_stream_01_early`) and `Working… · 4s` with the list followed to the newest + line (`chat_stream_02_mid`); the settled turn with the completed `bash 340ms` + tool chip, no indicator and the send control restored (`chat_stream_03_settled`); + and, after a mid-stream drag up, the transcript **staying** where the operator + left it while the turn still ran (`chat_stream_04_scrolled_up`). `adb -s + emulator-5556 logcat -d` held 0 `FATAL EXCEPTION` and 0 `E AndroidRuntime` + during the sweep. +- **APK publish.** `app-debug.apk` (18,262,465 bytes, sha256 + `ef435b662e6f7e7467b5ae25b69fb24f8904334af204224aad00bfd76879a84e`) copied to + `~/.local/share/pipelines/data/artifacts/126/1/pipelines-debug-wip-d7f6500.apk`; + `curl -I http://10.0.0.249:8080/artifacts/126/1/pipelines-debug-wip-d7f6500.apk` + returned `HTTP/1.1 200 OK`, `Content-Type: application/vnd.android.package-archive`, + `Content-Length: 18262465`, and a streamed `GET | shasum -a 256` matched. + +Not verified in this pass: the live brokered server (separate lane), streaming +against real OpenCode, and attachments — unchanged. `:app:lintDebug` could not run +in this environment: the Gradle JVM reports `25.0.4.1`, a version string the AGP +8.7.2 lint (IntelliJ `JavaVersion`) rejects, so lint fails before analysing the +tree; this is environmental and unrelated to the change. + +## Tool-call chips and the expandable panel (16 Oct 2026) + +The tool-call surface now matches the reference chat's `_ToolCalls`/`_ToolChip` +and `_buildPanel` (research §1.3) more closely. `android/` only; no new +dependency (still just the declared `material-icons-extended`), no Pipelines +server/Go/installer/Docker file touched, no run started from the app, and +`emulator-5556` only (lane A's `emulator-5554` was never addressed). + +- **Grouped chips.** A run of consecutive `tool` parts in one assistant turn is + now one `FlowRow` of compact chips (`ToolCalls`/`ToolChip`), so the turn's calls + sit together and wrap instead of each taking a full-width row. Each chip keeps + its kind icon, label (the edited file for an `edit`), `+N -M` diff stat, + duration and running spinner. +- **Overlay panel, scroll preserved.** Tapping a chip opens its INPUT/OUTPUT + panel as a `Popup` anchored to that chip, not as inline content. The former + inline panel re-flowed the transcript (a short chat visibly jumped); the popup + is outside the list layout, so opening, failing or dismissing it never moves the + conversation's scroll anchor. `ToolPanelPositionProvider` places it below the + chip, flips it above when there is no room, and keeps it inside the window + margins. The panel is dismissible by its own X or by tapping outside, and its + INPUT and OUTPUT blocks carry the shared `CodeBlock` copy action. +- **Visible failure.** An `error` tool call is now unmistakable: an + error-tinted chip with an `ErrorOutline` icon (`toolFailed`), and its panel + shows an ERROR block with the tool's error text (`ChatFormat.toolFailed`). +- **Diff cards preserved.** An `edit`-family chip still expands to the synthesised + green/red diff (`toolEditDiff`/`DiffView`), unchanged. + +Evidence (macOS, Gradle 9.7.1 `--offline`, `ANDROID_HOME=/Users/alan/Library/Android/sdk`): + +- `gradle :app:assembleDebug :app:testDebugUnitTest --offline` → `BUILD + SUCCESSFUL`; the JUnit XML totals **252 tests, 0 failures, 0 errors, 0 + skipped** (new `toolFailedOnlyForTheErrorStatus`, and + `ToolPanelPositionProviderTest`: below-the-chip, flip-above and margin-clamp + placement). +- The debug APK was installed on `emulator-5556` only and driven against + `.tool_calls_check/chat_stub_tools.py` (a stdlib `/connect` WebSocket double + serving the documented `internal/protocol/chat.go` JSON; test scaffolding, not + the app). Captures were validated with `android/scripts/capture_png.sh -s + emulator-5556`: `01_tools_collapsed.png` (the completed `bash 340ms` and + `src/layout.kt +2 -2 300ms` chips grouped), `02_tools_expanded.png` (the bash + chip expanded to INPUT pretty JSON + OUTPUT monospace with the transcript behind + it unmoved — the chips keep the same on-screen position), and + `03_error_chip_collapsed.png`/`04_error_chip_expanded.png` (the failed `shell` + chip in the error container and its panel INPUT + ERROR text). Tapping outside + the panel dismissed it with the chips still in place. `adb -s emulator-5556 + logcat -d` held 0 `FATAL EXCEPTION` and 0 `E AndroidRuntime` during the sweep. +- **APK publish.** `app-debug.apk` (18,262,465 bytes, sha256 + `160f10ea8a2dc80364bb7f38596d50d08557ccce01f99705b93a8b9bc9216c27`) copied to + `~/.local/share/pipelines/data/artifacts/129/1/pipelines-debug-wip-d7f6500.apk`; + `curl -I http://10.0.0.249:8080/artifacts/129/1/pipelines-debug-wip-d7f6500.apk` + returned `HTTP/1.1 200 OK`, `Content-Type: application/vnd.android.package-archive`, + `Content-Length: 18262465`, and a streamed `GET | shasum -a 256` matched. + +Not verified in this pass: the live brokered server (separate lane), streaming +against real OpenCode, and attachments — unchanged from the passes above. + +## Tool-call review fixes (16 Oct 2026) + +Two findings from the tool-call slice review, both in `android/`, under the same +constraints as above (no new dependency beyond the declared +`material-icons-extended`, no Pipelines server/Go/installer/Docker file touched, +no run started from the app, `emulator-5556` only). + +- **One elapsed derivation.** `ChatScreen.rememberElapsedMillis` re-derived the + running turn's elapsed time by subtracting the start itself, duplicating the + tested `ChatThread.elapsedMillis` and contradicting the two KDocs that claimed + the indicator shared the model's derivation. The composable now ticks a + `rememberElapsedNow(runningSince)` wall clock (once a second, and only while + the turn lives) and reads `thread.elapsedMillis(now)`, so the model and the UI + cannot drift. +- **No per-frame allocation.** `ToolCalls` keyed its open-panel `remember` on + `parts.map { it.id }`, allocating a list on every streaming recomposition. The + key is unnecessary — an id no longer among the calls simply matches nothing — + so `openId` is plain remembered state and a streaming rebuild no longer + allocates a key list. + +Evidence (macOS, Gradle 9.7.1 `--offline`, +`ANDROID_HOME=/Users/alan/Library/Android/sdk`): + +- `gradle :app:assembleDebug :app:testDebugUnitTest --offline` → `BUILD + SUCCESSFUL`; the JUnit XML totals **252 tests, 0 failures, 0 errors, 0 + skipped**. +- The rebuilt debug APK was installed on `emulator-5556` only and driven against + `.tool_calls_check/chat_stub_tools.py` and `chat_stub_error.py` (stdlib + `/connect` WebSocket doubles serving the documented `internal/protocol/chat.go` + JSON; test scaffolding, not the app). Captures were validated with + `android/scripts/capture_png.sh -s emulator-5556` into `.tool_calls_fix/`: + `01_tools_collapsed.png` (the completed `bash 340ms` and `src/layout.kt +2 -2 + 300ms` chips grouped), `02_tools_expanded.png` (the bash chip's INPUT pretty + JSON + OUTPUT monospace), `04_diff_card.png` (the edit chip's synthesised + green/red diff card), `03_dismissed_outside.png` (tapping outside dismissed the + panel with both chips at the same on-screen bounds), and + `05_error_chip_collapsed.png`/`06_error_chip_expanded.png` (the failed `shell` + chip in the error container, with a `Failed tool call` description, and its + panel INPUT + ERROR text). `adb -s emulator-5556 logcat -d` held 0 `FATAL + EXCEPTION` and 0 `E AndroidRuntime` during the sweep. +- **APK publish.** `app-debug.apk` (18,219,766 bytes, sha256 + `b607a879ce2f2ac324fe6fa254918a456337c678abc1781bb79a05d88d539984`) copied to + `~/.local/share/pipelines/data/artifacts/129/3/pipelines-debug-wip-d7f6500.apk`; + `curl -I http://10.0.0.75:8080/artifacts/129/3/pipelines-debug-wip-d7f6500.apk` + returned `HTTP/1.1 200 OK`, `Content-Type: application/vnd.android.package-archive`, + `Content-Length: 18219766`, and a streamed `GET | shasum -a 256` matched. This + supersedes the `129/1` publish above: it is built from the current source, so + the change is in the served artifact (a fresh rebuild can still differ in + `classes5.dex` — the reproducibility caveat recorded in review stands). + +Not verified in this pass: the live brokered server (separate lane), streaming +against real OpenCode, and attachments — unchanged from the passes above. +`:app:lintDebug` still cannot run here (the Gradle JVM's `25.0.4.1` version +string is rejected by AGP 8.7.2 lint), unchanged from the slice above. + +## Tool-call review fixes, second pass (16 Oct 2026) + +Three findings from the tool-call slice's second review, all in `android/` under +the same constraints (no new dependency beyond the declared +`material-icons-extended`, no Pipelines server/Go/installer/Docker file touched, no +run started from the app, `emulator-5556` only). + +- **A failed edit call shows its error.** The panel only rendered the diff for an + `edit`-family call, because the `diff != null` branch sat above the `failed` + check, so a *failed* edit showed no error text. The ERROR block is now emitted + whenever `toolFailed`, alongside the diff when one exists; a failed non-edit call + keeps INPUT + ERROR, and a successful edit still shows the diff alone. +- **The error message, not the partial output.** `ChatFormat.toolOutputText` + returned non-blank `state.content` before it considered `state.error`, and that + text fed the ERROR block, so a failed call carrying both showed its partial + output under the ERROR heading. The failed call's text now comes from the new + `toolErrorText`, which prefers the server's message and falls back to the output + text only when the failure carries none; `toolOutputText` is the OUTPUT + derivation again. +- **README mechanism corrected.** The follow-the-stream bullet claimed a + `snapshotFlow` on the list's scroll state; the code consumes + `listState.interactionSource.interactions` (`DragInteraction.Start`/`Stop`/`Cancel`), + as the bullet now says. + +Evidence (macOS, Gradle 9.7.1, `ANDROID_HOME=/Users/alan/Library/Android/sdk`): + +- `gradle :app:testDebugUnitTest :app:assembleDebug` → `BUILD SUCCESSFUL`; the + JUnit XML totals **253 tests, 0 failures, 0 errors, 0 skipped** (the failed-text + helper split into `toolOutputFallsBackToNoOutput` and + `toolErrorPrefersTheMessageOverPartialOutput`, which asserts the message wins over + a failed call's partial output). +- The rebuilt debug APK was installed on `emulator-5556` only and driven against + `.tool_calls_fix2/chat_stub_failed_edit.py` (the review tool stub plus a failed + `edit` call with `oldString`/`newString` and a failed `bash` call carrying both + partial output and an error; stdlib `/connect` WebSocket double, test + scaffolding, not the app). Captures were validated with + `android/scripts/capture_png.sh -s emulator-5556` into `.tool_calls_fix2/`: + `04_failed_edit_expanded.png` shows the failed edit chip's diff *and* its ERROR + block (`oldString not found in src/layout.kt`), and + `06_failed_bash_expanded.png` shows the failed bash call's INPUT + ERROR + (`exit status 2: src/nope.kt: No such file`), the error text — not the partial + output — under the ERROR heading. `uiautomator dump` confirms the same nodes; + `adb -s emulator-5556 logcat -d` held 0 `FATAL EXCEPTION` and 0 `E + AndroidRuntime` during the sweep. +- **APK publish.** `app-debug.apk` (18,219,360 bytes, sha256 + `4522aa62c8a3a59033aa2cca8b7fba65267dbdde473284585eaae47f03aeda2f`) copied to + `~/.local/share/pipelines/data/artifacts/129/5/pipelines-debug-wip-d7f6500.apk`; + `curl -I http://10.0.0.249:8080/artifacts/129/5/pipelines-debug-wip-d7f6500.apk` + returned `HTTP/1.1 200 OK`, `Content-Type: application/vnd.android.package-archive`, + `Content-Length: 18219360`, and a streamed `GET | shasum -a 256` matched. This + supersedes the `129/3` publish above: it is rebuilt from the current source, so + the fix is in the served artifact (the `classes5.dex` reproducibility caveat + recorded in review stands). + +Not verified in this pass: the live brokered server (separate lane), streaming +against real OpenCode, and attachments — unchanged from the passes above. diff --git a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt index 59dd52a0..59b0746b 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt @@ -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 } } } diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatFormat.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatFormat.kt index a1a98709..d940f8a6 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatFormat.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatFormat.kt @@ -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" diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatScreen.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatScreen.kt index 17a9a3c8..18d4ccb5 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatScreen.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatScreen.kt @@ -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) { + 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) { + // 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(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)) } } } diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt index ef38751c..b59bdbd1 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt @@ -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 = _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 { + 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 diff --git a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatFormatTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatFormatTest.kt index 9ecdacaf..6fe2f05a 100644 --- a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatFormatTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatFormatTest.kt @@ -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"))) diff --git a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatStreamTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatStreamTest.kt new file mode 100644 index 00000000..29a4c29b --- /dev/null +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatStreamTest.kt @@ -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() + 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) + } +} diff --git a/android/app/src/test/java/net/awan/pipelines/ui/chat/ToolPanelPositionProviderTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/chat/ToolPanelPositionProviderTest.kt new file mode 100644 index 00000000..ce0045cc --- /dev/null +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/ToolPanelPositionProviderTest.kt @@ -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))) + } +}