| <script lang="ts"> |
| import { toast } from 'svelte-sonner'; |
| import { getContext, tick } from 'svelte'; |
| |
| import fileSaver from 'file-saver'; |
| const { saveAs } = fileSaver; |
| |
| import { downloadChatAsPDF } from '$lib/apis/utils'; |
| import { copyToClipboard, createMessagesList } from '$lib/utils'; |
| |
| import { |
| showControls, |
| showArtifacts, |
| mobile, |
| temporaryChatEnabled, |
| theme, |
| user, |
| settings, |
| folders, |
| showEmbeds, |
| artifactContents |
| } from '$lib/stores'; |
| |
| import { getChatById } from '$lib/apis/chats'; |
| |
| import Dropdown from '$lib/components/common/Dropdown.svelte'; |
| import DropdownSub from '$lib/components/common/DropdownSub.svelte'; |
| import Tags from '$lib/components/chat/Tags.svelte'; |
| import Clipboard from '$lib/components/icons/Clipboard.svelte'; |
| import AdjustmentsHorizontal from '$lib/components/icons/AdjustmentsHorizontal.svelte'; |
| import Cube from '$lib/components/icons/Cube.svelte'; |
| import Folder from '$lib/components/icons/Folder.svelte'; |
| import Share from '$lib/components/icons/Share.svelte'; |
| import ArchiveBox from '$lib/components/icons/ArchiveBox.svelte'; |
| import GarbageBin from '$lib/components/icons/GarbageBin.svelte'; |
| import Messages from '$lib/components/chat/Messages.svelte'; |
| import Download from '$lib/components/icons/Download.svelte'; |
| |
| const i18n = getContext('i18n'); |
| |
| export let shareEnabled: boolean = false; |
| |
| export let shareHandler: Function; |
| export let moveChatHandler: Function; |
| |
| export let archiveChatHandler: Function; |
| export let deleteChatHandler: Function; |
| |
| |
| |
| export let chat; |
| export let onClose: Function = () => {}; |
| export let scrollToTop: (() => void) | null = null; |
| |
| let showFullMessages = false; |
| |
| const getChatAsText = async () => { |
| const history = chat.chat.history; |
| const messages = createMessagesList(history, history.currentId); |
| const chatText = messages.reduce((a, message, i, arr) => { |
| return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`; |
| }, ''); |
| |
| return chatText.trim(); |
| }; |
| |
| const downloadTxt = async () => { |
| const chatText = await getChatAsText(); |
| |
| let blob = new Blob([chatText], { |
| type: 'text/plain' |
| }); |
| |
| saveAs(blob, `chat-${chat.chat.title}.txt`); |
| }; |
| |
| const downloadPdf = async () => { |
| const [{ default: jsPDF }, { default: html2canvas }] = await Promise.all([ |
| import('jspdf'), |
| import('html2canvas-pro') |
| ]); |
| |
| if ($settings?.stylizedPdfExport ?? true) { |
| showFullMessages = true; |
| await tick(); |
| |
| const containerElement = document.getElementById('full-messages-container'); |
| if (containerElement) { |
| try { |
| const isDarkMode = document.documentElement.classList.contains('dark'); |
| const virtualWidth = 800; // px, fixed width for cloned element |
| |
| // Clone and style |
| const clonedElement = containerElement.cloneNode(true); |
| clonedElement.classList.add('text-black'); |
| clonedElement.classList.add('dark:text-white'); |
| clonedElement.style.width = `${virtualWidth}px`; |
| clonedElement.style.position = 'absolute'; |
| clonedElement.style.left = '-9999px'; |
| clonedElement.style.height = 'auto'; |
| document.body.appendChild(clonedElement); |
| |
| // Override content-visibility so html2canvas can capture all messages |
| clonedElement.querySelectorAll('.message-listitem').forEach((el) => { |
| el.style.contentVisibility = 'visible'; |
| }); |
| |
| |
| await new Promise((r) => requestAnimationFrame(r)); |
| |
| |
| const canvas = await html2canvas(clonedElement, { |
| backgroundColor: isDarkMode ? '#000' : '#fff', |
| useCORS: true, |
| scale: 2, // increase resolution |
| width: virtualWidth |
| }); |
| |
| document.body.removeChild(clonedElement); |
| |
| const pdf = new jsPDF('p', 'mm', 'a4'); |
| const pageWidthMM = 210; |
| const pageHeightMM = 297; |
| |
| |
| |
| const pxPerMM = canvas.width / virtualWidth; |
| |
| |
| |
| |
| const pxPerPDFMM = canvas.width / pageWidthMM; |
| |
| |
| const pagePixelHeight = Math.floor(pxPerPDFMM * pageHeightMM); |
| |
| let offsetY = 0; |
| let page = 0; |
| |
| while (offsetY < canvas.height) { |
| // Height of slice |
| const sliceHeight = Math.min(pagePixelHeight, canvas.height - offsetY); |
| |
| // Create temp canvas for slice |
| const pageCanvas = document.createElement('canvas'); |
| pageCanvas.width = canvas.width; |
| pageCanvas.height = sliceHeight; |
| |
| const ctx = pageCanvas.getContext('2d'); |
| |
| // Draw the slice of original canvas onto pageCanvas |
| ctx.drawImage( |
| canvas, |
| 0, |
| offsetY, |
| canvas.width, |
| sliceHeight, |
| 0, |
| 0, |
| canvas.width, |
| sliceHeight |
| ); |
| |
| const imgData = pageCanvas.toDataURL('image/jpeg', 0.7); |
| |
| // Calculate image height in PDF units keeping aspect ratio |
| const imgHeightMM = (sliceHeight * pageWidthMM) / canvas.width; |
| |
| if (page > 0) pdf.addPage(); |
| |
| if (isDarkMode) { |
| pdf.setFillColor(0, 0, 0); |
| pdf.rect(0, 0, pageWidthMM, pageHeightMM, 'F'); // black bg |
| } |
| |
| pdf.addImage(imgData, 'JPEG', 0, 0, pageWidthMM, imgHeightMM); |
| |
| offsetY += sliceHeight; |
| page++; |
| } |
| |
| pdf.save(`chat-${chat.chat.title}.pdf`); |
| |
| showFullMessages = false; |
| } catch (error) { |
| console.error('Error generating PDF', error); |
| } |
| } |
| } else { |
| console.log('Downloading PDF'); |
| |
| const chatText = await getChatAsText(); |
| |
| const doc = new jsPDF(); |
| |
| // Margins |
| const left = 15; |
| const top = 20; |
| const right = 15; |
| const bottom = 20; |
| |
| const pageWidth = doc.internal.pageSize.getWidth(); |
| const pageHeight = doc.internal.pageSize.getHeight(); |
| const usableWidth = pageWidth - left - right; |
| const usableHeight = pageHeight - top - bottom; |
| |
| // Font size and line height |
| const fontSize = 8; |
| doc.setFontSize(fontSize); |
| const lineHeight = fontSize * 1; // adjust if needed |
| |
| // Split the markdown into lines (handles \n) |
| const paragraphs = chatText.split('\n'); |
| |
| let y = top; |
| |
| for (let paragraph of paragraphs) { |
| // Wrap each paragraph to fit the width |
| const lines = doc.splitTextToSize(paragraph, usableWidth); |
| |
| for (let line of lines) { |
| // If the line would overflow the bottom, add a new page |
| if (y + lineHeight > pageHeight - bottom) { |
| doc.addPage(); |
| y = top; |
| } |
| doc.text(line, left, y); |
| y += lineHeight * 0.5; |
| } |
| |
| y += lineHeight * 0.1; |
| } |
| |
| doc.save(`chat-${chat.chat.title}.pdf`); |
| } |
| }; |
| |
| const downloadJSONExport = async () => { |
| if (chat.id) { |
| let chatObj = null; |
| |
| if ((chat?.id ?? '').startsWith('local') || $temporaryChatEnabled) { |
| chatObj = chat; |
| } else { |
| chatObj = await getChatById(localStorage.token, chat.id); |
| } |
| |
| let blob = new Blob([JSON.stringify([chatObj])], { |
| type: 'application/json' |
| }); |
| saveAs(blob, `chat-export-${Date.now()}.json`); |
| } |
| }; |
| </script> |
|
|
| {#if showFullMessages} |
| <div class="hidden w-full h-full flex-col"> |
| <div id="full-messages-container"> |
| <Messages |
| className="h-full flex pt-4 pb-8 w-full" |
| chatId={`chat-preview-${chat?.id ?? ''}`} |
| user={$user} |
| readOnly={true} |
| history={chat.chat.history} |
| messages={chat.chat.messages} |
| autoScroll={true} |
| sendMessage={() => {}} |
| continueResponse={() => {}} |
| regenerateResponse={() => {}} |
| messagesCount={null} |
| editCodeBlock={false} |
| /> |
| </div> |
| </div> |
| {/if} |
|
|
| <Dropdown |
| onOpenChange={(state) => { |
| if (state === false) { |
| onClose(); |
| } |
| }} |
| align="end" |
| sideOffset={8} |
| > |
| <slot /> |
|
|
| <div slot="content"> |
| <div |
| class="select-none min-w-[200px] max-w-[200px] rounded-2xl px-1 py-1 border border-gray-100 dark:border-gray-800 z-50 bg-white dark:bg-gray-850 dark:text-white shadow-lg transition" |
| > |
| |
| |
| {async () => { |
| await showSettings.set(!$showSettings); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| {$i18n.t('Settings')} |
| </DropdownMenu.Item> --> |
| <!-- Settings commented out block above --> |
|
|
| {#if scrollToTop} |
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| on:click={() => { |
| scrollToTop(); |
| }} |
| > |
| <svg |
| xmlns="http://www.w3.org/2000/svg" |
| fill="none" |
| viewBox="0 0 24 24" |
| stroke-width="1.5" |
| stroke="currentColor" |
| class="size-4" |
| > |
| <path |
| stroke-linecap="round" |
| stroke-linejoin="round" |
| d="M4.5 10.5 12 3m0 0 7.5 7.5M12 3v18" |
| /> |
| </svg> |
| {$i18n.t('Scroll to Top')} |
| </button> |
|
|
| <hr class="border-gray-50/30 dark:border-gray-800/30 my-1" /> |
| {/if} |
|
|
| {#if ($artifactContents ?? []).length > 0} |
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| id="chat-artifacts-button" |
| on:click={async () => { |
| await showControls.set(true); |
| await showArtifacts.set(true); |
| await showEmbeds.set(false); |
| }} |
| > |
| <Cube className=" size-4" strokeWidth="1.5" /> |
| {$i18n.t('Artifacts')} |
| </button> |
|
|
| <hr class="border-gray-50/30 dark:border-gray-800/30 my-1" /> |
| {/if} |
|
|
| {#if !$temporaryChatEnabled && ($user?.role === 'admin' || ($user.permissions?.chat?.share ?? true))} |
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| id="chat-share-button" |
| on:click={() => { |
| shareHandler(); |
| }} |
| > |
| <Share strokeWidth="1.5" /> |
| {$i18n.t('Share')} |
| </button> |
| {/if} |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| {$i18n.t('Download')} |
| |
| {#if $user?.role === 'admin' || ($user.permissions?.chat?.export ?? true)} |
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| on:click={() => { |
| downloadJSONExport(); |
| }} |
| > |
| {$i18n.t('Export chat (.json)')} |
| </button> |
| {/if} |
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| on:click={() => { |
| downloadTxt(); |
| }} |
| > |
| {$i18n.t('Plain text (.txt)')} |
| </button> |
|
|
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| on:click={() => { |
| downloadPdf(); |
| }} |
| > |
| {$i18n.t('PDF document (.pdf)')} |
| </button> |
| </DropdownSub> |
|
|
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| id="chat-copy-button" |
| on:click={async () => { |
| const res = await copyToClipboard(await getChatAsText()).catch((e) => { |
| console.error(e); |
| }); |
|
|
| if (res) { |
| toast.success($i18n.t('Copied to clipboard')); |
| } |
| }} |
| > |
| <Clipboard className=" size-4" strokeWidth="1.5" /> |
| {$i18n.t('Copy')} |
| </button> |
|
|
| {#if !$temporaryChatEnabled && chat?.id} |
| <hr class="border-gray-50/30 dark:border-gray-800/30 my-1" /> |
|
|
| {#if $folders.length > 0} |
| <DropdownSub maxWidth={200}> |
| <button |
| slot="trigger" |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| > |
| <Folder strokeWidth="1.5" /> |
|
|
| {$i18n.t('Move')} |
| </button> |
| {#each $folders.sort((a, b) => b.updated_at - a.updated_at) as folder} |
| {#if folder?.id} |
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl overflow-hidden w-full" |
| on:click={() => { |
| moveChatHandler(chat.id, folder.id); |
| }} |
| > |
| |
| |
| |
|
|
| {folder.name ?? 'Folder'} |
| </button> |
| {/if} |
| {/each} |
| </DropdownSub> |
| {/if} |
|
|
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| on:click={() => { |
| archiveChatHandler(); |
| }} |
| > |
| <ArchiveBox className="size-4" strokeWidth="1.5" /> |
| {$i18n.t('Archive')} |
| </button> |
|
|
| <button |
| draggable="false" |
| class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl select-none w-full" |
| on:click={() => { |
| deleteChatHandler(); |
| }} |
| > |
| <GarbageBin strokeWidth="1.5" /> |
| {$i18n.t('Delete')} |
| </button> |
|
|
| <hr class="border-gray-50/30 dark:border-gray-800/30 my-1" /> |
|
|
| |
| {chat.id} |
| |
| {/if} |
| </div> |
| </div> |
| </Dropdown> |
|
|