diff --git a/src/lib/components/workspace/Models/Knowledge/Selector.svelte b/src/lib/components/workspace/Models/Knowledge/Selector.svelte index a17a2d3aa7..7c6cb74d81 100644 --- a/src/lib/components/workspace/Models/Knowledge/Selector.svelte +++ b/src/lib/components/workspace/Models/Knowledge/Selector.svelte @@ -7,6 +7,8 @@ import { knowledge } from '$lib/stores'; import Dropdown from '$lib/components/common/Dropdown.svelte'; import Search from '$lib/components/icons/Search.svelte'; + import { getNoteList } from '$lib/apis/notes'; + import dayjs from 'dayjs'; const i18n = getContext('i18n'); const dispatch = createEventDispatcher(); @@ -27,8 +29,35 @@ : items; } - onMount(() => { - let legacy_documents = $knowledge.filter((item) => item?.meta?.document); + const decodeString = (str: string) => { + try { + return decodeURIComponent(str); + } catch (e) { + return str; + } + }; + + onMount(async () => { + let notes = await getNoteList(localStorage.token).catch(() => { + return []; + }); + + notes = notes.map((note) => { + return { + ...note, + type: 'note', + name: note.title, + description: dayjs(note.updated_at / 1000000).fromNow() + }; + }); + + let legacy_documents = $knowledge + .filter((item) => item?.meta?.document) + .map((item) => ({ + ...item, + type: 'file' + })); + let legacy_collections = legacy_documents.length > 0 ? [ @@ -37,7 +66,6 @@ legacy: true, type: 'collection', description: 'Deprecated (legacy collection), please create a new knowledge base.', - title: $i18n.t('All Documents'), collection_names: legacy_documents.map((item) => item.id) }, @@ -51,7 +79,6 @@ legacy: true, type: 'collection', description: 'Deprecated (legacy collection), please create a new knowledge base.', - collection_names: legacy_documents .filter((item) => (item?.meta?.tags ?? []).map((tag) => tag.name).includes(tag)) .map((item) => item.id) @@ -59,11 +86,46 @@ ] : []; - items = [...$knowledge, ...legacy_collections].map((item) => { + let collections = $knowledge + .filter((item) => !item?.meta?.document) + .map((item) => ({ + ...item, + type: 'collection' + })); + let collection_files = + $knowledge.length > 0 + ? [ + ...$knowledge + .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}`, + type: 'file' + })) + ] + : []; + + items = [ + ...notes, + ...collections, + ...collection_files, + ...legacy_collections, + ...legacy_documents + ].map((item) => { return { ...item, - ...(item?.legacy || item?.meta?.legacy || item?.meta?.document ? { legacy: true } : {}), - type: item?.meta?.document ? 'document' : 'collection' + ...(item?.legacy || item?.meta?.legacy || item?.meta?.document ? { legacy: true } : {}) }; }); @@ -85,7 +147,7 @@