2024-01-08 07:43:32 +00:00
|
|
|
<script lang="ts">
|
2024-10-02 00:35:35 +00:00
|
|
|
import { toast } from 'svelte-sonner';
|
2024-10-12 09:31:10 +00:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import relativeTime from 'dayjs/plugin/relativeTime';
|
|
|
|
|
dayjs.extend(relativeTime);
|
|
|
|
|
|
2025-07-04 16:26:01 +00:00
|
|
|
import { tick, getContext, onMount, onDestroy } from 'svelte';
|
2025-12-11 04:28:33 +00:00
|
|
|
|
|
|
|
|
import { removeLastWordFromString, isValidHttpUrl, isYoutubeUrl, decodeString } from '$lib/utils';
|
2025-09-12 16:31:57 +00:00
|
|
|
import Tooltip from '$lib/components/common/Tooltip.svelte';
|
|
|
|
|
import DocumentPage from '$lib/components/icons/DocumentPage.svelte';
|
|
|
|
|
import Database from '$lib/components/icons/Database.svelte';
|
|
|
|
|
import GlobeAlt from '$lib/components/icons/GlobeAlt.svelte';
|
|
|
|
|
import Youtube from '$lib/components/icons/Youtube.svelte';
|
2025-10-05 07:48:08 +00:00
|
|
|
import { folders } from '$lib/stores';
|
|
|
|
|
import Folder from '$lib/components/icons/Folder.svelte';
|
2025-12-11 04:19:19 +00:00
|
|
|
import { getFolders } from '$lib/apis/folders';
|
2025-12-11 04:28:33 +00:00
|
|
|
import { searchKnowledgeBases, searchKnowledgeFiles } from '$lib/apis/knowledge';
|
2024-01-08 07:43:32 +00:00
|
|
|
|
2024-03-02 20:38:51 +00:00
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
|
2025-09-12 16:31:57 +00:00
|
|
|
export let query = '';
|
2025-07-04 16:26:01 +00:00
|
|
|
export let onSelect = (e) => {};
|
2024-01-08 07:43:32 +00:00
|
|
|
|
|
|
|
|
let selectedIdx = 0;
|
2024-10-02 06:21:33 +00:00
|
|
|
let items = [];
|
2024-10-02 00:35:35 +00:00
|
|
|
|
2025-09-12 16:31:57 +00:00
|
|
|
export let filteredItems = [];
|
2025-12-11 04:28:33 +00:00
|
|
|
$: filteredItems = [
|
|
|
|
|
...(query.startsWith('http')
|
|
|
|
|
? isYoutubeUrl(query)
|
|
|
|
|
? [{ type: 'youtube', name: query, description: query }]
|
|
|
|
|
: [
|
|
|
|
|
{
|
|
|
|
|
type: 'web',
|
|
|
|
|
name: query,
|
|
|
|
|
description: query
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
: []),
|
|
|
|
|
...items
|
|
|
|
|
];
|
2024-02-03 23:48:44 +00:00
|
|
|
|
2025-09-12 16:31:57 +00:00
|
|
|
$: if (query) {
|
2024-01-08 07:43:32 +00:00
|
|
|
selectedIdx = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const selectUp = () => {
|
|
|
|
|
selectedIdx = Math.max(0, selectedIdx - 1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const selectDown = () => {
|
2024-10-02 05:45:04 +00:00
|
|
|
selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);
|
2024-01-08 07:43:32 +00:00
|
|
|
};
|
|
|
|
|
|
2025-09-12 16:31:57 +00:00
|
|
|
export const select = async () => {
|
|
|
|
|
// find item with data-selected=true
|
|
|
|
|
const item = document.querySelector(`[data-selected="true"]`);
|
|
|
|
|
if (item) {
|
|
|
|
|
// click the item
|
|
|
|
|
item.click();
|
2025-07-01 11:57:01 +00:00
|
|
|
}
|
|
|
|
|
};
|
2025-12-11 04:28:33 +00:00
|
|
|
|
|
|
|
|
let folderItems = [];
|
|
|
|
|
let knowledgeItems = [];
|
|
|
|
|
let fileItems = [];
|
|
|
|
|
|
|
|
|
|
$: items = [...folderItems, ...knowledgeItems, ...fileItems];
|
|
|
|
|
|
|
|
|
|
$: if (query !== null) {
|
|
|
|
|
getItems();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getItems = () => {
|
|
|
|
|
getFolderItems();
|
|
|
|
|
getKnowledgeItems();
|
|
|
|
|
getKnowledgeFileItems();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getFolderItems = async () => {
|
|
|
|
|
folderItems = $folders
|
|
|
|
|
.map((folder) => ({
|
|
|
|
|
...folder,
|
|
|
|
|
type: 'folder',
|
|
|
|
|
description: $i18n.t('Folder'),
|
|
|
|
|
title: folder.name
|
|
|
|
|
}))
|
|
|
|
|
.filter((folder) => folder.name.toLowerCase().includes(query.toLowerCase()));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getKnowledgeItems = async () => {
|
|
|
|
|
const res = await searchKnowledgeBases(localStorage.token, query).catch(() => {
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
|
knowledgeItems = res.items.map((item) => {
|
|
|
|
|
return {
|
|
|
|
|
...item,
|
|
|
|
|
type: 'collection'
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getKnowledgeFileItems = async () => {
|
|
|
|
|
const res = await searchKnowledgeFiles(localStorage.token, query).catch(() => {
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
|
fileItems = res.items.map((item) => {
|
|
|
|
|
return {
|
|
|
|
|
...item,
|
|
|
|
|
type: 'file',
|
|
|
|
|
name: item.filename,
|
|
|
|
|
description: item.collection ? item.collection.name : ''
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-07-01 11:57:01 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-08 21:17:25 +00:00
|
|
|
onMount(async () => {
|
2025-12-11 04:19:19 +00:00
|
|
|
if ($folders === null) {
|
|
|
|
|
await folders.set(await getFolders(localStorage.token));
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 22:14:23 +00:00
|
|
|
await tick();
|
2025-07-01 11:57:01 +00:00
|
|
|
});
|
2025-09-12 16:35:14 +00:00
|
|
|
|
|
|
|
|
const onKeyDown = (e) => {
|
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
select();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
onMount(() => {
|
|
|
|
|
window.addEventListener('keydown', onKeyDown);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
|
window.removeEventListener('keydown', onKeyDown);
|
|
|
|
|
});
|
2024-01-08 07:43:32 +00:00
|
|
|
</script>
|
|
|
|
|
|
2025-09-12 16:31:57 +00:00
|
|
|
{#if filteredItems.length > 0 || query.startsWith('http')}
|
|
|
|
|
{#each filteredItems as item, idx}
|
2025-12-11 04:28:33 +00:00
|
|
|
{#if idx === 0 || item?.type !== items[idx - 1]?.type}
|
|
|
|
|
<div class="px-2 text-xs text-gray-500 py-1">
|
|
|
|
|
{#if item?.type === 'folder'}
|
|
|
|
|
{$i18n.t('Folders')}
|
|
|
|
|
{:else if item?.type === 'collection'}
|
|
|
|
|
{$i18n.t('Collections')}
|
|
|
|
|
{:else if item?.type === 'file'}
|
|
|
|
|
{$i18n.t('Files')}
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
|
2025-09-12 16:31:57 +00:00
|
|
|
{#if !['youtube', 'web'].includes(item.type)}
|
|
|
|
|
<button
|
|
|
|
|
class=" px-2 py-1 rounded-xl w-full text-left flex justify-between items-center {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({
|
|
|
|
|
type: 'knowledge',
|
|
|
|
|
data: item
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
on:mousemove={() => {
|
|
|
|
|
selectedIdx = idx;
|
|
|
|
|
}}
|
|
|
|
|
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'
|
2025-11-27 02:33:27 +00:00
|
|
|
? `${item?.collection?.name} > ${$i18n.t('File')}`
|
2025-09-12 16:31:57 +00:00
|
|
|
: item?.type === 'collection'
|
|
|
|
|
? $i18n.t('Collection')
|
|
|
|
|
: ''}
|
|
|
|
|
placement="top"
|
|
|
|
|
>
|
|
|
|
|
{#if item?.type === 'collection'}
|
|
|
|
|
<Database className="size-4" />
|
2025-10-05 07:48:08 +00:00
|
|
|
{:else if item?.type === 'folder'}
|
|
|
|
|
<Folder className="size-4" />
|
2025-09-12 16:31:57 +00:00
|
|
|
{:else}
|
|
|
|
|
<DocumentPage className="size-4" />
|
|
|
|
|
{/if}
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
2025-11-09 20:44:27 +00:00
|
|
|
<Tooltip content={`${decodeString(item?.name)}`} placement="top-start">
|
2025-09-12 16:31:57 +00:00
|
|
|
<div class="line-clamp-1 flex-1">
|
|
|
|
|
{decodeString(item?.name)}
|
|
|
|
|
</div>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
{/if}
|
|
|
|
|
{/each}
|
|
|
|
|
|
2025-09-24 16:36:17 +00:00
|
|
|
{#if isYoutubeUrl(query)}
|
2025-09-12 16:31:57 +00:00
|
|
|
<button
|
|
|
|
|
class="px-2 py-1 rounded-xl w-full text-left bg-gray-50 dark:bg-gray-800 dark:text-gray-100 selected-command-option-button"
|
|
|
|
|
type="button"
|
|
|
|
|
data-selected={true}
|
|
|
|
|
on:click={() => {
|
|
|
|
|
if (isValidHttpUrl(query)) {
|
|
|
|
|
onSelect({
|
|
|
|
|
type: 'youtube',
|
|
|
|
|
data: query
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
toast.error(
|
|
|
|
|
$i18n.t('Oops! Looks like the URL is invalid. Please double-check and try again.')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div class=" text-black dark:text-gray-100 line-clamp-1 flex items-center gap-1">
|
|
|
|
|
<Tooltip content={$i18n.t('YouTube')} placement="top">
|
|
|
|
|
<Youtube className="size-4" />
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
|
|
|
|
<div class="truncate flex-1">
|
|
|
|
|
{query}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
{:else if query.startsWith('http')}
|
|
|
|
|
<button
|
|
|
|
|
class="px-2 py-1 rounded-xl w-full text-left bg-gray-50 dark:bg-gray-800 dark:text-gray-100 selected-command-option-button"
|
|
|
|
|
type="button"
|
|
|
|
|
data-selected={true}
|
|
|
|
|
on:click={() => {
|
|
|
|
|
if (isValidHttpUrl(query)) {
|
|
|
|
|
onSelect({
|
|
|
|
|
type: 'web',
|
|
|
|
|
data: query
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
toast.error(
|
|
|
|
|
$i18n.t('Oops! Looks like the URL is invalid. Please double-check and try again.')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div class=" text-black dark:text-gray-100 line-clamp-1 flex items-center gap-1">
|
|
|
|
|
<Tooltip content={$i18n.t('Web')} placement="top">
|
|
|
|
|
<GlobeAlt className="size-4" />
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
|
|
|
|
<div class="truncate flex-1">
|
|
|
|
|
{query}
|
2024-01-08 07:43:32 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-09-12 16:31:57 +00:00
|
|
|
</button>
|
|
|
|
|
{/if}
|
2024-01-08 07:43:32 +00:00
|
|
|
{/if}
|