ConfigureModelsModal.svelte 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. export let show = false;
  17. export let initHandler = () => {};
  18. let config = null;
  19. let selectedModelId = '';
  20. let defaultModelIds = [];
  21. let modelIds = [];
  22. let loading = false;
  23. let showResetModal = false;
  24. $: if (show) {
  25. init();
  26. }
  27. $: if (selectedModelId) {
  28. onModelSelect();
  29. }
  30. const onModelSelect = () => {
  31. if (selectedModelId === '') {
  32. return;
  33. }
  34. if (defaultModelIds.includes(selectedModelId)) {
  35. selectedModelId = '';
  36. return;
  37. }
  38. defaultModelIds = [...defaultModelIds, selectedModelId];
  39. selectedModelId = '';
  40. };
  41. const init = async () => {
  42. config = await getModelsConfig(localStorage.token);
  43. if (config?.DEFAULT_MODELS) {
  44. defaultModelIds = (config?.DEFAULT_MODELS).split(',').filter((id) => id);
  45. } else {
  46. defaultModelIds = [];
  47. }
  48. const modelOrderList = config.MODEL_ORDER_LIST || [];
  49. const allModelIds = $models.map((model) => model.id);
  50. // Create a Set for quick lookup of ordered IDs
  51. const orderedSet = new Set(modelOrderList);
  52. modelIds = [
  53. // Add all IDs from MODEL_ORDER_LIST that exist in allModelIds
  54. ...modelOrderList.filter((id) => orderedSet.has(id) && allModelIds.includes(id)),
  55. // Add remaining IDs not in MODEL_ORDER_LIST, sorted alphabetically
  56. ...allModelIds.filter((id) => !orderedSet.has(id)).sort((a, b) => a.localeCompare(b))
  57. ];
  58. };
  59. const submitHandler = async () => {
  60. loading = true;
  61. const res = await setModelsConfig(localStorage.token, {
  62. DEFAULT_MODELS: defaultModelIds.join(','),
  63. MODEL_ORDER_LIST: modelIds
  64. });
  65. if (res) {
  66. toast.success($i18n.t('Models configuration saved successfully'));
  67. initHandler();
  68. show = false;
  69. } else {
  70. toast.error($i18n.t('Failed to save models configuration'));
  71. }
  72. loading = false;
  73. };
  74. onMount(async () => {
  75. init();
  76. });
  77. </script>
  78. <ConfirmDialog
  79. title={$i18n.t('Reset All Models')}
  80. message={$i18n.t('This will delete all models including custom models and cannot be undone.')}
  81. bind:show={showResetModal}
  82. onConfirm={async () => {
  83. const res = deleteAllModels(localStorage.token);
  84. if (res) {
  85. toast.success($i18n.t('All models deleted successfully'));
  86. initHandler();
  87. }
  88. }}
  89. />
  90. <Modal size="sm" bind:show>
  91. <div>
  92. <div class=" flex justify-between dark:text-gray-100 px-5 pt-4 pb-2">
  93. <div class=" text-lg font-medium self-center font-primary">
  94. {$i18n.t('Settings')}
  95. </div>
  96. <button
  97. class="self-center"
  98. on:click={() => {
  99. show = false;
  100. }}
  101. >
  102. <svg
  103. xmlns="http://www.w3.org/2000/svg"
  104. viewBox="0 0 20 20"
  105. fill="currentColor"
  106. class="w-5 h-5"
  107. >
  108. <path
  109. 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"
  110. />
  111. </svg>
  112. </button>
  113. </div>
  114. <div class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 dark:text-gray-200">
  115. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  116. {#if config}
  117. <form
  118. class="flex flex-col w-full"
  119. on:submit|preventDefault={() => {
  120. submitHandler();
  121. }}
  122. >
  123. <div>
  124. <div class="flex flex-col w-full">
  125. <div class="mb-1 flex justify-between">
  126. <div class="text-xs text-gray-500">{$i18n.t('Reorder Models')}</div>
  127. </div>
  128. <ModelList bind:modelIds />
  129. </div>
  130. </div>
  131. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  132. <div>
  133. <div class="flex flex-col w-full">
  134. <div class="mb-1 flex justify-between">
  135. <div class="text-xs text-gray-500">{$i18n.t('Default Models')}</div>
  136. </div>
  137. <div class="flex items-center -mr-1">
  138. <select
  139. class="w-full py-1 text-sm rounded-lg bg-transparent {selectedModelId
  140. ? ''
  141. : 'text-gray-500'} placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden"
  142. bind:value={selectedModelId}
  143. >
  144. <option value="">{$i18n.t('Select a model')}</option>
  145. {#each $models as model}
  146. <option value={model.id} class="bg-gray-50 dark:bg-gray-700"
  147. >{model.name}</option
  148. >
  149. {/each}
  150. </select>
  151. </div>
  152. <!-- <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" /> -->
  153. {#if defaultModelIds.length > 0}
  154. <div class="flex flex-col">
  155. {#each defaultModelIds as modelId, modelIdx}
  156. <div class=" flex gap-2 w-full justify-between items-center">
  157. <div class=" text-sm flex-1 py-1 rounded-lg">
  158. {$models.find((model) => model.id === modelId)?.name}
  159. </div>
  160. <div class="shrink-0">
  161. <button
  162. type="button"
  163. on:click={() => {
  164. defaultModelIds = defaultModelIds.filter(
  165. (_, idx) => idx !== modelIdx
  166. );
  167. }}
  168. >
  169. <Minus strokeWidth="2" className="size-3.5" />
  170. </button>
  171. </div>
  172. </div>
  173. {/each}
  174. </div>
  175. {:else}
  176. <div class="text-gray-500 text-xs text-center py-2">
  177. {$i18n.t('No models selected')}
  178. </div>
  179. {/if}
  180. </div>
  181. </div>
  182. <div class="flex justify-between pt-3 text-sm font-medium gap-1.5">
  183. <Tooltip content={$i18n.t('This will delete all models including custom models')}>
  184. <button
  185. 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"
  186. type="button"
  187. on:click={() => {
  188. showResetModal = true;
  189. }}
  190. >
  191. <!-- {$i18n.t('Delete All Models')} -->
  192. {$i18n.t('Reset All Models')}
  193. </button>
  194. </Tooltip>
  195. <button
  196. 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
  197. ? ' cursor-not-allowed'
  198. : ''}"
  199. type="submit"
  200. disabled={loading}
  201. >
  202. {$i18n.t('Save')}
  203. {#if loading}
  204. <div class="ml-2 self-center">
  205. <svg
  206. class=" w-4 h-4"
  207. viewBox="0 0 24 24"
  208. fill="currentColor"
  209. xmlns="http://www.w3.org/2000/svg"
  210. ><style>
  211. .spinner_ajPY {
  212. transform-origin: center;
  213. animation: spinner_AtaB 0.75s infinite linear;
  214. }
  215. @keyframes spinner_AtaB {
  216. 100% {
  217. transform: rotate(360deg);
  218. }
  219. }
  220. </style><path
  221. 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"
  222. opacity=".25"
  223. /><path
  224. 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"
  225. class="spinner_ajPY"
  226. /></svg
  227. >
  228. </div>
  229. {/if}
  230. </button>
  231. </div>
  232. </form>
  233. {:else}
  234. <div>
  235. <Spinner />
  236. </div>
  237. {/if}
  238. </div>
  239. </div>
  240. </div>
  241. </Modal>