open-webui/src/lib/components/chat/MessageInput/Commands/Knowledge.svelte

230 lines
6.4 KiB
Svelte
Raw Normal View History

2024-01-08 15:43:32 +08:00
<script lang="ts">
2024-10-02 08:35:35 +08:00
import { toast } from 'svelte-sonner';
import Fuse from 'fuse.js';
2024-01-08 15:43:32 +08:00
2024-10-02 08:35:35 +08:00
import { createEventDispatcher, tick, getContext, onMount } from 'svelte';
2024-08-23 20:43:32 +08:00
import { removeLastWordFromString, isValidHttpUrl } from '$lib/utils';
2024-10-02 13:45:04 +08:00
import { knowledge } from '$lib/stores';
2024-01-08 15:43:32 +08:00
const i18n = getContext('i18n');
2024-01-08 15:43:32 +08:00
export let prompt = '';
2024-08-23 20:31:39 +08:00
export let command = '';
2024-01-08 15:43:32 +08:00
const dispatch = createEventDispatcher();
let selectedIdx = 0;
2024-10-02 14:21:33 +08:00
let items = [];
2024-10-02 08:35:35 +08:00
let fuse = null;
2024-10-02 13:45:04 +08:00
let filteredItems = [];
2024-10-02 08:35:35 +08:00
$: if (fuse) {
2024-10-02 13:45:04 +08:00
filteredItems = command.slice(1)
2024-10-02 08:35:35 +08:00
? fuse.search(command).map((e) => {
return e.item;
})
2024-10-02 14:21:33 +08:00
: items;
2024-10-02 08:35:35 +08:00
}
2024-08-23 20:31:39 +08:00
$: if (command) {
2024-01-08 15:43:32 +08:00
selectedIdx = 0;
}
export const selectUp = () => {
selectedIdx = Math.max(0, selectedIdx - 1);
};
export const selectDown = () => {
2024-10-02 13:45:04 +08:00
selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);
2024-01-08 15:43:32 +08:00
};
2024-10-02 08:35:35 +08:00
const confirmSelect = async (item) => {
dispatch('select', item);
2024-01-08 15:43:32 +08:00
2024-08-23 20:43:32 +08:00
prompt = removeLastWordFromString(prompt, command);
2024-01-08 15:43:32 +08:00
const chatInputElement = document.getElementById('chat-textarea');
await tick();
chatInputElement?.focus();
await tick();
};
2024-01-27 14:17:28 +08:00
const confirmSelectWeb = async (url) => {
dispatch('url', url);
2024-08-23 20:43:32 +08:00
prompt = removeLastWordFromString(prompt, command);
2024-01-27 14:17:28 +08:00
const chatInputElement = document.getElementById('chat-textarea');
await tick();
chatInputElement?.focus();
await tick();
};
2024-05-02 08:17:00 +08:00
const confirmSelectYoutube = async (url) => {
dispatch('youtube', url);
2024-08-23 20:43:32 +08:00
prompt = removeLastWordFromString(prompt, command);
2024-05-02 08:17:00 +08:00
const chatInputElement = document.getElementById('chat-textarea');
await tick();
chatInputElement?.focus();
await tick();
};
2024-10-02 08:35:35 +08:00
onMount(() => {
2024-10-02 14:21:33 +08:00
let legacy_documents = $knowledge.filter((item) => item?.meta?.document);
let legacy_collections =
legacy_documents.length > 0
? [
{
name: 'All Documents',
legacy: true,
type: 'collection',
2024-10-02 21:19:09 +08:00
description: 'Deprecated (legacy collection), please create a new knowledge base.',
2024-10-02 14:21:33 +08:00
title: $i18n.t('All Documents'),
collection_names: legacy_documents.map((item) => item.id)
},
...legacy_documents
.reduce((a, item) => {
return [...new Set([...a, ...(item?.meta?.tags ?? []).map((tag) => tag.name)])];
}, [])
.map((tag) => ({
name: tag,
legacy: true,
type: 'collection',
2024-10-02 21:19:09 +08:00
description: 'Deprecated (legacy collection), please create a new knowledge base.',
2024-10-02 14:21:33 +08:00
collection_names: legacy_documents
.filter((item) => (item?.meta?.tags ?? []).map((tag) => tag.name).includes(tag))
.map((item) => item.id)
}))
]
: [];
2024-10-04 15:59:19 +08:00
items = [...$knowledge, ...legacy_collections].map((item) => {
return {
...item,
2024-10-05 07:43:41 +08:00
...(item?.legacy || item?.meta?.legacy || item?.meta?.document ? { legacy: true } : {})
2024-10-04 15:59:19 +08:00
};
});
2024-10-02 14:21:33 +08:00
fuse = new Fuse(items, {
2024-10-02 08:35:35 +08:00
keys: ['name', 'description']
});
});
2024-01-08 15:43:32 +08:00
</script>
2024-10-02 13:45:04 +08:00
{#if filteredItems.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
2024-08-23 20:31:39 +08:00
<div
id="commands-container"
2024-10-12 16:27:55 +08:00
class="pl-8 pr-14 mb-3 text-left w-full absolute bottom-0 left-0 right-0 z-10"
2024-08-23 20:31:39 +08:00
>
2024-10-12 16:27:55 +08:00
<div class="flex w-full rounded-xl border border-gray-50 dark:border-gray-850">
2024-06-17 00:24:16 +08:00
<div
2024-10-12 16:27:55 +08:00
class="max-h-60 flex flex-col w-full rounded-xl bg-white dark:bg-gray-900 dark:text-gray-100"
2024-06-17 00:24:16 +08:00
>
<div class="m-1 overflow-y-auto p-1 rounded-r-xl space-y-0.5 scrollbar-hidden">
2024-10-02 13:45:04 +08:00
{#each filteredItems as item, idx}
2024-01-08 15:43:32 +08:00
<button
2024-10-02 08:35:35 +08:00
class=" px-3 py-1.5 rounded-xl w-full text-left {idx === selectedIdx
2024-06-17 00:24:16 +08:00
? ' bg-gray-50 dark:bg-gray-850 dark:text-gray-100 selected-command-option-button'
2024-01-08 15:43:32 +08:00
: ''}"
type="button"
on:click={() => {
2024-10-02 13:45:04 +08:00
console.log(item);
confirmSelect(item);
2024-01-08 15:43:32 +08:00
}}
on:mousemove={() => {
2024-10-02 08:35:35 +08:00
selectedIdx = idx;
2024-01-08 15:43:32 +08:00
}}
on:focus={() => {}}
>
2024-10-02 08:35:35 +08:00
<div class=" font-medium text-black dark:text-gray-100 flex items-center gap-1">
2024-10-02 14:21:33 +08:00
{#if item.legacy}
2024-10-02 08:35:35 +08:00
<div
2024-10-02 14:21:33 +08:00
class="bg-gray-500/20 text-gray-700 dark:text-gray-200 rounded uppercase text-xs font-bold px-1"
>
Legacy
</div>
{:else if item?.meta?.document}
<div
class="bg-gray-500/20 text-gray-700 dark:text-gray-200 rounded uppercase text-xs font-bold px-1"
2024-10-02 08:35:35 +08:00
>
2024-10-02 12:48:15 +08:00
Document
2024-10-02 08:35:35 +08:00
</div>
{:else}
<div
2024-10-02 14:21:33 +08:00
class="bg-green-500/20 text-green-700 dark:text-green-200 rounded uppercase text-xs font-bold px-1"
2024-10-02 08:35:35 +08:00
>
2024-10-02 13:45:04 +08:00
Collection
2024-10-02 08:35:35 +08:00
</div>
{/if}
2024-10-02 14:21:33 +08:00
<div class="line-clamp-1">
{item.name}
</div>
2024-10-02 08:35:35 +08:00
</div>
<div class=" text-xs text-gray-600 dark:text-gray-100 line-clamp-1">
2024-10-02 14:21:33 +08:00
{item?.description}
2024-10-02 08:35:35 +08:00
</div>
2024-01-08 15:43:32 +08:00
</button>
{/each}
2024-01-27 14:17:28 +08:00
2024-05-09 07:29:25 +08:00
{#if prompt
.split(' ')
.some((s) => s.substring(1).startsWith('https://www.youtube.com') || s
.substring(1)
.startsWith('https://youtu.be'))}
2024-05-02 08:17:00 +08:00
<button
2024-07-02 16:16:57 +08:00
class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-50 dark:bg-gray-850 dark:text-gray-100 selected-command-option-button"
2024-05-02 08:17:00 +08:00
type="button"
on:click={() => {
const url = prompt.split(' ')?.at(0)?.substring(1);
if (isValidHttpUrl(url)) {
confirmSelectYoutube(url);
} else {
toast.error(
$i18n.t(
'Oops! Looks like the URL is invalid. Please double-check and try again.'
)
);
}
}}
>
2024-07-02 16:16:57 +08:00
<div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
2024-05-02 08:17:00 +08:00
{prompt.split(' ')?.at(0)?.substring(1)}
</div>
<div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Youtube')}</div>
</button>
{:else if prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
2024-01-27 14:17:28 +08:00
<button
2024-07-02 16:16:57 +08:00
class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-50 dark:bg-gray-850 dark:text-gray-100 selected-command-option-button"
2024-01-27 14:17:28 +08:00
type="button"
on:click={() => {
const url = prompt.split(' ')?.at(0)?.substring(1);
if (isValidHttpUrl(url)) {
confirmSelectWeb(url);
2024-01-27 14:22:37 +08:00
} else {
toast.error(
$i18n.t(
'Oops! Looks like the URL is invalid. Please double-check and try again.'
)
2024-01-27 14:22:37 +08:00
);
2024-01-27 14:17:28 +08:00
}
}}
>
2024-07-02 16:16:57 +08:00
<div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
2024-01-27 14:17:28 +08:00
{prompt.split(' ')?.at(0)?.substring(1)}
</div>
<div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Web')}</div>
2024-01-27 14:17:28 +08:00
</button>
{/if}
2024-01-08 15:43:32 +08:00
</div>
</div>
</div>
</div>
{/if}