ModelSelector.svelte 3.03 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
		await updateUserSettings(localStorage.token, { ui: $settings });
26

27
		toast.success($i18n.t('Default model updated'));
28
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
29
30
31

	$: if (selectedModels.length > 0 && $models.length > 0) {
		selectedModels = selectedModels.map((model) =>
32
			$models.map((m) => m.id).includes(model) ? model : ''
Timothy J. Baek's avatar
Timothy J. Baek committed
33
34
		);
	}
35
36
</script>

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

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

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