Repository navigation
Description
Attach large and non-image files to a Session Chat prompt by streaming them to the server with progress and cancel, instead of base64-encoding everything into the prompt body, matching the official opencode desktop app.
Background
Both apps currently send every attachment as a data: URL inside the prompt JSON and reject files over 20 MB (AttachmentLimits, PromptJsonBodyWriter / PromptJSONBodyWriter). The server caps inline files[] entries at 20 MB (MAX_ATTACHMENT_BYTES in packages/core/src/session/prompt.ts), so that limit cannot be raised client-side.
opencode v2.0.6 added an upload route, and the desktop app (v2.0.8+) uses it for everything the model does not read natively. Source: packages/app/src/composer/attachments/{attachments.ts,destination.ts,uploads.tsx} and packages/protocol/src/groups/fs.ts on origin/dev.
Server contract (v2 only)
POST /api/experimental/fs/write?path=<path>&location[directory]=<dir>with the raw bytes asapplication/octet-streamand the usual Basic Auth. Creates missing parent directories, is not confined to the project directory, returns{ location, data: { path } }with the resolved absolute path.GET /api/inforeturnspaths.tmp. Desktop writes to<tmp>/uploads/<uuid>/<original filename>(one directory per file, no collisions). The uuid must be URL-safe (fixed upstream in v2.0.25).GET /api/fs/read/*?location[directory]=<dir>serves files with ETag, Last-Modified and byte ranges; desktop uses it to load image previews from the server.- Availability: v2.0.6+ on
origin/devandorigin/beta. Not on opencode 1.18.x (which has/api/fs/read|list|findbut no write) and there is no classic v1 equivalent. Route is marked experimental upstream.
Desktop policy to mirror
- Detect a mime per file: png/jpeg/gif/webp/pdf keep their type, text-like files normalize to
text/plain, everything else is sniffed on the first 4 KB for binary bytes. - If the selected model reads the type natively (
capabilities.input.image/.pdf) and the file is ≤ 20 MB, send it inline as adata:URL infiles[](current behaviour). - Otherwise upload it with
fs.write, then send:- a prompt text line
Attached file: \`` appended after the user text, and metadata.attachments: [{ name, mime, path }]on the prompt so the timeline can render a chip.
The agent opens the file with its own tools. The uploaded file is not placed infiles[].
- a prompt text line
- Upload UX: per-file progress with cancel, duplicate detection ("This file has already been uploaded"), "Upload failed" toast, and sending is blocked while any upload is pending.
Mobile decisions
These fill gaps the desktop policy leaves open. Both apps follow them.
- Text-like mime: a detected type starting with
text/, or one ofapplication/json,application/xml,application/yaml,application/javascript,application/x-sh, normalizes totext/plain. Anything else is sniffed on the first 4 KB: a NUL byte means binary (upload), none means text. Both apps share the same test vectors for this rule. - Duplicate detection: in scope. A file counts as a duplicate when its name and byte size match a file already attached to the current draft, pending or uploaded. The duplicate is not attached and the composer shows "This file has already been uploaded".
- Native-read check: uses
capabilities.input.image/.pdfof the selected model. If the app does not already expose these in the model domain type, the adapter adds them. - Preview location:
/api/fs/read/*is confined to itslocation[directory](onlyfs/writereaches outside it), so the session directory cannot read an upload. Read an upload withpaths.tmp(or the file's parent folder) aslocation[directory]and the relative path. Verified against a v2 server.
Decision
Prototype: shared privately, not linked here.
Composer upload progress is variant A. Keep the existing full-width attachment chip. While an upload is in flight, the detail line shows progress or failure, a hairline shows percent, and the chip remove control cancels. Do not add a separate upload notice, an upload-manager section, or a full-bleed composer preview. Send stays the accent circle and is disabled while any upload is pending.
Rejected for the composer: B (a composer notice owns progress; chips stay IMAGE / PDF / TEXT) and C (full-bleed image preview in the composer).
Timeline images, including an image on an assistant message:
- Show the image inline with the message, preserving aspect ratio. Cap height so a portrait or phone screenshot does not take over the thread. Do not crop the content to a square thumb. The prototype cap was 220px; that number is not a token.
- Tap opens a full-screen viewer that letterboxes on black and shows the whole image, for 16:9, 3:4, square, panorama, and phone-screenshot ratios.
- Tap outside the image, or the close control, dismisses. Tapping the image itself does not dismiss.
Capability and fallback
The write route is experimental. A changed, renamed, or missing contract must not surface a new error. The user only sees the errors the current data-URL path already shows, including This file is larger than 20 MB.
Do not branch on the server version string. /api/info reports local or 0.0.0-<channel> for source builds, and this route can change without a version bump. Follow V2ServerContract: read /openapi.json once per connection, default the feature off.
Turn streamed upload on only when the document still matches the shape we send:
POST /api/experimental/fs/writewith required querypath, anapplication/octet-streambody, and a 200 schema whosedata.pathresolvesGET /api/infodeclarespaths.tmp
Anything else — v1, missing document, parse failure, renamed path, dropped field — leaves the flag false. Do not discover the route by writing a file.
If the flag was true and the live call still fails, classify the failure:
| Failure | Meaning | Result |
|---|---|---|
404, 405, 400, 415, or a 200 body without data.path |
Contract miss | Clear the flag for this connection and send that file the old way. No "Upload failed". |
| Timeout, lost connection, 5xx, or 401/403 | Transport or auth failure | Remove the chip and show "Upload failed". Keep the flag on. Do not fall back silently. |
| User taps cancel | User cancel | Abort the request, remove the chip, show nothing. |
A file the old path cannot take (over 20 MB) after a contract miss still gets the existing This file is larger than 20 MB. error. A failed image preview through /api/fs/read/* degrades to the chip with no preview, not an error toast.
Acceptance criteria
- On a v2 server that exposes the write route, a non-image file of any size and an image over 20 MB attach, show progress, can be cancelled, and the sent prompt contains the
Attached file:line plusmetadata.attachments. - Images and PDFs the model reads natively and ≤ 20 MB keep going inline as today.
- On servers without the route, and on any contract mismatch or live write failure (404, 400, 415, timeout, body without
data.path), the current data-URL path is used unchanged. No new error is shown. The 20 MB limit still applies with the existing error. A failed server image preview degrades to a chip with no preview. - Sent messages in the timeline show an attachment chip for each non-image
metadata.attachmentsentry. Image attachments, including on an assistant message, preview inline with aspect ratio preserved and a height cap, loaded through/api/fs/read/*when the bytes are on the server. Tap opens a full-screen letterboxed viewer; tap outside the image or the close control dismisses. - Send is disabled while an upload is in flight. A transport or auth failure removes the chip and shows "Upload failed". A user cancel removes the chip and shows nothing. A contract miss falls back silently per the table above.
- Attaching a file with the same name and size as one already in the draft is refused with "This file has already been uploaded".
- Behaviour is a synchronized Feature Slice: same rules on iOS and Android, with unit tests for the inline-versus-upload decision and the prompt body shape.
Remaining:
- Upload remote and domain adapter behind
V2ServerContract: flag on only for the knownfs.write+paths.tmpshape; probe or live-call failure clears the flag and uses the data-URL path with no new error - Composer attach flow: mime detection (shared test vectors), inline-versus-upload decision, duplicate detection, progress and cancel on the existing attachment chip (variant A), send blocked while pending
- Failure classification: contract miss vs transport/auth failure vs user cancel, per the table
- Prompt build:
Attached file:text line andmetadata.attachmentsfor uploaded files; keep data-URL fallback - Timeline: metadata chips for non-image uploads; inline aspect-correct image previews via
/api/fs/read/*, including assistant messages, plus a full-screen viewer that dismisses on tap outside - Android implementation and tests
- iOS implementation and tests
Activity
ivanfenenko commentedon Oct 26, 2026
Recorded the Session Chat prototype decision.
Composer upload progress is variant A: progress, failure, and cancel stay on the existing attachment chip. Send stays the accent circle and is disabled while an upload is pending. Rejected a composer notice (B) and a full-bleed composer preview (C).
Timeline images, including assistant messages, stay aspect-correct with a height cap, and tap opens a letterboxed full-screen viewer. Tap outside the image, or close, dismisses. Tapping the image does not.
Prototype: http://ivans-mac-mini-1:8787/2026-10-26/2314-session-chat--image-ratios-and-dismiss/
ivanfenenko commentedon Oct 26, 2026
Experimental upload is optional.
V2ServerContractturns it on only when/openapi.jsonstill has thefs.write+paths.tmpshape we send. A missing, renamed, or rejected route clears the flag and uses the current data-URL path. The user does not see a new error; a file over 20 MB still gets the existing size error.ivanfenenko commentedon Oct 26, 2026
Stage 1 (timeline) is complete on both platforms: Android and iOS tests pass, landed as its own commit (6e3920c) before Stage 2. Stage 2 (streamed upload) follows in 664d151. Draft PR: #855
#644 Address PR review: classification, aligned platforms, less dupli…
#644 Retry the upload tmp directory when /api/info fails
#644 Streamed attachment uploads in Session Chat (#855)
ivanfenenko commentedon Oct 27, 2026
Shipped in #855 (merge 326bb0f).
Close finished issues with Fixes in pull requests (#868)
#870 Paste and drop attachments in Session Chat (#875)