diff --git a/web/claw/README.md b/web/claw/README.md index 7a53caa..f71d6f4 100644 --- a/web/claw/README.md +++ b/web/claw/README.md @@ -112,11 +112,16 @@ Two consequences worth knowing before changing this code: and again on the stream's `consent_resolved` echo; nothing persists client-side, so a reload rebuilds the cards from the replay. - **Attachments render inline.** An `attachment` event becomes a card - — media glyph, file name, B/KB/MB size, caption. Images fetch the - bearer-guarded bytes (`GET /v1/attachments/{id}`, blob → object URL, - like the identity images) and render capped at 240px; code and file - cards download on click. Object URLs cache per id, so a replay never - refetches. + — media glyph, file name, B/KB/MB size, caption. The media kind comes + from the file extension, decided by the daemon when it stores the + file, so a `.png` renders and a `.patch` downloads without the client + guessing. Images fetch the bearer-guarded bytes + (`GET /v1/attachments/{id}`, blob → object URL, like the identity + images) and render capped at 240px; every card downloads on click, + images included. A card lives inside the turn that produced it, so + the scan that fetches image bytes walks finished turns and the live + one, not just the top-level rows. Object URLs cache per id, so a + replay never refetches. - **Your images ride along.** The composer's `+` — and an image on the clipboard — stage as removable chips (`▣1 photo.png 1.2 MB`), the claw's own budget enforced client-side (8 a message, 5 MiB decoded diff --git a/web/claw/app/app.vue b/web/claw/app/app.vue index f7c03b9..7048230 100644 --- a/web/claw/app/app.vue +++ b/web/claw/app/app.vue @@ -22,7 +22,7 @@ import type { StreamStatus, } from '~/composables/useClaw' import { composeChips, useTranscript } from '~/composables/useTranscript' -import type { StagedImage } from '~/composables/useTranscript' +import type { AttachmentCard, StagedImage } from '~/composables/useTranscript' const TOKEN_STORE_KEY = 'claw.token' const ACTIVE_STORE_KEY = 'claw.activeKey' @@ -131,39 +131,49 @@ const attemptedAttachments = new Set() // their attachment ids, never the bytes; locally sent rows hold their // own object URLs. // -// The watch is deep (rows mutate in place as their bytes land), and it -// fires on every streamed delta, so the scan covers only the items the -// last pass did not see — the transcript only ever appends. -let scannedItems = 0 - -watch( - transcriptItems, - (items) => { - for (const item of items.slice(scannedItems)) { - if (item.kind === 'attachment') { - const { id, media, url } = item.attachment - if (media !== 'image' || url !== null || attemptedAttachments.has(id)) continue +// The cards sit at two depths. One handed over mid-turn lands in the +// open turn's entries and rides into the finished turn with it, so a +// scan of the top-level rows alone would never see it and the card +// would stay a name with no picture. Every path that can hold a card is +// walked here; the attempt set makes a repeated pass free. +function requestAttachmentImages(): void { + const fetchAttachment = (attachment: AttachmentCard) => { + const { id, media, url } = attachment + if (media !== 'image' || url !== null || attemptedAttachments.has(id)) return + attemptedAttachments.add(id) + claw + .attachmentUrl(id) + .then((fetched) => transcript.setAttachmentUrl(id, fetched)) + .catch((err) => noteApiError(err)) + } + for (const item of transcriptItems.value) { + if (item.kind === 'attachment') { + fetchAttachment(item.attachment) + } else if (item.kind === 'turn') { + for (const entry of item.turn.entries) { + if (entry.kind === 'attachment') fetchAttachment(entry.attachment) + } + } else if (item.kind === 'sent') { + for (const image of item.images) { + const { id, url } = image + if (id === null || url !== null || attemptedAttachments.has(id)) continue attemptedAttachments.add(id) claw .attachmentUrl(id) - .then((fetched) => transcript.setAttachmentUrl(id, fetched)) + .then((fetched) => transcript.setSentImageUrl(id, fetched)) .catch((err) => noteApiError(err)) - } else if (item.kind === 'sent') { - for (const image of item.images) { - const { id, url } = image - if (id === null || url !== null || attemptedAttachments.has(id)) continue - attemptedAttachments.add(id) - claw - .attachmentUrl(id) - .then((fetched) => transcript.setSentImageUrl(id, fetched)) - .catch((err) => noteApiError(err)) - } } } - scannedItems = items.length - }, - { deep: true }, -) + } + for (const entry of transcript.live.value?.entries ?? []) { + if (entry.kind === 'attachment') fetchAttachment(entry.attachment) + } +} + +watch([transcriptItems, transcript.live], requestAttachmentImages, { + deep: true, + immediate: true, +}) // --- the agent's identity -------------------------------------------- const identity = ref(null) @@ -652,7 +662,11 @@ async function downloadAttachment(id: string, name: string): Promise { const anchor = document.createElement('a') anchor.href = url anchor.download = name + // A click on a detached anchor is a no-op in some browsers; the + // element joins the document for the length of the click. + document.body.append(anchor) anchor.click() + anchor.remove() } catch (err) { noteApiError(err) } @@ -719,7 +733,7 @@ function resubscribe(): void { transcript.reset() olderAvailable.value = true attemptedAttachments.clear() - scannedItems = 0 + requestAttachmentImages() }, onStatus: (status) => { streamStatus.value = status @@ -729,7 +743,7 @@ function resubscribe(): void { // during the outage should retry against the recovered stream. if (status === 'reconnecting') { attemptedAttachments.clear() - scannedItems = 0 + requestAttachmentImages() } if (status === 'open') void refreshSessions() }, diff --git a/web/claw/app/components/TurnAttachmentCard.vue b/web/claw/app/components/TurnAttachmentCard.vue index e77ea05..0f73a63 100644 --- a/web/claw/app/components/TurnAttachmentCard.vue +++ b/web/claw/app/components/TurnAttachmentCard.vue @@ -1,7 +1,9 @@