ModelSelector.svelte 3.26 KB
Newer Older
1
<script lang="ts">
Timothy J. Baek's avatar
Timothy J. Baek committed
2
	import { models, showSettings, settings, user, mobile } from '$lib/stores';
3
	import { onMount, tick, getContext } from 'svelte';
Jannik Streidl's avatar
Jannik Streidl committed
4
	import { toast } from 'svelte-sonner';
5
	import Selector from './ModelSelector/Selector.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
6
	import Tooltip from '../common/Tooltip.svelte';
7

8
9
10
	import { setDefaultModels } from '$lib/apis/configs';
	import { updateUserSettings } from '$lib/apis/users';

11
12
	const i18n = getContext('i18n');

13
14
15
	export let selectedModels = [''];
	export let disabled = false;

16
17
	export let showSetDefault = true;

18
	const saveDefaultModel = async () => {
19
20
		const hasEmptyModel = selectedModels.filter((it) => it === '');
		if (hasEmptyModel.length) {
21
			toast.error($i18n.t('Choose a model before saving...'));
22
23
			return;
		}
24
		settings.set({ ...$settings, models: selectedModels });
25

26
		localStorage.setItem('settings', JSON.stringify($settings));
27
		await updateUserSettings(localStorage.token, { ui: $settings });
28
29

		if ($user.role === 'admin') {
Timothy J. Baek's avatar
Timothy J. Baek committed
30
			console.log('setting default models globally');
31
32
			await setDefaultModels(localStorage.token, selectedModels.join(','));
		}
33
		toast.success($i18n.t('Default model updated'));
34
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
35
36
37

	$: if (selectedModels.length > 0 && $models.length > 0) {
		selectedModels = selectedModels.map((model) =>
38
			$models.map((m) => m.id).includes(model) ? model : ''
Timothy J. Baek's avatar
Timothy J. Baek committed
39
40
		);
	}
41
42
</script>

Timothy J. Baek's avatar
Timothy J. Baek committed
43
<div class="flex flex-col w-full items-center md:items-start">
44
	{#each selectedModels as selectedModel, selectedModelIdx}
45
		<div class="flex w-full max-w-fit">
Timothy J. Baek's avatar
Timothy J. Baek committed
46
			<div class="overflow-hidden w-full">
47
				<div class="mr-1 max-w-full">
48
					<Selector
Timothy J. Baek's avatar
Timothy J. Baek committed
49
						placeholder={$i18n.t('Select a model')}
50
51
52
53
54
						items={$models.map((model) => ({
							value: model.id,
							label: model.name,
							model: model
						}))}
Timothy J. Baek's avatar
Timothy J. Baek committed
55
56
57
58
						bind:value={selectedModel}
					/>
				</div>
			</div>
59
60

			{#if selectedModelIdx === 0}
Timothy J. Baek's avatar
Timothy J. Baek committed
61
				<div class="  self-center mr-2 disabled:text-gray-600 disabled:hover:text-gray-600">
62
					<Tooltip content={$i18n.t('Add Model')}>
Timothy J. Baek's avatar
Timothy J. Baek committed
63
64
65
66
67
68
69
70
71
72
73
						<button
							class=" "
							{disabled}
							on:click={() => {
								selectedModels = [...selectedModels, ''];
							}}
						>
							<svg
								xmlns="http://www.w3.org/2000/svg"
								fill="none"
								viewBox="0 0 24 24"
74
								stroke-width="2"
Timothy J. Baek's avatar
Timothy J. Baek committed
75
								stroke="currentColor"
76
								class="size-3.5"
Timothy J. Baek's avatar
Timothy J. Baek committed
77
78
79
80
81
82
							>
								<path stroke-linecap="round" stroke-linejoin="round" d="M12 6v12m6-6H6" />
							</svg>
						</button>
					</Tooltip>
				</div>
83
			{:else}
Timothy J. Baek's avatar
Timothy J. Baek committed
84
				<div class="  self-center disabled:text-gray-600 disabled:hover:text-gray-600 mr-2">
85
					<Tooltip content={$i18n.t('Remove Model')}>
Timothy J. Baek's avatar
Timothy J. Baek committed
86
87
88
89
90
91
92
93
94
95
96
						<button
							{disabled}
							on:click={() => {
								selectedModels.splice(selectedModelIdx, 1);
								selectedModels = selectedModels;
							}}
						>
							<svg
								xmlns="http://www.w3.org/2000/svg"
								fill="none"
								viewBox="0 0 24 24"
97
								stroke-width="2"
Timothy J. Baek's avatar
Timothy J. Baek committed
98
								stroke="currentColor"
99
								class="size-3.5"
Timothy J. Baek's avatar
Timothy J. Baek committed
100
101
102
103
104
105
							>
								<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 12h-15" />
							</svg>
						</button>
					</Tooltip>
				</div>
106
107
108
109
110
			{/if}
		</div>
	{/each}
</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
111
{#if showSetDefault && !$mobile}
112
113
114
115
	<div class="text-left mt-0.5 ml-1 text-[0.7rem] text-gray-500">
		<button on:click={saveDefaultModel}> {$i18n.t('Set as default')}</button>
	</div>
{/if}