ConfigureModelsModal.svelte 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <script>
  2. import { toast } from 'svelte-sonner';
  3. import { createEventDispatcher, getContext, onMount } from 'svelte';
  4. const i18n = getContext('i18n');
  5. const dispatch = createEventDispatcher();
  6. import { models } from '$lib/stores';
  7. import { deleteAllModels } from '$lib/apis/models';
  8. import Modal from '$lib/components/common/Modal.svelte';
  9. import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  10. import Tooltip from '$lib/components/common/Tooltip.svelte';
  11. import ModelList from './ModelList.svelte';
  12. import { getModelsConfig, setModelsConfig } from '$lib/apis/configs';
  13. import Spinner from '$lib/components/common/Spinner.svelte';
  14. import Minus from '$lib/components/icons/Minus.svelte';
  15. import Plus from '$lib/components/icons/Plus.svelte';
  16. import ChevronUp from '$lib/components/icons/ChevronUp.svelte';
  17. import ChevronDown from '$lib/components/icons/ChevronDown.svelte';
  18. export let show = false;
  19. export let initHandler = () => {};
  20. let config = null;
  21. let selectedModelId = '';
  22. let defaultModelIds = [];
  23. let modelIds = [];
  24. let sortKey = '';
  25. let sortOrder = '';
  26. let loading = false;
  27. let showResetModal = false;
  28. $: if (show) {
  29. init();
  30. }
  31. $: if (selectedModelId) {
  32. onModelSelect();
  33. }
  34. const onModelSelect = () => {
  35. if (selectedModelId === '') {
  36. return;
  37. }
  38. if (defaultModelIds.includes(selectedModelId)) {
  39. selectedModelId = '';
  40. return;
  41. }
  42. defaultModelIds = [...defaultModelIds, selectedModelId];
  43. selectedModelId = '';
  44. };
  45. const init = async () => {
  46. config = await getModelsConfig(localStorage.token);
  47. if (config?.DEFAULT_MODELS) {
  48. defaultModelIds = (config?.DEFAULT_MODELS).split(',').filter((id) => id);
  49. } else {
  50. defaultModelIds = [];
  51. }
  52. const modelOrderList = config.MODEL_ORDER_LIST || [];
  53. const allModelIds = $models.map((model) => model.id);
  54. // Create a Set for quick lookup of ordered IDs
  55. const orderedSet = new Set(modelOrderList);
  56. modelIds = [
  57. // Add all IDs from MODEL_ORDER_LIST that exist in allModelIds
  58. ...modelOrderList.filter((id) => orderedSet.has(id) && allModelIds.includes(id)),
  59. // Add remaining IDs not in MODEL_ORDER_LIST, sorted alphabetically
  60. ...allModelIds.filter((id) => !orderedSet.has(id)).sort((a, b) => a.localeCompare(b))
  61. ];
  62. sortKey = '';
  63. sortOrder = '';
  64. };
  65. const submitHandler = async () => {
  66. loading = true;
  67. const res = await setModelsConfig(localStorage.token, {
  68. DEFAULT_MODELS: defaultModelIds.join(','),
  69. MODEL_ORDER_LIST: modelIds
  70. });
  71. if (res) {
  72. toast.success($i18n.t('Models configuration saved successfully'));
  73. initHandler();
  74. show = false;
  75. } else {
  76. toast.error($i18n.t('Failed to save models configuration'));
  77. }
  78. loading = false;
  79. };
  80. onMount(async () => {
  81. init();
  82. });
  83. </script>
  84. <ConfirmDialog
  85. title={$i18n.t('Reset All Models')}
  86. message={$i18n.t('This will delete all models including custom models and cannot be undone.')}
  87. bind:show={showResetModal}
  88. onConfirm={async () => {
  89. const res = deleteAllModels(localStorage.token);
  90. if (res) {
  91. toast.success($i18n.t('All models deleted successfully'));
  92. initHandler();
  93. }
  94. }}
  95. />
  96. <Modal size="sm" bind:show>
  97. <div>
  98. <div class=" flex justify-between dark:text-gray-100 px-5 pt-4 pb-2">
  99. <div class=" text-lg font-medium self-center font-primary">
  100. {$i18n.t('Settings')}
  101. </div>
  102. <button
  103. class="self-center"
  104. on:click={() => {
  105. show = false;
  106. }}
  107. >
  108. <svg
  109. xmlns="http://www.w3.org/2000/svg"
  110. viewBox="0 0 20 20"
  111. fill="currentColor"
  112. class="w-5 h-5"
  113. >
  114. <path
  115. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  116. />
  117. </svg>
  118. </button>
  119. </div>
  120. <div class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 dark:text-gray-200">
  121. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  122. {#if config}
  123. <form
  124. class="flex flex-col w-full"
  125. on:submit|preventDefault={() => {
  126. submitHandler();
  127. }}
  128. >
  129. <div>
  130. <div class="flex flex-col w-full">
  131. <button
  132. class="mb-1 flex gap-2"
  133. type="button"
  134. on:click={() => {
  135. sortKey = 'model';
  136. if (sortOrder === 'asc') {
  137. sortOrder = 'desc';
  138. } else {
  139. sortOrder = 'asc';
  140. }
  141. modelIds = modelIds
  142. .filter((id) => id !== '')
  143. .sort((a, b) => {
  144. const nameA = $models.find((model) => model.id === a)?.name || a;
  145. const nameB = $models.find((model) => model.id === b)?.name || b;
  146. return sortOrder === 'desc'
  147. ? nameA.localeCompare(nameB)
  148. : nameB.localeCompare(nameA);
  149. });
  150. }}
  151. >
  152. <div class="text-xs text-gray-500">{$i18n.t('Reorder Models')}</div>
  153. {#if sortKey === 'model'}
  154. <span class="font-normal self-center">
  155. {#if sortOrder === 'asc'}
  156. <ChevronUp className="size-3" />
  157. {:else}
  158. <ChevronDown className="size-3" />
  159. {/if}
  160. </span>
  161. {:else}
  162. <span class="invisible">
  163. <ChevronUp className="size-3" />
  164. </span>
  165. {/if}
  166. </button>
  167. <ModelList bind:modelIds />
  168. </div>
  169. </div>
  170. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  171. <div>
  172. <div class="flex flex-col w-full">
  173. <div class="mb-1 flex justify-between">
  174. <div class="text-xs text-gray-500">{$i18n.t('Default Models')}</div>
  175. </div>
  176. <div class="flex items-center -mr-1">
  177. <select
  178. class="w-full py-1 text-sm rounded-lg bg-transparent {selectedModelId
  179. ? ''
  180. : 'text-gray-500'} placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden"
  181. bind:value={selectedModelId}
  182. >
  183. <option value="">{$i18n.t('Select a model')}</option>
  184. {#each $models as model}
  185. <option value={model.id} class="bg-gray-50 dark:bg-gray-700"
  186. >{model.name}</option
  187. >
  188. {/each}
  189. </select>
  190. </div>
  191. <!-- <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" /> -->
  192. {#if defaultModelIds.length > 0}
  193. <div class="flex flex-col">
  194. {#each defaultModelIds as modelId, modelIdx}
  195. <div class=" flex gap-2 w-full justify-between items-center">
  196. <div class=" text-sm flex-1 py-1 rounded-lg">
  197. {$models.find((model) => model.id === modelId)?.name}
  198. </div>
  199. <div class="shrink-0">
  200. <button
  201. type="button"
  202. on:click={() => {
  203. defaultModelIds = defaultModelIds.filter(
  204. (_, idx) => idx !== modelIdx
  205. );
  206. }}
  207. >
  208. <Minus strokeWidth="2" className="size-3.5" />
  209. </button>
  210. </div>
  211. </div>
  212. {/each}
  213. </div>
  214. {:else}
  215. <div class="text-gray-500 text-xs text-center py-2">
  216. {$i18n.t('No models selected')}
  217. </div>
  218. {/if}
  219. </div>
  220. </div>
  221. <div class="flex justify-between pt-3 text-sm font-medium gap-1.5">
  222. <Tooltip content={$i18n.t('This will delete all models including custom models')}>
  223. <button
  224. class="px-3.5 py-1.5 text-sm font-medium dark:bg-black dark:hover:bg-gray-950 dark:text-white bg-white text-black hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
  225. type="button"
  226. on:click={() => {
  227. showResetModal = true;
  228. }}
  229. >
  230. <!-- {$i18n.t('Delete All Models')} -->
  231. {$i18n.t('Reset All Models')}
  232. </button>
  233. </Tooltip>
  234. <button
  235. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center {loading
  236. ? ' cursor-not-allowed'
  237. : ''}"
  238. type="submit"
  239. disabled={loading}
  240. >
  241. {$i18n.t('Save')}
  242. {#if loading}
  243. <div class="ml-2 self-center">
  244. <svg
  245. class=" w-4 h-4"
  246. viewBox="0 0 24 24"
  247. fill="currentColor"
  248. xmlns="http://www.w3.org/2000/svg"
  249. ><style>
  250. .spinner_ajPY {
  251. transform-origin: center;
  252. animation: spinner_AtaB 0.75s infinite linear;
  253. }
  254. @keyframes spinner_AtaB {
  255. 100% {
  256. transform: rotate(360deg);
  257. }
  258. }
  259. </style><path
  260. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  261. opacity=".25"
  262. /><path
  263. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  264. class="spinner_ajPY"
  265. /></svg
  266. >
  267. </div>
  268. {/if}
  269. </button>
  270. </div>
  271. </form>
  272. {:else}
  273. <div>
  274. <Spinner />
  275. </div>
  276. {/if}
  277. </div>
  278. </div>
  279. </div>
  280. </Modal>