refac: knowledge item modal

This commit is contained in:
Timothy Jaeryang Baek 2025-08-06 19:50:51 +04:00
parent 8e25eb104b
commit b9885529a5
2 changed files with 61 additions and 30 deletions

View file

@ -1,7 +1,8 @@
<script lang="ts"> <script lang="ts">
import { getContext, onMount } from 'svelte'; import { getContext, onMount, tick } from 'svelte';
import { formatFileSize, getLineCount } from '$lib/utils'; import { formatFileSize, getLineCount } from '$lib/utils';
import { WEBUI_API_BASE_URL } from '$lib/constants'; import { WEBUI_API_BASE_URL } from '$lib/constants';
import { getKnowledgeById } from '$lib/apis/knowledge';
const i18n = getContext('i18n'); const i18n = getContext('i18n');
@ -11,6 +12,7 @@
import Switch from './Switch.svelte'; import Switch from './Switch.svelte';
import Tooltip from './Tooltip.svelte'; import Tooltip from './Tooltip.svelte';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import Spinner from './Spinner.svelte';
export let item; export let item;
export let show = false; export let show = false;
@ -20,6 +22,7 @@
let isPdf = false; let isPdf = false;
let isAudio = false; let isAudio = false;
let loading = false;
$: isPDF = $: isPDF =
item?.meta?.content_type === 'application/pdf' || item?.meta?.content_type === 'application/pdf' ||
@ -33,6 +36,28 @@
(item?.name && item?.name.toLowerCase().endsWith('.m4a')) || (item?.name && item?.name.toLowerCase().endsWith('.m4a')) ||
(item?.name && item?.name.toLowerCase().endsWith('.webm')); (item?.name && item?.name.toLowerCase().endsWith('.webm'));
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;
}
await tick();
};
$: if (show) {
loadContent();
}
onMount(() => { onMount(() => {
console.log(item); console.log(item);
if (item?.context === 'full') { if (item?.context === 'full') {
@ -152,37 +177,43 @@
</div> </div>
<div class="max-h-[75vh] overflow-auto"> <div class="max-h-[75vh] overflow-auto">
{#if item?.type === 'collection'} {#if !loading}
<div> {#if item?.type === 'collection'}
{#each item?.files as file} <div>
<div class="flex items-center gap-2 mb-2"> {#each item?.files as file}
<div class="flex-shrink-0 text-xs"> <div class="flex items-center gap-2 mb-2">
{file?.meta?.name} <div class="flex-shrink-0 text-xs">
{file?.meta?.name}
</div>
</div> </div>
</div> {/each}
{/each}
</div>
{:else if isPDF}
<iframe
title={item?.name}
src={`${WEBUI_API_BASE_URL}/files/${item.id}/content`}
class="w-full h-[70vh] border-0 rounded-lg mt-4"
/>
{: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}
{#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> </div>
{:else if isPDF}
<iframe
title={item?.name}
src={`${WEBUI_API_BASE_URL}/files/${item.id}/content`}
class="w-full h-[70vh] border-0 rounded-lg mt-4"
/>
{: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}
{#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}
{/if} {/if}
{:else}
<div class="flex items-center justify-center py-6">
<Spinner className="size-5" />
</div>
{/if} {/if}
</div> </div>
</div> </div>

View file

@ -181,7 +181,7 @@
loading={file.status === 'uploading'} loading={file.status === 'uploading'}
type={file?.legacy type={file?.legacy
? `Legacy${file.type ? ` ${file.type}` : ''}` ? `Legacy${file.type ? ` ${file.type}` : ''}`
: (file?.type ?? 'Collection')} : (file?.type ?? 'collection')}
dismissible dismissible
on:dismiss={(e) => { on:dismiss={(e) => {
selectedItems = selectedItems.filter((_, idx) => idx !== fileIdx); selectedItems = selectedItems.filter((_, idx) => idx !== fileIdx);