ModelSelector.svelte 3.1 KB
Newer Older
1
<script lang="ts">
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
9
10
11
	import Selector from './ModelSelector/Selector.svelte';
	import Tooltip from '../common/Tooltip.svelte';

	const i18n = getContext('i18n');
12
13
14
15

	export let selectedModels = [''];
	export let disabled = false;

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

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

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

39
<div class="flex flex-col mt-0.5 w-full">
40
	{#each selectedModels as selectedModel, selectedModelIdx}
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
		<div class="flex w-full">
			<div class="overflow-hidden w-full">
				<div class="mr-0.5 max-w-full">
					<Selector
						placeholder={$i18n.t('Select a model')}
						items={$models
							.filter((model) => model.name !== 'hr')
							.map((model) => ({
								value: model.id,
								label: model.name,
								info: model
							}))}
						bind:value={selectedModel}
					/>
				</div>
			</div>
57
58

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

109
110
<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>
111
</div>