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(); } });