2023-11-20 01:47:07 +00:00
|
|
|
<script lang="ts">
|
2024-08-19 14:49:40 +00:00
|
|
|
import { models, showSettings, settings, user, mobile, config } from '$lib/stores';
|
2024-03-01 04:40:36 +00:00
|
|
|
import { onMount, tick, getContext } from 'svelte';
|
2024-03-01 09:18:07 +00:00
|
|
|
import { toast } from 'svelte-sonner';
|
2024-03-25 06:03:26 +00:00
|
|
|
import Selector from './ModelSelector/Selector.svelte';
|
2024-03-30 00:20:07 +00:00
|
|
|
import Tooltip from '../common/Tooltip.svelte';
|
2023-11-20 01:47:07 +00:00
|
|
|
|
2024-05-27 05:47:42 +00:00
|
|
|
import { setDefaultModels } from '$lib/apis/configs';
|
|
|
|
import { updateUserSettings } from '$lib/apis/users';
|
|
|
|
|
2024-03-01 04:40:36 +00:00
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
2023-11-20 01:47:07 +00:00
|
|
|
export let selectedModels = [''];
|
|
|
|
export let disabled = false;
|
|
|
|
|
2024-05-02 08:15:58 +00:00
|
|
|
export let showSetDefault = true;
|
|
|
|
|
2024-01-03 01:26:19 +00:00
|
|
|
const saveDefaultModel = async () => {
|
2023-12-30 07:35:08 +00:00
|
|
|
const hasEmptyModel = selectedModels.filter((it) => it === '');
|
|
|
|
if (hasEmptyModel.length) {
|
2024-03-02 20:38:51 +00:00
|
|
|
toast.error($i18n.t('Choose a model before saving...'));
|
2023-12-28 16:06:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-20 01:47:07 +00:00
|
|
|
settings.set({ ...$settings, models: selectedModels });
|
2024-05-27 05:47:42 +00:00
|
|
|
await updateUserSettings(localStorage.token, { ui: $settings });
|
2024-01-03 01:26:19 +00:00
|
|
|
|
2024-03-02 20:38:51 +00:00
|
|
|
toast.success($i18n.t('Default model updated'));
|
2023-11-20 01:47:07 +00:00
|
|
|
};
|
2024-01-03 00:06:11 +00:00
|
|
|
|
|
|
|
$: if (selectedModels.length > 0 && $models.length > 0) {
|
|
|
|
selectedModels = selectedModels.map((model) =>
|
2024-02-22 12:12:26 +00:00
|
|
|
$models.map((m) => m.id).includes(model) ? model : ''
|
2024-01-03 00:06:11 +00:00
|
|
|
);
|
|
|
|
}
|
2023-11-20 01:47:07 +00:00
|
|
|
</script>
|
|
|
|
|
2024-07-08 23:55:12 +00:00
|
|
|
<div class="flex flex-col w-full items-start">
|
2023-11-20 01:47:07 +00:00
|
|
|
{#each selectedModels as selectedModel, selectedModelIdx}
|
2024-05-02 19:33:04 +00:00
|
|
|
<div class="flex w-full max-w-fit">
|
2024-03-24 22:28:36 +00:00
|
|
|
<div class="overflow-hidden w-full">
|
2024-05-02 19:33:04 +00:00
|
|
|
<div class="mr-1 max-w-full">
|
2024-03-25 06:03:26 +00:00
|
|
|
<Selector
|
2024-03-24 22:28:36 +00:00
|
|
|
placeholder={$i18n.t('Select a model')}
|
2024-05-24 10:02:56 +00:00
|
|
|
items={$models.map((model) => ({
|
|
|
|
value: model.id,
|
|
|
|
label: model.name,
|
|
|
|
model: model
|
|
|
|
}))}
|
2024-08-19 14:49:40 +00:00
|
|
|
showTemporaryChatControl={$user.role === 'user'
|
|
|
|
? ($config?.permissions?.chat?.temporary ?? true)
|
|
|
|
: true}
|
2024-03-24 22:28:36 +00:00
|
|
|
bind:value={selectedModel}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-11-20 01:47:07 +00:00
|
|
|
|
|
|
|
{#if selectedModelIdx === 0}
|
2024-03-30 00:20:07 +00:00
|
|
|
<div class=" self-center mr-2 disabled:text-gray-600 disabled:hover:text-gray-600">
|
2024-04-30 08:46:16 +00:00
|
|
|
<Tooltip content={$i18n.t('Add Model')}>
|
2024-03-30 00:20:07 +00:00
|
|
|
<button
|
|
|
|
class=" "
|
|
|
|
{disabled}
|
|
|
|
on:click={() => {
|
|
|
|
selectedModels = [...selectedModels, ''];
|
|
|
|
}}
|
2024-08-25 21:43:28 +00:00
|
|
|
aria-label="Add Model"
|
2024-03-30 00:20:07 +00:00
|
|
|
>
|
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
2024-05-02 19:33:04 +00:00
|
|
|
stroke-width="2"
|
2024-03-30 00:20:07 +00:00
|
|
|
stroke="currentColor"
|
2024-05-02 19:33:04 +00:00
|
|
|
class="size-3.5"
|
2024-03-30 00:20:07 +00:00
|
|
|
>
|
|
|
|
<path stroke-linecap="round" stroke-linejoin="round" d="M12 6v12m6-6H6" />
|
|
|
|
</svg>
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2023-11-20 01:47:07 +00:00
|
|
|
{:else}
|
2024-03-30 00:20:07 +00:00
|
|
|
<div class=" self-center disabled:text-gray-600 disabled:hover:text-gray-600 mr-2">
|
2024-04-30 08:46:16 +00:00
|
|
|
<Tooltip content={$i18n.t('Remove Model')}>
|
2024-03-30 00:20:07 +00:00
|
|
|
<button
|
|
|
|
{disabled}
|
|
|
|
on:click={() => {
|
|
|
|
selectedModels.splice(selectedModelIdx, 1);
|
|
|
|
selectedModels = selectedModels;
|
|
|
|
}}
|
2024-08-25 21:43:28 +00:00
|
|
|
aria-label="Remove Model"
|
2024-03-30 00:20:07 +00:00
|
|
|
>
|
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
2024-05-02 19:33:04 +00:00
|
|
|
stroke-width="2"
|
2024-03-30 00:20:07 +00:00
|
|
|
stroke="currentColor"
|
2024-05-02 19:33:04 +00:00
|
|
|
class="size-3.5"
|
2024-03-30 00:20:07 +00:00
|
|
|
>
|
|
|
|
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 12h-15" />
|
|
|
|
</svg>
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2023-11-20 01:47:07 +00:00
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
|
2024-05-15 06:16:22 +00:00
|
|
|
{#if showSetDefault && !$mobile}
|
2024-09-05 15:03:09 +00:00
|
|
|
<div class="text-left mt-[1px] ml-1 text-[0.7rem] text-gray-500 font-primary">
|
2024-05-02 08:15:58 +00:00
|
|
|
<button on:click={saveDefaultModel}> {$i18n.t('Set as default')}</button>
|
|
|
|
</div>
|
|
|
|
{/if}
|