open-webui/src/lib/components/chat/MessageInput/Documents.svelte

202 lines
5.7 KiB
Svelte
Raw Normal View History

2024-01-08 07:43:32 +00:00
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { documents } from '$lib/stores';
2024-01-27 06:17:28 +00:00
import { removeFirstHashWord, isValidHttpUrl } from '$lib/utils';
import { tick, getContext } from 'svelte';
2024-03-01 09:18:07 +00:00
import { toast } from 'svelte-sonner';
2024-01-08 07:43:32 +00:00
const i18n = getContext('i18n');
2024-01-08 07:43:32 +00:00
export let prompt = '';
const dispatch = createEventDispatcher();
let selectedIdx = 0;
let filteredItems = [];
2024-01-08 07:43:32 +00:00
let filteredDocs = [];
let collections = [];
2024-02-04 01:21:51 +00:00
$: collections = [
2024-02-04 01:26:57 +00:00
...($documents.length > 0
? [
{
name: 'All Documents',
type: 'collection',
2024-05-07 06:43:25 +00:00
title: $i18n.t('All Documents'),
2024-02-04 01:26:57 +00:00
collection_names: $documents.map((doc) => doc.collection_name)
}
]
: []),
2024-02-04 01:21:51 +00:00
...$documents
.reduce((a, e, i, arr) => {
return [...new Set([...a, ...(e?.content?.tags ?? []).map((tag) => tag.name)])];
}, [])
.map((tag) => ({
name: tag,
type: 'collection',
collection_names: $documents
.filter((doc) => (doc?.content?.tags ?? []).map((tag) => tag.name).includes(tag))
.map((doc) => doc.collection_name)
}))
];
$: filteredCollections = collections
2024-02-04 01:21:51 +00:00
.filter((collection) => collection.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
.sort((a, b) => a.name.localeCompare(b.name));
2024-01-08 07:43:32 +00:00
$: filteredDocs = $documents
2024-02-04 01:21:51 +00:00
.filter((doc) => doc.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
2024-01-08 07:43:32 +00:00
.sort((a, b) => a.title.localeCompare(b.title));
$: filteredItems = [...filteredCollections, ...filteredDocs];
2024-01-08 07:43:32 +00:00
$: if (prompt) {
selectedIdx = 0;
2024-02-04 01:21:51 +00:00
console.log(filteredCollections);
2024-01-08 07:43:32 +00:00
}
export const selectUp = () => {
selectedIdx = Math.max(0, selectedIdx - 1);
};
export const selectDown = () => {
selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);
2024-01-08 07:43:32 +00:00
};
const confirmSelect = async (doc) => {
dispatch('select', doc);
prompt = removeFirstHashWord(prompt);
const chatInputElement = document.getElementById('chat-textarea');
await tick();
chatInputElement?.focus();
await tick();
};
2024-01-27 06:17:28 +00:00
const confirmSelectWeb = async (url) => {
dispatch('url', url);
prompt = removeFirstHashWord(prompt);
const chatInputElement = document.getElementById('chat-textarea');
await tick();
chatInputElement?.focus();
await tick();
};
2024-05-02 00:17:00 +00:00
const confirmSelectYoutube = async (url) => {
dispatch('youtube', url);
prompt = removeFirstHashWord(prompt);
const chatInputElement = document.getElementById('chat-textarea');
await tick();
chatInputElement?.focus();
await tick();
};
2024-01-08 07:43:32 +00:00
</script>
2024-02-04 01:21:51 +00:00
{#if filteredItems.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
2024-06-09 23:17:10 +00:00
<div class="pl-1 pr-12 mb-3 text-left w-full absolute bottom-0 left-0 right-0">
2024-02-25 21:27:19 +00:00
<div class="flex w-full px-2">
<div class=" bg-gray-100 dark:bg-gray-700 w-10 rounded-l-xl text-center">
2024-01-08 07:43:32 +00:00
<div class=" text-lg font-semibold mt-2">#</div>
</div>
<div class="max-h-60 flex flex-col w-full rounded-r-xl bg-white dark:bg-gray-850 dark:text-gray-100 ">
2024-02-25 21:27:19 +00:00
<div class="m-1 overflow-y-auto p-1 rounded-r-xl space-y-0.5">
{#each filteredItems as doc, docIdx}
2024-01-08 07:43:32 +00:00
<button
2024-02-25 21:27:19 +00:00
class=" px-3 py-1.5 rounded-xl w-full text-left {docIdx === selectedIdx
? ' bg-gray-100 dark:bg-gray-600 dark:text-gray-100 selected-command-option-button'
2024-01-08 07:43:32 +00:00
: ''}"
type="button"
on:click={() => {
2024-01-27 06:17:28 +00:00
console.log(doc);
2024-01-08 07:43:32 +00:00
confirmSelect(doc);
}}
on:mousemove={() => {
selectedIdx = docIdx;
}}
on:focus={() => {}}
>
{#if doc.type === 'collection'}
<div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
2024-02-04 01:21:51 +00:00
{doc?.title ?? `#${doc.name}`}
</div>
<div class=" text-xs text-gray-600 dark:text-gray-100 line-clamp-1">{$i18n.t('Collection')}</div>
{:else}
<div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
#{doc.name} ({doc.filename})
</div>
<div class=" text-xs text-gray-600 dark:text-gray-100 line-clamp-1">
{doc.title}
</div>
{/if}
2024-01-08 07:43:32 +00:00
</button>
{/each}
2024-01-27 06:17:28 +00:00
2024-05-08 23:29:25 +00:00
{#if prompt
.split(' ')
.some((s) => s.substring(1).startsWith('https://www.youtube.com') || s
.substring(1)
.startsWith('https://youtu.be'))}
2024-05-02 00:17:00 +00:00
<button
class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-100 selected-command-option-button"
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.'
)
);
}
}}
>
<div class=" font-medium text-black line-clamp-1">
{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 06:17:28 +00:00
<button
2024-02-25 21:27:19 +00:00
class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-100 selected-command-option-button"
2024-01-27 06:17:28 +00:00
type="button"
on:click={() => {
const url = prompt.split(' ')?.at(0)?.substring(1);
if (isValidHttpUrl(url)) {
confirmSelectWeb(url);
2024-01-27 06:22:37 +00:00
} else {
toast.error(
$i18n.t(
'Oops! Looks like the URL is invalid. Please double-check and try again.'
)
2024-01-27 06:22:37 +00:00
);
2024-01-27 06:17:28 +00:00
}
}}
>
<div class=" font-medium text-black line-clamp-1">
{prompt.split(' ')?.at(0)?.substring(1)}
</div>
<div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Web')}</div>
2024-01-27 06:17:28 +00:00
</button>
{/if}
2024-01-08 07:43:32 +00:00
</div>
</div>
</div>
</div>
{/if}