ConfigureModelsModal.svelte 7.9 KB

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