feat(web): Web Share Target -> Android system share sheet integration
Summary
Adds a Web Share Target to the PWA so HAPI appears in the Android system Share sheet. Operator long-presses a JPG/PDF/text in another app, taps HAPI, lands in a session picker, picks a session, and the file lands as an attachment on the existing composer ready to send.
Closes tiann/hapi#932.
Problem
Sharing a file (image, PDF, document) into a HAPI session today requires a four-step round trip: save the file locally → open HAPI → navigate to the session → use the composer's attach button to re-pick the file from the system file picker. The Android Share sheet is the canonical surface for "send this thing to that app," and the Web Share Target API is the canonical PWA mechanism to participate in it.
Approach
End-to-end pipeline reuses the existing attachment infrastructure (POST /sessions/:id/upload plus the composer's attachment adapter). No hub or shared-package changes; web-only diff.
- Manifest (
web/vite.config.ts) declaresshare_target: { action: '/share', method: 'POST', enctype: 'multipart/form-data' }with title/text/url params and a broad MIME accept list (image/*,application/pdf,text/*,application/json,application/zip,*/*fallback). - Service worker (
web/src/sw.ts) intercepts POST/share, parses the multipart payload, persists it in IndexedDB under a transfer id (TTL: 1h, swept on SW activate), and 303-redirects to/share?id=<id>. The 303 is mandatory — it forces Chrome to convert the POST into a GET so the SPA route can mount. - Picker route (
web/src/routes/share/index.tsx) reads the transfer from IDB, renders a preview (thumbnail / filename+size / multi-file summary / text-or-url), and shows a list of currently-active sessions plus a "+ New session" CTA. The session list is snapshotted at first non-loading render and sorted byupdatedAtdesc — without the snapshot the list reshuffles under the operator's finger as SSE updates roll in (operator dogfood feedback). - Composer hand-off (
web/src/components/SessionChat.tsx) uses a smallsharePendingStatesessionStorage primitive (one key, atomic consume on read) so the IDB transfer id survives the picker →/sessions/:id(or/sessions/new→/sessions/:id) navigation. A newShareSeedConsumercomponent mounts insideAssistantRuntimeProvider, waits forsession.active, then seeds composer text + attachments via the existingcomposer().setText()andcomposer().addAttachment()APIs and deletes the IDB row so refreshes don't replay.
The picker payload parsing and IDB ingest are extracted as pure functions (buildSharePayloadFromFormData, ingestShareRequest) so the SW lifecycle can stay un-tested while the multipart-shape logic gets covered by vitest.
Testing
- Unit (vitest, web): 11 new tests in
web/src/lib/sharePendingState.test.ts(3) andweb/src/lib/shareTransfer.test.ts(8). Cover text-only, single-image, multi-file, missing fields, empty MIME fallback, ingest redirect shape, error propagation, and atomic-consume of the sessionStorage hand-off. - Typecheck:
bun typecheckclean (cli, web, hub). - Test suite:
bun run test— 975/975 web pass; 11 new passing. - Manual: dogfooded on Pixel 10a / Android Chrome against this branch via the operator's daily-driver soup. Photos → Share → HAPI → pick session → JPG lands in composer attachment list → send → agent receives the image. Operator approval given.
Limitations
Disclosed up-front so reviewers and downstream users have realistic expectations:
- WebAPK regeneration is install-time only. Chrome on Android reads
share_targetfrom the manifest at WebAPK install. Existing PWA installs must be uninstalled (Settings → Apps → HAPI → Uninstall, not "Remove from Home Screen") and reinstalled via Chrome → 3-dot menu → "Install app" before HAPI appears in the Share sheet. Chrome's WebAPK auto-update path refreshes name/icon/theme but does not add new intent filters likeSEND. - iOS Safari ignores
share_targetentirely. This is a Safari limitation, not addressable from a PWA. iOS Share Extensions are a separate Apple-native code path, out of scope. - Some Chrome versions filter
*/*. The accept list keeps explicit MIME prefixes first; the*/*fallback is best-effort for "miscellaneous file shares" but is not honored on every Chrome build. - Native companion-app share intent (
dev.hapi.companion) is a complementary fast-follow that would let users share into HAPI without installing the PWA. Out of scope here.
Related
- web.dev: Web Share Target
- Issue: tiann/hapi#932