fix(web/claw): render and download the images a turn hands over

An attachment card lands inside the turn that produced it, and both
passes that touch image cards scanned only the top-level rows. The pass
that fetches bytes never saw the card, so it stayed a name and a size
with no picture. The pass that stores the fetched object URL had the
same blind spot, so even a fetch that ran dropped its result. Both now
walk finished turns and the live one, through one helper that knows
where cards live. Consents move to the same helper: a card whose turn
already closed had the same problem.

The download control was hidden for `media: "image"`, which left an
image with no way out of the store when its bytes did not paint, as
they never did. Every card offers it now. The anchor also joins the
document for the click, which some browsers require before they follow
a `download` on a blob URL.
This commit is contained in:
vhaudiquet committed 2026-11-24 21:17:02 +01:00
1 parent 1a1ace014a
commit 04b06e7623
4 files changed
+83 -67

No files matched your search

+10 -5
View File
@@ -112,11 +112,16 @@ Two consequences worth knowing before changing this code:
and again on the stream's `consent_resolved` echo; nothing persists and again on the stream's `consent_resolved` echo; nothing persists
client-side, so a reload rebuilds the cards from the replay. client-side, so a reload rebuilds the cards from the replay.
- **Attachments render inline.** An `attachment` event becomes a card - **Attachments render inline.** An `attachment` event becomes a card
— media glyph, file name, B/KB/MB size, caption. Images fetch the — media glyph, file name, B/KB/MB size, caption. The media kind comes
bearer-guarded bytes (`GET /v1/attachments/{id}`, blob → object URL, from the file extension, decided by the daemon when it stores the
like the identity images) and render capped at 240px; code and file file, so a `.png` renders and a `.patch` downloads without the client
cards download on click. Object URLs cache per id, so a replay never guessing. Images fetch the bearer-guarded bytes
refetches. (`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 - **Your images ride along.** The composer's `+` — and an image on the
clipboard — stage as removable chips (`▣1 photo.png 1.2 MB`), 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 claw's own budget enforced client-side (8 a message, 5 MiB decoded
+44 -30
View File
@@ -22,7 +22,7 @@ import type {
StreamStatus, StreamStatus,
} from '~/composables/useClaw' } from '~/composables/useClaw'
import { composeChips, useTranscript } from '~/composables/useTranscript' 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 TOKEN_STORE_KEY = 'claw.token'
const ACTIVE_STORE_KEY = 'claw.activeKey' const ACTIVE_STORE_KEY = 'claw.activeKey'
@@ -131,39 +131,49 @@ const attemptedAttachments = new Set<string>()
// their attachment ids, never the bytes; locally sent rows hold their // their attachment ids, never the bytes; locally sent rows hold their
// own object URLs. // own object URLs.
// //
// The watch is deep (rows mutate in place as their bytes land), and it // The cards sit at two depths. One handed over mid-turn lands in the
// fires on every streamed delta, so the scan covers only the items the // open turn's entries and rides into the finished turn with it, so a
// last pass did not see — the transcript only ever appends. // scan of the top-level rows alone would never see it and the card
let scannedItems = 0 // 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.
watch( function requestAttachmentImages(): void {
transcriptItems, const fetchAttachment = (attachment: AttachmentCard) => {
(items) => { const { id, media, url } = attachment
for (const item of items.slice(scannedItems)) { if (media !== 'image' || url !== null || attemptedAttachments.has(id)) return
if (item.kind === 'attachment') { attemptedAttachments.add(id)
const { id, media, url } = item.attachment claw
if (media !== 'image' || url !== null || attemptedAttachments.has(id)) continue .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) attemptedAttachments.add(id)
claw claw
.attachmentUrl(id) .attachmentUrl(id)
.then((fetched) => transcript.setAttachmentUrl(id, fetched)) .then((fetched) => transcript.setSentImageUrl(id, fetched))
.catch((err) => noteApiError(err)) .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 }
}, for (const entry of transcript.live.value?.entries ?? []) {
{ deep: true }, if (entry.kind === 'attachment') fetchAttachment(entry.attachment)
) }
}
watch([transcriptItems, transcript.live], requestAttachmentImages, {
deep: true,
immediate: true,
})
// --- the agent's identity -------------------------------------------- // --- the agent's identity --------------------------------------------
const identity = ref<AgentIdentity | null>(null) const identity = ref<AgentIdentity | null>(null)
@@ -652,7 +662,11 @@ async function downloadAttachment(id: string, name: string): Promise<void> {
const anchor = document.createElement('a') const anchor = document.createElement('a')
anchor.href = url anchor.href = url
anchor.download = name 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.click()
anchor.remove()
} catch (err) { } catch (err) {
noteApiError(err) noteApiError(err)
} }
@@ -719,7 +733,7 @@ function resubscribe(): void {
transcript.reset() transcript.reset()
olderAvailable.value = true olderAvailable.value = true
attemptedAttachments.clear() attemptedAttachments.clear()
scannedItems = 0 requestAttachmentImages()
}, },
onStatus: (status) => { onStatus: (status) => {
streamStatus.value = status streamStatus.value = status
@@ -729,7 +743,7 @@ function resubscribe(): void {
// during the outage should retry against the recovered stream. // during the outage should retry against the recovered stream.
if (status === 'reconnecting') { if (status === 'reconnecting') {
attemptedAttachments.clear() attemptedAttachments.clear()
scannedItems = 0 requestAttachmentImages()
} }
if (status === 'open') void refreshSessions() if (status === 'open') void refreshSessions()
}, },
@@ -1,7 +1,9 @@
<script setup lang="ts"> <script setup lang="ts">
// The agent handing a file over: a head row naming it, the image (when // The agent handing a file over: a head row naming it, the image (when
// there is one) under it, capped so a screenshot cannot take the // there is one) under it, capped so a screenshot cannot take the
// stream; code and file download on click. // stream. Every card downloads, images included: a picture the browser
// cannot paint, an SVG, or bytes that failed to fetch must still come
// out of the store.
import type { AttachmentCard } from '~/composables/useTranscript' import type { AttachmentCard } from '~/composables/useTranscript'
defineProps<{ attachment: AttachmentCard }>() defineProps<{ attachment: AttachmentCard }>()
@@ -35,7 +37,6 @@ function formatBytes(size: number): string {
<span class="attachment-name mono">{{ attachment.name }}</span> <span class="attachment-name mono">{{ attachment.name }}</span>
<span class="attachment-size mono">{{ formatBytes(attachment.size) }}</span> <span class="attachment-size mono">{{ formatBytes(attachment.size) }}</span>
<button <button
v-if="attachment.media !== 'image'"
class="ghost" class="ghost"
:title="`download ${attachment.name}`" :title="`download ${attachment.name}`"
@click="emit('download', attachment.id, attachment.name)" @click="emit('download', attachment.id, attachment.name)"
+26 -30
View File
@@ -409,21 +409,29 @@ export function useTranscript() {
if (pending !== -1) pendingSent.splice(pending, 1) if (pending !== -1) pendingSent.splice(pending, 1)
} }
/** A consent card resolved: flipped in place, or ignored when no /** The cards on the mirror, at either depth. A card lands in the open
* card carries the id. The in-flight turn's entries hold the cards a * turn's entries and rides into the finished turn with it, so a scan
* running turn asked for; finished turns keep their own copies. */ * of the top-level rows alone misses everything a turn handed over.
function resolveConsent(id: string, granted: boolean): void { * The returned objects are the mirror's own, not copies: flipping one
if (live.value) { * flips what the panel renders. */
for (const entry of live.value.entries) { function cards(): TurnEntry[] {
if (entry.kind === 'consent' && entry.consent.id === id) { const found: TurnEntry[] = [...(live.value?.entries ?? [])]
entry.consent.granted = granted for (const item of items.value) {
return if (item.kind === 'consent' || item.kind === 'attachment') {
} found.push(item)
} else if (item.kind === 'turn') {
found.push(...item.turn.entries)
} }
} }
for (const item of items.value) { return found
if (item.kind === 'consent' && item.consent.id === id) { }
item.consent.granted = granted
/** A consent card resolved: flipped in place, or ignored when no
* card carries the id. */
function resolveConsent(id: string, granted: boolean): void {
for (const card of cards()) {
if (card.kind === 'consent' && card.consent.id === id) {
card.consent.granted = granted
return return
} }
} }
@@ -432,17 +440,9 @@ export function useTranscript() {
/** An attachment's object URL landed; stored on its card. The useClaw /** An attachment's object URL landed; stored on its card. The useClaw
* cache keeps a reconnect's replay from refetching the bytes. */ * cache keeps a reconnect's replay from refetching the bytes. */
function setAttachmentUrl(id: string, url: string): void { function setAttachmentUrl(id: string, url: string): void {
if (live.value) { for (const card of cards()) {
for (const entry of live.value.entries) { if (card.kind === 'attachment' && card.attachment.id === id) {
if (entry.kind === 'attachment' && entry.attachment.id === id) { card.attachment.url = url
entry.attachment.url = url
return
}
}
}
for (const item of items.value) {
if (item.kind === 'attachment' && item.attachment.id === id) {
item.attachment.url = url
return return
} }
} }
@@ -466,16 +466,12 @@ export function useTranscript() {
/** Whether a card for this consent id is already on the mirror — a /** Whether a card for this consent id is already on the mirror — a
* replayed request never stacks a second card beside the first. */ * replayed request never stacks a second card beside the first. */
function findConsent(id: string): boolean { function findConsent(id: string): boolean {
if (live.value?.entries.some((e) => e.kind === 'consent' && e.consent.id === id)) return true return cards().some((card) => card.kind === 'consent' && card.consent.id === id)
return items.value.some((item) => item.kind === 'consent' && item.consent.id === id)
} }
/** Whether a card for this attachment id is already on the mirror. */ /** Whether a card for this attachment id is already on the mirror. */
function findAttachment(id: string): boolean { function findAttachment(id: string): boolean {
if (live.value?.entries.some((e) => e.kind === 'attachment' && e.attachment.id === id)) { return cards().some((card) => card.kind === 'attachment' && card.attachment.id === id)
return true
}
return items.value.some((item) => item.kind === 'attachment' && item.attachment.id === id)
} }
/** Reads one page of older turns (whole turns, oldest first) and /** Reads one page of older turns (whole turns, oldest first) and