ConfigureModelsModal.svelte 7.7 KB

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