chat: attach files to a message from the composer

Let the operator attach one or more files to the next chat message and send
them with it. The platform multi-document picker returns content URIs; the app
resolves only each file's name, size and MIME (confirming it can be opened) and
holds the attachments per chat, exactly like the draft. Nothing is read or
transmitted until the message is sent.

- Composer: an attach action, a strip of removable chips (image thumbnail or
  file glyph, name, size, remove), and a send that is blocked while any
  attachment is flagged.
- Wire: one shared builder, ChatProtocol.promptRequest(sessionId, text, files),
  carries the attached URIs on the chat_prompt frame, in attachment order and
  omitted for a text-only prompt so that shape is unchanged.
  PipelinesRepository.chatPrompt takes the same list.
- Transcript: a sent turn carries each attachment as a ChatProtocol.PART_FILE
  part, drawn through the existing UserParts -> FilePart display path rather
  than a second rendering.
- Honest failures: an unreadable or over-24-MiB file is attached with its reason
  and blocks the send; a server rejection takes the optimistic bubble back out
  and returns the files flagged with the server's message. Attaching never
  transmits on its own, and attachments clear once the message is sent.

Review follow-ups in the same slice: the composer now uses ui/Format.kt's
humanBytes instead of a duplicate formatter; optimistic user-turn ids come from a
monotonic counter so removing a rejected turn cannot re-mint a live key;
restoreAttachments keeps a file attached between send and rejection; the
thumbnail decode catches Exception rather than Throwable.

Checks made for this commit:
- The change is android/ only; the server, its Go source, the installer and the
  Docker files are untouched, no run is started from the app, and no dependency
  was added beyond the already-declared material-icons-extended.
- Packaged authoring skill references (internal/author/skill/references/): no
  update needed. operations.md documents the brokered chat server, which this
  change does not alter; the server still stores user turns as text and ignores
  the prompt's files, so that reference stays accurate.
- Packaged templates (internal/tui, internal/server) and the root README/topic
  docs: no update needed; none describes the Android chat composer. The
  Android-facing documentation lives in android/README.md, which this change
  updates with the attachment slice, its known server-side gap and its tests.

Tests: ChatAttachmentTest (5 cases), ChatAttachmentStateTest (5 cases) and a new
ChatProtocolTest case cover building the request with attachments (and omitting
them for a text-only prompt), removing one, the unreadable and oversized paths,
which files travel with the prompt, the optimistic turn's file parts, clearing
after send, and the rejected path restoring the files flagged. Verified by the
review stage with gradle --offline --no-daemon :app:testDebugUnitTest
:app:assembleDebug -> BUILD SUCCESSFUL, 487 tests / 0 failures / 0 errors /
0 skipped.
This commit is contained in:
2026-10-17 13:35:56 +01:00
parent d1a859ec7d
commit 829d6d3551
12 changed files with 921 additions and 30 deletions
+103
View File
@@ -5538,3 +5538,106 @@ The composable's frame timing (placing the list before its first draw, the
instant correction for a last message taller than the viewport, and the fallback
that brings an item appended below the fold into view) is argued from the pinned
Compose BOM (2024.10.00) and the pure decisions it delegates to, not observed.
## Attaching files to a message (17 Oct 2026)
A chat message could only be text even though the prompt already belongs to a
turn the phone builds. This slice lets the operator attach one or more files to
the next message, shows them with a name and an image thumbnail, lets one be
removed, carries them on the prompt, and clears them once the message is sent.
`android/` only; nothing in the Pipelines server, its Go source, the installer or
the Docker files was touched, no dependency was added beyond the
already-declared material-icons-extended, and no run is started from the app.
- **Attaching never transmits.** The picker (`ActivityResultContracts
.OpenMultipleDocuments`, the platform picker) returns content URIs; the app
resolves only each file's name, size and MIME and confirms it can be opened
(`resolveChatAttachment`, `ui/chat/ChatAttachments.kt`). The bytes are not read
here, so nothing leaves the device until the message carrying them is sent.
- **What is attached.** `ChatThread.attachments` holds `ChatAttachment` values
(uri/name/mime/size and an optional error), kept per chat exactly like the
draft, so switching chats never loses them. Each gets a store-assigned id
(`ChatStore.attach`) so it can be keyed and removed individually; the composer
strip shows an image thumbnail (a downsampled local decode, off the main
thread) or a file glyph, the name, the size, and a remove action.
- **The prompt carries them.** The send builds the request through one builder,
`ChatProtocol.promptRequest(sessionId, text, files)`: `files` is the attached
URIs from `attachmentFiles`, in attachment order, and is omitted entirely for a
text-only prompt (so the wire shape a text-only prompt always sent is
unchanged). `PipelinesRepository.chatPrompt` takes the same `files` list.
- **The transcript shows them.** A sent user turn carries each attachment as a
`ChatProtocol.PART_FILE` part (`attachmentPart`), which `MessageItem`'s
existing parts rendering draws (`UserParts` → `FilePart`): an image as a
preview, anything else as an icon and its name. There is no second attachment
list under the bubble.
- **Honest failures.** A file that cannot be queried or opened, or is over
`MAX_ATTACHMENT_BYTES` (25 MiB), is attached with the reason set against it
(`attachmentError`), never dropped. `ChatThread.canSend` is false while any
attachment is flagged, so the message is not sent without it; a server
rejection (`sendChatPrompt`) takes the optimistic bubble back out, returns the
files flagged with the server's message, and keeps the draft, so nothing is
silently lost.
- **Cleared after send.** `sendChatPrompt` clears the draft and the attachments
once the prompt is built, while the optimistic turn still shows them.
**Known server-side gap (verified by reading the tree, not live).** The brokered
server stores a user turn as text and does not forward the prompt's `files`
(`internal/server/chat.go` calls `broker.Prompt(ctx, sessionID, prompt.Text)`,
and `internal/opencode/chat.go` posts only `{"text": …}`), and the deployed
server predates chat. So the attachment the phone sends is not yet echoed back in
history; the transcript shows it from the app's own optimistic turn, and an
end-to-end file round-trip needs the server-side lane. This slice is the client
capability the request field and the display path already allow.
Tests (`ChatAttachmentTest` 5 cases, `ChatAttachmentStateTest` 5 cases, and a new
`ChatProtocolTest` case): the request built with attachments (and omitted for a
text-only prompt), removing one, the unreadable and oversized paths (reported by
name, blocking the send), which files travel with the prompt, the optimistic
turn's file parts, clearing after send, and the rejected path putting the files
back flagged.
Evidence (this run's implementer pass, macOS, JDK 17.0.20.1,
`ANDROID_HOME=/Users/alan/Library/Android/sdk`, Gradle 8.11.1 `--offline
--no-daemon` — a single-use daemon is forked for the JVM settings):
`gradle --offline --no-daemon :app:assembleDebug :app:testDebugUnitTest` →
`BUILD SUCCESSFUL`; the JUnit XMLs total **485 tests / 0 failures / 0 errors / 0
skipped**. The APK was published to this run's artifacts directory as
`pipelines-debug-wip-d1a859e.apk` (18,470,240 bytes, sha256
`378e7e8f03ca7e75108db1217aba3b81efca396fecfcbf9dec2a6bf964319dfa`; `d1a859e` is
`git rev-parse --short HEAD`, the tree being uncommitted WIP on top of it) and is
served at
`http://10.0.0.75:8080/artifacts/249/1/pipelines-debug-wip-d1a859e.apk`
(`HTTP/1.1 200 OK`, `Content-Type: application/vnd.android.package-archive`,
`Content-Length: 18470240`).
Not verified in this pass: the picker, thumbnails, composer strip and sent
message on a device — the brief keeps implementation verification to the build
and the unit tests, and visual review owns the device; and the live file
round-trip, which the server-side gap above makes unavailable.
### Review follow-ups (same slice, 17 Oct 2026)
A code-review pass found four concrete items, all fixed without changing the
request shape or the display path:
- The composer's own byte formatter duplicated `ui/Format.kt`'s documented
`humanBytes`. `attachmentError` and the chip's size line now call `humanBytes`,
so a size reads the same in the composer as on the artifact screens (a sub-MiB
size now keeps its one decimal, e.g. `900.0 KiB`).
- Optimistic user-turn ids come from a monotonic counter instead of the count of
live local turns. Removing a rejected turn no longer lets the next append mint
the id it held, so the transcript's per-id keys can never duplicate.
- `restoreAttachments` keeps a file attached between sending and the rejection
(it previously replaced the whole list and silently dropped it).
- The thumbnail decode catches `Exception`, not `Throwable`, so an `Error`-level
failure is no longer swallowed into a null glyph.
Re-verified with `gradle --offline --no-daemon :app:testDebugUnitTest
:app:assembleDebug` → `BUILD SUCCESSFUL`; the JUnit XMLs total **487 tests / 0
failures / 0 errors / 0 skipped** (two new `ChatAttachmentStateTest` cases cover
the counter and the kept file). The APK was republished for this stage as
`pipelines-debug-wip-d1a859e.apk` (18,470,240 bytes, sha256
`fbd640af7749cb483e5c01cd1ca06c074fc9db620049ab07d63c5a03c5298f63`) and is served
at `http://10.0.0.75:8080/artifacts/249/3/pipelines-debug-wip-d1a859e.apk`
(`HTTP/1.1 200 OK`, `Content-Type: application/vnd.android.package-archive`,
`Content-Length: 18470240`).