mirror of
				https://github.com/open-webui/open-webui
				synced 2025-06-26 18:26:48 +00:00 
			
		
		
		
	feat: custom model selector
This commit is contained in:
		
							parent
							
								
									ff8a55a861
								
							
						
					
					
						commit
						b218b02d93
					
				@ -3,6 +3,7 @@
 | 
			
		||||
	import { models, showSettings, settings, user } from '$lib/stores';
 | 
			
		||||
	import { onMount, tick, getContext } from 'svelte';
 | 
			
		||||
	import { toast } from 'svelte-sonner';
 | 
			
		||||
	import Select from '../common/Select.svelte';
 | 
			
		||||
 | 
			
		||||
	const i18n = getContext('i18n');
 | 
			
		||||
 | 
			
		||||
@ -32,30 +33,24 @@
 | 
			
		||||
	}
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<div class="flex flex-col my-2">
 | 
			
		||||
<div class="flex flex-col my-2 w-full">
 | 
			
		||||
	{#each selectedModels as selectedModel, selectedModelIdx}
 | 
			
		||||
		<div class="flex">
 | 
			
		||||
			<select
 | 
			
		||||
				id="models"
 | 
			
		||||
				class="outline-none bg-transparent text-lg font-semibold rounded-lg block w-full placeholder-gray-400"
 | 
			
		||||
		<div class="flex w-full">
 | 
			
		||||
			<div class="overflow-hidden w-full">
 | 
			
		||||
				<div class="mr-2 max-w-full">
 | 
			
		||||
					<Select
 | 
			
		||||
						placeholder={$i18n.t('Select a model')}
 | 
			
		||||
						items={$models
 | 
			
		||||
							.filter((model) => model.name !== 'hr')
 | 
			
		||||
							.map((model) => ({
 | 
			
		||||
								value: model.id,
 | 
			
		||||
								label:
 | 
			
		||||
									model.name + `${model.size ? ` (${(model.size / 1024 ** 3).toFixed(1)}GB)` : ''}`
 | 
			
		||||
							}))}
 | 
			
		||||
						bind:value={selectedModel}
 | 
			
		||||
				{disabled}
 | 
			
		||||
			>
 | 
			
		||||
				<option class=" text-gray-700" value="" selected disabled
 | 
			
		||||
					>{$i18n.t('Select a model')}</option
 | 
			
		||||
				>
 | 
			
		||||
 | 
			
		||||
				{#each $models as model}
 | 
			
		||||
					{#if model.name === 'hr'}
 | 
			
		||||
						<hr />
 | 
			
		||||
					{:else}
 | 
			
		||||
						<option value={model.id} class="text-gray-700 text-lg"
 | 
			
		||||
							>{model.name +
 | 
			
		||||
								`${model.size ? ` (${(model.size / 1024 ** 3).toFixed(1)}GB)` : ''}`}</option
 | 
			
		||||
						>
 | 
			
		||||
					{/if}
 | 
			
		||||
				{/each}
 | 
			
		||||
			</select>
 | 
			
		||||
					/>
 | 
			
		||||
				</div>
 | 
			
		||||
			</div>
 | 
			
		||||
 | 
			
		||||
			{#if selectedModelIdx === 0}
 | 
			
		||||
				<button
 | 
			
		||||
@ -136,6 +131,6 @@
 | 
			
		||||
	{/each}
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
<div class="text-left mt-1.5 text-xs text-gray-500">
 | 
			
		||||
<div class="text-left mt-1.5 ml-1 text-xs text-gray-500">
 | 
			
		||||
	<button on:click={saveDefaultModel}> {$i18n.t('Set as default')}</button>
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										84
									
								
								src/lib/components/common/Select.svelte
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								src/lib/components/common/Select.svelte
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,84 @@
 | 
			
		||||
<script lang="ts">
 | 
			
		||||
	import { Select } from 'bits-ui';
 | 
			
		||||
 | 
			
		||||
	import { flyAndScale } from '$lib/utils/transitions';
 | 
			
		||||
 | 
			
		||||
	import { createEventDispatcher } from 'svelte';
 | 
			
		||||
	import ChevronDown from '../icons/ChevronDown.svelte';
 | 
			
		||||
	import Check from '../icons/Check.svelte';
 | 
			
		||||
	import Search from '../icons/Search.svelte';
 | 
			
		||||
 | 
			
		||||
	const dispatch = createEventDispatcher();
 | 
			
		||||
 | 
			
		||||
	export let value = '';
 | 
			
		||||
	export let placeholder = 'Select a model';
 | 
			
		||||
	export let items = [
 | 
			
		||||
		{ value: 'mango', label: 'Mango' },
 | 
			
		||||
		{ value: 'watermelon', label: 'Watermelon' },
 | 
			
		||||
		{ value: 'apple', label: 'Apple' },
 | 
			
		||||
		{ value: 'pineapple', label: 'Pineapple' },
 | 
			
		||||
		{ value: 'orange', label: 'Orange' }
 | 
			
		||||
	];
 | 
			
		||||
 | 
			
		||||
	let searchValue = '';
 | 
			
		||||
 | 
			
		||||
	$: filteredItems = searchValue
 | 
			
		||||
		? items.filter((item) => item.value.includes(searchValue.toLowerCase()))
 | 
			
		||||
		: items;
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<Select.Root
 | 
			
		||||
	{items}
 | 
			
		||||
	onOpenChange={() => {
 | 
			
		||||
		searchValue = '';
 | 
			
		||||
	}}
 | 
			
		||||
	selected={items.find((item) => item.value === value)}
 | 
			
		||||
	onSelectedChange={(selectedItem) => {
 | 
			
		||||
		value = selectedItem.value;
 | 
			
		||||
	}}
 | 
			
		||||
>
 | 
			
		||||
	<Select.Trigger class="relative w-full" aria-label={placeholder}>
 | 
			
		||||
		<Select.Value
 | 
			
		||||
			class="inline-flex h-input px-0.5 w-full outline-none bg-transparent truncate text-lg font-semibold placeholder-gray-400  focus:outline-none"
 | 
			
		||||
			{placeholder}
 | 
			
		||||
		/>
 | 
			
		||||
		<ChevronDown className="absolute end-2 top-1/2 -translate-y-[45%] size-3.5" strokeWidth="2.5" />
 | 
			
		||||
	</Select.Trigger>
 | 
			
		||||
	<Select.Content
 | 
			
		||||
		class="w-full rounded-lg  bg-white dark:bg-gray-900 dark:text-white shadow-lg border border-gray-300/30 dark:border-gray-700/50  outline-none"
 | 
			
		||||
		transition={flyAndScale}
 | 
			
		||||
		sideOffset={4}
 | 
			
		||||
	>
 | 
			
		||||
		<div class="flex items-center gap-2.5 px-5 mt-3.5 mb-3">
 | 
			
		||||
			<Search className="size-4" strokeWidth="2.5" />
 | 
			
		||||
 | 
			
		||||
			<input
 | 
			
		||||
				bind:value={searchValue}
 | 
			
		||||
				class="w-full text-sm bg-transparent outline-none"
 | 
			
		||||
				placeholder="Search a model"
 | 
			
		||||
			/>
 | 
			
		||||
		</div>
 | 
			
		||||
 | 
			
		||||
		<hr class="border-gray-100 dark:border-gray-800" />
 | 
			
		||||
 | 
			
		||||
		<div class="px-3 my-2 max-h-80 overflow-y-auto">
 | 
			
		||||
			{#each filteredItems as item}
 | 
			
		||||
				<Select.Item
 | 
			
		||||
					class="flex w-full font-medium line-clamp-1 select-none items-center rounded-button py-2 pl-3 pr-1.5 text-sm  text-gray-700 dark:text-gray-100  outline-none transition-all duration-75 data-[highlighted]:bg-muted"
 | 
			
		||||
					value={item.value}
 | 
			
		||||
					label={item.label}
 | 
			
		||||
				>
 | 
			
		||||
					{item.label}
 | 
			
		||||
					<Select.ItemIndicator class="ml-auto" asChild={false}>
 | 
			
		||||
						<Check />
 | 
			
		||||
					</Select.ItemIndicator>
 | 
			
		||||
				</Select.Item>
 | 
			
		||||
			{:else}
 | 
			
		||||
				<span class="block px-5 py-2 text-sm text-gray-700 dark:text-gray-100">
 | 
			
		||||
					No results found
 | 
			
		||||
				</span>
 | 
			
		||||
			{/each}
 | 
			
		||||
		</div>
 | 
			
		||||
	</Select.Content>
 | 
			
		||||
	<Select.Input name="favoriteFruit" />
 | 
			
		||||
</Select.Root>
 | 
			
		||||
							
								
								
									
										15
									
								
								src/lib/components/icons/Check.svelte
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/lib/components/icons/Check.svelte
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
			
		||||
<script lang="ts">
 | 
			
		||||
	export let className = 'w-4 h-4';
 | 
			
		||||
	export let strokeWidth = '1.5';
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<svg
 | 
			
		||||
	xmlns="http://www.w3.org/2000/svg"
 | 
			
		||||
	fill="none"
 | 
			
		||||
	viewBox="0 0 24 24"
 | 
			
		||||
	stroke-width={strokeWidth}
 | 
			
		||||
	stroke="currentColor"
 | 
			
		||||
	class={className}
 | 
			
		||||
>
 | 
			
		||||
	<path stroke-linecap="round" stroke-linejoin="round" d="m4.5 12.75 6 6 9-13.5" />
 | 
			
		||||
</svg>
 | 
			
		||||
							
								
								
									
										15
									
								
								src/lib/components/icons/ChevronDown.svelte
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/lib/components/icons/ChevronDown.svelte
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
			
		||||
<script lang="ts">
 | 
			
		||||
	export let className = 'w-4 h-4';
 | 
			
		||||
	export let strokeWidth = '1.5';
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<svg
 | 
			
		||||
	xmlns="http://www.w3.org/2000/svg"
 | 
			
		||||
	fill="none"
 | 
			
		||||
	viewBox="0 0 24 24"
 | 
			
		||||
	stroke-width={strokeWidth}
 | 
			
		||||
	stroke="currentColor"
 | 
			
		||||
	class={className}
 | 
			
		||||
>
 | 
			
		||||
	<path stroke-linecap="round" stroke-linejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" />
 | 
			
		||||
</svg>
 | 
			
		||||
							
								
								
									
										19
									
								
								src/lib/components/icons/Search.svelte
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/lib/components/icons/Search.svelte
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
<script lang="ts">
 | 
			
		||||
	export let className = 'w-4 h-4';
 | 
			
		||||
	export let strokeWidth = '1.5';
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<svg
 | 
			
		||||
	xmlns="http://www.w3.org/2000/svg"
 | 
			
		||||
	fill="none"
 | 
			
		||||
	viewBox="0 0 24 24"
 | 
			
		||||
	stroke-width={strokeWidth}
 | 
			
		||||
	stroke="currentColor"
 | 
			
		||||
	class={className}
 | 
			
		||||
>
 | 
			
		||||
	<path
 | 
			
		||||
		stroke-linecap="round"
 | 
			
		||||
		stroke-linejoin="round"
 | 
			
		||||
		d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z"
 | 
			
		||||
	/>
 | 
			
		||||
</svg>
 | 
			
		||||
							
								
								
									
										48
									
								
								src/lib/utils/transitions/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								src/lib/utils/transitions/index.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,48 @@
 | 
			
		||||
import { cubicOut } from 'svelte/easing';
 | 
			
		||||
import type { TransitionConfig } from 'svelte/transition';
 | 
			
		||||
 | 
			
		||||
type FlyAndScaleParams = {
 | 
			
		||||
	y?: number;
 | 
			
		||||
	start?: number;
 | 
			
		||||
	duration?: number;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const defaultFlyAndScaleParams = { y: -8, start: 0.95, duration: 200 };
 | 
			
		||||
 | 
			
		||||
export const flyAndScale = (node: Element, params?: FlyAndScaleParams): TransitionConfig => {
 | 
			
		||||
	const style = getComputedStyle(node);
 | 
			
		||||
	const transform = style.transform === 'none' ? '' : style.transform;
 | 
			
		||||
	const withDefaults = { ...defaultFlyAndScaleParams, ...params };
 | 
			
		||||
 | 
			
		||||
	const scaleConversion = (valueA: number, scaleA: [number, number], scaleB: [number, number]) => {
 | 
			
		||||
		const [minA, maxA] = scaleA;
 | 
			
		||||
		const [minB, maxB] = scaleB;
 | 
			
		||||
 | 
			
		||||
		const percentage = (valueA - minA) / (maxA - minA);
 | 
			
		||||
		const valueB = percentage * (maxB - minB) + minB;
 | 
			
		||||
 | 
			
		||||
		return valueB;
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const styleToString = (style: Record<string, number | string | undefined>): string => {
 | 
			
		||||
		return Object.keys(style).reduce((str, key) => {
 | 
			
		||||
			if (style[key] === undefined) return str;
 | 
			
		||||
			return str + `${key}:${style[key]};`;
 | 
			
		||||
		}, '');
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	return {
 | 
			
		||||
		duration: withDefaults.duration ?? 200,
 | 
			
		||||
		delay: 0,
 | 
			
		||||
		css: (t) => {
 | 
			
		||||
			const y = scaleConversion(t, [0, 1], [withDefaults.y, 0]);
 | 
			
		||||
			const scale = scaleConversion(t, [0, 1], [withDefaults.start, 1]);
 | 
			
		||||
 | 
			
		||||
			return styleToString({
 | 
			
		||||
				transform: `${transform} translate3d(0, ${y}px, 0) scale(${scale})`,
 | 
			
		||||
				opacity: t
 | 
			
		||||
			});
 | 
			
		||||
		},
 | 
			
		||||
		easing: cubicOut
 | 
			
		||||
	};
 | 
			
		||||
};
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user