forked from open-webui/open-webui
		
	Merge pull request #617 from ollama-webui/doc-collection
feat: document collection
This commit is contained in:
		
						commit
						1f02940bbd
					
				
					 17 changed files with 817 additions and 310 deletions
				
			
		| 
						 | 
				
			
			@ -295,7 +295,7 @@
 | 
			
		|||
							files = [
 | 
			
		||||
								...files,
 | 
			
		||||
								{
 | 
			
		||||
									type: 'doc',
 | 
			
		||||
									type: e?.detail?.type ?? 'doc',
 | 
			
		||||
									...e.detail,
 | 
			
		||||
									upload_status: true
 | 
			
		||||
								}
 | 
			
		||||
| 
						 | 
				
			
			@ -446,6 +446,34 @@
 | 
			
		|||
												<div class=" text-gray-500 text-sm">Document</div>
 | 
			
		||||
											</div>
 | 
			
		||||
										</div>
 | 
			
		||||
									{:else if file.type === 'collection'}
 | 
			
		||||
										<div
 | 
			
		||||
											class="h-16 w-[15rem] flex items-center space-x-3 px-2.5 dark:bg-gray-600 rounded-xl border border-gray-200 dark:border-none"
 | 
			
		||||
										>
 | 
			
		||||
											<div class="p-2.5 bg-red-400 text-white rounded-lg">
 | 
			
		||||
												<svg
 | 
			
		||||
													xmlns="http://www.w3.org/2000/svg"
 | 
			
		||||
													viewBox="0 0 24 24"
 | 
			
		||||
													fill="currentColor"
 | 
			
		||||
													class="w-6 h-6"
 | 
			
		||||
												>
 | 
			
		||||
													<path
 | 
			
		||||
														d="M7.5 3.375c0-1.036.84-1.875 1.875-1.875h.375a3.75 3.75 0 0 1 3.75 3.75v1.875C13.5 8.161 14.34 9 15.375 9h1.875A3.75 3.75 0 0 1 21 12.75v3.375C21 17.16 20.16 18 19.125 18h-9.75A1.875 1.875 0 0 1 7.5 16.125V3.375Z"
 | 
			
		||||
													/>
 | 
			
		||||
													<path
 | 
			
		||||
														d="M15 5.25a5.23 5.23 0 0 0-1.279-3.434 9.768 9.768 0 0 1 6.963 6.963A5.23 5.23 0 0 0 17.25 7.5h-1.875A.375.375 0 0 1 15 7.125V5.25ZM4.875 6H6v10.125A3.375 3.375 0 0 0 9.375 19.5H16.5v1.125c0 1.035-.84 1.875-1.875 1.875h-9.75A1.875 1.875 0 0 1 3 20.625V7.875C3 6.839 3.84 6 4.875 6Z"
 | 
			
		||||
													/>
 | 
			
		||||
												</svg>
 | 
			
		||||
											</div>
 | 
			
		||||
 | 
			
		||||
											<div class="flex flex-col justify-center -space-y-0.5">
 | 
			
		||||
												<div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
 | 
			
		||||
													{file?.title ?? `#${file.name}`}
 | 
			
		||||
												</div>
 | 
			
		||||
 | 
			
		||||
												<div class=" text-gray-500 text-sm">Collection</div>
 | 
			
		||||
											</div>
 | 
			
		||||
										</div>
 | 
			
		||||
									{/if}
 | 
			
		||||
 | 
			
		||||
									<div class=" absolute -top-1 -right-1">
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,14 +10,50 @@
 | 
			
		|||
 | 
			
		||||
	const dispatch = createEventDispatcher();
 | 
			
		||||
	let selectedIdx = 0;
 | 
			
		||||
 | 
			
		||||
	let filteredItems = [];
 | 
			
		||||
	let filteredDocs = [];
 | 
			
		||||
 | 
			
		||||
	let collections = [];
 | 
			
		||||
 | 
			
		||||
	$: collections = [
 | 
			
		||||
		...($documents.length > 0
 | 
			
		||||
			? [
 | 
			
		||||
					{
 | 
			
		||||
						name: 'All Documents',
 | 
			
		||||
						type: 'collection',
 | 
			
		||||
						title: 'All Documents',
 | 
			
		||||
						collection_names: $documents.map((doc) => doc.collection_name)
 | 
			
		||||
					}
 | 
			
		||||
			  ]
 | 
			
		||||
			: []),
 | 
			
		||||
		...$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
 | 
			
		||||
		.filter((collection) => collection.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
 | 
			
		||||
		.sort((a, b) => a.name.localeCompare(b.name));
 | 
			
		||||
 | 
			
		||||
	$: filteredDocs = $documents
 | 
			
		||||
		.filter((p) => p.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
 | 
			
		||||
		.filter((doc) => doc.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
 | 
			
		||||
		.sort((a, b) => a.title.localeCompare(b.title));
 | 
			
		||||
 | 
			
		||||
	$: filteredItems = [...filteredCollections, ...filteredDocs];
 | 
			
		||||
 | 
			
		||||
	$: if (prompt) {
 | 
			
		||||
		selectedIdx = 0;
 | 
			
		||||
 | 
			
		||||
		console.log(filteredCollections);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	export const selectUp = () => {
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +61,7 @@
 | 
			
		|||
	};
 | 
			
		||||
 | 
			
		||||
	export const selectDown = () => {
 | 
			
		||||
		selectedIdx = Math.min(selectedIdx + 1, filteredDocs.length - 1);
 | 
			
		||||
		selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const confirmSelect = async (doc) => {
 | 
			
		||||
| 
						 | 
				
			
			@ -51,7 +87,7 @@
 | 
			
		|||
	};
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
{#if filteredDocs.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
 | 
			
		||||
{#if filteredItems.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
 | 
			
		||||
	<div class="md:px-2 mb-3 text-left w-full">
 | 
			
		||||
		<div class="flex w-full rounded-lg border border-gray-100 dark:border-gray-700">
 | 
			
		||||
			<div class=" bg-gray-100 dark:bg-gray-700 w-10 rounded-l-lg text-center">
 | 
			
		||||
| 
						 | 
				
			
			@ -60,7 +96,7 @@
 | 
			
		|||
 | 
			
		||||
			<div class="max-h-60 flex flex-col w-full rounded-r-lg">
 | 
			
		||||
				<div class=" overflow-y-auto bg-white p-2 rounded-tr-lg space-y-0.5">
 | 
			
		||||
					{#each filteredDocs as doc, docIdx}
 | 
			
		||||
					{#each filteredItems as doc, docIdx}
 | 
			
		||||
						<button
 | 
			
		||||
							class=" px-3 py-1.5 rounded-lg w-full text-left {docIdx === selectedIdx
 | 
			
		||||
								? ' bg-gray-100 selected-command-option-button'
 | 
			
		||||
| 
						 | 
				
			
			@ -68,6 +104,7 @@
 | 
			
		|||
							type="button"
 | 
			
		||||
							on:click={() => {
 | 
			
		||||
								console.log(doc);
 | 
			
		||||
 | 
			
		||||
								confirmSelect(doc);
 | 
			
		||||
							}}
 | 
			
		||||
							on:mousemove={() => {
 | 
			
		||||
| 
						 | 
				
			
			@ -75,13 +112,21 @@
 | 
			
		|||
							}}
 | 
			
		||||
							on:focus={() => {}}
 | 
			
		||||
						>
 | 
			
		||||
							<div class=" font-medium text-black line-clamp-1">
 | 
			
		||||
								#{doc.name} ({doc.filename})
 | 
			
		||||
							</div>
 | 
			
		||||
							{#if doc.type === 'collection'}
 | 
			
		||||
								<div class=" font-medium text-black line-clamp-1">
 | 
			
		||||
									{doc?.title ?? `#${doc.name}`}
 | 
			
		||||
								</div>
 | 
			
		||||
 | 
			
		||||
							<div class=" text-xs text-gray-600 line-clamp-1">
 | 
			
		||||
								{doc.title}
 | 
			
		||||
							</div>
 | 
			
		||||
								<div class=" text-xs text-gray-600 line-clamp-1">Collection</div>
 | 
			
		||||
							{:else}
 | 
			
		||||
								<div class=" font-medium text-black line-clamp-1">
 | 
			
		||||
									#{doc.name} ({doc.filename})
 | 
			
		||||
								</div>
 | 
			
		||||
 | 
			
		||||
								<div class=" text-xs text-gray-600 line-clamp-1">
 | 
			
		||||
									{doc.title}
 | 
			
		||||
								</div>
 | 
			
		||||
							{/if}
 | 
			
		||||
						</button>
 | 
			
		||||
					{/each}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -117,6 +117,35 @@
 | 
			
		|||
										<div class=" text-gray-500 text-sm">Document</div>
 | 
			
		||||
									</div>
 | 
			
		||||
								</button>
 | 
			
		||||
							{:else if file.type === 'collection'}
 | 
			
		||||
								<button
 | 
			
		||||
									class="h-16 w-[15rem] flex items-center space-x-3 px-2.5 dark:bg-gray-600 rounded-xl border border-gray-200 dark:border-none text-left"
 | 
			
		||||
									type="button"
 | 
			
		||||
								>
 | 
			
		||||
									<div class="p-2.5 bg-red-400 text-white rounded-lg">
 | 
			
		||||
										<svg
 | 
			
		||||
											xmlns="http://www.w3.org/2000/svg"
 | 
			
		||||
											viewBox="0 0 24 24"
 | 
			
		||||
											fill="currentColor"
 | 
			
		||||
											class="w-6 h-6"
 | 
			
		||||
										>
 | 
			
		||||
											<path
 | 
			
		||||
												d="M7.5 3.375c0-1.036.84-1.875 1.875-1.875h.375a3.75 3.75 0 0 1 3.75 3.75v1.875C13.5 8.161 14.34 9 15.375 9h1.875A3.75 3.75 0 0 1 21 12.75v3.375C21 17.16 20.16 18 19.125 18h-9.75A1.875 1.875 0 0 1 7.5 16.125V3.375Z"
 | 
			
		||||
											/>
 | 
			
		||||
											<path
 | 
			
		||||
												d="M15 5.25a5.23 5.23 0 0 0-1.279-3.434 9.768 9.768 0 0 1 6.963 6.963A5.23 5.23 0 0 0 17.25 7.5h-1.875A.375.375 0 0 1 15 7.125V5.25ZM4.875 6H6v10.125A3.375 3.375 0 0 0 9.375 19.5H16.5v1.125c0 1.035-.84 1.875-1.875 1.875h-9.75A1.875 1.875 0 0 1 3 20.625V7.875C3 6.839 3.84 6 4.875 6Z"
 | 
			
		||||
											/>
 | 
			
		||||
										</svg>
 | 
			
		||||
									</div>
 | 
			
		||||
 | 
			
		||||
									<div class="flex flex-col justify-center -space-y-0.5">
 | 
			
		||||
										<div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
 | 
			
		||||
											{file?.title ?? `#${file.name}`}
 | 
			
		||||
										</div>
 | 
			
		||||
 | 
			
		||||
										<div class=" text-gray-500 text-sm">Collection</div>
 | 
			
		||||
									</div>
 | 
			
		||||
								</button>
 | 
			
		||||
							{/if}
 | 
			
		||||
						</div>
 | 
			
		||||
					{/each}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue