feat(android): composer attachments (B-M3f)

Composer attachment tray wired end to end: "+" bottom sheet (photo
library / camera / files), upload-on-pick against POST upload
(JSON+base64), per-chip uploading -> ready/failed states with retry and
best-effort delete-on-remove, AttachmentMetadata riding SendMessageRequest
and the optimistic row so user bubbles thumbnail instantly, and a
UserTextBlockView upgrade decoding wire previewUrl data URLs (web-sent
messages thumbnail too).

Mobile-data compression policy (differs from web, which uploads
originals): recompressible images over 4 MB downscale to 2048 px JPEG
q85 (filename swaps to .jpg); GIF/SVG/non-images keep originals; hard
50 MB reject with a notice, plus a capped read guarding unknown-size
picks. previewUrl embeds a <=512 px JPEG thumb instead of the web's
full-size data URL to keep send bodies small.

Simplifications noted in KDoc: attachments do not persist in drafts v1
(holder onCleared discards un-sent uploads after best-effort deletes);
inactive sessions fail the upload chip until a text send auto-resumes.
Scheduled sends guard the wire constraint (scheduledAt excludes
attachments) with a loud check.

Seam: ChatSessionApi now extends AttachmentUploadApi (HapiApi methods
gain override); camera captures use a FileProvider cache scratch,
rememberSaveable across rotation.

Tests: pure policy decisions (plan/sample-size/filenames/data URLs),
controller state machine incl. mid-upload removal orphan cleanup,
VM send/retry/refusal flows with metadata assertions, MockWebServer
wire shapes for upload + delete. Full gate green (protocol/data/app
tests + assembleDebug).
This commit is contained in:
weishu
2026-08-18 10:46:44 +08:00
parent 0f347de1bc
commit ac5cebf32f
19 changed files with 1803 additions and 40 deletions
@@ -3,10 +3,33 @@ package app.hapi.data.api
import app.hapi.protocol.wire.ApprovePermissionRequest
import app.hapi.protocol.wire.CancelMessageResponse
import app.hapi.protocol.wire.CodexModelsResponse
import app.hapi.protocol.wire.DeleteUploadResponse
import app.hapi.protocol.wire.ResumeSessionResponse
import app.hapi.protocol.wire.SendMessageRequest
import app.hapi.protocol.wire.SlashCommandsResponse
import app.hapi.protocol.wire.SteerQueuedMessageResponse
import app.hapi.protocol.wire.UploadFileResponse
/**
* Message-attachment upload seam (B-M3f). Split from [ChatSessionApi] so the
* composer attachment controller can be tested against a two-method fake.
*/
interface AttachmentUploadApi {
/**
* `POST /api/sessions/:id/upload` — JSON + base64 (NOT multipart),
* ≤ 50 MB decoded → 413. The returned `path` becomes
* `AttachmentMetadata.path` in the send-message body.
*/
suspend fun uploadFile(
sessionId: String,
filename: String,
contentBase64: String,
mimeType: String,
): UploadFileResponse
/** `POST /api/sessions/:id/upload/delete` — best-effort orphan cleanup. */
suspend fun deleteUpload(sessionId: String, path: String): DeleteUploadResponse
}
/**
* The endpoint seam the chat interaction layer (B-M3ab) drives — send /
@@ -14,9 +37,10 @@ import app.hapi.protocol.wire.SteerQueuedMessageResponse
* session config. [HapiApi] is the production implementation; ViewModel tests
* substitute a scripted fake and assert the exact request bodies.
*
* Extends [MessagesApi] so one fake covers the window store's transport too.
* Extends [MessagesApi] so one fake covers the window store's transport too,
* and [AttachmentUploadApi] for the composer attachment flow (B-M3f).
*/
interface ChatSessionApi : MessagesApi {
interface ChatSessionApi : MessagesApi, AttachmentUploadApi {
/** `POST /api/sessions/:id/messages` — `{ok:true}`; the row arrives via SSE. */
suspend fun sendMessage(sessionId: String, message: SendMessageRequest)
@@ -520,7 +520,7 @@ class HapiApi(
* multipart), ≤ 50 MB decoded → 413. The returned `path` feeds the
* `attachments` of send-message.
*/
suspend fun uploadFile(
override suspend fun uploadFile(
sessionId: String,
filename: String,
contentBase64: String,
@@ -533,7 +533,7 @@ class HapiApi(
)
/** `POST /api/sessions/:id/upload/delete`. */
suspend fun deleteUpload(sessionId: String, path: String): DeleteUploadResponse =
override suspend fun deleteUpload(sessionId: String, path: String): DeleteUploadResponse =
request(
"POST",
url("api", "sessions", sessionId, "upload", "delete").build(),
@@ -166,6 +166,45 @@ class HapiApiTest {
)
}
@Test
fun `upload posts json base64 (not multipart) and yields the hub path`() {
server.enqueue(ok("""{"success":true,"path":"/tmp/uploads/shot.jpg"}"""))
val response = runBlocking {
session.api.uploadFile("s1", "shot.jpg", "QUJDRA==", "image/jpeg")
}
assertTrue(response.success)
assertEquals("/tmp/uploads/shot.jpg", response.path)
val request = server.takeRequest()
assertEquals("/api/sessions/s1/upload", request.path)
assertEquals("POST", request.method)
assertTrue(request.getHeader("Content-Type")!!.startsWith("application/json"))
assertEquals(
buildJsonObject {
put("filename", "shot.jpg")
put("content", "QUJDRA==")
put("mimeType", "image/jpeg")
},
Json.parseToJsonElement(request.body.readUtf8()).jsonObject,
)
}
@Test
fun `delete upload posts the path`() {
server.enqueue(ok("""{"success":true}"""))
val response = runBlocking { session.api.deleteUpload("s1", "/tmp/uploads/shot.jpg") }
assertTrue(response.success)
val request = server.takeRequest()
assertEquals("/api/sessions/s1/upload/delete", request.path)
assertEquals(
buildJsonObject { put("path", "/tmp/uploads/shot.jpg") },
Json.parseToJsonElement(request.body.readUtf8()).jsonObject,
)
}
@Test
fun `approve body supports nested answers`() {
server.enqueue(ok("""{"ok":true}"""))