From efbf6e80a5c121584736b5face7cfced9da707dd Mon Sep 17 00:00:00 2001 From: Aleksander Grygier Date: Wed, 4 Nov 2026 14:46:51 +0100 Subject: [PATCH] ui: show text and page thumbnails for PDF attachments, stop endless conversion retries (#31468) * fix(ui): show extracted text in PDF attachment preview Assisted-by: Claude (Opus 5.5 Medium) * feat(ui): show page thumbnails for PDF attachments sent as images Assisted-by: Claude (Opus 5.5 Medium) * ui: stop retrying PDF preview conversion after it fails A failed PDF-to-image conversion or text extraction reset its loading flag, which re-ran the effect and retried forever. Remember the failed item for each view and only retry when the item changes, so the error state stays on screen. Add a PdfConversionError story that feeds a broken PDF. Assisted-by: Claude (Opus 5.5 Medium) * ui: keep the text of PDFs sent as images and tidy the PDF preview Store the extracted text of a PDF sent as images in its content, so the Text view reads it instead of parsing the PDF again; only messages stored with the old placeholder extract it. The model still receives the images only. Guard retries on the error state, as the item remounts per id, skip an empty base64 payload, and share the status markup between views. Assisted-by: Claude (Opus 5.5 Medium) --- .../ChatAttachmentsListItem.svelte | 18 +- ...atAttachmentsListItemThumbnailImage.svelte | 10 + .../ChatAttachmentsPreview.svelte | 2 + ...hatAttachmentsPreviewCurrentItemPdf.svelte | 203 +++++++++++------- ...hatAttachmentsPreviewThumbnailStrip.svelte | 3 +- .../supported-file-types.constants.ts | 3 + tools/ui/src/lib/utils/attachment-display.ts | 11 +- .../src/lib/utils/convert-files-to-extra.ts | 4 +- .../ChatAttachmentsPreview.stories.svelte | 47 ++++ 9 files changed, 218 insertions(+), 83 deletions(-) create mode 100644 tools/ui/tests/stories/chat/screen/attachments/ChatAttachmentsPreview.stories.svelte diff --git a/tools/ui/src/lib/components/app/chat/ChatAttachments/ChatAttachmentsList/ChatAttachmentsListItem/ChatAttachmentsListItem.svelte b/tools/ui/src/lib/components/app/chat/ChatAttachments/ChatAttachmentsList/ChatAttachmentsListItem/ChatAttachmentsListItem.svelte index 754856d60..b62cbc2db 100644 --- a/tools/ui/src/lib/components/app/chat/ChatAttachments/ChatAttachmentsList/ChatAttachmentsListItem/ChatAttachmentsListItem.svelte +++ b/tools/ui/src/lib/components/app/chat/ChatAttachments/ChatAttachmentsList/ChatAttachmentsListItem/ChatAttachmentsListItem.svelte @@ -106,18 +106,22 @@ {readonly} width={imageWidth} /> -{:else if isPdfFile(item.attachment, item.uploadedFile)} - 0 ? `PDF, ${pageCount} ${pageCount === 1 ? 'page' : 'pages'}` : 'PDF'} name={item.name} onRemove={onFileRemove} onclick={() => onPreview?.(item)} + preview={item.preview} {readonly} - size={item.size} - textContent={item.textContent} - uploadedFile={item.uploadedFile} + width={imageWidth} /> {:else} void; onRemove?: (id: string) => void; name: string; @@ -20,6 +21,7 @@ height = 'h-16', id, imageClass = '', + label, name, onclick, onRemove, @@ -49,6 +51,14 @@ {@render image()} {/if} + {#if label} + + {label} + + {/if} + {#if !readonly}
(PdfViewMode.PAGES); let pdfImages = $state([]); let pdfImagesLoading = $state(false); + // a failure stays on screen: the effect does not retry while an error is set let pdfImagesError = $state(null); + let pdfText = $state(null); + let pdfTextLoading = $state(false); + let pdfTextError = $state(null); let language = $derived(getLanguageFromFilename(displayName)); + // older PDFs sent as images stored a placeholder in `content`, so their text is extracted again + let hasStoredText = $derived( + currentItem?.attachment?.type === AttachmentType.PDF + ? !( + currentItem.attachment.processedAsImages && + PDF_AS_IMAGES_LEGACY_CONTENT_REGEX.test(currentItem.attachment.content) + ) + : !!displayTextContent + ); + + let isLoading = $derived(pdfImagesLoading || pdfTextLoading); + + function getPdfFile(): File | null { + if (currentItem?.uploadedFile?.file) { + return currentItem.uploadedFile.file; + } + + if ( + currentItem?.attachment && + 'base64Data' in currentItem.attachment && + currentItem.attachment.base64Data + ) { + const byteArray = Uint8Array.from(atob(currentItem.attachment.base64Data), (char) => + char.charCodeAt(0) + ); + + return new File([byteArray], displayName, { type: MimeTypeApplication.PDF }); + } + + return null; + } + async function loadPdfImages() { if (pdfImages.length > 0 || pdfImagesLoading || !currentItem) return; + if ( + currentItem.attachment?.type === AttachmentType.PDF && + currentItem.attachment.images?.length + ) { + pdfImages = currentItem.attachment.images; + + return; + } + + if (pdfImagesError) return; + pdfImagesLoading = true; - pdfImagesError = null; try { - let file: File | null = null; + const file = getPdfFile(); - if (currentItem.uploadedFile?.file) { - file = currentItem.uploadedFile.file; - } else if (currentItem.attachment) { - // Check if we have pre-processed images - if ( - 'images' in currentItem.attachment && - currentItem.attachment.images && - Array.isArray(currentItem.attachment.images) && - currentItem.attachment.images.length > 0 - ) { - pdfImages = currentItem.attachment.images; - - return; - } - - // Convert base64 back to File for processing - if ('base64Data' in currentItem.attachment && currentItem.attachment.base64Data) { - const base64Data = currentItem.attachment.base64Data; - const byteCharacters = atob(base64Data); - const byteNumbers = new Array(byteCharacters.length); - - for (let i = 0; i < byteCharacters.length; i++) { - byteNumbers[i] = byteCharacters.charCodeAt(i); - } - const byteArray = new Uint8Array(byteNumbers); - - file = new File([byteArray], displayName, { type: 'application/pdf' }); - } - } - - if (file) { - pdfImages = await convertPDFToImage(file); - } else { + if (!file) { throw new Error('No PDF file available for conversion'); } + + pdfImages = await convertPDFToImage(file); } catch (error) { pdfImagesError = error instanceof Error ? error.message : 'Failed to load PDF images'; } finally { @@ -78,26 +94,62 @@ } } + async function loadPdfText() { + if (pdfText !== null || pdfTextLoading || !currentItem) return; + + if (hasStoredText) { + pdfText = displayTextContent ?? ''; + + return; + } + + if (pdfTextError) return; + + pdfTextLoading = true; + + try { + const file = getPdfFile(); + + if (!file) { + throw new Error('No PDF file available for text extraction'); + } + + pdfText = await convertPDFToText(file); + } catch (error) { + pdfTextError = error instanceof Error ? error.message : 'Failed to extract PDF text'; + } finally { + pdfTextLoading = false; + } + } + $effect(() => { if (pdfViewMode === PdfViewMode.PAGES) { loadPdfImages(); + } else { + loadPdfText(); } });