open-webui/src/lib/components/common/FileItemModal.svelte

266 lines
7 KiB
Svelte
Raw Normal View History

2024-09-28 08:53:25 +00:00
<script lang="ts">
2025-08-06 15:50:51 +00:00
import { getContext, onMount, tick } from 'svelte';
2024-09-28 08:53:25 +00:00
import { formatFileSize, getLineCount } from '$lib/utils';
2025-02-20 07:44:11 +00:00
import { WEBUI_API_BASE_URL } from '$lib/constants';
2025-08-06 15:50:51 +00:00
import { getKnowledgeById } from '$lib/apis/knowledge';
2024-09-28 08:53:25 +00:00
const i18n = getContext('i18n');
import Modal from './Modal.svelte';
import XMark from '../icons/XMark.svelte';
import Info from '../icons/Info.svelte';
2024-09-29 20:52:27 +00:00
import Switch from './Switch.svelte';
import Tooltip from './Tooltip.svelte';
import dayjs from 'dayjs';
2025-08-06 15:50:51 +00:00
import Spinner from './Spinner.svelte';
2025-08-25 14:22:28 +00:00
import { getFileById } from '$lib/apis/files';
2024-09-28 08:53:25 +00:00
2024-10-04 05:22:22 +00:00
export let item;
2024-09-28 08:53:25 +00:00
export let show = false;
2024-09-29 20:52:27 +00:00
export let edit = false;
let enableFullContent = false;
2025-05-04 13:29:59 +00:00
let isPdf = false;
let isAudio = false;
2025-08-06 15:50:51 +00:00
let loading = false;
2025-05-04 13:29:59 +00:00
2025-08-27 22:59:45 +00:00
let selectedTab = '';
2025-02-20 09:01:29 +00:00
$: isPDF =
item?.meta?.content_type === 'application/pdf' ||
(item?.name && item?.name.toLowerCase().endsWith('.pdf'));
2024-09-29 20:52:27 +00:00
2025-05-04 13:29:59 +00:00
$: isAudio =
2025-05-20 15:29:31 +00:00
(item?.meta?.content_type ?? '').startsWith('audio/') ||
2025-05-04 13:29:59 +00:00
(item?.name && item?.name.toLowerCase().endsWith('.mp3')) ||
(item?.name && item?.name.toLowerCase().endsWith('.wav')) ||
(item?.name && item?.name.toLowerCase().endsWith('.ogg')) ||
2025-05-04 14:17:35 +00:00
(item?.name && item?.name.toLowerCase().endsWith('.m4a')) ||
(item?.name && item?.name.toLowerCase().endsWith('.webm'));
2025-05-04 13:29:59 +00:00
2025-08-06 15:50:51 +00:00
const loadContent = async () => {
if (item?.type === 'collection') {
loading = true;
const knowledge = await getKnowledgeById(localStorage.token, item.id).catch((e) => {
console.error('Error fetching knowledge base:', e);
return null;
});
if (knowledge) {
item.files = knowledge.files || [];
}
loading = false;
2025-08-25 14:22:28 +00:00
} else if (item?.type === 'file') {
loading = true;
const file = await getFileById(localStorage.token, item.id).catch((e) => {
console.error('Error fetching file:', e);
return null;
});
if (file) {
item.file = file || {};
}
loading = false;
2025-08-06 15:50:51 +00:00
}
await tick();
};
$: if (show) {
loadContent();
}
2024-09-28 08:53:25 +00:00
onMount(() => {
2025-02-20 08:10:29 +00:00
console.log(item);
2024-10-04 05:22:22 +00:00
if (item?.context === 'full') {
2024-09-29 20:52:27 +00:00
enableFullContent = true;
}
2024-09-28 08:53:25 +00:00
});
</script>
2024-11-18 22:25:24 +00:00
<Modal bind:show size="lg">
2025-09-12 21:58:09 +00:00
<div class="font-primary px-4.5 py-3.5 w-full flex flex-col justify-center dark:text-gray-400">
2024-09-29 20:52:27 +00:00
<div class=" pb-2">
<div class="flex items-start justify-between">
<div>
<div class=" font-medium text-lg dark:text-gray-100">
<a
2025-02-20 07:44:11 +00:00
href="#"
2024-09-29 20:52:27 +00:00
class="hover:underline line-clamp-1"
2025-02-20 07:44:11 +00:00
on:click|preventDefault={() => {
if (!isPDF && item.url) {
2025-02-20 09:01:29 +00:00
window.open(
item.type === 'file' ? `${item.url}/content` : `${item.url}`,
'_blank'
);
2025-02-20 07:44:11 +00:00
}
}}
2024-09-29 20:52:27 +00:00
>
2024-10-04 05:22:22 +00:00
{item?.name ?? 'File'}
2024-09-29 20:52:27 +00:00
</a>
</div>
2024-09-28 08:53:25 +00:00
</div>
<div>
2024-09-29 20:52:27 +00:00
<button
on:click={() => {
show = false;
}}
>
<XMark />
</button>
</div>
</div>
<div>
<div class="flex flex-col items-center md:flex-row gap-1 justify-between w-full">
2025-08-27 22:59:45 +00:00
<div class=" flex flex-wrap text-xs gap-1 text-gray-500">
{#if item?.type === 'collection'}
{#if item?.type}
<div class="capitalize shrink-0">{item.type}</div>
{/if}
{#if item?.description}
<div class="line-clamp-1">{item.description}</div>
{/if}
{#if item?.created_at}
<div class="capitalize shrink-0">
{dayjs(item.created_at * 1000).format('LL')}
</div>
{/if}
{/if}
2024-10-04 05:22:22 +00:00
{#if item.size}
<div class="capitalize shrink-0">{formatFileSize(item.size)}</div>
2024-09-28 08:53:25 +00:00
{/if}
2024-10-04 05:22:22 +00:00
{#if item?.file?.data?.content}
2024-09-29 21:08:55 +00:00
<div class="capitalize shrink-0">
2025-08-27 23:01:53 +00:00
{$i18n.t('{{COUNT}} extracted lines', {
COUNT: getLineCount(item?.file?.data?.content ?? '')
})}
2024-09-29 21:08:55 +00:00
</div>
2024-09-28 08:53:25 +00:00
2024-09-29 20:52:27 +00:00
<div class="flex items-center gap-1 shrink-0">
2025-08-27 23:01:53 +00:00
{$i18n.t('Formatting may be inconsistent from source.')}
2024-09-28 08:53:25 +00:00
</div>
{/if}
2025-07-14 13:47:21 +00:00
{#if item?.knowledge}
<div class="capitalize shrink-0">
{$i18n.t('Knowledge Base')}
</div>
{/if}
2024-09-28 08:53:25 +00:00
</div>
2024-09-29 20:52:27 +00:00
{#if edit}
<div>
<Tooltip
content={enableFullContent
2025-03-07 11:59:09 +00:00
? $i18n.t(
'Inject the entire content as context for comprehensive processing, this is recommended for complex queries.'
)
: $i18n.t(
'Default to segmented retrieval for focused and relevant content extraction, this is recommended for most cases.'
)}
2024-09-29 20:52:27 +00:00
>
<div class="flex items-center gap-1.5 text-xs">
{#if enableFullContent}
2025-07-17 12:25:26 +00:00
{$i18n.t('Using Entire Document')}
2024-09-29 20:52:27 +00:00
{:else}
2025-07-17 12:25:26 +00:00
{$i18n.t('Using Focused Retrieval')}
2024-09-29 20:52:27 +00:00
{/if}
<Switch
bind:state={enableFullContent}
on:change={(e) => {
2024-10-04 05:22:22 +00:00
item.context = e.detail ? 'full' : undefined;
2024-09-29 20:52:27 +00:00
}}
/>
</div>
</Tooltip>
</div>
{/if}
</div>
2024-09-28 08:53:25 +00:00
</div>
</div>
2025-02-20 07:44:11 +00:00
<div class="max-h-[75vh] overflow-auto">
2025-08-06 15:50:51 +00:00
{#if !loading}
{#if item?.type === 'collection'}
<div>
{#each item?.files as file}
<div class="flex items-center gap-2 mb-2">
<div class="flex-shrink-0 text-xs">
{file?.meta?.name}
</div>
</div>
2025-08-06 15:50:51 +00:00
{/each}
</div>
{:else if isPDF}
2025-08-27 22:59:45 +00:00
<div
class="flex mb-2.5 scrollbar-none overflow-x-auto w-full border-b border-gray-100 dark:border-gray-800 text-center text-sm font-medium bg-transparent dark:text-gray-200"
>
<button
class="min-w-fit py-1.5 px-4 border-b {selectedTab === ''
? ' '
: ' border-transparent text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'} transition"
type="button"
on:click={() => {
selectedTab = '';
}}>{$i18n.t('Content')}</button
>
<button
class="min-w-fit py-1.5 px-4 border-b {selectedTab === 'preview'
? ' '
: ' border-transparent text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'} transition"
type="button"
on:click={() => {
selectedTab = 'preview';
}}>{$i18n.t('Preview')}</button
>
</div>
{#if selectedTab === 'preview'}
<iframe
title={item?.name}
src={`${WEBUI_API_BASE_URL}/files/${item.id}/content`}
class="w-full h-[70vh] border-0 rounded-lg"
/>
{:else}
<div class="max-h-96 overflow-scroll scrollbar-hidden text-xs whitespace-pre-wrap">
{item?.file?.data?.content ?? 'No content'}
</div>
{/if}
2025-08-06 15:50:51 +00:00
{:else}
{#if isAudio}
<audio
src={`${WEBUI_API_BASE_URL}/files/${item.id}/content`}
class="w-full border-0 rounded-lg mb-2"
controls
playsinline
/>
{/if}
2025-05-04 13:29:59 +00:00
2025-08-06 15:50:51 +00:00
{#if item?.file?.data}
<div class="max-h-96 overflow-scroll scrollbar-hidden text-xs whitespace-pre-wrap">
{item?.file?.data?.content ?? 'No content'}
</div>
{/if}
2025-07-14 13:47:21 +00:00
{/if}
2025-08-06 15:50:51 +00:00
{:else}
<div class="flex items-center justify-center py-6">
<Spinner className="size-5" />
</div>
2025-02-20 07:44:11 +00:00
{/if}
2024-09-28 08:53:25 +00:00
</div>
</div>
</Modal>