2025-09-14 07:54:06 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { onMount, tick, getContext } from 'svelte';
|
|
|
|
|
|
|
|
|
|
import { decodeString } from '$lib/utils';
|
2025-09-14 08:09:52 +00:00
|
|
|
import { knowledge } from '$lib/stores';
|
|
|
|
|
|
|
|
|
|
import { getKnowledgeBases } from '$lib/apis/knowledge';
|
2025-09-14 07:54:06 +00:00
|
|
|
|
|
|
|
|
import Tooltip from '$lib/components/common/Tooltip.svelte';
|
|
|
|
|
import Database from '$lib/components/icons/Database.svelte';
|
|
|
|
|
import DocumentPage from '$lib/components/icons/DocumentPage.svelte';
|
2025-09-14 08:11:17 +00:00
|
|
|
import Spinner from '$lib/components/common/Spinner.svelte';
|
2025-09-14 07:54:06 +00:00
|
|
|
|
|
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
|
|
|
|
|
export let onSelect = (e) => {};
|
|
|
|
|
|
2025-09-14 08:11:17 +00:00
|
|
|
let loaded = false;
|
2025-09-14 07:54:06 +00:00
|
|
|
let items = [];
|
|
|
|
|
let selectedIdx = 0;
|
|
|
|
|
|
|
|
|
|
onMount(async () => {
|
2025-09-14 08:09:52 +00:00
|
|
|
if ($knowledge === null) {
|
|
|
|
|
await knowledge.set(await getKnowledgeBases(localStorage.token));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let collections = $knowledge
|
2025-09-14 07:54:06 +00:00
|
|
|
.filter((item) => !item?.meta?.document)
|
|
|
|
|
.map((item) => ({
|
|
|
|
|
...item,
|
|
|
|
|
type: 'collection'
|
|
|
|
|
}));
|
2025-09-14 08:09:52 +00:00
|
|
|
``;
|
2025-09-14 07:54:06 +00:00
|
|
|
let collection_files =
|
2025-09-14 08:09:52 +00:00
|
|
|
$knowledge.length > 0
|
2025-09-14 07:54:06 +00:00
|
|
|
? [
|
2025-09-14 08:09:52 +00:00
|
|
|
...$knowledge
|
2025-09-14 07:54:06 +00:00
|
|
|
.reduce((a, item) => {
|
|
|
|
|
return [
|
|
|
|
|
...new Set([
|
|
|
|
|
...a,
|
|
|
|
|
...(item?.files ?? []).map((file) => ({
|
|
|
|
|
...file,
|
|
|
|
|
collection: { name: item.name, description: item.description } // DO NOT REMOVE, USED IN FILE DESCRIPTION/ATTACHMENT
|
|
|
|
|
}))
|
|
|
|
|
])
|
|
|
|
|
];
|
|
|
|
|
}, [])
|
|
|
|
|
.map((file) => ({
|
|
|
|
|
...file,
|
|
|
|
|
name: file?.meta?.name,
|
|
|
|
|
description: `${file?.collection?.name} - ${file?.collection?.description}`,
|
|
|
|
|
knowledge: true, // DO NOT REMOVE, USED TO INDICATE KNOWLEDGE BASE FILE
|
|
|
|
|
type: 'file'
|
|
|
|
|
}))
|
|
|
|
|
]
|
|
|
|
|
: [];
|
|
|
|
|
|
2025-12-10 06:07:12 +00:00
|
|
|
items = [...collections, ...collection_files];
|
2025-09-14 07:54:06 +00:00
|
|
|
await tick();
|
2025-09-14 08:11:17 +00:00
|
|
|
|
|
|
|
|
loaded = true;
|
2025-09-14 07:54:06 +00:00
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
2025-09-14 08:11:17 +00:00
|
|
|
{#if loaded}
|
|
|
|
|
<div class="flex flex-col gap-0.5">
|
|
|
|
|
{#each items as item, idx}
|
|
|
|
|
<button
|
|
|
|
|
class=" px-2.5 py-1 rounded-xl w-full text-left flex justify-between items-center text-sm {idx ===
|
|
|
|
|
selectedIdx
|
|
|
|
|
? ' bg-gray-50 dark:bg-gray-800 dark:text-gray-100 selected-command-option-button'
|
|
|
|
|
: ''}"
|
|
|
|
|
type="button"
|
|
|
|
|
on:click={() => {
|
|
|
|
|
console.log(item);
|
|
|
|
|
onSelect(item);
|
|
|
|
|
}}
|
|
|
|
|
on:mousemove={() => {
|
|
|
|
|
selectedIdx = idx;
|
|
|
|
|
}}
|
|
|
|
|
on:mouseleave={() => {
|
|
|
|
|
if (idx === 0) {
|
|
|
|
|
selectedIdx = -1;
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
data-selected={idx === selectedIdx}
|
|
|
|
|
>
|
|
|
|
|
<div class=" text-black dark:text-gray-100 flex items-center gap-1">
|
|
|
|
|
<Tooltip
|
|
|
|
|
content={item?.legacy
|
|
|
|
|
? $i18n.t('Legacy')
|
|
|
|
|
: item?.type === 'file'
|
|
|
|
|
? $i18n.t('File')
|
|
|
|
|
: item?.type === 'collection'
|
|
|
|
|
? $i18n.t('Collection')
|
|
|
|
|
: ''}
|
|
|
|
|
placement="top"
|
|
|
|
|
>
|
|
|
|
|
{#if item?.type === 'collection'}
|
|
|
|
|
<Database className="size-4" />
|
|
|
|
|
{:else}
|
|
|
|
|
<DocumentPage className="size-4" />
|
|
|
|
|
{/if}
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
|
|
|
|
<Tooltip content={item.description || decodeString(item?.name)} placement="top-start">
|
|
|
|
|
<div class="line-clamp-1 flex-1">
|
|
|
|
|
{decodeString(item?.name)}
|
|
|
|
|
</div>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
{/each}
|
|
|
|
|
</div>
|
|
|
|
|
{:else}
|
|
|
|
|
<div class="py-4.5">
|
|
|
|
|
<Spinner />
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|