2 import type { ChatAttachmentDisplayItem } from '$lib/types';
3 import { FileText, Eye, Info } from '@lucide/svelte';
4 import { Button } from '$lib/components/ui/button';
5 import * as Alert from '$lib/components/ui/alert';
6 import { SyntaxHighlightedCode } from '$lib/components/app';
7 import { getLanguageFromFilename } from '$lib/utils';
8 import { convertPDFToImage } from '$lib/utils/browser-only';
9 import { PdfViewMode } from '$lib/enums';
12 currentItem: ChatAttachmentDisplayItem | null;
14 displayTextContent: string | undefined;
15 hasVisionModality: boolean;
16 activeModelId?: string;
19 let { currentItem, displayName, displayTextContent, hasVisionModality, activeModelId }: Props =
22 let pdfViewMode = $state<PdfViewMode>(PdfViewMode.PAGES);
23 let pdfImages = $state<string[]>([]);
24 let pdfImagesLoading = $state(false);
25 let pdfImagesError = $state<string | null>(null);
27 let language = $derived(getLanguageFromFilename(displayName));
29 async function loadPdfImages() {
30 if (pdfImages.length > 0 || pdfImagesLoading || !currentItem) return;
32 pdfImagesLoading = true;
33 pdfImagesError = null;
36 let file: File | null = null;
38 if (currentItem.uploadedFile?.file) {
39 file = currentItem.uploadedFile.file;
40 } else if (currentItem.attachment) {
41 // Check if we have pre-processed images
43 'images' in currentItem.attachment &&
44 currentItem.attachment.images &&
45 Array.isArray(currentItem.attachment.images) &&
46 currentItem.attachment.images.length > 0
48 pdfImages = currentItem.attachment.images;
52 // Convert base64 back to File for processing
53 if ('base64Data' in currentItem.attachment && currentItem.attachment.base64Data) {
54 const base64Data = currentItem.attachment.base64Data;
55 const byteCharacters = atob(base64Data);
56 const byteNumbers = new Array(byteCharacters.length);
57 for (let i = 0; i < byteCharacters.length; i++) {
58 byteNumbers[i] = byteCharacters.charCodeAt(i);
60 const byteArray = new Uint8Array(byteNumbers);
61 file = new File([byteArray], displayName, { type: 'application/pdf' });
66 pdfImages = await convertPDFToImage(file);
68 throw new Error('No PDF file available for conversion');
71 pdfImagesError = error instanceof Error ? error.message : 'Failed to load PDF images';
73 pdfImagesLoading = false;
78 if (pdfViewMode === PdfViewMode.PAGES) {
84 <div class="mb-4 flex items-center justify-end gap-2">
86 variant={pdfViewMode === PdfViewMode.TEXT ? 'default' : 'outline'}
88 onclick={() => (pdfViewMode = PdfViewMode.TEXT)}
89 disabled={pdfImagesLoading}
91 <FileText class="mr-1 h-4 w-4" />
96 variant={pdfViewMode === PdfViewMode.PAGES ? 'default' : 'outline'}
98 onclick={() => (pdfViewMode = PdfViewMode.PAGES)}
99 disabled={pdfImagesLoading}
101 {#if pdfImagesLoading}
103 class="mr-1 h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent"
106 <Eye class="mr-1 h-4 w-4" />
112 {#if !hasVisionModality && activeModelId && currentItem}
113 <Alert.Root class="mb-4 max-w-4xl">
114 <Info class="h-4 w-4" />
115 <Alert.Title>Preview only</Alert.Title>
117 <span class="inline-flex">
118 The selected model does not support vision. Only the extracted
119 <!-- svelte-ignore a11y_click_events_have_key_events -->
120 <!-- svelte-ignore a11y_no_static_element_interactions -->
122 class="mx-1 cursor-pointer underline"
123 onclick={() => (pdfViewMode = PdfViewMode.TEXT)}
127 will be sent to the model.
133 {#if pdfImagesLoading}
134 <div class="flex flex-1 items-center justify-center p-8">
135 <div class="text-center">
137 class="mx-auto mb-4 h-8 w-8 animate-spin rounded-full border-4 border-white border-t-transparent"
139 <p class="text-white/70">Converting PDF to images...</p>
142 {:else if pdfImagesError}
143 <div class="flex flex-1 items-center justify-center p-8">
144 <div class="text-center">
145 <FileText class="mx-auto mb-4 h-16 w-16 text-white/50" />
146 <p class="mb-4 text-white/70">Failed to load PDF images</p>
147 <p class="text-sm text-white/50">{pdfImagesError}</p>
150 {:else if pdfImages.length > 0}
151 {#each pdfImages as image, index (image)}
152 <p class="mb-2 text-sm text-white/50">Page {index + 1}</p>
153 <img src={image} alt="PDF Page {index + 1}" class="mx-auto max-w-[85vw] rounded-lg shadow-lg" />
154 <div class="h-4"></div>
157 <div class="flex flex-1 items-center justify-center p-8">
158 <div class="text-center">
159 <FileText class="mx-auto mb-4 h-16 w-16 text-white/50" />
160 <p class="text-white/70">No PDF pages available</p>
165 {#if pdfViewMode === PdfViewMode.TEXT && displayTextContent}
166 <div class="px-4 pb-4">
167 <SyntaxHighlightedCode
169 code={displayTextContent}