AddConnectionModal.svelte 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { getContext, onMount } from 'svelte';
  4. const i18n = getContext('i18n');
  5. import { models } from '$lib/stores';
  6. import { verifyOpenAIConnection } from '$lib/apis/openai';
  7. import { verifyOllamaConnection } from '$lib/apis/ollama';
  8. import Modal from '$lib/components/common/Modal.svelte';
  9. import Plus from '$lib/components/icons/Plus.svelte';
  10. import Minus from '$lib/components/icons/Minus.svelte';
  11. import PencilSolid from '$lib/components/icons/PencilSolid.svelte';
  12. import SensitiveInput from '$lib/components/common/SensitiveInput.svelte';
  13. import Tooltip from '$lib/components/common/Tooltip.svelte';
  14. import Switch from '$lib/components/common/Switch.svelte';
  15. export let onSubmit: Function = () => {};
  16. export let onDelete: Function = () => {};
  17. export let show = false;
  18. export let edit = false;
  19. export let ollama = false;
  20. export let connection = null;
  21. let url = '';
  22. let key = '';
  23. let prefixId = '';
  24. let enable = true;
  25. let modelId = '';
  26. let modelIds = [];
  27. let loading = false;
  28. const verifyOllamaHandler = async () => {
  29. const res = await verifyOllamaConnection(localStorage.token, url, key).catch((error) => {
  30. toast.error(`${error}`);
  31. });
  32. if (res) {
  33. toast.success($i18n.t('Server connection verified'));
  34. }
  35. };
  36. const verifyOpenAIHandler = async () => {
  37. const res = await verifyOpenAIConnection(localStorage.token, url, key).catch((error) => {
  38. toast.error(`${error}`);
  39. });
  40. if (res) {
  41. toast.success($i18n.t('Server connection verified'));
  42. }
  43. };
  44. const verifyHandler = () => {
  45. if (ollama) {
  46. verifyOllamaHandler();
  47. } else {
  48. verifyOpenAIHandler();
  49. }
  50. };
  51. const addModelHandler = () => {
  52. if (modelId) {
  53. modelIds = [...modelIds, modelId];
  54. modelId = '';
  55. }
  56. };
  57. const submitHandler = async () => {
  58. loading = true;
  59. if (!ollama && (!url || !key)) {
  60. loading = false;
  61. toast.error('URL and Key are required');
  62. return;
  63. }
  64. const connection = {
  65. url,
  66. key,
  67. config: {
  68. enable: enable,
  69. prefix_id: prefixId,
  70. model_ids: modelIds
  71. }
  72. };
  73. await onSubmit(connection);
  74. loading = false;
  75. show = false;
  76. url = '';
  77. key = '';
  78. prefixId = '';
  79. modelIds = [];
  80. };
  81. const init = () => {
  82. if (connection) {
  83. url = connection.url;
  84. key = connection.key;
  85. enable = connection.config?.enable ?? true;
  86. prefixId = connection.config?.prefix_id ?? '';
  87. modelIds = connection.config?.model_ids ?? [];
  88. }
  89. };
  90. $: if (show) {
  91. init();
  92. }
  93. onMount(() => {
  94. init();
  95. });
  96. </script>
  97. <Modal size="sm" bind:show>
  98. <div>
  99. <div class=" flex justify-between dark:text-gray-100 px-5 pt-4 pb-2">
  100. <div class=" text-lg font-medium self-center font-primary">
  101. {#if edit}
  102. {$i18n.t('Edit Connection')}
  103. {:else}
  104. {$i18n.t('Add Connection')}
  105. {/if}
  106. </div>
  107. <button
  108. class="self-center"
  109. on:click={() => {
  110. show = false;
  111. }}
  112. >
  113. <svg
  114. xmlns="http://www.w3.org/2000/svg"
  115. viewBox="0 0 20 20"
  116. fill="currentColor"
  117. class="w-5 h-5"
  118. >
  119. <path
  120. 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"
  121. />
  122. </svg>
  123. </button>
  124. </div>
  125. <div class="flex flex-col md:flex-row w-full px-4 pb-4 md:space-x-4 dark:text-gray-200">
  126. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  127. <form
  128. class="flex flex-col w-full"
  129. on:submit={(e) => {
  130. e.preventDefault();
  131. submitHandler();
  132. }}
  133. >
  134. <div class="px-1">
  135. <div class="flex gap-2">
  136. <div class="flex flex-col w-full">
  137. <div class=" mb-0.5 text-xs text-gray-500">{$i18n.t('URL')}</div>
  138. <div class="flex-1">
  139. <input
  140. class="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none"
  141. type="text"
  142. bind:value={url}
  143. placeholder={$i18n.t('API Base URL')}
  144. autocomplete="off"
  145. required
  146. />
  147. </div>
  148. </div>
  149. <Tooltip content="Verify Connection" className="self-end -mb-1">
  150. <button
  151. class="self-center p-1 bg-transparent hover:bg-gray-100 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
  152. on:click={() => {
  153. verifyHandler();
  154. }}
  155. type="button"
  156. >
  157. <svg
  158. xmlns="http://www.w3.org/2000/svg"
  159. viewBox="0 0 20 20"
  160. fill="currentColor"
  161. class="w-4 h-4"
  162. >
  163. <path
  164. fill-rule="evenodd"
  165. d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H3.989a.75.75 0 00-.75.75v4.242a.75.75 0 001.5 0v-2.43l.31.31a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm1.23-3.723a.75.75 0 00.219-.53V2.929a.75.75 0 00-1.5 0V5.36l-.31-.31A7 7 0 003.239 8.188a.75.75 0 101.448.389A5.5 5.5 0 0113.89 6.11l.311.31h-2.432a.75.75 0 000 1.5h4.243a.75.75 0 00.53-.219z"
  166. clip-rule="evenodd"
  167. />
  168. </svg>
  169. </button>
  170. </Tooltip>
  171. <div class="flex flex-col flex-shrink-0 self-end">
  172. <Tooltip content={enable ? $i18n.t('Enabled') : $i18n.t('Disabled')}>
  173. <Switch bind:state={enable} />
  174. </Tooltip>
  175. </div>
  176. </div>
  177. <div class="flex gap-2 mt-2">
  178. <div class="flex flex-col w-full">
  179. <div class=" mb-0.5 text-xs text-gray-500">{$i18n.t('Key')}</div>
  180. <div class="flex-1">
  181. <SensitiveInput
  182. className="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none"
  183. bind:value={key}
  184. placeholder={$i18n.t('API Key')}
  185. required={!ollama}
  186. />
  187. </div>
  188. </div>
  189. <div class="flex flex-col w-full">
  190. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Prefix ID')}</div>
  191. <div class="flex-1">
  192. <Tooltip
  193. content={$i18n.t(
  194. 'Prefix ID is used to avoid conflicts with other connections by adding a prefix to the model IDs - leave empty to disable'
  195. )}
  196. >
  197. <input
  198. class="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none"
  199. type="text"
  200. bind:value={prefixId}
  201. placeholder={$i18n.t('Prefix ID')}
  202. autocomplete="off"
  203. />
  204. </Tooltip>
  205. </div>
  206. </div>
  207. </div>
  208. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  209. <div class="flex flex-col w-full">
  210. <div class="mb-1 flex justify-between">
  211. <div class="text-xs text-gray-500">{$i18n.t('Model IDs')}</div>
  212. </div>
  213. {#if modelIds.length > 0}
  214. <div class="flex flex-col">
  215. {#each modelIds as modelId, modelIdx}
  216. <div class=" flex gap-2 w-full justify-between items-center">
  217. <div class=" text-sm flex-1 py-1 rounded-lg">
  218. {modelId}
  219. </div>
  220. <div class="flex-shrink-0">
  221. <button
  222. type="button"
  223. on:click={() => {
  224. modelIds = modelIds.filter((_, idx) => idx !== modelIdx);
  225. }}
  226. >
  227. <Minus strokeWidth="2" className="size-3.5" />
  228. </button>
  229. </div>
  230. </div>
  231. {/each}
  232. </div>
  233. {:else}
  234. <div class="text-gray-500 text-xs text-center py-2 px-10">
  235. {#if ollama}
  236. {$i18n.t('Leave empty to include all models from "{{URL}}/api/tags" endpoint', {
  237. URL: url
  238. })}
  239. {:else}
  240. {$i18n.t('Leave empty to include all models from "{{URL}}/models" endpoint', {
  241. URL: url
  242. })}
  243. {/if}
  244. </div>
  245. {/if}
  246. </div>
  247. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  248. <div class="flex items-center">
  249. <input
  250. class="w-full py-1 text-sm rounded-lg bg-transparent {modelId
  251. ? ''
  252. : 'text-gray-500'} placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none"
  253. bind:value={modelId}
  254. placeholder={$i18n.t('Add a model ID')}
  255. />
  256. <div>
  257. <button
  258. type="button"
  259. on:click={() => {
  260. addModelHandler();
  261. }}
  262. >
  263. <Plus className="size-3.5" strokeWidth="2" />
  264. </button>
  265. </div>
  266. </div>
  267. </div>
  268. <div class="flex justify-end pt-3 text-sm font-medium gap-1.5">
  269. {#if edit}
  270. <button
  271. class="px-3.5 py-1.5 text-sm font-medium dark:bg-black dark:hover:bg-gray-900 dark:text-white bg-white text-black hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
  272. type="button"
  273. on:click={() => {
  274. onDelete();
  275. show = false;
  276. }}
  277. >
  278. {$i18n.t('Delete')}
  279. </button>
  280. {/if}
  281. <button
  282. 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
  283. ? ' cursor-not-allowed'
  284. : ''}"
  285. type="submit"
  286. disabled={loading}
  287. >
  288. {$i18n.t('Save')}
  289. {#if loading}
  290. <div class="ml-2 self-center">
  291. <svg
  292. class=" w-4 h-4"
  293. viewBox="0 0 24 24"
  294. fill="currentColor"
  295. xmlns="http://www.w3.org/2000/svg"
  296. ><style>
  297. .spinner_ajPY {
  298. transform-origin: center;
  299. animation: spinner_AtaB 0.75s infinite linear;
  300. }
  301. @keyframes spinner_AtaB {
  302. 100% {
  303. transform: rotate(360deg);
  304. }
  305. }
  306. </style><path
  307. 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"
  308. opacity=".25"
  309. /><path
  310. 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"
  311. class="spinner_ajPY"
  312. /></svg
  313. >
  314. </div>
  315. {/if}
  316. </button>
  317. </div>
  318. </form>
  319. </div>
  320. </div>
  321. </div>
  322. </Modal>