ModelSelector.svelte 3.18 KB
Newer Older
1
<script lang="ts">
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
	import { Collapsible } from 'bits-ui';

4
5
	import { setDefaultModels } from '$lib/apis/configs';
	import { models, showSettings, settings, user } from '$lib/stores';
6
	import { onMount, tick, getContext } from 'svelte';
Jannik Streidl's avatar
Jannik Streidl committed
7
	import { toast } from 'svelte-sonner';
8
	import Selector from './ModelSelector/Selector.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
9
	import Tooltip from '../common/Tooltip.svelte';
10

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
25
		settings.set({ ...$settings, models: selectedModels });
		localStorage.setItem('settings', JSON.stringify($settings));
26
27

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

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

Timothy J. Baek's avatar
Timothy J. Baek committed
41
<div class="flex flex-col mt-0.5 w-full">
42
	{#each selectedModels as selectedModel, selectedModelIdx}
43
		<div class="flex w-full max-w-fit">
Timothy J. Baek's avatar
Timothy J. Baek committed
44
			<div class="overflow-hidden w-full">
45
				<div class="mr-1 max-w-full">
46
					<Selector
Timothy J. Baek's avatar
Timothy J. Baek committed
47
48
49
50
51
						placeholder={$i18n.t('Select a model')}
						items={$models
							.filter((model) => model.name !== 'hr')
							.map((model) => ({
								value: model.id,
52
								label: model.name,
Timothy J. Baek's avatar
Timothy J. Baek committed
53
								info: model
Timothy J. Baek's avatar
Timothy J. Baek committed
54
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
85
86
87
88
89
90
91
92
93
94
95
96
				<div class="  self-center disabled:text-gray-600 disabled:hover:text-gray-600 mr-2">
					<Tooltip content="Remove Model">
						<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>

111
112
113
114
115
{#if showSetDefault}
	<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}